PPCTLSDynamicCall.cpp 8.0 KB

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