[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 13:52:50 PST 2012


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.

Shuxin




-------------- next part --------------
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