LLVM 20.0.0git
AArch64CleanupLocalDynamicTLSPass.cpp
Go to the documentation of this file.
1//===-- AArch64CleanupLocalDynamicTLSPass.cpp ---------------------*- C++ -*-=//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Local-dynamic access to thread-local variables proceeds in three stages.
10//
11// 1. The offset of this Module's thread-local area from TPIDR_EL0 is calculated
12// in much the same way as a general-dynamic TLS-descriptor access against
13// the special symbol _TLS_MODULE_BASE.
14// 2. The variable's offset from _TLS_MODULE_BASE_ is calculated using
15// instructions with "dtprel" modifiers.
16// 3. These two are added, together with TPIDR_EL0, to obtain the variable's
17// true address.
18//
19// This is only better than general-dynamic access to the variable if two or
20// more of the first stage TLS-descriptor calculations can be combined. This
21// pass looks through a function and performs such combinations.
22//
23//===----------------------------------------------------------------------===//
24#include "AArch64.h"
25#include "AArch64InstrInfo.h"
32using namespace llvm;
33
34#define TLSCLEANUP_PASS_NAME "AArch64 Local Dynamic TLS Access Clean-up"
35
36namespace {
37struct LDTLSCleanup : public MachineFunctionPass {
38 static char ID;
39 LDTLSCleanup() : MachineFunctionPass(ID) {
41 }
42
43 bool runOnMachineFunction(MachineFunction &MF) override {
44 if (skipFunction(MF.getFunction()))
45 return false;
46
48 if (AFI->getNumLocalDynamicTLSAccesses() < 2) {
49 // No point folding accesses if there isn't at least two.
50 return false;
51 }
52
54 &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
55 return VisitNode(DT->getRootNode(), 0);
56 }
57
58 // Visit the dominator subtree rooted at Node in pre-order.
59 // If TLSBaseAddrReg is non-null, then use that to replace any
60 // TLS_base_addr instructions. Otherwise, create the register
61 // when the first such instruction is seen, and then use it
62 // as we encounter more instructions.
63 bool VisitNode(MachineDomTreeNode *Node, unsigned TLSBaseAddrReg) {
64 MachineBasicBlock *BB = Node->getBlock();
65 bool Changed = false;
66
67 // Traverse the current block.
68 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;
69 ++I) {
70 switch (I->getOpcode()) {
71 case AArch64::TLSDESC_CALLSEQ:
72 // Make sure it's a local dynamic access.
73 if (!I->getOperand(0).isSymbol() ||
74 strcmp(I->getOperand(0).getSymbolName(), "_TLS_MODULE_BASE_"))
75 break;
76
77 if (TLSBaseAddrReg)
78 I = replaceTLSBaseAddrCall(*I, TLSBaseAddrReg);
79 else
80 I = setRegister(*I, &TLSBaseAddrReg);
81 Changed = true;
82 break;
83 default:
84 break;
85 }
86 }
87
88 // Visit the children of this block in the dominator tree.
89 for (MachineDomTreeNode *N : *Node) {
90 Changed |= VisitNode(N, TLSBaseAddrReg);
91 }
92
93 return Changed;
94 }
95
96 // Replace the TLS_base_addr instruction I with a copy from
97 // TLSBaseAddrReg, returning the new instruction.
98 MachineInstr *replaceTLSBaseAddrCall(MachineInstr &I,
99 unsigned TLSBaseAddrReg) {
100 MachineFunction *MF = I.getParent()->getParent();
102
103 // Insert a Copy from TLSBaseAddrReg to x0, which is where the rest of the
104 // code sequence assumes the address will be.
105 MachineInstr *Copy = BuildMI(*I.getParent(), I, I.getDebugLoc(),
106 TII->get(TargetOpcode::COPY), AArch64::X0)
107 .addReg(TLSBaseAddrReg);
108
109 // Update the call site info.
110 if (I.shouldUpdateCallSiteInfo())
111 I.getMF()->eraseCallSiteInfo(&I);
112
113 // Erase the TLS_base_addr instruction.
114 I.eraseFromParent();
115
116 return Copy;
117 }
118
119 // Create a virtual register in *TLSBaseAddrReg, and populate it by
120 // inserting a copy instruction after I. Returns the new instruction.
121 MachineInstr *setRegister(MachineInstr &I, unsigned *TLSBaseAddrReg) {
122 MachineFunction *MF = I.getParent()->getParent();
124
125 // Create a virtual register for the TLS base address.
127 *TLSBaseAddrReg = RegInfo.createVirtualRegister(&AArch64::GPR64RegClass);
128
129 // Insert a copy from X0 to TLSBaseAddrReg for later.
130 MachineInstr *Copy =
131 BuildMI(*I.getParent(), ++I.getIterator(), I.getDebugLoc(),
132 TII->get(TargetOpcode::COPY), *TLSBaseAddrReg)
133 .addReg(AArch64::X0);
134
135 return Copy;
136 }
137
138 StringRef getPassName() const override { return TLSCLEANUP_PASS_NAME; }
139
140 void getAnalysisUsage(AnalysisUsage &AU) const override {
141 AU.setPreservesCFG();
144 }
145};
146}
147
148INITIALIZE_PASS(LDTLSCleanup, "aarch64-local-dynamic-tls-cleanup",
149 TLSCLEANUP_PASS_NAME, false, false)
150
151char LDTLSCleanup::ID = 0;
153 return new LDTLSCleanup();
154}
#define TLSCLEANUP_PASS_NAME
const HexagonInstrInfo * TII
#define I(x, y, z)
Definition: MD5.cpp:58
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
AArch64FunctionInfo - This class is derived from MachineFunctionInfo and contains private AArch64-spe...
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:256
Base class for the actual dominator tree node.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:310
bool skipFunction(const Function &F) const
Optional passes call this function to check whether the pass should be skipped.
Definition: Pass.cpp:178
Analysis pass which computes a MachineDominatorTree.
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
MachineDomTreeNode * getRootNode() const
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
Representation of each machine instruction.
Definition: MachineInstr.h:69
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
TargetInstrInfo - Interface to description of machine instruction set.
virtual const TargetInstrInfo * getInstrInfo() const
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
void initializeLDTLSCleanupPass(PassRegistry &)
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
FunctionPass * createAArch64CleanupLocalDynamicTLSPass()
#define N