From 3d29df3e8a203b167d8071ea6f805b21db18a5af Mon Sep 17 00:00:00 2001 From: Owen Anderson Date: Wed, 8 Jul 2009 01:26:06 +0000 Subject: [PATCH] Push LLVMContext through GlobalVariables and IRBuilder. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@74985 91177308-0d34-0410-b5e6-96231b3b80d8 --- examples/BrainF/BrainF.cpp | 1 + include/llvm/GlobalVariable.h | 7 +++- include/llvm/Support/IRBuilder.h | 42 +++++++++++-------- lib/Analysis/DebugInfo.cpp | 24 +++++------ lib/AsmParser/LLParser.cpp | 7 ++-- lib/Bitcode/Reader/BitcodeReader.cpp | 2 +- lib/CodeGen/ShadowStackGC.cpp | 4 +- lib/Linker/LinkModules.cpp | 11 ++--- lib/Transforms/IPO/ExtractGV.cpp | 2 +- lib/Transforms/IPO/GlobalOpt.cpp | 24 +++++++---- .../Instrumentation/BlockProfiling.cpp | 4 +- .../Instrumentation/EdgeProfiling.cpp | 2 +- .../Instrumentation/RSProfiling.cpp | 6 ++- lib/Transforms/Scalar/SimplifyLibCalls.cpp | 3 +- lib/Transforms/Utils/CloneModule.cpp | 3 +- lib/Transforms/Utils/LowerInvoke.cpp | 9 ++-- lib/VMCore/Core.cpp | 3 +- lib/VMCore/Globals.cpp | 13 ++++-- lib/VMCore/Module.cpp | 10 +++-- tools/bugpoint/ExtractFunction.cpp | 6 ++- tools/bugpoint/Miscompilation.cpp | 6 ++- 21 files changed, 114 insertions(+), 75 deletions(-) diff --git a/examples/BrainF/BrainF.cpp b/examples/BrainF/BrainF.cpp index d3261d79965..7ae268f5929 100644 --- a/examples/BrainF/BrainF.cpp +++ b/examples/BrainF/BrainF.cpp @@ -128,6 +128,7 @@ void BrainF::header(LLVMContext& C) { get("Error: The head has left the tape.", true); GlobalVariable *aberrormsg = new GlobalVariable( + module->getContext(), msg_0->getType(), true, GlobalValue::InternalLinkage, diff --git a/include/llvm/GlobalVariable.h b/include/llvm/GlobalVariable.h index ae64ccf614b..a6505ad9cba 100644 --- a/include/llvm/GlobalVariable.h +++ b/include/llvm/GlobalVariable.h @@ -26,6 +26,7 @@ namespace llvm { +class LLVMContext; class Module; class Constant; template @@ -49,13 +50,15 @@ public: } /// GlobalVariable ctor - If a parent module is specified, the global is /// automatically inserted into the end of the specified modules global list. - GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage, + GlobalVariable(LLVMContext &Context, const Type *Ty, + bool isConstant, LinkageTypes Linkage, Constant *Initializer = 0, const std::string &Name = "", Module *Parent = 0, bool ThreadLocal = false, unsigned AddressSpace = 0); /// GlobalVariable ctor - This creates a global and inserts it before the /// specified other global. - GlobalVariable(const Type *Ty, bool isConstant, LinkageTypes Linkage, + GlobalVariable(LLVMContext &Context, const Type *Ty, + bool isConstant, LinkageTypes Linkage, Constant *Initializer, const std::string &Name, GlobalVariable *InsertBefore, bool ThreadLocal = false, unsigned AddressSpace = 0); diff --git a/include/llvm/Support/IRBuilder.h b/include/llvm/Support/IRBuilder.h index ed6a3f19ef7..f05ee861488 100644 --- a/include/llvm/Support/IRBuilder.h +++ b/include/llvm/Support/IRBuilder.h @@ -20,6 +20,7 @@ #include "llvm/GlobalAlias.h" #include "llvm/GlobalVariable.h" #include "llvm/Function.h" +#include "llvm/LLVMContext.h" #include "llvm/Support/ConstantFolder.h" namespace llvm { @@ -42,12 +43,16 @@ template class IRBuilder{ BasicBlock *BB; BasicBlock::iterator InsertPt; T Folder; + LLVMContext& Context; public: - IRBuilder(const T& F = T()) : Folder(F) { ClearInsertionPoint(); } - explicit IRBuilder(BasicBlock *TheBB, const T& F = T()) - : Folder(F) { SetInsertPoint(TheBB); } - IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F = T()) - : Folder(F) { SetInsertPoint(TheBB, IP); } + IRBuilder(const T& F = T(), LLVMContext &C = getGlobalContext()) : + Folder(F), Context(C) { ClearInsertionPoint(); } + explicit IRBuilder(BasicBlock *TheBB, const T& F = T(), + LLVMContext &C = getGlobalContext()) : + Folder(F), Context(C) { SetInsertPoint(TheBB); } + IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F = T(), + LLVMContext &C = getGlobalContext()) + : Folder(F), Context(C) { SetInsertPoint(TheBB, IP); } /// getFolder - Get the constant folder being used. const T& getFolder() { return Folder; } @@ -125,7 +130,7 @@ public: /// ReturnInst *CreateAggregateRet(Value * const* retVals, unsigned N) { const Type *RetType = BB->getParent()->getReturnType(); - Value *V = UndefValue::get(RetType); + Value *V = Context.getUndef(RetType); for (unsigned i = 0; i != N; ++i) V = CreateInsertValue(V, retVals[i], i, "mrv"); return Insert(ReturnInst::Create(V)); @@ -349,7 +354,7 @@ public: return Insert(GetElementPtrInst::Create(Ptr, Idx), Name); } Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const char *Name = "") { - Value *Idx = ConstantInt::get(Type::Int32Ty, Idx0); + Value *Idx = Context.getConstantInt(Type::Int32Ty, Idx0); if (Constant *PC = dyn_cast(Ptr)) return Folder.CreateGetElementPtr(PC, &Idx, 1); @@ -359,8 +364,8 @@ public: Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1, const char *Name = "") { Value *Idxs[] = { - ConstantInt::get(Type::Int32Ty, Idx0), - ConstantInt::get(Type::Int32Ty, Idx1) + Context.getConstantInt(Type::Int32Ty, Idx0), + Context.getConstantInt(Type::Int32Ty, Idx1) }; if (Constant *PC = dyn_cast(Ptr)) @@ -369,7 +374,7 @@ public: return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name); } Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const char *Name = "") { - Value *Idx = ConstantInt::get(Type::Int64Ty, Idx0); + Value *Idx = Context.getConstantInt(Type::Int64Ty, Idx0); if (Constant *PC = dyn_cast(Ptr)) return Folder.CreateGetElementPtr(PC, &Idx, 1); @@ -379,8 +384,8 @@ public: Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1, const char *Name = "") { Value *Idxs[] = { - ConstantInt::get(Type::Int64Ty, Idx0), - ConstantInt::get(Type::Int64Ty, Idx1) + Context.getConstantInt(Type::Int64Ty, Idx0), + Context.getConstantInt(Type::Int64Ty, Idx1) }; if (Constant *PC = dyn_cast(Ptr)) @@ -392,8 +397,9 @@ public: return CreateConstGEP2_32(Ptr, 0, Idx, Name); } Value *CreateGlobalString(const char *Str = "", const char *Name = "") { - Constant *StrConstant = ConstantArray::get(Str, true); - GlobalVariable *gv = new GlobalVariable(StrConstant->getType(), + Constant *StrConstant = Context.getConstantArray(Str, true); + GlobalVariable *gv = new GlobalVariable(Context, + StrConstant->getType(), true, GlobalValue::InternalLinkage, StrConstant, @@ -405,7 +411,7 @@ public: } Value *CreateGlobalStringPtr(const char *Str = "", const char *Name = "") { Value *gv = CreateGlobalString(Str, Name); - Value *zero = ConstantInt::get(Type::Int32Ty, 0); + Value *zero = Context.getConstantInt(Type::Int32Ty, 0); Value *Args[] = { zero, zero }; return CreateGEP(gv, Args, Args+2, Name); } @@ -697,13 +703,13 @@ public: /// CreateIsNull - Return an i1 value testing if \arg Arg is null. Value *CreateIsNull(Value *Arg, const char *Name = "") { - return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()), + return CreateICmpEQ(Arg, Context.getNullValue(Arg->getType()), Name); } /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null. Value *CreateIsNotNull(Value *Arg, const char *Name = "") { - return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()), + return CreateICmpNE(Arg, Context.getNullValue(Arg->getType()), Name); } @@ -718,7 +724,7 @@ public: Value *RHS_int = CreatePtrToInt(RHS, Type::Int64Ty); Value *Difference = CreateSub(LHS_int, RHS_int); return CreateSDiv(Difference, - ConstantExpr::getSizeOf(ArgType->getElementType()), + Context.getConstantExprSizeOf(ArgType->getElementType()), Name); } }; diff --git a/lib/Analysis/DebugInfo.cpp b/lib/Analysis/DebugInfo.cpp index 99a02443139..363ecabaf2b 100644 --- a/lib/Analysis/DebugInfo.cpp +++ b/lib/Analysis/DebugInfo.cpp @@ -490,7 +490,7 @@ Constant *DIFactory::GetStringConstant(const std::string &String) { Constant *ConstStr = VMContext.getConstantArray(String); // Otherwise create and return a new string global. - GlobalVariable *StrGV = new GlobalVariable(ConstStr->getType(), true, + GlobalVariable *StrGV = new GlobalVariable(VMContext,ConstStr->getType(), true, GlobalVariable::InternalLinkage, ConstStr, ".str", &M); StrGV->setSection("llvm.metadata"); @@ -516,7 +516,7 @@ DIArray DIFactory::GetOrCreateArray(DIDescriptor *Tys, unsigned NumTys) { DIDescriptor &Entry = SimpleConstantCache[Init]; if (!Entry.isNull()) return DIArray(Entry.getGV()); - GlobalVariable *GV = new GlobalVariable(Init->getType(), true, + GlobalVariable *GV = new GlobalVariable(VMContext, Init->getType(), true, GlobalValue::InternalLinkage, Init, "llvm.dbg.array", &M); GV->setSection("llvm.metadata"); @@ -542,7 +542,7 @@ DISubrange DIFactory::GetOrCreateSubrange(int64_t Lo, int64_t Hi) { M.addTypeName("llvm.dbg.subrange.type", Init->getType()); - GlobalVariable *GV = new GlobalVariable(Init->getType(), true, + GlobalVariable *GV = new GlobalVariable(VMContext, Init->getType(), true, GlobalValue::InternalLinkage, Init, "llvm.dbg.subrange", &M); GV->setSection("llvm.metadata"); @@ -579,7 +579,7 @@ DICompileUnit DIFactory::CreateCompileUnit(unsigned LangID, sizeof(Elts)/sizeof(Elts[0])); M.addTypeName("llvm.dbg.compile_unit.type", Init->getType()); - GlobalVariable *GV = new GlobalVariable(Init->getType(), true, + GlobalVariable *GV = new GlobalVariable(VMContext, Init->getType(), true, GlobalValue::LinkOnceAnyLinkage, Init, "llvm.dbg.compile_unit", &M); GV->setSection("llvm.metadata"); @@ -598,7 +598,7 @@ DIEnumerator DIFactory::CreateEnumerator(const std::string &Name, uint64_t Val){ sizeof(Elts)/sizeof(Elts[0])); M.addTypeName("llvm.dbg.enumerator.type", Init->getType()); - GlobalVariable *GV = new GlobalVariable(Init->getType(), true, + GlobalVariable *GV = new GlobalVariable(VMContext, Init->getType(), true, GlobalValue::InternalLinkage, Init, "llvm.dbg.enumerator", &M); GV->setSection("llvm.metadata"); @@ -632,7 +632,7 @@ DIBasicType DIFactory::CreateBasicType(DIDescriptor Context, sizeof(Elts)/sizeof(Elts[0])); M.addTypeName("llvm.dbg.basictype.type", Init->getType()); - GlobalVariable *GV = new GlobalVariable(Init->getType(), true, + GlobalVariable *GV = new GlobalVariable(VMContext, Init->getType(), true, GlobalValue::InternalLinkage, Init, "llvm.dbg.basictype", &M); GV->setSection("llvm.metadata"); @@ -668,7 +668,7 @@ DIDerivedType DIFactory::CreateDerivedType(unsigned Tag, sizeof(Elts)/sizeof(Elts[0])); M.addTypeName("llvm.dbg.derivedtype.type", Init->getType()); - GlobalVariable *GV = new GlobalVariable(Init->getType(), true, + GlobalVariable *GV = new GlobalVariable(VMContext, Init->getType(), true, GlobalValue::InternalLinkage, Init, "llvm.dbg.derivedtype", &M); GV->setSection("llvm.metadata"); @@ -708,7 +708,7 @@ DICompositeType DIFactory::CreateCompositeType(unsigned Tag, sizeof(Elts)/sizeof(Elts[0])); M.addTypeName("llvm.dbg.composite.type", Init->getType()); - GlobalVariable *GV = new GlobalVariable(Init->getType(), true, + GlobalVariable *GV = new GlobalVariable(VMContext, Init->getType(), true, GlobalValue::InternalLinkage, Init, "llvm.dbg.composite", &M); GV->setSection("llvm.metadata"); @@ -746,7 +746,7 @@ DISubprogram DIFactory::CreateSubprogram(DIDescriptor Context, sizeof(Elts)/sizeof(Elts[0])); M.addTypeName("llvm.dbg.subprogram.type", Init->getType()); - GlobalVariable *GV = new GlobalVariable(Init->getType(), true, + GlobalVariable *GV = new GlobalVariable(VMContext, Init->getType(), true, GlobalValue::LinkOnceAnyLinkage, Init, "llvm.dbg.subprogram", &M); GV->setSection("llvm.metadata"); @@ -780,7 +780,7 @@ DIFactory::CreateGlobalVariable(DIDescriptor Context, const std::string &Name, sizeof(Elts)/sizeof(Elts[0])); M.addTypeName("llvm.dbg.global_variable.type", Init->getType()); - GlobalVariable *GV = new GlobalVariable(Init->getType(), true, + GlobalVariable *GV = new GlobalVariable(VMContext, Init->getType(), true, GlobalValue::LinkOnceAnyLinkage, Init, "llvm.dbg.global_variable", &M); GV->setSection("llvm.metadata"); @@ -806,7 +806,7 @@ DIVariable DIFactory::CreateVariable(unsigned Tag, DIDescriptor Context, sizeof(Elts)/sizeof(Elts[0])); M.addTypeName("llvm.dbg.variable.type", Init->getType()); - GlobalVariable *GV = new GlobalVariable(Init->getType(), true, + GlobalVariable *GV = new GlobalVariable(VMContext, Init->getType(), true, GlobalValue::InternalLinkage, Init, "llvm.dbg.variable", &M); GV->setSection("llvm.metadata"); @@ -826,7 +826,7 @@ DIBlock DIFactory::CreateBlock(DIDescriptor Context) { sizeof(Elts)/sizeof(Elts[0])); M.addTypeName("llvm.dbg.block.type", Init->getType()); - GlobalVariable *GV = new GlobalVariable(Init->getType(), true, + GlobalVariable *GV = new GlobalVariable(VMContext, Init->getType(), true, GlobalValue::InternalLinkage, Init, "llvm.dbg.block", &M); GV->setSection("llvm.metadata"); diff --git a/lib/AsmParser/LLParser.cpp b/lib/AsmParser/LLParser.cpp index 3966ab3b5fc..49509d52245 100644 --- a/lib/AsmParser/LLParser.cpp +++ b/lib/AsmParser/LLParser.cpp @@ -516,7 +516,8 @@ bool LLParser::ParseGlobal(const std::string &Name, LocTy NameLoc, } if (GV == 0) { - GV = new GlobalVariable(Ty, false, GlobalValue::ExternalLinkage, 0, Name, + GV = new GlobalVariable(Context, Ty, false, + GlobalValue::ExternalLinkage, 0, Name, M, false, AddrSpace); } else { if (GV->getType()->getElementType() != Ty) @@ -607,7 +608,7 @@ GlobalValue *LLParser::GetGlobalVal(const std::string &Name, const Type *Ty, FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, Name, M); } else { - FwdVal = new GlobalVariable(PTy->getElementType(), false, + FwdVal = new GlobalVariable(Context, PTy->getElementType(), false, GlobalValue::ExternalWeakLinkage, 0, Name, M); } @@ -651,7 +652,7 @@ GlobalValue *LLParser::GetGlobalVal(unsigned ID, const Type *Ty, LocTy Loc) { } FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, "", M); } else { - FwdVal = new GlobalVariable(PTy->getElementType(), false, + FwdVal = new GlobalVariable(Context, PTy->getElementType(), false, GlobalValue::ExternalWeakLinkage, 0, "", M); } diff --git a/lib/Bitcode/Reader/BitcodeReader.cpp b/lib/Bitcode/Reader/BitcodeReader.cpp index d2cf93ec17e..b940b9e3ec0 100644 --- a/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/lib/Bitcode/Reader/BitcodeReader.cpp @@ -1258,7 +1258,7 @@ bool BitcodeReader::ParseModule(const std::string &ModuleID) { isThreadLocal = Record[7]; GlobalVariable *NewGV = - new GlobalVariable(Ty, isConstant, Linkage, 0, "", TheModule, + new GlobalVariable(Context, Ty, isConstant, Linkage, 0, "", TheModule, isThreadLocal, AddressSpace); NewGV->setAlignment(Alignment); if (!Section.empty()) diff --git a/lib/CodeGen/ShadowStackGC.cpp b/lib/CodeGen/ShadowStackGC.cpp index 2402f81bb04..efe7c0a6a9d 100644 --- a/lib/CodeGen/ShadowStackGC.cpp +++ b/lib/CodeGen/ShadowStackGC.cpp @@ -229,7 +229,7 @@ Constant *ShadowStackGC::GetFrameMap(Function &F) { // to be a ModulePass (which means it cannot be in the 'llc' pipeline // (which uses a FunctionPassManager (which segfaults (not asserts) if // provided a ModulePass))). - Constant *GV = new GlobalVariable(FrameMap->getType(), true, + Constant *GV = new GlobalVariable(*F.getContext(), FrameMap->getType(), true, GlobalVariable::InternalLinkage, FrameMap, "__gc_" + F.getName(), F.getParent()); @@ -292,7 +292,7 @@ bool ShadowStackGC::initializeCustomLowering(Module &M) { if (!Head) { // If the root chain does not exist, insert a new one with linkonce // linkage! - Head = new GlobalVariable(StackEntryPtrTy, false, + Head = new GlobalVariable(M.getContext(), StackEntryPtrTy, false, GlobalValue::LinkOnceAnyLinkage, Constant::getNullValue(StackEntryPtrTy), "llvm_gc_root_chain", &M); diff --git a/lib/Linker/LinkModules.cpp b/lib/Linker/LinkModules.cpp index b7ab5dff5d6..cb2b9e46b6a 100644 --- a/lib/Linker/LinkModules.cpp +++ b/lib/Linker/LinkModules.cpp @@ -573,7 +573,7 @@ static bool LinkGlobals(Module *Dest, const Module *Src, // symbol over in the dest module... the initializer will be filled in // later by LinkGlobalInits. GlobalVariable *NewDGV = - new GlobalVariable(SGV->getType()->getElementType(), + new GlobalVariable(Context, SGV->getType()->getElementType(), SGV->isConstant(), SGV->getLinkage(), /*init*/0, SGV->getName(), Dest, false, SGV->getType()->getAddressSpace()); @@ -606,7 +606,7 @@ static bool LinkGlobals(Module *Dest, const Module *Src, // AppendingVars map. The name is cleared out so that no linkage is // performed. GlobalVariable *NewDGV = - new GlobalVariable(SGV->getType()->getElementType(), + new GlobalVariable(Context, SGV->getType()->getElementType(), SGV->isConstant(), SGV->getLinkage(), /*init*/0, "", Dest, false, SGV->getType()->getAddressSpace()); @@ -634,8 +634,9 @@ static bool LinkGlobals(Module *Dest, const Module *Src, // we are replacing may be a function (if a prototype, weak, etc) or a // global variable. GlobalVariable *NewDGV = - new GlobalVariable(SGV->getType()->getElementType(), SGV->isConstant(), - NewLinkage, /*init*/0, DGV->getName(), Dest, false, + new GlobalVariable(Context, SGV->getType()->getElementType(), + SGV->isConstant(), NewLinkage, /*init*/0, + DGV->getName(), Dest, false, SGV->getType()->getAddressSpace()); // Propagate alignment, section, and visibility info. @@ -1156,7 +1157,7 @@ static bool LinkAppendingVars(Module *M, // Create the new global variable... GlobalVariable *NG = - new GlobalVariable(NewType, G1->isConstant(), G1->getLinkage(), + new GlobalVariable(Context, NewType, G1->isConstant(), G1->getLinkage(), /*init*/0, First->first, M, G1->isThreadLocal(), G1->getType()->getAddressSpace()); diff --git a/lib/Transforms/IPO/ExtractGV.cpp b/lib/Transforms/IPO/ExtractGV.cpp index 8cd5deb6dfe..e26bd3a2a0a 100644 --- a/lib/Transforms/IPO/ExtractGV.cpp +++ b/lib/Transforms/IPO/ExtractGV.cpp @@ -108,7 +108,7 @@ namespace { } ArrayType *AT = Context->getArrayType(SBP, AUGs.size()); Constant *Init = Context->getConstantArray(AT, AUGs); - GlobalValue *gv = new GlobalVariable(AT, false, + GlobalValue *gv = new GlobalVariable(M.getContext(), AT, false, GlobalValue::AppendingLinkage, Init, "llvm.used", &M); gv->setSection("llvm.metadata"); diff --git a/lib/Transforms/IPO/GlobalOpt.cpp b/lib/Transforms/IPO/GlobalOpt.cpp index f394a3af609..eb13067ff0c 100644 --- a/lib/Transforms/IPO/GlobalOpt.cpp +++ b/lib/Transforms/IPO/GlobalOpt.cpp @@ -490,12 +490,13 @@ static GlobalVariable *SRAGlobal(GlobalVariable *GV, const TargetData &TD, Context->getConstantInt(Type::Int32Ty, i), Context); assert(In && "Couldn't get element of initializer?"); - GlobalVariable *NGV = new GlobalVariable(STy->getElementType(i), false, + GlobalVariable *NGV = new GlobalVariable(*Context, STy->getElementType(i), + false, GlobalVariable::InternalLinkage, In, GV->getName()+"."+utostr(i), (Module *)NULL, GV->isThreadLocal(), - GV->getType()->getAddressSpace()); + GV->getType()->getAddressSpace()); Globals.insert(GV, NGV); NewGlobals.push_back(NGV); @@ -526,7 +527,8 @@ static GlobalVariable *SRAGlobal(GlobalVariable *GV, const TargetData &TD, Context); assert(In && "Couldn't get element of initializer?"); - GlobalVariable *NGV = new GlobalVariable(STy->getElementType(), false, + GlobalVariable *NGV = new GlobalVariable(*Context, STy->getElementType(), + false, GlobalVariable::InternalLinkage, In, GV->getName()+"."+utostr(i), (Module *)NULL, @@ -841,7 +843,8 @@ static GlobalVariable *OptimizeGlobalAddressOfMalloc(GlobalVariable *GV, // Create the new global variable. The contents of the malloc'd memory is // undefined, so initialize with an undef value. Constant *Init = Context->getUndef(MI->getAllocatedType()); - GlobalVariable *NewGV = new GlobalVariable(MI->getAllocatedType(), false, + GlobalVariable *NewGV = new GlobalVariable(*Context, MI->getAllocatedType(), + false, GlobalValue::InternalLinkage, Init, GV->getName()+".body", (Module *)NULL, @@ -862,7 +865,8 @@ static GlobalVariable *OptimizeGlobalAddressOfMalloc(GlobalVariable *GV, // If there is a comparison against null, we will insert a global bool to // keep track of whether the global was initialized yet or not. GlobalVariable *InitBool = - new GlobalVariable(Type::Int1Ty, false, GlobalValue::InternalLinkage, + new GlobalVariable(*Context, Type::Int1Ty, false, + GlobalValue::InternalLinkage, Context->getConstantIntFalse(), GV->getName()+".init", (Module *)NULL, GV->isThreadLocal()); bool InitBoolUsed = false; @@ -1282,7 +1286,8 @@ static GlobalVariable *PerformHeapAllocSRoA(GlobalVariable *GV, MallocInst *MI, const Type *PFieldTy = Context->getPointerTypeUnqual(FieldTy); GlobalVariable *NGV = - new GlobalVariable(PFieldTy, false, GlobalValue::InternalLinkage, + new GlobalVariable(*Context, PFieldTy, false, + GlobalValue::InternalLinkage, Context->getNullValue(PFieldTy), GV->getName() + ".f" + utostr(FieldNo), GV, GV->isThreadLocal()); @@ -1579,7 +1584,7 @@ static bool TryToShrinkGlobalToBoolean(GlobalVariable *GV, Constant *OtherVal, DOUT << " *** SHRINKING TO BOOL: " << *GV; // Create the new global, initializing it to false. - GlobalVariable *NewGV = new GlobalVariable(Type::Int1Ty, false, + GlobalVariable *NewGV = new GlobalVariable(*Context, Type::Int1Ty, false, GlobalValue::InternalLinkage, Context->getConstantIntFalse(), GV->getName()+".b", (Module *)NULL, @@ -1974,7 +1979,8 @@ static GlobalVariable *InstallGlobalCtors(GlobalVariable *GCL, } // Create the new global and insert it next to the existing list. - GlobalVariable *NGV = new GlobalVariable(CA->getType(), GCL->isConstant(), + GlobalVariable *NGV = new GlobalVariable(*Context, CA->getType(), + GCL->isConstant(), GCL->getLinkage(), CA, "", (Module *)NULL, GCL->isThreadLocal()); @@ -2222,7 +2228,7 @@ static bool EvaluateFunction(Function *F, Constant *&RetVal, } else if (AllocaInst *AI = dyn_cast(CurInst)) { if (AI->isArrayAllocation()) return false; // Cannot handle array allocs. const Type *Ty = AI->getType()->getElementType(); - AllocaTmps.push_back(new GlobalVariable(Ty, false, + AllocaTmps.push_back(new GlobalVariable(*Context, Ty, false, GlobalValue::InternalLinkage, Context->getUndef(Ty), AI->getName())); diff --git a/lib/Transforms/Instrumentation/BlockProfiling.cpp b/lib/Transforms/Instrumentation/BlockProfiling.cpp index 913680cdd09..6dfcc0b0512 100644 --- a/lib/Transforms/Instrumentation/BlockProfiling.cpp +++ b/lib/Transforms/Instrumentation/BlockProfiling.cpp @@ -65,7 +65,7 @@ bool FunctionProfiler::runOnModule(Module &M) { const Type *ATy = Context->getArrayType(Type::Int32Ty, NumFunctions); GlobalVariable *Counters = - new GlobalVariable(ATy, false, GlobalValue::InternalLinkage, + new GlobalVariable(M.getContext(), ATy, false, GlobalValue::InternalLinkage, Context->getNullValue(ATy), "FuncProfCounters", &M); // Instrument all of the functions... @@ -110,7 +110,7 @@ bool BlockProfiler::runOnModule(Module &M) { const Type *ATy = Context->getArrayType(Type::Int32Ty, NumBlocks); GlobalVariable *Counters = - new GlobalVariable(ATy, false, GlobalValue::InternalLinkage, + new GlobalVariable(M.getContext(), ATy, false, GlobalValue::InternalLinkage, Context->getNullValue(ATy), "BlockProfCounters", &M); // Instrument all of the blocks... diff --git a/lib/Transforms/Instrumentation/EdgeProfiling.cpp b/lib/Transforms/Instrumentation/EdgeProfiling.cpp index 88825b164cb..b625341273a 100644 --- a/lib/Transforms/Instrumentation/EdgeProfiling.cpp +++ b/lib/Transforms/Instrumentation/EdgeProfiling.cpp @@ -66,7 +66,7 @@ bool EdgeProfiler::runOnModule(Module &M) { const Type *ATy = Context->getArrayType(Type::Int32Ty, NumEdges); GlobalVariable *Counters = - new GlobalVariable(ATy, false, GlobalValue::InternalLinkage, + new GlobalVariable(M.getContext(), ATy, false, GlobalValue::InternalLinkage, Context->getNullValue(ATy), "EdgeProfCounters", &M); // Instrument all of the edges... diff --git a/lib/Transforms/Instrumentation/RSProfiling.cpp b/lib/Transforms/Instrumentation/RSProfiling.cpp index e487d2fb478..0999a279ef0 100644 --- a/lib/Transforms/Instrumentation/RSProfiling.cpp +++ b/lib/Transforms/Instrumentation/RSProfiling.cpp @@ -198,7 +198,8 @@ GlobalRandomCounter::GlobalRandomCounter(Module& M, const IntegerType* t, uint64_t resetval) : T(t) { ConstantInt* Init = M.getContext().getConstantInt(T, resetval); ResetValue = Init; - Counter = new GlobalVariable(T, false, GlobalValue::InternalLinkage, + Counter = new GlobalVariable(M.getContext(), T, false, + GlobalValue::InternalLinkage, Init, "RandomSteeringCounter", &M); } @@ -237,7 +238,8 @@ GlobalRandomCounterOpt::GlobalRandomCounterOpt(Module& M, const IntegerType* t, : AI(0), T(t) { ConstantInt* Init = M.getContext().getConstantInt(T, resetval); ResetValue = Init; - Counter = new GlobalVariable(T, false, GlobalValue::InternalLinkage, + Counter = new GlobalVariable(M.getContext(), T, false, + GlobalValue::InternalLinkage, Init, "RandomSteeringCounter", &M); } diff --git a/lib/Transforms/Scalar/SimplifyLibCalls.cpp b/lib/Transforms/Scalar/SimplifyLibCalls.cpp index ec48469f536..72308c80af3 100644 --- a/lib/Transforms/Scalar/SimplifyLibCalls.cpp +++ b/lib/Transforms/Scalar/SimplifyLibCalls.cpp @@ -1290,7 +1290,8 @@ struct VISIBILITY_HIDDEN PrintFOpt : public LibCallOptimization { // pass to be run after this pass, to merge duplicate strings. FormatStr.erase(FormatStr.end()-1); Constant *C = Context->getConstantArray(FormatStr, true); - C = new GlobalVariable(C->getType(), true,GlobalVariable::InternalLinkage, + C = new GlobalVariable(*Context, C->getType(), + true, GlobalVariable::InternalLinkage, C, "str", Callee->getParent()); EmitPutS(C, B); return CI->use_empty() ? (Value*)CI : diff --git a/lib/Transforms/Utils/CloneModule.cpp b/lib/Transforms/Utils/CloneModule.cpp index f6056364a7c..afebd5bd5d1 100644 --- a/lib/Transforms/Utils/CloneModule.cpp +++ b/lib/Transforms/Utils/CloneModule.cpp @@ -56,7 +56,8 @@ Module *llvm::CloneModule(const Module *M, // for (Module::const_global_iterator I = M->global_begin(), E = M->global_end(); I != E; ++I) { - GlobalVariable *GV = new GlobalVariable(I->getType()->getElementType(), + GlobalVariable *GV = new GlobalVariable(M->getContext(), + I->getType()->getElementType(), false, GlobalValue::ExternalLinkage, 0, I->getName(), New); diff --git a/lib/Transforms/Utils/LowerInvoke.cpp b/lib/Transforms/Utils/LowerInvoke.cpp index 8a585d2283f..1eefdc4b3ca 100644 --- a/lib/Transforms/Utils/LowerInvoke.cpp +++ b/lib/Transforms/Utils/LowerInvoke.cpp @@ -139,7 +139,8 @@ bool LowerInvoke::doInitialization(Module &M) { // Now that we've done that, insert the jmpbuf list head global, unless it // already exists. if (!(JBListHead = M.getGlobalVariable("llvm.sjljeh.jblist", PtrJBList))) { - JBListHead = new GlobalVariable(PtrJBList, false, + JBListHead = new GlobalVariable(M.getContext(), + PtrJBList, false, GlobalValue::LinkOnceAnyLinkage, Context->getNullValue(PtrJBList), "llvm.sjljeh.jblist", &M); @@ -182,7 +183,8 @@ void LowerInvoke::createAbortMessage(Module *M) { Context->getConstantArray("ERROR: Exception thrown, but not caught!\n"); AbortMessageLength = Msg->getNumOperands()-1; // don't include \0 - GlobalVariable *MsgGV = new GlobalVariable(Msg->getType(), true, + GlobalVariable *MsgGV = new GlobalVariable(M->getContext(), + Msg->getType(), true, GlobalValue::InternalLinkage, Msg, "abortmsg", M); std::vector GEPIdx(2, Context->getNullValue(Type::Int32Ty)); @@ -195,7 +197,8 @@ void LowerInvoke::createAbortMessage(Module *M) { "Recompile program with -enable-correct-eh-support.\n"); AbortMessageLength = Msg->getNumOperands()-1; // don't include \0 - GlobalVariable *MsgGV = new GlobalVariable(Msg->getType(), true, + GlobalVariable *MsgGV = new GlobalVariable(M->getContext(), + Msg->getType(), true, GlobalValue::InternalLinkage, Msg, "abortmsg", M); std::vector GEPIdx(2, Context->getNullValue(Type::Int32Ty)); diff --git a/lib/VMCore/Core.cpp b/lib/VMCore/Core.cpp index efc229d6f57..1ac66ed660b 100644 --- a/lib/VMCore/Core.cpp +++ b/lib/VMCore/Core.cpp @@ -700,7 +700,8 @@ void LLVMSetAlignment(LLVMValueRef Global, unsigned Bytes) { /*--.. Operations on global variables ......................................--*/ LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) { - return wrap(new GlobalVariable(unwrap(Ty), false, + LLVMContext &Context = unwrap(M)->getContext(); + return wrap(new GlobalVariable(Context, unwrap(Ty), false, GlobalValue::ExternalLinkage, 0, Name, unwrap(M))); } diff --git a/lib/VMCore/Globals.cpp b/lib/VMCore/Globals.cpp index 5abe1f9ac40..c31b7b5e76c 100644 --- a/lib/VMCore/Globals.cpp +++ b/lib/VMCore/Globals.cpp @@ -16,6 +16,7 @@ #include "llvm/GlobalVariable.h" #include "llvm/GlobalAlias.h" #include "llvm/DerivedTypes.h" +#include "llvm/LLVMContext.h" #include "llvm/Module.h" #include "llvm/ADT/SmallPtrSet.h" #include "llvm/Support/LeakDetector.h" @@ -93,11 +94,13 @@ void GlobalValue::copyAttributesFrom(const GlobalValue *Src) { // GlobalVariable Implementation //===----------------------------------------------------------------------===// -GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link, +GlobalVariable::GlobalVariable(LLVMContext &Context, const Type *Ty, + bool constant, LinkageTypes Link, Constant *InitVal, const std::string &Name, Module *ParentModule, bool ThreadLocal, unsigned AddressSpace) - : GlobalValue(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal, + : GlobalValue(Context.getPointerType(Ty, AddressSpace), + Value::GlobalVariableVal, OperandTraits::op_begin(this), InitVal != 0, Link, Name), isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) { @@ -113,11 +116,13 @@ GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link, ParentModule->getGlobalList().push_back(this); } -GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link, +GlobalVariable::GlobalVariable(LLVMContext &Context, const Type *Ty, + bool constant, LinkageTypes Link, Constant *InitVal, const std::string &Name, GlobalVariable *Before, bool ThreadLocal, unsigned AddressSpace) - : GlobalValue(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal, + : GlobalValue(Context.getPointerType(Ty, AddressSpace), + Value::GlobalVariableVal, OperandTraits::op_begin(this), InitVal != 0, Link, Name), isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) { diff --git a/lib/VMCore/Module.cpp b/lib/VMCore/Module.cpp index f057e81a649..6f3de0257e4 100644 --- a/lib/VMCore/Module.cpp +++ b/lib/VMCore/Module.cpp @@ -28,17 +28,20 @@ using namespace llvm; //===----------------------------------------------------------------------===// // Methods to implement the globals and functions lists. +// NOTE: It is ok to allocate the globals used for these methods from the +// global context, because all we ever do is use them to compare for equality. // GlobalVariable *ilist_traits::createSentinel() { - GlobalVariable *Ret = new GlobalVariable(Type::Int32Ty, false, + GlobalVariable *Ret = new GlobalVariable(getGlobalContext(), + Type::Int32Ty, false, GlobalValue::ExternalLinkage); // This should not be garbage monitored. LeakDetector::removeGarbageObject(Ret); return Ret; } GlobalAlias *ilist_traits::createSentinel() { - GlobalAlias *Ret = new GlobalAlias(Type::Int32Ty, + GlobalAlias *Ret = new GlobalAlias(Type::Int32Ty, GlobalValue::ExternalLinkage); // This should not be garbage monitored. LeakDetector::removeGarbageObject(Ret); @@ -270,7 +273,8 @@ Constant *Module::getOrInsertGlobal(const std::string &Name, const Type *Ty) { if (GV == 0) { // Nope, add it GlobalVariable *New = - new GlobalVariable(Ty, false, GlobalVariable::ExternalLinkage, 0, Name); + new GlobalVariable(getContext(), Ty, false, + GlobalVariable::ExternalLinkage, 0, Name); GlobalList.push_back(New); return New; // Return the new declaration. } diff --git a/tools/bugpoint/ExtractFunction.cpp b/tools/bugpoint/ExtractFunction.cpp index e4affbb0ddc..84eb1e7f743 100644 --- a/tools/bugpoint/ExtractFunction.cpp +++ b/tools/bugpoint/ExtractFunction.cpp @@ -236,7 +236,8 @@ static void SplitStaticCtorDtor(const char *GlobalName, Module *M1, Module *M2, GV->eraseFromParent(); if (!M1Tors.empty()) { Constant *M1Init = GetTorInit(M1Tors); - new GlobalVariable(M1Init->getType(), false, GlobalValue::AppendingLinkage, + new GlobalVariable(M1->getContext(), M1Init->getType(), false, + GlobalValue::AppendingLinkage, M1Init, GlobalName, M1); } @@ -247,7 +248,8 @@ static void SplitStaticCtorDtor(const char *GlobalName, Module *M1, Module *M2, GV->eraseFromParent(); if (!M2Tors.empty()) { Constant *M2Init = GetTorInit(M2Tors); - new GlobalVariable(M2Init->getType(), false, GlobalValue::AppendingLinkage, + new GlobalVariable(M2->getContext(), M2Init->getType(), false, + GlobalValue::AppendingLinkage, M2Init, GlobalName, M2); } } diff --git a/tools/bugpoint/Miscompilation.cpp b/tools/bugpoint/Miscompilation.cpp index b3260e13606..369f25a5161 100644 --- a/tools/bugpoint/Miscompilation.cpp +++ b/tools/bugpoint/Miscompilation.cpp @@ -703,7 +703,8 @@ static void CleanupAndPrepareModules(BugDriver &BD, Module *&Test, // 1. Add a string constant with its name to the global file Constant *InitArray = ConstantArray::get(F->getName()); GlobalVariable *funcName = - new GlobalVariable(InitArray->getType(), true /*isConstant*/, + new GlobalVariable(Safe->getContext(), + InitArray->getType(), true /*isConstant*/, GlobalValue::InternalLinkage, InitArray, F->getName() + "_name", Safe); @@ -722,7 +723,8 @@ static void CleanupAndPrepareModules(BugDriver &BD, Module *&Test, // Create a new global to hold the cached function pointer. Constant *NullPtr = ConstantPointerNull::get(F->getType()); GlobalVariable *Cache = - new GlobalVariable(F->getType(), false,GlobalValue::InternalLinkage, + new GlobalVariable(F->getParent()->getContext(), + F->getType(), false,GlobalValue::InternalLinkage, NullPtr,F->getName()+".fpcache", F->getParent()); // Construct a new stub function that will re-route calls to F -- 2.34.1