summaryrefslogtreecommitdiff
path: root/src/passes/CodePushing.cpp
diff options
context:
space:
mode:
authorAlon Zakai <azakai@google.com>2022-09-13 15:10:00 -0700
committerGitHub <noreply@github.com>2022-09-13 22:10:00 +0000
commit795a70b02032f57f14d97d7555af97f0e527b4b0 (patch)
treeec3f0625ddfef2b831b1801990a39eafb912d328 /src/passes/CodePushing.cpp
parentf1a3e682e864fcb827a01d3c5725d847053fe149 (diff)
downloadbinaryen-795a70b02032f57f14d97d7555af97f0e527b4b0.tar.gz
binaryen-795a70b02032f57f14d97d7555af97f0e527b4b0.tar.bz2
binaryen-795a70b02032f57f14d97d7555af97f0e527b4b0.zip
[Exceptions] Optimize in CodePushing even with exceptions thrown (#5028)
We had some concerns about this not working in the past, but thinking about it now, I believe it is safe to do. Specifically, a throw is either like a break or a return - either it jumps out to an outer scope (like a break) or it jumps out of the function (like a return), and both breaks and returns have already been handled here. This change has some nice effects on J2Wasm output, where there are quite a lot of throws, which we can now optimize around.
Diffstat (limited to 'src/passes/CodePushing.cpp')
-rw-r--r--src/passes/CodePushing.cpp20
1 files changed, 15 insertions, 5 deletions
diff --git a/src/passes/CodePushing.cpp b/src/passes/CodePushing.cpp
index 682f025c8..5026773d4 100644
--- a/src/passes/CodePushing.cpp
+++ b/src/passes/CodePushing.cpp
@@ -161,11 +161,21 @@ private:
// everything that matters if you want to be pushed past the pushPoint
EffectAnalyzer cumulativeEffects(passOptions, module);
cumulativeEffects.walk(list[pushPoint]);
- // it is ok to ignore the branching here, that is the crucial point of this
- // opt
- // TODO: it would be ok to ignore thrown exceptions here, if we know they
- // could not be caught and must go outside of the function
- cumulativeEffects.ignoreBranches();
+ // It is ok to ignore branching out of the block here, that is the crucial
+ // point of this optimization. That is, we are in a situation like this:
+ //
+ // {
+ // x = value;
+ // if (..) break;
+ // foo(x);
+ // }
+ //
+ // If the branch is taken, then that's fine, it will jump out of this block
+ // and reach some outer scope, and in that case we never need x at all
+ // (since we've proven before that x is not used outside of this block, see
+ // numGetsSoFar which we use for that). Similarly, control flow could
+ // transfer away via a return or an exception and that would be ok as well.
+ cumulativeEffects.ignoreControlFlowTransfers();
std::vector<LocalSet*> toPush;
Index i = pushPoint - 1;
while (1) {