[llvm] 10451de - [InstCombine] Remove code after non-terminator unreachable

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 22 07:00:21 PDT 2023


Author: Nikita Popov
Date: 2023-06-22T15:59:10+02:00
New Revision: 10451ded6d7ef799569652dfce81653d37f167b5

URL: https://github.com/llvm/llvm-project/commit/10451ded6d7ef799569652dfce81653d37f167b5
DIFF: https://github.com/llvm/llvm-project/commit/10451ded6d7ef799569652dfce81653d37f167b5.diff

LOG: [InstCombine] Remove code after non-terminator unreachable

Instruction after a non-terminator unreachable are ... unreachable,
so remove them. Reuse the same logic we use for removing
instructions from dead blocks.

Added: 
    

Modified: 
    llvm/lib/Transforms/InstCombine/InstCombineInternal.h
    llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
    llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
    llvm/test/Transforms/InstCombine/getelementptr.ll
    llvm/test/Transforms/InstCombine/pr44245.ll
    llvm/test/Transforms/InstCombine/unreachable-code.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
index 88fbca2b13392..27ac86c3641f2 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
+++ b/llvm/lib/Transforms/InstCombine/InstCombineInternal.h
@@ -393,12 +393,12 @@ class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
   /// without having to rewrite the CFG from within InstCombine.
   void CreateNonTerminatorUnreachable(Instruction *InsertAt) {
     auto &Ctx = InsertAt->getContext();
-    new StoreInst(ConstantInt::getTrue(Ctx),
-                  PoisonValue::get(Type::getInt1PtrTy(Ctx)),
-                  InsertAt);
+    auto *SI = new StoreInst(ConstantInt::getTrue(Ctx),
+                             PoisonValue::get(Type::getInt1PtrTy(Ctx)),
+                             /*isVolatile*/ false, Align(1));
+    InsertNewInstBefore(SI, *InsertAt);
   }
 
-
   /// Combiner aware instruction erasure.
   ///
   /// When dealing with an instruction that has side effects or produces a void
@@ -660,6 +660,9 @@ class LLVM_LIBRARY_VISIBILITY InstCombinerImpl final
   Value *EvaluateInDifferentType(Value *V, Type *Ty, bool isSigned);
 
   bool tryToSinkInstruction(Instruction *I, BasicBlock *DestBlock);
+
+  bool handleUnreachableFrom(Instruction *I);
+  bool handlePotentiallyDeadSuccessors(BasicBlock *BB, BasicBlock *LiveSucc);
 };
 
 class Negator final {

diff  --git a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
index 1843c121850b6..95472fc6e1ba6 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
@@ -1560,8 +1560,12 @@ Instruction *InstCombinerImpl::visitStoreInst(StoreInst &SI) {
   }
 
   // This is a non-terminator unreachable marker. Don't remove it.
-  if (isa<UndefValue>(Ptr))
+  if (isa<UndefValue>(Ptr)) {
+    // Remove unreachable instructions after the marker.
+    if (handleUnreachableFrom(SI.getNextNode()))
+      return &SI;
     return nullptr;
+  }
 
   // store undef, Ptr -> noop
   // FIXME: This is technically incorrect because it might overwrite a poison

diff  --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index d076a1fcf028f..9b40623547062 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -2667,24 +2667,21 @@ 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;
-
+// Under the assumption that I is unreachable, remove it and following
+// instructions.
+bool InstCombinerImpl::handleUnreachableFrom(Instruction *I) {
   bool Changed = false;
-  for (Instruction &Inst : make_early_inc_range(make_range(
-           std::next(BB->getTerminator()->getReverseIterator()), BB->rend()))) {
+  BasicBlock *BB = I->getParent();
+  for (Instruction &Inst : make_early_inc_range(
+           make_range(std::next(BB->getTerminator()->getReverseIterator()),
+                      std::next(I->getReverseIterator())))) {
     if (!Inst.use_empty() && !Inst.getType()->isTokenTy()) {
-      IC.replaceInstUsesWith(Inst, PoisonValue::get(Inst.getType()));
+      replaceInstUsesWith(Inst, PoisonValue::get(Inst.getType()));
       Changed = true;
     }
     if (Inst.isEHPad() || Inst.getType()->isTokenTy())
       continue;
-    IC.eraseInstFromFunction(Inst);
+    eraseInstFromFunction(Inst);
     Changed = true;
   }
 
@@ -2693,8 +2690,8 @@ static bool handlePotentiallyDeadBlock(BasicBlock *BB, InstCombiner &IC) {
     for (PHINode &PN : Succ->phis())
       for (Use &U : PN.incoming_values())
         if (PN.getIncomingBlock(U) == BB && !isa<PoisonValue>(U)) {
-          IC.replaceUse(U, PoisonValue::get(PN.getType()));
-          IC.addToWorklist(&PN);
+          replaceUse(U, PoisonValue::get(PN.getType()));
+          addToWorklist(&PN);
           Changed = true;
         }
 
@@ -2702,13 +2699,12 @@ static bool handlePotentiallyDeadBlock(BasicBlock *BB, InstCombiner &IC) {
   return Changed;
 }
 
-static bool handlePotentiallyDeadSuccessors(BasicBlock *BB,
-                                            BasicBlock *LiveSucc,
-                                            InstCombiner &IC) {
+bool InstCombinerImpl::handlePotentiallyDeadSuccessors(BasicBlock *BB,
+                                                       BasicBlock *LiveSucc) {
   bool Changed = false;
   for (BasicBlock *Succ : successors(BB))
-    if (Succ != LiveSucc)
-      Changed |= handlePotentiallyDeadBlock(Succ, IC);
+    if (Succ != LiveSucc && Succ->getSinglePredecessor())
+      Changed |= handleUnreachableFrom(&Succ->front());
   return Changed;
 }
 
@@ -2755,12 +2751,12 @@ Instruction *InstCombinerImpl::visitBranchInst(BranchInst &BI) {
     return &BI;
   }
 
-  if (isa<UndefValue>(Cond) && handlePotentiallyDeadSuccessors(
-                                   BI.getParent(), /*LiveSucc*/ nullptr, *this))
+  if (isa<UndefValue>(Cond) &&
+      handlePotentiallyDeadSuccessors(BI.getParent(), /*LiveSucc*/ nullptr))
     return &BI;
   if (auto *CI = dyn_cast<ConstantInt>(Cond))
-    if (handlePotentiallyDeadSuccessors(
-            BI.getParent(), BI.getSuccessor(!CI->getZExtValue()), *this))
+    if (handlePotentiallyDeadSuccessors(BI.getParent(),
+                                        BI.getSuccessor(!CI->getZExtValue())))
       return &BI;
 
   return nullptr;
@@ -2781,12 +2777,12 @@ Instruction *InstCombinerImpl::visitSwitchInst(SwitchInst &SI) {
     return replaceOperand(SI, 0, Op0);
   }
 
-  if (isa<UndefValue>(Cond) && handlePotentiallyDeadSuccessors(
-                                   SI.getParent(), /*LiveSucc*/ nullptr, *this))
+  if (isa<UndefValue>(Cond) &&
+      handlePotentiallyDeadSuccessors(SI.getParent(), /*LiveSucc*/ nullptr))
     return &SI;
   if (auto *CI = dyn_cast<ConstantInt>(Cond))
     if (handlePotentiallyDeadSuccessors(
-            SI.getParent(), SI.findCaseValue(CI)->getCaseSuccessor(), *this))
+            SI.getParent(), SI.findCaseValue(CI)->getCaseSuccessor()))
       return &SI;
 
   KnownBits Known = computeKnownBits(Cond, 0, &SI);

diff  --git a/llvm/test/Transforms/InstCombine/getelementptr.ll b/llvm/test/Transforms/InstCombine/getelementptr.ll
index bc3949885a4f9..9046cb6b4529e 100644
--- a/llvm/test/Transforms/InstCombine/getelementptr.ll
+++ b/llvm/test/Transforms/InstCombine/getelementptr.ll
@@ -495,8 +495,6 @@ define i1 @test23() {
 define void @test25() {
 ; CHECK-LABEL: @test25(
 ; CHECK-NEXT:  entry:
-; CHECK-NEXT:    store i1 true, ptr poison, align 1
-; CHECK-NEXT:    tail call void @foo25(i32 0, i64 poison)
 ; CHECK-NEXT:    unreachable
 ;
 entry:

diff  --git a/llvm/test/Transforms/InstCombine/pr44245.ll b/llvm/test/Transforms/InstCombine/pr44245.ll
index a0896fa38a75d..21c95ddc1c876 100644
--- a/llvm/test/Transforms/InstCombine/pr44245.ll
+++ b/llvm/test/Transforms/InstCombine/pr44245.ll
@@ -55,10 +55,9 @@ define void @test(i1 %c) {
 ; CHECK-NEXT:    [[TMP149:%.*]] = load ptr, ptr inttoptr (i64 16 to ptr), align 16
 ; CHECK-NEXT:    br label [[BB150]]
 ; CHECK:       bb150:
-; CHECK-NEXT:    [[DOTIN]] = phi ptr [ [[TMP184:%.*]], [[BB152]] ], [ [[TMP149]], [[BB147]] ]
+; CHECK-NEXT:    [[DOTIN]] = phi ptr [ poison, [[BB152]] ], [ [[TMP149]], [[BB147]] ]
 ; CHECK-NEXT:    br label [[BB47]]
 ; CHECK:       bb152:
-; CHECK-NEXT:    [[TMP184]] = load ptr, ptr inttoptr (i64 16 to ptr), align 16
 ; CHECK-NEXT:    store i1 true, ptr poison, align 1
 ; CHECK-NEXT:    br label [[BB150]]
 ;

diff  --git a/llvm/test/Transforms/InstCombine/unreachable-code.ll b/llvm/test/Transforms/InstCombine/unreachable-code.ll
index 2d1656fa2a54d..516f90f6503c5 100644
--- a/llvm/test/Transforms/InstCombine/unreachable-code.ll
+++ b/llvm/test/Transforms/InstCombine/unreachable-code.ll
@@ -193,7 +193,6 @@ default:
 define void @non_term_unreachable() {
 ; CHECK-LABEL: define void @non_term_unreachable() {
 ; CHECK-NEXT:    store i1 true, ptr poison, align 1
-; CHECK-NEXT:    call void @dummy()
 ; CHECK-NEXT:    ret void
 ;
   store i1 true, ptr poison
@@ -208,11 +207,9 @@ define i32 @non_term_unreachable_phi(i1 %c) {
 ; CHECK-NEXT:    br i1 [[C]], label [[IF:%.*]], label [[JOIN:%.*]]
 ; CHECK:       if:
 ; CHECK-NEXT:    store i1 true, ptr poison, align 1
-; CHECK-NEXT:    call void @dummy()
 ; CHECK-NEXT:    br label [[JOIN]]
 ; CHECK:       join:
-; CHECK-NEXT:    [[PHI:%.*]] = phi i32 [ 1, [[IF]] ], [ 2, [[ENTRY:%.*]] ]
-; CHECK-NEXT:    ret i32 [[PHI]]
+; CHECK-NEXT:    ret i32 2
 ;
 entry:
   br i1 %c, label %if, label %join


        


More information about the llvm-commits mailing list