Skip to content

Commit aa5adc0

Browse files
committed
[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.
1 parent a8f1ec5 commit aa5adc0

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2228,6 +2228,7 @@ static Value *isSafeToSpeculateStore(Instruction *I, BasicBlock *BrBB,
22282228
return nullptr;
22292229

22302230
Value *StorePtr = StoreToHoist->getPointerOperand();
2231+
Type *StoreTy = StoreToHoist->getValueOperand()->getType();
22312232

22322233
// Look for a store to the same pointer in BrBB.
22332234
unsigned MaxNumInstToLookAt = 9;
@@ -2244,7 +2245,8 @@ static Value *isSafeToSpeculateStore(Instruction *I, BasicBlock *BrBB,
22442245

22452246
if (auto *SI = dyn_cast<StoreInst>(&CurI)) {
22462247
// Found the previous store make sure it stores to the same location.
2247-
if (SI->getPointerOperand() == StorePtr)
2248+
if (SI->getPointerOperand() == StorePtr &&
2249+
SI->getValueOperand()->getType() == StoreTy)
22482250
// Found the previous store, return its value operand.
22492251
return SI->getValueOperand();
22502252
return nullptr; // Unknown store.

llvm/test/Transforms/SimplifyCFG/speculate-store.ll

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,27 @@ ret.end:
112112
ret void
113113
}
114114

115+
define void @different_type(ptr %ptr, i1 %cmp) {
116+
; CHECK-LABEL: @different_type(
117+
; CHECK-NEXT: store i32 0, ptr [[PTR:%.*]], align 4
118+
; CHECK-NEXT: br i1 [[CMP:%.*]], label [[IF_THEN:%.*]], label [[RET_END:%.*]]
119+
; CHECK: if.then:
120+
; CHECK-NEXT: store i64 1, ptr [[PTR]], align 4
121+
; CHECK-NEXT: br label [[RET_END]]
122+
; CHECK: ret.end:
123+
; CHECK-NEXT: ret void
124+
;
125+
store i32 0, ptr %ptr
126+
br i1 %cmp, label %if.then, label %ret.end
127+
128+
if.then:
129+
store i64 1, ptr %ptr
130+
br label %ret.end
131+
132+
ret.end:
133+
ret void
134+
}
135+
115136
; CHECK: !0 = !{!"branch_weights", i32 3, i32 5}
116137
!0 = !{!"branch_weights", i32 3, i32 5}
117138

0 commit comments

Comments
 (0)