Push LLVMContext through GlobalVariables and IRBuilder.
authorOwen Anderson <resistor@mac.com>
Wed, 8 Jul 2009 01:26:06 +0000 (01:26 +0000)
committerOwen Anderson <resistor@mac.com>
Wed, 8 Jul 2009 01:26:06 +0000 (01:26 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@74985 91177308-0d34-0410-b5e6-96231b3b80d8

21 files changed:
examples/BrainF/BrainF.cpp
include/llvm/GlobalVariable.h
include/llvm/Support/IRBuilder.h
lib/Analysis/DebugInfo.cpp
lib/AsmParser/LLParser.cpp
lib/Bitcode/Reader/BitcodeReader.cpp
lib/CodeGen/ShadowStackGC.cpp
lib/Linker/LinkModules.cpp
lib/Transforms/IPO/ExtractGV.cpp
lib/Transforms/IPO/GlobalOpt.cpp
lib/Transforms/Instrumentation/BlockProfiling.cpp
lib/Transforms/Instrumentation/EdgeProfiling.cpp
lib/Transforms/Instrumentation/RSProfiling.cpp
lib/Transforms/Scalar/SimplifyLibCalls.cpp
lib/Transforms/Utils/CloneModule.cpp
lib/Transforms/Utils/LowerInvoke.cpp
lib/VMCore/Core.cpp
lib/VMCore/Globals.cpp
lib/VMCore/Module.cpp
tools/bugpoint/ExtractFunction.cpp
tools/bugpoint/Miscompilation.cpp

index d3261d79965cc09f4da5cae40c38fdbcce006eec..7ae268f59297cf6e69926b5c513a68a05bd48caa 100644 (file)
@@ -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,
index ae64ccf614be3654cebeabd88a58fdd5974bf14b..a6505ad9cba69066c6f1805f6c3d6e71accc5423 100644 (file)
@@ -26,6 +26,7 @@
 
 namespace llvm {
 
+class LLVMContext;
 class Module;
 class Constant;
 template<typename ValueSubClass, typename ItemParentClass>
@@ -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);
index ed6a3f19ef7ab38a532eaf0b06d2995dc7911aff..f05ee861488cd6326514bf25c0086e5e8a042342 100644 (file)
@@ -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 <bool preserveNames=true, typename T = ConstantFolder> 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<Constant>(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<Constant>(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<Constant>(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<Constant>(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);
   }
 };
index 99a02443139ef23bfc676755721814eee221f80e..363ecabaf2b710de798f8bfc81e5f3ed4acb5b7d 100644 (file)
@@ -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");
index 3966ab3b5fc6e0dbeb791dfd3b1888c3e24043de..49509d5224574d2bbb07542bcf1d574e209eadbc 100644 (file)
@@ -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);
   }
   
index d2cf93ec17e38c250d3fa8810d3a1c55d32a07f7..b940b9e3ec01d7ad306dfef774ef5f6d564c9adf 100644 (file)
@@ -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())
index 2402f81bb04f3216438a43d9aba11548625e2de2..efe7c0a6a9d4bff1f7313629a8c6f00ad7a389c3 100644 (file)
@@ -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);
index b7ab5dff5d6db16d24b6e5622da6a6bec5f5838e..cb2b9e46b6a1da791c2725fd0427fa8fa9c397e8 100644 (file)
@@ -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());
 
index 8cd5deb6dfe7e1256c0c58d71b97cf5816ee7783..e26bd3a2a0a5ee4ae3ba42df364824cf5f6de4a9 100644 (file)
@@ -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");
index f394a3af609189f5839e837649fd0c1ec0d88d4e..eb13067ff0c8025bc3c770f595b537fb71803386 100644 (file)
@@ -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<AllocaInst>(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()));
index 913680cdd09f5ccfcff1f04ca02926ded0587b49..6dfcc0b0512e28b88639b22f5e81d51568be0356 100644 (file)
@@ -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...
index 88825b164cb2cda8f416aa4908c8925bc77be3dd..b625341273a8dc98a3a346207eea8f576d0e981e 100644 (file)
@@ -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...
index e487d2fb478efa1e10080d4e459e75047d8c3b6d..0999a279ef0dd379002be0dc9d879d49122d2272 100644 (file)
@@ -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);
 }
 
index ec48469f536ef5c8de1d7414148ace35e43f786d..72308c80af3c8ad5faaba90b15f8879c17ef14c9 100644 (file)
@@ -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 : 
index f6056364a7ce80403b0364f66e9e5d1392daddc0..afebd5bd5d158cd67c347da9370d0b7d66e30cd8 100644 (file)
@@ -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);
index 8a585d2283f6eee4b38ea66254aca9638c4d52ae..1eefdc4b3ca49d59764bb0ea26be0bf2a0df519a 100644 (file)
@@ -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<Constant*> 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<Constant*> GEPIdx(2, Context->getNullValue(Type::Int32Ty));
index efc229d6f57d0ef81ea66b2a071a07537e34e9cd..1ac66ed660bf3d28df375d45129d787d8cd86178 100644 (file)
@@ -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)));
 }
index 5abe1f9ac40d6cfb447ba5409b73e7c080b2f664..c31b7b5e76cd44f2a661010dfa15dca8c902db16 100644 (file)
@@ -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<GlobalVariable>::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<GlobalVariable>::op_begin(this),
                 InitVal != 0, Link, Name),
     isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
index f057e81a649bf0b3d3e2f27a715cd955b0c6c6ac..6f3de0257e439122b2b79f8769634714116392c6 100644 (file)
@@ -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<GlobalVariable>::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<GlobalAlias>::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.
   }
index e4affbb0ddcc331afab8e535476f336fc1d04fb9..84eb1e7f743331e436748df4594aae151bb67d2a 100644 (file)
@@ -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);
   }
 }
index b3260e13606f60609820b45eb2853c6f727b1109..369f25a516144cf28a47bdf3ceb308106c6c90b9 100644 (file)
@@ -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