llvm.org GIT mirror llvm / f0234fc
Implement the local-dynamic TLS model for x86 (PR3985) This implements codegen support for accesses to thread-local variables using the local-dynamic model, and adds a clean-up pass so that the base address for the TLS block can be re-used between local-dynamic access on an execution path. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@157818 91177308-0d34-0410-b5e6-96231b3b80d8 Hans Wennborg 8 years ago
13 changed file(s) with 353 addition(s) and 17 deletion(s). Raw diff Collapse all Expand all
9999 /// SYMBOL_LABEL @TLSGD
100100 MO_TLSGD,
101101
102 /// MO_TLSLD - On a symbol operand this indicates that the immediate is
103 /// the offset of the GOT entry with the TLS index for the module that
104 /// contains the symbol. When this index is passed to a call to to
105 /// __tls_get_addr, the function will return the base address of the TLS
106 /// block for the symbol.
107 ///
108 /// See 'ELF Handling for Thread-Local Storage' for more details.
109 /// SYMBOL_LABEL @TLSLD
110 MO_TLSLD,
111
112 /// MO_TLSLDM - On a symbol operand this indicates that the immediate is
113 /// the offset of the GOT entry with the TLS index for the module that
114 /// contains the symbol. When this index is passed to a call to to
115 /// ___tls_get_addr, the function will return the base address of the TLS
116 /// block for the symbol.
117 ///
118 /// See 'ELF Handling for Thread-Local Storage' for more details.
119 /// SYMBOL_LABEL @TLSLDM
120 MO_TLSLDM,
121
102122 /// MO_GOTTPOFF - On a symbol operand this indicates that the immediate is
103123 /// some TLS offset.
104124 ///
119139 /// See 'ELF Handling for Thread-Local Storage' for more details.
120140 /// SYMBOL_LABEL @TPOFF
121141 MO_TPOFF,
142
143 /// MO_DTPOFF - On a symbol operand this indicates that the immediate is
144 /// the offset of the GOT entry with the TLS offset of the symbol.
145 ///
146 /// See 'ELF Handling for Thread-Local Storage' for more details.
147 /// SYMBOL_LABEL @DTPOFF
148 MO_DTPOFF,
122149
123150 /// MO_NTPOFF - On a symbol operand this indicates that the immediate is
124151 /// some TLS offset.
3535 /// register for PIC on x86-32.
3636 FunctionPass* createGlobalBaseRegPass();
3737
38 /// createCleanupLocalDynamicTLSPass() - This pass combines multiple accesses
39 /// to local-dynamic TLS variables so that the TLS base address for the module
40 /// is only fetched once per execution path through the function.
41 FunctionPass *createCleanupLocalDynamicTLSPass();
42
3843 /// createX86FloatingPointStackifierPass - This function returns a pass which
3944 /// converts floating point register references and pseudo instructions into
4045 /// floating point stack references and physical instructions.
185185 O << '-' << *MF->getPICBaseSymbol();
186186 break;
187187 case X86II::MO_TLSGD: O << "@TLSGD"; break;
188 case X86II::MO_TLSLD: O << "@TLSLD"; break;
189 case X86II::MO_TLSLDM: O << "@TLSLDM"; break;
188190 case X86II::MO_GOTTPOFF: O << "@GOTTPOFF"; break;
189191 case X86II::MO_INDNTPOFF: O << "@INDNTPOFF"; break;
190192 case X86II::MO_TPOFF: O << "@TPOFF"; break;
193 case X86II::MO_DTPOFF: O << "@DTPOFF"; break;
191194 case X86II::MO_NTPOFF: O << "@NTPOFF"; break;
192195 case X86II::MO_GOTNTPOFF: O << "@GOTNTPOFF"; break;
193196 case X86II::MO_GOTPCREL: O << "@GOTPCREL"; break;
72627262 static SDValue
72637263 GetTLSADDR(SelectionDAG &DAG, SDValue Chain, GlobalAddressSDNode *GA,
72647264 SDValue *InFlag, const EVT PtrVT, unsigned ReturnReg,
7265 unsigned char OperandFlags) {
7265 unsigned char OperandFlags, bool LocalDynamic = false) {
72667266 MachineFrameInfo *MFI = DAG.getMachineFunction().getFrameInfo();
72677267 SDVTList NodeTys = DAG.getVTList(MVT::Other, MVT::Glue);
72687268 DebugLoc dl = GA->getDebugLoc();
72707270 GA->getValueType(0),
72717271 GA->getOffset(),
72727272 OperandFlags);
7273
7274 X86ISD::NodeType CallType = LocalDynamic ? X86ISD::TLSBASEADDR
7275 : X86ISD::TLSADDR;
7276
72737277 if (InFlag) {
72747278 SDValue Ops[] = { Chain, TGA, *InFlag };
7275 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 3);
7279 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 3);
72767280 } else {
72777281 SDValue Ops[] = { Chain, TGA };
7278 Chain = DAG.getNode(X86ISD::TLSADDR, dl, NodeTys, Ops, 2);
7282 Chain = DAG.getNode(CallType, dl, NodeTys, Ops, 2);
72797283 }
72807284
72817285 // TLSADDR will be codegen'ed as call. Inform MFI that function has calls.
73057309 const EVT PtrVT) {
73067310 return GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT,
73077311 X86::RAX, X86II::MO_TLSGD);
7312 }
7313
7314 static SDValue LowerToTLSLocalDynamicModel(GlobalAddressSDNode *GA,
7315 SelectionDAG &DAG,
7316 const EVT PtrVT,
7317 bool is64Bit) {
7318 DebugLoc dl = GA->getDebugLoc();
7319
7320 // Get the start address of the TLS block for this module.
7321 X86MachineFunctionInfo* MFI = DAG.getMachineFunction()
7322 .getInfo();
7323 MFI->incNumLocalDynamicTLSAccesses();
7324
7325 SDValue Base;
7326 if (is64Bit) {
7327 Base = GetTLSADDR(DAG, DAG.getEntryNode(), GA, NULL, PtrVT, X86::RAX,
7328 X86II::MO_TLSLD, /*LocalDynamic=*/true);
7329 } else {
7330 SDValue InFlag;
7331 SDValue Chain = DAG.getCopyToReg(DAG.getEntryNode(), dl, X86::EBX,
7332 DAG.getNode(X86ISD::GlobalBaseReg, DebugLoc(), PtrVT), InFlag);
7333 InFlag = Chain.getValue(1);
7334 Base = GetTLSADDR(DAG, Chain, GA, &InFlag, PtrVT, X86::EAX,
7335 X86II::MO_TLSLDM, /*LocalDynamic=*/true);
7336 }
7337
7338 // Note: the CleanupLocalDynamicTLSPass will remove redundant computations
7339 // of Base.
7340
7341 // Build x@dtpoff.
7342 unsigned char OperandFlags = X86II::MO_DTPOFF;
7343 unsigned WrapperKind = X86ISD::Wrapper;
7344 SDValue TGA = DAG.getTargetGlobalAddress(GA->getGlobal(), dl,
7345 GA->getValueType(0),
7346 GA->getOffset(), OperandFlags);
7347 SDValue Offset = DAG.getNode(WrapperKind, dl, PtrVT, TGA);
7348
7349 // Add x@dtpoff with the base.
7350 return DAG.getNode(ISD::ADD, dl, PtrVT, Offset, Base);
73087351 }
73097352
73107353 // Lower ISD::GlobalTLSAddress using the "initial exec" or "local exec" model.
73717414 const GlobalValue *GV = GA->getGlobal();
73727415
73737416 if (Subtarget->isTargetELF()) {
7374 // TODO: implement the "local dynamic" model
7375
73767417 // If GV is an alias then use the aliasee for determining
73777418 // thread-localness.
73787419 if (const GlobalAlias *GA = dyn_cast(GV))
73827423
73837424 switch (model) {
73847425 case TLSModel::GeneralDynamic:
7385 case TLSModel::LocalDynamic: // not implemented
73867426 if (Subtarget->is64Bit())
73877427 return LowerToTLSGeneralDynamicModel64(GA, DAG, getPointerTy());
73887428 return LowerToTLSGeneralDynamicModel32(GA, DAG, getPointerTy());
7389
7429 case TLSModel::LocalDynamic:
7430 return LowerToTLSLocalDynamicModel(GA, DAG, getPointerTy(),
7431 Subtarget->is64Bit());
73907432 case TLSModel::InitialExec:
73917433 case TLSModel::LocalExec:
73927434 return LowerToTLSExecModel(GA, DAG, getPointerTy(), model,
1125611298 case X86ISD::FRSQRT: return "X86ISD::FRSQRT";
1125711299 case X86ISD::FRCP: return "X86ISD::FRCP";
1125811300 case X86ISD::TLSADDR: return "X86ISD::TLSADDR";
11301 case X86ISD::TLSBASEADDR: return "X86ISD::TLSBASEADDR";
1125911302 case X86ISD::TLSCALL: return "X86ISD::TLSCALL";
1126011303 case X86ISD::EH_RETURN: return "X86ISD::EH_RETURN";
1126111304 case X86ISD::TC_RETURN: return "X86ISD::TC_RETURN";
205205
206206 // TLSADDR - Thread Local Storage.
207207 TLSADDR,
208
209 // TLSBASEADDR - Thread Local Storage. A call to get the start address
210 // of the TLS block for the current module.
211 TLSBASEADDR,
208212
209213 // TLSCALL - Thread Local Storage. When calling to an OS provided
210214 // thunk at the address from an earlier relocation.
374374 MM0, MM1, MM2, MM3, MM4, MM5, MM6, MM7,
375375 XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6, XMM7,
376376 XMM8, XMM9, XMM10, XMM11, XMM12, XMM13, XMM14, XMM15, EFLAGS],
377 Uses = [ESP] in
377 Uses = [ESP] in {
378378 def TLS_addr32 : I<0, Pseudo, (outs), (ins i32mem:$sym),
379379 "# TLS_addr32",
380380 [(X86tlsaddr tls32addr:$sym)]>,
381381 Requires<[In32BitMode]>;
382 def TLS_base_addr32 : I<0, Pseudo, (outs), (ins i32mem:$sym),
383 "# TLS_base_addr32",
384 [(X86tlsbaseaddr tls32baseaddr:$sym)]>,
385 Requires<[In32BitMode]>;
386 }
382387
383388 // All calls clobber the non-callee saved registers. RSP is marked as
384389 // a use to prevent stack-pointer assignments that appear immediately
388393 MM0, MM1, MM2, MM3, MM4, MM5, MM6, MM7,
389394 XMM0, XMM1, XMM2, XMM3, XMM4, XMM5, XMM6, XMM7,
390395 XMM8, XMM9, XMM10, XMM11, XMM12, XMM13, XMM14, XMM15, EFLAGS],
391 Uses = [RSP] in
396 Uses = [RSP] in {
392397 def TLS_addr64 : I<0, Pseudo, (outs), (ins i64mem:$sym),
393398 "# TLS_addr64",
394399 [(X86tlsaddr tls64addr:$sym)]>,
395400 Requires<[In64BitMode]>;
401 def TLS_base_addr64 : I<0, Pseudo, (outs), (ins i64mem:$sym),
402 "# TLS_base_addr64",
403 [(X86tlsbaseaddr tls64baseaddr:$sym)]>,
404 Requires<[In64BitMode]>;
405 }
396406
397407 // Darwin TLS Support
398408 // For i386, the address of the thunk is passed on the stack, on return the
2020 #include "llvm/LLVMContext.h"
2121 #include "llvm/ADT/STLExtras.h"
2222 #include "llvm/CodeGen/MachineConstantPool.h"
23 #include "llvm/CodeGen/MachineDominators.h"
2324 #include "llvm/CodeGen/MachineFrameInfo.h"
2425 #include "llvm/CodeGen/MachineInstrBuilder.h"
2526 #include "llvm/CodeGen/MachineRegisterInfo.h"
39893990 AU.setPreservesCFG();
39903991 MachineFunctionPass::getAnalysisUsage(AU);
39913992 }
3993
3994 private:
3995 unsigned BaseReg;
39923996 };
39933997 }
39943998
39953999 char CGBR::ID = 0;
39964000 FunctionPass*
39974001 llvm::createGlobalBaseRegPass() { return new CGBR(); }
4002
4003 namespace {
4004 struct LDTLSCleanup : public MachineFunctionPass {
4005 static char ID;
4006 LDTLSCleanup() : MachineFunctionPass(ID) {}
4007
4008 virtual bool runOnMachineFunction(MachineFunction &MF) {
4009 X86MachineFunctionInfo* MFI = MF.getInfo();
4010 if (MFI->getNumLocalDynamicTLSAccesses() < 2) {
4011 // No point folding accesses if there isn't at least two.
4012 return false;
4013 }
4014
4015 MachineDominatorTree *DT = &getAnalysis();
4016 return VisitNode(DT->getRootNode(), 0);
4017 }
4018
4019 // Visit the dominator subtree rooted at Node in pre-order.
4020 // If TLSBaseAddrReg is non-null, then use that to replace any
4021 // TLS_base_addr instructions. Otherwise, create the register
4022 // when the first such instruction is seen, and then use it
4023 // as we encounter more instructions.
4024 bool VisitNode(MachineDomTreeNode *Node, unsigned TLSBaseAddrReg) {
4025 MachineBasicBlock *BB = Node->getBlock();
4026 bool Changed = false;
4027
4028 // Traverse the current block.
4029 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;
4030 ++I) {
4031 switch (I->getOpcode()) {
4032 case X86::TLS_base_addr32:
4033 case X86::TLS_base_addr64:
4034 if (TLSBaseAddrReg)
4035 I = ReplaceTLSBaseAddrCall(I, TLSBaseAddrReg);
4036 else
4037 I = SetRegister(I, &TLSBaseAddrReg);
4038 Changed = true;
4039 break;
4040 default:
4041 break;
4042 }
4043 }
4044
4045 // Visit the children of this block in the dominator tree.
4046 for (MachineDomTreeNode::iterator I = Node->begin(), E = Node->end();
4047 I != E; ++I) {
4048 Changed |= VisitNode(*I, TLSBaseAddrReg);
4049 }
4050
4051 return Changed;
4052 }
4053
4054 // Replace the TLS_base_addr instruction I with a copy from
4055 // TLSBaseAddrReg, returning the new instruction.
4056 MachineInstr *ReplaceTLSBaseAddrCall(MachineInstr *I,
4057 unsigned TLSBaseAddrReg) {
4058 MachineFunction *MF = I->getParent()->getParent();
4059 const X86TargetMachine *TM =
4060 static_cast(&MF->getTarget());
4061 const bool is64Bit = TM->getSubtarget().is64Bit();
4062 const X86InstrInfo *TII = TM->getInstrInfo();
4063
4064 // Insert a Copy from TLSBaseAddrReg to RAX/EAX.
4065 MachineInstr *Copy = BuildMI(*I->getParent(), I, I->getDebugLoc(),
4066 TII->get(TargetOpcode::COPY),
4067 is64Bit ? X86::RAX : X86::EAX)
4068 .addReg(TLSBaseAddrReg);
4069
4070 // Erase the TLS_base_addr instruction.
4071 I->eraseFromParent();
4072
4073 return Copy;
4074 }
4075
4076 // Create a virtal register in *TLSBaseAddrReg, and populate it by
4077 // inserting a copy instruction after I. Returns the new instruction.
4078 MachineInstr *SetRegister(MachineInstr *I, unsigned *TLSBaseAddrReg) {
4079 MachineFunction *MF = I->getParent()->getParent();
4080 const X86TargetMachine *TM =
4081 static_cast(&MF->getTarget());
4082 const bool is64Bit = TM->getSubtarget().is64Bit();
4083 const X86InstrInfo *TII = TM->getInstrInfo();
4084
4085 // Create a virtual register for the TLS base address.
4086 MachineRegisterInfo &RegInfo = MF->getRegInfo();
4087 *TLSBaseAddrReg = RegInfo.createVirtualRegister(is64Bit
4088 ? &X86::GR64RegClass
4089 : &X86::GR32RegClass);
4090
4091 // Insert a copy from RAX/EAX to TLSBaseAddrReg.
4092 MachineInstr *Next = I->getNextNode();
4093 MachineInstr *Copy = BuildMI(*I->getParent(), Next, I->getDebugLoc(),
4094 TII->get(TargetOpcode::COPY),
4095 *TLSBaseAddrReg)
4096 .addReg(is64Bit ? X86::RAX : X86::EAX);
4097
4098 return Copy;
4099 }
4100
4101 virtual const char *getPassName() const {
4102 return "Local Dynamic TLS Access Clean-up";
4103 }
4104
4105 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
4106 AU.setPreservesCFG();
4107 AU.addRequired();
4108 MachineFunctionPass::getAnalysisUsage(AU);
4109 }
4110 };
4111 }
4112
4113 char LDTLSCleanup::ID = 0;
4114 FunctionPass*
4115 llvm::createCleanupLocalDynamicTLSPass() { return new LDTLSCleanup(); }
9595 def SDTX86Wrapper : SDTypeProfile<1, 1, [SDTCisSameAs<0, 1>, SDTCisPtrTy<0>]>;
9696
9797 def SDT_X86TLSADDR : SDTypeProfile<0, 1, [SDTCisInt<0>]>;
98
99 def SDT_X86TLSBASEADDR : SDTypeProfile<0, 1, [SDTCisInt<0>]>;
98100
99101 def SDT_X86TLSCALL : SDTypeProfile<0, 1, [SDTCisInt<0>]>;
100102
202204 def X86tlsaddr : SDNode<"X86ISD::TLSADDR", SDT_X86TLSADDR,
203205 [SDNPHasChain, SDNPOptInGlue, SDNPOutGlue]>;
204206
207 def X86tlsbaseaddr : SDNode<"X86ISD::TLSBASEADDR", SDT_X86TLSBASEADDR,
208 [SDNPHasChain, SDNPOptInGlue, SDNPOutGlue]>;
209
205210 def X86ehret : SDNode<"X86ISD::EH_RETURN", SDT_X86EHRET,
206211 [SDNPHasChain]>;
207212
491496 def tls32addr : ComplexPattern
492497 [tglobaltlsaddr], []>;
493498
499 def tls32baseaddr : ComplexPattern
500 [tglobaltlsaddr], []>;
501
494502 def lea64addr : ComplexPattern
495503 [add, sub, mul, X86mul_imm, shl, or, frameindex,
496504 X86WrapperRIP], []>;
497505
498506 def tls64addr : ComplexPattern
507 [tglobaltlsaddr], []>;
508
509 def tls64baseaddr : ComplexPattern
499510 [tglobaltlsaddr], []>;
500511
501512 //===----------------------------------------------------------------------===//
155155 break;
156156 case X86II::MO_SECREL: RefKind = MCSymbolRefExpr::VK_SECREL; break;
157157 case X86II::MO_TLSGD: RefKind = MCSymbolRefExpr::VK_TLSGD; break;
158 case X86II::MO_TLSLD: RefKind = MCSymbolRefExpr::VK_TLSLD; break;
159 case X86II::MO_TLSLDM: RefKind = MCSymbolRefExpr::VK_TLSLDM; break;
158160 case X86II::MO_GOTTPOFF: RefKind = MCSymbolRefExpr::VK_GOTTPOFF; break;
159161 case X86II::MO_INDNTPOFF: RefKind = MCSymbolRefExpr::VK_INDNTPOFF; break;
160162 case X86II::MO_TPOFF: RefKind = MCSymbolRefExpr::VK_TPOFF; break;
163 case X86II::MO_DTPOFF: RefKind = MCSymbolRefExpr::VK_DTPOFF; break;
161164 case X86II::MO_NTPOFF: RefKind = MCSymbolRefExpr::VK_NTPOFF; break;
162165 case X86II::MO_GOTNTPOFF: RefKind = MCSymbolRefExpr::VK_GOTNTPOFF; break;
163166 case X86II::MO_GOTPCREL: RefKind = MCSymbolRefExpr::VK_GOTPCREL; break;
550553 static void LowerTlsAddr(MCStreamer &OutStreamer,
551554 X86MCInstLower &MCInstLowering,
552555 const MachineInstr &MI) {
553 bool is64Bits = MI.getOpcode() == X86::TLS_addr64;
556
557 bool is64Bits = MI.getOpcode() == X86::TLS_addr64 ||
558 MI.getOpcode() == X86::TLS_base_addr64;
559
560 bool needsPadding = MI.getOpcode() == X86::TLS_addr64;
561
554562 MCContext &context = OutStreamer.getContext();
555563
556 if (is64Bits) {
564 if (needsPadding) {
557565 MCInst prefix;
558566 prefix.setOpcode(X86::DATA16_PREFIX);
559567 OutStreamer.EmitInstruction(prefix);
560568 }
569
570 MCSymbolRefExpr::VariantKind SRVK;
571 switch (MI.getOpcode()) {
572 case X86::TLS_addr32:
573 case X86::TLS_addr64:
574 SRVK = MCSymbolRefExpr::VK_TLSGD;
575 break;
576 case X86::TLS_base_addr32:
577 SRVK = MCSymbolRefExpr::VK_TLSLDM;
578 break;
579 case X86::TLS_base_addr64:
580 SRVK = MCSymbolRefExpr::VK_TLSLD;
581 break;
582 default:
583 llvm_unreachable("unexpected opcode");
584 }
585
561586 MCSymbol *sym = MCInstLowering.GetSymbolFromOperand(MI.getOperand(3));
562 const MCSymbolRefExpr *symRef =
563 MCSymbolRefExpr::Create(sym, MCSymbolRefExpr::VK_TLSGD, context);
587 const MCSymbolRefExpr *symRef = MCSymbolRefExpr::Create(sym, SRVK, context);
564588
565589 MCInst LEA;
566590 if (is64Bits) {
582606 }
583607 OutStreamer.EmitInstruction(LEA);
584608
585 if (is64Bits) {
609 if (needsPadding) {
586610 MCInst prefix;
587611 prefix.setOpcode(X86::DATA16_PREFIX);
588612 OutStreamer.EmitInstruction(prefix);
644668
645669 case X86::TLS_addr32:
646670 case X86::TLS_addr64:
671 case X86::TLS_base_addr32:
672 case X86::TLS_base_addr64:
647673 return LowerTlsAddr(OutStreamer, MCInstLowering, *MI);
648674
649675 case X86::MOVPC32r: {
713739 MCInstLowering.Lower(MI, TmpInst);
714740 OutStreamer.EmitInstruction(TmpInst);
715741 }
716
6565 /// ArgumentStackSize - The number of bytes on stack consumed by the arguments
6666 /// being passed on the stack.
6767 unsigned ArgumentStackSize;
68 /// NumLocalDynamics - Number of local-dynamic TLS accesses.
69 unsigned NumLocalDynamics;
6870
6971 public:
7072 X86MachineFunctionInfo() : ForceFramePointer(false),
7880 RegSaveFrameIndex(0),
7981 VarArgsGPOffset(0),
8082 VarArgsFPOffset(0),
81 ArgumentStackSize(0) {}
83 ArgumentStackSize(0),
84 NumLocalDynamics(0) {}
8285
8386 explicit X86MachineFunctionInfo(MachineFunction &MF)
8487 : ForceFramePointer(false),
9295 RegSaveFrameIndex(0),
9396 VarArgsGPOffset(0),
9497 VarArgsFPOffset(0),
95 ArgumentStackSize(0) {}
98 ArgumentStackSize(0),
99 NumLocalDynamics(0) {}
96100
97101 bool getForceFramePointer() const { return ForceFramePointer;}
98102 void setForceFramePointer(bool forceFP) { ForceFramePointer = forceFP; }
129133
130134 unsigned getArgumentStackSize() const { return ArgumentStackSize; }
131135 void setArgumentStackSize(unsigned size) { ArgumentStackSize = size; }
136
137 unsigned getNumLocalDynamicTLSAccesses() const { return NumLocalDynamics; }
138 void incNumLocalDynamicTLSAccesses() { ++NumLocalDynamics; }
132139 };
133140
134141 } // End llvm namespace
146146 // Install an instruction selector.
147147 PM->add(createX86ISelDag(getX86TargetMachine(), getOptLevel()));
148148
149 // For ELF, cleanup any local-dynamic TLS accesses.
150 if (getX86Subtarget().isTargetELF() && getOptLevel() != CodeGenOpt::None)
151 PM->add(createCleanupLocalDynamicTLSPass());
152
149153 // For 32-bit, prepend instructions to set the "global base reg" for PIC.
150154 if (!getX86Subtarget().is64Bit())
151155 PM->add(createGlobalBaseRegPass());
0 ; RUN: llc < %s -march=x86-64 -mtriple=x86_64-linux-gnu -relocation-model=pic | FileCheck %s
1
2 @x = internal thread_local global i32 0, align 4
3 @y = internal thread_local global i32 0, align 4
4
5 ; get_x and get_y are here to prevent x and y to be optimized away as 0
6
7 define i32* @get_x() {
8 entry:
9 ret i32* @x
10 ; FIXME: This function uses a single thread-local variable,
11 ; so we might want to fall back to general-dynamic here.
12 ; CHECK: get_x:
13 ; CHECK: leaq x@TLSLD(%rip), %rdi
14 ; CHECK-NEXT: callq __tls_get_addr@PLT
15 ; CHECK: x@DTPOFF
16 }
17
18 define i32* @get_y() {
19 entry:
20 ret i32* @y
21 }
22
23 define i32 @f(i32 %i) {
24 entry:
25 %cmp = icmp eq i32 %i, 1
26 br i1 %cmp, label %return, label %if.else
27 ; This bb does not access TLS, so should not call __tls_get_addr.
28 ; CHECK: f:
29 ; CHECK-NOT: __tls_get_addr
30 ; CHECK: je
31
32
33 if.else:
34 %0 = load i32* @x, align 4
35 %cmp1 = icmp eq i32 %i, 2
36 br i1 %cmp1, label %if.then2, label %return
37 ; Now we call __tls_get_addr.
38 ; CHECK: # %if.else
39 ; CHECK: leaq x@TLSLD(%rip), %rdi
40 ; CHECK-NEXT: callq __tls_get_addr@PLT
41 ; CHECK: x@DTPOFF
42
43
44 if.then2:
45 %1 = load i32* @y, align 4
46 %add = add nsw i32 %1, %0
47 br label %return
48 ; This accesses TLS, but is dominated by the previous block,
49 ; so should not have to call __tls_get_addr again.
50 ; CHECK: # %if.then2
51 ; CHECK-NOT: __tls_get_addr
52 ; CHECK: y@DTPOFF
53
54
55 return:
56 %retval.0 = phi i32 [ %add, %if.then2 ], [ 5, %entry ], [ %0, %if.else ]
57 ret i32 %retval.0
58 }
11 ; RUN: llc < %s -march=x86-64 -mtriple=x86_64-linux-gnu -relocation-model=pic | FileCheck -check-prefix=X64 %s
22
33 @i = thread_local global i32 15
4 @j = internal thread_local global i32 42
5 @k = internal thread_local global i32 42
46
57 define i32 @f1() {
68 entry:
6365 ; X64: callq __tls_get_addr@PLT
6466
6567
68 define i32 @f5() nounwind {
69 entry:
70 %0 = load i32* @j, align 4
71 %1 = load i32* @k, align 4
72 %add = add nsw i32 %0, %1
73 ret i32 %add
74 }
6675
76 ; X32: f5:
77 ; X32: leal {{[jk]}}@TLSLDM
78 ; X32-NEXT: calll ___tls_get_addr@PLT
79 ; X32-NEXT: movl {{[jk]}}@DTPOFF(%eax)
80 ; X32-NEXT: addl {{[jk]}}@DTPOFF(%eax)
81
82 ; X64: f5:
83 ; X64: leaq {{[jk]}}@TLSLD(%rip), %rdi
84 ; X64-NEXT: callq __tls_get_addr@PLT
85 ; X64-NEXT: movl {{[jk]}}@DTPOFF(%rax)
86 ; X64-NEXT: addl {{[jk]}}@DTPOFF(%rax)