[llvm] de9ad98 - Fix endless loop in optimizePhiConst with integer constant switch condition

Matthias Braun via llvm-commits llvm-commits at lists.llvm.org
Wed May 11 08:49:07 PDT 2022


Author: Matthias Braun
Date: 2022-05-11T08:49:01-07:00
New Revision: de9ad98d2d6358e6aa773f0bb5258b629bde9389

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

LOG: Fix endless loop in optimizePhiConst with integer constant switch condition

Avoid endless loop in degenerate case with an integer constant as switch
condition as reported in https://reviews.llvm.org/D124552

Added: 
    

Modified: 
    llvm/lib/CodeGen/CodeGenPrepare.cpp
    llvm/test/Transforms/CodeGenPrepare/X86/switch-phi-const.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/CodeGen/CodeGenPrepare.cpp b/llvm/lib/CodeGen/CodeGenPrepare.cpp
index 2fd9be8c4c21..813fbd1956f9 100644
--- a/llvm/lib/CodeGen/CodeGenPrepare.cpp
+++ b/llvm/lib/CodeGen/CodeGenPrepare.cpp
@@ -7037,9 +7037,13 @@ bool CodeGenPrepare::optimizeSwitchPhiConstants(SwitchInst *SI) {
   // change the code to:
   //   switch(x) { case 42: phi(x, ...) }
 
+  Value *Condition = SI->getCondition();
+  // Avoid endless loop in degenerate case.
+  if (isa<ConstantInt>(*Condition))
+    return false;
+
   bool Changed = false;
   BasicBlock *SwitchBB = SI->getParent();
-  Value *Condition = SI->getCondition();
   Type *ConditionType = Condition->getType();
 
   for (const SwitchInst::CaseHandle &Case : SI->cases()) {

diff  --git a/llvm/test/Transforms/CodeGenPrepare/X86/switch-phi-const.ll b/llvm/test/Transforms/CodeGenPrepare/X86/switch-phi-const.ll
index 5dd53a689224..2854cd02c935 100644
--- a/llvm/test/Transforms/CodeGenPrepare/X86/switch-phi-const.ll
+++ b/llvm/test/Transforms/CodeGenPrepare/X86/switch-phi-const.ll
@@ -126,3 +126,32 @@ case_13:
 default:
   ret void
 }
+
+define void @switch_phi_const_degenerate() {
+; CHECK-LABEL: @switch_phi_const_degenerate(
+; CHECK-NEXT:  bb0:
+; CHECK-NEXT:    br i1 undef, label [[CASE_42:%.*]], label [[BB1:%.*]]
+; CHECK:       bb1:
+; CHECK-NEXT:    br label [[CASE_42]]
+; CHECK:       case_42:
+; CHECK-NEXT:    [[X:%.*]] = phi i32 [ 0, [[BB0:%.*]] ], [ 42, [[BB1]] ]
+; CHECK-NEXT:    store volatile i32 [[X]], i32* @effect, align 4
+; CHECK-NEXT:    ret void
+;
+bb0:
+  br i1 undef, label %case_42, label %bb1
+
+bb1:
+  switch i32 42, label %unreachable [
+  i32 42, label %case_42
+  ]
+
+case_42:
+  ; We should not end up in an endless loop 42 with the switch condition 42.
+  %x = phi i32 [0, %bb0], [42, %bb1]
+  store volatile i32 %x, i32* @effect, align 4
+  ret void
+
+unreachable:
+  unreachable
+}


        


More information about the llvm-commits mailing list