blob: 8f313d9d01c4179e69b97dc8e113e85c855d9f35 [file] [log] [blame]
Bill Schmidt82f1c772015-02-10 19:09:05 +00001//===---------- PPCTLSDynamicCall.cpp - TLS Dynamic Call Fixup ------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Bill Schmidt82f1c772015-02-10 19:09:05 +00006//
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
Bill Schmidt82f1c772015-02-10 19:09:05 +000023#include "PPC.h"
24#include "PPCInstrBuilder.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000025#include "PPCInstrInfo.h"
Bill Schmidt82f1c772015-02-10 19:09:05 +000026#include "PPCTargetMachine.h"
Matthias Braunf8422972017-12-13 02:51:04 +000027#include "llvm/CodeGen/LiveIntervals.h"
Bill Schmidt82f1c772015-02-10 19:09:05 +000028#include "llvm/CodeGen/MachineFunctionPass.h"
29#include "llvm/CodeGen/MachineInstrBuilder.h"
30#include "llvm/Support/Debug.h"
31#include "llvm/Support/raw_ostream.h"
32
33using namespace llvm;
34
35#define DEBUG_TYPE "ppc-tls-dynamic-call"
36
Bill Schmidt82f1c772015-02-10 19:09:05 +000037namespace {
38 struct PPCTLSDynamicCall : public MachineFunctionPass {
39 static char ID;
40 PPCTLSDynamicCall() : MachineFunctionPass(ID) {
41 initializePPCTLSDynamicCallPass(*PassRegistry::getPassRegistry());
42 }
43
Bill Schmidt82f1c772015-02-10 19:09:05 +000044 const PPCInstrInfo *TII;
45 LiveIntervals *LIS;
46
47protected:
48 bool processBlock(MachineBasicBlock &MBB) {
49 bool Changed = false;
Hiroshi Inoue6989caa2017-06-29 14:13:38 +000050 bool NeedFence = true;
Eric Christopher1df0c512015-02-20 18:44:15 +000051 bool Is64Bit = MBB.getParent()->getSubtarget<PPCSubtarget>().isPPC64();
Bill Schmidt82f1c772015-02-10 19:09:05 +000052
53 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
Hal Finkel82e1fc52015-05-21 23:45:49 +000054 I != IE;) {
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +000055 MachineInstr &MI = *I;
Bill Schmidt82f1c772015-02-10 19:09:05 +000056
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +000057 if (MI.getOpcode() != PPC::ADDItlsgdLADDR &&
58 MI.getOpcode() != PPC::ADDItlsldLADDR &&
59 MI.getOpcode() != PPC::ADDItlsgdLADDR32 &&
60 MI.getOpcode() != PPC::ADDItlsldLADDR32) {
Hiroshi Inoue6989caa2017-06-29 14:13:38 +000061
62 // Although we create ADJCALLSTACKDOWN and ADJCALLSTACKUP
63 // as scheduling fences, we skip creating fences if we already
64 // have existing ADJCALLSTACKDOWN/UP to avoid nesting,
65 // which causes verification error with -verify-machineinstrs.
66 if (MI.getOpcode() == PPC::ADJCALLSTACKDOWN)
67 NeedFence = false;
68 else if (MI.getOpcode() == PPC::ADJCALLSTACKUP)
69 NeedFence = true;
70
Hal Finkel82e1fc52015-05-21 23:45:49 +000071 ++I;
Bill Schmidt82f1c772015-02-10 19:09:05 +000072 continue;
Hal Finkel82e1fc52015-05-21 23:45:49 +000073 }
Bill Schmidt82f1c772015-02-10 19:09:05 +000074
Nicola Zaghend34e60c2018-05-14 12:53:11 +000075 LLVM_DEBUG(dbgs() << "TLS Dynamic Call Fixup:\n " << MI);
Bill Schmidt82f1c772015-02-10 19:09:05 +000076
Daniel Sanders0c476112019-08-15 19:22:08 +000077 Register OutReg = MI.getOperand(0).getReg();
78 Register InReg = MI.getOperand(1).getReg();
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +000079 DebugLoc DL = MI.getDebugLoc();
Bill Schmidt82f1c772015-02-10 19:09:05 +000080 unsigned GPR3 = Is64Bit ? PPC::X3 : PPC::R3;
81 unsigned Opc1, Opc2;
Benjamin Kramer3bc1edf2016-07-02 11:41:39 +000082 const unsigned OrigRegs[] = {OutReg, InReg, GPR3};
Bill Schmidt82f1c772015-02-10 19:09:05 +000083
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +000084 switch (MI.getOpcode()) {
Bill Schmidt82f1c772015-02-10 19:09:05 +000085 default:
86 llvm_unreachable("Opcode inconsistency error");
87 case PPC::ADDItlsgdLADDR:
88 Opc1 = PPC::ADDItlsgdL;
89 Opc2 = PPC::GETtlsADDR;
90 break;
91 case PPC::ADDItlsldLADDR:
92 Opc1 = PPC::ADDItlsldL;
93 Opc2 = PPC::GETtlsldADDR;
94 break;
95 case PPC::ADDItlsgdLADDR32:
96 Opc1 = PPC::ADDItlsgdL32;
97 Opc2 = PPC::GETtlsADDR32;
98 break;
99 case PPC::ADDItlsldLADDR32:
100 Opc1 = PPC::ADDItlsldL32;
101 Opc2 = PPC::GETtlsldADDR32;
102 break;
103 }
104
Hiroshi Inoue6989caa2017-06-29 14:13:38 +0000105 // We create ADJCALLSTACKUP and ADJCALLSTACKDOWN around _tls_get_addr
Hiroshi Inoue0f7f59f2018-06-13 08:54:13 +0000106 // as scheduling fence to avoid it is scheduled before
Hiroshi Inoue6989caa2017-06-29 14:13:38 +0000107 // mflr in the prologue and the address in LR is clobbered (PR25839).
108 // We don't really need to save data to the stack - the clobbered
Kyle Buttbfcff382016-01-08 02:06:19 +0000109 // registers are already saved when the SDNode (e.g. PPCaddiTlsgdLAddr)
110 // gets translated to the pseudo instruction (e.g. ADDItlsgdLADDR).
Hiroshi Inoue6989caa2017-06-29 14:13:38 +0000111 if (NeedFence)
112 BuildMI(MBB, I, DL, TII->get(PPC::ADJCALLSTACKDOWN)).addImm(0)
113 .addImm(0);
Kyle Buttbfcff382016-01-08 02:06:19 +0000114
Bill Schmidt82f1c772015-02-10 19:09:05 +0000115 // Expand into two ops built prior to the existing instruction.
116 MachineInstr *Addi = BuildMI(MBB, I, DL, TII->get(Opc1), GPR3)
117 .addReg(InReg);
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000118 Addi->addOperand(MI.getOperand(2));
Bill Schmidt82f1c772015-02-10 19:09:05 +0000119
120 // The ADDItls* instruction is the first instruction in the
121 // repair range.
122 MachineBasicBlock::iterator First = I;
123 --First;
124
125 MachineInstr *Call = (BuildMI(MBB, I, DL, TII->get(Opc2), GPR3)
126 .addReg(GPR3));
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000127 Call->addOperand(MI.getOperand(3));
Bill Schmidt82f1c772015-02-10 19:09:05 +0000128
Hiroshi Inoue6989caa2017-06-29 14:13:38 +0000129 if (NeedFence)
130 BuildMI(MBB, I, DL, TII->get(PPC::ADJCALLSTACKUP)).addImm(0).addImm(0);
Kyle Buttbfcff382016-01-08 02:06:19 +0000131
Bill Schmidt82f1c772015-02-10 19:09:05 +0000132 BuildMI(MBB, I, DL, TII->get(TargetOpcode::COPY), OutReg)
133 .addReg(GPR3);
134
135 // The COPY is the last instruction in the repair range.
136 MachineBasicBlock::iterator Last = I;
137 --Last;
138
139 // Move past the original instruction and remove it.
140 ++I;
Duncan P. N. Exon Smithe5a22f42016-07-27 13:24:16 +0000141 MI.removeFromParent();
Bill Schmidt82f1c772015-02-10 19:09:05 +0000142
143 // Repair the live intervals.
144 LIS->repairIntervalsInRange(&MBB, First, Last, OrigRegs);
145 Changed = true;
146 }
147
148 return Changed;
149 }
150
151public:
152 bool runOnMachineFunction(MachineFunction &MF) override {
Eric Christopher1df0c512015-02-20 18:44:15 +0000153 TII = MF.getSubtarget<PPCSubtarget>().getInstrInfo();
Bill Schmidt82f1c772015-02-10 19:09:05 +0000154 LIS = &getAnalysis<LiveIntervals>();
155
156 bool Changed = false;
157
158 for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
159 MachineBasicBlock &B = *I++;
160 if (processBlock(B))
161 Changed = true;
162 }
163
164 return Changed;
165 }
166
167 void getAnalysisUsage(AnalysisUsage &AU) const override {
168 AU.addRequired<LiveIntervals>();
169 AU.addPreserved<LiveIntervals>();
170 AU.addRequired<SlotIndexes>();
171 AU.addPreserved<SlotIndexes>();
172 MachineFunctionPass::getAnalysisUsage(AU);
173 }
174 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000175}
Bill Schmidt82f1c772015-02-10 19:09:05 +0000176
177INITIALIZE_PASS_BEGIN(PPCTLSDynamicCall, DEBUG_TYPE,
178 "PowerPC TLS Dynamic Call Fixup", false, false)
179INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
180INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
181INITIALIZE_PASS_END(PPCTLSDynamicCall, DEBUG_TYPE,
182 "PowerPC TLS Dynamic Call Fixup", false, false)
183
184char PPCTLSDynamicCall::ID = 0;
185FunctionPass*
186llvm::createPPCTLSDynamicCallPass() { return new PPCTLSDynamicCall(); }