LSR: fix IVUsers isSimplifiedLoopNest to perform a full domtree walk
authorAndrew Trick <atrick@apple.com>
Tue, 20 Mar 2012 21:24:40 +0000 (21:24 +0000)
committerAndrew Trick <atrick@apple.com>
Tue, 20 Mar 2012 21:24:40 +0000 (21:24 +0000)
instead of skipping the current loop.

My prior fix was incomplete because of an overzealous compile-time optimization:
Better fix for: <rdar://problem/11049788> Segmentation fault: 11 in LoopStrengthReduce

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

lib/Analysis/IVUsers.cpp
test/Transforms/LoopStrengthReduce/2012-03-15-nopreheader.ll

index c598b72c0de560c481884e5534b664265892725a..308729fcf124a0dface5cccebb593fd47100480d 100644 (file)
@@ -79,22 +79,31 @@ static bool isInteresting(const SCEV *S, const Instruction *I, const Loop *L,
   return false;
 }
 
-/// Return true if this loop and all loop headers that dominate it are in
-/// simplified form.
-static bool isSimplifiedLoopNest(Loop *L, const DominatorTree *DT,
-                                 const LoopInfo *LI) {
-  if (!L->isLoopSimplifyForm())
-    return false;
-
-  for (DomTreeNode *Rung = DT->getNode(L->getLoopPreheader());
+/// Return true if all loop headers that dominate this block are in simplified
+/// form.
+static bool isSimplifiedLoopNest(BasicBlock *BB, const DominatorTree *DT,
+                                 const LoopInfo *LI,
+                                 SmallPtrSet<Loop*,16> &SimpleLoopNests) {
+  Loop *NearestLoop = 0;
+  for (DomTreeNode *Rung = DT->getNode(BB);
        Rung; Rung = Rung->getIDom()) {
-    BasicBlock *BB = Rung->getBlock();
-    const Loop *DomLoop = LI->getLoopFor(BB);
-    if (DomLoop && DomLoop->getHeader() == BB) {
+    BasicBlock *DomBB = Rung->getBlock();
+    Loop *DomLoop = LI->getLoopFor(DomBB);
+    if (DomLoop && DomLoop->getHeader() == DomBB) {
+      // If the domtree walk reaches a loop with no preheader, return false.
       if (!DomLoop->isLoopSimplifyForm())
         return false;
+      // If we have already checked this loop nest, stop checking.
+      if (SimpleLoopNests.count(DomLoop))
+        break;
+      // If we have not already checked this loop nest, remember the loop
+      // header nearest to BB. The nearest loop may not contain BB.
+      if (!NearestLoop)
+        NearestLoop = DomLoop;
     }
   }
+  if (NearestLoop)
+    SimpleLoopNests.insert(NearestLoop);
   return true;
 }
 
@@ -137,15 +146,10 @@ bool IVUsers::AddUsersIfInteresting(Instruction *I,
     if (isa<PHINode>(User) && Processed.count(User))
       continue;
 
-    Loop *UserLoop = LI->getLoopFor(User->getParent());
-
     // Only consider IVUsers that are dominated by simplified loop
     // headers. Otherwise, SCEVExpander will crash.
-    if (UserLoop && !SimpleLoopNests.count(UserLoop)) {
-      if (!isSimplifiedLoopNest(UserLoop, DT, LI))
-        return false;
-      SimpleLoopNests.insert(UserLoop);
-    }
+    if (!isSimplifiedLoopNest(User->getParent(), DT, LI, SimpleLoopNests))
+      return false;
 
     // Descend recursively, but not into PHI nodes outside the current loop.
     // It's important to see the entire expression outside the loop to get
@@ -154,7 +158,7 @@ bool IVUsers::AddUsersIfInteresting(Instruction *I,
     // If User is already in Processed, we don't want to recurse into it again,
     // but do want to record a second reference in the same instruction.
     bool AddUserToIVUsers = false;
-    if (UserLoop != L) {
+    if (LI->getLoopFor(User->getParent()) != L) {
       if (isa<PHINode>(User) || Processed.count(User) ||
           !AddUsersIfInteresting(User, SimpleLoopNests)) {
         DEBUG(dbgs() << "FOUND USER in other loop: " << *User << '\n'
index 207e9d74f5d79bada1555b334d4b377777d1b2cc..01f9276dc28c257d28a6483a8eefbab73a6678fd 100644 (file)
@@ -48,3 +48,76 @@ for.inc:                                          ; preds = %lor.rhs234, %land.l
 return:                                           ; preds = %if.end677, %doshell, %if.then96
   ret void
 }
+
+define void @nopreheader2() nounwind ssp {
+entry:
+  indirectbr i8* undef, [label %while.cond, label %return]
+
+while.cond:                                       ; preds = %while.cond.backedge, %entry
+  indirectbr i8* undef, [label %while.cond.backedge, label %lor.rhs]
+
+lor.rhs:                                          ; preds = %while.cond
+  indirectbr i8* undef, [label %while.cond.backedge, label %while.end]
+
+while.cond.backedge:                              ; preds = %lor.rhs, %while.cond
+  indirectbr i8* undef, [label %while.cond]
+
+while.end:                                        ; preds = %lor.rhs
+  indirectbr i8* undef, [label %if.then18, label %return]
+
+if.then18:                                        ; preds = %while.end
+  indirectbr i8* undef, [label %if.end35, label %lor.lhs.false]
+
+lor.lhs.false:                                    ; preds = %if.then18
+  indirectbr i8* undef, [label %if.end35, label %return]
+
+if.end35:                                         ; preds = %lor.lhs.false, %if.then18
+  indirectbr i8* undef, [label %while.cond36]
+
+while.cond36:                                     ; preds = %while.body49, %if.end35
+  %0 = phi i64 [ %indvar.next13, %while.body49 ], [ 0, %if.end35 ]
+  indirectbr i8* undef, [label %while.body49, label %lor.rhs42]
+
+lor.rhs42:                                        ; preds = %while.cond36
+  indirectbr i8* undef, [label %while.body49, label %while.end52]
+
+while.body49:                                     ; preds = %lor.rhs42, %while.cond36
+  %indvar.next13 = add i64 %0, 1
+  indirectbr i8* undef, [label %while.cond36]
+
+while.end52:                                      ; preds = %lor.rhs42
+  indirectbr i8* undef, [label %land.lhs.true, label %return]
+
+land.lhs.true:                                    ; preds = %while.end52
+  indirectbr i8* undef, [label %while.cond66.preheader, label %return]
+
+while.cond66.preheader:                           ; preds = %land.lhs.true
+  indirectbr i8* undef, [label %while.cond66]
+
+while.cond66:                                     ; preds = %while.body77, %while.cond66.preheader
+  indirectbr i8* undef, [label %land.rhs, label %while.cond81.preheader]
+
+land.rhs:                                         ; preds = %while.cond66
+  indirectbr i8* undef, [label %while.body77, label %while.cond81.preheader]
+
+while.cond81.preheader:                           ; preds = %land.rhs, %while.cond66
+  %tmp45 = add i64 undef, %0
+  %tmp46 = add i64 %tmp45, undef
+  indirectbr i8* undef, [label %while.cond81]
+
+while.body77:                                     ; preds = %land.rhs
+  indirectbr i8* undef, [label %while.cond66]
+
+while.cond81:                                     ; preds = %while.body94, %while.cond81.preheader
+  %tmp25 = add i64 %tmp46, undef
+  indirectbr i8* undef, [label %while.body94, label %lor.rhs87]
+
+lor.rhs87:                                        ; preds = %while.cond81
+  indirectbr i8* undef, [label %while.body94, label %return]
+
+while.body94:                                     ; preds = %lor.rhs87, %while.cond81
+  indirectbr i8* undef, [label %while.cond81]
+
+return:                                           ; preds = %if.end216, %land.lhs.true183, %land.lhs.true, %while.end52, %lor.lhs.false, %while.end, %entry
+  ret void
+}