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