Miscellaneous cleanups:
  * Convert post to pre-increment for for loops
  * Use generic programming more
  * Use new Value::cast* instructions
  * Use new Module, Method, & BasicBlock forwarding methods
  * Use new facilities in STLExtras.h
  * Use new Instruction::isPHINode() method


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@96 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Transforms/IPO/InlineSimple.cpp b/lib/Transforms/IPO/InlineSimple.cpp
index 0a191a5..e5bc171 100644
--- a/lib/Transforms/IPO/InlineSimple.cpp
+++ b/lib/Transforms/IPO/InlineSimple.cpp
@@ -36,9 +36,9 @@
 static inline void RemapInstruction(Instruction *I, 
 				    map<const Value *, Value*> &ValueMap) {
 
-  for (unsigned op = 0; const Value *Op = I->getOperand(op); op++) {
+  for (unsigned op = 0; const Value *Op = I->getOperand(op); ++op) {
     Value *V = ValueMap[Op];
-    if (!V && Op->getValueType() == Value::MethodVal) 
+    if (!V && Op->isMethod()) 
       continue;  // Methods don't get relocated
 
     if (!V) {
@@ -60,7 +60,7 @@
 // exists in the instruction stream.  Similiarly this will inline a recursive
 // method by one level.
 //
-bool InlineMethod(BasicBlock::InstListType::iterator CIIt) {
+bool InlineMethod(BasicBlock::iterator CIIt) {
   assert((*CIIt)->getInstType() == Instruction::Call && 
 	 "InlineMethod only works on CallInst nodes!");
   assert((*CIIt)->getParent() && "Instruction not embedded in basic block!");
@@ -124,9 +124,8 @@
   // Loop over all of the basic blocks in the method, inlining them as 
   // appropriate.  Keep track of the first basic block of the method...
   //
-  for (Method::BasicBlocksType::const_iterator BI = 
-	 CalledMeth->getBasicBlocks().begin(); 
-       BI != CalledMeth->getBasicBlocks().end(); BI++) {
+  for (Method::const_iterator BI = CalledMeth->begin(); 
+       BI != CalledMeth->end(); ++BI) {
     const BasicBlock *BB = *BI;
     assert(BB->getTerminator() && "BasicBlock doesn't have terminator!?!?");
     
@@ -143,8 +142,8 @@
    
     // Loop over all instructions copying them over...
     Instruction *NewInst;
-    for (BasicBlock::InstListType::const_iterator II = BB->getInstList().begin();
-	 II != (BB->getInstList().end()-1); II++) {
+    for (BasicBlock::const_iterator II = BB->begin();
+	 II != (BB->end()-1); ++II) {
       IBB->getInstList().push_back((NewInst = (*II)->clone()));
       ValueMap[*II] = NewInst;                  // Add instruction map to value.
     }
@@ -193,16 +192,14 @@
   // Loop over all of the instructions in the method, fixing up operand 
   // references as we go.  This uses ValueMap to do all the hard work.
   //
-  for (Method::BasicBlocksType::const_iterator BI = 
-	 CalledMeth->getBasicBlocks().begin(); 
-       BI != CalledMeth->getBasicBlocks().end(); BI++) {
+  for (Method::const_iterator BI = CalledMeth->begin(); 
+       BI != CalledMeth->end(); ++BI) {
     const BasicBlock *BB = *BI;
     BasicBlock *NBB = (BasicBlock*)ValueMap[BB];
 
     // Loop over all instructions, fixing each one as we find it...
     //
-    for (BasicBlock::InstListType::iterator II = NBB->getInstList().begin();
-	 II != NBB->getInstList().end(); II++)
+    for (BasicBlock::iterator II = NBB->begin(); II != NBB->end(); II++)
       RemapInstruction(*II, ValueMap);
   }
 
@@ -214,7 +211,7 @@
   TerminatorInst *Br = OrigBB->getTerminator();
   assert(Br && Br->getInstType() == Instruction::Br && 
 	 "splitBasicBlock broken!");
-  Br->setOperand(0, ValueMap[CalledMeth->getBasicBlocks().front()]);
+  Br->setOperand(0, ValueMap[CalledMeth->front()]);
 
   // Since we are now done with the CallInst, we can finally delete it.
   delete CI;
@@ -225,10 +222,9 @@
   assert(CI->getParent() && "CallInst not embeded in BasicBlock!");
   BasicBlock *PBB = CI->getParent();
 
-  BasicBlock::InstListType::iterator CallIt = find(PBB->getInstList().begin(),
-						   PBB->getInstList().end(),
-						   CI);
-  assert(CallIt != PBB->getInstList().end() && 
+  BasicBlock::iterator CallIt = find(PBB->begin(), PBB->end(), CI);
+
+  assert(CallIt != PBB->end() && 
 	 "CallInst has parent that doesn't contain CallInst?!?");
   return InlineMethod(CallIt);
 }
@@ -241,10 +237,10 @@
   if (CI->getParent()->getParent() == M) return false;
 
   // Don't inline something too big.  This is a really crappy heuristic
-  if (M->getBasicBlocks().size() > 3) return false;
+  if (M->size() > 3) return false;
 
   // Don't inline into something too big. This is a **really** crappy heuristic
-  if (CI->getParent()->getParent()->getBasicBlocks().size() > 10) return false;
+  if (CI->getParent()->getParent()->size() > 10) return false;
 
   // Go ahead and try just about anything else.
   return true;
@@ -252,8 +248,7 @@
 
 
 static inline bool DoMethodInlining(BasicBlock *BB) {
-  for (BasicBlock::InstListType::iterator I = BB->getInstList().begin();
-       I != BB->getInstList().end(); I++) {
+  for (BasicBlock::iterator I = BB->begin(); I != BB->end(); ++I) {
     if ((*I)->getInstType() == Instruction::Call) {
       // Check to see if we should inline this method
       CallInst *CI = (CallInst*)*I;
@@ -266,15 +261,14 @@
 }
 
 bool DoMethodInlining(Method *M) {
-  Method::BasicBlocksType &BBs = M->getBasicBlocks();
   bool Changed = false;
 
   // Loop through now and inline instructions a basic block at a time...
-  for (Method::BasicBlocksType::iterator I = BBs.begin(); I != BBs.end(); )
+  for (Method::iterator I = M->begin(); I != M->end(); )
     if (DoMethodInlining(*I)) {
       Changed = true;
       // Iterator is now invalidated by new basic blocks inserted
-      I = BBs.begin();
+      I = M->begin();
     } else {
       ++I;
     }
diff --git a/lib/Transforms/Scalar/DCE.cpp b/lib/Transforms/Scalar/DCE.cpp
index 14ee8d5..5bc3c5b 100644
--- a/lib/Transforms/Scalar/DCE.cpp
+++ b/lib/Transforms/Scalar/DCE.cpp
@@ -61,7 +61,7 @@
       delete Vals.remove(DI);
       Changed = true;
     } else {
-      DI++;
+      ++DI;
     }
   }
   return Changed;
@@ -79,8 +79,8 @@
   if (PI == pred_end(BB) || ++PI != pred_end(BB)) 
     return false;   // More than one predecessor...
 
-  Instruction *I = BB->getInstList().front();
-  if (I->getInstType() != Instruction::PHINode) return false;  // No PHI nodes
+  Instruction *I = BB->front();
+  if (!I->isPHINode()) return false;  // No PHI nodes
 
   //cerr << "Killing PHIs from " << BB;
   //cerr << "Pred #0 = " << *pred_begin(BB);
@@ -93,10 +93,10 @@
     Value *V = PN->getOperand(0);
 
     PN->replaceAllUsesWith(V);      // Replace PHI node with its single value.
-    delete BB->getInstList().remove(BB->getInstList().begin());
+    delete BB->getInstList().remove(BB->begin());
 
-    I = BB->getInstList().front();
-  } while (I->getInstType() == Instruction::PHINode);
+    I = BB->front();
+  } while (I->isPHINode());
 	
   return true;  // Yes, we nuked at least one phi node
 }
@@ -154,8 +154,8 @@
   
   // Okay, now we know that we need to remove predecessor #pred_idx from all
   // PHI nodes.  Iterate over each PHI node fixing them up
-  BasicBlock::InstListType::iterator II(BB->getInstList().begin());
-  for (; (*II)->getInstType() == Instruction::PHINode; ++II) {
+  BasicBlock::iterator II(BB->begin());
+  for (; (*II)->isPHINode(); ++II) {
     PHINode *PN = (PHINode*)*II;
     PN->removeIncomingValue(BB);
 
@@ -177,25 +177,36 @@
 // Assumption: BB is the single predecessor of Succ.
 //
 static void PropogatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ) {
-  assert(BB && Succ && *pred_begin(Succ) == BB && "BB is only pred of Succ" &&
-	 ++pred_begin(Succ) == pred_end(Succ));
+  assert(Succ->front()->isPHINode() && "Only works on PHId BBs!");
 
   // If there is more than one predecessor, and there are PHI nodes in
   // the successor, then we need to add incoming edges for the PHI nodes
-  pred_iterator PI(pred_begin(BB));
-  for (; PI != pred_end(BB); ++PI) {
-    // TODO:
-  }
+  //
+  const vector<BasicBlock*> BBPreds(pred_begin(BB), pred_end(BB));
+
+  BasicBlock::iterator I = Succ->begin();
+  do {                     // Loop over all of the PHI nodes in the successor BB
+    PHINode *PN = (PHINode*)*I;
+    Value *OldVal = PN->removeIncomingValue(BB);
+    assert(OldVal && "No entry in PHI for Pred BB!");
+
+    for (vector<BasicBlock*>::const_iterator PredI = BBPreds.begin(), 
+	   End = BBPreds.end(); PredI != End; ++PredI) {
+      // Add an incoming value for each of the new incoming values...
+      PN->addIncoming(OldVal, *PredI);
+    }
+
+    ++I;
+  } while ((*I)->isPHINode());
 }
 
 static bool DoDCEPass(Method *M) {
-  Method::BasicBlocksType &BBs = M->getBasicBlocks();
-  Method::BasicBlocksType::iterator BBIt, BBEnd = BBs.end();
-  if (BBs.begin() == BBEnd) return false;  // Nothing to do
+  Method::iterator BBIt, BBEnd = M->end();
+  if (M->begin() == BBEnd) return false;  // Nothing to do
   bool Changed = false;
 
   // Loop through now and remove instructions that have no uses...
-  for (BBIt = BBs.begin(); BBIt != BBEnd; BBIt++) {
+  for (BBIt = M->begin(); BBIt != BBEnd; ++BBIt) {
     Changed |= RemoveUnusedDefs((*BBIt)->getInstList(), BasicBlockDCE());
     Changed |= RemoveSingularPHIs(*BBIt);
   }
@@ -203,11 +214,11 @@
   // Loop over all of the basic blocks (except the first one) and remove them
   // if they are unneeded...
   //
-  for (BBIt = BBs.begin(), ++BBIt; BBIt != BBs.end(); ++BBIt) {
+  for (BBIt = M->begin(), ++BBIt; BBIt != M->end(); ++BBIt) {
     BasicBlock *BB = *BBIt;
     assert(BB->getTerminator() && "Degenerate basic block encountered!");
 
-#if 0
+#if 0  // This is know to basically work?
     // Remove basic blocks that have no predecessors... which are unreachable.
     if (pred_begin(BB) == pred_end(BB) &&
 	!BB->hasConstantPoolReferences() && 0) {
@@ -215,43 +226,46 @@
 
       // Loop through all of our successors and make sure they know that one
       // of their predecessors is going away.
-      for (succ_iterator SI = succ_begin(BB), EI = succ_end(BB); SI != EI; ++SI)
-	RemovePredecessorFromBlock(*SI, BB);
+      for_each(succ_begin(BB), succ_end(BB),
+	       bind_2nd(RemovePredecessorFromBlock, BB));
 
-      while (!BB->getInstList().empty()) {
-	Instruction *I = BB->getInstList().front();
+      while (!BB->empty()) {
+	Instruction *I = BB->front();
 	// If this instruction is used, replace uses with an arbitrary
 	// constant value.  Because control flow can't get here, we don't care
 	// what we replace the value with.
 	if (!I->use_empty()) ReplaceUsesWithConstant(I);
 
 	// Remove the instruction from the basic block
-	delete BB->getInstList().remove(BB->getInstList().begin());
+	delete BB->getInstList().remove(BB->begin());
       }
-      delete BBs.remove(BBIt);
+      delete M->getBasicBlocks().remove(BBIt);
       --BBIt;  // remove puts use on the next block, we want the previous one
       Changed = true;
       continue;
     } 
+#endif
 
+#if 0  // This has problems
     // Check to see if this block has no instructions and only a single 
     // successor.  If so, replace block references with successor.
     succ_iterator SI(succ_begin(BB));
     if (SI != succ_end(BB) && ++SI == succ_end(BB)) {  // One succ?
-      Instruction *I = BB->getInstList().front();
+      Instruction *I = BB->front();
       if (I->isTerminator()) {   // Terminator is the only instruction!
+	BasicBlock *Succ = *succ_begin(BB); // There is exactly one successor
+	cerr << "Killing Trivial BB: \n" << BB;
 
-	if (Succ->getInstList().front()->getInstType() == Instruction::PHINode){
-	  // Add entries to the PHI nodes so that the PHI nodes have the right
-	  // number of entries...
+	if (Succ->front()->isPHINode()) {
+	  // If our successor has PHI nodes, then we need to update them to
+	  // include entries for BB's predecessors, not for BB itself.
+	  //
 	  PropogatePredecessorsForPHIs(BB, Succ);
 	}
 
-	BasicBlock *Succ = *succ_begin(BB); // There is exactly one successor
 	BB->replaceAllUsesWith(Succ);
-	cerr << "Killing Trivial BB: \n" << BB;
 
-	BB = BBs.remove(BBIt);
+	BB = M->getBasicBlocks().remove(BBIt);
 	--BBIt; // remove puts use on the next block, we want the previous one
 	
 	if (BB->hasName() && !Succ->hasName())  // Transfer name if we can
@@ -280,21 +294,21 @@
 	//cerr << "Merging: " << BB << "into: " << Pred;
 
 	// Delete the unconditianal branch from the predecessor...
-	BasicBlock::InstListType::iterator DI = Pred->getInstList().end();
+	BasicBlock::iterator DI = Pred->end();
 	assert(Pred->getTerminator() && 
 	       "Degenerate basic block encountered!");  // Empty bb???      
 	delete Pred->getInstList().remove(--DI);        // Destroy uncond branch
 	
 	// Move all definitions in the succecessor to the predecessor...
-	while (!BB->getInstList().empty()) {
-	  DI = BB->getInstList().begin();
+	while (!BB->empty()) {
+	  DI = BB->begin();
 	  Instruction *Def = BB->getInstList().remove(DI); // Remove from front
 	  Pred->getInstList().push_back(Def);              // Add to end...
 	}
 
 	// Remove basic block from the method... and advance iterator to the
 	// next valid block...
-	BB = BBs.remove(BBIt);
+	BB = M->getBasicBlocks().remove(BBIt);
 	--BBIt;  // remove puts us on the NEXT bb.  We want the prev BB
 	Changed = true;
 
diff --git a/lib/Transforms/Scalar/SymbolStripping.cpp b/lib/Transforms/Scalar/SymbolStripping.cpp
index af5f18f..33b6005 100644
--- a/lib/Transforms/Scalar/SymbolStripping.cpp
+++ b/lib/Transforms/Scalar/SymbolStripping.cpp
@@ -23,7 +23,7 @@
   if (SymTab == 0) return false;    // No symbol table?  No problem.
   bool RemovedSymbol = false;
 
-  for (SymbolTable::iterator I = SymTab->begin(); I != SymTab->end(); I++) {
+  for (SymbolTable::iterator I = SymTab->begin(); I != SymTab->end(); ++I) {
     map<const string, Value *> &Plane = I->second;
     
     map<const string, Value *>::iterator B;