10b57cec5SDimitry Andric //===---------- PPCTLSDynamicCall.cpp - TLS Dynamic Call Fixup ------------===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric // 70b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 80b57cec5SDimitry Andric // 90b57cec5SDimitry Andric // This pass expands ADDItls{ld,gd}LADDR[32] machine instructions into 100b57cec5SDimitry Andric // separate ADDItls[gd]L[32] and GETtlsADDR[32] instructions, both of 110b57cec5SDimitry Andric // which define GPR3. A copy is added from GPR3 to the target virtual 120b57cec5SDimitry Andric // register of the original instruction. The GETtlsADDR[32] is really 130b57cec5SDimitry Andric // a call instruction, so its target register is constrained to be GPR3. 140b57cec5SDimitry Andric // This is not true of ADDItls[gd]L[32], but there is a legacy linker 150b57cec5SDimitry Andric // optimization bug that requires the target register of the addi of 160b57cec5SDimitry Andric // a local- or general-dynamic TLS access sequence to be GPR3. 170b57cec5SDimitry Andric // 180b57cec5SDimitry Andric // This is done in a late pass so that TLS variable accesses can be 190b57cec5SDimitry Andric // fully commoned by MachineCSE. 200b57cec5SDimitry Andric // 210b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 220b57cec5SDimitry Andric 230b57cec5SDimitry Andric #include "PPC.h" 240b57cec5SDimitry Andric #include "PPCInstrBuilder.h" 250b57cec5SDimitry Andric #include "PPCInstrInfo.h" 260b57cec5SDimitry Andric #include "PPCTargetMachine.h" 270b57cec5SDimitry Andric #include "llvm/CodeGen/LiveIntervals.h" 280b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h" 290b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h" 30480093f4SDimitry Andric #include "llvm/InitializePasses.h" 310b57cec5SDimitry Andric #include "llvm/Support/Debug.h" 320b57cec5SDimitry Andric #include "llvm/Support/raw_ostream.h" 330b57cec5SDimitry Andric 340b57cec5SDimitry Andric using namespace llvm; 350b57cec5SDimitry Andric 360b57cec5SDimitry Andric #define DEBUG_TYPE "ppc-tls-dynamic-call" 370b57cec5SDimitry Andric 380b57cec5SDimitry Andric namespace { 390b57cec5SDimitry Andric struct PPCTLSDynamicCall : public MachineFunctionPass { 400b57cec5SDimitry Andric static char ID; 410b57cec5SDimitry Andric PPCTLSDynamicCall() : MachineFunctionPass(ID) { 420b57cec5SDimitry Andric initializePPCTLSDynamicCallPass(*PassRegistry::getPassRegistry()); 430b57cec5SDimitry Andric } 440b57cec5SDimitry Andric 450b57cec5SDimitry Andric const PPCInstrInfo *TII; 460b57cec5SDimitry Andric 470b57cec5SDimitry Andric protected: 480b57cec5SDimitry Andric bool processBlock(MachineBasicBlock &MBB) { 490b57cec5SDimitry Andric bool Changed = false; 500b57cec5SDimitry Andric bool NeedFence = true; 510b57cec5SDimitry Andric bool Is64Bit = MBB.getParent()->getSubtarget<PPCSubtarget>().isPPC64(); 52fe6060f1SDimitry Andric bool IsAIX = MBB.getParent()->getSubtarget<PPCSubtarget>().isAIXABI(); 53e8d8bef9SDimitry Andric bool IsPCREL = false; 540b57cec5SDimitry Andric 550b57cec5SDimitry Andric for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end(); 560b57cec5SDimitry Andric I != IE;) { 570b57cec5SDimitry Andric MachineInstr &MI = *I; 58e8d8bef9SDimitry Andric IsPCREL = isPCREL(MI); 59*06c3fb27SDimitry Andric // There are a number of slight differences in code generation 60*06c3fb27SDimitry Andric // when we call .__get_tpointer (32-bit AIX TLS). 61*06c3fb27SDimitry Andric bool IsTLSTPRelMI = MI.getOpcode() == PPC::GETtlsTpointer32AIX; 620b57cec5SDimitry Andric 630b57cec5SDimitry Andric if (MI.getOpcode() != PPC::ADDItlsgdLADDR && 640b57cec5SDimitry Andric MI.getOpcode() != PPC::ADDItlsldLADDR && 650b57cec5SDimitry Andric MI.getOpcode() != PPC::ADDItlsgdLADDR32 && 66fe6060f1SDimitry Andric MI.getOpcode() != PPC::ADDItlsldLADDR32 && 67fe6060f1SDimitry Andric MI.getOpcode() != PPC::TLSGDAIX && 68*06c3fb27SDimitry Andric MI.getOpcode() != PPC::TLSGDAIX8 && !IsTLSTPRelMI && !IsPCREL) { 690b57cec5SDimitry Andric // Although we create ADJCALLSTACKDOWN and ADJCALLSTACKUP 700b57cec5SDimitry Andric // as scheduling fences, we skip creating fences if we already 710b57cec5SDimitry Andric // have existing ADJCALLSTACKDOWN/UP to avoid nesting, 720b57cec5SDimitry Andric // which causes verification error with -verify-machineinstrs. 730b57cec5SDimitry Andric if (MI.getOpcode() == PPC::ADJCALLSTACKDOWN) 740b57cec5SDimitry Andric NeedFence = false; 750b57cec5SDimitry Andric else if (MI.getOpcode() == PPC::ADJCALLSTACKUP) 760b57cec5SDimitry Andric NeedFence = true; 770b57cec5SDimitry Andric 780b57cec5SDimitry Andric ++I; 790b57cec5SDimitry Andric continue; 800b57cec5SDimitry Andric } 810b57cec5SDimitry Andric 820b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "TLS Dynamic Call Fixup:\n " << MI); 830b57cec5SDimitry Andric 848bcb0991SDimitry Andric Register OutReg = MI.getOperand(0).getReg(); 85e8d8bef9SDimitry Andric Register InReg = PPC::NoRegister; 865ffd83dbSDimitry Andric Register GPR3 = Is64Bit ? PPC::X3 : PPC::R3; 87fe6060f1SDimitry Andric Register GPR4 = Is64Bit ? PPC::X4 : PPC::R4; 88*06c3fb27SDimitry Andric if (!IsPCREL && !IsTLSTPRelMI) 89e8d8bef9SDimitry Andric InReg = MI.getOperand(1).getReg(); 90e8d8bef9SDimitry Andric DebugLoc DL = MI.getDebugLoc(); 910b57cec5SDimitry Andric 92e8d8bef9SDimitry Andric unsigned Opc1, Opc2; 930b57cec5SDimitry Andric switch (MI.getOpcode()) { 940b57cec5SDimitry Andric default: 950b57cec5SDimitry Andric llvm_unreachable("Opcode inconsistency error"); 960b57cec5SDimitry Andric case PPC::ADDItlsgdLADDR: 970b57cec5SDimitry Andric Opc1 = PPC::ADDItlsgdL; 980b57cec5SDimitry Andric Opc2 = PPC::GETtlsADDR; 990b57cec5SDimitry Andric break; 1000b57cec5SDimitry Andric case PPC::ADDItlsldLADDR: 1010b57cec5SDimitry Andric Opc1 = PPC::ADDItlsldL; 1020b57cec5SDimitry Andric Opc2 = PPC::GETtlsldADDR; 1030b57cec5SDimitry Andric break; 1040b57cec5SDimitry Andric case PPC::ADDItlsgdLADDR32: 1050b57cec5SDimitry Andric Opc1 = PPC::ADDItlsgdL32; 1060b57cec5SDimitry Andric Opc2 = PPC::GETtlsADDR32; 1070b57cec5SDimitry Andric break; 1080b57cec5SDimitry Andric case PPC::ADDItlsldLADDR32: 1090b57cec5SDimitry Andric Opc1 = PPC::ADDItlsldL32; 1100b57cec5SDimitry Andric Opc2 = PPC::GETtlsldADDR32; 1110b57cec5SDimitry Andric break; 112fe6060f1SDimitry Andric case PPC::TLSGDAIX8: 113fe6060f1SDimitry Andric // TLSGDAIX8 is expanded to two copies and GET_TLS_ADDR, so we only 114fe6060f1SDimitry Andric // set Opc2 here. 115fe6060f1SDimitry Andric Opc2 = PPC::GETtlsADDR64AIX; 116fe6060f1SDimitry Andric break; 117fe6060f1SDimitry Andric case PPC::TLSGDAIX: 118fe6060f1SDimitry Andric // TLSGDAIX is expanded to two copies and GET_TLS_ADDR, so we only 119fe6060f1SDimitry Andric // set Opc2 here. 120fe6060f1SDimitry Andric Opc2 = PPC::GETtlsADDR32AIX; 121fe6060f1SDimitry Andric break; 122*06c3fb27SDimitry Andric case PPC::GETtlsTpointer32AIX: 123*06c3fb27SDimitry Andric // GETtlsTpointer32AIX is expanded to a call to GET_TPOINTER on AIX 124*06c3fb27SDimitry Andric // 32-bit mode within PPCAsmPrinter. This instruction does not need 125*06c3fb27SDimitry Andric // to change, so Opc2 is set to the same instruction opcode. 126*06c3fb27SDimitry Andric Opc2 = PPC::GETtlsTpointer32AIX; 127*06c3fb27SDimitry Andric break; 128e8d8bef9SDimitry Andric case PPC::PADDI8pc: 129e8d8bef9SDimitry Andric assert(IsPCREL && "Expecting General/Local Dynamic PCRel"); 130e8d8bef9SDimitry Andric Opc1 = PPC::PADDI8pc; 131e8d8bef9SDimitry Andric Opc2 = MI.getOperand(2).getTargetFlags() == 132e8d8bef9SDimitry Andric PPCII::MO_GOT_TLSGD_PCREL_FLAG 133e8d8bef9SDimitry Andric ? PPC::GETtlsADDRPCREL 134e8d8bef9SDimitry Andric : PPC::GETtlsldADDRPCREL; 1350b57cec5SDimitry Andric } 1360b57cec5SDimitry Andric 1370b57cec5SDimitry Andric // We create ADJCALLSTACKUP and ADJCALLSTACKDOWN around _tls_get_addr 1380b57cec5SDimitry Andric // as scheduling fence to avoid it is scheduled before 1390b57cec5SDimitry Andric // mflr in the prologue and the address in LR is clobbered (PR25839). 1400b57cec5SDimitry Andric // We don't really need to save data to the stack - the clobbered 1410b57cec5SDimitry Andric // registers are already saved when the SDNode (e.g. PPCaddiTlsgdLAddr) 1420b57cec5SDimitry Andric // gets translated to the pseudo instruction (e.g. ADDItlsgdLADDR). 1430b57cec5SDimitry Andric if (NeedFence) 1440b57cec5SDimitry Andric BuildMI(MBB, I, DL, TII->get(PPC::ADJCALLSTACKDOWN)).addImm(0) 1450b57cec5SDimitry Andric .addImm(0); 1460b57cec5SDimitry Andric 147fe6060f1SDimitry Andric if (IsAIX) { 148fe6060f1SDimitry Andric // The variable offset and region handle are copied in r4 and r3. The 149fe6060f1SDimitry Andric // copies are followed by GETtlsADDR32AIX/GETtlsADDR64AIX. 150*06c3fb27SDimitry Andric if (!IsTLSTPRelMI) { 151fe6060f1SDimitry Andric BuildMI(MBB, I, DL, TII->get(TargetOpcode::COPY), GPR4) 152fe6060f1SDimitry Andric .addReg(MI.getOperand(1).getReg()); 153fe6060f1SDimitry Andric BuildMI(MBB, I, DL, TII->get(TargetOpcode::COPY), GPR3) 154fe6060f1SDimitry Andric .addReg(MI.getOperand(2).getReg()); 155fe6060f1SDimitry Andric BuildMI(MBB, I, DL, TII->get(Opc2), GPR3).addReg(GPR3).addReg(GPR4); 156*06c3fb27SDimitry Andric } else 157*06c3fb27SDimitry Andric // The opcode of GETtlsTpointer32AIX does not change, because later 158*06c3fb27SDimitry Andric // this instruction will be expanded into a call to .__get_tpointer, 159*06c3fb27SDimitry Andric // which will return the thread pointer into r3. 160*06c3fb27SDimitry Andric BuildMI(MBB, I, DL, TII->get(Opc2), GPR3); 161fe6060f1SDimitry Andric } else { 162e8d8bef9SDimitry Andric MachineInstr *Addi; 163e8d8bef9SDimitry Andric if (IsPCREL) { 164e8d8bef9SDimitry Andric Addi = BuildMI(MBB, I, DL, TII->get(Opc1), GPR3).addImm(0); 165e8d8bef9SDimitry Andric } else { 1660b57cec5SDimitry Andric // Expand into two ops built prior to the existing instruction. 167e8d8bef9SDimitry Andric assert(InReg != PPC::NoRegister && "Operand must be a register"); 168e8d8bef9SDimitry Andric Addi = BuildMI(MBB, I, DL, TII->get(Opc1), GPR3).addReg(InReg); 169e8d8bef9SDimitry Andric } 170e8d8bef9SDimitry Andric 1710b57cec5SDimitry Andric Addi->addOperand(MI.getOperand(2)); 1720b57cec5SDimitry Andric 173fe6060f1SDimitry Andric MachineInstr *Call = 174fe6060f1SDimitry Andric (BuildMI(MBB, I, DL, TII->get(Opc2), GPR3).addReg(GPR3)); 175e8d8bef9SDimitry Andric if (IsPCREL) 176e8d8bef9SDimitry Andric Call->addOperand(MI.getOperand(2)); 177e8d8bef9SDimitry Andric else 1780b57cec5SDimitry Andric Call->addOperand(MI.getOperand(3)); 179fe6060f1SDimitry Andric } 1800b57cec5SDimitry Andric if (NeedFence) 1810b57cec5SDimitry Andric BuildMI(MBB, I, DL, TII->get(PPC::ADJCALLSTACKUP)).addImm(0).addImm(0); 1820b57cec5SDimitry Andric 1830b57cec5SDimitry Andric BuildMI(MBB, I, DL, TII->get(TargetOpcode::COPY), OutReg) 1840b57cec5SDimitry Andric .addReg(GPR3); 1850b57cec5SDimitry Andric 1860b57cec5SDimitry Andric // Move past the original instruction and remove it. 1870b57cec5SDimitry Andric ++I; 1880b57cec5SDimitry Andric MI.removeFromParent(); 1890b57cec5SDimitry Andric 1900b57cec5SDimitry Andric Changed = true; 1910b57cec5SDimitry Andric } 1920b57cec5SDimitry Andric 1930b57cec5SDimitry Andric return Changed; 1940b57cec5SDimitry Andric } 1950b57cec5SDimitry Andric 1960b57cec5SDimitry Andric public: 197e8d8bef9SDimitry Andric bool isPCREL(const MachineInstr &MI) { 198e8d8bef9SDimitry Andric return (MI.getOpcode() == PPC::PADDI8pc) && 199e8d8bef9SDimitry Andric (MI.getOperand(2).getTargetFlags() == 200e8d8bef9SDimitry Andric PPCII::MO_GOT_TLSGD_PCREL_FLAG || 201e8d8bef9SDimitry Andric MI.getOperand(2).getTargetFlags() == 202e8d8bef9SDimitry Andric PPCII::MO_GOT_TLSLD_PCREL_FLAG); 203e8d8bef9SDimitry Andric } 204e8d8bef9SDimitry Andric 2050b57cec5SDimitry Andric bool runOnMachineFunction(MachineFunction &MF) override { 2060b57cec5SDimitry Andric TII = MF.getSubtarget<PPCSubtarget>().getInstrInfo(); 2070b57cec5SDimitry Andric 2080b57cec5SDimitry Andric bool Changed = false; 2090b57cec5SDimitry Andric 210349cc55cSDimitry Andric for (MachineBasicBlock &B : llvm::make_early_inc_range(MF)) 2110b57cec5SDimitry Andric if (processBlock(B)) 2120b57cec5SDimitry Andric Changed = true; 2130b57cec5SDimitry Andric 2140b57cec5SDimitry Andric return Changed; 2150b57cec5SDimitry Andric } 2160b57cec5SDimitry Andric 2170b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override { 2180b57cec5SDimitry Andric AU.addRequired<LiveIntervals>(); 2190b57cec5SDimitry Andric AU.addRequired<SlotIndexes>(); 2200b57cec5SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU); 2210b57cec5SDimitry Andric } 2220b57cec5SDimitry Andric }; 2230b57cec5SDimitry Andric } 2240b57cec5SDimitry Andric 2250b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(PPCTLSDynamicCall, DEBUG_TYPE, 2260b57cec5SDimitry Andric "PowerPC TLS Dynamic Call Fixup", false, false) 2270b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(LiveIntervals) 2280b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(SlotIndexes) 2290b57cec5SDimitry Andric INITIALIZE_PASS_END(PPCTLSDynamicCall, DEBUG_TYPE, 2300b57cec5SDimitry Andric "PowerPC TLS Dynamic Call Fixup", false, false) 2310b57cec5SDimitry Andric 2320b57cec5SDimitry Andric char PPCTLSDynamicCall::ID = 0; 2330b57cec5SDimitry Andric FunctionPass* 2340b57cec5SDimitry Andric llvm::createPPCTLSDynamicCallPass() { return new PPCTLSDynamicCall(); } 235