/Users/buildslave/jenkins/sharedspace/clang-stage2-coverage-R@2/llvm/lib/Target/PowerPC/PPCTLSDynamicCall.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | //===---------- PPCTLSDynamicCall.cpp - TLS Dynamic Call Fixup ------------===// |
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 expands ADDItls{ld,gd}LADDR[32] machine instructions into |
11 | | // separate ADDItls[gd]L[32] and GETtlsADDR[32] instructions, both of |
12 | | // which define GPR3. A copy is added from GPR3 to the target virtual |
13 | | // register of the original instruction. The GETtlsADDR[32] is really |
14 | | // a call instruction, so its target register is constrained to be GPR3. |
15 | | // This is not true of ADDItls[gd]L[32], but there is a legacy linker |
16 | | // optimization bug that requires the target register of the addi of |
17 | | // a local- or general-dynamic TLS access sequence to be GPR3. |
18 | | // |
19 | | // This is done in a late pass so that TLS variable accesses can be |
20 | | // fully commoned by MachineCSE. |
21 | | // |
22 | | //===----------------------------------------------------------------------===// |
23 | | |
24 | | #include "PPC.h" |
25 | | #include "PPCInstrBuilder.h" |
26 | | #include "PPCInstrInfo.h" |
27 | | #include "PPCTargetMachine.h" |
28 | | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
29 | | #include "llvm/CodeGen/MachineFunctionPass.h" |
30 | | #include "llvm/CodeGen/MachineInstrBuilder.h" |
31 | | #include "llvm/Support/Debug.h" |
32 | | #include "llvm/Support/raw_ostream.h" |
33 | | |
34 | | using namespace llvm; |
35 | | |
36 | | #define DEBUG_TYPE "ppc-tls-dynamic-call" |
37 | | |
38 | | namespace llvm { |
39 | | void initializePPCTLSDynamicCallPass(PassRegistry&); |
40 | | } |
41 | | |
42 | | namespace { |
43 | | struct PPCTLSDynamicCall : public MachineFunctionPass { |
44 | | static char ID; |
45 | 931 | PPCTLSDynamicCall() : MachineFunctionPass(ID) { |
46 | 931 | initializePPCTLSDynamicCallPass(*PassRegistry::getPassRegistry()); |
47 | 931 | } |
48 | | |
49 | | const PPCInstrInfo *TII; |
50 | | LiveIntervals *LIS; |
51 | | |
52 | | protected: |
53 | 9.98k | bool processBlock(MachineBasicBlock &MBB) { |
54 | 9.98k | bool Changed = false; |
55 | 9.98k | bool NeedFence = true; |
56 | 9.98k | bool Is64Bit = MBB.getParent()->getSubtarget<PPCSubtarget>().isPPC64(); |
57 | 9.98k | |
58 | 9.98k | for (MachineBasicBlock::iterator I = MBB.begin(), IE = MBB.end(); |
59 | 80.0k | I != IE80.0k ;) { |
60 | 70.0k | MachineInstr &MI = *I; |
61 | 70.0k | |
62 | 70.0k | if (MI.getOpcode() != PPC::ADDItlsgdLADDR && |
63 | 70.0k | MI.getOpcode() != PPC::ADDItlsldLADDR && |
64 | 70.0k | MI.getOpcode() != PPC::ADDItlsgdLADDR32 && |
65 | 70.0k | MI.getOpcode() != PPC::ADDItlsldLADDR3270.0k ) { |
66 | 70.0k | |
67 | 70.0k | // Although we create ADJCALLSTACKDOWN and ADJCALLSTACKUP |
68 | 70.0k | // as scheduling fences, we skip creating fences if we already |
69 | 70.0k | // have existing ADJCALLSTACKDOWN/UP to avoid nesting, |
70 | 70.0k | // which causes verification error with -verify-machineinstrs. |
71 | 70.0k | if (MI.getOpcode() == PPC::ADJCALLSTACKDOWN) |
72 | 1.20k | NeedFence = false; |
73 | 68.8k | else if (68.8k MI.getOpcode() == PPC::ADJCALLSTACKUP68.8k ) |
74 | 1.20k | NeedFence = true; |
75 | 70.0k | |
76 | 70.0k | ++I; |
77 | 70.0k | continue; |
78 | 70.0k | } |
79 | 20 | |
80 | 20 | DEBUG20 (dbgs() << "TLS Dynamic Call Fixup:\n " << MI); |
81 | 20 | |
82 | 20 | unsigned OutReg = MI.getOperand(0).getReg(); |
83 | 20 | unsigned InReg = MI.getOperand(1).getReg(); |
84 | 20 | DebugLoc DL = MI.getDebugLoc(); |
85 | 20 | unsigned GPR3 = Is64Bit ? PPC::X316 : PPC::R34 ; |
86 | 20 | unsigned Opc1, Opc2; |
87 | 20 | const unsigned OrigRegs[] = {OutReg, InReg, GPR3}; |
88 | 20 | |
89 | 20 | switch (MI.getOpcode()) { |
90 | 0 | default: |
91 | 0 | llvm_unreachable("Opcode inconsistency error"); |
92 | 12 | case PPC::ADDItlsgdLADDR: |
93 | 12 | Opc1 = PPC::ADDItlsgdL; |
94 | 12 | Opc2 = PPC::GETtlsADDR; |
95 | 12 | break; |
96 | 4 | case PPC::ADDItlsldLADDR: |
97 | 4 | Opc1 = PPC::ADDItlsldL; |
98 | 4 | Opc2 = PPC::GETtlsldADDR; |
99 | 4 | break; |
100 | 2 | case PPC::ADDItlsgdLADDR32: |
101 | 2 | Opc1 = PPC::ADDItlsgdL32; |
102 | 2 | Opc2 = PPC::GETtlsADDR32; |
103 | 2 | break; |
104 | 2 | case PPC::ADDItlsldLADDR32: |
105 | 2 | Opc1 = PPC::ADDItlsldL32; |
106 | 2 | Opc2 = PPC::GETtlsldADDR32; |
107 | 2 | break; |
108 | 20 | } |
109 | 20 | |
110 | 20 | // We create ADJCALLSTACKUP and ADJCALLSTACKDOWN around _tls_get_addr |
111 | 20 | // as schduling fence to avoid it is scheduled before |
112 | 20 | // mflr in the prologue and the address in LR is clobbered (PR25839). |
113 | 20 | // We don't really need to save data to the stack - the clobbered |
114 | 20 | // registers are already saved when the SDNode (e.g. PPCaddiTlsgdLAddr) |
115 | 20 | // gets translated to the pseudo instruction (e.g. ADDItlsgdLADDR). |
116 | 20 | if (20 NeedFence20 ) |
117 | 19 | BuildMI(MBB, I, DL, TII->get(PPC::ADJCALLSTACKDOWN)).addImm(0) |
118 | 19 | .addImm(0); |
119 | 20 | |
120 | 20 | // Expand into two ops built prior to the existing instruction. |
121 | 20 | MachineInstr *Addi = BuildMI(MBB, I, DL, TII->get(Opc1), GPR3) |
122 | 20 | .addReg(InReg); |
123 | 20 | Addi->addOperand(MI.getOperand(2)); |
124 | 20 | |
125 | 20 | // The ADDItls* instruction is the first instruction in the |
126 | 20 | // repair range. |
127 | 20 | MachineBasicBlock::iterator First = I; |
128 | 20 | --First; |
129 | 20 | |
130 | 20 | MachineInstr *Call = (BuildMI(MBB, I, DL, TII->get(Opc2), GPR3) |
131 | 20 | .addReg(GPR3)); |
132 | 20 | Call->addOperand(MI.getOperand(3)); |
133 | 20 | |
134 | 20 | if (NeedFence) |
135 | 19 | BuildMI(MBB, I, DL, TII->get(PPC::ADJCALLSTACKUP)).addImm(0).addImm(0); |
136 | 70.0k | |
137 | 70.0k | BuildMI(MBB, I, DL, TII->get(TargetOpcode::COPY), OutReg) |
138 | 70.0k | .addReg(GPR3); |
139 | 70.0k | |
140 | 70.0k | // The COPY is the last instruction in the repair range. |
141 | 70.0k | MachineBasicBlock::iterator Last = I; |
142 | 70.0k | --Last; |
143 | 70.0k | |
144 | 70.0k | // Move past the original instruction and remove it. |
145 | 70.0k | ++I; |
146 | 70.0k | MI.removeFromParent(); |
147 | 70.0k | |
148 | 70.0k | // Repair the live intervals. |
149 | 70.0k | LIS->repairIntervalsInRange(&MBB, First, Last, OrigRegs); |
150 | 70.0k | Changed = true; |
151 | 70.0k | } |
152 | 9.98k | |
153 | 9.98k | return Changed; |
154 | 9.98k | } |
155 | | |
156 | | public: |
157 | 5.96k | bool runOnMachineFunction(MachineFunction &MF) override { |
158 | 5.96k | TII = MF.getSubtarget<PPCSubtarget>().getInstrInfo(); |
159 | 5.96k | LIS = &getAnalysis<LiveIntervals>(); |
160 | 5.96k | |
161 | 5.96k | bool Changed = false; |
162 | 5.96k | |
163 | 15.9k | for (MachineFunction::iterator I = MF.begin(); I != MF.end()15.9k ;) { |
164 | 9.98k | MachineBasicBlock &B = *I++; |
165 | 9.98k | if (processBlock(B)) |
166 | 19 | Changed = true; |
167 | 9.98k | } |
168 | 5.96k | |
169 | 5.96k | return Changed; |
170 | 5.96k | } |
171 | | |
172 | 924 | void getAnalysisUsage(AnalysisUsage &AU) const override { |
173 | 924 | AU.addRequired<LiveIntervals>(); |
174 | 924 | AU.addPreserved<LiveIntervals>(); |
175 | 924 | AU.addRequired<SlotIndexes>(); |
176 | 924 | AU.addPreserved<SlotIndexes>(); |
177 | 924 | MachineFunctionPass::getAnalysisUsage(AU); |
178 | 924 | } |
179 | | }; |
180 | | } |
181 | | |
182 | 90.0k | INITIALIZE_PASS_BEGIN90.0k (PPCTLSDynamicCall, DEBUG_TYPE,
|
183 | 90.0k | "PowerPC TLS Dynamic Call Fixup", false, false) |
184 | 90.0k | INITIALIZE_PASS_DEPENDENCY(LiveIntervals) |
185 | 90.0k | INITIALIZE_PASS_DEPENDENCY(SlotIndexes) |
186 | 90.0k | INITIALIZE_PASS_END(PPCTLSDynamicCall, DEBUG_TYPE, |
187 | | "PowerPC TLS Dynamic Call Fixup", false, false) |
188 | | |
189 | | char PPCTLSDynamicCall::ID = 0; |
190 | | FunctionPass* |
191 | 929 | llvm::createPPCTLSDynamicCallPass() { return new PPCTLSDynamicCall(); } |