PPCTLSDynamicCall.cpp 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. //===---------- PPCTLSDynamicCall.cpp - TLS Dynamic Call Fixup ------------===//
  2. //
  3. // 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
  6. //
  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. #include "PPC.h"
  23. #include "PPCInstrBuilder.h"
  24. #include "PPCInstrInfo.h"
  25. #include "PPCTargetMachine.h"
  26. #include "llvm/CodeGen/LiveIntervals.h"
  27. #include "llvm/CodeGen/MachineFunctionPass.h"
  28. #include "llvm/CodeGen/MachineInstrBuilder.h"
  29. #include "llvm/InitializePasses.h"
  30. #include "llvm/Support/Debug.h"
  31. #include "llvm/Support/raw_ostream.h"
  32. using namespace llvm;
  33. #define DEBUG_TYPE "ppc-tls-dynamic-call"
  34. namespace {
  35. struct PPCTLSDynamicCall : public MachineFunctionPass {
  36. static char ID;
  37. PPCTLSDynamicCall() : MachineFunctionPass(ID) {
  38. initializePPCTLSDynamicCallPass(*PassRegistry::getPassRegistry());
  39. }
  40. const PPCInstrInfo *TII;
  41. LiveIntervals *LIS;
  42. protected:
  43. bool processBlock(MachineBasicBlock &MBB) {
  44. bool Changed = false;
  45. bool NeedFence = true;
  46. bool Is64Bit = MBB.getParent()->getSubtarget<PPCSubtarget>().isPPC64();
  47. bool IsAIX = MBB.getParent()->getSubtarget<PPCSubtarget>().isAIXABI();
  48. bool IsPCREL = false;
  49. for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end();
  50. I != IE;) {
  51. MachineInstr &MI = *I;
  52. IsPCREL = isPCREL(MI);
  53. if (MI.getOpcode() != PPC::ADDItlsgdLADDR &&
  54. MI.getOpcode() != PPC::ADDItlsldLADDR &&
  55. MI.getOpcode() != PPC::ADDItlsgdLADDR32 &&
  56. MI.getOpcode() != PPC::ADDItlsldLADDR32 &&
  57. MI.getOpcode() != PPC::TLSGDAIX &&
  58. MI.getOpcode() != PPC::TLSGDAIX8 && !IsPCREL) {
  59. // Although we create ADJCALLSTACKDOWN and ADJCALLSTACKUP
  60. // as scheduling fences, we skip creating fences if we already
  61. // have existing ADJCALLSTACKDOWN/UP to avoid nesting,
  62. // which causes verification error with -verify-machineinstrs.
  63. if (MI.getOpcode() == PPC::ADJCALLSTACKDOWN)
  64. NeedFence = false;
  65. else if (MI.getOpcode() == PPC::ADJCALLSTACKUP)
  66. NeedFence = true;
  67. ++I;
  68. continue;
  69. }
  70. LLVM_DEBUG(dbgs() << "TLS Dynamic Call Fixup:\n " << MI);
  71. Register OutReg = MI.getOperand(0).getReg();
  72. Register InReg = PPC::NoRegister;
  73. Register GPR3 = Is64Bit ? PPC::X3 : PPC::R3;
  74. Register GPR4 = Is64Bit ? PPC::X4 : PPC::R4;
  75. SmallVector<Register, 3> OrigRegs = {OutReg, GPR3};
  76. if (!IsPCREL) {
  77. InReg = MI.getOperand(1).getReg();
  78. OrigRegs.push_back(InReg);
  79. }
  80. DebugLoc DL = MI.getDebugLoc();
  81. unsigned Opc1, Opc2;
  82. switch (MI.getOpcode()) {
  83. default:
  84. llvm_unreachable("Opcode inconsistency error");
  85. case PPC::ADDItlsgdLADDR:
  86. Opc1 = PPC::ADDItlsgdL;
  87. Opc2 = PPC::GETtlsADDR;
  88. break;
  89. case PPC::ADDItlsldLADDR:
  90. Opc1 = PPC::ADDItlsldL;
  91. Opc2 = PPC::GETtlsldADDR;
  92. break;
  93. case PPC::ADDItlsgdLADDR32:
  94. Opc1 = PPC::ADDItlsgdL32;
  95. Opc2 = PPC::GETtlsADDR32;
  96. break;
  97. case PPC::ADDItlsldLADDR32:
  98. Opc1 = PPC::ADDItlsldL32;
  99. Opc2 = PPC::GETtlsldADDR32;
  100. break;
  101. case PPC::TLSGDAIX8:
  102. // TLSGDAIX8 is expanded to two copies and GET_TLS_ADDR, so we only
  103. // set Opc2 here.
  104. Opc2 = PPC::GETtlsADDR64AIX;
  105. break;
  106. case PPC::TLSGDAIX:
  107. // TLSGDAIX is expanded to two copies and GET_TLS_ADDR, so we only
  108. // set Opc2 here.
  109. Opc2 = PPC::GETtlsADDR32AIX;
  110. break;
  111. case PPC::PADDI8pc:
  112. assert(IsPCREL && "Expecting General/Local Dynamic PCRel");
  113. Opc1 = PPC::PADDI8pc;
  114. Opc2 = MI.getOperand(2).getTargetFlags() ==
  115. PPCII::MO_GOT_TLSGD_PCREL_FLAG
  116. ? PPC::GETtlsADDRPCREL
  117. : PPC::GETtlsldADDRPCREL;
  118. }
  119. // We create ADJCALLSTACKUP and ADJCALLSTACKDOWN around _tls_get_addr
  120. // as scheduling fence to avoid it is scheduled before
  121. // mflr in the prologue and the address in LR is clobbered (PR25839).
  122. // We don't really need to save data to the stack - the clobbered
  123. // registers are already saved when the SDNode (e.g. PPCaddiTlsgdLAddr)
  124. // gets translated to the pseudo instruction (e.g. ADDItlsgdLADDR).
  125. if (NeedFence)
  126. BuildMI(MBB, I, DL, TII->get(PPC::ADJCALLSTACKDOWN)).addImm(0)
  127. .addImm(0);
  128. // The ADDItls* instruction is the first instruction in the
  129. // repair range.
  130. MachineBasicBlock::iterator First = I;
  131. --First;
  132. if (IsAIX) {
  133. // The variable offset and region handle are copied in r4 and r3. The
  134. // copies are followed by GETtlsADDR32AIX/GETtlsADDR64AIX.
  135. BuildMI(MBB, I, DL, TII->get(TargetOpcode::COPY), GPR4)
  136. .addReg(MI.getOperand(1).getReg());
  137. BuildMI(MBB, I, DL, TII->get(TargetOpcode::COPY), GPR3)
  138. .addReg(MI.getOperand(2).getReg());
  139. BuildMI(MBB, I, DL, TII->get(Opc2), GPR3).addReg(GPR3).addReg(GPR4);
  140. } else {
  141. MachineInstr *Addi;
  142. if (IsPCREL) {
  143. Addi = BuildMI(MBB, I, DL, TII->get(Opc1), GPR3).addImm(0);
  144. } else {
  145. // Expand into two ops built prior to the existing instruction.
  146. assert(InReg != PPC::NoRegister && "Operand must be a register");
  147. Addi = BuildMI(MBB, I, DL, TII->get(Opc1), GPR3).addReg(InReg);
  148. }
  149. Addi->addOperand(MI.getOperand(2));
  150. MachineInstr *Call =
  151. (BuildMI(MBB, I, DL, TII->get(Opc2), GPR3).addReg(GPR3));
  152. if (IsPCREL)
  153. Call->addOperand(MI.getOperand(2));
  154. else
  155. Call->addOperand(MI.getOperand(3));
  156. }
  157. if (NeedFence)
  158. BuildMI(MBB, I, DL, TII->get(PPC::ADJCALLSTACKUP)).addImm(0).addImm(0);
  159. BuildMI(MBB, I, DL, TII->get(TargetOpcode::COPY), OutReg)
  160. .addReg(GPR3);
  161. // The COPY is the last instruction in the repair range.
  162. MachineBasicBlock::iterator Last = I;
  163. --Last;
  164. // Move past the original instruction and remove it.
  165. ++I;
  166. MI.removeFromParent();
  167. // Repair the live intervals.
  168. LIS->repairIntervalsInRange(&MBB, First, Last, OrigRegs);
  169. Changed = true;
  170. }
  171. return Changed;
  172. }
  173. public:
  174. bool isPCREL(const MachineInstr &MI) {
  175. return (MI.getOpcode() == PPC::PADDI8pc) &&
  176. (MI.getOperand(2).getTargetFlags() ==
  177. PPCII::MO_GOT_TLSGD_PCREL_FLAG ||
  178. MI.getOperand(2).getTargetFlags() ==
  179. PPCII::MO_GOT_TLSLD_PCREL_FLAG);
  180. }
  181. bool runOnMachineFunction(MachineFunction &MF) override {
  182. TII = MF.getSubtarget<PPCSubtarget>().getInstrInfo();
  183. LIS = &getAnalysis<LiveIntervals>();
  184. bool Changed = false;
  185. for (MachineBasicBlock &B : llvm::make_early_inc_range(MF))
  186. if (processBlock(B))
  187. Changed = true;
  188. return Changed;
  189. }
  190. void getAnalysisUsage(AnalysisUsage &AU) const override {
  191. AU.addRequired<LiveIntervals>();
  192. AU.addPreserved<LiveIntervals>();
  193. AU.addRequired<SlotIndexes>();
  194. AU.addPreserved<SlotIndexes>();
  195. MachineFunctionPass::getAnalysisUsage(AU);
  196. }
  197. };
  198. }
  199. INITIALIZE_PASS_BEGIN(PPCTLSDynamicCall, DEBUG_TYPE,
  200. "PowerPC TLS Dynamic Call Fixup", false, false)
  201. INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
  202. INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
  203. INITIALIZE_PASS_END(PPCTLSDynamicCall, DEBUG_TYPE,
  204. "PowerPC TLS Dynamic Call Fixup", false, false)
  205. char PPCTLSDynamicCall::ID = 0;
  206. FunctionPass*
  207. llvm::createPPCTLSDynamicCallPass() { return new PPCTLSDynamicCall(); }