[NaryReassociate] avoid running forever
authorJingyue Wu <jingyue@google.com>
Wed, 13 May 2015 18:12:24 +0000 (18:12 +0000)
committerJingyue Wu <jingyue@google.com>
Wed, 13 May 2015 18:12:24 +0000 (18:12 +0000)
Avoid running forever by checking we are not reassociating an expression into
the same form.

Tested with @avoid_infinite_loops in nary-add.ll

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

lib/Transforms/Scalar/NaryReassociate.cpp
test/Transforms/NaryReassociate/nary-add.ll

index 7d3080dcd9bf424a916195fa08ec7a288398c00d..af61068c97bdf5130be4c379ed6c50b4b8b7c8a4 100644 (file)
@@ -217,10 +217,14 @@ Instruction *NaryReassociate::tryReassociateAdd(Value *LHS, Value *RHS,
     //   = (A + RHS) + B or (B + RHS) + A
     const SCEV *AExpr = SE->getSCEV(A), *BExpr = SE->getSCEV(B);
     const SCEV *RHSExpr = SE->getSCEV(RHS);
-    if (auto *NewI = tryReassociatedAdd(SE->getAddExpr(AExpr, RHSExpr), B, I))
-      return NewI;
-    if (auto *NewI = tryReassociatedAdd(SE->getAddExpr(BExpr, RHSExpr), A, I))
-      return NewI;
+    if (BExpr != RHSExpr) {
+      if (auto *NewI = tryReassociatedAdd(SE->getAddExpr(AExpr, RHSExpr), B, I))
+        return NewI;
+    }
+    if (AExpr != RHSExpr) {
+      if (auto *NewI = tryReassociatedAdd(SE->getAddExpr(BExpr, RHSExpr), A, I))
+        return NewI;
+    }
   }
   return nullptr;
 }
index 39d7c59ef9d8bb43e61b8d5215829ed28b1e47f8..b3093ff6ecd62696c9ff47159aa671e157f9d13f 100644 (file)
@@ -196,3 +196,14 @@ define void @iterative(i32 %a, i32 %b, i32 %c) {
 
   ret void
 }
+
+define void @avoid_infinite_loop(i32 %a, i32 %b) {
+; CHECK-LABEL: @avoid_infinite_loop
+  %ab = add i32 %a, %b
+; CHECK-NEXT: %ab
+  %ab2 = add i32 %ab, %b
+; CHECK-NEXT: %ab2
+  call void @foo(i32 %ab2)
+; CHECK-NEXT: @foo(i32 %ab2)
+  ret void
+}