1 //===---------- PPCTLSDynamicCall.cpp - TLS Dynamic Call Fixup ------------===//
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
7 //===----------------------------------------------------------------------===//
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.
18 // This is done in a late pass so that TLS variable accesses can be
19 // fully commoned by MachineCSE.
21 //===----------------------------------------------------------------------===//
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"
36 #define DEBUG_TYPE "ppc-tls-dynamic-call"
39 struct PPCTLSDynamicCall
: public MachineFunctionPass
{
41 PPCTLSDynamicCall() : MachineFunctionPass(ID
) {
42 initializePPCTLSDynamicCallPass(*PassRegistry::getPassRegistry());
45 const PPCInstrInfo
*TII
;
49 bool processBlock(MachineBasicBlock
&MBB
) {
51 bool NeedFence
= true;
52 bool Is64Bit
= MBB
.getParent()->getSubtarget
<PPCSubtarget
>().isPPC64();
53 bool IsAIX
= MBB
.getParent()->getSubtarget
<PPCSubtarget
>().isAIXABI();
56 for (MachineBasicBlock::iterator I
= MBB
.begin(), IE
= MBB
.end();
58 MachineInstr
&MI
= *I
;
59 IsPCREL
= isPCREL(MI
);
61 if (MI
.getOpcode() != PPC::ADDItlsgdLADDR
&&
62 MI
.getOpcode() != PPC::ADDItlsldLADDR
&&
63 MI
.getOpcode() != PPC::ADDItlsgdLADDR32
&&
64 MI
.getOpcode() != PPC::ADDItlsldLADDR32
&&
65 MI
.getOpcode() != PPC::TLSGDAIX
&&
66 MI
.getOpcode() != PPC::TLSGDAIX8
&& !IsPCREL
) {
67 // Although we create ADJCALLSTACKDOWN and ADJCALLSTACKUP
68 // as scheduling fences, we skip creating fences if we already
69 // have existing ADJCALLSTACKDOWN/UP to avoid nesting,
70 // which causes verification error with -verify-machineinstrs.
71 if (MI
.getOpcode() == PPC::ADJCALLSTACKDOWN
)
73 else if (MI
.getOpcode() == PPC::ADJCALLSTACKUP
)
80 LLVM_DEBUG(dbgs() << "TLS Dynamic Call Fixup:\n " << MI
);
82 Register OutReg
= MI
.getOperand(0).getReg();
83 Register InReg
= PPC::NoRegister
;
84 Register GPR3
= Is64Bit
? PPC::X3
: PPC::R3
;
85 Register GPR4
= Is64Bit
? PPC::X4
: PPC::R4
;
86 SmallVector
<Register
, 3> OrigRegs
= {OutReg
, GPR3
};
88 InReg
= MI
.getOperand(1).getReg();
89 OrigRegs
.push_back(InReg
);
91 DebugLoc DL
= MI
.getDebugLoc();
94 switch (MI
.getOpcode()) {
96 llvm_unreachable("Opcode inconsistency error");
97 case PPC::ADDItlsgdLADDR
:
98 Opc1
= PPC::ADDItlsgdL
;
99 Opc2
= PPC::GETtlsADDR
;
101 case PPC::ADDItlsldLADDR
:
102 Opc1
= PPC::ADDItlsldL
;
103 Opc2
= PPC::GETtlsldADDR
;
105 case PPC::ADDItlsgdLADDR32
:
106 Opc1
= PPC::ADDItlsgdL32
;
107 Opc2
= PPC::GETtlsADDR32
;
109 case PPC::ADDItlsldLADDR32
:
110 Opc1
= PPC::ADDItlsldL32
;
111 Opc2
= PPC::GETtlsldADDR32
;
114 // TLSGDAIX8 is expanded to two copies and GET_TLS_ADDR, so we only
116 Opc2
= PPC::GETtlsADDR64AIX
;
119 // TLSGDAIX is expanded to two copies and GET_TLS_ADDR, so we only
121 Opc2
= PPC::GETtlsADDR32AIX
;
124 assert(IsPCREL
&& "Expecting General/Local Dynamic PCRel");
125 Opc1
= PPC::PADDI8pc
;
126 Opc2
= MI
.getOperand(2).getTargetFlags() ==
127 PPCII::MO_GOT_TLSGD_PCREL_FLAG
128 ? PPC::GETtlsADDRPCREL
129 : PPC::GETtlsldADDRPCREL
;
132 // We create ADJCALLSTACKUP and ADJCALLSTACKDOWN around _tls_get_addr
133 // as scheduling fence to avoid it is scheduled before
134 // mflr in the prologue and the address in LR is clobbered (PR25839).
135 // We don't really need to save data to the stack - the clobbered
136 // registers are already saved when the SDNode (e.g. PPCaddiTlsgdLAddr)
137 // gets translated to the pseudo instruction (e.g. ADDItlsgdLADDR).
139 BuildMI(MBB
, I
, DL
, TII
->get(PPC::ADJCALLSTACKDOWN
)).addImm(0)
142 // The ADDItls* instruction is the first instruction in the
144 MachineBasicBlock::iterator First
= I
;
148 // The variable offset and region handle are copied in r4 and r3. The
149 // copies are followed by GETtlsADDR32AIX/GETtlsADDR64AIX.
150 BuildMI(MBB
, I
, DL
, TII
->get(TargetOpcode::COPY
), GPR4
)
151 .addReg(MI
.getOperand(1).getReg());
152 BuildMI(MBB
, I
, DL
, TII
->get(TargetOpcode::COPY
), GPR3
)
153 .addReg(MI
.getOperand(2).getReg());
154 BuildMI(MBB
, I
, DL
, TII
->get(Opc2
), GPR3
).addReg(GPR3
).addReg(GPR4
);
158 Addi
= BuildMI(MBB
, I
, DL
, TII
->get(Opc1
), GPR3
).addImm(0);
160 // Expand into two ops built prior to the existing instruction.
161 assert(InReg
!= PPC::NoRegister
&& "Operand must be a register");
162 Addi
= BuildMI(MBB
, I
, DL
, TII
->get(Opc1
), GPR3
).addReg(InReg
);
165 Addi
->addOperand(MI
.getOperand(2));
168 (BuildMI(MBB
, I
, DL
, TII
->get(Opc2
), GPR3
).addReg(GPR3
));
170 Call
->addOperand(MI
.getOperand(2));
172 Call
->addOperand(MI
.getOperand(3));
175 BuildMI(MBB
, I
, DL
, TII
->get(PPC::ADJCALLSTACKUP
)).addImm(0).addImm(0);
177 BuildMI(MBB
, I
, DL
, TII
->get(TargetOpcode::COPY
), OutReg
)
180 // The COPY is the last instruction in the repair range.
181 MachineBasicBlock::iterator Last
= I
;
184 // Move past the original instruction and remove it.
186 MI
.removeFromParent();
188 // Repair the live intervals.
189 LIS
->repairIntervalsInRange(&MBB
, First
, Last
, OrigRegs
);
197 bool isPCREL(const MachineInstr
&MI
) {
198 return (MI
.getOpcode() == PPC::PADDI8pc
) &&
199 (MI
.getOperand(2).getTargetFlags() ==
200 PPCII::MO_GOT_TLSGD_PCREL_FLAG
||
201 MI
.getOperand(2).getTargetFlags() ==
202 PPCII::MO_GOT_TLSLD_PCREL_FLAG
);
205 bool runOnMachineFunction(MachineFunction
&MF
) override
{
206 TII
= MF
.getSubtarget
<PPCSubtarget
>().getInstrInfo();
207 LIS
= &getAnalysis
<LiveIntervals
>();
209 bool Changed
= false;
211 for (MachineFunction::iterator I
= MF
.begin(); I
!= MF
.end();) {
212 MachineBasicBlock
&B
= *I
++;
220 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
221 AU
.addRequired
<LiveIntervals
>();
222 AU
.addPreserved
<LiveIntervals
>();
223 AU
.addRequired
<SlotIndexes
>();
224 AU
.addPreserved
<SlotIndexes
>();
225 MachineFunctionPass::getAnalysisUsage(AU
);
230 INITIALIZE_PASS_BEGIN(PPCTLSDynamicCall
, DEBUG_TYPE
,
231 "PowerPC TLS Dynamic Call Fixup", false, false)
232 INITIALIZE_PASS_DEPENDENCY(LiveIntervals
)
233 INITIALIZE_PASS_DEPENDENCY(SlotIndexes
)
234 INITIALIZE_PASS_END(PPCTLSDynamicCall
, DEBUG_TYPE
,
235 "PowerPC TLS Dynamic Call Fixup", false, false)
237 char PPCTLSDynamicCall::ID
= 0;
239 llvm::createPPCTLSDynamicCallPass() { return new PPCTLSDynamicCall(); }