r367447 - [InstCombine] canonicalize fneg before fmul/fdiv
Sanjay Patel via cfe-commits
cfe-commits at lists.llvm.org
Wed Jul 31 09:53:22 PDT 2019
Author: spatel
Date: Wed Jul 31 09:53:22 2019
New Revision: 367447
URL: http://llvm.org/viewvc/llvm-project?rev=367447&view=rev
Log:
[InstCombine] canonicalize fneg before fmul/fdiv
Reverse the canonicalization of fneg relative to fmul/fdiv. That makes it
easier to implement the transforms (and possibly other fneg transforms) in
1 place because we can always start the pattern match from fneg (either the
legacy binop or the new unop).
There's a secondary practical benefit seen in PR21914 and PR42681:
https://bugs.llvm.org/show_bug.cgi?id=21914
https://bugs.llvm.org/show_bug.cgi?id=42681
...hoisting fneg rather than sinking seems to play nicer with LICM in IR
(although this change may expose analysis holes in the other direction).
1. The instcombine test changes show the expected neutral IR diffs from
reversing the order.
2. The reassociation tests show that we were missing an optimization
opportunity to fold away fneg-of-fneg. My reading of IEEE-754 says
that all of these transforms are allowed (regardless of binop/unop
fneg version) because:
"For all other operations [besides copy/abs/negate/copysign], this
standard does not specify the sign bit of a NaN result."
In all of these transforms, we always have some other binop
(fadd/fsub/fmul/fdiv), so we are free to flip the sign bit of a
potential intermediate NaN operand.
(If that interpretation is wrong, then we must already have a bug in
the existing transforms?)
3. The clang tests shouldn't exist as-is, but that's effectively a
revert of rL367149 (the test broke with an extension of the
pre-existing fneg canonicalization in rL367146).
Differential Revision: https://reviews.llvm.org/D65399
Modified:
cfe/trunk/test/CodeGen/complex-math.c
Modified: cfe/trunk/test/CodeGen/complex-math.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/complex-math.c?rev=367447&r1=367446&r2=367447&view=diff
==============================================================================
--- cfe/trunk/test/CodeGen/complex-math.c (original)
+++ cfe/trunk/test/CodeGen/complex-math.c Wed Jul 31 09:53:22 2019
@@ -148,10 +148,11 @@ float _Complex div_float_rc(float a, flo
// AARCH64-FASTMATH: [[CCpDD:%.*]] = fadd fast float [[CC]], [[DD]]
//
// BC = 0
- // AARCH64-FASTMATH: [[AD:%.*]] = fmul fast float [[D]], %a
- // AARCH64-FASTMATH: [[BCmAD:%.*]] = fdiv fast float [[AC]], [[CCpDD]]
- // AARCH64-FASTMATH: [[DIV:%.*]] = fdiv fast float [[AD]], [[CCpDD]]
- // AARCH64-FASTMATH: fsub fast float -0.000000e+00, [[DIV]]
+ // AARCH64-FASTMATH: [[NEGA:%.*]] = fsub fast float -0.000000e+00, %a
+ // AARCH64-FASTMATH: [[AD:%.*]] = fmul fast float [[D]], [[NEGA]]
+ //
+ // AARCH64-FASTMATH: fdiv fast float [[AC]], [[CCpDD]]
+ // AARCH64-FASTMATH: fdiv fast float [[AD]], [[CCpDD]]
// AARCH64-FASTMATH: ret
return a / b;
}
@@ -325,10 +326,11 @@ double _Complex div_double_rc(double a,
// AARCH64-FASTMATH: [[CCpDD:%.*]] = fadd fast double [[CC]], [[DD]]
//
// BC = 0
- // AARCH64-FASTMATH: [[AD:%.*]] = fmul fast double [[D]], %a
- // AARCH64-FASTMATH: [[BCmAD:%.*]] = fdiv fast double [[AC]], [[CCpDD]]
- // AARCH64-FASTMATH: [[DIV:%.*]] = fdiv fast double [[AD]], [[CCpDD]]
- // AARCH64-FASTMATH: fsub fast double -0.000000e+00, [[DIV]]
+ // AARCH64-FASTMATH: [[NEGA:%.*]] = fsub fast double -0.000000e+00, %a
+ // AARCH64-FASTMATH: [[AD:%.*]] = fmul fast double [[D]], [[NEGA]]
+ //
+ // AARCH64-FASTMATH: fdiv fast double [[AC]], [[CCpDD]]
+ // AARCH64-FASTMATH: fdiv fast double [[AD]], [[CCpDD]]
// AARCH64-FASTMATH: ret
return a / b;
}
@@ -520,10 +522,11 @@ long double _Complex div_long_double_rc(
// AARCH64-FASTMATH: [[CCpDD:%.*]] = fadd fast fp128 [[CC]], [[DD]]
//
// BC = 0
- // AARCH64-FASTMATH: [[AD:%.*]] = fmul fast fp128 [[D]], %a
- // AARCH64-FASTMATH: [[BCmAD:%.*]] = fdiv fast fp128 [[AC]], [[CCpDD]]
- // AARCH64-FASTMATH: [[DIV:%.*]] = fdiv fast fp128 [[AD]], [[CCpDD]]
- // AARCH64-FASTMATH: fsub fast fp128 0xL00000000000000008000000000000000, [[DIV]]
+ // AARCH64-FASTMATH: [[NEGA:%.*]] = fsub fast fp128 0xL00000000000000008000000000000000, %a
+ // AARCH64-FASTMATH: [[AD:%.*]] = fmul fast fp128 [[D]], [[NEGA]]
+ //
+ // AARCH64-FASTMATH: fdiv fast fp128 [[AC]], [[CCpDD]]
+ // AARCH64-FASTMATH: fdiv fast fp128 [[AD]], [[CCpDD]]
// AARCH64-FASTMATH: ret
return a / b;
}
More information about the cfe-commits
mailing list