Skip to content

Commit

Permalink
Remove m_OneUse requirement only for max, not min
Browse files Browse the repository at this point in the history
If it is ever determined that min doesn't need one-use, then we can in fact remove that version entirely.
  • Loading branch information
Rose committed Feb 22, 2024
1 parent ae3e142 commit 5a8264c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
28 changes: 21 additions & 7 deletions llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2277,14 +2277,28 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {

// max X, -X --> fabs X
// min X, -X --> -(fabs X)
// TODO: Remove one-use limitation? That is obviously better for max.
// It would be an extra instruction for min (fnabs), but that is
// still likely better for analysis and codegen.
if ((match(Arg0, m_OneUse(m_FNeg(m_Value(X)))) && Arg1 == X) ||
(match(Arg1, m_OneUse(m_FNeg(m_Value(X)))) && Arg0 == X)) {

// No one-use. Only for max.
// TODO: Remove one-use limitation? That is obviously better for max,
// hence why we don't check for one-use for that. However,
// it would be an extra instruction for min (fnabs), but
// that is still likely better for analysis and codegen.
// If so, then allow this if-statement clause to handle min,
// and delete the clause below this one.
if ((((match(Arg0, m_FNeg(m_Value(X)))) && match(Arg1, m_Specific(X))) ||
(match(Arg1, m_FNeg(m_Value(X))) && match(Arg0, m_Specific(X)))) &&
IID != Intrinsic::minimum && IID != Intrinsic::minnum) {
Value *R = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, X, II);
return replaceInstUsesWith(*II, R);
}

// One-use version for min.
if ((match(Arg0, m_OneUse(m_FNeg(m_Value(X)))) &&
match(Arg1, m_Specific(X))) ||
(match(Arg1, m_OneUse(m_FNeg(m_Value(X)))) &&
match(Arg0, m_Specific(X)))) {
Value *R = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, X, II);
if (IID == Intrinsic::minimum || IID == Intrinsic::minnum)
R = Builder.CreateFNegFMF(R, II);
R = Builder.CreateFNegFMF(R, II);
return replaceInstUsesWith(*II, R);
}

Expand Down
2 changes: 1 addition & 1 deletion llvm/test/Transforms/InstCombine/maximum.ll
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ define float @negated_op_extra_use(float %x) {
; CHECK-LABEL: @negated_op_extra_use(
; CHECK-NEXT: [[NEGX:%.*]] = fneg float [[X:%.*]]
; CHECK-NEXT: call void @use(float [[NEGX]])
; CHECK-NEXT: [[R:%.*]] = call float @llvm.maximum.f32(float [[NEGX]], float [[X]])
; CHECK-NEXT: [[R:%.*]] = call float @llvm.fabs.f32(float [[X]])
; CHECK-NEXT: ret float [[R]]
;
%negx = fneg float %x
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/Transforms/InstCombine/maxnum.ll
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ define float @negated_op_extra_use(float %x) {
; CHECK-LABEL: @negated_op_extra_use(
; CHECK-NEXT: [[NEGX:%.*]] = fneg float [[X:%.*]]
; CHECK-NEXT: call void @use(float [[NEGX]])
; CHECK-NEXT: [[R:%.*]] = call float @llvm.maxnum.f32(float [[NEGX]], float [[X]])
; CHECK-NEXT: [[R:%.*]] = call float @llvm.fabs.f32(float [[X]])
; CHECK-NEXT: ret float [[R]]
;
%negx = fneg float %x
Expand Down

0 comments on commit 5a8264c

Please sign in to comment.