Avoid clobbering FldSeq annotations in cloneExpr
authorJoseph Tremoulet <jotrem@microsoft.com>
Wed, 19 Oct 2016 22:14:24 +0000 (18:14 -0400)
committerJoseph Tremoulet <jotrem@microsoft.com>
Fri, 4 Nov 2016 14:42:44 +0000 (10:42 -0400)
The overload of cloneExpr that takes a variable number and value to
perform replacements is intended to be used by the loop unroller, which
runs between Morph (where FldSeq annotations are generated) and value
numbering (where FldSeq annotations are consumed).  Update that codepath
to call `LabelIndex` when replacing a `GTF_VAR_ARR_INDEX` node, and to
avoid calling `gtFoldExpr` (which does not preserve these annotations).
Downstream constant folding should be capable of doing that optimization.

Commit migrated from https://github.com/dotnet/coreclr/commit/4748bb354f00b6d48aae4a85ac3b1c955e6fe0ca

src/coreclr/src/jit/gentree.cpp

index 777ce7d..1ad72f0 100644 (file)
@@ -7777,6 +7777,10 @@ GenTreePtr Compiler::gtCloneExpr(
                 if (tree->gtLclVarCommon.gtLclNum == varNum)
                 {
                     copy = gtNewIconNode(varVal, tree->gtType);
+                    if (tree->gtFlags & GTF_VAR_ARR_INDEX)
+                    {
+                        copy->LabelIndex(this);
+                    }
                 }
                 else
                 {
@@ -8119,18 +8123,6 @@ GenTreePtr Compiler::gtCloneExpr(
             copy->CopyReg(tree);
         }
 
-        // We can call gtCloneExpr() before we have called fgMorph when we expand a GT_INDEX node in fgMorphArrayIndex()
-        // The method gtFoldExpr() expects to be run after fgMorph so it will set the GTF_DEBUG_NODE_MORPHED
-        // flag on nodes that it adds/modifies.  Then when we call fgMorph we will assert.
-        // We really only will need to fold when this method is used to replace references to
-        // local variable with an integer.
-        //
-        if (varNum != (unsigned)-1)
-        {
-            /* Try to do some folding */
-            copy = gtFoldExpr(copy);
-        }
-
         goto DONE;
     }