diff options
| author | Owen Anderson <resistor@mac.com> | 2012-05-07 20:47:23 +0000 |
|---|---|---|
| committer | Owen Anderson <resistor@mac.com> | 2012-05-07 20:47:23 +0000 |
| commit | f4f80e1f39a2da2de2623eb9d176b77bdbcac009 (patch) | |
| tree | e2b5d9f3556922e72be93d9b05088ca0237b7a6d | |
| parent | 4496c44e5f64bc5ac52ed8d1a77c0a3dfe8b4f6c (diff) | |
| download | bcm5719-llvm-f4f80e1f39a2da2de2623eb9d176b77bdbcac009.tar.gz bcm5719-llvm-f4f80e1f39a2da2de2623eb9d176b77bdbcac009.zip | |
Teach reassociate to commute FMul's and FAdd's in order to canonicalize the order of their operands across instructions. This allows for greater CSE opportunities.
llvm-svn: 156323
| -rw-r--r-- | llvm/lib/Transforms/Scalar/Reassociate.cpp | 32 | ||||
| -rw-r--r-- | llvm/test/Transforms/Reassociate/fp-commute.ll | 16 |
2 files changed, 44 insertions, 4 deletions
diff --git a/llvm/lib/Transforms/Scalar/Reassociate.cpp b/llvm/lib/Transforms/Scalar/Reassociate.cpp index cc98ba0f21c..010f17aae0b 100644 --- a/llvm/lib/Transforms/Scalar/Reassociate.cpp +++ b/llvm/lib/Transforms/Scalar/Reassociate.cpp @@ -1212,10 +1212,34 @@ void Reassociate::ReassociateInst(BasicBlock::iterator &BBI) { BI = NI; } - // Reject cases where it is pointless to do this. - if (!isa<BinaryOperator>(BI) || BI->getType()->isFloatingPointTy() || - BI->getType()->isVectorTy()) - return; // Floating point ops are not associative. + // Floating point binary operators are not associative, but we can still + // commute (some) of them, to canonicalize the order of their operands. + // This can potentially expose more CSE opportunities, and makes writing + // other transformations simpler. + if (isa<BinaryOperator>(BI) && + (BI->getType()->isFloatingPointTy() || BI->getType()->isVectorTy())) { + // FAdd and FMul can be commuted. + if (BI->getOpcode() != Instruction::FMul && + BI->getOpcode() != Instruction::FAdd) + return; + + Value *LHS = BI->getOperand(0); + Value *RHS = BI->getOperand(1); + unsigned LHSRank = getRank(LHS); + unsigned RHSRank = getRank(RHS); + + // Sort the operands by rank. + if (RHSRank < LHSRank) { + BI->setOperand(0, RHS); + BI->setOperand(1, LHS); + } + + return; + } + + // Do not reassociate operations that we do not understand. + if (!isa<BinaryOperator>(BI)) + return; // Do not reassociate boolean (i1) expressions. We want to preserve the // original order of evaluation for short-circuited comparisons that diff --git a/llvm/test/Transforms/Reassociate/fp-commute.ll b/llvm/test/Transforms/Reassociate/fp-commute.ll new file mode 100644 index 00000000000..5644f7ca701 --- /dev/null +++ b/llvm/test/Transforms/Reassociate/fp-commute.ll @@ -0,0 +1,16 @@ +; RUN: opt -reassociate -S < %s | FileCheck %s + +target triple = "armv7-apple-ios" + +; CHECK: test +define float @test(float %x, float %y) { +entry: +; CHECK: fmul float %x, %y +; CHECK: fmul float %x, %y + %0 = fmul float %x, %y + %1 = fmul float %y, %x + %2 = fsub float %0, %1 + ret float %1 +} + + |

