Skip to content

Commit 73d9974

Browse files
authored
[InstCombine] Avoid self-replacing in getUndefReplacement (llvm#161500)
Self-replacing has a different meaning in InstCombine. It will replace all uses with poison. Closes llvm#161492.
1 parent c090548 commit 73d9974

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

llvm/lib/Transforms/InstCombine/InstructionCombining.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5212,7 +5212,7 @@ Instruction *InstCombinerImpl::visitFreeze(FreezeInst &I) {
52125212
else if (match(U, m_Select(m_Specific(&I), m_Constant(), m_Value())))
52135213
V = ConstantInt::getTrue(Ty);
52145214
else if (match(U, m_c_Select(m_Specific(&I), m_Value(V)))) {
5215-
if (!isGuaranteedNotToBeUndefOrPoison(V, &AC, &I, &DT))
5215+
if (V == &I || !isGuaranteedNotToBeUndefOrPoison(V, &AC, &I, &DT))
52165216
V = NullValue;
52175217
} else if (auto *PHI = dyn_cast<PHINode>(U)) {
52185218
if (Value *MaybeV = pickCommonConstantFromPHI(*PHI))
@@ -5225,6 +5225,7 @@ Instruction *InstCombinerImpl::visitFreeze(FreezeInst &I) {
52255225
BestValue = NullValue;
52265226
}
52275227
assert(BestValue && "Must have at least one use");
5228+
assert(BestValue != &I && "Cannot replace with itself");
52285229
return BestValue;
52295230
};
52305231

llvm/test/Transforms/InstCombine/freeze.ll

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1464,6 +1464,27 @@ define ptr @freeze_ptrmask_nonnull(ptr %p, i64 noundef %m) {
14641464
ret ptr %fr
14651465
}
14661466

1467+
define i64 @pr161492_1(i1 %cond) {
1468+
; CHECK-LABEL: define i64 @pr161492_1(
1469+
; CHECK-SAME: i1 [[COND:%.*]]) {
1470+
; CHECK-NEXT: ret i64 0
1471+
;
1472+
%fr1 = freeze i64 poison
1473+
%fr2 = freeze i64 poison
1474+
%ret = select i1 %cond, i64 %fr1, i64 %fr2
1475+
ret i64 %ret
1476+
}
1477+
1478+
define i64 @pr161492_2(i1 %cond) {
1479+
; CHECK-LABEL: define i64 @pr161492_2(
1480+
; CHECK-SAME: i1 [[COND:%.*]]) {
1481+
; CHECK-NEXT: ret i64 0
1482+
;
1483+
%fr = freeze i64 poison
1484+
%ret = select i1 %cond, i64 %fr, i64 %fr
1485+
ret i64 %ret
1486+
}
1487+
14671488
!0 = !{}
14681489
!1 = !{i64 4}
14691490
!2 = !{i32 0, i32 100}

0 commit comments

Comments
 (0)