xref: /llvm-project/llvm/lib/Target/AMDGPU/SILowerControlFlow.cpp (revision 701c21ea107280011c74cb0c41e9db9a3bd30cee)
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, bool ExecModified);
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 static bool opcodeEmitsNoInsts(unsigned Opc) {
129   switch (Opc) {
130   case TargetOpcode::IMPLICIT_DEF:
131   case TargetOpcode::KILL:
132   case TargetOpcode::BUNDLE:
133   case TargetOpcode::CFI_INSTRUCTION:
134   case TargetOpcode::EH_LABEL:
135   case TargetOpcode::GC_LABEL:
136   case TargetOpcode::DBG_VALUE:
137     return true;
138   default:
139     return false;
140   }
141 }
142 
143 bool SILowerControlFlow::shouldSkip(MachineBasicBlock *From,
144                                     MachineBasicBlock *To) {
145 
146   unsigned NumInstr = 0;
147   MachineFunction *MF = From->getParent();
148 
149   for (MachineFunction::iterator MBBI(From), ToI(To), End = MF->end();
150        MBBI != End && MBBI != ToI; ++MBBI) {
151     MachineBasicBlock &MBB = *MBBI;
152 
153     for (MachineBasicBlock::iterator I = MBB.begin(), E = MBB.end();
154          NumInstr < SkipThreshold && I != E; ++I) {
155       if (opcodeEmitsNoInsts(I->getOpcode()))
156         continue;
157 
158       // When a uniform loop is inside non-uniform control flow, the branch
159       // leaving the loop might be an S_CBRANCH_VCCNZ, which is never taken
160       // when EXEC = 0. We should skip the loop lest it becomes infinite.
161       if (I->getOpcode() == AMDGPU::S_CBRANCH_VCCNZ)
162         return true;
163 
164       if (++NumInstr >= SkipThreshold)
165         return true;
166     }
167   }
168 
169   return false;
170 }
171 
172 void SILowerControlFlow::Skip(MachineInstr &From, MachineOperand &To) {
173 
174   if (!shouldSkip(*From.getParent()->succ_begin(), To.getMBB()))
175     return;
176 
177   DebugLoc DL = From.getDebugLoc();
178   BuildMI(*From.getParent(), &From, DL, TII->get(AMDGPU::S_CBRANCH_EXECZ))
179     .addOperand(To);
180 }
181 
182 void SILowerControlFlow::SkipIfDead(MachineInstr &MI) {
183 
184   MachineBasicBlock &MBB = *MI.getParent();
185   DebugLoc DL = MI.getDebugLoc();
186 
187   if (MBB.getParent()->getFunction()->getCallingConv() != CallingConv::AMDGPU_PS ||
188       !shouldSkip(&MBB, &MBB.getParent()->back()))
189     return;
190 
191   MachineBasicBlock::iterator Insert = &MI;
192   ++Insert;
193 
194   // If the exec mask is non-zero, skip the next two instructions
195   BuildMI(MBB, Insert, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
196     .addImm(3);
197 
198   // Exec mask is zero: Export to NULL target...
199   BuildMI(MBB, Insert, DL, TII->get(AMDGPU::EXP))
200           .addImm(0)
201           .addImm(0x09) // V_008DFC_SQ_EXP_NULL
202           .addImm(0)
203           .addImm(1)
204           .addImm(1)
205           .addReg(AMDGPU::VGPR0)
206           .addReg(AMDGPU::VGPR0)
207           .addReg(AMDGPU::VGPR0)
208           .addReg(AMDGPU::VGPR0);
209 
210   // ... and terminate wavefront
211   BuildMI(MBB, Insert, DL, TII->get(AMDGPU::S_ENDPGM));
212 }
213 
214 void SILowerControlFlow::If(MachineInstr &MI) {
215   MachineBasicBlock &MBB = *MI.getParent();
216   DebugLoc DL = MI.getDebugLoc();
217   unsigned Reg = MI.getOperand(0).getReg();
218   unsigned Vcc = MI.getOperand(1).getReg();
219 
220   BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_AND_SAVEEXEC_B64), Reg)
221           .addReg(Vcc);
222 
223   BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_XOR_B64), Reg)
224           .addReg(AMDGPU::EXEC)
225           .addReg(Reg);
226 
227   Skip(MI, MI.getOperand(2));
228 
229   MI.eraseFromParent();
230 }
231 
232 void SILowerControlFlow::Else(MachineInstr &MI, bool ExecModified) {
233   MachineBasicBlock &MBB = *MI.getParent();
234   DebugLoc DL = MI.getDebugLoc();
235   unsigned Dst = MI.getOperand(0).getReg();
236   unsigned Src = MI.getOperand(1).getReg();
237 
238   BuildMI(MBB, MBB.getFirstNonPHI(), DL,
239           TII->get(AMDGPU::S_OR_SAVEEXEC_B64), Dst)
240           .addReg(Src); // Saved EXEC
241 
242   if (ExecModified) {
243     // Adjust the saved exec to account for the modifications during the flow
244     // block that contains the ELSE. This can happen when WQM mode is switched
245     // off.
246     BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_AND_B64), Dst)
247             .addReg(AMDGPU::EXEC)
248             .addReg(Dst);
249   }
250 
251   BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_XOR_B64), AMDGPU::EXEC)
252           .addReg(AMDGPU::EXEC)
253           .addReg(Dst);
254 
255   Skip(MI, MI.getOperand(2));
256 
257   MI.eraseFromParent();
258 }
259 
260 void SILowerControlFlow::Break(MachineInstr &MI) {
261   MachineBasicBlock &MBB = *MI.getParent();
262   DebugLoc DL = MI.getDebugLoc();
263 
264   unsigned Dst = MI.getOperand(0).getReg();
265   unsigned Src = MI.getOperand(1).getReg();
266 
267   BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_OR_B64), Dst)
268           .addReg(AMDGPU::EXEC)
269           .addReg(Src);
270 
271   MI.eraseFromParent();
272 }
273 
274 void SILowerControlFlow::IfBreak(MachineInstr &MI) {
275   MachineBasicBlock &MBB = *MI.getParent();
276   DebugLoc DL = MI.getDebugLoc();
277 
278   unsigned Dst = MI.getOperand(0).getReg();
279   unsigned Vcc = MI.getOperand(1).getReg();
280   unsigned Src = MI.getOperand(2).getReg();
281 
282   BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_OR_B64), Dst)
283           .addReg(Vcc)
284           .addReg(Src);
285 
286   MI.eraseFromParent();
287 }
288 
289 void SILowerControlFlow::ElseBreak(MachineInstr &MI) {
290   MachineBasicBlock &MBB = *MI.getParent();
291   DebugLoc DL = MI.getDebugLoc();
292 
293   unsigned Dst = MI.getOperand(0).getReg();
294   unsigned Saved = MI.getOperand(1).getReg();
295   unsigned Src = MI.getOperand(2).getReg();
296 
297   BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_OR_B64), Dst)
298           .addReg(Saved)
299           .addReg(Src);
300 
301   MI.eraseFromParent();
302 }
303 
304 void SILowerControlFlow::Loop(MachineInstr &MI) {
305   MachineBasicBlock &MBB = *MI.getParent();
306   DebugLoc DL = MI.getDebugLoc();
307   unsigned Src = MI.getOperand(0).getReg();
308 
309   BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_ANDN2_B64), AMDGPU::EXEC)
310           .addReg(AMDGPU::EXEC)
311           .addReg(Src);
312 
313   BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
314     .addOperand(MI.getOperand(1));
315 
316   MI.eraseFromParent();
317 }
318 
319 void SILowerControlFlow::EndCf(MachineInstr &MI) {
320   MachineBasicBlock &MBB = *MI.getParent();
321   DebugLoc DL = MI.getDebugLoc();
322   unsigned Reg = MI.getOperand(0).getReg();
323 
324   BuildMI(MBB, MBB.getFirstNonPHI(), DL,
325           TII->get(AMDGPU::S_OR_B64), AMDGPU::EXEC)
326           .addReg(AMDGPU::EXEC)
327           .addReg(Reg);
328 
329   MI.eraseFromParent();
330 }
331 
332 void SILowerControlFlow::Branch(MachineInstr &MI) {
333   if (MI.getOperand(0).getMBB() == MI.getParent()->getNextNode())
334     MI.eraseFromParent();
335 
336   // If these aren't equal, this is probably an infinite loop.
337 }
338 
339 void SILowerControlFlow::Kill(MachineInstr &MI) {
340   MachineBasicBlock &MBB = *MI.getParent();
341   DebugLoc DL = MI.getDebugLoc();
342   const MachineOperand &Op = MI.getOperand(0);
343 
344 #ifndef NDEBUG
345   CallingConv::ID CallConv = MBB.getParent()->getFunction()->getCallingConv();
346   // Kill is only allowed in pixel / geometry shaders.
347   assert(CallConv == CallingConv::AMDGPU_PS ||
348          CallConv == CallingConv::AMDGPU_GS);
349 #endif
350 
351   // Clear this thread from the exec mask if the operand is negative
352   if ((Op.isImm())) {
353     // Constant operand: Set exec mask to 0 or do nothing
354     if (Op.getImm() & 0x80000000) {
355       BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B64), AMDGPU::EXEC)
356               .addImm(0);
357     }
358   } else {
359     BuildMI(MBB, &MI, DL, TII->get(AMDGPU::V_CMPX_LE_F32_e32))
360            .addImm(0)
361            .addOperand(Op);
362   }
363 
364   MI.eraseFromParent();
365 }
366 
367 void SILowerControlFlow::LoadM0(MachineInstr &MI, MachineInstr *MovRel, int Offset) {
368 
369   MachineBasicBlock &MBB = *MI.getParent();
370   DebugLoc DL = MI.getDebugLoc();
371   MachineBasicBlock::iterator I = MI;
372 
373   unsigned Save = MI.getOperand(1).getReg();
374   unsigned Idx = MI.getOperand(3).getReg();
375 
376   if (AMDGPU::SReg_32RegClass.contains(Idx)) {
377     if (Offset) {
378       BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0)
379               .addReg(Idx)
380               .addImm(Offset);
381     } else {
382       BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
383               .addReg(Idx);
384     }
385     MBB.insert(I, MovRel);
386   } else {
387 
388     assert(AMDGPU::SReg_64RegClass.contains(Save));
389     assert(AMDGPU::VGPR_32RegClass.contains(Idx));
390 
391     // Save the EXEC mask
392     BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B64), Save)
393             .addReg(AMDGPU::EXEC);
394 
395     // Read the next variant into VCC (lower 32 bits) <- also loop target
396     BuildMI(MBB, &MI, DL, TII->get(AMDGPU::V_READFIRSTLANE_B32),
397             AMDGPU::VCC_LO)
398             .addReg(Idx);
399 
400     // Move index from VCC into M0
401     BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B32), AMDGPU::M0)
402             .addReg(AMDGPU::VCC_LO);
403 
404     // Compare the just read M0 value to all possible Idx values
405     BuildMI(MBB, &MI, DL, TII->get(AMDGPU::V_CMP_EQ_U32_e32))
406       .addReg(AMDGPU::M0)
407       .addReg(Idx);
408 
409     // Update EXEC, save the original EXEC value to VCC
410     BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_AND_SAVEEXEC_B64), AMDGPU::VCC)
411             .addReg(AMDGPU::VCC);
412 
413     if (Offset) {
414       BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_ADD_I32), AMDGPU::M0)
415               .addReg(AMDGPU::M0)
416               .addImm(Offset);
417     }
418     // Do the actual move
419     MBB.insert(I, MovRel);
420 
421     // Update EXEC, switch all done bits to 0 and all todo bits to 1
422     BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_XOR_B64), AMDGPU::EXEC)
423             .addReg(AMDGPU::EXEC)
424             .addReg(AMDGPU::VCC);
425 
426     // Loop back to V_READFIRSTLANE_B32 if there are still variants to cover
427     BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
428       .addImm(-7);
429 
430     // Restore EXEC
431     BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_MOV_B64), AMDGPU::EXEC)
432             .addReg(Save);
433 
434   }
435   MI.eraseFromParent();
436 }
437 
438 /// \param @VecReg The register which holds element zero of the vector
439 ///                 being addressed into.
440 /// \param[out] @Reg The base register to use in the indirect addressing instruction.
441 /// \param[in,out] @Offset As an input, this is the constant offset part of the
442 //                         indirect Index. e.g. v0 = v[VecReg + Offset]
443 //                         As an output, this is a constant value that needs
444 //                         to be added to the value stored in M0.
445 void SILowerControlFlow::computeIndirectRegAndOffset(unsigned VecReg,
446                                                      unsigned &Reg,
447                                                      int &Offset) {
448   unsigned SubReg = TRI->getSubReg(VecReg, AMDGPU::sub0);
449   if (!SubReg)
450     SubReg = VecReg;
451 
452   const TargetRegisterClass *RC = TRI->getPhysRegClass(SubReg);
453   int RegIdx = TRI->getHWRegIndex(SubReg) + Offset;
454 
455   if (RegIdx < 0) {
456     Offset = RegIdx;
457     RegIdx = 0;
458   } else {
459     Offset = 0;
460   }
461 
462   Reg = RC->getRegister(RegIdx);
463 }
464 
465 void SILowerControlFlow::IndirectSrc(MachineInstr &MI) {
466 
467   MachineBasicBlock &MBB = *MI.getParent();
468   DebugLoc DL = MI.getDebugLoc();
469 
470   unsigned Dst = MI.getOperand(0).getReg();
471   unsigned Vec = MI.getOperand(2).getReg();
472   int Off = MI.getOperand(4).getImm();
473   unsigned Reg;
474 
475   computeIndirectRegAndOffset(Vec, Reg, Off);
476 
477   MachineInstr *MovRel =
478     BuildMI(*MBB.getParent(), DL, TII->get(AMDGPU::V_MOVRELS_B32_e32), Dst)
479             .addReg(Reg)
480             .addReg(Vec, RegState::Implicit);
481 
482   LoadM0(MI, MovRel, Off);
483 }
484 
485 void SILowerControlFlow::IndirectDst(MachineInstr &MI) {
486 
487   MachineBasicBlock &MBB = *MI.getParent();
488   DebugLoc DL = MI.getDebugLoc();
489 
490   unsigned Dst = MI.getOperand(0).getReg();
491   int Off = MI.getOperand(4).getImm();
492   unsigned Val = MI.getOperand(5).getReg();
493   unsigned Reg;
494 
495   computeIndirectRegAndOffset(Dst, Reg, Off);
496 
497   MachineInstr *MovRel =
498     BuildMI(*MBB.getParent(), DL, TII->get(AMDGPU::V_MOVRELD_B32_e32))
499             .addReg(Reg, RegState::Define)
500             .addReg(Val)
501             .addReg(Dst, RegState::Implicit);
502 
503   LoadM0(MI, MovRel, Off);
504 }
505 
506 bool SILowerControlFlow::runOnMachineFunction(MachineFunction &MF) {
507   TII = static_cast<const SIInstrInfo *>(MF.getSubtarget().getInstrInfo());
508   TRI =
509       static_cast<const SIRegisterInfo *>(MF.getSubtarget().getRegisterInfo());
510   SIMachineFunctionInfo *MFI = MF.getInfo<SIMachineFunctionInfo>();
511 
512   bool HaveKill = false;
513   bool NeedFlat = false;
514   unsigned Depth = 0;
515 
516   for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
517        BI != BE; ++BI) {
518 
519     MachineBasicBlock *EmptyMBBAtEnd = NULL;
520     MachineBasicBlock &MBB = *BI;
521     MachineBasicBlock::iterator I, Next;
522     bool ExecModified = false;
523 
524     for (I = MBB.begin(); I != MBB.end(); I = Next) {
525       Next = std::next(I);
526 
527       MachineInstr &MI = *I;
528 
529       // Flat uses m0 in case it needs to access LDS.
530       if (TII->isFLAT(MI))
531         NeedFlat = true;
532 
533       for (const auto &Def : I->defs()) {
534         if (Def.isReg() && Def.isDef() && Def.getReg() == AMDGPU::EXEC) {
535           ExecModified = true;
536           break;
537         }
538       }
539 
540       switch (MI.getOpcode()) {
541         default: break;
542         case AMDGPU::SI_IF:
543           ++Depth;
544           If(MI);
545           break;
546 
547         case AMDGPU::SI_ELSE:
548           Else(MI, ExecModified);
549           break;
550 
551         case AMDGPU::SI_BREAK:
552           Break(MI);
553           break;
554 
555         case AMDGPU::SI_IF_BREAK:
556           IfBreak(MI);
557           break;
558 
559         case AMDGPU::SI_ELSE_BREAK:
560           ElseBreak(MI);
561           break;
562 
563         case AMDGPU::SI_LOOP:
564           ++Depth;
565           Loop(MI);
566           break;
567 
568         case AMDGPU::SI_END_CF:
569           if (--Depth == 0 && HaveKill) {
570             SkipIfDead(MI);
571             HaveKill = false;
572           }
573           EndCf(MI);
574           break;
575 
576         case AMDGPU::SI_KILL:
577           if (Depth == 0)
578             SkipIfDead(MI);
579           else
580             HaveKill = true;
581           Kill(MI);
582           break;
583 
584         case AMDGPU::S_BRANCH:
585           Branch(MI);
586           break;
587 
588         case AMDGPU::SI_INDIRECT_SRC_V1:
589         case AMDGPU::SI_INDIRECT_SRC_V2:
590         case AMDGPU::SI_INDIRECT_SRC_V4:
591         case AMDGPU::SI_INDIRECT_SRC_V8:
592         case AMDGPU::SI_INDIRECT_SRC_V16:
593           IndirectSrc(MI);
594           break;
595 
596         case AMDGPU::SI_INDIRECT_DST_V1:
597         case AMDGPU::SI_INDIRECT_DST_V2:
598         case AMDGPU::SI_INDIRECT_DST_V4:
599         case AMDGPU::SI_INDIRECT_DST_V8:
600         case AMDGPU::SI_INDIRECT_DST_V16:
601           IndirectDst(MI);
602           break;
603 
604         case AMDGPU::S_ENDPGM: {
605           if (MF.getInfo<SIMachineFunctionInfo>()->returnsVoid())
606             break;
607 
608           // Graphics shaders returning non-void shouldn't contain S_ENDPGM,
609           // because external bytecode will be appended at the end.
610           if (BI != --MF.end() || I != MBB.getFirstTerminator()) {
611             // S_ENDPGM is not the last instruction. Add an empty block at
612             // the end and jump there.
613             if (!EmptyMBBAtEnd) {
614               EmptyMBBAtEnd = MF.CreateMachineBasicBlock();
615               MF.insert(MF.end(), EmptyMBBAtEnd);
616             }
617 
618             MBB.addSuccessor(EmptyMBBAtEnd);
619             BuildMI(*BI, I, MI.getDebugLoc(), TII->get(AMDGPU::S_BRANCH))
620                     .addMBB(EmptyMBBAtEnd);
621           }
622 
623           I->eraseFromParent();
624           break;
625         }
626       }
627     }
628   }
629 
630   if (NeedFlat && MFI->IsKernel) {
631     // TODO: What to use with function calls?
632     // We will need to Initialize the flat scratch register pair.
633     if (NeedFlat)
634       MFI->setHasFlatInstructions(true);
635   }
636 
637   return true;
638 }
639