From 795a70b02032f57f14d97d7555af97f0e527b4b0 Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Tue, 13 Sep 2022 15:10:00 -0700 Subject: [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. --- src/passes/CodePushing.cpp | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'src/passes/CodePushing.cpp') 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 toPush; Index i = pushPoint - 1; while (1) { -- cgit v1.2.3