InstCombine: Add fma with constant transforms

DAGCombine already does these.

llvm-svn: 290860
This commit is contained in:
Matt Arsenault 2017-01-03 04:32:35 +00:00
parent 1cc294c85d
commit b264c94963
2 changed files with 90 additions and 5 deletions

View File

@ -1583,12 +1583,19 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) {
}
case Intrinsic::fma:
case Intrinsic::fmuladd: {
Value *LHS = nullptr;
Value *RHS = nullptr;
Value *Src0 = II->getArgOperand(0);
Value *Src1 = II->getArgOperand(1);
// Canonicalize constants into the RHS.
if (isa<Constant>(Src0) && !isa<Constant>(Src1)) {
II->setArgOperand(0, Src1);
II->setArgOperand(1, Src0);
std::swap(Src0, Src1);
}
Value *LHS = nullptr;
Value *RHS = nullptr;
// fma fneg(x), fneg(y), z -> fma x, y, z
if (match(Src0, m_FNeg(m_Value(LHS))) &&
match(Src1, m_FNeg(m_Value(RHS)))) {
@ -1609,6 +1616,13 @@ Instruction *InstCombiner::visitCallInst(CallInst &CI) {
return replaceInstUsesWith(*II, NewCall);
}
// fma x, 1, z -> fadd x, z
if (match(Src1, m_FPOne())) {
Instruction *RI = BinaryOperator::CreateFAdd(Src0, II->getArgOperand(2));
RI->copyFastMathFlags(II);
return RI;
}
break;
}
case Intrinsic::ppc_altivec_lvx:

View File

@ -1,6 +1,7 @@
; RUN: opt -S -instcombine < %s | FileCheck %s
declare float @llvm.fma.f32(float, float, float) #1
declare <2 x float> @llvm.fma.v2f32(<2 x float>, <2 x float>, <2 x float>) #1
declare float @llvm.fmuladd.f32(float, float, float) #1
declare float @llvm.fabs.f32(float) #1
@ -25,7 +26,7 @@ define float @fma_fneg_x_fneg_y_fast(float %x, float %y, float %z) {
}
; CHECK-LABEL: @fma_fneg_const_fneg_y(
; CHECK: %fma = call float @llvm.fma.f32(float bitcast (i32 ptrtoint (i32* @external to i32) to float), float %y, float %z)
; CHECK: %fma = call float @llvm.fma.f32(float %y, float bitcast (i32 ptrtoint (i32* @external to i32) to float), float %z)
define float @fma_fneg_const_fneg_y(float %y, float %z) {
%y.fneg = fsub float -0.0, %y
%fma = call float @llvm.fma.f32(float fsub (float -0.0, float bitcast (i32 ptrtoint (i32* @external to i32) to float)), float %y.fneg, float %z)
@ -86,7 +87,7 @@ define float @fmuladd_fneg_x_fneg_y_fast(float %x, float %y, float %z) {
}
; CHECK-LABEL: @fmuladd_fneg_const_fneg_y(
; CHECK: %fmuladd = call float @llvm.fmuladd.f32(float bitcast (i32 ptrtoint (i32* @external to i32) to float), float %y, float %z)
; CHECK: %fmuladd = call float @llvm.fmuladd.f32(float %y, float bitcast (i32 ptrtoint (i32* @external to i32) to float), float %z)
define float @fmuladd_fneg_const_fneg_y(float %y, float %z) {
%y.fneg = fsub float -0.0, %y
%fmuladd = call float @llvm.fmuladd.f32(float fsub (float -0.0, float bitcast (i32 ptrtoint (i32* @external to i32) to float)), float %y.fneg, float %z)
@ -128,5 +129,75 @@ define float @fmuladd_fabs_x_fabs_x_fast(float %x, float %z) {
ret float %fmuladd
}
; CHECK-LABEL: @fma_k_y_z(
; CHECK: %fma = call float @llvm.fma.f32(float %y, float 4.000000e+00, float %z)
define float @fma_k_y_z(float %y, float %z) {
%fma = call float @llvm.fma.f32(float 4.0, float %y, float %z)
ret float %fma
}
; CHECK-LABEL: @fma_k_y_z_fast(
; CHECK: %fma = call fast float @llvm.fma.f32(float %y, float 4.000000e+00, float %z)
define float @fma_k_y_z_fast(float %y, float %z) {
%fma = call fast float @llvm.fma.f32(float 4.0, float %y, float %z)
ret float %fma
}
; CHECK-LABEL: @fmuladd_k_y_z_fast(
; CHECK: %fmuladd = call fast float @llvm.fmuladd.f32(float %y, float 4.000000e+00, float %z)
define float @fmuladd_k_y_z_fast(float %y, float %z) {
%fmuladd = call fast float @llvm.fmuladd.f32(float 4.0, float %y, float %z)
ret float %fmuladd
}
; CHECK-LABEL: @fma_1_y_z(
; CHECK: %fma = fadd float %y, %z
define float @fma_1_y_z(float %y, float %z) {
%fma = call float @llvm.fma.f32(float 1.0, float %y, float %z)
ret float %fma
}
; CHECK-LABEL: @fma_x_1_z(
; CHECK: %fma = fadd float %x, %z
define float @fma_x_1_z(float %x, float %z) {
%fma = call float @llvm.fma.f32(float %x, float 1.0, float %z)
ret float %fma
}
; CHECK-LABEL: @fma_x_1_z_v2f32(
; CHECK: %fma = fadd <2 x float> %x, %z
define <2 x float> @fma_x_1_z_v2f32(<2 x float> %x, <2 x float> %z) {
%fma = call <2 x float> @llvm.fma.v2f32(<2 x float> %x, <2 x float> <float 1.0, float 1.0>, <2 x float> %z)
ret <2 x float> %fma
}
; CHECK-LABEL: @fma_x_1_2_z_v2f32(
; CHECK: %fma = call <2 x float> @llvm.fma.v2f32(<2 x float> %x, <2 x float> <float 1.000000e+00, float 2.000000e+00>, <2 x float> %z)
define <2 x float> @fma_x_1_2_z_v2f32(<2 x float> %x, <2 x float> %z) {
%fma = call <2 x float> @llvm.fma.v2f32(<2 x float> %x, <2 x float> <float 1.0, float 2.0>, <2 x float> %z)
ret <2 x float> %fma
}
; CHECK-LABEL: @fma_x_1_z_fast(
; CHECK: %fma = fadd fast float %x, %z
define float @fma_x_1_z_fast(float %x, float %z) {
%fma = call fast float @llvm.fma.f32(float %x, float 1.0, float %z)
ret float %fma
}
; CHECK-LABEL: @fma_1_1_z(
; CHECK: %fma = fadd float %z, 1.0
define float @fma_1_1_z(float %z) {
%fma = call float @llvm.fma.f32(float 1.0, float 1.0, float %z)
ret float %fma
}
; CHECK-LABEL: @fmuladd_x_1_z_fast(
; CHECK: %fmuladd = fadd fast float %x, %z
define float @fmuladd_x_1_z_fast(float %x, float %z) {
%fmuladd = call fast float @llvm.fmuladd.f32(float %x, float 1.0, float %z)
ret float %fmuladd
}
attributes #0 = { nounwind }
attributes #1 = { nounwind readnone }