[llvm] r222005 - [Reassociate] Canonicalize constants to RHS operand.

Chad Rosier mcrosier at codeaurora.org
Fri Nov 14 09:05:59 PST 2014


Author: mcrosier
Date: Fri Nov 14 11:05:59 2014
New Revision: 222005

URL: http://llvm.org/viewvc/llvm-project?rev=222005&view=rev
Log:
[Reassociate] Canonicalize constants to RHS operand.

Modified:
    llvm/trunk/lib/Transforms/Scalar/Reassociate.cpp
    llvm/trunk/test/Transforms/Reassociate/2006-04-27-ReassociateVector.ll
    llvm/trunk/test/Transforms/Reassociate/canonicalize-neg-const.ll
    llvm/trunk/test/Transforms/Reassociate/fast-ReassociateVector.ll

Modified: llvm/trunk/lib/Transforms/Scalar/Reassociate.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/Reassociate.cpp?rev=222005&r1=222004&r2=222005&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/Reassociate.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/Reassociate.cpp Fri Nov 14 11:05:59 2014
@@ -176,6 +176,7 @@ namespace {
   private:
     void BuildRankMap(Function &F);
     unsigned getRank(Value *V);
+    void canonicalizeOperands(Instruction *I);
     void ReassociateExpression(BinaryOperator *I);
     void RewriteExprTree(BinaryOperator *I, SmallVectorImpl<ValueEntry> &Ops);
     Value *OptimizeExpression(BinaryOperator *I,
@@ -331,6 +332,20 @@ unsigned Reassociate::getRank(Value *V)
   return ValueRankMap[I] = Rank;
 }
 
+void Reassociate::canonicalizeOperands(Instruction *I) {
+  assert(isa<BinaryOperator>(I) && "Expected binary operator.");
+  assert(I->isCommutative() && "Expected commutative operator.");
+
+  Value *LHS = I->getOperand(0);
+  Value *RHS = I->getOperand(1);
+  unsigned LHSRank = getRank(LHS);
+  unsigned RHSRank = getRank(RHS);
+
+  // Canonicalize constants to RHS.  Otherwise, sort the operands by rank.
+  if (isa<Constant>(LHS) || RHSRank < LHSRank)
+    cast<BinaryOperator>(I)->swapOperands();
+}
+
 static BinaryOperator *CreateAdd(Value *S1, Value *S2, const Twine &Name,
                                  Instruction *InsertBefore, Value *FlagsOp) {
   if (S1->getType()->isIntegerTy())
@@ -2070,18 +2085,8 @@ void Reassociate::OptimizeInst(Instructi
 
     // FAdd and FMul can be commuted.
     unsigned Opcode = I->getOpcode();
-    if (Opcode == Instruction::FMul || Opcode == Instruction::FAdd) {
-      Value *LHS = I->getOperand(0);
-      Value *RHS = I->getOperand(1);
-      unsigned LHSRank = getRank(LHS);
-      unsigned RHSRank = getRank(RHS);
-
-      // Sort the operands by rank.
-      if (RHSRank < LHSRank) {
-        I->setOperand(0, RHS);
-        I->setOperand(1, LHS);
-      }
-    }
+    if (Opcode == Instruction::FMul || Opcode == Instruction::FAdd)
+      canonicalizeOperands(I);
 
     // FIXME: We should commute vector instructions as well.  However, this 
     // requires further analysis to determine the effect on later passes.

Modified: llvm/trunk/test/Transforms/Reassociate/2006-04-27-ReassociateVector.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/Reassociate/2006-04-27-ReassociateVector.ll?rev=222005&r1=222004&r2=222005&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/Reassociate/2006-04-27-ReassociateVector.ll (original)
+++ llvm/trunk/test/Transforms/Reassociate/2006-04-27-ReassociateVector.ll Fri Nov 14 11:05:59 2014
@@ -3,7 +3,7 @@
 define <4 x float> @test1() {
 ; CHECK-LABEL: test1
 ; CHECK-NEXT: %tmp1 = fsub <4 x float> zeroinitializer, zeroinitializer
-; CHECK-NEXT: %tmp2 = fmul <4 x float> zeroinitializer, %tmp1
+; CHECK-NEXT: %tmp2 = fmul <4 x float> %tmp1, zeroinitializer
 ; CHECK-NEXT: ret <4 x float> %tmp2
 
   %tmp1 = fsub <4 x float> zeroinitializer, zeroinitializer

Modified: llvm/trunk/test/Transforms/Reassociate/canonicalize-neg-const.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/Reassociate/canonicalize-neg-const.ll?rev=222005&r1=222004&r2=222005&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/Reassociate/canonicalize-neg-const.ll (original)
+++ llvm/trunk/test/Transforms/Reassociate/canonicalize-neg-const.ll Fri Nov 14 11:05:59 2014
@@ -3,7 +3,7 @@
 ; (x + 0.1234 * y) * (x + -0.1234 * y) -> (x + 0.1234 * y) * (x - 0.1234 * y)
 define double @test1(double %x, double %y) {
 ; CHECK-LABEL: @test1
-; CHECK-NEXT: fmul double 1.234000e-01, %y
+; CHECK-NEXT: fmul double %y, 1.234000e-01
 ; CHECK-NEXT: fadd double %x, %mul
 ; CHECK-NEXT: fsub double %x, %mul
 ; CHECK-NEXT: fmul double %add{{.*}}, %add{{.*}}
@@ -64,7 +64,7 @@ define i64 @test4(i64 %x, i64 %y) {
 ; Canonicalize (x - -0.1234 * y)
 define double @test5(double %x, double %y) {
 ; CHECK-LABEL: @test5
-; CHECK-NEXT: fmul double 1.234000e-01, %y
+; CHECK-NEXT: fmul double %y, 1.234000e-01
 ; CHECK-NEXT: fadd double %x, %mul
 ; CHECK-NEXT: ret double
 
@@ -76,7 +76,7 @@ define double @test5(double %x, double %
 ; Don't modify (-0.1234 * y - x)
 define double @test6(double %x, double %y) {
 ; CHECK-LABEL: @test6
-; CHECK-NEXT: fmul double -1.234000e-01, %y
+; CHECK-NEXT: fmul double %y, -1.234000e-01
 ; CHECK-NEXT: fsub double %mul, %x
 ; CHECK-NEXT: ret double %sub
 
@@ -88,7 +88,7 @@ define double @test6(double %x, double %
 ; Canonicalize (-0.1234 * y + x) -> (x - 0.1234 * y)
 define double @test7(double %x, double %y) {
 ; CHECK-LABEL: @test7
-; CHECK-NEXT: fmul double 1.234000e-01, %y
+; CHECK-NEXT: fmul double %y, 1.234000e-01
 ; CHECK-NEXT: fsub double %x, %mul
 ; CHECK-NEXT: ret double %add
 

Modified: llvm/trunk/test/Transforms/Reassociate/fast-ReassociateVector.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/Reassociate/fast-ReassociateVector.ll?rev=222005&r1=222004&r2=222005&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/Reassociate/fast-ReassociateVector.ll (original)
+++ llvm/trunk/test/Transforms/Reassociate/fast-ReassociateVector.ll Fri Nov 14 11:05:59 2014
@@ -1,10 +1,10 @@
 ; RUN: opt < %s -reassociate -S | FileCheck %s
 
-; Don't handle floating point vector operations.
+; Canonicalize operands, but don't optimize floating point vector operations.
 define <4 x float> @test1() {
 ; CHECK-LABEL: test1
 ; CHECK-NEXT: %tmp1 = fsub fast <4 x float> zeroinitializer, zeroinitializer
-; CHECK-NEXT: %tmp2 = fmul fast <4 x float> zeroinitializer, %tmp1
+; CHECK-NEXT: %tmp2 = fmul fast <4 x float> %tmp1, zeroinitializer
 
   %tmp1 = fsub fast <4 x float> zeroinitializer, zeroinitializer
   %tmp2 = fmul fast <4 x float> zeroinitializer, %tmp1





More information about the llvm-commits mailing list