Skip to content

Commit 18a1f08

Browse files
committed
[InstCombine] Use SimplifyFMulInst to simplify multiply in fma.
This allows us to fold fma's that multiply with 0.0. Also, the multiply by 1.0 case is handled there as well. The fneg/fabs cases are not handled by SimplifyFMulInst, so we need to keep them. Reviewers: spatel, anemet, lebedev.ri Reviewed By: spatel Differential Revision: https://reviews.llvm.org/D67351 llvm-svn: 371518
1 parent 8886d01 commit 18a1f08

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2258,9 +2258,11 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) {
22582258
return II;
22592259
}
22602260

2261-
// fma x, 1, z -> fadd x, z
2262-
if (match(Src1, m_FPOne())) {
2263-
auto *FAdd = BinaryOperator::CreateFAdd(Src0, II->getArgOperand(2));
2261+
// Try to simplify the underlying FMul.
2262+
if (Value *V = SimplifyFMulInst(II->getArgOperand(0), II->getArgOperand(1),
2263+
II->getFastMathFlags(),
2264+
SQ.getWithInstruction(II))) {
2265+
auto *FAdd = BinaryOperator::CreateFAdd(V, II->getArgOperand(2));
22642266
FAdd->copyFastMathFlags(II);
22652267
return FAdd;
22662268
}

llvm/test/Transforms/InstCombine/fma.ll

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,7 @@ define float @fmuladd_x_1_z_fast(float %x, float %z) {
372372
define <2 x double> @fmuladd_a_0_b(<2 x double> %a, <2 x double> %b) {
373373
; CHECK-LABEL: @fmuladd_a_0_b(
374374
; CHECK-NEXT: entry:
375-
; CHECK-NEXT: [[RES:%.*]] = call nnan nsz <2 x double> @llvm.fmuladd.v2f64(<2 x double> [[A:%.*]], <2 x double> zeroinitializer, <2 x double> [[B:%.*]])
376-
; CHECK-NEXT: ret <2 x double> [[RES]]
375+
; CHECK-NEXT: ret <2 x double> [[B:%.*]]
377376
;
378377
entry:
379378
%res = call nnan nsz <2 x double> @llvm.fmuladd.v2f64(<2 x double> %a, <2 x double> zeroinitializer, <2 x double> %b)
@@ -383,8 +382,7 @@ entry:
383382
define <2 x double> @fmuladd_0_a_b(<2 x double> %a, <2 x double> %b) {
384383
; CHECK-LABEL: @fmuladd_0_a_b(
385384
; CHECK-NEXT: entry:
386-
; CHECK-NEXT: [[RES:%.*]] = call nnan nsz <2 x double> @llvm.fmuladd.v2f64(<2 x double> [[A:%.*]], <2 x double> zeroinitializer, <2 x double> [[B:%.*]])
387-
; CHECK-NEXT: ret <2 x double> [[RES]]
385+
; CHECK-NEXT: ret <2 x double> [[B:%.*]]
388386
;
389387
entry:
390388
%res = call nnan nsz <2 x double> @llvm.fmuladd.v2f64(<2 x double> zeroinitializer, <2 x double> %a, <2 x double> %b)
@@ -407,8 +405,7 @@ declare <2 x double> @llvm.fmuladd.v2f64(<2 x double>, <2 x double>, <2 x double
407405
define <2 x double> @fma_a_0_b(<2 x double> %a, <2 x double> %b) {
408406
; CHECK-LABEL: @fma_a_0_b(
409407
; CHECK-NEXT: entry:
410-
; CHECK-NEXT: [[RES:%.*]] = call nnan nsz <2 x double> @llvm.fma.v2f64(<2 x double> [[A:%.*]], <2 x double> zeroinitializer, <2 x double> [[B:%.*]])
411-
; CHECK-NEXT: ret <2 x double> [[RES]]
408+
; CHECK-NEXT: ret <2 x double> [[B:%.*]]
412409
;
413410
entry:
414411
%res = call nnan nsz <2 x double> @llvm.fma.v2f64(<2 x double> %a, <2 x double> zeroinitializer, <2 x double> %b)
@@ -418,8 +415,7 @@ entry:
418415
define <2 x double> @fma_0_a_b(<2 x double> %a, <2 x double> %b) {
419416
; CHECK-LABEL: @fma_0_a_b(
420417
; CHECK-NEXT: entry:
421-
; CHECK-NEXT: [[RES:%.*]] = call nnan nsz <2 x double> @llvm.fma.v2f64(<2 x double> [[A:%.*]], <2 x double> zeroinitializer, <2 x double> [[B:%.*]])
422-
; CHECK-NEXT: ret <2 x double> [[RES]]
418+
; CHECK-NEXT: ret <2 x double> [[B:%.*]]
423419
;
424420
entry:
425421
%res = call nnan nsz <2 x double> @llvm.fma.v2f64(<2 x double> zeroinitializer, <2 x double> %a, <2 x double> %b)
@@ -440,8 +436,7 @@ entry:
440436
define <2 x double> @fma_sqrt(<2 x double> %a, <2 x double> %b) {
441437
; CHECK-LABEL: @fma_sqrt(
442438
; CHECK-NEXT: entry:
443-
; CHECK-NEXT: [[SQRT:%.*]] = call fast <2 x double> @llvm.sqrt.v2f64(<2 x double> [[A:%.*]])
444-
; CHECK-NEXT: [[RES:%.*]] = call fast <2 x double> @llvm.fma.v2f64(<2 x double> [[SQRT]], <2 x double> [[SQRT]], <2 x double> [[B:%.*]])
439+
; CHECK-NEXT: [[RES:%.*]] = fadd fast <2 x double> [[A:%.*]], [[B:%.*]]
445440
; CHECK-NEXT: ret <2 x double> [[RES]]
446441
;
447442
entry:

0 commit comments

Comments
 (0)