summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2016-05-28 15:01:35 -0700
committerAlon Zakai <alonzakai@gmail.com>2016-05-28 15:01:35 -0700
commit5615ca92dd2c3c60bcc13d87cbb20267495a8a89 (patch)
tree191e9d1b4c2bfb81d611e3601ab2b9a5e462b492 /src
parent4c07db98d1d02856549c1478902d4d44ad57c50c (diff)
downloadbinaryen-5615ca92dd2c3c60bcc13d87cbb20267495a8a89.tar.gz
binaryen-5615ca92dd2c3c60bcc13d87cbb20267495a8a89.tar.bz2
binaryen-5615ca92dd2c3c60bcc13d87cbb20267495a8a89.zip
canonicalize the order in reorder-locals, by using first-appearance to break ties
Diffstat (limited to 'src')
-rw-r--r--src/passes/ReorderLocals.cpp19
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();
+ }
}
};