Privatize the StructType table, which unfortunately involves routing contexts through a number of APIs.

llvm-svn: 78258
This commit is contained in:
Owen Anderson 2009-08-05 23:16:16 +00:00
parent 6ee52e0047
commit 3d0e1b855d
18 changed files with 84 additions and 58 deletions

View File

@ -407,8 +407,10 @@ protected:
public:
// ConstantStruct accessors
static Constant* get(const StructType* T, const std::vector<Constant*>& V);
static Constant* get(const std::vector<Constant*>& V, bool Packed = false);
static Constant* get(Constant* const *Vals, unsigned NumVals,
static Constant* get(LLVMContext &Context,
const std::vector<Constant*>& V, bool Packed = false);
static Constant* get(LLVMContext &Context,
Constant* const *Vals, unsigned NumVals,
bool Packed = false);
/// Transparently provide more efficient getOperand methods.

View File

@ -31,6 +31,7 @@ class PointerValType;
class VectorValType;
class IntegerValType;
class APInt;
struct LLVMContext;
class DerivedType : public Type {
friend class Type;
@ -240,20 +241,22 @@ public:
/// StructType::get - This static method is the primary way to create a
/// StructType.
///
static StructType *get(const std::vector<const Type*> &Params,
static StructType *get(LLVMContext &Context,
const std::vector<const Type*> &Params,
bool isPacked=false);
/// StructType::get - Create an empty structure type.
///
static StructType *get(bool isPacked=false) {
return get(std::vector<const Type*>(), isPacked);
static StructType *get(LLVMContext &Context, bool isPacked=false) {
return get(Context, std::vector<const Type*>(), isPacked);
}
/// StructType::get - This static method is a convenience method for
/// creating structure types by specifying the elements as arguments.
/// Note that this method always returns a non-packed struct. To get
/// an empty struct, pass NULL, NULL.
static StructType *get(const Type *type, ...) END_WITH_NULL;
static StructType *get(LLVMContext &Context,
const Type *type, ...) END_WITH_NULL;
/// isValidElementType - Return true if the specified type is valid as a
/// element type.

View File

@ -470,7 +470,7 @@ DIFactory::DIFactory(Module &m)
: M(m), VMContext(M.getContext()), StopPointFn(0), FuncStartFn(0),
RegionStartFn(0), RegionEndFn(0),
DeclareFn(0) {
EmptyStructPtr = PointerType::getUnqual(StructType::get());
EmptyStructPtr = PointerType::getUnqual(StructType::get(VMContext));
}
/// getCastToEmpty - Return this descriptor as a Constant* with type '{}*'.
@ -546,7 +546,8 @@ DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) {
ConstantInt::get(Type::Int64Ty, Hi)
};
Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
Constant *Init = ConstantStruct::get(VMContext, Elts,
sizeof(Elts)/sizeof(Elts[0]));
// If we already have this range, just return the uniqued version.
DIDescriptor &Entry = SimpleConstantCache[Init];
@ -587,7 +588,8 @@ DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID,
ConstantInt::get(Type::Int32Ty, RunTimeVer)
};
Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
Constant *Init = ConstantStruct::get(VMContext, Elts,
sizeof(Elts)/sizeof(Elts[0]));
M.addTypeName("llvm.dbg.compile_unit.type", Init->getType());
GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
@ -605,7 +607,8 @@ DIEnumerator DIFactory::CreateEnumerator(const std::string &Name, uint64_t Val){
ConstantInt::get(Type::Int64Ty, Val)
};
Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
Constant *Init = ConstantStruct::get(VMContext, Elts,
sizeof(Elts)/sizeof(Elts[0]));
M.addTypeName("llvm.dbg.enumerator.type", Init->getType());
GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
@ -638,7 +641,8 @@ DIBasicType DIFactory::CreateBasicType(DIDescriptor Context,
ConstantInt::get(Type::Int32Ty, Encoding)
};
Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
Constant *Init = ConstantStruct::get(VMContext, Elts,
sizeof(Elts)/sizeof(Elts[0]));
M.addTypeName("llvm.dbg.basictype.type", Init->getType());
GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
@ -673,7 +677,8 @@ DIDerivedType DIFactory::CreateDerivedType(unsigned Tag,
getCastToEmpty(DerivedFrom)
};
Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
Constant *Init = ConstantStruct::get(VMContext, Elts,
sizeof(Elts)/sizeof(Elts[0]));
M.addTypeName("llvm.dbg.derivedtype.type", Init->getType());
GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
@ -712,7 +717,8 @@ DICompositeType DIFactory::CreateCompositeType(unsigned Tag,
ConstantInt::get(Type::Int32Ty, RuntimeLang)
};
Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
Constant *Init = ConstantStruct::get(VMContext, Elts,
sizeof(Elts)/sizeof(Elts[0]));
M.addTypeName("llvm.dbg.composite.type", Init->getType());
GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
@ -749,7 +755,8 @@ DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context,
ConstantInt::get(Type::Int1Ty, isDefinition)
};
Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
Constant *Init = ConstantStruct::get(VMContext, Elts,
sizeof(Elts)/sizeof(Elts[0]));
M.addTypeName("llvm.dbg.subprogram.type", Init->getType());
GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
@ -782,7 +789,8 @@ DIFactory::CreateGlobalVariable(DIDescriptor Context, const std::string &Name,
ConstantExpr::getBitCast(Val, EmptyStructPtr)
};
Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
Constant *Init = ConstantStruct::get(VMContext, Elts,
sizeof(Elts)/sizeof(Elts[0]));
M.addTypeName("llvm.dbg.global_variable.type", Init->getType());
GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
@ -807,7 +815,8 @@ DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context,
getCastToEmpty(Type)
};
Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
Constant *Init = ConstantStruct::get(VMContext, Elts,
sizeof(Elts)/sizeof(Elts[0]));
M.addTypeName("llvm.dbg.variable.type", Init->getType());
GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
@ -826,7 +835,8 @@ DIBlock DIFactory::CreateBlock(DIDescriptor Context) {
getCastToEmpty(Context)
};
Constant *Init = ConstantStruct::get(Elts, sizeof(Elts)/sizeof(Elts[0]));
Constant *Init = ConstantStruct::get(VMContext, Elts,
sizeof(Elts)/sizeof(Elts[0]));
M.addTypeName("llvm.dbg.block.type", Init->getType());
GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,

View File

@ -1381,7 +1381,7 @@ bool LLParser::ParseStructType(PATypeHolder &Result, bool Packed) {
Lex.Lex(); // Consume the '{'
if (EatIfPresent(lltok::rbrace)) {
Result = StructType::get(Packed);
Result = StructType::get(Context, Packed);
return false;
}
@ -1413,7 +1413,7 @@ bool LLParser::ParseStructType(PATypeHolder &Result, bool Packed) {
std::vector<const Type*> ParamsListTy;
for (unsigned i = 0, e = ParamsList.size(); i != e; ++i)
ParamsListTy.push_back(ParamsList[i].get());
Result = HandleUpRefs(StructType::get(ParamsListTy, Packed));
Result = HandleUpRefs(StructType::get(Context, ParamsListTy, Packed));
return false;
}
@ -1772,7 +1772,8 @@ bool LLParser::ParseValID(ValID &ID) {
ParseToken(lltok::rbrace, "expected end of struct constant"))
return true;
ID.ConstantVal = ConstantStruct::get(Elts.data(), Elts.size(), false);
ID.ConstantVal = ConstantStruct::get(Context, Elts.data(),
Elts.size(), false);
ID.Kind = ValID::t_Constant;
return false;
}
@ -1792,7 +1793,7 @@ bool LLParser::ParseValID(ValID &ID) {
if (isPackedStruct) {
ID.ConstantVal =
ConstantStruct::get(Elts.data(), Elts.size(), true);
ConstantStruct::get(Context, Elts.data(), Elts.size(), true);
ID.Kind = ValID::t_Constant;
return false;
}

View File

@ -292,7 +292,7 @@ void BitcodeReaderValueList::ResolveConstantForwardRefs() {
NewC = ConstantArray::get(UserCA->getType(), &NewOps[0],
NewOps.size());
} else if (ConstantStruct *UserCS = dyn_cast<ConstantStruct>(UserC)) {
NewC = ConstantStruct::get(&NewOps[0], NewOps.size(),
NewC = ConstantStruct::get(Context, &NewOps[0], NewOps.size(),
UserCS->getType()->isPacked());
} else if (isa<ConstantVector>(UserC)) {
NewC = ConstantVector::get(&NewOps[0], NewOps.size());
@ -580,7 +580,7 @@ bool BitcodeReader::ParseTypeTable() {
std::vector<const Type*> EltTys;
for (unsigned i = 1, e = Record.size(); i != e; ++i)
EltTys.push_back(getTypeByID(Record[i], true));
ResultTy = StructType::get(EltTys, Record[0]);
ResultTy = StructType::get(Context, EltTys, Record[0]);
break;
}
case bitc::TYPE_CODE_ARRAY: // ARRAY: [numelts, eltty]

View File

@ -206,12 +206,12 @@ Constant *ShadowStackGC::GetFrameMap(Function &F) {
};
Constant *DescriptorElts[] = {
ConstantStruct::get(BaseElts, 2),
ConstantStruct::get(F.getContext(), BaseElts, 2),
ConstantArray::get(ArrayType::get(VoidPtr, NumMeta),
Metadata.begin(), NumMeta)
};
Constant *FrameMap = ConstantStruct::get(DescriptorElts, 2);
Constant *FrameMap = ConstantStruct::get(F.getContext(), DescriptorElts, 2);
std::string TypeName("gc_map.");
TypeName += utostr(NumMeta);
@ -245,7 +245,7 @@ const Type* ShadowStackGC::GetConcreteStackEntryType(Function &F) {
EltTys.push_back(StackEntryTy);
for (size_t I = 0; I != Roots.size(); I++)
EltTys.push_back(Roots[I].second->getAllocatedType());
Type *Ty = StructType::get(EltTys);
Type *Ty = StructType::get(F.getContext(), EltTys);
std::string TypeName("gc_stackentry.");
TypeName += F.getName();
@ -265,7 +265,7 @@ bool ShadowStackGC::initializeCustomLowering(Module &M) {
std::vector<const Type*> EltTys;
EltTys.push_back(Type::Int32Ty); // 32 bits is ok up to a 32GB stack frame. :)
EltTys.push_back(Type::Int32Ty); // Specifies length of variable length array.
StructType *FrameMapTy = StructType::get(EltTys);
StructType *FrameMapTy = StructType::get(M.getContext(), EltTys);
M.addTypeName("gc_map", FrameMapTy);
PointerType *FrameMapPtrTy = PointerType::getUnqual(FrameMapTy);
@ -279,7 +279,7 @@ bool ShadowStackGC::initializeCustomLowering(Module &M) {
EltTys.clear();
EltTys.push_back(PointerType::getUnqual(RecursiveTy));
EltTys.push_back(FrameMapPtrTy);
PATypeHolder LinkTyH = StructType::get(EltTys);
PATypeHolder LinkTyH = StructType::get(M.getContext(), EltTys);
RecursiveTy->refineAbstractTypeTo(LinkTyH.get());
StackEntryTy = cast<StructType>(LinkTyH.get());

View File

@ -271,7 +271,8 @@ ProgramInfo::getSourceFiles(bool RequiresCompleteMap) {
// should be on the use list of the llvm.dbg.translation_units global.
//
GlobalVariable *Units =
M->getGlobalVariable("llvm.dbg.translation_units", StructType::get());
M->getGlobalVariable("llvm.dbg.translation_units",
StructType::get(M->getContext()));
if (Units == 0)
throw "Program contains no debugging information!";
@ -353,7 +354,7 @@ ProgramInfo::getSourceFunctions(bool RequiresCompleteMap) {
// should be on the use list of the llvm.dbg.translation_units global.
//
GlobalVariable *Units =
M->getGlobalVariable("llvm.dbg.globals", StructType::get());
M->getGlobalVariable("llvm.dbg.globals", StructType::get(M->getContext()));
if (Units == 0)
throw "Program contains no debugging information!";

View File

@ -638,7 +638,7 @@ bool DAE::RemoveDeadStuffFromFunction(Function *F) {
// something and {} into void.
// Make the new struct packed if we used to return a packed struct
// already.
NRetTy = StructType::get(RetTypes, STy->isPacked());
NRetTy = StructType::get(STy->getContext(), RetTypes, STy->isPacked());
else if (RetTypes.size() == 1)
// One return type? Just a simple value then, but only if we didn't use to
// return a struct with that simple value before.

View File

@ -1961,7 +1961,7 @@ static GlobalVariable *InstallGlobalCtors(GlobalVariable *GCL,
CSVals[1] = Constant::getNullValue(PFTy);
CSVals[0] = ConstantInt::get(Type::Int32Ty, 2147483647);
}
CAList.push_back(ConstantStruct::get(CSVals));
CAList.push_back(ConstantStruct::get(Context, CSVals));
}
// Create the array initializer.
@ -2069,7 +2069,7 @@ static Constant *EvaluateStoreInto(Constant *Init, Constant *Val,
Elts[Idx] = EvaluateStoreInto(Elts[Idx], Val, Addr, OpNo+1, Context);
// Return the modified struct.
return ConstantStruct::get(&Elts[0], Elts.size(), STy->isPacked());
return ConstantStruct::get(Context, &Elts[0], Elts.size(), STy->isPacked());
} else {
ConstantInt *CI = cast<ConstantInt>(Addr->getOperand(OpNo));
const ArrayType *ATy = cast<ArrayType>(Init->getType());

View File

@ -275,7 +275,7 @@ Function *CodeExtractor::constructFunction(const Values &inputs,
if (AggregateArgs && (inputs.size() + outputs.size() > 0)) {
PointerType *StructPtr =
PointerType::getUnqual(StructType::get(paramTy));
PointerType::getUnqual(StructType::get(M->getContext(), paramTy));
paramTy.clear();
paramTy.push_back(StructPtr);
}
@ -382,7 +382,7 @@ emitCallAndSwitchStatement(Function *newFunction, BasicBlock *codeReplacer,
ArgTypes.push_back((*v)->getType());
// Allocate a struct at the beginning of this function
Type *StructArgTy = StructType::get(ArgTypes);
Type *StructArgTy = StructType::get(newFunction->getContext(), ArgTypes);
Struct =
new AllocaInst(StructArgTy, 0, "structArg",
codeReplacer->getParent()->begin()->begin());

View File

@ -128,7 +128,7 @@ bool LowerInvoke::doInitialization(Module &M) {
Elements.push_back(JmpBufTy);
OpaqueType *OT = OpaqueType::get();
Elements.push_back(PointerType::getUnqual(OT));
PATypeHolder JBLType(StructType::get(Elements));
PATypeHolder JBLType(StructType::get(M.getContext(), Elements));
OT->refineAbstractTypeTo(JBLType.get()); // Complete the cycle.
JBLinkTy = JBLType.get();
M.addTypeName("llvm.sjljeh.jmpbufty", JBLinkTy);

View File

@ -519,7 +519,7 @@ Constant *llvm::ConstantFoldInsertValueInstruction(LLVMContext &Context,
Ops[i] = const_cast<Constant*>(Op);
}
if (isa<StructType>(AggTy))
return ConstantStruct::get(Ops);
return ConstantStruct::get(Context, Ops);
else
return ConstantArray::get(cast<ArrayType>(AggTy), Ops);
}
@ -548,7 +548,7 @@ Constant *llvm::ConstantFoldInsertValueInstruction(LLVMContext &Context,
Ops[i] = const_cast<Constant*>(Op);
}
if (isa<StructType>(AggTy))
return ConstantStruct::get(Ops);
return ConstantStruct::get(Context, Ops);
else
return ConstantArray::get(cast<ArrayType>(AggTy), Ops);
}
@ -565,7 +565,7 @@ Constant *llvm::ConstantFoldInsertValueInstruction(LLVMContext &Context,
}
Constant *C;
if (isa<StructType>(Agg->getType()))
C = ConstantStruct::get(Ops);
C = ConstantStruct::get(Context, Ops);
else
C = ConstantArray::get(cast<ArrayType>(Agg->getType()), Ops);
return C;

View File

@ -532,18 +532,20 @@ Constant* ConstantStruct::get(const StructType* T,
return ConstantAggregateZero::get(T);
}
Constant* ConstantStruct::get(const std::vector<Constant*>& V, bool packed) {
Constant* ConstantStruct::get(LLVMContext &Context,
const std::vector<Constant*>& V, bool packed) {
std::vector<const Type*> StructEls;
StructEls.reserve(V.size());
for (unsigned i = 0, e = V.size(); i != e; ++i)
StructEls.push_back(V[i]->getType());
return get(StructType::get(StructEls, packed), V);
return get(StructType::get(Context, StructEls, packed), V);
}
Constant* ConstantStruct::get(Constant* const *Vals, unsigned NumVals,
Constant* ConstantStruct::get(LLVMContext &Context,
Constant* const *Vals, unsigned NumVals,
bool Packed) {
// FIXME: make this the primary ctor method.
return get(std::vector<Constant*>(Vals, Vals+NumVals), Packed);
return get(Context, std::vector<Constant*>(Vals, Vals+NumVals), Packed);
}
ConstantVector::ConstantVector(const VectorType *T,
@ -1355,7 +1357,8 @@ Constant* ConstantExpr::getSizeOf(const Type* Ty) {
Constant* ConstantExpr::getAlignOf(const Type* Ty) {
// alignof is implemented as: (i64) gep ({i8,Ty}*)null, 0, 1
const Type *AligningTy = StructType::get(Type::Int8Ty, Ty, NULL);
const Type *AligningTy = StructType::get(Ty->getContext(),
Type::Int8Ty, Ty, NULL);
Constant *NullPtr = Constant::getNullValue(AligningTy->getPointerTo());
Constant *Zero = ConstantInt::get(Type::Int32Ty, 0);
Constant *One = ConstantInt::get(Type::Int32Ty, 1);

View File

@ -217,7 +217,7 @@ LLVMTypeRef LLVMStructType(LLVMTypeRef *ElementTypes,
*E = ElementTypes + ElementCount; I != E; ++I)
Tys.push_back(unwrap(*I));
return wrap(StructType::get(Tys, Packed != 0));
return wrap(StructType::get(getGlobalContext(), Tys, Packed != 0));
}
unsigned LLVMCountStructElementTypes(LLVMTypeRef StructTy) {
@ -411,7 +411,8 @@ LLVMValueRef LLVMConstArray(LLVMTypeRef ElementTy,
LLVMValueRef LLVMConstStruct(LLVMValueRef *ConstantVals, unsigned Count,
int Packed) {
return wrap(ConstantStruct::get(unwrap<Constant>(ConstantVals, Count),
return wrap(ConstantStruct::get(getGlobalContext(),
unwrap<Constant>(ConstantVals, Count),
Count, Packed != 0));
}

View File

@ -132,6 +132,7 @@ struct LLVMContextImpl {
TypeMap<VectorValType, VectorType> VectorTypes;
TypeMap<PointerValType, PointerType> PointerTypes;
TypeMap<FunctionValType, FunctionType> FunctionTypes;
TypeMap<StructValType, StructType> StructTypes;
LLVMContextImpl() : TheTrueVal(0), TheFalseVal(0) { }
};

View File

@ -849,22 +849,23 @@ bool VectorType::isValidElementType(const Type *ElemTy) {
// Struct Type Factory...
//
static ManagedStatic<TypeMap<StructValType, StructType> > StructTypes;
StructType *StructType::get(const std::vector<const Type*> &ETypes,
StructType *StructType::get(LLVMContext &Context,
const std::vector<const Type*> &ETypes,
bool isPacked) {
StructValType STV(ETypes, isPacked);
StructType *ST = 0;
LLVMContextImpl *pImpl = Context.pImpl;
sys::SmartScopedLock<true> L(*TypeMapLock);
ST = StructTypes->get(STV);
ST = pImpl->StructTypes.get(STV);
if (!ST) {
// Value not found. Derive a new type!
ST = (StructType*) operator new(sizeof(StructType) +
sizeof(PATypeHandle) * ETypes.size());
new (ST) StructType(ETypes, isPacked);
StructTypes->add(STV, ST);
pImpl->StructTypes.add(STV, ST);
}
#ifdef DEBUG_MERGE_TYPES
DOUT << "Derived new type: " << *ST << "\n";
@ -872,7 +873,7 @@ StructType *StructType::get(const std::vector<const Type*> &ETypes,
return ST;
}
StructType *StructType::get(const Type *type, ...) {
StructType *StructType::get(LLVMContext &Context, const Type *type, ...) {
va_list ap;
std::vector<const llvm::Type*> StructFields;
va_start(ap, type);
@ -880,7 +881,7 @@ StructType *StructType::get(const Type *type, ...) {
StructFields.push_back(type);
type = va_arg(ap, llvm::Type*);
}
return llvm::StructType::get(StructFields);
return llvm::StructType::get(Context, StructFields);
}
bool StructType::isValidElementType(const Type *ElemTy) {
@ -1146,11 +1147,13 @@ void VectorType::typeBecameConcrete(const DerivedType *AbsTy) {
//
void StructType::refineAbstractType(const DerivedType *OldType,
const Type *NewType) {
StructTypes->RefineAbstractType(this, OldType, NewType);
LLVMContextImpl *pImpl = OldType->getContext().pImpl;
pImpl->StructTypes.RefineAbstractType(this, OldType, NewType);
}
void StructType::typeBecameConcrete(const DerivedType *AbsTy) {
StructTypes->TypeBecameConcrete(this, AbsTy);
LLVMContextImpl *pImpl = AbsTy->getContext().pImpl;
pImpl->StructTypes.TypeBecameConcrete(this, AbsTy);
}
// refineAbstractType - Called when a contained type is found to be more

View File

@ -186,7 +186,8 @@ static Constant *GetTorInit(std::vector<std::pair<Function*, int> > &TorList) {
std::vector<Constant*> Elts;
Elts.push_back(ConstantInt::get(Type::Int32Ty, TorList[i].second));
Elts.push_back(TorList[i].first);
ArrayElts.push_back(ConstantStruct::get(Elts));
ArrayElts.push_back(ConstantStruct::get(
TorList[i].first->getContext(), Elts));
}
return ConstantArray::get(ArrayType::get(ArrayElts[0]->getType(),
ArrayElts.size()),

View File

@ -146,7 +146,7 @@ static void EmitTypeForValueType(raw_ostream &OS, MVT::SimpleValueType VT) {
OS << "IntegerType::get(" << BitWidth << ")";
} else if (VT == MVT::Other) {
// MVT::OtherVT is used to mean the empty struct type here.
OS << "StructType::get()";
OS << "StructType::get(Context)";
} else if (VT == MVT::f32) {
OS << "Type::FloatTy";
} else if (VT == MVT::f64) {
@ -177,7 +177,7 @@ static void EmitTypeGenerate(raw_ostream &OS,
return;
}
OS << "StructType::get(";
OS << "StructType::get(Context, ";
for (std::vector<Record*>::const_iterator
I = ArgTypes.begin(), E = ArgTypes.end(); I != E; ++I) {