+static inline bool hasNullValue(unsigned TyID) {
+ return TyID != Type::LabelTyID && TyID != Type::TypeTyID &&
+ TyID != Type::VoidTyID;
+}
+
+/// getOrCreateCompactionTableSlot - This method is used to build up the initial
+/// approximation of the compaction table.
+unsigned SlotCalculator::getOrCreateCompactionTableSlot(const Value *V) {
+ if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(V))
+ V = CPR->getValue();
+ std::map<const Value*, unsigned>::iterator I =
+ CompactionNodeMap.lower_bound(V);
+ if (I != CompactionNodeMap.end() && I->first == V)
+ return I->second; // Already exists?
+
+ // Make sure the type is in the table.
+ unsigned Ty;
+ if (!CompactionTable[Type::TypeTyID].empty())
+ Ty = getOrCreateCompactionTableSlot(V->getType());
+ else // If the type plane was decompactified, use the global plane ID
+ Ty = getSlot(V->getType());
+ if (CompactionTable.size() <= Ty)
+ CompactionTable.resize(Ty+1);
+
+ assert(!isa<Type>(V) || ModuleLevel.empty());
+
+ TypePlane &TyPlane = CompactionTable[Ty];
+
+ // Make sure to insert the null entry if the thing we are inserting is not a
+ // null constant.
+ if (TyPlane.empty() && hasNullValue(V->getType()->getPrimitiveID())) {
+ Value *ZeroInitializer = Constant::getNullValue(V->getType());
+ if (V != ZeroInitializer) {
+ TyPlane.push_back(ZeroInitializer);
+ CompactionNodeMap[ZeroInitializer] = 0;
+ }
+ }
+
+ unsigned SlotNo = TyPlane.size();
+ TyPlane.push_back(V);
+ CompactionNodeMap.insert(std::make_pair(V, SlotNo));
+ return SlotNo;
+}
+
+
+/// buildCompactionTable - Since all of the function constants and types are
+/// stored in the module-level constant table, we don't need to emit a function
+/// constant table. Also due to this, the indices for various constants and
+/// types might be very large in large programs. In order to avoid blowing up
+/// the size of instructions in the bytecode encoding, we build a compaction
+/// table, which defines a mapping from function-local identifiers to global
+/// identifiers.
+void SlotCalculator::buildCompactionTable(const Function *F) {
+ assert(CompactionNodeMap.empty() && "Compaction table already built!");
+ // First step, insert the primitive types.
+ CompactionTable.resize(Type::TypeTyID+1);
+ for (unsigned i = 0; i != Type::FirstDerivedTyID; ++i) {
+ const Type *PrimTy = Type::getPrimitiveType((Type::PrimitiveID)i);
+ CompactionTable[Type::TypeTyID].push_back(PrimTy);
+ CompactionNodeMap[PrimTy] = i;
+ }
+
+ // Next, include any types used by function arguments.
+ for (Function::const_aiterator I = F->abegin(), E = F->aend(); I != E; ++I)
+ getOrCreateCompactionTableSlot(I->getType());
+
+ // Next, find all of the types and values that are referred to by the
+ // instructions in the program.
+ for (const_inst_iterator I = inst_begin(F), E = inst_end(F); I != E; ++I) {
+ getOrCreateCompactionTableSlot(I->getType());
+ for (unsigned op = 0, e = I->getNumOperands(); op != e; ++op)
+ if (isa<Constant>(I->getOperand(op)) ||
+ isa<GlobalValue>(I->getOperand(op)))
+ getOrCreateCompactionTableSlot(I->getOperand(op));
+ if (const VANextInst *VAN = dyn_cast<VANextInst>(&*I))
+ getOrCreateCompactionTableSlot(VAN->getArgType());
+ }
+
+ // Do the types in the symbol table
+ const SymbolTable &ST = F->getSymbolTable();
+ for (SymbolTable::type_const_iterator TI = ST.type_begin(),
+ TE = ST.type_end(); TI != TE; ++TI)
+ getOrCreateCompactionTableSlot(TI->second);
+
+ // Now do the constants and global values
+ for (SymbolTable::plane_const_iterator PI = ST.plane_begin(),
+ PE = ST.plane_end(); PI != PE; ++PI)
+ for (SymbolTable::value_const_iterator VI = PI->second.begin(),
+ VE = PI->second.end(); VI != VE; ++VI)
+ if (isa<Constant>(VI->second) || isa<GlobalValue>(VI->second))
+ getOrCreateCompactionTableSlot(VI->second);
+
+ // Now that we have all of the values in the table, and know what types are
+ // referenced, make sure that there is at least the zero initializer in any
+ // used type plane. Since the type was used, we will be emitting instructions
+ // to the plane even if there are no constants in it.
+ CompactionTable.resize(CompactionTable[Type::TypeTyID].size());
+ for (unsigned i = 0, e = CompactionTable.size(); i != e; ++i)
+ if (CompactionTable[i].empty() && i != Type::VoidTyID &&
+ i != Type::LabelTyID) {
+ const Type *Ty = cast<Type>(CompactionTable[Type::TypeTyID][i]);
+ getOrCreateCompactionTableSlot(Constant::getNullValue(Ty));
+ }
+
+ // Okay, now at this point, we have a legal compaction table. Since we want
+ // to emit the smallest possible binaries, do not compactify the type plane if
+ // it will not save us anything. Because we have not yet incorporated the
+ // function body itself yet, we don't know whether or not it's a good idea to
+ // compactify other planes. We will defer this decision until later.
+ TypePlane &GlobalTypes = Table[Type::TypeTyID];
+
+ // All of the values types will be scrunched to the start of the types plane
+ // of the global table. Figure out just how many there are.
+ assert(!GlobalTypes.empty() && "No global types???");
+ unsigned NumFCTypes = GlobalTypes.size()-1;
+ while (!cast<Type>(GlobalTypes[NumFCTypes])->isFirstClassType())
+ --NumFCTypes;
+
+ // If there are fewer that 64 types, no instructions will be exploded due to
+ // the size of the type operands. Thus there is no need to compactify types.
+ // Also, if the compaction table contains most of the entries in the global
+ // table, there really is no reason to compactify either.
+ if (NumFCTypes < 64) {
+ // Decompactifying types is tricky, because we have to move type planes all
+ // over the place. At least we don't need to worry about updating the
+ // CompactionNodeMap for non-types though.
+ std::vector<TypePlane> TmpCompactionTable;
+ std::swap(CompactionTable, TmpCompactionTable);
+ TypePlane Types;
+ std::swap(Types, TmpCompactionTable[Type::TypeTyID]);
+
+ // Move each plane back over to the uncompactified plane
+ while (!Types.empty()) {
+ const Type *Ty = cast<Type>(Types.back());
+ Types.pop_back();
+ CompactionNodeMap.erase(Ty); // Decompactify type!
+
+ if (Ty != Type::TypeTy) {
+ // Find the global slot number for this type.
+ int TySlot = getSlot(Ty);
+ assert(TySlot != -1 && "Type doesn't exist in global table?");
+
+ // Now we know where to put the compaction table plane.
+ if (CompactionTable.size() <= unsigned(TySlot))
+ CompactionTable.resize(TySlot+1);
+ // Move the plane back into the compaction table.
+ std::swap(CompactionTable[TySlot], TmpCompactionTable[Types.size()]);
+
+ // And remove the empty plane we just moved in.
+ TmpCompactionTable.pop_back();
+ }
+ }
+ }
+}
+
+
+/// pruneCompactionTable - Once the entire function being processed has been
+/// incorporated into the current compaction table, look over the compaction
+/// table and check to see if there are any values whose compaction will not
+/// save us any space in the bytecode file. If compactifying these values
+/// serves no purpose, then we might as well not even emit the compactification
+/// information to the bytecode file, saving a bit more space.
+///
+/// Note that the type plane has already been compactified if possible.
+///
+void SlotCalculator::pruneCompactionTable() {
+ TypePlane &TyPlane = CompactionTable[Type::TypeTyID];
+ for (unsigned ctp = 0, e = CompactionTable.size(); ctp != e; ++ctp)
+ if (ctp != Type::TypeTyID && !CompactionTable[ctp].empty()) {
+ TypePlane &CPlane = CompactionTable[ctp];
+ unsigned GlobalSlot = ctp;
+ if (!TyPlane.empty())
+ GlobalSlot = getGlobalSlot(TyPlane[ctp]);
+
+ if (GlobalSlot >= Table.size())
+ Table.resize(GlobalSlot+1);
+ TypePlane &GPlane = Table[GlobalSlot];
+
+ unsigned ModLevel = getModuleLevel(ctp);
+ unsigned NumFunctionObjs = CPlane.size()-ModLevel;
+
+ // If the maximum index required if all entries in this plane were merged
+ // into the global plane is less than 64, go ahead and eliminate the
+ // plane.
+ bool PrunePlane = GPlane.size() + NumFunctionObjs < 64;
+
+ // If there are no function-local values defined, and the maximum
+ // referenced global entry is less than 64, we don't need to compactify.
+ if (!PrunePlane && NumFunctionObjs == 0) {
+ unsigned MaxIdx = 0;
+ for (unsigned i = 0; i != ModLevel; ++i) {
+ unsigned Idx = NodeMap[CPlane[i]];
+ if (Idx > MaxIdx) MaxIdx = Idx;
+ }
+ PrunePlane = MaxIdx < 64;
+ }
+
+ // Ok, finally, if we decided to prune this plane out of the compaction
+ // table, do so now.
+ if (PrunePlane) {
+ TypePlane OldPlane;
+ std::swap(OldPlane, CPlane);
+
+ // Loop over the function local objects, relocating them to the global
+ // table plane.
+ for (unsigned i = ModLevel, e = OldPlane.size(); i != e; ++i) {
+ const Value *V = OldPlane[i];
+ CompactionNodeMap.erase(V);
+ assert(NodeMap.count(V) == 0 && "Value already in table??");
+ getOrCreateSlot(V);
+ }
+
+ // For compactified global values, just remove them from the compaction
+ // node map.
+ for (unsigned i = 0; i != ModLevel; ++i)
+ CompactionNodeMap.erase(OldPlane[i]);
+
+ // Update the new modulelevel for this plane.
+ assert(ctp < ModuleLevel.size() && "Cannot set modulelevel!");
+ ModuleLevel[ctp] = GPlane.size()-NumFunctionObjs;
+ assert((int)ModuleLevel[ctp] >= 0 && "Bad computation!");
+ }
+ }
+}
+
+
+int SlotCalculator::getSlot(const Value *V) const {
+ // If there is a CompactionTable active...
+ if (!CompactionNodeMap.empty()) {
+ std::map<const Value*, unsigned>::const_iterator I =
+ CompactionNodeMap.find(V);
+ if (I != CompactionNodeMap.end())
+ return (int)I->second;
+ // Otherwise, if it's not in the compaction table, it must be in a
+ // non-compactified plane.
+ }
+
+ std::map<const Value*, unsigned>::const_iterator I = NodeMap.find(V);
+ if (I != NodeMap.end())
+ return (int)I->second;
+
+ // Do not number ConstantPointerRef's at all. They are an abomination.
+ if (const ConstantPointerRef *CPR = dyn_cast<ConstantPointerRef>(V))
+ return getSlot(CPR->getValue());
+
+ return -1;