LLVM 22.0.0git
SystemZLDCleanup.cpp
Go to the documentation of this file.
1//===-- SystemZLDCleanup.cpp - Clean up local-dynamic TLS accesses --------===//
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
23
24using namespace llvm;
25
26namespace {
27
28class SystemZLDCleanup : public MachineFunctionPass {
29public:
30 static char ID;
31 SystemZLDCleanup() : MachineFunctionPass(ID), TII(nullptr), MF(nullptr) {}
32
33 bool runOnMachineFunction(MachineFunction &MF) override;
34 void getAnalysisUsage(AnalysisUsage &AU) const override;
35
36private:
37 bool VisitNode(MachineDomTreeNode *Node, unsigned TLSBaseAddrReg);
38 MachineInstr *ReplaceTLSCall(MachineInstr *I, unsigned TLSBaseAddrReg);
39 MachineInstr *SetRegister(MachineInstr *I, unsigned *TLSBaseAddrReg);
40
41 const SystemZInstrInfo *TII;
42 MachineFunction *MF;
43};
44
45char SystemZLDCleanup::ID = 0;
46
47} // end anonymous namespace
48
49INITIALIZE_PASS(SystemZLDCleanup, "systemz-ld-cleanup",
50 "SystemZ Local Dynamic TLS Access Clean-up", false, false)
51
53 return new SystemZLDCleanup();
54}
55
56void SystemZLDCleanup::getAnalysisUsage(AnalysisUsage &AU) const {
57 AU.setPreservesCFG();
58 AU.addRequired<MachineDominatorTreeWrapperPass>();
60}
61
62bool SystemZLDCleanup::runOnMachineFunction(MachineFunction &F) {
63 if (skipFunction(F.getFunction()))
64 return false;
65
66 TII = F.getSubtarget<SystemZSubtarget>().getInstrInfo();
67 MF = &F;
68
69 SystemZMachineFunctionInfo* MFI = F.getInfo<SystemZMachineFunctionInfo>();
70 if (MFI->getNumLocalDynamicTLSAccesses() < 2) {
71 // No point folding accesses if there isn't at least two.
72 return false;
73 }
74
75 MachineDominatorTree *DT =
76 &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
77 return VisitNode(DT->getRootNode(), 0);
78}
79
80// Visit the dominator subtree rooted at Node in pre-order.
81// If TLSBaseAddrReg is non-null, then use that to replace any
82// TLS_LDCALL instructions. Otherwise, create the register
83// when the first such instruction is seen, and then use it
84// as we encounter more instructions.
85bool SystemZLDCleanup::VisitNode(MachineDomTreeNode *Node,
86 unsigned TLSBaseAddrReg) {
87 MachineBasicBlock *BB = Node->getBlock();
88 bool Changed = false;
89
90 // Traverse the current block.
91 for (auto I = BB->begin(), E = BB->end(); I != E; ++I) {
92 switch (I->getOpcode()) {
93 case SystemZ::TLS_LDCALL:
94 if (TLSBaseAddrReg)
95 I = ReplaceTLSCall(&*I, TLSBaseAddrReg);
96 else
97 I = SetRegister(&*I, &TLSBaseAddrReg);
98 Changed = true;
99 break;
100 default:
101 break;
102 }
103 }
104
105 // Visit the children of this block in the dominator tree.
106 for (auto &N : *Node)
107 Changed |= VisitNode(N, TLSBaseAddrReg);
108
109 return Changed;
110}
111
112// Replace the TLS_LDCALL instruction I with a copy from TLSBaseAddrReg,
113// returning the new instruction.
114MachineInstr *SystemZLDCleanup::ReplaceTLSCall(MachineInstr *I,
115 unsigned TLSBaseAddrReg) {
116 // Insert a Copy from TLSBaseAddrReg to R2.
117 MachineInstr *Copy = BuildMI(*I->getParent(), I, I->getDebugLoc(),
118 TII->get(TargetOpcode::COPY), SystemZ::R2D)
119 .addReg(TLSBaseAddrReg);
120
121 // Erase the TLS_LDCALL instruction.
123
124 return Copy;
125}
126
127// Create a virtual register in *TLSBaseAddrReg, and populate it by
128// inserting a copy instruction after I. Returns the new instruction.
129MachineInstr *SystemZLDCleanup::SetRegister(MachineInstr *I,
130 unsigned *TLSBaseAddrReg) {
131 // Create a virtual register for the TLS base address.
132 MachineRegisterInfo &RegInfo = MF->getRegInfo();
133 *TLSBaseAddrReg = RegInfo.createVirtualRegister(&SystemZ::GR64BitRegClass);
134
135 // Insert a copy from R2 to TLSBaseAddrReg.
136 MachineInstr *Next = I->getNextNode();
137 MachineInstr *Copy = BuildMI(*I->getParent(), Next, I->getDebugLoc(),
138 TII->get(TargetOpcode::COPY), *TLSBaseAddrReg)
139 .addReg(SystemZ::R2D);
140
141 return Copy;
142}
143
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
const HexagonInstrInfo * TII
#define F(x, y, z)
Definition MD5.cpp:55
#define I(x, y, z)
Definition MD5.cpp:58
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
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
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
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.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
LLVM_ABI void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
LLVM_ABI Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
Changed
NodeAddr< NodeBase * > Node
Definition RDFGraph.h:381
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.
FunctionPass * createSystemZLDCleanupPass(SystemZTargetMachine &TM)
DomTreeNodeBase< MachineBasicBlock > MachineDomTreeNode
FunctionAddr VTableAddr Next
Definition InstrProf.h:141
#define N