Convert lva{Inc,Dec}RefCntsCB to a visitor.
authorPat Gavlin <pagavlin@microsoft.com>
Tue, 27 Jun 2017 18:34:33 +0000 (11:34 -0700)
committerPat Gavlin <pagavlin@microsoft.com>
Wed, 28 Jun 2017 17:05:01 +0000 (10:05 -0700)
Commit migrated from https://github.com/dotnet/coreclr/commit/76809bbdbd1300da55cdda2c5187e4637f8b2223

src/coreclr/src/jit/compiler.h
src/coreclr/src/jit/earlyprop.cpp
src/coreclr/src/jit/flowgraph.cpp
src/coreclr/src/jit/lclvars.cpp
src/coreclr/src/jit/optimizer.cpp

index 483c7a2..ca67ad6 100644 (file)
@@ -2636,11 +2636,9 @@ public:
 
     VARSET_VALRET_TP lvaStmtLclMask(GenTreePtr stmt);
 
-    static fgWalkPreFn lvaIncRefCntsCB;
     void lvaIncRefCnts(GenTreePtr tree);
-
-    static fgWalkPreFn lvaDecRefCntsCB;
     void lvaDecRefCnts(GenTreePtr tree);
+
     void lvaDecRefCnts(BasicBlock* basicBlock, GenTreePtr tree);
     void lvaRecursiveDecRefCounts(GenTreePtr tree);
     void lvaRecursiveIncRefCounts(GenTreePtr tree);
@@ -9881,6 +9879,36 @@ public:
     }
 };
 
+class IncLclVarRefCountsVisitor final : public GenTreeVisitor<IncLclVarRefCountsVisitor>
+{
+public:
+    enum
+    {
+        DoPreOrder = true,
+        DoLclVarsOnly = true
+    };
+
+    IncLclVarRefCountsVisitor(Compiler* compiler);
+    Compiler::fgWalkResult PreOrderVisit(GenTree** use, GenTree* user);
+
+    static Compiler::fgWalkResult WalkTree(Compiler* compiler, GenTree* tree);
+};
+
+class DecLclVarRefCountsVisitor final : public GenTreeVisitor<DecLclVarRefCountsVisitor>
+{
+public:
+    enum
+    {
+        DoPreOrder = true,
+        DoLclVarsOnly = true
+    };
+
+    DecLclVarRefCountsVisitor(Compiler* compiler);
+    Compiler::fgWalkResult PreOrderVisit(GenTree** use, GenTree* user);
+
+    static Compiler::fgWalkResult WalkTree(Compiler* compiler, GenTree* tree);
+};
+
 /*
 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
index 51de631..7f32961 100644 (file)
@@ -325,7 +325,7 @@ bool Compiler::optEarlyPropRewriteTree(GenTreePtr tree)
             actualValCopy = gtNewLargeOperNode(GT_ADD, TYP_INT);
         }
 
-        fgWalkTreePre(&tree, Compiler::lvaDecRefCntsCB, (void*)this, true);
+        DecLclVarRefCountsVisitor::WalkTree(this, tree);
 
         actualValCopy->CopyFrom(actualVal, this);
         actualValCopy->gtType = origType;
@@ -334,7 +334,7 @@ bool Compiler::optEarlyPropRewriteTree(GenTreePtr tree)
             actualValCopy->LabelIndex(this);
         }
 
-        fgWalkTreePre(&actualValCopy, Compiler::lvaIncRefCntsCB, (void*)this, true);
+        IncLclVarRefCountsVisitor::WalkTree(this, actualValCopy);
 
         if (actualValCopy != tree)
         {
index 2d48b96..9878c69 100644 (file)
@@ -9271,7 +9271,7 @@ void Compiler::fgUpdateRefCntForClone(BasicBlock* addedToBlock, GenTreePtr clone
     if (lvaLocalVarRefCounted)
     {
         compCurBB = addedToBlock;
-        fgWalkTreePre(&clonedTree, Compiler::lvaIncRefCntsCB, (void*)this, true);
+        IncLclVarRefCountsVisitor::WalkTree(this, clonedTree);
     }
 }
 
@@ -9289,10 +9289,10 @@ void Compiler::fgUpdateRefCntForExtract(GenTreePtr wholeTree, GenTreePtr keptTre
          */
         if (keptTree)
         {
-            fgWalkTreePre(&keptTree, Compiler::lvaIncRefCntsCB, (void*)this, true);
+            IncLclVarRefCountsVisitor::WalkTree(this, keptTree);
         }
 
-        fgWalkTreePre(&wholeTree, Compiler::lvaDecRefCntsCB, (void*)this, true);
+        DecLclVarRefCountsVisitor::WalkTree(this, wholeTree);
     }
 }
 
@@ -9541,7 +9541,7 @@ DONE:
         {
             if (fgStmtListThreaded)
             {
-                fgWalkTreePre(&stmt->gtStmtExpr, Compiler::lvaDecRefCntsCB, (void*)this, true);
+                DecLclVarRefCountsVisitor::WalkTree(this, stmt->gtStmtExpr);
             }
         }
     }
index e89052e..2541716 100644 (file)
@@ -2688,16 +2688,6 @@ BasicBlock::weight_t BasicBlock::getBBWeight(Compiler* comp)
     }
 }
 
-/*****************************************************************************
- *
- *  Callback used by the tree walker to call lvaDecRefCnts
- */
-Compiler::fgWalkResult Compiler::lvaDecRefCntsCB(GenTreePtr* pTree, fgWalkData* data)
-{
-    data->compiler->lvaDecRefCnts(*pTree);
-    return WALK_CONTINUE;
-}
-
 // Decrement the ref counts for all locals contained in the tree and its children.
 void Compiler::lvaRecursiveDecRefCounts(GenTreePtr tree)
 {
@@ -2714,28 +2704,25 @@ void Compiler::lvaRecursiveDecRefCounts(GenTreePtr tree)
     }
     else
     {
-        fgWalkTreePre(&tree, Compiler::lvaDecRefCntsCB, (void*)this, true);
+        DecLclVarRefCountsVisitor::WalkTree(this, tree);
     }
 }
 
-// Increment the ref counts for all locals contained in the tree and its children.
-void Compiler::lvaRecursiveIncRefCounts(GenTreePtr tree)
+DecLclVarRefCountsVisitor::DecLclVarRefCountsVisitor(Compiler* compiler)
+    : GenTreeVisitor<DecLclVarRefCountsVisitor>(compiler)
 {
-    assert(lvaLocalVarRefCounted);
+}
 
-    // We could just use the recursive walker for all cases but that is a
-    // fairly heavyweight thing to spin up when we're usually just handling a leaf.
-    if (tree->OperIsLeaf())
-    {
-        if (tree->OperIsLocal())
-        {
-            lvaIncRefCnts(tree);
-        }
-    }
-    else
-    {
-        fgWalkTreePre(&tree, Compiler::lvaIncRefCntsCB, (void*)this, true);
-    }
+Compiler::fgWalkResult DecLclVarRefCountsVisitor::PreOrderVisit(GenTree** use, GenTree* user)
+{
+    m_compiler->lvaDecRefCnts(*use);
+    return fgWalkResult::WALK_CONTINUE;
+}
+
+Compiler::fgWalkResult DecLclVarRefCountsVisitor::WalkTree(Compiler* compiler, GenTree* tree)
+{
+    DecLclVarRefCountsVisitor visitor(compiler);
+    return static_cast<GenTreeVisitor<DecLclVarRefCountsVisitor>*>(&visitor)->WalkTree(&tree, nullptr);
 }
 
 /*****************************************************************************
@@ -2796,14 +2783,41 @@ void Compiler::lvaDecRefCnts(BasicBlock* block, GenTreePtr tree)
     }
 }
 
-/*****************************************************************************
- *
- *  Callback used by the tree walker to call lvaIncRefCnts
- */
-Compiler::fgWalkResult Compiler::lvaIncRefCntsCB(GenTreePtr* pTree, fgWalkData* data)
+// Increment the ref counts for all locals contained in the tree and its children.
+void Compiler::lvaRecursiveIncRefCounts(GenTreePtr tree)
 {
-    data->compiler->lvaIncRefCnts(*pTree);
-    return WALK_CONTINUE;
+    assert(lvaLocalVarRefCounted);
+
+    // We could just use the recursive walker for all cases but that is a
+    // fairly heavyweight thing to spin up when we're usually just handling a leaf.
+    if (tree->OperIsLeaf())
+    {
+        if (tree->OperIsLocal())
+        {
+            lvaIncRefCnts(tree);
+        }
+    }
+    else
+    {
+        IncLclVarRefCountsVisitor::WalkTree(this, tree);
+    }
+}
+
+IncLclVarRefCountsVisitor::IncLclVarRefCountsVisitor(Compiler* compiler)
+    : GenTreeVisitor<IncLclVarRefCountsVisitor>(compiler)
+{
+}
+
+Compiler::fgWalkResult IncLclVarRefCountsVisitor::PreOrderVisit(GenTree** use, GenTree* user)
+{
+    m_compiler->lvaIncRefCnts(*use);
+    return fgWalkResult::WALK_CONTINUE;
+}
+
+Compiler::fgWalkResult IncLclVarRefCountsVisitor::WalkTree(Compiler* compiler, GenTree* tree)
+{
+    IncLclVarRefCountsVisitor visitor(compiler);
+    return static_cast<GenTreeVisitor<IncLclVarRefCountsVisitor>*>(&visitor)->WalkTree(&tree, nullptr);
 }
 
 /*****************************************************************************
index 17f0ab4..edf8b27 100644 (file)
@@ -8105,7 +8105,7 @@ void Compiler::optOptimizeBoolsGcStress(BasicBlock* condBlock)
 
     // Bump up the ref-counts of any variables in 'comparandClone'
     compCurBB = condBlock;
-    fgWalkTreePre(&comparandClone, Compiler::lvaIncRefCntsCB, (void*)this, true);
+    IncLclVarRefCountsVisitor::WalkTree(this, comparandClone);
 
     noway_assert(relop->gtOp.gtOp1 == comparand);
     genTreeOps oper   = compStressCompile(STRESS_OPT_BOOLS_GC, 50) ? GT_OR : GT_AND;