LLVM 24.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"
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();
45 }
46};
47} // end anonymous namespace
48
49char X86CleanupLocalDynamicTLSLegacy::ID = 0;
50
52 return new X86CleanupLocalDynamicTLSLegacy();
53}
54
55// Replace the TLS_base_addr instruction I with a copy from
56// TLSBaseAddrReg, returning the new instruction.
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(*I.getParent(), I, I.getDebugLoc(), TII->get(TargetOpcode::COPY),
67 is64Bit ? X86::RAX : X86::EAX)
68 .addReg(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.
86 *TLSBaseAddrReg = RegInfo.createVirtualRegister(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(*I.getParent(), Next, I.getDebugLoc(),
92 TII->get(TargetOpcode::COPY), *TLSBaseAddrReg)
93 .addReg(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, TLSBaseAddrReg);
115 else
116 I = SetRegister(*I, &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(I, TLSBaseAddrReg);
127
128 return Changed;
129}
130
132 return VisitNode(DT.getRootNode(), Register());
133}
134
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(MF.getFunction()) || shouldSkipLocalDynamicTLS(MF))
147 return false;
148
149 MachineDominatorTree &DT =
150 getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
151 return cleanupLocalDynamicTLS(DT);
152}
153
154PreservedAnalyses
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:275
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
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
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
This is an optimization pass for GlobalISel generic memory operations.
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()
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Next
Definition InstrProf.h:147