For PR1195:
[oota-llvm.git] / lib / Bytecode / Reader / Reader.cpp
index 355fc1df4c0cdd3b8c2a88a6f0084354c9d968a8..200f0d7a2fe3fa1080ea0eec02b792e71510484a 100644 (file)
 #include "llvm/Constants.h"
 #include "llvm/InlineAsm.h"
 #include "llvm/Instructions.h"
-#include "llvm/SymbolTable.h"
 #include "llvm/TypeSymbolTable.h"
 #include "llvm/Bytecode/Format.h"
 #include "llvm/Config/alloca.h"
 #include "llvm/Support/GetElementPtrTypeIterator.h"
-#include "llvm/Support/Compressor.h"
 #include "llvm/Support/MathExtras.h"
+#include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringExtras.h"
 #include <sstream>
 #include <algorithm>
@@ -54,6 +53,7 @@ namespace {
 inline void BytecodeReader::error(const std::string& err) {
   ErrorMsg = err + " (Vers=" + itostr(RevisionNum) + ", Pos=" 
     + itostr(At-MemStart) + ")";
+  if (Handler) Handler->handleError(ErrorMsg);
   longjmp(context,1);
 }
 
@@ -85,7 +85,6 @@ inline unsigned BytecodeReader::read_uint() {
 inline unsigned BytecodeReader::read_vbr_uint() {
   unsigned Shift = 0;
   unsigned Result = 0;
-  BufPtr Save = At;
 
   do {
     if (At == BlockEnd)
@@ -93,7 +92,6 @@ inline unsigned BytecodeReader::read_vbr_uint() {
     Result |= (unsigned)((*At++) & 0x7F) << Shift;
     Shift += 7;
   } while (At[-1] & 0x80);
-  if (Handler) Handler->handleVBR32(At-Save);
   return Result;
 }
 
@@ -101,7 +99,6 @@ inline unsigned BytecodeReader::read_vbr_uint() {
 inline uint64_t BytecodeReader::read_vbr_uint64() {
   unsigned Shift = 0;
   uint64_t Result = 0;
-  BufPtr Save = At;
 
   do {
     if (At == BlockEnd)
@@ -109,7 +106,6 @@ inline uint64_t BytecodeReader::read_vbr_uint64() {
     Result |= (uint64_t)((*At++) & 0x7F) << Shift;
     Shift += 7;
   } while (At[-1] & 0x80);
-  if (Handler) Handler->handleVBR64(At-Save);
   return Result;
 }
 
@@ -136,6 +132,17 @@ inline std::string BytecodeReader::read_str() {
   return std::string((char*)OldAt, Size);
 }
 
+void BytecodeReader::read_str(SmallVectorImpl<char> &StrData) {
+  StrData.clear();
+  unsigned Size = read_vbr_uint();
+  const unsigned char *OldAt = At;
+  At += Size;
+  if (At > BlockEnd)             // Size invalid?
+    error("Ran out of data reading a string!");
+  StrData.append(OldAt, At);
+}
+
+
 /// Read an arbitrary block of data
 inline void BytecodeReader::read_data(void *Ptr, void *End) {
   unsigned char *Start = (unsigned char *)Ptr;
@@ -186,8 +193,8 @@ inline bool BytecodeReader::hasImplicitNull(unsigned TyID) {
   return TyID != Type::LabelTyID && TyID != Type::VoidTyID;
 }
 
-/// Obtain a type given a typeid and account for things like compaction tables,
-/// function level vs module level, and the offsetting for the primitive types.
+/// Obtain a type given a typeid and account for things like function level vs 
+/// module level, and the offsetting for the primitive types.
 const Type *BytecodeReader::getType(unsigned ID) {
   if (ID <= Type::LastPrimitiveTyID)
     if (const Type *T = Type::getPrimitiveType((Type::TypeID)ID))
@@ -196,12 +203,6 @@ const Type *BytecodeReader::getType(unsigned ID) {
   // Otherwise, derived types need offset...
   ID -= Type::FirstDerivedTyID;
 
-  if (!CompactionTypes.empty()) {
-    if (ID >= CompactionTypes.size())
-      error("Type ID out of range for compaction table!");
-    return CompactionTypes[ID].first;
-  }
-
   // Is it a module-level type?
   if (ID < ModuleTypes.size())
     return ModuleTypes[ID].get();
@@ -223,20 +224,11 @@ inline const Type* BytecodeReader::readType() {
 }
 
 /// Get the slot number associated with a type accounting for primitive
-/// types, compaction tables, and function level vs module level.
+/// types and function level vs module level.
 unsigned BytecodeReader::getTypeSlot(const Type *Ty) {
   if (Ty->isPrimitiveType())
     return Ty->getTypeID();
 
-  // Scan the compaction table for the type if needed.
-  if (!CompactionTypes.empty()) {
-    for (unsigned i = 0, e = CompactionTypes.size(); i != e; ++i)
-      if (CompactionTypes[i].first == Ty)
-        return Type::FirstDerivedTyID + i;
-
-    error("Couldn't find type specified in compaction table!");
-  }
-
   // Check the function level types first...
   TypeListTy::iterator I = std::find(FunctionTypes.begin(),
                                      FunctionTypes.end(), Ty);
@@ -266,86 +258,30 @@ unsigned BytecodeReader::getTypeSlot(const Type *Ty) {
   return Type::FirstDerivedTyID + IT->second;
 }
 
-/// This is just like getType, but when a compaction table is in use, it is
-/// ignored.  It also ignores function level types.
-/// @see getType
-const Type *BytecodeReader::getGlobalTableType(unsigned Slot) {
-  if (Slot < Type::FirstDerivedTyID) {
-    const Type *Ty = Type::getPrimitiveType((Type::TypeID)Slot);
-    if (!Ty)
-      error("Not a primitive type ID?");
-    return Ty;
-  }
-  Slot -= Type::FirstDerivedTyID;
-  if (Slot >= ModuleTypes.size())
-    error("Illegal compaction table type reference!");
-  return ModuleTypes[Slot];
-}
-
-/// This is just like getTypeSlot, but when a compaction table is in use, it
-/// is ignored. It also ignores function level types.
-unsigned BytecodeReader::getGlobalTableTypeSlot(const Type *Ty) {
-  if (Ty->isPrimitiveType())
-    return Ty->getTypeID();
-  
-  // If we don't have our cache yet, build it now.
-  if (ModuleTypeIDCache.empty()) {
-    unsigned N = 0;
-    ModuleTypeIDCache.reserve(ModuleTypes.size());
-    for (TypeListTy::iterator I = ModuleTypes.begin(), E = ModuleTypes.end();
-         I != E; ++I, ++N)
-      ModuleTypeIDCache.push_back(std::make_pair(*I, N));
-    
-    std::sort(ModuleTypeIDCache.begin(), ModuleTypeIDCache.end());
-  }
-  
-  // Binary search the cache for the entry.
-  std::vector<std::pair<const Type*, unsigned> >::iterator IT =
-    std::lower_bound(ModuleTypeIDCache.begin(), ModuleTypeIDCache.end(),
-                     std::make_pair(Ty, 0U));
-  if (IT == ModuleTypeIDCache.end() || IT->first != Ty)
-    error("Didn't find type in ModuleTypes.");
-  
-  return Type::FirstDerivedTyID + IT->second;
-}
-
 /// Retrieve a value of a given type and slot number, possibly creating
 /// it if it doesn't already exist.
 Value * BytecodeReader::getValue(unsigned type, unsigned oNum, bool Create) {
   assert(type != Type::LabelTyID && "getValue() cannot get blocks!");
   unsigned Num = oNum;
 
-  // If there is a compaction table active, it defines the low-level numbers.
-  // If not, the module values define the low-level numbers.
-  if (CompactionValues.size() > type && !CompactionValues[type].empty()) {
-    if (Num < CompactionValues[type].size())
-      return CompactionValues[type][Num];
-    Num -= CompactionValues[type].size();
-  } else {
-    // By default, the global type id is the type id passed in
-    unsigned GlobalTyID = type;
-
-    // If the type plane was compactified, figure out the global type ID by
-    // adding the derived type ids and the distance.
-    if (!CompactionTypes.empty() && type >= Type::FirstDerivedTyID)
-      GlobalTyID = CompactionTypes[type-Type::FirstDerivedTyID].second;
-
-    if (hasImplicitNull(GlobalTyID)) {
-      const Type *Ty = getType(type);
-      if (!isa<OpaqueType>(Ty)) {
-        if (Num == 0)
-          return Constant::getNullValue(Ty);
-        --Num;
-      }
-    }
+  // By default, the global type id is the type id passed in
+  unsigned GlobalTyID = type;
 
-    if (GlobalTyID < ModuleValues.size() && ModuleValues[GlobalTyID]) {
-      if (Num < ModuleValues[GlobalTyID]->size())
-        return ModuleValues[GlobalTyID]->getOperand(Num);
-      Num -= ModuleValues[GlobalTyID]->size();
+  if (hasImplicitNull(GlobalTyID)) {
+    const Type *Ty = getType(type);
+    if (!isa<OpaqueType>(Ty)) {
+      if (Num == 0)
+        return Constant::getNullValue(Ty);
+      --Num;
     }
   }
 
+  if (GlobalTyID < ModuleValues.size() && ModuleValues[GlobalTyID]) {
+    if (Num < ModuleValues[GlobalTyID]->size())
+      return ModuleValues[GlobalTyID]->getOperand(Num);
+    Num -= ModuleValues[GlobalTyID]->size();
+  }
+
   if (FunctionValues.size() > type &&
       FunctionValues[type] &&
       Num < FunctionValues[type]->size())
@@ -370,38 +306,6 @@ Value * BytecodeReader::getValue(unsigned type, unsigned oNum, bool Create) {
   return 0; // just silence warning, error calls longjmp
 }
 
-/// This is just like getValue, but when a compaction table is in use, it
-/// is ignored.  Also, no forward references or other fancy features are
-/// supported.
-Value* BytecodeReader::getGlobalTableValue(unsigned TyID, unsigned SlotNo) {
-  if (SlotNo == 0)
-    return Constant::getNullValue(getType(TyID));
-
-  if (!CompactionTypes.empty() && TyID >= Type::FirstDerivedTyID) {
-    TyID -= Type::FirstDerivedTyID;
-    if (TyID >= CompactionTypes.size())
-      error("Type ID out of range for compaction table!");
-    TyID = CompactionTypes[TyID].second;
-  }
-
-  --SlotNo;
-
-  if (TyID >= ModuleValues.size() || ModuleValues[TyID] == 0 ||
-      SlotNo >= ModuleValues[TyID]->size()) {
-    if (TyID >= ModuleValues.size() || ModuleValues[TyID] == 0)
-      error("Corrupt compaction table entry!"
-            + utostr(TyID) + ", " + utostr(SlotNo) + ": "
-            + utostr(ModuleValues.size()));
-    else
-      error("Corrupt compaction table entry!"
-            + utostr(TyID) + ", " + utostr(SlotNo) + ": "
-            + utostr(ModuleValues.size()) + ", "
-            + utohexstr(reinterpret_cast<uint64_t>(((void*)ModuleValues[TyID])))
-            + ", "
-            + utostr(ModuleValues[TyID]->size()));
-  }
-  return ModuleValues[TyID]->getOperand(SlotNo);
-}
 
 /// Just like getValue, except that it returns a null pointer
 /// only on error.  It always returns a constant (meaning that if the value is
@@ -468,7 +372,7 @@ void BytecodeReader::insertArguments(Function* F) {
 /// This method parses a single instruction. The instruction is
 /// inserted at the end of the \p BB provided. The arguments of
 /// the instruction are provided in the \p Oprnds vector.
-void BytecodeReader::ParseInstruction(std::vector<unsigned> &Oprnds,
+void BytecodeReader::ParseInstruction(SmallVector<unsigned, 8> &Oprnds,
                                       BasicBlock* BB) {
   BufPtr SaveAt = At;
 
@@ -545,10 +449,6 @@ void BytecodeReader::ParseInstruction(std::vector<unsigned> &Oprnds,
   // of opcodes.
   Instruction* Result = 0;
 
-  // We have enough info to inform the handler now.
-  if (Handler) 
-    Handler->handleInstruction(Opcode, InstTy, Oprnds, At-SaveAt);
-
   // First, handle the easy binary operators case
   if (Opcode >= Instruction::BinaryOpsBegin &&
       Opcode <  Instruction::BinaryOpsEnd  && Oprnds.size() == 2) {
@@ -582,12 +482,12 @@ void BytecodeReader::ParseInstruction(std::vector<unsigned> &Oprnds,
       break;
     }
     case Instruction::InsertElement: {
-      const PackedType *PackedTy = dyn_cast<PackedType>(InstTy);
-      if (!PackedTy || Oprnds.size() != 3)
+      const VectorType *VectorTy = dyn_cast<VectorType>(InstTy);
+      if (!VectorTy || Oprnds.size() != 3)
         error("Invalid insertelement instruction!");
       
       Value *V1 = getValue(iType, Oprnds[0]);
-      Value *V2 = getValue(getTypeSlot(PackedTy->getElementType()),Oprnds[1]);
+      Value *V2 = getValue(getTypeSlot(VectorTy->getElementType()),Oprnds[1]);
       Value *V3 = getValue(Int32TySlot, Oprnds[2]);
         
       if (!InsertElementInst::isValidOperands(V1, V2, V3))
@@ -596,13 +496,13 @@ void BytecodeReader::ParseInstruction(std::vector<unsigned> &Oprnds,
       break;
     }
     case Instruction::ShuffleVector: {
-      const PackedType *PackedTy = dyn_cast<PackedType>(InstTy);
-      if (!PackedTy || Oprnds.size() != 3)
+      const VectorType *VectorTy = dyn_cast<VectorType>(InstTy);
+      if (!VectorTy || Oprnds.size() != 3)
         error("Invalid shufflevector instruction!");
       Value *V1 = getValue(iType, Oprnds[0]);
       Value *V2 = getValue(iType, Oprnds[1]);
-      const PackedType *EltTy = 
-        PackedType::get(Type::Int32Ty, PackedTy->getNumElements());
+      const VectorType *EltTy = 
+        VectorType::get(Type::Int32Ty, VectorTy->getNumElements());
       Value *V3 = getValue(getTypeSlot(EltTy), Oprnds[2]);
       if (!ShuffleVectorInst::isValidOperands(V1, V2, V3))
         error("Invalid shufflevector instruction!");
@@ -709,13 +609,6 @@ void BytecodeReader::ParseInstruction(std::vector<unsigned> &Oprnds,
           static_cast<unsigned short>(Oprnds[2]),
           getValue(iType, Oprnds[0]), getValue(iType, Oprnds[1]));
       break;
-    case Instruction::Shl:
-    case Instruction::LShr:
-    case Instruction::AShr:
-      Result = new ShiftInst(Instruction::OtherOps(Opcode),
-                             getValue(iType, Oprnds[0]),
-                             getValue(Int8TySlot, Oprnds[1]));
-      break;
     case Instruction::Ret:
       if (Oprnds.size() == 0)
         Result = new ReturnInst();
@@ -776,7 +669,7 @@ void BytecodeReader::ParseInstruction(std::vector<unsigned> &Oprnds,
       const FunctionType *FTy = dyn_cast<FunctionType>(PTy->getElementType());
       if (FTy == 0) error("Call to non function pointer value!");
 
-      std::vector<Value *> Params;
+      SmallVector<Value *, 8> Params;
       if (!FTy->isVarArg()) {
         FunctionType::param_iterator It = FTy->param_begin();
 
@@ -809,7 +702,7 @@ void BytecodeReader::ParseInstruction(std::vector<unsigned> &Oprnds,
           Params.push_back(getValue(Oprnds[i], Oprnds[i+1]));
       }
 
-      Result = new CallInst(F, Params);
+      Result = new CallInst(F, &Params[0], Params.size());
       if (isTailCall) cast<CallInst>(Result)->setTailCall();
       if (CallingConv) cast<CallInst>(Result)->setCallingConv(CallingConv);
       break;
@@ -827,7 +720,7 @@ void BytecodeReader::ParseInstruction(std::vector<unsigned> &Oprnds,
       if (FTy == 0)
         error("Invoke to non function pointer value!");
 
-      std::vector<Value *> Params;
+      SmallVector<Value *, 8> Params;
       BasicBlock *Normal, *Except;
       unsigned CallingConv = Oprnds.back();
       Oprnds.pop_back();
@@ -863,7 +756,7 @@ void BytecodeReader::ParseInstruction(std::vector<unsigned> &Oprnds,
           Params.push_back(getValue(Oprnds[i], Oprnds[i+1]));
       }
 
-      Result = new InvokeInst(F, Normal, Except, Params);
+      Result = new InvokeInst(F, Normal, Except, &Params[0], Params.size());
       if (CallingConv) cast<InvokeInst>(Result)->setCallingConv(CallingConv);
       break;
     }
@@ -902,7 +795,7 @@ void BytecodeReader::ParseInstruction(std::vector<unsigned> &Oprnds,
       if (Oprnds.size() == 0 || !isa<PointerType>(InstTy))
         error("Invalid getelementptr instruction!");
 
-      std::vector<Value*> Idx;
+      SmallVector<Value*, 8> Idx;
 
       const Type *NextTy = InstTy;
       for (unsigned i = 1, e = Oprnds.size(); i != e; ++i) {
@@ -926,10 +819,12 @@ void BytecodeReader::ParseInstruction(std::vector<unsigned> &Oprnds,
           ValIdx >>= 1;
         }
         Idx.push_back(getValue(IdxTy, ValIdx));
-        NextTy = GetElementPtrInst::getIndexedType(InstTy, Idx, true);
+        NextTy = GetElementPtrInst::getIndexedType(InstTy, &Idx[0], Idx.size(),
+                                                   true);
       }
 
-      Result = new GetElementPtrInst(getValue(iType, Oprnds[0]), Idx);
+      Result = new GetElementPtrInst(getValue(iType, Oprnds[0]),
+                                     &Idx[0], Idx.size());
       break;
     }
     case 62:   // volatile load
@@ -968,6 +863,11 @@ void BytecodeReader::ParseInstruction(std::vector<unsigned> &Oprnds,
   else
     TypeSlot = getTypeSlot(Result->getType());
 
+  // We have enough info to inform the handler now.
+  if (Handler) 
+    Handler->handleInstruction(Opcode, InstTy, &Oprnds[0], Oprnds.size(),
+                               Result, At-SaveAt);
+
   insertValue(Result, TypeSlot, FunctionValues);
 }
 
@@ -997,7 +897,7 @@ BasicBlock *BytecodeReader::getBasicBlock(unsigned ID) {
 /// @returns the number of basic blocks encountered.
 unsigned BytecodeReader::ParseInstructionList(Function* F) {
   unsigned BlockNo = 0;
-  std::vector<unsigned> Args;
+  SmallVector<unsigned, 8> Args;
 
   while (moreInBlock()) {
     if (Handler) Handler->handleBasicBlockBegin(BlockNo);
@@ -1043,17 +943,19 @@ void BytecodeReader::ParseTypeSymbolTable(TypeSymbolTable *TST) {
 /// CurrentFunction's symbol table. For Module level symbol tables, the
 /// CurrentFunction argument must be zero.
 void BytecodeReader::ParseValueSymbolTable(Function *CurrentFunction,
-                                           SymbolTable *ST) {
+                                           ValueSymbolTable *VST) {
                                       
-  if (Handler) Handler->handleSymbolTableBegin(CurrentFunction,ST);
+  if (Handler) Handler->handleValueSymbolTableBegin(CurrentFunction,VST);
 
   // Allow efficient basic block lookup by number.
-  std::vector<BasicBlock*> BBMap;
+  SmallVector<BasicBlock*, 32> BBMap;
   if (CurrentFunction)
     for (Function::iterator I = CurrentFunction->begin(),
            E = CurrentFunction->end(); I != E; ++I)
       BBMap.push_back(I);
 
+  SmallVector<char, 32> NameStr;
+  
   while (moreInBlock()) {
     // Symtab block header: [num entries][type id number]
     unsigned NumEntries = read_vbr_uint();
@@ -1062,91 +964,26 @@ void BytecodeReader::ParseValueSymbolTable(Function *CurrentFunction,
     for (unsigned i = 0; i != NumEntries; ++i) {
       // Symtab entry: [def slot #][name]
       unsigned slot = read_vbr_uint();
-      std::string Name = read_str();
+      read_str(NameStr);
       Value *V = 0;
       if (Typ == LabelTySlot) {
-        if (slot < BBMap.size())
-          V = BBMap[slot];
+        V = (slot < BBMap.size()) ? BBMap[slot] : 0;
       } else {
-        V = getValue(Typ, slot, false); // Find mapping...
+        V = getValue(Typ, slot, false); // Find mapping.
       }
+      if (Handler) Handler->handleSymbolTableValue(Typ, slot,
+                                                   &NameStr[0], NameStr.size());
       if (V == 0)
-        error("Failed value look-up for name '" + Name + "'");
-      V->setName(Name);
+        error("Failed value look-up for name '" + 
+              std::string(NameStr.begin(), NameStr.end()) + "', type #" + 
+              utostr(Typ) + " slot #" + utostr(slot));
+      V->setName(&NameStr[0], NameStr.size());
+      
+      NameStr.clear();
     }
   }
   checkPastBlockEnd("Symbol Table");
-  if (Handler) Handler->handleSymbolTableEnd();
-}
-
-/// Read in the types portion of a compaction table.
-void BytecodeReader::ParseCompactionTypes(unsigned NumEntries) {
-  for (unsigned i = 0; i != NumEntries; ++i) {
-    unsigned TypeSlot = read_vbr_uint();
-    const Type *Typ = getGlobalTableType(TypeSlot);
-    CompactionTypes.push_back(std::make_pair(Typ, TypeSlot));
-    if (Handler) Handler->handleCompactionTableType(i, TypeSlot, Typ);
-  }
-}
-
-/// Parse a compaction table.
-void BytecodeReader::ParseCompactionTable() {
-
-  // Notify handler that we're beginning a compaction table.
-  if (Handler) Handler->handleCompactionTableBegin();
-
-  // Get the types for the compaction table.
-  unsigned NumEntries = read_vbr_uint();
-  ParseCompactionTypes(NumEntries);
-
-  // Compaction tables live in separate blocks so we have to loop
-  // until we've read the whole thing.
-  while (moreInBlock()) {
-    // Read the number of Value* entries in the compaction table
-    unsigned NumEntries = read_vbr_uint();
-    unsigned Ty = 0;
-
-    // Decode the type from value read in. Most compaction table
-    // planes will have one or two entries in them. If that's the
-    // case then the length is encoded in the bottom two bits and
-    // the higher bits encode the type. This saves another VBR value.
-    if ((NumEntries & 3) == 3) {
-      // In this case, both low-order bits are set (value 3). This
-      // is a signal that the typeid follows.
-      NumEntries >>= 2;
-      Ty = read_vbr_uint();
-    } else {
-      // In this case, the low-order bits specify the number of entries
-      // and the high order bits specify the type.
-      Ty = NumEntries >> 2;
-      NumEntries &= 3;
-    }
-
-    // Make sure we have enough room for the plane.
-    if (Ty >= CompactionValues.size())
-      CompactionValues.resize(Ty+1);
-
-    // Make sure the plane is empty or we have some kind of error.
-    if (!CompactionValues[Ty].empty())
-      error("Compaction table plane contains multiple entries!");
-
-    // Notify handler about the plane.
-    if (Handler) Handler->handleCompactionTablePlane(Ty, NumEntries);
-
-    // Push the implicit zero.
-    CompactionValues[Ty].push_back(Constant::getNullValue(getType(Ty)));
-
-    // Read in each of the entries, put them in the compaction table
-    // and notify the handler that we have a new compaction table value.
-    for (unsigned i = 0; i != NumEntries; ++i) {
-      unsigned ValSlot = read_vbr_uint();
-      Value *V = getGlobalTableValue(Ty, ValSlot);
-      CompactionValues[Ty].push_back(V);
-      if (Handler) Handler->handleCompactionTableValue(i, Ty, ValSlot);
-    }
-  }
-  // Notify handler that the compaction table is done.
-  if (Handler) Handler->handleCompactionTableEnd();
+  if (Handler) Handler->handleValueSymbolTableEnd();
 }
 
 // Parse a single type. The typeid is read in first. If its a primitive type
@@ -1192,10 +1029,10 @@ const Type *BytecodeReader::ParseType() {
     Result =  ArrayType::get(ElementType, NumElements);
     break;
   }
-  case Type::PackedTyID: {
+  case Type::VectorTyID: {
     const Type *ElementType = readType();
     unsigned NumElements = read_vbr_uint();
-    Result =  PackedType::get(ElementType, NumElements);
+    Result =  VectorType::get(ElementType, NumElements);
     break;
   }
   case Type::StructTyID: {
@@ -1321,7 +1158,7 @@ Value *BytecodeReader::ParseConstantPoolValue(unsigned TypeID) {
     --isExprNumArgs;
 
     // FIXME: Encoding of constant exprs could be much more compact!
-    std::vector<Constant*> ArgVec;
+    SmallVector<Constant*, 8> ArgVec;
     ArgVec.reserve(isExprNumArgs);
     unsigned Opcode = read_vbr_uint();
 
@@ -1341,26 +1178,30 @@ Value *BytecodeReader::ParseConstantPoolValue(unsigned TypeID) {
 
       Constant *Result = ConstantExpr::getCast(Opcode, ArgVec[0], 
                                                getType(TypeID));
-      if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result);
+      if (Handler) Handler->handleConstantExpression(Opcode, &ArgVec[0],
+                                                     ArgVec.size(), Result);
       return Result;
     } else if (Opcode == Instruction::GetElementPtr) { // GetElementPtr
-      std::vector<Constant*> IdxList(ArgVec.begin()+1, ArgVec.end());
-      Constant *Result = ConstantExpr::getGetElementPtr(ArgVec[0], IdxList);
-      if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result);
+      Constant *Result = ConstantExpr::getGetElementPtr(ArgVec[0], &ArgVec[1],
+                                                        ArgVec.size()-1);
+      if (Handler) Handler->handleConstantExpression(Opcode, &ArgVec[0],
+                                                     ArgVec.size(), Result);
       return Result;
     } else if (Opcode == Instruction::Select) {
       if (ArgVec.size() != 3)
         error("Select instruction must have three arguments.");
       Constant* Result = ConstantExpr::getSelect(ArgVec[0], ArgVec[1],
                                                  ArgVec[2]);
-      if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result);
+      if (Handler) Handler->handleConstantExpression(Opcode, &ArgVec[0],
+                                                     ArgVec.size(), Result);
       return Result;
     } else if (Opcode == Instruction::ExtractElement) {
       if (ArgVec.size() != 2 ||
           !ExtractElementInst::isValidOperands(ArgVec[0], ArgVec[1]))
         error("Invalid extractelement constand expr arguments");
       Constant* Result = ConstantExpr::getExtractElement(ArgVec[0], ArgVec[1]);
-      if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result);
+      if (Handler) Handler->handleConstantExpression(Opcode, &ArgVec[0],
+                                                     ArgVec.size(), Result);
       return Result;
     } else if (Opcode == Instruction::InsertElement) {
       if (ArgVec.size() != 3 ||
@@ -1369,7 +1210,8 @@ Value *BytecodeReader::ParseConstantPoolValue(unsigned TypeID) {
         
       Constant *Result = 
         ConstantExpr::getInsertElement(ArgVec[0], ArgVec[1], ArgVec[2]);
-      if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result);
+      if (Handler) Handler->handleConstantExpression(Opcode, &ArgVec[0],
+                                                     ArgVec.size(), Result);
       return Result;
     } else if (Opcode == Instruction::ShuffleVector) {
       if (ArgVec.size() != 3 ||
@@ -1377,25 +1219,29 @@ Value *BytecodeReader::ParseConstantPoolValue(unsigned TypeID) {
         error("Invalid shufflevector constant expr arguments.");
       Constant *Result = 
         ConstantExpr::getShuffleVector(ArgVec[0], ArgVec[1], ArgVec[2]);
-      if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result);
+      if (Handler) Handler->handleConstantExpression(Opcode, &ArgVec[0],
+                                                     ArgVec.size(), Result);
       return Result;
     } else if (Opcode == Instruction::ICmp) {
       if (ArgVec.size() != 2) 
         error("Invalid ICmp constant expr arguments.");
       unsigned predicate = read_vbr_uint();
       Constant *Result = ConstantExpr::getICmp(predicate, ArgVec[0], ArgVec[1]);
-      if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result);
+      if (Handler) Handler->handleConstantExpression(Opcode, &ArgVec[0],
+                                                     ArgVec.size(), Result);
       return Result;
     } else if (Opcode == Instruction::FCmp) {
       if (ArgVec.size() != 2) 
         error("Invalid FCmp constant expr arguments.");
       unsigned predicate = read_vbr_uint();
       Constant *Result = ConstantExpr::getFCmp(predicate, ArgVec[0], ArgVec[1]);
-      if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result);
+      if (Handler) Handler->handleConstantExpression(Opcode, &ArgVec[0], 
+                                                     ArgVec.size(), Result);
       return Result;
     } else {                            // All other 2-operand expressions
       Constant* Result = ConstantExpr::get(Opcode, ArgVec[0], ArgVec[1]);
-      if (Handler) Handler->handleConstantExpression(Opcode, ArgVec, Result);
+      if (Handler) Handler->handleConstantExpression(Opcode, &ArgVec[0], 
+                                                     ArgVec.size(), Result);
       return Result;
     }
   }
@@ -1419,7 +1265,7 @@ Value *BytecodeReader::ParseConstantPoolValue(unsigned TypeID) {
       Result = ConstantInt::get(IT, Val);
       if (Handler) Handler->handleConstantValue(Result);
     } else 
-      assert("Integer types > 64 bits not supported");
+      assert(0 && "Integer types > 64 bits not supported");
     break;
   }
   case Type::FloatTyID: {
@@ -1448,7 +1294,8 @@ Value *BytecodeReader::ParseConstantPoolValue(unsigned TypeID) {
       Elements.push_back(getConstantValue(TypeSlot,
                                           read_vbr_uint()));
     Result = ConstantArray::get(AT, Elements);
-    if (Handler) Handler->handleConstantArray(AT, Elements, TypeSlot, Result);
+    if (Handler) Handler->handleConstantArray(AT, &Elements[0], Elements.size(),
+                                              TypeSlot, Result);
     break;
   }
 
@@ -1462,12 +1309,13 @@ Value *BytecodeReader::ParseConstantPoolValue(unsigned TypeID) {
                                           read_vbr_uint()));
 
     Result = ConstantStruct::get(ST, Elements);
-    if (Handler) Handler->handleConstantStruct(ST, Elements, Result);
+    if (Handler) Handler->handleConstantStruct(ST, &Elements[0],Elements.size(),
+                                               Result);
     break;
   }
 
-  case Type::PackedTyID: {
-    const PackedType *PT = cast<PackedType>(Ty);
+  case Type::VectorTyID: {
+    const VectorType *PT = cast<VectorType>(Ty);
     unsigned NumElements = PT->getNumElements();
     unsigned TypeSlot = getTypeSlot(PT->getElementType());
     std::vector<Constant*> Elements;
@@ -1475,8 +1323,9 @@ Value *BytecodeReader::ParseConstantPoolValue(unsigned TypeID) {
     while (NumElements--)     // Read all of the elements of the constant.
       Elements.push_back(getConstantValue(TypeSlot,
                                           read_vbr_uint()));
-    Result = ConstantPacked::get(PT, Elements);
-    if (Handler) Handler->handleConstantPacked(PT, Elements, TypeSlot, Result);
+    Result = ConstantVector::get(PT, Elements);
+    if (Handler) Handler->handleConstantVector(PT, &Elements[0],Elements.size(),
+                                               TypeSlot, Result);
     break;
   }
 
@@ -1667,8 +1516,7 @@ void BytecodeReader::ParseFunctionBody(Function* F) {
     case BytecodeFormat::ConstantPoolBlockID:
       if (!InsertedArguments) {
         // Insert arguments into the value table before we parse the first basic
-        // block in the function, but after we potentially read in the
-        // compaction table.
+        // block in the function
         insertArguments(F);
         InsertedArguments = true;
       }
@@ -1676,14 +1524,9 @@ void BytecodeReader::ParseFunctionBody(Function* F) {
       ParseConstantPool(FunctionValues, FunctionTypes, true);
       break;
 
-    case BytecodeFormat::CompactionTableBlockID:
-      ParseCompactionTable();
-      break;
-
     case BytecodeFormat::InstructionListBlockID: {
       // Insert arguments into the value table before we parse the instruction
-      // list for the function, but after we potentially read in the compaction
-      // table.
+      // list for the function
       if (!InsertedArguments) {
         insertArguments(F);
         InsertedArguments = true;
@@ -1732,8 +1575,6 @@ void BytecodeReader::ParseFunctionBody(Function* F) {
 
   // Clear out function-level types...
   FunctionTypes.clear();
-  CompactionTypes.clear();
-  CompactionValues.clear();
   freeTable(FunctionValues);
 
   if (Handler) Handler->handleFunctionEnd(F);
@@ -2013,14 +1854,14 @@ void BytecodeReader::ParseModuleGlobalInfo() {
   TheModule->setTargetTriple(triple);
   if (Handler)
     Handler->handleTargetTriple(triple);
-
+  
   // Read the data layout string and place into the module.
   std::string datalayout = read_str();
   TheModule->setDataLayout(datalayout);
   // FIXME: Implement
   // if (Handler)
     // Handler->handleDataLayout(datalayout);
-  
+
   if (At != BlockEnd) {
     // If the file has section info in it, read the section names now.
     unsigned NumSections = read_vbr_uint();
@@ -2052,31 +1893,14 @@ void BytecodeReader::ParseModuleGlobalInfo() {
 /// Parse the version information and decode it by setting flags on the
 /// Reader that enable backward compatibility of the reader.
 void BytecodeReader::ParseVersionInfo() {
-  unsigned Version = read_vbr_uint();
-
-  // Unpack version number: low four bits are for flags, top bits = version
-  Module::Endianness  Endianness;
-  Module::PointerSize PointerSize;
-  Endianness  = (Version & 1) ? Module::BigEndian : Module::LittleEndian;
-  PointerSize = (Version & 2) ? Module::Pointer64 : Module::Pointer32;
-
-  bool hasNoEndianness = Version & 4;
-  bool hasNoPointerSize = Version & 8;
-
-  RevisionNum = Version >> 4;
+  unsigned RevisionNum = read_vbr_uint();
 
   // We don't provide backwards compatibility in the Reader any more. To
   // upgrade, the user should use llvm-upgrade.
   if (RevisionNum < 7)
     error("Bytecode formats < 7 are no longer supported. Use llvm-upgrade.");
 
-  if (hasNoEndianness) Endianness  = Module::AnyEndianness;
-  if (hasNoPointerSize) PointerSize = Module::AnyPointerSize;
-
-  TheModule->setEndianness(Endianness);
-  TheModule->setPointerSize(PointerSize);
-
-  if (Handler) Handler->handleVersionInfo(RevisionNum, Endianness, PointerSize);
+  if (Handler) Handler->handleVersionInfo(RevisionNum);
 }
 
 /// Parse a whole module.
@@ -2173,6 +1997,7 @@ void BytecodeReader::ParseModule() {
 /// and \p Length parameters.
 bool BytecodeReader::ParseBytecode(volatile BufPtr Buf, unsigned Length,
                                    const std::string &ModuleID,
+                                   BCDecompressor_t *Decompressor, 
                                    std::string* ErrMsg) {
 
   /// We handle errors by
@@ -2207,13 +2032,16 @@ bool BytecodeReader::ParseBytecode(volatile BufPtr Buf, unsigned Length,
 
   // If this is a compressed file
   if (Sig == ('l' | ('l' << 8) | ('v' << 16) | ('c' << 24))) {
+    if (!Decompressor) {
+      error("Compressed bytecode found, but not decompressor available");
+    }
 
     // Invoke the decompression of the bytecode. Note that we have to skip the
     // file's magic number which is not part of the compressed block. Hence,
     // the Buf+4 and Length-4. The result goes into decompressedBlock, a data
     // member for retention until BytecodeReader is destructed.
-    unsigned decompressedLength = Compressor::decompressToNewBuffer(
-        (char*)Buf+4,Length-4,decompressedBlock);
+    unsigned decompressedLength = 
+      Decompressor((char*)Buf+4,Length-4,decompressedBlock, 0);
 
     // We must adjust the buffer pointers used by the bytecode reader to point
     // into the new decompressed block. After decompression, the