diff options
Diffstat (limited to 'src/passes/RemoveUnusedBrs.cpp')
-rw-r--r-- | src/passes/RemoveUnusedBrs.cpp | 53 |
1 files changed, 26 insertions, 27 deletions
diff --git a/src/passes/RemoveUnusedBrs.cpp b/src/passes/RemoveUnusedBrs.cpp index f80f9ba39..ea8a66612 100644 --- a/src/passes/RemoveUnusedBrs.cpp +++ b/src/passes/RemoveUnusedBrs.cpp @@ -65,7 +65,7 @@ stealSlice(Builder& builder, Block* input, Index from, Index to) { static bool canTurnIfIntoBrIf(Expression* ifCondition, Expression* brValue, PassOptions& options, - FeatureSet features) { + Module& wasm) { // if the if isn't even reached, this is all dead code anyhow if (ifCondition->type == Type::unreachable) { return false; @@ -73,11 +73,11 @@ static bool canTurnIfIntoBrIf(Expression* ifCondition, if (!brValue) { return true; } - EffectAnalyzer value(options, features, brValue); + EffectAnalyzer value(options, wasm, brValue); if (value.hasSideEffects()) { return false; } - return !EffectAnalyzer(options, features, ifCondition).invalidates(value); + return !EffectAnalyzer(options, wasm, ifCondition).invalidates(value); } // Check if it is not worth it to run code unconditionally. This @@ -329,13 +329,12 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs>> { } void visitIf(If* curr) { - FeatureSet features = getModule()->features; if (!curr->ifFalse) { // if without an else. try to reduce // if (condition) br => br_if (condition) if (Break* br = curr->ifTrue->dynCast<Break>()) { if (canTurnIfIntoBrIf( - curr->condition, br->value, getPassOptions(), features)) { + curr->condition, br->value, getPassOptions(), *getModule())) { if (!br->condition) { br->condition = curr->condition; } else { @@ -360,7 +359,7 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs>> { } // Of course we can't do this if the br's condition has side // effects, as we would then execute those unconditionally. - if (EffectAnalyzer(getPassOptions(), features, br->condition) + if (EffectAnalyzer(getPassOptions(), *getModule(), br->condition) .hasSideEffects()) { return; } @@ -555,7 +554,7 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs>> { return false; } // if there is control flow, we must stop looking - if (EffectAnalyzer(getPassOptions(), getModule()->features, curr) + if (EffectAnalyzer(getPassOptions(), *getModule(), curr) .transfersControlFlow()) { return false; } @@ -851,7 +850,6 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs>> { // the if is dead // * note that we do this at the end, because un-conditionalizing can // interfere with optimizeLoop()ing. - FeatureSet features = getModule()->features; auto& list = curr->list; for (Index i = 0; i < list.size(); i++) { auto* iff = list[i]->dynCast<If>(); @@ -862,8 +860,10 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs>> { } auto* ifTrueBreak = iff->ifTrue->dynCast<Break>(); if (ifTrueBreak && !ifTrueBreak->condition && - canTurnIfIntoBrIf( - iff->condition, ifTrueBreak->value, passOptions, features)) { + canTurnIfIntoBrIf(iff->condition, + ifTrueBreak->value, + passOptions, + *getModule())) { // we are an if-else where the ifTrue is a break without a // condition, so we can do this ifTrueBreak->condition = iff->condition; @@ -875,8 +875,10 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs>> { // otherwise, perhaps we can flip the if auto* ifFalseBreak = iff->ifFalse->dynCast<Break>(); if (ifFalseBreak && !ifFalseBreak->condition && - canTurnIfIntoBrIf( - iff->condition, ifFalseBreak->value, passOptions, features)) { + canTurnIfIntoBrIf(iff->condition, + ifFalseBreak->value, + passOptions, + *getModule())) { ifFalseBreak->condition = Builder(*getModule()).makeUnary(EqZInt32, iff->condition); ifFalseBreak->finalize(); @@ -905,7 +907,7 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs>> { if (shrink && br2->type != Type::unreachable) { // Join adjacent br_ifs to the same target, making one br_if // with a "selectified" condition that executes both. - if (!EffectAnalyzer(passOptions, features, br2->condition) + if (!EffectAnalyzer(passOptions, *getModule(), br2->condition) .hasSideEffects()) { // it's ok to execute them both, do it Builder builder(*getModule()); @@ -933,10 +935,9 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs>> { // This switch has just one target no matter what; replace with a br // if we can (to do so, we must put the condition before a possible // value). - if (!curr->value || EffectAnalyzer::canReorder(passOptions, - getModule()->features, - curr->condition, - curr->value)) { + if (!curr->value || + EffectAnalyzer::canReorder( + passOptions, *getModule(), curr->condition, curr->value)) { Builder builder(*getModule()); replaceCurrent(builder.makeSequence( builder.makeDrop(curr->condition), // might have side effects @@ -1002,12 +1003,11 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs>> { } else { // To use an if, the value must have no side effects, as in the // if it may not execute. - FeatureSet features = getModule()->features; - if (!EffectAnalyzer(passOptions, features, br->value) + if (!EffectAnalyzer(passOptions, *getModule(), br->value) .hasSideEffects()) { // We also need to reorder the condition and the value. if (EffectAnalyzer::canReorder( - passOptions, features, br->condition, br->value)) { + passOptions, *getModule(), br->condition, br->value)) { ExpressionManipulator::nop(list[0]); replaceCurrent( builder.makeIf(br->condition, br->value, curr)); @@ -1045,9 +1045,9 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs>> { // after ignoring the br_if. list[0] = &nop; auto canReorder = EffectAnalyzer::canReorder( - passOptions, features, br->condition, curr); + passOptions, *getModule(), br->condition, curr); auto hasSideEffects = - EffectAnalyzer(passOptions, features, curr) + EffectAnalyzer(passOptions, *getModule(), curr) .hasSideEffects(); list[0] = old; if (canReorder && !hasSideEffects && @@ -1099,16 +1099,15 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs>> { } // Check if side effects allow this: we need to execute the two arms // unconditionally, and also to make the condition run last. - FeatureSet features = getModule()->features; - EffectAnalyzer ifTrue(passOptions, features, iff->ifTrue); + EffectAnalyzer ifTrue(passOptions, *getModule(), iff->ifTrue); if (ifTrue.hasSideEffects()) { return nullptr; } - EffectAnalyzer ifFalse(passOptions, features, iff->ifFalse); + EffectAnalyzer ifFalse(passOptions, *getModule(), iff->ifFalse); if (ifFalse.hasSideEffects()) { return nullptr; } - EffectAnalyzer condition(passOptions, features, iff->condition); + EffectAnalyzer condition(passOptions, *getModule(), iff->condition); if (condition.invalidates(ifTrue) || condition.invalidates(ifFalse)) { return nullptr; } @@ -1363,7 +1362,7 @@ struct RemoveUnusedBrs : public WalkerPass<PostWalker<RemoveUnusedBrs>> { } // if the condition has side effects, we can't replace many // appearances of it with a single one - if (EffectAnalyzer(passOptions, getModule()->features, conditionValue) + if (EffectAnalyzer(passOptions, *getModule(), conditionValue) .hasSideEffects()) { start++; continue; |