summaryrefslogtreecommitdiff
path: root/src/passes
diff options
context:
space:
mode:
Diffstat (limited to 'src/passes')
-rw-r--r--src/passes/CMakeLists.txt1
-rw-r--r--src/passes/Intrinsics.cpp50
-rw-r--r--src/passes/pass.cpp3
-rw-r--r--src/passes/passes.h1
4 files changed, 55 insertions, 0 deletions
diff --git a/src/passes/CMakeLists.txt b/src/passes/CMakeLists.txt
index b61b895ef..a49ae8980 100644
--- a/src/passes/CMakeLists.txt
+++ b/src/passes/CMakeLists.txt
@@ -35,6 +35,7 @@ set(passes_SOURCES
Inlining.cpp
InstrumentLocals.cpp
InstrumentMemory.cpp
+ Intrinsics.cpp
LegalizeJSInterface.cpp
LimitSegments.cpp
LocalCSE.cpp
diff --git a/src/passes/Intrinsics.cpp b/src/passes/Intrinsics.cpp
new file mode 100644
index 000000000..1b5a50bbb
--- /dev/null
+++ b/src/passes/Intrinsics.cpp
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2021 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.
+ */
+
+#include "ir/intrinsics.h"
+#include "pass.h"
+#include "wasm-builder.h"
+#include "wasm.h"
+
+namespace wasm {
+
+struct IntrinsicLowering : public WalkerPass<PostWalker<IntrinsicLowering>> {
+ bool isFunctionParallel() override { return true; }
+
+ Pass* create() override { return new IntrinsicLowering; }
+
+ void visitCall(Call* curr) {
+ if (Intrinsics(*getModule()).isCallWithoutEffects(curr)) {
+ // Turn into a call, by using the final operand as the function to call.
+ auto& operands = curr->operands;
+ auto* target = operands.back();
+ operands.pop_back();
+ // We could rely on later optimizations here, but at least ensure we emit
+ // a direct call when we can, to avoid a performance cliff if the user
+ // forgets to optimize.
+ Builder builder(*getModule());
+ if (auto* refFunc = target->dynCast<RefFunc>()) {
+ replaceCurrent(builder.makeCall(refFunc->func, operands, curr->type));
+ } else {
+ replaceCurrent(builder.makeCallRef(target, operands, curr->type));
+ }
+ }
+ }
+};
+
+Pass* createIntrinsicLoweringPass() { return new IntrinsicLowering(); }
+
+} // namespace wasm
diff --git a/src/passes/pass.cpp b/src/passes/pass.cpp
index 878ecb3cb..5f53a1d78 100644
--- a/src/passes/pass.cpp
+++ b/src/passes/pass.cpp
@@ -164,6 +164,9 @@ void PassRegistry::registerPasses() {
registerPass("inlining-optimizing",
"inline functions and optimizes where we inlined",
createInliningOptimizingPass);
+ registerPass("intrinsic-lowering",
+ "lower away binaryen intrinsics",
+ createIntrinsicLoweringPass);
registerPass("legalize-js-interface",
"legalizes i64 types on the import/export boundary",
createLegalizeJSInterfacePass);
diff --git a/src/passes/passes.h b/src/passes/passes.h
index 715e5c443..47b246bd4 100644
--- a/src/passes/passes.h
+++ b/src/passes/passes.h
@@ -62,6 +62,7 @@ Pass* createLimitSegmentsPass();
Pass* createLocalCSEPass();
Pass* createLocalSubtypingPass();
Pass* createLogExecutionPass();
+Pass* createIntrinsicLoweringPass();
Pass* createInstrumentLocalsPass();
Pass* createInstrumentMemoryPass();
Pass* createLoopInvariantCodeMotionPass();