[llvm] [CVP] Keep `ReachableCaseCount` in sync with range of condition (PR #142302)

Yingwei Zheng via llvm-commits llvm-commits at lists.llvm.org
Sat May 31 21:55:51 PDT 2025


https://github.com/dtcxzyw created https://github.com/llvm/llvm-project/pull/142302

Closes https://github.com/llvm/llvm-project/issues/142286.


>From 50c01fd2a32cd16216208c7d5f441f716aede22d Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Sun, 1 Jun 2025 12:35:14 +0800
Subject: [PATCH 1/2] [CVP] Add pre-commit tests. NFC.

---
 .../CorrelatedValuePropagation/switch.ll      | 38 +++++++++++++++++++
 1 file changed, 38 insertions(+)

diff --git a/llvm/test/Transforms/CorrelatedValuePropagation/switch.ll b/llvm/test/Transforms/CorrelatedValuePropagation/switch.ll
index a0794d5efe932..456807bc499f8 100644
--- a/llvm/test/Transforms/CorrelatedValuePropagation/switch.ll
+++ b/llvm/test/Transforms/CorrelatedValuePropagation/switch.ll
@@ -294,6 +294,44 @@ cleanup:
   ret i32 %retval.0
 }
 
+; Make sure that we don't branch into unreachable.
+
+define void @pr142286() {
+; CHECK-LABEL: define void @pr142286() {
+; CHECK-NEXT:  start:
+; CHECK-NEXT:    br label [[LOOP:%.*]]
+; CHECK:       loop:
+; CHECK-NEXT:    br label [[LOOP2:%.*]]
+; CHECK:       loop2:
+; CHECK-NEXT:    br label [[LOOP3:%.*]]
+; CHECK:       loop3:
+; CHECK-NEXT:    br label [[DEFAULT_UNREACHABLE:%.*]]
+; CHECK:       default.unreachable:
+; CHECK-NEXT:    unreachable
+; CHECK:       exit:
+; CHECK-NEXT:    ret void
+;
+start:
+  br label %loop
+
+loop:
+  %phi = phi i8 [ -1, %start ], [ 0, %loop3 ]
+  br label %loop2
+
+loop2:
+  br label %loop3
+
+loop3:
+  switch i8 %phi, label %exit [
+  i8 0, label %loop3
+  i8 1, label %loop2
+  i8 2, label %loop
+  ]
+
+exit:
+  ret void
+}
+
 declare i32 @call0()
 declare i32 @call1()
 declare i32 @call2()

>From 4686664f341d5f5ea13b4bd625b69e612d6db1f9 Mon Sep 17 00:00:00 2001
From: Yingwei Zheng <dtcxzyw2333 at gmail.com>
Date: Sun, 1 Jun 2025 12:39:11 +0800
Subject: [PATCH 2/2] [CVP] Keep ReachableCaseCount in sync with range of
 condition

---
 llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp | 8 +++++---
 llvm/test/Transforms/CorrelatedValuePropagation/switch.ll | 4 +---
 2 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp b/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
index 5226aeb66f65a..4228d0529dc13 100644
--- a/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
+++ b/llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp
@@ -370,7 +370,7 @@ static bool processSwitch(SwitchInst *I, LazyValueInfo *LVI,
   { // Scope for SwitchInstProfUpdateWrapper. It must not live during
     // ConstantFoldTerminator() as the underlying SwitchInst can be changed.
     SwitchInstProfUpdateWrapper SI(*I);
-    unsigned ReachableCaseCount = 0;
+    SmallVector<APInt, 4> ReachableCaseValues;
 
     for (auto CI = SI->case_begin(), CE = SI->case_end(); CI != CE;) {
       ConstantInt *Case = CI->getCaseValue();
@@ -407,13 +407,15 @@ static bool processSwitch(SwitchInst *I, LazyValueInfo *LVI,
 
       // Increment the case iterator since we didn't delete it.
       ++CI;
-      ++ReachableCaseCount;
+      ReachableCaseValues.push_back(Case->getValue());
     }
 
-    if (ReachableCaseCount > 1 && !SI->defaultDestUnreachable()) {
+    if (ReachableCaseValues.size() > 1 && !SI->defaultDestUnreachable()) {
       BasicBlock *DefaultDest = SI->getDefaultDest();
       ConstantRange CR = LVI->getConstantRangeAtUse(I->getOperandUse(0),
                                                     /*UndefAllowed*/ false);
+      unsigned ReachableCaseCount = count_if(
+          ReachableCaseValues, [&](const APInt &V) { return CR.contains(V); });
       // The default dest is unreachable if all cases are covered.
       if (!CR.isSizeLargerThan(ReachableCaseCount)) {
         BasicBlock *NewUnreachableBB =
diff --git a/llvm/test/Transforms/CorrelatedValuePropagation/switch.ll b/llvm/test/Transforms/CorrelatedValuePropagation/switch.ll
index 456807bc499f8..7e6aa3eeebe20 100644
--- a/llvm/test/Transforms/CorrelatedValuePropagation/switch.ll
+++ b/llvm/test/Transforms/CorrelatedValuePropagation/switch.ll
@@ -305,9 +305,7 @@ define void @pr142286() {
 ; CHECK:       loop2:
 ; CHECK-NEXT:    br label [[LOOP3:%.*]]
 ; CHECK:       loop3:
-; CHECK-NEXT:    br label [[DEFAULT_UNREACHABLE:%.*]]
-; CHECK:       default.unreachable:
-; CHECK-NEXT:    unreachable
+; CHECK-NEXT:    br label [[EXIT:%.*]]
 ; CHECK:       exit:
 ; CHECK-NEXT:    ret void
 ;



More information about the llvm-commits mailing list