blob: d31da4585ffeff1db10efa580404afedb9380caa [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;
Bill Wendlingb5632b52013-06-07 20:28:55 +000050 const SIRegisterInfo *TRI;
Tom Stellard82d3d452013-01-18 21:15:53 +000051 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
Christian Konig9ff8dc82013-03-01 09:46:04 +000091 /// \brief Do we need def2def checks?
92 bool unorderedDefines(MachineInstr &MI);
93
Tom Stellard82d3d452013-01-18 21:15:53 +000094 /// \brief Resolve all operand dependencies to counter requirements
95 Counters handleOperands(MachineInstr &MI);
96
97public:
98 SIInsertWaits(TargetMachine &tm) :
99 MachineFunctionPass(ID),
Bill Wendlingb5632b52013-06-07 20:28:55 +0000100 TII(0),
101 TRI(0) { }
Tom Stellard82d3d452013-01-18 21:15:53 +0000102
103 virtual bool runOnMachineFunction(MachineFunction &MF);
104
105 const char *getPassName() const {
106 return "SI insert wait instructions";
107 }
108
109};
110
111} // End anonymous namespace
112
113char SIInsertWaits::ID = 0;
114
115const Counters SIInsertWaits::WaitCounts = { { 15, 7, 7 } };
116const Counters SIInsertWaits::ZeroCounts = { { 0, 0, 0 } };
117
118FunctionPass *llvm::createSIInsertWaits(TargetMachine &tm) {
119 return new SIInsertWaits(tm);
120}
121
122Counters SIInsertWaits::getHwCounts(MachineInstr &MI) {
123
124 uint64_t TSFlags = TII->get(MI.getOpcode()).TSFlags;
125 Counters Result;
126
127 Result.Named.VM = !!(TSFlags & SIInstrFlags::VM_CNT);
128
129 // Only consider stores or EXP for EXP_CNT
130 Result.Named.EXP = !!(TSFlags & SIInstrFlags::EXP_CNT &&
Christian Konig9ff8dc82013-03-01 09:46:04 +0000131 (MI.getOpcode() == AMDGPU::EXP || MI.getDesc().mayStore()));
Tom Stellard82d3d452013-01-18 21:15:53 +0000132
133 // LGKM may uses larger values
134 if (TSFlags & SIInstrFlags::LGKM_CNT) {
135
136 MachineOperand &Op = MI.getOperand(0);
Michel Danzer7740daa2013-07-10 16:36:43 +0000137 if (!Op.isReg())
138 Op = MI.getOperand(1);
Tom Stellard82d3d452013-01-18 21:15:53 +0000139 assert(Op.isReg() && "First LGKM operand must be a register!");
140
141 unsigned Reg = Op.getReg();
Bill Wendlingb5632b52013-06-07 20:28:55 +0000142 unsigned Size = TRI->getMinimalPhysRegClass(Reg)->getSize();
Tom Stellard82d3d452013-01-18 21:15:53 +0000143 Result.Named.LGKM = Size > 4 ? 2 : 1;
144
145 } else {
146 Result.Named.LGKM = 0;
147 }
148
149 return Result;
150}
151
152bool SIInsertWaits::isOpRelevant(MachineOperand &Op) {
153
154 // Constants are always irrelevant
155 if (!Op.isReg())
156 return false;
157
158 // Defines are always relevant
159 if (Op.isDef())
160 return true;
161
162 // For exports all registers are relevant
163 MachineInstr &MI = *Op.getParent();
164 if (MI.getOpcode() == AMDGPU::EXP)
165 return true;
166
167 // For stores the stored value is also relevant
168 if (!MI.getDesc().mayStore())
169 return false;
170
171 for (MachineInstr::mop_iterator I = MI.operands_begin(),
172 E = MI.operands_end(); I != E; ++I) {
173
174 if (I->isReg() && I->isUse())
175 return Op.isIdenticalTo(*I);
176 }
177
178 return false;
179}
180
181RegInterval SIInsertWaits::getRegInterval(MachineOperand &Op) {
182
183 if (!Op.isReg())
184 return std::make_pair(0, 0);
185
186 unsigned Reg = Op.getReg();
Bill Wendlingb5632b52013-06-07 20:28:55 +0000187 unsigned Size = TRI->getMinimalPhysRegClass(Reg)->getSize();
Tom Stellard82d3d452013-01-18 21:15:53 +0000188
189 assert(Size >= 4);
190
191 RegInterval Result;
Bill Wendlingb5632b52013-06-07 20:28:55 +0000192 Result.first = TRI->getEncodingValue(Reg);
Tom Stellard82d3d452013-01-18 21:15:53 +0000193 Result.second = Result.first + Size / 4;
194
195 return Result;
196}
197
198void SIInsertWaits::pushInstruction(MachineInstr &MI) {
199
200 // Get the hardware counter increments and sum them up
201 Counters Increment = getHwCounts(MI);
202 unsigned Sum = 0;
203
204 for (unsigned i = 0; i < 3; ++i) {
205 LastIssued.Array[i] += Increment.Array[i];
206 Sum += Increment.Array[i];
207 }
208
209 // If we don't increase anything then that's it
210 if (Sum == 0)
211 return;
212
213 // Remember which export instructions we have seen
214 if (Increment.Named.EXP) {
215 ExpInstrTypesSeen |= MI.getOpcode() == AMDGPU::EXP ? 1 : 2;
216 }
217
218 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
219
220 MachineOperand &Op = MI.getOperand(i);
221 if (!isOpRelevant(Op))
222 continue;
223
224 RegInterval Interval = getRegInterval(Op);
225 for (unsigned j = Interval.first; j < Interval.second; ++j) {
226
227 // Remember which registers we define
228 if (Op.isDef())
229 DefinedRegs[j] = LastIssued;
230
231 // and which one we are using
232 if (Op.isUse())
233 UsedRegs[j] = LastIssued;
234 }
235 }
236}
237
238bool SIInsertWaits::insertWait(MachineBasicBlock &MBB,
239 MachineBasicBlock::iterator I,
240 const Counters &Required) {
241
242 // End of program? No need to wait on anything
243 if (I != MBB.end() && I->getOpcode() == AMDGPU::S_ENDPGM)
244 return false;
245
246 // Figure out if the async instructions execute in order
247 bool Ordered[3];
248
249 // VM_CNT is always ordered
250 Ordered[0] = true;
251
252 // EXP_CNT is unordered if we have both EXP & VM-writes
253 Ordered[1] = ExpInstrTypesSeen == 3;
254
255 // LGKM_CNT is handled as always unordered. TODO: Handle LDS and GDS
256 Ordered[2] = false;
257
258 // The values we are going to put into the S_WAITCNT instruction
259 Counters Counts = WaitCounts;
260
261 // Do we really need to wait?
262 bool NeedWait = false;
263
264 for (unsigned i = 0; i < 3; ++i) {
265
266 if (Required.Array[i] <= WaitedOn.Array[i])
267 continue;
268
269 NeedWait = true;
270
271 if (Ordered[i]) {
272 unsigned Value = LastIssued.Array[i] - Required.Array[i];
273
274 // adjust the value to the real hardware posibilities
275 Counts.Array[i] = std::min(Value, WaitCounts.Array[i]);
276
277 } else
278 Counts.Array[i] = 0;
279
280 // Remember on what we have waited on
281 WaitedOn.Array[i] = LastIssued.Array[i] - Counts.Array[i];
282 }
283
284 if (!NeedWait)
285 return false;
286
287 // Reset EXP_CNT instruction types
288 if (Counts.Named.EXP == 0)
289 ExpInstrTypesSeen = 0;
290
291 // Build the wait instruction
292 BuildMI(MBB, I, DebugLoc(), TII->get(AMDGPU::S_WAITCNT))
293 .addImm((Counts.Named.VM & 0xF) |
294 ((Counts.Named.EXP & 0x7) << 4) |
295 ((Counts.Named.LGKM & 0x7) << 8));
296
297 return true;
298}
299
300/// \brief helper function for handleOperands
301static void increaseCounters(Counters &Dst, const Counters &Src) {
302
303 for (unsigned i = 0; i < 3; ++i)
304 Dst.Array[i] = std::max(Dst.Array[i], Src.Array[i]);
305}
306
307Counters SIInsertWaits::handleOperands(MachineInstr &MI) {
308
309 Counters Result = ZeroCounts;
310
311 // For each register affected by this
312 // instruction increase the result sequence
313 for (unsigned i = 0, e = MI.getNumOperands(); i != e; ++i) {
314
315 MachineOperand &Op = MI.getOperand(i);
316 RegInterval Interval = getRegInterval(Op);
317 for (unsigned j = Interval.first; j < Interval.second; ++j) {
318
Christian Konig9ff8dc82013-03-01 09:46:04 +0000319 if (Op.isDef()) {
Tom Stellard82d3d452013-01-18 21:15:53 +0000320 increaseCounters(Result, UsedRegs[j]);
Christian Konigae621a22013-03-18 11:33:45 +0000321 increaseCounters(Result, DefinedRegs[j]);
Christian Konig9ff8dc82013-03-01 09:46:04 +0000322 }
Tom Stellard82d3d452013-01-18 21:15:53 +0000323
324 if (Op.isUse())
325 increaseCounters(Result, DefinedRegs[j]);
326 }
327 }
328
329 return Result;
330}
331
332bool SIInsertWaits::runOnMachineFunction(MachineFunction &MF) {
Tom Stellard82d3d452013-01-18 21:15:53 +0000333 bool Changes = false;
334
Bill Wendlingb5632b52013-06-07 20:28:55 +0000335 TII = static_cast<const SIInstrInfo*>(MF.getTarget().getInstrInfo());
336 TRI = static_cast<const SIRegisterInfo*>(MF.getTarget().getRegisterInfo());
337
Tom Stellard82d3d452013-01-18 21:15:53 +0000338 MRI = &MF.getRegInfo();
339
340 WaitedOn = ZeroCounts;
341 LastIssued = ZeroCounts;
342
343 memset(&UsedRegs, 0, sizeof(UsedRegs));
344 memset(&DefinedRegs, 0, sizeof(DefinedRegs));
345
346 for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
347 BI != BE; ++BI) {
348
349 MachineBasicBlock &MBB = *BI;
350 for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
351 I != E; ++I) {
352
353 Changes |= insertWait(MBB, I, handleOperands(*I));
354 pushInstruction(*I);
355 }
356
357 // Wait for everything at the end of the MBB
358 Changes |= insertWait(MBB, MBB.getFirstTerminator(), LastIssued);
359 }
360
361 return Changes;
362}