LLVM 24.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
15#include "SystemZ.h"
26#include "llvm/IR/Analysis.h"
27
28using namespace llvm;
29
30namespace {
31
32class SystemZLDCleanupLegacy : public MachineFunctionPass {
33public:
34 static char ID;
35 SystemZLDCleanupLegacy() : MachineFunctionPass(ID) {}
36
37 bool runOnMachineFunction(MachineFunction &MF) override;
38 void getAnalysisUsage(AnalysisUsage &AU) const override;
39};
40
41char SystemZLDCleanupLegacy::ID = 0;
42
43} // end anonymous namespace
44
45INITIALIZE_PASS(SystemZLDCleanupLegacy, "systemz-ld-cleanup",
46 "SystemZ Local Dynamic TLS Access Clean-up", false, false)
47
49 return new SystemZLDCleanupLegacy();
50}
51
52void SystemZLDCleanupLegacy::getAnalysisUsage(AnalysisUsage &AU) const {
53 AU.setPreservesCFG();
54 AU.addRequired<MachineDominatorTreeWrapperPass>();
56}
57
58// Replace the TLS_LDCALL instruction I with a copy from TLSBaseAddrReg,
59// returning the new instruction.
60static MachineInstr *replaceTLSCall(MachineInstr *I, unsigned TLSBaseAddrReg,
61 const TargetInstrInfo *TII) {
62 // Insert a Copy from TLSBaseAddrReg to R2.
63 MachineInstr *Copy = BuildMI(*I->getParent(), I, I->getDebugLoc(),
64 TII->get(TargetOpcode::COPY), SystemZ::R2D)
65 .addReg(TLSBaseAddrReg);
66
67 // Erase the TLS_LDCALL instruction.
68 I->eraseFromParent();
69
70 return Copy;
71}
72
73// Create a virtual register in *TLSBaseAddrReg, and populate it by
74// inserting a copy instruction after I. Returns the new instruction.
76 unsigned *TLSBaseAddrReg,
77 const TargetInstrInfo *TII) {
78 // Create a virtual register for the TLS base address.
80 *TLSBaseAddrReg = RegInfo.createVirtualRegister(&SystemZ::GR64BitRegClass);
81
82 // Insert a copy from R2 to TLSBaseAddrReg.
83 MachineInstr *Next = I->getNextNode();
84 MachineInstr *Copy = BuildMI(*I->getParent(), Next, I->getDebugLoc(),
85 TII->get(TargetOpcode::COPY), *TLSBaseAddrReg)
86 .addReg(SystemZ::R2D);
87
88 return Copy;
89}
90
91// Visit the dominator subtree rooted at Node in pre-order.
92// If TLSBaseAddrReg is non-null, then use that to replace any
93// TLS_LDCALL instructions. Otherwise, create the register
94// when the first such instruction is seen, and then use it
95// as we encounter more instructions.
97 unsigned TLSBaseAddrReg, const TargetInstrInfo *TII) {
98 MachineBasicBlock *BB = Node->getBlock();
99 bool Changed = false;
100
101 // Traverse the current block.
102 for (auto I = BB->begin(), E = BB->end(); I != E; ++I) {
103 switch (I->getOpcode()) {
104 case SystemZ::TLS_LDCALL:
105 if (TLSBaseAddrReg)
106 I = replaceTLSCall(&*I, TLSBaseAddrReg, TII);
107 else
108 I = setRegister(MF, &*I, &TLSBaseAddrReg, TII);
109 Changed = true;
110 break;
111 default:
112 break;
113 }
114 }
115
116 // Visit the children of this block in the dominator tree.
117 for (MachineDomTreeNode *Child : *Node)
118 Changed |= visitNode(MF, Child, TLSBaseAddrReg, TII);
119
120 return Changed;
121}
122
125 const TargetInstrInfo *TII =
126 MF.getSubtarget<SystemZSubtarget>().getInstrInfo();
127
129 if (MFI->getNumLocalDynamicTLSAccesses() < 2) {
130 // No point folding accesses if there isn't at least two.
131 return false;
132 }
133
134 MachineDominatorTree *DT = GetMDT();
135 return visitNode(MF, DT->getRootNode(), 0, TII);
136}
137
138bool SystemZLDCleanupLegacy::runOnMachineFunction(MachineFunction &MF) {
139 if (skipFunction(MF.getFunction()))
140 return false;
141
142 return runLDCleanupOnMF(MF, [&]() {
143 return &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
144 });
145}
146
147PreservedAnalyses
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
const HexagonInstrInfo * TII
#define I(x, y, z)
Definition MD5.cpp:57
static bool visitNode(MBBInfo &MBBI, ScopedHTType &ScopedHT)
Visit MBB.
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
static MachineInstr * setRegister(MachineFunction &MF, MachineInstr *I, unsigned *TLSBaseAddrReg, const TargetInstrInfo *TII)
static MachineInstr * replaceTLSCall(MachineInstr *I, unsigned TLSBaseAddrReg, const TargetInstrInfo *TII)
static bool runLDCleanupOnMF(MachineFunction &MF, function_ref< MachineDominatorTree *()> GetMDT)
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:280
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:312
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
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
TargetInstrInfo - Interface to description of machine instruction set.
An efficient, type-erasing, non-owning reference to a callable.
Pass manager infrastructure for declaring and invalidating analyses.
This is an optimization pass for GlobalISel generic memory operations.
FunctionPass * createSystemZLDCleanupLegacyPass(SystemZTargetMachine &TM)
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
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Next
Definition InstrProf.h:147