Skip to content

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

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

Merged
merged 6 commits into from
Jun 27, 2024
Merged
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
14 changes: 10 additions & 4 deletions llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2511,16 +2511,22 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) {
}
case Intrinsic::fabs: {
Value *Cond, *TVal, *FVal;
if (match(II->getArgOperand(0),
m_Select(m_Value(Cond), m_Value(TVal), m_Value(FVal)))) {
Value *Arg = II->getArgOperand(0);
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.

We can merge this fold with fabs(copysign(x, y)) -> fabs(x).

```suggestion
    if (match(Arg, m_CombineOr(m_FNeg(m_Value(X)), m_CopySign(m_Value(X), m_Value(Unused))))) {

CallInst *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, X, II);
return replaceInstUsesWith(CI, Fabs);
}

if (match(Arg, m_Select(m_Value(Cond), m_Value(TVal), m_Value(FVal)))) {
// fabs (select Cond, TrueC, FalseC) --> select Cond, AbsT, AbsF
if (isa<Constant>(TVal) || isa<Constant>(FVal)) {
CallInst *AbsT = Builder.CreateCall(II->getCalledFunction(), {TVal});
CallInst *AbsF = Builder.CreateCall(II->getCalledFunction(), {FVal});
SelectInst *SI = SelectInst::Create(Cond, AbsT, AbsF);
FastMathFlags FMF1 = II->getFastMathFlags();
FastMathFlags FMF2 =
cast<SelectInst>(II->getArgOperand(0))->getFastMathFlags();
FastMathFlags FMF2 = cast<SelectInst>(Arg)->getFastMathFlags();
FMF2.setNoSignedZeros(false);
SI->setFastMathFlags(FMF1 | FMF2);
return SI;
Expand Down
186 changes: 186 additions & 0 deletions llvm/test/Transforms/InstCombine/fabs-fneg-fold.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 5
; RUN: opt -S -passes=instcombine %s | FileCheck %s

define float @fabs_fneg_basic(float %x) {
; CHECK-LABEL: define float @fabs_fneg_basic(
; CHECK-SAME: float [[X:%.*]]) {
; 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-SAME: <2 x float> [[X:%.*]]) {
; 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-SAME: double [[X:%.*]]) {
; 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-SAME: <4 x double> [[X:%.*]]) {
; 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-SAME: half [[X:%.*]]) {
; 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
}

define float @fabs_copysign_nnan(float %x, float %y) {
; CHECK-LABEL: define float @fabs_copysign_nnan(
; CHECK-SAME: float [[X:%.*]], float [[Y:%.*]]) {
; CHECK-NEXT: [[FABS:%.*]] = call nnan float @llvm.fabs.f32(float [[X]])
; CHECK-NEXT: ret float [[FABS]]
;
%copysign = call float @llvm.copysign.f32(float %x, float %y)
%fabs = call nnan float @llvm.fabs.f32(float %copysign)
ret float %fabs
}


define float @fabs_copysign_ninf(float %x, float %y) {
; CHECK-LABEL: define float @fabs_copysign_ninf(
; CHECK-SAME: float [[X:%.*]], float [[Y:%.*]]) {
; CHECK-NEXT: [[FABS:%.*]] = call ninf float @llvm.fabs.f32(float [[X]])
; CHECK-NEXT: ret float [[FABS]]
;
%copysign = call float @llvm.copysign.f32(float %x, float %y)
%fabs = call ninf float @llvm.fabs.f32(float %copysign)
ret float %fabs
}

define float @fabs_copysign_nsz(float %x, float %y) {
; CHECK-LABEL: define float @fabs_copysign_nsz(
; CHECK-SAME: float [[X:%.*]], float [[Y:%.*]]) {
; CHECK-NEXT: [[FABS:%.*]] = call nsz float @llvm.fabs.f32(float [[X]])
; CHECK-NEXT: ret float [[FABS]]
;
%copysign = call float @llvm.copysign.f32(float %x, float %y)
%fabs = call nsz float @llvm.fabs.f32(float %copysign)
ret float %fabs
}

define float @fabs_copysign_nnan_negative(float %x, float %y) {
; CHECK-LABEL: define float @fabs_copysign_nnan_negative(
; CHECK-SAME: float [[X:%.*]], float [[Y:%.*]]) {
; CHECK-NEXT: [[FABS:%.*]] = call nnan float @llvm.fabs.f32(float [[X]])
; CHECK-NEXT: ret float [[FABS]]
;
%copysign = call float @llvm.copysign.f32(float %x, float %y)
%fabs = call nnan float @llvm.fabs.f32(float %copysign)
ret float %fabs
}


define float @fabs_copysign_ninf_negative(float %x, float %y) {
; CHECK-LABEL: define float @fabs_copysign_ninf_negative(
; CHECK-SAME: float [[X:%.*]], float [[Y:%.*]]) {
; CHECK-NEXT: [[FABS:%.*]] = call ninf float @llvm.fabs.f32(float [[X]])
; CHECK-NEXT: ret float [[FABS]]
;
%copysign = call float @llvm.copysign.f32(float %x, float %y)
%fabs = call ninf float @llvm.fabs.f32(float %copysign)
ret float %fabs
}

define float @fabs_copysign_nsz_negative(float %x, float %y) {
; CHECK-LABEL: define float @fabs_copysign_nsz_negative(
; CHECK-SAME: float [[X:%.*]], float [[Y:%.*]]) {
; CHECK-NEXT: [[FABS:%.*]] = call nsz float @llvm.fabs.f32(float [[X]])
; CHECK-NEXT: ret float [[FABS]]
;
%copysign = call float @llvm.copysign.f32(float %x, float %y)
%fabs = call nsz float @llvm.fabs.f32(float %copysign)
ret float %fabs
}

define float @fabs_fneg_no_fabs(float %x) {
; CHECK-LABEL: define float @fabs_fneg_no_fabs(
; CHECK-SAME: float [[X:%.*]]) {
; CHECK-NEXT: [[NEG:%.*]] = fneg float [[X]]
; CHECK-NEXT: ret float [[NEG]]
;
%neg = fneg float %x
ret float %neg
}

define <2 x float> @fabs_fneg_splat_v2f32(<2 x float> %x) {
; CHECK-LABEL: define <2 x float> @fabs_fneg_splat_v2f32(
; CHECK-SAME: <2 x float> [[X:%.*]]) {
; CHECK-NEXT: ret <2 x float> <float 2.000000e+00, float 2.000000e+00>
;
%neg = fneg <2 x float> <float -2.0, float -2.0>
%fabs = call <2 x float> @llvm.fabs.v2f32(<2 x float> %neg)
ret <2 x float> %fabs
}


define <2 x float> @fabs_fneg_splat_poison_v2f32(<2 x float> %x) {
; CHECK-LABEL: define <2 x float> @fabs_fneg_splat_poison_v2f32(
; CHECK-SAME: <2 x float> [[X:%.*]]) {
; CHECK-NEXT: [[FABS:%.*]] = call <2 x float> @llvm.fabs.v2f32(<2 x float> <float -2.000000e+00, float poison>)
; CHECK-NEXT: ret <2 x float> [[FABS]]
;
%neg = fneg <2 x float> <float 2.0, float poison>
%fabs = call <2 x float> @llvm.fabs.v2f32(<2 x float> %neg)
ret <2 x float> %fabs
}

define <2 x float> @fabs_fneg_non_splat_v2f32(<2 x float> %x) {
; CHECK-LABEL: define <2 x float> @fabs_fneg_non_splat_v2f32(
; CHECK-SAME: <2 x float> [[X:%.*]]) {
; CHECK-NEXT: ret <2 x float> <float 2.000000e+00, float 3.000000e+00>
;
%neg = fneg <2 x float> <float 2.0, float 3.0>
%fabs = call <2 x float> @llvm.fabs.v2f32(<2 x float> %neg)
ret <2 x float> %fabs
}

declare void @use(float)

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



Loading