1 //===-- SILowerControlFlow.cpp - Use predicates for control flow ----------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 /// \file
10 /// This pass lowers the pseudo control flow instructions to real
11 /// machine instructions.
12 ///
13 /// All control flow is handled using predicated instructions and
14 /// a predicate stack.  Each Scalar ALU controls the operations of 64 Vector
15 /// ALUs.  The Scalar ALU can update the predicate for any of the Vector ALUs
16 /// by writting to the 64-bit EXEC register (each bit corresponds to a
17 /// single vector ALU).  Typically, for predicates, a vector ALU will write
18 /// to its bit of the VCC register (like EXEC VCC is 64-bits, one for each
19 /// Vector ALU) and then the ScalarALU will AND the VCC register with the
20 /// EXEC to update the predicates.
21 ///
22 /// For example:
23 /// %vcc = V_CMP_GT_F32 %vgpr1, %vgpr2
24 /// %sgpr0 = SI_IF %vcc
25 ///   %vgpr0 = V_ADD_F32 %vgpr0, %vgpr0
26 /// %sgpr0 = SI_ELSE %sgpr0
27 ///   %vgpr0 = V_SUB_F32 %vgpr0, %vgpr0
28 /// SI_END_CF %sgpr0
29 ///
30 /// becomes:
31 ///
32 /// %sgpr0 = S_AND_SAVEEXEC_B64 %vcc  // Save and update the exec mask
33 /// %sgpr0 = S_XOR_B64 %sgpr0, %exec  // Clear live bits from saved exec mask
34 /// S_CBRANCH_EXECZ label0            // This instruction is an optional
35 ///                                   // optimization which allows us to
36 ///                                   // branch if all the bits of
37 ///                                   // EXEC are zero.
38 /// %vgpr0 = V_ADD_F32 %vgpr0, %vgpr0 // Do the IF block of the branch
39 ///
40 /// label0:
41 /// %sgpr0 = S_OR_SAVEEXEC_B64 %exec   // Restore the exec mask for the Then block
42 /// %exec = S_XOR_B64 %sgpr0, %exec    // Clear live bits from saved exec mask
43 /// S_BRANCH_EXECZ label1              // Use our branch optimization
44 ///                                    // instruction again.
45 /// %vgpr0 = V_SUB_F32 %vgpr0, %vgpr   // Do the THEN block
46 /// label1:
47 /// %exec = S_OR_B64 %exec, %sgpr0     // Re-enable saved exec mask bits
48 //===----------------------------------------------------------------------===//
49 
50 #include "AMDGPU.h"
51 #include "AMDGPUSubtarget.h"
52 #include "SIInstrInfo.h"
53 #include "MCTargetDesc/AMDGPUMCTargetDesc.h"
54 #include "llvm/ADT/SmallSet.h"
55 #include "llvm/ADT/SmallVector.h"
56 #include "llvm/ADT/StringRef.h"
57 #include "llvm/CodeGen/LiveIntervals.h"
58 #include "llvm/CodeGen/MachineBasicBlock.h"
59 #include "llvm/CodeGen/MachineFunction.h"
60 #include "llvm/CodeGen/MachineFunctionPass.h"
61 #include "llvm/CodeGen/MachineInstr.h"
62 #include "llvm/CodeGen/MachineInstrBuilder.h"
63 #include "llvm/CodeGen/MachineOperand.h"
64 #include "llvm/CodeGen/MachineRegisterInfo.h"
65 #include "llvm/CodeGen/Passes.h"
66 #include "llvm/CodeGen/SlotIndexes.h"
67 #include "llvm/CodeGen/TargetRegisterInfo.h"
68 #include "llvm/MC/MCRegisterInfo.h"
69 #include "llvm/Pass.h"
70 #include <cassert>
71 #include <iterator>
72 
73 using namespace llvm;
74 
75 #define DEBUG_TYPE "si-lower-control-flow"
76 
77 namespace {
78 
79 class SILowerControlFlow : public MachineFunctionPass {
80 private:
81   const SIRegisterInfo *TRI = nullptr;
82   const SIInstrInfo *TII = nullptr;
83   LiveIntervals *LIS = nullptr;
84   MachineRegisterInfo *MRI = nullptr;
85   DenseSet<const MachineInstr*> LoweredEndCf;
86 
87   const TargetRegisterClass *BoolRC = nullptr;
88   unsigned AndOpc;
89   unsigned OrOpc;
90   unsigned XorOpc;
91   unsigned MovTermOpc;
92   unsigned Andn2TermOpc;
93   unsigned XorTermrOpc;
94   unsigned OrSaveExecOpc;
95   unsigned Exec;
96 
97   void emitIf(MachineInstr &MI);
98   void emitElse(MachineInstr &MI);
99   void emitIfBreak(MachineInstr &MI);
100   void emitLoop(MachineInstr &MI);
101   void emitEndCf(MachineInstr &MI);
102 
103   void findMaskOperands(MachineInstr &MI, unsigned OpNo,
104                         SmallVectorImpl<MachineOperand> &Src) const;
105 
106   void combineMasks(MachineInstr &MI);
107 
108   // Skip to the next instruction, ignoring debug instructions, and trivial
109   // block boundaries (blocks that have one (typically fallthrough) successor,
110   // and the successor has one predecessor.
111   MachineBasicBlock::iterator
112   skipIgnoreExecInstsTrivialSucc(MachineBasicBlock &MBB,
113                                  MachineBasicBlock::iterator It) const;
114 
115 public:
116   static char ID;
117 
118   SILowerControlFlow() : MachineFunctionPass(ID) {}
119 
120   bool runOnMachineFunction(MachineFunction &MF) override;
121 
122   StringRef getPassName() const override {
123     return "SI Lower control flow pseudo instructions";
124   }
125 
126   void getAnalysisUsage(AnalysisUsage &AU) const override {
127     // Should preserve the same set that TwoAddressInstructions does.
128     AU.addPreserved<SlotIndexes>();
129     AU.addPreserved<LiveIntervals>();
130     AU.addPreservedID(LiveVariablesID);
131     AU.addPreservedID(MachineLoopInfoID);
132     AU.addPreservedID(MachineDominatorsID);
133     AU.setPreservesCFG();
134     MachineFunctionPass::getAnalysisUsage(AU);
135   }
136 };
137 
138 } // end anonymous namespace
139 
140 char SILowerControlFlow::ID = 0;
141 
142 INITIALIZE_PASS(SILowerControlFlow, DEBUG_TYPE,
143                "SI lower control flow", false, false)
144 
145 static void setImpSCCDefDead(MachineInstr &MI, bool IsDead) {
146   MachineOperand &ImpDefSCC = MI.getOperand(3);
147   assert(ImpDefSCC.getReg() == AMDGPU::SCC && ImpDefSCC.isDef());
148 
149   ImpDefSCC.setIsDead(IsDead);
150 }
151 
152 char &llvm::SILowerControlFlowID = SILowerControlFlow::ID;
153 
154 static bool isSimpleIf(const MachineInstr &MI, const MachineRegisterInfo *MRI,
155                        const SIInstrInfo *TII) {
156   Register SaveExecReg = MI.getOperand(0).getReg();
157   auto U = MRI->use_instr_nodbg_begin(SaveExecReg);
158 
159   if (U == MRI->use_instr_nodbg_end() ||
160       std::next(U) != MRI->use_instr_nodbg_end() ||
161       U->getOpcode() != AMDGPU::SI_END_CF)
162     return false;
163 
164   // Check for SI_KILL_*_TERMINATOR on path from if to endif.
165   // if there is any such terminator simplififcations are not safe.
166   auto SMBB = MI.getParent();
167   auto EMBB = U->getParent();
168   DenseSet<const MachineBasicBlock*> Visited;
169   SmallVector<MachineBasicBlock*, 4> Worklist(SMBB->succ_begin(),
170                                               SMBB->succ_end());
171 
172   while (!Worklist.empty()) {
173     MachineBasicBlock *MBB = Worklist.pop_back_val();
174 
175     if (MBB == EMBB || !Visited.insert(MBB).second)
176       continue;
177     for(auto &Term : MBB->terminators())
178       if (TII->isKillTerminator(Term.getOpcode()))
179         return false;
180 
181     Worklist.append(MBB->succ_begin(), MBB->succ_end());
182   }
183 
184   return true;
185 }
186 
187 void SILowerControlFlow::emitIf(MachineInstr &MI) {
188   MachineBasicBlock &MBB = *MI.getParent();
189   const DebugLoc &DL = MI.getDebugLoc();
190   MachineBasicBlock::iterator I(&MI);
191   Register SaveExecReg = MI.getOperand(0).getReg();
192   MachineOperand& Cond = MI.getOperand(1);
193   assert(Cond.getSubReg() == AMDGPU::NoSubRegister);
194 
195   MachineOperand &ImpDefSCC = MI.getOperand(4);
196   assert(ImpDefSCC.getReg() == AMDGPU::SCC && ImpDefSCC.isDef());
197 
198   // If there is only one use of save exec register and that use is SI_END_CF,
199   // we can optimize SI_IF by returning the full saved exec mask instead of
200   // just cleared bits.
201   bool SimpleIf = isSimpleIf(MI, MRI, TII);
202 
203   // Add an implicit def of exec to discourage scheduling VALU after this which
204   // will interfere with trying to form s_and_saveexec_b64 later.
205   Register CopyReg = SimpleIf ? SaveExecReg
206                        : MRI->createVirtualRegister(BoolRC);
207   MachineInstr *CopyExec =
208     BuildMI(MBB, I, DL, TII->get(AMDGPU::COPY), CopyReg)
209     .addReg(Exec)
210     .addReg(Exec, RegState::ImplicitDefine);
211 
212   Register Tmp = MRI->createVirtualRegister(BoolRC);
213 
214   MachineInstr *And =
215     BuildMI(MBB, I, DL, TII->get(AndOpc), Tmp)
216     .addReg(CopyReg)
217     .add(Cond);
218 
219   setImpSCCDefDead(*And, true);
220 
221   MachineInstr *Xor = nullptr;
222   if (!SimpleIf) {
223     Xor =
224       BuildMI(MBB, I, DL, TII->get(XorOpc), SaveExecReg)
225       .addReg(Tmp)
226       .addReg(CopyReg);
227     setImpSCCDefDead(*Xor, ImpDefSCC.isDead());
228   }
229 
230   // Use a copy that is a terminator to get correct spill code placement it with
231   // fast regalloc.
232   MachineInstr *SetExec =
233     BuildMI(MBB, I, DL, TII->get(MovTermOpc), Exec)
234     .addReg(Tmp, RegState::Kill);
235 
236   // Insert the S_CBRANCH_EXECZ instruction which will be optimized later
237   // during SIRemoveShortExecBranches.
238   MachineInstr *NewBr = BuildMI(MBB, I, DL, TII->get(AMDGPU::S_CBRANCH_EXECZ))
239                             .add(MI.getOperand(2));
240 
241   if (!LIS) {
242     MI.eraseFromParent();
243     return;
244   }
245 
246   LIS->InsertMachineInstrInMaps(*CopyExec);
247 
248   // Replace with and so we don't need to fix the live interval for condition
249   // register.
250   LIS->ReplaceMachineInstrInMaps(MI, *And);
251 
252   if (!SimpleIf)
253     LIS->InsertMachineInstrInMaps(*Xor);
254   LIS->InsertMachineInstrInMaps(*SetExec);
255   LIS->InsertMachineInstrInMaps(*NewBr);
256 
257   LIS->removeAllRegUnitsForPhysReg(AMDGPU::EXEC);
258   MI.eraseFromParent();
259 
260   // FIXME: Is there a better way of adjusting the liveness? It shouldn't be
261   // hard to add another def here but I'm not sure how to correctly update the
262   // valno.
263   LIS->removeInterval(SaveExecReg);
264   LIS->createAndComputeVirtRegInterval(SaveExecReg);
265   LIS->createAndComputeVirtRegInterval(Tmp);
266   if (!SimpleIf)
267     LIS->createAndComputeVirtRegInterval(CopyReg);
268 }
269 
270 void SILowerControlFlow::emitElse(MachineInstr &MI) {
271   MachineBasicBlock &MBB = *MI.getParent();
272   const DebugLoc &DL = MI.getDebugLoc();
273 
274   Register DstReg = MI.getOperand(0).getReg();
275 
276   bool ExecModified = MI.getOperand(3).getImm() != 0;
277   MachineBasicBlock::iterator Start = MBB.begin();
278 
279   // We are running before TwoAddressInstructions, and si_else's operands are
280   // tied. In order to correctly tie the registers, split this into a copy of
281   // the src like it does.
282   Register CopyReg = MRI->createVirtualRegister(BoolRC);
283   MachineInstr *CopyExec =
284     BuildMI(MBB, Start, DL, TII->get(AMDGPU::COPY), CopyReg)
285       .add(MI.getOperand(1)); // Saved EXEC
286 
287   // This must be inserted before phis and any spill code inserted before the
288   // else.
289   Register SaveReg = ExecModified ?
290     MRI->createVirtualRegister(BoolRC) : DstReg;
291   MachineInstr *OrSaveExec =
292     BuildMI(MBB, Start, DL, TII->get(OrSaveExecOpc), SaveReg)
293     .addReg(CopyReg);
294 
295   MachineBasicBlock *DestBB = MI.getOperand(2).getMBB();
296 
297   MachineBasicBlock::iterator ElsePt(MI);
298 
299   if (ExecModified) {
300     MachineInstr *And =
301       BuildMI(MBB, ElsePt, DL, TII->get(AndOpc), DstReg)
302       .addReg(Exec)
303       .addReg(SaveReg);
304 
305     if (LIS)
306       LIS->InsertMachineInstrInMaps(*And);
307   }
308 
309   MachineInstr *Xor =
310     BuildMI(MBB, ElsePt, DL, TII->get(XorTermrOpc), Exec)
311     .addReg(Exec)
312     .addReg(DstReg);
313 
314   MachineInstr *Branch =
315       BuildMI(MBB, ElsePt, DL, TII->get(AMDGPU::S_CBRANCH_EXECZ))
316           .addMBB(DestBB);
317 
318   if (!LIS) {
319     MI.eraseFromParent();
320     return;
321   }
322 
323   LIS->RemoveMachineInstrFromMaps(MI);
324   MI.eraseFromParent();
325 
326   LIS->InsertMachineInstrInMaps(*CopyExec);
327   LIS->InsertMachineInstrInMaps(*OrSaveExec);
328 
329   LIS->InsertMachineInstrInMaps(*Xor);
330   LIS->InsertMachineInstrInMaps(*Branch);
331 
332   // src reg is tied to dst reg.
333   LIS->removeInterval(DstReg);
334   LIS->createAndComputeVirtRegInterval(DstReg);
335   LIS->createAndComputeVirtRegInterval(CopyReg);
336   if (ExecModified)
337     LIS->createAndComputeVirtRegInterval(SaveReg);
338 
339   // Let this be recomputed.
340   LIS->removeAllRegUnitsForPhysReg(AMDGPU::EXEC);
341 }
342 
343 void SILowerControlFlow::emitIfBreak(MachineInstr &MI) {
344   MachineBasicBlock &MBB = *MI.getParent();
345   const DebugLoc &DL = MI.getDebugLoc();
346   auto Dst = MI.getOperand(0).getReg();
347 
348   // Skip ANDing with exec if the break condition is already masked by exec
349   // because it is a V_CMP in the same basic block. (We know the break
350   // condition operand was an i1 in IR, so if it is a VALU instruction it must
351   // be one with a carry-out.)
352   bool SkipAnding = false;
353   if (MI.getOperand(1).isReg()) {
354     if (MachineInstr *Def = MRI->getUniqueVRegDef(MI.getOperand(1).getReg())) {
355       SkipAnding = Def->getParent() == MI.getParent()
356           && SIInstrInfo::isVALU(*Def);
357     }
358   }
359 
360   // AND the break condition operand with exec, then OR that into the "loop
361   // exit" mask.
362   MachineInstr *And = nullptr, *Or = nullptr;
363   if (!SkipAnding) {
364     Register AndReg = MRI->createVirtualRegister(BoolRC);
365     And = BuildMI(MBB, &MI, DL, TII->get(AndOpc), AndReg)
366              .addReg(Exec)
367              .add(MI.getOperand(1));
368     Or = BuildMI(MBB, &MI, DL, TII->get(OrOpc), Dst)
369              .addReg(AndReg)
370              .add(MI.getOperand(2));
371     if (LIS)
372       LIS->createAndComputeVirtRegInterval(AndReg);
373   } else
374     Or = BuildMI(MBB, &MI, DL, TII->get(OrOpc), Dst)
375              .add(MI.getOperand(1))
376              .add(MI.getOperand(2));
377 
378   if (LIS) {
379     if (And)
380       LIS->InsertMachineInstrInMaps(*And);
381     LIS->ReplaceMachineInstrInMaps(MI, *Or);
382   }
383 
384   MI.eraseFromParent();
385 }
386 
387 void SILowerControlFlow::emitLoop(MachineInstr &MI) {
388   MachineBasicBlock &MBB = *MI.getParent();
389   const DebugLoc &DL = MI.getDebugLoc();
390 
391   MachineInstr *AndN2 =
392       BuildMI(MBB, &MI, DL, TII->get(Andn2TermOpc), Exec)
393           .addReg(Exec)
394           .add(MI.getOperand(0));
395 
396   MachineInstr *Branch =
397       BuildMI(MBB, &MI, DL, TII->get(AMDGPU::S_CBRANCH_EXECNZ))
398           .add(MI.getOperand(1));
399 
400   if (LIS) {
401     LIS->ReplaceMachineInstrInMaps(MI, *AndN2);
402     LIS->InsertMachineInstrInMaps(*Branch);
403   }
404 
405   MI.eraseFromParent();
406 }
407 
408 MachineBasicBlock::iterator
409 SILowerControlFlow::skipIgnoreExecInstsTrivialSucc(
410   MachineBasicBlock &MBB, MachineBasicBlock::iterator It) const {
411 
412   SmallSet<const MachineBasicBlock *, 4> Visited;
413   MachineBasicBlock *B = &MBB;
414   do {
415     if (!Visited.insert(B).second)
416       return MBB.end();
417 
418     auto E = B->end();
419     for ( ; It != E; ++It) {
420       if (TII->mayReadEXEC(*MRI, *It))
421         break;
422     }
423 
424     if (It != E)
425       return It;
426 
427     if (B->succ_size() != 1)
428       return MBB.end();
429 
430     // If there is one trivial successor, advance to the next block.
431     MachineBasicBlock *Succ = *B->succ_begin();
432 
433     It = Succ->begin();
434     B = Succ;
435   } while (true);
436 }
437 
438 void SILowerControlFlow::emitEndCf(MachineInstr &MI) {
439   MachineBasicBlock &MBB = *MI.getParent();
440   MachineRegisterInfo &MRI = MBB.getParent()->getRegInfo();
441   unsigned CFMask = MI.getOperand(0).getReg();
442   MachineInstr *Def = MRI.getUniqueVRegDef(CFMask);
443   const DebugLoc &DL = MI.getDebugLoc();
444 
445   // If the only instruction immediately following this END_CF is an another
446   // END_CF in the only successor we can avoid emitting exec mask restore here.
447   auto Next = skipIgnoreExecInstsTrivialSucc(MBB, std::next(MI.getIterator()));
448   if (Next != MBB.end() && (Next->getOpcode() == AMDGPU::SI_END_CF ||
449                             LoweredEndCf.count(&*Next))) {
450     LLVM_DEBUG(dbgs() << "Skip redundant "; MI.dump());
451     if (LIS)
452       LIS->RemoveMachineInstrFromMaps(MI);
453     MI.eraseFromParent();
454     return;
455   }
456 
457   MachineBasicBlock::iterator InsPt =
458       Def && Def->getParent() == &MBB ? std::next(MachineBasicBlock::iterator(Def))
459                                : MBB.begin();
460   MachineInstr *NewMI = BuildMI(MBB, InsPt, DL, TII->get(OrOpc), Exec)
461                             .addReg(Exec)
462                             .add(MI.getOperand(0));
463 
464   LoweredEndCf.insert(NewMI);
465 
466   if (LIS)
467     LIS->ReplaceMachineInstrInMaps(MI, *NewMI);
468 
469   MI.eraseFromParent();
470 
471   if (LIS)
472     LIS->handleMove(*NewMI);
473 }
474 
475 // Returns replace operands for a logical operation, either single result
476 // for exec or two operands if source was another equivalent operation.
477 void SILowerControlFlow::findMaskOperands(MachineInstr &MI, unsigned OpNo,
478        SmallVectorImpl<MachineOperand> &Src) const {
479   MachineOperand &Op = MI.getOperand(OpNo);
480   if (!Op.isReg() || !Register::isVirtualRegister(Op.getReg())) {
481     Src.push_back(Op);
482     return;
483   }
484 
485   MachineInstr *Def = MRI->getUniqueVRegDef(Op.getReg());
486   if (!Def || Def->getParent() != MI.getParent() ||
487       !(Def->isFullCopy() || (Def->getOpcode() == MI.getOpcode())))
488     return;
489 
490   // Make sure we do not modify exec between def and use.
491   // A copy with implcitly defined exec inserted earlier is an exclusion, it
492   // does not really modify exec.
493   for (auto I = Def->getIterator(); I != MI.getIterator(); ++I)
494     if (I->modifiesRegister(AMDGPU::EXEC, TRI) &&
495         !(I->isCopy() && I->getOperand(0).getReg() != Exec))
496       return;
497 
498   for (const auto &SrcOp : Def->explicit_operands())
499     if (SrcOp.isReg() && SrcOp.isUse() &&
500         (Register::isVirtualRegister(SrcOp.getReg()) || SrcOp.getReg() == Exec))
501       Src.push_back(SrcOp);
502 }
503 
504 // Search and combine pairs of equivalent instructions, like
505 // S_AND_B64 x, (S_AND_B64 x, y) => S_AND_B64 x, y
506 // S_OR_B64  x, (S_OR_B64  x, y) => S_OR_B64  x, y
507 // One of the operands is exec mask.
508 void SILowerControlFlow::combineMasks(MachineInstr &MI) {
509   assert(MI.getNumExplicitOperands() == 3);
510   SmallVector<MachineOperand, 4> Ops;
511   unsigned OpToReplace = 1;
512   findMaskOperands(MI, 1, Ops);
513   if (Ops.size() == 1) OpToReplace = 2; // First operand can be exec or its copy
514   findMaskOperands(MI, 2, Ops);
515   if (Ops.size() != 3) return;
516 
517   unsigned UniqueOpndIdx;
518   if (Ops[0].isIdenticalTo(Ops[1])) UniqueOpndIdx = 2;
519   else if (Ops[0].isIdenticalTo(Ops[2])) UniqueOpndIdx = 1;
520   else if (Ops[1].isIdenticalTo(Ops[2])) UniqueOpndIdx = 1;
521   else return;
522 
523   Register Reg = MI.getOperand(OpToReplace).getReg();
524   MI.RemoveOperand(OpToReplace);
525   MI.addOperand(Ops[UniqueOpndIdx]);
526   if (MRI->use_empty(Reg))
527     MRI->getUniqueVRegDef(Reg)->eraseFromParent();
528 }
529 
530 bool SILowerControlFlow::runOnMachineFunction(MachineFunction &MF) {
531   const GCNSubtarget &ST = MF.getSubtarget<GCNSubtarget>();
532   TII = ST.getInstrInfo();
533   TRI = &TII->getRegisterInfo();
534 
535   // This doesn't actually need LiveIntervals, but we can preserve them.
536   LIS = getAnalysisIfAvailable<LiveIntervals>();
537   MRI = &MF.getRegInfo();
538   BoolRC = TRI->getBoolRC();
539 
540   if (ST.isWave32()) {
541     AndOpc = AMDGPU::S_AND_B32;
542     OrOpc = AMDGPU::S_OR_B32;
543     XorOpc = AMDGPU::S_XOR_B32;
544     MovTermOpc = AMDGPU::S_MOV_B32_term;
545     Andn2TermOpc = AMDGPU::S_ANDN2_B32_term;
546     XorTermrOpc = AMDGPU::S_XOR_B32_term;
547     OrSaveExecOpc = AMDGPU::S_OR_SAVEEXEC_B32;
548     Exec = AMDGPU::EXEC_LO;
549   } else {
550     AndOpc = AMDGPU::S_AND_B64;
551     OrOpc = AMDGPU::S_OR_B64;
552     XorOpc = AMDGPU::S_XOR_B64;
553     MovTermOpc = AMDGPU::S_MOV_B64_term;
554     Andn2TermOpc = AMDGPU::S_ANDN2_B64_term;
555     XorTermrOpc = AMDGPU::S_XOR_B64_term;
556     OrSaveExecOpc = AMDGPU::S_OR_SAVEEXEC_B64;
557     Exec = AMDGPU::EXEC;
558   }
559 
560   MachineFunction::iterator NextBB;
561   for (MachineFunction::iterator BI = MF.begin(), BE = MF.end();
562        BI != BE; BI = NextBB) {
563     NextBB = std::next(BI);
564     MachineBasicBlock &MBB = *BI;
565 
566     MachineBasicBlock::iterator I, Next, Last;
567 
568     for (I = MBB.begin(), Last = MBB.end(); I != MBB.end(); I = Next) {
569       Next = std::next(I);
570       MachineInstr &MI = *I;
571 
572       switch (MI.getOpcode()) {
573       case AMDGPU::SI_IF:
574         emitIf(MI);
575         break;
576 
577       case AMDGPU::SI_ELSE:
578         emitElse(MI);
579         break;
580 
581       case AMDGPU::SI_IF_BREAK:
582         emitIfBreak(MI);
583         break;
584 
585       case AMDGPU::SI_LOOP:
586         emitLoop(MI);
587         break;
588 
589       case AMDGPU::SI_END_CF:
590         emitEndCf(MI);
591         break;
592 
593       case AMDGPU::S_AND_B64:
594       case AMDGPU::S_OR_B64:
595       case AMDGPU::S_AND_B32:
596       case AMDGPU::S_OR_B32:
597         // Cleanup bit manipulations on exec mask
598         combineMasks(MI);
599         Last = I;
600         continue;
601 
602       default:
603         Last = I;
604         continue;
605       }
606 
607       // Replay newly inserted code to combine masks
608       Next = (Last == MBB.end()) ? MBB.begin() : Last;
609     }
610   }
611 
612   LoweredEndCf.clear();
613 
614   return true;
615 }
616