diff options
author | Alon Zakai <azakai@google.com> | 2021-11-19 16:58:10 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-19 16:58:10 -0800 |
commit | d77e24464961b6eee9fc5004f3b9581aa8196096 (patch) | |
tree | 6f9d50f470590b2b3dddc48bac9d62fe94573b61 /src/passes/RedundantSetElimination.cpp | |
parent | 01eccfff22e0dbe806f077eec26ed43f4a85e656 (diff) | |
download | binaryen-d77e24464961b6eee9fc5004f3b9581aa8196096.tar.gz binaryen-d77e24464961b6eee9fc5004f3b9581aa8196096.tar.bz2 binaryen-d77e24464961b6eee9fc5004f3b9581aa8196096.zip |
[NFC] Move value numbering code to a header (#4345)
This just moves code out of RedundantSetElimination.
Diffstat (limited to 'src/passes/RedundantSetElimination.cpp')
-rw-r--r-- | src/passes/RedundantSetElimination.cpp | 81 |
1 files changed, 33 insertions, 48 deletions
diff --git a/src/passes/RedundantSetElimination.cpp b/src/passes/RedundantSetElimination.cpp index f42119a92..56fca4bee 100644 --- a/src/passes/RedundantSetElimination.cpp +++ b/src/passes/RedundantSetElimination.cpp @@ -35,6 +35,7 @@ #include <cfg/cfg-traversal.h> #include <ir/literal-utils.h> +#include <ir/numbering.h> #include <ir/properties.h> #include <ir/utils.h> #include <pass.h> @@ -44,11 +45,9 @@ namespace wasm { -// We do a very simple numbering of local values, just a unique -// number for constants so far, enough to see -// trivial duplication. LocalValues maps each local index to -// its current value -typedef std::vector<Index> LocalValues; +// Map each local index to its current value number (which is computed in +// ValueNumbering). +using LocalValues = std::vector<Index>; namespace { @@ -84,6 +83,10 @@ struct RedundantSetElimination if (numLocals == 0) { return; // nothing to do } + + // Create a unique value for use to mark unseen locations. + unseenValue = valueNumbering.getUniqueValue(); + // create the CFG by walking the IR CFGWalker<RedundantSetElimination, Visitor<RedundantSetElimination>, Info>:: doWalkFunction(func); @@ -93,47 +96,30 @@ struct RedundantSetElimination optimize(); } - // numbering + // Use a value numbering for the values of expressions. + ValueNumbering valueNumbering; - Index nextValue = 1; // 0 is reserved for the "unseen value" - // each constant has a value - std::unordered_map<Literals, Index> literalValues; - // each value can have a value - std::unordered_map<Expression*, Index> expressionValues; - // each block has values for each merge + // In additon to valueNumbering, each block has values for each merge. std::unordered_map<BasicBlock*, std::unordered_map<Index, Index>> blockMergeValues; - Index getUnseenValue() { // we haven't seen this location yet - return 0; - } - Index getUniqueValue() { -#ifdef RSE_DEBUG - std::cout << "new unique value " << nextValue << '\n'; -#endif - return nextValue++; - } + // A value that indicates we haven't seen this location yet. + Index unseenValue; - Index getLiteralValue(Literals lit) { - auto iter = literalValues.find(lit); - if (iter != literalValues.end()) { - return iter->second; - } + Index getUniqueValue() { + auto value = valueNumbering.getUniqueValue(); #ifdef RSE_DEBUG - std::cout << "new literal value for " << lit << '\n'; + std::cout << "new unique value " << value << '\n'; #endif - return literalValues[lit] = getUniqueValue(); + return value; } - Index getExpressionValue(Expression* expr) { - auto iter = expressionValues.find(expr); - if (iter != expressionValues.end()) { - return iter->second; - } + Index getValue(Literals lit) { + auto value = valueNumbering.getValue(lit); #ifdef RSE_DEBUG - std::cout << "new expr value for " << expr << '\n'; + std::cout << "lit value " << value << '\n'; #endif - return expressionValues[expr] = getUniqueValue(); + return value; } Index getBlockMergeValue(BasicBlock* block, Index index) { @@ -162,17 +148,16 @@ struct RedundantSetElimination return value == iter2->second; } - Index getValue(Expression* value, LocalValues& currValues) { - if (Properties::isConstantExpression(value)) { - // a constant - return getLiteralValue(Properties::getLiterals(value)); - } else if (auto* get = value->dynCast<LocalGet>()) { + Index getValue(Expression* expr, LocalValues& currValues) { + if (auto* get = expr->dynCast<LocalGet>()) { // a copy of whatever that was return currValues[get->index]; - } else { - // get the value's own unique value - return getExpressionValue(value); } + auto value = valueNumbering.getValue(expr); +#ifdef RSE_DEBUG + std::cout << "expr value " << value << '\n'; +#endif + return value; } // flowing @@ -196,20 +181,20 @@ struct RedundantSetElimination #endif start[i] = getUniqueValue(); } else { - start[i] = getLiteralValue(Literal::makeZeros(type)); + start[i] = getValue(Literal::makeZeros(type)); } } } else { // other blocks have all unseen values to begin with for (Index i = 0; i < numLocals; i++) { - start[i] = getUnseenValue(); + start[i] = unseenValue; } } // the ends all begin unseen LocalValues& end = block->contents.end; end.resize(numLocals); for (Index i = 0; i < numLocals; i++) { - end[i] = getUnseenValue(); + end[i] = unseenValue; } } // keep working while stuff is flowing. we use a unique deferred queue @@ -280,9 +265,9 @@ struct RedundantSetElimination iter++; while (iter != in.end()) { auto otherValue = (*iter)->contents.end[i]; - if (value == getUnseenValue()) { + if (value == unseenValue) { value = otherValue; - } else if (otherValue == getUnseenValue()) { + } else if (otherValue == unseenValue) { // nothing to do, other has no information } else if (value != otherValue) { // 2 different values, this is a merged value |