[llvm] ca18e21 - [InstCombine] Remove instructions in dead blocks during combining
Nikita Popov via llvm-commits
llvm-commits at lists.llvm.org
Fri May 26 07:43:44 PDT 2023
Author: Nikita Popov
Date: 2023-05-26T16:43:34+02:00
New Revision: ca18e21951a89f3115694fc64cd64a4b06cd5873
URL: https://github.com/llvm/llvm-project/commit/ca18e21951a89f3115694fc64cd64a4b06cd5873
DIFF: https://github.com/llvm/llvm-project/commit/ca18e21951a89f3115694fc64cd64a4b06cd5873.diff
LOG: [InstCombine] Remove instructions in dead blocks during combining
We already do this during initial worklist population. Doing this
as part of primary combining allows us to remove instructions in
blocks that were rendered dead by condition folding within the
same instcombine iteration.
Added:
Modified:
llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
llvm/test/Transforms/InstCombine/unreachable-code.ll
Removed:
################################################################################
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index 0df85be126bc..2af6ba5c8182 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -2550,6 +2550,41 @@ Instruction *InstCombinerImpl::visitUnconditionalBranchInst(BranchInst &BI) {
return nullptr;
}
+/// If a block is dead due to a known branch condition, remove instructions
+/// in it.
+static bool handlePotentiallyDeadBlock(BasicBlock *BB, InstCombiner &IC) {
+ // We only know one edge to this block is dead, but there may be others.
+ // TODO: We could track dead edges globally.
+ if (!BB->getSinglePredecessor())
+ return false;
+
+ bool Changed = false;
+ for (Instruction &Inst : make_early_inc_range(make_range(
+ std::next(BB->getTerminator()->getReverseIterator()), BB->rend()))) {
+ if (!Inst.use_empty() && !Inst.getType()->isTokenTy()) {
+ IC.replaceInstUsesWith(Inst, PoisonValue::get(Inst.getType()));
+ Changed = true;
+ }
+ if (Inst.isEHPad() || Inst.getType()->isTokenTy())
+ continue;
+ IC.eraseInstFromFunction(Inst);
+ Changed = true;
+ }
+
+ // TODO: Successor blocks may also be dead.
+ return Changed;
+}
+
+static bool handlePotentiallyDeadSuccessors(BasicBlock *BB,
+ BasicBlock *LiveSucc,
+ InstCombiner &IC) {
+ bool Changed = false;
+ for (BasicBlock *Succ : successors(BB))
+ if (Succ != LiveSucc)
+ Changed |= handlePotentiallyDeadBlock(Succ, IC);
+ return Changed;
+}
+
Instruction *InstCombinerImpl::visitBranchInst(BranchInst &BI) {
if (BI.isUnconditional())
return visitUnconditionalBranchInst(BI);
@@ -2593,6 +2628,14 @@ Instruction *InstCombinerImpl::visitBranchInst(BranchInst &BI) {
return &BI;
}
+ if (isa<UndefValue>(Cond) && handlePotentiallyDeadSuccessors(
+ BI.getParent(), /*LiveSucc*/ nullptr, *this))
+ return &BI;
+ if (auto *CI = dyn_cast<ConstantInt>(Cond))
+ if (handlePotentiallyDeadSuccessors(
+ BI.getParent(), BI.getSuccessor(!CI->getZExtValue()), *this))
+ return &BI;
+
return nullptr;
}
@@ -2611,6 +2654,14 @@ Instruction *InstCombinerImpl::visitSwitchInst(SwitchInst &SI) {
return replaceOperand(SI, 0, Op0);
}
+ if (isa<UndefValue>(Cond) && handlePotentiallyDeadSuccessors(
+ SI.getParent(), /*LiveSucc*/ nullptr, *this))
+ return &SI;
+ if (auto *CI = dyn_cast<ConstantInt>(Cond))
+ if (handlePotentiallyDeadSuccessors(
+ SI.getParent(), SI.findCaseValue(CI)->getCaseSuccessor(), *this))
+ return &SI;
+
KnownBits Known = computeKnownBits(Cond, 0, &SI);
unsigned LeadingKnownZeros = Known.countMinLeadingZeros();
unsigned LeadingKnownOnes = Known.countMinLeadingOnes();
diff --git a/llvm/test/Transforms/InstCombine/unreachable-code.ll b/llvm/test/Transforms/InstCombine/unreachable-code.ll
index 4a85d088d20d..7f17dfa537be 100644
--- a/llvm/test/Transforms/InstCombine/unreachable-code.ll
+++ b/llvm/test/Transforms/InstCombine/unreachable-code.ll
@@ -1,5 +1,5 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 2
-; RUN: opt -S -passes=instcombine < %s | FileCheck %s
+; RUN: opt -S -passes=instcombine -instcombine-infinite-loop-threshold=2 < %s | FileCheck %s
declare void @dummy()
More information about the llvm-commits
mailing list