From 5da280167ac6f3e76d27c109ee08ae1747405b5c Mon Sep 17 00:00:00 2001 From: Alon Zakai Date: Mon, 11 Apr 2016 21:21:50 -0700 Subject: drop completely unused locals in ReorderLocals --- src/passes/ReorderLocals.cpp | 7 ++++++- src/passes/SimplifyLocals.cpp | 4 ++++ 2 files changed, 10 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/passes/ReorderLocals.cpp b/src/passes/ReorderLocals.cpp index e378408b6..7f64c00f3 100644 --- a/src/passes/ReorderLocals.cpp +++ b/src/passes/ReorderLocals.cpp @@ -31,12 +31,17 @@ struct ReorderLocals : public WalkerPass> { std::map counts; void visitFunction(Function *curr) { - sort(curr->locals.begin(), curr->locals.end(), [this](NameType a, NameType b) -> bool { + auto& locals = curr->locals; + sort(locals.begin(), locals.end(), [this](NameType a, NameType b) -> bool { if (this->counts[a.name] == this->counts[b.name]) { return strcmp(a.name.str, b.name.str) > 0; } return this->counts[a.name] > this->counts[b.name]; }); + // drop completely unused locals + while (locals.size() > 0 && counts[locals.back().name] == 0) { + locals.pop_back(); + } counts.clear(); } diff --git a/src/passes/SimplifyLocals.cpp b/src/passes/SimplifyLocals.cpp index faf6e3a6e..408fea7ab 100644 --- a/src/passes/SimplifyLocals.cpp +++ b/src/passes/SimplifyLocals.cpp @@ -20,6 +20,10 @@ // This "sinks" set_locals, pushing them to the next get_local where possible, // and removing the set if there are no gets remaining (the latter is // particularly useful in ssa mode, but not only). +// +// After this pass, some locals may be completely unused. reorder-locals +// can get rid of those (the operation is trivial there after it sorts by use +// frequency). #include #include -- cgit v1.2.3