summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlon Zakai <azakai@google.com>2024-09-18 16:03:50 -0700
committerGitHub <noreply@github.com>2024-09-18 16:03:50 -0700
commite2ce099f7e85b506cb45d306ec7c89a237d2e7c6 (patch)
tree92a21efab63c88525db973cc89744a11ddd25078 /src
parent5e4a4bae6544eda7d6e5be923bd1086921ffcb1b (diff)
downloadbinaryen-e2ce099f7e85b506cb45d306ec7c89a237d2e7c6.tar.gz
binaryen-e2ce099f7e85b506cb45d306ec7c89a237d2e7c6.tar.bz2
binaryen-e2ce099f7e85b506cb45d306ec7c89a237d2e7c6.zip
[NFC] Avoid collecting unnecessary parents in OptimizeAddedConstants (#6953)
This makes the pass 20% faster.
Diffstat (limited to 'src')
-rw-r--r--src/passes/OptimizeAddedConstants.cpp26
1 files changed, 24 insertions, 2 deletions
diff --git a/src/passes/OptimizeAddedConstants.cpp b/src/passes/OptimizeAddedConstants.cpp
index f03109223..142f10024 100644
--- a/src/passes/OptimizeAddedConstants.cpp
+++ b/src/passes/OptimizeAddedConstants.cpp
@@ -32,13 +32,35 @@
#include <ir/local-graph.h>
#include <ir/local-utils.h>
-#include <ir/parents.h>
#include <pass.h>
#include <wasm-builder.h>
#include <wasm.h>
namespace wasm {
+namespace {
+
+// Similar to Parents from parents.h, but we only care about gets, so it is much
+// more efficient to just collect their parents.
+struct GetParents {
+ GetParents(Expression* expr) { inner.walk(expr); }
+
+ Expression* getParent(LocalGet* curr) const {
+ auto iter = inner.parentMap.find(curr);
+ assert(iter != inner.parentMap.end());
+ return iter->second;
+ }
+
+private:
+ struct Inner : public ExpressionStackWalker<Inner> {
+ void visitLocalGet(LocalGet* curr) { parentMap[curr] = getParent(); }
+
+ std::unordered_map<Expression*, Expression*> parentMap;
+ } inner;
+};
+
+} // anonymous namespace
+
template<typename P, typename T> class MemoryAccessOptimizer {
public:
MemoryAccessOptimizer(P* parent,
@@ -356,7 +378,7 @@ private:
// g(a, offset=10)
// but if x has other uses, then avoid doing so - we'll be doing that add
// anyhow, so the load/store offset trick won't actually help.
- Parents parents(getFunction()->body);
+ GetParents parents(getFunction()->body);
for (auto& [location, _] : localGraph->locations) {
if (auto* set = location->dynCast<LocalSet>()) {
if (auto* add = set->value->dynCast<Binary>()) {