LLVM  3.7.0
AArch64CleanupLocalDynamicTLSPass.cpp
Go to the documentation of this file.
1 //===-- AArch64CleanupLocalDynamicTLSPass.cpp ---------------------*- C++ -*-=//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // Local-dynamic access to thread-local variables proceeds in three stages.
11 //
12 // 1. The offset of this Module's thread-local area from TPIDR_EL0 is calculated
13 // in much the same way as a general-dynamic TLS-descriptor access against
14 // the special symbol _TLS_MODULE_BASE.
15 // 2. The variable's offset from _TLS_MODULE_BASE_ is calculated using
16 // instructions with "dtprel" modifiers.
17 // 3. These two are added, together with TPIDR_EL0, to obtain the variable's
18 // true address.
19 //
20 // This is only better than general-dynamic access to the variable if two or
21 // more of the first stage TLS-descriptor calculations can be combined. This
22 // pass looks through a function and performs such combinations.
23 //
24 //===----------------------------------------------------------------------===//
25 #include "AArch64.h"
26 #include "AArch64InstrInfo.h"
28 #include "AArch64TargetMachine.h"
34 using namespace llvm;
35 
36 namespace {
37 struct LDTLSCleanup : public MachineFunctionPass {
38  static char ID;
39  LDTLSCleanup() : MachineFunctionPass(ID) {}
40 
41  bool runOnMachineFunction(MachineFunction &MF) override {
43  if (AFI->getNumLocalDynamicTLSAccesses() < 2) {
44  // No point folding accesses if there isn't at least two.
45  return false;
46  }
47 
48  MachineDominatorTree *DT = &getAnalysis<MachineDominatorTree>();
49  return VisitNode(DT->getRootNode(), 0);
50  }
51 
52  // Visit the dominator subtree rooted at Node in pre-order.
53  // If TLSBaseAddrReg is non-null, then use that to replace any
54  // TLS_base_addr instructions. Otherwise, create the register
55  // when the first such instruction is seen, and then use it
56  // as we encounter more instructions.
57  bool VisitNode(MachineDomTreeNode *Node, unsigned TLSBaseAddrReg) {
58  MachineBasicBlock *BB = Node->getBlock();
59  bool Changed = false;
60 
61  // Traverse the current block.
62  for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;
63  ++I) {
64  switch (I->getOpcode()) {
66  // Make sure it's a local dynamic access.
67  if (!I->getOperand(0).isSymbol() ||
68  strcmp(I->getOperand(0).getSymbolName(), "_TLS_MODULE_BASE_"))
69  break;
70 
71  if (TLSBaseAddrReg)
72  I = replaceTLSBaseAddrCall(I, TLSBaseAddrReg);
73  else
74  I = setRegister(I, &TLSBaseAddrReg);
75  Changed = true;
76  break;
77  default:
78  break;
79  }
80  }
81 
82  // Visit the children of this block in the dominator tree.
83  for (MachineDomTreeNode *N : *Node) {
84  Changed |= VisitNode(N, TLSBaseAddrReg);
85  }
86 
87  return Changed;
88  }
89 
90  // Replace the TLS_base_addr instruction I with a copy from
91  // TLSBaseAddrReg, returning the new instruction.
92  MachineInstr *replaceTLSBaseAddrCall(MachineInstr *I,
93  unsigned TLSBaseAddrReg) {
94  MachineFunction *MF = I->getParent()->getParent();
96 
97  // Insert a Copy from TLSBaseAddrReg to x0, which is where the rest of the
98  // code sequence assumes the address will be.
99  MachineInstr *Copy = BuildMI(*I->getParent(), I, I->getDebugLoc(),
100  TII->get(TargetOpcode::COPY),
101  AArch64::X0).addReg(TLSBaseAddrReg);
102 
103  // Erase the TLS_base_addr instruction.
104  I->eraseFromParent();
105 
106  return Copy;
107  }
108 
109  // Create a virtal register in *TLSBaseAddrReg, and populate it by
110  // inserting a copy instruction after I. Returns the new instruction.
111  MachineInstr *setRegister(MachineInstr *I, unsigned *TLSBaseAddrReg) {
112  MachineFunction *MF = I->getParent()->getParent();
113  const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
114 
115  // Create a virtual register for the TLS base address.
116  MachineRegisterInfo &RegInfo = MF->getRegInfo();
117  *TLSBaseAddrReg = RegInfo.createVirtualRegister(&AArch64::GPR64RegClass);
118 
119  // Insert a copy from X0 to TLSBaseAddrReg for later.
120  MachineInstr *Next = I->getNextNode();
121  MachineInstr *Copy = BuildMI(*I->getParent(), Next, I->getDebugLoc(),
122  TII->get(TargetOpcode::COPY),
123  *TLSBaseAddrReg).addReg(AArch64::X0);
124 
125  return Copy;
126  }
127 
128  const char *getPassName() const override {
129  return "Local Dynamic TLS Access Clean-up";
130  }
131 
132  void getAnalysisUsage(AnalysisUsage &AU) const override {
133  AU.setPreservesCFG();
136  }
137 };
138 }
139 
140 char LDTLSCleanup::ID = 0;
142  return new LDTLSCleanup();
143 }
const MachineFunction * getParent() const
getParent - Return the MachineFunction containing this basic block.
AArch64FunctionInfo - This class is derived from MachineFunctionInfo and contains private AArch64-spe...
unsigned createVirtualRegister(const TargetRegisterClass *RegClass)
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
MachineDomTreeNode * getRootNode() const
COPY - Target-independent register copy.
Definition: TargetOpcodes.h:86
AnalysisUsage & addRequired()
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
const HexagonInstrInfo * TII
NodeTy * getNextNode()
Get the next node, or 0 for the list tail.
Definition: ilist_node.h:80
void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
Base class for the actual dominator tree node.
const MachineBasicBlock * getParent() const
Definition: MachineInstr.h:120
TargetInstrInfo - Interface to description of machine instruction set.
bundle_iterator< MachineInstr, instr_iterator > iterator
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
Represent the analysis usage information of a pass.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:294
MachineInstrBuilder BuildMI(MachineFunction &MF, DebugLoc DL, const MCInstrDesc &MCID)
BuildMI - Builder interface.
const MCInstrDesc & get(unsigned Opcode) const
Return the machine instruction descriptor that corresponds to the specified instruction opcode...
Definition: MCInstrInfo.h:45
unsigned getNumLocalDynamicTLSAccesses() const
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:263
NodeT * getBlock() const
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
Definition: MachineInstr.h:238
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
Representation of each machine instruction.
Definition: MachineInstr.h:51
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
#define I(x, y, z)
Definition: MD5.cpp:54
#define N
virtual const TargetInstrInfo * getInstrInfo() const
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
FunctionPass * createAArch64CleanupLocalDynamicTLSPass()