diff options
author | Alon Zakai <alonzakai@gmail.com> | 2016-05-29 16:39:17 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2016-05-29 16:39:17 -0700 |
commit | f33f1dbbee7b3f95d8437f1ee60c9075013858b6 (patch) | |
tree | f0f7a4bc5cd7d948f4285298b3b3930f30cc0185 /src/passes/ReorderLocals.cpp | |
parent | 1715b4a1ec845f1dd6b08f48a599f346beb0f758 (diff) | |
parent | 44aeb85b2fa2c743e2d0f7e00349f99cfcbc7639 (diff) | |
download | binaryen-f33f1dbbee7b3f95d8437f1ee60c9075013858b6.tar.gz binaryen-f33f1dbbee7b3f95d8437f1ee60c9075013858b6.tar.bz2 binaryen-f33f1dbbee7b3f95d8437f1ee60c9075013858b6.zip |
Merge pull request #550 from WebAssembly/dfe-nice
Duplicate function elimination
Diffstat (limited to 'src/passes/ReorderLocals.cpp')
-rw-r--r-- | src/passes/ReorderLocals.cpp | 19 |
1 files changed, 14 insertions, 5 deletions
diff --git a/src/passes/ReorderLocals.cpp b/src/passes/ReorderLocals.cpp index 0e626e0a2..eea8ea962 100644 --- a/src/passes/ReorderLocals.cpp +++ b/src/passes/ReorderLocals.cpp @@ -17,7 +17,8 @@ // // Sorts locals by access frequency. // - +// Secondarily, sort by first appearance. This canonicalizes the order. +// #include <memory> @@ -29,7 +30,8 @@ namespace wasm { struct ReorderLocals : public WalkerPass<PostWalker<ReorderLocals, Visitor<ReorderLocals>>> { bool isFunctionParallel() { return true; } - std::map<Index, uint32_t> counts; + std::map<Index, Index> counts; // local => times it is used + std::map<Index, Index> firstUses; // local => index in the list of which local is first seen void visitFunction(Function *curr) { Index num = curr->getNumLocals(); @@ -44,10 +46,11 @@ struct ReorderLocals : public WalkerPass<PostWalker<ReorderLocals, Visitor<Reord if (curr->isParam(b) && curr->isParam(a)) { return a < b; } - if (this->counts[a] == this->counts[b]) { - return a < b; + if (counts[a] == counts[b]) { + if (counts[a] == 0) return a < b; + return firstUses[a] < firstUses[b]; } - return this->counts[a] > this->counts[b]; + return counts[a] > counts[b]; }); // sorting left params in front, perhaps slightly reordered. verify and fix. for (size_t i = 0; i < curr->params.size(); i++) { @@ -116,10 +119,16 @@ struct ReorderLocals : public WalkerPass<PostWalker<ReorderLocals, Visitor<Reord void visitGetLocal(GetLocal *curr) { counts[curr->index]++; + if (firstUses.count(curr->index) == 0) { + firstUses[curr->index] = firstUses.size(); + } } void visitSetLocal(SetLocal *curr) { counts[curr->index]++; + if (firstUses.count(curr->index) == 0) { + firstUses[curr->index] = firstUses.size(); + } } }; |