Allow folding of `LONG` nodes with field sequences (#57605)
authorSingleAccretion <62474226+SingleAccretion@users.noreply.github.com>
Fri, 17 Sep 2021 20:19:18 +0000 (23:19 +0300)
committerGitHub <noreply@github.com>
Fri, 17 Sep 2021 20:19:18 +0000 (13:19 -0700)
* Allow folding of LONG nodes with field sequences

Apparently, such folding was disallowed, for unclear reasons
likely pertaining to the fact that "gtFoldExprConst" is very old.

* Drop the special case from fgMorphCommutative

src/coreclr/jit/gentree.cpp
src/coreclr/jit/morph.cpp

index ff9f653..491c7b3 100644 (file)
@@ -14915,6 +14915,7 @@ GenTree* Compiler::gtFoldExprConst(GenTree* tree)
             // has TYP_INT the result needs to be sign extended rather than zero extended.
             tree->BashToConst(static_cast<int>(i1));
             tree->AsIntCon()->gtFieldSeq = fieldSeq;
+
             if (vnStore != nullptr)
             {
                 fgValueNumberTreeConst(tree);
@@ -15012,6 +15013,9 @@ GenTree* Compiler::gtFoldExprConst(GenTree* tree)
                         goto INTEGRAL_OVF;
                     }
                     lval1 = ltemp;
+#ifdef TARGET_64BIT
+                    fieldSeq = GetFieldSeqStore()->Append(op1->AsIntCon()->gtFieldSeq, op2->AsIntCon()->gtFieldSeq);
+#endif
                     break;
 
                 case GT_SUB:
@@ -15117,11 +15121,13 @@ GenTree* Compiler::gtFoldExprConst(GenTree* tree)
             }
 
         CNS_LONG:
-
+#if !defined(TARGET_64BIT)
             if (fieldSeq != FieldSeqStore::NotAField())
             {
+                assert(!"Field sequences on CNS_LNG nodes!?");
                 return tree;
             }
+#endif // !defined(TARGET_64BIT)
 
             JITDUMP("\nFolding long operator with constant nodes into a constant:\n");
             DISPTREE(tree);
@@ -15130,6 +15136,9 @@ GenTree* Compiler::gtFoldExprConst(GenTree* tree)
                    (tree->gtDebugFlags & GTF_DEBUG_NODE_LARGE));
 
             tree->BashToConst(lval1);
+#ifdef TARGET_64BIT
+            tree->AsIntCon()->gtFieldSeq = fieldSeq;
+#endif
             if (vnStore != nullptr)
             {
                 fgValueNumberTreeConst(tree);
index 79f0e20..59d088f 100644 (file)
@@ -11032,13 +11032,7 @@ GenTree* Compiler::fgMorphCommutative(GenTreeOp* tree)
 
     cns1->SetIconValue(foldedCns->IconValue());
     cns1->SetVNsFromNode(foldedCns);
-
-    if (oper == GT_ADD)
-    {
-        // Note that gtFoldExprConst doesn't maintain fieldSeq when folding constant
-        // trees of TYP_LONG.
-        cns1->gtFieldSeq = GetFieldSeqStore()->Append(cns1->gtFieldSeq, cns2->gtFieldSeq);
-    }
+    cns1->gtFieldSeq = foldedCns->gtFieldSeq;
 
     op1 = tree->gtGetOp1();
     op1->SetVNsFromNode(tree);