/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Target/SystemZ/SystemZLDCleanup.cpp
Line | Count | Source (jump to first uncovered line) |
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 "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" |
24 | | |
25 | | using namespace llvm; |
26 | | |
27 | | namespace { |
28 | | |
29 | | class SystemZLDCleanup : public MachineFunctionPass { |
30 | | public: |
31 | | static char ID; |
32 | | SystemZLDCleanup(const SystemZTargetMachine &tm) |
33 | 1.00k | : MachineFunctionPass(ID), TII(nullptr), MF(nullptr) {} |
34 | | |
35 | 8.09k | StringRef getPassName() const override { |
36 | 8.09k | return "SystemZ Local Dynamic TLS Access Clean-up"; |
37 | 8.09k | } |
38 | | |
39 | | bool runOnMachineFunction(MachineFunction &MF) override; |
40 | | void getAnalysisUsage(AnalysisUsage &AU) const override; |
41 | | |
42 | | private: |
43 | | bool VisitNode(MachineDomTreeNode *Node, unsigned TLSBaseAddrReg); |
44 | | MachineInstr *ReplaceTLSCall(MachineInstr *I, unsigned TLSBaseAddrReg); |
45 | | MachineInstr *SetRegister(MachineInstr *I, unsigned *TLSBaseAddrReg); |
46 | | |
47 | | const SystemZInstrInfo *TII; |
48 | | MachineFunction *MF; |
49 | | }; |
50 | | |
51 | | char SystemZLDCleanup::ID = 0; |
52 | | |
53 | | } // end anonymous namespace |
54 | | |
55 | 1.00k | FunctionPass *llvm::createSystemZLDCleanupPass(SystemZTargetMachine &TM) { |
56 | 1.00k | return new SystemZLDCleanup(TM); |
57 | 1.00k | } |
58 | | |
59 | 986 | void SystemZLDCleanup::getAnalysisUsage(AnalysisUsage &AU) const { |
60 | 986 | AU.setPreservesCFG(); |
61 | 986 | AU.addRequired<MachineDominatorTree>(); |
62 | 986 | MachineFunctionPass::getAnalysisUsage(AU); |
63 | 986 | } |
64 | | |
65 | 8.09k | bool SystemZLDCleanup::runOnMachineFunction(MachineFunction &F) { |
66 | 8.09k | if (skipFunction(F.getFunction())) |
67 | 0 | return false; |
68 | 8.09k | |
69 | 8.09k | TII = static_cast<const SystemZInstrInfo *>(F.getSubtarget().getInstrInfo()); |
70 | 8.09k | MF = &F; |
71 | 8.09k | |
72 | 8.09k | SystemZMachineFunctionInfo* MFI = F.getInfo<SystemZMachineFunctionInfo>(); |
73 | 8.09k | if (MFI->getNumLocalDynamicTLSAccesses() < 2) { |
74 | 8.09k | // No point folding accesses if there isn't at least two. |
75 | 8.09k | return false; |
76 | 8.09k | } |
77 | 1 | |
78 | 1 | MachineDominatorTree *DT = &getAnalysis<MachineDominatorTree>(); |
79 | 1 | return VisitNode(DT->getRootNode(), 0); |
80 | 1 | } |
81 | | |
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 | 1 | unsigned TLSBaseAddrReg) { |
89 | 1 | MachineBasicBlock *BB = Node->getBlock(); |
90 | 1 | bool Changed = false; |
91 | 1 | |
92 | 1 | // Traverse the current block. |
93 | 26 | for (auto I = BB->begin(), E = BB->end(); I != E; ++I25 ) { |
94 | 25 | switch (I->getOpcode()) { |
95 | 25 | case SystemZ::TLS_LDCALL: |
96 | 2 | if (TLSBaseAddrReg) |
97 | 1 | I = ReplaceTLSCall(&*I, TLSBaseAddrReg); |
98 | 1 | else |
99 | 1 | I = SetRegister(&*I, &TLSBaseAddrReg); |
100 | 2 | Changed = true; |
101 | 2 | break; |
102 | 25 | default: |
103 | 23 | break; |
104 | 25 | } |
105 | 25 | } |
106 | 1 | |
107 | 1 | // Visit the children of this block in the dominator tree. |
108 | 1 | for (auto I = Node->begin(), E = Node->end(); I != E; ++I0 ) |
109 | 0 | Changed |= VisitNode(*I, TLSBaseAddrReg); |
110 | 1 | |
111 | 1 | return Changed; |
112 | 1 | } |
113 | | |
114 | | // Replace the TLS_LDCALL instruction I with a copy from TLSBaseAddrReg, |
115 | | // returning the new instruction. |
116 | | MachineInstr *SystemZLDCleanup::ReplaceTLSCall(MachineInstr *I, |
117 | 1 | unsigned TLSBaseAddrReg) { |
118 | 1 | // Insert a Copy from TLSBaseAddrReg to R2. |
119 | 1 | MachineInstr *Copy = BuildMI(*I->getParent(), I, I->getDebugLoc(), |
120 | 1 | TII->get(TargetOpcode::COPY), SystemZ::R2D) |
121 | 1 | .addReg(TLSBaseAddrReg); |
122 | 1 | |
123 | 1 | // Erase the TLS_LDCALL instruction. |
124 | 1 | I->eraseFromParent(); |
125 | 1 | |
126 | 1 | return Copy; |
127 | 1 | } |
128 | | |
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 | 1 | unsigned *TLSBaseAddrReg) { |
133 | 1 | // Create a virtual register for the TLS base address. |
134 | 1 | MachineRegisterInfo &RegInfo = MF->getRegInfo(); |
135 | 1 | *TLSBaseAddrReg = RegInfo.createVirtualRegister(&SystemZ::GR64BitRegClass); |
136 | 1 | |
137 | 1 | // Insert a copy from R2 to TLSBaseAddrReg. |
138 | 1 | MachineInstr *Next = I->getNextNode(); |
139 | 1 | MachineInstr *Copy = BuildMI(*I->getParent(), Next, I->getDebugLoc(), |
140 | 1 | TII->get(TargetOpcode::COPY), *TLSBaseAddrReg) |
141 | 1 | .addReg(SystemZ::R2D); |
142 | 1 | |
143 | 1 | return Copy; |
144 | 1 | } |
145 | | |