[llvm-commits] [llvm] r156379 - in /llvm/trunk: lib/Transforms/Scalar/Reassociate.cpp test/Transforms/Reassociate/2012-05-08-UndefLeak.ll test/Transforms/Reassociate/multistep.ll

Duncan Sands baldrick at free.fr
Tue May 8 05:16:05 PDT 2012


Author: baldrick
Date: Tue May  8 07:16:05 2012
New Revision: 156379

URL: http://llvm.org/viewvc/llvm-project?rev=156379&view=rev
Log:
Calling ReassociateExpression recursively is extremely dangerous since it will
replace the operands of expressions with only one use with undef and generate
a new expression for the original without using RAUW to update the original.
Thus any copies of the original expression held in a vector may end up
referring to some bogus value - and using a ValueHandle won't help since there
is no RAUW.  There is already a mechanism for getting the effect of recursion
non-recursively: adding the value to be recursed on to RedoInsts.  But it wasn't
being used systematically.  Have various places where recursion had snuck in at
some point use the RedoInsts mechanism instead.  Fixes PR12169.

Added:
    llvm/trunk/test/Transforms/Reassociate/2012-05-08-UndefLeak.ll
    llvm/trunk/test/Transforms/Reassociate/multistep.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=156379&r1=156378&r2=156379&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/Reassociate.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/Reassociate.cpp Tue May  8 07:16:05 2012
@@ -910,14 +910,14 @@
     // A*A*B + A*A*C   -->   A*(A*B+A*C)   -->   A*(A*(B+C))
     assert(NumAddedValues > 1 && "Each occurrence should contribute a value");
     (void)NumAddedValues;
-    V = ReassociateExpression(cast<BinaryOperator>(V));
+    RedoInsts.push_back(V);
 
     // Create the multiply.
     Value *V2 = BinaryOperator::CreateMul(V, MaxOccVal, "tmp", I);
 
     // Rerun associate on the multiply in case the inner expression turned into
     // a multiply.  We want to make sure that we keep things in canonical form.
-    V2 = ReassociateExpression(cast<BinaryOperator>(V2));
+    RedoInsts.push_back(V2);
 
     // If every add operand included the factor (e.g. "A*B + A*C"), then the
     // entire result expression is just the multiply "A*(B+C)".
@@ -1070,9 +1070,8 @@
 
     // Reset the base value of the first factor to the new expression tree.
     // We'll remove all the factors with the same power in a second pass.
-    Factors[LastIdx].Base
-      = ReassociateExpression(
-          cast<BinaryOperator>(buildMultiplyTree(Builder, InnerProduct)));
+    Factors[LastIdx].Base = buildMultiplyTree(Builder, InnerProduct);
+    RedoInsts.push_back(Factors[LastIdx].Base);
 
     LastIdx = Idx;
   }
@@ -1098,8 +1097,9 @@
   if (OuterProduct.size() == 1)
     return OuterProduct.front();
 
-  return ReassociateExpression(
-    cast<BinaryOperator>(buildMultiplyTree(Builder, OuterProduct)));
+  Value *V = buildMultiplyTree(Builder, OuterProduct);
+  RedoInsts.push_back(V);
+  return V;
 }
 
 Value *Reassociate::OptimizeMul(BinaryOperator *I,

Added: llvm/trunk/test/Transforms/Reassociate/2012-05-08-UndefLeak.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/Reassociate/2012-05-08-UndefLeak.ll?rev=156379&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/Reassociate/2012-05-08-UndefLeak.ll (added)
+++ llvm/trunk/test/Transforms/Reassociate/2012-05-08-UndefLeak.ll Tue May  8 07:16:05 2012
@@ -0,0 +1,79 @@
+; RUN: opt < %s -reassociate -S | FileCheck %s
+; PR12169
+
+define i64 @f(i64 %x0) {
+; CHECK-NOT: undef
+  %t0 = add i64 %x0, 1
+  %t1 = add i64 %x0, 2
+  %t2 = add i64 %x0, 3
+  %t3 = add i64 %x0, 4
+  %t4 = add i64 %x0, 5
+  %t5 = add i64 %x0, 6
+  %t6 = add i64 %x0, 7
+  %t7 = add i64 %x0, 8
+  %t8 = add i64 %x0, 9
+  %t9 = add i64 %x0, 10
+  %t10 = add i64 %x0, 11
+  %t11 = add i64 %x0, 12
+  %t12 = add i64 %x0, 13
+  %t13 = add i64 %x0, 14
+  %t14 = add i64 %x0, 15
+  %t15 = add i64 %x0, 16
+  %t16 = add i64 %x0, 17
+  %t17 = add i64 %x0, 18
+  %t18 = add i64 %t17, %t0
+  %t19 = add i64 %t18, %t1
+  %t20 = add i64 %t19, %t2
+  %t21 = add i64 %t20, %t3
+  %t22 = add i64 %t21, %t4
+  %t23 = add i64 %t22, %t5
+  %t24 = add i64 %t23, %t6
+  %t25 = add i64 %t24, %t7
+  %t26 = add i64 %t25, %t8
+  %t27 = add i64 %t26, %t9
+  %t28 = add i64 %t27, %t10
+  %t29 = add i64 %t28, %t11
+  %t30 = add i64 %t29, %t12
+  %t31 = add i64 %t30, %t13
+  %t32 = add i64 %t31, %t14
+  %t33 = add i64 %t32, %t15
+  %t34 = add i64 %t33, %t16
+  %t35 = add i64 %t34, %x0
+  %t36 = add i64 %t0, %t1
+  %t37 = add i64 %t36, %t2
+  %t38 = add i64 %t37, %t3
+  %t39 = add i64 %t38, %t4
+  %t40 = add i64 %t39, %t5
+  %t41 = add i64 %t40, %t6
+  %t42 = add i64 %t41, %t7
+  %t43 = add i64 %t42, %t8
+  %t44 = add i64 %t43, %t9
+  %t45 = add i64 %t44, %t10
+  %t46 = add i64 %t45, %t11
+  %t47 = add i64 %t46, %t12
+  %t48 = add i64 %t47, %t13
+  %t49 = add i64 %t48, %t14
+  %t50 = add i64 %t49, %t15
+  %t51 = add i64 %t50, %t16
+  %t52 = add i64 %t51, %t17
+  %t53 = add i64 %t52, %t18
+  %t54 = add i64 %t53, %t19
+  %t55 = add i64 %t54, %t20
+  %t56 = add i64 %t55, %t21
+  %t57 = add i64 %t56, %t22
+  %t58 = add i64 %t57, %t23
+  %t59 = add i64 %t58, %t24
+  %t60 = add i64 %t59, %t25
+  %t61 = add i64 %t60, %t26
+  %t62 = add i64 %t61, %t27
+  %t63 = add i64 %t62, %t28
+  %t64 = add i64 %t63, %t29
+  %t65 = add i64 %t64, %t30
+  %t66 = add i64 %t65, %t31
+  %t67 = add i64 %t66, %t32
+  %t68 = add i64 %t67, %t33
+  %t69 = add i64 %t68, %t34
+  %t70 = add i64 %t69, %t35
+  %t71 = add i64 %t70, %x0
+  ret i64 %t71
+}

Added: llvm/trunk/test/Transforms/Reassociate/multistep.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/Reassociate/multistep.ll?rev=156379&view=auto
==============================================================================
--- llvm/trunk/test/Transforms/Reassociate/multistep.ll (added)
+++ llvm/trunk/test/Transforms/Reassociate/multistep.ll Tue May  8 07:16:05 2012
@@ -0,0 +1,31 @@
+; RUN: opt < %s -reassociate -S | FileCheck %s
+
+define i64 @multistep1(i64 %a, i64 %b, i64 %c) {
+; Check that a*a*b+a*a*c is turned into a*(a*(b+c)).
+; CHECK: @multistep1
+  %t0 = mul i64 %a, %b
+  %t1 = mul i64 %a, %t0 ; a*(a*b)
+  %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: mul i64 %tmp{{.*}}, %a
+; CHECK-NEXT: mul i64 %tmp{{.*}}, %a
+; CHECK-NEXT: ret
+  ret i64 %t4
+}
+
+define i64 @multistep2(i64 %a, i64 %b, i64 %c, i64 %d) {
+; Check that a*b+a*c+d is turned into a*(b+c)+d.
+; CHECK: @multistep2
+  %t0 = mul i64 %a, %b
+  %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: ret
+  ret i64 %t3
+}
+





More information about the llvm-commits mailing list