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"
16#include "SystemZMachineFunctionInfo.h"
17#include "SystemZTargetMachine.h"
18#include "llvm/CodeGen/MachineDominators.h"
19#include "llvm/CodeGen/MachineFunctionAnalysisManager.h"
20#include "llvm/CodeGen/MachineFunctionPass.h"
21#include "llvm/CodeGen/MachineInstrBuilder.h"
22#include "llvm/CodeGen/MachinePassManager.h"
23#include "llvm/CodeGen/MachineRegisterInfo.h"
24#include "llvm/CodeGen/TargetInstrInfo.h"
25#include "llvm/CodeGen/TargetRegisterInfo.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
48FunctionPass *llvm::createSystemZLDCleanupLegacyPass(SystemZTargetMachine &TM) {
49 return new SystemZLDCleanupLegacy();
50}
51
52void SystemZLDCleanupLegacy::getAnalysisUsage(AnalysisUsage &AU) const {
53 AU.setPreservesCFG();
54 AU.addRequired<MachineDominatorTreeWrapperPass>();
55 MachineFunctionPass::getAnalysisUsage(AU);
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(BB&: *I->getParent(), I, MIMD: I->getDebugLoc(),
64 MCID: TII->get(Opcode: TargetOpcode::COPY), DestReg: SystemZ::R2D)
65 .addReg(RegNo: 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.
75static MachineInstr *setRegister(MachineFunction &MF, MachineInstr *I,
76 unsigned *TLSBaseAddrReg,
77 const TargetInstrInfo *TII) {
78 // Create a virtual register for the TLS base address.
79 MachineRegisterInfo &RegInfo = MF.getRegInfo();
80 *TLSBaseAddrReg = RegInfo.createVirtualRegister(RegClass: &SystemZ::GR64BitRegClass);
81
82 // Insert a copy from R2 to TLSBaseAddrReg.
83 MachineInstr *Next = I->getNextNode();
84 MachineInstr *Copy = BuildMI(BB&: *I->getParent(), I: Next, MIMD: I->getDebugLoc(),
85 MCID: TII->get(Opcode: TargetOpcode::COPY), DestReg: *TLSBaseAddrReg)
86 .addReg(RegNo: 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.
96static bool visitNode(MachineFunction &MF, MachineDomTreeNode *Node,
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: &*I, TLSBaseAddrReg, TII);
107 else
108 I = setRegister(MF, I: &*I, TLSBaseAddrReg: &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, Node: Child, TLSBaseAddrReg, TII);
119
120 return Changed;
121}
122
123static bool runLDCleanupOnMF(MachineFunction &MF,
124 function_ref<MachineDominatorTree *()> GetMDT) {
125 const TargetInstrInfo *TII =
126 MF.getSubtarget<SystemZSubtarget>().getInstrInfo();
127
128 SystemZMachineFunctionInfo *MFI = MF.getInfo<SystemZMachineFunctionInfo>();
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, Node: DT->getRootNode(), TLSBaseAddrReg: 0, TII);
136}
137
138bool SystemZLDCleanupLegacy::runOnMachineFunction(MachineFunction &MF) {
139 if (skipFunction(F: MF.getFunction()))
140 return false;
141
142 return runLDCleanupOnMF(MF, GetMDT: [&]() {
143 return &getAnalysis<MachineDominatorTreeWrapperPass>().getDomTree();
144 });
145}
146
147PreservedAnalyses
148SystemZLDCleanupPass::run(MachineFunction &MF,
149 MachineFunctionAnalysisManager &MFAM) {
150 bool Changed = runLDCleanupOnMF(
151 MF, GetMDT: [&]() { return &MFAM.getResult<MachineDominatorTreeAnalysis>(IR&: MF); });
152 if (!Changed)
153 return PreservedAnalyses::all();
154 return getMachineFunctionPassPreservedAnalyses().preserveSet<CFGAnalyses>();
155}
156