[FIX] Do not create a SCoP in the presence of infinite loops

  If a loop has no exiting blocks the region covering we use during
  schedule genertion might not cover that loop properly. For now we bail
  out as we would not optimize these loops anyway.

llvm-svn: 265260
diff --git a/polly/lib/Analysis/ScopInfo.cpp b/polly/lib/Analysis/ScopInfo.cpp
index b61d9fa..f3271c2 100644
--- a/polly/lib/Analysis/ScopInfo.cpp
+++ b/polly/lib/Analysis/ScopInfo.cpp
@@ -3618,8 +3618,15 @@
   Loop *L = getLoopSurroundingRegion(getRegion(), LI);
   LoopStackTy LoopStack({LoopStackElementTy(L, nullptr, 0)});
   buildSchedule(getRegion().getNode(), LoopStack, SD, LI);
-  assert(LoopStack.size() == 1 && LoopStack.back().L == L);
-  Schedule = LoopStack[0].Schedule;
+  assert(!LoopStack.empty());
+  if (LoopStack.size() == 1 && LoopStack.back().L == L) {
+    Schedule = LoopStack[0].Schedule;
+  } else {
+    // If something went wrong we have to cleanup.
+    assert(!hasFeasibleRuntimeContext());
+    while (!LoopStack.empty())
+      isl_schedule_free(LoopStack.pop_back_val().Schedule);
+  }
 }
 
 /// To generate a schedule for the elements in a Region we traverse the Region
@@ -3662,9 +3669,22 @@
   // iterator. If it is set we have to explore the next sub-region/block from
   // the iterator (if any) to guarantee progress. If it is not set we first try
   // the next queued sub-region/blocks.
+  unsigned RemainingWork = WorkList.size() + DelayList.size() + 1;
   while (!WorkList.empty() || !DelayList.empty()) {
+    Loop *LastLoop = LoopStack.back().L;
     RegionNode *RN;
 
+    // FIXME: We will bail out if we cannot make progress. So far that is only
+    //        known to happen in the presence of infinite loops without an exit
+    //        edge. For such cases there is no region covering only the loop and
+    //        our reasoning fails.
+    if (WorkList.size() + DelayList.size() >= RemainingWork) {
+      invalidate(INFINITELOOP, R->getEntry()->getTerminator()->getDebugLoc());
+      LastLoop = nullptr;
+    }
+
+    RemainingWork = WorkList.size() + DelayList.size();
+
     if ((LastRNWaiting && !WorkList.empty()) || DelayList.size() == 0) {
       RN = WorkList.front();
       WorkList.pop_front();
@@ -3678,9 +3698,8 @@
     if (!getRegion().contains(L))
       L = OuterScopLoop;
 
-    Loop *LastLoop = LoopStack.back().L;
     if (LastLoop != L) {
-      if (!LastLoop->contains(L)) {
+      if (LastLoop && !LastLoop->contains(L)) {
         LastRNWaiting = true;
         DelayList.push_back(RN);
         continue;