Skip to content

[InstCombine] Add fold for fabs(-x) -> fabs(x) #94183

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2517,6 +2517,13 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
}
case Intrinsic::fabs: {
Value *Cond, *TVal, *FVal;
Value* Arg = II->getArgOperand(0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you replace all following uses of II->getArgOperand(0) with Arg?

Value* X;
// fabs (-X) --> fabs (X)
if (match(Arg, m_FNeg(m_Value(X)))) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Somewhere is a strip sign operations helper function that also covers copysign

return replaceInstUsesWith(CI, Builder.CreateUnaryIntrinsic(Intrinsic::fabs, X));
}

if (match(II->getArgOperand(0),
m_Select(m_Value(Cond), m_Value(TVal), m_Value(FVal)))) {
// fabs (select Cond, TrueC, FalseC) --> select Cond, AbsT, AbsF
Expand Down
57 changes: 57 additions & 0 deletions llvm/test/Transforms/InstCombine/fabs-fneg-fold.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
; RUN: opt -S -passes=instcombine %s | FileCheck %s
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please regenerate check lines with llvm/utils/update_test_checks.py


define float @fabs_fneg_f32(float %x) {
; CHECK-LABEL: define float @fabs_fneg_f32(
; CHECK-NEXT: [[FABS:%.*]] = call float @llvm.fabs.f32(float [[X:%.*]])
; CHECK-NEXT: ret float [[FABS]]
;
%neg = fneg float %x
%fabs = call float @llvm.fabs.f32(float %neg)
ret float %fabs
}

define <2 x float> @fabs_fneg_v2f32(<2 x float> %x) {
; CHECK-LABEL: define <2 x float> @fabs_fneg_v2f32(
; CHECK-NEXT: [[FABS:%.*]] = call <2 x float> @llvm.fabs.v2f32(<2 x float> [[X:%.*]])
; CHECK-NEXT: ret <2 x float> [[FABS]]
;
%neg = fneg <2 x float> %x
%fabs = call <2 x float> @llvm.fabs.v2f32(<2 x float> %neg)
ret <2 x float> %fabs
}

define double @fabs_fneg_f64(double %x) {
; CHECK-LABEL: define double @fabs_fneg_f64(
; CHECK-NEXT: [[FABS:%.*]] = call double @llvm.fabs.f64(double [[X:%.*]])
; CHECK-NEXT: ret double [[FABS]]
;
%neg = fneg double %x
%fabs = call double @llvm.fabs.f64(double %neg)
ret double %fabs
}

define <4 x double> @fabs_fneg_v4f64(<4 x double> %x) {
; CHECK-LABEL: define <4 x double> @fabs_fneg_v4f64(
; CHECK-NEXT: [[FABS:%.*]] = call <4 x double> @llvm.fabs.v4f64(<4 x double> [[X:%.*]])
; CHECK-NEXT: ret <4 x double> [[FABS]]
;
%neg = fneg <4 x double> %x
%fabs = call <4 x double> @llvm.fabs.v4f64(<4 x double> %neg)
ret <4 x double> %fabs
}

define half @fabs_fneg_f16(half %x) {
; CHECK-LABEL: define half @fabs_fneg_f16(
; CHECK-NEXT: [[FABS:%.*]] = call half @llvm.fabs.f16(half [[X:%.*]])
; CHECK-NEXT: ret half [[FABS]]
;
%neg = fneg half %x
%fabs = call half @llvm.fabs.f16(half %neg)
ret half %fabs
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should test flag preservation behavior

declare float @llvm.fabs.f32(float)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These declarations are unnecessary.

declare <2 x float> @llvm.fabs.v2f32(<2 x float>)
declare double @llvm.fabs.f64(double)
declare <4 x double> @llvm.fabs.v4f64(<4 x double>)
declare half @llvm.fabs.f16(half)
Loading