1//===- X86CleanupLocalDynamicTLS.cpp - Cleanup local dynamic TLS access ---===//
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 combines multiple accesses to local-dynamic TLS variables so that
10// the TLS base address for the module is only fetched once per execution path
11// through the function.
12//
13//===----------------------------------------------------------------------===//
14
15#include "X86.h"
16#include "X86InstrInfo.h"
17#include "X86MachineFunctionInfo.h"
18#include "X86Subtarget.h"
19#include "llvm/CodeGen/MachineDominators.h"
20#include "llvm/CodeGen/MachineFunctionPass.h"
21#include "llvm/CodeGen/MachineInstrBuilder.h"
22#include "llvm/CodeGen/MachineRegisterInfo.h"
23#include "llvm/InitializePasses.h"
24
25using namespace llvm;
26
27#define DEBUG_TYPE "x86-cleanup-local-dynamic-tls"
28
29namespace {
30class X86CleanupLocalDynamicTLSLegacy : public MachineFunctionPass {
31public:
32 static char ID;
33
34 X86CleanupLocalDynamicTLSLegacy() : MachineFunctionPass(ID) {}
35
36 StringRef getPassName() const override {
37 return "Local Dynamic TLS Access Clean-up";
38 }
39
40 bool runOnMachineFunction(MachineFunction &MF) override;
41
42 void getAnalysisUsage(AnalysisUsage &AU) const override {
43 AU.setPreservesCFG();
44 AU.addRequired<MachineDominatorTreeWrapperPass>();
45 MachineFunctionPass::getAnalysisUsage(AU);
46 }
47};
48} // end anonymous namespace
49
50char X86CleanupLocalDynamicTLSLegacy::ID = 0;
51
52FunctionPass *llvm::createCleanupLocalDynamicTLSLegacyPass() {
53 return new X86CleanupLocalDynamicTLSLegacy();
54}
55
56// Replace the TLS_base_addr instruction I with a copy from
57// TLSBaseAddrReg, returning the new instruction.
58static MachineInstr *ReplaceTLSBaseAddrCall(MachineInstr &I,
59 Register TLSBaseAddrReg) {
60 MachineFunction *MF = I.getParent()->getParent();
61 const X86Subtarget &STI = MF->getSubtarget<X86Subtarget>();
62 const bool is64Bit = STI.is64Bit();
63 const X86InstrInfo *TII = STI.getInstrInfo();
64
65 // Insert a Copy from TLSBaseAddrReg to RAX/EAX.
66 MachineInstr *Copy =
67 BuildMI(BB&: *I.getParent(), I, MIMD: I.getDebugLoc(), MCID: TII->get(Opcode: TargetOpcode::COPY),
68 DestReg: is64Bit ? X86::RAX : X86::EAX)
69 .addReg(RegNo: TLSBaseAddrReg);
70
71 // Erase the TLS_base_addr instruction.
72 I.eraseFromParent();
73
74 return Copy;
75}
76
77// Create a virtual register in *TLSBaseAddrReg, and populate it by
78// inserting a copy instruction after I. Returns the new instruction.
79static MachineInstr *SetRegister(MachineInstr &I, Register *TLSBaseAddrReg) {
80 MachineFunction *MF = I.getParent()->getParent();
81 const X86Subtarget &STI = MF->getSubtarget<X86Subtarget>();
82 const bool is64Bit = STI.is64Bit();
83 const X86InstrInfo *TII = STI.getInstrInfo();
84
85 // Create a virtual register for the TLS base address.
86 MachineRegisterInfo &RegInfo = MF->getRegInfo();
87 *TLSBaseAddrReg = RegInfo.createVirtualRegister(RegClass: is64Bit ? &X86::GR64RegClass
88 : &X86::GR32RegClass);
89
90 // Insert a copy from RAX/EAX to TLSBaseAddrReg.
91 MachineInstr *Next = I.getNextNode();
92 MachineInstr *Copy = BuildMI(BB&: *I.getParent(), I: Next, MIMD: I.getDebugLoc(),
93 MCID: TII->get(Opcode: TargetOpcode::COPY), DestReg: *TLSBaseAddrReg)
94 .addReg(RegNo: is64Bit ? X86::RAX : X86::EAX);
95
96 return Copy;
97}
98
99// Visit the dominator subtree rooted at Node in pre-order.
100// If TLSBaseAddrReg is non-null, then use that to replace any
101// TLS_base_addr instructions. Otherwise, create the register
102// when the first such instruction is seen, and then use it
103// as we encounter more instructions.
104static bool VisitNode(MachineDomTreeNode *Node, Register TLSBaseAddrReg) {
105 MachineBasicBlock *BB = Node->getBlock();
106 bool Changed = false;
107
108 // Traverse the current block.
109 for (MachineBasicBlock::iterator I = BB->begin(), E = BB->end(); I != E;
110 ++I) {
111 switch (I->getOpcode()) {
112 case X86::TLS_base_addr32:
113 case X86::TLS_base_addr64:
114 if (TLSBaseAddrReg)
115 I = ReplaceTLSBaseAddrCall(I&: *I, TLSBaseAddrReg);
116 else
117 I = SetRegister(I&: *I, TLSBaseAddrReg: &TLSBaseAddrReg);
118 Changed = true;
119 break;
120 default:
121 break;
122 }
123 }
124
125 // Visit the children of this block in the dominator tree.
126 for (MachineDomTreeNode *I : Node->children())
127 Changed |= VisitNode(Node: I, TLSBaseAddrReg);
128
129 return Changed;
130}
131
132static bool cleanupLocalDynamicTLS(MachineDominatorTree &DT) {
133 return VisitNode(Node: DT.getRootNode(), TLSBaseAddrReg: Register());
134}
135
136static bool shouldSkipLocalDynamicTLS(MachineFunction &MF) {
137 X86MachineFunctionInfo *MFI = MF.getInfo<X86MachineFunctionInfo>();
138 if (MFI->getNumLocalDynamicTLSAccesses() < 2) {
139 // No point folding accesses if there isn't at least two.
140 return true;
141 }
142 return false;
143}
144
145bool X86CleanupLocalDynamicTLSLegacy::runOnMachineFunction(
146 MachineFunction &MF) {
147 if (skipFunction(F: MF.getFunction()) || shouldSkipLocalDynamicTLS(MF))
148 return false;
149
150 MachineDominatorTree &DT =
151 getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
152 return cleanupLocalDynamicTLS(DT);
153}
154
155PreservedAnalyses
156X86CleanupLocalDynamicTLSPass::run(MachineFunction &MF,
157 MachineFunctionAnalysisManager &MFAM) {
158 if (shouldSkipLocalDynamicTLS(MF))
159 return PreservedAnalyses::all();
160
161 MachineDominatorTree &DT = MFAM.getResult<MachineDominatorTreeAnalysis>(IR&: MF);
162 return cleanupLocalDynamicTLS(DT) ? getMachineFunctionPassPreservedAnalyses()
163 .preserveSet<CFGAnalyses>()
164 : PreservedAnalyses::all();
165}
166