[SCEV] Fix getLoopBackedgeTakenCounts
authorSanjoy Das <sanjoy@playingwithpointers.com>
Wed, 23 Dec 2015 17:48:14 +0000 (17:48 +0000)
committerSanjoy Das <sanjoy@playingwithpointers.com>
Wed, 23 Dec 2015 17:48:14 +0000 (17:48 +0000)
The way `getLoopBackedgeTakenCounts` is written right now isn't
correct. It will try to compute and store the BE counts of a Loop
 #{child loop} number of times (which may be zero).

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

lib/Analysis/ScalarEvolution.cpp

index 1ca72accc42e069338c730290b412f74cde3e212..10994d9ccfde9ced2fb426a0d60c1c94fcecd2f2 100644 (file)
@@ -9439,23 +9439,22 @@ static void replaceSubString(std::string &Str, StringRef From, StringRef To) {
 /// getLoopBackedgeTakenCounts - Helper method for verifyAnalysis.
 static void
 getLoopBackedgeTakenCounts(Loop *L, VerifyMap &Map, ScalarEvolution &SE) {
-  for (Loop::reverse_iterator I = L->rbegin(), E = L->rend(); I != E; ++I) {
-    getLoopBackedgeTakenCounts(*I, Map, SE); // recurse.
-
-    std::string &S = Map[L];
-    if (S.empty()) {
-      raw_string_ostream OS(S);
-      SE.getBackedgeTakenCount(L)->print(OS);
-
-      // false and 0 are semantically equivalent. This can happen in dead loops.
-      replaceSubString(OS.str(), "false", "0");
-      // Remove wrap flags, their use in SCEV is highly fragile.
-      // FIXME: Remove this when SCEV gets smarter about them.
-      replaceSubString(OS.str(), "<nw>", "");
-      replaceSubString(OS.str(), "<nsw>", "");
-      replaceSubString(OS.str(), "<nuw>", "");
-    }
-  }
+  std::string &S = Map[L];
+  if (S.empty()) {
+    raw_string_ostream OS(S);
+    SE.getBackedgeTakenCount(L)->print(OS);
+
+    // false and 0 are semantically equivalent. This can happen in dead loops.
+    replaceSubString(OS.str(), "false", "0");
+    // Remove wrap flags, their use in SCEV is highly fragile.
+    // FIXME: Remove this when SCEV gets smarter about them.
+    replaceSubString(OS.str(), "<nw>", "");
+    replaceSubString(OS.str(), "<nsw>", "");
+    replaceSubString(OS.str(), "<nuw>", "");
+  }
+
+  for (auto *L : reverse(*L))
+    getLoopBackedgeTakenCounts(L, Map, SE); // recurse.
 }
 
 void ScalarEvolution::verify() const {