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