[llvm] [InstCombine] Fix profile propagation in foldNestedSelects (PR #229635)
via llvm-commits
llvm-commits at lists.llvm.org
Tue Oct 6 19:02:26 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
Author: Aiden Grossman (boomanaiden154)
<details>
<summary>Changes</summary>
We can recover profile information for one of the created selects given the condition is either the same as an existing select or the direct inverse. For the other we have to mark the profile as unknown.
---
Full diff: https://github.com/llvm/llvm-project/pull/229635.diff
4 Files Affected:
- (modified) llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp (+18-6)
- (modified) llvm/test/Transforms/InstCombine/nested-select.ll (+15-8)
- (modified) llvm/test/Transforms/InstCombine/select-select.ll (+18-16)
- (modified) llvm/utils/profcheck-xfail.txt (-1)
``````````diff
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
index 8374c005e7998..d265464b9764e 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -3676,8 +3676,11 @@ static Instruction *foldNestedSelects(SelectInst &OuterSelVal,
return nullptr;
// Canonicalize inversion of the innermost `select`'s condition.
- if (match(InnerSel.Cond, m_Not(m_Value(InnerSel.Cond))))
+ bool SwapInnerSelCond = false;
+ if (match(InnerSel.Cond, m_Not(m_Value(InnerSel.Cond)))) {
std::swap(InnerSel.TrueVal, InnerSel.FalseVal);
+ SwapInnerSelCond = !SwapInnerSelCond;
+ }
Value *AltCond = nullptr;
auto matchOuterCond = [OuterSel, IsAndVariant, &AltCond](auto m_InnerCond) {
@@ -3702,16 +3705,25 @@ static Instruction *foldNestedSelects(SelectInst &OuterSelVal,
// Done!
std::swap(InnerSel.TrueVal, InnerSel.FalseVal);
InnerSel.Cond = NotInnerCond;
+ SwapInnerSelCond = !SwapInnerSelCond;
} else // Not the pattern we were looking for.
return nullptr;
- Value *SelInner = Builder.CreateSelect(
+ // We mark the select with AltCond as having an unknown profile given the
+ // condition is derived from an and/or. We might have profile information on
+ // the operands of the and/or, but there is no guarantee that they are
+ // independent.
+ Value *SelInner = Builder.CreateSelectWithUnknownProfile(
AltCond, IsAndVariant ? OuterSel.TrueVal : InnerSel.FalseVal,
- IsAndVariant ? InnerSel.TrueVal : OuterSel.FalseVal);
+ IsAndVariant ? InnerSel.TrueVal : OuterSel.FalseVal, DEBUG_TYPE);
SelInner->takeName(InnerSelVal);
- return SelectInst::Create(InnerSel.Cond,
- IsAndVariant ? SelInner : InnerSel.TrueVal,
- !IsAndVariant ? SelInner : InnerSel.FalseVal);
+ SelectInst *SI = SelectInst::Create(
+ InnerSel.Cond, IsAndVariant ? SelInner : InnerSel.TrueVal,
+ !IsAndVariant ? SelInner : InnerSel.FalseVal, "", nullptr,
+ ProfcheckDisableMetadataFixes ? nullptr : cast<Instruction>(InnerSelVal));
+ if (SwapInnerSelCond)
+ SI->swapProfMetadata();
+ return SI;
}
/// Return true if V is poison or \p Expected given that ValAssumedPoison is
diff --git a/llvm/test/Transforms/InstCombine/nested-select.ll b/llvm/test/Transforms/InstCombine/nested-select.ll
index 67cd608cad4e8..a04d3a3fcafa7 100644
--- a/llvm/test/Transforms/InstCombine/nested-select.ll
+++ b/llvm/test/Transforms/InstCombine/nested-select.ll
@@ -1,4 +1,4 @@
-; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --check-globals smart
; RUN: opt -passes=instcombine -S < %s | FileCheck %s
declare void @use.i1(i1)
@@ -289,16 +289,16 @@ define i1 @orcond.001.inv.outer.cond(i1 %inner.cond, i1 %alt.cond, i1 %inner.sel
}
; In %inner.sel, %inner.cond is inverted
-define i1 @andcond.010.inv.inner.cond.in.inner.sel(i1 %inner.cond, i1 %alt.cond, i1 %inner.sel.trueval, i1 %inner.sel.falseval, i1 %outer.sel.trueval) {
+define i1 @andcond.010.inv.inner.cond.in.inner.sel(i1 %inner.cond, i1 %alt.cond, i1 %inner.sel.trueval, i1 %inner.sel.falseval, i1 %outer.sel.trueval) !prof !0 {
; CHECK-LABEL: @andcond.010.inv.inner.cond.in.inner.sel(
-; CHECK-NEXT: [[INNER_SEL:%.*]] = select i1 [[ALT_COND:%.*]], i1 [[OUTER_SEL_TRUEVAL:%.*]], i1 false
-; CHECK-NEXT: [[OUTER_SEL:%.*]] = select i1 [[INNER_COND:%.*]], i1 [[INNER_SEL]], i1 [[INNER_SEL_FALSEVAL:%.*]]
+; CHECK-NEXT: [[INNER_SEL:%.*]] = select i1 [[ALT_COND:%.*]], i1 [[OUTER_SEL_TRUEVAL:%.*]], i1 false, !prof [[PROF1:![0-9]+]]
+; CHECK-NEXT: [[OUTER_SEL:%.*]] = select i1 [[INNER_COND:%.*]], i1 [[INNER_SEL]], i1 [[INNER_SEL_FALSEVAL:%.*]], !prof [[PROF2:![0-9]+]]
; CHECK-NEXT: ret i1 [[OUTER_SEL]]
;
- %outer.cond = select i1 %inner.cond, i1 %alt.cond, i1 false ; and %inner.cond, %alt.cond
+ %outer.cond = select i1 %inner.cond, i1 %alt.cond, i1 false ; and %inner.cond, %alt.cond, !prof !1
%not.inner.cond = xor i1 %inner.cond, -1
- %inner.sel = select i1 %not.inner.cond, i1 %inner.sel.falseval, i1 false
- %outer.sel = select i1 %outer.cond, i1 %outer.sel.trueval, i1 %inner.sel
+ %inner.sel = select i1 %not.inner.cond, i1 %inner.sel.falseval, i1 false, !prof !2
+ %outer.sel = select i1 %outer.cond, i1 %outer.sel.trueval, i1 %inner.sel, !prof !3
ret i1 %outer.sel
}
define i1 @orcond.010.inv.inner.cond.in.inner.sel(i1 %inner.cond, i1 %alt.cond, i1 %inner.sel.trueval, i1 %inner.sel.falseval, i1 %outer.sel.falseval) {
@@ -736,4 +736,11 @@ define i8 @test_mask_signed_fail(i8 %x) {
ret i8 %r
}
-
+!0 = !{!"function_entry_count", i32 10}
+!1 = !{!"branch_weights", i32 2, i32 3}
+!2 = !{!"branch_weights", i32 3, i32 2}
+!3 = !{!"branch_weights", i32 5, i32 7}
+;.
+; CHECK: [[PROF1]] = !{!"unknown", !"instcombine"}
+; CHECK: [[PROF2]] = !{!"branch_weights", i32 2, i32 3}
+;.
diff --git a/llvm/test/Transforms/InstCombine/select-select.ll b/llvm/test/Transforms/InstCombine/select-select.ll
index e4a7b8f73143d..be6aed456fbf3 100644
--- a/llvm/test/Transforms/InstCombine/select-select.ll
+++ b/llvm/test/Transforms/InstCombine/select-select.ll
@@ -16,19 +16,19 @@ define float @foo1(float %a) {
ret float %f
}
-define float @foo2(float %a) {
+define float @foo2(float %a) !prof !0 {
; CHECK-LABEL: @foo2(
; CHECK-NEXT: [[B:%.*]] = fcmp ule float [[A:%.*]], 0.000000e+00
; CHECK-NEXT: [[TMP1:%.*]] = fcmp olt float [[A]], 1.000000e+00
-; CHECK-NEXT: [[E:%.*]] = select i1 [[TMP1]], float [[A]], float 1.000000e+00
-; CHECK-NEXT: [[F:%.*]] = select i1 [[B]], float 0.000000e+00, float [[E]]
+; CHECK-NEXT: [[E:%.*]] = select i1 [[TMP1]], float [[A]], float 1.000000e+00, !prof [[PROF1:![0-9]+]]
+; CHECK-NEXT: [[F:%.*]] = select i1 [[B]], float 0.000000e+00, float [[E]], !prof [[PROF2:![0-9]+]]
; CHECK-NEXT: ret float [[F]]
;
%b = fcmp ogt float %a, 0.0
- %c = select i1 %b, float %a, float 0.0
+ %c = select i1 %b, float %a, float 0.0, !prof !1
%d = fcmp olt float %c, 1.0
- %e = select i1 %b, float %a, float 0.0
- %f = select i1 %d, float %e, float 1.0
+ %e = select i1 %b, float %a, float 0.0, !prof !2
+ %f = select i1 %d, float %e, float 1.0, !prof !3
ret float %f
}
@@ -535,8 +535,8 @@ define <2 x i8> @strong_order_cmp_eq_ugt_vector_poison3(<2 x i32> %a, <2 x i32>
define i32 @selectSelect11(i1 %cond1, i1 %cond2, i32 %var, i32 %defaultVal) !prof !0 {
; CHECK-LABEL: @selectSelect11(
-; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[COND2:%.*]], i1 true, i1 [[COND1:%.*]], !prof [[PROF1:![0-9]+]]
-; CHECK-NEXT: [[SEL2:%.*]] = select i1 [[TMP1]], i32 [[DEFAULTVAL:%.*]], i32 [[VAR:%.*]], !prof [[PROF2:![0-9]+]]
+; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[COND2:%.*]], i1 true, i1 [[COND1:%.*]], !prof [[PROF3:![0-9]+]]
+; CHECK-NEXT: [[SEL2:%.*]] = select i1 [[TMP1]], i32 [[DEFAULTVAL:%.*]], i32 [[VAR:%.*]], !prof [[PROF1]]
; CHECK-NEXT: ret i32 [[SEL2]]
;
%sel1 = select i1 %cond1, i32 %defaultVal, i32 %var, !prof !1
@@ -546,8 +546,8 @@ define i32 @selectSelect11(i1 %cond1, i1 %cond2, i32 %var, i32 %defaultVal) !pro
define i32 @selectSelect22(i1 %cond1, i1 %cond2, i32 %var, i32 %defaultVal) !prof !0 {
; CHECK-LABEL: @selectSelect22(
-; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[COND2:%.*]], i1 [[COND1:%.*]], i1 false, !prof [[PROF1]]
-; CHECK-NEXT: [[SEL2:%.*]] = select i1 [[TMP1]], i32 [[VAR:%.*]], i32 [[DEFAULTVAL:%.*]], !prof [[PROF2]]
+; CHECK-NEXT: [[TMP1:%.*]] = select i1 [[COND2:%.*]], i1 [[COND1:%.*]], i1 false, !prof [[PROF3]]
+; CHECK-NEXT: [[SEL2:%.*]] = select i1 [[TMP1]], i32 [[VAR:%.*]], i32 [[DEFAULTVAL:%.*]], !prof [[PROF1]]
; CHECK-NEXT: ret i32 [[SEL2]]
;
%sel1 = select i1 %cond1, i32 %var, i32 %defaultVal, !prof !1
@@ -558,8 +558,8 @@ define i32 @selectSelect22(i1 %cond1, i1 %cond2, i32 %var, i32 %defaultVal) !pro
define i32 @selectSelect12(i1 %cond1, i1 %cond2, i32 %var, i32 %defaultVal) !prof !0 {
; CHECK-LABEL: @selectSelect12(
; CHECK-NEXT: [[TMP1:%.*]] = xor i1 [[COND1:%.*]], true
-; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[COND2:%.*]], i1 [[TMP1]], i1 false, !prof [[PROF1]]
-; CHECK-NEXT: [[SEL2:%.*]] = select i1 [[TMP2]], i32 [[VAR:%.*]], i32 [[DEFAULTVAL:%.*]], !prof [[PROF2]]
+; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[COND2:%.*]], i1 [[TMP1]], i1 false, !prof [[PROF3]]
+; CHECK-NEXT: [[SEL2:%.*]] = select i1 [[TMP2]], i32 [[VAR:%.*]], i32 [[DEFAULTVAL:%.*]], !prof [[PROF1]]
; CHECK-NEXT: ret i32 [[SEL2]]
;
%sel1 = select i1 %cond1, i32 %defaultVal,i32 %var, !prof !1
@@ -570,8 +570,8 @@ define i32 @selectSelect12(i1 %cond1, i1 %cond2, i32 %var, i32 %defaultVal) !pro
define i32 @selectSelect21(i1 %cond1, i1 %cond2, i32 %var, i32 %defaultVal) !prof !0 {
; CHECK-LABEL: @selectSelect21(
; CHECK-NEXT: [[TMP1:%.*]] = xor i1 [[COND1:%.*]], true
-; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[COND2:%.*]], i1 true, i1 [[TMP1]], !prof [[PROF1]]
-; CHECK-NEXT: [[SEL2:%.*]] = select i1 [[TMP2]], i32 [[DEFAULTVAL:%.*]], i32 [[VAR:%.*]], !prof [[PROF2]]
+; CHECK-NEXT: [[TMP2:%.*]] = select i1 [[COND2:%.*]], i1 true, i1 [[TMP1]], !prof [[PROF3]]
+; CHECK-NEXT: [[SEL2:%.*]] = select i1 [[TMP2]], i32 [[DEFAULTVAL:%.*]], i32 [[VAR:%.*]], !prof [[PROF1]]
; CHECK-NEXT: ret i32 [[SEL2]]
;
%sel1 = select i1 %cond1, i32 %var, i32 %defaultVal, !prof !1
@@ -693,10 +693,12 @@ declare void @use32(i32)
!0 = !{!"function_entry_count", i64 1000}
!1 = !{!"branch_weights", i32 2, i32 3}
!2 = !{!"branch_weights", i32 5, i32 3}
+!3 = !{!"branch_weights", i32 7, i32 11}
;.
; CHECK: attributes #[[ATTR0:[0-9]+]] = { nocallback nocreateundeforpoison nofree nosync nounwind speculatable willreturn memory(none) }
;.
; CHECK: [[META0:![0-9]+]] = !{!"function_entry_count", i64 1000}
-; CHECK: [[PROF1]] = !{!"branch_weights", i32 5, i32 3}
-; CHECK: [[PROF2]] = !{!"unknown", !"instcombine"}
+; CHECK: [[PROF1]] = !{!"unknown", !"instcombine"}
+; CHECK: [[PROF2]] = !{!"branch_weights", i32 3, i32 5}
+; CHECK: [[PROF3]] = !{!"branch_weights", i32 5, i32 3}
;.
diff --git a/llvm/utils/profcheck-xfail.txt b/llvm/utils/profcheck-xfail.txt
index 42e30c2089be9..87c9baf6bc40a 100644
--- a/llvm/utils/profcheck-xfail.txt
+++ b/llvm/utils/profcheck-xfail.txt
@@ -41,7 +41,6 @@ Transforms/InstCombine/minmax-fold.ll
Transforms/InstCombine/minmax-intrinsics.ll
Transforms/InstCombine/mul-inseltpoison.ll
Transforms/InstCombine/mul.ll
-Transforms/InstCombine/nested-select.ll
Transforms/InstCombine/pow-1.ll
Transforms/InstCombine/pow-3.ll
Transforms/InstCombine/pow-sqrt.ll
``````````
</details>
https://github.com/llvm/llvm-project/pull/229635
More information about the llvm-commits
mailing list