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