AArch64CleanupLocalDynamicTLSPass.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. //===-- AArch64CleanupLocalDynamicTLSPass.cpp ---------------------*- C++ -*-=//
  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. // Local-dynamic access to thread-local variables proceeds in three stages.
  10. //
  11. // 1. The offset of this Module's thread-local area from TPIDR_EL0 is calculated
  12. // in much the same way as a general-dynamic TLS-descriptor access against
  13. // the special symbol _TLS_MODULE_BASE.
  14. // 2. The variable's offset from _TLS_MODULE_BASE_ is calculated using
  15. // instructions with "dtprel" modifiers.
  16. // 3. These two are added, together with TPIDR_EL0, to obtain the variable's
  17. // true address.
  18. //
  19. // This is only better than general-dynamic access to the variable if two or
  20. // more of the first stage TLS-descriptor calculations can be combined. This
  21. // pass looks through a function and performs such combinations.
  22. //
  23. //===----------------------------------------------------------------------===//
  24. #include "AArch64.h"
  25. #include "AArch64InstrInfo.h"
  26. #include "AArch64MachineFunctionInfo.h"
  27. #include "llvm/CodeGen/MachineDominators.h"
  28. #include "llvm/CodeGen/MachineFunction.h"
  29. #include "llvm/CodeGen/MachineFunctionPass.h"
  30. #include "llvm/CodeGen/MachineInstrBuilder.h"
  31. #include "llvm/CodeGen/MachineRegisterInfo.h"
  32. using namespace llvm;
  33. #define TLSCLEANUP_PASS_NAME "AArch64 Local Dynamic TLS Access Clean-up"
  34. namespace {
  35. struct LDTLSCleanup : public MachineFunctionPass {
  36. static char ID;
  37. LDTLSCleanup() : MachineFunctionPass(ID) {
  38. initializeLDTLSCleanupPass(*PassRegistry::getPassRegistry());
  39. }
  40. bool runOnMachineFunction(MachineFunction &MF) override {
  41. if (skipFunction(MF.getFunction()))
  42. return false;
  43. AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>();
  44. if (AFI->getNumLocalDynamicTLSAccesses() < 2) {
  45. // No point folding accesses if there isn't at least two.
  46. return false;
  47. }
  48. MachineDominatorTree *DT = &getAnalysis<MachineDominatorTree>();
  49. return VisitNode(DT->getRootNode(), 0);
  50. }
  51. // Visit the dominator subtree rooted at Node in pre-order.
  52. // If TLSBaseAddrReg is non-null, then use that to replace any
  53. // TLS_base_addr instructions. Otherwise, create the register
  54. // when the first such instruction is seen, and then use it
  55. // as we encounter more instructions.
  56. bool VisitNode(MachineDomTreeNode *Node, unsigned TLSBaseAddrReg) {
  57. MachineBasicBlock *BB = Node->getBlock();
  58. bool Changed = false;
  59. // Traverse the current block.
  60. for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;
  61. ++I) {
  62. switch (I->getOpcode()) {
  63. case AArch64::TLSDESC_CALLSEQ:
  64. // Make sure it's a local dynamic access.
  65. if (!I->getOperand(0).isSymbol() ||
  66. strcmp(I->getOperand(0).getSymbolName(), "_TLS_MODULE_BASE_"))
  67. break;
  68. if (TLSBaseAddrReg)
  69. I = replaceTLSBaseAddrCall(*I, TLSBaseAddrReg);
  70. else
  71. I = setRegister(*I, &TLSBaseAddrReg);
  72. Changed = true;
  73. break;
  74. default:
  75. break;
  76. }
  77. }
  78. // Visit the children of this block in the dominator tree.
  79. for (MachineDomTreeNode *N : *Node) {
  80. Changed |= VisitNode(N, TLSBaseAddrReg);
  81. }
  82. return Changed;
  83. }
  84. // Replace the TLS_base_addr instruction I with a copy from
  85. // TLSBaseAddrReg, returning the new instruction.
  86. MachineInstr *replaceTLSBaseAddrCall(MachineInstr &I,
  87. unsigned TLSBaseAddrReg) {
  88. MachineFunction *MF = I.getParent()->getParent();
  89. const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
  90. // Insert a Copy from TLSBaseAddrReg to x0, which is where the rest of the
  91. // code sequence assumes the address will be.
  92. MachineInstr *Copy = BuildMI(*I.getParent(), I, I.getDebugLoc(),
  93. TII->get(TargetOpcode::COPY), AArch64::X0)
  94. .addReg(TLSBaseAddrReg);
  95. // Update the call site info.
  96. if (I.shouldUpdateCallSiteInfo())
  97. I.getMF()->eraseCallSiteInfo(&I);
  98. // Erase the TLS_base_addr instruction.
  99. I.eraseFromParent();
  100. return Copy;
  101. }
  102. // Create a virtual register in *TLSBaseAddrReg, and populate it by
  103. // inserting a copy instruction after I. Returns the new instruction.
  104. MachineInstr *setRegister(MachineInstr &I, unsigned *TLSBaseAddrReg) {
  105. MachineFunction *MF = I.getParent()->getParent();
  106. const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
  107. // Create a virtual register for the TLS base address.
  108. MachineRegisterInfo &RegInfo = MF->getRegInfo();
  109. *TLSBaseAddrReg = RegInfo.createVirtualRegister(&AArch64::GPR64RegClass);
  110. // Insert a copy from X0 to TLSBaseAddrReg for later.
  111. MachineInstr *Copy =
  112. BuildMI(*I.getParent(), ++I.getIterator(), I.getDebugLoc(),
  113. TII->get(TargetOpcode::COPY), *TLSBaseAddrReg)
  114. .addReg(AArch64::X0);
  115. return Copy;
  116. }
  117. StringRef getPassName() const override { return TLSCLEANUP_PASS_NAME; }
  118. void getAnalysisUsage(AnalysisUsage &AU) const override {
  119. AU.setPreservesCFG();
  120. AU.addRequired<MachineDominatorTree>();
  121. MachineFunctionPass::getAnalysisUsage(AU);
  122. }
  123. };
  124. }
  125. INITIALIZE_PASS(LDTLSCleanup, "aarch64-local-dynamic-tls-cleanup",
  126. TLSCLEANUP_PASS_NAME, false, false)
  127. char LDTLSCleanup::ID = 0;
  128. FunctionPass *llvm::createAArch64CleanupLocalDynamicTLSPass() {
  129. return new LDTLSCleanup();
  130. }