LSR fix: broaden the check for loop preheaders.

It's becoming clear that LoopSimplify needs to unconditionally create loop preheaders. But that is a bigger fix. For now, continuing to hack LSR.
Fixes rdar://10701050 "Cannot split an edge from an IndirectBrInst" assert.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@148288 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Transforms/Scalar/LoopStrengthReduce.cpp b/lib/Transforms/Scalar/LoopStrengthReduce.cpp
index 85f1389..baf5669 100644
--- a/lib/Transforms/Scalar/LoopStrengthReduce.cpp
+++ b/lib/Transforms/Scalar/LoopStrengthReduce.cpp
@@ -4518,11 +4518,19 @@
   if (!L->isLoopSimplifyForm())
     return;
 
-  // All outer loops must have preheaders, or SCEVExpander may not be able to
-  // materialize an AddRecExpr whose Start is an outer AddRecExpr.
-  for (const Loop *OuterLoop = L; (OuterLoop = OuterLoop->getParentLoop());) {
-    if (!OuterLoop->getLoopPreheader())
-      return;
+  // All dominating loops must have preheaders, or SCEVExpander may not be able
+  // to materialize an AddRecExpr whose Start is an outer AddRecExpr.
+  //
+  // FIXME: This is a little absurd. I think LoopSimplify should be taught
+  // to create a preheader under any circumstance.
+  for (DomTreeNode *Rung = DT.getNode(L->getLoopPreheader());
+       Rung; Rung = Rung->getIDom()) {
+    BasicBlock *BB = Rung->getBlock();
+    const Loop *DomLoop = LI.getLoopFor(BB);
+    if (DomLoop && DomLoop->getHeader() == BB) {
+      if (!DomLoop->getLoopPreheader())
+        return;
+    }
   }
   // If there's no interesting work to be done, bail early.
   if (IU.empty()) return;