[llvm] aa5adc0 - [SimplifyCFG] Fix if conversion with opaque pointers

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Jul 21 13:24:36 PDT 2021


Author: Nikita Popov
Date: 2021-07-21T22:24:07+02:00
New Revision: aa5adc0c1cd011c4861b609c0cf0db3221289810

URL: https://github.com/llvm/llvm-project/commit/aa5adc0c1cd011c4861b609c0cf0db3221289810
DIFF: https://github.com/llvm/llvm-project/commit/aa5adc0c1cd011c4861b609c0cf0db3221289810.diff

LOG: [SimplifyCFG] Fix if conversion with opaque pointers

We need to make sure that the value types are the same. Otherwise
we both may not have the necessary dereferenceability implication,
nor can we directly form the desired select pattern.

Without opaque pointers this is enforced implicitly through the
pointer comparison.

Added: 
    

Modified: 
    llvm/lib/Transforms/Utils/SimplifyCFG.cpp
    llvm/test/Transforms/SimplifyCFG/speculate-store.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index c0a31e3bdca9..04983ac39d05 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -2228,6 +2228,7 @@ static Value *isSafeToSpeculateStore(Instruction *I, BasicBlock *BrBB,
     return nullptr;
 
   Value *StorePtr = StoreToHoist->getPointerOperand();
+  Type *StoreTy = StoreToHoist->getValueOperand()->getType();
 
   // Look for a store to the same pointer in BrBB.
   unsigned MaxNumInstToLookAt = 9;
@@ -2244,7 +2245,8 @@ static Value *isSafeToSpeculateStore(Instruction *I, BasicBlock *BrBB,
 
     if (auto *SI = dyn_cast<StoreInst>(&CurI)) {
       // Found the previous store make sure it stores to the same location.
-      if (SI->getPointerOperand() == StorePtr)
+      if (SI->getPointerOperand() == StorePtr &&
+          SI->getValueOperand()->getType() == StoreTy)
         // Found the previous store, return its value operand.
         return SI->getValueOperand();
       return nullptr; // Unknown store.

diff  --git a/llvm/test/Transforms/SimplifyCFG/speculate-store.ll b/llvm/test/Transforms/SimplifyCFG/speculate-store.ll
index 27c9ed5f2e77..6d08d3cd32fe 100644
--- a/llvm/test/Transforms/SimplifyCFG/speculate-store.ll
+++ b/llvm/test/Transforms/SimplifyCFG/speculate-store.ll
@@ -112,6 +112,27 @@ ret.end:
   ret void
 }
 
+define void @
diff erent_type(ptr %ptr, i1 %cmp) {
+; CHECK-LABEL: @
diff erent_type(
+; CHECK-NEXT:    store i32 0, ptr [[PTR:%.*]], align 4
+; CHECK-NEXT:    br i1 [[CMP:%.*]], label [[IF_THEN:%.*]], label [[RET_END:%.*]]
+; CHECK:       if.then:
+; CHECK-NEXT:    store i64 1, ptr [[PTR]], align 4
+; CHECK-NEXT:    br label [[RET_END]]
+; CHECK:       ret.end:
+; CHECK-NEXT:    ret void
+;
+  store i32 0, ptr %ptr
+  br i1 %cmp, label %if.then, label %ret.end
+
+if.then:
+  store i64 1, ptr %ptr
+  br label %ret.end
+
+ret.end:
+  ret void
+}
+
 ; CHECK: !0 = !{!"branch_weights", i32 3, i32 5}
 !0 = !{!"branch_weights", i32 3, i32 5}
 


        


More information about the llvm-commits mailing list