Improve encapsulation in the Loop and LoopInfo classes by eliminating the
authorChris Lattner <sabre@nondot.org>
Thu, 8 Jan 2004 00:09:44 +0000 (00:09 +0000)
committerChris Lattner <sabre@nondot.org>
Thu, 8 Jan 2004 00:09:44 +0000 (00:09 +0000)
getSubLoops/getTopLevelLoops methods, replacing them with iterator-based
accessors.

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

include/llvm/Analysis/LoopInfo.h
lib/Analysis/LoopInfo.cpp
lib/Transforms/Scalar/IndVarSimplify.cpp
lib/Transforms/Scalar/LICM.cpp
lib/Transforms/Utils/LoopSimplify.cpp

index 1458b3bd5f68de57166063fbb83e0c6ee3ad4334..52339f44ea5dd23a8bfc408a0e348e8065d30bf7 100644 (file)
@@ -56,9 +56,11 @@ public:
   /// contains - Return true of the specified basic block is in this loop
   bool contains(const BasicBlock *BB) const;
 
-  /// getSubLoops - Return the loops contained entirely within this loop
+  /// iterator/begin/end - Return the loops contained entirely within this loop.
   ///
-  const std::vector<Loop*> &getSubLoops() const { return SubLoops; }
+  typedef std::vector<Loop*>::const_iterator iterator;
+  iterator begin() const { return SubLoops.begin(); }
+  iterator end() const { return SubLoops.end(); }
 
   /// getBlocks - Get a list of the basic blocks which make up this loop.
   ///
@@ -147,7 +149,12 @@ class LoopInfo : public FunctionPass {
 public:
   ~LoopInfo() { releaseMemory(); }
 
-  const std::vector<Loop*> &getTopLevelLoops() const { return TopLevelLoops; }
+  /// iterator/begin/end - The interface to the top-level loops in the current
+  /// function.
+  ///
+  typedef std::vector<Loop*>::const_iterator iterator;
+  iterator begin() const { return TopLevelLoops.begin(); }
+  iterator end() const { return TopLevelLoops.end(); }
 
   /// getLoopFor - Return the inner most loop that BB lives in.  If a basic
   /// block is in no loop (for example the entry node), null is returned.
@@ -206,10 +213,10 @@ template <> struct GraphTraits<const Loop*> {
 
   static NodeType *getEntryNode(const Loop *L) { return L; }
   static inline ChildIteratorType child_begin(NodeType *N) { 
-    return N->getSubLoops().begin();
+    return N->begin();
   }
   static inline ChildIteratorType child_end(NodeType *N) { 
-    return N->getSubLoops().end();
+    return N->end();
   }
 };
 
@@ -219,10 +226,10 @@ template <> struct GraphTraits<Loop*> {
 
   static NodeType *getEntryNode(Loop *L) { return L; }
   static inline ChildIteratorType child_begin(NodeType *N) { 
-    return N->getSubLoops().begin();
+    return N->begin();
   }
   static inline ChildIteratorType child_end(NodeType *N) { 
-    return N->getSubLoops().end();
+    return N->end();
   }
 };
 
index 68e7d2f845daddbde6e1110f0dba4128e1f0ee37..41c4b555bd14a88c930aeca7d3dc23e390d1953c 100644 (file)
@@ -72,8 +72,8 @@ void Loop::print(std::ostream &OS, unsigned Depth) const {
 
   OS << "\n";
 
-  for (unsigned i = 0, e = getSubLoops().size(); i != e; ++i)
-    getSubLoops()[i]->print(OS, Depth+2);
+  for (iterator I = begin(), E = end(); I != E; ++I)
+    (*I)->print(OS, Depth+2);
 }
 
 void Loop::dump() const {
index ed02e3a00bb9fd26d72eb92f843de3295b0f0699..f631b69f0a08d35688fd0e5382b346a305e6fd94 100644 (file)
@@ -43,8 +43,8 @@ namespace {
       Changed = false;
 
       // Induction Variables live in the header nodes of loops
-      for (unsigned i = 0, e = Loops->getTopLevelLoops().size(); i != e; ++i)
-        runOnLoop(Loops->getTopLevelLoops()[i]);
+      for (LoopInfo::iterator I = Loops->begin(), E = Loops->end(); I != E; ++I)
+        runOnLoop(*I);
       return Changed;
     }
 
@@ -77,8 +77,8 @@ Pass *llvm::createIndVarSimplifyPass() {
 
 void IndVarSimplify::runOnLoop(Loop *Loop) {
   // Transform all subloops before this loop...
-  for (unsigned i = 0, e = Loop->getSubLoops().size(); i != e; ++i)
-    runOnLoop(Loop->getSubLoops()[i]);
+  for (LoopInfo::iterator I = Loop->begin(), E = Loop->end(); I != E; ++I)
+    runOnLoop(*I);
 
   // Get the header node for this loop.  All of the phi nodes that could be
   // induction variables must live in this basic block.
index c26fb6bef4f275674de64c1a12f27f7b4ab02281..190d407764286cbc6874fac7d6cb33ee7be9e4c7 100644 (file)
@@ -113,8 +113,8 @@ namespace {
     ///
     bool inSubLoop(BasicBlock *BB) {
       assert(CurLoop->contains(BB) && "Only valid if BB is IN the loop");
-      for (unsigned i = 0, e = CurLoop->getSubLoops().size(); i != e; ++i)
-        if (CurLoop->getSubLoops()[i]->contains(BB))
+      for (Loop::iterator I = CurLoop->begin(), E = CurLoop->end(); I != E; ++I)
+        if ((*I)->contains(BB))
           return true;  // A subloop actually contains this block!
       return false;
     }
@@ -221,9 +221,7 @@ bool LICM::runOnFunction(Function &) {
   DT = &getAnalysis<DominatorTree>();
 
   // Hoist expressions out of all of the top-level loops.
-  const std::vector<Loop*> &TopLevelLoops = LI->getTopLevelLoops();
-  for (std::vector<Loop*>::const_iterator I = TopLevelLoops.begin(),
-         E = TopLevelLoops.end(); I != E; ++I) {
+  for (LoopInfo::iterator I = LI->begin(), E = LI->end(); I != E; ++I) {
     AliasSetTracker AST(*AA);
     visitLoop(*I, AST);
   }
@@ -235,8 +233,7 @@ bool LICM::runOnFunction(Function &) {
 ///
 void LICM::visitLoop(Loop *L, AliasSetTracker &AST) {
   // Recurse through all subloops before we process this loop...
-  for (std::vector<Loop*>::const_iterator I = L->getSubLoops().begin(),
-         E = L->getSubLoops().end(); I != E; ++I) {
+  for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I) {
     AliasSetTracker SubAST(*AA);
     visitLoop(*I, SubAST);
 
index 9beae4fd075a57f54bede2191f2623fc6b2a8f04..45f31aa4a897d9393b0983de6921f27661ff3efc 100644 (file)
@@ -91,8 +91,8 @@ bool LoopSimplify::runOnFunction(Function &F) {
   bool Changed = false;
   LoopInfo &LI = getAnalysis<LoopInfo>();
 
-  for (unsigned i = 0, e = LI.getTopLevelLoops().size(); i != e; ++i)
-    Changed |= ProcessLoop(LI.getTopLevelLoops()[i]);
+  for (LoopInfo::iterator I = LI.begin(), E = LI.end(); I != E; ++I)
+    Changed |= ProcessLoop(*I);
 
   return Changed;
 }
@@ -136,9 +136,8 @@ bool LoopSimplify::ProcessLoop(Loop *L) {
     Changed = true;
   }
 
-  const std::vector<Loop*> &SubLoops = L->getSubLoops();
-  for (unsigned i = 0, e = SubLoops.size(); i != e; ++i)
-    Changed |= ProcessLoop(SubLoops[i]);
+  for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I)
+    Changed |= ProcessLoop(*I);
   return Changed;
 }
 
@@ -227,9 +226,8 @@ BasicBlock *LoopSimplify::SplitBlockPredecessors(BasicBlock *BB,
 static void ChangeExitBlock(Loop *L, BasicBlock *OldExit, BasicBlock *NewExit) {
   if (L->hasExitBlock(OldExit)) {
     L->changeExitBlock(OldExit, NewExit);
-    const std::vector<Loop*> &SubLoops = L->getSubLoops();
-    for (unsigned i = 0, e = SubLoops.size(); i != e; ++i)
-      ChangeExitBlock(SubLoops[i], OldExit, NewExit);
+    for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I)
+      ChangeExitBlock(*I, OldExit, NewExit);
   }
 }
 
@@ -266,16 +264,19 @@ void LoopSimplify::InsertPreheaderForLoop(Loop *L) {
   // is a sibling loop, ie, one with the same parent loop, or one if it's
   // children.
   //
-  const std::vector<Loop*> *ParentSubLoops;
-  if (Loop *Parent = L->getParentLoop())
-    ParentSubLoops = &Parent->getSubLoops();
-  else       // Must check top-level loops...
-    ParentSubLoops = &getAnalysis<LoopInfo>().getTopLevelLoops();
+  LoopInfo::iterator ParentLoops, ParentLoopsE;
+  if (Loop *Parent = L->getParentLoop()) {
+    ParentLoops = Parent->begin();
+    ParentLoopsE = Parent->end();
+  } else {      // Must check top-level loops...
+    ParentLoops = getAnalysis<LoopInfo>().begin();
+    ParentLoopsE = getAnalysis<LoopInfo>().end();
+  }
 
   // Loop over all sibling loops, performing the substitution (recursively to
   // include child loops)...
-  for (unsigned i = 0, e = ParentSubLoops->size(); i != e; ++i)
-    ChangeExitBlock((*ParentSubLoops)[i], Header, NewBB);
+  for (; ParentLoops != ParentLoopsE; ++ParentLoops)
+    ChangeExitBlock(*ParentLoops, Header, NewBB);
   
   DominatorSet &DS = getAnalysis<DominatorSet>();  // Update dominator info
   {