Skip to content

Commit 06c8a90

Browse files
committed
[Transforms] Remove m_OneUse requirement for max, but not min
If it is ever determined that min doesn't need one-use, then we can remove the one-use requirement entirely.
1 parent 218aa72 commit 06c8a90

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2288,11 +2288,18 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
22882288

22892289
// max X, -X --> fabs X
22902290
// min X, -X --> -(fabs X)
2291-
// TODO: Remove one-use limitation? That is obviously better for max.
2292-
// It would be an extra instruction for min (fnabs), but that is
2293-
// still likely better for analysis and codegen.
2294-
if ((match(Arg0, m_OneUse(m_FNeg(m_Value(X)))) && Arg1 == X) ||
2295-
(match(Arg1, m_OneUse(m_FNeg(m_Value(X)))) && Arg0 == X)) {
2291+
// TODO: Remove one-use limitation? That is obviously better for max,
2292+
// hence why we don't check for one-use for that. However,
2293+
// it would be an extra instruction for min (fnabs), but
2294+
// that is still likely better for analysis and codegen.
2295+
auto IsMinMaxOrXNegX = [IID, &X](Value *Op0, Value *Op1) {
2296+
if (match(Op0, m_FNeg(m_Value(X))) && match(Op1, m_Specific(X)))
2297+
return Op0->hasOneUse() ||
2298+
(IID != Intrinsic::minimum && IID != Intrinsic::minnum);
2299+
return false;
2300+
};
2301+
2302+
if (IsMinMaxOrXNegX(Arg0, Arg1) || IsMinMaxOrXNegX(Arg1, Arg0)) {
22962303
Value *R = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, X, II);
22972304
if (IID == Intrinsic::minimum || IID == Intrinsic::minnum)
22982305
R = Builder.CreateFNegFMF(R, II);

llvm/test/Transforms/InstCombine/maximum.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ define float @negated_op_extra_use(float %x) {
436436
; CHECK-LABEL: @negated_op_extra_use(
437437
; CHECK-NEXT: [[NEGX:%.*]] = fneg float [[X:%.*]]
438438
; CHECK-NEXT: call void @use(float [[NEGX]])
439-
; CHECK-NEXT: [[R:%.*]] = call float @llvm.maximum.f32(float [[NEGX]], float [[X]])
439+
; CHECK-NEXT: [[R:%.*]] = call float @llvm.fabs.f32(float [[X]])
440440
; CHECK-NEXT: ret float [[R]]
441441
;
442442
%negx = fneg float %x
@@ -449,7 +449,7 @@ define float @negated_op_extra_use_comm(float %x) {
449449
; CHECK-LABEL: @negated_op_extra_use_comm(
450450
; CHECK-NEXT: [[NEGX:%.*]] = fneg float [[X:%.*]]
451451
; CHECK-NEXT: call void @use(float [[NEGX]])
452-
; CHECK-NEXT: [[R:%.*]] = call float @llvm.maximum.f32(float [[X]], float [[NEGX]])
452+
; CHECK-NEXT: [[R:%.*]] = call float @llvm.fabs.f32(float [[X]])
453453
; CHECK-NEXT: ret float [[R]]
454454
;
455455
%negx = fneg float %x

llvm/test/Transforms/InstCombine/maxnum.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -458,7 +458,7 @@ define float @negated_op_extra_use(float %x) {
458458
; CHECK-LABEL: @negated_op_extra_use(
459459
; CHECK-NEXT: [[NEGX:%.*]] = fneg float [[X:%.*]]
460460
; CHECK-NEXT: call void @use(float [[NEGX]])
461-
; CHECK-NEXT: [[R:%.*]] = call float @llvm.maxnum.f32(float [[NEGX]], float [[X]])
461+
; CHECK-NEXT: [[R:%.*]] = call float @llvm.fabs.f32(float [[X]])
462462
; CHECK-NEXT: ret float [[R]]
463463
;
464464
%negx = fneg float %x
@@ -471,7 +471,7 @@ define float @negated_op_extra_use_comm(float %x) {
471471
; CHECK-LABEL: @negated_op_extra_use_comm(
472472
; CHECK-NEXT: [[NEGX:%.*]] = fneg float [[X:%.*]]
473473
; CHECK-NEXT: call void @use(float [[NEGX]])
474-
; CHECK-NEXT: [[R:%.*]] = call float @llvm.maxnum.f32(float [[X]], float [[NEGX]])
474+
; CHECK-NEXT: [[R:%.*]] = call float @llvm.fabs.f32(float [[X]])
475475
; CHECK-NEXT: ret float [[R]]
476476
;
477477
%negx = fneg float %x

0 commit comments

Comments
 (0)