Skip to content

Commit 081882e

Browse files
authored
[InstCombine] Remove m_OneUse requirement for max, but not min (#81505)
If it is ever determined that min doesn't need one-use, then we can remove the one-use requirement entirely.
1 parent 53a656d commit 081882e

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
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: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,11 +436,24 @@ 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
443443
call void @use(float %negx)
444444
%r = call float @llvm.maximum.f32(float %negx, float %x)
445445
ret float %r
446446
}
447+
448+
define float @negated_op_extra_use_comm(float %x) {
449+
; CHECK-LABEL: @negated_op_extra_use_comm(
450+
; CHECK-NEXT: [[NEGX:%.*]] = fneg float [[X:%.*]]
451+
; CHECK-NEXT: call void @use(float [[NEGX]])
452+
; CHECK-NEXT: [[R:%.*]] = call float @llvm.fabs.f32(float [[X]])
453+
; CHECK-NEXT: ret float [[R]]
454+
;
455+
%negx = fneg float %x
456+
call void @use(float %negx)
457+
%r = call float @llvm.maximum.f32(float %x, float %negx)
458+
ret float %r
459+
}

llvm/test/Transforms/InstCombine/maxnum.ll

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -458,11 +458,24 @@ 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
465465
call void @use(float %negx)
466466
%r = call float @llvm.maxnum.f32(float %negx, float %x)
467467
ret float %r
468468
}
469+
470+
define float @negated_op_extra_use_comm(float %x) {
471+
; CHECK-LABEL: @negated_op_extra_use_comm(
472+
; CHECK-NEXT: [[NEGX:%.*]] = fneg float [[X:%.*]]
473+
; CHECK-NEXT: call void @use(float [[NEGX]])
474+
; CHECK-NEXT: [[R:%.*]] = call float @llvm.fabs.f32(float [[X]])
475+
; CHECK-NEXT: ret float [[R]]
476+
;
477+
%negx = fneg float %x
478+
call void @use(float %negx)
479+
%r = call float @llvm.maxnum.f32(float %x, float %negx)
480+
ret float %r
481+
}

0 commit comments

Comments
 (0)