[llvm] [InstCombine] Fix Issue 65107 (PR #78444)

via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 19 13:47:00 PST 2024


https://github.com/ParkHanbum updated https://github.com/llvm/llvm-project/pull/78444

>From 89c6fc50296e1b82b9f15143c89d867e82e32d32 Mon Sep 17 00:00:00 2001
From: Hanbum Park <kese111 at gmail.com>
Date: Sat, 20 Jan 2024 04:22:57 +0900
Subject: [PATCH] [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.
---
 llvm/lib/Transforms/Utils/Local.cpp                     | 5 +++++
 llvm/test/Transforms/InstCombine/phi-select-constant.ll | 3 +--
 2 files changed, 6 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp
index c76cc9db16d7e7..a64c9449cb0ef6 100644
--- a/llvm/lib/Transforms/Utils/Local.cpp
+++ b/llvm/lib/Transforms/Utils/Local.cpp
@@ -2779,6 +2779,11 @@ llvm::removeAllNonTerminatorAndEHPadInstructions(BasicBlock *BB) {
   Instruction *EndInst = BB->getTerminator(); // Last not to be deleted.
   // RemoveDIs: erasing debug-info must be done manually.
   EndInst->dropDbgValues();
+
+  if (isa<ReturnInst>(EndInst) && EndInst->getNumOperands() > 0 &&
+      isa<Instruction>(EndInst->getOperand(0)))
+    EndInst->setOperand(0, PoisonValue::get(EndInst->getOperand(0)->getType()));
+
   while (EndInst != &BB->front()) {
     // Delete the next to last instruction.
     Instruction *Inst = &*--EndInst->getIterator();
diff --git a/llvm/test/Transforms/InstCombine/phi-select-constant.ll b/llvm/test/Transforms/InstCombine/phi-select-constant.ll
index 1260ef47f65ef4..601a3d208a0841 100644
--- a/llvm/test/Transforms/InstCombine/phi-select-constant.ll
+++ b/llvm/test/Transforms/InstCombine/phi-select-constant.ll
@@ -140,12 +140,11 @@ end:
 define i16 @sink_to_unreachable_crash(i1 %a)  {
 ; CHECK-LABEL: @sink_to_unreachable_crash(
 ; CHECK-NEXT:  entry:
-; CHECK-NEXT:    [[S:%.*]] = select i1 [[A:%.*]], i16 0, i16 5
 ; CHECK-NEXT:    br label [[INF_LOOP:%.*]]
 ; CHECK:       inf_loop:
 ; CHECK-NEXT:    br label [[INF_LOOP]]
 ; CHECK:       unreachable:
-; CHECK-NEXT:    ret i16 [[S]]
+; CHECK-NEXT:    ret i16 poison
 ;
 entry:
   %s = select i1 %a, i16 0, i16 5



More information about the llvm-commits mailing list