1*9880d681SAndroid Build Coastguard Worker //===-- SystemZLDCleanup.cpp - Clean up local-dynamic TLS accesses --------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This pass combines multiple accesses to local-dynamic TLS variables so that
11*9880d681SAndroid Build Coastguard Worker // the TLS base address for the module is only fetched once per execution path
12*9880d681SAndroid Build Coastguard Worker // through the function.
13*9880d681SAndroid Build Coastguard Worker //
14*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
15*9880d681SAndroid Build Coastguard Worker
16*9880d681SAndroid Build Coastguard Worker #include "SystemZTargetMachine.h"
17*9880d681SAndroid Build Coastguard Worker #include "SystemZMachineFunctionInfo.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineDominators.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineFunctionPass.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineInstrBuilder.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/CodeGen/MachineRegisterInfo.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetInstrInfo.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetMachine.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/Target/TargetRegisterInfo.h"
25*9880d681SAndroid Build Coastguard Worker
26*9880d681SAndroid Build Coastguard Worker using namespace llvm;
27*9880d681SAndroid Build Coastguard Worker
28*9880d681SAndroid Build Coastguard Worker namespace {
29*9880d681SAndroid Build Coastguard Worker
30*9880d681SAndroid Build Coastguard Worker class SystemZLDCleanup : public MachineFunctionPass {
31*9880d681SAndroid Build Coastguard Worker public:
32*9880d681SAndroid Build Coastguard Worker static char ID;
SystemZLDCleanup(const SystemZTargetMachine & tm)33*9880d681SAndroid Build Coastguard Worker SystemZLDCleanup(const SystemZTargetMachine &tm)
34*9880d681SAndroid Build Coastguard Worker : MachineFunctionPass(ID), TII(nullptr), MF(nullptr) {}
35*9880d681SAndroid Build Coastguard Worker
getPassName() const36*9880d681SAndroid Build Coastguard Worker const char *getPassName() const override {
37*9880d681SAndroid Build Coastguard Worker return "SystemZ Local Dynamic TLS Access Clean-up";
38*9880d681SAndroid Build Coastguard Worker }
39*9880d681SAndroid Build Coastguard Worker
40*9880d681SAndroid Build Coastguard Worker bool runOnMachineFunction(MachineFunction &MF) override;
41*9880d681SAndroid Build Coastguard Worker void getAnalysisUsage(AnalysisUsage &AU) const override;
42*9880d681SAndroid Build Coastguard Worker
43*9880d681SAndroid Build Coastguard Worker private:
44*9880d681SAndroid Build Coastguard Worker bool VisitNode(MachineDomTreeNode *Node, unsigned TLSBaseAddrReg);
45*9880d681SAndroid Build Coastguard Worker MachineInstr *ReplaceTLSCall(MachineInstr *I, unsigned TLSBaseAddrReg);
46*9880d681SAndroid Build Coastguard Worker MachineInstr *SetRegister(MachineInstr *I, unsigned *TLSBaseAddrReg);
47*9880d681SAndroid Build Coastguard Worker
48*9880d681SAndroid Build Coastguard Worker const SystemZInstrInfo *TII;
49*9880d681SAndroid Build Coastguard Worker MachineFunction *MF;
50*9880d681SAndroid Build Coastguard Worker };
51*9880d681SAndroid Build Coastguard Worker
52*9880d681SAndroid Build Coastguard Worker char SystemZLDCleanup::ID = 0;
53*9880d681SAndroid Build Coastguard Worker
54*9880d681SAndroid Build Coastguard Worker } // end anonymous namespace
55*9880d681SAndroid Build Coastguard Worker
createSystemZLDCleanupPass(SystemZTargetMachine & TM)56*9880d681SAndroid Build Coastguard Worker FunctionPass *llvm::createSystemZLDCleanupPass(SystemZTargetMachine &TM) {
57*9880d681SAndroid Build Coastguard Worker return new SystemZLDCleanup(TM);
58*9880d681SAndroid Build Coastguard Worker }
59*9880d681SAndroid Build Coastguard Worker
getAnalysisUsage(AnalysisUsage & AU) const60*9880d681SAndroid Build Coastguard Worker void SystemZLDCleanup::getAnalysisUsage(AnalysisUsage &AU) const {
61*9880d681SAndroid Build Coastguard Worker AU.setPreservesCFG();
62*9880d681SAndroid Build Coastguard Worker AU.addRequired<MachineDominatorTree>();
63*9880d681SAndroid Build Coastguard Worker MachineFunctionPass::getAnalysisUsage(AU);
64*9880d681SAndroid Build Coastguard Worker }
65*9880d681SAndroid Build Coastguard Worker
runOnMachineFunction(MachineFunction & F)66*9880d681SAndroid Build Coastguard Worker bool SystemZLDCleanup::runOnMachineFunction(MachineFunction &F) {
67*9880d681SAndroid Build Coastguard Worker if (skipFunction(*F.getFunction()))
68*9880d681SAndroid Build Coastguard Worker return false;
69*9880d681SAndroid Build Coastguard Worker
70*9880d681SAndroid Build Coastguard Worker TII = static_cast<const SystemZInstrInfo *>(F.getSubtarget().getInstrInfo());
71*9880d681SAndroid Build Coastguard Worker MF = &F;
72*9880d681SAndroid Build Coastguard Worker
73*9880d681SAndroid Build Coastguard Worker SystemZMachineFunctionInfo* MFI = F.getInfo<SystemZMachineFunctionInfo>();
74*9880d681SAndroid Build Coastguard Worker if (MFI->getNumLocalDynamicTLSAccesses() < 2) {
75*9880d681SAndroid Build Coastguard Worker // No point folding accesses if there isn't at least two.
76*9880d681SAndroid Build Coastguard Worker return false;
77*9880d681SAndroid Build Coastguard Worker }
78*9880d681SAndroid Build Coastguard Worker
79*9880d681SAndroid Build Coastguard Worker MachineDominatorTree *DT = &getAnalysis<MachineDominatorTree>();
80*9880d681SAndroid Build Coastguard Worker return VisitNode(DT->getRootNode(), 0);
81*9880d681SAndroid Build Coastguard Worker }
82*9880d681SAndroid Build Coastguard Worker
83*9880d681SAndroid Build Coastguard Worker // Visit the dominator subtree rooted at Node in pre-order.
84*9880d681SAndroid Build Coastguard Worker // If TLSBaseAddrReg is non-null, then use that to replace any
85*9880d681SAndroid Build Coastguard Worker // TLS_LDCALL instructions. Otherwise, create the register
86*9880d681SAndroid Build Coastguard Worker // when the first such instruction is seen, and then use it
87*9880d681SAndroid Build Coastguard Worker // as we encounter more instructions.
VisitNode(MachineDomTreeNode * Node,unsigned TLSBaseAddrReg)88*9880d681SAndroid Build Coastguard Worker bool SystemZLDCleanup::VisitNode(MachineDomTreeNode *Node,
89*9880d681SAndroid Build Coastguard Worker unsigned TLSBaseAddrReg) {
90*9880d681SAndroid Build Coastguard Worker MachineBasicBlock *BB = Node->getBlock();
91*9880d681SAndroid Build Coastguard Worker bool Changed = false;
92*9880d681SAndroid Build Coastguard Worker
93*9880d681SAndroid Build Coastguard Worker // Traverse the current block.
94*9880d681SAndroid Build Coastguard Worker for (auto I = BB->begin(), E = BB->end(); I != E; ++I) {
95*9880d681SAndroid Build Coastguard Worker switch (I->getOpcode()) {
96*9880d681SAndroid Build Coastguard Worker case SystemZ::TLS_LDCALL:
97*9880d681SAndroid Build Coastguard Worker if (TLSBaseAddrReg)
98*9880d681SAndroid Build Coastguard Worker I = ReplaceTLSCall(&*I, TLSBaseAddrReg);
99*9880d681SAndroid Build Coastguard Worker else
100*9880d681SAndroid Build Coastguard Worker I = SetRegister(&*I, &TLSBaseAddrReg);
101*9880d681SAndroid Build Coastguard Worker Changed = true;
102*9880d681SAndroid Build Coastguard Worker break;
103*9880d681SAndroid Build Coastguard Worker default:
104*9880d681SAndroid Build Coastguard Worker break;
105*9880d681SAndroid Build Coastguard Worker }
106*9880d681SAndroid Build Coastguard Worker }
107*9880d681SAndroid Build Coastguard Worker
108*9880d681SAndroid Build Coastguard Worker // Visit the children of this block in the dominator tree.
109*9880d681SAndroid Build Coastguard Worker for (auto I = Node->begin(), E = Node->end(); I != E; ++I)
110*9880d681SAndroid Build Coastguard Worker Changed |= VisitNode(*I, TLSBaseAddrReg);
111*9880d681SAndroid Build Coastguard Worker
112*9880d681SAndroid Build Coastguard Worker return Changed;
113*9880d681SAndroid Build Coastguard Worker }
114*9880d681SAndroid Build Coastguard Worker
115*9880d681SAndroid Build Coastguard Worker // Replace the TLS_LDCALL instruction I with a copy from TLSBaseAddrReg,
116*9880d681SAndroid Build Coastguard Worker // returning the new instruction.
ReplaceTLSCall(MachineInstr * I,unsigned TLSBaseAddrReg)117*9880d681SAndroid Build Coastguard Worker MachineInstr *SystemZLDCleanup::ReplaceTLSCall(MachineInstr *I,
118*9880d681SAndroid Build Coastguard Worker unsigned TLSBaseAddrReg) {
119*9880d681SAndroid Build Coastguard Worker // Insert a Copy from TLSBaseAddrReg to R2.
120*9880d681SAndroid Build Coastguard Worker MachineInstr *Copy = BuildMI(*I->getParent(), I, I->getDebugLoc(),
121*9880d681SAndroid Build Coastguard Worker TII->get(TargetOpcode::COPY), SystemZ::R2D)
122*9880d681SAndroid Build Coastguard Worker .addReg(TLSBaseAddrReg);
123*9880d681SAndroid Build Coastguard Worker
124*9880d681SAndroid Build Coastguard Worker // Erase the TLS_LDCALL instruction.
125*9880d681SAndroid Build Coastguard Worker I->eraseFromParent();
126*9880d681SAndroid Build Coastguard Worker
127*9880d681SAndroid Build Coastguard Worker return Copy;
128*9880d681SAndroid Build Coastguard Worker }
129*9880d681SAndroid Build Coastguard Worker
130*9880d681SAndroid Build Coastguard Worker // Create a virtal register in *TLSBaseAddrReg, and populate it by
131*9880d681SAndroid Build Coastguard Worker // inserting a copy instruction after I. Returns the new instruction.
SetRegister(MachineInstr * I,unsigned * TLSBaseAddrReg)132*9880d681SAndroid Build Coastguard Worker MachineInstr *SystemZLDCleanup::SetRegister(MachineInstr *I,
133*9880d681SAndroid Build Coastguard Worker unsigned *TLSBaseAddrReg) {
134*9880d681SAndroid Build Coastguard Worker // Create a virtual register for the TLS base address.
135*9880d681SAndroid Build Coastguard Worker MachineRegisterInfo &RegInfo = MF->getRegInfo();
136*9880d681SAndroid Build Coastguard Worker *TLSBaseAddrReg = RegInfo.createVirtualRegister(&SystemZ::GR64BitRegClass);
137*9880d681SAndroid Build Coastguard Worker
138*9880d681SAndroid Build Coastguard Worker // Insert a copy from R2 to TLSBaseAddrReg.
139*9880d681SAndroid Build Coastguard Worker MachineInstr *Next = I->getNextNode();
140*9880d681SAndroid Build Coastguard Worker MachineInstr *Copy = BuildMI(*I->getParent(), Next, I->getDebugLoc(),
141*9880d681SAndroid Build Coastguard Worker TII->get(TargetOpcode::COPY), *TLSBaseAddrReg)
142*9880d681SAndroid Build Coastguard Worker .addReg(SystemZ::R2D);
143*9880d681SAndroid Build Coastguard Worker
144*9880d681SAndroid Build Coastguard Worker return Copy;
145*9880d681SAndroid Build Coastguard Worker }
146*9880d681SAndroid Build Coastguard Worker
147