Skip to content

Commit 89c6fc5

Browse files
committed
[InstCombine] If return-inst in unreachable refers to an inst change it to poison (#65107)
Instructions in unreachable basic blocks are removed, but terminators are not. In this case, even instructions that are only referenced by a terminator, such as a return instruction, cannot be processed properly. This patch changes the operand of a return instruction in an unreachable basic block to poison if it refers to the instruction, allowing the instruction to be properly processed.
1 parent a9f39ff commit 89c6fc5

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

llvm/lib/Transforms/Utils/Local.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2779,6 +2779,11 @@ llvm::removeAllNonTerminatorAndEHPadInstructions(BasicBlock *BB) {
27792779
Instruction *EndInst = BB->getTerminator(); // Last not to be deleted.
27802780
// RemoveDIs: erasing debug-info must be done manually.
27812781
EndInst->dropDbgValues();
2782+
2783+
if (isa<ReturnInst>(EndInst) && EndInst->getNumOperands() > 0 &&
2784+
isa<Instruction>(EndInst->getOperand(0)))
2785+
EndInst->setOperand(0, PoisonValue::get(EndInst->getOperand(0)->getType()));
2786+
27822787
while (EndInst != &BB->front()) {
27832788
// Delete the next to last instruction.
27842789
Instruction *Inst = &*--EndInst->getIterator();

llvm/test/Transforms/InstCombine/phi-select-constant.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,11 @@ end:
140140
define i16 @sink_to_unreachable_crash(i1 %a) {
141141
; CHECK-LABEL: @sink_to_unreachable_crash(
142142
; CHECK-NEXT: entry:
143-
; CHECK-NEXT: [[S:%.*]] = select i1 [[A:%.*]], i16 0, i16 5
144143
; CHECK-NEXT: br label [[INF_LOOP:%.*]]
145144
; CHECK: inf_loop:
146145
; CHECK-NEXT: br label [[INF_LOOP]]
147146
; CHECK: unreachable:
148-
; CHECK-NEXT: ret i16 [[S]]
147+
; CHECK-NEXT: ret i16 poison
149148
;
150149
entry:
151150
%s = select i1 %a, i16 0, i16 5

0 commit comments

Comments
 (0)