Skip to content

Commit c61d565

Browse files
authored
[AArch64] Set scalar fneg to free for fnmul (#104814)
A fneg(fmul(..)) or fmul(fneg(..)) can be folded into a fnmul under AArch64. https://clang.godbolt.org/z/znPj34Mae This discounts the cost of the fneg in such patterns to be free.
1 parent 1c9d8a6 commit c61d565

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

llvm/lib/Target/AArch64/AArch64TargetTransformInfo.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3242,6 +3242,15 @@ InstructionCost AArch64TTIImpl::getArithmeticInstrCost(
32423242
return LT.first;
32433243

32443244
case ISD::FNEG:
3245+
// Scalar fmul(fneg) or fneg(fmul) can be converted to fnmul
3246+
if ((Ty->isFloatTy() || Ty->isDoubleTy() ||
3247+
(Ty->isHalfTy() && ST->hasFullFP16())) &&
3248+
CxtI &&
3249+
((CxtI->hasOneUse() &&
3250+
match(*CxtI->user_begin(), m_FMul(m_Value(), m_Value()))) ||
3251+
match(CxtI->getOperand(0), m_FMul(m_Value(), m_Value()))))
3252+
return 0;
3253+
[[fallthrough]];
32453254
case ISD::FADD:
32463255
case ISD::FSUB:
32473256
// Increase the cost for half and bfloat types if not architecturally

llvm/test/Analysis/CostModel/AArch64/arith-fp.ll

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ define i32 @fneg(i32 %arg) {
133133

134134
define i32 @fmulfneg(i32 %arg) {
135135
; CHECK-LABEL: 'fmulfneg'
136-
; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %F16 = fneg half undef
136+
; CHECK-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %F16 = fneg half undef
137137
; CHECK-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %F16M = fmul half %F16, undef
138138
; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %V2F16 = fneg <2 x half> undef
139139
; CHECK-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %V2F16M = fmul <2 x half> %V2F16, undef
@@ -143,15 +143,15 @@ define i32 @fmulfneg(i32 %arg) {
143143
; CHECK-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %V8F16M = fmul <8 x half> %V8F16, undef
144144
; CHECK-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %V16F16 = fneg <16 x half> undef
145145
; CHECK-NEXT: Cost Model: Found an estimated cost of 4 for instruction: %V16F16M = fmul <16 x half> %V16F16, undef
146-
; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %F32 = fneg float undef
146+
; CHECK-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %F32 = fneg float undef
147147
; CHECK-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %F32M = fmul float %F32, undef
148148
; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %V2F32 = fneg <2 x float> undef
149149
; CHECK-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %V2F32M = fmul <2 x float> %V2F32, undef
150150
; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %V4F32 = fneg <4 x float> undef
151151
; CHECK-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %V4F32M = fmul <4 x float> %V4F32, undef
152152
; CHECK-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %V8F32 = fneg <8 x float> undef
153153
; CHECK-NEXT: Cost Model: Found an estimated cost of 4 for instruction: %V8F32M = fmul <8 x float> %V8F32, undef
154-
; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %F64 = fneg double undef
154+
; CHECK-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %F64 = fneg double undef
155155
; CHECK-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %F64M = fmul double %F64, undef
156156
; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %V2F64 = fneg <2 x double> undef
157157
; CHECK-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %V2F64M = fmul <2 x double> %V2F64, undef
@@ -192,7 +192,7 @@ define i32 @fmulfneg(i32 %arg) {
192192
define i32 @fnegfmul(i32 %arg) {
193193
; CHECK-LABEL: 'fnegfmul'
194194
; CHECK-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %F16M = fmul half undef, undef
195-
; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %F16 = fneg half %F16M
195+
; CHECK-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %F16 = fneg half %F16M
196196
; CHECK-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %V2F16M = fmul <2 x half> undef, undef
197197
; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %V2F16 = fneg <2 x half> %V2F16M
198198
; CHECK-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %V4F16M = fmul <4 x half> undef, undef
@@ -202,15 +202,15 @@ define i32 @fnegfmul(i32 %arg) {
202202
; CHECK-NEXT: Cost Model: Found an estimated cost of 4 for instruction: %V16F16M = fmul <16 x half> undef, undef
203203
; CHECK-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %V16F16 = fneg <16 x half> %V16F16M
204204
; CHECK-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %F32M = fmul float undef, undef
205-
; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %F32 = fneg float %F32M
205+
; CHECK-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %F32 = fneg float %F32M
206206
; CHECK-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %V2F32M = fmul <2 x float> undef, undef
207207
; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %V2F32 = fneg <2 x float> %V2F32M
208208
; CHECK-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %V4F32M = fmul <4 x float> undef, undef
209209
; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %V4F32 = fneg <4 x float> %V4F32M
210210
; CHECK-NEXT: Cost Model: Found an estimated cost of 4 for instruction: %V8F32M = fmul <8 x float> undef, undef
211211
; CHECK-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %V8F32 = fneg <8 x float> %V8F32M
212212
; CHECK-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %F64M = fmul double undef, undef
213-
; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %F64 = fneg double %F64M
213+
; CHECK-NEXT: Cost Model: Found an estimated cost of 0 for instruction: %F64 = fneg double %F64M
214214
; CHECK-NEXT: Cost Model: Found an estimated cost of 2 for instruction: %V2F64M = fmul <2 x double> undef, undef
215215
; CHECK-NEXT: Cost Model: Found an estimated cost of 1 for instruction: %V2F64 = fneg <2 x double> %V2F64M
216216
; CHECK-NEXT: Cost Model: Found an estimated cost of 4 for instruction: %V4F64M = fmul <4 x double> undef, undef

0 commit comments

Comments
 (0)