xref: /llvm-project/llvm/lib/Target/AMDGPU/SILowerControlFlow.cpp (revision ef160de3e5af3c8e51928fbe7b096af3d9471880)
1 //===-- 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 This pass lowers the pseudo control flow instructions to real
12 /// machine instructions.
13 ///
14 /// All control flow is handled using predicated instructions and
15 /// a predicate stack.  Each Scalar ALU controls the operations of 64 Vector
16 /// ALUs.  The Scalar ALU can update the predicate for any of the Vector ALUs
17 /// by writting to the 64-bit EXEC register (each bit corresponds to a
18 /// single vector ALU).  Typically, for predicates, a vector ALU will write
19 /// to its bit of the VCC register (like EXEC VCC is 64-bits, one for each
20 /// Vector ALU) and then the ScalarALU will AND the VCC register with the
21 /// EXEC to update the predicates.
22 ///
23 /// For example:
24 /// %VCC = V_CMP_GT_F32 %VGPR1, %VGPR2
25 /// %SGPR0 = SI_IF %VCC
26 ///   %VGPR0 = V_ADD_F32 %VGPR0, %VGPR0
27 /// %SGPR0 = SI_ELSE %SGPR0
28 ///   %VGPR0 = V_SUB_F32 %VGPR0, %VGPR0
29 /// SI_END_CF %SGPR0
30 ///
31 /// becomes:
32 ///
33 /// %SGPR0 = S_AND_SAVEEXEC_B64 %VCC  // Save and update the exec mask
34 /// %SGPR0 = S_XOR_B64 %SGPR0, %EXEC  // Clear live bits from saved exec mask
35 /// S_CBRANCH_EXECZ label0            // This instruction is an optional
36 ///                                   // optimization which allows us to
37 ///                                   // branch if all the bits of
38 ///                                   // EXEC are zero.
39 /// %VGPR0 = V_ADD_F32 %VGPR0, %VGPR0 // Do the IF block of the branch
40 ///
41 /// label0:
42 /// %SGPR0 = S_OR_SAVEEXEC_B64 %EXEC   // Restore the exec mask for the Then block
43 /// %EXEC = S_XOR_B64 %SGPR0, %EXEC    // Clear live bits from saved exec mask
44 /// S_BRANCH_EXECZ label1              // Use our branch optimization
45 ///                                    // instruction again.
46 /// %VGPR0 = V_SUB_F32 %VGPR0, %VGPR   // Do the THEN block
47 /// label1:
48 /// %EXEC = S_OR_B64 %EXEC, %SGPR0     // Re-enable saved exec mask bits
49 //===----------------------------------------------------------------------===//
50 
51 #include "AMDGPU.h"
52 #include "AMDGPUSubtarget.h"
53 #include "SIInstrInfo.h"
54 #include "SIMachineFunctionInfo.h"
55 #include "llvm/CodeGen/MachineFrameInfo.h"
56 #include "llvm/CodeGen/MachineFunction.h"
57 #include "llvm/CodeGen/MachineFunctionPass.h"
58 #include "llvm/CodeGen/MachineInstrBuilder.h"
59 #include "llvm/CodeGen/MachineRegisterInfo.h"
60 #include "llvm/IR/Constants.h"
61 
62 using namespace llvm;
63 
64 #define DEBUG_TYPE "si-lower-control-flow"
65 
66 namespace {
67 
68 class SILowerControlFlow : public MachineFunctionPass {
69 private:
70   static const unsigned SkipThreshold = 12;
71 
72   const SIRegisterInfo *TRI;
73   const SIInstrInfo *TII;
74 
75   bool shouldSkip(MachineBasicBlock *From, MachineBasicBlock *To);
76 
77   void Skip(MachineInstr &From, MachineOperand &To);
78   void SkipIfDead(MachineInstr &MI);
79 
80   void If(MachineInstr &MI);
81   void Else(MachineInstr &MI);
82   void Break(MachineInstr &MI);
83   void IfBreak(MachineInstr &MI);
84   void ElseBreak(MachineInstr &MI);
85   void Loop(MachineInstr &MI);
86   void EndCf(MachineInstr &MI);
87 
88   void Kill(MachineInstr &MI);
89   void Branch(MachineInstr &MI);
90 
91   void LoadM0(MachineInstr &MI, MachineInstr *MovRel, int Offset = 0);
92   void computeIndirectRegAndOffset(unsigned VecReg, unsigned &Reg, int &Offset);
93   void IndirectSrc(MachineInstr &MI);
94   void IndirectDst(MachineInstr &MI);
95 
96 public:
97   static char ID;
98 
99   SILowerControlFlow() :
100     MachineFunctionPass(ID), TRI(nullptr), TII(nullptr) { }
101 
102   bool runOnMachineFunction(MachineFunction &MF) override;
103 
104   const char *getPassName() const override {
105     return "SI Lower control flow pseudo instructions";
106   }
107 
108   void getAnalysisUsage(AnalysisUsage &AU) const override {
109     AU.setPreservesCFG();
110     MachineFunctionPass::getAnalysisUsage(AU);
111   }
112 };
113 
114 } // End anonymous namespace
115 
116 char SILowerControlFlow::ID = 0;
117 
118 INITIALIZE_PASS(SILowerControlFlow, DEBUG_TYPE,
119                 "SI lower control flow", false, false)
120 
121 char &llvm::SILowerControlFlowPassID = SILowerControlFlow::ID;
122 
123 
124 FunctionPass *llvm::createSILowerControlFlowPass() {
125   return new SILowerControlFlow();
126 }
127 
128 bool SILowerControlFlow::shouldSkip(MachineBasicBlock *From,
129                                     MachineBasicBlock *To) {
130 
131   unsigned NumInstr = 0;
132 
133   for (MachineBasicBlock *MBB = From; MBB != To && !MBB->succ_empty();
134        MBB = *MBB->succ_begin()) {
135 
136     for (MachineBasicBlock::iterator I = MBB->begin(), E = MBB->end();
137          NumInstr < SkipThreshold && I != E; ++I) {
138 
139       if (I->isBundle() || !I->isBundled())
140         // When a uniform loop is inside non-uniform control flow, the branch
141         // leaving the loop might be an S_CBRANCH_VCCNZ, which is never taken
142         // when EXEC = 0. We should skip the loop lest it becomes infinite.
143         if (I->getOpcode() == AMDGPU::S_CBRANCH_VCCNZ)
144           return true;
145 
146         if (++NumInstr >= SkipThreshold)
147           return true;
148     }
149   }
150 
151   return false;
152 }
153 
154 void SILowerControlFlow::Skip(MachineInstr &From, MachineOperand &To) {
155 
156   if (!shouldSkip(*From.getParent()->succ_begin(), To.getMBB()))
157     return;
158 
159   DebugLoc DL = From.getDebugLoc();
160   BuildMI(*From.getParent(), &From, DL, TII->get(AMDGPU::S_CBRANCH_EXECZ))
161     .addOperand(To);
162 }
163 
164 void SILowerControlFlow::SkipIfDead(MachineInstr &MI) {
165 
166   MachineBasicBlock &MBB = *MI.getParent();
167   DebugLoc DL = MI.getDebugLoc();
168 
169   if (MBB.getParent()->getInfo<SIMachineFunctionInfo>()->getShaderType() !=
170       ShaderType::PIXEL ||
171       !shouldSkip(&MBB, &MBB.getParent()->back()))
172     return;
173 
174   MachineBasicBlock::iterator Insert = &MI;
175   ++Insert;
176 
177   // If the exec mask is non-zero, skip the next two instructions
178   BuildMI(MBB, Insert, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
179     .addImm(3);
180 
181   // Exec mask is zero: Export to NULL target...
182   BuildMI(MBB, Insert, DL, TII->get(AMDGPU::EXP))
183           .addImm(0)
184           .addImm(0x09) // V_008DFC_SQ_EXP_NULL
185           .addImm(0)
186           .addImm(1)
187           .addImm(1)
188           .addReg(AMDGPU::VGPR0)
189           .addReg(AMDGPU::VGPR0)
190           .addReg(AMDGPU::VGPR0)
191           .addReg(AMDGPU::VGPR0);
192 
193   // ... and terminate wavefront
194   BuildMI(MBB, Insert, DL, TII->get(AMDGPU::S_ENDPGM));
195 }
196 
197 void SILowerControlFlow::If(MachineInstr &MI) {
198   MachineBasicBlock &MBB = *MI.getParent();
199   DebugLoc DL = MI.getDebugLoc();
200   unsigned Reg = MI.getOperand(0).getReg();
201   unsigned Vcc = MI.getOperand(1).getReg();
202 
203   BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_AND_SAVEEXEC_B64), Reg)
204           .addReg(Vcc);
205 
206   BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_XOR_B64), Reg)
207           .addReg(AMDGPU::EXEC)
208           .addReg(Reg);
209 
210   Skip(MI, MI.getOperand(2));
211 
212   MI.eraseFromParent();
213 }
214 
215 void SILowerControlFlow::Else(MachineInstr &MI) {
216   MachineBasicBlock &MBB = *MI.getParent();
217   DebugLoc DL = MI.getDebugLoc();
218   unsigned Dst = MI.getOperand(0).getReg();
219   unsigned Src = MI.getOperand(1).getReg();
220 
221   BuildMI(MBB, MBB.getFirstNonPHI(), DL,
222           TII->get(AMDGPU::S_OR_SAVEEXEC_B64), Dst)
223           .addReg(Src); // Saved EXEC
224 
225   BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_XOR_B64), AMDGPU::EXEC)
226           .addReg(AMDGPU::EXEC)
227           .addReg(Dst);
228 
229   Skip(MI, MI.getOperand(2));
230 
231   MI.eraseFromParent();
232 }
233 
234 void SILowerControlFlow::Break(MachineInstr &MI) {
235   MachineBasicBlock &MBB = *MI.getParent();
236   DebugLoc DL = MI.getDebugLoc();
237 
238   unsigned Dst = MI.getOperand(0).getReg();
239   unsigned Src = MI.getOperand(1).getReg();
240 
241   BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_OR_B64), Dst)
242           .addReg(AMDGPU::EXEC)
243           .addReg(Src);
244 
245   MI.eraseFromParent();
246 }
247 
248 void SILowerControlFlow::IfBreak(MachineInstr &MI) {
249   MachineBasicBlock &MBB = *MI.getParent();
250   DebugLoc DL = MI.getDebugLoc();
251 
252   unsigned Dst = MI.getOperand(0).getReg();
253   unsigned Vcc = MI.getOperand(1).getReg();
254   unsigned Src = MI.getOperand(2).getReg();
255 
256   BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_OR_B64), Dst)
257           .addReg(Vcc)
258           .addReg(Src);
259 
260   MI.eraseFromParent();
261 }
262 
263 void SILowerControlFlow::ElseBreak(MachineInstr &MI) {
264   MachineBasicBlock &MBB = *MI.getParent();
265   DebugLoc DL = MI.getDebugLoc();
266 
267   unsigned Dst = MI.getOperand(0).getReg();
268   unsigned Saved = MI.getOperand(1).getReg();
269   unsigned Src = MI.getOperand(2).getReg();
270 
271   BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_OR_B64), Dst)
272           .addReg(Saved)
273           .addReg(Src);
274 
275   MI.eraseFromParent();
276 }
277 
278 void SILowerControlFlow::Loop(MachineInstr &MI) {
279   MachineBasicBlock &MBB = *MI.getParent();
280   DebugLoc DL = MI.getDebugLoc();
281   unsigned Src = MI.getOperand(0).getReg();
282 
283   BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_ANDN2_B64), AMDGPU::EXEC)
284           .addReg(AMDGPU::EXEC)
285           .addReg(Src);
286 
287   BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
288     .addOperand(MI.getOperand(1));
289 
290   MI.eraseFromParent();
291 }
292 
293 void SILowerControlFlow::EndCf(MachineInstr &MI) {
294   MachineBasicBlock &MBB = *MI.getParent();
295   DebugLoc DL = MI.getDebugLoc();
296   unsigned Reg = MI.getOperand(0).getReg();
297 
298   BuildMI(MBB, MBB.getFirstNonPHI(), DL,
299           TII->get(AMDGPU::S_OR_B64), AMDGPU::EXEC)
300           .addReg(AMDGPU::EXEC)
301           .addReg(Reg);
302 
303   MI.eraseFromParent();
304 }
305 
306 void SILowerControlFlow::Branch(MachineInstr &MI) {
307   if (MI.getOperand(0).getMBB() == MI.getParent()->getNextNode())
308     MI.eraseFromParent();
309 
310   // If these aren't equal, this is probably an infinite loop.
311 }
312 
313 void SILowerControlFlow::Kill(MachineInstr &MI) {
314   MachineBasicBlock &MBB = *MI.getParent();
315   DebugLoc DL = MI.getDebugLoc();
316   const MachineOperand &Op = MI.getOperand(0);
317 
318 #ifndef NDEBUG
319   const SIMachineFunctionInfo *MFI
320     = MBB.getParent()->getInfo<SIMachineFunctionInfo>();
321   // Kill is only allowed in pixel / geometry shaders.
322   assert(MFI->getShaderType() == ShaderType::PIXEL ||
323          MFI->getShaderType() == ShaderType::GEOMETRY);
324 #endif
325 
326   // Clear this thread from the exec mask if the operand is negative
327   if ((Op.isImm())) {
328     // Constant operand: Set exec mask to 0 or do nothing
329     if (Op.getImm() & 0x80000000) {
330       BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B64), AMDGPU::EXEC)
331               .addImm(0);
332     }
333   } else {
334     BuildMI(MBB, &MI, DL, TII->get(AMDGPU::V_CMPX_LE_F32_e32))
335            .addImm(0)
336            .addOperand(Op);
337   }
338 
339   MI.eraseFromParent();
340 }
341 
342 void SILowerControlFlow::LoadM0(MachineInstr &MI, MachineInstr *MovRel, int Offset) {
343 
344   MachineBasicBlock &MBB = *MI.getParent();
345   DebugLoc DL = MI.getDebugLoc();
346   MachineBasicBlock::iterator I = MI;
347 
348   unsigned Save = MI.getOperand(1).getReg();
349   unsigned Idx = MI.getOperand(3).getReg();
350 
351   if (AMDGPU::SReg_32RegClass.contains(Idx)) {
352     if (Offset) {
353       BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0)
354               .addReg(Idx)
355               .addImm(Offset);
356     } else {
357       BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
358               .addReg(Idx);
359     }
360     MBB.insert(I, MovRel);
361   } else {
362 
363     assert(AMDGPU::SReg_64RegClass.contains(Save));
364     assert(AMDGPU::VGPR_32RegClass.contains(Idx));
365 
366     // Save the EXEC mask
367     BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B64), Save)
368             .addReg(AMDGPU::EXEC);
369 
370     // Read the next variant into VCC (lower 32 bits) <- also loop target
371     BuildMI(MBB, &MI, DL, TII->get(AMDGPU::V_READFIRSTLANE_B32),
372             AMDGPU::VCC_LO)
373             .addReg(Idx);
374 
375     // Move index from VCC into M0
376     BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
377             .addReg(AMDGPU::VCC_LO);
378 
379     // Compare the just read M0 value to all possible Idx values
380     BuildMI(MBB, &MI, DL, TII->get(AMDGPU::V_CMP_EQ_U32_e32))
381       .addReg(AMDGPU::M0)
382       .addReg(Idx);
383 
384     // Update EXEC, save the original EXEC value to VCC
385     BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_AND_SAVEEXEC_B64), AMDGPU::VCC)
386             .addReg(AMDGPU::VCC);
387 
388     if (Offset) {
389       BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0)
390               .addReg(AMDGPU::M0)
391               .addImm(Offset);
392     }
393     // Do the actual move
394     MBB.insert(I, MovRel);
395 
396     // Update EXEC, switch all done bits to 0 and all todo bits to 1
397     BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_XOR_B64), AMDGPU::EXEC)
398             .addReg(AMDGPU::EXEC)
399             .addReg(AMDGPU::VCC);
400 
401     // Loop back to V_READFIRSTLANE_B32 if there are still variants to cover
402     BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
403       .addImm(-7);
404 
405     // Restore EXEC
406     BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B64), AMDGPU::EXEC)
407             .addReg(Save);
408 
409   }
410   MI.eraseFromParent();
411 }
412 
413 /// \param @VecReg The register which holds element zero of the vector
414 ///                 being addressed into.
415 /// \param[out] @Reg The base register to use in the indirect addressing instruction.
416 /// \param[in,out] @Offset As an input, this is the constant offset part of the
417 //                         indirect Index. e.g. v0 = v[VecReg + Offset]
418 //                         As an output, this is a constant value that needs
419 //                         to be added to the value stored in M0.
420 void SILowerControlFlow::computeIndirectRegAndOffset(unsigned VecReg,
421                                                      unsigned &Reg,
422                                                      int &Offset) {
423   unsigned SubReg = TRI->getSubReg(VecReg, AMDGPU::sub0);
424   if (!SubReg)
425     SubReg = VecReg;
426 
427   const TargetRegisterClass *RC = TRI->getPhysRegClass(SubReg);
428   int RegIdx = TRI->getHWRegIndex(SubReg) + Offset;
429 
430   if (RegIdx < 0) {
431     Offset = RegIdx;
432     RegIdx = 0;
433   } else {
434     Offset = 0;
435   }
436 
437   Reg = RC->getRegister(RegIdx);
438 }
439 
440 void SILowerControlFlow::IndirectSrc(MachineInstr &MI) {
441 
442   MachineBasicBlock &MBB = *MI.getParent();
443   DebugLoc DL = MI.getDebugLoc();
444 
445   unsigned Dst = MI.getOperand(0).getReg();
446   unsigned Vec = MI.getOperand(2).getReg();
447   int Off = MI.getOperand(4).getImm();
448   unsigned Reg;
449 
450   computeIndirectRegAndOffset(Vec, Reg, Off);
451 
452   MachineInstr *MovRel =
453     BuildMI(*MBB.getParent(), DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst)
454             .addReg(Reg)
455             .addReg(Vec, RegState::Implicit);
456 
457   LoadM0(MI, MovRel, Off);
458 }
459 
460 void SILowerControlFlow::IndirectDst(MachineInstr &MI) {
461 
462   MachineBasicBlock &MBB = *MI.getParent();
463   DebugLoc DL = MI.getDebugLoc();
464 
465   unsigned Dst = MI.getOperand(0).getReg();
466   int Off = MI.getOperand(4).getImm();
467   unsigned Val = MI.getOperand(5).getReg();
468   unsigned Reg;
469 
470   computeIndirectRegAndOffset(Dst, Reg, Off);
471 
472   MachineInstr *MovRel =
473     BuildMI(*MBB.getParent(), DL, TII->get(AMDGPU::V_MOVRELD_B32_e32))
474             .addReg(Reg, RegState::Define)
475             .addReg(Val)
476             .addReg(Dst, RegState::Implicit);
477 
478   LoadM0(MI, MovRel, Off);
479 }
480 
481 bool SILowerControlFlow::runOnMachineFunction(MachineFunction &MF) {
482   TII = static_cast<const SIInstrInfo *>(MF.getSubtarget().getInstrInfo());
483   TRI =
484       static_cast<const SIRegisterInfo *>(MF.getSubtarget().getRegisterInfo());
485   SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
486 
487   bool HaveKill = false;
488   bool NeedWQM = false;
489   bool NeedFlat = false;
490   unsigned Depth = 0;
491 
492   for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
493        BI != BE; ++BI) {
494 
495     MachineBasicBlock *EmptyMBBAtEnd = NULL;
496     MachineBasicBlock &MBB = *BI;
497     MachineBasicBlock::iterator I, Next;
498     for (I = MBB.begin(); I != MBB.end(); I = Next) {
499       Next = std::next(I);
500 
501       MachineInstr &MI = *I;
502       if (TII->isWQM(MI) || TII->isDS(MI))
503         NeedWQM = true;
504 
505       // Flat uses m0 in case it needs to access LDS.
506       if (TII->isFLAT(MI))
507         NeedFlat = true;
508 
509       switch (MI.getOpcode()) {
510         default: break;
511         case AMDGPU::SI_IF:
512           ++Depth;
513           If(MI);
514           break;
515 
516         case AMDGPU::SI_ELSE:
517           Else(MI);
518           break;
519 
520         case AMDGPU::SI_BREAK:
521           Break(MI);
522           break;
523 
524         case AMDGPU::SI_IF_BREAK:
525           IfBreak(MI);
526           break;
527 
528         case AMDGPU::SI_ELSE_BREAK:
529           ElseBreak(MI);
530           break;
531 
532         case AMDGPU::SI_LOOP:
533           ++Depth;
534           Loop(MI);
535           break;
536 
537         case AMDGPU::SI_END_CF:
538           if (--Depth == 0 && HaveKill) {
539             SkipIfDead(MI);
540             HaveKill = false;
541           }
542           EndCf(MI);
543           break;
544 
545         case AMDGPU::SI_KILL:
546           if (Depth == 0)
547             SkipIfDead(MI);
548           else
549             HaveKill = true;
550           Kill(MI);
551           break;
552 
553         case AMDGPU::S_BRANCH:
554           Branch(MI);
555           break;
556 
557         case AMDGPU::SI_INDIRECT_SRC_V1:
558         case AMDGPU::SI_INDIRECT_SRC_V2:
559         case AMDGPU::SI_INDIRECT_SRC_V4:
560         case AMDGPU::SI_INDIRECT_SRC_V8:
561         case AMDGPU::SI_INDIRECT_SRC_V16:
562           IndirectSrc(MI);
563           break;
564 
565         case AMDGPU::SI_INDIRECT_DST_V1:
566         case AMDGPU::SI_INDIRECT_DST_V2:
567         case AMDGPU::SI_INDIRECT_DST_V4:
568         case AMDGPU::SI_INDIRECT_DST_V8:
569         case AMDGPU::SI_INDIRECT_DST_V16:
570           IndirectDst(MI);
571           break;
572 
573         case AMDGPU::S_ENDPGM: {
574           if (MF.getInfo<SIMachineFunctionInfo>()->returnsVoid())
575             break;
576 
577           // Graphics shaders returning non-void shouldn't contain S_ENDPGM,
578           // because external bytecode will be appended at the end.
579           if (BI != --MF.end() || I != MBB.getFirstTerminator()) {
580             // S_ENDPGM is not the last instruction. Add an empty block at
581             // the end and jump there.
582             if (!EmptyMBBAtEnd) {
583               EmptyMBBAtEnd = MF.CreateMachineBasicBlock();
584               MF.insert(MF.end(), EmptyMBBAtEnd);
585             }
586 
587             MBB.addSuccessor(EmptyMBBAtEnd);
588             BuildMI(*BI, I, MI.getDebugLoc(), TII->get(AMDGPU::S_BRANCH))
589                     .addMBB(EmptyMBBAtEnd);
590           }
591 
592           I->eraseFromParent();
593           break;
594         }
595       }
596     }
597   }
598 
599   if (NeedWQM && MFI->getShaderType() == ShaderType::PIXEL) {
600     MachineBasicBlock &MBB = MF.front();
601     BuildMI(MBB, MBB.getFirstNonPHI(), DebugLoc(), TII->get(AMDGPU::S_WQM_B64),
602             AMDGPU::EXEC).addReg(AMDGPU::EXEC);
603   }
604 
605   if (NeedFlat && MFI->IsKernel) {
606     // TODO: What to use with function calls?
607     // We will need to Initialize the flat scratch register pair.
608     if (NeedFlat)
609       MFI->setHasFlatInstructions(true);
610   }
611 
612   return true;
613 }
614