Fix rva[cns] folding (#81854)
authorEgor Bogatov <egorbo@gmail.com>
Fri, 24 Feb 2023 08:48:24 +0000 (09:48 +0100)
committerGitHub <noreply@github.com>
Fri, 24 Feb 2023 08:48:24 +0000 (09:48 +0100)
src/coreclr/jit/valuenum.cpp

index 29603b2..ae3446d 100644 (file)
@@ -9609,14 +9609,6 @@ void Compiler::fgValueNumberTreeConst(GenTree* tree)
                 {
                     vnStore->AddToEmbeddedHandleMap(cns->IconValue(), cns->gtCompileTimeHandle);
                 }
-                else if ((handleFlags == GTF_ICON_STATIC_HDL) && (cns->gtFieldSeq != nullptr) &&
-                         (cns->gtFieldSeq->GetKind() == FieldSeq::FieldKind::SimpleStaticKnownAddress))
-                {
-                    assert(cns->IconValue() == cns->gtFieldSeq->GetOffset());
-
-                    // For now we're interested only in SimpleStaticKnownAddress
-                    vnStore->AddToFieldAddressToFieldSeqMap(cns->gtVNPair.GetLiberal(), cns->gtFieldSeq);
-                }
             }
             else if ((typ == TYP_LONG) || (typ == TYP_ULONG))
             {
@@ -9626,6 +9618,14 @@ void Compiler::fgValueNumberTreeConst(GenTree* tree)
             {
                 tree->gtVNPair.SetBoth(vnStore->VNForIntCon(int(tree->AsIntConCommon()->IconValue())));
             }
+
+            if (tree->IsCnsIntOrI() && (tree->AsIntCon()->gtFieldSeq != nullptr) &&
+                (tree->AsIntCon()->gtFieldSeq->GetKind() == FieldSeq::FieldKind::SimpleStaticKnownAddress))
+            {
+                // For now we're interested only in SimpleStaticKnownAddress
+                vnStore->AddToFieldAddressToFieldSeqMap(tree->AsIntCon()->gtVNPair.GetLiberal(),
+                                                        tree->AsIntCon()->gtFieldSeq);
+            }
             break;
 
 #ifdef FEATURE_SIMD
@@ -9947,17 +9947,14 @@ static bool GetStaticFieldSeqAndAddress(ValueNumStore* vnStore, GenTree* tree, s
 
     // Base address is expected to be static field's address
     ValueNum treeVN = tree->gtVNPair.GetLiberal();
-    if (tree->gtVNPair.BothEqual() && vnStore->IsVNHandle(treeVN) &&
-        (vnStore->GetHandleFlags(treeVN) == GTF_ICON_STATIC_HDL))
+    if (tree->gtVNPair.BothEqual() && vnStore->IsVNConstant(treeVN))
     {
         FieldSeq* fldSeq = vnStore->GetFieldSeqFromAddress(treeVN);
         if (fldSeq != nullptr)
         {
             assert(fldSeq->GetKind() == FieldSeq::FieldKind::SimpleStaticKnownAddress);
-            assert(fldSeq->GetOffset() == vnStore->CoercedConstantValue<ssize_t>(treeVN));
-
             *pFseq      = fldSeq;
-            *byteOffset = val;
+            *byteOffset = vnStore->CoercedConstantValue<ssize_t>(treeVN) - fldSeq->GetOffset() + val;
             return true;
         }
     }