LLVM 23.0.0git
X86CleanupLocalDynamicTLS.cpp
Go to the documentation of this file.
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"
18#include "X86Subtarget.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();
46 }
47};
48} // end anonymous namespace
49
50char X86CleanupLocalDynamicTLSLegacy::ID = 0;
51
53 return new X86CleanupLocalDynamicTLSLegacy();
54}
55
56// Replace the TLS_base_addr instruction I with a copy from
57// TLSBaseAddrReg, returning the new instruction.
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(*I.getParent(), I, I.getDebugLoc(), TII->get(TargetOpcode::COPY),
68 is64Bit ? X86::RAX : X86::EAX)
69 .addReg(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.
87 *TLSBaseAddrReg = RegInfo.createVirtualRegister(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(*I.getParent(), Next, I.getDebugLoc(),
93 TII->get(TargetOpcode::COPY), *TLSBaseAddrReg)
94 .addReg(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, TLSBaseAddrReg);
116 else
117 I = SetRegister(*I, &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(I, TLSBaseAddrReg);
128
129 return Changed;
130}
131
133 return VisitNode(DT.getRootNode(), Register());
134}
135
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(MF.getFunction()) || shouldSkipLocalDynamicTLS(MF))
148 return false;
149
150 MachineDominatorTree &DT =
151 getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
152 return cleanupLocalDynamicTLS(DT);
153}
154
155PreservedAnalyses
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
const HexagonInstrInfo * TII
#define I(x, y, z)
Definition MD5.cpp:57
static MachineInstr * ReplaceTLSBaseAddrCall(MachineInstr &I, Register TLSBaseAddrReg)
static bool VisitNode(MachineDomTreeNode *Node, Register TLSBaseAddrReg)
static bool cleanupLocalDynamicTLS(MachineDominatorTree &DT)
static bool shouldSkipLocalDynamicTLS(MachineFunction &MF)
static MachineInstr * SetRegister(MachineInstr &I, Register *TLSBaseAddrReg)
static bool is64Bit(const char *name)
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition Pass.cpp:270
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
DomTreeNodeBase< NodeT > * getRootNode()
getRootNode - This returns the entry node for the CFG of the function.
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
MachineInstrBundleIterator< MachineInstr > iterator
Analysis pass which computes a MachineDominatorTree.
Analysis pass which computes a MachineDominatorTree.
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
Representation of each machine instruction.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
Wrapper class representing virtual and physical registers.
Definition Register.h:20
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
X86MachineFunctionInfo - This class is derived from MachineFunction and contains private X86 target-s...
const X86InstrInfo * getInstrInfo() const override
Changed
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
DomTreeNodeBase< MachineBasicBlock > MachineDomTreeNode
FunctionPass * createCleanupLocalDynamicTLSLegacyPass()
FunctionAddr VTableAddr Next
Definition InstrProf.h:141