1 //===-- SystemZLDCleanup.cpp - Clean up local-dynamic TLS accesses --------===//
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
7 //===----------------------------------------------------------------------===//
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.
13 //===----------------------------------------------------------------------===//
15 #include "SystemZMachineFunctionInfo.h"
16 #include "SystemZTargetMachine.h"
17 #include "llvm/CodeGen/MachineDominators.h"
18 #include "llvm/CodeGen/MachineFunctionPass.h"
19 #include "llvm/CodeGen/MachineInstrBuilder.h"
20 #include "llvm/CodeGen/MachineRegisterInfo.h"
21 #include "llvm/CodeGen/TargetInstrInfo.h"
22 #include "llvm/CodeGen/TargetRegisterInfo.h"
23 #include "llvm/Target/TargetMachine.h"
29 class SystemZLDCleanup
: public MachineFunctionPass
{
32 SystemZLDCleanup(const SystemZTargetMachine
&tm
)
33 : MachineFunctionPass(ID
), TII(nullptr), MF(nullptr) {}
35 StringRef
getPassName() const override
{
36 return "SystemZ Local Dynamic TLS Access Clean-up";
39 bool runOnMachineFunction(MachineFunction
&MF
) override
;
40 void getAnalysisUsage(AnalysisUsage
&AU
) const override
;
43 bool VisitNode(MachineDomTreeNode
*Node
, unsigned TLSBaseAddrReg
);
44 MachineInstr
*ReplaceTLSCall(MachineInstr
*I
, unsigned TLSBaseAddrReg
);
45 MachineInstr
*SetRegister(MachineInstr
*I
, unsigned *TLSBaseAddrReg
);
47 const SystemZInstrInfo
*TII
;
51 char SystemZLDCleanup::ID
= 0;
53 } // end anonymous namespace
55 FunctionPass
*llvm::createSystemZLDCleanupPass(SystemZTargetMachine
&TM
) {
56 return new SystemZLDCleanup(TM
);
59 void SystemZLDCleanup::getAnalysisUsage(AnalysisUsage
&AU
) const {
61 AU
.addRequired
<MachineDominatorTree
>();
62 MachineFunctionPass::getAnalysisUsage(AU
);
65 bool SystemZLDCleanup::runOnMachineFunction(MachineFunction
&F
) {
66 if (skipFunction(F
.getFunction()))
69 TII
= static_cast<const SystemZInstrInfo
*>(F
.getSubtarget().getInstrInfo());
72 SystemZMachineFunctionInfo
* MFI
= F
.getInfo
<SystemZMachineFunctionInfo
>();
73 if (MFI
->getNumLocalDynamicTLSAccesses() < 2) {
74 // No point folding accesses if there isn't at least two.
78 MachineDominatorTree
*DT
= &getAnalysis
<MachineDominatorTree
>();
79 return VisitNode(DT
->getRootNode(), 0);
82 // Visit the dominator subtree rooted at Node in pre-order.
83 // If TLSBaseAddrReg is non-null, then use that to replace any
84 // TLS_LDCALL instructions. Otherwise, create the register
85 // when the first such instruction is seen, and then use it
86 // as we encounter more instructions.
87 bool SystemZLDCleanup::VisitNode(MachineDomTreeNode
*Node
,
88 unsigned TLSBaseAddrReg
) {
89 MachineBasicBlock
*BB
= Node
->getBlock();
92 // Traverse the current block.
93 for (auto I
= BB
->begin(), E
= BB
->end(); I
!= E
; ++I
) {
94 switch (I
->getOpcode()) {
95 case SystemZ::TLS_LDCALL
:
97 I
= ReplaceTLSCall(&*I
, TLSBaseAddrReg
);
99 I
= SetRegister(&*I
, &TLSBaseAddrReg
);
107 // Visit the children of this block in the dominator tree.
108 for (auto I
= Node
->begin(), E
= Node
->end(); I
!= E
; ++I
)
109 Changed
|= VisitNode(*I
, TLSBaseAddrReg
);
114 // Replace the TLS_LDCALL instruction I with a copy from TLSBaseAddrReg,
115 // returning the new instruction.
116 MachineInstr
*SystemZLDCleanup::ReplaceTLSCall(MachineInstr
*I
,
117 unsigned TLSBaseAddrReg
) {
118 // Insert a Copy from TLSBaseAddrReg to R2.
119 MachineInstr
*Copy
= BuildMI(*I
->getParent(), I
, I
->getDebugLoc(),
120 TII
->get(TargetOpcode::COPY
), SystemZ::R2D
)
121 .addReg(TLSBaseAddrReg
);
123 // Erase the TLS_LDCALL instruction.
124 I
->eraseFromParent();
129 // Create a virtual register in *TLSBaseAddrReg, and populate it by
130 // inserting a copy instruction after I. Returns the new instruction.
131 MachineInstr
*SystemZLDCleanup::SetRegister(MachineInstr
*I
,
132 unsigned *TLSBaseAddrReg
) {
133 // Create a virtual register for the TLS base address.
134 MachineRegisterInfo
&RegInfo
= MF
->getRegInfo();
135 *TLSBaseAddrReg
= RegInfo
.createVirtualRegister(&SystemZ::GR64BitRegClass
);
137 // Insert a copy from R2 to TLSBaseAddrReg.
138 MachineInstr
*Next
= I
->getNextNode();
139 MachineInstr
*Copy
= BuildMI(*I
->getParent(), Next
, I
->getDebugLoc(),
140 TII
->get(TargetOpcode::COPY
), *TLSBaseAddrReg
)
141 .addReg(SystemZ::R2D
);