Revert "[Reassociate] As the expression tree is rewritten make sure the operands are"

This reverts commit r222142.  This is causing/exposing an execution-time regression
in spec2006/gcc and coremark on AArch64/A57/Ofast.

Conflicts:

	test/Transforms/Reassociate/optional-flags.ll

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@222398 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chad Rosier
2014-11-19 23:21:20 +00:00
parent fb1c650fd0
commit 503ec9826c
17 changed files with 60 additions and 62 deletions

View File

@ -791,11 +791,14 @@ void Reassociate::RewriteExprTree(BinaryOperator *I,
Value *OldLHS = Op->getOperand(0); Value *OldLHS = Op->getOperand(0);
Value *OldRHS = Op->getOperand(1); Value *OldRHS = Op->getOperand(1);
// The new operation differs trivially from the original. if (NewLHS == OldLHS && NewRHS == OldRHS)
if ((NewLHS == OldLHS && NewRHS == OldRHS) || // Nothing changed, leave it alone.
(NewLHS == OldRHS && NewRHS == OldLHS)) { break;
if (NewLHS == OldRHS && NewRHS == OldLHS) {
// The order of the operands was reversed. Swap them.
DEBUG(dbgs() << "RA: " << *Op << '\n'); DEBUG(dbgs() << "RA: " << *Op << '\n');
canonicalizeOperands(Op); Op->swapOperands();
DEBUG(dbgs() << "TO: " << *Op << '\n'); DEBUG(dbgs() << "TO: " << *Op << '\n');
MadeChange = true; MadeChange = true;
++NumChanged; ++NumChanged;
@ -817,8 +820,6 @@ void Reassociate::RewriteExprTree(BinaryOperator *I,
NodesToRewrite.push_back(BO); NodesToRewrite.push_back(BO);
Op->setOperand(1, NewRHS); Op->setOperand(1, NewRHS);
} }
// Put the operands in canonical form.
canonicalizeOperands(Op);
DEBUG(dbgs() << "TO: " << *Op << '\n'); DEBUG(dbgs() << "TO: " << *Op << '\n');
ExpressionChanged = Op; ExpressionChanged = Op;
@ -855,7 +856,6 @@ void Reassociate::RewriteExprTree(BinaryOperator *I,
// into it. // into it.
BinaryOperator *BO = isReassociableOp(Op->getOperand(0), Opcode); BinaryOperator *BO = isReassociableOp(Op->getOperand(0), Opcode);
if (BO && !NotRewritable.count(BO)) { if (BO && !NotRewritable.count(BO)) {
canonicalizeOperands(Op);
Op = BO; Op = BO;
continue; continue;
} }
@ -880,7 +880,6 @@ void Reassociate::RewriteExprTree(BinaryOperator *I,
DEBUG(dbgs() << "RA: " << *Op << '\n'); DEBUG(dbgs() << "RA: " << *Op << '\n');
Op->setOperand(0, NewOp); Op->setOperand(0, NewOp);
canonicalizeOperands(Op);
DEBUG(dbgs() << "TO: " << *Op << '\n'); DEBUG(dbgs() << "TO: " << *Op << '\n');
ExpressionChanged = Op; ExpressionChanged = Op;
MadeChange = true; MadeChange = true;

View File

@ -2,7 +2,7 @@
define i32 @test1(i32 %A, i32 %B) { define i32 @test1(i32 %A, i32 %B) {
; CHECK-LABEL: test1 ; CHECK-LABEL: test1
; CHECK: %Z = add i32 %A, %B ; CHECK: %Z = add i32 %B, %A
; CHECK: ret i32 %Z ; CHECK: ret i32 %Z
%W = add i32 %B, -5 %W = add i32 %B, -5
%Y = add i32 %A, 5 %Y = add i32 %A, 5

View File

@ -16,7 +16,7 @@ define i32 @test1(i32 %A, i32 %B) {
; With sub reassociation, constant folding can eliminate the two 12 constants. ; With sub reassociation, constant folding can eliminate the two 12 constants.
define i32 @test2(i32 %A, i32 %B, i32 %C, i32 %D) { define i32 @test2(i32 %A, i32 %B, i32 %C, i32 %D) {
; CHECK-LABEL: test2 ; CHECK-LABEL: test2
; CHECK-NEXT: %sum = add i32 %A, %B ; CHECK-NEXT: %sum = add i32 %B, %A
; CHECK-NEXT: %sum1 = add i32 %sum, %C ; CHECK-NEXT: %sum1 = add i32 %sum, %C
; CHECK-NEXT: %Q = sub i32 %D, %sum1 ; CHECK-NEXT: %Q = sub i32 %D, %sum1
; CHECK-NEXT: ret i32 %Q ; CHECK-NEXT: ret i32 %Q

View File

@ -17,7 +17,7 @@ define i32 @test2(i32 %reg109, i32 %reg1111) {
ret i32 %reg117 ret i32 %reg117
; CHECK-LABEL: @test2 ; CHECK-LABEL: @test2
; CHECK-NEXT: %reg117 = add i32 %reg109, %reg1111 ; CHECK-NEXT: %reg117 = add i32 %reg1111, %reg109
; CHECK-NEXT: ret i32 %reg117 ; CHECK-NEXT: ret i32 %reg117
} }
@ -121,7 +121,7 @@ define i32 @test7(i32 %A, i32 %B, i32 %C) {
ret i32 %r ret i32 %r
; CHECK-LABEL: @test7 ; CHECK-LABEL: @test7
; CHECK-NEXT: add i32 %B, %C ; CHECK-NEXT: add i32 %C, %B
; CHECK-NEXT: mul i32 ; CHECK-NEXT: mul i32
; CHECK-NEXT: mul i32 ; CHECK-NEXT: mul i32
; CHECK-NEXT: ret i32 ; CHECK-NEXT: ret i32
@ -135,7 +135,7 @@ define i32 @test8(i32 %X, i32 %Y, i32 %Z) {
ret i32 %C ret i32 %C
; CHECK-LABEL: @test8 ; CHECK-LABEL: @test8
; CHECK-NEXT: %A = mul i32 %X, %Y ; CHECK-NEXT: %A = mul i32 %Y, %X
; CHECK-NEXT: %C = sub i32 %Z, %A ; CHECK-NEXT: %C = sub i32 %Z, %A
; CHECK-NEXT: ret i32 %C ; CHECK-NEXT: ret i32 %C
} }

View File

@ -53,7 +53,7 @@ define double @test3(double %x, double %y) {
define i64 @test4(i64 %x, i64 %y) { define i64 @test4(i64 %x, i64 %y) {
; CHECK-LABEL: @test4 ; CHECK-LABEL: @test4
; CHECK-NEXT: mul i64 %y, 1234 ; CHECK-NEXT: mul i64 %y, 1234
; CHECK-NEXT: add i64 %x, %mul ; CHECK-NEXT: add i64 %mul, %x
; CHECK-NEXT: ret i64 %sub ; CHECK-NEXT: ret i64 %sub
%mul = mul i64 %y, -1234 %mul = mul i64 %y, -1234

View File

@ -4,8 +4,8 @@ declare void @use(i32)
define void @test1(i32 %x, i32 %y) { define void @test1(i32 %x, i32 %y) {
; CHECK-LABEL: test1 ; CHECK-LABEL: test1
; CHECK: mul i32 %x, %y ; CHECK: mul i32 %y, %x
; CHECK: mul i32 %x, %y ; CHECK: mul i32 %y, %x
; CHECK: sub i32 %1, %2 ; CHECK: sub i32 %1, %2
; CHECK: call void @use(i32 %{{.*}}) ; CHECK: call void @use(i32 %{{.*}})
; CHECK: call void @use(i32 %{{.*}}) ; CHECK: call void @use(i32 %{{.*}})

View File

@ -49,8 +49,8 @@ define float @test3(float %A, float %B, float %C, float %D) {
; With sub reassociation, constant folding can eliminate the two 12 constants. ; With sub reassociation, constant folding can eliminate the two 12 constants.
define float @test4(float %A, float %B, float %C, float %D) { define float @test4(float %A, float %B, float %C, float %D) {
; CHECK-LABEL: test4 ; CHECK-LABEL: test4
; CHECK-NEXT: %A.neg = fsub fast float -0.000000e+00, %A ; CHECK-NEXT: %B.neg = fsub fast float -0.000000e+00, %B
; CHECK-NEXT: %O.neg = fsub fast float %A.neg, %B ; CHECK-NEXT: %O.neg = fsub fast float %B.neg, %A
; CHECK-NEXT: %P = fsub fast float %O.neg, %C ; CHECK-NEXT: %P = fsub fast float %O.neg, %C
; CHECK-NEXT: %Q = fadd fast float %P, %D ; CHECK-NEXT: %Q = fadd fast float %P, %D
; CHECK-NEXT: ret float %Q ; CHECK-NEXT: ret float %Q

View File

@ -26,7 +26,7 @@ define float @test2(float %reg109, float %reg1111) {
define float @test3(float %reg109, float %reg1111) { define float @test3(float %reg109, float %reg1111) {
; CHECK-LABEL: @test3 ; CHECK-LABEL: @test3
; CHECK-NEXT: %reg117 = fadd fast float %reg1111, %reg109 ; CHECK-NEXT: %reg117 = fadd fast float %reg109, %reg1111
; CHECK-NEXT: ret float %reg117 ; CHECK-NEXT: ret float %reg117
%reg115 = fadd fast float %reg109, -3.000000e+01 %reg115 = fadd fast float %reg109, -3.000000e+01
@ -106,7 +106,7 @@ define void @test6() {
define float @test7(float %A, float %B, float %C) { define float @test7(float %A, float %B, float %C) {
; CHECK-LABEL: @test7 ; CHECK-LABEL: @test7
; CHECK-NEXT: fadd fast float %B, %C ; CHECK-NEXT: fadd fast float %C, %B
; CHECK-NEXT: fmul fast float %A, %A ; CHECK-NEXT: fmul fast float %A, %A
; CHECK-NEXT: fmul fast float %1, %tmp2 ; CHECK-NEXT: fmul fast float %1, %tmp2
; CHECK-NEXT: ret float ; CHECK-NEXT: ret float
@ -121,7 +121,7 @@ define float @test7(float %A, float %B, float %C) {
define float @test8(float %X, float %Y, float %Z) { define float @test8(float %X, float %Y, float %Z) {
; CHECK-LABEL: @test8 ; CHECK-LABEL: @test8
; CHECK-NEXT: fmul fast float %X, %Y ; CHECK-NEXT: fmul fast float %Y, %X
; CHECK-NEXT: fsub fast float %Z ; CHECK-NEXT: fsub fast float %Z
; CHECK-NEXT: ret float ; CHECK-NEXT: ret float
@ -269,8 +269,8 @@ define float @test19(float %A, float %B) {
; With sub reassociation, constant folding can eliminate the uses of %a. ; With sub reassociation, constant folding can eliminate the uses of %a.
define float @test20(float %a, float %b, float %c) nounwind { define float @test20(float %a, float %b, float %c) nounwind {
; CHECK-LABEL: @test20 ; CHECK-LABEL: @test20
; CHECK-NEXT: fsub fast float -0.000000e+00, %c ; CHECK-NEXT: fsub fast float -0.000000e+00, %b
; CHECK-NEXT: fsub fast float %c.neg, %b ; CHECK-NEXT: fsub fast float %b.neg, %c
; CHECK-NEXT: ret float ; CHECK-NEXT: ret float
; FIXME: Should be able to generate the below, which may expose more ; FIXME: Should be able to generate the below, which may expose more

View File

@ -4,8 +4,8 @@ declare void @use(float)
define void @test1(float %x, float %y) { define void @test1(float %x, float %y) {
; CHECK-LABEL: test1 ; CHECK-LABEL: test1
; CHECK: fmul fast float %x, %y ; CHECK: fmul fast float %y, %x
; CHECK: fmul fast float %x, %y ; CHECK: fmul fast float %y, %x
; CHECK: fsub fast float %1, %2 ; CHECK: fsub fast float %1, %2
; CHECK: call void @use(float %{{.*}}) ; CHECK: call void @use(float %{{.*}})
; CHECK: call void @use(float %{{.*}}) ; CHECK: call void @use(float %{{.*}})
@ -20,8 +20,8 @@ define void @test1(float %x, float %y) {
define float @test2(float %x, float %y) { define float @test2(float %x, float %y) {
; CHECK-LABEL: test2 ; CHECK-LABEL: test2
; CHECK-NEXT: fmul fast float %x, %y ; CHECK-NEXT: fmul fast float %y, %x
; CHECK-NEXT: fmul fast float %x, %y ; CHECK-NEXT: fmul fast float %y, %x
; CHECK-NEXT: fsub fast float %1, %2 ; CHECK-NEXT: fsub fast float %1, %2
; CHECK-NEXT: ret float %3 ; CHECK-NEXT: ret float %3
@ -33,8 +33,8 @@ define float @test2(float %x, float %y) {
define float @test3(float %x, float %y) { define float @test3(float %x, float %y) {
; CHECK-LABEL: test3 ; CHECK-LABEL: test3
; CHECK-NEXT: %factor = fmul fast float %x, 2.000000e+00 ; CHECK-NEXT: %factor = fmul fast float %y, 2.000000e+00
; CHECK-NEXT: %tmp1 = fmul fast float %factor, %y ; CHECK-NEXT: %tmp1 = fmul fast float %factor, %x
; CHECK-NEXT: ret float %tmp1 ; CHECK-NEXT: ret float %tmp1
%1 = fmul fast float %x, %y %1 = fmul fast float %x, %y

View File

@ -3,9 +3,9 @@
define float @fmultistep1(float %a, float %b, float %c) { define float @fmultistep1(float %a, float %b, float %c) {
; Check that a*a*b+a*a*c is turned into a*(a*(b+c)). ; Check that a*a*b+a*a*c is turned into a*(a*(b+c)).
; CHECK-LABEL: @fmultistep1 ; CHECK-LABEL: @fmultistep1
; CHECK-NEXT: fadd fast float %b, %c ; CHECK-NEXT: fadd fast float %c, %b
; CHECK-NEXT: fmul fast float %a, %tmp2 ; CHECK-NEXT: fmul fast float %a, %tmp2
; CHECK-NEXT: fmul fast float %a, %tmp3 ; CHECK-NEXT: fmul fast float %tmp3, %a
; CHECK-NEXT: ret float ; CHECK-NEXT: ret float
%t0 = fmul fast float %a, %b %t0 = fmul fast float %a, %b
@ -19,9 +19,9 @@ define float @fmultistep1(float %a, float %b, float %c) {
define float @fmultistep2(float %a, float %b, float %c, float %d) { define float @fmultistep2(float %a, float %b, float %c, float %d) {
; Check that a*b+a*c+d is turned into a*(b+c)+d. ; Check that a*b+a*c+d is turned into a*(b+c)+d.
; CHECK-LABEL: @fmultistep2 ; CHECK-LABEL: @fmultistep2
; CHECK-NEXT: fadd fast float %b, %c ; CHECK-NEXT: fadd fast float %c, %b
; CHECK-NEXT: fmul fast float %a, %tmp ; CHECK-NEXT: fmul fast float %tmp, %a
; CHECK-NEXT: fadd fast float %d, %tmp1 ; CHECK-NEXT: fadd fast float %tmp1, %d
; CHECK-NEXT: ret float ; CHECK-NEXT: ret float
%t0 = fmul fast float %a, %b %t0 = fmul fast float %a, %b

View File

@ -3,9 +3,9 @@
define float @foo(float %a,float %b, float %c) { define float @foo(float %a,float %b, float %c) {
; CHECK: %mul3 = fmul float %a, %b ; CHECK: %mul3 = fmul float %a, %b
; CHECK-NEXT: fmul fast float %c, 2.000000e+00 ; CHECK-NEXT: fmul fast float %c, 2.000000e+00
; CHECK-NEXT: fadd fast float %b, %factor ; CHECK-NEXT: fadd fast float %factor, %b
; CHECK-NEXT: fmul fast float %a, %tmp1 ; CHECK-NEXT: fmul fast float %tmp1, %a
; CHECK-NEXT: fadd fast float %mul3, %tmp2 ; CHECK-NEXT: fadd fast float %tmp2, %mul3
; CHECK-NEXT: ret float ; CHECK-NEXT: ret float
%mul1 = fmul fast float %a, %c %mul1 = fmul fast float %a, %c
%mul2 = fmul fast float %a, %b %mul2 = fmul fast float %a, %b

View File

@ -8,9 +8,9 @@ define i64 @multistep1(i64 %a, i64 %b, i64 %c) {
%t2 = mul i64 %a, %c %t2 = mul i64 %a, %c
%t3 = mul i64 %a, %t2 ; a*(a*c) %t3 = mul i64 %a, %t2 ; a*(a*c)
%t4 = add i64 %t1, %t3 %t4 = add i64 %t1, %t3
; CHECK-NEXT: add i64 %b, %c ; CHECK-NEXT: add i64 %c, %b
; CHECK-NEXT: mul i64 %a, %tmp{{.*}}
; CHECK-NEXT: mul i64 %a, %tmp{{.*}} ; CHECK-NEXT: mul i64 %a, %tmp{{.*}}
; CHECK-NEXT: mul i64 %tmp{{.*}}, %a
; CHECK-NEXT: ret ; CHECK-NEXT: ret
ret i64 %t4 ret i64 %t4
} }
@ -22,9 +22,9 @@ define i64 @multistep2(i64 %a, i64 %b, i64 %c, i64 %d) {
%t1 = mul i64 %a, %c %t1 = mul i64 %a, %c
%t2 = add i64 %t1, %d ; a*c+d %t2 = add i64 %t1, %d ; a*c+d
%t3 = add i64 %t0, %t2 ; a*b+(a*c+d) %t3 = add i64 %t0, %t2 ; a*b+(a*c+d)
; CHECK-NEXT: add i64 %b, %c ; CHECK-NEXT: add i64 %c, %b
; CHECK-NEXT: mul i64 %a, %tmp{{.*}} ; CHECK-NEXT: mul i64 %tmp{{.*}}, %a
; CHECK-NEXT: add i64 %d, %tmp{{.*}} ; CHECK-NEXT: add i64 %tmp{{.*}}, %d
; CHECK-NEXT: ret ; CHECK-NEXT: ret
ret i64 %t3 ret i64 %t3
} }

View File

@ -27,11 +27,11 @@ define void @test2(i32 %a, i32 %b, i32 %c, i32 %d) {
; The initial add doesn't change so should not lose the nsw flag. ; The initial add doesn't change so should not lose the nsw flag.
; CHECK-LABEL: @test2( ; CHECK-LABEL: @test2(
%a0 = add nsw i32 %b, %a %a0 = add nsw i32 %b, %a
; CHECK-NEXT: %a0 = add nsw i32 %a, %b ; CHECK-NEXT: %a0 = add nsw i32 %b, %a
%a1 = add nsw i32 %a0, %d %a1 = add nsw i32 %a0, %d
; CHECK-NEXT: %a1 = add i32 %a0, %c ; CHECK-NEXT: %a1 = add i32 %a0, %c
%a2 = add nsw i32 %a1, %c %a2 = add nsw i32 %a1, %c
; CHECK-NEXT: %a2 = add i32 %d, %a1 ; CHECK-NEXT: %a2 = add i32 %a1, %d
call void @use(i32 %a2) call void @use(i32 %a2)
; CHECK-NEXT: call void @use ; CHECK-NEXT: call void @use
ret void ret void

View File

@ -1,29 +1,28 @@
; RUN: opt -S -reassociate -dce < %s | FileCheck %s ; RUN: opt -S -reassociate < %s | FileCheck %s
; rdar://8944681 ; rdar://8944681
; Reassociate should clear optional flags like nsw when reassociating. ; Reassociate should clear optional flags like nsw when reassociating.
; CHECK-LABEL: @test0( ; CHECK-LABEL: @test0(
; CHECK: %z = add i64 %b, 2 ; CHECK: %y = add i64 %b, %a
define i64 @test0(i64 %a, i64 %b) { ; CHECK: %z = add i64 %y, %c
%x = add nsw i64 %a, 2 define i64 @test0(i64 %a, i64 %b, i64 %c) {
%y = add nsw i64 %x, %b %y = add nsw i64 %c, %b
%z = sub nsw i64 %y, %a %z = add i64 %y, %a
ret i64 %z ret i64 %z
} }
; CHECK-LABEL: @test1( ; CHECK-LABEL: @test1(
; CHECK: %y = mul i64 %a, 6 ; CHECK: %y = add i64 %b, %a
; CHECK: %z = sub nsw i64 %y, %a ; CHECK: %z = add i64 %y, %c
define i64 @test1(i64 %a, i64 %b) { define i64 @test1(i64 %a, i64 %b, i64 %c) {
%x = add nsw i64 %a, %a %y = add i64 %c, %b
%y = mul nsw i64 %x, 3 %z = add nsw i64 %y, %a
%z = sub nsw i64 %y, %a
ret i64 %z ret i64 %z
} }
; PR9215 ; PR9215
; CHECK: %s = add nsw i32 %x, %y ; CHECK: %s = add nsw i32 %y, %x
define i32 @test2(i32 %x, i32 %y) { define i32 @test2(i32 %x, i32 %y) {
%s = add nsw i32 %x, %y %s = add nsw i32 %x, %y
ret i32 %s ret i32 %s

View File

@ -3,7 +3,7 @@
define i32 @test1(i32 %X, i32 %Y) { define i32 @test1(i32 %X, i32 %Y) {
; CHECK-LABEL: test1 ; CHECK-LABEL: test1
; CHECK-NEXT: %tmp = add i32 %X, %Y ; CHECK-NEXT: %tmp = add i32 %Y, %X
; CHECK-NEXT: %tmp1 = shl i32 %tmp, 1 ; CHECK-NEXT: %tmp1 = shl i32 %tmp, 1
; CHECK-NEXT: ret i32 %tmp1 ; CHECK-NEXT: ret i32 %tmp1

View File

@ -15,7 +15,7 @@ define i32 @test1(i32 %A, i32 %B) {
; With sub reassociation, constant folding can eliminate the uses of %a. ; With sub reassociation, constant folding can eliminate the uses of %a.
define i32 @test2(i32 %a, i32 %b, i32 %c) nounwind { define i32 @test2(i32 %a, i32 %b, i32 %c) nounwind {
; CHECK-LABEL: @test2 ; CHECK-LABEL: @test2
; CHECK-NEXT: %sum = add i32 %b, %c ; CHECK-NEXT: %sum = add i32 %c, %b
; CHECK-NEXT: %tmp7 = sub i32 0, %sum ; CHECK-NEXT: %tmp7 = sub i32 0, %sum
; CHECK-NEXT: ret i32 %tmp7 ; CHECK-NEXT: ret i32 %tmp7

View File

@ -45,7 +45,7 @@ define i32 @xor3(i32 %x, i32 %y) {
;CHECK-LABEL: @xor3( ;CHECK-LABEL: @xor3(
;CHECK: %and.ra = and i32 %x, -436 ;CHECK: %and.ra = and i32 %x, -436
;CHECK: %xor = xor i32 %y, 123 ;CHECK: %xor = xor i32 %y, 123
;CHECK: %xor1 = xor i32 %and.ra, %xor ;CHECK: %xor1 = xor i32 %xor, %and.ra
} }
; Test rule: (x | c1) ^ c2 = (x & ~c1) ^ (c1 ^ c2) ; Test rule: (x | c1) ^ c2 = (x & ~c1) ^ (c1 ^ c2)
@ -57,7 +57,7 @@ define i32 @xor4(i32 %x, i32 %y) {
; CHECK-LABEL: @xor4( ; CHECK-LABEL: @xor4(
; CHECK: %and = and i32 %x, -124 ; CHECK: %and = and i32 %x, -124
; CHECK: %xor = xor i32 %y, 435 ; CHECK: %xor = xor i32 %y, 435
; CHECK: %xor1 = xor i32 %and, %xor ; CHECK: %xor1 = xor i32 %xor, %and
} }
; ========================================================================== ; ==========================================================================
@ -89,7 +89,7 @@ define i32 @xor_special2(i32 %x, i32 %y) {
ret i32 %xor1 ret i32 %xor1
; CHECK-LABEL: @xor_special2( ; CHECK-LABEL: @xor_special2(
; CHECK: %xor = xor i32 %y, 123 ; CHECK: %xor = xor i32 %y, 123
; CHECK: %xor1 = xor i32 %x, %xor ; CHECK: %xor1 = xor i32 %xor, %x
; CHECK: ret i32 %xor1 ; CHECK: ret i32 %xor1
} }