-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2517,6 +2517,13 @@ Instruction *InstCombinerImpl::visitCallInst(CallInst &CI) { | |
} | ||
case Intrinsic::fabs: { | ||
Value *Cond, *TVal, *FVal; | ||
Value* Arg = II->getArgOperand(0); | ||
Value* X; | ||
// fabs (-X) --> fabs (X) | ||
if (match(Arg, m_FNeg(m_Value(X)))) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
; RUN: opt -S -passes=instcombine %s | FileCheck %s | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please regenerate check lines with |
||
|
||
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 | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should test flag preservation behavior |
||
declare float @llvm.fabs.f32(float) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
There was a problem hiding this comment.
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)
withArg
?