diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/passes/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/passes/Precompute.cpp | 103 | ||||
-rw-r--r-- | src/passes/pass.cpp | 2 | ||||
-rw-r--r-- | src/wasm-builder.h | 1 | ||||
-rw-r--r-- | src/wasm-interpreter.h | 38 | ||||
-rw-r--r-- | src/wasm.cpp | 3 |
6 files changed, 111 insertions, 37 deletions
diff --git a/src/passes/CMakeLists.txt b/src/passes/CMakeLists.txt index 9e69cbae2..2ccf5f040 100644 --- a/src/passes/CMakeLists.txt +++ b/src/passes/CMakeLists.txt @@ -11,6 +11,7 @@ SET(passes_SOURCES NameList.cpp OptimizeInstructions.cpp PostEmscripten.cpp + Precompute.cpp Print.cpp RemoveImports.cpp RemoveMemory.cpp diff --git a/src/passes/Precompute.cpp b/src/passes/Precompute.cpp new file mode 100644 index 000000000..db6999cbd --- /dev/null +++ b/src/passes/Precompute.cpp @@ -0,0 +1,103 @@ +/* + * Copyright 2016 WebAssembly Community Group participants + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +// +// 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. +// + +#include <wasm.h> +#include <pass.h> +#include <wasm-builder.h> +#include <wasm-interpreter.h> + +namespace wasm { + +// Execute an expression by itself. Errors if we hit anything we need anything not in the expression itself standalone. +class StandaloneExpressionRunner : public ExpressionRunner<StandaloneExpressionRunner> { +public: + struct NonstandaloneException {}; // TODO: use a flow with a special name, as this is likely very slow + + Flow visitLoop(Loop* curr) { + // loops might be infinite, so must be careful + // but we can't tell if non-infinite, since we don't have state, so loops are just impossible to optimize for now + throw NonstandaloneException(); + } + + Flow visitCall(Call* curr) { + throw NonstandaloneException(); + } + Flow visitCallImport(CallImport* curr) { + throw NonstandaloneException(); + } + Flow visitCallIndirect(CallIndirect* curr) { + throw NonstandaloneException(); + } + Flow visitGetLocal(GetLocal *curr) { + throw NonstandaloneException(); + } + Flow visitSetLocal(SetLocal *curr) { + throw NonstandaloneException(); + } + Flow visitLoad(Load *curr) { + throw NonstandaloneException(); + } + Flow visitStore(Store *curr) { + throw NonstandaloneException(); + } + Flow visitHost(Host *curr) { + throw NonstandaloneException(); + } + + void trap(const char* why) override { + throw NonstandaloneException(); + } +}; + +struct Precompute : public WalkerPass<PostWalker<Precompute, UnifiedExpressionVisitor<Precompute>>> { + bool isFunctionParallel() override { return true; } + + Pass* create() override { return new Precompute; } + + void visitExpression(Expression* curr) { + if (curr->is<Const>()) return; + // try to evaluate this into a const + Flow flow; + try { + flow = StandaloneExpressionRunner().visit(curr); + } catch (StandaloneExpressionRunner::NonstandaloneException& e) { + return; + } + if (flow.breaking()) return; // TODO: can create a break as a replacement + if (isConcreteWasmType(flow.value.type)) { + replaceCurrent(Builder(*getModule()).makeConst(flow.value)); + } + } +}; + +static RegisterPass<Precompute> registerPass("precompute", "computes compile-time evaluatable expressions"); + +} // namespace wasm + diff --git a/src/passes/pass.cpp b/src/passes/pass.cpp index babb4635b..c5315a044 100644 --- a/src/passes/pass.cpp +++ b/src/passes/pass.cpp @@ -63,6 +63,7 @@ void PassRunner::addDefaultOptimizationPasses() { add("remove-unused-brs"); add("remove-unused-names"); add("optimize-instructions"); + add("precompute"); add("simplify-locals"); add("vacuum"); // previous pass creates garbage add("remove-unused-brs"); // simplify-locals opens opportunities for phi optimizations @@ -80,6 +81,7 @@ void PassRunner::addDefaultFunctionOptimizationPasses() { add("remove-unused-brs"); add("remove-unused-names"); add("optimize-instructions"); + add("precompute"); add("simplify-locals"); add("vacuum"); // previous pass creates garbage add("remove-unused-brs"); // simplify-locals opens opportunities for phi optimizations diff --git a/src/wasm-builder.h b/src/wasm-builder.h index ae8d5fa4c..2eb051251 100644 --- a/src/wasm-builder.h +++ b/src/wasm-builder.h @@ -134,6 +134,7 @@ public: return ret; } Const* makeConst(Literal value) { + assert(isConcreteWasmType(value.type)); auto* ret = wasm.allocator.alloc<Const>(); ret->value = value; ret->type = value.type; diff --git a/src/wasm-interpreter.h b/src/wasm-interpreter.h index 55edbdfa0..f7e213b93 100644 --- a/src/wasm-interpreter.h +++ b/src/wasm-interpreter.h @@ -39,8 +39,7 @@ using namespace cashew; // Utilities -IString WASM("wasm"), - RETURN_FLOW("*return:)*"); +extern Name WASM, RETURN_FLOW; enum { maxCallDepth = 250 @@ -492,41 +491,6 @@ public: } }; -// Execute an expression by itself. Errors if we hit anything we need anything not in the expression itself standalone. -class StandaloneExpressionRunner : public ExpressionRunner<StandaloneExpressionRunner> { -public: - struct NonstandaloneException {}; - - Flow visitCall(Call* curr) { - throw NonstandaloneException(); - } - Flow visitCallImport(CallImport* curr) { - throw NonstandaloneException(); - } - Flow visitCallIndirect(CallIndirect* curr) { - throw NonstandaloneException(); - } - Flow visitGetLocal(GetLocal *curr) { - throw NonstandaloneException(); - } - Flow visitSetLocal(SetLocal *curr) { - throw NonstandaloneException(); - } - Flow visitLoad(Load *curr) { - throw NonstandaloneException(); - } - Flow visitStore(Store *curr) { - throw NonstandaloneException(); - } - Flow visitHost(Host *curr) { - throw NonstandaloneException(); - } - - void trap(const char* why) { - throw NonstandaloneException(); - } -}; - // // An instance of a WebAssembly module, which can execute it via AST interpretation. // diff --git a/src/wasm.cpp b/src/wasm.cpp index 48a0f276a..dd06a8d14 100644 --- a/src/wasm.cpp +++ b/src/wasm.cpp @@ -20,6 +20,9 @@ namespace wasm { +Name WASM("wasm"), + RETURN_FLOW("*return:)*"); + struct TypeSeeker : public PostWalker<TypeSeeker, Visitor<TypeSeeker>> { Expression* target; // look for this one Name targetName; |