[llvm] r221462 - [Reassociate] Don't reassociate when mixing regular and fast-math FP
Chad Rosier
mcrosier at codeaurora.org
Thu Nov 6 08:46:37 PST 2014
Author: mcrosier
Date: Thu Nov 6 10:46:37 2014
New Revision: 221462
URL: http://llvm.org/viewvc/llvm-project?rev=221462&view=rev
Log:
[Reassociate] Don't reassociate when mixing regular and fast-math FP
instructions. Inlining might cause such cases and it's not valid to
reassociate floating-point instructions without the unsafe algebra flag.
Patch by Mehdi Amini <mehdi_amini at apple.com>!
Added:
llvm/trunk/test/Transforms/Reassociate/mixed-fast-nonfast-fp.ll
Modified:
llvm/trunk/lib/Transforms/Scalar/Reassociate.cpp
Modified: llvm/trunk/lib/Transforms/Scalar/Reassociate.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/Reassociate.cpp?rev=221462&r1=221461&r2=221462&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/Reassociate.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/Reassociate.cpp Thu Nov 6 10:46:37 2014
@@ -237,7 +237,9 @@ FunctionPass *llvm::createReassociatePas
/// opcode and if it only has one use.
static BinaryOperator *isReassociableOp(Value *V, unsigned Opcode) {
if (V->hasOneUse() && isa<Instruction>(V) &&
- cast<Instruction>(V)->getOpcode() == Opcode)
+ cast<Instruction>(V)->getOpcode() == Opcode &&
+ (!isa<FPMathOperator>(V) ||
+ cast<Instruction>(V)->hasUnsafeAlgebra()))
return cast<BinaryOperator>(V);
return nullptr;
}
@@ -246,7 +248,9 @@ static BinaryOperator *isReassociableOp(
unsigned Opcode2) {
if (V->hasOneUse() && isa<Instruction>(V) &&
(cast<Instruction>(V)->getOpcode() == Opcode1 ||
- cast<Instruction>(V)->getOpcode() == Opcode2))
+ cast<Instruction>(V)->getOpcode() == Opcode2) &&
+ (!isa<FPMathOperator>(V) ||
+ cast<Instruction>(V)->hasUnsafeAlgebra()))
return cast<BinaryOperator>(V);
return nullptr;
}
@@ -662,7 +666,9 @@ static bool LinearizeExprTree(BinaryOper
// expression. This means that it can safely be modified. See if we
// can usefully morph it into an expression of the right kind.
assert((!isa<Instruction>(Op) ||
- cast<Instruction>(Op)->getOpcode() != Opcode) &&
+ cast<Instruction>(Op)->getOpcode() != Opcode
+ || (isa<FPMathOperator>(Op) &&
+ !cast<Instruction>(Op)->hasUnsafeAlgebra())) &&
"Should have been handled above!");
assert(Op->hasOneUse() && "Has uses outside the expression tree!");
Added: llvm/trunk/test/Transforms/Reassociate/mixed-fast-nonfast-fp.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/Reassociate/mixed-fast-nonfast-fp.ll?rev=221462&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/Reassociate/mixed-fast-nonfast-fp.ll (added)
+++ llvm/trunk/test/Transforms/Reassociate/mixed-fast-nonfast-fp.ll Thu Nov 6 10:46:37 2014
@@ -0,0 +1,18 @@
+; RUN: opt -reassociate %s -S | FileCheck %s
+
+define float @foo(float %a,float %b, float %c) {
+; CHECK: %mul3 = fmul float %a, %b
+; CHECK-NEXT: fmul fast float %c, 2.000000e+00
+; CHECK-NEXT: fadd fast float %factor, %b
+; CHECK-NEXT: fmul fast float %tmp1, %a
+; CHECK-NEXT: fadd fast float %tmp2, %mul3
+; CHECK-NEXT: ret float
+ %mul1 = fmul fast float %a, %c
+ %mul2 = fmul fast float %a, %b
+ %mul3 = fmul float %a, %b
+ %mul4 = fmul fast float %a, %c
+ %add1 = fadd fast float %mul1, %mul3
+ %add2 = fadd fast float %mul4, %mul2
+ %add3 = fadd fast float %add1, %add2
+ ret float %add3
+}
More information about the llvm-commits
mailing list