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 LiveIntervals *LIS; 470b57cec5SDimitry Andric 480b57cec5SDimitry Andric protected: 490b57cec5SDimitry Andric bool processBlock(MachineBasicBlock &MBB) { 500b57cec5SDimitry Andric bool Changed = false; 510b57cec5SDimitry Andric bool NeedFence = true; 520b57cec5SDimitry Andric bool Is64Bit = MBB.getParent()->getSubtarget<PPCSubtarget>().isPPC64(); 53fe6060f1SDimitry Andric bool IsAIX = MBB.getParent()->getSubtarget<PPCSubtarget>().isAIXABI(); 54e8d8bef9SDimitry Andric bool IsPCREL = false; 550b57cec5SDimitry Andric 560b57cec5SDimitry Andric for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end(); 570b57cec5SDimitry Andric I != IE;) { 580b57cec5SDimitry Andric MachineInstr &MI = *I; 59e8d8bef9SDimitry Andric IsPCREL = isPCREL(MI); 600b57cec5SDimitry Andric 610b57cec5SDimitry Andric if (MI.getOpcode() != PPC::ADDItlsgdLADDR && 620b57cec5SDimitry Andric MI.getOpcode() != PPC::ADDItlsldLADDR && 630b57cec5SDimitry Andric MI.getOpcode() != PPC::ADDItlsgdLADDR32 && 64fe6060f1SDimitry Andric MI.getOpcode() != PPC::ADDItlsldLADDR32 && 65fe6060f1SDimitry Andric MI.getOpcode() != PPC::TLSGDAIX && 66fe6060f1SDimitry Andric MI.getOpcode() != PPC::TLSGDAIX8 && !IsPCREL) { 670b57cec5SDimitry Andric // Although we create ADJCALLSTACKDOWN and ADJCALLSTACKUP 680b57cec5SDimitry Andric // as scheduling fences, we skip creating fences if we already 690b57cec5SDimitry Andric // have existing ADJCALLSTACKDOWN/UP to avoid nesting, 700b57cec5SDimitry Andric // which causes verification error with -verify-machineinstrs. 710b57cec5SDimitry Andric if (MI.getOpcode() == PPC::ADJCALLSTACKDOWN) 720b57cec5SDimitry Andric NeedFence = false; 730b57cec5SDimitry Andric else if (MI.getOpcode() == PPC::ADJCALLSTACKUP) 740b57cec5SDimitry Andric NeedFence = true; 750b57cec5SDimitry Andric 760b57cec5SDimitry Andric ++I; 770b57cec5SDimitry Andric continue; 780b57cec5SDimitry Andric } 790b57cec5SDimitry Andric 800b57cec5SDimitry Andric LLVM_DEBUG(dbgs() << "TLS Dynamic Call Fixup:\n " << MI); 810b57cec5SDimitry Andric 828bcb0991SDimitry Andric Register OutReg = MI.getOperand(0).getReg(); 83e8d8bef9SDimitry Andric Register InReg = PPC::NoRegister; 845ffd83dbSDimitry Andric Register GPR3 = Is64Bit ? PPC::X3 : PPC::R3; 85fe6060f1SDimitry Andric Register GPR4 = Is64Bit ? PPC::X4 : PPC::R4; 86e8d8bef9SDimitry Andric SmallVector<Register, 3> OrigRegs = {OutReg, GPR3}; 87e8d8bef9SDimitry Andric if (!IsPCREL) { 88e8d8bef9SDimitry Andric InReg = MI.getOperand(1).getReg(); 89e8d8bef9SDimitry Andric OrigRegs.push_back(InReg); 90e8d8bef9SDimitry Andric } 91e8d8bef9SDimitry Andric DebugLoc DL = MI.getDebugLoc(); 920b57cec5SDimitry Andric 93e8d8bef9SDimitry Andric unsigned Opc1, Opc2; 940b57cec5SDimitry Andric switch (MI.getOpcode()) { 950b57cec5SDimitry Andric default: 960b57cec5SDimitry Andric llvm_unreachable("Opcode inconsistency error"); 970b57cec5SDimitry Andric case PPC::ADDItlsgdLADDR: 980b57cec5SDimitry Andric Opc1 = PPC::ADDItlsgdL; 990b57cec5SDimitry Andric Opc2 = PPC::GETtlsADDR; 1000b57cec5SDimitry Andric break; 1010b57cec5SDimitry Andric case PPC::ADDItlsldLADDR: 1020b57cec5SDimitry Andric Opc1 = PPC::ADDItlsldL; 1030b57cec5SDimitry Andric Opc2 = PPC::GETtlsldADDR; 1040b57cec5SDimitry Andric break; 1050b57cec5SDimitry Andric case PPC::ADDItlsgdLADDR32: 1060b57cec5SDimitry Andric Opc1 = PPC::ADDItlsgdL32; 1070b57cec5SDimitry Andric Opc2 = PPC::GETtlsADDR32; 1080b57cec5SDimitry Andric break; 1090b57cec5SDimitry Andric case PPC::ADDItlsldLADDR32: 1100b57cec5SDimitry Andric Opc1 = PPC::ADDItlsldL32; 1110b57cec5SDimitry Andric Opc2 = PPC::GETtlsldADDR32; 1120b57cec5SDimitry Andric break; 113fe6060f1SDimitry Andric case PPC::TLSGDAIX8: 114fe6060f1SDimitry Andric // TLSGDAIX8 is expanded to two copies and GET_TLS_ADDR, so we only 115fe6060f1SDimitry Andric // set Opc2 here. 116fe6060f1SDimitry Andric Opc2 = PPC::GETtlsADDR64AIX; 117fe6060f1SDimitry Andric break; 118fe6060f1SDimitry Andric case PPC::TLSGDAIX: 119fe6060f1SDimitry Andric // TLSGDAIX is expanded to two copies and GET_TLS_ADDR, so we only 120fe6060f1SDimitry Andric // set Opc2 here. 121fe6060f1SDimitry Andric Opc2 = PPC::GETtlsADDR32AIX; 122fe6060f1SDimitry Andric break; 123e8d8bef9SDimitry Andric case PPC::PADDI8pc: 124e8d8bef9SDimitry Andric assert(IsPCREL && "Expecting General/Local Dynamic PCRel"); 125e8d8bef9SDimitry Andric Opc1 = PPC::PADDI8pc; 126e8d8bef9SDimitry Andric Opc2 = MI.getOperand(2).getTargetFlags() == 127e8d8bef9SDimitry Andric PPCII::MO_GOT_TLSGD_PCREL_FLAG 128e8d8bef9SDimitry Andric ? PPC::GETtlsADDRPCREL 129e8d8bef9SDimitry Andric : PPC::GETtlsldADDRPCREL; 1300b57cec5SDimitry Andric } 1310b57cec5SDimitry Andric 1320b57cec5SDimitry Andric // We create ADJCALLSTACKUP and ADJCALLSTACKDOWN around _tls_get_addr 1330b57cec5SDimitry Andric // as scheduling fence to avoid it is scheduled before 1340b57cec5SDimitry Andric // mflr in the prologue and the address in LR is clobbered (PR25839). 1350b57cec5SDimitry Andric // We don't really need to save data to the stack - the clobbered 1360b57cec5SDimitry Andric // registers are already saved when the SDNode (e.g. PPCaddiTlsgdLAddr) 1370b57cec5SDimitry Andric // gets translated to the pseudo instruction (e.g. ADDItlsgdLADDR). 1380b57cec5SDimitry Andric if (NeedFence) 1390b57cec5SDimitry Andric BuildMI(MBB, I, DL, TII->get(PPC::ADJCALLSTACKDOWN)).addImm(0) 1400b57cec5SDimitry Andric .addImm(0); 1410b57cec5SDimitry Andric 142fe6060f1SDimitry Andric // The ADDItls* instruction is the first instruction in the 143fe6060f1SDimitry Andric // repair range. 144fe6060f1SDimitry Andric MachineBasicBlock::iterator First = I; 145fe6060f1SDimitry Andric --First; 146fe6060f1SDimitry 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. 150fe6060f1SDimitry Andric BuildMI(MBB, I, DL, TII->get(TargetOpcode::COPY), GPR4) 151fe6060f1SDimitry Andric .addReg(MI.getOperand(1).getReg()); 152fe6060f1SDimitry Andric BuildMI(MBB, I, DL, TII->get(TargetOpcode::COPY), GPR3) 153fe6060f1SDimitry Andric .addReg(MI.getOperand(2).getReg()); 154fe6060f1SDimitry Andric BuildMI(MBB, I, DL, TII->get(Opc2), GPR3).addReg(GPR3).addReg(GPR4); 155fe6060f1SDimitry Andric } else { 156e8d8bef9SDimitry Andric MachineInstr *Addi; 157e8d8bef9SDimitry Andric if (IsPCREL) { 158e8d8bef9SDimitry Andric Addi = BuildMI(MBB, I, DL, TII->get(Opc1), GPR3).addImm(0); 159e8d8bef9SDimitry Andric } else { 1600b57cec5SDimitry Andric // Expand into two ops built prior to the existing instruction. 161e8d8bef9SDimitry Andric assert(InReg != PPC::NoRegister && "Operand must be a register"); 162e8d8bef9SDimitry Andric Addi = BuildMI(MBB, I, DL, TII->get(Opc1), GPR3).addReg(InReg); 163e8d8bef9SDimitry Andric } 164e8d8bef9SDimitry Andric 1650b57cec5SDimitry Andric Addi->addOperand(MI.getOperand(2)); 1660b57cec5SDimitry Andric 167fe6060f1SDimitry Andric MachineInstr *Call = 168fe6060f1SDimitry Andric (BuildMI(MBB, I, DL, TII->get(Opc2), GPR3).addReg(GPR3)); 169e8d8bef9SDimitry Andric if (IsPCREL) 170e8d8bef9SDimitry Andric Call->addOperand(MI.getOperand(2)); 171e8d8bef9SDimitry Andric else 1720b57cec5SDimitry Andric Call->addOperand(MI.getOperand(3)); 173fe6060f1SDimitry Andric } 1740b57cec5SDimitry Andric if (NeedFence) 1750b57cec5SDimitry Andric BuildMI(MBB, I, DL, TII->get(PPC::ADJCALLSTACKUP)).addImm(0).addImm(0); 1760b57cec5SDimitry Andric 1770b57cec5SDimitry Andric BuildMI(MBB, I, DL, TII->get(TargetOpcode::COPY), OutReg) 1780b57cec5SDimitry Andric .addReg(GPR3); 1790b57cec5SDimitry Andric 1800b57cec5SDimitry Andric // The COPY is the last instruction in the repair range. 1810b57cec5SDimitry Andric MachineBasicBlock::iterator Last = I; 1820b57cec5SDimitry Andric --Last; 1830b57cec5SDimitry Andric 1840b57cec5SDimitry Andric // Move past the original instruction and remove it. 1850b57cec5SDimitry Andric ++I; 1860b57cec5SDimitry Andric MI.removeFromParent(); 1870b57cec5SDimitry Andric 1880b57cec5SDimitry Andric // Repair the live intervals. 1890b57cec5SDimitry Andric LIS->repairIntervalsInRange(&MBB, First, Last, OrigRegs); 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 LIS = &getAnalysis<LiveIntervals>(); 2080b57cec5SDimitry Andric 2090b57cec5SDimitry Andric bool Changed = false; 2100b57cec5SDimitry Andric 211*349cc55cSDimitry Andric for (MachineBasicBlock &B : llvm::make_early_inc_range(MF)) 2120b57cec5SDimitry Andric if (processBlock(B)) 2130b57cec5SDimitry Andric Changed = true; 2140b57cec5SDimitry Andric 2150b57cec5SDimitry Andric return Changed; 2160b57cec5SDimitry Andric } 2170b57cec5SDimitry Andric 2180b57cec5SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override { 2190b57cec5SDimitry Andric AU.addRequired<LiveIntervals>(); 2200b57cec5SDimitry Andric AU.addPreserved<LiveIntervals>(); 2210b57cec5SDimitry Andric AU.addRequired<SlotIndexes>(); 2220b57cec5SDimitry Andric AU.addPreserved<SlotIndexes>(); 2230b57cec5SDimitry Andric MachineFunctionPass::getAnalysisUsage(AU); 2240b57cec5SDimitry Andric } 2250b57cec5SDimitry Andric }; 2260b57cec5SDimitry Andric } 2270b57cec5SDimitry Andric 2280b57cec5SDimitry Andric INITIALIZE_PASS_BEGIN(PPCTLSDynamicCall, DEBUG_TYPE, 2290b57cec5SDimitry Andric "PowerPC TLS Dynamic Call Fixup", false, false) 2300b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(LiveIntervals) 2310b57cec5SDimitry Andric INITIALIZE_PASS_DEPENDENCY(SlotIndexes) 2320b57cec5SDimitry Andric INITIALIZE_PASS_END(PPCTLSDynamicCall, DEBUG_TYPE, 2330b57cec5SDimitry Andric "PowerPC TLS Dynamic Call Fixup", false, false) 2340b57cec5SDimitry Andric 2350b57cec5SDimitry Andric char PPCTLSDynamicCall::ID = 0; 2360b57cec5SDimitry Andric FunctionPass* 2370b57cec5SDimitry Andric llvm::createPPCTLSDynamicCallPass() { return new PPCTLSDynamicCall(); } 238