aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/Transforms/Utils/Local.cpp
diff options
context:
space:
mode:
authorhanbeom <kese111@gmail.com>2024-03-06 17:42:33 +0900
committerGitHub <noreply@github.com>2024-03-06 09:42:33 +0100
commit6cdf596c52f028ea7d150e0696f967fbff443ccf (patch)
tree37c1d71add50b4c71408fda1e26c28d49a4c66e5 /llvm/lib/Transforms/Utils/Local.cpp
parent9f45c5e1a65a1abf4920b617d36ed05e73c04bea (diff)
downloadllvm-6cdf596c52f028ea7d150e0696f967fbff443ccf.zip
llvm-6cdf596c52f028ea7d150e0696f967fbff443ccf.tar.gz
llvm-6cdf596c52f028ea7d150e0696f967fbff443ccf.tar.bz2
[InstCombine] If inst in unreachable refers to an inst change it to poison (#78444)
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. Fixes #65107.
Diffstat (limited to 'llvm/lib/Transforms/Utils/Local.cpp')
-rw-r--r--llvm/lib/Transforms/Utils/Local.cpp22
1 files changed, 20 insertions, 2 deletions
diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp
index c4a8843..d3bb8907 100644
--- a/llvm/lib/Transforms/Utils/Local.cpp
+++ b/llvm/lib/Transforms/Utils/Local.cpp
@@ -2762,6 +2762,23 @@ bool llvm::replaceAllDbgUsesWith(Instruction &From, Value &To,
return false;
}
+bool llvm::handleUnreachableTerminator(
+ Instruction *I, SmallVectorImpl<Value *> &PoisonedValues) {
+ bool Changed = false;
+ // RemoveDIs: erase debug-info on this instruction manually.
+ I->dropDbgValues();
+ for (Use &U : I->operands()) {
+ Value *Op = U.get();
+ if (isa<Instruction>(Op) && !Op->getType()->isTokenTy()) {
+ U.set(PoisonValue::get(Op->getType()));
+ PoisonedValues.push_back(Op);
+ Changed = true;
+ }
+ }
+
+ return Changed;
+}
+
std::pair<unsigned, unsigned>
llvm::removeAllNonTerminatorAndEHPadInstructions(BasicBlock *BB) {
unsigned NumDeadInst = 0;
@@ -2769,8 +2786,9 @@ llvm::removeAllNonTerminatorAndEHPadInstructions(BasicBlock *BB) {
// Delete the instructions backwards, as it has a reduced likelihood of
// having to update as many def-use and use-def chains.
Instruction *EndInst = BB->getTerminator(); // Last not to be deleted.
- // RemoveDIs: erasing debug-info must be done manually.
- EndInst->dropDbgValues();
+ SmallVector<Value *> Uses;
+ handleUnreachableTerminator(EndInst, Uses);
+
while (EndInst != &BB->front()) {
// Delete the next to last instruction.
Instruction *Inst = &*--EndInst->getIterator();