1ff0cc061SDimitry Andric //===---------- PPCTLSDynamicCall.cpp - TLS Dynamic Call Fixup ------------===//
2ff0cc061SDimitry Andric //
3ff0cc061SDimitry Andric //                     The LLVM Compiler Infrastructure
4ff0cc061SDimitry Andric //
5ff0cc061SDimitry Andric // This file is distributed under the University of Illinois Open Source
6ff0cc061SDimitry Andric // License. See LICENSE.TXT for details.
7ff0cc061SDimitry Andric //
8ff0cc061SDimitry Andric //===----------------------------------------------------------------------===//
9ff0cc061SDimitry Andric //
10ff0cc061SDimitry Andric // This pass expands ADDItls{ld,gd}LADDR[32] machine instructions into
11ff0cc061SDimitry Andric // separate ADDItls[gd]L[32] and GETtlsADDR[32] instructions, both of
12ff0cc061SDimitry Andric // which define GPR3.  A copy is added from GPR3 to the target virtual
13ff0cc061SDimitry Andric // register of the original instruction.  The GETtlsADDR[32] is really
14ff0cc061SDimitry Andric // a call instruction, so its target register is constrained to be GPR3.
15ff0cc061SDimitry Andric // This is not true of ADDItls[gd]L[32], but there is a legacy linker
16ff0cc061SDimitry Andric // optimization bug that requires the target register of the addi of
17ff0cc061SDimitry Andric // a local- or general-dynamic TLS access sequence to be GPR3.
18ff0cc061SDimitry Andric //
19ff0cc061SDimitry Andric // This is done in a late pass so that TLS variable accesses can be
20ff0cc061SDimitry Andric // fully commoned by MachineCSE.
21ff0cc061SDimitry Andric //
22ff0cc061SDimitry Andric //===----------------------------------------------------------------------===//
23ff0cc061SDimitry Andric 
24ff0cc061SDimitry Andric #include "PPC.h"
25ff0cc061SDimitry Andric #include "PPCInstrBuilder.h"
26db17bf38SDimitry Andric #include "PPCInstrInfo.h"
27ff0cc061SDimitry Andric #include "PPCTargetMachine.h"
282cab237bSDimitry Andric #include "llvm/CodeGen/LiveIntervals.h"
29ff0cc061SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
30ff0cc061SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
31ff0cc061SDimitry Andric #include "llvm/Support/Debug.h"
32ff0cc061SDimitry Andric #include "llvm/Support/raw_ostream.h"
33ff0cc061SDimitry Andric 
34ff0cc061SDimitry Andric using namespace llvm;
35ff0cc061SDimitry Andric 
36ff0cc061SDimitry Andric #define DEBUG_TYPE "ppc-tls-dynamic-call"
37ff0cc061SDimitry Andric 
38ff0cc061SDimitry Andric namespace llvm {
39ff0cc061SDimitry Andric   void initializePPCTLSDynamicCallPass(PassRegistry&);
40ff0cc061SDimitry Andric }
41ff0cc061SDimitry Andric 
42ff0cc061SDimitry Andric namespace {
43ff0cc061SDimitry Andric   struct PPCTLSDynamicCall : public MachineFunctionPass {
44ff0cc061SDimitry Andric     static char ID;
PPCTLSDynamicCall__anon587cb2110111::PPCTLSDynamicCall45ff0cc061SDimitry Andric     PPCTLSDynamicCall() : MachineFunctionPass(ID) {
46ff0cc061SDimitry Andric       initializePPCTLSDynamicCallPass(*PassRegistry::getPassRegistry());
47ff0cc061SDimitry Andric     }
48ff0cc061SDimitry Andric 
49ff0cc061SDimitry Andric     const PPCInstrInfo *TII;
50ff0cc061SDimitry Andric     LiveIntervals *LIS;
51ff0cc061SDimitry Andric 
52ff0cc061SDimitry Andric protected:
processBlock__anon587cb2110111::PPCTLSDynamicCall53ff0cc061SDimitry Andric     bool processBlock(MachineBasicBlock &MBB) {
54ff0cc061SDimitry Andric       bool Changed = false;
55a580b014SDimitry Andric       bool NeedFence = true;
56ff0cc061SDimitry Andric       bool Is64Bit = MBB.getParent()->getSubtarget<PPCSubtarget>().isPPC64();
57ff0cc061SDimitry Andric 
58ff0cc061SDimitry Andric       for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
59ff0cc061SDimitry Andric            I != IE;) {
60d88c1a5aSDimitry Andric         MachineInstr &MI = *I;
61ff0cc061SDimitry Andric 
62d88c1a5aSDimitry Andric         if (MI.getOpcode() != PPC::ADDItlsgdLADDR &&
63d88c1a5aSDimitry Andric             MI.getOpcode() != PPC::ADDItlsldLADDR &&
64d88c1a5aSDimitry Andric             MI.getOpcode() != PPC::ADDItlsgdLADDR32 &&
65d88c1a5aSDimitry Andric             MI.getOpcode() != PPC::ADDItlsldLADDR32) {
66a580b014SDimitry Andric 
67a580b014SDimitry Andric           // Although we create ADJCALLSTACKDOWN and ADJCALLSTACKUP
68a580b014SDimitry Andric           // as scheduling fences, we skip creating fences if we already
69a580b014SDimitry Andric           // have existing ADJCALLSTACKDOWN/UP to avoid nesting,
70a580b014SDimitry Andric           // which causes verification error with -verify-machineinstrs.
71a580b014SDimitry Andric           if (MI.getOpcode() == PPC::ADJCALLSTACKDOWN)
72a580b014SDimitry Andric             NeedFence = false;
73a580b014SDimitry Andric           else if (MI.getOpcode() == PPC::ADJCALLSTACKUP)
74a580b014SDimitry Andric             NeedFence = true;
75a580b014SDimitry Andric 
76ff0cc061SDimitry Andric           ++I;
77ff0cc061SDimitry Andric           continue;
78ff0cc061SDimitry Andric         }
79ff0cc061SDimitry Andric 
80*4ba319b5SDimitry Andric         LLVM_DEBUG(dbgs() << "TLS Dynamic Call Fixup:\n    " << MI);
81ff0cc061SDimitry Andric 
82d88c1a5aSDimitry Andric         unsigned OutReg = MI.getOperand(0).getReg();
83d88c1a5aSDimitry Andric         unsigned InReg = MI.getOperand(1).getReg();
84d88c1a5aSDimitry Andric         DebugLoc DL = MI.getDebugLoc();
85ff0cc061SDimitry Andric         unsigned GPR3 = Is64Bit ? PPC::X3 : PPC::R3;
86ff0cc061SDimitry Andric         unsigned Opc1, Opc2;
873ca95b02SDimitry Andric         const unsigned OrigRegs[] = {OutReg, InReg, GPR3};
88ff0cc061SDimitry Andric 
89d88c1a5aSDimitry Andric         switch (MI.getOpcode()) {
90ff0cc061SDimitry Andric         default:
91ff0cc061SDimitry Andric           llvm_unreachable("Opcode inconsistency error");
92ff0cc061SDimitry Andric         case PPC::ADDItlsgdLADDR:
93ff0cc061SDimitry Andric           Opc1 = PPC::ADDItlsgdL;
94ff0cc061SDimitry Andric           Opc2 = PPC::GETtlsADDR;
95ff0cc061SDimitry Andric           break;
96ff0cc061SDimitry Andric         case PPC::ADDItlsldLADDR:
97ff0cc061SDimitry Andric           Opc1 = PPC::ADDItlsldL;
98ff0cc061SDimitry Andric           Opc2 = PPC::GETtlsldADDR;
99ff0cc061SDimitry Andric           break;
100ff0cc061SDimitry Andric         case PPC::ADDItlsgdLADDR32:
101ff0cc061SDimitry Andric           Opc1 = PPC::ADDItlsgdL32;
102ff0cc061SDimitry Andric           Opc2 = PPC::GETtlsADDR32;
103ff0cc061SDimitry Andric           break;
104ff0cc061SDimitry Andric         case PPC::ADDItlsldLADDR32:
105ff0cc061SDimitry Andric           Opc1 = PPC::ADDItlsldL32;
106ff0cc061SDimitry Andric           Opc2 = PPC::GETtlsldADDR32;
107ff0cc061SDimitry Andric           break;
108ff0cc061SDimitry Andric         }
109ff0cc061SDimitry Andric 
110a580b014SDimitry Andric         // We create ADJCALLSTACKUP and ADJCALLSTACKDOWN around _tls_get_addr
111*4ba319b5SDimitry Andric         // as scheduling fence to avoid it is scheduled before
112a580b014SDimitry Andric         // mflr in the prologue and the address in LR is clobbered (PR25839).
113a580b014SDimitry Andric         // We don't really need to save data to the stack - the clobbered
114444ed5c5SDimitry Andric         // registers are already saved when the SDNode (e.g. PPCaddiTlsgdLAddr)
115444ed5c5SDimitry Andric         // gets translated to the pseudo instruction (e.g. ADDItlsgdLADDR).
116a580b014SDimitry Andric         if (NeedFence)
1175517e702SDimitry Andric           BuildMI(MBB, I, DL, TII->get(PPC::ADJCALLSTACKDOWN)).addImm(0)
1185517e702SDimitry Andric                                                               .addImm(0);
119444ed5c5SDimitry Andric 
120ff0cc061SDimitry Andric         // Expand into two ops built prior to the existing instruction.
121ff0cc061SDimitry Andric         MachineInstr *Addi = BuildMI(MBB, I, DL, TII->get(Opc1), GPR3)
122ff0cc061SDimitry Andric           .addReg(InReg);
123d88c1a5aSDimitry Andric         Addi->addOperand(MI.getOperand(2));
124ff0cc061SDimitry Andric 
125ff0cc061SDimitry Andric         // The ADDItls* instruction is the first instruction in the
126ff0cc061SDimitry Andric         // repair range.
127ff0cc061SDimitry Andric         MachineBasicBlock::iterator First = I;
128ff0cc061SDimitry Andric         --First;
129ff0cc061SDimitry Andric 
130ff0cc061SDimitry Andric         MachineInstr *Call = (BuildMI(MBB, I, DL, TII->get(Opc2), GPR3)
131ff0cc061SDimitry Andric                               .addReg(GPR3));
132d88c1a5aSDimitry Andric         Call->addOperand(MI.getOperand(3));
133ff0cc061SDimitry Andric 
134a580b014SDimitry Andric         if (NeedFence)
135444ed5c5SDimitry Andric           BuildMI(MBB, I, DL, TII->get(PPC::ADJCALLSTACKUP)).addImm(0).addImm(0);
136444ed5c5SDimitry Andric 
137ff0cc061SDimitry Andric         BuildMI(MBB, I, DL, TII->get(TargetOpcode::COPY), OutReg)
138ff0cc061SDimitry Andric           .addReg(GPR3);
139ff0cc061SDimitry Andric 
140ff0cc061SDimitry Andric         // The COPY is the last instruction in the repair range.
141ff0cc061SDimitry Andric         MachineBasicBlock::iterator Last = I;
142ff0cc061SDimitry Andric         --Last;
143ff0cc061SDimitry Andric 
144ff0cc061SDimitry Andric         // Move past the original instruction and remove it.
145ff0cc061SDimitry Andric         ++I;
146d88c1a5aSDimitry Andric         MI.removeFromParent();
147ff0cc061SDimitry Andric 
148ff0cc061SDimitry Andric         // Repair the live intervals.
149ff0cc061SDimitry Andric         LIS->repairIntervalsInRange(&MBB, First, Last, OrigRegs);
150ff0cc061SDimitry Andric         Changed = true;
151ff0cc061SDimitry Andric       }
152ff0cc061SDimitry Andric 
153ff0cc061SDimitry Andric       return Changed;
154ff0cc061SDimitry Andric     }
155ff0cc061SDimitry Andric 
156ff0cc061SDimitry Andric public:
runOnMachineFunction__anon587cb2110111::PPCTLSDynamicCall157ff0cc061SDimitry Andric     bool runOnMachineFunction(MachineFunction &MF) override {
158ff0cc061SDimitry Andric       TII = MF.getSubtarget<PPCSubtarget>().getInstrInfo();
159ff0cc061SDimitry Andric       LIS = &getAnalysis<LiveIntervals>();
160ff0cc061SDimitry Andric 
161ff0cc061SDimitry Andric       bool Changed = false;
162ff0cc061SDimitry Andric 
163ff0cc061SDimitry Andric       for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
164ff0cc061SDimitry Andric         MachineBasicBlock &B = *I++;
165ff0cc061SDimitry Andric         if (processBlock(B))
166ff0cc061SDimitry Andric           Changed = true;
167ff0cc061SDimitry Andric       }
168ff0cc061SDimitry Andric 
169ff0cc061SDimitry Andric       return Changed;
170ff0cc061SDimitry Andric     }
171ff0cc061SDimitry Andric 
getAnalysisUsage__anon587cb2110111::PPCTLSDynamicCall172ff0cc061SDimitry Andric     void getAnalysisUsage(AnalysisUsage &AU) const override {
173ff0cc061SDimitry Andric       AU.addRequired<LiveIntervals>();
174ff0cc061SDimitry Andric       AU.addPreserved<LiveIntervals>();
175ff0cc061SDimitry Andric       AU.addRequired<SlotIndexes>();
176ff0cc061SDimitry Andric       AU.addPreserved<SlotIndexes>();
177ff0cc061SDimitry Andric       MachineFunctionPass::getAnalysisUsage(AU);
178ff0cc061SDimitry Andric     }
179ff0cc061SDimitry Andric   };
1803dac3a9bSDimitry Andric }
181ff0cc061SDimitry Andric 
182ff0cc061SDimitry Andric INITIALIZE_PASS_BEGIN(PPCTLSDynamicCall, DEBUG_TYPE,
183ff0cc061SDimitry Andric                       "PowerPC TLS Dynamic Call Fixup", false, false)
184ff0cc061SDimitry Andric INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
185ff0cc061SDimitry Andric INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
186ff0cc061SDimitry Andric INITIALIZE_PASS_END(PPCTLSDynamicCall, DEBUG_TYPE,
187ff0cc061SDimitry Andric                     "PowerPC TLS Dynamic Call Fixup", false, false)
188ff0cc061SDimitry Andric 
189ff0cc061SDimitry Andric char PPCTLSDynamicCall::ID = 0;
190ff0cc061SDimitry Andric FunctionPass*
createPPCTLSDynamicCallPass()191ff0cc061SDimitry Andric llvm::createPPCTLSDynamicCallPass() { return new PPCTLSDynamicCall(); }
192