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