[Reassociate] As the expression tree is rewritten make sure the operands are
authorChad Rosier <mcrosier@codeaurora.org>
Mon, 17 Nov 2014 16:33:50 +0000 (16:33 +0000)
committerChad Rosier <mcrosier@codeaurora.org>
Mon, 17 Nov 2014 16:33:50 +0000 (16:33 +0000)
emitted in canonical form.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@222142 91177308-0d34-0410-b5e6-96231b3b80d8

17 files changed:
lib/Transforms/Scalar/Reassociate.cpp
test/Transforms/Reassociate/2002-05-15-MissedTree.ll
test/Transforms/Reassociate/2002-05-15-SubReassociate.ll
test/Transforms/Reassociate/basictest.ll
test/Transforms/Reassociate/canonicalize-neg-const.ll
test/Transforms/Reassociate/commute.ll
test/Transforms/Reassociate/fast-SubReassociate.ll
test/Transforms/Reassociate/fast-basictest.ll
test/Transforms/Reassociate/fast-fp-commute.ll
test/Transforms/Reassociate/fast-multistep.ll
test/Transforms/Reassociate/mixed-fast-nonfast-fp.ll
test/Transforms/Reassociate/multistep.ll
test/Transforms/Reassociate/no-op.ll
test/Transforms/Reassociate/optional-flags.ll
test/Transforms/Reassociate/shift-factor.ll
test/Transforms/Reassociate/subtest.ll
test/Transforms/Reassociate/xor_reassoc.ll

index abe7f28cece5bf2dcc4484015d0e52984e392267..04e724077c12c07b251abebddff9e3e46596bbad 100644 (file)
@@ -791,14 +791,11 @@ void Reassociate::RewriteExprTree(BinaryOperator *I,
       Value *OldLHS = Op->getOperand(0);
       Value *OldRHS = Op->getOperand(1);
 
-      if (NewLHS == OldLHS && NewRHS == OldRHS)
-        // Nothing changed, leave it alone.
-        break;
-
-      if (NewLHS == OldRHS && NewRHS == OldLHS) {
-        // The order of the operands was reversed.  Swap them.
+      // The new operation differs trivially from the original.
+      if ((NewLHS == OldLHS && NewRHS == OldRHS) ||
+          (NewLHS == OldRHS && NewRHS == OldLHS)) {
         DEBUG(dbgs() << "RA: " << *Op << '\n');
-        Op->swapOperands();
+        canonicalizeOperands(Op);
         DEBUG(dbgs() << "TO: " << *Op << '\n');
         MadeChange = true;
         ++NumChanged;
@@ -820,6 +817,8 @@ void Reassociate::RewriteExprTree(BinaryOperator *I,
           NodesToRewrite.push_back(BO);
         Op->setOperand(1, NewRHS);
       }
+      // Put the operands in canonical form.
+      canonicalizeOperands(Op);
       DEBUG(dbgs() << "TO: " << *Op << '\n');
 
       ExpressionChanged = Op;
@@ -856,6 +855,7 @@ void Reassociate::RewriteExprTree(BinaryOperator *I,
     // into it.
     BinaryOperator *BO = isReassociableOp(Op->getOperand(0), Opcode);
     if (BO && !NotRewritable.count(BO)) {
+      canonicalizeOperands(Op);
       Op = BO;
       continue;
     }
@@ -880,6 +880,7 @@ void Reassociate::RewriteExprTree(BinaryOperator *I,
 
     DEBUG(dbgs() << "RA: " << *Op << '\n');
     Op->setOperand(0, NewOp);
+    canonicalizeOperands(Op);
     DEBUG(dbgs() << "TO: " << *Op << '\n');
     ExpressionChanged = Op;
     MadeChange = true;
index 5f3c9209aedab7b6c5fc08deec8f07662fc43793..e7e1ce842fe642749e4ef6aee3b570cfd80747fb 100644 (file)
@@ -2,7 +2,7 @@
 
 define i32 @test1(i32 %A, i32 %B) {
 ; CHECK-LABEL: test1
-; CHECK: %Z = add i32 %B, %A
+; CHECK: %Z = add i32 %A, %B
 ; CHECK: ret i32 %Z
        %W = add i32 %B, -5
        %Y = add i32 %A, 5
index 29c178ffec3ec9c9b85351eface88fdd865043bd..1473c72c0534842e1fc1566849e7c9e7056bb665 100644 (file)
@@ -16,7 +16,7 @@ define i32 @test1(i32 %A, i32 %B) {
 ; With sub reassociation, constant folding can eliminate the two 12 constants.
 define i32 @test2(i32 %A, i32 %B, i32 %C, i32 %D) {
 ; CHECK-LABEL: test2
-; CHECK-NEXT: %sum = add i32 %B, %A
+; CHECK-NEXT: %sum = add i32 %A, %B
 ; CHECK-NEXT: %sum1 = add i32 %sum, %C
 ; CHECK-NEXT: %Q = sub i32 %D, %sum1
 ; CHECK-NEXT: ret i32 %Q
index 0194ce2e302cafd00e44bfb1a940c197ce0a3262..ed09ffdb7b0d879deb147bb243115af467169560 100644 (file)
@@ -17,7 +17,7 @@ define i32 @test2(i32 %reg109, i32 %reg1111) {
   ret i32 %reg117
 
 ; CHECK-LABEL: @test2
-; CHECK-NEXT: %reg117 = add i32 %reg1111, %reg109
+; CHECK-NEXT: %reg117 = add i32 %reg109, %reg1111
 ; CHECK-NEXT: ret i32 %reg117
 }
 
@@ -121,7 +121,7 @@ define i32 @test7(i32 %A, i32 %B, i32 %C) {
   ret i32 %r
 
 ; CHECK-LABEL: @test7
-; CHECK-NEXT: add i32 %C, %B
+; CHECK-NEXT: add i32 %B, %C
 ; CHECK-NEXT: mul i32 
 ; CHECK-NEXT: mul i32 
 ; CHECK-NEXT: ret i32 
@@ -135,7 +135,7 @@ define i32 @test8(i32 %X, i32 %Y, i32 %Z) {
   ret i32 %C
 
 ; CHECK-LABEL: @test8
-; CHECK-NEXT: %A = mul i32 %Y, %X
+; CHECK-NEXT: %A = mul i32 %X, %Y
 ; CHECK-NEXT: %C = sub i32 %Z, %A
 ; CHECK-NEXT: ret i32 %C
 }
index e85a963f6ddac32b54f90029703b324cad33c8de..1c86312afad2443e74848dacdfdf47744da9a8ad 100644 (file)
@@ -53,7 +53,7 @@ define double @test3(double %x, double %y) {
 define i64 @test4(i64 %x, i64 %y) {
 ; CHECK-LABEL: @test4
 ; CHECK-NEXT: mul i64 %y, 1234
-; CHECK-NEXT: add i64 %mul, %x
+; CHECK-NEXT: add i64 %x, %mul
 ; CHECK-NEXT: ret i64 %sub
 
   %mul = mul i64 %y, -1234
index 760e51b05e1c3bdb989d97a968257ae52c7a336e..1c80248170c85eb669aacb84288539bb608caa0d 100644 (file)
@@ -4,8 +4,8 @@ declare void @use(i32)
 
 define void @test1(i32 %x, i32 %y) {
 ; CHECK-LABEL: test1
-; CHECK: mul i32 %y, %x
-; CHECK: mul i32 %y, %x
+; CHECK: mul i32 %x, %y
+; CHECK: mul i32 %x, %y
 ; CHECK: sub i32 %1, %2
 ; CHECK: call void @use(i32 %{{.*}})
 ; CHECK: call void @use(i32 %{{.*}})
index db4191a4fa1b3e3c469fca79e15277cdd19f5bab..4afd64664c58044cfee2096bcd1b1e8211a63fb5 100644 (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.
 define float @test4(float %A, float %B, float %C, float %D) {
 ; CHECK-LABEL: test4
-; CHECK-NEXT: %B.neg = fsub fast float -0.000000e+00, %B
-; CHECK-NEXT: %O.neg = fsub fast float %B.neg, %A
+; CHECK-NEXT: %A.neg = fsub fast float -0.000000e+00, %A
+; CHECK-NEXT: %O.neg = fsub fast float %A.neg, %B
 ; CHECK-NEXT: %P = fsub fast float %O.neg, %C
 ; CHECK-NEXT: %Q = fadd fast float %P, %D
 ; CHECK-NEXT: ret float %Q
index 67b07f4d26990de8584b6a47855d4039d5afa92d..4d287fb40cf39e2a2c212e512a86cf1af199e07a 100644 (file)
@@ -26,7 +26,7 @@ define float @test2(float %reg109, float %reg1111) {
 
 define float @test3(float %reg109, float %reg1111) {
 ; CHECK-LABEL: @test3
-; CHECK-NEXT: %reg117 = fadd fast float %reg109, %reg1111
+; CHECK-NEXT: %reg117 = fadd fast float %reg1111, %reg109
 ; CHECK-NEXT:  ret float %reg117
 
   %reg115 = fadd fast float %reg109, -3.000000e+01
@@ -106,7 +106,7 @@ define void @test6() {
 
 define float @test7(float %A, float %B, float %C) {
 ; CHECK-LABEL: @test7
-; CHECK-NEXT: fadd fast float %C, %B
+; CHECK-NEXT: fadd fast float %B, %C
 ; CHECK-NEXT: fmul fast float %A, %A
 ; CHECK-NEXT: fmul fast float %1, %tmp2
 ; 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) {
 ; CHECK-LABEL: @test8
-; CHECK-NEXT: fmul fast float %Y, %X
+; CHECK-NEXT: fmul fast float %X, %Y
 ; CHECK-NEXT: fsub fast float %Z
 ; 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.
 define float @test20(float %a, float %b, float %c) nounwind  {
 ; CHECK-LABEL: @test20
-; CHECK-NEXT: fsub fast float -0.000000e+00, %b
-; CHECK-NEXT: fsub fast float %b.neg, %c
+; CHECK-NEXT: fsub fast float -0.000000e+00, %c
+; CHECK-NEXT: fsub fast float %c.neg, %b
 ; CHECK-NEXT: ret float
 
 ; FIXME: Should be able to generate the below, which may expose more
index ad89607a21e4db0fc696f659cb749753766e65ff..d3e0d9f98822c6eb0222f2929281e74195626a63 100644 (file)
@@ -4,8 +4,8 @@ declare void @use(float)
 
 define void @test1(float %x, float %y) {
 ; CHECK-LABEL: test1
-; CHECK: fmul fast float %y, %x
-; CHECK: fmul fast float %y, %x
+; CHECK: fmul fast float %x, %y
+; CHECK: fmul fast float %x, %y
 ; CHECK: fsub fast float %1, %2
 ; 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) {
 ; CHECK-LABEL: test2
-; CHECK-NEXT: fmul fast float %y, %x
-; CHECK-NEXT: fmul fast float %y, %x
+; CHECK-NEXT: fmul fast float %x, %y
+; CHECK-NEXT: fmul fast float %x, %y
 ; CHECK-NEXT: fsub fast float %1, %2
 ; CHECK-NEXT: ret float %3
 
@@ -33,8 +33,8 @@ define float @test2(float %x, float %y) {
 
 define float @test3(float %x, float %y) {
 ; CHECK-LABEL: test3
-; CHECK-NEXT: %factor = fmul fast float %y, 2.000000e+00
-; CHECK-NEXT: %tmp1 = fmul fast float %factor, %x
+; CHECK-NEXT: %factor = fmul fast float %x, 2.000000e+00
+; CHECK-NEXT: %tmp1 = fmul fast float %factor, %y
 ; CHECK-NEXT: ret float %tmp1
 
   %1 = fmul fast float %x, %y
index 45e15c7f35391bf4f91b9ac1ba7465bd55510e3e..5671b0fecd26175728ceb2365fc7633a73095e0f 100644 (file)
@@ -3,9 +3,9 @@
 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-LABEL: @fmultistep1
-; CHECK-NEXT: fadd fast float %c, %b
+; CHECK-NEXT: fadd fast float %b, %c
 ; CHECK-NEXT: fmul fast float %a, %tmp2
-; CHECK-NEXT: fmul fast float %tmp3, %a
+; CHECK-NEXT: fmul fast float %a, %tmp3
 ; CHECK-NEXT: ret float
 
   %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) {
 ; Check that a*b+a*c+d is turned into a*(b+c)+d.
 ; CHECK-LABEL: @fmultistep2
-; CHECK-NEXT: fadd fast float %c, %b
-; CHECK-NEXT: fmul fast float %tmp, %a
-; CHECK-NEXT: fadd fast float %tmp1, %d
+; CHECK-NEXT: fadd fast float %b, %c
+; CHECK-NEXT: fmul fast float %a, %tmp
+; CHECK-NEXT: fadd fast float %d, %tmp1
 ; CHECK-NEXT: ret float
 
   %t0 = fmul fast float %a, %b
index f51c0c17fe7d2f8c8c6690727e2cc1837454a7bf..8ca0ff655f36dc6b36bf3ed6d855ea096fb1f0f6 100644 (file)
@@ -3,9 +3,9 @@
 define float @foo(float %a,float %b, float %c) {
 ; CHECK: %mul3 = fmul float %a, %b
 ; CHECK-NEXT: fmul fast float %c, 2.000000e+00
-; CHECK-NEXT: fadd fast float %factor, %b
-; CHECK-NEXT: fmul fast float %tmp1, %a
-; CHECK-NEXT: fadd fast float %tmp2, %mul3
+; CHECK-NEXT: fadd fast float %b, %factor
+; CHECK-NEXT: fmul fast float %a, %tmp1
+; CHECK-NEXT: fadd fast float %mul3, %tmp2
 ; CHECK-NEXT: ret float
   %mul1 = fmul fast float %a, %c
   %mul2 = fmul fast float %a, %b
index c499646a8b6ac9e3e4ab1be887d0902a1a4e591e..03ed18342df975a5280cb2d3ae09f442a4376a8b 100644 (file)
@@ -8,9 +8,9 @@ define i64 @multistep1(i64 %a, i64 %b, i64 %c) {
   %t2 = mul i64 %a, %c
   %t3 = mul i64 %a, %t2 ; a*(a*c)
   %t4 = add i64 %t1, %t3
-; CHECK-NEXT: add i64 %c, %b
+; CHECK-NEXT: add i64 %b, %c
+; CHECK-NEXT: mul i64 %a, %tmp{{.*}}
 ; CHECK-NEXT: mul i64 %a, %tmp{{.*}}
-; CHECK-NEXT: mul i64 %tmp{{.*}}, %a
 ; CHECK-NEXT: ret
   ret i64 %t4
 }
@@ -22,9 +22,9 @@ define i64 @multistep2(i64 %a, i64 %b, i64 %c, i64 %d) {
   %t1 = mul i64 %a, %c
   %t2 = add i64 %t1, %d ; a*c+d
   %t3 = add i64 %t0, %t2 ; a*b+(a*c+d)
-; CHECK-NEXT: add i64 %c, %b
-; CHECK-NEXT: mul i64 %tmp{{.*}}, %a
-; CHECK-NEXT: add i64 %tmp{{.*}}, %d
+; CHECK-NEXT: add i64 %b, %c
+; CHECK-NEXT: mul i64 %a, %tmp{{.*}}
+; CHECK-NEXT: add i64 %d, %tmp{{.*}}
 ; CHECK-NEXT: ret
   ret i64 %t3
 }
index 7b02df99464b85f0f57ac75a4d54adfbc54866a3..fe8d78328966555e7fdc15918ef9d1ef31bf9b9d 100644 (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.
 ; CHECK-LABEL: @test2(
   %a0 = add nsw i32 %b, %a
-; CHECK-NEXT: %a0 = add nsw i32 %b, %a
+; CHECK-NEXT: %a0 = add nsw i32 %a, %b
   %a1 = add nsw i32 %a0, %d
 ; CHECK-NEXT: %a1 = add i32 %a0, %c
   %a2 = add nsw i32 %a1, %c
-; CHECK-NEXT: %a2 = add i32 %a1, %d
+; CHECK-NEXT: %a2 = add i32 %d, %a1
   call void @use(i32 %a2)
 ; CHECK-NEXT: call void @use
   ret void
index bf599be78bc9dd1756cab64d8a237ad77b7680ca..b8cd03e8cf4ccd7d7a5b4d16bb6ff9c1545d35db 100644 (file)
@@ -4,8 +4,8 @@
 ; Reassociate should clear optional flags like nsw when reassociating.
 
 ; CHECK-LABEL: @test0(
-; CHECK: %y = add i64 %b, %a
-; CHECK: %z = add i64 %y, %c
+; CHECK: %y = add i64 %a, %b
+; CHECK: %z = add i64 %c, %y
 define i64 @test0(i64 %a, i64 %b, i64 %c) {
   %y = add nsw i64 %c, %b
   %z = add i64 %y, %a
@@ -13,8 +13,8 @@ define i64 @test0(i64 %a, i64 %b, i64 %c) {
 }
 
 ; CHECK-LABEL: @test1(
-; CHECK: %y = add i64 %b, %a
-; CHECK: %z = add i64 %y, %c
+; CHECK: %y = add i64 %a, %b
+; CHECK: %z = add i64 %c, %y
 define i64 @test1(i64 %a, i64 %b, i64 %c) {
   %y = add i64 %c, %b
   %z = add nsw i64 %y, %a
@@ -22,7 +22,7 @@ define i64 @test1(i64 %a, i64 %b, i64 %c) {
 }
 
 ; PR9215
-; CHECK: %s = add nsw i32 %y, %x
+; CHECK: %s = add nsw i32 %x, %y
 define i32 @test2(i32 %x, i32 %y) {
   %s = add nsw i32 %x, %y
   ret i32 %s
index 8fbf1b9d4bc50eab53765e462a2d49a0e4dd8686..f63ed59819560e797ee4299464d60b350d43bd46 100644 (file)
@@ -3,7 +3,7 @@
 
 define i32 @test1(i32 %X, i32 %Y) {
 ; CHECK-LABEL: test1
-; CHECK-NEXT: %tmp = add i32 %Y, %X
+; CHECK-NEXT: %tmp = add i32 %X, %Y
 ; CHECK-NEXT: %tmp1 = shl i32 %tmp, 1
 ; CHECK-NEXT: ret i32 %tmp1
 
index e6263d85522c9051f9fdf0a9219e7c9cc0772008..e0406a6a284223db4bbe715babc2563ed720705e 100644 (file)
@@ -15,7 +15,7 @@ define i32 @test1(i32 %A, i32 %B) {
 ; With sub reassociation, constant folding can eliminate the uses of %a.
 define i32 @test2(i32 %a, i32 %b, i32 %c) nounwind  {
 ; CHECK-LABEL: @test2
-; CHECK-NEXT: %sum = add i32 %c, %b
+; CHECK-NEXT: %sum = add i32 %b, %c
 ; CHECK-NEXT: %tmp7 = sub i32 0, %sum
 ; CHECK-NEXT: ret i32 %tmp7
 
index a22689805fb582ae7a9f3b5901213eeca1ff288d..7af4f1557018058d3454fa07cec403b964c41fc3 100644 (file)
@@ -45,7 +45,7 @@ define i32 @xor3(i32 %x, i32 %y) {
 ;CHECK-LABEL: @xor3(
 ;CHECK: %and.ra = and i32 %x, -436
 ;CHECK: %xor = xor i32 %y, 123
-;CHECK: %xor1 = xor i32 %xor, %and.ra
+;CHECK: %xor1 = xor i32 %and.ra, %xor
 }
 
 ; Test rule: (x | c1) ^ c2 = (x & ~c1) ^ (c1 ^ c2)
@@ -57,7 +57,7 @@ define i32 @xor4(i32 %x, i32 %y) {
 ; CHECK-LABEL: @xor4(
 ; CHECK: %and = and i32 %x, -124
 ; CHECK: %xor = xor i32 %y, 435
-; CHECK: %xor1 = xor i32 %xor, %and
+; CHECK: %xor1 = xor i32 %and, %xor
 }
 
 ; ==========================================================================
@@ -89,7 +89,7 @@ define i32 @xor_special2(i32 %x, i32 %y) {
   ret i32 %xor1
 ; CHECK-LABEL: @xor_special2(
 ; CHECK: %xor = xor i32 %y, 123
-; CHECK: %xor1 = xor i32 %xor, %x
+; CHECK: %xor1 = xor i32 %x, %xor
 ; CHECK: ret i32 %xor1
 }