Clean optVNConstantPropOnTree.
authorSergey Andreenko <seandree@microsoft.com>
Wed, 27 Mar 2019 00:16:07 +0000 (17:16 -0700)
committerSergey Andreenko <seandree@microsoft.com>
Tue, 9 Apr 2019 23:42:11 +0000 (16:42 -0700)
Create a new tree with const val and attach side-effects to it.
Extract common code and delete returns in the of the switch.

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

src/coreclr/src/jit/assertionprop.cpp

index 7c4a0c3..7b11b01 100644 (file)
@@ -2388,8 +2388,8 @@ GenTree* Compiler::optVNConstantPropOnTree(BasicBlock* block, GenTree* tree)
     }
 
     // We want to use the Normal ValueNumber when checking for constants.
-    ValueNum vnCns = vnStore->VNConservativeNormalValue(tree->gtVNPair);
-    ValueNum vnLib = vnStore->VNLiberalNormalValue(tree->gtVNPair);
+    ValueNumPair vnPair = tree->gtVNPair;
+    ValueNum     vnCns  = vnStore->VNConservativeNormalValue(vnPair);
 
     // Check if node evaluates to a constant.
     if (!vnStore->IsVNConstant(vnCns))
@@ -2397,7 +2397,7 @@ GenTree* Compiler::optVNConstantPropOnTree(BasicBlock* block, GenTree* tree)
         return nullptr;
     }
 
-    GenTree* newTree = tree;
+    GenTree* conValTree = nullptr;
     switch (vnStore->TypeOfVN(vnCns))
     {
         case TYP_FLOAT:
@@ -2407,20 +2407,13 @@ GenTree* Compiler::optVNConstantPropOnTree(BasicBlock* block, GenTree* tree)
             if (tree->TypeGet() == TYP_INT)
             {
                 // Same sized reinterpretation of bits to integer
-                newTree = optPrepareTreeForReplacement(tree, tree);
-                tree->ChangeOperConst(GT_CNS_INT);
-                tree->gtIntCon.gtIconVal = *(reinterpret_cast<int*>(&value));
-                tree->gtVNPair           = ValueNumPair(vnLib, vnCns);
+                conValTree = gtNewIconNode(*(reinterpret_cast<int*>(&value)));
             }
             else
             {
                 // Implicit assignment conversion to float or double
                 assert(varTypeIsFloating(tree->TypeGet()));
-
-                newTree = optPrepareTreeForReplacement(tree, tree);
-                tree->ChangeOperConst(GT_CNS_DBL);
-                tree->gtDblCon.gtDconVal = value;
-                tree->gtVNPair           = ValueNumPair(vnLib, vnCns);
+                conValTree = gtNewDconNode(value, tree->TypeGet());
             }
             break;
         }
@@ -2431,21 +2424,13 @@ GenTree* Compiler::optVNConstantPropOnTree(BasicBlock* block, GenTree* tree)
 
             if (tree->TypeGet() == TYP_LONG)
             {
-                // Same sized reinterpretation of bits to long
-                newTree = optPrepareTreeForReplacement(tree, tree);
-                tree->ChangeOperConst(GT_CNS_NATIVELONG);
-                tree->gtIntConCommon.SetLngValue(*(reinterpret_cast<INT64*>(&value)));
-                tree->gtVNPair = ValueNumPair(vnLib, vnCns);
+                conValTree = gtNewLconNode(*(reinterpret_cast<INT64*>(&value)));
             }
             else
             {
                 // Implicit assignment conversion to float or double
                 assert(varTypeIsFloating(tree->TypeGet()));
-
-                newTree = optPrepareTreeForReplacement(tree, tree);
-                tree->ChangeOperConst(GT_CNS_DBL);
-                tree->gtDblCon.gtDconVal = value;
-                tree->gtVNPair           = ValueNumPair(vnLib, vnCns);
+                conValTree = gtNewDconNode(value, tree->TypeGet());
             }
             break;
         }
@@ -2460,9 +2445,7 @@ GenTree* Compiler::optVNConstantPropOnTree(BasicBlock* block, GenTree* tree)
                 // to be recorded as a relocation with the VM.
                 if (!opts.compReloc)
                 {
-                    newTree           = gtNewIconHandleNode(value, vnStore->GetHandleFlags(vnCns));
-                    newTree->gtVNPair = ValueNumPair(vnLib, vnCns);
-                    newTree           = optPrepareTreeForReplacement(tree, newTree);
+                    conValTree = gtNewIconHandleNode(value, vnStore->GetHandleFlags(vnCns));
                 }
             }
             else
@@ -2472,18 +2455,12 @@ GenTree* Compiler::optVNConstantPropOnTree(BasicBlock* block, GenTree* tree)
                 {
                     case TYP_INT:
                         // Implicit assignment conversion to smaller integer
-                        newTree = optPrepareTreeForReplacement(tree, tree);
-                        tree->ChangeOperConst(GT_CNS_INT);
-                        tree->gtIntCon.gtIconVal = (int)value;
-                        tree->gtVNPair           = ValueNumPair(vnLib, vnCns);
+                        conValTree = gtNewIconNode(static_cast<int>(value));
                         break;
 
                     case TYP_LONG:
                         // Same type no conversion required
-                        newTree = optPrepareTreeForReplacement(tree, tree);
-                        tree->ChangeOperConst(GT_CNS_NATIVELONG);
-                        tree->gtIntConCommon.SetLngValue(value);
-                        tree->gtVNPair = ValueNumPair(vnLib, vnCns);
+                        conValTree = gtNewLconNode(value);
                         break;
 
                     case TYP_FLOAT:
@@ -2494,32 +2471,30 @@ GenTree* Compiler::optVNConstantPropOnTree(BasicBlock* block, GenTree* tree)
 
                     case TYP_DOUBLE:
                         // Same sized reinterpretation of bits to double
-                        newTree = optPrepareTreeForReplacement(tree, tree);
-                        tree->ChangeOperConst(GT_CNS_DBL);
-                        tree->gtDblCon.gtDconVal = *(reinterpret_cast<double*>(&value));
-                        tree->gtVNPair           = ValueNumPair(vnLib, vnCns);
+                        conValTree = gtNewDconNode(*(reinterpret_cast<double*>(&value)));
                         break;
 
                     default:
-                        return nullptr;
+                        // Can't optimize.
+                        break;
                 }
             }
         }
         break;
 
         case TYP_REF:
-            if (tree->TypeGet() != TYP_REF)
+        {
+            if (tree->TypeGet() != TYP_REF) // TODO seandree: what are other cases here?
             {
-                return nullptr;
+                // Can't optimize.
+                break;
             }
 
             assert(vnStore->ConstantValue<size_t>(vnCns) == 0);
-            newTree = optPrepareTreeForReplacement(tree, tree);
-            tree->ChangeOperConst(GT_CNS_INT);
-            tree->gtIntCon.gtIconVal = 0;
-            tree->ClearIconHandleMask();
-            tree->gtVNPair = ValueNumPair(vnLib, vnCns);
-            break;
+
+            conValTree = gtNewIconNode(0, TYP_REF);
+        }
+        break;
 
         case TYP_INT:
         {
@@ -2531,9 +2506,7 @@ GenTree* Compiler::optVNConstantPropOnTree(BasicBlock* block, GenTree* tree)
                 // to be recorded as a relocation with the VM.
                 if (!opts.compReloc)
                 {
-                    newTree           = gtNewIconHandleNode(value, vnStore->GetHandleFlags(vnCns));
-                    newTree->gtVNPair = ValueNumPair(vnLib, vnCns);
-                    newTree           = optPrepareTreeForReplacement(tree, newTree);
+                    conValTree = gtNewIconHandleNode(value, vnStore->GetHandleFlags(vnCns));
                 }
             }
             else
@@ -2544,27 +2517,17 @@ GenTree* Compiler::optVNConstantPropOnTree(BasicBlock* block, GenTree* tree)
                     case TYP_REF:
                     case TYP_INT:
                         // Same type no conversion required
-                        newTree = optPrepareTreeForReplacement(tree, tree);
-                        tree->ChangeOperConst(GT_CNS_INT);
-                        tree->gtIntCon.gtIconVal = value;
-                        tree->ClearIconHandleMask();
-                        tree->gtVNPair = ValueNumPair(vnLib, vnCns);
+                        conValTree = gtNewIconNode(static_cast<int>(value));
                         break;
 
                     case TYP_LONG:
                         // Implicit assignment conversion to larger integer
-                        newTree = optPrepareTreeForReplacement(tree, tree);
-                        tree->ChangeOperConst(GT_CNS_NATIVELONG);
-                        tree->gtIntConCommon.SetLngValue(value);
-                        tree->gtVNPair = ValueNumPair(vnLib, vnCns);
+                        conValTree = gtNewLconNode(static_cast<int>(value));
                         break;
 
                     case TYP_FLOAT:
                         // Same sized reinterpretation of bits to float
-                        newTree = optPrepareTreeForReplacement(tree, tree);
-                        tree->ChangeOperConst(GT_CNS_DBL);
-                        tree->gtDblCon.gtDconVal = *(reinterpret_cast<float*>(&value));
-                        tree->gtVNPair           = ValueNumPair(vnLib, vnCns);
+                        conValTree = gtNewDconNode(*(reinterpret_cast<float*>(&value)), TYP_FLOAT);
                         break;
 
                     case TYP_DOUBLE:
@@ -2574,16 +2537,29 @@ GenTree* Compiler::optVNConstantPropOnTree(BasicBlock* block, GenTree* tree)
                         break;
 
                     default:
-                        return nullptr;
+                        // Can't optimize.
+                        break;
                 }
             }
         }
         break;
 
         default:
-            return nullptr;
+            // Can't optimize.
+            break;
+    }
+
+    if (conValTree != nullptr)
+    {
+        // Were able to optimize, replace as COMMA(side_effects, const value tree);
+        conValTree->gtVNPair = vnPair;
+        return optPrepareTreeForReplacement(tree, conValTree);
+    }
+    else
+    {
+        // Was not able to optimize.
+        return nullptr;
     }
-    return newTree;
 }
 
 /*******************************************************************************************************