diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/ir/effects.h | 38 | ||||
-rw-r--r-- | src/js/binaryen.js-post.js | 4 | ||||
-rw-r--r-- | src/passes/LoopInvariantCodeMotion.cpp | 7 | ||||
-rw-r--r-- | src/passes/SimplifyGlobals.cpp | 14 |
4 files changed, 42 insertions, 21 deletions
diff --git a/src/ir/effects.h b/src/ir/effects.h index e33a33818..f24f0ac3f 100644 --- a/src/ir/effects.h +++ b/src/ir/effects.h @@ -71,7 +71,8 @@ public: bool calls = false; std::set<Index> localsRead; std::set<Index> localsWritten; - std::set<Name> globalsRead; + std::set<Name> mutableGlobalsRead; + std::set<Name> immutableGlobalsRead; std::set<Name> globalsWritten; bool readsMemory = false; bool writesMemory = false; @@ -128,8 +129,11 @@ public: bool accessesLocal() const { return localsRead.size() + localsWritten.size() > 0; } + bool accessesMutableGlobal() const { + return globalsWritten.size() + mutableGlobalsRead.size() > 0; + } bool accessesGlobal() const { - return globalsRead.size() + globalsWritten.size() > 0; + return accessesMutableGlobal() + immutableGlobalsRead.size() > 0; } bool accessesMemory() const { return calls || readsMemory || writesMemory; } bool accessesTable() const { return calls || readsTable || writesTable; } @@ -156,10 +160,9 @@ public: return globalsWritten.size() || writesMemory || writesTable || writesStruct || writesArray || isAtomic || calls; } - bool readsGlobalState() const { - return globalsRead.size() || readsMemory || readsTable || - readsMutableStruct || readsImmutableStruct || readsArray || - isAtomic || calls; + bool readsMutableGlobalState() const { + return mutableGlobalsRead.size() || readsMemory || readsTable || + readsMutableStruct || readsArray || isAtomic || calls; } bool hasNonTrapSideEffects() const { @@ -233,17 +236,17 @@ public: return true; } } - if ((other.calls && accessesGlobal()) || - (calls && other.accessesGlobal())) { + if ((other.calls && accessesMutableGlobal()) || + (calls && other.accessesMutableGlobal())) { return true; } for (auto global : globalsWritten) { - if (other.globalsRead.count(global) || + if (other.mutableGlobalsRead.count(global) || other.globalsWritten.count(global)) { return true; } } - for (auto global : globalsRead) { + for (auto global : mutableGlobalsRead) { if (other.globalsWritten.count(global)) { return true; } @@ -292,8 +295,11 @@ public: for (auto i : other.localsWritten) { localsWritten.insert(i); } - for (auto i : other.globalsRead) { - globalsRead.insert(i); + for (auto i : other.mutableGlobalsRead) { + mutableGlobalsRead.insert(i); + } + for (auto i : other.immutableGlobalsRead) { + immutableGlobalsRead.insert(i); } for (auto i : other.globalsWritten) { globalsWritten.insert(i); @@ -445,7 +451,11 @@ private: parent.localsWritten.insert(curr->index); } void visitGlobalGet(GlobalGet* curr) { - parent.globalsRead.insert(curr->name); + if (parent.module.getGlobal(curr->name)->mutable_ == Mutable) { + parent.mutableGlobalsRead.insert(curr->name); + } else { + parent.immutableGlobalsRead.insert(curr->name); + } } void visitGlobalSet(GlobalSet* curr) { parent.globalsWritten.insert(curr->name); @@ -768,7 +778,7 @@ public: if (localsWritten.size() > 0) { effects |= SideEffects::WritesLocal; } - if (globalsRead.size() > 0) { + if (mutableGlobalsRead.size() + immutableGlobalsRead.size() > 0) { effects |= SideEffects::ReadsGlobal; } if (globalsWritten.size() > 0) { diff --git a/src/js/binaryen.js-post.js b/src/js/binaryen.js-post.js index 3f8b6545b..ee90aa1fc 100644 --- a/src/js/binaryen.js-post.js +++ b/src/js/binaryen.js-post.js @@ -3181,7 +3181,9 @@ Module['getExpressionInfo'] = function(expr) { // Gets the side effects of the specified expression Module['getSideEffects'] = function(expr, module) { - return Module['_BinaryenExpressionGetSideEffects'](expr, module); + assert(module); // guard against incorrect old API usage: a module must be + // provided here. + return Module['_BinaryenExpressionGetSideEffects'](expr, module['ptr']); }; Module['createType'] = function(types) { diff --git a/src/passes/LoopInvariantCodeMotion.cpp b/src/passes/LoopInvariantCodeMotion.cpp index 1007e57b3..f339f8126 100644 --- a/src/passes/LoopInvariantCodeMotion.cpp +++ b/src/passes/LoopInvariantCodeMotion.cpp @@ -120,9 +120,10 @@ struct LoopInvariantCodeMotion // The rest of the loop's effects matter too, we must also // take into account global state like interacting loads and // stores. - bool unsafeToMove = - effects.writesGlobalState() || effectsSoFar.invalidates(effects) || - (effects.readsGlobalState() && loopEffects.writesGlobalState()); + bool unsafeToMove = effects.writesGlobalState() || + effectsSoFar.invalidates(effects) || + (effects.readsMutableGlobalState() && + loopEffects.writesGlobalState()); // TODO: look into optimizing this with exceptions. for now, disallow if (effects.throws || loopEffects.throws) { unsafeToMove = true; diff --git a/src/passes/SimplifyGlobals.cpp b/src/passes/SimplifyGlobals.cpp index 0943c04fe..be7cc6ca1 100644 --- a/src/passes/SimplifyGlobals.cpp +++ b/src/passes/SimplifyGlobals.cpp @@ -123,11 +123,19 @@ struct GlobalUseScanner : public WalkerPass<PostWalker<GlobalUseScanner>> { // See if reading a specific global is the only effect the first has. EffectAnalyzer firstEffects(getPassOptions(), *getModule(), first); - if (firstEffects.globalsRead.size() != 1) { + if (firstEffects.immutableGlobalsRead.size() + + firstEffects.mutableGlobalsRead.size() != + 1) { return Name(); } - auto global = *firstEffects.globalsRead.begin(); - firstEffects.globalsRead.clear(); + Name global; + if (firstEffects.immutableGlobalsRead.size() == 1) { + global = *firstEffects.immutableGlobalsRead.begin(); + firstEffects.immutableGlobalsRead.clear(); + } else { + global = *firstEffects.mutableGlobalsRead.begin(); + firstEffects.mutableGlobalsRead.clear(); + } if (firstEffects.hasAnything()) { return Name(); } |