[llvm-commits] [PATCH] [Fast-math] In relaxed FP mode, Instruction::isAssociative() returns true for fmul/fadd

Shuxin Yang shuxin.llvm at gmail.com
Wed Nov 28 14:59:16 PST 2012


Hi, Eli:

    Thank you for your feedback. I add two testing-cases per your request.

Thanks
Shuxin

On 11/28/12 2:12 PM, Eli Friedman wrote:
> On Wed, Nov 28, 2012 at 1:52 PM, Shuxin Yang <shuxin.llvm at gmail.com> wrote:
>> Hi,
>>
>>     The tiny patch attached to this mail is to let
>> Instruction::isAssociative() return true for fmul/fadd
>> if the compiler is using relaxed floating-point mode.
>>
>>      I thought this change could open a big can of worm. Turn out we are
>> fortunate. I stress-tested
>> the change by *ALWAYS* considering fmul/fadd as reassociative regardless
>> which fp mode the
>> compiler is using, and I tested regression test, SingleSrc, MultipleSrc.  I
>> only saw one regression:
>> test/Transforms/InstCombine/2006-10-26-VectorReassoc.ll, which is
>> false-positive.
>>
>>     It seems that Instcombine is transparently doing a great job for FP
>> operators
>> if Instrunction::isAssociative() returns true.
>>
>> Thanks you for review.
> Testcase?  Also, do we handle mixing associative operations and
> non-associative operations correctly?
>
> -Eli

-------------- next part --------------
Index: test/Transforms/InstSimplify/fast-math2.ll
===================================================================
--- test/Transforms/InstSimplify/fast-math2.ll	(revision 0)
+++ test/Transforms/InstSimplify/fast-math2.ll	(revision 0)
@@ -0,0 +1,23 @@
+; RUN: opt < %s -instcombine -S | FileCheck %s
+
+; testing-case "float fold(float a) { return 1.2f * a * 2.3f; }"
+; 1.2f and 2.3f is supposed to be fold.
+define float @fold(float %a) {
+fold:
+  %mul = fmul fast float %a, 0x3FF3333340000000
+  %mul1 = fmul fast float %mul, 0x4002666660000000
+  ret float %mul1
+; CHECK: fold
+; CHECK: fmul float %a, 0x4006147AE0000000
+}
+
+; Same testing-case as the one used in fold() except that the operators have
+; fixed FP mode.
+define float @notfold(float %a) {
+notfold:
+; CHECK: notfold
+; CHECK-NOT: fmul float %a, 0x4006147AE0000000
+  %mul = fmul fast float %a, 0x3FF3333340000000
+  %mul1 = fmul float %mul, 0x4002666660000000
+  ret float %mul1
+}
Index: include/llvm/Instruction.h
===================================================================
--- include/llvm/Instruction.h	(revision 168807)
+++ include/llvm/Instruction.h	(working copy)
@@ -253,7 +253,7 @@
   ///
   /// In LLVM, the Add, Mul, And, Or, and Xor operators are associative.
   ///
-  bool isAssociative() const { return isAssociative(getOpcode()); }
+  bool isAssociative() const;
   static bool isAssociative(unsigned op);
 
   /// isCommutative - Return true if the instruction is commutative:
Index: lib/VMCore/Instruction.cpp
===================================================================
--- lib/VMCore/Instruction.cpp	(revision 168807)
+++ lib/VMCore/Instruction.cpp	(working copy)
@@ -468,6 +468,20 @@
          Opcode == Add || Opcode == Mul;
 }
 
+bool Instruction::isAssociative() const {
+  unsigned Opcode = getOpcode();
+  if (isAssociative(Opcode))
+    return true;
+
+  switch (Opcode) {
+  case FMul:
+  case FAdd:
+    return cast<FPMathOperator>(this)->hasUnsafeAlgebra();
+  default:
+    return false;
+  }
+}
+
 /// isCommutative - Return true if the instruction is commutative:
 ///
 ///   Commutative operators satisfy: (x op y) === (y op x)


More information about the llvm-commits mailing list