aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSnehasish Kumar <snehasishk@google.com>2026-02-03 03:40:55 +0000
committerSnehasish Kumar <snehasishk@google.com>2026-02-10 22:50:07 +0000
commit30d4ba04003054af2690072a7985aeee0a674623 (patch)
tree08e18d094e8cea228abcf167e2d5da3eb37aa50d
parent4308916c5a90d7656f9246cffb3c659fa7747027 (diff)
downloadllvm-users/snehasish/fix-clamp-float-prof.tar.gz
llvm-users/snehasish/fix-clamp-float-prof.tar.bz2
llvm-users/snehasish/fix-clamp-float-prof.zip
InstCombine: Propagate profile metadata in floating point clamp and swap canonicalizationusers/snehasish/fix-clamp-float-prof
-rw-r--r--llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp19
-rw-r--r--llvm/test/Transforms/InstCombine/clamp-to-minmax.ll17
-rw-r--r--llvm/utils/profcheck-xfail.txt4
3 files changed, 31 insertions, 9 deletions
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
index 39fcb70a64b5..16ae4ecc1c78 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -4430,6 +4430,9 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
FMF.setNoInfs(true);
Value *NewSel =
Builder.CreateSelectFMF(NewCond, FalseVal, TrueVal, FMF);
+ if (!ProfcheckDisableMetadataFixes)
+ if (auto *NewSelI = dyn_cast<Instruction>(NewSel))
+ NewSelI->copyMetadata(SI);
return replaceInstUsesWith(SI, NewSel);
}
}
@@ -4634,10 +4637,22 @@ Instruction *InstCombinerImpl::visitSelectInst(SelectInst &SI) {
if (CmpInst::isIntPredicate(MinMaxPred))
Cmp = Builder.CreateICmp(MinMaxPred, LHS, RHS);
else
- Cmp = Builder.CreateFCmpFMF(MinMaxPred, LHS, RHS,
+ Cmp = Builder.CreateFCmpFMF(MinMaxPred, LHS, RHS,
cast<Instruction>(SI.getCondition()));
- Value *NewSI = Builder.CreateSelect(Cmp, LHS, RHS, SI.getName(), &SI);
+ Value *NewSI;
+ if (!ProfcheckDisableMetadataFixes) {
+ NewSI =
+ createSelectInstWithUnknownProfile(Cmp, LHS, RHS, SI.getName());
+ auto *NewInst = cast<Instruction>(NewSI);
+ NewInst->copyMetadata(SI);
+ if (auto *SIFPOp = dyn_cast<FPMathOperator>(&SI))
+ if (isa<FPMathOperator>(NewInst))
+ NewInst->setFastMathFlags(SIFPOp->getFastMathFlags());
+ Builder.Insert(NewInst);
+ } else {
+ NewSI = Builder.CreateSelect(Cmp, LHS, RHS, SI.getName(), &SI);
+ }
if (!IsCastNeeded)
return replaceInstUsesWith(SI, NewSI);
diff --git a/llvm/test/Transforms/InstCombine/clamp-to-minmax.ll b/llvm/test/Transforms/InstCombine/clamp-to-minmax.ll
index 0ccaa9c39365..3a1e460348a5 100644
--- a/llvm/test/Transforms/InstCombine/clamp-to-minmax.ll
+++ b/llvm/test/Transforms/InstCombine/clamp-to-minmax.ll
@@ -1,20 +1,20 @@
-; 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 < %s -passes=instcombine -S | FileCheck %s
; RUN: opt < %s -passes=instcombine -use-constant-fp-for-fixed-length-splat -use-constant-int-for-fixed-length-splat -S | FileCheck %s
; (X < C1) ? C1 : MIN(X, C2)
-define float @clamp_float_fast_ordered_strict_maxmin(float %x) {
+define float @clamp_float_fast_ordered_strict_maxmin(float %x) !prof !0 {
; CHECK-LABEL: @clamp_float_fast_ordered_strict_maxmin(
; CHECK-NEXT: [[CMP2:%.*]] = fcmp fast olt float [[X:%.*]], 2.550000e+02
-; CHECK-NEXT: [[MIN:%.*]] = select i1 [[CMP2]], float [[X]], float 2.550000e+02
+; CHECK-NEXT: [[MIN:%.*]] = select i1 [[CMP2]], float [[X]], float 2.550000e+02, !prof [[PROF1:![0-9]+]]
; CHECK-NEXT: [[DOTINV:%.*]] = fcmp fast oge float [[MIN]], 1.000000e+00
-; CHECK-NEXT: [[R1:%.*]] = select nnan ninf i1 [[DOTINV]], float [[MIN]], float 1.000000e+00
+; CHECK-NEXT: [[R1:%.*]] = select nnan ninf i1 [[DOTINV]], float [[MIN]], float 1.000000e+00, !prof [[PROF1]]
; CHECK-NEXT: ret float [[R1]]
;
%cmp2 = fcmp fast olt float %x, 255.0
- %min = select i1 %cmp2, float %x, float 255.0
+ %min = select i1 %cmp2, float %x, float 255.0, !prof !1
%cmp1 = fcmp fast olt float %x, 1.0
- %r = select i1 %cmp1, float 1.0, float %min
+ %r = select i1 %cmp1, float 1.0, float %min, !prof !1
ret float %r
}
@@ -590,3 +590,8 @@ define <2 x float> @mixed_clamp_to_float_vec(<2 x i32> %x) {
%r = select <2 x i1> %lo_cmp, <2 x float> <float 1.0, float 1.0>, <2 x float> %f_min
ret <2 x float> %r
}
+!0 = !{!"function_entry_count", i64 1000}
+!1 = !{!"branch_weights", i32 10, i32 20}
+;.
+; CHECK: [[PROF1]] = !{!"branch_weights", i32 10, i32 20}
+;.
diff --git a/llvm/utils/profcheck-xfail.txt b/llvm/utils/profcheck-xfail.txt
index 45dee8a51c9c..8cf70ff61f00 100644
--- a/llvm/utils/profcheck-xfail.txt
+++ b/llvm/utils/profcheck-xfail.txt
@@ -215,7 +215,9 @@ Transforms/InstCombine/binop-cast.ll
Transforms/InstCombine/binop-select-cast-of-select-cond.ll
Transforms/InstCombine/bit-checks.ll
Transforms/InstCombine/canonicalize-clamp-like-pattern-between-zero-and-positive-threshold.ll
-Transforms/InstCombine/clamp-to-minmax.ll
+Transforms/InstCombine/cttz.ll
+Transforms/InstCombine/div.ll
+Transforms/InstCombine/div-shift.ll
Transforms/InstCombine/fabs.ll
Transforms/InstCombine/fcmp-select.ll
Transforms/InstCombine/ffs-1.ll