summaryrefslogtreecommitdiff
path: root/src/passes/OptimizeInstructions.cpp
diff options
context:
space:
mode:
authorAlon Zakai <azakai@google.com>2022-05-27 11:16:39 -0700
committerGitHub <noreply@github.com>2022-05-27 11:16:39 -0700
commitf21774f3865e506b6dea912af8f8be16fd29dacb (patch)
treecd08af70a3301d44c231e093acbb0799a62123bb /src/passes/OptimizeInstructions.cpp
parentac2190c1bd1a0be12a1d25e62695f791f270d050 (diff)
downloadbinaryen-f21774f3865e506b6dea912af8f8be16fd29dacb.tar.gz
binaryen-f21774f3865e506b6dea912af8f8be16fd29dacb.tar.bz2
binaryen-f21774f3865e506b6dea912af8f8be16fd29dacb.zip
OptimizeInstructions: Turn call_ref of a select into an if over two direct calls (#4660)
This extends the existing call_indirect code to do the same for call_ref, basically. The shared code is added to a new helper utility.
Diffstat (limited to 'src/passes/OptimizeInstructions.cpp')
-rw-r--r--src/passes/OptimizeInstructions.cpp18
1 files changed, 18 insertions, 0 deletions
diff --git a/src/passes/OptimizeInstructions.cpp b/src/passes/OptimizeInstructions.cpp
index 4b499213f..c4a5b2ed4 100644
--- a/src/passes/OptimizeInstructions.cpp
+++ b/src/passes/OptimizeInstructions.cpp
@@ -42,6 +42,8 @@
#include <support/threads.h>
#include <wasm.h>
+#include "call-utils.h"
+
// TODO: Use the new sign-extension opcodes where appropriate. This needs to be
// conditionalized on the availability of atomics.
@@ -1334,6 +1336,22 @@ struct OptimizeInstructions
curr->operands.back() = builder.makeBlock({set, drop, get});
replaceCurrent(builder.makeCall(
ref->func, curr->operands, curr->type, curr->isReturn));
+ return;
+ }
+
+ // If the target is a select of two different constants, we can emit an if
+ // over two direct calls.
+ if (auto* calls = CallUtils::convertToDirectCalls(
+ curr,
+ [](Expression* target) -> CallUtils::IndirectCallInfo {
+ if (auto* refFunc = target->dynCast<RefFunc>()) {
+ return CallUtils::Known{refFunc->func};
+ }
+ return CallUtils::Unknown{};
+ },
+ *getFunction(),
+ *getModule())) {
+ replaceCurrent(calls);
}
}