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