[llvm] [InstCombine] Preserve profile metadata when folding fneg selects (PR #228754)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Oct 3 11:12:55 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-transforms
Author: Alok Kumar Sharma (alokkrsharma)
<details>
<summary>Changes</summary>
Reuse select metadata in the fneg transformations while honoring ProfcheckDisableMetadataFixes for profcheck ablations.
---
Full diff: https://github.com/llvm/llvm-project/pull/228754.diff
2 Files Affected:
- (modified) llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp (+13-3)
- (modified) llvm/test/Transforms/InstCombine/fneg.ll (+23-8)
``````````diff
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
index 281e3b353f6dd..3ebcbe44c4c3d 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
@@ -39,6 +39,10 @@ using namespace PatternMatch;
#define DEBUG_TYPE "instcombine"
+namespace llvm {
+extern cl::opt<bool> ProfcheckDisableMetadataFixes;
+}
+
namespace {
/// Class representing coefficient of floating-point addend.
@@ -3244,14 +3248,18 @@ Instruction *InstCombinerImpl::visitFNeg(UnaryOperator &I) {
Value *P;
if (match(X, m_FNeg(m_Value(P)))) {
Value *NegY = Builder.CreateFNegFMF(Y, &I, Y->getName() + ".neg");
- SelectInst *NewSel = SelectInst::Create(Cond, P, NegY);
+ SelectInst *NewSel = SelectInst::Create(
+ Cond, P, NegY, "", nullptr,
+ ProfcheckDisableMetadataFixes ? nullptr : cast<SelectInst>(Op));
propagateSelectFMF(NewSel, P == Y);
return NewSel;
}
// -(Cond ? X : -P) --> Cond ? -X : P
if (match(Y, m_FNeg(m_Value(P)))) {
Value *NegX = Builder.CreateFNegFMF(X, &I, X->getName() + ".neg");
- SelectInst *NewSel = SelectInst::Create(Cond, NegX, P);
+ SelectInst *NewSel = SelectInst::Create(
+ Cond, NegX, P, "", nullptr,
+ ProfcheckDisableMetadataFixes ? nullptr : cast<SelectInst>(Op));
propagateSelectFMF(NewSel, P == X);
return NewSel;
}
@@ -3261,7 +3269,9 @@ Instruction *InstCombinerImpl::visitFNeg(UnaryOperator &I) {
if (match(X, m_ImmConstant()) || match(Y, m_ImmConstant())) {
Value *NegX = Builder.CreateFNegFMF(X, &I, X->getName() + ".neg");
Value *NegY = Builder.CreateFNegFMF(Y, &I, Y->getName() + ".neg");
- SelectInst *NewSel = SelectInst::Create(Cond, NegX, NegY);
+ SelectInst *NewSel = SelectInst::Create(
+ Cond, NegX, NegY, "", nullptr,
+ ProfcheckDisableMetadataFixes ? nullptr : cast<SelectInst>(Op));
propagateSelectFMF(NewSel, /*CommonOperand=*/true);
return NewSel;
}
diff --git a/llvm/test/Transforms/InstCombine/fneg.ll b/llvm/test/Transforms/InstCombine/fneg.ll
index d863871eb3fc8..2a8985a793397 100644
--- a/llvm/test/Transforms/InstCombine/fneg.ll
+++ b/llvm/test/Transforms/InstCombine/fneg.ll
@@ -1,10 +1,13 @@
-; 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
; RUN: opt < %s -passes=instcombine -S | FileCheck %s
declare float @llvm.ldexp.f32.i32(float, i32)
declare <2 x float> @llvm.ldexp.v2f32.v2i32(<2 x float>, <2 x i32>)
declare void @use(float)
+;.
+; CHECK: @g = external global i16, align 1
+;.
define float @fneg_fneg(float %a) {
;
; CHECK-LABEL: @fneg_fneg(
@@ -696,11 +699,11 @@ define float @fake_fneg_nsz_fadd_constant_expr(float %x) {
define float @select_fneg_true(float %x, float %y, i1 %b) {
; CHECK-LABEL: @select_fneg_true(
; CHECK-NEXT: [[Y_NEG:%.*]] = fneg float [[Y:%.*]]
-; CHECK-NEXT: [[R:%.*]] = select i1 [[B:%.*]], float [[X:%.*]], float [[Y_NEG]]
+; CHECK-NEXT: [[R:%.*]] = select i1 [[B:%.*]], float [[X:%.*]], float [[Y_NEG]], !prof [[PROF1:![0-9]+]]
; CHECK-NEXT: ret float [[R]]
;
%nx = fneg float %x
- %s = select i1 %b, float %nx, float %y
+ %s = select i1 %b, float %nx, float %y, !prof !1
%r = fneg float %s
ret float %r
}
@@ -720,11 +723,11 @@ define <2 x float> @select_fneg_false(<2 x float> %x, <2 x float> %y, <2 x i1> %
define float @select_fneg_false_no_nsz(float %x, float %y, i1 %b) {
; CHECK-LABEL: @select_fneg_false_no_nsz(
; CHECK-NEXT: [[X_NEG:%.*]] = fneg nnan ninf nsz float [[X:%.*]]
-; CHECK-NEXT: [[R:%.*]] = select nnan ninf i1 [[B:%.*]], float [[X_NEG]], float [[Y:%.*]]
+; CHECK-NEXT: [[R:%.*]] = select nnan ninf i1 [[B:%.*]], float [[X_NEG]], float [[Y:%.*]], !prof [[PROF2:![0-9]+]]
; CHECK-NEXT: ret float [[R]]
;
%ny = fneg float %y
- %s = select i1 %b, float %x, float %ny
+ %s = select i1 %b, float %x, float %ny, !prof !2
%r = fneg ninf nnan nsz float %s
ret float %r
}
@@ -1057,7 +1060,7 @@ define float @fneg_ldexp_contract(float %x, i32 %n) {
define float @fneg_ldexp_metadata(float %x, i32 %n) {
; CHECK-LABEL: @fneg_ldexp_metadata(
; CHECK-NEXT: [[TMP1:%.*]] = fneg float [[X:%.*]]
-; CHECK-NEXT: [[NEG:%.*]] = call float @llvm.ldexp.f32.i32(float [[TMP1]], i32 [[N:%.*]]), !arst [[META1:![0-9]+]]
+; CHECK-NEXT: [[NEG:%.*]] = call float @llvm.ldexp.f32.i32(float [[TMP1]], i32 [[N:%.*]]), !arst [[META3:![0-9]+]]
; CHECK-NEXT: ret float [[NEG]]
;
%ldexp = call float @llvm.ldexp.f32.i32(float %x, i32 %n), !arst !0
@@ -1088,10 +1091,10 @@ define <2 x float> @test_fneg_vec(<2 x i1> %cond) {
define float @test_fneg_select_var_constant(i1 %cond, float %x) {
; CHECK-LABEL: @test_fneg_select_var_constant(
; CHECK-NEXT: [[X_NEG:%.*]] = fneg float [[X:%.*]]
-; CHECK-NEXT: [[NEG:%.*]] = select i1 [[COND:%.*]], float [[X_NEG]], float 0.000000e+00
+; CHECK-NEXT: [[NEG:%.*]] = select i1 [[COND:%.*]], float [[X_NEG]], float 0.000000e+00, !prof [[PROF4:![0-9]+]]
; CHECK-NEXT: ret float [[NEG]]
;
- %sel1 = select i1 %cond, float %x, float -0.0
+ %sel1 = select i1 %cond, float %x, float -0.0, !prof !3
%neg = fneg float %sel1
ret float %neg
}
@@ -1381,3 +1384,15 @@ define <2 x float> @test_fneg_mul_combine_contract_ninf_with_vec_const3(<2 x flo
}
!0 = !{}
+!1 = !{!"branch_weights", i32 8, i32 2}
+!2 = !{!"branch_weights", i32 3, i32 7}
+!3 = !{!"branch_weights", i32 5, i32 5}
+;.
+; CHECK: attributes #[[ATTR0:[0-9]+]] = { nocallback nocreateundeforpoison nofree nosync nounwind speculatable willreturn memory(none) }
+;.
+; CHECK: [[META0]] = !{float 2.500000e+00}
+; CHECK: [[PROF1]] = !{!"branch_weights", i32 8, i32 2}
+; CHECK: [[PROF2]] = !{!"branch_weights", i32 3, i32 7}
+; CHECK: [[META3]] = !{}
+; CHECK: [[PROF4]] = !{!"branch_weights", i32 5, i32 5}
+;.
``````````
</details>
https://github.com/llvm/llvm-project/pull/228754
More information about the llvm-commits
mailing list