1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2019 Valve Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21bf215546Sopenharmony_ci * IN THE SOFTWARE.
22bf215546Sopenharmony_ci *
23bf215546Sopenharmony_ci */
24bf215546Sopenharmony_ci
25bf215546Sopenharmony_ci#include "aco_builder.h"
26bf215546Sopenharmony_ci#include "aco_ir.h"
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci#include "util/u_math.h"
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci#include <set>
31bf215546Sopenharmony_ci#include <vector>
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_cinamespace aco {
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_cinamespace {
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_cienum WQMState : uint8_t {
38bf215546Sopenharmony_ci   Unspecified = 0,
39bf215546Sopenharmony_ci   Exact = 1 << 0,
40bf215546Sopenharmony_ci   WQM = 1 << 1, /* with control flow applied */
41bf215546Sopenharmony_ci};
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_cienum mask_type : uint8_t {
44bf215546Sopenharmony_ci   mask_type_global = 1 << 0,
45bf215546Sopenharmony_ci   mask_type_exact = 1 << 1,
46bf215546Sopenharmony_ci   mask_type_wqm = 1 << 2,
47bf215546Sopenharmony_ci   mask_type_loop = 1 << 3, /* active lanes of a loop */
48bf215546Sopenharmony_ci};
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_cistruct wqm_ctx {
51bf215546Sopenharmony_ci   Program* program;
52bf215546Sopenharmony_ci   /* state for WQM propagation */
53bf215546Sopenharmony_ci   std::set<unsigned> worklist;
54bf215546Sopenharmony_ci   std::vector<bool> branch_wqm; /* true if the branch condition in this block should be in wqm */
55bf215546Sopenharmony_ci   wqm_ctx(Program* program_)
56bf215546Sopenharmony_ci       : program(program_), branch_wqm(program->blocks.size())
57bf215546Sopenharmony_ci   {
58bf215546Sopenharmony_ci      for (unsigned i = 0; i < program->blocks.size(); i++)
59bf215546Sopenharmony_ci         worklist.insert(i);
60bf215546Sopenharmony_ci   }
61bf215546Sopenharmony_ci};
62bf215546Sopenharmony_ci
63bf215546Sopenharmony_cistruct loop_info {
64bf215546Sopenharmony_ci   Block* loop_header;
65bf215546Sopenharmony_ci   uint16_t num_exec_masks;
66bf215546Sopenharmony_ci   bool has_divergent_break;
67bf215546Sopenharmony_ci   bool has_divergent_continue;
68bf215546Sopenharmony_ci   bool has_discard; /* has a discard or demote */
69bf215546Sopenharmony_ci   loop_info(Block* b, uint16_t num, bool breaks, bool cont, bool discard)
70bf215546Sopenharmony_ci       : loop_header(b), num_exec_masks(num), has_divergent_break(breaks),
71bf215546Sopenharmony_ci         has_divergent_continue(cont), has_discard(discard)
72bf215546Sopenharmony_ci   {}
73bf215546Sopenharmony_ci};
74bf215546Sopenharmony_ci
75bf215546Sopenharmony_cistruct block_info {
76bf215546Sopenharmony_ci   std::vector<std::pair<Operand, uint8_t>>
77bf215546Sopenharmony_ci      exec; /* Vector of exec masks. Either a temporary or const -1. */
78bf215546Sopenharmony_ci   std::vector<WQMState> instr_needs;
79bf215546Sopenharmony_ci   uint8_t block_needs;
80bf215546Sopenharmony_ci};
81bf215546Sopenharmony_ci
82bf215546Sopenharmony_cistruct exec_ctx {
83bf215546Sopenharmony_ci   Program* program;
84bf215546Sopenharmony_ci   std::vector<block_info> info;
85bf215546Sopenharmony_ci   std::vector<loop_info> loop;
86bf215546Sopenharmony_ci   bool handle_wqm = false;
87bf215546Sopenharmony_ci   exec_ctx(Program* program_) : program(program_), info(program->blocks.size()) {}
88bf215546Sopenharmony_ci};
89bf215546Sopenharmony_ci
90bf215546Sopenharmony_cibool
91bf215546Sopenharmony_cineeds_exact(aco_ptr<Instruction>& instr)
92bf215546Sopenharmony_ci{
93bf215546Sopenharmony_ci   if (instr->isMUBUF()) {
94bf215546Sopenharmony_ci      return instr->mubuf().disable_wqm;
95bf215546Sopenharmony_ci   } else if (instr->isMTBUF()) {
96bf215546Sopenharmony_ci      return instr->mtbuf().disable_wqm;
97bf215546Sopenharmony_ci   } else if (instr->isMIMG()) {
98bf215546Sopenharmony_ci      return instr->mimg().disable_wqm;
99bf215546Sopenharmony_ci   } else if (instr->isFlatLike()) {
100bf215546Sopenharmony_ci      return instr->flatlike().disable_wqm;
101bf215546Sopenharmony_ci   } else {
102bf215546Sopenharmony_ci      /* Require Exact for p_jump_to_epilog because if p_exit_early_if is
103bf215546Sopenharmony_ci       * emitted inside the same block, the main FS will always jump to the PS
104bf215546Sopenharmony_ci       * epilog without considering the exec mask.
105bf215546Sopenharmony_ci       */
106bf215546Sopenharmony_ci      return instr->isEXP() || instr->opcode == aco_opcode::p_jump_to_epilog;
107bf215546Sopenharmony_ci   }
108bf215546Sopenharmony_ci}
109bf215546Sopenharmony_ci
110bf215546Sopenharmony_civoid
111bf215546Sopenharmony_cimark_block_wqm(wqm_ctx& ctx, unsigned block_idx)
112bf215546Sopenharmony_ci{
113bf215546Sopenharmony_ci   if (ctx.branch_wqm[block_idx])
114bf215546Sopenharmony_ci      return;
115bf215546Sopenharmony_ci
116bf215546Sopenharmony_ci   for (Block& block : ctx.program->blocks) {
117bf215546Sopenharmony_ci      if (block.index >= block_idx && block.kind & block_kind_top_level)
118bf215546Sopenharmony_ci         break;
119bf215546Sopenharmony_ci      ctx.branch_wqm[block.index] = true;
120bf215546Sopenharmony_ci      ctx.worklist.insert(block.index);
121bf215546Sopenharmony_ci   }
122bf215546Sopenharmony_ci}
123bf215546Sopenharmony_ci
124bf215546Sopenharmony_civoid
125bf215546Sopenharmony_ciget_block_needs(wqm_ctx& ctx, exec_ctx& exec_ctx, Block* block)
126bf215546Sopenharmony_ci{
127bf215546Sopenharmony_ci   block_info& info = exec_ctx.info[block->index];
128bf215546Sopenharmony_ci
129bf215546Sopenharmony_ci   std::vector<WQMState> instr_needs(block->instructions.size());
130bf215546Sopenharmony_ci
131bf215546Sopenharmony_ci   bool propagate_wqm = ctx.branch_wqm[block->index];
132bf215546Sopenharmony_ci   for (int i = block->instructions.size() - 1; i >= 0; --i) {
133bf215546Sopenharmony_ci      aco_ptr<Instruction>& instr = block->instructions[i];
134bf215546Sopenharmony_ci
135bf215546Sopenharmony_ci      if (instr->opcode == aco_opcode::p_wqm)
136bf215546Sopenharmony_ci         propagate_wqm = true;
137bf215546Sopenharmony_ci
138bf215546Sopenharmony_ci      bool pred_by_exec = needs_exec_mask(instr.get()) ||
139bf215546Sopenharmony_ci                          instr->opcode == aco_opcode::p_logical_end ||
140bf215546Sopenharmony_ci                          instr->isBranch();
141bf215546Sopenharmony_ci
142bf215546Sopenharmony_ci      if (needs_exact(instr))
143bf215546Sopenharmony_ci         instr_needs[i] = Exact;
144bf215546Sopenharmony_ci      else if (propagate_wqm && pred_by_exec)
145bf215546Sopenharmony_ci         instr_needs[i] = WQM;
146bf215546Sopenharmony_ci      else
147bf215546Sopenharmony_ci         instr_needs[i] = Unspecified;
148bf215546Sopenharmony_ci
149bf215546Sopenharmony_ci      info.block_needs |= instr_needs[i];
150bf215546Sopenharmony_ci   }
151bf215546Sopenharmony_ci
152bf215546Sopenharmony_ci   info.instr_needs = instr_needs;
153bf215546Sopenharmony_ci
154bf215546Sopenharmony_ci   /* for "if (<cond>) <wqm code>" or "while (<cond>) <wqm code>",
155bf215546Sopenharmony_ci    * <cond> should be computed in WQM */
156bf215546Sopenharmony_ci   if (info.block_needs & WQM) {
157bf215546Sopenharmony_ci      mark_block_wqm(ctx, block->index);
158bf215546Sopenharmony_ci   }
159bf215546Sopenharmony_ci}
160bf215546Sopenharmony_ci
161bf215546Sopenharmony_civoid
162bf215546Sopenharmony_cicalculate_wqm_needs(exec_ctx& exec_ctx)
163bf215546Sopenharmony_ci{
164bf215546Sopenharmony_ci   wqm_ctx ctx(exec_ctx.program);
165bf215546Sopenharmony_ci
166bf215546Sopenharmony_ci   while (!ctx.worklist.empty()) {
167bf215546Sopenharmony_ci      unsigned block_index = *std::prev(ctx.worklist.end());
168bf215546Sopenharmony_ci      ctx.worklist.erase(std::prev(ctx.worklist.end()));
169bf215546Sopenharmony_ci
170bf215546Sopenharmony_ci      Block& block = exec_ctx.program->blocks[block_index];
171bf215546Sopenharmony_ci      get_block_needs(ctx, exec_ctx, &block);
172bf215546Sopenharmony_ci   }
173bf215546Sopenharmony_ci
174bf215546Sopenharmony_ci   exec_ctx.handle_wqm = true;
175bf215546Sopenharmony_ci}
176bf215546Sopenharmony_ci
177bf215546Sopenharmony_ciOperand
178bf215546Sopenharmony_ciget_exec_op(Operand t)
179bf215546Sopenharmony_ci{
180bf215546Sopenharmony_ci   if (t.isUndefined())
181bf215546Sopenharmony_ci      return Operand(exec, t.regClass());
182bf215546Sopenharmony_ci   else
183bf215546Sopenharmony_ci      return t;
184bf215546Sopenharmony_ci}
185bf215546Sopenharmony_ci
186bf215546Sopenharmony_civoid
187bf215546Sopenharmony_citransition_to_WQM(exec_ctx& ctx, Builder bld, unsigned idx)
188bf215546Sopenharmony_ci{
189bf215546Sopenharmony_ci   if (ctx.info[idx].exec.back().second & mask_type_wqm)
190bf215546Sopenharmony_ci      return;
191bf215546Sopenharmony_ci   if (ctx.info[idx].exec.back().second & mask_type_global) {
192bf215546Sopenharmony_ci      Operand exec_mask = ctx.info[idx].exec.back().first;
193bf215546Sopenharmony_ci      if (exec_mask.isUndefined()) {
194bf215546Sopenharmony_ci         exec_mask = bld.copy(bld.def(bld.lm), Operand(exec, bld.lm));
195bf215546Sopenharmony_ci         ctx.info[idx].exec.back().first = exec_mask;
196bf215546Sopenharmony_ci      }
197bf215546Sopenharmony_ci
198bf215546Sopenharmony_ci      exec_mask = bld.sop1(Builder::s_wqm, Definition(exec, bld.lm), bld.def(s1, scc),
199bf215546Sopenharmony_ci                           get_exec_op(exec_mask));
200bf215546Sopenharmony_ci      ctx.info[idx].exec.emplace_back(exec_mask, mask_type_global | mask_type_wqm);
201bf215546Sopenharmony_ci      return;
202bf215546Sopenharmony_ci   }
203bf215546Sopenharmony_ci   /* otherwise, the WQM mask should be one below the current mask */
204bf215546Sopenharmony_ci   ctx.info[idx].exec.pop_back();
205bf215546Sopenharmony_ci   assert(ctx.info[idx].exec.back().second & mask_type_wqm);
206bf215546Sopenharmony_ci   assert(ctx.info[idx].exec.back().first.size() == bld.lm.size());
207bf215546Sopenharmony_ci   assert(ctx.info[idx].exec.back().first.isTemp());
208bf215546Sopenharmony_ci   ctx.info[idx].exec.back().first =
209bf215546Sopenharmony_ci      bld.copy(Definition(exec, bld.lm), ctx.info[idx].exec.back().first);
210bf215546Sopenharmony_ci}
211bf215546Sopenharmony_ci
212bf215546Sopenharmony_civoid
213bf215546Sopenharmony_citransition_to_Exact(exec_ctx& ctx, Builder bld, unsigned idx)
214bf215546Sopenharmony_ci{
215bf215546Sopenharmony_ci   if (ctx.info[idx].exec.back().second & mask_type_exact)
216bf215546Sopenharmony_ci      return;
217bf215546Sopenharmony_ci   /* We can't remove the loop exec mask, because that can cause exec.size() to
218bf215546Sopenharmony_ci    * be less than num_exec_masks. The loop exec mask also needs to be kept
219bf215546Sopenharmony_ci    * around for various uses. */
220bf215546Sopenharmony_ci   if ((ctx.info[idx].exec.back().second & mask_type_global) &&
221bf215546Sopenharmony_ci       !(ctx.info[idx].exec.back().second & mask_type_loop)) {
222bf215546Sopenharmony_ci      ctx.info[idx].exec.pop_back();
223bf215546Sopenharmony_ci      assert(ctx.info[idx].exec.back().second & mask_type_exact);
224bf215546Sopenharmony_ci      assert(ctx.info[idx].exec.back().first.size() == bld.lm.size());
225bf215546Sopenharmony_ci      assert(ctx.info[idx].exec.back().first.isTemp());
226bf215546Sopenharmony_ci      ctx.info[idx].exec.back().first =
227bf215546Sopenharmony_ci         bld.copy(Definition(exec, bld.lm), ctx.info[idx].exec.back().first);
228bf215546Sopenharmony_ci      return;
229bf215546Sopenharmony_ci   }
230bf215546Sopenharmony_ci   /* otherwise, we create an exact mask and push to the stack */
231bf215546Sopenharmony_ci   Operand wqm = ctx.info[idx].exec.back().first;
232bf215546Sopenharmony_ci   if (wqm.isUndefined()) {
233bf215546Sopenharmony_ci      wqm = bld.sop1(Builder::s_and_saveexec, bld.def(bld.lm), bld.def(s1, scc),
234bf215546Sopenharmony_ci                     Definition(exec, bld.lm), ctx.info[idx].exec[0].first, Operand(exec, bld.lm));
235bf215546Sopenharmony_ci   } else {
236bf215546Sopenharmony_ci      bld.sop2(Builder::s_and, Definition(exec, bld.lm), bld.def(s1, scc),
237bf215546Sopenharmony_ci               ctx.info[idx].exec[0].first, wqm);
238bf215546Sopenharmony_ci   }
239bf215546Sopenharmony_ci   ctx.info[idx].exec.back().first = Operand(wqm);
240bf215546Sopenharmony_ci   ctx.info[idx].exec.emplace_back(Operand(bld.lm), mask_type_exact);
241bf215546Sopenharmony_ci}
242bf215546Sopenharmony_ci
243bf215546Sopenharmony_ciunsigned
244bf215546Sopenharmony_ciadd_coupling_code(exec_ctx& ctx, Block* block, std::vector<aco_ptr<Instruction>>& instructions)
245bf215546Sopenharmony_ci{
246bf215546Sopenharmony_ci   unsigned idx = block->index;
247bf215546Sopenharmony_ci   Builder bld(ctx.program, &instructions);
248bf215546Sopenharmony_ci   std::vector<unsigned>& preds = block->linear_preds;
249bf215546Sopenharmony_ci
250bf215546Sopenharmony_ci   /* start block */
251bf215546Sopenharmony_ci   if (idx == 0) {
252bf215546Sopenharmony_ci      aco_ptr<Instruction>& startpgm = block->instructions[0];
253bf215546Sopenharmony_ci      assert(startpgm->opcode == aco_opcode::p_startpgm);
254bf215546Sopenharmony_ci      bld.insert(std::move(startpgm));
255bf215546Sopenharmony_ci
256bf215546Sopenharmony_ci      unsigned count = 1;
257bf215546Sopenharmony_ci      if (block->instructions[1]->opcode == aco_opcode::p_init_scratch) {
258bf215546Sopenharmony_ci         bld.insert(std::move(block->instructions[1]));
259bf215546Sopenharmony_ci         count++;
260bf215546Sopenharmony_ci      }
261bf215546Sopenharmony_ci
262bf215546Sopenharmony_ci      Operand start_exec(bld.lm);
263bf215546Sopenharmony_ci
264bf215546Sopenharmony_ci      /* exec seems to need to be manually initialized with combined shaders */
265bf215546Sopenharmony_ci      if (ctx.program->stage.num_sw_stages() > 1 || ctx.program->stage.hw == HWStage::NGG) {
266bf215546Sopenharmony_ci         start_exec = Operand::c32_or_c64(-1u, bld.lm == s2);
267bf215546Sopenharmony_ci         bld.copy(Definition(exec, bld.lm), start_exec);
268bf215546Sopenharmony_ci      }
269bf215546Sopenharmony_ci
270bf215546Sopenharmony_ci      if (ctx.handle_wqm) {
271bf215546Sopenharmony_ci         ctx.info[0].exec.emplace_back(start_exec, mask_type_global | mask_type_exact);
272bf215546Sopenharmony_ci         /* if this block needs WQM, initialize already */
273bf215546Sopenharmony_ci         if (ctx.info[0].block_needs & WQM)
274bf215546Sopenharmony_ci            transition_to_WQM(ctx, bld, 0);
275bf215546Sopenharmony_ci      } else {
276bf215546Sopenharmony_ci         uint8_t mask = mask_type_global;
277bf215546Sopenharmony_ci         if (ctx.program->needs_wqm) {
278bf215546Sopenharmony_ci            bld.sop1(Builder::s_wqm, Definition(exec, bld.lm), bld.def(s1, scc),
279bf215546Sopenharmony_ci                     Operand(exec, bld.lm));
280bf215546Sopenharmony_ci            mask |= mask_type_wqm;
281bf215546Sopenharmony_ci         } else {
282bf215546Sopenharmony_ci            mask |= mask_type_exact;
283bf215546Sopenharmony_ci         }
284bf215546Sopenharmony_ci         ctx.info[0].exec.emplace_back(start_exec, mask);
285bf215546Sopenharmony_ci      }
286bf215546Sopenharmony_ci
287bf215546Sopenharmony_ci      return count;
288bf215546Sopenharmony_ci   }
289bf215546Sopenharmony_ci
290bf215546Sopenharmony_ci   /* loop entry block */
291bf215546Sopenharmony_ci   if (block->kind & block_kind_loop_header) {
292bf215546Sopenharmony_ci      assert(preds[0] == idx - 1);
293bf215546Sopenharmony_ci      ctx.info[idx].exec = ctx.info[idx - 1].exec;
294bf215546Sopenharmony_ci      loop_info& info = ctx.loop.back();
295bf215546Sopenharmony_ci      while (ctx.info[idx].exec.size() > info.num_exec_masks)
296bf215546Sopenharmony_ci         ctx.info[idx].exec.pop_back();
297bf215546Sopenharmony_ci
298bf215546Sopenharmony_ci      /* create ssa names for outer exec masks */
299bf215546Sopenharmony_ci      if (info.has_discard) {
300bf215546Sopenharmony_ci         aco_ptr<Pseudo_instruction> phi;
301bf215546Sopenharmony_ci         for (int i = 0; i < info.num_exec_masks - 1; i++) {
302bf215546Sopenharmony_ci            phi.reset(create_instruction<Pseudo_instruction>(aco_opcode::p_linear_phi,
303bf215546Sopenharmony_ci                                                             Format::PSEUDO, preds.size(), 1));
304bf215546Sopenharmony_ci            phi->definitions[0] = bld.def(bld.lm);
305bf215546Sopenharmony_ci            phi->operands[0] = get_exec_op(ctx.info[preds[0]].exec[i].first);
306bf215546Sopenharmony_ci            ctx.info[idx].exec[i].first = bld.insert(std::move(phi));
307bf215546Sopenharmony_ci         }
308bf215546Sopenharmony_ci      }
309bf215546Sopenharmony_ci
310bf215546Sopenharmony_ci      /* create ssa name for restore mask */
311bf215546Sopenharmony_ci      if (info.has_divergent_break) {
312bf215546Sopenharmony_ci         /* this phi might be trivial but ensures a parallelcopy on the loop header */
313bf215546Sopenharmony_ci         aco_ptr<Pseudo_instruction> phi{create_instruction<Pseudo_instruction>(
314bf215546Sopenharmony_ci            aco_opcode::p_linear_phi, Format::PSEUDO, preds.size(), 1)};
315bf215546Sopenharmony_ci         phi->definitions[0] = bld.def(bld.lm);
316bf215546Sopenharmony_ci         phi->operands[0] = get_exec_op(ctx.info[preds[0]].exec[info.num_exec_masks - 1].first);
317bf215546Sopenharmony_ci         ctx.info[idx].exec.back().first = bld.insert(std::move(phi));
318bf215546Sopenharmony_ci      }
319bf215546Sopenharmony_ci
320bf215546Sopenharmony_ci      /* create ssa name for loop active mask */
321bf215546Sopenharmony_ci      aco_ptr<Pseudo_instruction> phi{create_instruction<Pseudo_instruction>(
322bf215546Sopenharmony_ci         aco_opcode::p_linear_phi, Format::PSEUDO, preds.size(), 1)};
323bf215546Sopenharmony_ci      if (info.has_divergent_continue)
324bf215546Sopenharmony_ci         phi->definitions[0] = bld.def(bld.lm);
325bf215546Sopenharmony_ci      else
326bf215546Sopenharmony_ci         phi->definitions[0] = Definition(exec, bld.lm);
327bf215546Sopenharmony_ci      phi->operands[0] = get_exec_op(ctx.info[preds[0]].exec.back().first);
328bf215546Sopenharmony_ci      Temp loop_active = bld.insert(std::move(phi));
329bf215546Sopenharmony_ci
330bf215546Sopenharmony_ci      if (info.has_divergent_break) {
331bf215546Sopenharmony_ci         uint8_t mask_type =
332bf215546Sopenharmony_ci            (ctx.info[idx].exec.back().second & (mask_type_wqm | mask_type_exact)) | mask_type_loop;
333bf215546Sopenharmony_ci         ctx.info[idx].exec.emplace_back(loop_active, mask_type);
334bf215546Sopenharmony_ci      } else {
335bf215546Sopenharmony_ci         ctx.info[idx].exec.back().first = Operand(loop_active);
336bf215546Sopenharmony_ci         ctx.info[idx].exec.back().second |= mask_type_loop;
337bf215546Sopenharmony_ci      }
338bf215546Sopenharmony_ci
339bf215546Sopenharmony_ci      /* create a parallelcopy to move the active mask to exec */
340bf215546Sopenharmony_ci      unsigned i = 0;
341bf215546Sopenharmony_ci      if (info.has_divergent_continue) {
342bf215546Sopenharmony_ci         while (block->instructions[i]->opcode != aco_opcode::p_logical_start) {
343bf215546Sopenharmony_ci            bld.insert(std::move(block->instructions[i]));
344bf215546Sopenharmony_ci            i++;
345bf215546Sopenharmony_ci         }
346bf215546Sopenharmony_ci         uint8_t mask_type = ctx.info[idx].exec.back().second & (mask_type_wqm | mask_type_exact);
347bf215546Sopenharmony_ci         assert(ctx.info[idx].exec.back().first.size() == bld.lm.size());
348bf215546Sopenharmony_ci         ctx.info[idx].exec.emplace_back(
349bf215546Sopenharmony_ci            bld.copy(Definition(exec, bld.lm), ctx.info[idx].exec.back().first), mask_type);
350bf215546Sopenharmony_ci      }
351bf215546Sopenharmony_ci
352bf215546Sopenharmony_ci      return i;
353bf215546Sopenharmony_ci   }
354bf215546Sopenharmony_ci
355bf215546Sopenharmony_ci   /* loop exit block */
356bf215546Sopenharmony_ci   if (block->kind & block_kind_loop_exit) {
357bf215546Sopenharmony_ci      Block* header = ctx.loop.back().loop_header;
358bf215546Sopenharmony_ci      loop_info& info = ctx.loop.back();
359bf215546Sopenharmony_ci
360bf215546Sopenharmony_ci      for (ASSERTED unsigned pred : preds)
361bf215546Sopenharmony_ci         assert(ctx.info[pred].exec.size() >= info.num_exec_masks);
362bf215546Sopenharmony_ci
363bf215546Sopenharmony_ci      /* fill the loop header phis */
364bf215546Sopenharmony_ci      std::vector<unsigned>& header_preds = header->linear_preds;
365bf215546Sopenharmony_ci      int instr_idx = 0;
366bf215546Sopenharmony_ci      if (info.has_discard) {
367bf215546Sopenharmony_ci         while (instr_idx < info.num_exec_masks - 1) {
368bf215546Sopenharmony_ci            aco_ptr<Instruction>& phi = header->instructions[instr_idx];
369bf215546Sopenharmony_ci            assert(phi->opcode == aco_opcode::p_linear_phi);
370bf215546Sopenharmony_ci            for (unsigned i = 1; i < phi->operands.size(); i++)
371bf215546Sopenharmony_ci               phi->operands[i] = get_exec_op(ctx.info[header_preds[i]].exec[instr_idx].first);
372bf215546Sopenharmony_ci            instr_idx++;
373bf215546Sopenharmony_ci         }
374bf215546Sopenharmony_ci      }
375bf215546Sopenharmony_ci
376bf215546Sopenharmony_ci      {
377bf215546Sopenharmony_ci         aco_ptr<Instruction>& phi = header->instructions[instr_idx++];
378bf215546Sopenharmony_ci         assert(phi->opcode == aco_opcode::p_linear_phi);
379bf215546Sopenharmony_ci         for (unsigned i = 1; i < phi->operands.size(); i++)
380bf215546Sopenharmony_ci            phi->operands[i] =
381bf215546Sopenharmony_ci               get_exec_op(ctx.info[header_preds[i]].exec[info.num_exec_masks - 1].first);
382bf215546Sopenharmony_ci      }
383bf215546Sopenharmony_ci
384bf215546Sopenharmony_ci      if (info.has_divergent_break) {
385bf215546Sopenharmony_ci         aco_ptr<Instruction>& phi = header->instructions[instr_idx];
386bf215546Sopenharmony_ci         assert(phi->opcode == aco_opcode::p_linear_phi);
387bf215546Sopenharmony_ci         for (unsigned i = 1; i < phi->operands.size(); i++)
388bf215546Sopenharmony_ci            phi->operands[i] =
389bf215546Sopenharmony_ci               get_exec_op(ctx.info[header_preds[i]].exec[info.num_exec_masks].first);
390bf215546Sopenharmony_ci      }
391bf215546Sopenharmony_ci
392bf215546Sopenharmony_ci      assert(!(block->kind & block_kind_top_level) || info.num_exec_masks <= 2);
393bf215546Sopenharmony_ci
394bf215546Sopenharmony_ci      /* create the loop exit phis if not trivial */
395bf215546Sopenharmony_ci      for (unsigned exec_idx = 0; exec_idx < info.num_exec_masks; exec_idx++) {
396bf215546Sopenharmony_ci         Operand same = ctx.info[preds[0]].exec[exec_idx].first;
397bf215546Sopenharmony_ci         uint8_t type = ctx.info[header_preds[0]].exec[exec_idx].second;
398bf215546Sopenharmony_ci         bool trivial = true;
399bf215546Sopenharmony_ci
400bf215546Sopenharmony_ci         for (unsigned i = 1; i < preds.size() && trivial; i++) {
401bf215546Sopenharmony_ci            if (ctx.info[preds[i]].exec[exec_idx].first != same)
402bf215546Sopenharmony_ci               trivial = false;
403bf215546Sopenharmony_ci         }
404bf215546Sopenharmony_ci
405bf215546Sopenharmony_ci         if (trivial) {
406bf215546Sopenharmony_ci            ctx.info[idx].exec.emplace_back(same, type);
407bf215546Sopenharmony_ci         } else {
408bf215546Sopenharmony_ci            /* create phi for loop footer */
409bf215546Sopenharmony_ci            aco_ptr<Pseudo_instruction> phi{create_instruction<Pseudo_instruction>(
410bf215546Sopenharmony_ci               aco_opcode::p_linear_phi, Format::PSEUDO, preds.size(), 1)};
411bf215546Sopenharmony_ci            phi->definitions[0] = bld.def(bld.lm);
412bf215546Sopenharmony_ci            if (exec_idx == info.num_exec_masks - 1u) {
413bf215546Sopenharmony_ci               phi->definitions[0] = Definition(exec, bld.lm);
414bf215546Sopenharmony_ci            }
415bf215546Sopenharmony_ci            for (unsigned i = 0; i < phi->operands.size(); i++)
416bf215546Sopenharmony_ci               phi->operands[i] = get_exec_op(ctx.info[preds[i]].exec[exec_idx].first);
417bf215546Sopenharmony_ci            ctx.info[idx].exec.emplace_back(bld.insert(std::move(phi)), type);
418bf215546Sopenharmony_ci         }
419bf215546Sopenharmony_ci      }
420bf215546Sopenharmony_ci
421bf215546Sopenharmony_ci      assert(ctx.info[idx].exec.size() == info.num_exec_masks);
422bf215546Sopenharmony_ci      ctx.loop.pop_back();
423bf215546Sopenharmony_ci
424bf215546Sopenharmony_ci   } else if (preds.size() == 1) {
425bf215546Sopenharmony_ci      ctx.info[idx].exec = ctx.info[preds[0]].exec;
426bf215546Sopenharmony_ci   } else {
427bf215546Sopenharmony_ci      assert(preds.size() == 2);
428bf215546Sopenharmony_ci      /* if one of the predecessors ends in exact mask, we pop it from stack */
429bf215546Sopenharmony_ci      unsigned num_exec_masks =
430bf215546Sopenharmony_ci         std::min(ctx.info[preds[0]].exec.size(), ctx.info[preds[1]].exec.size());
431bf215546Sopenharmony_ci
432bf215546Sopenharmony_ci      if (block->kind & block_kind_merge)
433bf215546Sopenharmony_ci         num_exec_masks--;
434bf215546Sopenharmony_ci      if (block->kind & block_kind_top_level)
435bf215546Sopenharmony_ci         num_exec_masks = std::min(num_exec_masks, 2u);
436bf215546Sopenharmony_ci
437bf215546Sopenharmony_ci      /* create phis for diverged exec masks */
438bf215546Sopenharmony_ci      for (unsigned i = 0; i < num_exec_masks; i++) {
439bf215546Sopenharmony_ci         /* skip trivial phis */
440bf215546Sopenharmony_ci         if (ctx.info[preds[0]].exec[i].first == ctx.info[preds[1]].exec[i].first) {
441bf215546Sopenharmony_ci            Operand t = ctx.info[preds[0]].exec[i].first;
442bf215546Sopenharmony_ci            /* discard/demote can change the state of the current exec mask */
443bf215546Sopenharmony_ci            assert(!t.isTemp() ||
444bf215546Sopenharmony_ci                   ctx.info[preds[0]].exec[i].second == ctx.info[preds[1]].exec[i].second);
445bf215546Sopenharmony_ci            uint8_t mask = ctx.info[preds[0]].exec[i].second & ctx.info[preds[1]].exec[i].second;
446bf215546Sopenharmony_ci            ctx.info[idx].exec.emplace_back(t, mask);
447bf215546Sopenharmony_ci            continue;
448bf215546Sopenharmony_ci         }
449bf215546Sopenharmony_ci
450bf215546Sopenharmony_ci         Temp phi = bld.pseudo(aco_opcode::p_linear_phi, bld.def(bld.lm),
451bf215546Sopenharmony_ci                               get_exec_op(ctx.info[preds[0]].exec[i].first),
452bf215546Sopenharmony_ci                               get_exec_op(ctx.info[preds[1]].exec[i].first));
453bf215546Sopenharmony_ci         uint8_t mask_type = ctx.info[preds[0]].exec[i].second & ctx.info[preds[1]].exec[i].second;
454bf215546Sopenharmony_ci         ctx.info[idx].exec.emplace_back(phi, mask_type);
455bf215546Sopenharmony_ci      }
456bf215546Sopenharmony_ci   }
457bf215546Sopenharmony_ci
458bf215546Sopenharmony_ci   unsigned i = 0;
459bf215546Sopenharmony_ci   while (block->instructions[i]->opcode == aco_opcode::p_phi ||
460bf215546Sopenharmony_ci          block->instructions[i]->opcode == aco_opcode::p_linear_phi) {
461bf215546Sopenharmony_ci      bld.insert(std::move(block->instructions[i]));
462bf215546Sopenharmony_ci      i++;
463bf215546Sopenharmony_ci   }
464bf215546Sopenharmony_ci
465bf215546Sopenharmony_ci   /* try to satisfy the block's needs */
466bf215546Sopenharmony_ci   if (ctx.handle_wqm) {
467bf215546Sopenharmony_ci      if (block->kind & block_kind_top_level && ctx.info[idx].exec.size() == 2) {
468bf215546Sopenharmony_ci         if (ctx.info[idx].block_needs == 0 || ctx.info[idx].block_needs == Exact) {
469bf215546Sopenharmony_ci            ctx.info[idx].exec.back().second |= mask_type_global;
470bf215546Sopenharmony_ci            transition_to_Exact(ctx, bld, idx);
471bf215546Sopenharmony_ci            ctx.handle_wqm = false;
472bf215546Sopenharmony_ci         }
473bf215546Sopenharmony_ci      }
474bf215546Sopenharmony_ci   }
475bf215546Sopenharmony_ci
476bf215546Sopenharmony_ci   /* restore exec mask after divergent control flow */
477bf215546Sopenharmony_ci   if (block->kind & (block_kind_loop_exit | block_kind_merge) &&
478bf215546Sopenharmony_ci       !ctx.info[idx].exec.back().first.isUndefined()) {
479bf215546Sopenharmony_ci      Operand restore = ctx.info[idx].exec.back().first;
480bf215546Sopenharmony_ci      assert(restore.size() == bld.lm.size());
481bf215546Sopenharmony_ci      bld.copy(Definition(exec, bld.lm), restore);
482bf215546Sopenharmony_ci      if (!restore.isConstant())
483bf215546Sopenharmony_ci         ctx.info[idx].exec.back().first = Operand(bld.lm);
484bf215546Sopenharmony_ci   }
485bf215546Sopenharmony_ci
486bf215546Sopenharmony_ci   return i;
487bf215546Sopenharmony_ci}
488bf215546Sopenharmony_ci
489bf215546Sopenharmony_ci/* Avoid live-range splits in Exact mode:
490bf215546Sopenharmony_ci * Because the data register of atomic VMEM instructions
491bf215546Sopenharmony_ci * is shared between src and dst, it might be necessary
492bf215546Sopenharmony_ci * to create live-range splits during RA.
493bf215546Sopenharmony_ci * Make the live-range splits explicit in WQM mode.
494bf215546Sopenharmony_ci */
495bf215546Sopenharmony_civoid
496bf215546Sopenharmony_cihandle_atomic_data(exec_ctx& ctx, Builder& bld, unsigned block_idx, aco_ptr<Instruction>& instr)
497bf215546Sopenharmony_ci{
498bf215546Sopenharmony_ci   /* check if this is an atomic VMEM instruction */
499bf215546Sopenharmony_ci   int idx = -1;
500bf215546Sopenharmony_ci   if (!instr->isVMEM() || instr->definitions.empty())
501bf215546Sopenharmony_ci      return;
502bf215546Sopenharmony_ci   else if (instr->isMIMG())
503bf215546Sopenharmony_ci      idx = instr->operands[2].isTemp() ? 2 : -1;
504bf215546Sopenharmony_ci   else if (instr->operands.size() == 4)
505bf215546Sopenharmony_ci      idx = 3;
506bf215546Sopenharmony_ci
507bf215546Sopenharmony_ci   if (idx != -1) {
508bf215546Sopenharmony_ci      /* insert explicit copy of atomic data in WQM-mode */
509bf215546Sopenharmony_ci      transition_to_WQM(ctx, bld, block_idx);
510bf215546Sopenharmony_ci      Temp data = instr->operands[idx].getTemp();
511bf215546Sopenharmony_ci      data = bld.copy(bld.def(data.regClass()), data);
512bf215546Sopenharmony_ci      instr->operands[idx].setTemp(data);
513bf215546Sopenharmony_ci   }
514bf215546Sopenharmony_ci}
515bf215546Sopenharmony_ci
516bf215546Sopenharmony_civoid
517bf215546Sopenharmony_ciprocess_instructions(exec_ctx& ctx, Block* block, std::vector<aco_ptr<Instruction>>& instructions,
518bf215546Sopenharmony_ci                     unsigned idx)
519bf215546Sopenharmony_ci{
520bf215546Sopenharmony_ci   WQMState state;
521bf215546Sopenharmony_ci   if (ctx.info[block->index].exec.back().second & mask_type_wqm) {
522bf215546Sopenharmony_ci      state = WQM;
523bf215546Sopenharmony_ci   } else {
524bf215546Sopenharmony_ci      assert(!ctx.handle_wqm || ctx.info[block->index].exec.back().second & mask_type_exact);
525bf215546Sopenharmony_ci      state = Exact;
526bf215546Sopenharmony_ci   }
527bf215546Sopenharmony_ci
528bf215546Sopenharmony_ci   /* if the block doesn't need both, WQM and Exact, we can skip processing the instructions */
529bf215546Sopenharmony_ci   bool process = (ctx.handle_wqm && (ctx.info[block->index].block_needs & state) !=
530bf215546Sopenharmony_ci                                        (ctx.info[block->index].block_needs & (WQM | Exact))) ||
531bf215546Sopenharmony_ci                  block->kind & block_kind_uses_discard || block->kind & block_kind_needs_lowering;
532bf215546Sopenharmony_ci   if (!process) {
533bf215546Sopenharmony_ci      std::vector<aco_ptr<Instruction>>::iterator it = std::next(block->instructions.begin(), idx);
534bf215546Sopenharmony_ci      instructions.insert(instructions.end(),
535bf215546Sopenharmony_ci                          std::move_iterator<std::vector<aco_ptr<Instruction>>::iterator>(it),
536bf215546Sopenharmony_ci                          std::move_iterator<std::vector<aco_ptr<Instruction>>::iterator>(
537bf215546Sopenharmony_ci                             block->instructions.end()));
538bf215546Sopenharmony_ci      return;
539bf215546Sopenharmony_ci   }
540bf215546Sopenharmony_ci
541bf215546Sopenharmony_ci   Builder bld(ctx.program, &instructions);
542bf215546Sopenharmony_ci
543bf215546Sopenharmony_ci   for (; idx < block->instructions.size(); idx++) {
544bf215546Sopenharmony_ci      aco_ptr<Instruction> instr = std::move(block->instructions[idx]);
545bf215546Sopenharmony_ci
546bf215546Sopenharmony_ci      WQMState needs = ctx.handle_wqm ? ctx.info[block->index].instr_needs[idx] : Unspecified;
547bf215546Sopenharmony_ci
548bf215546Sopenharmony_ci      if (needs == WQM && state != WQM) {
549bf215546Sopenharmony_ci         transition_to_WQM(ctx, bld, block->index);
550bf215546Sopenharmony_ci         state = WQM;
551bf215546Sopenharmony_ci      } else if (needs == Exact) {
552bf215546Sopenharmony_ci         if (ctx.info[block->index].block_needs & WQM)
553bf215546Sopenharmony_ci            handle_atomic_data(ctx, bld, block->index, instr);
554bf215546Sopenharmony_ci         transition_to_Exact(ctx, bld, block->index);
555bf215546Sopenharmony_ci         state = Exact;
556bf215546Sopenharmony_ci      }
557bf215546Sopenharmony_ci
558bf215546Sopenharmony_ci      if (instr->opcode == aco_opcode::p_discard_if) {
559bf215546Sopenharmony_ci         Operand current_exec = Operand(exec, bld.lm);
560bf215546Sopenharmony_ci
561bf215546Sopenharmony_ci         if (ctx.info[block->index].exec.size() >= 2) {
562bf215546Sopenharmony_ci            if (needs == WQM) {
563bf215546Sopenharmony_ci               /* Preserve the WQM mask */
564bf215546Sopenharmony_ci               ctx.info[block->index].exec[1].second &= ~mask_type_global;
565bf215546Sopenharmony_ci            } else if (block->kind & block_kind_top_level) {
566bf215546Sopenharmony_ci               /* Transition to Exact without extra instruction. Since needs != WQM, we won't need
567bf215546Sopenharmony_ci                * WQM again.
568bf215546Sopenharmony_ci                */
569bf215546Sopenharmony_ci               ctx.info[block->index].exec.resize(1);
570bf215546Sopenharmony_ci               assert(ctx.info[block->index].exec[0].second == (mask_type_exact | mask_type_global));
571bf215546Sopenharmony_ci               current_exec = get_exec_op(ctx.info[block->index].exec.back().first);
572bf215546Sopenharmony_ci               ctx.info[block->index].exec[0].first = Operand(bld.lm);
573bf215546Sopenharmony_ci            }
574bf215546Sopenharmony_ci         }
575bf215546Sopenharmony_ci
576bf215546Sopenharmony_ci         Temp cond, exit_cond;
577bf215546Sopenharmony_ci         if (instr->operands[0].isConstant()) {
578bf215546Sopenharmony_ci            assert(instr->operands[0].constantValue() == -1u);
579bf215546Sopenharmony_ci            /* save condition and set exec to zero */
580bf215546Sopenharmony_ci            exit_cond = bld.tmp(s1);
581bf215546Sopenharmony_ci            cond =
582bf215546Sopenharmony_ci               bld.sop1(Builder::s_and_saveexec, bld.def(bld.lm), bld.scc(Definition(exit_cond)),
583bf215546Sopenharmony_ci                        Definition(exec, bld.lm), Operand::zero(), Operand(exec, bld.lm));
584bf215546Sopenharmony_ci         } else {
585bf215546Sopenharmony_ci            cond = instr->operands[0].getTemp();
586bf215546Sopenharmony_ci            /* discard from current exec */
587bf215546Sopenharmony_ci            exit_cond = bld.sop2(Builder::s_andn2, Definition(exec, bld.lm), bld.def(s1, scc),
588bf215546Sopenharmony_ci                                 current_exec, cond)
589bf215546Sopenharmony_ci                           .def(1)
590bf215546Sopenharmony_ci                           .getTemp();
591bf215546Sopenharmony_ci         }
592bf215546Sopenharmony_ci
593bf215546Sopenharmony_ci         /* discard from inner to outer exec mask on stack */
594bf215546Sopenharmony_ci         int num = ctx.info[block->index].exec.size() - 2;
595bf215546Sopenharmony_ci         for (int i = num; i >= 0; i--) {
596bf215546Sopenharmony_ci            Instruction* andn2 = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc),
597bf215546Sopenharmony_ci                                          ctx.info[block->index].exec[i].first, cond);
598bf215546Sopenharmony_ci            ctx.info[block->index].exec[i].first = Operand(andn2->definitions[0].getTemp());
599bf215546Sopenharmony_ci            exit_cond = andn2->definitions[1].getTemp();
600bf215546Sopenharmony_ci         }
601bf215546Sopenharmony_ci
602bf215546Sopenharmony_ci         instr->opcode = aco_opcode::p_exit_early_if;
603bf215546Sopenharmony_ci         instr->operands[0] = bld.scc(exit_cond);
604bf215546Sopenharmony_ci         assert(!ctx.handle_wqm || (ctx.info[block->index].exec[0].second & mask_type_wqm) == 0);
605bf215546Sopenharmony_ci
606bf215546Sopenharmony_ci      } else if (instr->opcode == aco_opcode::p_is_helper) {
607bf215546Sopenharmony_ci         Definition dst = instr->definitions[0];
608bf215546Sopenharmony_ci         assert(dst.size() == bld.lm.size());
609bf215546Sopenharmony_ci         if (state == Exact) {
610bf215546Sopenharmony_ci            instr.reset(create_instruction<SOP1_instruction>(bld.w64or32(Builder::s_mov),
611bf215546Sopenharmony_ci                                                             Format::SOP1, 1, 1));
612bf215546Sopenharmony_ci            instr->operands[0] = Operand::zero();
613bf215546Sopenharmony_ci            instr->definitions[0] = dst;
614bf215546Sopenharmony_ci         } else {
615bf215546Sopenharmony_ci            std::pair<Operand, uint8_t>& exact_mask = ctx.info[block->index].exec[0];
616bf215546Sopenharmony_ci            assert(exact_mask.second & mask_type_exact);
617bf215546Sopenharmony_ci
618bf215546Sopenharmony_ci            instr.reset(create_instruction<SOP2_instruction>(bld.w64or32(Builder::s_andn2),
619bf215546Sopenharmony_ci                                                             Format::SOP2, 2, 2));
620bf215546Sopenharmony_ci            instr->operands[0] = Operand(exec, bld.lm); /* current exec */
621bf215546Sopenharmony_ci            instr->operands[1] = Operand(exact_mask.first);
622bf215546Sopenharmony_ci            instr->definitions[0] = dst;
623bf215546Sopenharmony_ci            instr->definitions[1] = bld.def(s1, scc);
624bf215546Sopenharmony_ci         }
625bf215546Sopenharmony_ci      } else if (instr->opcode == aco_opcode::p_demote_to_helper) {
626bf215546Sopenharmony_ci         /* turn demote into discard_if with only exact masks */
627bf215546Sopenharmony_ci         assert((ctx.info[block->index].exec[0].second & mask_type_exact) &&
628bf215546Sopenharmony_ci                (ctx.info[block->index].exec[0].second & mask_type_global));
629bf215546Sopenharmony_ci
630bf215546Sopenharmony_ci         int num;
631bf215546Sopenharmony_ci         Temp cond, exit_cond;
632bf215546Sopenharmony_ci         if (instr->operands[0].isConstant()) {
633bf215546Sopenharmony_ci            assert(instr->operands[0].constantValue() == -1u);
634bf215546Sopenharmony_ci            /* transition to exact and set exec to zero */
635bf215546Sopenharmony_ci            exit_cond = bld.tmp(s1);
636bf215546Sopenharmony_ci            cond =
637bf215546Sopenharmony_ci               bld.sop1(Builder::s_and_saveexec, bld.def(bld.lm), bld.scc(Definition(exit_cond)),
638bf215546Sopenharmony_ci                        Definition(exec, bld.lm), Operand::zero(), Operand(exec, bld.lm));
639bf215546Sopenharmony_ci
640bf215546Sopenharmony_ci            num = ctx.info[block->index].exec.size() - 2;
641bf215546Sopenharmony_ci            if (!(ctx.info[block->index].exec.back().second & mask_type_exact)) {
642bf215546Sopenharmony_ci               ctx.info[block->index].exec.back().first = Operand(cond);
643bf215546Sopenharmony_ci               ctx.info[block->index].exec.emplace_back(Operand(bld.lm), mask_type_exact);
644bf215546Sopenharmony_ci            }
645bf215546Sopenharmony_ci         } else {
646bf215546Sopenharmony_ci            /* demote_if: transition to exact */
647bf215546Sopenharmony_ci            if (block->kind & block_kind_top_level && ctx.info[block->index].exec.size() == 2 &&
648bf215546Sopenharmony_ci                ctx.info[block->index].exec.back().second & mask_type_global) {
649bf215546Sopenharmony_ci               /* We don't need to actually copy anything into exact, since the s_andn2
650bf215546Sopenharmony_ci                * instructions later will do that.
651bf215546Sopenharmony_ci                */
652bf215546Sopenharmony_ci               ctx.info[block->index].exec.pop_back();
653bf215546Sopenharmony_ci            } else {
654bf215546Sopenharmony_ci               transition_to_Exact(ctx, bld, block->index);
655bf215546Sopenharmony_ci            }
656bf215546Sopenharmony_ci            assert(instr->operands[0].isTemp());
657bf215546Sopenharmony_ci            cond = instr->operands[0].getTemp();
658bf215546Sopenharmony_ci            num = ctx.info[block->index].exec.size() - 1;
659bf215546Sopenharmony_ci         }
660bf215546Sopenharmony_ci
661bf215546Sopenharmony_ci         for (int i = num; i >= 0; i--) {
662bf215546Sopenharmony_ci            if (ctx.info[block->index].exec[i].second & mask_type_exact) {
663bf215546Sopenharmony_ci               Instruction* andn2 =
664bf215546Sopenharmony_ci                  bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.def(s1, scc),
665bf215546Sopenharmony_ci                           get_exec_op(ctx.info[block->index].exec[i].first), cond);
666bf215546Sopenharmony_ci               if (i == (int)ctx.info[block->index].exec.size() - 1)
667bf215546Sopenharmony_ci                  andn2->definitions[0] = Definition(exec, bld.lm);
668bf215546Sopenharmony_ci
669bf215546Sopenharmony_ci               ctx.info[block->index].exec[i].first = Operand(andn2->definitions[0].getTemp());
670bf215546Sopenharmony_ci               exit_cond = andn2->definitions[1].getTemp();
671bf215546Sopenharmony_ci            } else {
672bf215546Sopenharmony_ci               assert(i != 0);
673bf215546Sopenharmony_ci            }
674bf215546Sopenharmony_ci         }
675bf215546Sopenharmony_ci         instr->opcode = aco_opcode::p_exit_early_if;
676bf215546Sopenharmony_ci         instr->operands[0] = bld.scc(exit_cond);
677bf215546Sopenharmony_ci         state = Exact;
678bf215546Sopenharmony_ci
679bf215546Sopenharmony_ci      } else if (instr->opcode == aco_opcode::p_elect) {
680bf215546Sopenharmony_ci         bool all_lanes_enabled = ctx.info[block->index].exec.back().first.constantEquals(-1u);
681bf215546Sopenharmony_ci         Definition dst = instr->definitions[0];
682bf215546Sopenharmony_ci
683bf215546Sopenharmony_ci         if (all_lanes_enabled) {
684bf215546Sopenharmony_ci            bld.copy(Definition(dst), Operand::c32_or_c64(1u, dst.size() == 2));
685bf215546Sopenharmony_ci         } else {
686bf215546Sopenharmony_ci            Temp first_lane_idx = bld.sop1(Builder::s_ff1_i32, bld.def(s1), Operand(exec, bld.lm));
687bf215546Sopenharmony_ci            bld.sop2(Builder::s_lshl, Definition(dst), bld.def(s1, scc),
688bf215546Sopenharmony_ci                     Operand::c32_or_c64(1u, dst.size() == 2), Operand(first_lane_idx));
689bf215546Sopenharmony_ci         }
690bf215546Sopenharmony_ci         instr.reset();
691bf215546Sopenharmony_ci         continue;
692bf215546Sopenharmony_ci      }
693bf215546Sopenharmony_ci
694bf215546Sopenharmony_ci      bld.insert(std::move(instr));
695bf215546Sopenharmony_ci   }
696bf215546Sopenharmony_ci}
697bf215546Sopenharmony_ci
698bf215546Sopenharmony_civoid
699bf215546Sopenharmony_ciadd_branch_code(exec_ctx& ctx, Block* block)
700bf215546Sopenharmony_ci{
701bf215546Sopenharmony_ci   unsigned idx = block->index;
702bf215546Sopenharmony_ci   Builder bld(ctx.program, block);
703bf215546Sopenharmony_ci
704bf215546Sopenharmony_ci   if (idx == ctx.program->blocks.size() - 1)
705bf215546Sopenharmony_ci      return;
706bf215546Sopenharmony_ci
707bf215546Sopenharmony_ci   /* try to disable wqm handling */
708bf215546Sopenharmony_ci   if (ctx.handle_wqm && block->kind & block_kind_top_level) {
709bf215546Sopenharmony_ci      if (ctx.info[idx].exec.size() == 3) {
710bf215546Sopenharmony_ci         assert(ctx.info[idx].exec[1].second == mask_type_wqm);
711bf215546Sopenharmony_ci         ctx.info[idx].exec.pop_back();
712bf215546Sopenharmony_ci      }
713bf215546Sopenharmony_ci      assert(ctx.info[idx].exec.size() <= 2);
714bf215546Sopenharmony_ci
715bf215546Sopenharmony_ci      if (!(ctx.info[idx].instr_needs.back() & WQM)) {
716bf215546Sopenharmony_ci         /* transition to Exact if the branch doesn't need WQM */
717bf215546Sopenharmony_ci         aco_ptr<Instruction> branch = std::move(block->instructions.back());
718bf215546Sopenharmony_ci         block->instructions.pop_back();
719bf215546Sopenharmony_ci         ctx.info[idx].exec.back().second |= mask_type_global;
720bf215546Sopenharmony_ci         transition_to_Exact(ctx, bld, idx);
721bf215546Sopenharmony_ci         bld.insert(std::move(branch));
722bf215546Sopenharmony_ci         ctx.handle_wqm = false;
723bf215546Sopenharmony_ci      }
724bf215546Sopenharmony_ci   }
725bf215546Sopenharmony_ci
726bf215546Sopenharmony_ci   if (block->kind & block_kind_loop_preheader) {
727bf215546Sopenharmony_ci      /* collect information about the succeeding loop */
728bf215546Sopenharmony_ci      bool has_divergent_break = false;
729bf215546Sopenharmony_ci      bool has_divergent_continue = false;
730bf215546Sopenharmony_ci      bool has_discard = false;
731bf215546Sopenharmony_ci      unsigned loop_nest_depth = ctx.program->blocks[idx + 1].loop_nest_depth;
732bf215546Sopenharmony_ci
733bf215546Sopenharmony_ci      for (unsigned i = idx + 1; ctx.program->blocks[i].loop_nest_depth >= loop_nest_depth; i++) {
734bf215546Sopenharmony_ci         Block& loop_block = ctx.program->blocks[i];
735bf215546Sopenharmony_ci
736bf215546Sopenharmony_ci         if (loop_block.kind & block_kind_uses_discard)
737bf215546Sopenharmony_ci            has_discard = true;
738bf215546Sopenharmony_ci         if (loop_block.loop_nest_depth != loop_nest_depth)
739bf215546Sopenharmony_ci            continue;
740bf215546Sopenharmony_ci
741bf215546Sopenharmony_ci         if (loop_block.kind & block_kind_uniform)
742bf215546Sopenharmony_ci            continue;
743bf215546Sopenharmony_ci         else if (loop_block.kind & block_kind_break)
744bf215546Sopenharmony_ci            has_divergent_break = true;
745bf215546Sopenharmony_ci         else if (loop_block.kind & block_kind_continue)
746bf215546Sopenharmony_ci            has_divergent_continue = true;
747bf215546Sopenharmony_ci      }
748bf215546Sopenharmony_ci
749bf215546Sopenharmony_ci      unsigned num_exec_masks = ctx.info[idx].exec.size();
750bf215546Sopenharmony_ci      if (block->kind & block_kind_top_level)
751bf215546Sopenharmony_ci         num_exec_masks = std::min(num_exec_masks, 2u);
752bf215546Sopenharmony_ci
753bf215546Sopenharmony_ci      ctx.loop.emplace_back(&ctx.program->blocks[block->linear_succs[0]], num_exec_masks,
754bf215546Sopenharmony_ci                            has_divergent_break, has_divergent_continue, has_discard);
755bf215546Sopenharmony_ci   }
756bf215546Sopenharmony_ci
757bf215546Sopenharmony_ci   /* For normal breaks, this is the exec mask. For discard+break, it's the
758bf215546Sopenharmony_ci    * old exec mask before it was zero'd.
759bf215546Sopenharmony_ci    */
760bf215546Sopenharmony_ci   Operand break_cond = Operand(exec, bld.lm);
761bf215546Sopenharmony_ci
762bf215546Sopenharmony_ci   if (block->kind & block_kind_continue_or_break) {
763bf215546Sopenharmony_ci      assert(ctx.program->blocks[ctx.program->blocks[block->linear_succs[1]].linear_succs[0]].kind &
764bf215546Sopenharmony_ci             block_kind_loop_header);
765bf215546Sopenharmony_ci      assert(ctx.program->blocks[ctx.program->blocks[block->linear_succs[0]].linear_succs[0]].kind &
766bf215546Sopenharmony_ci             block_kind_loop_exit);
767bf215546Sopenharmony_ci      assert(block->instructions.back()->opcode == aco_opcode::p_branch);
768bf215546Sopenharmony_ci      block->instructions.pop_back();
769bf215546Sopenharmony_ci
770bf215546Sopenharmony_ci      bool need_parallelcopy = false;
771bf215546Sopenharmony_ci      while (!(ctx.info[idx].exec.back().second & mask_type_loop)) {
772bf215546Sopenharmony_ci         ctx.info[idx].exec.pop_back();
773bf215546Sopenharmony_ci         need_parallelcopy = true;
774bf215546Sopenharmony_ci      }
775bf215546Sopenharmony_ci
776bf215546Sopenharmony_ci      if (need_parallelcopy)
777bf215546Sopenharmony_ci         ctx.info[idx].exec.back().first =
778bf215546Sopenharmony_ci            bld.copy(Definition(exec, bld.lm), ctx.info[idx].exec.back().first);
779bf215546Sopenharmony_ci      bld.branch(aco_opcode::p_cbranch_nz, bld.def(s2), Operand(exec, bld.lm),
780bf215546Sopenharmony_ci                 block->linear_succs[1], block->linear_succs[0]);
781bf215546Sopenharmony_ci      return;
782bf215546Sopenharmony_ci   }
783bf215546Sopenharmony_ci
784bf215546Sopenharmony_ci   if (block->kind & block_kind_uniform) {
785bf215546Sopenharmony_ci      Pseudo_branch_instruction& branch = block->instructions.back()->branch();
786bf215546Sopenharmony_ci      if (branch.opcode == aco_opcode::p_branch) {
787bf215546Sopenharmony_ci         branch.target[0] = block->linear_succs[0];
788bf215546Sopenharmony_ci      } else {
789bf215546Sopenharmony_ci         branch.target[0] = block->linear_succs[1];
790bf215546Sopenharmony_ci         branch.target[1] = block->linear_succs[0];
791bf215546Sopenharmony_ci      }
792bf215546Sopenharmony_ci      return;
793bf215546Sopenharmony_ci   }
794bf215546Sopenharmony_ci
795bf215546Sopenharmony_ci   if (block->kind & block_kind_branch) {
796bf215546Sopenharmony_ci      // orig = s_and_saveexec_b64
797bf215546Sopenharmony_ci      assert(block->linear_succs.size() == 2);
798bf215546Sopenharmony_ci      assert(block->instructions.back()->opcode == aco_opcode::p_cbranch_z);
799bf215546Sopenharmony_ci      Temp cond = block->instructions.back()->operands[0].getTemp();
800bf215546Sopenharmony_ci      block->instructions.pop_back();
801bf215546Sopenharmony_ci
802bf215546Sopenharmony_ci      uint8_t mask_type = ctx.info[idx].exec.back().second & (mask_type_wqm | mask_type_exact);
803bf215546Sopenharmony_ci      if (ctx.info[idx].exec.back().first.constantEquals(-1u)) {
804bf215546Sopenharmony_ci         bld.copy(Definition(exec, bld.lm), cond);
805bf215546Sopenharmony_ci      } else {
806bf215546Sopenharmony_ci         Temp old_exec = bld.sop1(Builder::s_and_saveexec, bld.def(bld.lm), bld.def(s1, scc),
807bf215546Sopenharmony_ci                                  Definition(exec, bld.lm), cond, Operand(exec, bld.lm));
808bf215546Sopenharmony_ci
809bf215546Sopenharmony_ci         ctx.info[idx].exec.back().first = Operand(old_exec);
810bf215546Sopenharmony_ci      }
811bf215546Sopenharmony_ci
812bf215546Sopenharmony_ci      /* add next current exec to the stack */
813bf215546Sopenharmony_ci      ctx.info[idx].exec.emplace_back(Operand(bld.lm), mask_type);
814bf215546Sopenharmony_ci
815bf215546Sopenharmony_ci      bld.branch(aco_opcode::p_cbranch_z, bld.def(s2), Operand(exec, bld.lm),
816bf215546Sopenharmony_ci                 block->linear_succs[1], block->linear_succs[0]);
817bf215546Sopenharmony_ci      return;
818bf215546Sopenharmony_ci   }
819bf215546Sopenharmony_ci
820bf215546Sopenharmony_ci   if (block->kind & block_kind_invert) {
821bf215546Sopenharmony_ci      // exec = s_andn2_b64 (original_exec, exec)
822bf215546Sopenharmony_ci      assert(block->instructions.back()->opcode == aco_opcode::p_branch);
823bf215546Sopenharmony_ci      block->instructions.pop_back();
824bf215546Sopenharmony_ci      assert(ctx.info[idx].exec.size() >= 2);
825bf215546Sopenharmony_ci      Operand orig_exec = ctx.info[idx].exec[ctx.info[idx].exec.size() - 2].first;
826bf215546Sopenharmony_ci      bld.sop2(Builder::s_andn2, Definition(exec, bld.lm), bld.def(s1, scc), orig_exec,
827bf215546Sopenharmony_ci               Operand(exec, bld.lm));
828bf215546Sopenharmony_ci
829bf215546Sopenharmony_ci      bld.branch(aco_opcode::p_cbranch_z, bld.def(s2), Operand(exec, bld.lm),
830bf215546Sopenharmony_ci                 block->linear_succs[1], block->linear_succs[0]);
831bf215546Sopenharmony_ci      return;
832bf215546Sopenharmony_ci   }
833bf215546Sopenharmony_ci
834bf215546Sopenharmony_ci   if (block->kind & block_kind_break) {
835bf215546Sopenharmony_ci      // loop_mask = s_andn2_b64 (loop_mask, exec)
836bf215546Sopenharmony_ci      assert(block->instructions.back()->opcode == aco_opcode::p_branch);
837bf215546Sopenharmony_ci      block->instructions.pop_back();
838bf215546Sopenharmony_ci
839bf215546Sopenharmony_ci      Temp cond = Temp();
840bf215546Sopenharmony_ci      for (int exec_idx = ctx.info[idx].exec.size() - 2; exec_idx >= 0; exec_idx--) {
841bf215546Sopenharmony_ci         cond = bld.tmp(s1);
842bf215546Sopenharmony_ci         Operand exec_mask = ctx.info[idx].exec[exec_idx].first;
843bf215546Sopenharmony_ci         exec_mask = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.scc(Definition(cond)),
844bf215546Sopenharmony_ci                              exec_mask, break_cond);
845bf215546Sopenharmony_ci         ctx.info[idx].exec[exec_idx].first = exec_mask;
846bf215546Sopenharmony_ci         if (ctx.info[idx].exec[exec_idx].second & mask_type_loop)
847bf215546Sopenharmony_ci            break;
848bf215546Sopenharmony_ci      }
849bf215546Sopenharmony_ci
850bf215546Sopenharmony_ci      /* check if the successor is the merge block, otherwise set exec to 0 */
851bf215546Sopenharmony_ci      // TODO: this could be done better by directly branching to the merge block
852bf215546Sopenharmony_ci      unsigned succ_idx = ctx.program->blocks[block->linear_succs[1]].linear_succs[0];
853bf215546Sopenharmony_ci      Block& succ = ctx.program->blocks[succ_idx];
854bf215546Sopenharmony_ci      if (!(succ.kind & block_kind_invert || succ.kind & block_kind_merge)) {
855bf215546Sopenharmony_ci         bld.copy(Definition(exec, bld.lm), Operand::zero(bld.lm.bytes()));
856bf215546Sopenharmony_ci      }
857bf215546Sopenharmony_ci
858bf215546Sopenharmony_ci      bld.branch(aco_opcode::p_cbranch_nz, bld.def(s2), bld.scc(cond), block->linear_succs[1],
859bf215546Sopenharmony_ci                 block->linear_succs[0]);
860bf215546Sopenharmony_ci      return;
861bf215546Sopenharmony_ci   }
862bf215546Sopenharmony_ci
863bf215546Sopenharmony_ci   if (block->kind & block_kind_continue) {
864bf215546Sopenharmony_ci      assert(block->instructions.back()->opcode == aco_opcode::p_branch);
865bf215546Sopenharmony_ci      block->instructions.pop_back();
866bf215546Sopenharmony_ci
867bf215546Sopenharmony_ci      Temp cond = Temp();
868bf215546Sopenharmony_ci      for (int exec_idx = ctx.info[idx].exec.size() - 2; exec_idx >= 0; exec_idx--) {
869bf215546Sopenharmony_ci         if (ctx.info[idx].exec[exec_idx].second & mask_type_loop)
870bf215546Sopenharmony_ci            break;
871bf215546Sopenharmony_ci         cond = bld.tmp(s1);
872bf215546Sopenharmony_ci         Operand exec_mask = ctx.info[idx].exec[exec_idx].first;
873bf215546Sopenharmony_ci         exec_mask = bld.sop2(Builder::s_andn2, bld.def(bld.lm), bld.scc(Definition(cond)),
874bf215546Sopenharmony_ci                              exec_mask, Operand(exec, bld.lm));
875bf215546Sopenharmony_ci         ctx.info[idx].exec[exec_idx].first = exec_mask;
876bf215546Sopenharmony_ci      }
877bf215546Sopenharmony_ci      assert(cond != Temp());
878bf215546Sopenharmony_ci
879bf215546Sopenharmony_ci      /* check if the successor is the merge block, otherwise set exec to 0 */
880bf215546Sopenharmony_ci      // TODO: this could be done better by directly branching to the merge block
881bf215546Sopenharmony_ci      unsigned succ_idx = ctx.program->blocks[block->linear_succs[1]].linear_succs[0];
882bf215546Sopenharmony_ci      Block& succ = ctx.program->blocks[succ_idx];
883bf215546Sopenharmony_ci      if (!(succ.kind & block_kind_invert || succ.kind & block_kind_merge)) {
884bf215546Sopenharmony_ci         bld.copy(Definition(exec, bld.lm), Operand::zero(bld.lm.bytes()));
885bf215546Sopenharmony_ci      }
886bf215546Sopenharmony_ci
887bf215546Sopenharmony_ci      bld.branch(aco_opcode::p_cbranch_nz, bld.def(s2), bld.scc(cond), block->linear_succs[1],
888bf215546Sopenharmony_ci                 block->linear_succs[0]);
889bf215546Sopenharmony_ci      return;
890bf215546Sopenharmony_ci   }
891bf215546Sopenharmony_ci}
892bf215546Sopenharmony_ci
893bf215546Sopenharmony_civoid
894bf215546Sopenharmony_ciprocess_block(exec_ctx& ctx, Block* block)
895bf215546Sopenharmony_ci{
896bf215546Sopenharmony_ci   std::vector<aco_ptr<Instruction>> instructions;
897bf215546Sopenharmony_ci   instructions.reserve(block->instructions.size());
898bf215546Sopenharmony_ci
899bf215546Sopenharmony_ci   unsigned idx = add_coupling_code(ctx, block, instructions);
900bf215546Sopenharmony_ci
901bf215546Sopenharmony_ci   assert(block->index != ctx.program->blocks.size() - 1 ||
902bf215546Sopenharmony_ci          ctx.info[block->index].exec.size() <= 2);
903bf215546Sopenharmony_ci
904bf215546Sopenharmony_ci   process_instructions(ctx, block, instructions, idx);
905bf215546Sopenharmony_ci
906bf215546Sopenharmony_ci   block->instructions = std::move(instructions);
907bf215546Sopenharmony_ci
908bf215546Sopenharmony_ci   add_branch_code(ctx, block);
909bf215546Sopenharmony_ci}
910bf215546Sopenharmony_ci
911bf215546Sopenharmony_ci} /* end namespace */
912bf215546Sopenharmony_ci
913bf215546Sopenharmony_civoid
914bf215546Sopenharmony_ciinsert_exec_mask(Program* program)
915bf215546Sopenharmony_ci{
916bf215546Sopenharmony_ci   exec_ctx ctx(program);
917bf215546Sopenharmony_ci
918bf215546Sopenharmony_ci   if (program->needs_wqm && program->needs_exact)
919bf215546Sopenharmony_ci      calculate_wqm_needs(ctx);
920bf215546Sopenharmony_ci
921bf215546Sopenharmony_ci   for (Block& block : program->blocks)
922bf215546Sopenharmony_ci      process_block(ctx, &block);
923bf215546Sopenharmony_ci}
924bf215546Sopenharmony_ci
925bf215546Sopenharmony_ci} // namespace aco
926