[llvm] r336215 - [Constants] add identity constants for fadd/fmul

Sanjay Patel via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 3 10:12:59 PDT 2018


Author: spatel
Date: Tue Jul  3 10:12:59 2018
New Revision: 336215

URL: http://llvm.org/viewvc/llvm-project?rev=336215&view=rev
Log:
[Constants] add identity constants for fadd/fmul

As the test diffs show, the current users of getBinOpIdentity()
are InstCombine and Reassociate. SLP vectorizer is a candidate
for using this functionality too (D28907).

The InstCombine shuffle improvements are part of the planned
enhancements noted in D48830.

InstCombine actually has several other uses of getBinOpIdentity() 
via SimplifyUsingDistributiveLaws(), but we don't call that for 
any FP ops. Fixing that might be another part of removing the
custom reassociation in InstCombine that is only done for fadd+fmul.

Modified:
    llvm/trunk/lib/IR/Constants.cpp
    llvm/trunk/test/Transforms/InstCombine/shuffle_select.ll
    llvm/trunk/test/Transforms/Reassociate/binop-identity.ll
    llvm/trunk/test/Transforms/Reassociate/reassoc-intermediate-fnegs.ll

Modified: llvm/trunk/lib/IR/Constants.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/Constants.cpp?rev=336215&r1=336214&r2=336215&view=diff
==============================================================================
--- llvm/trunk/lib/IR/Constants.cpp (original)
+++ llvm/trunk/lib/IR/Constants.cpp Tue Jul  3 10:12:59 2018
@@ -2281,7 +2281,12 @@ Constant *ConstantExpr::getBinOpIdentity
   case Instruction::And:
     return Constant::getAllOnesValue(Ty);
 
-  // FIXME: FAdd / FMul?
+  // TODO: If the fadd has 'nsz', should we return +0.0?
+  case Instruction::FAdd:
+    return ConstantFP::getNegativeZero(Ty);
+
+  case Instruction::FMul:
+    return ConstantFP::get(Ty, 1.0);
   }
 }
 

Modified: llvm/trunk/test/Transforms/InstCombine/shuffle_select.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/InstCombine/shuffle_select.ll?rev=336215&r1=336214&r2=336215&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/InstCombine/shuffle_select.ll (original)
+++ llvm/trunk/test/Transforms/InstCombine/shuffle_select.ll Tue Jul  3 10:12:59 2018
@@ -163,8 +163,7 @@ define <4 x i32> @srem(<4 x i32> %v) {
 
 define <4 x float> @fadd(<4 x float> %v) {
 ; CHECK-LABEL: @fadd(
-; CHECK-NEXT:    [[B:%.*]] = fadd <4 x float> [[V:%.*]], <float 4.100000e+01, float 4.200000e+01, float 4.300000e+01, float 4.400000e+01>
-; CHECK-NEXT:    [[S:%.*]] = shufflevector <4 x float> [[B]], <4 x float> [[V]], <4 x i32> <i32 0, i32 1, i32 6, i32 7>
+; CHECK-NEXT:    [[S:%.*]] = fadd <4 x float> [[V:%.*]], <float 4.100000e+01, float 4.200000e+01, float -0.000000e+00, float -0.000000e+00>
 ; CHECK-NEXT:    ret <4 x float> [[S]]
 ;
   %b = fadd <4 x float> %v, <float 41.0, float 42.0, float 43.0, float 44.0>
@@ -187,8 +186,7 @@ define <4 x double> @fsub(<4 x double> %
 
 define <4 x float> @fmul(<4 x float> %v) {
 ; CHECK-LABEL: @fmul(
-; CHECK-NEXT:    [[B:%.*]] = fmul nnan ninf <4 x float> [[V:%.*]], <float 4.100000e+01, float 4.200000e+01, float 4.300000e+01, float 4.400000e+01>
-; CHECK-NEXT:    [[S:%.*]] = shufflevector <4 x float> [[B]], <4 x float> [[V]], <4 x i32> <i32 0, i32 5, i32 6, i32 7>
+; CHECK-NEXT:    [[S:%.*]] = fmul nnan ninf <4 x float> [[V:%.*]], <float 4.100000e+01, float 1.000000e+00, float 1.000000e+00, float 1.000000e+00>
 ; CHECK-NEXT:    ret <4 x float> [[S]]
 ;
   %b = fmul nnan ninf <4 x float> %v, <float 41.0, float 42.0, float 43.0, float 44.0>

Modified: llvm/trunk/test/Transforms/Reassociate/binop-identity.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/Reassociate/binop-identity.ll?rev=336215&r1=336214&r2=336215&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/Reassociate/binop-identity.ll (original)
+++ llvm/trunk/test/Transforms/Reassociate/binop-identity.ll Tue Jul  3 10:12:59 2018
@@ -48,7 +48,7 @@ define i8 @xor_0(i8 %x) {
   ret i8 %a2
 }
 
-; FIXME
+; FIXME - the binop identity constant for fadd is -0.0, so this didn't fold.
 
 define float @fadd_0(float %x) {
 ; CHECK-LABEL: @fadd_0(
@@ -60,12 +60,9 @@ define float @fadd_0(float %x) {
   ret float %a2
 }
 
-; FIXME
-
 define float @fmul_1(float %x) {
 ; CHECK-LABEL: @fmul_1(
-; CHECK-NEXT:    [[A2:%.*]] = fmul fast float [[X:%.*]], 1.000000e+00
-; CHECK-NEXT:    ret float [[A2]]
+; CHECK-NEXT:    ret float [[X:%.*]]
 ;
   %a1 = fmul fast float %x, 4.0
   %a2 = fmul fast float %a1, 0.25

Modified: llvm/trunk/test/Transforms/Reassociate/reassoc-intermediate-fnegs.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/Reassociate/reassoc-intermediate-fnegs.ll?rev=336215&r1=336214&r2=336215&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/Reassociate/reassoc-intermediate-fnegs.ll (original)
+++ llvm/trunk/test/Transforms/Reassociate/reassoc-intermediate-fnegs.ll Tue Jul  3 10:12:59 2018
@@ -1,11 +1,12 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
 ; RUN: opt < %s -reassociate -S | FileCheck %s
 
 ; Input is A op (B op C)
 
 define half @faddsubAssoc1(half %a, half %b) {
 ; CHECK-LABEL: @faddsubAssoc1(
-; CHECK-NEXT:    [[T2_NEG:%.*]] = fmul fast half %a, 0xH4500
-; CHECK-NEXT:    [[REASS_MUL:%.*]] = fmul fast half %b, 0xH4500
+; CHECK-NEXT:    [[T2_NEG:%.*]] = fmul fast half [[A:%.*]], 0xH4500
+; CHECK-NEXT:    [[REASS_MUL:%.*]] = fmul fast half [[B:%.*]], 0xH4500
 ; CHECK-NEXT:    [[T51:%.*]] = fsub fast half [[REASS_MUL]], [[T2_NEG]]
 ; CHECK-NEXT:    [[T5:%.*]] = fadd fast half [[REASS_MUL]], [[T2_NEG]]
 ; CHECK-NEXT:    ret half [[T51]]
@@ -22,9 +23,8 @@ define half @faddsubAssoc1(half %a, half
 
 define half @faddsubAssoc2(half %a, half %b) {
 ; CHECK-LABEL: @faddsubAssoc2(
-; CHECK-NEXT:    [[T2:%.*]] = fmul fast half %a, 0xH4500
-; CHECK-NEXT:    [[REASS_MUL:%.*]] = fmul fast half %b, 0xH3C00
-; CHECK-NEXT:    [[T5:%.*]] = fadd fast half [[REASS_MUL]], [[T2]]
+; CHECK-NEXT:    [[T2:%.*]] = fmul fast half [[A:%.*]], 0xH4500
+; CHECK-NEXT:    [[T5:%.*]] = fadd fast half [[B:%.*]], [[T2]]
 ; CHECK-NEXT:    ret half [[T5]]
 ;
   %t1 = fmul fast half %b, 0xH4200 ; 3*b




More information about the llvm-commits mailing list