summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/passes/Precompute.cpp18
-rw-r--r--src/passes/Vacuum.cpp15
2 files changed, 21 insertions, 12 deletions
diff --git a/src/passes/Precompute.cpp b/src/passes/Precompute.cpp
index a341ba938..008058d58 100644
--- a/src/passes/Precompute.cpp
+++ b/src/passes/Precompute.cpp
@@ -15,23 +15,14 @@
*/
//
-// Removes dead, i.e. unreachable, code.
-//
-// We keep a record of when control flow is reachable. When it isn't, we
-// kill (turn into unreachable). We then fold away entire unreachable
-// expressions.
-//
-// When dead code causes an operation to not happen, like a store, a call
-// or an add, we replace with a block with a list of what does happen.
-// That isn't necessarily smaller, but blocks are friendlier to other
-// optimizations: blocks can be merged and eliminated, and they clearly
-// have no side effects.
+// Computes code at compile time where possible.
//
#include <wasm.h>
#include <pass.h>
#include <wasm-builder.h>
#include <wasm-interpreter.h>
+#include <ast_utils.h>
namespace wasm {
@@ -90,7 +81,7 @@ struct Precompute : public WalkerPass<PostWalker<Precompute, UnifiedExpressionVi
Pass* create() override { return new Precompute; }
void visitExpression(Expression* curr) {
- if (curr->is<Const>()) return;
+ if (curr->is<Const>() || curr->is<Nop>()) return;
// try to evaluate this into a const
Flow flow;
try {
@@ -99,8 +90,11 @@ struct Precompute : public WalkerPass<PostWalker<Precompute, UnifiedExpressionVi
return;
}
if (flow.breaking()) return; // TODO: can create a break as a replacement in some cases (not NONSTANDALONE)
+ // this was precomputed
if (isConcreteWasmType(flow.value.type)) {
replaceCurrent(Builder(*getModule()).makeConst(flow.value));
+ } else {
+ ExpressionManipulator::nop(curr);
}
}
};
diff --git a/src/passes/Vacuum.cpp b/src/passes/Vacuum.cpp
index 03becb04c..c3fed2328 100644
--- a/src/passes/Vacuum.cpp
+++ b/src/passes/Vacuum.cpp
@@ -186,6 +186,21 @@ struct Vacuum : public WalkerPass<ExpressionStackWalker<Vacuum, Visitor<Vacuum>>
}
void visitIf(If* curr) {
+ // if the condition is a constant, just apply it
+ // we can just return the ifTrue or ifFalse.
+ if (auto* value = curr->condition->dynCast<Const>()) {
+ if (value->value.getInteger()) {
+ replaceCurrent(curr->ifTrue);
+ return;
+ } else {
+ if (curr->ifFalse) {
+ replaceCurrent(curr->ifFalse);
+ } else {
+ ExpressionManipulator::nop(curr);
+ }
+ return;
+ }
+ }
if (curr->ifFalse) {
if (curr->ifFalse->is<Nop>()) {
curr->ifFalse = nullptr;