[PATCH] D123830: [InstCombine] Complete folding of fneg-of-fabs
Chenbing.Zheng via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Tue Apr 19 23:10:52 PDT 2022
Chenbing.Zheng updated this revision to Diff 423801.
Chenbing.Zheng retitled this revision from "[InstCombine] Optimize folding of fneg-of-fabs" to "[InstCombine] Complete folding of fneg-of-fabs".
Chenbing.Zheng edited the summary of this revision.
Chenbing.Zheng added a comment.
address comments
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D123830/new/
https://reviews.llvm.org/D123830
Files:
llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
llvm/test/Transforms/InstCombine/fneg.ll
Index: llvm/test/Transforms/InstCombine/fneg.ll
===================================================================
--- llvm/test/Transforms/InstCombine/fneg.ll
+++ llvm/test/Transforms/InstCombine/fneg.ll
@@ -714,13 +714,10 @@
ret float %fneg1
}
-; TODO: This should reduce to fneg-of-fabs.
-
define float @fnabs(float %a) {
; CHECK-LABEL: @fnabs(
-; CHECK-NEXT: [[CMP:%.*]] = fcmp olt float [[A:%.*]], 0.000000e+00
-; CHECK-NEXT: [[A_NEG:%.*]] = fneg fast float [[A]]
-; CHECK-NEXT: [[FNEG1:%.*]] = select fast i1 [[CMP]], float [[A]], float [[A_NEG]]
+; CHECK-NEXT: [[TMP1:%.*]] = call fast float @llvm.fabs.f32(float [[A:%.*]])
+; CHECK-NEXT: [[FNEG1:%.*]] = fneg fast float [[TMP1]]
; CHECK-NEXT: ret float [[FNEG1]]
;
%fneg = fneg float %a
@@ -732,6 +729,19 @@
define float @fnabs_1(float %a) {
; CHECK-LABEL: @fnabs_1(
+; CHECK-NEXT: [[TMP1:%.*]] = call fast float @llvm.fabs.f32(float [[A:%.*]])
+; CHECK-NEXT: [[FNEG1:%.*]] = fneg fast float [[TMP1]]
+; CHECK-NEXT: ret float [[FNEG1]]
+;
+ %fneg = fneg float %a
+ %cmp = fcmp ogt float %a, %fneg
+ %sel = select i1 %cmp, float %a, float %fneg
+ %fneg1 = fneg fast float %sel
+ ret float %fneg1
+}
+
+define float @fnabs_2(float %a) {
+; CHECK-LABEL: @fnabs_2(
; CHECK-NEXT: [[TMP1:%.*]] = call nsz float @llvm.fabs.f32(float [[A:%.*]])
; CHECK-NEXT: [[FNEG1:%.*]] = fneg float [[TMP1]]
; CHECK-NEXT: ret float [[FNEG1]]
Index: llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
===================================================================
--- llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
+++ llvm/lib/Transforms/InstCombine/InstCombineSelect.cpp
@@ -2809,6 +2809,30 @@
Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, TrueVal, &SI);
return replaceInstUsesWith(SI, Fabs);
}
+ // With nnan and nsz
+ // (X < +/-0.0) ? X : -X --> -fabs(x)
+ // (X <= +/-0.0) ? X : -X --> -fabs(x)
+ if (match(CondVal, m_FCmp(Pred, m_Specific(TrueVal), m_AnyZeroFP())) &&
+ match(FalseVal, m_FNeg(m_Specific(TrueVal))) && SI.hasNoSignedZeros() &&
+ (Pred == FCmpInst::FCMP_OLT || Pred == FCmpInst::FCMP_OLE ||
+ Pred == FCmpInst::FCMP_ULT || Pred == FCmpInst::FCMP_ULE)) {
+ Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, TrueVal, &SI);
+ Instruction *NewFNeg = UnaryOperator::CreateFNeg(Fabs);
+ NewFNeg->setFastMathFlags(SI.getFastMathFlags());
+ return NewFNeg;
+ }
+ // With nnan and nsz
+ // (X > +/-0.0) ? -X : X --> -fabs(x)
+ // (X >= +/-0.0) ? -X : X --> -fabs(x)
+ if (match(CondVal, m_FCmp(Pred, m_Specific(FalseVal), m_AnyZeroFP())) &&
+ match(TrueVal, m_FNeg(m_Specific(FalseVal))) && SI.hasNoSignedZeros() &&
+ (Pred == FCmpInst::FCMP_OGT || Pred == FCmpInst::FCMP_OGE ||
+ Pred == FCmpInst::FCMP_UGT || Pred == FCmpInst::FCMP_UGE)) {
+ Value *Fabs = Builder.CreateUnaryIntrinsic(Intrinsic::fabs, FalseVal, &SI);
+ Instruction *NewFNeg = UnaryOperator::CreateFNeg(Fabs);
+ NewFNeg->setFastMathFlags(SI.getFastMathFlags());
+ return NewFNeg;
+ }
// See if we are selecting two values based on a comparison of the two values.
if (ICmpInst *ICI = dyn_cast<ICmpInst>(CondVal))
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D123830.423801.patch
Type: text/x-patch
Size: 3223 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220420/116d5699/attachment.bin>
More information about the llvm-commits
mailing list