1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
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 FROM,
20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21bf215546Sopenharmony_ci * SOFTWARE.
22bf215546Sopenharmony_ci *
23bf215546Sopenharmony_ci * Authors:
24bf215546Sopenharmony_ci *    Rob Clark <robclark@freedesktop.org>
25bf215546Sopenharmony_ci */
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include "util/ralloc.h"
28bf215546Sopenharmony_ci#include "util/u_math.h"
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci#include "ir3.h"
31bf215546Sopenharmony_ci#include "ir3_shader.h"
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_ci/*
34bf215546Sopenharmony_ci * Legalize:
35bf215546Sopenharmony_ci *
36bf215546Sopenharmony_ci * The legalize pass handles ensuring sufficient nop's and sync flags for
37bf215546Sopenharmony_ci * correct execution.
38bf215546Sopenharmony_ci *
39bf215546Sopenharmony_ci * 1) Iteratively determine where sync ((sy)/(ss)) flags are needed,
40bf215546Sopenharmony_ci *    based on state flowing out of predecessor blocks until there is
41bf215546Sopenharmony_ci *    no further change.  In some cases this requires inserting nops.
42bf215546Sopenharmony_ci * 2) Mark (ei) on last varying input
43bf215546Sopenharmony_ci * 3) Final nop scheduling for instruction latency
44bf215546Sopenharmony_ci * 4) Resolve jumps and schedule blocks, marking potential convergence
45bf215546Sopenharmony_ci *    points with (jp)
46bf215546Sopenharmony_ci */
47bf215546Sopenharmony_ci
48bf215546Sopenharmony_cistruct ir3_legalize_ctx {
49bf215546Sopenharmony_ci   struct ir3_compiler *compiler;
50bf215546Sopenharmony_ci   struct ir3_shader_variant *so;
51bf215546Sopenharmony_ci   gl_shader_stage type;
52bf215546Sopenharmony_ci   int max_bary;
53bf215546Sopenharmony_ci   bool early_input_release;
54bf215546Sopenharmony_ci   bool has_inputs;
55bf215546Sopenharmony_ci};
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_cistruct ir3_legalize_state {
58bf215546Sopenharmony_ci   regmask_t needs_ss;
59bf215546Sopenharmony_ci   regmask_t needs_ss_war; /* write after read */
60bf215546Sopenharmony_ci   regmask_t needs_sy;
61bf215546Sopenharmony_ci};
62bf215546Sopenharmony_ci
63bf215546Sopenharmony_cistruct ir3_legalize_block_data {
64bf215546Sopenharmony_ci   bool valid;
65bf215546Sopenharmony_ci   struct ir3_legalize_state state;
66bf215546Sopenharmony_ci};
67bf215546Sopenharmony_ci
68bf215546Sopenharmony_ci/* We want to evaluate each block from the position of any other
69bf215546Sopenharmony_ci * predecessor block, in order that the flags set are the union of
70bf215546Sopenharmony_ci * all possible program paths.
71bf215546Sopenharmony_ci *
72bf215546Sopenharmony_ci * To do this, we need to know the output state (needs_ss/ss_war/sy)
73bf215546Sopenharmony_ci * of all predecessor blocks.  The tricky thing is loops, which mean
74bf215546Sopenharmony_ci * that we can't simply recursively process each predecessor block
75bf215546Sopenharmony_ci * before legalizing the current block.
76bf215546Sopenharmony_ci *
77bf215546Sopenharmony_ci * How we handle that is by looping over all the blocks until the
78bf215546Sopenharmony_ci * results converge.  If the output state of a given block changes
79bf215546Sopenharmony_ci * in a given pass, this means that all successor blocks are not
80bf215546Sopenharmony_ci * yet fully legalized.
81bf215546Sopenharmony_ci */
82bf215546Sopenharmony_ci
83bf215546Sopenharmony_cistatic bool
84bf215546Sopenharmony_cilegalize_block(struct ir3_legalize_ctx *ctx, struct ir3_block *block)
85bf215546Sopenharmony_ci{
86bf215546Sopenharmony_ci   struct ir3_legalize_block_data *bd = block->data;
87bf215546Sopenharmony_ci
88bf215546Sopenharmony_ci   if (bd->valid)
89bf215546Sopenharmony_ci      return false;
90bf215546Sopenharmony_ci
91bf215546Sopenharmony_ci   struct ir3_instruction *last_n = NULL;
92bf215546Sopenharmony_ci   struct list_head instr_list;
93bf215546Sopenharmony_ci   struct ir3_legalize_state prev_state = bd->state;
94bf215546Sopenharmony_ci   struct ir3_legalize_state *state = &bd->state;
95bf215546Sopenharmony_ci   bool last_input_needs_ss = false;
96bf215546Sopenharmony_ci   bool has_tex_prefetch = false;
97bf215546Sopenharmony_ci   bool mergedregs = ctx->so->mergedregs;
98bf215546Sopenharmony_ci
99bf215546Sopenharmony_ci   /* our input state is the OR of all predecessor blocks' state: */
100bf215546Sopenharmony_ci   for (unsigned i = 0; i < block->predecessors_count; i++) {
101bf215546Sopenharmony_ci      struct ir3_block *predecessor = block->predecessors[i];
102bf215546Sopenharmony_ci      struct ir3_legalize_block_data *pbd = predecessor->data;
103bf215546Sopenharmony_ci      struct ir3_legalize_state *pstate = &pbd->state;
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_ci      /* Our input (ss)/(sy) state is based on OR'ing the output
106bf215546Sopenharmony_ci       * state of all our predecessor blocks
107bf215546Sopenharmony_ci       */
108bf215546Sopenharmony_ci      regmask_or(&state->needs_ss, &state->needs_ss, &pstate->needs_ss);
109bf215546Sopenharmony_ci      regmask_or(&state->needs_ss_war, &state->needs_ss_war,
110bf215546Sopenharmony_ci                 &pstate->needs_ss_war);
111bf215546Sopenharmony_ci      regmask_or(&state->needs_sy, &state->needs_sy, &pstate->needs_sy);
112bf215546Sopenharmony_ci   }
113bf215546Sopenharmony_ci
114bf215546Sopenharmony_ci   /* We need to take phsyical-only edges into account when tracking shared
115bf215546Sopenharmony_ci    * registers.
116bf215546Sopenharmony_ci    */
117bf215546Sopenharmony_ci   for (unsigned i = 0; i < block->physical_predecessors_count; i++) {
118bf215546Sopenharmony_ci      struct ir3_block *predecessor = block->physical_predecessors[i];
119bf215546Sopenharmony_ci      struct ir3_legalize_block_data *pbd = predecessor->data;
120bf215546Sopenharmony_ci      struct ir3_legalize_state *pstate = &pbd->state;
121bf215546Sopenharmony_ci
122bf215546Sopenharmony_ci      regmask_or_shared(&state->needs_ss, &state->needs_ss, &pstate->needs_ss);
123bf215546Sopenharmony_ci   }
124bf215546Sopenharmony_ci
125bf215546Sopenharmony_ci   unsigned input_count = 0;
126bf215546Sopenharmony_ci
127bf215546Sopenharmony_ci   foreach_instr (n, &block->instr_list) {
128bf215546Sopenharmony_ci      if (is_input(n)) {
129bf215546Sopenharmony_ci         input_count++;
130bf215546Sopenharmony_ci      }
131bf215546Sopenharmony_ci   }
132bf215546Sopenharmony_ci
133bf215546Sopenharmony_ci   unsigned inputs_remaining = input_count;
134bf215546Sopenharmony_ci
135bf215546Sopenharmony_ci   /* Either inputs are in the first block or we expect inputs to be released
136bf215546Sopenharmony_ci    * with the end of the program.
137bf215546Sopenharmony_ci    */
138bf215546Sopenharmony_ci   assert(input_count == 0 || !ctx->early_input_release ||
139bf215546Sopenharmony_ci          block == ir3_after_preamble(block->shader));
140bf215546Sopenharmony_ci
141bf215546Sopenharmony_ci   /* remove all the instructions from the list, we'll be adding
142bf215546Sopenharmony_ci    * them back in as we go
143bf215546Sopenharmony_ci    */
144bf215546Sopenharmony_ci   list_replace(&block->instr_list, &instr_list);
145bf215546Sopenharmony_ci   list_inithead(&block->instr_list);
146bf215546Sopenharmony_ci
147bf215546Sopenharmony_ci   foreach_instr_safe (n, &instr_list) {
148bf215546Sopenharmony_ci      unsigned i;
149bf215546Sopenharmony_ci
150bf215546Sopenharmony_ci      n->flags &= ~(IR3_INSTR_SS | IR3_INSTR_SY);
151bf215546Sopenharmony_ci
152bf215546Sopenharmony_ci      /* _meta::tex_prefetch instructions removed later in
153bf215546Sopenharmony_ci       * collect_tex_prefetches()
154bf215546Sopenharmony_ci       */
155bf215546Sopenharmony_ci      if (is_meta(n) && (n->opc != OPC_META_TEX_PREFETCH))
156bf215546Sopenharmony_ci         continue;
157bf215546Sopenharmony_ci
158bf215546Sopenharmony_ci      if (is_input(n)) {
159bf215546Sopenharmony_ci         struct ir3_register *inloc = n->srcs[0];
160bf215546Sopenharmony_ci         assert(inloc->flags & IR3_REG_IMMED);
161bf215546Sopenharmony_ci         ctx->max_bary = MAX2(ctx->max_bary, inloc->iim_val);
162bf215546Sopenharmony_ci      }
163bf215546Sopenharmony_ci
164bf215546Sopenharmony_ci      if ((last_n && is_barrier(last_n)) || n->opc == OPC_SHPE) {
165bf215546Sopenharmony_ci         n->flags |= IR3_INSTR_SS | IR3_INSTR_SY;
166bf215546Sopenharmony_ci         last_input_needs_ss = false;
167bf215546Sopenharmony_ci         regmask_init(&state->needs_ss_war, mergedregs);
168bf215546Sopenharmony_ci         regmask_init(&state->needs_ss, mergedregs);
169bf215546Sopenharmony_ci         regmask_init(&state->needs_sy, mergedregs);
170bf215546Sopenharmony_ci      }
171bf215546Sopenharmony_ci
172bf215546Sopenharmony_ci      if (last_n && (last_n->opc == OPC_PREDT)) {
173bf215546Sopenharmony_ci         n->flags |= IR3_INSTR_SS;
174bf215546Sopenharmony_ci         regmask_init(&state->needs_ss_war, mergedregs);
175bf215546Sopenharmony_ci         regmask_init(&state->needs_ss, mergedregs);
176bf215546Sopenharmony_ci      }
177bf215546Sopenharmony_ci
178bf215546Sopenharmony_ci      /* NOTE: consider dst register too.. it could happen that
179bf215546Sopenharmony_ci       * texture sample instruction (for example) writes some
180bf215546Sopenharmony_ci       * components which are unused.  A subsequent instruction
181bf215546Sopenharmony_ci       * that writes the same register can race w/ the sam instr
182bf215546Sopenharmony_ci       * resulting in undefined results:
183bf215546Sopenharmony_ci       */
184bf215546Sopenharmony_ci      for (i = 0; i < n->dsts_count + n->srcs_count; i++) {
185bf215546Sopenharmony_ci         struct ir3_register *reg;
186bf215546Sopenharmony_ci         if (i < n->dsts_count)
187bf215546Sopenharmony_ci            reg = n->dsts[i];
188bf215546Sopenharmony_ci         else
189bf215546Sopenharmony_ci            reg = n->srcs[i - n->dsts_count];
190bf215546Sopenharmony_ci
191bf215546Sopenharmony_ci         if (reg_gpr(reg)) {
192bf215546Sopenharmony_ci
193bf215546Sopenharmony_ci            /* TODO: we probably only need (ss) for alu
194bf215546Sopenharmony_ci             * instr consuming sfu result.. need to make
195bf215546Sopenharmony_ci             * some tests for both this and (sy)..
196bf215546Sopenharmony_ci             */
197bf215546Sopenharmony_ci            if (regmask_get(&state->needs_ss, reg)) {
198bf215546Sopenharmony_ci               n->flags |= IR3_INSTR_SS;
199bf215546Sopenharmony_ci               last_input_needs_ss = false;
200bf215546Sopenharmony_ci               regmask_init(&state->needs_ss_war, mergedregs);
201bf215546Sopenharmony_ci               regmask_init(&state->needs_ss, mergedregs);
202bf215546Sopenharmony_ci            }
203bf215546Sopenharmony_ci
204bf215546Sopenharmony_ci            if (regmask_get(&state->needs_sy, reg)) {
205bf215546Sopenharmony_ci               n->flags |= IR3_INSTR_SY;
206bf215546Sopenharmony_ci               regmask_init(&state->needs_sy, mergedregs);
207bf215546Sopenharmony_ci            }
208bf215546Sopenharmony_ci         }
209bf215546Sopenharmony_ci      }
210bf215546Sopenharmony_ci
211bf215546Sopenharmony_ci      foreach_dst (reg, n) {
212bf215546Sopenharmony_ci         if (regmask_get(&state->needs_ss_war, reg)) {
213bf215546Sopenharmony_ci            n->flags |= IR3_INSTR_SS;
214bf215546Sopenharmony_ci            last_input_needs_ss = false;
215bf215546Sopenharmony_ci            regmask_init(&state->needs_ss_war, mergedregs);
216bf215546Sopenharmony_ci            regmask_init(&state->needs_ss, mergedregs);
217bf215546Sopenharmony_ci         }
218bf215546Sopenharmony_ci      }
219bf215546Sopenharmony_ci
220bf215546Sopenharmony_ci      /* cat5+ does not have an (ss) bit, if needed we need to
221bf215546Sopenharmony_ci       * insert a nop to carry the sync flag.  Would be kinda
222bf215546Sopenharmony_ci       * clever if we were aware of this during scheduling, but
223bf215546Sopenharmony_ci       * this should be a pretty rare case:
224bf215546Sopenharmony_ci       */
225bf215546Sopenharmony_ci      if ((n->flags & IR3_INSTR_SS) && (opc_cat(n->opc) >= 5)) {
226bf215546Sopenharmony_ci         struct ir3_instruction *nop;
227bf215546Sopenharmony_ci         nop = ir3_NOP(block);
228bf215546Sopenharmony_ci         nop->flags |= IR3_INSTR_SS;
229bf215546Sopenharmony_ci         n->flags &= ~IR3_INSTR_SS;
230bf215546Sopenharmony_ci      }
231bf215546Sopenharmony_ci
232bf215546Sopenharmony_ci      /* need to be able to set (ss) on first instruction: */
233bf215546Sopenharmony_ci      if (list_is_empty(&block->instr_list) && (opc_cat(n->opc) >= 5))
234bf215546Sopenharmony_ci         ir3_NOP(block);
235bf215546Sopenharmony_ci
236bf215546Sopenharmony_ci      if (ctx->compiler->samgq_workaround &&
237bf215546Sopenharmony_ci          ctx->type != MESA_SHADER_FRAGMENT &&
238bf215546Sopenharmony_ci          ctx->type != MESA_SHADER_COMPUTE && n->opc == OPC_SAMGQ) {
239bf215546Sopenharmony_ci         struct ir3_instruction *samgp;
240bf215546Sopenharmony_ci
241bf215546Sopenharmony_ci         list_delinit(&n->node);
242bf215546Sopenharmony_ci
243bf215546Sopenharmony_ci         for (i = 0; i < 4; i++) {
244bf215546Sopenharmony_ci            samgp = ir3_instr_clone(n);
245bf215546Sopenharmony_ci            samgp->opc = OPC_SAMGP0 + i;
246bf215546Sopenharmony_ci            if (i > 1)
247bf215546Sopenharmony_ci               samgp->flags |= IR3_INSTR_SY;
248bf215546Sopenharmony_ci         }
249bf215546Sopenharmony_ci      } else {
250bf215546Sopenharmony_ci         list_delinit(&n->node);
251bf215546Sopenharmony_ci         list_addtail(&n->node, &block->instr_list);
252bf215546Sopenharmony_ci      }
253bf215546Sopenharmony_ci
254bf215546Sopenharmony_ci      if (is_sfu(n))
255bf215546Sopenharmony_ci         regmask_set(&state->needs_ss, n->dsts[0]);
256bf215546Sopenharmony_ci
257bf215546Sopenharmony_ci      foreach_dst (dst, n) {
258bf215546Sopenharmony_ci         if (dst->flags & IR3_REG_SHARED)
259bf215546Sopenharmony_ci            regmask_set(&state->needs_ss, dst);
260bf215546Sopenharmony_ci      }
261bf215546Sopenharmony_ci
262bf215546Sopenharmony_ci      if (is_tex_or_prefetch(n)) {
263bf215546Sopenharmony_ci         regmask_set(&state->needs_sy, n->dsts[0]);
264bf215546Sopenharmony_ci         if (n->opc == OPC_META_TEX_PREFETCH)
265bf215546Sopenharmony_ci            has_tex_prefetch = true;
266bf215546Sopenharmony_ci      } else if (n->opc == OPC_RESINFO) {
267bf215546Sopenharmony_ci         regmask_set(&state->needs_ss, n->dsts[0]);
268bf215546Sopenharmony_ci         ir3_NOP(block)->flags |= IR3_INSTR_SS;
269bf215546Sopenharmony_ci         last_input_needs_ss = false;
270bf215546Sopenharmony_ci      } else if (is_load(n)) {
271bf215546Sopenharmony_ci         if (is_local_mem_load(n))
272bf215546Sopenharmony_ci            regmask_set(&state->needs_ss, n->dsts[0]);
273bf215546Sopenharmony_ci         else
274bf215546Sopenharmony_ci            regmask_set(&state->needs_sy, n->dsts[0]);
275bf215546Sopenharmony_ci      } else if (is_atomic(n->opc)) {
276bf215546Sopenharmony_ci         if (is_bindless_atomic(n->opc)) {
277bf215546Sopenharmony_ci            regmask_set(&state->needs_sy, n->srcs[2]);
278bf215546Sopenharmony_ci         } else if (is_global_a3xx_atomic(n->opc) ||
279bf215546Sopenharmony_ci                    is_global_a6xx_atomic(n->opc)) {
280bf215546Sopenharmony_ci            regmask_set(&state->needs_sy, n->dsts[0]);
281bf215546Sopenharmony_ci         } else {
282bf215546Sopenharmony_ci            regmask_set(&state->needs_ss, n->dsts[0]);
283bf215546Sopenharmony_ci         }
284bf215546Sopenharmony_ci      }
285bf215546Sopenharmony_ci
286bf215546Sopenharmony_ci      if (is_ssbo(n->opc) || is_global_a3xx_atomic(n->opc) ||
287bf215546Sopenharmony_ci          is_bindless_atomic(n->opc))
288bf215546Sopenharmony_ci         ctx->so->has_ssbo = true;
289bf215546Sopenharmony_ci
290bf215546Sopenharmony_ci      /* both tex/sfu appear to not always immediately consume
291bf215546Sopenharmony_ci       * their src register(s):
292bf215546Sopenharmony_ci       */
293bf215546Sopenharmony_ci      if (is_tex(n) || is_sfu(n) || is_mem(n)) {
294bf215546Sopenharmony_ci         foreach_src (reg, n) {
295bf215546Sopenharmony_ci            regmask_set(&state->needs_ss_war, reg);
296bf215546Sopenharmony_ci         }
297bf215546Sopenharmony_ci      }
298bf215546Sopenharmony_ci
299bf215546Sopenharmony_ci      if (ctx->early_input_release && is_input(n)) {
300bf215546Sopenharmony_ci         last_input_needs_ss |= (n->opc == OPC_LDLV);
301bf215546Sopenharmony_ci
302bf215546Sopenharmony_ci         assert(inputs_remaining > 0);
303bf215546Sopenharmony_ci         inputs_remaining--;
304bf215546Sopenharmony_ci         if (inputs_remaining == 0) {
305bf215546Sopenharmony_ci            /* This is the last input. We add the (ei) flag to release
306bf215546Sopenharmony_ci             * varying memory after this executes. If it's an ldlv,
307bf215546Sopenharmony_ci             * however, we need to insert a dummy bary.f on which we can
308bf215546Sopenharmony_ci             * set the (ei) flag. We may also need to insert an (ss) to
309bf215546Sopenharmony_ci             * guarantee that all ldlv's have finished fetching their
310bf215546Sopenharmony_ci             * results before releasing the varying memory.
311bf215546Sopenharmony_ci             */
312bf215546Sopenharmony_ci            struct ir3_instruction *last_input = n;
313bf215546Sopenharmony_ci            if (n->opc == OPC_LDLV) {
314bf215546Sopenharmony_ci               struct ir3_instruction *baryf;
315bf215546Sopenharmony_ci
316bf215546Sopenharmony_ci               /* (ss)bary.f (ei)r63.x, 0, r0.x */
317bf215546Sopenharmony_ci               baryf = ir3_instr_create(block, OPC_BARY_F, 1, 2);
318bf215546Sopenharmony_ci               ir3_dst_create(baryf, regid(63, 0), 0);
319bf215546Sopenharmony_ci               ir3_src_create(baryf, 0, IR3_REG_IMMED)->iim_val = 0;
320bf215546Sopenharmony_ci               ir3_src_create(baryf, regid(0, 0), 0);
321bf215546Sopenharmony_ci
322bf215546Sopenharmony_ci               last_input = baryf;
323bf215546Sopenharmony_ci            }
324bf215546Sopenharmony_ci
325bf215546Sopenharmony_ci            last_input->dsts[0]->flags |= IR3_REG_EI;
326bf215546Sopenharmony_ci            if (last_input_needs_ss) {
327bf215546Sopenharmony_ci               last_input->flags |= IR3_INSTR_SS;
328bf215546Sopenharmony_ci               regmask_init(&state->needs_ss_war, mergedregs);
329bf215546Sopenharmony_ci               regmask_init(&state->needs_ss, mergedregs);
330bf215546Sopenharmony_ci            }
331bf215546Sopenharmony_ci         }
332bf215546Sopenharmony_ci      }
333bf215546Sopenharmony_ci
334bf215546Sopenharmony_ci      last_n = n;
335bf215546Sopenharmony_ci   }
336bf215546Sopenharmony_ci
337bf215546Sopenharmony_ci   assert(inputs_remaining == 0 || !ctx->early_input_release);
338bf215546Sopenharmony_ci
339bf215546Sopenharmony_ci   if (has_tex_prefetch && !ctx->has_inputs) {
340bf215546Sopenharmony_ci      /* texture prefetch, but *no* inputs.. we need to insert a
341bf215546Sopenharmony_ci       * dummy bary.f at the top of the shader to unblock varying
342bf215546Sopenharmony_ci       * storage:
343bf215546Sopenharmony_ci       */
344bf215546Sopenharmony_ci      struct ir3_instruction *baryf;
345bf215546Sopenharmony_ci
346bf215546Sopenharmony_ci      /* (ss)bary.f (ei)r63.x, 0, r0.x */
347bf215546Sopenharmony_ci      baryf = ir3_instr_create(block, OPC_BARY_F, 1, 2);
348bf215546Sopenharmony_ci      ir3_dst_create(baryf, regid(63, 0), 0)->flags |= IR3_REG_EI;
349bf215546Sopenharmony_ci      ir3_src_create(baryf, 0, IR3_REG_IMMED)->iim_val = 0;
350bf215546Sopenharmony_ci      ir3_src_create(baryf, regid(0, 0), 0);
351bf215546Sopenharmony_ci
352bf215546Sopenharmony_ci      /* insert the dummy bary.f at head: */
353bf215546Sopenharmony_ci      list_delinit(&baryf->node);
354bf215546Sopenharmony_ci      list_add(&baryf->node, &block->instr_list);
355bf215546Sopenharmony_ci   }
356bf215546Sopenharmony_ci
357bf215546Sopenharmony_ci   bd->valid = true;
358bf215546Sopenharmony_ci
359bf215546Sopenharmony_ci   if (memcmp(&prev_state, state, sizeof(*state))) {
360bf215546Sopenharmony_ci      /* our output state changed, this invalidates all of our
361bf215546Sopenharmony_ci       * successors:
362bf215546Sopenharmony_ci       */
363bf215546Sopenharmony_ci      for (unsigned i = 0; i < ARRAY_SIZE(block->successors); i++) {
364bf215546Sopenharmony_ci         if (!block->successors[i])
365bf215546Sopenharmony_ci            break;
366bf215546Sopenharmony_ci         struct ir3_legalize_block_data *pbd = block->successors[i]->data;
367bf215546Sopenharmony_ci         pbd->valid = false;
368bf215546Sopenharmony_ci      }
369bf215546Sopenharmony_ci   }
370bf215546Sopenharmony_ci
371bf215546Sopenharmony_ci   return true;
372bf215546Sopenharmony_ci}
373bf215546Sopenharmony_ci
374bf215546Sopenharmony_ci/* Expands dsxpp and dsypp macros to:
375bf215546Sopenharmony_ci *
376bf215546Sopenharmony_ci * dsxpp.1 dst, src
377bf215546Sopenharmony_ci * dsxpp.1.p dst, src
378bf215546Sopenharmony_ci *
379bf215546Sopenharmony_ci * We apply this after flags syncing, as we don't want to sync in between the
380bf215546Sopenharmony_ci * two (which might happen if dst == src).  We do it before nop scheduling
381bf215546Sopenharmony_ci * because that needs to count actual instructions.
382bf215546Sopenharmony_ci */
383bf215546Sopenharmony_cistatic bool
384bf215546Sopenharmony_ciapply_fine_deriv_macro(struct ir3_legalize_ctx *ctx, struct ir3_block *block)
385bf215546Sopenharmony_ci{
386bf215546Sopenharmony_ci   struct list_head instr_list;
387bf215546Sopenharmony_ci
388bf215546Sopenharmony_ci   /* remove all the instructions from the list, we'll be adding
389bf215546Sopenharmony_ci    * them back in as we go
390bf215546Sopenharmony_ci    */
391bf215546Sopenharmony_ci   list_replace(&block->instr_list, &instr_list);
392bf215546Sopenharmony_ci   list_inithead(&block->instr_list);
393bf215546Sopenharmony_ci
394bf215546Sopenharmony_ci   foreach_instr_safe (n, &instr_list) {
395bf215546Sopenharmony_ci      list_addtail(&n->node, &block->instr_list);
396bf215546Sopenharmony_ci
397bf215546Sopenharmony_ci      if (n->opc == OPC_DSXPP_MACRO || n->opc == OPC_DSYPP_MACRO) {
398bf215546Sopenharmony_ci         n->opc = (n->opc == OPC_DSXPP_MACRO) ? OPC_DSXPP_1 : OPC_DSYPP_1;
399bf215546Sopenharmony_ci
400bf215546Sopenharmony_ci         struct ir3_instruction *op_p = ir3_instr_clone(n);
401bf215546Sopenharmony_ci         op_p->flags = IR3_INSTR_P;
402bf215546Sopenharmony_ci
403bf215546Sopenharmony_ci         ctx->so->need_fine_derivatives = true;
404bf215546Sopenharmony_ci      }
405bf215546Sopenharmony_ci   }
406bf215546Sopenharmony_ci
407bf215546Sopenharmony_ci   return true;
408bf215546Sopenharmony_ci}
409bf215546Sopenharmony_ci
410bf215546Sopenharmony_ci/* NOTE: branch instructions are always the last instruction(s)
411bf215546Sopenharmony_ci * in the block.  We take advantage of this as we resolve the
412bf215546Sopenharmony_ci * branches, since "if (foo) break;" constructs turn into
413bf215546Sopenharmony_ci * something like:
414bf215546Sopenharmony_ci *
415bf215546Sopenharmony_ci *   block3 {
416bf215546Sopenharmony_ci *   	...
417bf215546Sopenharmony_ci *   	0029:021: mov.s32s32 r62.x, r1.y
418bf215546Sopenharmony_ci *   	0082:022: br !p0.x, target=block5
419bf215546Sopenharmony_ci *   	0083:023: br p0.x, target=block4
420bf215546Sopenharmony_ci *   	// succs: if _[0029:021: mov.s32s32] block4; else block5;
421bf215546Sopenharmony_ci *   }
422bf215546Sopenharmony_ci *   block4 {
423bf215546Sopenharmony_ci *   	0084:024: jump, target=block6
424bf215546Sopenharmony_ci *   	// succs: block6;
425bf215546Sopenharmony_ci *   }
426bf215546Sopenharmony_ci *   block5 {
427bf215546Sopenharmony_ci *   	0085:025: jump, target=block7
428bf215546Sopenharmony_ci *   	// succs: block7;
429bf215546Sopenharmony_ci *   }
430bf215546Sopenharmony_ci *
431bf215546Sopenharmony_ci * ie. only instruction in block4/block5 is a jump, so when
432bf215546Sopenharmony_ci * resolving branches we can easily detect this by checking
433bf215546Sopenharmony_ci * that the first instruction in the target block is itself
434bf215546Sopenharmony_ci * a jump, and setup the br directly to the jump's target
435bf215546Sopenharmony_ci * (and strip back out the now unreached jump)
436bf215546Sopenharmony_ci *
437bf215546Sopenharmony_ci * TODO sometimes we end up with things like:
438bf215546Sopenharmony_ci *
439bf215546Sopenharmony_ci *    br !p0.x, #2
440bf215546Sopenharmony_ci *    br p0.x, #12
441bf215546Sopenharmony_ci *    add.u r0.y, r0.y, 1
442bf215546Sopenharmony_ci *
443bf215546Sopenharmony_ci * If we swapped the order of the branches, we could drop one.
444bf215546Sopenharmony_ci */
445bf215546Sopenharmony_cistatic struct ir3_block *
446bf215546Sopenharmony_ciresolve_dest_block(struct ir3_block *block)
447bf215546Sopenharmony_ci{
448bf215546Sopenharmony_ci   /* special case for last block: */
449bf215546Sopenharmony_ci   if (!block->successors[0])
450bf215546Sopenharmony_ci      return block;
451bf215546Sopenharmony_ci
452bf215546Sopenharmony_ci   /* NOTE that we may or may not have inserted the jump
453bf215546Sopenharmony_ci    * in the target block yet, so conditions to resolve
454bf215546Sopenharmony_ci    * the dest to the dest block's successor are:
455bf215546Sopenharmony_ci    *
456bf215546Sopenharmony_ci    *   (1) successor[1] == NULL &&
457bf215546Sopenharmony_ci    *   (2) (block-is-empty || only-instr-is-jump)
458bf215546Sopenharmony_ci    */
459bf215546Sopenharmony_ci   if (block->successors[1] == NULL) {
460bf215546Sopenharmony_ci      if (list_is_empty(&block->instr_list)) {
461bf215546Sopenharmony_ci         return block->successors[0];
462bf215546Sopenharmony_ci      } else if (list_length(&block->instr_list) == 1) {
463bf215546Sopenharmony_ci         struct ir3_instruction *instr =
464bf215546Sopenharmony_ci            list_first_entry(&block->instr_list, struct ir3_instruction, node);
465bf215546Sopenharmony_ci         if (instr->opc == OPC_JUMP) {
466bf215546Sopenharmony_ci            /* If this jump is backwards, then we will probably convert
467bf215546Sopenharmony_ci             * the jump being resolved to a backwards jump, which will
468bf215546Sopenharmony_ci             * change a loop-with-continue or loop-with-if into a
469bf215546Sopenharmony_ci             * doubly-nested loop and change the convergence behavior.
470bf215546Sopenharmony_ci             * Disallow this here.
471bf215546Sopenharmony_ci             */
472bf215546Sopenharmony_ci            if (block->successors[0]->index <= block->index)
473bf215546Sopenharmony_ci               return block;
474bf215546Sopenharmony_ci            return block->successors[0];
475bf215546Sopenharmony_ci         }
476bf215546Sopenharmony_ci      }
477bf215546Sopenharmony_ci   }
478bf215546Sopenharmony_ci   return block;
479bf215546Sopenharmony_ci}
480bf215546Sopenharmony_ci
481bf215546Sopenharmony_cistatic void
482bf215546Sopenharmony_ciremove_unused_block(struct ir3_block *old_target)
483bf215546Sopenharmony_ci{
484bf215546Sopenharmony_ci   list_delinit(&old_target->node);
485bf215546Sopenharmony_ci
486bf215546Sopenharmony_ci   /* If there are any physical predecessors due to fallthroughs, then they may
487bf215546Sopenharmony_ci    * fall through to any of the physical successors of this block. But we can
488bf215546Sopenharmony_ci    * only fit two, so just pick the "earliest" one, i.e. the fallthrough if
489bf215546Sopenharmony_ci    * possible.
490bf215546Sopenharmony_ci    *
491bf215546Sopenharmony_ci    * TODO: we really ought to have unlimited numbers of physical successors,
492bf215546Sopenharmony_ci    * both because of this and because we currently don't model some scenarios
493bf215546Sopenharmony_ci    * with nested break/continue correctly.
494bf215546Sopenharmony_ci    */
495bf215546Sopenharmony_ci   struct ir3_block *new_target;
496bf215546Sopenharmony_ci   if (old_target->physical_successors[1] &&
497bf215546Sopenharmony_ci       old_target->physical_successors[1]->start_ip <
498bf215546Sopenharmony_ci       old_target->physical_successors[0]->start_ip) {
499bf215546Sopenharmony_ci      new_target = old_target->physical_successors[1];
500bf215546Sopenharmony_ci   } else {
501bf215546Sopenharmony_ci      new_target = old_target->physical_successors[0];
502bf215546Sopenharmony_ci   }
503bf215546Sopenharmony_ci
504bf215546Sopenharmony_ci   for (unsigned i = 0; i < old_target->physical_predecessors_count; i++) {
505bf215546Sopenharmony_ci      struct ir3_block *pred = old_target->physical_predecessors[i];
506bf215546Sopenharmony_ci      if (pred->physical_successors[0] == old_target) {
507bf215546Sopenharmony_ci         if (!new_target) {
508bf215546Sopenharmony_ci            /* If we remove a physical successor, make sure the only physical
509bf215546Sopenharmony_ci             * successor is the first one.
510bf215546Sopenharmony_ci             */
511bf215546Sopenharmony_ci            pred->physical_successors[0] = pred->physical_successors[1];
512bf215546Sopenharmony_ci            pred->physical_successors[1] = NULL;
513bf215546Sopenharmony_ci         } else {
514bf215546Sopenharmony_ci            pred->physical_successors[0] = new_target;
515bf215546Sopenharmony_ci         }
516bf215546Sopenharmony_ci      } else {
517bf215546Sopenharmony_ci         assert(pred->physical_successors[1] == old_target);
518bf215546Sopenharmony_ci         pred->physical_successors[1] = new_target;
519bf215546Sopenharmony_ci      }
520bf215546Sopenharmony_ci      if (new_target)
521bf215546Sopenharmony_ci         ir3_block_add_physical_predecessor(new_target, pred);
522bf215546Sopenharmony_ci   }
523bf215546Sopenharmony_ci
524bf215546Sopenharmony_ci   /* cleanup dangling predecessors: */
525bf215546Sopenharmony_ci   for (unsigned i = 0; i < ARRAY_SIZE(old_target->successors); i++) {
526bf215546Sopenharmony_ci      if (old_target->successors[i]) {
527bf215546Sopenharmony_ci         struct ir3_block *succ = old_target->successors[i];
528bf215546Sopenharmony_ci         ir3_block_remove_predecessor(succ, old_target);
529bf215546Sopenharmony_ci      }
530bf215546Sopenharmony_ci   }
531bf215546Sopenharmony_ci
532bf215546Sopenharmony_ci   for (unsigned i = 0; i < ARRAY_SIZE(old_target->physical_successors); i++) {
533bf215546Sopenharmony_ci      if (old_target->physical_successors[i]) {
534bf215546Sopenharmony_ci         struct ir3_block *succ = old_target->physical_successors[i];
535bf215546Sopenharmony_ci         ir3_block_remove_physical_predecessor(succ, old_target);
536bf215546Sopenharmony_ci      }
537bf215546Sopenharmony_ci   }
538bf215546Sopenharmony_ci}
539bf215546Sopenharmony_ci
540bf215546Sopenharmony_cistatic bool
541bf215546Sopenharmony_ciretarget_jump(struct ir3_instruction *instr, struct ir3_block *new_target)
542bf215546Sopenharmony_ci{
543bf215546Sopenharmony_ci   struct ir3_block *old_target = instr->cat0.target;
544bf215546Sopenharmony_ci   struct ir3_block *cur_block = instr->block;
545bf215546Sopenharmony_ci
546bf215546Sopenharmony_ci   /* update current blocks successors to reflect the retargetting: */
547bf215546Sopenharmony_ci   if (cur_block->successors[0] == old_target) {
548bf215546Sopenharmony_ci      cur_block->successors[0] = new_target;
549bf215546Sopenharmony_ci   } else {
550bf215546Sopenharmony_ci      assert(cur_block->successors[1] == old_target);
551bf215546Sopenharmony_ci      cur_block->successors[1] = new_target;
552bf215546Sopenharmony_ci   }
553bf215546Sopenharmony_ci
554bf215546Sopenharmony_ci   /* also update physical_successors: */
555bf215546Sopenharmony_ci   if (cur_block->physical_successors[0] == old_target) {
556bf215546Sopenharmony_ci      cur_block->physical_successors[0] = new_target;
557bf215546Sopenharmony_ci   } else {
558bf215546Sopenharmony_ci      assert(cur_block->physical_successors[1] == old_target);
559bf215546Sopenharmony_ci      cur_block->physical_successors[1] = new_target;
560bf215546Sopenharmony_ci   }
561bf215546Sopenharmony_ci
562bf215546Sopenharmony_ci   /* update new target's predecessors: */
563bf215546Sopenharmony_ci   ir3_block_add_predecessor(new_target, cur_block);
564bf215546Sopenharmony_ci   ir3_block_add_physical_predecessor(new_target, cur_block);
565bf215546Sopenharmony_ci
566bf215546Sopenharmony_ci   /* and remove old_target's predecessor: */
567bf215546Sopenharmony_ci   ir3_block_remove_predecessor(old_target, cur_block);
568bf215546Sopenharmony_ci   ir3_block_remove_physical_predecessor(old_target, cur_block);
569bf215546Sopenharmony_ci
570bf215546Sopenharmony_ci   instr->cat0.target = new_target;
571bf215546Sopenharmony_ci
572bf215546Sopenharmony_ci   if (old_target->predecessors_count == 0) {
573bf215546Sopenharmony_ci      remove_unused_block(old_target);
574bf215546Sopenharmony_ci      return true;
575bf215546Sopenharmony_ci   }
576bf215546Sopenharmony_ci
577bf215546Sopenharmony_ci   return false;
578bf215546Sopenharmony_ci}
579bf215546Sopenharmony_ci
580bf215546Sopenharmony_cistatic bool
581bf215546Sopenharmony_ciopt_jump(struct ir3 *ir)
582bf215546Sopenharmony_ci{
583bf215546Sopenharmony_ci   bool progress = false;
584bf215546Sopenharmony_ci
585bf215546Sopenharmony_ci   unsigned index = 0;
586bf215546Sopenharmony_ci   foreach_block (block, &ir->block_list)
587bf215546Sopenharmony_ci      block->index = index++;
588bf215546Sopenharmony_ci
589bf215546Sopenharmony_ci   foreach_block (block, &ir->block_list) {
590bf215546Sopenharmony_ci      foreach_instr (instr, &block->instr_list) {
591bf215546Sopenharmony_ci         if (!is_flow(instr) || !instr->cat0.target)
592bf215546Sopenharmony_ci            continue;
593bf215546Sopenharmony_ci
594bf215546Sopenharmony_ci         struct ir3_block *tblock = resolve_dest_block(instr->cat0.target);
595bf215546Sopenharmony_ci         if (tblock != instr->cat0.target) {
596bf215546Sopenharmony_ci            progress = true;
597bf215546Sopenharmony_ci
598bf215546Sopenharmony_ci            /* Exit early if we deleted a block to avoid iterator
599bf215546Sopenharmony_ci             * weirdness/assert fails
600bf215546Sopenharmony_ci             */
601bf215546Sopenharmony_ci            if (retarget_jump(instr, tblock))
602bf215546Sopenharmony_ci               return true;
603bf215546Sopenharmony_ci         }
604bf215546Sopenharmony_ci      }
605bf215546Sopenharmony_ci
606bf215546Sopenharmony_ci      /* Detect the case where the block ends either with:
607bf215546Sopenharmony_ci       * - A single unconditional jump to the next block.
608bf215546Sopenharmony_ci       * - Two jump instructions with opposite conditions, and one of the
609bf215546Sopenharmony_ci       *   them jumps to the next block.
610bf215546Sopenharmony_ci       * We can remove the one that jumps to the next block in either case.
611bf215546Sopenharmony_ci       */
612bf215546Sopenharmony_ci      if (list_is_empty(&block->instr_list))
613bf215546Sopenharmony_ci         continue;
614bf215546Sopenharmony_ci
615bf215546Sopenharmony_ci      struct ir3_instruction *jumps[2] = {NULL, NULL};
616bf215546Sopenharmony_ci      jumps[0] =
617bf215546Sopenharmony_ci         list_last_entry(&block->instr_list, struct ir3_instruction, node);
618bf215546Sopenharmony_ci      if (!list_is_singular(&block->instr_list))
619bf215546Sopenharmony_ci         jumps[1] =
620bf215546Sopenharmony_ci            list_last_entry(&jumps[0]->node, struct ir3_instruction, node);
621bf215546Sopenharmony_ci
622bf215546Sopenharmony_ci      if (jumps[0]->opc == OPC_JUMP)
623bf215546Sopenharmony_ci         jumps[1] = NULL;
624bf215546Sopenharmony_ci      else if (jumps[0]->opc != OPC_B || !jumps[1] || jumps[1]->opc != OPC_B)
625bf215546Sopenharmony_ci         continue;
626bf215546Sopenharmony_ci
627bf215546Sopenharmony_ci      for (unsigned i = 0; i < 2; i++) {
628bf215546Sopenharmony_ci         if (!jumps[i])
629bf215546Sopenharmony_ci            continue;
630bf215546Sopenharmony_ci
631bf215546Sopenharmony_ci         struct ir3_block *tblock = jumps[i]->cat0.target;
632bf215546Sopenharmony_ci         if (&tblock->node == block->node.next) {
633bf215546Sopenharmony_ci            list_delinit(&jumps[i]->node);
634bf215546Sopenharmony_ci            progress = true;
635bf215546Sopenharmony_ci            break;
636bf215546Sopenharmony_ci         }
637bf215546Sopenharmony_ci      }
638bf215546Sopenharmony_ci   }
639bf215546Sopenharmony_ci
640bf215546Sopenharmony_ci   return progress;
641bf215546Sopenharmony_ci}
642bf215546Sopenharmony_ci
643bf215546Sopenharmony_cistatic void
644bf215546Sopenharmony_ciresolve_jumps(struct ir3 *ir)
645bf215546Sopenharmony_ci{
646bf215546Sopenharmony_ci   foreach_block (block, &ir->block_list)
647bf215546Sopenharmony_ci      foreach_instr (instr, &block->instr_list)
648bf215546Sopenharmony_ci         if (is_flow(instr) && instr->cat0.target) {
649bf215546Sopenharmony_ci            struct ir3_instruction *target = list_first_entry(
650bf215546Sopenharmony_ci               &instr->cat0.target->instr_list, struct ir3_instruction, node);
651bf215546Sopenharmony_ci
652bf215546Sopenharmony_ci            instr->cat0.immed = (int)target->ip - (int)instr->ip;
653bf215546Sopenharmony_ci         }
654bf215546Sopenharmony_ci}
655bf215546Sopenharmony_ci
656bf215546Sopenharmony_cistatic void
657bf215546Sopenharmony_cimark_jp(struct ir3_block *block)
658bf215546Sopenharmony_ci{
659bf215546Sopenharmony_ci   /* We only call this on the end block (in kill_sched) or after retargeting
660bf215546Sopenharmony_ci    * all jumps to empty blocks (in mark_xvergence_points) so there's no need to
661bf215546Sopenharmony_ci    * worry about empty blocks.
662bf215546Sopenharmony_ci    */
663bf215546Sopenharmony_ci   assert(!list_is_empty(&block->instr_list));
664bf215546Sopenharmony_ci
665bf215546Sopenharmony_ci   struct ir3_instruction *target =
666bf215546Sopenharmony_ci      list_first_entry(&block->instr_list, struct ir3_instruction, node);
667bf215546Sopenharmony_ci   target->flags |= IR3_INSTR_JP;
668bf215546Sopenharmony_ci}
669bf215546Sopenharmony_ci
670bf215546Sopenharmony_ci/* Mark points where control flow converges or diverges.
671bf215546Sopenharmony_ci *
672bf215546Sopenharmony_ci * Divergence points could actually be re-convergence points where
673bf215546Sopenharmony_ci * "parked" threads are recoverged with threads that took the opposite
674bf215546Sopenharmony_ci * path last time around.  Possibly it is easier to think of (jp) as
675bf215546Sopenharmony_ci * "the execution mask might have changed".
676bf215546Sopenharmony_ci */
677bf215546Sopenharmony_cistatic void
678bf215546Sopenharmony_cimark_xvergence_points(struct ir3 *ir)
679bf215546Sopenharmony_ci{
680bf215546Sopenharmony_ci   foreach_block (block, &ir->block_list) {
681bf215546Sopenharmony_ci      /* We need to insert (jp) if an entry in the "branch stack" is created for
682bf215546Sopenharmony_ci       * our block. This happens if there is a predecessor to our block that may
683bf215546Sopenharmony_ci       * fallthrough to an earlier block in the physical CFG, either because it
684bf215546Sopenharmony_ci       * ends in a non-uniform conditional branch or because there's a
685bf215546Sopenharmony_ci       * fallthrough for an block in-between that also starts with (jp) and was
686bf215546Sopenharmony_ci       * pushed on the branch stack already.
687bf215546Sopenharmony_ci       */
688bf215546Sopenharmony_ci      for (unsigned i = 0; i < block->predecessors_count; i++) {
689bf215546Sopenharmony_ci         struct ir3_block *pred = block->predecessors[i];
690bf215546Sopenharmony_ci
691bf215546Sopenharmony_ci         for (unsigned j = 0; j < ARRAY_SIZE(pred->physical_successors); j++) {
692bf215546Sopenharmony_ci            if (pred->physical_successors[j] != NULL &&
693bf215546Sopenharmony_ci                pred->physical_successors[j]->start_ip < block->start_ip)
694bf215546Sopenharmony_ci               mark_jp(block);
695bf215546Sopenharmony_ci
696bf215546Sopenharmony_ci            /* If the predecessor just falls through to this block, we still
697bf215546Sopenharmony_ci             * need to check if it "falls through" by jumping to the block. This
698bf215546Sopenharmony_ci             * can happen if opt_jump fails and the block ends in two branches,
699bf215546Sopenharmony_ci             * or if there's an empty if-statement (which currently can happen
700bf215546Sopenharmony_ci             * with binning shaders after dead-code elimination) and the block
701bf215546Sopenharmony_ci             * before ends with a conditional branch directly to this block.
702bf215546Sopenharmony_ci             */
703bf215546Sopenharmony_ci            if (pred->physical_successors[j] == block) {
704bf215546Sopenharmony_ci               foreach_instr_rev (instr, &pred->instr_list) {
705bf215546Sopenharmony_ci                  if (!is_flow(instr))
706bf215546Sopenharmony_ci                     break;
707bf215546Sopenharmony_ci                  if (instr->cat0.target == block) {
708bf215546Sopenharmony_ci                     mark_jp(block);
709bf215546Sopenharmony_ci                     break;
710bf215546Sopenharmony_ci                  }
711bf215546Sopenharmony_ci               }
712bf215546Sopenharmony_ci            }
713bf215546Sopenharmony_ci         }
714bf215546Sopenharmony_ci      }
715bf215546Sopenharmony_ci   }
716bf215546Sopenharmony_ci}
717bf215546Sopenharmony_ci
718bf215546Sopenharmony_ci/* Insert the branch/jump instructions for flow control between blocks.
719bf215546Sopenharmony_ci * Initially this is done naively, without considering if the successor
720bf215546Sopenharmony_ci * block immediately follows the current block (ie. so no jump required),
721bf215546Sopenharmony_ci * but that is cleaned up in opt_jump().
722bf215546Sopenharmony_ci *
723bf215546Sopenharmony_ci * TODO what ensures that the last write to p0.x in a block is the
724bf215546Sopenharmony_ci * branch condition?  Have we been getting lucky all this time?
725bf215546Sopenharmony_ci */
726bf215546Sopenharmony_cistatic void
727bf215546Sopenharmony_ciblock_sched(struct ir3 *ir)
728bf215546Sopenharmony_ci{
729bf215546Sopenharmony_ci   foreach_block (block, &ir->block_list) {
730bf215546Sopenharmony_ci      if (block->successors[1]) {
731bf215546Sopenharmony_ci         /* if/else, conditional branches to "then" or "else": */
732bf215546Sopenharmony_ci         struct ir3_instruction *br1, *br2;
733bf215546Sopenharmony_ci
734bf215546Sopenharmony_ci         if (block->brtype == IR3_BRANCH_GETONE ||
735bf215546Sopenharmony_ci             block->brtype == IR3_BRANCH_SHPS) {
736bf215546Sopenharmony_ci            /* getone/shps can't be inverted, and it wouldn't even make sense
737bf215546Sopenharmony_ci             * to follow it with an inverted branch, so follow it by an
738bf215546Sopenharmony_ci             * unconditional branch.
739bf215546Sopenharmony_ci             */
740bf215546Sopenharmony_ci            assert(!block->condition);
741bf215546Sopenharmony_ci            if (block->brtype == IR3_BRANCH_GETONE)
742bf215546Sopenharmony_ci               br1 = ir3_GETONE(block);
743bf215546Sopenharmony_ci            else
744bf215546Sopenharmony_ci               br1 = ir3_SHPS(block);
745bf215546Sopenharmony_ci            br1->cat0.target = block->successors[1];
746bf215546Sopenharmony_ci
747bf215546Sopenharmony_ci            br2 = ir3_JUMP(block);
748bf215546Sopenharmony_ci            br2->cat0.target = block->successors[0];
749bf215546Sopenharmony_ci         } else {
750bf215546Sopenharmony_ci            assert(block->condition);
751bf215546Sopenharmony_ci
752bf215546Sopenharmony_ci            /* create "else" branch first (since "then" block should
753bf215546Sopenharmony_ci             * frequently/always end up being a fall-thru):
754bf215546Sopenharmony_ci             */
755bf215546Sopenharmony_ci            br1 = ir3_instr_create(block, OPC_B, 0, 1);
756bf215546Sopenharmony_ci            ir3_src_create(br1, regid(REG_P0, 0), 0)->def =
757bf215546Sopenharmony_ci               block->condition->dsts[0];
758bf215546Sopenharmony_ci            br1->cat0.inv1 = true;
759bf215546Sopenharmony_ci            br1->cat0.target = block->successors[1];
760bf215546Sopenharmony_ci
761bf215546Sopenharmony_ci            /* "then" branch: */
762bf215546Sopenharmony_ci            br2 = ir3_instr_create(block, OPC_B, 0, 1);
763bf215546Sopenharmony_ci            ir3_src_create(br2, regid(REG_P0, 0), 0)->def =
764bf215546Sopenharmony_ci               block->condition->dsts[0];
765bf215546Sopenharmony_ci            br2->cat0.target = block->successors[0];
766bf215546Sopenharmony_ci
767bf215546Sopenharmony_ci            switch (block->brtype) {
768bf215546Sopenharmony_ci            case IR3_BRANCH_COND:
769bf215546Sopenharmony_ci               br1->cat0.brtype = br2->cat0.brtype = BRANCH_PLAIN;
770bf215546Sopenharmony_ci               break;
771bf215546Sopenharmony_ci            case IR3_BRANCH_ALL:
772bf215546Sopenharmony_ci               br1->cat0.brtype = BRANCH_ANY;
773bf215546Sopenharmony_ci               br2->cat0.brtype = BRANCH_ALL;
774bf215546Sopenharmony_ci               break;
775bf215546Sopenharmony_ci            case IR3_BRANCH_ANY:
776bf215546Sopenharmony_ci               br1->cat0.brtype = BRANCH_ALL;
777bf215546Sopenharmony_ci               br2->cat0.brtype = BRANCH_ANY;
778bf215546Sopenharmony_ci               break;
779bf215546Sopenharmony_ci            case IR3_BRANCH_GETONE:
780bf215546Sopenharmony_ci            case IR3_BRANCH_SHPS:
781bf215546Sopenharmony_ci               unreachable("can't get here");
782bf215546Sopenharmony_ci            }
783bf215546Sopenharmony_ci         }
784bf215546Sopenharmony_ci      } else if (block->successors[0]) {
785bf215546Sopenharmony_ci         /* otherwise unconditional jump to next block: */
786bf215546Sopenharmony_ci         struct ir3_instruction *jmp;
787bf215546Sopenharmony_ci
788bf215546Sopenharmony_ci         jmp = ir3_JUMP(block);
789bf215546Sopenharmony_ci         jmp->cat0.target = block->successors[0];
790bf215546Sopenharmony_ci      }
791bf215546Sopenharmony_ci   }
792bf215546Sopenharmony_ci}
793bf215546Sopenharmony_ci
794bf215546Sopenharmony_ci/* Here we workaround the fact that kill doesn't actually kill the thread as
795bf215546Sopenharmony_ci * GL expects. The last instruction always needs to be an end instruction,
796bf215546Sopenharmony_ci * which means that if we're stuck in a loop where kill is the only way out,
797bf215546Sopenharmony_ci * then we may have to jump out to the end. kill may also have the d3d
798bf215546Sopenharmony_ci * semantics of converting the thread to a helper thread, rather than setting
799bf215546Sopenharmony_ci * the exec mask to 0, in which case the helper thread could get stuck in an
800bf215546Sopenharmony_ci * infinite loop.
801bf215546Sopenharmony_ci *
802bf215546Sopenharmony_ci * We do this late, both to give the scheduler the opportunity to reschedule
803bf215546Sopenharmony_ci * kill instructions earlier and to avoid having to create a separate basic
804bf215546Sopenharmony_ci * block.
805bf215546Sopenharmony_ci *
806bf215546Sopenharmony_ci * TODO: Assuming that the wavefront doesn't stop as soon as all threads are
807bf215546Sopenharmony_ci * killed, we might benefit by doing this more aggressively when the remaining
808bf215546Sopenharmony_ci * part of the program after the kill is large, since that would let us
809bf215546Sopenharmony_ci * skip over the instructions when there are no non-killed threads left.
810bf215546Sopenharmony_ci */
811bf215546Sopenharmony_cistatic void
812bf215546Sopenharmony_cikill_sched(struct ir3 *ir, struct ir3_shader_variant *so)
813bf215546Sopenharmony_ci{
814bf215546Sopenharmony_ci   /* True if we know that this block will always eventually lead to the end
815bf215546Sopenharmony_ci    * block:
816bf215546Sopenharmony_ci    */
817bf215546Sopenharmony_ci   bool always_ends = true;
818bf215546Sopenharmony_ci   bool added = false;
819bf215546Sopenharmony_ci   struct ir3_block *last_block =
820bf215546Sopenharmony_ci      list_last_entry(&ir->block_list, struct ir3_block, node);
821bf215546Sopenharmony_ci
822bf215546Sopenharmony_ci   foreach_block_rev (block, &ir->block_list) {
823bf215546Sopenharmony_ci      for (unsigned i = 0; i < 2 && block->successors[i]; i++) {
824bf215546Sopenharmony_ci         if (block->successors[i]->start_ip <= block->end_ip)
825bf215546Sopenharmony_ci            always_ends = false;
826bf215546Sopenharmony_ci      }
827bf215546Sopenharmony_ci
828bf215546Sopenharmony_ci      if (always_ends)
829bf215546Sopenharmony_ci         continue;
830bf215546Sopenharmony_ci
831bf215546Sopenharmony_ci      foreach_instr_safe (instr, &block->instr_list) {
832bf215546Sopenharmony_ci         if (instr->opc != OPC_KILL)
833bf215546Sopenharmony_ci            continue;
834bf215546Sopenharmony_ci
835bf215546Sopenharmony_ci         struct ir3_instruction *br = ir3_instr_create(block, OPC_B, 0, 1);
836bf215546Sopenharmony_ci         ir3_src_create(br, instr->srcs[0]->num, instr->srcs[0]->flags)->wrmask =
837bf215546Sopenharmony_ci            1;
838bf215546Sopenharmony_ci         br->cat0.target =
839bf215546Sopenharmony_ci            list_last_entry(&ir->block_list, struct ir3_block, node);
840bf215546Sopenharmony_ci
841bf215546Sopenharmony_ci         list_del(&br->node);
842bf215546Sopenharmony_ci         list_add(&br->node, &instr->node);
843bf215546Sopenharmony_ci
844bf215546Sopenharmony_ci         added = true;
845bf215546Sopenharmony_ci      }
846bf215546Sopenharmony_ci   }
847bf215546Sopenharmony_ci
848bf215546Sopenharmony_ci   if (added) {
849bf215546Sopenharmony_ci      /* I'm not entirely sure how the branchstack works, but we probably
850bf215546Sopenharmony_ci       * need to add at least one entry for the divergence which is resolved
851bf215546Sopenharmony_ci       * at the end:
852bf215546Sopenharmony_ci       */
853bf215546Sopenharmony_ci      so->branchstack++;
854bf215546Sopenharmony_ci
855bf215546Sopenharmony_ci      /* We don't update predecessors/successors, so we have to do this
856bf215546Sopenharmony_ci       * manually:
857bf215546Sopenharmony_ci       */
858bf215546Sopenharmony_ci      mark_jp(last_block);
859bf215546Sopenharmony_ci   }
860bf215546Sopenharmony_ci}
861bf215546Sopenharmony_ci
862bf215546Sopenharmony_ci/* Insert nop's required to make this a legal/valid shader program: */
863bf215546Sopenharmony_cistatic void
864bf215546Sopenharmony_cinop_sched(struct ir3 *ir, struct ir3_shader_variant *so)
865bf215546Sopenharmony_ci{
866bf215546Sopenharmony_ci   foreach_block (block, &ir->block_list) {
867bf215546Sopenharmony_ci      struct ir3_instruction *last = NULL;
868bf215546Sopenharmony_ci      struct list_head instr_list;
869bf215546Sopenharmony_ci
870bf215546Sopenharmony_ci      /* remove all the instructions from the list, we'll be adding
871bf215546Sopenharmony_ci       * them back in as we go
872bf215546Sopenharmony_ci       */
873bf215546Sopenharmony_ci      list_replace(&block->instr_list, &instr_list);
874bf215546Sopenharmony_ci      list_inithead(&block->instr_list);
875bf215546Sopenharmony_ci
876bf215546Sopenharmony_ci      foreach_instr_safe (instr, &instr_list) {
877bf215546Sopenharmony_ci         unsigned delay = ir3_delay_calc(block, instr, so->mergedregs);
878bf215546Sopenharmony_ci
879bf215546Sopenharmony_ci         /* NOTE: I think the nopN encoding works for a5xx and
880bf215546Sopenharmony_ci          * probably a4xx, but not a3xx.  So far only tested on
881bf215546Sopenharmony_ci          * a6xx.
882bf215546Sopenharmony_ci          */
883bf215546Sopenharmony_ci
884bf215546Sopenharmony_ci         if ((delay > 0) && (ir->compiler->gen >= 6) && last &&
885bf215546Sopenharmony_ci             ((opc_cat(last->opc) == 2) || (opc_cat(last->opc) == 3)) &&
886bf215546Sopenharmony_ci             (last->repeat == 0)) {
887bf215546Sopenharmony_ci            /* the previous cat2/cat3 instruction can encode at most 3 nop's: */
888bf215546Sopenharmony_ci            unsigned transfer = MIN2(delay, 3 - last->nop);
889bf215546Sopenharmony_ci            last->nop += transfer;
890bf215546Sopenharmony_ci            delay -= transfer;
891bf215546Sopenharmony_ci         }
892bf215546Sopenharmony_ci
893bf215546Sopenharmony_ci         if ((delay > 0) && last && (last->opc == OPC_NOP)) {
894bf215546Sopenharmony_ci            /* the previous nop can encode at most 5 repeats: */
895bf215546Sopenharmony_ci            unsigned transfer = MIN2(delay, 5 - last->repeat);
896bf215546Sopenharmony_ci            last->repeat += transfer;
897bf215546Sopenharmony_ci            delay -= transfer;
898bf215546Sopenharmony_ci         }
899bf215546Sopenharmony_ci
900bf215546Sopenharmony_ci         if (delay > 0) {
901bf215546Sopenharmony_ci            assert(delay <= 6);
902bf215546Sopenharmony_ci            ir3_NOP(block)->repeat = delay - 1;
903bf215546Sopenharmony_ci         }
904bf215546Sopenharmony_ci
905bf215546Sopenharmony_ci         list_addtail(&instr->node, &block->instr_list);
906bf215546Sopenharmony_ci         last = instr;
907bf215546Sopenharmony_ci      }
908bf215546Sopenharmony_ci   }
909bf215546Sopenharmony_ci}
910bf215546Sopenharmony_ci
911bf215546Sopenharmony_cibool
912bf215546Sopenharmony_ciir3_legalize(struct ir3 *ir, struct ir3_shader_variant *so, int *max_bary)
913bf215546Sopenharmony_ci{
914bf215546Sopenharmony_ci   struct ir3_legalize_ctx *ctx = rzalloc(ir, struct ir3_legalize_ctx);
915bf215546Sopenharmony_ci   bool mergedregs = so->mergedregs;
916bf215546Sopenharmony_ci   bool progress;
917bf215546Sopenharmony_ci
918bf215546Sopenharmony_ci   ctx->so = so;
919bf215546Sopenharmony_ci   ctx->max_bary = -1;
920bf215546Sopenharmony_ci   ctx->compiler = ir->compiler;
921bf215546Sopenharmony_ci   ctx->type = ir->type;
922bf215546Sopenharmony_ci
923bf215546Sopenharmony_ci   /* allocate per-block data: */
924bf215546Sopenharmony_ci   foreach_block (block, &ir->block_list) {
925bf215546Sopenharmony_ci      struct ir3_legalize_block_data *bd =
926bf215546Sopenharmony_ci         rzalloc(ctx, struct ir3_legalize_block_data);
927bf215546Sopenharmony_ci
928bf215546Sopenharmony_ci      regmask_init(&bd->state.needs_ss_war, mergedregs);
929bf215546Sopenharmony_ci      regmask_init(&bd->state.needs_ss, mergedregs);
930bf215546Sopenharmony_ci      regmask_init(&bd->state.needs_sy, mergedregs);
931bf215546Sopenharmony_ci
932bf215546Sopenharmony_ci      block->data = bd;
933bf215546Sopenharmony_ci   }
934bf215546Sopenharmony_ci
935bf215546Sopenharmony_ci   /* We may have failed to pull all input loads into the first block.
936bf215546Sopenharmony_ci    * In such case at the moment we aren't able to find a better place
937bf215546Sopenharmony_ci    * to for (ei) than the end of the program.
938bf215546Sopenharmony_ci    * a5xx and a6xx do automatically release varying storage at the end.
939bf215546Sopenharmony_ci    */
940bf215546Sopenharmony_ci   ctx->early_input_release = true;
941bf215546Sopenharmony_ci   struct ir3_block *start_block = ir3_after_preamble(ir);
942bf215546Sopenharmony_ci   foreach_block (block, &ir->block_list) {
943bf215546Sopenharmony_ci      foreach_instr (instr, &block->instr_list) {
944bf215546Sopenharmony_ci         if (is_input(instr)) {
945bf215546Sopenharmony_ci            ctx->has_inputs = true;
946bf215546Sopenharmony_ci            if (block != start_block) {
947bf215546Sopenharmony_ci               ctx->early_input_release = false;
948bf215546Sopenharmony_ci               break;
949bf215546Sopenharmony_ci            }
950bf215546Sopenharmony_ci         }
951bf215546Sopenharmony_ci      }
952bf215546Sopenharmony_ci   }
953bf215546Sopenharmony_ci
954bf215546Sopenharmony_ci   assert(ctx->early_input_release || ctx->compiler->gen >= 5);
955bf215546Sopenharmony_ci
956bf215546Sopenharmony_ci   /* process each block: */
957bf215546Sopenharmony_ci   do {
958bf215546Sopenharmony_ci      progress = false;
959bf215546Sopenharmony_ci      foreach_block (block, &ir->block_list) {
960bf215546Sopenharmony_ci         progress |= legalize_block(ctx, block);
961bf215546Sopenharmony_ci      }
962bf215546Sopenharmony_ci   } while (progress);
963bf215546Sopenharmony_ci
964bf215546Sopenharmony_ci   *max_bary = ctx->max_bary;
965bf215546Sopenharmony_ci
966bf215546Sopenharmony_ci   block_sched(ir);
967bf215546Sopenharmony_ci   if (so->type == MESA_SHADER_FRAGMENT)
968bf215546Sopenharmony_ci      kill_sched(ir, so);
969bf215546Sopenharmony_ci
970bf215546Sopenharmony_ci   foreach_block (block, &ir->block_list) {
971bf215546Sopenharmony_ci      progress |= apply_fine_deriv_macro(ctx, block);
972bf215546Sopenharmony_ci   }
973bf215546Sopenharmony_ci
974bf215546Sopenharmony_ci   nop_sched(ir, so);
975bf215546Sopenharmony_ci
976bf215546Sopenharmony_ci   while (opt_jump(ir))
977bf215546Sopenharmony_ci      ;
978bf215546Sopenharmony_ci
979bf215546Sopenharmony_ci   ir3_count_instructions(ir);
980bf215546Sopenharmony_ci   resolve_jumps(ir);
981bf215546Sopenharmony_ci
982bf215546Sopenharmony_ci   mark_xvergence_points(ir);
983bf215546Sopenharmony_ci
984bf215546Sopenharmony_ci   ralloc_free(ctx);
985bf215546Sopenharmony_ci
986bf215546Sopenharmony_ci   return true;
987bf215546Sopenharmony_ci}
988