182f1c775SBill Schmidt //===---------- PPCTLSDynamicCall.cpp - TLS Dynamic Call Fixup ------------===//
282f1c775SBill Schmidt //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
682f1c775SBill Schmidt //
782f1c775SBill Schmidt //===----------------------------------------------------------------------===//
882f1c775SBill Schmidt //
982f1c775SBill Schmidt // This pass expands ADDItls{ld,gd}LADDR[32] machine instructions into
1082f1c775SBill Schmidt // separate ADDItls[gd]L[32] and GETtlsADDR[32] instructions, both of
1182f1c775SBill Schmidt // which define GPR3.  A copy is added from GPR3 to the target virtual
1282f1c775SBill Schmidt // register of the original instruction.  The GETtlsADDR[32] is really
1382f1c775SBill Schmidt // a call instruction, so its target register is constrained to be GPR3.
1482f1c775SBill Schmidt // This is not true of ADDItls[gd]L[32], but there is a legacy linker
1582f1c775SBill Schmidt // optimization bug that requires the target register of the addi of
1682f1c775SBill Schmidt // a local- or general-dynamic TLS access sequence to be GPR3.
1782f1c775SBill Schmidt //
1882f1c775SBill Schmidt // This is done in a late pass so that TLS variable accesses can be
1982f1c775SBill Schmidt // fully commoned by MachineCSE.
2082f1c775SBill Schmidt //
2182f1c775SBill Schmidt //===----------------------------------------------------------------------===//
2282f1c775SBill Schmidt 
2382f1c775SBill Schmidt #include "PPC.h"
2482f1c775SBill Schmidt #include "PPCInstrBuilder.h"
256bda14b3SChandler Carruth #include "PPCInstrInfo.h"
2682f1c775SBill Schmidt #include "PPCTargetMachine.h"
27f842297dSMatthias Braun #include "llvm/CodeGen/LiveIntervals.h"
2882f1c775SBill Schmidt #include "llvm/CodeGen/MachineFunctionPass.h"
2982f1c775SBill Schmidt #include "llvm/CodeGen/MachineInstrBuilder.h"
3005da2fe5SReid Kleckner #include "llvm/InitializePasses.h"
3182f1c775SBill Schmidt #include "llvm/Support/Debug.h"
3282f1c775SBill Schmidt #include "llvm/Support/raw_ostream.h"
3382f1c775SBill Schmidt 
3482f1c775SBill Schmidt using namespace llvm;
3582f1c775SBill Schmidt 
3682f1c775SBill Schmidt #define DEBUG_TYPE "ppc-tls-dynamic-call"
3782f1c775SBill Schmidt 
3882f1c775SBill Schmidt namespace {
3982f1c775SBill Schmidt   struct PPCTLSDynamicCall : public MachineFunctionPass {
4082f1c775SBill Schmidt     static char ID;
PPCTLSDynamicCall__anona980151c0111::PPCTLSDynamicCall4182f1c775SBill Schmidt     PPCTLSDynamicCall() : MachineFunctionPass(ID) {
4282f1c775SBill Schmidt       initializePPCTLSDynamicCallPass(*PassRegistry::getPassRegistry());
4382f1c775SBill Schmidt     }
4482f1c775SBill Schmidt 
4582f1c775SBill Schmidt     const PPCInstrInfo *TII;
4682f1c775SBill Schmidt 
4782f1c775SBill Schmidt protected:
processBlock__anona980151c0111::PPCTLSDynamicCall4882f1c775SBill Schmidt     bool processBlock(MachineBasicBlock &MBB) {
4982f1c775SBill Schmidt       bool Changed = false;
506989caa9SHiroshi Inoue       bool NeedFence = true;
511df0c519SEric Christopher       bool Is64Bit = MBB.getParent()->getSubtarget<PPCSubtarget>().isPPC64();
52b0f01153SNemanja Ivanovic       bool IsAIX = MBB.getParent()->getSubtarget<PPCSubtarget>().isAIXABI();
53652a8f15SVictor Huang       bool IsPCREL = false;
5482f1c775SBill Schmidt 
5582f1c775SBill Schmidt       for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
5682e1fc5fSHal Finkel            I != IE;) {
57e5a22f44SDuncan P. N. Exon Smith         MachineInstr &MI = *I;
58652a8f15SVictor Huang         IsPCREL = isPCREL(MI);
5982f1c775SBill Schmidt 
60e5a22f44SDuncan P. N. Exon Smith         if (MI.getOpcode() != PPC::ADDItlsgdLADDR &&
61e5a22f44SDuncan P. N. Exon Smith             MI.getOpcode() != PPC::ADDItlsldLADDR &&
62e5a22f44SDuncan P. N. Exon Smith             MI.getOpcode() != PPC::ADDItlsgdLADDR32 &&
63b0f01153SNemanja Ivanovic             MI.getOpcode() != PPC::ADDItlsldLADDR32 &&
64535a4192SLei Huang             MI.getOpcode() != PPC::TLSGDAIX &&
65535a4192SLei Huang             MI.getOpcode() != PPC::TLSGDAIX8 && !IsPCREL) {
666989caa9SHiroshi Inoue           // Although we create ADJCALLSTACKDOWN and ADJCALLSTACKUP
676989caa9SHiroshi Inoue           // as scheduling fences, we skip creating fences if we already
686989caa9SHiroshi Inoue           // have existing ADJCALLSTACKDOWN/UP to avoid nesting,
696989caa9SHiroshi Inoue           // which causes verification error with -verify-machineinstrs.
706989caa9SHiroshi Inoue           if (MI.getOpcode() == PPC::ADJCALLSTACKDOWN)
716989caa9SHiroshi Inoue             NeedFence = false;
726989caa9SHiroshi Inoue           else if (MI.getOpcode() == PPC::ADJCALLSTACKUP)
736989caa9SHiroshi Inoue             NeedFence = true;
746989caa9SHiroshi Inoue 
7582e1fc5fSHal Finkel           ++I;
7682f1c775SBill Schmidt           continue;
7782e1fc5fSHal Finkel         }
7882f1c775SBill Schmidt 
79d34e60caSNicola Zaghen         LLVM_DEBUG(dbgs() << "TLS Dynamic Call Fixup:\n    " << MI);
8082f1c775SBill Schmidt 
810c476111SDaniel Sanders         Register OutReg = MI.getOperand(0).getReg();
82b74b80bbSKamau Bridgeman         Register InReg = PPC::NoRegister;
83ca0ace72SMatt Arsenault         Register GPR3 = Is64Bit ? PPC::X3 : PPC::R3;
84b0f01153SNemanja Ivanovic         Register GPR4 = Is64Bit ? PPC::X4 : PPC::R4;
85*3ff319c6SJay Foad         if (!IsPCREL)
86b74b80bbSKamau Bridgeman           InReg = MI.getOperand(1).getReg();
87b74b80bbSKamau Bridgeman         DebugLoc DL = MI.getDebugLoc();
8882f1c775SBill Schmidt 
89b74b80bbSKamau Bridgeman         unsigned Opc1, Opc2;
90e5a22f44SDuncan P. N. Exon Smith         switch (MI.getOpcode()) {
9182f1c775SBill Schmidt         default:
9282f1c775SBill Schmidt           llvm_unreachable("Opcode inconsistency error");
9382f1c775SBill Schmidt         case PPC::ADDItlsgdLADDR:
9482f1c775SBill Schmidt           Opc1 = PPC::ADDItlsgdL;
9582f1c775SBill Schmidt           Opc2 = PPC::GETtlsADDR;
9682f1c775SBill Schmidt           break;
9782f1c775SBill Schmidt         case PPC::ADDItlsldLADDR:
9882f1c775SBill Schmidt           Opc1 = PPC::ADDItlsldL;
9982f1c775SBill Schmidt           Opc2 = PPC::GETtlsldADDR;
10082f1c775SBill Schmidt           break;
10182f1c775SBill Schmidt         case PPC::ADDItlsgdLADDR32:
10282f1c775SBill Schmidt           Opc1 = PPC::ADDItlsgdL32;
10382f1c775SBill Schmidt           Opc2 = PPC::GETtlsADDR32;
10482f1c775SBill Schmidt           break;
10582f1c775SBill Schmidt         case PPC::ADDItlsldLADDR32:
10682f1c775SBill Schmidt           Opc1 = PPC::ADDItlsldL32;
10782f1c775SBill Schmidt           Opc2 = PPC::GETtlsldADDR32;
10882f1c775SBill Schmidt           break;
109535a4192SLei Huang         case PPC::TLSGDAIX8:
110535a4192SLei Huang           // TLSGDAIX8 is expanded to two copies and GET_TLS_ADDR, so we only
111535a4192SLei Huang           // set Opc2 here.
112535a4192SLei Huang           Opc2 = PPC::GETtlsADDR64AIX;
113535a4192SLei Huang           break;
114b0f01153SNemanja Ivanovic         case PPC::TLSGDAIX:
115b0f01153SNemanja Ivanovic           // TLSGDAIX is expanded to two copies and GET_TLS_ADDR, so we only
116b0f01153SNemanja Ivanovic           // set Opc2 here.
117b0f01153SNemanja Ivanovic           Opc2 = PPC::GETtlsADDR32AIX;
118b0f01153SNemanja Ivanovic           break;
119b74b80bbSKamau Bridgeman         case PPC::PADDI8pc:
120652a8f15SVictor Huang           assert(IsPCREL && "Expecting General/Local Dynamic PCRel");
121b74b80bbSKamau Bridgeman           Opc1 = PPC::PADDI8pc;
122652a8f15SVictor Huang           Opc2 = MI.getOperand(2).getTargetFlags() ==
123652a8f15SVictor Huang                          PPCII::MO_GOT_TLSGD_PCREL_FLAG
1241f5c4a0dSVictor Huang                      ? PPC::GETtlsADDRPCREL
1251f5c4a0dSVictor Huang                      : PPC::GETtlsldADDRPCREL;
12682f1c775SBill Schmidt         }
12782f1c775SBill Schmidt 
1286989caa9SHiroshi Inoue         // We create ADJCALLSTACKUP and ADJCALLSTACKDOWN around _tls_get_addr
1290f7f59f0SHiroshi Inoue         // as scheduling fence to avoid it is scheduled before
1306989caa9SHiroshi Inoue         // mflr in the prologue and the address in LR is clobbered (PR25839).
1316989caa9SHiroshi Inoue         // We don't really need to save data to the stack - the clobbered
132bfcff385SKyle Butt         // registers are already saved when the SDNode (e.g. PPCaddiTlsgdLAddr)
133bfcff385SKyle Butt         // gets translated to the pseudo instruction (e.g. ADDItlsgdLADDR).
1346989caa9SHiroshi Inoue         if (NeedFence)
135d526b13eSSerge Pavlov           BuildMI(MBB, I, DL, TII->get(PPC::ADJCALLSTACKDOWN)).addImm(0)
136d526b13eSSerge Pavlov                                                               .addImm(0);
137bfcff385SKyle Butt 
138b0f01153SNemanja Ivanovic         if (IsAIX) {
139b0f01153SNemanja Ivanovic           // The variable offset and region handle are copied in r4 and r3. The
140535a4192SLei Huang           // copies are followed by GETtlsADDR32AIX/GETtlsADDR64AIX.
141b0f01153SNemanja Ivanovic           BuildMI(MBB, I, DL, TII->get(TargetOpcode::COPY), GPR4)
142b0f01153SNemanja Ivanovic               .addReg(MI.getOperand(1).getReg());
143b0f01153SNemanja Ivanovic           BuildMI(MBB, I, DL, TII->get(TargetOpcode::COPY), GPR3)
144b0f01153SNemanja Ivanovic               .addReg(MI.getOperand(2).getReg());
145b0f01153SNemanja Ivanovic           BuildMI(MBB, I, DL, TII->get(Opc2), GPR3).addReg(GPR3).addReg(GPR4);
146b0f01153SNemanja Ivanovic         } else {
147b74b80bbSKamau Bridgeman           MachineInstr *Addi;
148652a8f15SVictor Huang           if (IsPCREL) {
149b74b80bbSKamau Bridgeman             Addi = BuildMI(MBB, I, DL, TII->get(Opc1), GPR3).addImm(0);
150b74b80bbSKamau Bridgeman           } else {
15182f1c775SBill Schmidt             // Expand into two ops built prior to the existing instruction.
152b74b80bbSKamau Bridgeman             assert(InReg != PPC::NoRegister && "Operand must be a register");
153b74b80bbSKamau Bridgeman             Addi = BuildMI(MBB, I, DL, TII->get(Opc1), GPR3).addReg(InReg);
154b74b80bbSKamau Bridgeman           }
155b74b80bbSKamau Bridgeman 
156e5a22f44SDuncan P. N. Exon Smith           Addi->addOperand(MI.getOperand(2));
15782f1c775SBill Schmidt 
158b0f01153SNemanja Ivanovic           MachineInstr *Call =
159b0f01153SNemanja Ivanovic               (BuildMI(MBB, I, DL, TII->get(Opc2), GPR3).addReg(GPR3));
160652a8f15SVictor Huang           if (IsPCREL)
161b74b80bbSKamau Bridgeman             Call->addOperand(MI.getOperand(2));
162b74b80bbSKamau Bridgeman           else
163e5a22f44SDuncan P. N. Exon Smith             Call->addOperand(MI.getOperand(3));
164b0f01153SNemanja Ivanovic         }
1656989caa9SHiroshi Inoue         if (NeedFence)
166bfcff385SKyle Butt           BuildMI(MBB, I, DL, TII->get(PPC::ADJCALLSTACKUP)).addImm(0).addImm(0);
167bfcff385SKyle Butt 
16882f1c775SBill Schmidt         BuildMI(MBB, I, DL, TII->get(TargetOpcode::COPY), OutReg)
16982f1c775SBill Schmidt           .addReg(GPR3);
17082f1c775SBill Schmidt 
17182f1c775SBill Schmidt         // Move past the original instruction and remove it.
17282f1c775SBill Schmidt         ++I;
173e5a22f44SDuncan P. N. Exon Smith         MI.removeFromParent();
17482f1c775SBill Schmidt 
17582f1c775SBill Schmidt         Changed = true;
17682f1c775SBill Schmidt       }
17782f1c775SBill Schmidt 
17882f1c775SBill Schmidt       return Changed;
17982f1c775SBill Schmidt     }
18082f1c775SBill Schmidt 
18182f1c775SBill Schmidt public:
isPCREL__anona980151c0111::PPCTLSDynamicCall182652a8f15SVictor Huang   bool isPCREL(const MachineInstr &MI) {
183b74b80bbSKamau Bridgeman     return (MI.getOpcode() == PPC::PADDI8pc) &&
184b74b80bbSKamau Bridgeman            (MI.getOperand(2).getTargetFlags() ==
185652a8f15SVictor Huang                 PPCII::MO_GOT_TLSGD_PCREL_FLAG ||
186652a8f15SVictor Huang             MI.getOperand(2).getTargetFlags() ==
187652a8f15SVictor Huang                 PPCII::MO_GOT_TLSLD_PCREL_FLAG);
188b74b80bbSKamau Bridgeman   }
189652a8f15SVictor Huang 
runOnMachineFunction__anona980151c0111::PPCTLSDynamicCall19082f1c775SBill Schmidt     bool runOnMachineFunction(MachineFunction &MF) override {
1911df0c519SEric Christopher       TII = MF.getSubtarget<PPCSubtarget>().getInstrInfo();
19282f1c775SBill Schmidt 
19382f1c775SBill Schmidt       bool Changed = false;
19482f1c775SBill Schmidt 
1952c4ba3e9SKazu Hirata       for (MachineBasicBlock &B : llvm::make_early_inc_range(MF))
19682f1c775SBill Schmidt         if (processBlock(B))
19782f1c775SBill Schmidt           Changed = true;
19882f1c775SBill Schmidt 
19982f1c775SBill Schmidt       return Changed;
20082f1c775SBill Schmidt     }
20182f1c775SBill Schmidt 
getAnalysisUsage__anona980151c0111::PPCTLSDynamicCall20282f1c775SBill Schmidt     void getAnalysisUsage(AnalysisUsage &AU) const override {
20382f1c775SBill Schmidt       AU.addRequired<LiveIntervals>();
20482f1c775SBill Schmidt       AU.addRequired<SlotIndexes>();
20582f1c775SBill Schmidt       MachineFunctionPass::getAnalysisUsage(AU);
20682f1c775SBill Schmidt     }
20782f1c775SBill Schmidt   };
208f00654e3SAlexander Kornienko }
20982f1c775SBill Schmidt 
21082f1c775SBill Schmidt INITIALIZE_PASS_BEGIN(PPCTLSDynamicCall, DEBUG_TYPE,
21182f1c775SBill Schmidt                       "PowerPC TLS Dynamic Call Fixup", false, false)
21282f1c775SBill Schmidt INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
21382f1c775SBill Schmidt INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
21482f1c775SBill Schmidt INITIALIZE_PASS_END(PPCTLSDynamicCall, DEBUG_TYPE,
21582f1c775SBill Schmidt                     "PowerPC TLS Dynamic Call Fixup", false, false)
21682f1c775SBill Schmidt 
21782f1c775SBill Schmidt char PPCTLSDynamicCall::ID = 0;
21882f1c775SBill Schmidt FunctionPass*
createPPCTLSDynamicCallPass()21982f1c775SBill Schmidt llvm::createPPCTLSDynamicCallPass() { return new PPCTLSDynamicCall(); }
220