Speed up Dfg common sub-expression elimination

Added a DfgVertex::user() mechanism for storing data in vertices.
Similar in spirit to AstNode user data, but the generation counter is
stored in the DfgGraph the vertex is held under. Use this to cache
DfgVertex::hash results, and also speed up DfgVertex hashing in general.

Use these and additional improvements to speed up CSE.
This commit is contained in:
Geza Lore
2022-10-06 19:59:01 +01:00
parent 97add4d57a
commit 4f0158b5e0
5 changed files with 132 additions and 66 deletions
+6 -3
View File
@@ -126,7 +126,6 @@ class DfgToAstVisitor final : DfgVisitor {
// Map from an AstVar, to the canonical AstVar that can be substituted for that AstVar
std::unordered_map<AstVar*, AstVar*> m_canonVars;
V3UniqueNames m_tmpNames{"_VdfgTmp"}; // For generating temporary names
DfgVertex::HashCache m_hashCache; // For caching hashes
// METHODS
@@ -167,7 +166,7 @@ class DfgToAstVisitor final : DfgVisitor {
// Given a DfgVertex, return an AstVar that will hold the value of the given DfgVertex once we
// are done with converting this Dfg into Ast form.
AstVar* getResultVar(const DfgVertex* vtxp) {
AstVar* getResultVar(DfgVertex* vtxp) {
const auto pair = m_resultVars.emplace(vtxp, nullptr);
AstVar*& varp = pair.first->second;
if (pair.second) {
@@ -187,7 +186,7 @@ class DfgToAstVisitor final : DfgVisitor {
} else {
// No DfgVarPacked driven fully by this node. Create a temporary.
// TODO: should we reuse parts when the AstVar is used as an rvalue?
const string name = m_tmpNames.get(vtxp->hash(m_hashCache).toString());
const string name = m_tmpNames.get(vtxp->hash().toString());
// Note: It is ok for these temporary variables to be always unsigned. They are
// read only by other expressions within the graph and all expressions interpret
// their operands based on the expression type, not the operand type.
@@ -330,6 +329,10 @@ class DfgToAstVisitor final : DfgVisitor {
explicit DfgToAstVisitor(DfgGraph& dfg, V3DfgOptimizationContext& ctx)
: m_modp{dfg.modulep()}
, m_ctx{ctx} {
// Used by DfgVertex::hash
const auto userDataInUse = dfg.userDataInUse();
// We can eliminate some variables completely
std::vector<AstVar*> redundantVarps;