Changed the fundemental architecture of Operands for Instructions.  Now
Operands are maintained as a vector<Use> in the User class, and operator
iterators are provided as before.  Getting an operand no longer requires
a virtual function call.

WARNING: getOperand(x) where x >= getNumOperands() will now assert instead
of returning null!


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@149 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Bytecode/Reader/InstructionReader.cpp b/lib/Bytecode/Reader/InstructionReader.cpp
index fd919c9..dac7d37 100644
--- a/lib/Bytecode/Reader/InstructionReader.cpp
+++ b/lib/Bytecode/Reader/InstructionReader.cpp
@@ -80,8 +80,11 @@
     break;
   }
 
-  //cerr << "NO: "  << Result.NumOperands   << " opcode: " << Result.Opcode 
-  //   << " Ty: " << Result.Ty->getName() << " arg1: "   << Result.Arg1 << endl;
+#if 0
+  cerr << "NO: "  << Result.NumOperands   << " opcode: " << Result.Opcode 
+       << " Ty: " << Result.Ty->getName() << " arg1: "   << Result.Arg1 
+       << " arg2: "   << Result.Arg2 << " arg3: "   << Result.Arg3 << endl;
+#endif
   return false;
 }
 
@@ -198,13 +201,13 @@
     return false;
   } else if (Raw.Opcode == Instruction::Malloc) {
     if (Raw.NumOperands > 2) return true;
-    Value *Sz = (Raw.NumOperands == 2) ? getValue(Type::UIntTy, Raw.Arg2) : 0;
-    Res = new MallocInst((ConstPoolType*)getValue(Type::TypeTy, Raw.Arg1), Sz);
+    Value *Sz = Raw.NumOperands ? getValue(Type::UIntTy, Raw.Arg1) : 0;
+    Res = new MallocInst(Raw.Ty, Sz);
     return false;
   } else if (Raw.Opcode == Instruction::Alloca) {
     if (Raw.NumOperands > 2) return true;
-    Value *Sz = (Raw.NumOperands == 2) ? getValue(Type::UIntTy, Raw.Arg2) : 0;
-    Res = new AllocaInst((ConstPoolType*)getValue(Type::TypeTy, Raw.Arg1), Sz);
+    Value *Sz = Raw.NumOperands ? getValue(Type::UIntTy, Raw.Arg1) : 0;
+    Res = new AllocaInst(Raw.Ty, Sz);
     return false;
   } else if (Raw.Opcode == Instruction::Free) {
     Value *Val = getValue(Raw.Ty, Raw.Arg1);
@@ -213,6 +216,7 @@
     return false;
   }
 
-  cerr << "Unrecognized instruction! " << Raw.Opcode << endl;
+  cerr << "Unrecognized instruction! " << Raw.Opcode 
+       << " ADDR = 0x" << (void*)Buf << endl;
   return true;
 }
diff --git a/lib/Bytecode/Reader/ReaderInternals.h b/lib/Bytecode/Reader/ReaderInternals.h
index 3bb0472..fffcdb8 100644
--- a/lib/Bytecode/Reader/ReaderInternals.h
+++ b/lib/Bytecode/Reader/ReaderInternals.h
@@ -94,16 +94,9 @@
 
 struct InstPlaceHolderHelper : public Instruction {
   InstPlaceHolderHelper(const Type *Ty) : Instruction(Ty, UserOp1, "") {}
-  inline virtual void dropAllReferences() {}
   virtual string getOpcode() const { return "placeholder"; }
 
   virtual Instruction *clone() const { abort(); return 0; }
-
-  // No "operands"...
-  virtual Value *getOperand(unsigned i) { return 0; }
-  virtual const Value *getOperand(unsigned i) const { return 0; }
-  virtual bool setOperand(unsigned i, Value *Val) { return false; }
-  virtual unsigned getNumOperands() const { return 0; }
 };
 
 struct BBPlaceHolderHelper : public BasicBlock {
diff --git a/lib/Bytecode/Writer/ConstantWriter.cpp b/lib/Bytecode/Writer/ConstantWriter.cpp
index e65ea45..5ca0fca 100644
--- a/lib/Bytecode/Writer/ConstantWriter.cpp
+++ b/lib/Bytecode/Writer/ConstantWriter.cpp
@@ -128,7 +128,7 @@
 
   case Type::StructTyID: {
     const ConstPoolStruct *CPS = (const ConstPoolStruct*)CPV;
-    const vector<ConstPoolUse> &Vals = CPS->getValues();
+    const vector<Use> &Vals = CPS->getValues();
 
     for (unsigned i = 0; i < Vals.size(); ++i) {
       int Slot = Table.getValSlot(Vals[i]);
diff --git a/lib/Bytecode/Writer/InstructionWriter.cpp b/lib/Bytecode/Writer/InstructionWriter.cpp
index c7c04ef..0cd93c0 100644
--- a/lib/Bytecode/Writer/InstructionWriter.cpp
+++ b/lib/Bytecode/Writer/InstructionWriter.cpp
@@ -32,14 +32,13 @@
   output_vbr(I->getInstType(), Out);             // Instruction Opcode ID
   output_vbr(Type, Out);                         // Result type
 
-  unsigned NumArgs;  // Count the number of arguments to the instruction
-  for (NumArgs = 0; I->getOperand(NumArgs); NumArgs++) /*empty*/;
+  unsigned NumArgs = I->getNumOperands();
   output_vbr(NumArgs, Out);
 
-  for (unsigned i = 0; const Value *N = I->getOperand(i); i++) {
-    assert(i < NumArgs && "Count of arguments failed!");
-
+  for (unsigned i = 0; i < NumArgs; ++i) {
+    const Value *N = I->getOperand(i);
     int Slot = Table.getValSlot(N);
+    assert(Slot >= 0 && "No slot number for value!?!?");      
     output_vbr((unsigned)Slot, Out);
   }
   align32(Out);    // We must maintain correct alignment!
@@ -110,25 +109,24 @@
   //
   unsigned Opcode = (3 << 30) | (IType << 24) | (Type << 18) |
                     (Slots[0] << 12) | (Slots[1] << 6) | (Slots[2] << 0);
-  //  cerr << "3 " << IType << " " << Type << " " << Slots[0] << " " 
-  //       << Slots[1] << " " << Slots[2] << endl;
+  //cerr << "3 " << IType << " " << Type << " " << Slots[0] << " " 
+  //     << Slots[1] << " " << Slots[2] << endl;
   output(Opcode, Out);
 }
 
 bool BytecodeWriter::processInstruction(const Instruction *I) {
   assert(I->getInstType() < 64 && "Opcode too big???");
 
-  unsigned NumOperands = 0;
+  unsigned NumOperands = I->getNumOperands();
   int MaxOpSlot = 0;
-  int Slots[3]; Slots[0] = (1 << 12)-1;
+  int Slots[3]; Slots[0] = (1 << 12)-1;   // Marker to signify 0 operands
 
-  const Value *Def;
-  while ((Def = I->getOperand(NumOperands))) {
+  for (unsigned i = 0; i < NumOperands; ++i) {
+    const Value *Def = I->getOperand(i);
     int slot = Table.getValSlot(Def);
     assert(slot != -1 && "Broken bytecode!");
     if (slot > MaxOpSlot) MaxOpSlot = slot;
-    if (NumOperands < 3) Slots[NumOperands] = slot;
-    NumOperands++;
+    if (i < 3) Slots[i] = slot;
   }
 
   // Figure out which type to encode with the instruction.  Typically we want
@@ -137,12 +135,10 @@
   // the first param is actually interesting).  But if we have no arguments
   // we take the type of the instruction itself.  
   //
-
-  const Type *Ty;
-  if (NumOperands)
-    Ty = I->getOperand(0)->getType();
-  else
-    Ty = I->getType();
+  const Type *Ty = NumOperands ? I->getOperand(0)->getType() : I->getType();
+  if (I->getInstType() == Instruction::Malloc || 
+      I->getInstType() == Instruction::Alloca)
+    Ty = I->getType();  // Malloc & Alloca ALWAYS want to encode the return type
 
   unsigned Type;
   int Slot = Table.getValSlot(Ty);
@@ -179,6 +175,8 @@
     break;
   }
 
+  // If we weren't handled before here, we either have a large number of operands
+  // or a large operand index that we are refering to.
   outputInstructionFormat0(I, Table, Type, Out);
   return false;
 }
diff --git a/lib/Transforms/IPO/InlineSimple.cpp b/lib/Transforms/IPO/InlineSimple.cpp
index d85b33d..de76dcd 100644
--- a/lib/Transforms/IPO/InlineSimple.cpp
+++ b/lib/Transforms/IPO/InlineSimple.cpp
@@ -37,7 +37,8 @@
 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, E = I->getNumOperands(); op != E; ++op) {
+    const Value *Op = I->getOperand(op);
     Value *V = ValueMap[Op];
     if (!V && Op->isMethod()) 
       continue;  // Methods don't get relocated
@@ -115,11 +116,9 @@
   //
   Method::ArgumentListType::const_iterator PTI = 
     CalledMeth->getArgumentList().begin();
-  for (unsigned a = 1; Value *Operand = CI->getOperand(a); ++a, ++PTI) {
-    ValueMap[*PTI] = Operand;
-  }
+  for (unsigned a = 1, E = CI->getNumOperands(); a != E; ++a, ++PTI)
+    ValueMap[*PTI] = CI->getOperand(a);
   
-
   ValueMap[NewBB] = NewBB;  // Returns get converted to reference NewBB
 
   // Loop over all of the basic blocks in the method, inlining them as 
diff --git a/lib/Transforms/Scalar/ConstantProp.cpp b/lib/Transforms/Scalar/ConstantProp.cpp
index 7a0254b..91a21c3 100644
--- a/lib/Transforms/Scalar/ConstantProp.cpp
+++ b/lib/Transforms/Scalar/ConstantProp.cpp
@@ -132,9 +132,9 @@
     BasicBlock *Dest1 = BI->getOperand(0)->castBasicBlockAsserting();
     BasicBlock *Dest2 = BI->getOperand(1)->castBasicBlockAsserting();
 
-    if (BI->getOperand(2)->isConstant()) {    // Are we branching on constant?
+    if (BI->getCondition()->isConstant()) {    // Are we branching on constant?
       // YES.  Change to unconditional branch...
-      ConstPoolBool *Cond = (ConstPoolBool*)BI->getOperand(2);
+      ConstPoolBool *Cond = (ConstPoolBool*)BI->getCondition();
       BasicBlock *Destination = Cond->getValue() ? Dest1 : Dest2;
       BasicBlock *OldDest     = Cond->getValue() ? Dest2 : Dest1;
 
@@ -147,9 +147,9 @@
       assert(BI->getParent() && "Terminator not inserted in block!");
       OldDest->removePredecessor(BI->getParent());
 
-      BI->setOperand(0, Destination);  // Set the unconditional destination
-      BI->setOperand(1, 0);            // Clear the conditional destination
-      BI->setOperand(2, 0);            // Clear the condition...
+      // Set the unconditional destination, and change the insn to be an
+      // unconditional branch.
+      BI->setUnconditionalDest(Destination);
       return true;
     } else if (Dest2 == Dest1) {       // Conditional branch to same location?
       // This branch matches something like this:  
@@ -160,9 +160,8 @@
       assert(BI->getParent() && "Terminator not inserted in block!");
       Dest1->removePredecessor(BI->getParent());
 
-      // Nuke the second destination, and the use of the condition variable
-      BI->setOperand(1, 0);            // Clear the conditional destination
-      BI->setOperand(2, 0);            // Clear the condition...
+      // Change a conditional branch to unconditional.
+      BI->setUnconditionalDest(Dest1);
       return true;
     }
   }
@@ -192,7 +191,7 @@
     PHINode *PN = (PHINode*)Inst; // If it's a PHI node and only has one operand
                                   // Then replace it directly with that operand.
     assert(PN->getOperand(0) && "PHI Node must have at least one operand!");
-    if (PN->getOperand(1) == 0) {       // If the PHI Node has exactly 1 operand
+    if (PN->getNumOperands() == 1) {    // If the PHI Node has exactly 1 operand
       Value *V = PN->getOperand(0);
       PN->replaceAllUsesWith(V);                 // Replace all uses of this PHI
                                                  // Unlink from basic block
diff --git a/lib/Transforms/Scalar/DCE.cpp b/lib/Transforms/Scalar/DCE.cpp
index 8e37279..fa2c9c7 100644
--- a/lib/Transforms/Scalar/DCE.cpp
+++ b/lib/Transforms/Scalar/DCE.cpp
@@ -91,7 +91,7 @@
 
   do {
     PHINode *PN = (PHINode*)I;
-    assert(PN->getOperand(2) == 0 && "PHI node should only have one value!");
+    assert(PN->getNumOperands() == 2 && "PHI node should only have one value!");
     Value *V = PN->getOperand(0);
 
     PN->replaceAllUsesWith(V);      // Replace PHI node with its single value.
diff --git a/lib/Transforms/Scalar/SCCP.cpp b/lib/Transforms/Scalar/SCCP.cpp
index 8b59e19..5bb75c7 100644
--- a/lib/Transforms/Scalar/SCCP.cpp
+++ b/lib/Transforms/Scalar/SCCP.cpp
@@ -408,10 +408,10 @@
 	markExecutable(Succ);
     } else if (SCValue.isConstant()) {
       ConstPoolVal *CPV = SCValue.getConstant();
-      for (SwitchInst::dest_iterator I = SI->dest_begin(), E = SI->dest_end();
-	   I != E; ++I) {
-	if (I->first->equals(CPV)) {   // Found the right branch...
-	  markExecutable(I->second);
+      // Make sure to skip the "default value" which isn't a value
+      for (unsigned i = 1, E = SI->getNumSuccessors(); i != E; ++i) {
+	if (SI->getSuccessorValue(i)->equals(CPV)) {// Found the right branch...
+	  markExecutable(SI->getSuccessor(i));
 	  return;
 	}
       }
diff --git a/lib/VMCore/AsmWriter.cpp b/lib/VMCore/AsmWriter.cpp
index c68cd79..e2b2495 100644
--- a/lib/VMCore/AsmWriter.cpp
+++ b/lib/VMCore/AsmWriter.cpp
@@ -157,10 +157,10 @@
   Out << I->getOpcode();
 
   // Print out the type of the operands...
-  const Value *Operand = I->getOperand(0);
+  const Value *Operand = I->getNumOperands() ? I->getOperand(0) : 0;
 
   // Special case conditional branches to swizzle the condition out to the front
-  if (I->getInstType() == Instruction::Br && I->getOperand(1)) {
+  if (I->getInstType() == Instruction::Br && I->getNumOperands() > 1) {
     writeOperand(I->getOperand(2), true);
     Out << ",";
     writeOperand(Operand, true);
@@ -172,9 +172,9 @@
     writeOperand(Operand         , true); Out << ",";
     writeOperand(I->getOperand(1), true); Out << " [";
 
-    for (unsigned op = 2; (Operand = I->getOperand(op)); op += 2) {
+    for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
       Out << "\n\t\t";
-      writeOperand(Operand, true); Out << ",";
+      writeOperand(I->getOperand(op  ), true); Out << ",";
       writeOperand(I->getOperand(op+1), true);
     }
     Out << "\n\t]";
@@ -183,8 +183,9 @@
 
     Out << " [";  writeOperand(Operand, false); Out << ",";
     writeOperand(I->getOperand(1), false); Out << " ]";
-    for (unsigned op = 2; (Operand = I->getOperand(op)); op += 2) {
-      Out << ", [";  writeOperand(Operand, false); Out << ",";
+    for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; op += 2) {
+      Out << ", [";  
+      writeOperand(I->getOperand(op  ), false); Out << ",";
       writeOperand(I->getOperand(op+1), false); Out << " ]";
     }
   } else if (I->getInstType() == Instruction::Ret && !Operand) {
@@ -192,20 +193,19 @@
   } else if (I->getInstType() == Instruction::Call) {
     writeOperand(Operand, true);
     Out << "(";
-    Operand = I->getOperand(1);
-    if (Operand) writeOperand(Operand, true);
-    for (unsigned op = 2; (Operand = I->getOperand(op)); ++op) {
+    if (I->getNumOperands() > 1) writeOperand(I->getOperand(1), true);
+    for (unsigned op = 2, Eop = I->getNumOperands(); op < Eop; ++op) {
       Out << ",";
-      writeOperand(Operand, true);
+      writeOperand(I->getOperand(op), true);
     }
 
     Out << " )";
   } else if (I->getInstType() == Instruction::Malloc || 
 	     I->getInstType() == Instruction::Alloca) {
-    Out << " " << ((const PointerType*)((ConstPoolType*)Operand)
-		   ->getValue())->getValueType();
-    if ((Operand = I->getOperand(1))) {
-      Out << ","; writeOperand(Operand, true);
+    Out << " " << ((const PointerType*)I->getType())->getValueType();
+    if (I->getNumOperands()) {
+      Out << ",";
+      writeOperand(I->getOperand(0), true);
     }
 
   } else if (Operand) {   // Print the normal way...
@@ -215,9 +215,9 @@
     // different type operands (for example br), then they are all printed.
     bool PrintAllTypes = false;
     const Type *TheType = Operand->getType();
-    unsigned i;
 
-    for (i = 1; (Operand = I->getOperand(i)); i++) {
+    for (unsigned i = 1, E = I->getNumOperands(); i != E; ++i) {
+      Operand = I->getOperand(i);
       if (Operand->getType() != TheType) {
 	PrintAllTypes = true;       // We have differing types!  Print them all!
 	break;
@@ -227,9 +227,9 @@
     if (!PrintAllTypes)
       Out << " " << I->getOperand(0)->getType();
 
-    for (unsigned i = 0; (Operand = I->getOperand(i)); i++) {
+    for (unsigned i = 0, E = I->getNumOperands(); i != E; ++i) {
       if (i) Out << ",";
-      writeOperand(Operand, PrintAllTypes);
+      writeOperand(I->getOperand(i), PrintAllTypes);
     }
   }
 
@@ -262,8 +262,8 @@
   } else {
     int Slot = Table.getValSlot(Operand);
     
-    if (Operand->isConstant()) {
-      Out << " " << ((ConstPoolVal*)Operand)->getStrValue();
+    if (const ConstPoolVal *CPV = Operand->castConstant()) {
+      Out << " " << CPV->getStrValue();
     } else {
       if (Slot >= 0)  Out << " %" << Slot;
       else if (PrintName)
diff --git a/lib/VMCore/ConstantPool.cpp b/lib/VMCore/ConstantPool.cpp
index 77ed50a..13463a1 100644
--- a/lib/VMCore/ConstantPool.cpp
+++ b/lib/VMCore/ConstantPool.cpp
@@ -219,7 +219,7 @@
   : ConstPoolVal(T, Name) {
   for (unsigned i = 0; i < V.size(); i++) {
     assert(V[i]->getType() == T->getElementType());
-    Val.push_back(ConstPoolUse(V[i], this));
+    Operands.push_back(Use(V[i], this));
   }
 }
 
@@ -231,7 +231,7 @@
 
   for (unsigned i = 0; i < V.size(); i++) {
     assert(V[i]->getType() == ETypes[i]);
-    Val.push_back(ConstPoolUse(V[i], this));
+    Operands.push_back(Use(V[i], this));
   }
 }
 
@@ -265,14 +265,14 @@
 
 ConstPoolArray::ConstPoolArray(const ConstPoolArray &CPA)
   : ConstPoolVal(CPA.getType()) {
-  for (unsigned i = 0; i < CPA.Val.size(); i++)
-    Val.push_back(ConstPoolUse((ConstPoolVal*)CPA.Val[i], this));
+  for (unsigned i = 0; i < CPA.Operands.size(); i++)
+    Operands.push_back(Use(CPA.Operands[i], this));
 }
 
 ConstPoolStruct::ConstPoolStruct(const ConstPoolStruct &CPS)
   : ConstPoolVal(CPS.getType()) {
-  for (unsigned i = 0; i < CPS.Val.size(); i++)
-    Val.push_back(ConstPoolUse((ConstPoolVal*)CPS.Val[i], this));
+  for (unsigned i = 0; i < CPS.Operands.size(); i++)
+    Operands.push_back(Use(CPS.Operands[i], this));
 }
 
 //===----------------------------------------------------------------------===//
@@ -301,12 +301,12 @@
 
 string ConstPoolArray::getStrValue() const {
   string Result = "[";
-  if (Val.size()) {
-    Result += " " + Val[0]->getType()->getName() + 
-	      " " + Val[0]->getStrValue();
-    for (unsigned i = 1; i < Val.size(); i++)
-      Result += ", " + Val[i]->getType()->getName() + 
-	         " " + Val[i]->getStrValue();
+  if (Operands.size()) {
+    Result += " " + Operands[0]->getType()->getName() + 
+	      " " + Operands[0]->castConstantAsserting()->getStrValue();
+    for (unsigned i = 1; i < Operands.size(); i++)
+      Result += ", " + Operands[i]->getType()->getName() + 
+	         " " + Operands[i]->castConstantAsserting()->getStrValue();
   }
 
   return Result + " ]";
@@ -314,12 +314,12 @@
 
 string ConstPoolStruct::getStrValue() const {
   string Result = "{";
-  if (Val.size()) {
-    Result += " " + Val[0]->getType()->getName() + 
-	      " " + Val[0]->getStrValue();
-    for (unsigned i = 1; i < Val.size(); i++)
-      Result += ", " + Val[i]->getType()->getName() + 
-	         " " + Val[i]->getStrValue();
+  if (Operands.size()) {
+    Result += " " + Operands[0]->getType()->getName() + 
+	      " " + Operands[0]->castConstantAsserting()->getStrValue();
+    for (unsigned i = 1; i < Operands.size(); i++)
+      Result += ", " + Operands[i]->getType()->getName() + 
+	         " " + Operands[i]->castConstantAsserting()->getStrValue();
   }
 
   return Result + " }";
@@ -356,9 +356,11 @@
 bool ConstPoolArray::equals(const ConstPoolVal *V) const {
   assert(getType() == V->getType());
   ConstPoolArray *AV = (ConstPoolArray*)V;
-  if (Val.size() != AV->Val.size()) return false;
-  for (unsigned i = 0; i < Val.size(); i++)
-    if (!Val[i]->equals(AV->Val[i])) return false;
+  if (Operands.size() != AV->Operands.size()) return false;
+  for (unsigned i = 0; i < Operands.size(); i++)
+    if (!Operands[i]->castConstantAsserting()->equals(
+               AV->Operands[i]->castConstantAsserting()))
+      return false;
 
   return true;
 }
@@ -366,9 +368,11 @@
 bool ConstPoolStruct::equals(const ConstPoolVal *V) const {
   assert(getType() == V->getType());
   ConstPoolStruct *SV = (ConstPoolStruct*)V;
-  if (Val.size() != SV->Val.size()) return false;
-  for (unsigned i = 0; i < Val.size(); i++)
-    if (!Val[i]->equals(SV->Val[i])) return false;
+  if (Operands.size() != SV->Operands.size()) return false;
+  for (unsigned i = 0; i < Operands.size(); i++)
+    if (!Operands[i]->castConstantAsserting()->equals(
+           SV->Operands[i]->castConstantAsserting()))
+      return false;
 
   return true;
 }
diff --git a/lib/VMCore/InstrTypes.cpp b/lib/VMCore/InstrTypes.cpp
index baafe0b..cda99a9 100644
--- a/lib/VMCore/InstrTypes.cpp
+++ b/lib/VMCore/InstrTypes.cpp
@@ -9,7 +9,7 @@
 #include "llvm/Method.h"
 #include "llvm/SymbolTable.h"
 #include "llvm/Type.h"
-#include <algorithm>
+#include <algorithm>  // find
 
 // TODO: Move to getUnaryOperator iUnary.cpp when and if it exists!
 UnaryOperator *UnaryOperator::create(unsigned Op, Value *Source) {
@@ -52,49 +52,25 @@
 
 PHINode::PHINode(const PHINode &PN) 
   : Instruction(PN.getType(), Instruction::PHINode) {
-  
-  for (unsigned i = 0; i < PN.IncomingValues.size(); i++)
-    IncomingValues.push_back(
-	make_pair(Use(PN.IncomingValues[i].first, this),
-		  BasicBlockUse(PN.IncomingValues[i].second, this)));
-}
-
-void PHINode::dropAllReferences() {
-  IncomingValues.clear();
-}
-
-bool PHINode::setOperand(unsigned i, Value *Val) {
-  assert(Val && "PHI node must only reference nonnull definitions!");
-  if (i >= IncomingValues.size()*2) return false;
-
-  if (i & 1) {
-    IncomingValues[i/2].second = Val->castBasicBlockAsserting();
-  } else {
-    IncomingValues[i/2].first  = Val;
+  Operands.reserve(PN.Operands.size());
+  for (unsigned i = 0; i < PN.Operands.size(); i+=2) {
+    Operands.push_back(Use(PN.Operands[i], this));
+    Operands.push_back(Use(PN.Operands[i+1], this));
   }
-  return true;
 }
 
 void PHINode::addIncoming(Value *D, BasicBlock *BB) {
-  IncomingValues.push_back(make_pair(Use(D, this), BasicBlockUse(BB, this)));
+  Operands.push_back(Use(D, this));
+  Operands.push_back(Use(BB, this));
 }
 
-struct FindBBEntry {
-  const BasicBlock *BB;
-  inline FindBBEntry(const BasicBlock *bb) : BB(bb) {}
-  inline bool operator()(const pair<Use,BasicBlockUse> &Entry) {
-    return Entry.second == BB;
-  }
-};
-
-
 // removeIncomingValue - Remove an incoming value.  This is useful if a
 // predecessor basic block is deleted.
 Value *PHINode::removeIncomingValue(const BasicBlock *BB) {
-  vector<PairTy>::iterator Idx = find_if(IncomingValues.begin(), 
-					 IncomingValues.end(), FindBBEntry(BB));
-  assert(Idx != IncomingValues.end() && "BB not in PHI node!");
-  Value *Removed = Idx->first;
-  IncomingValues.erase(Idx);
+  op_iterator Idx = find(Operands.begin(), Operands.end(), (const Value*)BB);
+  assert(Idx != Operands.end() && "BB not in PHI node!");
+  --Idx;  // Back up to value prior to Basic block
+  Value *Removed = *Idx;
+  Operands.erase(Idx, Idx+2);  // Erase Value and BasicBlock
   return Removed;
 }
diff --git a/lib/VMCore/Value.cpp b/lib/VMCore/Value.cpp
index fa3c0f6..a051bcb 100644
--- a/lib/VMCore/Value.cpp
+++ b/lib/VMCore/Value.cpp
@@ -89,14 +89,13 @@
 void User::replaceUsesOfWith(Value *From, Value *To) {
   if (From == To) return;   // Duh what?
 
-  for (unsigned OpNum = 0; Value *D = getOperand(OpNum); ++OpNum) {   
-    if (D == From) {  // Okay, this operand is pointing to our fake def.
+  for (unsigned i = 0, E = getNumOperands(); i != E; ++i)
+    if (getOperand(i) == From) {  // Is This operand is pointing to oldval?
       // The side effects of this setOperand call include linking to
       // "To", adding "this" to the uses list of To, and
       // most importantly, removing "this" from the use list of "From".
-      setOperand(OpNum, To); // Fix it now...
+      setOperand(i, To); // Fix it now...
     }
-  }
 }
 
 
diff --git a/lib/VMCore/iBranch.cpp b/lib/VMCore/iBranch.cpp
index 05f3505..d0f437e 100644
--- a/lib/VMCore/iBranch.cpp
+++ b/lib/VMCore/iBranch.cpp
@@ -13,9 +13,17 @@
 #endif
 
 BranchInst::BranchInst(BasicBlock *True, BasicBlock *False, Value *Cond) 
-  : TerminatorInst(Instruction::Br), TrueDest(True, this), 
-    FalseDest(False, this), Condition(Cond, this) {
+  : TerminatorInst(Instruction::Br) {
   assert(True != 0 && "True branch destination may not be null!!!");
+  Operands.reserve(False ? 3 : 1);
+  Operands.push_back(Use(True, this));
+  if (False) {
+    Operands.push_back(Use(False, this));
+    Operands.push_back(Use(Cond, this));
+  }
+
+  assert(!!False == !!Cond &&
+	 "Either both cond and false or neither can be specified!");
 
 #ifndef NDEBUG
   if (Cond != 0 && Cond->getType() != Type::BoolTy)
@@ -25,45 +33,12 @@
          "May only branch on boolean predicates!!!!");
 }
 
-BranchInst::BranchInst(const BranchInst &BI) 
-  : TerminatorInst(Instruction::Br), TrueDest(BI.TrueDest, this), 
-    FalseDest(BI.FalseDest, this), Condition(BI.Condition, this) {
-}
-
-
-void BranchInst::dropAllReferences() {
-  Condition = 0;
-  TrueDest = FalseDest = 0;
-}
-
-const Value *BranchInst::getOperand(unsigned i) const {
-    return (i == 0) ? (Value*)TrueDest : 
-          ((i == 1) ? (Value*)FalseDest : 
-          ((i == 2) ? (Value*)Condition : 0));
-}
-
-const BasicBlock *BranchInst::getSuccessor(unsigned i) const {
-  return (i == 0) ? (const BasicBlock*)TrueDest : 
-        ((i == 1) ? (const BasicBlock*)FalseDest : 0);
-}
-
-bool BranchInst::setOperand(unsigned i, Value *Val) { 
-  switch (i) {
-  case 0:
-    assert(Val && "Can't change primary direction to 0!");
-    assert(Val->getType() == Type::LabelTy);
-    TrueDest = (BasicBlock*)Val;
-    return true;
-  case 1:
-    assert(Val == 0 || Val->getType() == Type::LabelTy);
-    FalseDest = (BasicBlock*)Val;
-    return true;
-  case 2:
-    Condition = Val;
-    assert(!Condition || Condition->getType() == Type::BoolTy && 
-           "Condition expr must be a boolean expression!");
-    return true;
-  } 
-
-  return false;
+BranchInst::BranchInst(const BranchInst &BI) : TerminatorInst(Instruction::Br) {
+  Operands.reserve(BI.Operands.size());
+  Operands.push_back(Use(BI.Operands[0], this));
+  if (BI.Operands.size() != 1) {
+    assert(BI.Operands.size() == 3 && "BR can have 1 or 3 operands!");
+    Operands.push_back(Use(BI.Operands[1], this));
+    Operands.push_back(Use(BI.Operands[2], this));
+  }
 }
diff --git a/lib/VMCore/iCall.cpp b/lib/VMCore/iCall.cpp
index 7632798..67826ff 100644
--- a/lib/VMCore/iCall.cpp
+++ b/lib/VMCore/iCall.cpp
@@ -1,6 +1,6 @@
-//===-- iCall.cpp - Implement the Call & Invoke instructions -----*- C++ -*--=//
+//===-- iCall.cpp - Implement the call & icall instructions ------*- C++ -*--=//
 //
-// This file implements the call and invoke instructions.
+// This file implements the call and icall instructions.
 //
 //===----------------------------------------------------------------------===//
 
@@ -8,9 +8,12 @@
 #include "llvm/DerivedTypes.h"
 #include "llvm/Method.h"
 
-CallInst::CallInst(Method *m, vector<Value*> &params, 
+CallInst::CallInst(Method *M, vector<Value*> &params, 
                    const string &Name) 
-  : Instruction(m->getReturnType(), Instruction::Call, Name), M(m, this) {
+  : Instruction(M->getReturnType(), Instruction::Call, Name) {
+
+  Operands.reserve(1+params.size());
+  Operands.push_back(Use(M, this));
 
   const MethodType* MT = M->getMethodType();
   const MethodType::ParamTypes &PL = MT->getParamTypes();
@@ -19,29 +22,15 @@
   MethodType::ParamTypes::const_iterator It = PL.begin();
 #endif
   for (unsigned i = 0; i < params.size(); i++) {
-    assert(*It++ == params[i]->getType());
-    Params.push_back(Use(params[i], this));
+    assert(*It++ == params[i]->getType() && "Call Operands not correct type!");
+    Operands.push_back(Use(params[i], this));
   }
 }
 
 CallInst::CallInst(const CallInst &CI) 
-  : Instruction(CI.getType(), Instruction::Call), M(CI.M, this) {
-  for (unsigned i = 0; i < CI.Params.size(); i++)
-    Params.push_back(Use(CI.Params[i], this));
+  : Instruction(CI.getType(), Instruction::Call) {
+  Operands.reserve(CI.Operands.size());
+  for (unsigned i = 0; i < CI.Operands.size(); ++i)
+    Operands.push_back(Use(CI.Operands[i], this));
 }
 
-void CallInst::dropAllReferences() { 
-  M = 0;
-  Params.clear(); 
-}
-
-bool CallInst::setOperand(unsigned i, Value *Val) {
-  if (i > Params.size()) return false;
-  if (i == 0) {
-    M = Val->castMethodAsserting();
-  } else {
-    // TODO: assert = method arg type
-    Params[i-1] = Val;
-  }
-  return true;
-}
diff --git a/lib/VMCore/iReturn.cpp b/lib/VMCore/iReturn.cpp
index 7fa04fb..b28b04f 100644
--- a/lib/VMCore/iReturn.cpp
+++ b/lib/VMCore/iReturn.cpp
@@ -1,25 +1,3 @@
-//===-- iReturn.cpp - Implement the Return instruction -----------*- C++ -*--=//
-//
-// This file implements the Return instruction...
-//
-//===----------------------------------------------------------------------===//
 
-#include "llvm/iTerminators.h"
 
-ReturnInst::ReturnInst(Value *V)
-  : TerminatorInst(Instruction::Ret), Val(V, this) {
-}
 
-ReturnInst::ReturnInst(const ReturnInst &RI)
-  : TerminatorInst(Instruction::Ret), Val(RI.Val, this) {
-}
-
-void ReturnInst::dropAllReferences() {
-  Val = 0;
-}
-
-bool ReturnInst::setOperand(unsigned i, Value *V) { 
-  if (i) return false; 
-  Val = V;
-  return true;
-}
diff --git a/lib/VMCore/iSwitch.cpp b/lib/VMCore/iSwitch.cpp
index 2a1eec2..e5e3c50 100644
--- a/lib/VMCore/iSwitch.cpp
+++ b/lib/VMCore/iSwitch.cpp
@@ -10,72 +10,24 @@
 #include "llvm/Type.h"
 #endif
 
-SwitchInst::SwitchInst(Value *V, BasicBlock *DefV) 
-  : TerminatorInst(Instruction::Switch), 
-    DefaultDest(DefV, this), Val(V, this) {
-  assert(Val && DefV);
+SwitchInst::SwitchInst(Value *V, BasicBlock *DefDest) 
+  : TerminatorInst(Instruction::Switch) {
+  assert(V && DefDest);
+  Operands.push_back(Use(V, this));
+  Operands.push_back(Use(DefDest, this));
 }
 
 SwitchInst::SwitchInst(const SwitchInst &SI) 
-  : TerminatorInst(Instruction::Switch), DefaultDest(SI.DefaultDest), 
-    Val(SI.Val) {
+  : TerminatorInst(Instruction::Switch) {
+  Operands.reserve(SI.Operands.size());
 
-  for (dest_const_iterator I = SI.Destinations.begin(), 
-	                 end = SI.Destinations.end(); I != end; ++I)
-    Destinations.push_back(dest_value(ConstPoolUse(I->first, this), 
-				      BasicBlockUse(I->second, this)));
+  for (unsigned i = 0, E = SI.Operands.size(); i != E; i+=2) {
+    Operands.push_back(Use(SI.Operands[i], this));
+    Operands.push_back(Use(SI.Operands[i+1], this));
+  }
 }
 
-
 void SwitchInst::dest_push_back(ConstPoolVal *OnVal, BasicBlock *Dest) {
-  Destinations.push_back(dest_value(ConstPoolUse(OnVal, this), 
-                                    BasicBlockUse(Dest, this)));
-}
-
-void SwitchInst::dropAllReferences() {
-  Val = 0;
-  DefaultDest = 0;
-  Destinations.clear();
-}
-
-const BasicBlock *SwitchInst::getSuccessor(unsigned idx) const {
-  if (idx == 0) return DefaultDest;
-  if (idx > Destinations.size()) return 0;
-  return Destinations[idx-1].second;
-}
-
-unsigned SwitchInst::getNumOperands() const {
-  return 2+Destinations.size();
-}
-
-const Value *SwitchInst::getOperand(unsigned i) const {
-  if (i == 0) return Val;
-  else if (i == 1) return DefaultDest;
-
-  unsigned slot = (i-2) >> 1;
-  if (slot >= Destinations.size()) return 0;
-  
-  if (i & 1) return Destinations[slot].second;
-  return Destinations[slot].first;
-}
-
-bool SwitchInst::setOperand(unsigned i, Value *V) {
-  if (i == 0) { Val = V; return true; }
-  else if (i == 1) { 
-    assert(V->getType() == Type::LabelTy); 
-    DefaultDest = (BasicBlock*)V;
-    return true; 
-  }
-
-  unsigned slot = (i-2) >> 1;
-  if (slot >= Destinations.size()) return 0;
-
-  if (i & 1) {
-    assert(V->getType() == Type::LabelTy);
-    Destinations[slot].second = (BasicBlock*)V;
-  } else {
-    // TODO: assert constant
-    Destinations[slot].first = (ConstPoolVal*)V;
-  }
-  return true;
+  Operands.push_back(Use(OnVal, this));
+  Operands.push_back(Use(Dest, this));
 }