llvm.org GIT mirror llvm / e9b11b4
Switch GlobalVariable ctors to a sane API, where *either* a context or a module is required. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@75025 91177308-0d34-0410-b5e6-96231b3b80d8 Owen Anderson 11 years ago
21 changed file(s) with 136 addition(s) and 166 deletion(s). Raw diff Collapse all Expand all
127127 get("Error: The head has left the tape.", true);
128128
129129 GlobalVariable *aberrormsg = new GlobalVariable(
130 module->getContext(),
130 *module,
131131 msg_0->getType(),
132132 true,
133133 GlobalValue::InternalLinkage,
134134 msg_0,
135 "aberrormsg",
136 module);
135 "aberrormsg");
137136
138137 //declare i32 @puts(i8 *)
139138 Function *puts_func = cast(module->
2525
2626 namespace llvm {
2727
28 class LLVMContext;
2928 class Module;
3029 class Constant;
30 class LLVMContext;
3131 template
3232 class SymbolTableListTraits;
3333
4949 }
5050 /// GlobalVariable ctor - If a parent module is specified, the global is
5151 /// automatically inserted into the end of the specified modules global list.
52 GlobalVariable(LLVMContext &Context, const Type *Ty,
53 bool isConstant, LinkageTypes Linkage,
52 GlobalVariable(LLVMContext &Context, const Type *Ty, bool isConstant,
53 LinkageTypes Linkage,
5454 Constant *Initializer = 0, const std::string &Name = "",
55 Module *Parent = 0, bool ThreadLocal = false,
56 unsigned AddressSpace = 0);
55 bool ThreadLocal = false, unsigned AddressSpace = 0);
5756 /// GlobalVariable ctor - This creates a global and inserts it before the
5857 /// specified other global.
59 GlobalVariable(LLVMContext &Context, const Type *Ty,
60 bool isConstant, LinkageTypes Linkage,
61 Constant *Initializer, const std::string &Name,
62 GlobalVariable *InsertBefore, bool ThreadLocal = false,
58 GlobalVariable(Module &M, const Type *Ty, bool isConstant,
59 LinkageTypes Linkage, Constant *Initializer,
60 const std::string &Name,
61 GlobalVariable *InsertBefore = 0, bool ThreadLocal = false,
6362 unsigned AddressSpace = 0);
6463
6564 ~GlobalVariable() {
1919 #include "llvm/GlobalAlias.h"
2020 #include "llvm/GlobalVariable.h"
2121 #include "llvm/Function.h"
22 #include "llvm/LLVMContext.h"
2322 #include "llvm/Support/ConstantFolder.h"
2423
2524 namespace llvm {
4241 BasicBlock *BB;
4342 BasicBlock::iterator InsertPt;
4443 T Folder;
45 LLVMContext& Context;
4644 public:
47 IRBuilder(const T& F = T(), LLVMContext &C = getGlobalContext()) :
48 Folder(F), Context(C) { ClearInsertionPoint(); }
49 explicit IRBuilder(BasicBlock *TheBB, const T& F = T(),
50 LLVMContext &C = getGlobalContext()) :
51 Folder(F), Context(C) { SetInsertPoint(TheBB); }
52 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F = T(),
53 LLVMContext &C = getGlobalContext())
54 : Folder(F), Context(C) { SetInsertPoint(TheBB, IP); }
45 IRBuilder(const T& F = T()) : Folder(F) { ClearInsertionPoint(); }
46 explicit IRBuilder(BasicBlock *TheBB, const T& F = T())
47 : Folder(F) { SetInsertPoint(TheBB); }
48 IRBuilder(BasicBlock *TheBB, BasicBlock::iterator IP, const T& F = T())
49 : Folder(F) { SetInsertPoint(TheBB, IP); }
5550
5651 /// getFolder - Get the constant folder being used.
5752 const T& getFolder() { return Folder; }
129124 ///
130125 ReturnInst *CreateAggregateRet(Value * const* retVals, unsigned N) {
131126 const Type *RetType = BB->getParent()->getReturnType();
132 Value *V = Context.getUndef(RetType);
127 Value *V = UndefValue::get(RetType);
133128 for (unsigned i = 0; i != N; ++i)
134129 V = CreateInsertValue(V, retVals[i], i, "mrv");
135130 return Insert(ReturnInst::Create(V));
353348 return Insert(GetElementPtrInst::Create(Ptr, Idx), Name);
354349 }
355350 Value *CreateConstGEP1_32(Value *Ptr, unsigned Idx0, const char *Name = "") {
356 Value *Idx = Context.getConstantInt(Type::Int32Ty, Idx0);
351 Value *Idx = ConstantInt::get(Type::Int32Ty, Idx0);
357352
358353 if (Constant *PC = dyn_cast(Ptr))
359354 return Folder.CreateGetElementPtr(PC, &Idx, 1);
363358 Value *CreateConstGEP2_32(Value *Ptr, unsigned Idx0, unsigned Idx1,
364359 const char *Name = "") {
365360 Value *Idxs[] = {
366 Context.getConstantInt(Type::Int32Ty, Idx0),
367 Context.getConstantInt(Type::Int32Ty, Idx1)
361 ConstantInt::get(Type::Int32Ty, Idx0),
362 ConstantInt::get(Type::Int32Ty, Idx1)
368363 };
369364
370365 if (Constant *PC = dyn_cast(Ptr))
373368 return Insert(GetElementPtrInst::Create(Ptr, Idxs, Idxs+2), Name);
374369 }
375370 Value *CreateConstGEP1_64(Value *Ptr, uint64_t Idx0, const char *Name = "") {
376 Value *Idx = Context.getConstantInt(Type::Int64Ty, Idx0);
371 Value *Idx = ConstantInt::get(Type::Int64Ty, Idx0);
377372
378373 if (Constant *PC = dyn_cast(Ptr))
379374 return Folder.CreateGetElementPtr(PC, &Idx, 1);
383378 Value *CreateConstGEP2_64(Value *Ptr, uint64_t Idx0, uint64_t Idx1,
384379 const char *Name = "") {
385380 Value *Idxs[] = {
386 Context.getConstantInt(Type::Int64Ty, Idx0),
387 Context.getConstantInt(Type::Int64Ty, Idx1)
381 ConstantInt::get(Type::Int64Ty, Idx0),
382 ConstantInt::get(Type::Int64Ty, Idx1)
388383 };
389384
390385 if (Constant *PC = dyn_cast(Ptr))
396391 return CreateConstGEP2_32(Ptr, 0, Idx, Name);
397392 }
398393 Value *CreateGlobalString(const char *Str = "", const char *Name = "") {
399 Constant *StrConstant = Context.getConstantArray(Str, true);
400 GlobalVariable *gv = new GlobalVariable(Context,
394 Constant *StrConstant = ConstantArray::get(Str, true);
395 GlobalVariable *gv = new GlobalVariable(*BB->getParent()->getParent(),
401396 StrConstant->getType(),
402397 true,
403398 GlobalValue::InternalLinkage,
404399 StrConstant,
405400 "",
406 BB->getParent()->getParent(),
401 0,
407402 false);
408403 gv->setName(Name);
409404 return gv;
410405 }
411406 Value *CreateGlobalStringPtr(const char *Str = "", const char *Name = "") {
412407 Value *gv = CreateGlobalString(Str, Name);
413 Value *zero = Context.getConstantInt(Type::Int32Ty, 0);
408 Value *zero = ConstantInt::get(Type::Int32Ty, 0);
414409 Value *Args[] = { zero, zero };
415410 return CreateGEP(gv, Args, Args+2, Name);
416411 }
687682
688683 /// CreateIsNull - Return an i1 value testing if \arg Arg is null.
689684 Value *CreateIsNull(Value *Arg, const char *Name = "") {
690 return CreateICmpEQ(Arg, Context.getNullValue(Arg->getType()),
685 return CreateICmpEQ(Arg, Constant::getNullValue(Arg->getType()),
691686 Name);
692687 }
693688
694689 /// CreateIsNotNull - Return an i1 value testing if \arg Arg is not null.
695690 Value *CreateIsNotNull(Value *Arg, const char *Name = "") {
696 return CreateICmpNE(Arg, Context.getNullValue(Arg->getType()),
691 return CreateICmpNE(Arg, Constant::getNullValue(Arg->getType()),
697692 Name);
698693 }
699694
708703 Value *RHS_int = CreatePtrToInt(RHS, Type::Int64Ty);
709704 Value *Difference = CreateSub(LHS_int, RHS_int);
710705 return CreateSDiv(Difference,
711 Context.getConstantExprSizeOf(ArgType->getElementType()),
706 ConstantExpr::getSizeOf(ArgType->getElementType()),
712707 Name);
713708 }
714709 };
489489 Constant *ConstStr = VMContext.getConstantArray(String);
490490
491491 // Otherwise create and return a new string global.
492 GlobalVariable *StrGV = new GlobalVariable(VMContext,ConstStr->getType(), true,
492 GlobalVariable *StrGV = new GlobalVariable(M, ConstStr->getType(), true,
493493 GlobalVariable::InternalLinkage,
494 ConstStr, ".str", &M);
494 ConstStr, ".str");
495495 StrGV->setSection("llvm.metadata");
496496 return Slot = VMContext.getConstantExprBitCast(StrGV, DestTy);
497497 }
515515 DIDescriptor &Entry = SimpleConstantCache[Init];
516516 if (!Entry.isNull()) return DIArray(Entry.getGV());
517517
518 GlobalVariable *GV = new GlobalVariable(VMContext, Init->getType(), true,
518 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
519519 GlobalValue::InternalLinkage,
520 Init, "llvm.dbg.array", &M);
520 Init, "llvm.dbg.array");
521521 GV->setSection("llvm.metadata");
522522 Entry = DIDescriptor(GV);
523523 return DIArray(GV);
541541
542542 M.addTypeName("llvm.dbg.subrange.type", Init->getType());
543543
544 GlobalVariable *GV = new GlobalVariable(VMContext, Init->getType(), true,
544 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
545545 GlobalValue::InternalLinkage,
546 Init, "llvm.dbg.subrange", &M);
546 Init, "llvm.dbg.subrange");
547547 GV->setSection("llvm.metadata");
548548 Entry = DIDescriptor(GV);
549549 return DISubrange(GV);
578578 sizeof(Elts)/sizeof(Elts[0]));
579579
580580 M.addTypeName("llvm.dbg.compile_unit.type", Init->getType());
581 GlobalVariable *GV = new GlobalVariable(VMContext, Init->getType(), true,
581 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
582582 GlobalValue::LinkOnceAnyLinkage,
583 Init, "llvm.dbg.compile_unit", &M);
583 Init, "llvm.dbg.compile_unit");
584584 GV->setSection("llvm.metadata");
585585 return DICompileUnit(GV);
586586 }
597597 sizeof(Elts)/sizeof(Elts[0]));
598598
599599 M.addTypeName("llvm.dbg.enumerator.type", Init->getType());
600 GlobalVariable *GV = new GlobalVariable(VMContext, Init->getType(), true,
600 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
601601 GlobalValue::InternalLinkage,
602 Init, "llvm.dbg.enumerator", &M);
602 Init, "llvm.dbg.enumerator");
603603 GV->setSection("llvm.metadata");
604604 return DIEnumerator(GV);
605605 }
631631 sizeof(Elts)/sizeof(Elts[0]));
632632
633633 M.addTypeName("llvm.dbg.basictype.type", Init->getType());
634 GlobalVariable *GV = new GlobalVariable(VMContext, Init->getType(), true,
634 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
635635 GlobalValue::InternalLinkage,
636 Init, "llvm.dbg.basictype", &M);
636 Init, "llvm.dbg.basictype");
637637 GV->setSection("llvm.metadata");
638638 return DIBasicType(GV);
639639 }
667667 sizeof(Elts)/sizeof(Elts[0]));
668668
669669 M.addTypeName("llvm.dbg.derivedtype.type", Init->getType());
670 GlobalVariable *GV = new GlobalVariable(VMContext, Init->getType(), true,
670 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
671671 GlobalValue::InternalLinkage,
672 Init, "llvm.dbg.derivedtype", &M);
672 Init, "llvm.dbg.derivedtype");
673673 GV->setSection("llvm.metadata");
674674 return DIDerivedType(GV);
675675 }
707707 sizeof(Elts)/sizeof(Elts[0]));
708708
709709 M.addTypeName("llvm.dbg.composite.type", Init->getType());
710 GlobalVariable *GV = new GlobalVariable(VMContext, Init->getType(), true,
710 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
711711 GlobalValue::InternalLinkage,
712 Init, "llvm.dbg.composite", &M);
712 Init, "llvm.dbg.composite");
713713 GV->setSection("llvm.metadata");
714714 return DICompositeType(GV);
715715 }
745745 sizeof(Elts)/sizeof(Elts[0]));
746746
747747 M.addTypeName("llvm.dbg.subprogram.type", Init->getType());
748 GlobalVariable *GV = new GlobalVariable(VMContext, Init->getType(), true,
748 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
749749 GlobalValue::LinkOnceAnyLinkage,
750 Init, "llvm.dbg.subprogram", &M);
750 Init, "llvm.dbg.subprogram");
751751 GV->setSection("llvm.metadata");
752752 return DISubprogram(GV);
753753 }
779779 sizeof(Elts)/sizeof(Elts[0]));
780780
781781 M.addTypeName("llvm.dbg.global_variable.type", Init->getType());
782 GlobalVariable *GV = new GlobalVariable(VMContext, Init->getType(), true,
782 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
783783 GlobalValue::LinkOnceAnyLinkage,
784 Init, "llvm.dbg.global_variable", &M);
784 Init, "llvm.dbg.global_variable");
785785 GV->setSection("llvm.metadata");
786786 return DIGlobalVariable(GV);
787787 }
805805 sizeof(Elts)/sizeof(Elts[0]));
806806
807807 M.addTypeName("llvm.dbg.variable.type", Init->getType());
808 GlobalVariable *GV = new GlobalVariable(VMContext, Init->getType(), true,
808 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
809809 GlobalValue::InternalLinkage,
810 Init, "llvm.dbg.variable", &M);
810 Init, "llvm.dbg.variable");
811811 GV->setSection("llvm.metadata");
812812 return DIVariable(GV);
813813 }
825825 sizeof(Elts)/sizeof(Elts[0]));
826826
827827 M.addTypeName("llvm.dbg.block.type", Init->getType());
828 GlobalVariable *GV = new GlobalVariable(VMContext, Init->getType(), true,
828 GlobalVariable *GV = new GlobalVariable(M, Init->getType(), true,
829829 GlobalValue::InternalLinkage,
830 Init, "llvm.dbg.block", &M);
830 Init, "llvm.dbg.block");
831831 GV->setSection("llvm.metadata");
832832 return DIBlock(GV);
833833 }
515515 }
516516
517517 if (GV == 0) {
518 GV = new GlobalVariable(Context, Ty, false,
519 GlobalValue::ExternalLinkage, 0, Name,
520 M, false, AddrSpace);
518 GV = new GlobalVariable(*M, Ty, false, GlobalValue::ExternalLinkage, 0,
519 Name, 0, false, AddrSpace);
521520 } else {
522521 if (GV->getType()->getElementType() != Ty)
523522 return Error(TyLoc,
607606
608607 FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, Name, M);
609608 } else {
610 FwdVal = new GlobalVariable(Context, PTy->getElementType(), false,
611 GlobalValue::ExternalWeakLinkage, 0, Name, M);
609 FwdVal = new GlobalVariable(*M, PTy->getElementType(), false,
610 GlobalValue::ExternalWeakLinkage, 0, Name);
612611 }
613612
614613 ForwardRefVals[Name] = std::make_pair(FwdVal, Loc);
651650 }
652651 FwdVal = Function::Create(FT, GlobalValue::ExternalWeakLinkage, "", M);
653652 } else {
654 FwdVal = new GlobalVariable(Context, PTy->getElementType(), false,
655 GlobalValue::ExternalWeakLinkage, 0, "", M);
653 FwdVal = new GlobalVariable(*M, PTy->getElementType(), false,
654 GlobalValue::ExternalWeakLinkage, 0, "");
656655 }
657656
658657 ForwardRefValIDs[ID] = std::make_pair(FwdVal, Loc);
12531253 isThreadLocal = Record[7];
12541254
12551255 GlobalVariable *NewGV =
1256 new GlobalVariable(Context, Ty, isConstant, Linkage, 0, "", TheModule,
1256 new GlobalVariable(*TheModule, Ty, isConstant, Linkage, 0, "", 0,
12571257 isThreadLocal, AddressSpace);
12581258 NewGV->setAlignment(Alignment);
12591259 if (!Section.empty())
228228 // to be a ModulePass (which means it cannot be in the 'llc' pipeline
229229 // (which uses a FunctionPassManager (which segfaults (not asserts) if
230230 // provided a ModulePass))).
231 Constant *GV = new GlobalVariable(*F.getContext(), FrameMap->getType(), true,
231 Constant *GV = new GlobalVariable(*F.getParent(), FrameMap->getType(), true,
232232 GlobalVariable::InternalLinkage,
233 FrameMap, "__gc_" + F.getName(),
234 F.getParent());
233 FrameMap, "__gc_" + F.getName());
235234
236235 Constant *GEPIndices[2] = { ConstantInt::get(Type::Int32Ty, 0),
237236 ConstantInt::get(Type::Int32Ty, 0) };
291290 if (!Head) {
292291 // If the root chain does not exist, insert a new one with linkonce
293292 // linkage!
294 Head = new GlobalVariable(M.getContext(), StackEntryPtrTy, false,
293 Head = new GlobalVariable(M, StackEntryPtrTy, false,
295294 GlobalValue::LinkOnceAnyLinkage,
296295 Constant::getNullValue(StackEntryPtrTy),
297 "llvm_gc_root_chain", &M);
296 "llvm_gc_root_chain");
298297 } else if (Head->hasExternalLinkage() && Head->isDeclaration()) {
299298 Head->setInitializer(Constant::getNullValue(StackEntryPtrTy));
300299 Head->setLinkage(GlobalValue::LinkOnceAnyLinkage);
572572 // symbol over in the dest module... the initializer will be filled in
573573 // later by LinkGlobalInits.
574574 GlobalVariable *NewDGV =
575 new GlobalVariable(Context, SGV->getType()->getElementType(),
575 new GlobalVariable(*Dest, SGV->getType()->getElementType(),
576576 SGV->isConstant(), SGV->getLinkage(), /*init*/0,
577 SGV->getName(), Dest, false,
577 SGV->getName(), 0, false,
578578 SGV->getType()->getAddressSpace());
579579 // Propagate alignment, visibility and section info.
580580 CopyGVAttributes(NewDGV, SGV);
605605 // AppendingVars map. The name is cleared out so that no linkage is
606606 // performed.
607607 GlobalVariable *NewDGV =
608 new GlobalVariable(Context, SGV->getType()->getElementType(),
608 new GlobalVariable(*Dest, SGV->getType()->getElementType(),
609609 SGV->isConstant(), SGV->getLinkage(), /*init*/0,
610 "", Dest, false,
610 "", 0, false,
611611 SGV->getType()->getAddressSpace());
612612
613613 // Set alignment allowing CopyGVAttributes merge it with alignment of SGV.
633633 // we are replacing may be a function (if a prototype, weak, etc) or a
634634 // global variable.
635635 GlobalVariable *NewDGV =
636 new GlobalVariable(Context, SGV->getType()->getElementType(),
636 new GlobalVariable(*Dest, SGV->getType()->getElementType(),
637637 SGV->isConstant(), NewLinkage, /*init*/0,
638 DGV->getName(), Dest, false,
638 DGV->getName(), 0, false,
639639 SGV->getType()->getAddressSpace());
640640
641641 // Propagate alignment, section, and visibility info.
11561156
11571157 // Create the new global variable...
11581158 GlobalVariable *NG =
1159 new GlobalVariable(Context, NewType, G1->isConstant(), G1->getLinkage(),
1160 /*init*/0, First->first, M, G1->isThreadLocal(),
1159 new GlobalVariable(*M, NewType, G1->isConstant(), G1->getLinkage(),
1160 /*init*/0, First->first, 0, G1->isThreadLocal(),
11611161 G1->getType()->getAddressSpace());
11621162
11631163 // Propagate alignment, visibility and section info.
107107 }
108108 ArrayType *AT = Context->getArrayType(SBP, AUGs.size());
109109 Constant *Init = Context->getConstantArray(AT, AUGs);
110 GlobalValue *gv = new GlobalVariable(M.getContext(), AT, false,
110 GlobalValue *gv = new GlobalVariable(M, AT, false,
111111 GlobalValue::AppendingLinkage,
112 Init, "llvm.used", &M);
112 Init, "llvm.used");
113113 gv->setSection("llvm.metadata");
114114 }
115115
489489 Context->getConstantInt(Type::Int32Ty, i),
490490 Context);
491491 assert(In && "Couldn't get element of initializer?");
492 GlobalVariable *NGV = new GlobalVariable(*Context, STy->getElementType(i),
493 false,
492 GlobalVariable *NGV = new GlobalVariable(*Context,
493 STy->getElementType(i), false,
494494 GlobalVariable::InternalLinkage,
495495 In, GV->getName()+"."+utostr(i),
496 (Module *)NULL,
497496 GV->isThreadLocal(),
498497 GV->getType()->getAddressSpace());
499498 Globals.insert(GV, NGV);
526525 Context);
527526 assert(In && "Couldn't get element of initializer?");
528527
529 GlobalVariable *NGV = new GlobalVariable(*Context, STy->getElementType(),
530 false,
528 GlobalVariable *NGV = new GlobalVariable(*Context,
529 STy->getElementType(), false,
531530 GlobalVariable::InternalLinkage,
532531 In, GV->getName()+"."+utostr(i),
533 (Module *)NULL,
534532 GV->isThreadLocal(),
535 GV->getType()->getAddressSpace());
533 GV->getType()->getAddressSpace());
536534 Globals.insert(GV, NGV);
537535 NewGlobals.push_back(NGV);
538536
841839
842840 // Create the new global variable. The contents of the malloc'd memory is
843841 // undefined, so initialize with an undef value.
844 Constant *Init = Context->getUndef(MI->getAllocatedType());
845 GlobalVariable *NewGV = new GlobalVariable(*Context, MI->getAllocatedType(),
846 false,
847 GlobalValue::InternalLinkage, Init,
848 GV->getName()+".body",
849 (Module *)NULL,
850 GV->isThreadLocal());
851842 // FIXME: This new global should have the alignment returned by malloc. Code
852843 // could depend on malloc returning large alignment (on the mac, 16 bytes) but
853844 // this would only guarantee some lower alignment.
854 GV->getParent()->getGlobalList().insert(GV, NewGV);
855
845 Constant *Init = Context->getUndef(MI->getAllocatedType());
846 GlobalVariable *NewGV = new GlobalVariable(*GV->getParent(),
847 MI->getAllocatedType(), false,
848 GlobalValue::InternalLinkage, Init,
849 GV->getName()+".body",
850 GV,
851 GV->isThreadLocal());
852
856853 // Anything that used the malloc now uses the global directly.
857854 MI->replaceAllUsesWith(NewGV);
858855
867864 new GlobalVariable(*Context, Type::Int1Ty, false,
868865 GlobalValue::InternalLinkage,
869866 Context->getConstantIntFalse(), GV->getName()+".init",
870 (Module *)NULL, GV->isThreadLocal());
867 GV->isThreadLocal());
871868 bool InitBoolUsed = false;
872869
873870 // Loop over all uses of GV, processing them in turn.
12851282 const Type *PFieldTy = Context->getPointerTypeUnqual(FieldTy);
12861283
12871284 GlobalVariable *NGV =
1288 new GlobalVariable(*Context, PFieldTy, false,
1289 GlobalValue::InternalLinkage,
1285 new GlobalVariable(*GV->getParent(),
1286 PFieldTy, false, GlobalValue::InternalLinkage,
12901287 Context->getNullValue(PFieldTy),
12911288 GV->getName() + ".f" + utostr(FieldNo), GV,
12921289 GV->isThreadLocal());
15861583 GlobalVariable *NewGV = new GlobalVariable(*Context, Type::Int1Ty, false,
15871584 GlobalValue::InternalLinkage, Context->getConstantIntFalse(),
15881585 GV->getName()+".b",
1589 (Module *)NULL,
15901586 GV->isThreadLocal());
15911587 GV->getParent()->getGlobalList().insert(GV, NewGV);
15921588
19811977 GlobalVariable *NGV = new GlobalVariable(*Context, CA->getType(),
19821978 GCL->isConstant(),
19831979 GCL->getLinkage(), CA, "",
1984 (Module *)NULL,
19851980 GCL->isThreadLocal());
19861981 GCL->getParent()->getGlobalList().insert(GCL, NGV);
19871982 NGV->takeName(GCL);
6464
6565 const Type *ATy = Context->getArrayType(Type::Int32Ty, NumFunctions);
6666 GlobalVariable *Counters =
67 new GlobalVariable(M.getContext(), ATy, false, GlobalValue::InternalLinkage,
68 Context->getNullValue(ATy), "FuncProfCounters", &M);
67 new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage,
68 Context->getNullValue(ATy), "FuncProfCounters");
6969
7070 // Instrument all of the functions...
7171 unsigned i = 0;
109109
110110 const Type *ATy = Context->getArrayType(Type::Int32Ty, NumBlocks);
111111 GlobalVariable *Counters =
112 new GlobalVariable(M.getContext(), ATy, false, GlobalValue::InternalLinkage,
113 Context->getNullValue(ATy), "BlockProfCounters", &M);
112 new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage,
113 Context->getNullValue(ATy), "BlockProfCounters");
114114
115115 // Instrument all of the blocks...
116116 unsigned i = 0;
6565
6666 const Type *ATy = Context->getArrayType(Type::Int32Ty, NumEdges);
6767 GlobalVariable *Counters =
68 new GlobalVariable(M.getContext(), ATy, false, GlobalValue::InternalLinkage,
69 Context->getNullValue(ATy), "EdgeProfCounters", &M);
68 new GlobalVariable(M, ATy, false, GlobalValue::InternalLinkage,
69 Context->getNullValue(ATy), "EdgeProfCounters");
7070
7171 // Instrument all of the edges...
7272 unsigned i = 0;
197197 uint64_t resetval) : T(t) {
198198 ConstantInt* Init = M.getContext().getConstantInt(T, resetval);
199199 ResetValue = Init;
200 Counter = new GlobalVariable(M.getContext(), T, false,
201 GlobalValue::InternalLinkage,
202 Init, "RandomSteeringCounter", &M);
200 Counter = new GlobalVariable(M, T, false, GlobalValue::InternalLinkage,
201 Init, "RandomSteeringCounter");
203202 }
204203
205204 GlobalRandomCounter::~GlobalRandomCounter() {}
237236 : AI(0), T(t) {
238237 ConstantInt* Init = M.getContext().getConstantInt(T, resetval);
239238 ResetValue = Init;
240 Counter = new GlobalVariable(M.getContext(), T, false,
241 GlobalValue::InternalLinkage,
242 Init, "RandomSteeringCounter", &M);
239 Counter = new GlobalVariable(M, T, false, GlobalValue::InternalLinkage,
240 Init, "RandomSteeringCounter");
243241 }
244242
245243 GlobalRandomCounterOpt::~GlobalRandomCounterOpt() {}
12891289 // pass to be run after this pass, to merge duplicate strings.
12901290 FormatStr.erase(FormatStr.end()-1);
12911291 Constant *C = Context->getConstantArray(FormatStr, true);
1292 C = new GlobalVariable(*Context, C->getType(),
1293 true, GlobalVariable::InternalLinkage,
1294 C, "str", Callee->getParent());
1292 C = new GlobalVariable(*Callee->getParent(), C->getType(), true,
1293 GlobalVariable::InternalLinkage, C, "str");
12951294 EmitPutS(C, B);
12961295 return CI->use_empty() ? (Value*)CI :
12971296 Context->getConstantInt(CI->getType(), FormatStr.size()+1);
5555 //
5656 for (Module::const_global_iterator I = M->global_begin(), E = M->global_end();
5757 I != E; ++I) {
58 GlobalVariable *GV = new GlobalVariable(M->getContext(),
58 GlobalVariable *GV = new GlobalVariable(*New,
5959 I->getType()->getElementType(),
6060 false,
6161 GlobalValue::ExternalLinkage, 0,
62 I->getName(), New);
62 I->getName());
6363 GV->setAlignment(I->getAlignment());
6464 ValueMap[I] = GV;
6565 }
138138 // Now that we've done that, insert the jmpbuf list head global, unless it
139139 // already exists.
140140 if (!(JBListHead = M.getGlobalVariable("llvm.sjljeh.jblist", PtrJBList))) {
141 JBListHead = new GlobalVariable(M.getContext(),
142 PtrJBList, false,
141 JBListHead = new GlobalVariable(M, PtrJBList, false,
143142 GlobalValue::LinkOnceAnyLinkage,
144143 Context->getNullValue(PtrJBList),
145 "llvm.sjljeh.jblist", &M);
144 "llvm.sjljeh.jblist");
146145 }
147146
148147 // VisualStudio defines setjmp as _setjmp via #include / ,
182181 Context->getConstantArray("ERROR: Exception thrown, but not caught!\n");
183182 AbortMessageLength = Msg->getNumOperands()-1; // don't include \0
184183
185 GlobalVariable *MsgGV = new GlobalVariable(M->getContext(),
186 Msg->getType(), true,
184 GlobalVariable *MsgGV = new GlobalVariable(*M, Msg->getType(), true,
187185 GlobalValue::InternalLinkage,
188 Msg, "abortmsg", M);
186 Msg, "abortmsg");
189187 std::vector GEPIdx(2, Context->getNullValue(Type::Int32Ty));
190188 AbortMessage = Context->getConstantExprGetElementPtr(MsgGV, &GEPIdx[0], 2);
191189 } else {
196194 "Recompile program with -enable-correct-eh-support.\n");
197195 AbortMessageLength = Msg->getNumOperands()-1; // don't include \0
198196
199 GlobalVariable *MsgGV = new GlobalVariable(M->getContext(),
200 Msg->getType(), true,
197 GlobalVariable *MsgGV = new GlobalVariable(*M, Msg->getType(), true,
201198 GlobalValue::InternalLinkage,
202 Msg, "abortmsg", M);
199 Msg, "abortmsg");
203200 std::vector GEPIdx(2, Context->getNullValue(Type::Int32Ty));
204201 AbortMessage = ConstantExpr::getGetElementPtr(MsgGV, &GEPIdx[0], 2);
205202 }
699699 /*--.. Operations on global variables ......................................--*/
700700
701701 LLVMValueRef LLVMAddGlobal(LLVMModuleRef M, LLVMTypeRef Ty, const char *Name) {
702 LLVMContext &Context = unwrap(M)->getContext();
703 return wrap(new GlobalVariable(Context, unwrap(Ty), false,
704 GlobalValue::ExternalLinkage, 0, Name,
705 unwrap(M)));
702 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
703 GlobalValue::ExternalLinkage, 0, Name));
706704 }
707705
708706 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
9696 GlobalVariable::GlobalVariable(LLVMContext &Context, const Type *Ty,
9797 bool constant, LinkageTypes Link,
9898 Constant *InitVal, const std::string &Name,
99 Module *ParentModule, bool ThreadLocal,
100 unsigned AddressSpace)
99 bool ThreadLocal, unsigned AddressSpace)
101100 : GlobalValue(Context.getPointerType(Ty, AddressSpace),
102101 Value::GlobalVariableVal,
103102 OperandTraits::op_begin(this),
110109 }
111110
112111 LeakDetector::addGarbageObject(this);
113
114 if (ParentModule)
115 ParentModule->getGlobalList().push_back(this);
116 }
117
118 GlobalVariable::GlobalVariable(LLVMContext &Context, const Type *Ty,
119 bool constant, LinkageTypes Link,
120 Constant *InitVal, const std::string &Name,
112 }
113
114 GlobalVariable::GlobalVariable(Module &M, const Type *Ty, bool constant,
115 LinkageTypes Link, Constant *InitVal,
116 const std::string &Name,
121117 GlobalVariable *Before, bool ThreadLocal,
122118 unsigned AddressSpace)
123 : GlobalValue(Context.getPointerType(Ty, AddressSpace),
124 Value::GlobalVariableVal,
119 : GlobalValue(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal,
125120 OperandTraits::op_begin(this),
126121 InitVal != 0, Link, Name),
127122 isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
135130
136131 if (Before)
137132 Before->getParent()->getGlobalList().insert(Before, this);
133 else
134 M.getGlobalList().push_back(this);
138135 }
139136
140137 void GlobalVariable::setParent(Module *parent) {
2727
2828 //===----------------------------------------------------------------------===//
2929 // Methods to implement the globals and functions lists.
30 // NOTE: It is ok to allocate the globals used for these methods from the
31 // global context, because all we ever do is use them to compare for equality.
3230 //
3331
3432 GlobalVariable *ilist_traits::createSentinel() {
35 GlobalVariable *Ret = new GlobalVariable(getGlobalContext(),
36 Type::Int32Ty, false,
37 GlobalValue::ExternalLinkage);
33 GlobalVariable *Ret = new GlobalVariable(getGlobalContext(), Type::Int32Ty,
34 false, GlobalValue::ExternalLinkage);
3835 // This should not be garbage monitored.
3936 LeakDetector::removeGarbageObject(Ret);
4037 return Ret;
4138 }
4239 GlobalAlias *ilist_traits::createSentinel() {
43 GlobalAlias *Ret = new GlobalAlias(Type::Int32Ty,
40 GlobalAlias *Ret = new GlobalAlias(Type::Int32Ty,
4441 GlobalValue::ExternalLinkage);
4542 // This should not be garbage monitored.
4643 LeakDetector::removeGarbageObject(Ret);
272269 if (GV == 0) {
273270 // Nope, add it
274271 GlobalVariable *New =
275 new GlobalVariable(getContext(), Ty, false,
276 GlobalVariable::ExternalLinkage, 0, Name);
277 GlobalList.push_back(New);
278 return New; // Return the new declaration.
272 new GlobalVariable(*this, Ty, false, GlobalVariable::ExternalLinkage,
273 0, Name);
274 return New; // Return the new declaration.
279275 }
280276
281277 // If the variable exists but has the wrong type, return a bitcast to the
235235 GV->eraseFromParent();
236236 if (!M1Tors.empty()) {
237237 Constant *M1Init = GetTorInit(M1Tors);
238 new GlobalVariable(M1->getContext(), M1Init->getType(), false,
238 new GlobalVariable(*M1, M1Init->getType(), false,
239239 GlobalValue::AppendingLinkage,
240 M1Init, GlobalName, M1);
240 M1Init, GlobalName);
241241 }
242242
243243 GV = M2->getNamedGlobal(GlobalName);
247247 GV->eraseFromParent();
248248 if (!M2Tors.empty()) {
249249 Constant *M2Init = GetTorInit(M2Tors);
250 new GlobalVariable(M2->getContext(), M2Init->getType(), false,
250 new GlobalVariable(*M2, M2Init->getType(), false,
251251 GlobalValue::AppendingLinkage,
252 M2Init, GlobalName, M2);
252 M2Init, GlobalName);
253253 }
254254 }
255255
702702 // 1. Add a string constant with its name to the global file
703703 Constant *InitArray = ConstantArray::get(F->getName());
704704 GlobalVariable *funcName =
705 new GlobalVariable(Safe->getContext(),
706 InitArray->getType(), true /*isConstant*/,
705 new GlobalVariable(*Safe, InitArray->getType(), true /*isConstant*/,
707706 GlobalValue::InternalLinkage, InitArray,
708 F->getName() + "_name", Safe);
707 F->getName() + "_name");
709708
710709 // 2. Use `GetElementPtr *funcName, 0, 0' to convert the string to an
711710 // sbyte* so it matches the signature of the resolver function.
722721 // Create a new global to hold the cached function pointer.
723722 Constant *NullPtr = ConstantPointerNull::get(F->getType());
724723 GlobalVariable *Cache =
725 new GlobalVariable(F->getParent()->getContext(),
726 F->getType(), false,GlobalValue::InternalLinkage,
727 NullPtr,F->getName()+".fpcache", F->getParent());
724 new GlobalVariable(*F->getParent(), F->getType(),
725 false, GlobalValue::InternalLinkage,
726 NullPtr,F->getName()+".fpcache");
728727
729728 // Construct a new stub function that will re-route calls to F
730729 const FunctionType *FuncTy = F->getFunctionType();