getTypeSlot can never fail

llvm-svn: 34129
This commit is contained in:
Chris Lattner 2007-02-10 05:17:48 +00:00
parent ce57e528f5
commit 832676a772
3 changed files with 26 additions and 62 deletions

View File

@ -284,10 +284,10 @@ unsigned SlotCalculator::getSlot(const Value *V) const {
return (int)I->second; return (int)I->second;
} }
int SlotCalculator::getTypeSlot(const Type*T) const { unsigned SlotCalculator::getTypeSlot(const Type*T) const {
std::map<const Type*, unsigned>::const_iterator I = TypeMap.find(T); std::map<const Type*, unsigned>::const_iterator I = TypeMap.find(T);
assert(I != TypeMap.end() && "Type not in slotcalc!"); assert(I != TypeMap.end() && "Type not in slotcalc!");
return (int)I->second; return I->second;
} }
void SlotCalculator::CreateSlotIfNeeded(const Value *V) { void SlotCalculator::CreateSlotIfNeeded(const Value *V) {

View File

@ -66,7 +66,7 @@ public:
/// plane. This returns < 0 on error! /// plane. This returns < 0 on error!
/// ///
unsigned getSlot(const Value *V) const; unsigned getSlot(const Value *V) const;
int getTypeSlot(const Type* T) const; unsigned getTypeSlot(const Type* T) const;
inline unsigned getNumPlanes() const { return Table.size(); } inline unsigned getNumPlanes() const { return Table.size(); }
inline unsigned getNumTypes() const { return Types.size(); } inline unsigned getNumTypes() const { return Types.size(); }

View File

@ -214,9 +214,7 @@ void BytecodeWriter::outputType(const Type *T) {
break; break;
case Type::FunctionTyID: { case Type::FunctionTyID: {
const FunctionType *MT = cast<FunctionType>(T); const FunctionType *MT = cast<FunctionType>(T);
int Slot = Table.getTypeSlot(MT->getReturnType()); output_typeid(Table.getTypeSlot(MT->getReturnType()));
assert(Slot != -1 && "Type used but not available!!");
output_typeid((unsigned)Slot);
output_vbr(unsigned(MT->getParamAttrs(0))); output_vbr(unsigned(MT->getParamAttrs(0)));
// Output the number of arguments to function (+1 if varargs): // Output the number of arguments to function (+1 if varargs):
@ -226,9 +224,7 @@ void BytecodeWriter::outputType(const Type *T) {
FunctionType::param_iterator I = MT->param_begin(); FunctionType::param_iterator I = MT->param_begin();
unsigned Idx = 1; unsigned Idx = 1;
for (; I != MT->param_end(); ++I) { for (; I != MT->param_end(); ++I) {
Slot = Table.getTypeSlot(*I); output_typeid(Table.getTypeSlot(*I));
assert(Slot != -1 && "Type used but not available!!");
output_typeid((unsigned)Slot);
output_vbr(unsigned(MT->getParamAttrs(Idx))); output_vbr(unsigned(MT->getParamAttrs(Idx)));
Idx++; Idx++;
} }
@ -241,18 +237,14 @@ void BytecodeWriter::outputType(const Type *T) {
case Type::ArrayTyID: { case Type::ArrayTyID: {
const ArrayType *AT = cast<ArrayType>(T); const ArrayType *AT = cast<ArrayType>(T);
int Slot = Table.getTypeSlot(AT->getElementType()); output_typeid(Table.getTypeSlot(AT->getElementType()));
assert(Slot != -1 && "Type used but not available!!");
output_typeid((unsigned)Slot);
output_vbr(AT->getNumElements()); output_vbr(AT->getNumElements());
break; break;
} }
case Type::PackedTyID: { case Type::PackedTyID: {
const PackedType *PT = cast<PackedType>(T); const PackedType *PT = cast<PackedType>(T);
int Slot = Table.getTypeSlot(PT->getElementType()); output_typeid(Table.getTypeSlot(PT->getElementType()));
assert(Slot != -1 && "Type used but not available!!");
output_typeid((unsigned)Slot);
output_vbr(PT->getNumElements()); output_vbr(PT->getNumElements());
break; break;
} }
@ -262,9 +254,7 @@ void BytecodeWriter::outputType(const Type *T) {
// Output all of the element types... // Output all of the element types...
for (StructType::element_iterator I = ST->element_begin(), for (StructType::element_iterator I = ST->element_begin(),
E = ST->element_end(); I != E; ++I) { E = ST->element_end(); I != E; ++I) {
int Slot = Table.getTypeSlot(*I); output_typeid(Table.getTypeSlot(*I));
assert(Slot != -1 && "Type used but not available!!");
output_typeid((unsigned)Slot);
} }
// Terminate list with VoidTy // Terminate list with VoidTy
@ -272,13 +262,9 @@ void BytecodeWriter::outputType(const Type *T) {
break; break;
} }
case Type::PointerTyID: { case Type::PointerTyID:
const PointerType *PT = cast<PointerType>(T); output_typeid(Table.getTypeSlot(cast<PointerType>(T)->getElementType()));
int Slot = Table.getTypeSlot(PT->getElementType());
assert(Slot != -1 && "Type used but not available!!");
output_typeid((unsigned)Slot);
break; break;
}
case Type::OpaqueTyID: case Type::OpaqueTyID:
// No need to emit anything, just the count of opaque types is enough. // No need to emit anything, just the count of opaque types is enough.
@ -306,10 +292,8 @@ void BytecodeWriter::outputConstant(const Constant *CPV) {
output_vbr(CE->getOpcode()); // Put out the CE op code output_vbr(CE->getOpcode()); // Put out the CE op code
for (User::const_op_iterator OI = CE->op_begin(); OI != CE->op_end(); ++OI){ for (User::const_op_iterator OI = CE->op_begin(); OI != CE->op_end(); ++OI){
unsigned Slot = Table.getSlot(*OI); output_vbr(Table.getSlot(*OI));
output_vbr(Slot); output_typeid(Table.getTypeSlot((*OI)->getType()));
Slot = Table.getTypeSlot((*OI)->getType());
output_typeid(Slot);
} }
if (CE->isCompare()) if (CE->isCompare())
output_vbr((unsigned)CE->getPredicate()); output_vbr((unsigned)CE->getPredicate());
@ -408,9 +392,7 @@ void BytecodeWriter::outputConstantStrings() {
// Emit all of the strings. // Emit all of the strings.
for (I = Table.string_begin(); I != E; ++I) { for (I = Table.string_begin(); I != E; ++I) {
const ConstantArray *Str = *I; const ConstantArray *Str = *I;
int Slot = Table.getTypeSlot(Str->getType()); output_typeid(Table.getTypeSlot(Str->getType()));
assert(Slot != -1 && "Constant string of unknown type?");
output_typeid((unsigned)Slot);
// Now that we emitted the type (which indicates the size of the string), // Now that we emitted the type (which indicates the size of the string),
// emit all of the characters. // emit all of the characters.
@ -445,9 +427,7 @@ void BytecodeWriter::outputInstructionFormat0(const Instruction *I,
output_vbr(Table.getSlot(I->getOperand(i))); output_vbr(Table.getSlot(I->getOperand(i)));
if (isa<CastInst>(I) || isa<VAArgInst>(I)) { if (isa<CastInst>(I) || isa<VAArgInst>(I)) {
int Slot = Table.getTypeSlot(I->getType()); output_typeid(Table.getTypeSlot(I->getType()));
assert(Slot != -1 && "Cast return type unknown?");
output_typeid((unsigned)Slot);
} else if (isa<CmpInst>(I)) { } else if (isa<CmpInst>(I)) {
output_vbr(unsigned(cast<CmpInst>(I)->getPredicate())); output_vbr(unsigned(cast<CmpInst>(I)->getPredicate()));
} else if (isa<InvokeInst>(I)) { } else if (isa<InvokeInst>(I)) {
@ -525,12 +505,9 @@ void BytecodeWriter::outputInstrVarArgsCall(const Instruction *I,
for (unsigned i = NumFixedOperands, e = I->getNumOperands(); i != e; ++i) { for (unsigned i = NumFixedOperands, e = I->getNumOperands(); i != e; ++i) {
// Output Arg Type ID // Output Arg Type ID
int Slot = Table.getTypeSlot(I->getOperand(i)->getType()); output_typeid(Table.getTypeSlot(I->getOperand(i)->getType()));
assert(Slot >= 0 && "No slot number for value!?!?");
output_typeid((unsigned)Slot);
// Output arg ID itself // Output arg ID itself
assert(Slot >= 0 && "No slot number for value!?!?");
output_vbr(Table.getSlot(I->getOperand(i))); output_vbr(Table.getSlot(I->getOperand(i)));
} }
@ -650,10 +627,7 @@ void BytecodeWriter::outputInstruction(const Instruction &I) {
break; break;
} }
unsigned Type; unsigned Type = Table.getTypeSlot(Ty);
int Slot = Table.getTypeSlot(Ty);
assert(Slot != -1 && "Type not available!!?!");
Type = (unsigned)Slot;
// Varargs calls and invokes are encoded entirely different from any other // Varargs calls and invokes are encoded entirely different from any other
// instructions. // instructions.
@ -689,7 +663,6 @@ void BytecodeWriter::outputInstruction(const Instruction &I) {
// Cast has to encode the destination type as the second argument in the // Cast has to encode the destination type as the second argument in the
// packet, or else we won't know what type to cast to! // packet, or else we won't know what type to cast to!
Slots[1] = Table.getTypeSlot(I.getType()); Slots[1] = Table.getTypeSlot(I.getType());
assert(Slots[1] != ~0U && "Cast return type unknown?");
if (Slots[1] > MaxOpSlot) MaxOpSlot = Slots[1]; if (Slots[1] > MaxOpSlot) MaxOpSlot = Slots[1];
NumOperands++; NumOperands++;
} else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) { } else if (const AllocationInst *AI = dyn_cast<AllocationInst>(&I)) {
@ -864,10 +837,8 @@ void BytecodeWriter::outputConstantsInPlane(const std::vector<const Value*>
// //
output_vbr(NC); output_vbr(NC);
// Put out the Type ID Number... // Put out the Type ID Number.
int Slot = Table.getTypeSlot(Plane.front()->getType()); output_typeid(Table.getTypeSlot(Plane.front()->getType()));
assert (Slot != -1 && "Type in constant pool but not in function!!");
output_typeid((unsigned)Slot);
for (unsigned i = ValNo; i < ValNo+NC; ++i) { for (unsigned i = ValNo; i < ValNo+NC; ++i) {
const Value *V = Plane[i]; const Value *V = Plane[i];
@ -939,8 +910,7 @@ void BytecodeWriter::outputModuleInfoBlock(const Module *M) {
// Output the types for the global variables in the module... // Output the types for the global variables in the module...
for (Module::const_global_iterator I = M->global_begin(), for (Module::const_global_iterator I = M->global_begin(),
End = M->global_end(); I != End; ++I) { End = M->global_end(); I != End; ++I) {
int Slot = Table.getTypeSlot(I->getType()); unsigned Slot = Table.getTypeSlot(I->getType());
assert(Slot != -1 && "Module global vars is broken!");
assert((I->hasInitializer() || !I->hasInternalLinkage()) && assert((I->hasInitializer() || !I->hasInternalLinkage()) &&
"Global must have an initializer or have external linkage!"); "Global must have an initializer or have external linkage!");
@ -954,11 +924,11 @@ void BytecodeWriter::outputModuleInfoBlock(const Module *M) {
// If we need to use the extension byte, set linkage=3(internal) and // If we need to use the extension byte, set linkage=3(internal) and
// initializer = 0 (impossible!). // initializer = 0 (impossible!).
if (!HasExtensionWord) { if (!HasExtensionWord) {
unsigned oSlot = ((unsigned)Slot << 5) | (getEncodedLinkage(I) << 2) | unsigned oSlot = (Slot << 5) | (getEncodedLinkage(I) << 2) |
(I->hasInitializer() << 1) | (unsigned)I->isConstant(); (I->hasInitializer() << 1) | (unsigned)I->isConstant();
output_vbr(oSlot); output_vbr(oSlot);
} else { } else {
unsigned oSlot = ((unsigned)Slot << 5) | (3 << 2) | unsigned oSlot = (Slot << 5) | (3 << 2) |
(0 << 1) | (unsigned)I->isConstant(); (0 << 1) | (unsigned)I->isConstant();
output_vbr(oSlot); output_vbr(oSlot);
@ -986,13 +956,11 @@ void BytecodeWriter::outputModuleInfoBlock(const Module *M) {
if (I->hasInitializer()) if (I->hasInitializer())
output_vbr(Table.getSlot((Value*)I->getInitializer())); output_vbr(Table.getSlot((Value*)I->getInitializer()));
} }
output_typeid((unsigned)Table.getTypeSlot(Type::VoidTy)); output_typeid(Table.getTypeSlot(Type::VoidTy));
// Output the types of the functions in this module. // Output the types of the functions in this module.
for (Module::const_iterator I = M->begin(), End = M->end(); I != End; ++I) { for (Module::const_iterator I = M->begin(), End = M->end(); I != End; ++I) {
int Slot = Table.getTypeSlot(I->getType()); unsigned Slot = Table.getTypeSlot(I->getType());
assert(Slot != -1 && "Module slot calculator is broken!");
assert(Slot >= Type::FirstDerivedTyID && "Derived type not in range!");
assert(((Slot << 6) >> 6) == Slot && "Slot # too big!"); assert(((Slot << 6) >> 6) == Slot && "Slot # too big!");
unsigned CC = I->getCallingConv()+1; unsigned CC = I->getCallingConv()+1;
unsigned ID = (Slot << 5) | (CC & 15); unsigned ID = (Slot << 5) | (CC & 15);
@ -1037,7 +1005,7 @@ void BytecodeWriter::outputModuleInfoBlock(const Module *M) {
} }
} }
} }
output_vbr((unsigned)Table.getTypeSlot(Type::VoidTy) << 5); output_vbr(Table.getTypeSlot(Type::VoidTy) << 5);
// Emit the list of dependent libraries for the Module. // Emit the list of dependent libraries for the Module.
Module::lib_iterator LI = M->lib_begin(); Module::lib_iterator LI = M->lib_begin();
@ -1104,7 +1072,7 @@ void BytecodeWriter::outputTypeSymbolTable(const TypeSymbolTable &TST) {
for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end(); for (TypeSymbolTable::const_iterator TI = TST.begin(), TE = TST.end();
TI != TE; ++TI) { TI != TE; ++TI) {
// Symtab entry:[def slot #][name] // Symtab entry:[def slot #][name]
output_typeid((unsigned)Table.getTypeSlot(TI->second)); output_typeid(Table.getTypeSlot(TI->second));
output(TI->first); output(TI->first);
} }
} }
@ -1129,8 +1097,6 @@ void BytecodeWriter::outputValueSymbolTable(const ValueSymbolTable &VST) {
for (PlaneMap::const_iterator PI = Planes.begin(), PE = Planes.end(); for (PlaneMap::const_iterator PI = Planes.begin(), PE = Planes.end();
PI != PE; ++PI) { PI != PE; ++PI) {
int Slot;
PlaneMapVector::const_iterator I = PI->second.begin(); PlaneMapVector::const_iterator I = PI->second.begin();
PlaneMapVector::const_iterator End = PI->second.end(); PlaneMapVector::const_iterator End = PI->second.end();
@ -1140,9 +1106,7 @@ void BytecodeWriter::outputValueSymbolTable(const ValueSymbolTable &VST) {
output_vbr((unsigned)PI->second.size()); output_vbr((unsigned)PI->second.size());
// Write the slot number of the type for this plane // Write the slot number of the type for this plane
Slot = Table.getTypeSlot(PI->first); output_typeid(Table.getTypeSlot(PI->first));
assert(Slot != -1 && "Type in symtab, but not in table!");
output_typeid((unsigned)Slot);
// Write each of the values in this plane // Write each of the values in this plane
for (; I != End; ++I) { for (; I != End; ++I) {