blob: 24fc9293695cfa6d7400eef1be17577031a5fdd5 [file] [log] [blame]
Tom Stellard82d3d452013-01-18 21:15:53 +00001//===-- SILowerControlFlow.cpp - Use predicates for control flow ----------===//
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/// \file
11/// \brief Insert wait instructions for memory reads and writes.
12///
13/// Memory reads and writes are issued asynchronously, so we need to insert
14/// S_WAITCNT instructions when we want to access any of their results or
15/// overwrite any register that's used asynchronously.
16//
17//===----------------------------------------------------------------------===//
18
19#include "AMDGPU.h"
20#include "SIInstrInfo.h"
21#include "SIMachineFunctionInfo.h"
22#include "llvm/CodeGen/MachineFunction.h"
23#include "llvm/CodeGen/MachineFunctionPass.h"
24#include "llvm/CodeGen/MachineInstrBuilder.h"
25#include "llvm/CodeGen/MachineRegisterInfo.h"
26
27using namespace llvm;
28
29namespace {
30
31/// \brief One variable for each of the hardware counters
32typedef union {
33 struct {
34 unsigned VM;
35 unsigned EXP;
36 unsigned LGKM;
37 } Named;
38 unsigned Array[3];
39
40} Counters;
41
42typedef Counters RegCounters[512];
43typedef std::pair<unsigned, unsigned> RegInterval;
44
45class SIInsertWaits : public MachineFunctionPass {
46
47private:
48 static char ID;
49 const SIInstrInfo *TII;
50 const SIRegisterInfo &TRI;
51 const MachineRegisterInfo *MRI;
52
53 /// \brief Constant hardware limits
54 static const Counters WaitCounts;
55
56 /// \brief Constant zero value
57 static const Counters ZeroCounts;
58
59 /// \brief Counter values we have already waited on.
60 Counters WaitedOn;
61
62 /// \brief Counter values for last instruction issued.
63 Counters LastIssued;
64
65 /// \brief Registers used by async instructions.
66 RegCounters UsedRegs;
67
68 /// \brief Registers defined by async instructions.
69 RegCounters DefinedRegs;
70
71 /// \brief Different export instruction types seen since last wait.
72 unsigned ExpInstrTypesSeen;
73
74 /// \brief Get increment/decrement amount for this instruction.
75 Counters getHwCounts(MachineInstr &MI);
76
77 /// \brief Is operand relevant for async execution?
78 bool isOpRelevant(MachineOperand &Op);
79
80 /// \brief Get register interval an operand affects.
81 RegInterval getRegInterval(MachineOperand &Op);
82
83 /// \brief Handle instructions async components
84 void pushInstruction(MachineInstr &MI);
85
86 /// \brief Insert the actual wait instruction
87 bool insertWait(MachineBasicBlock &MBB,
88 MachineBasicBlock::iterator I,
89 const Counters &Counts);
90
91 /// \brief Resolve all operand dependencies to counter requirements
92 Counters handleOperands(MachineInstr &MI);
93
94public:
95 SIInsertWaits(TargetMachine &tm) :
96 MachineFunctionPass(ID),
97 TII(static_cast<const SIInstrInfo*>(tm.getInstrInfo())),
98 TRI(TII->getRegisterInfo()) { }
99
100 virtual bool runOnMachineFunction(MachineFunction &MF);
101
102 const char *getPassName() const {
103 return "SI insert wait instructions";
104 }
105
106};
107
108} // End anonymous namespace
109
110char SIInsertWaits::ID = 0;
111
112const Counters SIInsertWaits::WaitCounts = { { 15, 7, 7 } };
113const Counters SIInsertWaits::ZeroCounts = { { 0, 0, 0 } };
114
115FunctionPass *llvm::createSIInsertWaits(TargetMachine &tm) {
116 return new SIInsertWaits(tm);
117}
118
119Counters SIInsertWaits::getHwCounts(MachineInstr &MI) {
120
121 uint64_t TSFlags = TII->get(MI.getOpcode()).TSFlags;
122 Counters Result;
123
124 Result.Named.VM = !!(TSFlags & SIInstrFlags::VM_CNT);
125
126 // Only consider stores or EXP for EXP_CNT
127 Result.Named.EXP = !!(TSFlags & SIInstrFlags::EXP_CNT &&
128 (MI.getOpcode() == AMDGPU::EXP || !MI.getDesc().mayStore()));
129
130 // LGKM may uses larger values
131 if (TSFlags & SIInstrFlags::LGKM_CNT) {
132
133 MachineOperand &Op = MI.getOperand(0);
134 assert(Op.isReg() && "First LGKM operand must be a register!");
135
136 unsigned Reg = Op.getReg();
137 unsigned Size = TRI.getMinimalPhysRegClass(Reg)->getSize();
138 Result.Named.LGKM = Size > 4 ? 2 : 1;
139
140 } else {
141 Result.Named.LGKM = 0;
142 }
143
144 return Result;
145}
146
147bool SIInsertWaits::isOpRelevant(MachineOperand &Op) {
148
149 // Constants are always irrelevant
150 if (!Op.isReg())
151 return false;
152
153 // Defines are always relevant
154 if (Op.isDef())
155 return true;
156
157 // For exports all registers are relevant
158 MachineInstr &MI = *Op.getParent();
159 if (MI.getOpcode() == AMDGPU::EXP)
160 return true;
161
162 // For stores the stored value is also relevant
163 if (!MI.getDesc().mayStore())
164 return false;
165
166 for (MachineInstr::mop_iterator I = MI.operands_begin(),
167 E = MI.operands_end(); I != E; ++I) {
168
169 if (I->isReg() && I->isUse())
170 return Op.isIdenticalTo(*I);
171 }
172
173 return false;
174}
175
176RegInterval SIInsertWaits::getRegInterval(MachineOperand &Op) {
177
178 if (!Op.isReg())
179 return std::make_pair(0, 0);
180
181 unsigned Reg = Op.getReg();
182 unsigned Size = TRI.getMinimalPhysRegClass(Reg)->getSize();
183
184 assert(Size >= 4);
185
186 RegInterval Result;
187 Result.first = TRI.getEncodingValue(Reg);
188 Result.second = Result.first + Size / 4;
189
190 return Result;
191}
192
193void SIInsertWaits::pushInstruction(MachineInstr &MI) {
194
195 // Get the hardware counter increments and sum them up
196 Counters Increment = getHwCounts(MI);
197 unsigned Sum = 0;
198
199 for (unsigned i = 0; i < 3; ++i) {
200 LastIssued.Array[i] += Increment.Array[i];
201 Sum += Increment.Array[i];
202 }
203
204 // If we don't increase anything then that's it
205 if (Sum == 0)
206 return;
207
208 // Remember which export instructions we have seen
209 if (Increment.Named.EXP) {
210 ExpInstrTypesSeen |= MI.getOpcode() == AMDGPU::EXP ? 1 : 2;
211 }
212
213 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
214
215 MachineOperand &Op = MI.getOperand(i);
216 if (!isOpRelevant(Op))
217 continue;
218
219 RegInterval Interval = getRegInterval(Op);
220 for (unsigned j = Interval.first; j < Interval.second; ++j) {
221
222 // Remember which registers we define
223 if (Op.isDef())
224 DefinedRegs[j] = LastIssued;
225
226 // and which one we are using
227 if (Op.isUse())
228 UsedRegs[j] = LastIssued;
229 }
230 }
231}
232
233bool SIInsertWaits::insertWait(MachineBasicBlock &MBB,
234 MachineBasicBlock::iterator I,
235 const Counters &Required) {
236
237 // End of program? No need to wait on anything
238 if (I != MBB.end() && I->getOpcode() == AMDGPU::S_ENDPGM)
239 return false;
240
241 // Figure out if the async instructions execute in order
242 bool Ordered[3];
243
244 // VM_CNT is always ordered
245 Ordered[0] = true;
246
247 // EXP_CNT is unordered if we have both EXP & VM-writes
248 Ordered[1] = ExpInstrTypesSeen == 3;
249
250 // LGKM_CNT is handled as always unordered. TODO: Handle LDS and GDS
251 Ordered[2] = false;
252
253 // The values we are going to put into the S_WAITCNT instruction
254 Counters Counts = WaitCounts;
255
256 // Do we really need to wait?
257 bool NeedWait = false;
258
259 for (unsigned i = 0; i < 3; ++i) {
260
261 if (Required.Array[i] <= WaitedOn.Array[i])
262 continue;
263
264 NeedWait = true;
265
266 if (Ordered[i]) {
267 unsigned Value = LastIssued.Array[i] - Required.Array[i];
268
269 // adjust the value to the real hardware posibilities
270 Counts.Array[i] = std::min(Value, WaitCounts.Array[i]);
271
272 } else
273 Counts.Array[i] = 0;
274
275 // Remember on what we have waited on
276 WaitedOn.Array[i] = LastIssued.Array[i] - Counts.Array[i];
277 }
278
279 if (!NeedWait)
280 return false;
281
282 // Reset EXP_CNT instruction types
283 if (Counts.Named.EXP == 0)
284 ExpInstrTypesSeen = 0;
285
286 // Build the wait instruction
287 BuildMI(MBB, I, DebugLoc(), TII->get(AMDGPU::S_WAITCNT))
288 .addImm((Counts.Named.VM & 0xF) |
289 ((Counts.Named.EXP & 0x7) << 4) |
290 ((Counts.Named.LGKM & 0x7) << 8));
291
292 return true;
293}
294
295/// \brief helper function for handleOperands
296static void increaseCounters(Counters &Dst, const Counters &Src) {
297
298 for (unsigned i = 0; i < 3; ++i)
299 Dst.Array[i] = std::max(Dst.Array[i], Src.Array[i]);
300}
301
302Counters SIInsertWaits::handleOperands(MachineInstr &MI) {
303
304 Counters Result = ZeroCounts;
305
306 // For each register affected by this
307 // instruction increase the result sequence
308 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
309
310 MachineOperand &Op = MI.getOperand(i);
311 RegInterval Interval = getRegInterval(Op);
312 for (unsigned j = Interval.first; j < Interval.second; ++j) {
313
314 if (Op.isDef())
315 increaseCounters(Result, UsedRegs[j]);
316
317 if (Op.isUse())
318 increaseCounters(Result, DefinedRegs[j]);
319 }
320 }
321
322 return Result;
323}
324
325bool SIInsertWaits::runOnMachineFunction(MachineFunction &MF) {
326
327 bool Changes = false;
328
329 MRI = &MF.getRegInfo();
330
331 WaitedOn = ZeroCounts;
332 LastIssued = ZeroCounts;
333
334 memset(&UsedRegs, 0, sizeof(UsedRegs));
335 memset(&DefinedRegs, 0, sizeof(DefinedRegs));
336
337 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
338 BI != BE; ++BI) {
339
340 MachineBasicBlock &MBB = *BI;
341 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
342 I != E; ++I) {
343
344 Changes |= insertWait(MBB, I, handleOperands(*I));
345 pushInstruction(*I);
346 }
347
348 // Wait for everything at the end of the MBB
349 Changes |= insertWait(MBB, MBB.getFirstTerminator(), LastIssued);
350 }
351
352 return Changes;
353}