summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2017-09-05 11:33:22 -0700
committerAlon Zakai <alonzakai@gmail.com>2017-09-06 09:36:24 -0700
commit68b57f6c015af1eaf95b039b0bc2bc3b9efbab4e (patch)
treeeedfada9fbc282d91bc46a5f9dd6ffc7ee1ac4c5 /src
parent3d1c3a3a342d4d22dcda4e45e4f4aae93b464ec8 (diff)
downloadbinaryen-68b57f6c015af1eaf95b039b0bc2bc3b9efbab4e.tar.gz
binaryen-68b57f6c015af1eaf95b039b0bc2bc3b9efbab4e.tar.bz2
binaryen-68b57f6c015af1eaf95b039b0bc2bc3b9efbab4e.zip
when if arms are identical, merging them may change the type of the if, if it has a forced type
Diffstat (limited to 'src')
-rw-r--r--src/passes/OptimizeInstructions.cpp29
1 files changed, 23 insertions, 6 deletions
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp
index acbf39447..0b47d3398 100644
--- a/src/passes/OptimizeInstructions.cpp
+++ b/src/passes/OptimizeInstructions.cpp
@@ -620,16 +620,33 @@ struct OptimizeInstructions : public WalkerPass<PostWalker<OptimizeInstructions,
std::swap(iff->ifTrue, iff->ifFalse);
}
}
- if (ExpressionAnalyzer::equal(iff->ifTrue, iff->ifFalse)) {
+ if (iff->condition->type != unreachable && ExpressionAnalyzer::equal(iff->ifTrue, iff->ifFalse)) {
// sides are identical, fold
- if (!EffectAnalyzer(getPassOptions(), iff->condition).hasSideEffects()) {
+ // if we can replace the if with one arm, and no side effects in the condition, do that
+ auto needCondition = EffectAnalyzer(getPassOptions(), iff->condition).hasSideEffects();
+ auto typeIsIdentical = iff->ifTrue->type == iff->type;
+ if (typeIsIdentical && !needCondition) {
return iff->ifTrue;
} else {
Builder builder(*getModule());
- return builder.makeSequence(
- builder.makeDrop(iff->condition),
- iff->ifTrue
- );
+ if (typeIsIdentical) {
+ return builder.makeSequence(
+ builder.makeDrop(iff->condition),
+ iff->ifTrue
+ );
+ } else {
+ // the types diff. as the condition is reachable, that means the if must be
+ // concrete while the arm is not
+ assert(isConcreteWasmType(iff->type) && iff->ifTrue->type == unreachable);
+ // emit a block with a forced type
+ auto* ret = builder.makeBlock();
+ if (needCondition) {
+ ret->list.push_back(builder.makeDrop(iff->condition));
+ }
+ ret->list.push_back(iff->ifTrue);
+ ret->finalize(iff->type);
+ return ret;
+ }
}
}
}