xref: /freebsd-src/contrib/llvm-project/llvm/lib/Target/AArch64/AArch64CleanupLocalDynamicTLSPass.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
10b57cec5SDimitry Andric //===-- AArch64CleanupLocalDynamicTLSPass.cpp ---------------------*- C++ -*-=//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // Local-dynamic access to thread-local variables proceeds in three stages.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric // 1. The offset of this Module's thread-local area from TPIDR_EL0 is calculated
120b57cec5SDimitry Andric //    in much the same way as a general-dynamic TLS-descriptor access against
130b57cec5SDimitry Andric //    the special symbol _TLS_MODULE_BASE.
140b57cec5SDimitry Andric // 2. The variable's offset from _TLS_MODULE_BASE_ is calculated using
150b57cec5SDimitry Andric //    instructions with "dtprel" modifiers.
160b57cec5SDimitry Andric // 3. These two are added, together with TPIDR_EL0, to obtain the variable's
170b57cec5SDimitry Andric //    true address.
180b57cec5SDimitry Andric //
190b57cec5SDimitry Andric // This is only better than general-dynamic access to the variable if two or
200b57cec5SDimitry Andric // more of the first stage TLS-descriptor calculations can be combined. This
210b57cec5SDimitry Andric // pass looks through a function and performs such combinations.
220b57cec5SDimitry Andric //
230b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
240b57cec5SDimitry Andric #include "AArch64.h"
250b57cec5SDimitry Andric #include "AArch64InstrInfo.h"
260b57cec5SDimitry Andric #include "AArch64MachineFunctionInfo.h"
270b57cec5SDimitry Andric #include "llvm/CodeGen/MachineDominators.h"
280b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunction.h"
290b57cec5SDimitry Andric #include "llvm/CodeGen/MachineFunctionPass.h"
300b57cec5SDimitry Andric #include "llvm/CodeGen/MachineInstrBuilder.h"
310b57cec5SDimitry Andric #include "llvm/CodeGen/MachineRegisterInfo.h"
320b57cec5SDimitry Andric using namespace llvm;
330b57cec5SDimitry Andric 
340b57cec5SDimitry Andric #define TLSCLEANUP_PASS_NAME "AArch64 Local Dynamic TLS Access Clean-up"
350b57cec5SDimitry Andric 
360b57cec5SDimitry Andric namespace {
370b57cec5SDimitry Andric struct LDTLSCleanup : public MachineFunctionPass {
380b57cec5SDimitry Andric   static char ID;
390b57cec5SDimitry Andric   LDTLSCleanup() : MachineFunctionPass(ID) {
400b57cec5SDimitry Andric     initializeLDTLSCleanupPass(*PassRegistry::getPassRegistry());
410b57cec5SDimitry Andric   }
420b57cec5SDimitry Andric 
430b57cec5SDimitry Andric   bool runOnMachineFunction(MachineFunction &MF) override {
440b57cec5SDimitry Andric     if (skipFunction(MF.getFunction()))
450b57cec5SDimitry Andric       return false;
460b57cec5SDimitry Andric 
470b57cec5SDimitry Andric     AArch64FunctionInfo *AFI = MF.getInfo<AArch64FunctionInfo>();
480b57cec5SDimitry Andric     if (AFI->getNumLocalDynamicTLSAccesses() < 2) {
490b57cec5SDimitry Andric       // No point folding accesses if there isn't at least two.
500b57cec5SDimitry Andric       return false;
510b57cec5SDimitry Andric     }
520b57cec5SDimitry Andric 
53*0fca6ea1SDimitry Andric     MachineDominatorTree *DT =
54*0fca6ea1SDimitry Andric         &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
550b57cec5SDimitry Andric     return VisitNode(DT->getRootNode(), 0);
560b57cec5SDimitry Andric   }
570b57cec5SDimitry Andric 
580b57cec5SDimitry Andric   // Visit the dominator subtree rooted at Node in pre-order.
590b57cec5SDimitry Andric   // If TLSBaseAddrReg is non-null, then use that to replace any
600b57cec5SDimitry Andric   // TLS_base_addr instructions. Otherwise, create the register
610b57cec5SDimitry Andric   // when the first such instruction is seen, and then use it
620b57cec5SDimitry Andric   // as we encounter more instructions.
630b57cec5SDimitry Andric   bool VisitNode(MachineDomTreeNode *Node, unsigned TLSBaseAddrReg) {
640b57cec5SDimitry Andric     MachineBasicBlock *BB = Node->getBlock();
650b57cec5SDimitry Andric     bool Changed = false;
660b57cec5SDimitry Andric 
670b57cec5SDimitry Andric     // Traverse the current block.
680b57cec5SDimitry Andric     for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;
690b57cec5SDimitry Andric          ++I) {
700b57cec5SDimitry Andric       switch (I->getOpcode()) {
710b57cec5SDimitry Andric       case AArch64::TLSDESC_CALLSEQ:
720b57cec5SDimitry Andric         // Make sure it's a local dynamic access.
730b57cec5SDimitry Andric         if (!I->getOperand(0).isSymbol() ||
740b57cec5SDimitry Andric             strcmp(I->getOperand(0).getSymbolName(), "_TLS_MODULE_BASE_"))
750b57cec5SDimitry Andric           break;
760b57cec5SDimitry Andric 
770b57cec5SDimitry Andric         if (TLSBaseAddrReg)
780b57cec5SDimitry Andric           I = replaceTLSBaseAddrCall(*I, TLSBaseAddrReg);
790b57cec5SDimitry Andric         else
800b57cec5SDimitry Andric           I = setRegister(*I, &TLSBaseAddrReg);
810b57cec5SDimitry Andric         Changed = true;
820b57cec5SDimitry Andric         break;
830b57cec5SDimitry Andric       default:
840b57cec5SDimitry Andric         break;
850b57cec5SDimitry Andric       }
860b57cec5SDimitry Andric     }
870b57cec5SDimitry Andric 
880b57cec5SDimitry Andric     // Visit the children of this block in the dominator tree.
890b57cec5SDimitry Andric     for (MachineDomTreeNode *N : *Node) {
900b57cec5SDimitry Andric       Changed |= VisitNode(N, TLSBaseAddrReg);
910b57cec5SDimitry Andric     }
920b57cec5SDimitry Andric 
930b57cec5SDimitry Andric     return Changed;
940b57cec5SDimitry Andric   }
950b57cec5SDimitry Andric 
960b57cec5SDimitry Andric   // Replace the TLS_base_addr instruction I with a copy from
970b57cec5SDimitry Andric   // TLSBaseAddrReg, returning the new instruction.
980b57cec5SDimitry Andric   MachineInstr *replaceTLSBaseAddrCall(MachineInstr &I,
990b57cec5SDimitry Andric                                        unsigned TLSBaseAddrReg) {
1000b57cec5SDimitry Andric     MachineFunction *MF = I.getParent()->getParent();
1010b57cec5SDimitry Andric     const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
1020b57cec5SDimitry Andric 
1030b57cec5SDimitry Andric     // Insert a Copy from TLSBaseAddrReg to x0, which is where the rest of the
1040b57cec5SDimitry Andric     // code sequence assumes the address will be.
1050b57cec5SDimitry Andric     MachineInstr *Copy = BuildMI(*I.getParent(), I, I.getDebugLoc(),
1060b57cec5SDimitry Andric                                  TII->get(TargetOpcode::COPY), AArch64::X0)
1070b57cec5SDimitry Andric                              .addReg(TLSBaseAddrReg);
1080b57cec5SDimitry Andric 
1095ffd83dbSDimitry Andric     // Update the call site info.
1105ffd83dbSDimitry Andric     if (I.shouldUpdateCallSiteInfo())
1115ffd83dbSDimitry Andric       I.getMF()->eraseCallSiteInfo(&I);
1125ffd83dbSDimitry Andric 
1130b57cec5SDimitry Andric     // Erase the TLS_base_addr instruction.
1140b57cec5SDimitry Andric     I.eraseFromParent();
1150b57cec5SDimitry Andric 
1160b57cec5SDimitry Andric     return Copy;
1170b57cec5SDimitry Andric   }
1180b57cec5SDimitry Andric 
1190b57cec5SDimitry Andric   // Create a virtual register in *TLSBaseAddrReg, and populate it by
1200b57cec5SDimitry Andric   // inserting a copy instruction after I. Returns the new instruction.
1210b57cec5SDimitry Andric   MachineInstr *setRegister(MachineInstr &I, unsigned *TLSBaseAddrReg) {
1220b57cec5SDimitry Andric     MachineFunction *MF = I.getParent()->getParent();
1230b57cec5SDimitry Andric     const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo();
1240b57cec5SDimitry Andric 
1250b57cec5SDimitry Andric     // Create a virtual register for the TLS base address.
1260b57cec5SDimitry Andric     MachineRegisterInfo &RegInfo = MF->getRegInfo();
1270b57cec5SDimitry Andric     *TLSBaseAddrReg = RegInfo.createVirtualRegister(&AArch64::GPR64RegClass);
1280b57cec5SDimitry Andric 
1290b57cec5SDimitry Andric     // Insert a copy from X0 to TLSBaseAddrReg for later.
1300b57cec5SDimitry Andric     MachineInstr *Copy =
1310b57cec5SDimitry Andric         BuildMI(*I.getParent(), ++I.getIterator(), I.getDebugLoc(),
1320b57cec5SDimitry Andric                 TII->get(TargetOpcode::COPY), *TLSBaseAddrReg)
1330b57cec5SDimitry Andric             .addReg(AArch64::X0);
1340b57cec5SDimitry Andric 
1350b57cec5SDimitry Andric     return Copy;
1360b57cec5SDimitry Andric   }
1370b57cec5SDimitry Andric 
1380b57cec5SDimitry Andric   StringRef getPassName() const override { return TLSCLEANUP_PASS_NAME; }
1390b57cec5SDimitry Andric 
1400b57cec5SDimitry Andric   void getAnalysisUsage(AnalysisUsage &AU) const override {
1410b57cec5SDimitry Andric     AU.setPreservesCFG();
142*0fca6ea1SDimitry Andric     AU.addRequired<MachineDominatorTreeWrapperPass>();
1430b57cec5SDimitry Andric     MachineFunctionPass::getAnalysisUsage(AU);
1440b57cec5SDimitry Andric   }
1450b57cec5SDimitry Andric };
1460b57cec5SDimitry Andric }
1470b57cec5SDimitry Andric 
1480b57cec5SDimitry Andric INITIALIZE_PASS(LDTLSCleanup, "aarch64-local-dynamic-tls-cleanup",
1490b57cec5SDimitry Andric                 TLSCLEANUP_PASS_NAME, false, false)
1500b57cec5SDimitry Andric 
1510b57cec5SDimitry Andric char LDTLSCleanup::ID = 0;
1520b57cec5SDimitry Andric FunctionPass *llvm::createAArch64CleanupLocalDynamicTLSPass() {
1530b57cec5SDimitry Andric   return new LDTLSCleanup();
1540b57cec5SDimitry Andric }
155