[llvm] [InstSimplify] Discard unnecessary select when the conditions are ICmps with EQ (PR #179183)

Gábor Spaits via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 2 00:57:05 PST 2026


https://github.com/spaits updated https://github.com/llvm/llvm-project/pull/179183

>From 4000ded11e3935098a88b0a34326b3da85beaf07 Mon Sep 17 00:00:00 2001
From: Gabor Spaits <gaborspaits1 at gmail.com>
Date: Sun, 1 Feb 2026 21:21:18 +0100
Subject: [PATCH 1/2] Pre commit tests

---
 .../Transforms/InstSimplify/select-select.ll  | 122 ++++++++++++++++++
 1 file changed, 122 insertions(+)
 create mode 100644 llvm/test/Transforms/InstSimplify/select-select.ll

diff --git a/llvm/test/Transforms/InstSimplify/select-select.ll b/llvm/test/Transforms/InstSimplify/select-select.ll
new file mode 100644
index 0000000000000..2430bc42a5efe
--- /dev/null
+++ b/llvm/test/Transforms/InstSimplify/select-select.ll
@@ -0,0 +1,122 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt < %s -passes=instsimplify -S | FileCheck %s
+
+define i32 @selectSelectICmpICmp1(i32 %6, i32 %44) {
+; CHECK-LABEL: define i32 @selectSelectICmpICmp1(
+; CHECK-SAME: i32 [[TMP0:%.*]], i32 [[TMP1:%.*]]) {
+; CHECK-NEXT:    [[TMP4:%.*]] = icmp eq i32 [[TMP1]], 4
+; CHECK-NEXT:    [[TMP3:%.*]] = icmp eq i32 [[TMP0]], 4
+; CHECK-NEXT:    [[DOT_I:%.*]] = select i1 [[TMP3]], i32 [[TMP1]], i32 [[TMP0]]
+; CHECK-NEXT:    [[DOT0_I8:%.*]] = select i1 [[TMP4]], i32 [[TMP0]], i32 [[DOT_I]]
+; CHECK-NEXT:    ret i32 [[DOT0_I8]]
+;
+  %46 = icmp eq i32 %44, 4
+  %47 = icmp eq i32 %6, 4
+  %..i = select i1 %47, i32 %44, i32 %6
+  %.0.i8 = select i1 %46, i32 %6, i32 %..i
+  ret i32 %.0.i8
+}
+
+define i32 @selectSelectICmpICmp2(i32 %6, i32 %44) {
+; CHECK-LABEL: define i32 @selectSelectICmpICmp2(
+; CHECK-SAME: i32 [[TMP0:%.*]], i32 [[TMP1:%.*]]) {
+; CHECK-NEXT:    [[TMP4:%.*]] = icmp eq i32 [[TMP1]], 4
+; CHECK-NEXT:    [[TMP3:%.*]] = icmp eq i32 [[TMP0]], 4
+; CHECK-NEXT:    [[DOT_I:%.*]] = select i1 [[TMP3]], i32 [[TMP0]], i32 [[TMP1]]
+; CHECK-NEXT:    [[DOT0_I8:%.*]] = select i1 [[TMP4]], i32 [[DOT_I]], i32 [[TMP0]]
+; CHECK-NEXT:    ret i32 [[DOT0_I8]]
+;
+  %46 = icmp eq i32 %44, 4
+  %47 = icmp eq i32 %6, 4
+  %..i = select i1 %47, i32 %6, i32 %44
+  %.0.i8 = select i1 %46, i32 %..i, i32 %6
+  ret i32 %.0.i8
+}
+
+define i32 @selectSelectICmpICmp1MultiUseEliminatedSelect(i32 %6, i32 %44) {
+; CHECK-LABEL: define i32 @selectSelectICmpICmp1MultiUseEliminatedSelect(
+; CHECK-SAME: i32 [[TMP0:%.*]], i32 [[TMP1:%.*]]) {
+; CHECK-NEXT:    [[TMP4:%.*]] = icmp eq i32 [[TMP1]], 4
+; CHECK-NEXT:    [[TMP3:%.*]] = icmp eq i32 [[TMP0]], 4
+; CHECK-NEXT:    [[DOT_I:%.*]] = select i1 [[TMP3]], i32 [[TMP1]], i32 [[TMP0]]
+; CHECK-NEXT:    [[DOT0_I8:%.*]] = select i1 [[TMP4]], i32 [[TMP0]], i32 [[DOT_I]]
+; CHECK-NEXT:    call void @use32(i32 [[DOT0_I8]])
+; CHECK-NEXT:    ret i32 [[DOT0_I8]]
+;
+  %46 = icmp eq i32 %44, 4
+  %47 = icmp eq i32 %6, 4
+  %..i = select i1 %47, i32 %44, i32 %6
+  %.0.i8 = select i1 %46, i32 %6, i32 %..i
+  call void @use32(i32 %.0.i8)
+  ret i32 %.0.i8
+}
+
+define i32 @selectSelectICmpICmp2MultiUseEliminatedSelect(i32 %6, i32 %44) {
+; CHECK-LABEL: define i32 @selectSelectICmpICmp2MultiUseEliminatedSelect(
+; CHECK-SAME: i32 [[TMP0:%.*]], i32 [[TMP1:%.*]]) {
+; CHECK-NEXT:    [[TMP4:%.*]] = icmp eq i32 [[TMP1]], 4
+; CHECK-NEXT:    [[TMP3:%.*]] = icmp eq i32 [[TMP0]], 4
+; CHECK-NEXT:    [[DOT_I:%.*]] = select i1 [[TMP3]], i32 [[TMP0]], i32 [[TMP1]]
+; CHECK-NEXT:    [[DOT0_I8:%.*]] = select i1 [[TMP4]], i32 [[DOT_I]], i32 [[TMP0]]
+; CHECK-NEXT:    call void @use32(i32 [[DOT0_I8]])
+; CHECK-NEXT:    ret i32 [[DOT0_I8]]
+;
+  %46 = icmp eq i32 %44, 4
+  %47 = icmp eq i32 %6, 4
+  %..i = select i1 %47, i32 %6, i32 %44
+  %.0.i8 = select i1 %46, i32 %..i, i32 %6
+  call void @use32(i32 %.0.i8)
+  ret i32 %.0.i8
+}
+
+
+define i32 @negative_selectSelectICmpICmpBadOrder1(i32 %6, i32 %44) {
+; CHECK-LABEL: define i32 @negative_selectSelectICmpICmpBadOrder1(
+; CHECK-SAME: i32 [[TMP0:%.*]], i32 [[TMP1:%.*]]) {
+; CHECK-NEXT:    [[TMP3:%.*]] = icmp eq i32 [[TMP1]], 4
+; CHECK-NEXT:    [[TMP4:%.*]] = icmp eq i32 [[TMP0]], 4
+; CHECK-NEXT:    [[DOT_I:%.*]] = select i1 [[TMP4]], i32 [[TMP0]], i32 [[TMP1]]
+; CHECK-NEXT:    [[DOT0_I8:%.*]] = select i1 [[TMP3]], i32 [[TMP0]], i32 [[DOT_I]]
+; CHECK-NEXT:    ret i32 [[DOT0_I8]]
+;
+  %46 = icmp eq i32 %44, 4
+  %47 = icmp eq i32 %6, 4
+  %..i = select i1 %47, i32 %6, i32 %44
+  %.0.i8 = select i1 %46, i32 %6, i32 %..i
+  ret i32 %.0.i8
+}
+
+
+define i32 @negative_selectSelectICmpICmpBadOrder2(i32 %6, i32 %44) {
+; CHECK-LABEL: define i32 @negative_selectSelectICmpICmpBadOrder2(
+; CHECK-SAME: i32 [[TMP0:%.*]], i32 [[TMP1:%.*]]) {
+; CHECK-NEXT:    ret i32 [[TMP0]]
+;
+  %46 = icmp eq i32 %44, 4
+  %47 = icmp eq i32 %6, 4
+  %..i = select i1 %47, i32 %44, i32 %6
+  %.0.i8 = select i1 %46, i32 %..i, i32 %6
+  ret i32 %.0.i8
+}
+
+define void @realExample(ptr %0, i32 %6, i32 %44) {
+; CHECK-LABEL: define void @realExample(
+; CHECK-SAME: ptr [[TMP0:%.*]], i32 [[TMP1:%.*]], i32 [[TMP2:%.*]]) {
+; CHECK-NEXT:    [[TMP6:%.*]] = icmp eq i32 [[TMP2]], 4
+; CHECK-NEXT:    [[TMP4:%.*]] = icmp eq i32 [[TMP1]], 4
+; CHECK-NEXT:    [[DOT_I:%.*]] = select i1 [[TMP4]], i32 [[TMP2]], i32 [[TMP1]]
+; CHECK-NEXT:    [[DOT0_I8:%.*]] = select i1 [[TMP6]], i32 [[TMP1]], i32 [[DOT_I]]
+; CHECK-NEXT:    [[TMP5:%.*]] = getelementptr inbounds nuw i8, ptr [[TMP0]], i64 12
+; CHECK-NEXT:    store i32 [[DOT0_I8]], ptr [[TMP5]], align 4
+; CHECK-NEXT:    ret void
+;
+  %46 = icmp eq i32 %44, 4
+  %47 = icmp eq i32 %6, 4
+  %..i = select i1 %47, i32 %44, i32 %6
+  %.0.i8 = select i1 %46, i32 %6, i32 %..i
+  %50 = getelementptr inbounds nuw i8, ptr %0, i64 12
+  store i32 %.0.i8, ptr %50
+  ret void
+}
+
+declare void @use32(i32)

>From 08f5ac1dcd4661741cdbb3282a37dfebdf0a259d Mon Sep 17 00:00:00 2001
From: Gabor Spaits <gaborspaits1 at gmail.com>
Date: Mon, 2 Feb 2026 09:43:43 +0100
Subject: [PATCH 2/2] [InstSimplify] Discard unnecessary select when the
 conditions are ICmps

This PR was motivated by the discussion in PR #177410. That PR has caused a regression: https://github.com/dtcxzyw/llvm-opt-benchmark/pull/3371#discussion_r2722785220 .

This PR tries to address that. It addresses the issue before the optimization added in #177410 runs.
When I looked at the pre regression state of the code I noticed that one of the select isn't needed:
```llvm
define i32 @src(i32 %6, i32 %44) {
  %46 = icmp eq i32 %44, 4
  %47 = icmp eq i32 %6, 4

  %..i = select i1 %47, i32 %44, i32 %6
  %.0.i8 = select i1 %46, i32 %6, i32 %..i
  ; Return the %6, if that isn't 4 otherwise return %47
  ret i32 %.0.i8
}

define i32 @tgt(i32 %6, i32 %44) {
  %47 = icmp eq i32 %6, 4
  %selR = select i1 %47, i32 %44, i32 %6
  ret i32 %selR
}
```
So it is good enogh if we just gave the first select.
Alive2 link for the tests: https://alive2.llvm.org/ce/z/GVtLBT .
---
 llvm/lib/Analysis/InstructionSimplify.cpp     | 26 +++++++++++++++++++
 .../Transforms/InstSimplify/select-select.ll  | 14 +++-------
 2 files changed, 30 insertions(+), 10 deletions(-)

diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 0e0c092271a38..0ffbcd848f0dd 100644
--- a/llvm/lib/Analysis/InstructionSimplify.cpp
+++ b/llvm/lib/Analysis/InstructionSimplify.cpp
@@ -4980,6 +4980,29 @@ bool isSelectWithIdenticalPHI(PHINode &PN, PHINode &IdenticalPN) {
   return true;
 }
 
+static Value *simplifySelectWithSelect(Value *Cond, Value *TrueVal,
+                                       Value *FalseVal) {
+  CmpPredicate Pred1, Pred2;
+  Value *Cmp1LHS, *Cmp1RHS, *Cmp2LHS, *Cmp2RHS;
+
+  if (!match(Cond, m_ICmp(Pred2, m_Value(Cmp2LHS), m_Value(Cmp2RHS))))
+    return nullptr;
+
+  if (!match(FalseVal,
+             m_Select(m_ICmp(Pred1, m_Value(Cmp1LHS), m_Value(Cmp1RHS)),
+                      m_Value(), m_Specific(TrueVal))))
+    return nullptr;
+
+  if (Pred1 != ICmpInst::ICMP_EQ || Pred2 != ICmpInst::ICMP_EQ)
+    return nullptr;
+
+  if (Cmp1LHS == Cmp2LHS || Cmp1LHS == Cmp2RHS || Cmp1RHS == Cmp2LHS ||
+      Cmp1RHS == Cmp2RHS)
+    return FalseVal;
+
+  return nullptr;
+};
+
 /// Given operands for a SelectInst, see if we can fold the result.
 /// If not, this returns null.
 static Value *simplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
@@ -5142,6 +5165,9 @@ static Value *simplifySelectInst(Value *Cond, Value *TrueVal, Value *FalseVal,
       return ConstantVector::get(NewC);
   }
 
+  if (Value *V = simplifySelectWithSelect(Cond, TrueVal, FalseVal))
+    return V;
+
   if (Value *V =
           simplifySelectWithICmpCond(Cond, TrueVal, FalseVal, Q, MaxRecurse))
     return V;
diff --git a/llvm/test/Transforms/InstSimplify/select-select.ll b/llvm/test/Transforms/InstSimplify/select-select.ll
index 2430bc42a5efe..db0329a5fe8ed 100644
--- a/llvm/test/Transforms/InstSimplify/select-select.ll
+++ b/llvm/test/Transforms/InstSimplify/select-select.ll
@@ -4,11 +4,9 @@
 define i32 @selectSelectICmpICmp1(i32 %6, i32 %44) {
 ; CHECK-LABEL: define i32 @selectSelectICmpICmp1(
 ; CHECK-SAME: i32 [[TMP0:%.*]], i32 [[TMP1:%.*]]) {
-; CHECK-NEXT:    [[TMP4:%.*]] = icmp eq i32 [[TMP1]], 4
 ; CHECK-NEXT:    [[TMP3:%.*]] = icmp eq i32 [[TMP0]], 4
 ; CHECK-NEXT:    [[DOT_I:%.*]] = select i1 [[TMP3]], i32 [[TMP1]], i32 [[TMP0]]
-; CHECK-NEXT:    [[DOT0_I8:%.*]] = select i1 [[TMP4]], i32 [[TMP0]], i32 [[DOT_I]]
-; CHECK-NEXT:    ret i32 [[DOT0_I8]]
+; CHECK-NEXT:    ret i32 [[DOT_I]]
 ;
   %46 = icmp eq i32 %44, 4
   %47 = icmp eq i32 %6, 4
@@ -36,12 +34,10 @@ define i32 @selectSelectICmpICmp2(i32 %6, i32 %44) {
 define i32 @selectSelectICmpICmp1MultiUseEliminatedSelect(i32 %6, i32 %44) {
 ; CHECK-LABEL: define i32 @selectSelectICmpICmp1MultiUseEliminatedSelect(
 ; CHECK-SAME: i32 [[TMP0:%.*]], i32 [[TMP1:%.*]]) {
-; CHECK-NEXT:    [[TMP4:%.*]] = icmp eq i32 [[TMP1]], 4
 ; CHECK-NEXT:    [[TMP3:%.*]] = icmp eq i32 [[TMP0]], 4
 ; CHECK-NEXT:    [[DOT_I:%.*]] = select i1 [[TMP3]], i32 [[TMP1]], i32 [[TMP0]]
-; CHECK-NEXT:    [[DOT0_I8:%.*]] = select i1 [[TMP4]], i32 [[TMP0]], i32 [[DOT_I]]
-; CHECK-NEXT:    call void @use32(i32 [[DOT0_I8]])
-; CHECK-NEXT:    ret i32 [[DOT0_I8]]
+; CHECK-NEXT:    call void @use32(i32 [[DOT_I]])
+; CHECK-NEXT:    ret i32 [[DOT_I]]
 ;
   %46 = icmp eq i32 %44, 4
   %47 = icmp eq i32 %6, 4
@@ -102,12 +98,10 @@ define i32 @negative_selectSelectICmpICmpBadOrder2(i32 %6, i32 %44) {
 define void @realExample(ptr %0, i32 %6, i32 %44) {
 ; CHECK-LABEL: define void @realExample(
 ; CHECK-SAME: ptr [[TMP0:%.*]], i32 [[TMP1:%.*]], i32 [[TMP2:%.*]]) {
-; CHECK-NEXT:    [[TMP6:%.*]] = icmp eq i32 [[TMP2]], 4
 ; CHECK-NEXT:    [[TMP4:%.*]] = icmp eq i32 [[TMP1]], 4
 ; CHECK-NEXT:    [[DOT_I:%.*]] = select i1 [[TMP4]], i32 [[TMP2]], i32 [[TMP1]]
-; CHECK-NEXT:    [[DOT0_I8:%.*]] = select i1 [[TMP6]], i32 [[TMP1]], i32 [[DOT_I]]
 ; CHECK-NEXT:    [[TMP5:%.*]] = getelementptr inbounds nuw i8, ptr [[TMP0]], i64 12
-; CHECK-NEXT:    store i32 [[DOT0_I8]], ptr [[TMP5]], align 4
+; CHECK-NEXT:    store i32 [[DOT_I]], ptr [[TMP5]], align 4
 ; CHECK-NEXT:    ret void
 ;
   %46 = icmp eq i32 %44, 4



More information about the llvm-commits mailing list