Instruction::isAssociative() returns true for fmul/fadd if they are tagged "unsafe...
authorShuxin Yang <shuxin.llvm@gmail.com>
Thu, 29 Nov 2012 01:47:31 +0000 (01:47 +0000)
committerShuxin Yang <shuxin.llvm@gmail.com>
Thu, 29 Nov 2012 01:47:31 +0000 (01:47 +0000)
Approved by: Eli and Michael.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@168848 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/Instruction.h
lib/VMCore/Instruction.cpp
test/Transforms/InstCombine/fast-math.ll [new file with mode: 0644]

index 22c8289c73ae3e36cbe0f9b081296db8c6154a59..2c1d41c705b92b1a100f2b030353e47800a93c16 100644 (file)
@@ -253,7 +253,7 @@ public:
   ///
   /// 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 4208144cb5b5c6df0b174218cb505bdc02eb3058..d93c1d7a22c04b26ca75d53265a89b8597429558 100644 (file)
@@ -468,6 +468,20 @@ bool Instruction::isAssociative(unsigned Opcode) {
          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)
diff --git a/test/Transforms/InstCombine/fast-math.ll b/test/Transforms/InstCombine/fast-math.ll
new file mode 100644 (file)
index 0000000..4224140
--- /dev/null
@@ -0,0 +1,32 @@
+; 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: %mul = fmul fast float %a, 0x3FF3333340000000
+  %mul = fmul fast float %a, 0x3FF3333340000000
+  %mul1 = fmul float %mul, 0x4002666660000000
+  ret float %mul1
+}
+
+define float @fold2(float %a) {
+notfold2:
+; CHECK: fold2
+; CHECK: fmul float %a, 0x4006147AE0000000
+  %mul = fmul float %a, 0x3FF3333340000000
+  %mul1 = fmul fast float %mul, 0x4002666660000000
+  ret float %mul1
+}