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
168 if (IsAIX) {
169 if (IsTLSLDAIXMI) {
170 // The relative order between the node that loads the variable
171 // offset from the TOC, and the .__tls_get_mod node is being tuned
172 // here. It is better to put the variable offset TOC load after the
173 // call, since this node can use clobbers r4/r5.
174 // Search for the pattern of the two nodes that load from the TOC
175 // (either for the variable offset or for the module handle), and
176 // then move the variable offset TOC load right before the node that
177 // uses the OutReg of the .__tls_get_mod node.
178 unsigned LDTocOp =
179 Is64Bit ? (IsLargeModel ? PPC::LDtocL : PPC::LDtoc)
180 : (IsLargeModel ? PPC::LWZtocL : PPC::LWZtoc);
181 if (!RegInfo.use_empty(OutReg)) {
182 std::set<MachineInstr *> Uses;
183 // Collect all instructions that use the OutReg.
184 for (MachineInstr &UseMI : RegInfo.use_instructions(OutReg))
185 Uses.insert(&UseMI);
186 // Find the first user (e.g.: lwax/stfdx) of the OutReg within the
187 // current BB.
188 MachineBasicBlock::iterator UseIter = MBB.begin();
189 for (MachineBasicBlock::iterator IE = MBB.end(); UseIter != IE;
190 ++UseIter)
191 if (Uses.count(&*UseIter))
192 break;
193
194 // Additional handling is required when UserIter (the first user
195 // of OutReg) is pointing to a valid node that loads from the TOC.
196 // Check the pattern and do the movement if the pattern matches.
197 if (UseIter != MBB.end()) {
198 // Collect all associated nodes that load from the TOC. Use
199 // hasOneDef() to guard against unexpected scenarios.
200 std::set<MachineInstr *> LoadFromTocs;
201 for (MachineOperand &MO : UseIter->operands())
202 if (MO.isReg() && MO.isUse()) {
203 Register MOReg = MO.getReg();
204 if (RegInfo.hasOneDef(MOReg)) {
205 MachineInstr *Temp =
206 RegInfo.getOneDef(MOReg)->getParent();
207 // For the current TLSLDAIX node, get the corresponding
208 // node that loads from the TOC for the InReg. Otherwise,
209 // Temp probably pointed to the variable offset TOC load
210 // we would like to move.
211 if (Temp == &MI && RegInfo.hasOneDef(InReg))
212 Temp = RegInfo.getOneDef(InReg)->getParent();
213 if (Temp->getOpcode() == LDTocOp)
214 LoadFromTocs.insert(Temp);
215 } else {
216 // FIXME: analyze this scenario if there is one.
217 LoadFromTocs.clear();
218 break;
219 }
220 }
221
222 // Check the two nodes that loaded from the TOC: one should be
223 // "_$TLSML", and the other will be moved before the node that
224 // uses the OutReg of the .__tls_get_mod node.
225 if (LoadFromTocs.size() == 2) {
226 MachineBasicBlock::iterator TLSMLIter = MBB.end();
227 MachineBasicBlock::iterator OffsetIter = MBB.end();
228 // Make sure the two nodes that loaded from the TOC are within
229 // the current BB, and that one of them is from the "_$TLSML"
230 // pseudo symbol, while the other is from the variable.
231 for (MachineBasicBlock::iterator I = MBB.begin(),
232 IE = MBB.end();
233 I != IE; ++I)
234 if (LoadFromTocs.count(&*I)) {
235 MachineOperand MO = I->getOperand(1);
236 if (MO.isGlobal() && MO.getGlobal()->hasName() &&
237 MO.getGlobal()->getName() == "_$TLSML")
238 TLSMLIter = I;
239 else
240 OffsetIter = I;
241 }
242 // Perform the movement when the desired scenario has been
243 // identified, which should be when both of the iterators are
244 // valid.
245 if (TLSMLIter != MBB.end() && OffsetIter != MBB.end())
246 OffsetIter->moveBefore(&*UseIter);
247 }
248 }
249 }
250 // The module-handle is copied into r3. The copy is followed by
251 // GETtlsMOD32AIX/GETtlsMOD64AIX.
252 BuildMI(MBB, I, DL, TII->get(TargetOpcode::COPY), GPR3)
253 .addReg(InReg);
254 // The call to .__tls_get_mod.
255 Call = BuildMI(MBB, I, DL, TII->get(Opc2), GPR3).addReg(GPR3);
256 } else if (!IsTLSTPRelMI) {
257 // The variable offset and region handle (for TLSGD) are copied in
258 // r4 and r3. The copies are followed by
259 // GETtlsADDR32AIX/GETtlsADDR64AIX.
260 BuildMI(MBB, I, DL, TII->get(TargetOpcode::COPY), GPR4)
261 .addReg(MI.getOperand(1).getReg());
262 BuildMI(MBB, I, DL, TII->get(TargetOpcode::COPY), GPR3)
263 .addReg(MI.getOperand(2).getReg());
264 Call = BuildMI(MBB, I, DL, TII->get(Opc2), GPR3)
265 .addReg(GPR3)
266 .addReg(GPR4);
267 } else
268 // The opcode of GETtlsTpointer32AIX does not change, because later
269 // this instruction will be expanded into a call to .__get_tpointer,
270 // which will return the thread pointer into r3.
271 Call = BuildMI(MBB, I, DL, TII->get(Opc2), GPR3);
272 } else {
273 MachineInstr *Addi;
274 if (IsPCREL) {
275 Addi = BuildMI(MBB, I, DL, TII->get(Opc1), GPR3).addImm(0);
276 } else {
277 // Expand into two ops built prior to the existing instruction.
278 assert(InReg != PPC::NoRegister && "Operand must be a register");
279 Addi = BuildMI(MBB, I, DL, TII->get(Opc1), GPR3).addReg(InReg);
280 }
281
282 Addi->addOperand(MI.getOperand(2));
283
284 Call = BuildMI(MBB, I, DL, TII->get(Opc2), GPR3).addReg(GPR3);
285 if (IsPCREL)
286 Call->addOperand(MI.getOperand(2));
287 else
288 Call->addOperand(MI.getOperand(3));
289 }
290
291 Call->setPhysRegsDeadExcept(GPR3, *Subtarget.getRegisterInfo());
292
293 if (NeedFence)
294 BuildMI(MBB, I, DL, TII->get(PPC::ADJCALLSTACKUP)).addImm(0).addImm(0);
295
296 BuildMI(MBB, I, DL, TII->get(TargetOpcode::COPY), OutReg)
297 .addReg(GPR3);
298
299 // Move past the original instruction and remove it.
300 ++I;
301 MI.removeFromParent();
302
303 Changed = true;
304 }
305
306 return Changed;
307 }
308
309public:
310 bool isPCREL(const MachineInstr &MI) {
311 return (MI.getOpcode() == PPC::PADDI8pc) &&
312 (MI.getOperand(2).getTargetFlags() ==
314 MI.getOperand(2).getTargetFlags() ==
316 }
317
318 bool runOnMachineFunction(MachineFunction &MF) override {
319 TII = MF.getSubtarget<PPCSubtarget>().getInstrInfo();
320
321 bool Changed = false;
322
324 if (processBlock(B))
325 Changed = true;
326
327 return Changed;
328 }
329
330 void getAnalysisUsage(AnalysisUsage &AU) const override {
335 }
336 };
337}
338
340 "PowerPC TLS Dynamic Call Fixup", false, false)
344 "PowerPC TLS Dynamic Call Fixup", false, false)
345
346char PPCTLSDynamicCall::ID = 0;
348llvm::createPPCTLSDynamicCallPass() { return new PPCTLSDynamicCall(); }
MachineInstrBuilder & UseMI
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
const PPCRegisterInfo * getRegisterInfo() const override
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:263
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.
@ 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:177
@ 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:171
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:649
FunctionPass * createPPCTLSDynamicCallPass()
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209