xref: /llvm-project/llvm/lib/Target/PowerPC/PPCTLSDynamicCall.cpp (revision b74b80bb2d31dfb09559fcb71a1c474a421c633d)
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 "PPCInstrBuilder.h"
25 #include "PPCInstrInfo.h"
26 #include "PPCTargetMachine.h"
27 #include "llvm/CodeGen/LiveIntervals.h"
28 #include "llvm/CodeGen/MachineFunctionPass.h"
29 #include "llvm/CodeGen/MachineInstrBuilder.h"
30 #include "llvm/InitializePasses.h"
31 #include "llvm/Support/Debug.h"
32 #include "llvm/Support/raw_ostream.h"
33 
34 using namespace llvm;
35 
36 #define DEBUG_TYPE "ppc-tls-dynamic-call"
37 
38 namespace {
39   struct PPCTLSDynamicCall : public MachineFunctionPass {
40     static char ID;
41     PPCTLSDynamicCall() : MachineFunctionPass(ID) {
42       initializePPCTLSDynamicCallPass(*PassRegistry::getPassRegistry());
43     }
44 
45     const PPCInstrInfo *TII;
46     LiveIntervals *LIS;
47 
48 protected:
49     bool processBlock(MachineBasicBlock &MBB) {
50       bool Changed = false;
51       bool NeedFence = true;
52       bool Is64Bit = MBB.getParent()->getSubtarget<PPCSubtarget>().isPPC64();
53       bool IsTLSGDPCREL = false;
54 
55       for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
56            I != IE;) {
57         MachineInstr &MI = *I;
58         IsTLSGDPCREL = isTLSGDPCREL(MI);
59 
60         if (MI.getOpcode() != PPC::ADDItlsgdLADDR &&
61             MI.getOpcode() != PPC::ADDItlsldLADDR &&
62             MI.getOpcode() != PPC::ADDItlsgdLADDR32 &&
63             MI.getOpcode() != PPC::ADDItlsldLADDR32 && !IsTLSGDPCREL) {
64           // Although we create ADJCALLSTACKDOWN and ADJCALLSTACKUP
65           // as scheduling fences, we skip creating fences if we already
66           // have existing ADJCALLSTACKDOWN/UP to avoid nesting,
67           // which causes verification error with -verify-machineinstrs.
68           if (MI.getOpcode() == PPC::ADJCALLSTACKDOWN)
69             NeedFence = false;
70           else if (MI.getOpcode() == PPC::ADJCALLSTACKUP)
71             NeedFence = true;
72 
73           ++I;
74           continue;
75         }
76 
77         LLVM_DEBUG(dbgs() << "TLS Dynamic Call Fixup:\n    " << MI);
78 
79         Register OutReg = MI.getOperand(0).getReg();
80         Register InReg = PPC::NoRegister;
81         Register GPR3 = Is64Bit ? PPC::X3 : PPC::R3;
82         SmallVector<Register, 3> OrigRegs = {OutReg, GPR3};
83         if (!IsTLSGDPCREL) {
84           InReg = MI.getOperand(1).getReg();
85           OrigRegs.push_back(InReg);
86         }
87         DebugLoc DL = MI.getDebugLoc();
88 
89         unsigned Opc1, Opc2;
90         switch (MI.getOpcode()) {
91         default:
92           llvm_unreachable("Opcode inconsistency error");
93         case PPC::ADDItlsgdLADDR:
94           Opc1 = PPC::ADDItlsgdL;
95           Opc2 = PPC::GETtlsADDR;
96           break;
97         case PPC::ADDItlsldLADDR:
98           Opc1 = PPC::ADDItlsldL;
99           Opc2 = PPC::GETtlsldADDR;
100           break;
101         case PPC::ADDItlsgdLADDR32:
102           Opc1 = PPC::ADDItlsgdL32;
103           Opc2 = PPC::GETtlsADDR32;
104           break;
105         case PPC::ADDItlsldLADDR32:
106           Opc1 = PPC::ADDItlsldL32;
107           Opc2 = PPC::GETtlsldADDR32;
108           break;
109         case PPC::PADDI8pc:
110           assert(IsTLSGDPCREL && "Expecting General Dynamic PCRel");
111           Opc1 = PPC::PADDI8pc;
112           Opc2 = PPC::GETtlsADDR;
113         }
114 
115         // We create ADJCALLSTACKUP and ADJCALLSTACKDOWN around _tls_get_addr
116         // as scheduling fence to avoid it is scheduled before
117         // mflr in the prologue and the address in LR is clobbered (PR25839).
118         // We don't really need to save data to the stack - the clobbered
119         // registers are already saved when the SDNode (e.g. PPCaddiTlsgdLAddr)
120         // gets translated to the pseudo instruction (e.g. ADDItlsgdLADDR).
121         if (NeedFence)
122           BuildMI(MBB, I, DL, TII->get(PPC::ADJCALLSTACKDOWN)).addImm(0)
123                                                               .addImm(0);
124 
125         MachineInstr *Addi;
126         if (IsTLSGDPCREL) {
127           Addi = BuildMI(MBB, I, DL, TII->get(Opc1), GPR3).addImm(0);
128         } else {
129           // Expand into two ops built prior to the existing instruction.
130           assert(InReg != PPC::NoRegister && "Operand must be a register");
131           Addi = BuildMI(MBB, I, DL, TII->get(Opc1), GPR3).addReg(InReg);
132         }
133 
134         Addi->addOperand(MI.getOperand(2));
135 
136         // The ADDItls* instruction is the first instruction in the
137         // repair range.
138         MachineBasicBlock::iterator First = I;
139         --First;
140 
141         MachineInstr *Call = (BuildMI(MBB, I, DL, TII->get(Opc2), GPR3)
142                               .addReg(GPR3));
143         if (IsTLSGDPCREL)
144           Call->addOperand(MI.getOperand(2));
145         else
146           Call->addOperand(MI.getOperand(3));
147 
148         if (NeedFence)
149           BuildMI(MBB, I, DL, TII->get(PPC::ADJCALLSTACKUP)).addImm(0).addImm(0);
150 
151         BuildMI(MBB, I, DL, TII->get(TargetOpcode::COPY), OutReg)
152           .addReg(GPR3);
153 
154         // The COPY is the last instruction in the repair range.
155         MachineBasicBlock::iterator Last = I;
156         --Last;
157 
158         // Move past the original instruction and remove it.
159         ++I;
160         MI.removeFromParent();
161 
162         // Repair the live intervals.
163         LIS->repairIntervalsInRange(&MBB, First, Last, OrigRegs);
164         Changed = true;
165       }
166 
167       return Changed;
168     }
169 
170 public:
171   bool isTLSGDPCREL(const MachineInstr &MI) {
172     return (MI.getOpcode() == PPC::PADDI8pc) &&
173            (MI.getOperand(2).getTargetFlags() ==
174             PPCII::MO_GOT_TLSGD_PCREL_FLAG);
175   }
176     bool runOnMachineFunction(MachineFunction &MF) override {
177       TII = MF.getSubtarget<PPCSubtarget>().getInstrInfo();
178       LIS = &getAnalysis<LiveIntervals>();
179 
180       bool Changed = false;
181 
182       for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
183         MachineBasicBlock &B = *I++;
184         if (processBlock(B))
185           Changed = true;
186       }
187 
188       return Changed;
189     }
190 
191     void getAnalysisUsage(AnalysisUsage &AU) const override {
192       AU.addRequired<LiveIntervals>();
193       AU.addPreserved<LiveIntervals>();
194       AU.addRequired<SlotIndexes>();
195       AU.addPreserved<SlotIndexes>();
196       MachineFunctionPass::getAnalysisUsage(AU);
197     }
198   };
199 }
200 
201 INITIALIZE_PASS_BEGIN(PPCTLSDynamicCall, DEBUG_TYPE,
202                       "PowerPC TLS Dynamic Call Fixup", false, false)
203 INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
204 INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
205 INITIALIZE_PASS_END(PPCTLSDynamicCall, DEBUG_TYPE,
206                     "PowerPC TLS Dynamic Call Fixup", false, false)
207 
208 char PPCTLSDynamicCall::ID = 0;
209 FunctionPass*
210 llvm::createPPCTLSDynamicCallPass() { return new PPCTLSDynamicCall(); }
211