LLVM 24.0.0git
PPCTLSDynamicCall.cpp
Go to the documentation of this file.
1//===---------- PPCTLSDynamicCall.cpp - TLS Dynamic Call Fixup ------------===//
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// This pass expands ADDItls{ld,gd}LADDR[32] machine instructions into
10// separate ADDItls[gd]L[32] and GETtlsADDR[32] instructions, both of
11// which define GPR3. A copy is added from GPR3 to the target virtual
12// register of the original instruction. The GETtlsADDR[32] is really
13// a call instruction, so its target register is constrained to be GPR3.
14// This is not true of ADDItls[gd]L[32], but there is a legacy linker
15// optimization bug that requires the target register of the addi of
16// a local- or general-dynamic TLS access sequence to be GPR3.
17//
18// This is done in a late pass so that TLS variable accesses can be
19// fully commoned by MachineCSE.
20//
21//===----------------------------------------------------------------------===//
22
23#include "PPC.h"
24#include "PPCInstrInfo.h"
25#include "PPCTargetMachine.h"
32#include "llvm/Support/Debug.h"
34
35using namespace llvm;
36
37#define DEBUG_TYPE "ppc-tls-dynamic-call"
38
39namespace {
40 struct PPCTLSDynamicCall : public MachineFunctionPass {
41 static char ID;
42 PPCTLSDynamicCall() : MachineFunctionPass(ID) {}
43
44 const PPCInstrInfo *TII;
45
46protected:
47 bool processBlock(MachineBasicBlock &MBB) {
48 bool Changed = false;
49 bool NeedFence = true;
50 const PPCSubtarget &Subtarget =
51 MBB.getParent()->getSubtarget<PPCSubtarget>();
52 bool Is64Bit = Subtarget.isPPC64();
53 bool IsAIX = Subtarget.isAIXABI();
54 bool IsLargeModel =
56 bool IsPCREL = false;
57 MachineFunction *MF = MBB.getParent();
59
60 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
61 I != IE;) {
62 MachineInstr &MI = *I;
63 IsPCREL = isPCREL(MI);
64 // There are a number of slight differences in code generation
65 // when we call .__get_tpointer (32-bit AIX TLS).
66 bool IsTLSTPRelMI = MI.getOpcode() == PPC::GETtlsTpointer32AIX;
67 bool IsTLSLDAIXMI = (MI.getOpcode() == PPC::TLSLDAIX8 ||
68 MI.getOpcode() == PPC::TLSLDAIX);
69
70 if (MI.getOpcode() != PPC::ADDItlsgdLADDR &&
71 MI.getOpcode() != PPC::ADDItlsldLADDR &&
72 MI.getOpcode() != PPC::ADDItlsgdLADDR32 &&
73 MI.getOpcode() != PPC::ADDItlsldLADDR32 &&
74 MI.getOpcode() != PPC::TLSGDAIX &&
75 MI.getOpcode() != PPC::TLSGDAIX8 && !IsTLSTPRelMI && !IsPCREL &&
76 !IsTLSLDAIXMI) {
77 // Although we create ADJCALLSTACKDOWN and ADJCALLSTACKUP
78 // as scheduling fences, we skip creating fences if we already
79 // have existing ADJCALLSTACKDOWN/UP to avoid nesting,
80 // which causes verification error with -verify-machineinstrs.
81 if (MI.getOpcode() == PPC::ADJCALLSTACKDOWN)
82 NeedFence = false;
83 else if (MI.getOpcode() == PPC::ADJCALLSTACKUP)
84 NeedFence = true;
85
86 ++I;
87 continue;
88 }
89
90 LLVM_DEBUG(dbgs() << "TLS Dynamic Call Fixup:\n " << MI);
91
92 Register OutReg = MI.getOperand(0).getReg();
93 Register InReg = PPC::NoRegister;
94 Register GPR3 = Is64Bit ? PPC::X3 : PPC::R3;
95 Register GPR4 = Is64Bit ? PPC::X4 : PPC::R4;
96 if (!IsPCREL && !IsTLSTPRelMI)
97 InReg = MI.getOperand(1).getReg();
98 DebugLoc DL = MI.getDebugLoc();
99
100 unsigned Opc1, Opc2;
101 switch (MI.getOpcode()) {
102 default:
103 llvm_unreachable("Opcode inconsistency error");
104 case PPC::ADDItlsgdLADDR:
105 Opc1 = PPC::ADDItlsgdL;
106 Opc2 = PPC::GETtlsADDR;
107 break;
108 case PPC::ADDItlsldLADDR:
109 Opc1 = PPC::ADDItlsldL;
110 Opc2 = PPC::GETtlsldADDR;
111 break;
112 case PPC::ADDItlsgdLADDR32:
113 Opc1 = PPC::ADDItlsgdL32;
114 Opc2 = PPC::GETtlsADDR32;
115 break;
116 case PPC::ADDItlsldLADDR32:
117 Opc1 = PPC::ADDItlsldL32;
118 Opc2 = PPC::GETtlsldADDR32;
119 break;
120 case PPC::TLSLDAIX:
121 // TLSLDAIX is expanded to one copy and GET_TLS_MOD, so we only set
122 // Opc2 here.
123 Opc2 = PPC::GETtlsMOD32AIX;
124 break;
125 case PPC::TLSLDAIX8:
126 // TLSLDAIX8 is expanded to one copy and GET_TLS_MOD, so we only set
127 // Opc2 here.
128 Opc2 = PPC::GETtlsMOD64AIX;
129 break;
130 case PPC::TLSGDAIX8:
131 // TLSGDAIX8 is expanded to two copies and GET_TLS_ADDR, so we only
132 // set Opc2 here.
133 Opc2 = PPC::GETtlsADDR64AIX;
134 break;
135 case PPC::TLSGDAIX:
136 // TLSGDAIX is expanded to two copies and GET_TLS_ADDR, so we only
137 // set Opc2 here.
138 Opc2 = PPC::GETtlsADDR32AIX;
139 break;
140 case PPC::GETtlsTpointer32AIX:
141 // GETtlsTpointer32AIX is expanded to a call to GET_TPOINTER on AIX
142 // 32-bit mode within PPCAsmPrinter. This instruction does not need
143 // to change, so Opc2 is set to the same instruction opcode.
144 Opc2 = PPC::GETtlsTpointer32AIX;
145 break;
146 case PPC::PADDI8pc:
147 assert(IsPCREL && "Expecting General/Local Dynamic PCRel");
148 Opc1 = PPC::PADDI8pc;
149 Opc2 = MI.getOperand(2).getTargetFlags() ==
151 ? PPC::GETtlsADDRPCREL
152 : PPC::GETtlsldADDRPCREL;
153 }
154
155 // We create ADJCALLSTACKUP and ADJCALLSTACKDOWN around _tls_get_addr
156 // as scheduling fence to avoid it is scheduled before
157 // mflr in the prologue and the address in LR is clobbered (PR25839).
158 // We don't really need to save data to the stack - the clobbered
159 // registers are already saved when the SDNode (e.g. PPCaddiTlsgdLAddr)
160 // gets translated to the pseudo instruction (e.g. ADDItlsgdLADDR).
161 if (NeedFence) {
162 MBB.getParent()->getFrameInfo().setAdjustsStack(true);
163 BuildMI(MBB, I, DL, TII->get(PPC::ADJCALLSTACKDOWN)).addImm(0)
164 .addImm(0);
165 }
166
167 if (IsAIX) {
168 if (IsTLSLDAIXMI) {
169 // The relative order between the node that loads the variable
170 // offset from the TOC, and the .__tls_get_mod node is being tuned
171 // here. It is better to put the variable offset TOC load after the
172 // call, since this node can use clobbers r4/r5.
173 // Search for the pattern of the two nodes that load from the TOC
174 // (either for the variable offset or for the module handle), and
175 // then move the variable offset TOC load right before the node that
176 // uses the OutReg of the .__tls_get_mod node.
177 unsigned LDTocOp =
178 Is64Bit ? (IsLargeModel ? PPC::LDtocL : PPC::LDtoc)
179 : (IsLargeModel ? PPC::LWZtocL : PPC::LWZtoc);
180 if (!RegInfo.use_empty(OutReg)) {
181 std::set<MachineInstr *> Uses;
182 // Collect all instructions that use the OutReg.
183 for (MachineOperand &MO : RegInfo.use_operands(OutReg))
184 Uses.insert(MO.getParent());
185 // Find the first user (e.g.: lwax/stfdx) of the OutReg within the
186 // current BB.
187 MachineBasicBlock::iterator UseIter = MBB.begin();
188 for (MachineBasicBlock::iterator IE = MBB.end(); UseIter != IE;
189 ++UseIter)
190 if (Uses.count(&*UseIter))
191 break;
192
193 // Additional handling is required when UserIter (the first user
194 // of OutReg) is pointing to a valid node that loads from the TOC.
195 // Check the pattern and do the movement if the pattern matches.
196 if (UseIter != MBB.end()) {
197 // Collect all associated nodes that load from the TOC. Use
198 // hasOneDef() to guard against unexpected scenarios.
199 std::set<MachineInstr *> LoadFromTocs;
200 for (MachineOperand &MO : UseIter->operands())
201 if (MO.isReg() && MO.isUse()) {
202 Register MOReg = MO.getReg();
203 if (RegInfo.hasOneDef(MOReg)) {
204 MachineInstr *Temp =
205 RegInfo.getOneDef(MOReg)->getParent();
206 // For the current TLSLDAIX node, get the corresponding
207 // node that loads from the TOC for the InReg. Otherwise,
208 // Temp probably pointed to the variable offset TOC load
209 // we would like to move.
210 if (Temp == &MI && RegInfo.hasOneDef(InReg))
211 Temp = RegInfo.getOneDef(InReg)->getParent();
212 if (Temp->getOpcode() == LDTocOp)
213 LoadFromTocs.insert(Temp);
214 } else {
215 // FIXME: analyze this scenario if there is one.
216 LoadFromTocs.clear();
217 break;
218 }
219 }
220
221 // Check the two nodes that loaded from the TOC: one should be
222 // "_$TLSML", and the other will be moved before the node that
223 // uses the OutReg of the .__tls_get_mod node.
224 if (LoadFromTocs.size() == 2) {
225 MachineBasicBlock::iterator TLSMLIter = MBB.end();
226 MachineBasicBlock::iterator OffsetIter = MBB.end();
227 // Make sure the two nodes that loaded from the TOC are within
228 // the current BB, and that one of them is from the "_$TLSML"
229 // pseudo symbol, while the other is from the variable.
230 for (MachineBasicBlock::iterator I = MBB.begin(),
231 IE = MBB.end();
232 I != IE; ++I)
233 if (LoadFromTocs.count(&*I)) {
234 MachineOperand MO = I->getOperand(1);
235 if (MO.isGlobal() && MO.getGlobal()->hasName() &&
236 MO.getGlobal()->getName() == "_$TLSML")
237 TLSMLIter = I;
238 else
239 OffsetIter = I;
240 }
241 // Perform the movement when the desired scenario has been
242 // identified, which should be when both of the iterators are
243 // valid.
244 if (TLSMLIter != MBB.end() && OffsetIter != MBB.end())
245 OffsetIter->moveBefore(&*UseIter);
246 }
247 }
248 }
249 // The module-handle is copied into r3. The copy is followed by
250 // GETtlsMOD32AIX/GETtlsMOD64AIX.
251 BuildMI(MBB, I, DL, TII->get(TargetOpcode::COPY), GPR3)
252 .addReg(InReg);
253 // The call to .__tls_get_mod.
254 BuildMI(MBB, I, DL, TII->get(Opc2), GPR3).addReg(GPR3);
255 } else if (!IsTLSTPRelMI) {
256 // The variable offset and region handle (for TLSGD) are copied in
257 // r4 and r3. The copies are followed by
258 // GETtlsADDR32AIX/GETtlsADDR64AIX.
259 BuildMI(MBB, I, DL, TII->get(TargetOpcode::COPY), GPR4)
260 .addReg(MI.getOperand(1).getReg());
261 BuildMI(MBB, I, DL, TII->get(TargetOpcode::COPY), GPR3)
262 .addReg(MI.getOperand(2).getReg());
263 BuildMI(MBB, I, DL, TII->get(Opc2), GPR3).addReg(GPR3).addReg(GPR4);
264 } else
265 // The opcode of GETtlsTpointer32AIX does not change, because later
266 // this instruction will be expanded into a call to .__get_tpointer,
267 // which will return the thread pointer into r3.
268 BuildMI(MBB, I, DL, TII->get(Opc2), GPR3);
269 } else {
270 MachineInstr *Addi;
271 if (IsPCREL) {
272 Addi = BuildMI(MBB, I, DL, TII->get(Opc1), GPR3).addImm(0);
273 } else {
274 // Expand into two ops built prior to the existing instruction.
275 assert(InReg != PPC::NoRegister && "Operand must be a register");
276 Addi = BuildMI(MBB, I, DL, TII->get(Opc1), GPR3).addReg(InReg);
277 }
278
279 Addi->addOperand(MI.getOperand(2));
280
282 (BuildMI(MBB, I, DL, TII->get(Opc2), GPR3).addReg(GPR3));
283 if (IsPCREL)
284 Call->addOperand(MI.getOperand(2));
285 else
286 Call->addOperand(MI.getOperand(3));
287 }
288 if (NeedFence)
289 BuildMI(MBB, I, DL, TII->get(PPC::ADJCALLSTACKUP)).addImm(0).addImm(0);
290
291 BuildMI(MBB, I, DL, TII->get(TargetOpcode::COPY), OutReg)
292 .addReg(GPR3);
293
294 // Move past the original instruction and remove it.
295 ++I;
296 MI.removeFromParent();
297
298 Changed = true;
299 }
300
301 return Changed;
302 }
303
304public:
305 bool isPCREL(const MachineInstr &MI) {
306 return (MI.getOpcode() == PPC::PADDI8pc) &&
307 (MI.getOperand(2).getTargetFlags() ==
309 MI.getOperand(2).getTargetFlags() ==
311 }
312
313 bool runOnMachineFunction(MachineFunction &MF) override {
314 TII = MF.getSubtarget<PPCSubtarget>().getInstrInfo();
315
316 bool Changed = false;
317
319 if (processBlock(B))
320 Changed = true;
321
322 return Changed;
323 }
324
325 void getAnalysisUsage(AnalysisUsage &AU) const override {
330 }
331 };
332}
333
335 "PowerPC TLS Dynamic Call Fixup", false, false)
339 "PowerPC TLS Dynamic Call Fixup", false, false)
340
341char PPCTLSDynamicCall::ID = 0;
343llvm::createPPCTLSDynamicCallPass() { return new PPCTLSDynamicCall(); }
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define DEBUG_TYPE
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition MD5.cpp:57
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
Remove Loads Into Fake Uses
#define LLVM_DEBUG(...)
Definition Debug.h:119
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
A debug info location.
Definition DebugLoc.h:126
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
MachineInstrBundleIterator< MachineInstr > iterator
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.
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.
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
LLVM_ABI void addOperand(MachineFunction &MF, const MachineOperand &Op)
Add the specified operand to the instruction.
MachineOperand class - Representation of each machine instruction operand.
const GlobalValue * getGlobal() const
bool isGlobal() const
isGlobal - Tests if this is a MO_GlobalAddress operand.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
bool isAIXABI() const
const PPCTargetMachine & getTargetMachine() const
Wrapper class representing virtual and physical registers.
Definition Register.h:20
CodeModel::Model getCodeModel() const
Returns the code model.
bool hasName() const
Definition Value.h:261
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:319
CallInst * Call
Changed
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
@ MO_GOT_TLSLD_PCREL_FLAG
MO_GOT_TLSLD_PCREL_FLAG - A combintaion of flags, if these bits are set they should produce the reloc...
Definition PPC.h:168
@ MO_GOT_TLSGD_PCREL_FLAG
MO_GOT_TLSGD_PCREL_FLAG - A combintaion of flags, if these bits are set they should produce the reloc...
Definition PPC.h:162
This is an optimization pass for GlobalISel generic memory operations.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:633
FunctionPass * createPPCTLSDynamicCallPass()
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209