Skip to content

Commit 1efaaf6

Browse files
committed
Remove m_OneUse check only for max, not min
If it is ever determined that min doesn't need one-use, then we can in fact remove that version entirely.
1 parent ec5f4a4 commit 1efaaf6

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2266,17 +2266,29 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
22662266

22672267
// max X, -X --> fabs X
22682268
// min X, -X --> -(fabs X)
2269-
// TODO: Remove one-use limitation? That is obviously better for max.
2270-
// It would be an extra instruction for min (fnabs), but that is
2271-
// still likely better for analysis and codegen.
2272-
if ((match(Arg0, m_OneUse(m_FNeg(m_Value(X)))) && Arg1 == X) ||
2273-
(match(Arg1, m_OneUse(m_FNeg(m_Value(X)))) && Arg0 == X)) {
2269+
if ((match(Arg0, m_OneUse(m_FNeg(m_Value(X)))) &&
2270+
match(Arg1, m_Specific(X))) ||
2271+
(match(Arg1, m_OneUse(m_FNeg(m_Value(X)))) &&
2272+
match(Arg0, m_Specific(X)))) {
22742273
Value *R = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, X, II);
22752274
if (IID == Intrinsic::minimum || IID == Intrinsic::minnum)
22762275
R = Builder.CreateFNegFMF(R, II);
22772276
return replaceInstUsesWith(*II, R);
22782277
}
22792278

2279+
// No one-use. Only for max.
2280+
// TODO: Remove one-use limitation? That is obviously better for max,
2281+
// hence why we don't check for one-use for that. However,
2282+
// it would be an extra instruction for min (fnabs), but
2283+
// that is still likely better for analysis and codegen. If so, delete
2284+
// one-use version
2285+
if ((((match(Arg0, m_FNeg(m_Value(X)))) && match(Arg1, m_Specific(X))) ||
2286+
(match(Arg1, m_FNeg(m_Value(X))) && match(Arg0, m_Specific(X)))) &&
2287+
IID != Intrinsic::minimum && IID != Intrinsic::minnum) {
2288+
Value *R = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, X, II);
2289+
return replaceInstUsesWith(*II, R);
2290+
}
2291+
22802292
break;
22812293
}
22822294
case Intrinsic::matrix_multiply: {

llvm/test/Transforms/InstCombine/maximum.ll

Lines changed: 1 addition & 1 deletion
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

llvm/test/Transforms/InstCombine/maxnum.ll

Lines changed: 1 addition & 1 deletion
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

0 commit comments

Comments
 (0)