[PATCH] D151691: Revert "[InstCombine] Handle undef when pruning unreachable code"

Thomas Symalla via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue May 30 00:17:32 PDT 2023


tsymalla created this revision.
tsymalla added a reviewer: foad.
Herald added subscribers: StephenFan, hiraditya.
Herald added a project: All.
tsymalla requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

This reverts commit 1fc425380e9860a6beb53fa68d02e7fb14969963 <https://reviews.llvm.org/rG1fc425380e9860a6beb53fa68d02e7fb14969963>.
This is causing some test failures in cases like

for (;;) {
	callIntrinsicStore...
	switch (undef) {

  		case 0:
  			break;

}
}

where LLVM is completely optimizing away the loop. The IR in mind is
something like the following.
So, independent of which jump target we take in 10, we will still be
able to reach the final merge block. With the optimization, the loop
body (add) wil be optimized away and we will never reach the merge
block.

entry:

  br label %0

0:

  br label %1, !llvm.loop

1:

  br label %2

2:

  %3 = phi i32 [ 0, %1 ], [ %14, %13 ]
  br label %4, !llvm.loop !3

4:                                                ; preds = %2

  %5 = icmp slt i32 %3, 1
  br i1 %5, label %6, label %15

6:                                                ; preds = %4

  br label %7

7:                                                ; preds = %6

  br label %8, !llvm.loop !4

8:                                                ; preds = %7

  br label %9

9:                                                ; preds = %8

  br label %10

10:                                               ; preds = %9

  br i1 undef, label %11, label %12

11:                                               ; preds = %10

  br label %15

12:                                               ; preds = %10

  br label %13

13:                                               ; preds = %12

  %14 = add i32 %3, 1
  br label %2, !llvm.loop !5

15:                                               ; preds = %11, %4

  br label %17

16:                                               ; No predecessors!

  br label %17

17:                                               ; preds = %15, %16

  call void (...) @write(...)
  ret void


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D151691

Files:
  llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
  llvm/test/Transforms/InstCombine/unreachable-code.ll


Index: llvm/test/Transforms/InstCombine/unreachable-code.ll
===================================================================
--- llvm/test/Transforms/InstCombine/unreachable-code.ll
+++ llvm/test/Transforms/InstCombine/unreachable-code.ll
@@ -52,8 +52,10 @@
 ; 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
@@ -127,8 +129,10 @@
 ; 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
Index: llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -3954,21 +3954,15 @@
     // 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); 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();
+    if (BranchInst *BI = dyn_cast<BranchInst>(TI)) {
+      if (BI->isConditional() && isa<ConstantInt>(BI->getCondition())) {
+        bool CondVal = cast<ConstantInt>(BI->getCondition())->getZExtValue();
         BasicBlock *ReachableBB = BI->getSuccessor(!CondVal);
         Worklist.push_back(ReachableBB);
         continue;
       }
     } else if (SwitchInst *SI = dyn_cast<SwitchInst>(TI)) {
-      if (isa<UndefValue>(SI->getCondition()))
-        // Switch on undef is UB.
-        continue;
-      if (auto *Cond = dyn_cast<ConstantInt>(SI->getCondition())) {
+      if (ConstantInt *Cond = dyn_cast<ConstantInt>(SI->getCondition())) {
         Worklist.push_back(SI->findCaseValue(Cond)->getCaseSuccessor());
         continue;
       }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D151691.526534.patch
Type: text/x-patch
Size: 2348 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230530/253f2b8e/attachment.bin>


More information about the llvm-commits mailing list