xref: /freebsd/contrib/llvm-project/llvm/lib/Target/PowerPC/PPCTLSDynamicCall.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===---------- PPCTLSDynamicCall.cpp - TLS Dynamic Call Fixup ------------===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This pass expands ADDItls{ld,gd}LADDR[32] machine instructions into
100b57cec5SDimitry Andric // separate ADDItls[gd]L[32] and GETtlsADDR[32] instructions, both of
110b57cec5SDimitry Andric // which define GPR3.  A copy is added from GPR3 to the target virtual
120b57cec5SDimitry Andric // register of the original instruction.  The GETtlsADDR[32] is really
130b57cec5SDimitry Andric // a call instruction, so its target register is constrained to be GPR3.
140b57cec5SDimitry Andric // This is not true of ADDItls[gd]L[32], but there is a legacy linker
150b57cec5SDimitry Andric // optimization bug that requires the target register of the addi of
160b57cec5SDimitry Andric // a local- or general-dynamic TLS access sequence to be GPR3.
170b57cec5SDimitry Andric //
180b57cec5SDimitry Andric // This is done in a late pass so that TLS variable accesses can be
190b57cec5SDimitry Andric // fully commoned by MachineCSE.
200b57cec5SDimitry Andric //
210b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
220b57cec5SDimitry Andric 
230b57cec5SDimitry Andric #include "PPC.h"
240b57cec5SDimitry Andric #include "PPCInstrBuilder.h"
250b57cec5SDimitry Andric #include "PPCInstrInfo.h"
260b57cec5SDimitry Andric #include "PPCTargetMachine.h"
270b57cec5SDimitry Andric #include "llvm/CodeGen/LiveIntervals.h"
28*0fca6ea1SDimitry Andric #include "llvm/CodeGen/MachineFrameInfo.h"
290b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
300b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
31480093f4SDimitry Andric #include "llvm/InitializePasses.h"
320b57cec5SDimitry Andric #include "llvm/Support/Debug.h"
330b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h"
340b57cec5SDimitry Andric 
350b57cec5SDimitry Andric using namespace llvm;
360b57cec5SDimitry Andric 
370b57cec5SDimitry Andric #define DEBUG_TYPE "ppc-tls-dynamic-call"
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric namespace {
400b57cec5SDimitry Andric   struct PPCTLSDynamicCall : public MachineFunctionPass {
410b57cec5SDimitry Andric     static char ID;
PPCTLSDynamicCall__anonbafe3dec0111::PPCTLSDynamicCall420b57cec5SDimitry Andric     PPCTLSDynamicCall() : MachineFunctionPass(ID) {
430b57cec5SDimitry Andric       initializePPCTLSDynamicCallPass(*PassRegistry::getPassRegistry());
440b57cec5SDimitry Andric     }
450b57cec5SDimitry Andric 
460b57cec5SDimitry Andric     const PPCInstrInfo *TII;
470b57cec5SDimitry Andric 
480b57cec5SDimitry Andric protected:
processBlock__anonbafe3dec0111::PPCTLSDynamicCall490b57cec5SDimitry Andric     bool processBlock(MachineBasicBlock &MBB) {
500b57cec5SDimitry Andric       bool Changed = false;
510b57cec5SDimitry Andric       bool NeedFence = true;
52*0fca6ea1SDimitry Andric       const PPCSubtarget &Subtarget =
53*0fca6ea1SDimitry Andric           MBB.getParent()->getSubtarget<PPCSubtarget>();
54*0fca6ea1SDimitry Andric       bool Is64Bit = Subtarget.isPPC64();
55*0fca6ea1SDimitry Andric       bool IsAIX = Subtarget.isAIXABI();
56*0fca6ea1SDimitry Andric       bool IsLargeModel =
57*0fca6ea1SDimitry Andric           Subtarget.getTargetMachine().getCodeModel() == CodeModel::Large;
58e8d8bef9SDimitry Andric       bool IsPCREL = false;
59*0fca6ea1SDimitry Andric       MachineFunction *MF = MBB.getParent();
60*0fca6ea1SDimitry Andric       MachineRegisterInfo &RegInfo = MF->getRegInfo();
610b57cec5SDimitry Andric 
620b57cec5SDimitry Andric       for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
630b57cec5SDimitry Andric            I != IE;) {
640b57cec5SDimitry Andric         MachineInstr &MI = *I;
65e8d8bef9SDimitry Andric         IsPCREL = isPCREL(MI);
6606c3fb27SDimitry Andric         // There are a number of slight differences in code generation
6706c3fb27SDimitry Andric         // when we call .__get_tpointer (32-bit AIX TLS).
6806c3fb27SDimitry Andric         bool IsTLSTPRelMI = MI.getOpcode() == PPC::GETtlsTpointer32AIX;
69*0fca6ea1SDimitry Andric         bool IsTLSLDAIXMI = (MI.getOpcode() == PPC::TLSLDAIX8 ||
70*0fca6ea1SDimitry Andric                              MI.getOpcode() == PPC::TLSLDAIX);
710b57cec5SDimitry Andric 
720b57cec5SDimitry Andric         if (MI.getOpcode() != PPC::ADDItlsgdLADDR &&
730b57cec5SDimitry Andric             MI.getOpcode() != PPC::ADDItlsldLADDR &&
740b57cec5SDimitry Andric             MI.getOpcode() != PPC::ADDItlsgdLADDR32 &&
75fe6060f1SDimitry Andric             MI.getOpcode() != PPC::ADDItlsldLADDR32 &&
76fe6060f1SDimitry Andric             MI.getOpcode() != PPC::TLSGDAIX &&
77*0fca6ea1SDimitry Andric             MI.getOpcode() != PPC::TLSGDAIX8 && !IsTLSTPRelMI && !IsPCREL &&
78*0fca6ea1SDimitry Andric             !IsTLSLDAIXMI) {
790b57cec5SDimitry Andric           // Although we create ADJCALLSTACKDOWN and ADJCALLSTACKUP
800b57cec5SDimitry Andric           // as scheduling fences, we skip creating fences if we already
810b57cec5SDimitry Andric           // have existing ADJCALLSTACKDOWN/UP to avoid nesting,
820b57cec5SDimitry Andric           // which causes verification error with -verify-machineinstrs.
830b57cec5SDimitry Andric           if (MI.getOpcode() == PPC::ADJCALLSTACKDOWN)
840b57cec5SDimitry Andric             NeedFence = false;
850b57cec5SDimitry Andric           else if (MI.getOpcode() == PPC::ADJCALLSTACKUP)
860b57cec5SDimitry Andric             NeedFence = true;
870b57cec5SDimitry Andric 
880b57cec5SDimitry Andric           ++I;
890b57cec5SDimitry Andric           continue;
900b57cec5SDimitry Andric         }
910b57cec5SDimitry Andric 
920b57cec5SDimitry Andric         LLVM_DEBUG(dbgs() << "TLS Dynamic Call Fixup:\n    " << MI);
930b57cec5SDimitry Andric 
948bcb0991SDimitry Andric         Register OutReg = MI.getOperand(0).getReg();
95e8d8bef9SDimitry Andric         Register InReg = PPC::NoRegister;
965ffd83dbSDimitry Andric         Register GPR3 = Is64Bit ? PPC::X3 : PPC::R3;
97fe6060f1SDimitry Andric         Register GPR4 = Is64Bit ? PPC::X4 : PPC::R4;
9806c3fb27SDimitry Andric         if (!IsPCREL && !IsTLSTPRelMI)
99e8d8bef9SDimitry Andric           InReg = MI.getOperand(1).getReg();
100e8d8bef9SDimitry Andric         DebugLoc DL = MI.getDebugLoc();
1010b57cec5SDimitry Andric 
102e8d8bef9SDimitry Andric         unsigned Opc1, Opc2;
1030b57cec5SDimitry Andric         switch (MI.getOpcode()) {
1040b57cec5SDimitry Andric         default:
1050b57cec5SDimitry Andric           llvm_unreachable("Opcode inconsistency error");
1060b57cec5SDimitry Andric         case PPC::ADDItlsgdLADDR:
1070b57cec5SDimitry Andric           Opc1 = PPC::ADDItlsgdL;
1080b57cec5SDimitry Andric           Opc2 = PPC::GETtlsADDR;
1090b57cec5SDimitry Andric           break;
1100b57cec5SDimitry Andric         case PPC::ADDItlsldLADDR:
1110b57cec5SDimitry Andric           Opc1 = PPC::ADDItlsldL;
1120b57cec5SDimitry Andric           Opc2 = PPC::GETtlsldADDR;
1130b57cec5SDimitry Andric           break;
1140b57cec5SDimitry Andric         case PPC::ADDItlsgdLADDR32:
1150b57cec5SDimitry Andric           Opc1 = PPC::ADDItlsgdL32;
1160b57cec5SDimitry Andric           Opc2 = PPC::GETtlsADDR32;
1170b57cec5SDimitry Andric           break;
1180b57cec5SDimitry Andric         case PPC::ADDItlsldLADDR32:
1190b57cec5SDimitry Andric           Opc1 = PPC::ADDItlsldL32;
1200b57cec5SDimitry Andric           Opc2 = PPC::GETtlsldADDR32;
1210b57cec5SDimitry Andric           break;
122*0fca6ea1SDimitry Andric         case PPC::TLSLDAIX:
123*0fca6ea1SDimitry Andric           // TLSLDAIX is expanded to one copy and GET_TLS_MOD, so we only set
124*0fca6ea1SDimitry Andric           // Opc2 here.
125*0fca6ea1SDimitry Andric           Opc2 = PPC::GETtlsMOD32AIX;
126*0fca6ea1SDimitry Andric           break;
127*0fca6ea1SDimitry Andric         case PPC::TLSLDAIX8:
128*0fca6ea1SDimitry Andric           // TLSLDAIX8 is expanded to one copy and GET_TLS_MOD, so we only set
129*0fca6ea1SDimitry Andric           // Opc2 here.
130*0fca6ea1SDimitry Andric           Opc2 = PPC::GETtlsMOD64AIX;
131*0fca6ea1SDimitry Andric           break;
132fe6060f1SDimitry Andric         case PPC::TLSGDAIX8:
133fe6060f1SDimitry Andric           // TLSGDAIX8 is expanded to two copies and GET_TLS_ADDR, so we only
134fe6060f1SDimitry Andric           // set Opc2 here.
135fe6060f1SDimitry Andric           Opc2 = PPC::GETtlsADDR64AIX;
136fe6060f1SDimitry Andric           break;
137fe6060f1SDimitry Andric         case PPC::TLSGDAIX:
138fe6060f1SDimitry Andric           // TLSGDAIX is expanded to two copies and GET_TLS_ADDR, so we only
139fe6060f1SDimitry Andric           // set Opc2 here.
140fe6060f1SDimitry Andric           Opc2 = PPC::GETtlsADDR32AIX;
141fe6060f1SDimitry Andric           break;
14206c3fb27SDimitry Andric         case PPC::GETtlsTpointer32AIX:
14306c3fb27SDimitry Andric           // GETtlsTpointer32AIX is expanded to a call to GET_TPOINTER on AIX
14406c3fb27SDimitry Andric           // 32-bit mode within PPCAsmPrinter. This instruction does not need
14506c3fb27SDimitry Andric           // to change, so Opc2 is set to the same instruction opcode.
14606c3fb27SDimitry Andric           Opc2 = PPC::GETtlsTpointer32AIX;
14706c3fb27SDimitry Andric           break;
148e8d8bef9SDimitry Andric         case PPC::PADDI8pc:
149e8d8bef9SDimitry Andric           assert(IsPCREL && "Expecting General/Local Dynamic PCRel");
150e8d8bef9SDimitry Andric           Opc1 = PPC::PADDI8pc;
151e8d8bef9SDimitry Andric           Opc2 = MI.getOperand(2).getTargetFlags() ==
152e8d8bef9SDimitry Andric                          PPCII::MO_GOT_TLSGD_PCREL_FLAG
153e8d8bef9SDimitry Andric                      ? PPC::GETtlsADDRPCREL
154e8d8bef9SDimitry Andric                      : PPC::GETtlsldADDRPCREL;
1550b57cec5SDimitry Andric         }
1560b57cec5SDimitry Andric 
1570b57cec5SDimitry Andric         // We create ADJCALLSTACKUP and ADJCALLSTACKDOWN around _tls_get_addr
1580b57cec5SDimitry Andric         // as scheduling fence to avoid it is scheduled before
1590b57cec5SDimitry Andric         // mflr in the prologue and the address in LR is clobbered (PR25839).
1600b57cec5SDimitry Andric         // We don't really need to save data to the stack - the clobbered
1610b57cec5SDimitry Andric         // registers are already saved when the SDNode (e.g. PPCaddiTlsgdLAddr)
1620b57cec5SDimitry Andric         // gets translated to the pseudo instruction (e.g. ADDItlsgdLADDR).
163*0fca6ea1SDimitry Andric         if (NeedFence) {
164*0fca6ea1SDimitry Andric           MBB.getParent()->getFrameInfo().setAdjustsStack(true);
1650b57cec5SDimitry Andric           BuildMI(MBB, I, DL, TII->get(PPC::ADJCALLSTACKDOWN)).addImm(0)
1660b57cec5SDimitry Andric                                                               .addImm(0);
167*0fca6ea1SDimitry Andric         }
1680b57cec5SDimitry Andric 
169fe6060f1SDimitry Andric         if (IsAIX) {
170*0fca6ea1SDimitry Andric           if (IsTLSLDAIXMI) {
171*0fca6ea1SDimitry Andric             // The relative order between the node that loads the variable
172*0fca6ea1SDimitry Andric             // offset from the TOC, and the .__tls_get_mod node is being tuned
173*0fca6ea1SDimitry Andric             // here. It is better to put the variable offset TOC load after the
174*0fca6ea1SDimitry Andric             // call, since this node can use clobbers r4/r5.
175*0fca6ea1SDimitry Andric             // Search for the pattern of the two nodes that load from the TOC
176*0fca6ea1SDimitry Andric             // (either for the variable offset or for the module handle), and
177*0fca6ea1SDimitry Andric             // then move the variable offset TOC load right before the node that
178*0fca6ea1SDimitry Andric             // uses the OutReg of the .__tls_get_mod node.
179*0fca6ea1SDimitry Andric             unsigned LDTocOp =
180*0fca6ea1SDimitry Andric                 Is64Bit ? (IsLargeModel ? PPC::LDtocL : PPC::LDtoc)
181*0fca6ea1SDimitry Andric                         : (IsLargeModel ? PPC::LWZtocL : PPC::LWZtoc);
182*0fca6ea1SDimitry Andric             if (!RegInfo.use_empty(OutReg)) {
183*0fca6ea1SDimitry Andric               std::set<MachineInstr *> Uses;
184*0fca6ea1SDimitry Andric               // Collect all instructions that use the OutReg.
185*0fca6ea1SDimitry Andric               for (MachineOperand &MO : RegInfo.use_operands(OutReg))
186*0fca6ea1SDimitry Andric                 Uses.insert(MO.getParent());
187*0fca6ea1SDimitry Andric               // Find the first user (e.g.: lwax/stfdx) of the OutReg within the
188*0fca6ea1SDimitry Andric               // current BB.
189*0fca6ea1SDimitry Andric               MachineBasicBlock::iterator UseIter = MBB.begin();
190*0fca6ea1SDimitry Andric               for (MachineBasicBlock::iterator IE = MBB.end(); UseIter != IE;
191*0fca6ea1SDimitry Andric                    ++UseIter)
192*0fca6ea1SDimitry Andric                 if (Uses.count(&*UseIter))
193*0fca6ea1SDimitry Andric                   break;
194*0fca6ea1SDimitry Andric 
195*0fca6ea1SDimitry Andric               // Additional handling is required when UserIter (the first user
196*0fca6ea1SDimitry Andric               // of OutReg) is pointing to a valid node that loads from the TOC.
197*0fca6ea1SDimitry Andric               // Check the pattern and do the movement if the pattern matches.
198*0fca6ea1SDimitry Andric               if (UseIter != MBB.end()) {
199*0fca6ea1SDimitry Andric                 // Collect all associated nodes that load from the TOC. Use
200*0fca6ea1SDimitry Andric                 // hasOneDef() to guard against unexpected scenarios.
201*0fca6ea1SDimitry Andric                 std::set<MachineInstr *> LoadFromTocs;
202*0fca6ea1SDimitry Andric                 for (MachineOperand &MO : UseIter->operands())
203*0fca6ea1SDimitry Andric                   if (MO.isReg() && MO.isUse()) {
204*0fca6ea1SDimitry Andric                     Register MOReg = MO.getReg();
205*0fca6ea1SDimitry Andric                     if (RegInfo.hasOneDef(MOReg)) {
206*0fca6ea1SDimitry Andric                       MachineInstr *Temp =
207*0fca6ea1SDimitry Andric                           RegInfo.getOneDef(MOReg)->getParent();
208*0fca6ea1SDimitry Andric                       // For the current TLSLDAIX node, get the corresponding
209*0fca6ea1SDimitry Andric                       // node that loads from the TOC for the InReg. Otherwise,
210*0fca6ea1SDimitry Andric                       // Temp probably pointed to the variable offset TOC load
211*0fca6ea1SDimitry Andric                       // we would like to move.
212*0fca6ea1SDimitry Andric                       if (Temp == &MI && RegInfo.hasOneDef(InReg))
213*0fca6ea1SDimitry Andric                         Temp = RegInfo.getOneDef(InReg)->getParent();
214*0fca6ea1SDimitry Andric                       if (Temp->getOpcode() == LDTocOp)
215*0fca6ea1SDimitry Andric                         LoadFromTocs.insert(Temp);
216*0fca6ea1SDimitry Andric                     } else {
217*0fca6ea1SDimitry Andric                       // FIXME: analyze this scenario if there is one.
218*0fca6ea1SDimitry Andric                       LoadFromTocs.clear();
219*0fca6ea1SDimitry Andric                       break;
220*0fca6ea1SDimitry Andric                     }
221*0fca6ea1SDimitry Andric                   }
222*0fca6ea1SDimitry Andric 
223*0fca6ea1SDimitry Andric                 // Check the two nodes that loaded from the TOC: one should be
224*0fca6ea1SDimitry Andric                 // "_$TLSML", and the other will be moved before the node that
225*0fca6ea1SDimitry Andric                 // uses the OutReg of the .__tls_get_mod node.
226*0fca6ea1SDimitry Andric                 if (LoadFromTocs.size() == 2) {
227*0fca6ea1SDimitry Andric                   MachineBasicBlock::iterator TLSMLIter = MBB.end();
228*0fca6ea1SDimitry Andric                   MachineBasicBlock::iterator OffsetIter = MBB.end();
229*0fca6ea1SDimitry Andric                   // Make sure the two nodes that loaded from the TOC are within
230*0fca6ea1SDimitry Andric                   // the current BB, and that one of them is from the "_$TLSML"
231*0fca6ea1SDimitry Andric                   // pseudo symbol, while the other is from the variable.
232*0fca6ea1SDimitry Andric                   for (MachineBasicBlock::iterator I = MBB.begin(),
233*0fca6ea1SDimitry Andric                                                    IE = MBB.end();
234*0fca6ea1SDimitry Andric                        I != IE; ++I)
235*0fca6ea1SDimitry Andric                     if (LoadFromTocs.count(&*I)) {
236*0fca6ea1SDimitry Andric                       MachineOperand MO = I->getOperand(1);
237*0fca6ea1SDimitry Andric                       if (MO.isGlobal() && MO.getGlobal()->hasName() &&
238*0fca6ea1SDimitry Andric                           MO.getGlobal()->getName() == "_$TLSML")
239*0fca6ea1SDimitry Andric                         TLSMLIter = I;
240*0fca6ea1SDimitry Andric                       else
241*0fca6ea1SDimitry Andric                         OffsetIter = I;
242*0fca6ea1SDimitry Andric                     }
243*0fca6ea1SDimitry Andric                   // Perform the movement when the desired scenario has been
244*0fca6ea1SDimitry Andric                   // identified, which should be when both of the iterators are
245*0fca6ea1SDimitry Andric                   // valid.
246*0fca6ea1SDimitry Andric                   if (TLSMLIter != MBB.end() && OffsetIter != MBB.end())
247*0fca6ea1SDimitry Andric                     OffsetIter->moveBefore(&*UseIter);
248*0fca6ea1SDimitry Andric                 }
249*0fca6ea1SDimitry Andric               }
250*0fca6ea1SDimitry Andric             }
251*0fca6ea1SDimitry Andric             // The module-handle is copied into r3. The copy is followed by
252*0fca6ea1SDimitry Andric             // GETtlsMOD32AIX/GETtlsMOD64AIX.
253*0fca6ea1SDimitry Andric             BuildMI(MBB, I, DL, TII->get(TargetOpcode::COPY), GPR3)
254*0fca6ea1SDimitry Andric                 .addReg(InReg);
255*0fca6ea1SDimitry Andric             // The call to .__tls_get_mod.
256*0fca6ea1SDimitry Andric             BuildMI(MBB, I, DL, TII->get(Opc2), GPR3).addReg(GPR3);
257*0fca6ea1SDimitry Andric           } else if (!IsTLSTPRelMI) {
258*0fca6ea1SDimitry Andric             // The variable offset and region handle (for TLSGD) are copied in
259*0fca6ea1SDimitry Andric             // r4 and r3. The copies are followed by
260*0fca6ea1SDimitry Andric             // GETtlsADDR32AIX/GETtlsADDR64AIX.
261fe6060f1SDimitry Andric             BuildMI(MBB, I, DL, TII->get(TargetOpcode::COPY), GPR4)
262fe6060f1SDimitry Andric                 .addReg(MI.getOperand(1).getReg());
263fe6060f1SDimitry Andric             BuildMI(MBB, I, DL, TII->get(TargetOpcode::COPY), GPR3)
264fe6060f1SDimitry Andric                 .addReg(MI.getOperand(2).getReg());
265fe6060f1SDimitry Andric             BuildMI(MBB, I, DL, TII->get(Opc2), GPR3).addReg(GPR3).addReg(GPR4);
26606c3fb27SDimitry Andric           } else
26706c3fb27SDimitry Andric             // The opcode of GETtlsTpointer32AIX does not change, because later
26806c3fb27SDimitry Andric             // this instruction will be expanded into a call to .__get_tpointer,
26906c3fb27SDimitry Andric             // which will return the thread pointer into r3.
27006c3fb27SDimitry Andric             BuildMI(MBB, I, DL, TII->get(Opc2), GPR3);
271fe6060f1SDimitry Andric         } else {
272e8d8bef9SDimitry Andric           MachineInstr *Addi;
273e8d8bef9SDimitry Andric           if (IsPCREL) {
274e8d8bef9SDimitry Andric             Addi = BuildMI(MBB, I, DL, TII->get(Opc1), GPR3).addImm(0);
275e8d8bef9SDimitry Andric           } else {
2760b57cec5SDimitry Andric             // Expand into two ops built prior to the existing instruction.
277e8d8bef9SDimitry Andric             assert(InReg != PPC::NoRegister && "Operand must be a register");
278e8d8bef9SDimitry Andric             Addi = BuildMI(MBB, I, DL, TII->get(Opc1), GPR3).addReg(InReg);
279e8d8bef9SDimitry Andric           }
280e8d8bef9SDimitry Andric 
2810b57cec5SDimitry Andric           Addi->addOperand(MI.getOperand(2));
2820b57cec5SDimitry Andric 
283fe6060f1SDimitry Andric           MachineInstr *Call =
284fe6060f1SDimitry Andric               (BuildMI(MBB, I, DL, TII->get(Opc2), GPR3).addReg(GPR3));
285e8d8bef9SDimitry Andric           if (IsPCREL)
286e8d8bef9SDimitry Andric             Call->addOperand(MI.getOperand(2));
287e8d8bef9SDimitry Andric           else
2880b57cec5SDimitry Andric             Call->addOperand(MI.getOperand(3));
289fe6060f1SDimitry Andric         }
2900b57cec5SDimitry Andric         if (NeedFence)
2910b57cec5SDimitry Andric           BuildMI(MBB, I, DL, TII->get(PPC::ADJCALLSTACKUP)).addImm(0).addImm(0);
2920b57cec5SDimitry Andric 
2930b57cec5SDimitry Andric         BuildMI(MBB, I, DL, TII->get(TargetOpcode::COPY), OutReg)
2940b57cec5SDimitry Andric           .addReg(GPR3);
2950b57cec5SDimitry Andric 
2960b57cec5SDimitry Andric         // Move past the original instruction and remove it.
2970b57cec5SDimitry Andric         ++I;
2980b57cec5SDimitry Andric         MI.removeFromParent();
2990b57cec5SDimitry Andric 
3000b57cec5SDimitry Andric         Changed = true;
3010b57cec5SDimitry Andric       }
3020b57cec5SDimitry Andric 
3030b57cec5SDimitry Andric       return Changed;
3040b57cec5SDimitry Andric     }
3050b57cec5SDimitry Andric 
3060b57cec5SDimitry Andric public:
isPCREL__anonbafe3dec0111::PPCTLSDynamicCall307e8d8bef9SDimitry Andric   bool isPCREL(const MachineInstr &MI) {
308e8d8bef9SDimitry Andric     return (MI.getOpcode() == PPC::PADDI8pc) &&
309e8d8bef9SDimitry Andric            (MI.getOperand(2).getTargetFlags() ==
310e8d8bef9SDimitry Andric                 PPCII::MO_GOT_TLSGD_PCREL_FLAG ||
311e8d8bef9SDimitry Andric             MI.getOperand(2).getTargetFlags() ==
312e8d8bef9SDimitry Andric                 PPCII::MO_GOT_TLSLD_PCREL_FLAG);
313e8d8bef9SDimitry Andric   }
314e8d8bef9SDimitry Andric 
runOnMachineFunction__anonbafe3dec0111::PPCTLSDynamicCall3150b57cec5SDimitry Andric     bool runOnMachineFunction(MachineFunction &MF) override {
3160b57cec5SDimitry Andric       TII = MF.getSubtarget<PPCSubtarget>().getInstrInfo();
3170b57cec5SDimitry Andric 
3180b57cec5SDimitry Andric       bool Changed = false;
3190b57cec5SDimitry Andric 
320349cc55cSDimitry Andric       for (MachineBasicBlock &B : llvm::make_early_inc_range(MF))
3210b57cec5SDimitry Andric         if (processBlock(B))
3220b57cec5SDimitry Andric           Changed = true;
3230b57cec5SDimitry Andric 
3240b57cec5SDimitry Andric       return Changed;
3250b57cec5SDimitry Andric     }
3260b57cec5SDimitry Andric 
getAnalysisUsage__anonbafe3dec0111::PPCTLSDynamicCall3270b57cec5SDimitry Andric     void getAnalysisUsage(AnalysisUsage &AU) const override {
328*0fca6ea1SDimitry Andric       AU.addRequired<LiveIntervalsWrapperPass>();
329*0fca6ea1SDimitry Andric       AU.addRequired<SlotIndexesWrapperPass>();
3300b57cec5SDimitry Andric       MachineFunctionPass::getAnalysisUsage(AU);
3310b57cec5SDimitry Andric     }
3320b57cec5SDimitry Andric   };
3330b57cec5SDimitry Andric }
3340b57cec5SDimitry Andric 
3350b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(PPCTLSDynamicCall, DEBUG_TYPE,
3360b57cec5SDimitry Andric                       "PowerPC TLS Dynamic Call Fixup", false, false)
337*0fca6ea1SDimitry Andric INITIALIZE_PASS_DEPENDENCY(LiveIntervalsWrapperPass)
338*0fca6ea1SDimitry Andric INITIALIZE_PASS_DEPENDENCY(SlotIndexesWrapperPass)
3390b57cec5SDimitry Andric INITIALIZE_PASS_END(PPCTLSDynamicCall, DEBUG_TYPE,
3400b57cec5SDimitry Andric                     "PowerPC TLS Dynamic Call Fixup", false, false)
3410b57cec5SDimitry Andric 
3420b57cec5SDimitry Andric char PPCTLSDynamicCall::ID = 0;
3430b57cec5SDimitry Andric FunctionPass*
createPPCTLSDynamicCallPass()3440b57cec5SDimitry Andric llvm::createPPCTLSDynamicCallPass() { return new PPCTLSDynamicCall(); }
345