summaryrefslogtreecommitdiff
path: root/src/passes
diff options
context:
space:
mode:
Diffstat (limited to 'src/passes')
-rw-r--r--src/passes/MergeBlocks.cpp34
-rw-r--r--src/passes/OptimizeInstructions.cpp46
2 files changed, 55 insertions, 25 deletions
diff --git a/src/passes/MergeBlocks.cpp b/src/passes/MergeBlocks.cpp
index d32948bee..7c086a920 100644
--- a/src/passes/MergeBlocks.cpp
+++ b/src/passes/MergeBlocks.cpp
@@ -73,14 +73,23 @@ namespace wasm {
// For example, if there is a switch targeting us, we can't do it - we can't remove the value from other targets
struct ProblemFinder : public ControlFlowWalker<ProblemFinder> {
Name origin;
- bool foundSwitch = false;
+ bool foundProblem = false;
// count br_ifs, and dropped br_ifs. if they don't match, then a br_if flow value is used, and we can't drop it
Index brIfs = 0;
Index droppedBrIfs = 0;
+ PassOptions& passOptions;
+
+ ProblemFinder(PassOptions& passOptions) : passOptions(passOptions) {}
void visitBreak(Break* curr) {
- if (curr->name == origin && curr->condition) {
- brIfs++;
+ if (curr->name == origin) {
+ if (curr->condition) {
+ brIfs++;
+ }
+ // if the value has side effects, we can't remove it
+ if (EffectAnalyzer(passOptions, curr->value).hasSideEffects()) {
+ foundProblem = true;
+ }
}
}
@@ -94,12 +103,12 @@ struct ProblemFinder : public ControlFlowWalker<ProblemFinder> {
void visitSwitch(Switch* curr) {
if (curr->default_ == origin) {
- foundSwitch = true;
+ foundProblem = true;
return;
}
for (auto& target : curr->targets) {
if (target == origin) {
- foundSwitch = true;
+ foundProblem = true;
return;
}
}
@@ -107,7 +116,7 @@ struct ProblemFinder : public ControlFlowWalker<ProblemFinder> {
bool found() {
assert(brIfs >= droppedBrIfs);
- return foundSwitch || brIfs > droppedBrIfs;
+ return foundProblem || brIfs > droppedBrIfs;
}
};
@@ -115,6 +124,9 @@ struct ProblemFinder : public ControlFlowWalker<ProblemFinder> {
// While doing so it can create new blocks, so optimize blocks as well.
struct BreakValueDropper : public ControlFlowWalker<BreakValueDropper> {
Name origin;
+ PassOptions& passOptions;
+
+ BreakValueDropper(PassOptions& passOptions) : passOptions(passOptions) {}
void visitBlock(Block* curr);
@@ -143,7 +155,7 @@ struct BreakValueDropper : public ControlFlowWalker<BreakValueDropper> {
};
// core block optimizer routine
-static void optimizeBlock(Block* curr, Module* module) {
+static void optimizeBlock(Block* curr, Module* module, PassOptions& passOptions) {
bool more = true;
bool changed = false;
while (more) {
@@ -159,14 +171,14 @@ static void optimizeBlock(Block* curr, Module* module) {
if (child->name.is()) {
Expression* expression = child;
// check if it's ok to remove the value from all breaks to us
- ProblemFinder finder;
+ ProblemFinder finder(passOptions);
finder.origin = child->name;
finder.walk(expression);
if (finder.found()) {
child = nullptr;
} else {
// fix up breaks
- BreakValueDropper fixer;
+ BreakValueDropper fixer(passOptions);
fixer.origin = child->name;
fixer.setModule(module);
fixer.walk(expression);
@@ -217,7 +229,7 @@ static void optimizeBlock(Block* curr, Module* module) {
}
void BreakValueDropper::visitBlock(Block* curr) {
- optimizeBlock(curr, getModule());
+ optimizeBlock(curr, getModule(), passOptions);
}
struct MergeBlocks : public WalkerPass<PostWalker<MergeBlocks>> {
@@ -226,7 +238,7 @@ struct MergeBlocks : public WalkerPass<PostWalker<MergeBlocks>> {
Pass* create() override { return new MergeBlocks; }
void visitBlock(Block *curr) {
- optimizeBlock(curr, getModule());
+ optimizeBlock(curr, getModule(), getPassOptions());
}
Block* optimize(Expression* curr, Expression*& child, Block* outer = nullptr, Expression** dependency1 = nullptr, Expression** dependency2 = nullptr) {
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp
index de6f96ccc..4fd7cbe8d 100644
--- a/src/passes/OptimizeInstructions.cpp
+++ b/src/passes/OptimizeInstructions.cpp
@@ -29,6 +29,7 @@
#include <ast/effects.h>
#include <ast/manipulation.h>
#include <ast/properties.h>
+#include <ast/literal-utils.h>
namespace wasm {
@@ -188,14 +189,14 @@ Index getMaxBits(Expression* curr, LocalInfoProvider* localInfoProvider) {
case OrInt32: case XorInt32: return std::max(getMaxBits(binary->left, localInfoProvider), getMaxBits(binary->right, localInfoProvider));
case ShlInt32: {
if (auto* shifts = binary->right->dynCast<Const>()) {
- return std::min(Index(32), getMaxBits(binary->left, localInfoProvider) + shifts->value.geti32());
+ return std::min(Index(32), getMaxBits(binary->left, localInfoProvider) + Bits::getEffectiveShifts(shifts));
}
return 32;
}
case ShrUInt32: {
if (auto* shift = binary->right->dynCast<Const>()) {
auto maxBits = getMaxBits(binary->left, localInfoProvider);
- auto shifts = std::min(Index(shift->value.geti32()), maxBits); // can ignore more shifts than zero us out
+ auto shifts = std::min(Index(Bits::getEffectiveShifts(shift)), maxBits); // can ignore more shifts than zero us out
return std::max(Index(0), maxBits - shifts);
}
return 32;
@@ -204,7 +205,7 @@ Index getMaxBits(Expression* curr, LocalInfoProvider* localInfoProvider) {
if (auto* shift = binary->right->dynCast<Const>()) {
auto maxBits = getMaxBits(binary->left, localInfoProvider);
if (maxBits == 32) return 32;
- auto shifts = std::min(Index(shift->value.geti32()), maxBits); // can ignore more shifts than zero us out
+ auto shifts = std::min(Index(Bits::getEffectiveShifts(shift)), maxBits); // can ignore more shifts than zero us out
return std::max(Index(0), maxBits - shifts);
}
return 32;
@@ -533,9 +534,16 @@ struct OptimizeInstructions : public WalkerPass<PostWalker<OptimizeInstructions,
} else if (left->op == OrInt32) {
leftRight->value = leftRight->value.or_(right->value);
return left;
- } else if (left->op == ShlInt32 || left->op == ShrUInt32 || left->op == ShrSInt32) {
- leftRight->value = leftRight->value.add(right->value);
- return left;
+ } else if (left->op == ShlInt32 || left->op == ShrUInt32 || left->op == ShrSInt32 ||
+ left->op == ShlInt64 || left->op == ShrUInt64 || left->op == ShrSInt64) {
+ // shifts only use an effective amount from the constant, so adding must
+ // be done carefully
+ auto total = Bits::getEffectiveShifts(leftRight) + Bits::getEffectiveShifts(right);
+ if (total == Bits::getEffectiveShifts(total, right->type)) {
+ // no overflow, we can do this
+ leftRight->value = LiteralUtils::makeLiteralFromInt32(total, right->type);
+ return left;
+ } // TODO: handle overflows
}
}
}
@@ -874,14 +882,17 @@ private:
auto* left = binary->left;
auto* right = binary->right;
if (!Properties::emitsBoolean(left) || !Properties::emitsBoolean(right)) return nullptr;
- auto leftEffects = EffectAnalyzer(getPassOptions(), left).hasSideEffects();
- auto rightEffects = EffectAnalyzer(getPassOptions(), right).hasSideEffects();
- if (leftEffects && rightEffects) return nullptr; // both must execute
- // canonicalize with side effects, if any, happening on the left
- if (rightEffects) {
+ auto leftEffects = EffectAnalyzer(getPassOptions(), left);
+ auto rightEffects = EffectAnalyzer(getPassOptions(), right);
+ auto leftHasSideEffects = leftEffects.hasSideEffects();
+ auto rightHasSideEffects = rightEffects.hasSideEffects();
+ if (leftHasSideEffects && rightHasSideEffects) return nullptr; // both must execute
+ // canonicalize with side effects, if any, happening on the left
+ if (rightHasSideEffects) {
if (CostAnalyzer(left).cost < MIN_COST) return nullptr; // avoidable code is too cheap
+ if (leftEffects.invalidates(rightEffects)) return nullptr; // cannot reorder
std::swap(left, right);
- } else if (leftEffects) {
+ } else if (leftHasSideEffects) {
if (CostAnalyzer(right).cost < MIN_COST) return nullptr; // avoidable code is too cheap
} else {
// no side effects, reorder based on cost estimation
@@ -908,8 +919,15 @@ private:
// it's better to do the opposite for gzip purposes as well as for readability.
auto* last = ptr->dynCast<Const>();
if (last) {
- last->value = Literal(int32_t(last->value.geti32() + offset));
- offset = 0;
+ // don't do this if it would wrap the pointer
+ uint64_t value64 = last->value.geti32();
+ uint64_t offset64 = offset;
+ if (value64 <= std::numeric_limits<int32_t>::max() &&
+ offset64 <= std::numeric_limits<int32_t>::max() &&
+ value64 + offset64 <= std::numeric_limits<int32_t>::max()) {
+ last->value = Literal(int32_t(value64 + offset64));
+ offset = 0;
+ }
}
}