PPCTLSDynamicCall.cpp 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 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 && !IsPCREL) {
  56. // Although we create ADJCALLSTACKDOWN and ADJCALLSTACKUP
  57. // as scheduling fences, we skip creating fences if we already
  58. // have existing ADJCALLSTACKDOWN/UP to avoid nesting,
  59. // which causes verification error with -verify-machineinstrs.
  60. if (MI.getOpcode() == PPC::ADJCALLSTACKDOWN)
  61. NeedFence = false;
  62. else if (MI.getOpcode() == PPC::ADJCALLSTACKUP)
  63. NeedFence = true;
  64. ++I;
  65. continue;
  66. }
  67. LLVM_DEBUG(dbgs() << "TLS Dynamic Call Fixup:\n " << MI);
  68. Register OutReg = MI.getOperand(0).getReg();
  69. Register InReg = PPC::NoRegister;
  70. Register GPR3 = Is64Bit ? PPC::X3 : PPC::R3;
  71. SmallVector<Register, 3> OrigRegs = {OutReg, GPR3};
  72. if (!IsPCREL) {
  73. InReg = MI.getOperand(1).getReg();
  74. OrigRegs.push_back(InReg);
  75. }
  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::PADDI8pc:
  98. assert(IsPCREL && "Expecting General/Local Dynamic PCRel");
  99. Opc1 = PPC::PADDI8pc;
  100. Opc2 = MI.getOperand(2).getTargetFlags() ==
  101. PPCII::MO_GOT_TLSGD_PCREL_FLAG
  102. ? PPC::GETtlsADDRPCREL
  103. : PPC::GETtlsldADDRPCREL;
  104. }
  105. // We create ADJCALLSTACKUP and ADJCALLSTACKDOWN around _tls_get_addr
  106. // as scheduling fence to avoid it is scheduled before
  107. // mflr in the prologue and the address in LR is clobbered (PR25839).
  108. // We don't really need to save data to the stack - the clobbered
  109. // registers are already saved when the SDNode (e.g. PPCaddiTlsgdLAddr)
  110. // gets translated to the pseudo instruction (e.g. ADDItlsgdLADDR).
  111. if (NeedFence)
  112. BuildMI(MBB, I, DL, TII->get(PPC::ADJCALLSTACKDOWN)).addImm(0)
  113. .addImm(0);
  114. MachineInstr *Addi;
  115. if (IsPCREL) {
  116. Addi = BuildMI(MBB, I, DL, TII->get(Opc1), GPR3).addImm(0);
  117. } else {
  118. // Expand into two ops built prior to the existing instruction.
  119. assert(InReg != PPC::NoRegister && "Operand must be a register");
  120. Addi = BuildMI(MBB, I, DL, TII->get(Opc1), GPR3).addReg(InReg);
  121. }
  122. Addi->addOperand(MI.getOperand(2));
  123. // The ADDItls* instruction is the first instruction in the
  124. // repair range.
  125. MachineBasicBlock::iterator First = I;
  126. --First;
  127. MachineInstr *Call = (BuildMI(MBB, I, DL, TII->get(Opc2), GPR3)
  128. .addReg(GPR3));
  129. if (IsPCREL)
  130. Call->addOperand(MI.getOperand(2));
  131. else
  132. Call->addOperand(MI.getOperand(3));
  133. if (NeedFence)
  134. BuildMI(MBB, I, DL, TII->get(PPC::ADJCALLSTACKUP)).addImm(0).addImm(0);
  135. BuildMI(MBB, I, DL, TII->get(TargetOpcode::COPY), OutReg)
  136. .addReg(GPR3);
  137. // The COPY is the last instruction in the repair range.
  138. MachineBasicBlock::iterator Last = I;
  139. --Last;
  140. // Move past the original instruction and remove it.
  141. ++I;
  142. MI.removeFromParent();
  143. // Repair the live intervals.
  144. LIS->repairIntervalsInRange(&MBB, First, Last, OrigRegs);
  145. Changed = true;
  146. }
  147. return Changed;
  148. }
  149. public:
  150. bool isPCREL(const MachineInstr &MI) {
  151. return (MI.getOpcode() == PPC::PADDI8pc) &&
  152. (MI.getOperand(2).getTargetFlags() ==
  153. PPCII::MO_GOT_TLSGD_PCREL_FLAG ||
  154. MI.getOperand(2).getTargetFlags() ==
  155. PPCII::MO_GOT_TLSLD_PCREL_FLAG);
  156. }
  157. bool runOnMachineFunction(MachineFunction &MF) override {
  158. TII = MF.getSubtarget<PPCSubtarget>().getInstrInfo();
  159. LIS = &getAnalysis<LiveIntervals>();
  160. bool Changed = false;
  161. for (MachineFunction::iterator I = MF.begin(); I != MF.end();) {
  162. MachineBasicBlock &B = *I++;
  163. if (processBlock(B))
  164. Changed = true;
  165. }
  166. return Changed;
  167. }
  168. void getAnalysisUsage(AnalysisUsage &AU) const override {
  169. AU.addRequired<LiveIntervals>();
  170. AU.addPreserved<LiveIntervals>();
  171. AU.addRequired<SlotIndexes>();
  172. AU.addPreserved<SlotIndexes>();
  173. MachineFunctionPass::getAnalysisUsage(AU);
  174. }
  175. };
  176. }
  177. INITIALIZE_PASS_BEGIN(PPCTLSDynamicCall, DEBUG_TYPE,
  178. "PowerPC TLS Dynamic Call Fixup", false, false)
  179. INITIALIZE_PASS_DEPENDENCY(LiveIntervals)
  180. INITIALIZE_PASS_DEPENDENCY(SlotIndexes)
  181. INITIALIZE_PASS_END(PPCTLSDynamicCall, DEBUG_TYPE,
  182. "PowerPC TLS Dynamic Call Fixup", false, false)
  183. char PPCTLSDynamicCall::ID = 0;
  184. FunctionPass*
  185. llvm::createPPCTLSDynamicCallPass() { return new PPCTLSDynamicCall(); }