Add support for the switch instruction to the CWriter
[oota-llvm.git] / lib / Analysis / LoopInfo.cpp
index 479cbf43d1747cf63465044876258a6239e37782..d7d8a5b9e56d1744f87f6936143c0421b8dde37a 100644 (file)
@@ -24,19 +24,55 @@ bool Loop::contains(const BasicBlock *BB) const {
   return find(Blocks.begin(), Blocks.end(), BB) != Blocks.end();
 }
 
+bool Loop::isLoopExit(const BasicBlock *BB) const {
+  for (BasicBlock::succ_const_iterator SI = succ_begin(BB), SE = succ_end(BB);
+       SI != SE; ++SI) {
+    if (!contains(*SI))
+      return true;
+  }
+  return false;
+}
+
+unsigned Loop::getNumBackEdges() const {
+  unsigned NumBackEdges = 0;
+  BasicBlock *H = getHeader();
+
+  for (std::vector<BasicBlock*>::const_iterator I = Blocks.begin(),
+         E = Blocks.end(); I != E; ++I)
+    for (BasicBlock::succ_iterator SI = succ_begin(*I), SE = succ_end(*I);
+         SI != SE; ++SI)
+      if (*SI == H)
+       ++NumBackEdges;
+  
+  return NumBackEdges;
+}
+
 void Loop::print(std::ostream &OS) const {
   OS << std::string(getLoopDepth()*2, ' ') << "Loop Containing: ";
 
   for (unsigned i = 0; i < getBlocks().size(); ++i) {
     if (i) OS << ",";
-    WriteAsOperand(OS, (const Value*)getBlocks()[i]);
+    WriteAsOperand(OS, getBlocks()[i], false);
   }
+  if (!ExitBlocks.empty()) {
+    OS << "\tExitBlocks: ";
+    for (unsigned i = 0; i < getExitBlocks().size(); ++i) {
+      if (i) OS << ",";
+      WriteAsOperand(OS, getExitBlocks()[i], false);
+    }
+  }
+
   OS << "\n";
 
-  std::copy(getSubLoops().begin(), getSubLoops().end(),
-            std::ostream_iterator<const Loop*>(OS, "\n"));
+  for (unsigned i = 0, e = getSubLoops().size(); i != e; ++i)
+    getSubLoops()[i]->print(OS);
 }
 
+void Loop::dump() const {
+  print(std::cerr);
+}
+
+
 //===----------------------------------------------------------------------===//
 // LoopInfo implementation
 //
@@ -78,6 +114,12 @@ void LoopInfo::getAnalysisUsage(AnalysisUsage &AU) const {
 void LoopInfo::print(std::ostream &OS) const {
   for (unsigned i = 0; i < TopLevelLoops.size(); ++i)
     TopLevelLoops[i]->print(OS);
+#if 0
+  for (std::map<BasicBlock*, Loop*>::const_iterator I = BBMap.begin(),
+         E = BBMap.end(); I != E; ++I)
+    OS << "BB '" << I->first->getName() << "' level = "
+       << I->second->LoopDepth << "\n";
+#endif
 }
 
 Loop *LoopInfo::ConsiderForLoop(BasicBlock *BB, const DominatorSet &DS) {
@@ -101,31 +143,41 @@ Loop *LoopInfo::ConsiderForLoop(BasicBlock *BB, const DominatorSet &DS) {
     BasicBlock *X = TodoStack.back();
     TodoStack.pop_back();
 
-    if (!L->contains(X)) {                  // As of yet unprocessed??
+    if (!L->contains(X)) {         // As of yet unprocessed??
       L->Blocks.push_back(X);
-
+      
       // Add all of the predecessors of X to the end of the work stack...
       TodoStack.insert(TodoStack.end(), pred_begin(X), pred_end(X));
     }
   }
 
-  // Add the basic blocks that comprise this loop to the BBMap so that this
-  // loop can be found for them.  Also check subsidary basic blocks to see if
-  // they start subloops of their own.
-  //
-  for (std::vector<BasicBlock*>::reverse_iterator I = L->Blocks.rbegin(),
-        E = L->Blocks.rend(); I != E; ++I) {
-
-    // Check to see if this block starts a new loop
+  // If there are any loops nested within this loop, create them now!
+  for (std::vector<BasicBlock*>::iterator I = L->Blocks.begin(),
+        E = L->Blocks.end(); I != E; ++I)
     if (Loop *NewLoop = ConsiderForLoop(*I, DS)) {
       L->SubLoops.push_back(NewLoop);
       NewLoop->ParentLoop = L;
     }
-  
-    if (BBMap.find(*I) == BBMap.end())
-      BBMap.insert(std::make_pair(*I, L));
+
+
+  // Add the basic blocks that comprise this loop to the BBMap so that this
+  // loop can be found for them.
+  //
+  for (std::vector<BasicBlock*>::iterator I = L->Blocks.begin(),
+        E = L->Blocks.end(); I != E; ++I) {
+    std::map<BasicBlock*, Loop*>::iterator BBMI = BBMap.lower_bound(*I);
+    if (BBMI == BBMap.end() || BBMI->first != *I)  // Not in map yet...
+      BBMap.insert(BBMI, std::make_pair(*I, L));   // Must be at this level
   }
 
+  // Now that we know all of the blocks that make up this loop, see if there are
+  // any branches to outside of the loop... building the ExitBlocks list.
+  for (std::vector<BasicBlock*>::iterator BI = L->Blocks.begin(),
+         BE = L->Blocks.end(); BI != BE; ++BI)
+    for (succ_iterator I = succ_begin(*BI), E = succ_end(*BI); I != E; ++I)
+      if (!L->contains(*I))               // Not in current loop?
+        L->ExitBlocks.push_back(*I);      // It must be an exit block...
+
   return L;
 }
 
@@ -147,10 +199,18 @@ BasicBlock *Loop::getLoopPreheader() const {
   BasicBlock *Header = getHeader();
   for (pred_iterator PI = pred_begin(Header), PE = pred_end(Header);
        PI != PE; ++PI)
-    if (!contains(*PI)) { // If the block is not in the loop...
-      if (Out) return 0;  // Multiple predecessors outside the loop
+    if (!contains(*PI)) {     // If the block is not in the loop...
+      if (Out && Out != *PI)
+        return 0;             // Multiple predecessors outside the loop
       Out = *PI;
     }
+  
+  // Make sure there is only one exit out of the preheader...
+  succ_iterator SI = succ_begin(Out);
+  ++SI;
+  if (SI != succ_end(Out))
+    return 0;  // Multiple exits from the block, must not be a preheader.
+
 
   // If there is exactly one preheader, return it.  If there was zero, then Out
   // is still null.
@@ -178,3 +238,19 @@ void Loop::addBasicBlockToLoop(BasicBlock *NewBB, LoopInfo &LI) {
     L = L->getParentLoop();
   }
 }
+
+/// changeExitBlock - This method is used to update loop information.  All
+/// instances of the specified Old basic block are removed from the exit list
+/// and replaced with New.
+///
+void Loop::changeExitBlock(BasicBlock *Old, BasicBlock *New) {
+  assert(Old != New && "Cannot changeExitBlock to the same thing!");
+  assert(Old && New && "Cannot changeExitBlock to or from a null node!");
+  assert(hasExitBlock(Old) && "Old exit block not found!");
+  std::vector<BasicBlock*>::iterator
+    I = std::find(ExitBlocks.begin(), ExitBlocks.end(), Old);
+  while (I != ExitBlocks.end()) {
+    *I = New;
+    I = std::find(I+1, ExitBlocks.end(), Old);
+  }
+}