LLVM 19.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
24
25using namespace llvm;
26
27namespace {
28
29class SystemZLDCleanup : public MachineFunctionPass {
30public:
31 static char ID;
32 SystemZLDCleanup() : MachineFunctionPass(ID), TII(nullptr), MF(nullptr) {
34 }
35
36 bool runOnMachineFunction(MachineFunction &MF) override;
37 void getAnalysisUsage(AnalysisUsage &AU) const override;
38
39private:
40 bool VisitNode(MachineDomTreeNode *Node, unsigned TLSBaseAddrReg);
41 MachineInstr *ReplaceTLSCall(MachineInstr *I, unsigned TLSBaseAddrReg);
42 MachineInstr *SetRegister(MachineInstr *I, unsigned *TLSBaseAddrReg);
43
44 const SystemZInstrInfo *TII;
46};
47
48char SystemZLDCleanup::ID = 0;
49
50} // end anonymous namespace
51
52INITIALIZE_PASS(SystemZLDCleanup, "systemz-ld-cleanup",
53 "SystemZ Local Dynamic TLS Access Clean-up", false, false)
54
56 return new SystemZLDCleanup();
57}
58
59void SystemZLDCleanup::getAnalysisUsage(AnalysisUsage &AU) const {
60 AU.setPreservesCFG();
63}
64
65bool SystemZLDCleanup::runOnMachineFunction(MachineFunction &F) {
66 if (skipFunction(F.getFunction()))
67 return false;
68
69 TII = F.getSubtarget<SystemZSubtarget>().getInstrInfo();
70 MF = &F;
71
73 if (MFI->getNumLocalDynamicTLSAccesses() < 2) {
74 // No point folding accesses if there isn't at least two.
75 return false;
76 }
77
79 &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
80 return VisitNode(DT->getRootNode(), 0);
81}
82
83// Visit the dominator subtree rooted at Node in pre-order.
84// If TLSBaseAddrReg is non-null, then use that to replace any
85// TLS_LDCALL instructions. Otherwise, create the register
86// when the first such instruction is seen, and then use it
87// as we encounter more instructions.
88bool SystemZLDCleanup::VisitNode(MachineDomTreeNode *Node,
89 unsigned TLSBaseAddrReg) {
90 MachineBasicBlock *BB = Node->getBlock();
91 bool Changed = false;
92
93 // Traverse the current block.
94 for (auto I = BB->begin(), E = BB->end(); I != E; ++I) {
95 switch (I->getOpcode()) {
96 case SystemZ::TLS_LDCALL:
97 if (TLSBaseAddrReg)
98 I = ReplaceTLSCall(&*I, TLSBaseAddrReg);
99 else
100 I = SetRegister(&*I, &TLSBaseAddrReg);
101 Changed = true;
102 break;
103 default:
104 break;
105 }
106 }
107
108 // Visit the children of this block in the dominator tree.
109 for (auto &N : *Node)
110 Changed |= VisitNode(N, TLSBaseAddrReg);
111
112 return Changed;
113}
114
115// Replace the TLS_LDCALL instruction I with a copy from TLSBaseAddrReg,
116// returning the new instruction.
117MachineInstr *SystemZLDCleanup::ReplaceTLSCall(MachineInstr *I,
118 unsigned TLSBaseAddrReg) {
119 // Insert a Copy from TLSBaseAddrReg to R2.
120 MachineInstr *Copy = BuildMI(*I->getParent(), I, I->getDebugLoc(),
121 TII->get(TargetOpcode::COPY), SystemZ::R2D)
122 .addReg(TLSBaseAddrReg);
123
124 // Erase the TLS_LDCALL instruction.
125 I->eraseFromParent();
126
127 return Copy;
128}
129
130// Create a virtual register in *TLSBaseAddrReg, and populate it by
131// inserting a copy instruction after I. Returns the new instruction.
132MachineInstr *SystemZLDCleanup::SetRegister(MachineInstr *I,
133 unsigned *TLSBaseAddrReg) {
134 // Create a virtual register for the TLS base address.
135 MachineRegisterInfo &RegInfo = MF->getRegInfo();
136 *TLSBaseAddrReg = RegInfo.createVirtualRegister(&SystemZ::GR64BitRegClass);
137
138 // Insert a copy from R2 to TLSBaseAddrReg.
139 MachineInstr *Next = I->getNextNode();
140 MachineInstr *Copy = BuildMI(*I->getParent(), Next, I->getDebugLoc(),
141 TII->get(TargetOpcode::COPY), *TLSBaseAddrReg)
142 .addReg(SystemZ::R2D);
143
144 return Copy;
145}
146
const HexagonInstrInfo * TII
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
const char LLVMTargetMachineRef TM
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
void setPreservesCFG()
This function should be called by the pass, iff they do not:
Definition: Pass.cpp:269
Base class for the actual dominator tree node.
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:311
Analysis pass which computes a MachineDominatorTree.
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
MachineDomTreeNode * getRootNode() const
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.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
Representation of each machine instruction.
Definition: MachineInstr.h:69
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
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: AddressRanges.h:18
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
FunctionPass * createSystemZLDCleanupPass(SystemZTargetMachine &TM)
void initializeSystemZLDCleanupPass(PassRegistry &)
#define N