[llvm] [SimplifyCFG] Handle that first matched eq cond in if chain can be Extra condition. (PR #154007)

Andreas Jonson via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 17 01:34:23 PDT 2025


https://github.com/andjo403 created https://github.com/llvm/llvm-project/pull/154007

Proof: https://alive2.llvm.org/ce/z/TozSD6

Noticed as a regression when I tried to add trunc nuw handling.

>From f009efead55b02f1834b92fd9c534a85214d7752 Mon Sep 17 00:00:00 2001
From: Andreas Jonson <andjo403 at hotmail.com>
Date: Sun, 17 Aug 2025 10:23:25 +0200
Subject: [PATCH 1/2] [SimplifyCFG] Add test with multiple eq cond in if chain
 (NFC)

---
 .../Transforms/SimplifyCFG/switch_create.ll   | 66 +++++++++++++++++++
 1 file changed, 66 insertions(+)

diff --git a/llvm/test/Transforms/SimplifyCFG/switch_create.ll b/llvm/test/Transforms/SimplifyCFG/switch_create.ll
index f446d718f8206..f9fcc8615c0ce 100644
--- a/llvm/test/Transforms/SimplifyCFG/switch_create.ll
+++ b/llvm/test/Transforms/SimplifyCFG/switch_create.ll
@@ -1068,3 +1068,69 @@ if:
 else:
   ret void
 }
+
+define void @extra_cond_is_eq_cmp(i8 %c, i32 %x)  {
+; CHECK-LABEL: @extra_cond_is_eq_cmp(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[X:%.*]], 32
+; CHECK-NEXT:    [[CMP4:%.*]] = icmp eq i8 [[C:%.*]], 97
+; CHECK-NEXT:    [[OR_COND:%.*]] = or i1 [[CMP]], [[CMP4]]
+; CHECK-NEXT:    [[CMP9:%.*]] = icmp eq i8 [[C]], 99
+; CHECK-NEXT:    [[TMP0:%.*]] = or i1 [[OR_COND]], [[CMP9]]
+; CHECK-NEXT:    br i1 [[TMP0]], label [[IF_THEN:%.*]], label [[SWITCH_EARLY_TEST:%.*]]
+; CHECK:       common.ret:
+; CHECK-NEXT:    ret void
+; CHECK:       if.then:
+; CHECK-NEXT:    tail call void @foo1()
+; CHECK-NEXT:    br label [[SWITCH_EARLY_TEST]]
+;
+entry:
+  %cmp = icmp eq i32 %x, 32
+  %cmp4 = icmp eq i8 %c, 97
+  %or.cond = or i1 %cmp, %cmp4
+  %cmp9 = icmp eq i8 %c, 99
+  %or.cond11 = or i1 %or.cond, %cmp9
+  br i1 %or.cond11, label %if.then, label %if.end
+
+if.then:
+  tail call void @foo1()
+  ret void
+
+if.end:
+  ret void
+
+}
+
+define void @extra_cond_is_eq_cmp_c(i8 %c, i32 %x)  {
+; CHECK-LABEL: @extra_cond_is_eq_cmp_c(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[X:%.*]], 32
+; CHECK-NEXT:    [[TMP0:%.*]] = freeze i1 [[CMP]]
+; CHECK-NEXT:    br i1 [[TMP0]], label [[IF_THEN:%.*]], label [[SWITCH_EARLY_TEST:%.*]]
+; CHECK:       switch.early.test:
+; CHECK-NEXT:    switch i8 [[C:%.*]], label [[COMMON_RET:%.*]] [
+; CHECK-NEXT:      i8 99, label [[IF_THEN]]
+; CHECK-NEXT:      i8 97, label [[IF_THEN]]
+; CHECK-NEXT:    ]
+; CHECK:       common.ret:
+; CHECK-NEXT:    ret void
+; CHECK:       if.then:
+; CHECK-NEXT:    tail call void @foo1()
+; CHECK-NEXT:    br label [[COMMON_RET]]
+;
+entry:
+  %cmp = icmp eq i32 %x, 32
+  %cmp4 = icmp eq i8 %c, 97
+  %or.cond = or i1 %cmp4, %cmp
+  %cmp9 = icmp eq i8 %c, 99
+  %or.cond11 = or i1 %or.cond, %cmp9
+  br i1 %or.cond11, label %if.then, label %if.end
+
+if.then:
+  tail call void @foo1()
+  ret void
+
+if.end:
+  ret void
+
+}

>From 2e35b842b0fd096c5ffcef2c04b6a688a26f055a Mon Sep 17 00:00:00 2001
From: Andreas Jonson <andjo403 at hotmail.com>
Date: Sun, 17 Aug 2025 10:28:24 +0200
Subject: [PATCH 2/2] [SimplifyCFG] Handle that first matched eq cond in if
 chain can be Extra condition.

---
 llvm/lib/Transforms/Utils/SimplifyCFG.cpp         | 14 ++++++++++++++
 llvm/test/Transforms/SimplifyCFG/switch_create.ll | 12 +++++++-----
 2 files changed, 21 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
index 1436e479ba091..42b35c0c7e69e 100644
--- a/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyCFG.cpp
@@ -564,9 +564,19 @@ struct ConstantComparesGatherer {
   /// Number of comparisons matched in the and/or chain
   unsigned UsedICmps = 0;
 
+  // Used to check if the first matched CompValue shall be the Extra check.
+  bool IgnoreFirstMatch = false;
+
   /// Construct and compute the result for the comparison instruction Cond
   ConstantComparesGatherer(Instruction *Cond, const DataLayout &DL) : DL(DL) {
     gather(Cond);
+    if (CompValue)
+      return;
+    Extra = nullptr;
+    Vals.clear();
+    UsedICmps = 0;
+    IgnoreFirstMatch = true;
+    gather(Cond);
   }
 
   ConstantComparesGatherer(const ConstantComparesGatherer &) = delete;
@@ -577,6 +587,10 @@ struct ConstantComparesGatherer {
   /// Try to set the current value used for the comparison, it succeeds only if
   /// it wasn't set before or if the new value is the same as the old one
   bool setValueOnce(Value *NewVal) {
+    if (IgnoreFirstMatch && NewVal) {
+      IgnoreFirstMatch = false;
+      return false;
+    }
     if (CompValue && CompValue != NewVal)
       return false;
     CompValue = NewVal;
diff --git a/llvm/test/Transforms/SimplifyCFG/switch_create.ll b/llvm/test/Transforms/SimplifyCFG/switch_create.ll
index f9fcc8615c0ce..5f3c7b7c0fd5d 100644
--- a/llvm/test/Transforms/SimplifyCFG/switch_create.ll
+++ b/llvm/test/Transforms/SimplifyCFG/switch_create.ll
@@ -1073,16 +1073,18 @@ define void @extra_cond_is_eq_cmp(i8 %c, i32 %x)  {
 ; CHECK-LABEL: @extra_cond_is_eq_cmp(
 ; CHECK-NEXT:  entry:
 ; CHECK-NEXT:    [[CMP:%.*]] = icmp eq i32 [[X:%.*]], 32
-; CHECK-NEXT:    [[CMP4:%.*]] = icmp eq i8 [[C:%.*]], 97
-; CHECK-NEXT:    [[OR_COND:%.*]] = or i1 [[CMP]], [[CMP4]]
-; CHECK-NEXT:    [[CMP9:%.*]] = icmp eq i8 [[C]], 99
-; CHECK-NEXT:    [[TMP0:%.*]] = or i1 [[OR_COND]], [[CMP9]]
+; CHECK-NEXT:    [[TMP0:%.*]] = freeze i1 [[CMP]]
 ; CHECK-NEXT:    br i1 [[TMP0]], label [[IF_THEN:%.*]], label [[SWITCH_EARLY_TEST:%.*]]
+; CHECK:       switch.early.test:
+; CHECK-NEXT:    switch i8 [[C:%.*]], label [[COMMON_RET:%.*]] [
+; CHECK-NEXT:      i8 99, label [[IF_THEN]]
+; CHECK-NEXT:      i8 97, label [[IF_THEN]]
+; CHECK-NEXT:    ]
 ; CHECK:       common.ret:
 ; CHECK-NEXT:    ret void
 ; CHECK:       if.then:
 ; CHECK-NEXT:    tail call void @foo1()
-; CHECK-NEXT:    br label [[SWITCH_EARLY_TEST]]
+; CHECK-NEXT:    br label [[COMMON_RET]]
 ;
 entry:
   %cmp = icmp eq i32 %x, 32



More information about the llvm-commits mailing list