[SCCP] Get a copy of the state of CopyOf once.
authorFlorian Hahn <flo@fhahn.com>
Fri, 1 May 2020 13:43:15 +0000 (14:43 +0100)
committerFlorian Hahn <flo@fhahn.com>
Fri, 1 May 2020 13:46:35 +0000 (14:46 +0100)
This fixes potential reference invalidations, when no lattice value is
assigned for CopyOf. As the state of CopyOf won't change while in
handleCallResult, we can get a copy once and use that.

Should fix PR45749.

llvm/lib/Transforms/Scalar/SCCP.cpp

index ba97ed4..e5fd616 100644 (file)
@@ -1223,22 +1223,23 @@ void SCCPSolver::handleCallResult(CallBase &CB) {
       Value *CopyOf = CB.getOperand(0);
       auto *PI = getPredicateInfoFor(&CB);
       auto *PBranch = dyn_cast_or_null<PredicateBranch>(PI);
+      ValueLatticeElement OriginalVal = getValueState(CopyOf);
       if (!PI || !PBranch) {
-        mergeInValue(ValueState[&CB], &CB, getValueState(CopyOf));
+        mergeInValue(ValueState[&CB], &CB, OriginalVal);
         return;
       }
 
       // Everything below relies on the condition being a comparison.
       auto *Cmp = dyn_cast<CmpInst>(PBranch->Condition);
       if (!Cmp) {
-        mergeInValue(ValueState[&CB], &CB, getValueState(CopyOf));
+        mergeInValue(ValueState[&CB], &CB, OriginalVal);
         return;
       }
 
       Value *CmpOp0 = Cmp->getOperand(0);
       Value *CmpOp1 = Cmp->getOperand(1);
       if (CopyOf != CmpOp0 && CopyOf != CmpOp1) {
-        mergeInValue(ValueState[&CB], &CB, getValueState(CopyOf));
+        mergeInValue(ValueState[&CB], &CB, OriginalVal);
         return;
       }
 
@@ -1259,7 +1260,6 @@ void SCCPSolver::handleCallResult(CallBase &CB) {
 
       ValueLatticeElement CondVal = getValueState(CmpOp1);
       ValueLatticeElement &IV = ValueState[&CB];
-      ValueLatticeElement OriginalVal = getValueState(CopyOf);
       if (CondVal.isConstantRange() || OriginalVal.isConstantRange()) {
         auto NewCR =
             ConstantRange::getFull(DL.getTypeSizeInBits(CopyOf->getType()));
@@ -1299,7 +1299,7 @@ void SCCPSolver::handleCallResult(CallBase &CB) {
         return;
       }
 
-      return (void)mergeInValue(IV, &CB, getValueState(CopyOf));
+      return (void)mergeInValue(IV, &CB, OriginalVal);
     }
   }