[llvm-commits] [llvm] r156323 - in /llvm/trunk: lib/Transforms/Scalar/Reassociate.cpp test/Transforms/Reassociate/fp-commute.ll
Owen Anderson
resistor at mac.com
Mon May 7 13:47:23 PDT 2012
Author: resistor
Date: Mon May 7 15:47:23 2012
New Revision: 156323
URL: http://llvm.org/viewvc/llvm-project?rev=156323&view=rev
Log:
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.
Added:
llvm/trunk/test/Transforms/Reassociate/fp-commute.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=156323&r1=156322&r2=156323&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/Reassociate.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/Reassociate.cpp Mon May 7 15:47:23 2012
@@ -1212,10 +1212,34 @@
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
Added: llvm/trunk/test/Transforms/Reassociate/fp-commute.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/Reassociate/fp-commute.ll?rev=156323&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/Reassociate/fp-commute.ll (added)
+++ llvm/trunk/test/Transforms/Reassociate/fp-commute.ll Mon May 7 15:47:23 2012
@@ -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
+}
+
+
More information about the llvm-commits
mailing list