[PATCH] D68265: [InstCombine] Simplify fma multiplication to nan for undef or nan operands.
Florian Hahn via Phabricator via llvm-commits
llvm-commits at lists.llvm.org
Tue Oct 1 03:35:36 PDT 2019
fhahn created this revision.
fhahn added reviewers: cameron.mcinally, mcberg2017, spatel, arsenm.
Herald added subscribers: hiraditya, wdng.
Herald added a project: LLVM.
In similar fashion to D67721 <https://reviews.llvm.org/D67721>, we can simplify FMA multiplications if any
of the operands is NaN or undef. In instcombine, we will simplify the
FMA to an fadd with a NaN operand, which in turn gets folded to NaN.
Note that this just changes SimplifyFMAFMul, so we still not catch the
case where only the Add part of the FMA is Nan/Undef.
Repository:
rG LLVM Github Monorepo
https://reviews.llvm.org/D68265
Files:
llvm/lib/Analysis/InstructionSimplify.cpp
llvm/test/Transforms/InstCombine/fma.ll
Index: llvm/test/Transforms/InstCombine/fma.ll
===================================================================
--- llvm/test/Transforms/InstCombine/fma.ll
+++ llvm/test/Transforms/InstCombine/fma.ll
@@ -502,8 +502,7 @@
define <2 x double> @fma_nan_and_const_0(<2 x double> %b) {
; CHECK-LABEL: @fma_nan_and_const_0(
; CHECK-NEXT: entry:
-; CHECK-NEXT: [[RES:%.*]] = call nnan nsz <2 x double> @llvm.fma.v2f64(<2 x double> <double 0x7FF8000000000000, double 0x7FF8000000000000>, <2 x double> <double 1.291820e-08, double 9.123000e-06>, <2 x double> [[B:%.*]])
-; CHECK-NEXT: ret <2 x double> [[RES]]
+; CHECK-NEXT: ret <2 x double> <double 0x7FF8000000000000, double 0x7FF8000000000000>
;
entry:
%res = call nnan nsz <2 x double> @llvm.fma.v2f64(<2 x double> <double 0x7FF8000000000000, double 0x7FF8000000000000>, <2 x double> <double 0.0000000129182, double 0.000009123>, <2 x double> %b)
@@ -513,8 +512,7 @@
define <2 x double> @fma_nan_and_const_1(<2 x double> %b) {
; CHECK-LABEL: @fma_nan_and_const_1(
; CHECK-NEXT: entry:
-; CHECK-NEXT: [[RES:%.*]] = call nnan nsz <2 x double> @llvm.fma.v2f64(<2 x double> <double 1.291820e-08, double 9.123000e-06>, <2 x double> <double 0x7FF8000000000000, double 0x7FF8000000000000>, <2 x double> [[B:%.*]])
-; CHECK-NEXT: ret <2 x double> [[RES]]
+; CHECK-NEXT: ret <2 x double> <double 0x7FF8000000000000, double 0x7FF8000000000000>
;
entry:
%res = call nnan nsz <2 x double> @llvm.fma.v2f64(<2 x double> <double 0.0000000129182, double 0.000009123>, <2 x double> <double 0x7FF8000000000000, double 0x7FF8000000000000>, <2 x double> %b)
@@ -535,8 +533,7 @@
define <2 x double> @fma_undef_0(<2 x double> %b, <2 x double> %c) {
; CHECK-LABEL: @fma_undef_0(
; CHECK-NEXT: entry:
-; CHECK-NEXT: [[RES:%.*]] = call nnan nsz <2 x double> @llvm.fma.v2f64(<2 x double> [[B:%.*]], <2 x double> undef, <2 x double> [[C:%.*]])
-; CHECK-NEXT: ret <2 x double> [[RES]]
+; CHECK-NEXT: ret <2 x double> <double 0x7FF8000000000000, double 0x7FF8000000000000>
;
entry:
%res = call nnan nsz <2 x double> @llvm.fma.v2f64(<2 x double> <double undef, double undef>, <2 x double> %b, <2 x double> %c)
@@ -546,8 +543,7 @@
define <2 x double> @fma_undef_1(<2 x double> %b, <2 x double> %c) {
; CHECK-LABEL: @fma_undef_1(
; CHECK-NEXT: entry:
-; CHECK-NEXT: [[RES:%.*]] = call nnan nsz <2 x double> @llvm.fma.v2f64(<2 x double> [[B:%.*]], <2 x double> undef, <2 x double> [[C:%.*]])
-; CHECK-NEXT: ret <2 x double> [[RES]]
+; CHECK-NEXT: ret <2 x double> <double 0x7FF8000000000000, double 0x7FF8000000000000>
;
entry:
%res = call nnan nsz <2 x double> @llvm.fma.v2f64(<2 x double> %b, <2 x double> <double undef, double undef>, <2 x double> %c)
@@ -602,8 +598,7 @@
define <2 x double> @fma_nan_0(<2 x double> %b, <2 x double> %c) {
; CHECK-LABEL: @fma_nan_0(
; CHECK-NEXT: entry:
-; CHECK-NEXT: [[RES:%.*]] = call nnan nsz <2 x double> @llvm.fma.v2f64(<2 x double> [[B:%.*]], <2 x double> <double 0x7FF8000000000000, double 0x7FF8000000000000>, <2 x double> [[C:%.*]])
-; CHECK-NEXT: ret <2 x double> [[RES]]
+; CHECK-NEXT: ret <2 x double> <double 0x7FF8000000000000, double 0x7FF8000000000000>
;
entry:
%res = call nnan nsz <2 x double> @llvm.fma.v2f64(<2 x double> <double 0x7FF8000000000000, double 0x7FF8000000000000>, <2 x double> %b, <2 x double> %c)
@@ -612,8 +607,7 @@
define <2 x double> @fma_nan_1(<2 x double> %b, <2 x double> %c) {
; CHECK-LABEL: @fma_nan_1(
; CHECK-NEXT: entry:
-; CHECK-NEXT: [[RES:%.*]] = call nnan nsz <2 x double> @llvm.fma.v2f64(<2 x double> [[B:%.*]], <2 x double> <double 0x7FF8000000000000, double 0x7FF8000000000000>, <2 x double> [[C:%.*]])
-; CHECK-NEXT: ret <2 x double> [[RES]]
+; CHECK-NEXT: ret <2 x double> <double 0x7FF8000000000000, double 0x7FF8000000000000>
;
entry:
%res = call nnan nsz <2 x double> @llvm.fma.v2f64(<2 x double> %b, <2 x double> <double 0x7FF8000000000000, double 0x7FF8000000000000>, <2 x double> %c)
Index: llvm/lib/Analysis/InstructionSimplify.cpp
===================================================================
--- llvm/lib/Analysis/InstructionSimplify.cpp
+++ llvm/lib/Analysis/InstructionSimplify.cpp
@@ -4592,6 +4592,9 @@
static Value *SimplifyFMAFMul(Value *Op0, Value *Op1, FastMathFlags FMF,
const SimplifyQuery &Q, unsigned MaxRecurse) {
+ if (Constant *C = simplifyFPOp({Op0, Op1}))
+ return C;
+
// fmul X, 1.0 ==> X
if (match(Op1, m_FPOne()))
return Op0;
@@ -4626,9 +4629,6 @@
if (Constant *C = foldOrCommuteConstant(Instruction::FMul, Op0, Op1, Q))
return C;
- if (Constant *C = simplifyFPOp({Op0, Op1}))
- return C;
-
// Now apply simplifications that do not require rounding.
return SimplifyFMAFMul(Op0, Op1, FMF, Q, MaxRecurse);
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D68265.222578.patch
Type: text/x-patch
Size: 4824 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20191001/dd298c15/attachment-0001.bin>
More information about the llvm-commits
mailing list