[llvm] 1fc4253 - [InstCombine] Handle undef when pruning unreachable code

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Fri May 26 06:59:15 PDT 2023


Author: Nikita Popov
Date: 2023-05-26T15:55:28+02:00
New Revision: 1fc425380e9860a6beb53fa68d02e7fb14969963

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

LOG: [InstCombine] Handle undef when pruning unreachable code

If the branch condition is undef, then behavior is undefined and
neither of the successors are live.

This is to ensure that optimization quality does not decrease
when a constant gets replaced with undef/poison in this context.

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 8c10fe96aaf8..0df85be126bc 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -3903,15 +3903,21 @@ static bool prepareICWorklistFromFunction(Function &F, const DataLayout &DL,
     // Recursively visit successors.  If this is a branch or switch on a
     // constant, only visit the reachable successor.
     Instruction *TI = BB->getTerminator();
-    if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
-      if (BI->isConditional() && isa<ConstantInt>(BI->getCondition())) {
-        bool CondVal = cast<ConstantInt>(BI->getCondition())->getZExtValue();
+    if (BranchInst *BI = dyn_cast<BranchInst>(TI); BI && BI->isConditional()) {
+      if (isa<UndefValue>(BI->getCondition()))
+        // Branch on undef is UB.
+        continue;
+      if (auto *Cond = dyn_cast<ConstantInt>(BI->getCondition())) {
+        bool CondVal = Cond->getZExtValue();
         BasicBlock *ReachableBB = BI->getSuccessor(!CondVal);
         Worklist.push_back(ReachableBB);
         continue;
       }
     } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
-      if (ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition())) {
+      if (isa<UndefValue>(SI->getCondition()))
+        // Switch on undef is UB.
+        continue;
+      if (auto *Cond = dyn_cast<ConstantInt>(SI->getCondition())) {
         Worklist.push_back(SI->findCaseValue(Cond)->getCaseSuccessor());
         continue;
       }

diff  --git a/llvm/test/Transforms/InstCombine/unreachable-code.ll b/llvm/test/Transforms/InstCombine/unreachable-code.ll
index 391d73fc3bb4..4a85d088d20d 100644
--- a/llvm/test/Transforms/InstCombine/unreachable-code.ll
+++ b/llvm/test/Transforms/InstCombine/unreachable-code.ll
@@ -52,10 +52,8 @@ define void @br_undef(i1 %x) {
 ; CHECK-SAME: (i1 [[X:%.*]]) {
 ; CHECK-NEXT:    br i1 undef, label [[IF:%.*]], label [[ELSE:%.*]]
 ; CHECK:       if:
-; CHECK-NEXT:    call void @dummy()
 ; CHECK-NEXT:    ret void
 ; CHECK:       else:
-; CHECK-NEXT:    call void @dummy()
 ; CHECK-NEXT:    ret void
 ;
   %c = xor i1 %x, undef
@@ -129,10 +127,8 @@ define void @switch_undef(i32 %x) {
 ; CHECK-NEXT:    i32 0, label [[CASE0:%.*]]
 ; CHECK-NEXT:    ]
 ; CHECK:       case0:
-; CHECK-NEXT:    call void @dummy()
 ; CHECK-NEXT:    ret void
 ; CHECK:       default:
-; CHECK-NEXT:    call void @dummy()
 ; CHECK-NEXT:    ret void
 ;
   %v = xor i32 %x, undef


        


More information about the llvm-commits mailing list