llvm.org GIT mirror llvm / ce718ff
Extend the IL for selecting TLS models (PR9788) This allows the user/front-end to specify a model that is better than what LLVM would choose by default. For example, a variable might be declared as @x = thread_local(initialexec) global i32 42 if it will not be used in a shared library that is dlopen'ed. If the specified model isn't supported by the target, or if LLVM can make a better choice, a different model may be used. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@159077 91177308-0d34-0410-b5e6-96231b3b80d8 Hans Wennborg 8 years ago
24 changed file(s) with 707 addition(s) and 48 deletion(s). Raw diff Collapse all Expand all
863863

MODULE_CODE_GLOBALVAR Record

864864
865865
866

[GLOBALVAR, pointer type, isconst, initid, linkage, alignment, section, visibility, threadlocal]

866

[GLOBALVAR, pointer type, isconst, initid, linkage, alignment, section, visibility, threadlocal, unnamed_addr]

867867
868868

The GLOBALVAR record (code 7) marks the declaration or

869869 definition of a global variable. The operand fields are:

914914
915915
916916
917
  • threadlocal: If present and non-zero, indicates that the variable
  • 918 is thread_local
    917
  • threadlocal: If present, an encoding of the thread local storage
  • 918 mode of the variable:
    919
    920
  • not thread local: code 0
  • 921
  • thread local; default TLS model: code 1
  • 922
  • localdynamic: code 2
  • 923
  • initialexec: code 3
  • 924
  • localexec: code 4
  • 925
    926
    919927
    920928
  • unnamed_addr: If present and non-zero, indicates that the variable
  • 921929 has unnamed_addr
    837837

    Global variables define regions of memory allocated at compilation time

    838838 instead of run-time. Global variables may optionally be initialized, may
    839839 have an explicit section to be placed in, and may have an optional explicit
    840 alignment specified. A variable may be defined as "thread_local", which
    840 alignment specified.

    841
    842

    A variable may be defined as thread_local, which

    841843 means that it will not be shared by threads (each thread will have a
    842 separated copy of the variable). A variable may be defined as a global
    844 separated copy of the variable). Not all targets support thread-local
    845 variables. Optionally, a TLS model may be specified:

    846
    847
    848
    localdynamic:
    849
    For variables that are only used within the current shared library.
    850
    851
    initialexec:
    852
    For variables in modules that will not be loaded dynamically.
    853
    854
    localexec:
    855
    For variables defined in the executable and only used within it.
    856
    857
    858

    The models correspond to the ELF TLS models; see

    859 ELF
    860 Handling For Thread-Local Storage for more information on under which
    861 circumstances the different models may be used. The target may choose a
    862 different TLS model if the specified model is not supported, or if a better
    863 choice of model can be made.

    864
    865

    A variable may be defined as a global

    843866 "constant," which indicates that the contents of the variable
    844867 will never be modified (enabling better optimization, allowing the
    845868 global data to be placed in the read-only section of an executable, etc).
    890913
    891914
    
                      
                    
    892915 @G = addrspace(5) constant float 1.0, section "foo", align 4
    916
    917
    918

    The following example defines a thread-local global with

    919 the initialexec TLS model:

    920
    921
    
                      
                    
    922 @G = thread_local(initialexec) global i32 0, align 4
    893923
    894924
    895925
    4040 void setParent(Module *parent);
    4141
    4242 bool isConstantGlobal : 1; // Is this a global constant?
    43 bool isThreadLocalSymbol : 1; // Is this symbol "Thread Local"?
    43 unsigned threadLocalMode : 3; // Is this symbol "Thread Local",
    44 // if so, what is the desired model?
    4445
    4546 public:
    4647 // allocate space for exactly one operand
    4748 void *operator new(size_t s) {
    4849 return User::operator new(s, 1);
    4950 }
    50 /// GlobalVariable ctor - If a parent module is specified, the global is
    51 /// automatically inserted into the end of the specified modules global list.
    51
    52 enum ThreadLocalMode {
    53 NotThreadLocal = 0,
    54 GeneralDynamicTLSModel,
    55 LocalDynamicTLSModel,
    56 InitialExecTLSModel,
    57 LocalExecTLSModel
    58 };
    59
    60 // TODO: Remove these once Clang is updated.
    5261 GlobalVariable(Type *Ty, bool isConstant, LinkageTypes Linkage,
    5362 Constant *Initializer = 0, const Twine &Name = "",
    5463 bool ThreadLocal = false, unsigned AddressSpace = 0);
    64 GlobalVariable(Module &M, Type *Ty, bool isConstant,
    65 LinkageTypes Linkage, Constant *Initializer,
    66 const Twine &Name = "",
    67 GlobalVariable *InsertBefore = 0, bool ThreadLocal = false,
    68 unsigned AddressSpace = 0);
    69
    70 /// GlobalVariable ctor - If a parent module is specified, the global is
    71 /// automatically inserted into the end of the specified modules global list.
    72 // TODO: Put default param values back when ctors above are removed.
    73 GlobalVariable(Type *Ty, bool isConstant, LinkageTypes Linkage,
    74 Constant *Initializer, const Twine &Name,
    75 ThreadLocalMode, unsigned AddressSpace = 0);
    5576 /// GlobalVariable ctor - This creates a global and inserts it before the
    5677 /// specified other global.
    78 // TODO: Put default param values back when ctors above are removed.
    5779 GlobalVariable(Module &M, Type *Ty, bool isConstant,
    5880 LinkageTypes Linkage, Constant *Initializer,
    5981 const Twine &Name,
    60 GlobalVariable *InsertBefore = 0, bool ThreadLocal = false,
    82 GlobalVariable *InsertBefore,
    83 ThreadLocalMode,
    6184 unsigned AddressSpace = 0);
    6285
    6386 ~GlobalVariable() {
    134157 void setConstant(bool Val) { isConstantGlobal = Val; }
    135158
    136159 /// If the value is "Thread Local", its value isn't shared by the threads.
    137 bool isThreadLocal() const { return isThreadLocalSymbol; }
    138 void setThreadLocal(bool Val) { isThreadLocalSymbol = Val; }
    160 bool isThreadLocal() const { return threadLocalMode != NotThreadLocal; }
    161 void setThreadLocal(bool Val) {
    162 threadLocalMode = Val ? GeneralDynamicTLSModel : NotThreadLocal;
    163 }
    164 void setThreadLocalMode(ThreadLocalMode Val) { threadLocalMode = Val; }
    165 ThreadLocalMode getThreadLocalMode() const {
    166 return static_cast(threadLocalMode);
    167 }
    139168
    140169 /// copyAttributesFrom - copy all additional attributes (those not needed to
    141170 /// create a GlobalVariable) from the GlobalVariable Src to this one.
    473473 KEYWORD(extern_weak);
    474474 KEYWORD(external);
    475475 KEYWORD(thread_local);
    476 KEYWORD(localdynamic);
    477 KEYWORD(initialexec);
    478 KEYWORD(localexec);
    476479 KEYWORD(zeroinitializer);
    477480 KEYWORD(undef);
    478481 KEYWORD(null);
    644644 unsigned Linkage, bool HasLinkage,
    645645 unsigned Visibility) {
    646646 unsigned AddrSpace;
    647 bool ThreadLocal, IsConstant, UnnamedAddr;
    647 bool IsConstant, UnnamedAddr;
    648 GlobalVariable::ThreadLocalMode TLM;
    648649 LocTy UnnamedAddrLoc;
    649650 LocTy TyLoc;
    650651
    651652 Type *Ty = 0;
    652 if (ParseOptionalToken(lltok::kw_thread_local, ThreadLocal) ||
    653 if (ParseOptionalThreadLocal(TLM) ||
    653654 ParseOptionalAddrSpace(AddrSpace) ||
    654655 ParseOptionalToken(lltok::kw_unnamed_addr, UnnamedAddr,
    655656 &UnnamedAddrLoc) ||
    690691
    691692 if (GV == 0) {
    692693 GV = new GlobalVariable(*M, Ty, false, GlobalValue::ExternalLinkage, 0,
    693 Name, 0, false, AddrSpace);
    694 Name, 0, GlobalVariable::NotThreadLocal,
    695 AddrSpace);
    694696 } else {
    695697 if (GV->getType()->getElementType() != Ty)
    696698 return Error(TyLoc,
    709711 GV->setConstant(IsConstant);
    710712 GV->setLinkage((GlobalValue::LinkageTypes)Linkage);
    711713 GV->setVisibility((GlobalValue::VisibilityTypes)Visibility);
    712 GV->setThreadLocal(ThreadLocal);
    714 GV->setThreadLocalMode(TLM);
    713715 GV->setUnnamedAddr(UnnamedAddr);
    714716
    715717 // Parse attributes on the global.
    857859 return false;
    858860 }
    859861
    862 /// ParseTLSModel
    863 /// := 'localdynamic'
    864 /// := 'initialexec'
    865 /// := 'localexec'
    866 bool LLParser::ParseTLSModel(GlobalVariable::ThreadLocalMode &TLM) {
    867 switch (Lex.getKind()) {
    868 default:
    869 return TokError("expected localdynamic, initialexec or localexec");
    870 case lltok::kw_localdynamic:
    871 TLM = GlobalVariable::LocalDynamicTLSModel;
    872 break;
    873 case lltok::kw_initialexec:
    874 TLM = GlobalVariable::InitialExecTLSModel;
    875 break;
    876 case lltok::kw_localexec:
    877 TLM = GlobalVariable::LocalExecTLSModel;
    878 break;
    879 }
    880
    881 Lex.Lex();
    882 return false;
    883 }
    884
    885 /// ParseOptionalThreadLocal
    886 /// := /*empty*/
    887 /// := 'thread_local'
    888 /// := 'thread_local' '(' tlsmodel ')'
    889 bool LLParser::ParseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM) {
    890 TLM = GlobalVariable::NotThreadLocal;
    891 if (!EatIfPresent(lltok::kw_thread_local))
    892 return false;
    893
    894 TLM = GlobalVariable::GeneralDynamicTLSModel;
    895 if (Lex.getKind() == lltok::lparen) {
    896 Lex.Lex();
    897 return ParseTLSModel(TLM) ||
    898 ParseToken(lltok::rparen, "expected ')' after thread local model");
    899 }
    900 return false;
    901 }
    860902
    861903 /// ParseOptionalAddrSpace
    862904 /// := /*empty*/
    170170 Loc = Lex.getLoc();
    171171 return ParseUInt32(Val);
    172172 }
    173
    174 bool ParseTLSModel(GlobalVariable::ThreadLocalMode &TLM);
    175 bool ParseOptionalThreadLocal(GlobalVariable::ThreadLocalMode &TLM);
    173176 bool ParseOptionalAddrSpace(unsigned &AddrSpace);
    174177 bool ParseOptionalAttrs(Attributes &Attrs, unsigned AttrKind);
    175178 bool ParseOptionalLinkage(unsigned &Linkage, bool &HasLinkage);
    4343 kw_unnamed_addr,
    4444 kw_extern_weak,
    4545 kw_external, kw_thread_local,
    46 kw_localdynamic, kw_initialexec, kw_localexec,
    4647 kw_zeroinitializer,
    4748 kw_undef, kw_null,
    4849 kw_to,
    4950 kw_tail,
    5051 kw_target,
    5152 kw_triple,
    52 kw_unwind,
    53 kw_unwind,
    5354 kw_deplibs,
    5455 kw_datalayout,
    5556 kw_volatile,
    9898 case 0: return GlobalValue::DefaultVisibility;
    9999 case 1: return GlobalValue::HiddenVisibility;
    100100 case 2: return GlobalValue::ProtectedVisibility;
    101 }
    102 }
    103
    104 static GlobalVariable::ThreadLocalMode GetDecodedThreadLocalMode(unsigned Val) {
    105 switch (Val) {
    106 case 0: return GlobalVariable::NotThreadLocal;
    107 default: // Map unknown non-zero value to general dynamic.
    108 case 1: return GlobalVariable::GeneralDynamicTLSModel;
    109 case 2: return GlobalVariable::LocalDynamicTLSModel;
    110 case 3: return GlobalVariable::InitialExecTLSModel;
    111 case 4: return GlobalVariable::LocalExecTLSModel;
    101112 }
    102113 }
    103114
    15431554 GlobalValue::VisibilityTypes Visibility = GlobalValue::DefaultVisibility;
    15441555 if (Record.size() > 6)
    15451556 Visibility = GetDecodedVisibility(Record[6]);
    1546 bool isThreadLocal = false;
    1557
    1558 GlobalVariable::ThreadLocalMode TLM = GlobalVariable::NotThreadLocal;
    15471559 if (Record.size() > 7)
    1548 isThreadLocal = Record[7];
    1560 TLM = GetDecodedThreadLocalMode(Record[7]);
    15491561
    15501562 bool UnnamedAddr = false;
    15511563 if (Record.size() > 8)
    15531565
    15541566 GlobalVariable *NewGV =
    15551567 new GlobalVariable(*TheModule, Ty, isConstant, Linkage, 0, "", 0,
    1556 isThreadLocal, AddressSpace);
    1568 TLM, AddressSpace);
    15571569 NewGV->setAlignment(Alignment);
    15581570 if (!Section.empty())
    15591571 NewGV->setSection(Section);
    15601572 NewGV->setVisibility(Visibility);
    1561 NewGV->setThreadLocal(isThreadLocal);
    15621573 NewGV->setUnnamedAddr(UnnamedAddr);
    15631574
    15641575 ValueList.push_back(NewGV);
    378378 llvm_unreachable("Invalid visibility");
    379379 }
    380380
    381 static unsigned getEncodedThreadLocalMode(const GlobalVariable *GV) {
    382 switch (GV->getThreadLocalMode()) {
    383 case GlobalVariable::NotThreadLocal: return 0;
    384 case GlobalVariable::GeneralDynamicTLSModel: return 1;
    385 case GlobalVariable::LocalDynamicTLSModel: return 2;
    386 case GlobalVariable::InitialExecTLSModel: return 3;
    387 case GlobalVariable::LocalExecTLSModel: return 4;
    388 }
    389 llvm_unreachable("Invalid TLS model");
    390 }
    391
    381392 // Emit top-level description of module, including target triple, inline asm,
    382393 // descriptors for global variables, and function prototype info.
    383394 static void WriteModuleInfo(const Module *M, const ValueEnumerator &VE,
    486497 GV->getVisibility() != GlobalValue::DefaultVisibility ||
    487498 GV->hasUnnamedAddr()) {
    488499 Vals.push_back(getEncodedVisibility(GV));
    489 Vals.push_back(GV->isThreadLocal());
    500 Vals.push_back(getEncodedThreadLocalMode(GV));
    490501 Vals.push_back(GV->hasUnnamedAddr());
    491502 } else {
    492503 AbbrevToUse = SimpleGVarAbbrev;
    683683 GlobalVariable *NG =
    684684 new GlobalVariable(*DstGV->getParent(), NewType, SrcGV->isConstant(),
    685685 DstGV->getLinkage(), /*init*/0, /*name*/"", DstGV,
    686 DstGV->isThreadLocal(),
    686 DstGV->getThreadLocalMode(),
    687687 DstGV->getType()->getAddressSpace());
    688688
    689689 // Propagate alignment, visibility and section info.
    758758 new GlobalVariable(*DstM, TypeMap.get(SGV->getType()->getElementType()),
    759759 SGV->isConstant(), SGV->getLinkage(), /*init*/0,
    760760 SGV->getName(), /*insertbefore*/0,
    761 SGV->isThreadLocal(),
    761 SGV->getThreadLocalMode(),
    762762 SGV->getType()->getAddressSpace());
    763763 // Propagate alignment, visibility and section info.
    764764 copyGVAttributes(NewDGV, SGV);
    129129 private:
    130130 void printLinkageType(GlobalValue::LinkageTypes LT);
    131131 void printVisibilityType(GlobalValue::VisibilityTypes VisTypes);
    132 void printThreadLocalMode(GlobalVariable::ThreadLocalMode TLM);
    132133 void printCallingConv(CallingConv::ID cc);
    133134 void printEscapedString(const std::string& str);
    134135 void printCFP(const ConstantFP* CFP);
    321322 case GlobalValue::ProtectedVisibility:
    322323 Out << "GlobalValue::ProtectedVisibility";
    323324 break;
    325 }
    326 }
    327
    328 void CppWriter::printThreadLocalMode(GlobalVariable::ThreadLocalMode TLM) {
    329 switch (TLM) {
    330 case GlobalVariable::NotThreadLocal:
    331 Out << "GlobalVariable::NotThreadLocal";
    332 break;
    333 case GlobalVariable::GeneralDynamicTLSModel:
    334 Out << "GlobalVariable::GeneralDynamicTLSModel";
    335 break;
    336 case GlobalVariable::LocalDynamicTLSModel:
    337 Out << "GlobalVariable::LocalDynamicTLSModel";
    338 break;
    339 case GlobalVariable::InitialExecTLSModel:
    340 Out << "GlobalVariable::InitialExecTLSModel";
    341 break;
    342 case GlobalVariable::LocalExecTLSModel:
    343 Out << "GlobalVariable::LocalExecTLSModel";
    344 break;
    324345 }
    325346 }
    326347
    9951016 }
    9961017 if (GV->isThreadLocal()) {
    9971018 printCppName(GV);
    998 Out << "->setThreadLocal(true);";
    1019 Out << "->setThreadLocalMode(";
    1020 printThreadLocalMode(GV->getThreadLocalMode());
    1021 Out << ");";
    9991022 nl(Out);
    10001023 }
    10011024 if (is_inline) {
    7676 return CodeGenInfo->getCodeModel();
    7777 }
    7878
    79 /// Get the IR-specified TLS model for Var.
    80 static TLSModel::Model getSelectedTLSModel(const GlobalVariable *Var) {
    81 switch (Var->getThreadLocalMode()) {
    82 case GlobalVariable::NotThreadLocal:
    83 llvm_unreachable("getSelectedTLSModel for non-TLS variable");
    84 break;
    85 case GlobalVariable::GeneralDynamicTLSModel:
    86 return TLSModel::GeneralDynamic;
    87 case GlobalVariable::LocalDynamicTLSModel:
    88 return TLSModel::LocalDynamic;
    89 case GlobalVariable::InitialExecTLSModel:
    90 return TLSModel::InitialExec;
    91 case GlobalVariable::LocalExecTLSModel:
    92 return TLSModel::LocalExec;
    93 }
    94 llvm_unreachable("invalid TLS model");
    95 }
    96
    7997 TLSModel::Model TargetMachine::getTLSModel(const GlobalValue *GV) const {
    8098 // If GV is an alias then use the aliasee for determining
    8199 // thread-localness.
    85103
    86104 bool isLocal = Var->hasLocalLinkage();
    87105 bool isDeclaration = Var->isDeclaration();
    106 bool isPIC = getRelocationModel() == Reloc::PIC_;
    107 bool isPIE = Options.PositionIndependentExecutable;
    88108 // FIXME: what should we do for protected and internal visibility?
    89109 // For variables, is internal different from hidden?
    90110 bool isHidden = Var->hasHiddenVisibility();
    91111
    92 if (getRelocationModel() == Reloc::PIC_ &&
    93 !Options.PositionIndependentExecutable) {
    112 TLSModel::Model Model;
    113 if (isPIC && !isPIE) {
    94114 if (isLocal || isHidden)
    95 return TLSModel::LocalDynamic;
    115 Model = TLSModel::LocalDynamic;
    96116 else
    97 return TLSModel::GeneralDynamic;
    117 Model = TLSModel::GeneralDynamic;
    98118 } else {
    99119 if (!isDeclaration || isHidden)
    100 return TLSModel::LocalExec;
    120 Model = TLSModel::LocalExec;
    101121 else
    102 return TLSModel::InitialExec;
    122 Model = TLSModel::InitialExec;
    103123 }
    124
    125 // If the user specified a more specific model, use that.
    126 TLSModel::Model SelectedModel = getSelectedTLSModel(Var);
    127 if (SelectedModel > Model)
    128 return SelectedModel;
    129
    130 return Model;
    104131 }
    105132
    106133 /// getOptLevel - Returns the optimization level: None, Less,
    134161 void TargetMachine::setDataSections(bool V) {
    135162 DataSections = V;
    136163 }
    137
    516516 GlobalVariable *NGV = new GlobalVariable(STy->getElementType(i), false,
    517517 GlobalVariable::InternalLinkage,
    518518 In, GV->getName()+"."+Twine(i),
    519 GV->isThreadLocal(),
    519 GV->getThreadLocalMode(),
    520520 GV->getType()->getAddressSpace());
    521521 Globals.insert(GV, NGV);
    522522 NewGlobals.push_back(NGV);
    549549 GlobalVariable *NGV = new GlobalVariable(STy->getElementType(), false,
    550550 GlobalVariable::InternalLinkage,
    551551 In, GV->getName()+"."+Twine(i),
    552 GV->isThreadLocal(),
    552 GV->getThreadLocalMode(),
    553553 GV->getType()->getAddressSpace());
    554554 Globals.insert(GV, NGV);
    555555 NewGlobals.push_back(NGV);
    865865 UndefValue::get(GlobalType),
    866866 GV->getName()+".body",
    867867 GV,
    868 GV->isThreadLocal());
    868 GV->getThreadLocalMode());
    869869
    870870 // If there are bitcast users of the malloc (which is typical, usually we have
    871871 // a malloc + bitcast) then replace them with uses of the new global. Update
    898898 new GlobalVariable(Type::getInt1Ty(GV->getContext()), false,
    899899 GlobalValue::InternalLinkage,
    900900 ConstantInt::getFalse(GV->getContext()),
    901 GV->getName()+".init", GV->isThreadLocal());
    901 GV->getName()+".init", GV->getThreadLocalMode());
    902902 bool InitBoolUsed = false;
    903903
    904904 // Loop over all uses of GV, processing them in turn.
    13201320 PFieldTy, false, GlobalValue::InternalLinkage,
    13211321 Constant::getNullValue(PFieldTy),
    13221322 GV->getName() + ".f" + Twine(FieldNo), GV,
    1323 GV->isThreadLocal());
    1323 GV->getThreadLocalMode());
    13241324 FieldGlobals.push_back(NGV);
    13251325
    13261326 unsigned TypeSize = TD->getTypeAllocSize(FieldTy);
    16461646 GlobalValue::InternalLinkage,
    16471647 ConstantInt::getFalse(GV->getContext()),
    16481648 GV->getName()+".b",
    1649 GV->isThreadLocal());
    1649 GV->getThreadLocalMode());
    16501650 GV->getParent()->getGlobalList().insert(GV, NewGV);
    16511651
    16521652 Constant *InitVal = GV->getInitializer();
    20532053 // Create the new global and insert it next to the existing list.
    20542054 GlobalVariable *NGV = new GlobalVariable(CA->getType(), GCL->isConstant(),
    20552055 GCL->getLinkage(), CA, "",
    2056 GCL->isThreadLocal());
    2056 GCL->getThreadLocalMode());
    20572057 GCL->getParent()->getGlobalList().insert(GCL, NGV);
    20582058 NGV->takeName(GCL);
    20592059
    510510 // Create a new global variable with enough space for a redzone.
    511511 GlobalVariable *NewGlobal = new GlobalVariable(
    512512 M, NewTy, G->isConstant(), G->getLinkage(),
    513 NewInitializer, "", G, G->isThreadLocal());
    513 NewInitializer, "", G, G->getThreadLocalMode());
    514514 NewGlobal->copyAttributesFrom(G);
    515515 NewGlobal->setAlignment(RedzoneSize);
    516516
    447447 new GlobalVariable(*M, CounterTy, false,
    448448 GlobalValue::InternalLinkage,
    449449 Constant::getNullValue(CounterTy),
    450 "__llvm_gcov_ctr", 0, false, 0);
    450 "__llvm_gcov_ctr");
    451451 CountersBySP.push_back(std::make_pair(Counters, (MDNode*)SP));
    452452
    453453 UniqueVector ComplexEdgePreds;
    5252 I->isConstant(), I->getLinkage(),
    5353 (Constant*) 0, I->getName(),
    5454 (GlobalVariable*) 0,
    55 I->isThreadLocal(),
    55 I->getThreadLocalMode(),
    5656 I->getType()->getAddressSpace());
    5757 GV->copyAttributesFrom(I);
    5858 VMap[I] = GV;
    13751375 }
    13761376 }
    13771377
    1378 static void PrintThreadLocalModel(GlobalVariable::ThreadLocalMode TLM,
    1379 formatted_raw_ostream &Out) {
    1380 switch (TLM) {
    1381 case GlobalVariable::NotThreadLocal:
    1382 break;
    1383 case GlobalVariable::GeneralDynamicTLSModel:
    1384 Out << "thread_local ";
    1385 break;
    1386 case GlobalVariable::LocalDynamicTLSModel:
    1387 Out << "thread_local(localdynamic) ";
    1388 break;
    1389 case GlobalVariable::InitialExecTLSModel:
    1390 Out << "thread_local(initialexec) ";
    1391 break;
    1392 case GlobalVariable::LocalExecTLSModel:
    1393 Out << "thread_local(localexec) ";
    1394 break;
    1395 }
    1396 }
    1397
    13781398 void AssemblyWriter::printGlobal(const GlobalVariable *GV) {
    13791399 if (GV->isMaterializable())
    13801400 Out << "; Materializable\n";
    13871407
    13881408 PrintLinkage(GV->getLinkage(), Out);
    13891409 PrintVisibility(GV->getVisibility(), Out);
    1390
    1391 if (GV->isThreadLocal()) Out << "thread_local ";
    1410 PrintThreadLocalModel(GV->getThreadLocalMode(), Out);
    1411
    13921412 if (unsigned AddressSpace = GV->getType()->getAddressSpace())
    13931413 Out << "addrspace(" << AddressSpace << ") ";
    13941414 if (GV->hasUnnamedAddr()) Out << "unnamed_addr ";
    12091209 unsigned AddressSpace) {
    12101210 return wrap(new GlobalVariable(*unwrap(M), unwrap(Ty), false,
    12111211 GlobalValue::ExternalLinkage, 0, Name, 0,
    1212 false, AddressSpace));
    1212 GlobalVariable::NotThreadLocal, AddressSpace));
    12131213 }
    12141214
    12151215 LLVMValueRef LLVMGetNamedGlobal(LLVMModuleRef M, const char *Name) {
    7979 // GlobalVariable Implementation
    8080 //===----------------------------------------------------------------------===//
    8181
    82 // TODO: Remove once clang is updated.
    8283 GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
    8384 Constant *InitVal, const Twine &Name,
    8485 bool ThreadLocal, unsigned AddressSpace)
    85 : GlobalValue(PointerType::get(Ty, AddressSpace),
    86 : GlobalValue(PointerType::get(Ty, AddressSpace),
    8687 Value::GlobalVariableVal,
    8788 OperandTraits::op_begin(this),
    8889 InitVal != 0, Link, Name),
    89 isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
    90 isConstantGlobal(constant),
    91 threadLocalMode(ThreadLocal ? GeneralDynamicTLSModel : NotThreadLocal) {
    9092 if (InitVal) {
    9193 assert(InitVal->getType() == Ty &&
    9294 "Initializer should be the same type as the GlobalVariable!");
    9698 LeakDetector::addGarbageObject(this);
    9799 }
    98100
    101 // TODO: Remove once clang is updated.
    99102 GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
    100103 LinkageTypes Link, Constant *InitVal,
    101104 const Twine &Name,
    102105 GlobalVariable *Before, bool ThreadLocal,
    103106 unsigned AddressSpace)
    107 : GlobalValue(PointerType::get(Ty, AddressSpace),
    108 Value::GlobalVariableVal,
    109 OperandTraits::op_begin(this),
    110 InitVal != 0, Link, Name),
    111 isConstantGlobal(constant),
    112 threadLocalMode(ThreadLocal ? GeneralDynamicTLSModel : NotThreadLocal) {
    113 if (InitVal) {
    114 assert(InitVal->getType() == Ty &&
    115 "Initializer should be the same type as the GlobalVariable!");
    116 Op<0>() = InitVal;
    117 }
    118
    119 LeakDetector::addGarbageObject(this);
    120
    121 if (Before)
    122 Before->getParent()->getGlobalList().insert(Before, this);
    123 else
    124 M.getGlobalList().push_back(this);
    125 }
    126
    127 GlobalVariable::GlobalVariable(Type *Ty, bool constant, LinkageTypes Link,
    128 Constant *InitVal, const Twine &Name,
    129 ThreadLocalMode TLMode, unsigned AddressSpace)
    130 : GlobalValue(PointerType::get(Ty, AddressSpace),
    131 Value::GlobalVariableVal,
    132 OperandTraits::op_begin(this),
    133 InitVal != 0, Link, Name),
    134 isConstantGlobal(constant), threadLocalMode(TLMode) {
    135 if (InitVal) {
    136 assert(InitVal->getType() == Ty &&
    137 "Initializer should be the same type as the GlobalVariable!");
    138 Op<0>() = InitVal;
    139 }
    140
    141 LeakDetector::addGarbageObject(this);
    142 }
    143
    144 GlobalVariable::GlobalVariable(Module &M, Type *Ty, bool constant,
    145 LinkageTypes Link, Constant *InitVal,
    146 const Twine &Name,
    147 GlobalVariable *Before, ThreadLocalMode TLMode,
    148 unsigned AddressSpace)
    104149 : GlobalValue(PointerType::get(Ty, AddressSpace),
    105150 Value::GlobalVariableVal,
    106151 OperandTraits::op_begin(this),
    107152 InitVal != 0, Link, Name),
    108 isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) {
    153 isConstantGlobal(constant), threadLocalMode(TLMode) {
    109154 if (InitVal) {
    110155 assert(InitVal->getType() == Ty &&
    111156 "Initializer should be the same type as the GlobalVariable!");
    2727 Module &M = *BB->getParent()->getParent();
    2828 GlobalVariable *GV = new GlobalVariable(M, StrConstant->getType(),
    2929 true, GlobalValue::PrivateLinkage,
    30 StrConstant, "", 0, false);
    30 StrConstant);
    3131 GV->setName(Name);
    3232 GV->setUnnamedAddr(true);
    3333 return GV;
    0 ; RUN: llvm-as < %s | llvm-dis | llvm-as | llvm-dis | FileCheck %s
    1
    2 ; CHECK: @a = thread_local global i32 0
    3 ; CHECK: @b = thread_local(localdynamic) global i32 0
    4 ; CHECK: @c = thread_local(initialexec) global i32 0
    5 ; CHECK: @d = thread_local(localexec) global i32 0
    6
    7 @a = thread_local global i32 0
    8 @b = thread_local(localdynamic) global i32 0
    9 @c = thread_local(initialexec) global i32 0
    10 @d = thread_local(localexec) global i32 0
    0 ; RUN: llc -march=arm -mtriple=arm-linux-gnueabi < %s | FileCheck -check-prefix=CHECK-NONPIC %s
    1 ; RUN: llc -march=arm -mtriple=arm-linux-gnueabi -relocation-model=pic < %s | FileCheck -check-prefix=CHECK-PIC %s
    2
    3
    4 @external_gd = external thread_local global i32
    5 @internal_gd = internal thread_local global i32 42
    6
    7 @external_ld = external thread_local(localdynamic) global i32
    8 @internal_ld = internal thread_local(localdynamic) global i32 42
    9
    10 @external_ie = external thread_local(initialexec) global i32
    11 @internal_ie = internal thread_local(initialexec) global i32 42
    12
    13 @external_le = external thread_local(localexec) global i32
    14 @internal_le = internal thread_local(localexec) global i32 42
    15
    16 ; ----- no model specified -----
    17
    18 define i32* @f1() {
    19 entry:
    20 ret i32* @external_gd
    21
    22 ; Non-PIC code can use initial-exec, PIC code has to use general dynamic.
    23 ; CHECK-NONPIC: f1:
    24 ; CHECK-NONPIC: external_gd(gottpoff)
    25 ; CHECK-PIC: f1:
    26 ; CHECK-PIC: external_gd(tlsgd)
    27 }
    28
    29 define i32* @f2() {
    30 entry:
    31 ret i32* @internal_gd
    32
    33 ; Non-PIC code can use local exec, PIC code can use local dynamic,
    34 ; but that is not implemented, so falls back to general dynamic.
    35 ; CHECK-NONPIC: f2:
    36 ; CHECK-NONPIC: internal_gd(tpoff)
    37 ; CHECK-PIC: f2:
    38 ; CHECK-PIC: internal_gd(tlsgd)
    39 }
    40
    41
    42 ; ----- localdynamic specified -----
    43
    44 define i32* @f3() {
    45 entry:
    46 ret i32* @external_ld
    47
    48 ; Non-PIC code can use initial exec, PIC should use local dynamic,
    49 ; but that is not implemented, so falls back to general dynamic.
    50 ; CHECK-NONPIC: f3:
    51 ; CHECK-NONPIC: external_ld(gottpoff)
    52 ; CHECK-PIC: f3:
    53 ; CHECK-PIC: external_ld(tlsgd)
    54 }
    55
    56 define i32* @f4() {
    57 entry:
    58 ret i32* @internal_ld
    59
    60 ; Non-PIC code can use local exec, PIC code can use local dynamic,
    61 ; but that is not implemented, so it falls back to general dynamic.
    62 ; CHECK-NONPIC: f4:
    63 ; CHECK-NONPIC: internal_ld(tpoff)
    64 ; CHECK-PIC: f4:
    65 ; CHECK-PIC: internal_ld(tlsgd)
    66 }
    67
    68
    69 ; ----- initialexec specified -----
    70
    71 define i32* @f5() {
    72 entry:
    73 ret i32* @external_ie
    74
    75 ; Non-PIC and PIC code will use initial exec as specified.
    76 ; CHECK-NONPIC: f5:
    77 ; CHECK-NONPIC: external_ie(gottpoff)
    78 ; CHECK-PIC: f5:
    79 ; CHECK-PIC: external_ie(gottpoff)
    80 }
    81
    82 define i32* @f6() {
    83 entry:
    84 ret i32* @internal_ie
    85
    86 ; Non-PIC code can use local exec, PIC code use initial exec as specified.
    87 ; CHECK-NONPIC: f6:
    88 ; CHECK-NONPIC: internal_ie(tpoff)
    89 ; CHECK-PIC: f6:
    90 ; CHECK-PIC: internal_ie(gottpoff)
    91 }
    92
    93
    94 ; ----- localexec specified -----
    95
    96 define i32* @f7() {
    97 entry:
    98 ret i32* @external_le
    99
    100 ; Non-PIC and PIC code will use local exec as specified.
    101 ; CHECK-NONPIC: f7:
    102 ; CHECK-NONPIC: external_le(tpoff)
    103 ; CHECK-PIC: f7:
    104 ; CHECK-PIC: external_le(tpoff)
    105 }
    106
    107 define i32* @f8() {
    108 entry:
    109 ret i32* @internal_le
    110
    111 ; Non-PIC and PIC code will use local exec as specified.
    112 ; CHECK-NONPIC: f8:
    113 ; CHECK-NONPIC: internal_le(tpoff)
    114 ; CHECK-PIC: f8:
    115 ; CHECK-PIC: internal_le(tpoff)
    116 }
    0 ; RUN: llc -march=mipsel < %s | FileCheck -check-prefix=CHECK-PIC %s
    1 ; RUN: llc -march=mipsel -relocation-model=static < %s | FileCheck -check-prefix=CHECK-NONPIC %s
    2
    3 @external_gd = external thread_local global i32
    4 @internal_gd = internal thread_local global i32 42
    5
    6 @external_ld = external thread_local(localdynamic) global i32
    7 @internal_ld = internal thread_local(localdynamic) global i32 42
    8
    9 @external_ie = external thread_local(initialexec) global i32
    10 @internal_ie = internal thread_local(initialexec) global i32 42
    11
    12 @external_le = external thread_local(localexec) global i32
    13 @internal_le = internal thread_local(localexec) global i32 42
    14
    15 ; ----- no model specified -----
    16
    17 define i32* @f1() {
    18 entry:
    19 ret i32* @external_gd
    20
    21 ; Non-PIC code can use initial-exec, PIC code has to use general dynamic.
    22 ; CHECK-NONPIC: f1:
    23 ; CHECK-NONPIC: %gottprel
    24 ; CHECK-PIC: f1:
    25 ; CHECK-PIC: %tlsgd
    26 }
    27
    28 define i32* @f2() {
    29 entry:
    30 ret i32* @internal_gd
    31
    32 ; Non-PIC code can use local exec, PIC code can use local dynamic.
    33 ; CHECK-NONPIC: f2:
    34 ; CHECK-NONPIC: %tprel_hi
    35 ; CHECK-PIC: f2:
    36 ; CHECK-PIC: %tlsldm
    37 }
    38
    39
    40 ; ----- localdynamic specified -----
    41
    42 define i32* @f3() {
    43 entry:
    44 ret i32* @external_ld
    45
    46 ; Non-PIC code can use initial exec, PIC should use local dynamic.
    47 ; CHECK-NONPIC: f3:
    48 ; CHECK-NONPIC: %gottprel
    49 ; CHECK-PIC: f3:
    50 ; CHECK-PIC: %tlsldm
    51 }
    52
    53 define i32* @f4() {
    54 entry:
    55 ret i32* @internal_ld
    56
    57 ; Non-PIC code can use local exec, PIC code can use local dynamic.
    58 ; CHECK-NONPIC: f4:
    59 ; CHECK-NONPIC: %tprel_hi
    60 ; CHECK-PIC: f4:
    61 ; CHECK-PIC: %tlsldm
    62 }
    63
    64
    65 ; ----- initialexec specified -----
    66
    67 define i32* @f5() {
    68 entry:
    69 ret i32* @external_ie
    70
    71 ; Non-PIC and PIC code will use initial exec as specified.
    72 ; CHECK-NONPIC: f5:
    73 ; CHECK-NONPIC: %gottprel
    74 ; CHECK-PIC: f5:
    75 ; CHECK-PIC: %gottprel
    76 }
    77
    78 define i32* @f6() {
    79 entry:
    80 ret i32* @internal_ie
    81
    82 ; Non-PIC code can use local exec, PIC code use initial exec as specified.
    83 ; CHECK-NONPIC: f6:
    84 ; CHECK-NONPIC: %tprel_hi
    85 ; CHECK-PIC: f6:
    86 ; CHECK-PIC: %gottprel
    87 }
    88
    89
    90 ; ----- localexec specified -----
    91
    92 define i32* @f7() {
    93 entry:
    94 ret i32* @external_le
    95
    96 ; Non-PIC and PIC code will use local exec as specified.
    97 ; CHECK-NONPIC: f7:
    98 ; CHECK-NONPIC: %tprel_hi
    99 ; CHECK-PIC: f7:
    100 ; CHECK-PIC: %tprel_hi
    101 }
    102
    103 define i32* @f8() {
    104 entry:
    105 ret i32* @internal_le
    106
    107 ; Non-PIC and PIC code will use local exec as specified.
    108 ; CHECK-NONPIC: f8:
    109 ; CHECK-NONPIC: %tprel_hi
    110 ; CHECK-PIC: f8:
    111 ; CHECK-PIC: %tprel_hi
    112 }
    0 ; RUN: llc < %s -march=x86-64 -mtriple=x86_64-linux-gnu | FileCheck -check-prefix=X64 %s
    1 ; RUN: llc < %s -march=x86-64 -mtriple=x86_64-linux-gnu -relocation-model=pic | FileCheck -check-prefix=X64_PIC %s
    2 ; RUN: llc < %s -march=x86 -mtriple=i386-linux-gnu | FileCheck -check-prefix=X32 %s
    3 ; RUN: llc < %s -march=x86 -mtriple=i386-linux-gnu -relocation-model=pic | FileCheck -check-prefix=X32_PIC %s
    4
    5 ; Darwin always uses the same model.
    6 ; RUN: llc < %s -march=x86-64 -mtriple=x86_64-apple-darwin | FileCheck -check-prefix=DARWIN %s
    7
    8 @external_gd = external thread_local global i32
    9 @internal_gd = internal thread_local global i32 42
    10
    11 @external_ld = external thread_local(localdynamic) global i32
    12 @internal_ld = internal thread_local(localdynamic) global i32 42
    13
    14 @external_ie = external thread_local(initialexec) global i32
    15 @internal_ie = internal thread_local(initialexec) global i32 42
    16
    17 @external_le = external thread_local(localexec) global i32
    18 @internal_le = internal thread_local(localexec) global i32 42
    19
    20 ; ----- no model specified -----
    21
    22 define i32* @f1() {
    23 entry:
    24 ret i32* @external_gd
    25
    26 ; Non-PIC code can use initial-exec, PIC code has to use general dynamic.
    27 ; X64: f1:
    28 ; X64: external_gd@GOTTPOFF
    29 ; X32: f1:
    30 ; X32: external_gd@INDNTPOFF
    31 ; X64_PIC: f1:
    32 ; X64_PIC: external_gd@TLSGD
    33 ; X32_PIC: f1:
    34 ; X32_PIC: external_gd@TLSGD
    35 ; DARWIN: f1:
    36 ; DARWIN: _external_gd@TLVP
    37 }
    38
    39 define i32* @f2() {
    40 entry:
    41 ret i32* @internal_gd
    42
    43 ; Non-PIC code can use local exec, PIC code can use local dynamic.
    44 ; X64: f2:
    45 ; X64: internal_gd@TPOFF
    46 ; X32: f2:
    47 ; X32: internal_gd@NTPOFF
    48 ; X64_PIC: f2:
    49 ; X64_PIC: internal_gd@TLSLD
    50 ; X32_PIC: f2:
    51 ; X32_PIC: internal_gd@TLSLDM
    52 ; DARWIN: f2:
    53 ; DARWIN: _internal_gd@TLVP
    54 }
    55
    56
    57 ; ----- localdynamic specified -----
    58
    59 define i32* @f3() {
    60 entry:
    61 ret i32* @external_ld
    62
    63 ; Non-PIC code can use initial exec, PIC code use local dynamic as specified.
    64 ; X64: f3:
    65 ; X64: external_ld@GOTTPOFF
    66 ; X32: f3:
    67 ; X32: external_ld@INDNTPOFF
    68 ; X64_PIC: f3:
    69 ; X64_PIC: external_ld@TLSLD
    70 ; X32_PIC: f3:
    71 ; X32_PIC: external_ld@TLSLDM
    72 ; DARWIN: f3:
    73 ; DARWIN: _external_ld@TLVP
    74 }
    75
    76 define i32* @f4() {
    77 entry:
    78 ret i32* @internal_ld
    79
    80 ; Non-PIC code can use local exec, PIC code can use local dynamic.
    81 ; X64: f4:
    82 ; X64: internal_ld@TPOFF
    83 ; X32: f4:
    84 ; X32: internal_ld@NTPOFF
    85 ; X64_PIC: f4:
    86 ; X64_PIC: internal_ld@TLSLD
    87 ; X32_PIC: f4:
    88 ; X32_PIC: internal_ld@TLSLDM
    89 ; DARWIN: f4:
    90 ; DARWIN: _internal_ld@TLVP
    91 }
    92
    93
    94 ; ----- initialexec specified -----
    95
    96 define i32* @f5() {
    97 entry:
    98 ret i32* @external_ie
    99
    100 ; Non-PIC and PIC code will use initial exec as specified.
    101 ; X64: f5:
    102 ; X64: external_ie@GOTTPOFF
    103 ; X32: f5:
    104 ; X32: external_ie@INDNTPOFF
    105 ; X64_PIC: f5:
    106 ; X64_PIC: external_ie@GOTTPOFF
    107 ; X32_PIC: f5:
    108 ; X32_PIC: external_ie@GOTNTPOFF
    109 ; DARWIN: f5:
    110 ; DARWIN: _external_ie@TLVP
    111 }
    112
    113 define i32* @f6() {
    114 entry:
    115 ret i32* @internal_ie
    116
    117 ; Non-PIC code can use local exec, PIC code use initial exec as specified.
    118 ; X64: f6:
    119 ; X64: internal_ie@TPOFF
    120 ; X32: f6:
    121 ; X32: internal_ie@NTPOFF
    122 ; X64_PIC: f6:
    123 ; X64_PIC: internal_ie@GOTTPOFF
    124 ; X32_PIC: f6:
    125 ; X32_PIC: internal_ie@GOTNTPOFF
    126 ; DARWIN: f6:
    127 ; DARWIN: _internal_ie@TLVP
    128 }
    129
    130
    131 ; ----- localexec specified -----
    132
    133 define i32* @f7() {
    134 entry:
    135 ret i32* @external_le
    136
    137 ; Non-PIC and PIC code will use local exec as specified.
    138 ; X64: f7:
    139 ; X64: external_le@TPOFF
    140 ; X32: f7:
    141 ; X32: external_le@NTPOFF
    142 ; X64_PIC: f7:
    143 ; X64_PIC: external_le@TPOFF
    144 ; X32_PIC: f7:
    145 ; X32_PIC: external_le@NTPOFF
    146 ; DARWIN: f7:
    147 ; DARWIN: _external_le@TLVP
    148 }
    149
    150 define i32* @f8() {
    151 entry:
    152 ret i32* @internal_le
    153
    154 ; Non-PIC and PIC code will use local exec as specified.
    155 ; X64: f8:
    156 ; X64: internal_le@TPOFF
    157 ; X32: f8:
    158 ; X32: internal_le@NTPOFF
    159 ; X64_PIC: f8:
    160 ; X64_PIC: internal_le@TPOFF
    161 ; X32_PIC: f8:
    162 ; X32_PIC: internal_le@NTPOFF
    163 ; DARWIN: f8:
    164 ; DARWIN: _internal_le@TLVP
    165 }