Make forward sub a little bit more precise in GLOB_REF tagging (#75404)
authorSingleAccretion <62474226+SingleAccretion@users.noreply.github.com>
Mon, 12 Sep 2022 18:53:01 +0000 (21:53 +0300)
committerGitHub <noreply@github.com>
Mon, 12 Sep 2022 18:53:01 +0000 (11:53 -0700)
* Make forward sub more precise in GLOB_REF tagging

Do not set it right away when encountering LHSs.
Since LHSs come first in canonical execution order,
this unblocks some propagations into uses on the RHS.

* Revise to not affect the IR state

src/coreclr/jit/forwardsub.cpp

index f3e4cac..9271941 100644 (file)
@@ -207,7 +207,9 @@ public:
     Compiler::fgWalkResult PostOrderVisit(GenTree** use, GenTree* user)
     {
         m_treeSize++;
-        GenTree* const node = *use;
+
+        GenTree* const node  = *use;
+        bool const     isDef = (user != nullptr) && user->OperIs(GT_ASG) && (user->gtGetOp1() == node);
 
         if (node->OperIs(GT_LCL_VAR))
         {
@@ -220,7 +222,6 @@ public:
                 // Screen out contextual "uses"
                 //
                 GenTree* const parent = user;
-                bool const     isDef  = parent->OperIs(GT_ASG) && (parent->gtGetOp1() == node);
                 bool const     isAddr = parent->OperIs(GT_ADDR);
 
                 bool isCallTarget = false;
@@ -245,18 +246,21 @@ public:
             }
         }
 
-        if (node->OperIsLocal())
+        // Stores to and uses of address-exposed locals are modelled as global refs.
+        //
+        GenTree* lclNode = nullptr;
+        if (node->OperIsLocal() && !isDef)
         {
-            unsigned const lclNum = node->AsLclVarCommon()->GetLclNum();
-
-            // Uses of address-exposed locals are modelled as global refs.
-            //
-            LclVarDsc* const varDsc = m_compiler->lvaGetDesc(lclNum);
+            lclNode = node;
+        }
+        else if (node->OperIs(GT_ASG) && node->gtGetOp1()->OperIsLocal())
+        {
+            lclNode = node->gtGetOp1();
+        }
 
-            if (varDsc->IsAddressExposed())
-            {
-                m_accumulatedFlags |= GTF_GLOB_REF;
-            }
+        if ((lclNode != nullptr) && m_compiler->lvaGetDesc(lclNode->AsLclVarCommon())->IsAddressExposed())
+        {
+            m_accumulatedFlags |= GTF_GLOB_REF;
         }
 
         m_accumulatedFlags |= (node->gtFlags & GTF_GLOB_EFFECT);