From: Chad Rosier Date: Fri, 14 Nov 2014 17:05:59 +0000 (+0000) Subject: [Reassociate] Canonicalize constants to RHS operand. X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=7e61b1fb621d84a7e504f77e4deee206e7cd5075;p=oota-llvm.git [Reassociate] Canonicalize constants to RHS operand. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@222005 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Transforms/Scalar/Reassociate.cpp b/lib/Transforms/Scalar/Reassociate.cpp index c86317dd821..42e7aac9d84 100644 --- a/lib/Transforms/Scalar/Reassociate.cpp +++ b/lib/Transforms/Scalar/Reassociate.cpp @@ -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 &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(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(LHS) || RHSRank < LHSRank) + cast(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(Instruction *I) { // 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. diff --git a/test/Transforms/Reassociate/2006-04-27-ReassociateVector.ll b/test/Transforms/Reassociate/2006-04-27-ReassociateVector.ll index f7839554002..ea869842b18 100644 --- a/test/Transforms/Reassociate/2006-04-27-ReassociateVector.ll +++ b/test/Transforms/Reassociate/2006-04-27-ReassociateVector.ll @@ -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 diff --git a/test/Transforms/Reassociate/canonicalize-neg-const.ll b/test/Transforms/Reassociate/canonicalize-neg-const.ll index 80f433a498d..8952675a8cf 100644 --- a/test/Transforms/Reassociate/canonicalize-neg-const.ll +++ b/test/Transforms/Reassociate/canonicalize-neg-const.ll @@ -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 %y) { ; 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 %y) { ; 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 diff --git a/test/Transforms/Reassociate/fast-ReassociateVector.ll b/test/Transforms/Reassociate/fast-ReassociateVector.ll index e4b5e936f58..ab38edae0c4 100644 --- a/test/Transforms/Reassociate/fast-ReassociateVector.ll +++ b/test/Transforms/Reassociate/fast-ReassociateVector.ll @@ -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