summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2018-04-27 14:51:03 -0700
committerGitHub <noreply@github.com>2018-04-27 14:51:03 -0700
commitbe12baa0384c06e26effaff2ea45870c3030b589 (patch)
treedcb04a31fbedc91a43f6c732719ff3f74e87eede /src
parenta645da8fd22cea7789f07d4e0b88fdf38f9f5035 (diff)
downloadbinaryen-be12baa0384c06e26effaff2ea45870c3030b589.tar.gz
binaryen-be12baa0384c06e26effaff2ea45870c3030b589.tar.bz2
binaryen-be12baa0384c06e26effaff2ea45870c3030b589.zip
optimize selects of constant conditions (#1516)
Diffstat (limited to 'src')
-rw-r--r--src/passes/OptimizeInstructions.cpp20
1 files changed, 20 insertions, 0 deletions
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp
index 293adcbde..6a66ae603 100644
--- a/src/passes/OptimizeInstructions.cpp
+++ b/src/passes/OptimizeInstructions.cpp
@@ -756,6 +756,26 @@ struct OptimizeInstructions : public WalkerPass<PostWalker<OptimizeInstructions,
std::swap(select->ifTrue, select->ifFalse);
}
}
+ if (auto* c = select->condition->dynCast<Const>()) {
+ // constant condition, we can just pick the right side (barring side effects)
+ if (c->value.getInteger()) {
+ if (!EffectAnalyzer(getPassOptions(), select->ifFalse).hasSideEffects()) {
+ return select->ifTrue;
+ } else {
+ // don't bother - we would need to reverse the order using a temp local, which is bad
+ }
+ } else {
+ if (!EffectAnalyzer(getPassOptions(), select->ifTrue).hasSideEffects()) {
+ return select->ifFalse;
+ } else {
+ Builder builder(*getModule());
+ return builder.makeSequence(
+ builder.makeDrop(select->ifTrue),
+ select->ifFalse
+ );
+ }
+ }
+ }
if (ExpressionAnalyzer::equal(select->ifTrue, select->ifFalse)) {
// sides are identical, fold
EffectAnalyzer value(getPassOptions(), select->ifTrue);