[llvm] [InstCombine] Preserve profile metadata when folding fneg selects (PR #228754)
Alok Kumar Sharma via llvm-commits
llvm-commits at lists.llvm.org
Sat Oct 3 11:12:12 PDT 2026
https://github.com/alokkrsharma created https://github.com/llvm/llvm-project/pull/228754
Reuse select metadata in the fneg transformations while honoring ProfcheckDisableMetadataFixes for profcheck ablations.
>From 05b4461defc8ef9d7ecb028991ae06a8b779bc26 Mon Sep 17 00:00:00 2001
From: Alok Kumar Sharma <AlokKumar.Sharma at amd.com>
Date: Sat, 3 Oct 2026 23:29:07 +0530
Subject: [PATCH] [InstCombine] Preserve profile metadata when folding fneg
selects
Reuse select metadata in the fneg transformations while honoring
ProfcheckDisableMetadataFixes for profcheck ablations.
---
.../InstCombine/InstCombineAddSub.cpp | 16 ++++++++--
llvm/test/Transforms/InstCombine/fneg.ll | 31 ++++++++++++++-----
2 files changed, 36 insertions(+), 11 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp b/llvm/lib/Transforms/InstCombine/InstCombineAddSub.cpp
index 281e3b353f6dd2..3ebcbe44c4c3d9 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 d863871eb3fc8d..2a8985a793397b 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}
+;.
More information about the llvm-commits
mailing list