1 //===---------- PPCTLSDynamicCall.cpp - TLS Dynamic Call Fixup ------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This pass expands ADDItls{ld,gd}LADDR[32] machine instructions into 11 // separate ADDItls[gd]L[32] and GETtlsADDR[32] instructions, both of 12 // which define GPR3. A copy is added from GPR3 to the target virtual 13 // register of the original instruction. The GETtlsADDR[32] is really 14 // a call instruction, so its target register is constrained to be GPR3. 15 // This is not true of ADDItls[gd]L[32], but there is a legacy linker 16 // optimization bug that requires the target register of the addi of 17 // a local- or general-dynamic TLS access sequence to be GPR3. 18 // 19 // This is done in a late pass so that TLS variable accesses can be 20 // fully commoned by MachineCSE. 21 // 22 //===----------------------------------------------------------------------===// 23 24 #include "PPCInstrInfo.h" 25 #include "PPC.h" 26 #include "PPCInstrBuilder.h" 27 #include "PPCTargetMachine.h" 28 #include "llvm/CodeGen/LiveIntervalAnalysis.h" 29 #include "llvm/CodeGen/MachineFunctionPass.h" 30 #include "llvm/CodeGen/MachineInstrBuilder.h" 31 #include "llvm/Support/Debug.h" 32 #include "llvm/Support/raw_ostream.h" 33 34 using namespace llvm; 35 36 #define DEBUG_TYPE "ppc-tls-dynamic-call" 37 38 namespace llvm { 39 void initializePPCTLSDynamicCallPass(PassRegistry&); 40 } 41 42 namespace { 43 struct PPCTLSDynamicCall : public MachineFunctionPass { 44 static char ID; 45 PPCTLSDynamicCall() : MachineFunctionPass(ID) { 46 initializePPCTLSDynamicCallPass(*PassRegistry::getPassRegistry()); 47 } 48 49 const PPCTargetMachine *TM; 50 const PPCInstrInfo *TII; 51 LiveIntervals *LIS; 52 53 protected: 54 bool processBlock(MachineBasicBlock &MBB) { 55 bool Changed = false; 56 bool Is64Bit = TM->getSubtargetImpl()->isPPC64(); 57 58 for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end(); 59 I != IE; ++I) { 60 MachineInstr *MI = I; 61 62 if (MI->getOpcode() != PPC::ADDItlsgdLADDR && 63 MI->getOpcode() != PPC::ADDItlsldLADDR && 64 MI->getOpcode() != PPC::ADDItlsgdLADDR32 && 65 MI->getOpcode() != PPC::ADDItlsldLADDR32) 66 continue; 67 68 DEBUG(dbgs() << "TLS Dynamic Call Fixup:\n " << *MI;); 69 70 unsigned OutReg = MI->getOperand(0).getReg(); 71 unsigned InReg = MI->getOperand(1).getReg(); 72 DebugLoc DL = MI->getDebugLoc(); 73 unsigned GPR3 = Is64Bit ? PPC::X3 : PPC::R3; 74 unsigned Opc1, Opc2; 75 SmallVector<unsigned, 4> OrigRegs; 76 OrigRegs.push_back(OutReg); 77 OrigRegs.push_back(InReg); 78 OrigRegs.push_back(GPR3); 79 80 switch (MI->getOpcode()) { 81 default: 82 llvm_unreachable("Opcode inconsistency error"); 83 case PPC::ADDItlsgdLADDR: 84 Opc1 = PPC::ADDItlsgdL; 85 Opc2 = PPC::GETtlsADDR; 86 break; 87 case PPC::ADDItlsldLADDR: 88 Opc1 = PPC::ADDItlsldL; 89 Opc2 = PPC::GETtlsldADDR; 90 break; 91 case PPC::ADDItlsgdLADDR32: 92 Opc1 = PPC::ADDItlsgdL32; 93 Opc2 = PPC::GETtlsADDR32; 94 break; 95 case PPC::ADDItlsldLADDR32: 96 Opc1 = PPC::ADDItlsldL32; 97 Opc2 = PPC::GETtlsldADDR32; 98 break; 99 } 100 101 // Expand into two ops built prior to the existing instruction. 102 MachineInstr *Addi = BuildMI(MBB, I, DL, TII->get(Opc1), GPR3) 103 .addReg(InReg); 104 Addi->addOperand(MI->getOperand(2)); 105 106 // The ADDItls* instruction is the first instruction in the 107 // repair range. 108 MachineBasicBlock::iterator First = I; 109 --First; 110 111 MachineInstr *Call = (BuildMI(MBB, I, DL, TII->get(Opc2), GPR3) 112 .addReg(GPR3)); 113 Call->addOperand(MI->getOperand(3)); 114 115 BuildMI(MBB, I, DL, TII->get(TargetOpcode::COPY), OutReg) 116 .addReg(GPR3); 117 118 // The COPY is the last instruction in the repair range. 119 MachineBasicBlock::iterator Last = I; 120 --Last; 121 122 // Move past the original instruction and remove it. 123 ++I; 124 MI->removeFromParent(); 125 126 // Repair the live intervals. 127 LIS->repairIntervalsInRange(&MBB, First, Last, OrigRegs); 128 Changed = true; 129 } 130 131 return Changed; 132 } 133 134 public: 135 bool runOnMachineFunction(MachineFunction &MF) override { 136 TM = static_cast<const PPCTargetMachine *>(&MF.getTarget()); 137 TII = TM->getSubtargetImpl()->getInstrInfo(); 138 LIS = &getAnalysis<LiveIntervals>(); 139 140 bool Changed = false; 141 142 for (MachineFunction::iterator I = MF.begin(); I != MF.end();) { 143 MachineBasicBlock &B = *I++; 144 if (processBlock(B)) 145 Changed = true; 146 } 147 148 return Changed; 149 } 150 151 void getAnalysisUsage(AnalysisUsage &AU) const override { 152 AU.addRequired<LiveIntervals>(); 153 AU.addPreserved<LiveIntervals>(); 154 AU.addRequired<SlotIndexes>(); 155 AU.addPreserved<SlotIndexes>(); 156 MachineFunctionPass::getAnalysisUsage(AU); 157 } 158 }; 159 } 160 161 INITIALIZE_PASS_BEGIN(PPCTLSDynamicCall, DEBUG_TYPE, 162 "PowerPC TLS Dynamic Call Fixup", false, false) 163 INITIALIZE_PASS_DEPENDENCY(LiveIntervals) 164 INITIALIZE_PASS_DEPENDENCY(SlotIndexes) 165 INITIALIZE_PASS_END(PPCTLSDynamicCall, DEBUG_TYPE, 166 "PowerPC TLS Dynamic Call Fixup", false, false) 167 168 char PPCTLSDynamicCall::ID = 0; 169 FunctionPass* 170 llvm::createPPCTLSDynamicCallPass() { return new PPCTLSDynamicCall(); } 171