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

llvm-svn: 96
diff --git a/llvm/lib/VMCore/AsmWriter.cpp b/llvm/lib/VMCore/AsmWriter.cpp
index 97affcc..c68cd79 100644
--- a/llvm/lib/VMCore/AsmWriter.cpp
+++ b/llvm/lib/VMCore/AsmWriter.cpp
@@ -178,7 +178,7 @@
       writeOperand(I->getOperand(op+1), true);
     }
     Out << "\n\t]";
-  } else if (I->getInstType() == Instruction::PHINode) {
+  } else if (I->isPHINode()) {
     Out << " " << Operand->getType();
 
     Out << " [";  writeOperand(Operand, false); Out << ",";
@@ -262,7 +262,7 @@
   } else {
     int Slot = Table.getValSlot(Operand);
     
-    if (Operand->getValueType() == Value::ConstantVal) {
+    if (Operand->isConstant()) {
       Out << " " << ((ConstPoolVal*)Operand)->getStrValue();
     } else {
       if (Slot >= 0)  Out << " %" << Slot;
@@ -313,12 +313,11 @@
   // A Constant pool value may have a parent that is either a method or a 
   // module.  Untangle this now...
   //
-  if (CPV->getParent() == 0 || 
-      CPV->getParent()->getValueType() == Value::MethodVal) {
+  if (CPV->getParent() == 0 || CPV->getParent()->isMethod()) {
     SlotTable = new SlotCalculator((Method*)CPV->getParent(), true);
   } else {
-    assert(CPV->getParent()->getValueType() == Value::ModuleVal);
-    SlotTable = new SlotCalculator((Module*)CPV->getParent(), true);
+    SlotTable =
+      new SlotCalculator(CPV->getParent()->castModuleAsserting(), true);
   }
 
   AssemblyWriter W(o, *SlotTable);
diff --git a/llvm/lib/VMCore/BasicBlock.cpp b/llvm/lib/VMCore/BasicBlock.cpp
index f60bd46..36ad93d 100644
--- a/llvm/lib/VMCore/BasicBlock.cpp
+++ b/llvm/lib/VMCore/BasicBlock.cpp
@@ -73,7 +73,7 @@
 //
 bool BasicBlock::hasConstantPoolReferences() const {
   for (use_const_iterator I = use_begin(), E = use_end(); I != E; ++I)
-    if ((*I)->getValueType() == ConstantVal)
+    if ((*I)->isConstant())
       return true;
 
   return false;
@@ -91,7 +91,7 @@
 // cause a degenerate basic block to be formed, having a terminator inside of
 // the basic block). 
 //
-BasicBlock *BasicBlock::splitBasicBlock(InstListType::iterator I) {
+BasicBlock *BasicBlock::splitBasicBlock(iterator I) {
   assert(getTerminator() && "Can't use splitBasicBlock on degenerate BB!");
   assert(I != InstList.end() && 
 	 "Trying to get me to create degenerate basic block!");
@@ -102,7 +102,7 @@
   // to the new basic block...
   Instruction *Inst = 0;
   do {
-    InstListType::iterator EndIt = InstList.end();
+    iterator EndIt = end();
     Inst = InstList.remove(--EndIt);                  // Remove from end
     New->InstList.push_front(Inst);                   // Add to front
   } while (Inst != *I);   // Loop until we move the specified instruction.
diff --git a/llvm/lib/VMCore/ConstantPool.cpp b/llvm/lib/VMCore/ConstantPool.cpp
index d624c8da..77ed50a 100644
--- a/llvm/lib/VMCore/ConstantPool.cpp
+++ b/llvm/lib/VMCore/ConstantPool.cpp
@@ -85,9 +85,8 @@
 
 void ConstantPool::dropAllReferences() {
   for (unsigned i = 0; i < Planes.size(); i++)
-    for (PlaneType::iterator I = Planes[i]->begin();
-	 I != Planes[i]->end(); I++)
-      (*I)->dropAllReferences();
+    for_each(Planes[i]->begin(), Planes[i]->end(),
+	     mem_fun(&ConstPoolVal::dropAllReferences));
 }
 
 struct EqualsConstant {
@@ -280,10 +279,7 @@
 //                          getStrValue implementations
 
 string ConstPoolBool::getStrValue() const {
-  if (Val)
-    return "true";
-  else
-    return "false";
+  return Val ? "true" : "false";
 }
 
 string ConstPoolSInt::getStrValue() const {
diff --git a/llvm/lib/VMCore/Function.cpp b/llvm/lib/VMCore/Function.cpp
index 550c4f0..aa0d08b 100644
--- a/llvm/lib/VMCore/Function.cpp
+++ b/llvm/lib/VMCore/Function.cpp
@@ -29,8 +29,8 @@
   dropAllReferences();    // After this it is safe to delete instructions.
 
   // TODO: Should remove from the end, not the beginning of vector!
-  BasicBlocksType::iterator BI = BasicBlocks.begin();
-  while ((BI = BasicBlocks.begin()) != BasicBlocks.end())
+  iterator BI = begin();
+  while ((BI = begin()) != end())
     delete BasicBlocks.remove(BI);
 
   // Delete all of the method arguments and unlink from symbol table...
@@ -70,6 +70,5 @@
 // delete.
 //
 void Method::dropAllReferences() {
-  for_each(BasicBlocks.begin(), BasicBlocks.end(), 
-	   std::mem_fun(&BasicBlock::dropAllReferences));
+  for_each(begin(), end(), std::mem_fun(&BasicBlock::dropAllReferences));
 }
diff --git a/llvm/lib/VMCore/InstrTypes.cpp b/llvm/lib/VMCore/InstrTypes.cpp
index 142019b..baafe0b 100644
--- a/llvm/lib/VMCore/InstrTypes.cpp
+++ b/llvm/lib/VMCore/InstrTypes.cpp
@@ -68,8 +68,7 @@
   if (i >= IncomingValues.size()*2) return false;
 
   if (i & 1) {
-    assert(Val->getValueType() == BasicBlockVal && "Not a BB!");
-    IncomingValues[i/2].second = (BasicBlock*)Val;
+    IncomingValues[i/2].second = Val->castBasicBlockAsserting();
   } else {
     IncomingValues[i/2].first  = Val;
   }
diff --git a/llvm/lib/VMCore/Module.cpp b/llvm/lib/VMCore/Module.cpp
index 4f2eee4..7fa2802 100644
--- a/llvm/lib/VMCore/Module.cpp
+++ b/llvm/lib/VMCore/Module.cpp
@@ -37,6 +37,6 @@
 //
 void Module::dropAllReferences() {
   MethodListType::iterator MI = MethodList.begin();
-  for (; MI != MethodList.end(); MI++)
+  for (; MI != MethodList.end(); ++MI)
     (*MI)->dropAllReferences();
 }
diff --git a/llvm/lib/VMCore/SlotCalculator.cpp b/llvm/lib/VMCore/SlotCalculator.cpp
index 01fae37..f7eada1 100644
--- a/llvm/lib/VMCore/SlotCalculator.cpp
+++ b/llvm/lib/VMCore/SlotCalculator.cpp
@@ -176,12 +176,11 @@
   // Insert node into table and NodeMap...
   NodeMap[D] = Table[Ty].size();
 
-  if (Typ == Type::TypeTy &&      // If it's a type constant, add the Type also
-      D->getValueType() != Value::TypeVal) {
-    assert(D->getValueType() == Value::ConstantVal && 
-           "All Type instances should be constant types!");
-
-    const ConstPoolType *CPT = (const ConstPoolType*)D;
+  if (Typ == Type::TypeTy && !D->isType()) {
+    // If it's a type constant, add the Type also
+      
+    // All Type instances should be constant types!
+    const ConstPoolType *CPT = (const ConstPoolType*)D->castConstantAsserting();
     int Slot = getValSlot(CPT->getValue());
     if (Slot == -1) {
       // Only add if it's not already here!
diff --git a/llvm/lib/VMCore/SymbolTable.cpp b/llvm/lib/VMCore/SymbolTable.cpp
index 214f41f..07e4b87 100644
--- a/llvm/lib/VMCore/SymbolTable.cpp
+++ b/llvm/lib/VMCore/SymbolTable.cpp
@@ -15,9 +15,9 @@
 SymbolTable::~SymbolTable() {
 #ifndef NDEBUG   // Only do this in -g mode...
   bool Good = true;
-  for (iterator i = begin(); i != end(); i++) {
+  for (iterator i = begin(); i != end(); ++i) {
     if (i->second.begin() != i->second.end()) {
-      for (type_iterator I = i->second.begin(); I != i->second.end(); I++)
+      for (type_iterator I = i->second.begin(); I != i->second.end(); ++I)
         cerr << "Value still in symbol table! Type = " << i->first->getName() 
              << "  Name = " << I->first << endl;
       Good = false;
diff --git a/llvm/lib/VMCore/Type.cpp b/llvm/lib/VMCore/Type.cpp
index cf16309..5ae2cf9 100644
--- a/llvm/lib/VMCore/Type.cpp
+++ b/llvm/lib/VMCore/Type.cpp
@@ -157,13 +157,13 @@
 const MethodType *MethodType::getMethodType(const Type *ReturnType, 
                                             const vector<const Type*> &Params) {
   static vector<const MethodType*> ExistingMethodTypesCache;
-  for (unsigned i = 0; i < ExistingMethodTypesCache.size(); i++) {
+  for (unsigned i = 0; i < ExistingMethodTypesCache.size(); ++i) {
     const MethodType *T = ExistingMethodTypesCache[i];
     if (T->getReturnType() == ReturnType) {
       const ParamTypes &EParams = T->getParamTypes();
       ParamTypes::const_iterator I = Params.begin(); 
       ParamTypes::const_iterator J = EParams.begin(); 
-      for (; I != Params.end() && J != EParams.end(); I++, J++)
+      for (; I != Params.end() && J != EParams.end(); ++I, ++J)
         if (*I != *J) break;  // These types aren't equal!
 
       if (I == Params.end() && J == EParams.end()) {
@@ -189,7 +189,7 @@
   // Calculate the string name for the new type...
   string Name = ReturnType->getName() + " (";
   for (ParamTypes::const_iterator I = Params.begin();  
-       I != Params.end(); I++) {
+       I != Params.end(); ++I) {
     if (I != Params.begin())
       Name += ", ";
     Name += (*I)->getName();
@@ -211,7 +211,7 @@
   static vector<const ArrayType*> ExistingTypesCache;
 
   // Search cache for value...
-  for (unsigned i = 0; i < ExistingTypesCache.size(); i++) {
+  for (unsigned i = 0; i < ExistingTypesCache.size(); ++i) {
     const ArrayType *T = ExistingTypesCache[i];
 
     if (T->getElementType() == ElementType && 
@@ -237,13 +237,13 @@
 const StructType *StructType::getStructType(const ElementTypes &ETypes) {
   static vector<const StructType*> ExistingStructTypesCache;
 
-  for (unsigned i = 0; i < ExistingStructTypesCache.size(); i++) {
+  for (unsigned i = 0; i < ExistingStructTypesCache.size(); ++i) {
     const StructType *T = ExistingStructTypesCache[i];
 
     const ElementTypes &Elements = T->getElementTypes();
     ElementTypes::const_iterator I = ETypes.begin(); 
     ElementTypes::const_iterator J = Elements.begin(); 
-    for (; I != ETypes.end() && J != Elements.end(); I++, J++)
+    for (; I != ETypes.end() && J != Elements.end(); ++I, ++J)
       if (*I != *J) break;  // These types aren't equal!
     
     if (I == ETypes.end() && J == Elements.end()) {
@@ -269,7 +269,7 @@
   // Calculate the string name for the new type...
   string Name = "{ ";
   for (ElementTypes::const_iterator I = ETypes.begin();  
-       I != ETypes.end(); I++) {
+       I != ETypes.end(); ++I) {
     if (I != ETypes.begin())
       Name += ", ";
     Name += (*I)->getName();
@@ -290,7 +290,7 @@
   static vector<const PointerType*> ExistingTypesCache;
 
   // Search cache for value...
-  for (unsigned i = 0; i < ExistingTypesCache.size(); i++) {
+  for (unsigned i = 0; i < ExistingTypesCache.size(); ++i) {
     const PointerType *T = ExistingTypesCache[i];
 
     if (T->getValueType() == ValueType)
diff --git a/llvm/lib/VMCore/Value.cpp b/llvm/lib/VMCore/Value.cpp
index ee642f6..79d8e0a 100644
--- a/llvm/lib/VMCore/Value.cpp
+++ b/llvm/lib/VMCore/Value.cpp
@@ -34,7 +34,7 @@
   // a <badref>
   //
   if (Uses.begin() != Uses.end()) {
-    for (use_const_iterator I = Uses.begin(); I != Uses.end(); I++)
+    for (use_const_iterator I = Uses.begin(); I != Uses.end(); ++I)
       cerr << "Use still stuck around after Def is destroyed:" << *I << endl;
   }
 #endif
@@ -88,7 +88,7 @@
 void User::replaceUsesOfWith(Value *From, Value *To) {
   if (From == To) return;   // Duh what?
 
-  for (unsigned OpNum = 0; Value *D = getOperand(OpNum); OpNum++) {   
+  for (unsigned OpNum = 0; Value *D = getOperand(OpNum); ++OpNum) {   
     if (D == From) {  // Okay, this operand is pointing to our fake def.
       // The side effects of this setOperand call include linking to
       // "To", adding "this" to the uses list of To, and
@@ -140,7 +140,7 @@
   if (!SymTab) return false;
 
   for (SymbolTable::const_iterator I = SymTab->begin(); 
-       I != SymTab->end(); I++) {
+       I != SymTab->end(); ++I) {
     if (I->second.begin() != I->second.end())
       return true;                                // Found nonempty type plane!
   }
diff --git a/llvm/lib/VMCore/Verifier.cpp b/llvm/lib/VMCore/Verifier.cpp
index 820fa5c..5b6654d 100644
--- a/llvm/lib/VMCore/Verifier.cpp
+++ b/llvm/lib/VMCore/Verifier.cpp
@@ -71,10 +71,9 @@
 bool verify(const Method *M, vector<string> &ErrorMsgs) {
   bool Bad = false;
   
-  for (Method::BasicBlocksType::const_iterator BBIt = M->getBasicBlocks().begin();
-       BBIt != M->getBasicBlocks().end(); BBIt++) {
+  for (Method::const_iterator BBIt = M->begin();
+       BBIt != M->end(); ++BBIt)
     Bad |= verify(*BBIt, ErrorMsgs);
-  }
 
   return Bad;
 }
@@ -84,11 +83,8 @@
   assert(Type::FirstDerivedTyID-1 < sizeof(long)*8 && 
 	 "Resize ValidTypes table to handle more than 32 primitive types!");
 
-  for (Module::MethodListType::const_iterator MI = C->getMethodList().begin();
-       MI != C->getMethodList().end(); MI++) {
-    const Method *M = *MI;
-    Bad |= verify(M, ErrorMsgs);
-  }
+  for (Module::const_iterator MI = C->begin(); MI != C->end(); ++MI)
+    Bad |= verify(*MI, ErrorMsgs);
   
   return Bad;
 }
diff --git a/llvm/lib/VMCore/iCall.cpp b/llvm/lib/VMCore/iCall.cpp
index 9ff6bb6..7632798 100644
--- a/llvm/lib/VMCore/iCall.cpp
+++ b/llvm/lib/VMCore/iCall.cpp
@@ -14,7 +14,7 @@
 
   const MethodType* MT = M->getMethodType();
   const MethodType::ParamTypes &PL = MT->getParamTypes();
-  assert(params.size() == PL.size());
+  assert(params.size() == PL.size() && "Calling a function with bad signature");
 #ifndef NDEBUG
   MethodType::ParamTypes::const_iterator It = PL.begin();
 #endif
@@ -38,8 +38,7 @@
 bool CallInst::setOperand(unsigned i, Value *Val) {
   if (i > Params.size()) return false;
   if (i == 0) {
-    assert(Val->getValueType() == Value::MethodVal);
-    M = (Method*)Val;
+    M = Val->castMethodAsserting();
   } else {
     // TODO: assert = method arg type
     Params[i-1] = Val;