summaryrefslogtreecommitdiff
path: root/src/passes/param-utils.cpp
diff options
context:
space:
mode:
authorAlon Zakai <azakai@google.com>2022-03-25 07:34:05 -0700
committerGitHub <noreply@github.com>2022-03-25 07:34:05 -0700
commit05527357b307714dab259098e860e610f69ecf59 (patch)
tree07ef460494435a685debe1536bcbfc1d79789f24 /src/passes/param-utils.cpp
parent97d68ac572a0ffdc74fc5d8da2df65da42dc603e (diff)
downloadbinaryen-05527357b307714dab259098e860e610f69ecf59.tar.gz
binaryen-05527357b307714dab259098e860e610f69ecf59.tar.bz2
binaryen-05527357b307714dab259098e860e610f69ecf59.zip
[NFC] Move and generalize constant-parameter code from DeadArgumentElimination (#4547)
Similar to #4544, this moves the code to a utility function, and also slightly generalizes it to support a list of functions (and not just 1) and also a list of call_refs (and not just calls).
Diffstat (limited to 'src/passes/param-utils.cpp')
-rw-r--r--src/passes/param-utils.cpp62
1 files changed, 60 insertions, 2 deletions
diff --git a/src/passes/param-utils.cpp b/src/passes/param-utils.cpp
index 019df6d7f..ded96a826 100644
--- a/src/passes/param-utils.cpp
+++ b/src/passes/param-utils.cpp
@@ -16,6 +16,7 @@
#include "ir/function-utils.h"
#include "ir/local-graph.h"
+#include "ir/possible-constant.h"
#include "ir/type-updating.h"
#include "support/sorted_vector.h"
#include "wasm.h"
@@ -44,7 +45,7 @@ std::unordered_set<Index> getUsedParams(Function* func) {
return usedParams;
}
-bool removeParameter(const std::vector<Function*> funcs,
+bool removeParameter(const std::vector<Function*>& funcs,
Index index,
const std::vector<Call*>& calls,
const std::vector<CallRef*>& callRefs,
@@ -137,7 +138,7 @@ bool removeParameter(const std::vector<Function*> funcs,
return true;
}
-SortedVector removeParameters(const std::vector<Function*> funcs,
+SortedVector removeParameters(const std::vector<Function*>& funcs,
SortedVector indexes,
const std::vector<Call*>& calls,
const std::vector<CallRef*>& callRefs,
@@ -174,4 +175,61 @@ SortedVector removeParameters(const std::vector<Function*> funcs,
return removed;
}
+SortedVector applyConstantValues(const std::vector<Function*>& funcs,
+ const std::vector<Call*>& calls,
+ const std::vector<CallRef*>& callRefs,
+ Module* module) {
+ assert(funcs.size() > 0);
+ auto* first = funcs[0];
+#ifndef NDEBUG
+ for (auto* func : funcs) {
+ assert(func->type == first->type);
+ }
+#endif
+
+ SortedVector optimized;
+ auto numParams = first->getNumParams();
+ for (Index i = 0; i < numParams; i++) {
+ PossibleConstantValues value;
+
+ // Processes one operand.
+ auto processOperand = [&](Expression* operand) {
+ if (auto* c = operand->dynCast<Const>()) {
+ value.note(c->value);
+ return;
+ }
+ // TODO: refnull, immutable globals, etc.
+ // Not a constant, give up
+ value.noteUnknown();
+ };
+ for (auto* call : calls) {
+ processOperand(call->operands[i]);
+ if (!value.isConstant()) {
+ break;
+ }
+ }
+ for (auto* call : callRefs) {
+ processOperand(call->operands[i]);
+ if (!value.isConstant()) {
+ break;
+ }
+ }
+ if (!value.isConstant()) {
+ continue;
+ }
+
+ // Optimize: write the constant value in the function bodies, making them
+ // ignore the parameter's value.
+ Builder builder(*module);
+ for (auto* func : funcs) {
+ func->body = builder.makeSequence(
+ builder.makeLocalSet(i, builder.makeConst(value.getConstantLiteral())),
+ func->body);
+ }
+ optimized.insert(i);
+ }
+
+ return optimized;
+}
+
} // namespace wasm::ParamUtils