1 //===- SIInsertWaitcnts.cpp - Insert Wait Instructions --------------------===//
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 /// 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 "AMDGPUSubtarget.h"
21 #include "SIDefines.h"
22 #include "SIInstrInfo.h"
23 #include "SIMachineFunctionInfo.h"
24 #include "SIRegisterInfo.h"
25 #include "Utils/AMDGPUBaseInfo.h"
26 #include "llvm/ADT/DenseMap.h"
27 #include "llvm/ADT/DenseSet.h"
28 #include "llvm/ADT/PostOrderIterator.h"
29 #include "llvm/ADT/STLExtras.h"
30 #include "llvm/ADT/SmallVector.h"
31 #include "llvm/CodeGen/MachineBasicBlock.h"
32 #include "llvm/CodeGen/MachineFunction.h"
33 #include "llvm/CodeGen/MachineFunctionPass.h"
34 #include "llvm/CodeGen/MachineInstr.h"
35 #include "llvm/CodeGen/MachineInstrBuilder.h"
36 #include "llvm/CodeGen/MachineLoopInfo.h"
37 #include "llvm/CodeGen/MachineMemOperand.h"
38 #include "llvm/CodeGen/MachineOperand.h"
39 #include "llvm/CodeGen/MachineRegisterInfo.h"
40 #include "llvm/IR/DebugLoc.h"
41 #include "llvm/Pass.h"
42 #include "llvm/Support/Debug.h"
43 #include "llvm/Support/DebugCounter.h"
44 #include "llvm/Support/ErrorHandling.h"
45 #include "llvm/Support/raw_ostream.h"
46 #include <algorithm>
47 #include <cassert>
48 #include <cstdint>
49 #include <cstring>
50 #include <memory>
51 #include <utility>
52 #include <vector>
53 
54 using namespace llvm;
55 
56 #define DEBUG_TYPE "si-insert-waitcnts"
57 
58 DEBUG_COUNTER(ForceExpCounter, DEBUG_TYPE"-forceexp",
59               "Force emit s_waitcnt expcnt(0) instrs");
60 DEBUG_COUNTER(ForceLgkmCounter, DEBUG_TYPE"-forcelgkm",
61               "Force emit s_waitcnt lgkmcnt(0) instrs");
62 DEBUG_COUNTER(ForceVMCounter, DEBUG_TYPE"-forcevm",
63               "Force emit s_waitcnt vmcnt(0) instrs");
64 
65 static cl::opt<unsigned> ForceEmitZeroFlag(
66   "amdgpu-waitcnt-forcezero",
67   cl::desc("Force all waitcnt instrs to be emitted as s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)"),
68   cl::init(0), cl::Hidden);
69 
70 namespace {
71 
72 // Class of object that encapsulates latest instruction counter score
73 // associated with the operand.  Used for determining whether
74 // s_waitcnt instruction needs to be emited.
75 
76 #define CNT_MASK(t) (1u << (t))
77 
78 enum InstCounterType { VM_CNT = 0, LGKM_CNT, EXP_CNT, NUM_INST_CNTS };
79 
80 using RegInterval = std::pair<signed, signed>;
81 
82 struct {
83   int32_t VmcntMax;
84   int32_t ExpcntMax;
85   int32_t LgkmcntMax;
86   int32_t NumVGPRsMax;
87   int32_t NumSGPRsMax;
88 } HardwareLimits;
89 
90 struct {
91   unsigned VGPR0;
92   unsigned VGPRL;
93   unsigned SGPR0;
94   unsigned SGPRL;
95 } RegisterEncoding;
96 
97 enum WaitEventType {
98   VMEM_ACCESS,      // vector-memory read & write
99   LDS_ACCESS,       // lds read & write
100   GDS_ACCESS,       // gds read & write
101   SQ_MESSAGE,       // send message
102   SMEM_ACCESS,      // scalar-memory read & write
103   EXP_GPR_LOCK,     // export holding on its data src
104   GDS_GPR_LOCK,     // GDS holding on its data and addr src
105   EXP_POS_ACCESS,   // write to export position
106   EXP_PARAM_ACCESS, // write to export parameter
107   VMW_GPR_LOCK,     // vector-memory write holding on its data src
108   NUM_WAIT_EVENTS,
109 };
110 
111 // The mapping is:
112 //  0                .. SQ_MAX_PGM_VGPRS-1               real VGPRs
113 //  SQ_MAX_PGM_VGPRS .. NUM_ALL_VGPRS-1                  extra VGPR-like slots
114 //  NUM_ALL_VGPRS    .. NUM_ALL_VGPRS+SQ_MAX_PGM_SGPRS-1 real SGPRs
115 // We reserve a fixed number of VGPR slots in the scoring tables for
116 // special tokens like SCMEM_LDS (needed for buffer load to LDS).
117 enum RegisterMapping {
118   SQ_MAX_PGM_VGPRS = 256, // Maximum programmable VGPRs across all targets.
119   SQ_MAX_PGM_SGPRS = 256, // Maximum programmable SGPRs across all targets.
120   NUM_EXTRA_VGPRS = 1,    // A reserved slot for DS.
121   EXTRA_VGPR_LDS = 0,     // This is a placeholder the Shader algorithm uses.
122   NUM_ALL_VGPRS = SQ_MAX_PGM_VGPRS + NUM_EXTRA_VGPRS, // Where SGPR starts.
123 };
124 
125 #define ForAllWaitEventType(w)                                                 \
126   for (enum WaitEventType w = (enum WaitEventType)0;                           \
127        (w) < (enum WaitEventType)NUM_WAIT_EVENTS;                              \
128        (w) = (enum WaitEventType)((w) + 1))
129 
130 // This is a per-basic-block object that maintains current score brackets
131 // of each wait counter, and a per-register scoreboard for each wait counter.
132 // We also maintain the latest score for every event type that can change the
133 // waitcnt in order to know if there are multiple types of events within
134 // the brackets. When multiple types of event happen in the bracket,
135 // wait count may get decreased out of order, therefore we need to put in
136 // "s_waitcnt 0" before use.
137 class BlockWaitcntBrackets {
138 public:
139   BlockWaitcntBrackets(const SISubtarget *SubTarget) : ST(SubTarget) {
140     for (enum InstCounterType T = VM_CNT; T < NUM_INST_CNTS;
141          T = (enum InstCounterType)(T + 1)) {
142       memset(VgprScores[T], 0, sizeof(VgprScores[T]));
143     }
144   }
145 
146   ~BlockWaitcntBrackets() = default;
147 
148   static int32_t getWaitCountMax(InstCounterType T) {
149     switch (T) {
150     case VM_CNT:
151       return HardwareLimits.VmcntMax;
152     case LGKM_CNT:
153       return HardwareLimits.LgkmcntMax;
154     case EXP_CNT:
155       return HardwareLimits.ExpcntMax;
156     default:
157       break;
158     }
159     return 0;
160   }
161 
162   void setScoreLB(InstCounterType T, int32_t Val) {
163     assert(T < NUM_INST_CNTS);
164     if (T >= NUM_INST_CNTS)
165       return;
166     ScoreLBs[T] = Val;
167   }
168 
169   void setScoreUB(InstCounterType T, int32_t Val) {
170     assert(T < NUM_INST_CNTS);
171     if (T >= NUM_INST_CNTS)
172       return;
173     ScoreUBs[T] = Val;
174     if (T == EXP_CNT) {
175       int32_t UB = (int)(ScoreUBs[T] - getWaitCountMax(EXP_CNT));
176       if (ScoreLBs[T] < UB)
177         ScoreLBs[T] = UB;
178     }
179   }
180 
181   int32_t getScoreLB(InstCounterType T) {
182     assert(T < NUM_INST_CNTS);
183     if (T >= NUM_INST_CNTS)
184       return 0;
185     return ScoreLBs[T];
186   }
187 
188   int32_t getScoreUB(InstCounterType T) {
189     assert(T < NUM_INST_CNTS);
190     if (T >= NUM_INST_CNTS)
191       return 0;
192     return ScoreUBs[T];
193   }
194 
195   // Mapping from event to counter.
196   InstCounterType eventCounter(WaitEventType E) {
197     switch (E) {
198     case VMEM_ACCESS:
199       return VM_CNT;
200     case LDS_ACCESS:
201     case GDS_ACCESS:
202     case SQ_MESSAGE:
203     case SMEM_ACCESS:
204       return LGKM_CNT;
205     case EXP_GPR_LOCK:
206     case GDS_GPR_LOCK:
207     case VMW_GPR_LOCK:
208     case EXP_POS_ACCESS:
209     case EXP_PARAM_ACCESS:
210       return EXP_CNT;
211     default:
212       llvm_unreachable("unhandled event type");
213     }
214     return NUM_INST_CNTS;
215   }
216 
217   void setRegScore(int GprNo, InstCounterType T, int32_t Val) {
218     if (GprNo < NUM_ALL_VGPRS) {
219       if (GprNo > VgprUB) {
220         VgprUB = GprNo;
221       }
222       VgprScores[T][GprNo] = Val;
223     } else {
224       assert(T == LGKM_CNT);
225       if (GprNo - NUM_ALL_VGPRS > SgprUB) {
226         SgprUB = GprNo - NUM_ALL_VGPRS;
227       }
228       SgprScores[GprNo - NUM_ALL_VGPRS] = Val;
229     }
230   }
231 
232   int32_t getRegScore(int GprNo, InstCounterType T) {
233     if (GprNo < NUM_ALL_VGPRS) {
234       return VgprScores[T][GprNo];
235     }
236     return SgprScores[GprNo - NUM_ALL_VGPRS];
237   }
238 
239   void clear() {
240     memset(ScoreLBs, 0, sizeof(ScoreLBs));
241     memset(ScoreUBs, 0, sizeof(ScoreUBs));
242     memset(EventUBs, 0, sizeof(EventUBs));
243     for (enum InstCounterType T = VM_CNT; T < NUM_INST_CNTS;
244          T = (enum InstCounterType)(T + 1)) {
245       memset(VgprScores[T], 0, sizeof(VgprScores[T]));
246     }
247     memset(SgprScores, 0, sizeof(SgprScores));
248   }
249 
250   RegInterval getRegInterval(const MachineInstr *MI, const SIInstrInfo *TII,
251                              const MachineRegisterInfo *MRI,
252                              const SIRegisterInfo *TRI, unsigned OpNo,
253                              bool Def) const;
254 
255   void setExpScore(const MachineInstr *MI, const SIInstrInfo *TII,
256                    const SIRegisterInfo *TRI, const MachineRegisterInfo *MRI,
257                    unsigned OpNo, int32_t Val);
258 
259   void setWaitAtBeginning() { WaitAtBeginning = true; }
260   void clearWaitAtBeginning() { WaitAtBeginning = false; }
261   bool getWaitAtBeginning() const { return WaitAtBeginning; }
262   void setEventUB(enum WaitEventType W, int32_t Val) { EventUBs[W] = Val; }
263   int32_t getMaxVGPR() const { return VgprUB; }
264   int32_t getMaxSGPR() const { return SgprUB; }
265 
266   int32_t getEventUB(enum WaitEventType W) const {
267     assert(W < NUM_WAIT_EVENTS);
268     return EventUBs[W];
269   }
270 
271   bool counterOutOfOrder(InstCounterType T);
272   unsigned int updateByWait(InstCounterType T, int ScoreToWait);
273   void updateByEvent(const SIInstrInfo *TII, const SIRegisterInfo *TRI,
274                      const MachineRegisterInfo *MRI, WaitEventType E,
275                      MachineInstr &MI);
276 
277   bool hasPendingSMEM() const {
278     return (EventUBs[SMEM_ACCESS] > ScoreLBs[LGKM_CNT] &&
279             EventUBs[SMEM_ACCESS] <= ScoreUBs[LGKM_CNT]);
280   }
281 
282   bool hasPendingFlat() const {
283     return ((LastFlat[LGKM_CNT] > ScoreLBs[LGKM_CNT] &&
284              LastFlat[LGKM_CNT] <= ScoreUBs[LGKM_CNT]) ||
285             (LastFlat[VM_CNT] > ScoreLBs[VM_CNT] &&
286              LastFlat[VM_CNT] <= ScoreUBs[VM_CNT]));
287   }
288 
289   void setPendingFlat() {
290     LastFlat[VM_CNT] = ScoreUBs[VM_CNT];
291     LastFlat[LGKM_CNT] = ScoreUBs[LGKM_CNT];
292   }
293 
294   int pendingFlat(InstCounterType Ct) const { return LastFlat[Ct]; }
295 
296   void setLastFlat(InstCounterType Ct, int Val) { LastFlat[Ct] = Val; }
297 
298   bool getRevisitLoop() const { return RevisitLoop; }
299   void setRevisitLoop(bool RevisitLoopIn) { RevisitLoop = RevisitLoopIn; }
300 
301   void setPostOrder(int32_t PostOrderIn) { PostOrder = PostOrderIn; }
302   int32_t getPostOrder() const { return PostOrder; }
303 
304   void setWaitcnt(MachineInstr *WaitcntIn) { Waitcnt = WaitcntIn; }
305   void clearWaitcnt() { Waitcnt = nullptr; }
306   MachineInstr *getWaitcnt() const { return Waitcnt; }
307 
308   bool mixedExpTypes() const { return MixedExpTypes; }
309   void setMixedExpTypes(bool MixedExpTypesIn) {
310     MixedExpTypes = MixedExpTypesIn;
311   }
312 
313   void print(raw_ostream &);
314   void dump() { print(dbgs()); }
315 
316 private:
317   const SISubtarget *ST = nullptr;
318   bool WaitAtBeginning = false;
319   bool RevisitLoop = false;
320   bool MixedExpTypes = false;
321   int32_t PostOrder = 0;
322   MachineInstr *Waitcnt = nullptr;
323   int32_t ScoreLBs[NUM_INST_CNTS] = {0};
324   int32_t ScoreUBs[NUM_INST_CNTS] = {0};
325   int32_t EventUBs[NUM_WAIT_EVENTS] = {0};
326   // Remember the last flat memory operation.
327   int32_t LastFlat[NUM_INST_CNTS] = {0};
328   // wait_cnt scores for every vgpr.
329   // Keep track of the VgprUB and SgprUB to make merge at join efficient.
330   int32_t VgprUB = 0;
331   int32_t SgprUB = 0;
332   int32_t VgprScores[NUM_INST_CNTS][NUM_ALL_VGPRS];
333   // Wait cnt scores for every sgpr, only lgkmcnt is relevant.
334   int32_t SgprScores[SQ_MAX_PGM_SGPRS] = {0};
335 };
336 
337 // This is a per-loop-region object that records waitcnt status at the end of
338 // loop footer from the previous iteration. We also maintain an iteration
339 // count to track the number of times the loop has been visited. When it
340 // doesn't converge naturally, we force convergence by inserting s_waitcnt 0
341 // at the end of the loop footer.
342 class LoopWaitcntData {
343 public:
344   LoopWaitcntData() = default;
345   ~LoopWaitcntData() = default;
346 
347   void incIterCnt() { IterCnt++; }
348   void resetIterCnt() { IterCnt = 0; }
349   unsigned getIterCnt() { return IterCnt; }
350 
351   void setWaitcnt(MachineInstr *WaitcntIn) { LfWaitcnt = WaitcntIn; }
352   MachineInstr *getWaitcnt() const { return LfWaitcnt; }
353 
354   void print() { LLVM_DEBUG(dbgs() << "  iteration " << IterCnt << '\n';); }
355 
356 private:
357   // s_waitcnt added at the end of loop footer to stablize wait scores
358   // at the end of the loop footer.
359   MachineInstr *LfWaitcnt = nullptr;
360   // Number of iterations the loop has been visited, not including the initial
361   // walk over.
362   int32_t IterCnt = 0;
363 };
364 
365 class SIInsertWaitcnts : public MachineFunctionPass {
366 private:
367   const SISubtarget *ST = nullptr;
368   const SIInstrInfo *TII = nullptr;
369   const SIRegisterInfo *TRI = nullptr;
370   const MachineRegisterInfo *MRI = nullptr;
371   const MachineLoopInfo *MLI = nullptr;
372   AMDGPU::IsaInfo::IsaVersion IV;
373   AMDGPUAS AMDGPUASI;
374 
375   DenseSet<MachineBasicBlock *> BlockVisitedSet;
376   DenseSet<MachineInstr *> TrackedWaitcntSet;
377   DenseSet<MachineInstr *> VCCZBugHandledSet;
378 
379   DenseMap<MachineBasicBlock *, std::unique_ptr<BlockWaitcntBrackets>>
380       BlockWaitcntBracketsMap;
381 
382   std::vector<MachineBasicBlock *> BlockWaitcntProcessedSet;
383 
384   DenseMap<MachineLoop *, std::unique_ptr<LoopWaitcntData>> LoopWaitcntDataMap;
385 
386   std::vector<std::unique_ptr<BlockWaitcntBrackets>> KillWaitBrackets;
387 
388   // ForceEmitZeroWaitcnts: force all waitcnts insts to be s_waitcnt 0
389   // because of amdgpu-waitcnt-forcezero flag
390   bool ForceEmitZeroWaitcnts;
391   bool ForceEmitWaitcnt[NUM_INST_CNTS];
392 
393 public:
394   static char ID;
395 
396   SIInsertWaitcnts() : MachineFunctionPass(ID) {
397     (void)ForceExpCounter;
398     (void)ForceLgkmCounter;
399     (void)ForceVMCounter;
400   }
401 
402   bool runOnMachineFunction(MachineFunction &MF) override;
403 
404   StringRef getPassName() const override {
405     return "SI insert wait instructions";
406   }
407 
408   void getAnalysisUsage(AnalysisUsage &AU) const override {
409     AU.setPreservesCFG();
410     AU.addRequired<MachineLoopInfo>();
411     MachineFunctionPass::getAnalysisUsage(AU);
412   }
413 
414   void addKillWaitBracket(BlockWaitcntBrackets *Bracket) {
415     // The waitcnt information is copied because it changes as the block is
416     // traversed.
417     KillWaitBrackets.push_back(
418         llvm::make_unique<BlockWaitcntBrackets>(*Bracket));
419   }
420 
421   bool isForceEmitWaitcnt() const {
422     for (enum InstCounterType T = VM_CNT; T < NUM_INST_CNTS;
423          T = (enum InstCounterType)(T + 1))
424       if (ForceEmitWaitcnt[T])
425         return true;
426     return false;
427   }
428 
429   void setForceEmitWaitcnt() {
430 // For non-debug builds, ForceEmitWaitcnt has been initialized to false;
431 // For debug builds, get the debug counter info and adjust if need be
432 #ifndef NDEBUG
433     if (DebugCounter::isCounterSet(ForceExpCounter) &&
434         DebugCounter::shouldExecute(ForceExpCounter)) {
435       ForceEmitWaitcnt[EXP_CNT] = true;
436     } else {
437       ForceEmitWaitcnt[EXP_CNT] = false;
438     }
439 
440     if (DebugCounter::isCounterSet(ForceLgkmCounter) &&
441          DebugCounter::shouldExecute(ForceLgkmCounter)) {
442       ForceEmitWaitcnt[LGKM_CNT] = true;
443     } else {
444       ForceEmitWaitcnt[LGKM_CNT] = false;
445     }
446 
447     if (DebugCounter::isCounterSet(ForceVMCounter) &&
448         DebugCounter::shouldExecute(ForceVMCounter)) {
449       ForceEmitWaitcnt[VM_CNT] = true;
450     } else {
451       ForceEmitWaitcnt[VM_CNT] = false;
452     }
453 #endif // NDEBUG
454   }
455 
456   bool mayAccessLDSThroughFlat(const MachineInstr &MI) const;
457   void generateWaitcntInstBefore(MachineInstr &MI,
458                                   BlockWaitcntBrackets *ScoreBrackets);
459   void updateEventWaitcntAfter(MachineInstr &Inst,
460                                BlockWaitcntBrackets *ScoreBrackets);
461   void mergeInputScoreBrackets(MachineBasicBlock &Block);
462   bool isLoopBottom(const MachineLoop *Loop, const MachineBasicBlock *Block);
463   unsigned countNumBottomBlocks(const MachineLoop *Loop);
464   void insertWaitcntInBlock(MachineFunction &MF, MachineBasicBlock &Block);
465   void insertWaitcntBeforeCF(MachineBasicBlock &Block, MachineInstr *Inst);
466   bool isWaitcntStronger(unsigned LHS, unsigned RHS);
467   unsigned combineWaitcnt(unsigned LHS, unsigned RHS);
468 };
469 
470 } // end anonymous namespace
471 
472 RegInterval BlockWaitcntBrackets::getRegInterval(const MachineInstr *MI,
473                                                  const SIInstrInfo *TII,
474                                                  const MachineRegisterInfo *MRI,
475                                                  const SIRegisterInfo *TRI,
476                                                  unsigned OpNo,
477                                                  bool Def) const {
478   const MachineOperand &Op = MI->getOperand(OpNo);
479   if (!Op.isReg() || !TRI->isInAllocatableClass(Op.getReg()) ||
480       (Def && !Op.isDef()))
481     return {-1, -1};
482 
483   // A use via a PW operand does not need a waitcnt.
484   // A partial write is not a WAW.
485   assert(!Op.getSubReg() || !Op.isUndef());
486 
487   RegInterval Result;
488   const MachineRegisterInfo &MRIA = *MRI;
489 
490   unsigned Reg = TRI->getEncodingValue(Op.getReg());
491 
492   if (TRI->isVGPR(MRIA, Op.getReg())) {
493     assert(Reg >= RegisterEncoding.VGPR0 && Reg <= RegisterEncoding.VGPRL);
494     Result.first = Reg - RegisterEncoding.VGPR0;
495     assert(Result.first >= 0 && Result.first < SQ_MAX_PGM_VGPRS);
496   } else if (TRI->isSGPRReg(MRIA, Op.getReg())) {
497     assert(Reg >= RegisterEncoding.SGPR0 && Reg < SQ_MAX_PGM_SGPRS);
498     Result.first = Reg - RegisterEncoding.SGPR0 + NUM_ALL_VGPRS;
499     assert(Result.first >= NUM_ALL_VGPRS &&
500            Result.first < SQ_MAX_PGM_SGPRS + NUM_ALL_VGPRS);
501   }
502   // TODO: Handle TTMP
503   // else if (TRI->isTTMP(MRIA, Reg.getReg())) ...
504   else
505     return {-1, -1};
506 
507   const MachineInstr &MIA = *MI;
508   const TargetRegisterClass *RC = TII->getOpRegClass(MIA, OpNo);
509   unsigned Size = TRI->getRegSizeInBits(*RC);
510   Result.second = Result.first + (Size / 32);
511 
512   return Result;
513 }
514 
515 void BlockWaitcntBrackets::setExpScore(const MachineInstr *MI,
516                                        const SIInstrInfo *TII,
517                                        const SIRegisterInfo *TRI,
518                                        const MachineRegisterInfo *MRI,
519                                        unsigned OpNo, int32_t Val) {
520   RegInterval Interval = getRegInterval(MI, TII, MRI, TRI, OpNo, false);
521   LLVM_DEBUG({
522     const MachineOperand &Opnd = MI->getOperand(OpNo);
523     assert(TRI->isVGPR(*MRI, Opnd.getReg()));
524   });
525   for (signed RegNo = Interval.first; RegNo < Interval.second; ++RegNo) {
526     setRegScore(RegNo, EXP_CNT, Val);
527   }
528 }
529 
530 void BlockWaitcntBrackets::updateByEvent(const SIInstrInfo *TII,
531                                          const SIRegisterInfo *TRI,
532                                          const MachineRegisterInfo *MRI,
533                                          WaitEventType E, MachineInstr &Inst) {
534   const MachineRegisterInfo &MRIA = *MRI;
535   InstCounterType T = eventCounter(E);
536   int32_t CurrScore = getScoreUB(T) + 1;
537   // EventUB and ScoreUB need to be update regardless if this event changes
538   // the score of a register or not.
539   // Examples including vm_cnt when buffer-store or lgkm_cnt when send-message.
540   EventUBs[E] = CurrScore;
541   setScoreUB(T, CurrScore);
542 
543   if (T == EXP_CNT) {
544     // Check for mixed export types. If they are mixed, then a waitcnt exp(0)
545     // is required.
546     if (!MixedExpTypes) {
547       MixedExpTypes = counterOutOfOrder(EXP_CNT);
548     }
549 
550     // Put score on the source vgprs. If this is a store, just use those
551     // specific register(s).
552     if (TII->isDS(Inst) && (Inst.mayStore() || Inst.mayLoad())) {
553       // All GDS operations must protect their address register (same as
554       // export.)
555       if (Inst.getOpcode() != AMDGPU::DS_APPEND &&
556           Inst.getOpcode() != AMDGPU::DS_CONSUME) {
557         setExpScore(
558             &Inst, TII, TRI, MRI,
559             AMDGPU::getNamedOperandIdx(Inst.getOpcode(), AMDGPU::OpName::addr),
560             CurrScore);
561       }
562       if (Inst.mayStore()) {
563         setExpScore(
564             &Inst, TII, TRI, MRI,
565             AMDGPU::getNamedOperandIdx(Inst.getOpcode(), AMDGPU::OpName::data0),
566             CurrScore);
567         if (AMDGPU::getNamedOperandIdx(Inst.getOpcode(),
568                                        AMDGPU::OpName::data1) != -1) {
569           setExpScore(&Inst, TII, TRI, MRI,
570                       AMDGPU::getNamedOperandIdx(Inst.getOpcode(),
571                                                  AMDGPU::OpName::data1),
572                       CurrScore);
573         }
574       } else if (AMDGPU::getAtomicNoRetOp(Inst.getOpcode()) != -1 &&
575                  Inst.getOpcode() != AMDGPU::DS_GWS_INIT &&
576                  Inst.getOpcode() != AMDGPU::DS_GWS_SEMA_V &&
577                  Inst.getOpcode() != AMDGPU::DS_GWS_SEMA_BR &&
578                  Inst.getOpcode() != AMDGPU::DS_GWS_SEMA_P &&
579                  Inst.getOpcode() != AMDGPU::DS_GWS_BARRIER &&
580                  Inst.getOpcode() != AMDGPU::DS_APPEND &&
581                  Inst.getOpcode() != AMDGPU::DS_CONSUME &&
582                  Inst.getOpcode() != AMDGPU::DS_ORDERED_COUNT) {
583         for (unsigned I = 0, E = Inst.getNumOperands(); I != E; ++I) {
584           const MachineOperand &Op = Inst.getOperand(I);
585           if (Op.isReg() && !Op.isDef() && TRI->isVGPR(MRIA, Op.getReg())) {
586             setExpScore(&Inst, TII, TRI, MRI, I, CurrScore);
587           }
588         }
589       }
590     } else if (TII->isFLAT(Inst)) {
591       if (Inst.mayStore()) {
592         setExpScore(
593             &Inst, TII, TRI, MRI,
594             AMDGPU::getNamedOperandIdx(Inst.getOpcode(), AMDGPU::OpName::data),
595             CurrScore);
596       } else if (AMDGPU::getAtomicNoRetOp(Inst.getOpcode()) != -1) {
597         setExpScore(
598             &Inst, TII, TRI, MRI,
599             AMDGPU::getNamedOperandIdx(Inst.getOpcode(), AMDGPU::OpName::data),
600             CurrScore);
601       }
602     } else if (TII->isMIMG(Inst)) {
603       if (Inst.mayStore()) {
604         setExpScore(&Inst, TII, TRI, MRI, 0, CurrScore);
605       } else if (AMDGPU::getAtomicNoRetOp(Inst.getOpcode()) != -1) {
606         setExpScore(
607             &Inst, TII, TRI, MRI,
608             AMDGPU::getNamedOperandIdx(Inst.getOpcode(), AMDGPU::OpName::data),
609             CurrScore);
610       }
611     } else if (TII->isMTBUF(Inst)) {
612       if (Inst.mayStore()) {
613         setExpScore(&Inst, TII, TRI, MRI, 0, CurrScore);
614       }
615     } else if (TII->isMUBUF(Inst)) {
616       if (Inst.mayStore()) {
617         setExpScore(&Inst, TII, TRI, MRI, 0, CurrScore);
618       } else if (AMDGPU::getAtomicNoRetOp(Inst.getOpcode()) != -1) {
619         setExpScore(
620             &Inst, TII, TRI, MRI,
621             AMDGPU::getNamedOperandIdx(Inst.getOpcode(), AMDGPU::OpName::data),
622             CurrScore);
623       }
624     } else {
625       if (TII->isEXP(Inst)) {
626         // For export the destination registers are really temps that
627         // can be used as the actual source after export patching, so
628         // we need to treat them like sources and set the EXP_CNT
629         // score.
630         for (unsigned I = 0, E = Inst.getNumOperands(); I != E; ++I) {
631           MachineOperand &DefMO = Inst.getOperand(I);
632           if (DefMO.isReg() && DefMO.isDef() &&
633               TRI->isVGPR(MRIA, DefMO.getReg())) {
634             setRegScore(TRI->getEncodingValue(DefMO.getReg()), EXP_CNT,
635                         CurrScore);
636           }
637         }
638       }
639       for (unsigned I = 0, E = Inst.getNumOperands(); I != E; ++I) {
640         MachineOperand &MO = Inst.getOperand(I);
641         if (MO.isReg() && !MO.isDef() && TRI->isVGPR(MRIA, MO.getReg())) {
642           setExpScore(&Inst, TII, TRI, MRI, I, CurrScore);
643         }
644       }
645     }
646 #if 0 // TODO: check if this is handled by MUBUF code above.
647   } else if (Inst.getOpcode() == AMDGPU::BUFFER_STORE_DWORD ||
648        Inst.getOpcode() == AMDGPU::BUFFER_STORE_DWORDX2 ||
649        Inst.getOpcode() == AMDGPU::BUFFER_STORE_DWORDX4) {
650     MachineOperand *MO = TII->getNamedOperand(Inst, AMDGPU::OpName::data);
651     unsigned OpNo;//TODO: find the OpNo for this operand;
652     RegInterval Interval = getRegInterval(&Inst, TII, MRI, TRI, OpNo, false);
653     for (signed RegNo = Interval.first; RegNo < Interval.second;
654     ++RegNo) {
655       setRegScore(RegNo + NUM_ALL_VGPRS, t, CurrScore);
656     }
657 #endif
658   } else {
659     // Match the score to the destination registers.
660     for (unsigned I = 0, E = Inst.getNumOperands(); I != E; ++I) {
661       RegInterval Interval = getRegInterval(&Inst, TII, MRI, TRI, I, true);
662       if (T == VM_CNT && Interval.first >= NUM_ALL_VGPRS)
663         continue;
664       for (signed RegNo = Interval.first; RegNo < Interval.second; ++RegNo) {
665         setRegScore(RegNo, T, CurrScore);
666       }
667     }
668     if (TII->isDS(Inst) && Inst.mayStore()) {
669       setRegScore(SQ_MAX_PGM_VGPRS + EXTRA_VGPR_LDS, T, CurrScore);
670     }
671   }
672 }
673 
674 void BlockWaitcntBrackets::print(raw_ostream &OS) {
675   OS << '\n';
676   for (enum InstCounterType T = VM_CNT; T < NUM_INST_CNTS;
677        T = (enum InstCounterType)(T + 1)) {
678     int LB = getScoreLB(T);
679     int UB = getScoreUB(T);
680 
681     switch (T) {
682     case VM_CNT:
683       OS << "    VM_CNT(" << UB - LB << "): ";
684       break;
685     case LGKM_CNT:
686       OS << "    LGKM_CNT(" << UB - LB << "): ";
687       break;
688     case EXP_CNT:
689       OS << "    EXP_CNT(" << UB - LB << "): ";
690       break;
691     default:
692       OS << "    UNKNOWN(" << UB - LB << "): ";
693       break;
694     }
695 
696     if (LB < UB) {
697       // Print vgpr scores.
698       for (int J = 0; J <= getMaxVGPR(); J++) {
699         int RegScore = getRegScore(J, T);
700         if (RegScore <= LB)
701           continue;
702         int RelScore = RegScore - LB - 1;
703         if (J < SQ_MAX_PGM_VGPRS + EXTRA_VGPR_LDS) {
704           OS << RelScore << ":v" << J << " ";
705         } else {
706           OS << RelScore << ":ds ";
707         }
708       }
709       // Also need to print sgpr scores for lgkm_cnt.
710       if (T == LGKM_CNT) {
711         for (int J = 0; J <= getMaxSGPR(); J++) {
712           int RegScore = getRegScore(J + NUM_ALL_VGPRS, LGKM_CNT);
713           if (RegScore <= LB)
714             continue;
715           int RelScore = RegScore - LB - 1;
716           OS << RelScore << ":s" << J << " ";
717         }
718       }
719     }
720     OS << '\n';
721   }
722   OS << '\n';
723 }
724 
725 unsigned int BlockWaitcntBrackets::updateByWait(InstCounterType T,
726                                                 int ScoreToWait) {
727   unsigned int NeedWait = 0;
728   if (ScoreToWait == -1) {
729     // The score to wait is unknown. This implies that it was not encountered
730     // during the path of the CFG walk done during the current traversal but
731     // may be seen on a different path. Emit an s_wait counter with a
732     // conservative value of 0 for the counter.
733     NeedWait = CNT_MASK(T);
734     setScoreLB(T, getScoreUB(T));
735     return NeedWait;
736   }
737 
738   // If the score of src_operand falls within the bracket, we need an
739   // s_waitcnt instruction.
740   const int32_t LB = getScoreLB(T);
741   const int32_t UB = getScoreUB(T);
742   if ((UB >= ScoreToWait) && (ScoreToWait > LB)) {
743     if ((T == VM_CNT || T == LGKM_CNT) &&
744         hasPendingFlat() &&
745         !ST->hasFlatLgkmVMemCountInOrder()) {
746       // If there is a pending FLAT operation, and this is a VMem or LGKM
747       // waitcnt and the target can report early completion, then we need
748       // to force a waitcnt 0.
749       NeedWait = CNT_MASK(T);
750       setScoreLB(T, getScoreUB(T));
751     } else if (counterOutOfOrder(T)) {
752       // Counter can get decremented out-of-order when there
753       // are multiple types event in the bracket. Also emit an s_wait counter
754       // with a conservative value of 0 for the counter.
755       NeedWait = CNT_MASK(T);
756       setScoreLB(T, getScoreUB(T));
757     } else {
758       NeedWait = CNT_MASK(T);
759       setScoreLB(T, ScoreToWait);
760     }
761   }
762 
763   return NeedWait;
764 }
765 
766 // Where there are multiple types of event in the bracket of a counter,
767 // the decrement may go out of order.
768 bool BlockWaitcntBrackets::counterOutOfOrder(InstCounterType T) {
769   switch (T) {
770   case VM_CNT:
771     return false;
772   case LGKM_CNT: {
773     if (EventUBs[SMEM_ACCESS] > ScoreLBs[LGKM_CNT] &&
774         EventUBs[SMEM_ACCESS] <= ScoreUBs[LGKM_CNT]) {
775       // Scalar memory read always can go out of order.
776       return true;
777     }
778     int NumEventTypes = 0;
779     if (EventUBs[LDS_ACCESS] > ScoreLBs[LGKM_CNT] &&
780         EventUBs[LDS_ACCESS] <= ScoreUBs[LGKM_CNT]) {
781       NumEventTypes++;
782     }
783     if (EventUBs[GDS_ACCESS] > ScoreLBs[LGKM_CNT] &&
784         EventUBs[GDS_ACCESS] <= ScoreUBs[LGKM_CNT]) {
785       NumEventTypes++;
786     }
787     if (EventUBs[SQ_MESSAGE] > ScoreLBs[LGKM_CNT] &&
788         EventUBs[SQ_MESSAGE] <= ScoreUBs[LGKM_CNT]) {
789       NumEventTypes++;
790     }
791     if (NumEventTypes <= 1) {
792       return false;
793     }
794     break;
795   }
796   case EXP_CNT: {
797     // If there has been a mixture of export types, then a waitcnt exp(0) is
798     // required.
799     if (MixedExpTypes)
800       return true;
801     int NumEventTypes = 0;
802     if (EventUBs[EXP_GPR_LOCK] > ScoreLBs[EXP_CNT] &&
803         EventUBs[EXP_GPR_LOCK] <= ScoreUBs[EXP_CNT]) {
804       NumEventTypes++;
805     }
806     if (EventUBs[GDS_GPR_LOCK] > ScoreLBs[EXP_CNT] &&
807         EventUBs[GDS_GPR_LOCK] <= ScoreUBs[EXP_CNT]) {
808       NumEventTypes++;
809     }
810     if (EventUBs[VMW_GPR_LOCK] > ScoreLBs[EXP_CNT] &&
811         EventUBs[VMW_GPR_LOCK] <= ScoreUBs[EXP_CNT]) {
812       NumEventTypes++;
813     }
814     if (EventUBs[EXP_PARAM_ACCESS] > ScoreLBs[EXP_CNT] &&
815         EventUBs[EXP_PARAM_ACCESS] <= ScoreUBs[EXP_CNT]) {
816       NumEventTypes++;
817     }
818 
819     if (EventUBs[EXP_POS_ACCESS] > ScoreLBs[EXP_CNT] &&
820         EventUBs[EXP_POS_ACCESS] <= ScoreUBs[EXP_CNT]) {
821       NumEventTypes++;
822     }
823 
824     if (NumEventTypes <= 1) {
825       return false;
826     }
827     break;
828   }
829   default:
830     break;
831   }
832   return true;
833 }
834 
835 INITIALIZE_PASS_BEGIN(SIInsertWaitcnts, DEBUG_TYPE, "SI Insert Waitcnts", false,
836                       false)
837 INITIALIZE_PASS_END(SIInsertWaitcnts, DEBUG_TYPE, "SI Insert Waitcnts", false,
838                     false)
839 
840 char SIInsertWaitcnts::ID = 0;
841 
842 char &llvm::SIInsertWaitcntsID = SIInsertWaitcnts::ID;
843 
844 FunctionPass *llvm::createSIInsertWaitcntsPass() {
845   return new SIInsertWaitcnts();
846 }
847 
848 static bool readsVCCZ(const MachineInstr &MI) {
849   unsigned Opc = MI.getOpcode();
850   return (Opc == AMDGPU::S_CBRANCH_VCCNZ || Opc == AMDGPU::S_CBRANCH_VCCZ) &&
851          !MI.getOperand(1).isUndef();
852 }
853 
854 /// Given wait count encodings checks if LHS is stronger than RHS.
855 bool SIInsertWaitcnts::isWaitcntStronger(unsigned LHS, unsigned RHS) {
856   if (AMDGPU::decodeVmcnt(IV, LHS) > AMDGPU::decodeVmcnt(IV, RHS))
857     return false;
858   if (AMDGPU::decodeLgkmcnt(IV, LHS) > AMDGPU::decodeLgkmcnt(IV, RHS))
859     return false;
860   if (AMDGPU::decodeExpcnt(IV, LHS) > AMDGPU::decodeExpcnt(IV, RHS))
861     return false;
862   return true;
863 }
864 
865 /// Given wait count encodings create a new encoding which is stronger
866 /// or equal to both.
867 unsigned SIInsertWaitcnts::combineWaitcnt(unsigned LHS, unsigned RHS) {
868   unsigned VmCnt = std::min(AMDGPU::decodeVmcnt(IV, LHS),
869                             AMDGPU::decodeVmcnt(IV, RHS));
870   unsigned LgkmCnt = std::min(AMDGPU::decodeLgkmcnt(IV, LHS),
871                               AMDGPU::decodeLgkmcnt(IV, RHS));
872   unsigned ExpCnt = std::min(AMDGPU::decodeExpcnt(IV, LHS),
873                              AMDGPU::decodeExpcnt(IV, RHS));
874   return AMDGPU::encodeWaitcnt(IV, VmCnt, ExpCnt, LgkmCnt);
875 }
876 
877 ///  Generate s_waitcnt instruction to be placed before cur_Inst.
878 ///  Instructions of a given type are returned in order,
879 ///  but instructions of different types can complete out of order.
880 ///  We rely on this in-order completion
881 ///  and simply assign a score to the memory access instructions.
882 ///  We keep track of the active "score bracket" to determine
883 ///  if an access of a memory read requires an s_waitcnt
884 ///  and if so what the value of each counter is.
885 ///  The "score bracket" is bound by the lower bound and upper bound
886 ///  scores (*_score_LB and *_score_ub respectively).
887 void SIInsertWaitcnts::generateWaitcntInstBefore(
888     MachineInstr &MI, BlockWaitcntBrackets *ScoreBrackets) {
889   // To emit, or not to emit - that's the question!
890   // Start with an assumption that there is no need to emit.
891   unsigned int EmitWaitcnt = 0;
892 
893   // No need to wait before phi. If a phi-move exists, then the wait should
894   // has been inserted before the move. If a phi-move does not exist, then
895   // wait should be inserted before the real use. The same is true for
896   // sc-merge. It is not a coincident that all these cases correspond to the
897   // instructions that are skipped in the assembling loop.
898   bool NeedLineMapping = false; // TODO: Check on this.
899 
900   // ForceEmitZeroWaitcnt: force a single s_waitcnt 0 due to hw bug
901   bool ForceEmitZeroWaitcnt = false;
902 
903   setForceEmitWaitcnt();
904   bool IsForceEmitWaitcnt = isForceEmitWaitcnt();
905 
906   if (MI.isDebugInstr() &&
907       // TODO: any other opcode?
908       !NeedLineMapping) {
909     return;
910   }
911 
912   // See if an s_waitcnt is forced at block entry, or is needed at
913   // program end.
914   if (ScoreBrackets->getWaitAtBeginning()) {
915     // Note that we have already cleared the state, so we don't need to update
916     // it.
917     ScoreBrackets->clearWaitAtBeginning();
918     for (enum InstCounterType T = VM_CNT; T < NUM_INST_CNTS;
919          T = (enum InstCounterType)(T + 1)) {
920       EmitWaitcnt |= CNT_MASK(T);
921       ScoreBrackets->setScoreLB(T, ScoreBrackets->getScoreUB(T));
922     }
923   }
924 
925   // See if this instruction has a forced S_WAITCNT VM.
926   // TODO: Handle other cases of NeedsWaitcntVmBefore()
927   else if (MI.getOpcode() == AMDGPU::BUFFER_WBINVL1 ||
928            MI.getOpcode() == AMDGPU::BUFFER_WBINVL1_SC ||
929            MI.getOpcode() == AMDGPU::BUFFER_WBINVL1_VOL) {
930     EmitWaitcnt |=
931         ScoreBrackets->updateByWait(VM_CNT, ScoreBrackets->getScoreUB(VM_CNT));
932   }
933 
934   // All waits must be resolved at call return.
935   // NOTE: this could be improved with knowledge of all call sites or
936   //   with knowledge of the called routines.
937   if (MI.getOpcode() == AMDGPU::RETURN ||
938       MI.getOpcode() == AMDGPU::SI_RETURN_TO_EPILOG ||
939       MI.getOpcode() == AMDGPU::S_SETPC_B64_return) {
940     for (enum InstCounterType T = VM_CNT; T < NUM_INST_CNTS;
941          T = (enum InstCounterType)(T + 1)) {
942       if (ScoreBrackets->getScoreUB(T) > ScoreBrackets->getScoreLB(T)) {
943         ScoreBrackets->setScoreLB(T, ScoreBrackets->getScoreUB(T));
944         EmitWaitcnt |= CNT_MASK(T);
945       }
946     }
947   }
948   // Resolve vm waits before gs-done.
949   else if ((MI.getOpcode() == AMDGPU::S_SENDMSG ||
950             MI.getOpcode() == AMDGPU::S_SENDMSGHALT) &&
951            ((MI.getOperand(0).getImm() & AMDGPU::SendMsg::ID_MASK_) ==
952             AMDGPU::SendMsg::ID_GS_DONE)) {
953     if (ScoreBrackets->getScoreUB(VM_CNT) > ScoreBrackets->getScoreLB(VM_CNT)) {
954       ScoreBrackets->setScoreLB(VM_CNT, ScoreBrackets->getScoreUB(VM_CNT));
955       EmitWaitcnt |= CNT_MASK(VM_CNT);
956     }
957   }
958 #if 0 // TODO: the following blocks of logic when we have fence.
959   else if (MI.getOpcode() == SC_FENCE) {
960     const unsigned int group_size =
961       context->shader_info->GetMaxThreadGroupSize();
962     // group_size == 0 means thread group size is unknown at compile time
963     const bool group_is_multi_wave =
964       (group_size == 0 || group_size > target_info->GetWaveFrontSize());
965     const bool fence_is_global = !((SCInstInternalMisc*)Inst)->IsGroupFence();
966 
967     for (unsigned int i = 0; i < Inst->NumSrcOperands(); i++) {
968       SCRegType src_type = Inst->GetSrcType(i);
969       switch (src_type) {
970         case SCMEM_LDS:
971           if (group_is_multi_wave ||
972             context->OptFlagIsOn(OPT_R1100_LDSMEM_FENCE_CHICKEN_BIT)) {
973             EmitWaitcnt |= ScoreBrackets->updateByWait(LGKM_CNT,
974                                ScoreBrackets->getScoreUB(LGKM_CNT));
975             // LDS may have to wait for VM_CNT after buffer load to LDS
976             if (target_info->HasBufferLoadToLDS()) {
977               EmitWaitcnt |= ScoreBrackets->updateByWait(VM_CNT,
978                                  ScoreBrackets->getScoreUB(VM_CNT));
979             }
980           }
981           break;
982 
983         case SCMEM_GDS:
984           if (group_is_multi_wave || fence_is_global) {
985             EmitWaitcnt |= ScoreBrackets->updateByWait(EXP_CNT,
986               ScoreBrackets->getScoreUB(EXP_CNT));
987             EmitWaitcnt |= ScoreBrackets->updateByWait(LGKM_CNT,
988               ScoreBrackets->getScoreUB(LGKM_CNT));
989           }
990           break;
991 
992         case SCMEM_UAV:
993         case SCMEM_TFBUF:
994         case SCMEM_RING:
995         case SCMEM_SCATTER:
996           if (group_is_multi_wave || fence_is_global) {
997             EmitWaitcnt |= ScoreBrackets->updateByWait(EXP_CNT,
998               ScoreBrackets->getScoreUB(EXP_CNT));
999             EmitWaitcnt |= ScoreBrackets->updateByWait(VM_CNT,
1000               ScoreBrackets->getScoreUB(VM_CNT));
1001           }
1002           break;
1003 
1004         case SCMEM_SCRATCH:
1005         default:
1006           break;
1007       }
1008     }
1009   }
1010 #endif
1011 
1012   // Export & GDS instructions do not read the EXEC mask until after the export
1013   // is granted (which can occur well after the instruction is issued).
1014   // The shader program must flush all EXP operations on the export-count
1015   // before overwriting the EXEC mask.
1016   else {
1017     if (MI.modifiesRegister(AMDGPU::EXEC, TRI)) {
1018       // Export and GDS are tracked individually, either may trigger a waitcnt
1019       // for EXEC.
1020       EmitWaitcnt |= ScoreBrackets->updateByWait(
1021           EXP_CNT, ScoreBrackets->getEventUB(EXP_GPR_LOCK));
1022       EmitWaitcnt |= ScoreBrackets->updateByWait(
1023           EXP_CNT, ScoreBrackets->getEventUB(EXP_PARAM_ACCESS));
1024       EmitWaitcnt |= ScoreBrackets->updateByWait(
1025           EXP_CNT, ScoreBrackets->getEventUB(EXP_POS_ACCESS));
1026       EmitWaitcnt |= ScoreBrackets->updateByWait(
1027           EXP_CNT, ScoreBrackets->getEventUB(GDS_GPR_LOCK));
1028     }
1029 
1030 #if 0 // TODO: the following code to handle CALL.
1031     // The argument passing for CALLs should suffice for VM_CNT and LGKM_CNT.
1032     // However, there is a problem with EXP_CNT, because the call cannot
1033     // easily tell if a register is used in the function, and if it did, then
1034     // the referring instruction would have to have an S_WAITCNT, which is
1035     // dependent on all call sites. So Instead, force S_WAITCNT for EXP_CNTs
1036     // before the call.
1037     if (MI.getOpcode() == SC_CALL) {
1038       if (ScoreBrackets->getScoreUB(EXP_CNT) >
1039         ScoreBrackets->getScoreLB(EXP_CNT)) {
1040         ScoreBrackets->setScoreLB(EXP_CNT, ScoreBrackets->getScoreUB(EXP_CNT));
1041         EmitWaitcnt |= CNT_MASK(EXP_CNT);
1042       }
1043     }
1044 #endif
1045 
1046     // FIXME: Should not be relying on memoperands.
1047     // Look at the source operands of every instruction to see if
1048     // any of them results from a previous memory operation that affects
1049     // its current usage. If so, an s_waitcnt instruction needs to be
1050     // emitted.
1051     // If the source operand was defined by a load, add the s_waitcnt
1052     // instruction.
1053     for (const MachineMemOperand *Memop : MI.memoperands()) {
1054       unsigned AS = Memop->getAddrSpace();
1055       if (AS != AMDGPUASI.LOCAL_ADDRESS)
1056         continue;
1057       unsigned RegNo = SQ_MAX_PGM_VGPRS + EXTRA_VGPR_LDS;
1058       // VM_CNT is only relevant to vgpr or LDS.
1059       EmitWaitcnt |= ScoreBrackets->updateByWait(
1060           VM_CNT, ScoreBrackets->getRegScore(RegNo, VM_CNT));
1061     }
1062 
1063     for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
1064       const MachineOperand &Op = MI.getOperand(I);
1065       const MachineRegisterInfo &MRIA = *MRI;
1066       RegInterval Interval =
1067           ScoreBrackets->getRegInterval(&MI, TII, MRI, TRI, I, false);
1068       for (signed RegNo = Interval.first; RegNo < Interval.second; ++RegNo) {
1069         if (TRI->isVGPR(MRIA, Op.getReg())) {
1070           // VM_CNT is only relevant to vgpr or LDS.
1071           EmitWaitcnt |= ScoreBrackets->updateByWait(
1072               VM_CNT, ScoreBrackets->getRegScore(RegNo, VM_CNT));
1073         }
1074         EmitWaitcnt |= ScoreBrackets->updateByWait(
1075             LGKM_CNT, ScoreBrackets->getRegScore(RegNo, LGKM_CNT));
1076       }
1077     }
1078     // End of for loop that looks at all source operands to decide vm_wait_cnt
1079     // and lgk_wait_cnt.
1080 
1081     // Two cases are handled for destination operands:
1082     // 1) If the destination operand was defined by a load, add the s_waitcnt
1083     // instruction to guarantee the right WAW order.
1084     // 2) If a destination operand that was used by a recent export/store ins,
1085     // add s_waitcnt on exp_cnt to guarantee the WAR order.
1086     if (MI.mayStore()) {
1087       // FIXME: Should not be relying on memoperands.
1088       for (const MachineMemOperand *Memop : MI.memoperands()) {
1089         unsigned AS = Memop->getAddrSpace();
1090         if (AS != AMDGPUASI.LOCAL_ADDRESS)
1091           continue;
1092         unsigned RegNo = SQ_MAX_PGM_VGPRS + EXTRA_VGPR_LDS;
1093         EmitWaitcnt |= ScoreBrackets->updateByWait(
1094             VM_CNT, ScoreBrackets->getRegScore(RegNo, VM_CNT));
1095         EmitWaitcnt |= ScoreBrackets->updateByWait(
1096             EXP_CNT, ScoreBrackets->getRegScore(RegNo, EXP_CNT));
1097       }
1098     }
1099     for (unsigned I = 0, E = MI.getNumOperands(); I != E; ++I) {
1100       MachineOperand &Def = MI.getOperand(I);
1101       const MachineRegisterInfo &MRIA = *MRI;
1102       RegInterval Interval =
1103           ScoreBrackets->getRegInterval(&MI, TII, MRI, TRI, I, true);
1104       for (signed RegNo = Interval.first; RegNo < Interval.second; ++RegNo) {
1105         if (TRI->isVGPR(MRIA, Def.getReg())) {
1106           EmitWaitcnt |= ScoreBrackets->updateByWait(
1107               VM_CNT, ScoreBrackets->getRegScore(RegNo, VM_CNT));
1108           EmitWaitcnt |= ScoreBrackets->updateByWait(
1109               EXP_CNT, ScoreBrackets->getRegScore(RegNo, EXP_CNT));
1110         }
1111         EmitWaitcnt |= ScoreBrackets->updateByWait(
1112             LGKM_CNT, ScoreBrackets->getRegScore(RegNo, LGKM_CNT));
1113       }
1114     } // End of for loop that looks at all dest operands.
1115   }
1116 
1117   // Check to see if this is an S_BARRIER, and if an implicit S_WAITCNT 0
1118   // occurs before the instruction. Doing it here prevents any additional
1119   // S_WAITCNTs from being emitted if the instruction was marked as
1120   // requiring a WAITCNT beforehand.
1121   if (MI.getOpcode() == AMDGPU::S_BARRIER &&
1122       !ST->hasAutoWaitcntBeforeBarrier()) {
1123     EmitWaitcnt |=
1124         ScoreBrackets->updateByWait(VM_CNT, ScoreBrackets->getScoreUB(VM_CNT));
1125     EmitWaitcnt |= ScoreBrackets->updateByWait(
1126         EXP_CNT, ScoreBrackets->getScoreUB(EXP_CNT));
1127     EmitWaitcnt |= ScoreBrackets->updateByWait(
1128         LGKM_CNT, ScoreBrackets->getScoreUB(LGKM_CNT));
1129   }
1130 
1131   // TODO: Remove this work-around, enable the assert for Bug 457939
1132   //       after fixing the scheduler. Also, the Shader Compiler code is
1133   //       independent of target.
1134   if (readsVCCZ(MI) && ST->getGeneration() <= SISubtarget::SEA_ISLANDS) {
1135     if (ScoreBrackets->getScoreLB(LGKM_CNT) <
1136             ScoreBrackets->getScoreUB(LGKM_CNT) &&
1137         ScoreBrackets->hasPendingSMEM()) {
1138       // Wait on everything, not just LGKM.  vccz reads usually come from
1139       // terminators, and we always wait on everything at the end of the
1140       // block, so if we only wait on LGKM here, we might end up with
1141       // another s_waitcnt inserted right after this if there are non-LGKM
1142       // instructions still outstanding.
1143       // FIXME: this is too conservative / the comment is wrong.
1144       // We don't wait on everything at the end of the block and we combine
1145       // waitcnts so we should never have back-to-back waitcnts.
1146       ForceEmitZeroWaitcnt = true;
1147       EmitWaitcnt = true;
1148     }
1149   }
1150 
1151   // Does this operand processing indicate s_wait counter update?
1152   if (EmitWaitcnt || IsForceEmitWaitcnt) {
1153     int CntVal[NUM_INST_CNTS];
1154 
1155     bool UseDefaultWaitcntStrategy = true;
1156     if (ForceEmitZeroWaitcnt || ForceEmitZeroWaitcnts) {
1157       // Force all waitcnts to 0.
1158       for (enum InstCounterType T = VM_CNT; T < NUM_INST_CNTS;
1159            T = (enum InstCounterType)(T + 1)) {
1160         ScoreBrackets->setScoreLB(T, ScoreBrackets->getScoreUB(T));
1161       }
1162       CntVal[VM_CNT] = 0;
1163       CntVal[EXP_CNT] = 0;
1164       CntVal[LGKM_CNT] = 0;
1165       UseDefaultWaitcntStrategy = false;
1166     }
1167 
1168     if (UseDefaultWaitcntStrategy) {
1169       for (enum InstCounterType T = VM_CNT; T < NUM_INST_CNTS;
1170            T = (enum InstCounterType)(T + 1)) {
1171         if (EmitWaitcnt & CNT_MASK(T)) {
1172           int Delta =
1173               ScoreBrackets->getScoreUB(T) - ScoreBrackets->getScoreLB(T);
1174           int MaxDelta = ScoreBrackets->getWaitCountMax(T);
1175           if (Delta >= MaxDelta) {
1176             Delta = -1;
1177             if (T != EXP_CNT) {
1178               ScoreBrackets->setScoreLB(
1179                   T, ScoreBrackets->getScoreUB(T) - MaxDelta);
1180             }
1181             EmitWaitcnt &= ~CNT_MASK(T);
1182           }
1183           CntVal[T] = Delta;
1184         } else {
1185           // If we are not waiting for a particular counter then encode
1186           // it as -1 which means "don't care."
1187           CntVal[T] = -1;
1188         }
1189       }
1190     }
1191 
1192     // If we are not waiting on any counter we can skip the wait altogether.
1193     if (EmitWaitcnt != 0 || IsForceEmitWaitcnt) {
1194       MachineInstr *OldWaitcnt = ScoreBrackets->getWaitcnt();
1195       int Imm = (!OldWaitcnt) ? 0 : OldWaitcnt->getOperand(0).getImm();
1196       if (!OldWaitcnt ||
1197           (AMDGPU::decodeVmcnt(IV, Imm) !=
1198                           (CntVal[VM_CNT] & AMDGPU::getVmcntBitMask(IV))) ||
1199           (AMDGPU::decodeExpcnt(IV, Imm) !=
1200            (CntVal[EXP_CNT] & AMDGPU::getExpcntBitMask(IV))) ||
1201           (AMDGPU::decodeLgkmcnt(IV, Imm) !=
1202            (CntVal[LGKM_CNT] & AMDGPU::getLgkmcntBitMask(IV)))) {
1203         MachineLoop *ContainingLoop = MLI->getLoopFor(MI.getParent());
1204         if (ContainingLoop) {
1205           MachineBasicBlock *TBB = ContainingLoop->getHeader();
1206           BlockWaitcntBrackets *ScoreBracket =
1207               BlockWaitcntBracketsMap[TBB].get();
1208           if (!ScoreBracket) {
1209             assert(!BlockVisitedSet.count(TBB));
1210             BlockWaitcntBracketsMap[TBB] =
1211                 llvm::make_unique<BlockWaitcntBrackets>(ST);
1212             ScoreBracket = BlockWaitcntBracketsMap[TBB].get();
1213           }
1214           ScoreBracket->setRevisitLoop(true);
1215           LLVM_DEBUG(dbgs()
1216                          << "set-revisit2: Block"
1217                          << ContainingLoop->getHeader()->getNumber() << '\n';);
1218         }
1219       }
1220 
1221       // Update an existing waitcount, or make a new one.
1222       unsigned Enc = AMDGPU::encodeWaitcnt(IV,
1223                       ForceEmitWaitcnt[VM_CNT] ? 0 : CntVal[VM_CNT],
1224                       ForceEmitWaitcnt[EXP_CNT] ? 0 : CntVal[EXP_CNT],
1225                       ForceEmitWaitcnt[LGKM_CNT] ? 0 : CntVal[LGKM_CNT]);
1226       // We don't remove waitcnts that existed prior to the waitcnt
1227       // pass. Check if the waitcnt to-be-inserted can be avoided
1228       // or if the prev waitcnt can be updated.
1229       bool insertSWaitInst = true;
1230       for (MachineBasicBlock::iterator I = MI.getIterator(),
1231                                        B = MI.getParent()->begin();
1232            insertSWaitInst && I != B; --I) {
1233         if (I == MI.getIterator())
1234           continue;
1235 
1236         switch (I->getOpcode()) {
1237         case AMDGPU::S_WAITCNT:
1238           if (isWaitcntStronger(I->getOperand(0).getImm(), Enc))
1239             insertSWaitInst = false;
1240           else if (!OldWaitcnt) {
1241             OldWaitcnt = &*I;
1242             Enc = combineWaitcnt(I->getOperand(0).getImm(), Enc);
1243           }
1244           break;
1245         // TODO: skip over instructions which never require wait.
1246         }
1247         break;
1248       }
1249       if (insertSWaitInst) {
1250         if (OldWaitcnt && OldWaitcnt->getOpcode() == AMDGPU::S_WAITCNT) {
1251           if (ForceEmitZeroWaitcnts)
1252             LLVM_DEBUG(
1253                 dbgs()
1254                 << "Force emit s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)\n");
1255           if (IsForceEmitWaitcnt)
1256             LLVM_DEBUG(dbgs()
1257                        << "Force emit a s_waitcnt due to debug counter\n");
1258 
1259           OldWaitcnt->getOperand(0).setImm(Enc);
1260           if (!OldWaitcnt->getParent())
1261             MI.getParent()->insert(MI, OldWaitcnt);
1262 
1263           LLVM_DEBUG(dbgs() << "updateWaitcntInBlock\n"
1264                             << "Old Instr: " << MI << '\n'
1265                             << "New Instr: " << *OldWaitcnt << '\n');
1266         } else {
1267             auto SWaitInst = BuildMI(*MI.getParent(), MI.getIterator(),
1268                                MI.getDebugLoc(), TII->get(AMDGPU::S_WAITCNT))
1269                              .addImm(Enc);
1270             TrackedWaitcntSet.insert(SWaitInst);
1271 
1272             LLVM_DEBUG(dbgs() << "insertWaitcntInBlock\n"
1273                               << "Old Instr: " << MI << '\n'
1274                               << "New Instr: " << *SWaitInst << '\n');
1275         }
1276       }
1277 
1278       if (CntVal[EXP_CNT] == 0) {
1279         ScoreBrackets->setMixedExpTypes(false);
1280       }
1281     }
1282   }
1283 }
1284 
1285 void SIInsertWaitcnts::insertWaitcntBeforeCF(MachineBasicBlock &MBB,
1286                                              MachineInstr *Waitcnt) {
1287   if (MBB.empty()) {
1288     MBB.push_back(Waitcnt);
1289     return;
1290   }
1291 
1292   MachineBasicBlock::iterator It = MBB.end();
1293   MachineInstr *MI = &*(--It);
1294   if (MI->isBranch()) {
1295     MBB.insert(It, Waitcnt);
1296   } else {
1297     MBB.push_back(Waitcnt);
1298   }
1299 }
1300 
1301 // This is a flat memory operation. Check to see if it has memory
1302 // tokens for both LDS and Memory, and if so mark it as a flat.
1303 bool SIInsertWaitcnts::mayAccessLDSThroughFlat(const MachineInstr &MI) const {
1304   if (MI.memoperands_empty())
1305     return true;
1306 
1307   for (const MachineMemOperand *Memop : MI.memoperands()) {
1308     unsigned AS = Memop->getAddrSpace();
1309     if (AS == AMDGPUASI.LOCAL_ADDRESS || AS == AMDGPUASI.FLAT_ADDRESS)
1310       return true;
1311   }
1312 
1313   return false;
1314 }
1315 
1316 void SIInsertWaitcnts::updateEventWaitcntAfter(
1317     MachineInstr &Inst, BlockWaitcntBrackets *ScoreBrackets) {
1318   // Now look at the instruction opcode. If it is a memory access
1319   // instruction, update the upper-bound of the appropriate counter's
1320   // bracket and the destination operand scores.
1321   // TODO: Use the (TSFlags & SIInstrFlags::LGKM_CNT) property everywhere.
1322   if (TII->isDS(Inst) && TII->usesLGKM_CNT(Inst)) {
1323     if (TII->hasModifiersSet(Inst, AMDGPU::OpName::gds)) {
1324       ScoreBrackets->updateByEvent(TII, TRI, MRI, GDS_ACCESS, Inst);
1325       ScoreBrackets->updateByEvent(TII, TRI, MRI, GDS_GPR_LOCK, Inst);
1326     } else {
1327       ScoreBrackets->updateByEvent(TII, TRI, MRI, LDS_ACCESS, Inst);
1328     }
1329   } else if (TII->isFLAT(Inst)) {
1330     assert(Inst.mayLoad() || Inst.mayStore());
1331 
1332     if (TII->usesVM_CNT(Inst))
1333       ScoreBrackets->updateByEvent(TII, TRI, MRI, VMEM_ACCESS, Inst);
1334 
1335     if (TII->usesLGKM_CNT(Inst)) {
1336       ScoreBrackets->updateByEvent(TII, TRI, MRI, LDS_ACCESS, Inst);
1337 
1338       // This is a flat memory operation, so note it - it will require
1339       // that both the VM and LGKM be flushed to zero if it is pending when
1340       // a VM or LGKM dependency occurs.
1341       if (mayAccessLDSThroughFlat(Inst))
1342         ScoreBrackets->setPendingFlat();
1343     }
1344   } else if (SIInstrInfo::isVMEM(Inst) &&
1345              // TODO: get a better carve out.
1346              Inst.getOpcode() != AMDGPU::BUFFER_WBINVL1 &&
1347              Inst.getOpcode() != AMDGPU::BUFFER_WBINVL1_SC &&
1348              Inst.getOpcode() != AMDGPU::BUFFER_WBINVL1_VOL) {
1349     ScoreBrackets->updateByEvent(TII, TRI, MRI, VMEM_ACCESS, Inst);
1350     if (ST->vmemWriteNeedsExpWaitcnt() &&
1351         (Inst.mayStore() || AMDGPU::getAtomicNoRetOp(Inst.getOpcode()) != -1)) {
1352       ScoreBrackets->updateByEvent(TII, TRI, MRI, VMW_GPR_LOCK, Inst);
1353     }
1354   } else if (TII->isSMRD(Inst)) {
1355     ScoreBrackets->updateByEvent(TII, TRI, MRI, SMEM_ACCESS, Inst);
1356   } else {
1357     switch (Inst.getOpcode()) {
1358     case AMDGPU::S_SENDMSG:
1359     case AMDGPU::S_SENDMSGHALT:
1360       ScoreBrackets->updateByEvent(TII, TRI, MRI, SQ_MESSAGE, Inst);
1361       break;
1362     case AMDGPU::EXP:
1363     case AMDGPU::EXP_DONE: {
1364       int Imm = TII->getNamedOperand(Inst, AMDGPU::OpName::tgt)->getImm();
1365       if (Imm >= 32 && Imm <= 63)
1366         ScoreBrackets->updateByEvent(TII, TRI, MRI, EXP_PARAM_ACCESS, Inst);
1367       else if (Imm >= 12 && Imm <= 15)
1368         ScoreBrackets->updateByEvent(TII, TRI, MRI, EXP_POS_ACCESS, Inst);
1369       else
1370         ScoreBrackets->updateByEvent(TII, TRI, MRI, EXP_GPR_LOCK, Inst);
1371       break;
1372     }
1373     case AMDGPU::S_MEMTIME:
1374     case AMDGPU::S_MEMREALTIME:
1375       ScoreBrackets->updateByEvent(TII, TRI, MRI, SMEM_ACCESS, Inst);
1376       break;
1377     default:
1378       break;
1379     }
1380   }
1381 }
1382 
1383 // Merge the score brackets of the Block's predecessors;
1384 // this merged score bracket is used when adding waitcnts to the Block
1385 void SIInsertWaitcnts::mergeInputScoreBrackets(MachineBasicBlock &Block) {
1386   BlockWaitcntBrackets *ScoreBrackets = BlockWaitcntBracketsMap[&Block].get();
1387   int32_t MaxPending[NUM_INST_CNTS] = {0};
1388   int32_t MaxFlat[NUM_INST_CNTS] = {0};
1389   bool MixedExpTypes = false;
1390 
1391   // For single basic block loops, we need to retain the Block's
1392   // score bracket to have accurate Pred info. So, make a copy of Block's
1393   // score bracket, clear() it (which retains several important bits of info),
1394   // populate, and then replace en masse. For non-single basic block loops,
1395   // just clear Block's current score bracket and repopulate in-place.
1396   bool IsSelfPred;
1397   std::unique_ptr<BlockWaitcntBrackets> S;
1398 
1399   IsSelfPred = (std::find(Block.pred_begin(), Block.pred_end(), &Block))
1400     != Block.pred_end();
1401   if (IsSelfPred) {
1402     S = llvm::make_unique<BlockWaitcntBrackets>(*ScoreBrackets);
1403     ScoreBrackets = S.get();
1404   }
1405 
1406   ScoreBrackets->clear();
1407 
1408   // See if there are any uninitialized predecessors. If so, emit an
1409   // s_waitcnt 0 at the beginning of the block.
1410   for (MachineBasicBlock *Pred : Block.predecessors()) {
1411     BlockWaitcntBrackets *PredScoreBrackets =
1412         BlockWaitcntBracketsMap[Pred].get();
1413     bool Visited = BlockVisitedSet.count(Pred);
1414     if (!Visited || PredScoreBrackets->getWaitAtBeginning()) {
1415       continue;
1416     }
1417     for (enum InstCounterType T = VM_CNT; T < NUM_INST_CNTS;
1418          T = (enum InstCounterType)(T + 1)) {
1419       int span =
1420           PredScoreBrackets->getScoreUB(T) - PredScoreBrackets->getScoreLB(T);
1421       MaxPending[T] = std::max(MaxPending[T], span);
1422       span =
1423           PredScoreBrackets->pendingFlat(T) - PredScoreBrackets->getScoreLB(T);
1424       MaxFlat[T] = std::max(MaxFlat[T], span);
1425     }
1426 
1427     MixedExpTypes |= PredScoreBrackets->mixedExpTypes();
1428   }
1429 
1430   // TODO: Is SC Block->IsMainExit() same as Block.succ_empty()?
1431   // Also handle kills for exit block.
1432   if (Block.succ_empty() && !KillWaitBrackets.empty()) {
1433     for (unsigned int I = 0; I < KillWaitBrackets.size(); I++) {
1434       for (enum InstCounterType T = VM_CNT; T < NUM_INST_CNTS;
1435            T = (enum InstCounterType)(T + 1)) {
1436         int Span = KillWaitBrackets[I]->getScoreUB(T) -
1437                    KillWaitBrackets[I]->getScoreLB(T);
1438         MaxPending[T] = std::max(MaxPending[T], Span);
1439         Span = KillWaitBrackets[I]->pendingFlat(T) -
1440                KillWaitBrackets[I]->getScoreLB(T);
1441         MaxFlat[T] = std::max(MaxFlat[T], Span);
1442       }
1443 
1444       MixedExpTypes |= KillWaitBrackets[I]->mixedExpTypes();
1445     }
1446   }
1447 
1448   // Special handling for GDS_GPR_LOCK and EXP_GPR_LOCK.
1449   for (MachineBasicBlock *Pred : Block.predecessors()) {
1450     BlockWaitcntBrackets *PredScoreBrackets =
1451         BlockWaitcntBracketsMap[Pred].get();
1452     bool Visited = BlockVisitedSet.count(Pred);
1453     if (!Visited || PredScoreBrackets->getWaitAtBeginning()) {
1454       continue;
1455     }
1456 
1457     int GDSSpan = PredScoreBrackets->getEventUB(GDS_GPR_LOCK) -
1458                   PredScoreBrackets->getScoreLB(EXP_CNT);
1459     MaxPending[EXP_CNT] = std::max(MaxPending[EXP_CNT], GDSSpan);
1460     int EXPSpan = PredScoreBrackets->getEventUB(EXP_GPR_LOCK) -
1461                   PredScoreBrackets->getScoreLB(EXP_CNT);
1462     MaxPending[EXP_CNT] = std::max(MaxPending[EXP_CNT], EXPSpan);
1463   }
1464 
1465   // TODO: Is SC Block->IsMainExit() same as Block.succ_empty()?
1466   if (Block.succ_empty() && !KillWaitBrackets.empty()) {
1467     for (unsigned int I = 0; I < KillWaitBrackets.size(); I++) {
1468       int GDSSpan = KillWaitBrackets[I]->getEventUB(GDS_GPR_LOCK) -
1469                     KillWaitBrackets[I]->getScoreLB(EXP_CNT);
1470       MaxPending[EXP_CNT] = std::max(MaxPending[EXP_CNT], GDSSpan);
1471       int EXPSpan = KillWaitBrackets[I]->getEventUB(EXP_GPR_LOCK) -
1472                     KillWaitBrackets[I]->getScoreLB(EXP_CNT);
1473       MaxPending[EXP_CNT] = std::max(MaxPending[EXP_CNT], EXPSpan);
1474     }
1475   }
1476 
1477 #if 0
1478   // LC does not (unlike) add a waitcnt at beginning. Leaving it as marker.
1479   // TODO: how does LC distinguish between function entry and main entry?
1480   // If this is the entry to a function, force a wait.
1481   MachineBasicBlock &Entry = Block.getParent()->front();
1482   if (Entry.getNumber() == Block.getNumber()) {
1483     ScoreBrackets->setWaitAtBeginning();
1484     return;
1485   }
1486 #endif
1487 
1488   // Now set the current Block's brackets to the largest ending bracket.
1489   for (enum InstCounterType T = VM_CNT; T < NUM_INST_CNTS;
1490        T = (enum InstCounterType)(T + 1)) {
1491     ScoreBrackets->setScoreUB(T, MaxPending[T]);
1492     ScoreBrackets->setScoreLB(T, 0);
1493     ScoreBrackets->setLastFlat(T, MaxFlat[T]);
1494   }
1495 
1496   ScoreBrackets->setMixedExpTypes(MixedExpTypes);
1497 
1498   // Set the register scoreboard.
1499   for (MachineBasicBlock *Pred : Block.predecessors()) {
1500     if (!BlockVisitedSet.count(Pred)) {
1501       continue;
1502     }
1503 
1504     BlockWaitcntBrackets *PredScoreBrackets =
1505         BlockWaitcntBracketsMap[Pred].get();
1506 
1507     // Now merge the gpr_reg_score information
1508     for (enum InstCounterType T = VM_CNT; T < NUM_INST_CNTS;
1509          T = (enum InstCounterType)(T + 1)) {
1510       int PredLB = PredScoreBrackets->getScoreLB(T);
1511       int PredUB = PredScoreBrackets->getScoreUB(T);
1512       if (PredLB < PredUB) {
1513         int PredScale = MaxPending[T] - PredUB;
1514         // Merge vgpr scores.
1515         for (int J = 0; J <= PredScoreBrackets->getMaxVGPR(); J++) {
1516           int PredRegScore = PredScoreBrackets->getRegScore(J, T);
1517           if (PredRegScore <= PredLB)
1518             continue;
1519           int NewRegScore = PredScale + PredRegScore;
1520           ScoreBrackets->setRegScore(
1521               J, T, std::max(ScoreBrackets->getRegScore(J, T), NewRegScore));
1522         }
1523         // Also need to merge sgpr scores for lgkm_cnt.
1524         if (T == LGKM_CNT) {
1525           for (int J = 0; J <= PredScoreBrackets->getMaxSGPR(); J++) {
1526             int PredRegScore =
1527                 PredScoreBrackets->getRegScore(J + NUM_ALL_VGPRS, LGKM_CNT);
1528             if (PredRegScore <= PredLB)
1529               continue;
1530             int NewRegScore = PredScale + PredRegScore;
1531             ScoreBrackets->setRegScore(
1532                 J + NUM_ALL_VGPRS, LGKM_CNT,
1533                 std::max(
1534                     ScoreBrackets->getRegScore(J + NUM_ALL_VGPRS, LGKM_CNT),
1535                     NewRegScore));
1536           }
1537         }
1538       }
1539     }
1540 
1541     // Also merge the WaitEvent information.
1542     ForAllWaitEventType(W) {
1543       enum InstCounterType T = PredScoreBrackets->eventCounter(W);
1544       int PredEventUB = PredScoreBrackets->getEventUB(W);
1545       if (PredEventUB > PredScoreBrackets->getScoreLB(T)) {
1546         int NewEventUB =
1547             MaxPending[T] + PredEventUB - PredScoreBrackets->getScoreUB(T);
1548         if (NewEventUB > 0) {
1549           ScoreBrackets->setEventUB(
1550               W, std::max(ScoreBrackets->getEventUB(W), NewEventUB));
1551         }
1552       }
1553     }
1554   }
1555 
1556   // TODO: Is SC Block->IsMainExit() same as Block.succ_empty()?
1557   // Set the register scoreboard.
1558   if (Block.succ_empty() && !KillWaitBrackets.empty()) {
1559     for (unsigned int I = 0; I < KillWaitBrackets.size(); I++) {
1560       // Now merge the gpr_reg_score information.
1561       for (enum InstCounterType T = VM_CNT; T < NUM_INST_CNTS;
1562            T = (enum InstCounterType)(T + 1)) {
1563         int PredLB = KillWaitBrackets[I]->getScoreLB(T);
1564         int PredUB = KillWaitBrackets[I]->getScoreUB(T);
1565         if (PredLB < PredUB) {
1566           int PredScale = MaxPending[T] - PredUB;
1567           // Merge vgpr scores.
1568           for (int J = 0; J <= KillWaitBrackets[I]->getMaxVGPR(); J++) {
1569             int PredRegScore = KillWaitBrackets[I]->getRegScore(J, T);
1570             if (PredRegScore <= PredLB)
1571               continue;
1572             int NewRegScore = PredScale + PredRegScore;
1573             ScoreBrackets->setRegScore(
1574                 J, T, std::max(ScoreBrackets->getRegScore(J, T), NewRegScore));
1575           }
1576           // Also need to merge sgpr scores for lgkm_cnt.
1577           if (T == LGKM_CNT) {
1578             for (int J = 0; J <= KillWaitBrackets[I]->getMaxSGPR(); J++) {
1579               int PredRegScore =
1580                   KillWaitBrackets[I]->getRegScore(J + NUM_ALL_VGPRS, LGKM_CNT);
1581               if (PredRegScore <= PredLB)
1582                 continue;
1583               int NewRegScore = PredScale + PredRegScore;
1584               ScoreBrackets->setRegScore(
1585                   J + NUM_ALL_VGPRS, LGKM_CNT,
1586                   std::max(
1587                       ScoreBrackets->getRegScore(J + NUM_ALL_VGPRS, LGKM_CNT),
1588                       NewRegScore));
1589             }
1590           }
1591         }
1592       }
1593 
1594       // Also merge the WaitEvent information.
1595       ForAllWaitEventType(W) {
1596         enum InstCounterType T = KillWaitBrackets[I]->eventCounter(W);
1597         int PredEventUB = KillWaitBrackets[I]->getEventUB(W);
1598         if (PredEventUB > KillWaitBrackets[I]->getScoreLB(T)) {
1599           int NewEventUB =
1600               MaxPending[T] + PredEventUB - KillWaitBrackets[I]->getScoreUB(T);
1601           if (NewEventUB > 0) {
1602             ScoreBrackets->setEventUB(
1603                 W, std::max(ScoreBrackets->getEventUB(W), NewEventUB));
1604           }
1605         }
1606       }
1607     }
1608   }
1609 
1610   // Special case handling of GDS_GPR_LOCK and EXP_GPR_LOCK. Merge this for the
1611   // sequencing predecessors, because changes to EXEC require waitcnts due to
1612   // the delayed nature of these operations.
1613   for (MachineBasicBlock *Pred : Block.predecessors()) {
1614     if (!BlockVisitedSet.count(Pred)) {
1615       continue;
1616     }
1617 
1618     BlockWaitcntBrackets *PredScoreBrackets =
1619         BlockWaitcntBracketsMap[Pred].get();
1620 
1621     int pred_gds_ub = PredScoreBrackets->getEventUB(GDS_GPR_LOCK);
1622     if (pred_gds_ub > PredScoreBrackets->getScoreLB(EXP_CNT)) {
1623       int new_gds_ub = MaxPending[EXP_CNT] + pred_gds_ub -
1624                        PredScoreBrackets->getScoreUB(EXP_CNT);
1625       if (new_gds_ub > 0) {
1626         ScoreBrackets->setEventUB(
1627             GDS_GPR_LOCK,
1628             std::max(ScoreBrackets->getEventUB(GDS_GPR_LOCK), new_gds_ub));
1629       }
1630     }
1631     int pred_exp_ub = PredScoreBrackets->getEventUB(EXP_GPR_LOCK);
1632     if (pred_exp_ub > PredScoreBrackets->getScoreLB(EXP_CNT)) {
1633       int new_exp_ub = MaxPending[EXP_CNT] + pred_exp_ub -
1634                        PredScoreBrackets->getScoreUB(EXP_CNT);
1635       if (new_exp_ub > 0) {
1636         ScoreBrackets->setEventUB(
1637             EXP_GPR_LOCK,
1638             std::max(ScoreBrackets->getEventUB(EXP_GPR_LOCK), new_exp_ub));
1639       }
1640     }
1641   }
1642 
1643   // if a single block loop, update the score brackets. Not needed for other
1644   // blocks, as we did this in-place
1645   if (IsSelfPred) {
1646     BlockWaitcntBracketsMap[&Block] = llvm::make_unique<BlockWaitcntBrackets>(*ScoreBrackets);
1647   }
1648 }
1649 
1650 /// Return true if the given basic block is a "bottom" block of a loop.
1651 /// This works even if the loop is discontiguous. This also handles
1652 /// multiple back-edges for the same "header" block of a loop.
1653 bool SIInsertWaitcnts::isLoopBottom(const MachineLoop *Loop,
1654                                     const MachineBasicBlock *Block) {
1655   for (MachineBasicBlock *MBB : Loop->blocks()) {
1656     if (MBB == Block && MBB->isSuccessor(Loop->getHeader())) {
1657       return true;
1658     }
1659   }
1660   return false;
1661 }
1662 
1663 /// Count the number of "bottom" basic blocks of a loop.
1664 unsigned SIInsertWaitcnts::countNumBottomBlocks(const MachineLoop *Loop) {
1665   unsigned Count = 0;
1666   for (MachineBasicBlock *MBB : Loop->blocks()) {
1667     if (MBB->isSuccessor(Loop->getHeader())) {
1668       Count++;
1669     }
1670   }
1671   return Count;
1672 }
1673 
1674 // Generate s_waitcnt instructions where needed.
1675 void SIInsertWaitcnts::insertWaitcntInBlock(MachineFunction &MF,
1676                                             MachineBasicBlock &Block) {
1677   // Initialize the state information.
1678   mergeInputScoreBrackets(Block);
1679 
1680   BlockWaitcntBrackets *ScoreBrackets = BlockWaitcntBracketsMap[&Block].get();
1681 
1682   LLVM_DEBUG({
1683     dbgs() << "*** Block" << Block.getNumber() << " ***";
1684     ScoreBrackets->dump();
1685   });
1686 
1687   // Walk over the instructions.
1688   for (MachineBasicBlock::iterator Iter = Block.begin(), E = Block.end();
1689        Iter != E;) {
1690     MachineInstr &Inst = *Iter;
1691     // Remove any previously existing waitcnts.
1692     if (Inst.getOpcode() == AMDGPU::S_WAITCNT) {
1693       // Leave pre-existing waitcnts, but note their existence via setWaitcnt.
1694       // Remove the waitcnt-pass-generated waitcnts; the pass will add them back
1695       // as needed.
1696       if (!TrackedWaitcntSet.count(&Inst))
1697         ++Iter;
1698       else {
1699         ++Iter;
1700         Inst.removeFromParent();
1701       }
1702       ScoreBrackets->setWaitcnt(&Inst);
1703       continue;
1704     }
1705 
1706     // Kill instructions generate a conditional branch to the endmain block.
1707     // Merge the current waitcnt state into the endmain block information.
1708     // TODO: Are there other flavors of KILL instruction?
1709     if (Inst.getOpcode() == AMDGPU::KILL) {
1710       addKillWaitBracket(ScoreBrackets);
1711     }
1712 
1713     bool VCCZBugWorkAround = false;
1714     if (readsVCCZ(Inst) &&
1715         (!VCCZBugHandledSet.count(&Inst))) {
1716       if (ScoreBrackets->getScoreLB(LGKM_CNT) <
1717               ScoreBrackets->getScoreUB(LGKM_CNT) &&
1718           ScoreBrackets->hasPendingSMEM()) {
1719         if (ST->getGeneration() <= SISubtarget::SEA_ISLANDS)
1720           VCCZBugWorkAround = true;
1721       }
1722     }
1723 
1724     // Generate an s_waitcnt instruction to be placed before
1725     // cur_Inst, if needed.
1726     generateWaitcntInstBefore(Inst, ScoreBrackets);
1727 
1728     updateEventWaitcntAfter(Inst, ScoreBrackets);
1729 
1730 #if 0 // TODO: implement resource type check controlled by options with ub = LB.
1731     // If this instruction generates a S_SETVSKIP because it is an
1732     // indexed resource, and we are on Tahiti, then it will also force
1733     // an S_WAITCNT vmcnt(0)
1734     if (RequireCheckResourceType(Inst, context)) {
1735       // Force the score to as if an S_WAITCNT vmcnt(0) is emitted.
1736       ScoreBrackets->setScoreLB(VM_CNT,
1737       ScoreBrackets->getScoreUB(VM_CNT));
1738     }
1739 #endif
1740 
1741     ScoreBrackets->clearWaitcnt();
1742 
1743     LLVM_DEBUG({
1744       Inst.print(dbgs());
1745       ScoreBrackets->dump();
1746     });
1747 
1748     // Check to see if this is a GWS instruction. If so, and if this is CI or
1749     // VI, then the generated code sequence will include an S_WAITCNT 0.
1750     // TODO: Are these the only GWS instructions?
1751     if (Inst.getOpcode() == AMDGPU::DS_GWS_INIT ||
1752         Inst.getOpcode() == AMDGPU::DS_GWS_SEMA_V ||
1753         Inst.getOpcode() == AMDGPU::DS_GWS_SEMA_BR ||
1754         Inst.getOpcode() == AMDGPU::DS_GWS_SEMA_P ||
1755         Inst.getOpcode() == AMDGPU::DS_GWS_BARRIER) {
1756       // TODO: && context->target_info->GwsRequiresMemViolTest() ) {
1757       ScoreBrackets->updateByWait(VM_CNT, ScoreBrackets->getScoreUB(VM_CNT));
1758       ScoreBrackets->updateByWait(EXP_CNT, ScoreBrackets->getScoreUB(EXP_CNT));
1759       ScoreBrackets->updateByWait(LGKM_CNT,
1760                                   ScoreBrackets->getScoreUB(LGKM_CNT));
1761     }
1762 
1763     // TODO: Remove this work-around after fixing the scheduler and enable the
1764     // assert above.
1765     if (VCCZBugWorkAround) {
1766       // Restore the vccz bit.  Any time a value is written to vcc, the vcc
1767       // bit is updated, so we can restore the bit by reading the value of
1768       // vcc and then writing it back to the register.
1769       BuildMI(Block, Inst, Inst.getDebugLoc(), TII->get(AMDGPU::S_MOV_B64),
1770               AMDGPU::VCC)
1771           .addReg(AMDGPU::VCC);
1772       VCCZBugHandledSet.insert(&Inst);
1773     }
1774 
1775     ++Iter;
1776   }
1777 
1778   // Check if we need to force convergence at loop footer.
1779   MachineLoop *ContainingLoop = MLI->getLoopFor(&Block);
1780   if (ContainingLoop && isLoopBottom(ContainingLoop, &Block)) {
1781     LoopWaitcntData *WaitcntData = LoopWaitcntDataMap[ContainingLoop].get();
1782     WaitcntData->print();
1783     LLVM_DEBUG(dbgs() << '\n';);
1784 
1785     // The iterative waitcnt insertion algorithm aims for optimal waitcnt
1786     // placement, but doesn't guarantee convergence for a loop. Each
1787     // loop should take at most (n+1) iterations for it to converge naturally,
1788     // where n is the number of bottom blocks. If this threshold is reached and
1789     // the result hasn't converged, then we force convergence by inserting
1790     // a s_waitcnt at the end of loop footer.
1791     if (WaitcntData->getIterCnt() > (countNumBottomBlocks(ContainingLoop) + 1)) {
1792       // To ensure convergence, need to make wait events at loop footer be no
1793       // more than those from the previous iteration.
1794       // As a simplification, instead of tracking individual scores and
1795       // generating the precise wait count, just wait on 0.
1796       bool HasPending = false;
1797       MachineInstr *SWaitInst = WaitcntData->getWaitcnt();
1798       for (enum InstCounterType T = VM_CNT; T < NUM_INST_CNTS;
1799            T = (enum InstCounterType)(T + 1)) {
1800         if (ScoreBrackets->getScoreUB(T) > ScoreBrackets->getScoreLB(T)) {
1801           ScoreBrackets->setScoreLB(T, ScoreBrackets->getScoreUB(T));
1802           HasPending = true;
1803           break;
1804         }
1805       }
1806 
1807       if (HasPending) {
1808         if (!SWaitInst) {
1809           SWaitInst = BuildMI(Block, Block.getFirstNonPHI(),
1810                               DebugLoc(), TII->get(AMDGPU::S_WAITCNT))
1811                               .addImm(0);
1812           TrackedWaitcntSet.insert(SWaitInst);
1813 #if 0 // TODO: Format the debug output
1814           OutputTransformBanner("insertWaitcntInBlock",0,"Create:",context);
1815           OutputTransformAdd(SWaitInst, context);
1816 #endif
1817         }
1818 #if 0 // TODO: ??
1819         _DEV( REPORTED_STATS->force_waitcnt_converge = 1; )
1820 #endif
1821       }
1822 
1823       if (SWaitInst) {
1824         LLVM_DEBUG({
1825           SWaitInst->print(dbgs());
1826           dbgs() << "\nAdjusted score board:";
1827           ScoreBrackets->dump();
1828         });
1829 
1830         // Add this waitcnt to the block. It is either newly created or
1831         // created in previous iterations and added back since block traversal
1832         // always removes waitcnts.
1833         insertWaitcntBeforeCF(Block, SWaitInst);
1834         WaitcntData->setWaitcnt(SWaitInst);
1835       }
1836     }
1837   }
1838 }
1839 
1840 bool SIInsertWaitcnts::runOnMachineFunction(MachineFunction &MF) {
1841   ST = &MF.getSubtarget<SISubtarget>();
1842   TII = ST->getInstrInfo();
1843   TRI = &TII->getRegisterInfo();
1844   MRI = &MF.getRegInfo();
1845   MLI = &getAnalysis<MachineLoopInfo>();
1846   IV = AMDGPU::IsaInfo::getIsaVersion(ST->getFeatureBits());
1847   const SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
1848   AMDGPUASI = ST->getAMDGPUAS();
1849 
1850   ForceEmitZeroWaitcnts = ForceEmitZeroFlag;
1851   for (enum InstCounterType T = VM_CNT; T < NUM_INST_CNTS;
1852        T = (enum InstCounterType)(T + 1))
1853     ForceEmitWaitcnt[T] = false;
1854 
1855   HardwareLimits.VmcntMax = AMDGPU::getVmcntBitMask(IV);
1856   HardwareLimits.ExpcntMax = AMDGPU::getExpcntBitMask(IV);
1857   HardwareLimits.LgkmcntMax = AMDGPU::getLgkmcntBitMask(IV);
1858 
1859   HardwareLimits.NumVGPRsMax = ST->getAddressableNumVGPRs();
1860   HardwareLimits.NumSGPRsMax = ST->getAddressableNumSGPRs();
1861   assert(HardwareLimits.NumVGPRsMax <= SQ_MAX_PGM_VGPRS);
1862   assert(HardwareLimits.NumSGPRsMax <= SQ_MAX_PGM_SGPRS);
1863 
1864   RegisterEncoding.VGPR0 = TRI->getEncodingValue(AMDGPU::VGPR0);
1865   RegisterEncoding.VGPRL =
1866       RegisterEncoding.VGPR0 + HardwareLimits.NumVGPRsMax - 1;
1867   RegisterEncoding.SGPR0 = TRI->getEncodingValue(AMDGPU::SGPR0);
1868   RegisterEncoding.SGPRL =
1869       RegisterEncoding.SGPR0 + HardwareLimits.NumSGPRsMax - 1;
1870 
1871   TrackedWaitcntSet.clear();
1872   BlockVisitedSet.clear();
1873   VCCZBugHandledSet.clear();
1874   LoopWaitcntDataMap.clear();
1875   BlockWaitcntProcessedSet.clear();
1876 
1877   // Walk over the blocks in reverse post-dominator order, inserting
1878   // s_waitcnt where needed.
1879   ReversePostOrderTraversal<MachineFunction *> RPOT(&MF);
1880   bool Modified = false;
1881   for (ReversePostOrderTraversal<MachineFunction *>::rpo_iterator
1882            I = RPOT.begin(),
1883            E = RPOT.end(), J = RPOT.begin();
1884        I != E;) {
1885     MachineBasicBlock &MBB = **I;
1886 
1887     BlockVisitedSet.insert(&MBB);
1888 
1889     BlockWaitcntBrackets *ScoreBrackets = BlockWaitcntBracketsMap[&MBB].get();
1890     if (!ScoreBrackets) {
1891       BlockWaitcntBracketsMap[&MBB] = llvm::make_unique<BlockWaitcntBrackets>(ST);
1892       ScoreBrackets = BlockWaitcntBracketsMap[&MBB].get();
1893     }
1894     ScoreBrackets->setPostOrder(MBB.getNumber());
1895     MachineLoop *ContainingLoop = MLI->getLoopFor(&MBB);
1896     if (ContainingLoop && LoopWaitcntDataMap[ContainingLoop] == nullptr)
1897       LoopWaitcntDataMap[ContainingLoop] = llvm::make_unique<LoopWaitcntData>();
1898 
1899     // If we are walking into the block from before the loop, then guarantee
1900     // at least 1 re-walk over the loop to propagate the information, even if
1901     // no S_WAITCNT instructions were generated.
1902     if (ContainingLoop && ContainingLoop->getHeader() == &MBB) {
1903       unsigned Count = countNumBottomBlocks(ContainingLoop);
1904 
1905       // If the loop has multiple back-edges, and so more than one "bottom"
1906       // basic block, we have to guarantee a re-walk over every blocks.
1907       if ((std::count(BlockWaitcntProcessedSet.begin(),
1908                       BlockWaitcntProcessedSet.end(), &MBB) < Count)) {
1909         BlockWaitcntBracketsMap[&MBB]->setRevisitLoop(true);
1910         LLVM_DEBUG(dbgs() << "set-revisit1: Block"
1911                           << ContainingLoop->getHeader()->getNumber() << '\n';);
1912       }
1913     }
1914 
1915     // Walk over the instructions.
1916     insertWaitcntInBlock(MF, MBB);
1917 
1918     // Record that waitcnts have been processed at least once for this block.
1919     BlockWaitcntProcessedSet.push_back(&MBB);
1920 
1921     // See if we want to revisit the loop. If a loop has multiple back-edges,
1922     // we shouldn't revisit the same "bottom" basic block.
1923     if (ContainingLoop && isLoopBottom(ContainingLoop, &MBB) &&
1924         std::count(BlockWaitcntProcessedSet.begin(),
1925                    BlockWaitcntProcessedSet.end(), &MBB) == 1) {
1926       MachineBasicBlock *EntryBB = ContainingLoop->getHeader();
1927       BlockWaitcntBrackets *EntrySB = BlockWaitcntBracketsMap[EntryBB].get();
1928       if (EntrySB && EntrySB->getRevisitLoop()) {
1929         EntrySB->setRevisitLoop(false);
1930         J = I;
1931         int32_t PostOrder = EntrySB->getPostOrder();
1932         // TODO: Avoid this loop. Find another way to set I.
1933         for (ReversePostOrderTraversal<MachineFunction *>::rpo_iterator
1934                  X = RPOT.begin(),
1935                  Y = RPOT.end();
1936              X != Y; ++X) {
1937           MachineBasicBlock &MBBX = **X;
1938           if (MBBX.getNumber() == PostOrder) {
1939             I = X;
1940             break;
1941           }
1942         }
1943         LoopWaitcntData *WaitcntData = LoopWaitcntDataMap[ContainingLoop].get();
1944         WaitcntData->incIterCnt();
1945         LLVM_DEBUG(dbgs() << "revisit: Block" << EntryBB->getNumber() << '\n';);
1946         continue;
1947       } else {
1948         LoopWaitcntData *WaitcntData = LoopWaitcntDataMap[ContainingLoop].get();
1949         // Loop converged, reset iteration count. If this loop gets revisited,
1950         // it must be from an outer loop, the counter will restart, this will
1951         // ensure we don't force convergence on such revisits.
1952         WaitcntData->resetIterCnt();
1953       }
1954     }
1955 
1956     J = I;
1957     ++I;
1958   }
1959 
1960   SmallVector<MachineBasicBlock *, 4> EndPgmBlocks;
1961 
1962   bool HaveScalarStores = false;
1963 
1964   for (MachineFunction::iterator BI = MF.begin(), BE = MF.end(); BI != BE;
1965        ++BI) {
1966     MachineBasicBlock &MBB = *BI;
1967 
1968     for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end(); I != E;
1969          ++I) {
1970       if (!HaveScalarStores && TII->isScalarStore(*I))
1971         HaveScalarStores = true;
1972 
1973       if (I->getOpcode() == AMDGPU::S_ENDPGM ||
1974           I->getOpcode() == AMDGPU::SI_RETURN_TO_EPILOG)
1975         EndPgmBlocks.push_back(&MBB);
1976     }
1977   }
1978 
1979   if (HaveScalarStores) {
1980     // If scalar writes are used, the cache must be flushed or else the next
1981     // wave to reuse the same scratch memory can be clobbered.
1982     //
1983     // Insert s_dcache_wb at wave termination points if there were any scalar
1984     // stores, and only if the cache hasn't already been flushed. This could be
1985     // improved by looking across blocks for flushes in postdominating blocks
1986     // from the stores but an explicitly requested flush is probably very rare.
1987     for (MachineBasicBlock *MBB : EndPgmBlocks) {
1988       bool SeenDCacheWB = false;
1989 
1990       for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end(); I != E;
1991            ++I) {
1992         if (I->getOpcode() == AMDGPU::S_DCACHE_WB)
1993           SeenDCacheWB = true;
1994         else if (TII->isScalarStore(*I))
1995           SeenDCacheWB = false;
1996 
1997         // FIXME: It would be better to insert this before a waitcnt if any.
1998         if ((I->getOpcode() == AMDGPU::S_ENDPGM ||
1999              I->getOpcode() == AMDGPU::SI_RETURN_TO_EPILOG) &&
2000             !SeenDCacheWB) {
2001           Modified = true;
2002           BuildMI(*MBB, I, I->getDebugLoc(), TII->get(AMDGPU::S_DCACHE_WB));
2003         }
2004       }
2005     }
2006   }
2007 
2008   if (!MFI->isEntryFunction()) {
2009     // Wait for any outstanding memory operations that the input registers may
2010     // depend on. We can't track them and it's better to the wait after the
2011     // costly call sequence.
2012 
2013     // TODO: Could insert earlier and schedule more liberally with operations
2014     // that only use caller preserved registers.
2015     MachineBasicBlock &EntryBB = MF.front();
2016     BuildMI(EntryBB, EntryBB.getFirstNonPHI(), DebugLoc(), TII->get(AMDGPU::S_WAITCNT))
2017       .addImm(0);
2018 
2019     Modified = true;
2020   }
2021 
2022   return Modified;
2023 }
2024