summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorShuxin Yang <shuxin.llvm@gmail.com>2012-11-29 01:47:31 +0000
committerShuxin Yang <shuxin.llvm@gmail.com>2012-11-29 01:47:31 +0000
commit01ab5d718beeb33d1a9d0a18e1fa99fb55426e7b (patch)
tree046f1caa97547e2f25262f6e8678336750ec5e22
parent8bf22e5b5283da91aeff7685b01104db44de15e7 (diff)
downloadbcm5719-llvm-01ab5d718beeb33d1a9d0a18e1fa99fb55426e7b.tar.gz
bcm5719-llvm-01ab5d718beeb33d1a9d0a18e1fa99fb55426e7b.zip
Instruction::isAssociative() returns true for fmul/fadd if they are tagged "unsafe" mode.
Approved by: Eli and Michael. llvm-svn: 168848
-rw-r--r--llvm/include/llvm/Instruction.h2
-rw-r--r--llvm/lib/VMCore/Instruction.cpp14
-rw-r--r--llvm/test/Transforms/InstCombine/fast-math.ll32
3 files changed, 47 insertions, 1 deletions
diff --git a/llvm/include/llvm/Instruction.h b/llvm/include/llvm/Instruction.h
index 22c8289c73a..2c1d41c705b 100644
--- a/llvm/include/llvm/Instruction.h
+++ b/llvm/include/llvm/Instruction.h
@@ -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:
diff --git a/llvm/lib/VMCore/Instruction.cpp b/llvm/lib/VMCore/Instruction.cpp
index 4208144cb5b..d93c1d7a22c 100644
--- a/llvm/lib/VMCore/Instruction.cpp
+++ b/llvm/lib/VMCore/Instruction.cpp
@@ -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/llvm/test/Transforms/InstCombine/fast-math.ll b/llvm/test/Transforms/InstCombine/fast-math.ll
new file mode 100644
index 00000000000..42241409ff8
--- /dev/null
+++ b/llvm/test/Transforms/InstCombine/fast-math.ll
@@ -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
+}
OpenPOWER on IntegriCloud