[llvm] cde681c - [InstCombine] Replace phi operands in successors of unreachable block

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 9 03:31:16 PDT 2023


Author: Nikita Popov
Date: 2023-06-09T12:31:07+02:00
New Revision: cde681c865302f689f0937e5ec2e127a089a4db3

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

LOG: [InstCombine] Replace phi operands in successors of unreachable block

Set these operands to poison, which might allow folding the phi node,
or reduce the use count of an instruction.

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 311dd5a2244bb..6254112adf466 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -2552,6 +2552,16 @@ static bool handlePotentiallyDeadBlock(BasicBlock *BB, InstCombiner &IC) {
     Changed = true;
   }
 
+  // Replace phi node operands in successor blocks with poison.
+  for (BasicBlock *Succ : successors(BB))
+    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);
+          Changed = true;
+        }
+
   // TODO: Successor blocks may also be dead.
   return Changed;
 }

diff  --git a/llvm/test/Transforms/InstCombine/unreachable-code.ll b/llvm/test/Transforms/InstCombine/unreachable-code.ll
index d3674a015f3fa..d55e779b71a99 100644
--- a/llvm/test/Transforms/InstCombine/unreachable-code.ll
+++ b/llvm/test/Transforms/InstCombine/unreachable-code.ll
@@ -13,8 +13,7 @@ define i32 @br_true(i1 %x) {
 ; CHECK:       else:
 ; CHECK-NEXT:    br label [[JOIN]]
 ; CHECK:       join:
-; CHECK-NEXT:    [[PHI:%.*]] = phi i32 [ 1, [[IF]] ], [ 2, [[ELSE]] ]
-; CHECK-NEXT:    ret i32 [[PHI]]
+; CHECK-NEXT:    ret i32 1
 ;
   %c = or i1 %x, true
   br i1 %c, label %if, label %else
@@ -42,8 +41,7 @@ define i32 @br_false(i1 %x) {
 ; CHECK-NEXT:    call void @dummy()
 ; CHECK-NEXT:    br label [[JOIN]]
 ; CHECK:       join:
-; CHECK-NEXT:    [[PHI:%.*]] = phi i32 [ 1, [[IF]] ], [ 2, [[ELSE]] ]
-; CHECK-NEXT:    ret i32 [[PHI]]
+; CHECK-NEXT:    ret i32 2
 ;
   %c = and i1 %x, false
   br i1 %c, label %if, label %else
@@ -70,7 +68,7 @@ define i32 @br_undef(i1 %x) {
 ; CHECK:       else:
 ; CHECK-NEXT:    br label [[JOIN]]
 ; CHECK:       join:
-; CHECK-NEXT:    ret i32 poison
+; CHECK-NEXT:    ret i32 undef
 ;
   %c = xor i1 %x, undef
   br i1 %c, label %if, label %else
@@ -98,8 +96,7 @@ define i32 @br_true_phi_with_repeated_preds(i1 %x) {
 ; CHECK:       else:
 ; CHECK-NEXT:    br i1 false, label [[JOIN]], label [[JOIN]]
 ; CHECK:       join:
-; CHECK-NEXT:    [[PHI:%.*]] = phi i32 [ 1, [[IF]] ], [ 2, [[ELSE]] ], [ 2, [[ELSE]] ]
-; CHECK-NEXT:    ret i32 [[PHI]]
+; CHECK-NEXT:    ret i32 1
 ;
   %c = or i1 %x, true
   br i1 %c, label %if, label %else


        


More information about the llvm-commits mailing list