aboutsummaryrefslogtreecommitdiffstats
path: root/lib/Bytecode/Writer
diff options
context:
space:
mode:
Diffstat (limited to 'lib/Bytecode/Writer')
-rw-r--r--lib/Bytecode/Writer/SlotCalculator.cpp75
-rw-r--r--lib/Bytecode/Writer/SlotCalculator.h3
-rw-r--r--lib/Bytecode/Writer/Writer.cpp62
3 files changed, 84 insertions, 56 deletions
diff --git a/lib/Bytecode/Writer/SlotCalculator.cpp b/lib/Bytecode/Writer/SlotCalculator.cpp
index fdf7174..2d4cd0c 100644
--- a/lib/Bytecode/Writer/SlotCalculator.cpp
+++ b/lib/Bytecode/Writer/SlotCalculator.cpp
@@ -31,26 +31,45 @@
#include <functional>
using namespace llvm;
-#if 0
+#ifndef NDEBUG
#include "llvm/Support/Streams.h"
-#define SC_DEBUG(X) cerr << X
+#include "llvm/Support/CommandLine.h"
+static cl::opt<bool> SlotCalculatorDebugOption("scdebug",cl::init(false),
+ cl::desc("Enable SlotCalculator debug output"), cl::Hidden);
+#define SC_DEBUG(X) if (SlotCalculatorDebugOption) cerr << X
#else
#define SC_DEBUG(X)
#endif
+void SlotCalculator::insertPrimitives() {
+ // Preload the table with the built-in types. These built-in types are
+ // inserted first to ensure that they have low integer indices which helps to
+ // keep bytecode sizes small. Note that the first group of indices must match
+ // the Type::TypeIDs for the primitive types. After that the integer types are
+ // added, but the order and value is not critical. What is critical is that
+ // the indices of these "well known" slot numbers be properly maintained in
+ // Reader.h which uses them directly to extract values of these types.
+ SC_DEBUG("Inserting primitive types:\n");
+ // See WellKnownTypeSlots in Reader.h
+ insertType(Type::VoidTy, true); // 0: VoidTySlot
+ insertType(Type::FloatTy, true); // 1: FloatTySlot
+ insertType(Type::DoubleTy, true); // 2: DoubleTySlot
+ insertType(Type::LabelTy, true); // 3: LabelTySlot
+ assert(TypeMap.size() == Type::FirstDerivedTyID && "Invalid primitive insert");
+ // Above here *must* correspond 1:1 with the primitive types.
+ insertType(Type::Int1Ty, true); // 4: BoolTySlot
+ insertType(Type::Int8Ty, true); // 5: Int8TySlot
+ insertType(Type::Int16Ty, true); // 6: Int16TySlot
+ insertType(Type::Int32Ty, true); // 7: Int32TySlot
+ insertType(Type::Int64Ty, true); // 8: Int64TySlot
+}
+
SlotCalculator::SlotCalculator(const Module *M ) {
ModuleContainsAllFunctionConstants = false;
ModuleTypeLevel = 0;
TheModule = M;
- // Preload table... Make sure that all of the primitive types are in the table
- // and that their Primitive ID is equal to their slot #
- //
- SC_DEBUG("Inserting primitive types:\n");
- for (unsigned i = 0; i < Type::FirstDerivedTyID; ++i) {
- assert(Type::getPrimitiveType((Type::TypeID)i));
- insertType(Type::getPrimitiveType((Type::TypeID)i), true);
- }
+ insertPrimitives();
if (M == 0) return; // Empty table...
processModule();
@@ -60,14 +79,7 @@ SlotCalculator::SlotCalculator(const Function *M ) {
ModuleContainsAllFunctionConstants = false;
TheModule = M ? M->getParent() : 0;
- // Preload table... Make sure that all of the primitive types are in the table
- // and that their Primitive ID is equal to their slot #
- //
- SC_DEBUG("Inserting primitive types:\n");
- for (unsigned i = 0; i < Type::FirstDerivedTyID; ++i) {
- assert(Type::getPrimitiveType((Type::TypeID)i));
- insertType(Type::getPrimitiveType((Type::TypeID)i), true);
- }
+ insertPrimitives();
if (TheModule == 0) return; // Empty table...
@@ -423,15 +435,14 @@ unsigned SlotCalculator::getOrCreateCompactionTableSlot(const Value *V) {
/// getOrCreateCompactionTableSlot - This method is used to build up the initial
/// approximation of the compaction table.
unsigned SlotCalculator::getOrCreateCompactionTableSlot(const Type *T) {
- std::map<const Type*, unsigned>::iterator I =
- CompactionTypeMap.lower_bound(T);
+ CompactionTypeMapType::iterator I = CompactionTypeMap.lower_bound(T);
if (I != CompactionTypeMap.end() && I->first == T)
return I->second; // Already exists?
unsigned SlotNo = CompactionTypes.size();
- SC_DEBUG("Inserting Compaction Type #" << SlotNo << ": " << T << "\n");
+ SC_DEBUG("Inserting Compaction Type #" << SlotNo << ": " << *T << "\n");
CompactionTypes.push_back(T);
- CompactionTypeMap.insert(std::make_pair(T, SlotNo));
+ CompactionTypeMap[T] = SlotNo;
return SlotNo;
}
@@ -452,6 +463,16 @@ void SlotCalculator::buildCompactionTable(const Function *F) {
CompactionTypes.push_back(PrimTy);
CompactionTypeMap[PrimTy] = i;
}
+ CompactionTypeMap[Type::Int1Ty] = CompactionTypes.size();
+ CompactionTypes.push_back(Type::Int1Ty);
+ CompactionTypeMap[Type::Int8Ty] = CompactionTypes.size();
+ CompactionTypes.push_back(Type::Int8Ty);
+ CompactionTypeMap[Type::Int16Ty] = CompactionTypes.size();
+ CompactionTypes.push_back(Type::Int16Ty);
+ CompactionTypeMap[Type::Int32Ty] = CompactionTypes.size();
+ CompactionTypes.push_back(Type::Int32Ty);
+ CompactionTypeMap[Type::Int64Ty] = CompactionTypes.size();
+ CompactionTypes.push_back(Type::Int64Ty);
// Next, include any types used by function arguments.
for (Function::const_arg_iterator I = F->arg_begin(), E = F->arg_end();
@@ -485,7 +506,7 @@ void SlotCalculator::buildCompactionTable(const Function *F) {
if (CompactionTable[i].empty() && (i != Type::VoidTyID) &&
i != Type::LabelTyID) {
const Type *Ty = CompactionTypes[i];
- SC_DEBUG("Getting Null Value #" << i << " for Type " << Ty << "\n");
+ SC_DEBUG("Getting Null Value #" << i << " for Type " << *Ty << "\n");
assert(Ty->getTypeID() != Type::VoidTyID);
assert(Ty->getTypeID() != Type::LabelTyID);
getOrCreateCompactionTableSlot(Constant::getNullValue(Ty));
@@ -618,7 +639,8 @@ void SlotCalculator::pruneCompactionTable() {
/// to determine if its actually empty.
bool SlotCalculator::CompactionTableIsEmpty() const {
// Check a degenerate case, just in case.
- if (CompactionTable.size() == 0) return true;
+ if (CompactionTable.size() == 0)
+ return true;
// Check each plane
for (unsigned i = 0, e = CompactionTable.size(); i < e; ++i) {
@@ -830,7 +852,7 @@ int SlotCalculator::doInsertValue(const Value *D) {
unsigned DestSlot = NodeMap[D] = Table[Ty].size();
Table[Ty].push_back(D);
- SC_DEBUG(" Inserting value [" << Ty << "] = " << D << " slot=" <<
+ SC_DEBUG(" Inserting value [" << Ty << "] = " << *D << " slot=" <<
DestSlot << " [");
// G = Global, C = Constant, T = Type, F = Function, o = other
SC_DEBUG((isa<GlobalVariable>(D) ? "G" : (isa<Constant>(D) ? "C" :
@@ -848,7 +870,6 @@ int SlotCalculator::doInsertType(const Type *Ty) {
unsigned DestSlot = TypeMap[Ty] = Types.size();
Types.push_back(Ty);
- SC_DEBUG(" Inserting type [" << DestSlot << "] = " << Ty << "\n" );
+ SC_DEBUG(" Inserting type [" << DestSlot << "] = " << *Ty << "\n" );
return (int)DestSlot;
}
-
diff --git a/lib/Bytecode/Writer/SlotCalculator.h b/lib/Bytecode/Writer/SlotCalculator.h
index 405c0ed..de91d2e 100644
--- a/lib/Bytecode/Writer/SlotCalculator.h
+++ b/lib/Bytecode/Writer/SlotCalculator.h
@@ -177,6 +177,9 @@ private:
unsigned getOrCreateCompactionTableSlot(const Value *V);
unsigned getOrCreateCompactionTableSlot(const Type *V);
void pruneCompactionTable();
+
+ // insertPrimitives - helper for constructors to insert primitive types.
+ void insertPrimitives();
};
} // End llvm namespace
diff --git a/lib/Bytecode/Writer/Writer.cpp b/lib/Bytecode/Writer/Writer.cpp
index 9a04428..c7003cd 100644
--- a/lib/Bytecode/Writer/Writer.cpp
+++ b/lib/Bytecode/Writer/Writer.cpp
@@ -200,16 +200,18 @@ inline BytecodeBlock::~BytecodeBlock() { // Do backpatch when block goes out
void BytecodeWriter::outputType(const Type *T) {
const StructType* STy = dyn_cast<StructType>(T);
if(STy && STy->isPacked())
- output_vbr((unsigned)Type::BC_ONLY_PackedStructTyID);
+ output_vbr((unsigned)Type::PackedStructTyID);
else
output_vbr((unsigned)T->getTypeID());
// That's all there is to handling primitive types...
- if (T->isPrimitiveType()) {
+ if (T->isPrimitiveType())
return; // We might do this if we alias a prim type: %x = type int
- }
switch (T->getTypeID()) { // Handle derived types now.
+ case Type::IntegerTyID:
+ output_vbr(cast<IntegerType>(T)->getBitWidth());
+ break;
case Type::FunctionTyID: {
const FunctionType *MT = cast<FunctionType>(T);
int Slot = Table.getSlot(MT->getReturnType());
@@ -290,8 +292,8 @@ void BytecodeWriter::outputType(const Type *T) {
}
void BytecodeWriter::outputConstant(const Constant *CPV) {
- assert((CPV->getType()->isPrimitiveType() || !CPV->isNullValue()) &&
- "Shouldn't output null constants!");
+ assert(((CPV->getType()->isPrimitiveType() || CPV->getType()->isIntegral()) ||
+ !CPV->isNullValue()) && "Shouldn't output null constants!");
// We must check for a ConstantExpr before switching by type because
// a ConstantExpr can be of any type, and has no explicit value.
@@ -321,19 +323,21 @@ void BytecodeWriter::outputConstant(const Constant *CPV) {
}
switch (CPV->getType()->getTypeID()) {
- case Type::Int1TyID: // Boolean Types
- if (cast<ConstantInt>(CPV)->getZExtValue())
- output_vbr(1U);
- else
- output_vbr(0U);
- break;
-
- case Type::Int8TyID: // Unsigned integer types...
- case Type::Int16TyID:
- case Type::Int32TyID:
- case Type::Int64TyID:
- output_vbr(cast<ConstantInt>(CPV)->getZExtValue());
+ case Type::IntegerTyID: { // Integer types...
+ unsigned NumBits = cast<IntegerType>(CPV->getType())->getBitWidth();
+ if (NumBits == 1)
+ if (cast<ConstantInt>(CPV)->getZExtValue())
+ output_vbr(1U);
+ else
+ output_vbr(0U);
+ else if (NumBits <= 32)
+ output_vbr(uint32_t(cast<ConstantInt>(CPV)->getZExtValue()));
+ else if (NumBits <= 64)
+ output_vbr(uint64_t(cast<ConstantInt>(CPV)->getZExtValue()));
+ else
+ assert("Integer types > 64 bits not supported.");
break;
+ }
case Type::ArrayTyID: {
const ConstantArray *CPA = cast<ConstantArray>(CPV);
@@ -484,12 +488,12 @@ void BytecodeWriter::outputInstructionFormat0(const Instruction *I,
assert(Slot >= 0 && "No slot number for value!?!?");
if (isa<SequentialType>(*TI)) {
- unsigned IdxId;
- switch (I->getOperand(Idx)->getType()->getTypeID()) {
- default: assert(0 && "Unknown index type!");
- case Type::Int32TyID: IdxId = 0; break;
- case Type::Int64TyID: IdxId = 1; break;
- }
+ // These should be either 32-bits or 64-bits, however, with bit
+ // accurate types we just distinguish between less than or equal to
+ // 32-bits or greater than 32-bits.
+ const IntegerType *IdxTy =
+ cast<IntegerType>(I->getOperand(Idx)->getType());
+ unsigned IdxId = IdxTy->getBitWidth() <= 32 ? 0 : 1;
Slot = (Slot << 1) | IdxId;
}
output_vbr(unsigned(Slot));
@@ -735,12 +739,12 @@ void BytecodeWriter::outputInstruction(const Instruction &I) {
for (gep_type_iterator I = gep_type_begin(GEP), E = gep_type_end(GEP);
I != E; ++I, ++Idx)
if (isa<SequentialType>(*I)) {
- unsigned IdxId;
- switch (GEP->getOperand(Idx)->getType()->getTypeID()) {
- default: assert(0 && "Unknown index type!");
- case Type::Int32TyID: IdxId = 0; break;
- case Type::Int64TyID: IdxId = 1; break;
- }
+ // These should be either 32-bits or 64-bits, however, with bit
+ // accurate types we just distinguish between less than or equal to
+ // 32-bits or greater than 32-bits.
+ const IntegerType *IdxTy =
+ cast<IntegerType>(GEP->getOperand(Idx)->getType());
+ unsigned IdxId = IdxTy->getBitWidth() <= 32 ? 0 : 1;
Slots[Idx] = (Slots[Idx] << 1) | IdxId;
if (Slots[Idx] > MaxOpSlot) MaxOpSlot = Slots[Idx];
}