[llvm] [InstSimplify] Discard unnecessary `select` when the conditions are `ICmps` with EQ (PR #179183)
via llvm-commits
llvm-commits at lists.llvm.org
Mon Feb 2 02:10:28 PST 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
Author: Gábor Spaits (spaits)
<details>
<summary>Changes</summary>
This PR was motivated by the discussion with @<!-- -->dtcxzyw 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. In that case #<!-- -->177410 won't be run and we have even better code than before.
Alive2 link for the tests: https://alive2.llvm.org/ce/z/GVtLBT .
---
Full diff: https://github.com/llvm/llvm-project/pull/179183.diff
2 Files Affected:
- (modified) llvm/lib/Analysis/InstructionSimplify.cpp (+26)
- (added) llvm/test/Transforms/InstSimplify/select-select.ll (+116)
``````````diff
diff --git a/llvm/lib/Analysis/InstructionSimplify.cpp b/llvm/lib/Analysis/InstructionSimplify.cpp
index 0e0c092271a38..c4b858542d3b0 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
new file mode 100644
index 0000000000000..db0329a5fe8ed
--- /dev/null
+++ b/llvm/test/Transforms/InstSimplify/select-select.ll
@@ -0,0 +1,116 @@
+; 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: [[TMP3:%.*]] = icmp eq i32 [[TMP0]], 4
+; CHECK-NEXT: [[DOT_I:%.*]] = select i1 [[TMP3]], i32 [[TMP1]], i32 [[TMP0]]
+; CHECK-NEXT: ret i32 [[DOT_I]]
+;
+ %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: [[TMP3:%.*]] = icmp eq i32 [[TMP0]], 4
+; CHECK-NEXT: [[DOT_I:%.*]] = select i1 [[TMP3]], i32 [[TMP1]], i32 [[TMP0]]
+; 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
+ %..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: [[TMP4:%.*]] = icmp eq i32 [[TMP1]], 4
+; CHECK-NEXT: [[DOT_I:%.*]] = select i1 [[TMP4]], i32 [[TMP2]], i32 [[TMP1]]
+; CHECK-NEXT: [[TMP5:%.*]] = getelementptr inbounds nuw i8, ptr [[TMP0]], i64 12
+; CHECK-NEXT: store i32 [[DOT_I]], 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)
``````````
</details>
https://github.com/llvm/llvm-project/pull/179183
More information about the llvm-commits
mailing list