diff options
author | Max Graey <maxgraey@gmail.com> | 2020-11-04 00:14:21 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-03 14:14:21 -0800 |
commit | 97b1674067fd380571cdce8bf2200532de2dbc42 (patch) | |
tree | 93f1f9b0b140a99241e6eb039313418dc017a83e | |
parent | 081019104a09b5201fa1307a4758c19ca392c2d8 (diff) | |
download | binaryen-97b1674067fd380571cdce8bf2200532de2dbc42.tar.gz binaryen-97b1674067fd380571cdce8bf2200532de2dbc42.tar.bz2 binaryen-97b1674067fd380571cdce8bf2200532de2dbc42.zip |
Slight refactoring of handOptimize (#3305)
Move the checks for most unoptimizable expression types out into visitExpression and simplify some other code.
-rw-r--r-- | src/passes/OptimizeInstructions.cpp | 30 |
1 files changed, 15 insertions, 15 deletions
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp index 9b47f6a0f..ecd30d711 100644 --- a/src/passes/OptimizeInstructions.cpp +++ b/src/passes/OptimizeInstructions.cpp @@ -170,16 +170,19 @@ struct OptimizeInstructions } void visitExpression(Expression* curr) { + // if this contains dead code, don't bother trying to optimize it, the type + // might change (if might not be unreachable if just one arm is, for + // example). this optimization pass focuses on actually executing code. the + // only exceptions are control flow changes + if (curr->is<Const>() || curr->is<Call>() || curr->is<Nop>() || + (curr->type == Type::unreachable && !curr->is<Break>() && + !curr->is<Switch>() && !curr->is<If>())) { + return; + } // we may be able to apply multiple patterns, one may open opportunities // that look deeper NB: patterns must not have cycles - while (1) { - auto* handOptimized = handOptimize(curr); - if (handOptimized) { - curr = handOptimized; - replaceCurrent(curr); - continue; - } - break; + while ((curr = handOptimize(curr))) { + replaceCurrent(curr); } } @@ -200,15 +203,12 @@ struct OptimizeInstructions // Optimizations that don't yet fit in the pattern DSL, but could be // eventually maybe Expression* handOptimize(Expression* curr) { - FeatureSet features = getModule()->features; - // if this contains dead code, don't bother trying to optimize it, the type - // might change (if might not be unreachable if just one arm is, for - // example). this optimization pass focuses on actually executing code. the - // only exceptions are control flow changes - if (curr->type == Type::unreachable && !curr->is<Break>() && - !curr->is<Switch>() && !curr->is<If>()) { + if (curr->is<Const>()) { return nullptr; } + + FeatureSet features = getModule()->features; + if (auto* binary = curr->dynCast<Binary>()) { if (isSymmetricOrRelational(binary)) { canonicalize(binary); |