1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2014 Intel 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 * Authors:
24bf215546Sopenharmony_ci *    Connor Abbott (cwabbott0@gmail.com)
25bf215546Sopenharmony_ci *
26bf215546Sopenharmony_ci */
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci#include "nir.h"
29bf215546Sopenharmony_ci#include "nir_builder.h"
30bf215546Sopenharmony_ci#include "nir_control_flow_private.h"
31bf215546Sopenharmony_ci#include "nir_worklist.h"
32bf215546Sopenharmony_ci#include "util/half_float.h"
33bf215546Sopenharmony_ci#include <limits.h>
34bf215546Sopenharmony_ci#include <assert.h>
35bf215546Sopenharmony_ci#include <math.h>
36bf215546Sopenharmony_ci#include "util/u_math.h"
37bf215546Sopenharmony_ci#include "util/u_qsort.h"
38bf215546Sopenharmony_ci
39bf215546Sopenharmony_ci#include "main/menums.h" /* BITFIELD64_MASK */
40bf215546Sopenharmony_ci
41bf215546Sopenharmony_ci#ifndef NDEBUG
42bf215546Sopenharmony_ciuint32_t nir_debug = 0;
43bf215546Sopenharmony_cibool nir_debug_print_shader[MESA_SHADER_KERNEL + 1] = { 0 };
44bf215546Sopenharmony_ci
45bf215546Sopenharmony_cistatic const struct debug_named_value nir_debug_control[] = {
46bf215546Sopenharmony_ci   { "clone", NIR_DEBUG_CLONE,
47bf215546Sopenharmony_ci     "Test cloning a shader at each successful lowering/optimization call" },
48bf215546Sopenharmony_ci   { "serialize", NIR_DEBUG_SERIALIZE,
49bf215546Sopenharmony_ci     "Test serialize and deserialize shader at each successful lowering/optimization call" },
50bf215546Sopenharmony_ci   { "novalidate", NIR_DEBUG_NOVALIDATE,
51bf215546Sopenharmony_ci     "Disable shader validation at each successful lowering/optimization call" },
52bf215546Sopenharmony_ci   { "validate_ssa_dominance", NIR_DEBUG_VALIDATE_SSA_DOMINANCE,
53bf215546Sopenharmony_ci     "Validate SSA dominance in shader at each successful lowering/optimization call" },
54bf215546Sopenharmony_ci   { "validate_gc_list", NIR_DEBUG_VALIDATE_GC_LIST,
55bf215546Sopenharmony_ci     "Validate the instruction GC list at each successful lowering/optimization call" },
56bf215546Sopenharmony_ci   { "tgsi", NIR_DEBUG_TGSI,
57bf215546Sopenharmony_ci     "Dump NIR/TGSI shaders when doing a NIR<->TGSI translation" },
58bf215546Sopenharmony_ci   { "print", NIR_DEBUG_PRINT,
59bf215546Sopenharmony_ci     "Dump resulting shader after each successful lowering/optimization call" },
60bf215546Sopenharmony_ci   { "print_vs", NIR_DEBUG_PRINT_VS,
61bf215546Sopenharmony_ci     "Dump resulting vertex shader after each successful lowering/optimization call" },
62bf215546Sopenharmony_ci   { "print_tcs", NIR_DEBUG_PRINT_TCS,
63bf215546Sopenharmony_ci     "Dump resulting tessellation control shader after each successful lowering/optimization call" },
64bf215546Sopenharmony_ci   { "print_tes", NIR_DEBUG_PRINT_TES,
65bf215546Sopenharmony_ci     "Dump resulting tessellation evaluation shader after each successful lowering/optimization call" },
66bf215546Sopenharmony_ci   { "print_gs", NIR_DEBUG_PRINT_GS,
67bf215546Sopenharmony_ci     "Dump resulting geometry shader after each successful lowering/optimization call" },
68bf215546Sopenharmony_ci   { "print_fs", NIR_DEBUG_PRINT_FS,
69bf215546Sopenharmony_ci     "Dump resulting fragment shader after each successful lowering/optimization call" },
70bf215546Sopenharmony_ci   { "print_cs", NIR_DEBUG_PRINT_CS,
71bf215546Sopenharmony_ci     "Dump resulting compute shader after each successful lowering/optimization call" },
72bf215546Sopenharmony_ci   { "print_ts", NIR_DEBUG_PRINT_TS,
73bf215546Sopenharmony_ci     "Dump resulting task shader after each successful lowering/optimization call" },
74bf215546Sopenharmony_ci   { "print_ms", NIR_DEBUG_PRINT_MS,
75bf215546Sopenharmony_ci     "Dump resulting mesh shader after each successful lowering/optimization call" },
76bf215546Sopenharmony_ci   { "print_rgs", NIR_DEBUG_PRINT_RGS,
77bf215546Sopenharmony_ci     "Dump resulting raygen shader after each successful lowering/optimization call" },
78bf215546Sopenharmony_ci   { "print_ahs", NIR_DEBUG_PRINT_AHS,
79bf215546Sopenharmony_ci     "Dump resulting any-hit shader after each successful lowering/optimization call" },
80bf215546Sopenharmony_ci   { "print_chs", NIR_DEBUG_PRINT_CHS,
81bf215546Sopenharmony_ci     "Dump resulting closest-hit shader after each successful lowering/optimization call" },
82bf215546Sopenharmony_ci   { "print_mhs", NIR_DEBUG_PRINT_MHS,
83bf215546Sopenharmony_ci     "Dump resulting miss-hit shader after each successful lowering/optimization call" },
84bf215546Sopenharmony_ci   { "print_is", NIR_DEBUG_PRINT_IS,
85bf215546Sopenharmony_ci     "Dump resulting intersection shader after each successful lowering/optimization call" },
86bf215546Sopenharmony_ci   { "print_cbs", NIR_DEBUG_PRINT_CBS,
87bf215546Sopenharmony_ci     "Dump resulting callable shader after each successful lowering/optimization call" },
88bf215546Sopenharmony_ci   { "print_ks", NIR_DEBUG_PRINT_KS,
89bf215546Sopenharmony_ci     "Dump resulting kernel shader after each successful lowering/optimization call" },
90bf215546Sopenharmony_ci   { "print_consts", NIR_DEBUG_PRINT_CONSTS,
91bf215546Sopenharmony_ci     "Print const value near each use of const SSA variable" },
92bf215546Sopenharmony_ci   { NULL }
93bf215546Sopenharmony_ci};
94bf215546Sopenharmony_ci
95bf215546Sopenharmony_ciDEBUG_GET_ONCE_FLAGS_OPTION(nir_debug, "NIR_DEBUG", nir_debug_control, 0)
96bf215546Sopenharmony_ci
97bf215546Sopenharmony_cistatic void
98bf215546Sopenharmony_cinir_process_debug_variable_once(void)
99bf215546Sopenharmony_ci{
100bf215546Sopenharmony_ci   nir_debug = debug_get_option_nir_debug();
101bf215546Sopenharmony_ci   nir_debug_print_shader[MESA_SHADER_VERTEX]       = NIR_DEBUG(PRINT_VS);
102bf215546Sopenharmony_ci   nir_debug_print_shader[MESA_SHADER_TESS_CTRL]    = NIR_DEBUG(PRINT_TCS);
103bf215546Sopenharmony_ci   nir_debug_print_shader[MESA_SHADER_TESS_EVAL]    = NIR_DEBUG(PRINT_TES);
104bf215546Sopenharmony_ci   nir_debug_print_shader[MESA_SHADER_GEOMETRY]     = NIR_DEBUG(PRINT_GS);
105bf215546Sopenharmony_ci   nir_debug_print_shader[MESA_SHADER_FRAGMENT]     = NIR_DEBUG(PRINT_FS);
106bf215546Sopenharmony_ci   nir_debug_print_shader[MESA_SHADER_COMPUTE]      = NIR_DEBUG(PRINT_CS);
107bf215546Sopenharmony_ci   nir_debug_print_shader[MESA_SHADER_TASK]         = NIR_DEBUG(PRINT_TS);
108bf215546Sopenharmony_ci   nir_debug_print_shader[MESA_SHADER_MESH]         = NIR_DEBUG(PRINT_MS);
109bf215546Sopenharmony_ci   nir_debug_print_shader[MESA_SHADER_RAYGEN]       = NIR_DEBUG(PRINT_RGS);
110bf215546Sopenharmony_ci   nir_debug_print_shader[MESA_SHADER_ANY_HIT]      = NIR_DEBUG(PRINT_AHS);
111bf215546Sopenharmony_ci   nir_debug_print_shader[MESA_SHADER_CLOSEST_HIT]  = NIR_DEBUG(PRINT_CHS);
112bf215546Sopenharmony_ci   nir_debug_print_shader[MESA_SHADER_MISS]         = NIR_DEBUG(PRINT_MHS);
113bf215546Sopenharmony_ci   nir_debug_print_shader[MESA_SHADER_INTERSECTION] = NIR_DEBUG(PRINT_IS);
114bf215546Sopenharmony_ci   nir_debug_print_shader[MESA_SHADER_CALLABLE]     = NIR_DEBUG(PRINT_CBS);
115bf215546Sopenharmony_ci   nir_debug_print_shader[MESA_SHADER_KERNEL]       = NIR_DEBUG(PRINT_KS);
116bf215546Sopenharmony_ci}
117bf215546Sopenharmony_ci
118bf215546Sopenharmony_civoid
119bf215546Sopenharmony_cinir_process_debug_variable(void)
120bf215546Sopenharmony_ci{
121bf215546Sopenharmony_ci   static once_flag flag = ONCE_FLAG_INIT;
122bf215546Sopenharmony_ci   call_once(&flag, nir_process_debug_variable_once);
123bf215546Sopenharmony_ci}
124bf215546Sopenharmony_ci#endif
125bf215546Sopenharmony_ci
126bf215546Sopenharmony_ci/** Return true if the component mask "mask" with bit size "old_bit_size" can
127bf215546Sopenharmony_ci * be re-interpreted to be used with "new_bit_size".
128bf215546Sopenharmony_ci */
129bf215546Sopenharmony_cibool
130bf215546Sopenharmony_cinir_component_mask_can_reinterpret(nir_component_mask_t mask,
131bf215546Sopenharmony_ci                                   unsigned old_bit_size,
132bf215546Sopenharmony_ci                                   unsigned new_bit_size)
133bf215546Sopenharmony_ci{
134bf215546Sopenharmony_ci   assert(util_is_power_of_two_nonzero(old_bit_size));
135bf215546Sopenharmony_ci   assert(util_is_power_of_two_nonzero(new_bit_size));
136bf215546Sopenharmony_ci
137bf215546Sopenharmony_ci   if (old_bit_size == new_bit_size)
138bf215546Sopenharmony_ci      return true;
139bf215546Sopenharmony_ci
140bf215546Sopenharmony_ci   if (old_bit_size == 1 || new_bit_size == 1)
141bf215546Sopenharmony_ci      return false;
142bf215546Sopenharmony_ci
143bf215546Sopenharmony_ci   if (old_bit_size > new_bit_size) {
144bf215546Sopenharmony_ci      unsigned ratio = old_bit_size / new_bit_size;
145bf215546Sopenharmony_ci      return util_last_bit(mask) * ratio <= NIR_MAX_VEC_COMPONENTS;
146bf215546Sopenharmony_ci   }
147bf215546Sopenharmony_ci
148bf215546Sopenharmony_ci   unsigned iter = mask;
149bf215546Sopenharmony_ci   while (iter) {
150bf215546Sopenharmony_ci      int start, count;
151bf215546Sopenharmony_ci      u_bit_scan_consecutive_range(&iter, &start, &count);
152bf215546Sopenharmony_ci      start *= old_bit_size;
153bf215546Sopenharmony_ci      count *= old_bit_size;
154bf215546Sopenharmony_ci      if (start % new_bit_size != 0)
155bf215546Sopenharmony_ci         return false;
156bf215546Sopenharmony_ci      if (count % new_bit_size != 0)
157bf215546Sopenharmony_ci         return false;
158bf215546Sopenharmony_ci   }
159bf215546Sopenharmony_ci   return true;
160bf215546Sopenharmony_ci}
161bf215546Sopenharmony_ci
162bf215546Sopenharmony_ci/** Re-interprets a component mask "mask" with bit size "old_bit_size" so that
163bf215546Sopenharmony_ci * it can be used can be used with "new_bit_size".
164bf215546Sopenharmony_ci */
165bf215546Sopenharmony_cinir_component_mask_t
166bf215546Sopenharmony_cinir_component_mask_reinterpret(nir_component_mask_t mask,
167bf215546Sopenharmony_ci                               unsigned old_bit_size,
168bf215546Sopenharmony_ci                               unsigned new_bit_size)
169bf215546Sopenharmony_ci{
170bf215546Sopenharmony_ci   assert(nir_component_mask_can_reinterpret(mask, old_bit_size, new_bit_size));
171bf215546Sopenharmony_ci
172bf215546Sopenharmony_ci   if (old_bit_size == new_bit_size)
173bf215546Sopenharmony_ci      return mask;
174bf215546Sopenharmony_ci
175bf215546Sopenharmony_ci   nir_component_mask_t new_mask = 0;
176bf215546Sopenharmony_ci   unsigned iter = mask;
177bf215546Sopenharmony_ci   while (iter) {
178bf215546Sopenharmony_ci      int start, count;
179bf215546Sopenharmony_ci      u_bit_scan_consecutive_range(&iter, &start, &count);
180bf215546Sopenharmony_ci      start = start * old_bit_size / new_bit_size;
181bf215546Sopenharmony_ci      count = count * old_bit_size / new_bit_size;
182bf215546Sopenharmony_ci      new_mask |= BITFIELD_RANGE(start, count);
183bf215546Sopenharmony_ci   }
184bf215546Sopenharmony_ci   return new_mask;
185bf215546Sopenharmony_ci}
186bf215546Sopenharmony_ci
187bf215546Sopenharmony_cistatic void
188bf215546Sopenharmony_cinir_shader_destructor(void *ptr)
189bf215546Sopenharmony_ci{
190bf215546Sopenharmony_ci   nir_shader *shader = ptr;
191bf215546Sopenharmony_ci
192bf215546Sopenharmony_ci   /* Free all instrs from the shader, since they're not ralloced. */
193bf215546Sopenharmony_ci   list_for_each_entry_safe(nir_instr, instr, &shader->gc_list, gc_node) {
194bf215546Sopenharmony_ci      nir_instr_free(instr);
195bf215546Sopenharmony_ci   }
196bf215546Sopenharmony_ci}
197bf215546Sopenharmony_ci
198bf215546Sopenharmony_cinir_shader *
199bf215546Sopenharmony_cinir_shader_create(void *mem_ctx,
200bf215546Sopenharmony_ci                  gl_shader_stage stage,
201bf215546Sopenharmony_ci                  const nir_shader_compiler_options *options,
202bf215546Sopenharmony_ci                  shader_info *si)
203bf215546Sopenharmony_ci{
204bf215546Sopenharmony_ci   nir_shader *shader = rzalloc(mem_ctx, nir_shader);
205bf215546Sopenharmony_ci   ralloc_set_destructor(shader, nir_shader_destructor);
206bf215546Sopenharmony_ci
207bf215546Sopenharmony_ci#ifndef NDEBUG
208bf215546Sopenharmony_ci   nir_process_debug_variable();
209bf215546Sopenharmony_ci#endif
210bf215546Sopenharmony_ci
211bf215546Sopenharmony_ci   exec_list_make_empty(&shader->variables);
212bf215546Sopenharmony_ci
213bf215546Sopenharmony_ci   shader->options = options;
214bf215546Sopenharmony_ci
215bf215546Sopenharmony_ci   if (si) {
216bf215546Sopenharmony_ci      assert(si->stage == stage);
217bf215546Sopenharmony_ci      shader->info = *si;
218bf215546Sopenharmony_ci   } else {
219bf215546Sopenharmony_ci      shader->info.stage = stage;
220bf215546Sopenharmony_ci   }
221bf215546Sopenharmony_ci
222bf215546Sopenharmony_ci   exec_list_make_empty(&shader->functions);
223bf215546Sopenharmony_ci
224bf215546Sopenharmony_ci   list_inithead(&shader->gc_list);
225bf215546Sopenharmony_ci
226bf215546Sopenharmony_ci   shader->num_inputs = 0;
227bf215546Sopenharmony_ci   shader->num_outputs = 0;
228bf215546Sopenharmony_ci   shader->num_uniforms = 0;
229bf215546Sopenharmony_ci
230bf215546Sopenharmony_ci   return shader;
231bf215546Sopenharmony_ci}
232bf215546Sopenharmony_ci
233bf215546Sopenharmony_cistatic nir_register *
234bf215546Sopenharmony_cireg_create(void *mem_ctx, struct exec_list *list)
235bf215546Sopenharmony_ci{
236bf215546Sopenharmony_ci   nir_register *reg = ralloc(mem_ctx, nir_register);
237bf215546Sopenharmony_ci
238bf215546Sopenharmony_ci   list_inithead(&reg->uses);
239bf215546Sopenharmony_ci   list_inithead(&reg->defs);
240bf215546Sopenharmony_ci   list_inithead(&reg->if_uses);
241bf215546Sopenharmony_ci
242bf215546Sopenharmony_ci   reg->num_components = 0;
243bf215546Sopenharmony_ci   reg->bit_size = 32;
244bf215546Sopenharmony_ci   reg->num_array_elems = 0;
245bf215546Sopenharmony_ci   reg->divergent = false;
246bf215546Sopenharmony_ci
247bf215546Sopenharmony_ci   exec_list_push_tail(list, &reg->node);
248bf215546Sopenharmony_ci
249bf215546Sopenharmony_ci   return reg;
250bf215546Sopenharmony_ci}
251bf215546Sopenharmony_ci
252bf215546Sopenharmony_cinir_register *
253bf215546Sopenharmony_cinir_local_reg_create(nir_function_impl *impl)
254bf215546Sopenharmony_ci{
255bf215546Sopenharmony_ci   nir_register *reg = reg_create(ralloc_parent(impl), &impl->registers);
256bf215546Sopenharmony_ci   reg->index = impl->reg_alloc++;
257bf215546Sopenharmony_ci
258bf215546Sopenharmony_ci   return reg;
259bf215546Sopenharmony_ci}
260bf215546Sopenharmony_ci
261bf215546Sopenharmony_civoid
262bf215546Sopenharmony_cinir_reg_remove(nir_register *reg)
263bf215546Sopenharmony_ci{
264bf215546Sopenharmony_ci   exec_node_remove(&reg->node);
265bf215546Sopenharmony_ci}
266bf215546Sopenharmony_ci
267bf215546Sopenharmony_civoid
268bf215546Sopenharmony_cinir_shader_add_variable(nir_shader *shader, nir_variable *var)
269bf215546Sopenharmony_ci{
270bf215546Sopenharmony_ci   switch (var->data.mode) {
271bf215546Sopenharmony_ci   case nir_var_function_temp:
272bf215546Sopenharmony_ci      assert(!"nir_shader_add_variable cannot be used for local variables");
273bf215546Sopenharmony_ci      return;
274bf215546Sopenharmony_ci
275bf215546Sopenharmony_ci   case nir_var_shader_temp:
276bf215546Sopenharmony_ci   case nir_var_shader_in:
277bf215546Sopenharmony_ci   case nir_var_shader_out:
278bf215546Sopenharmony_ci   case nir_var_uniform:
279bf215546Sopenharmony_ci   case nir_var_mem_ubo:
280bf215546Sopenharmony_ci   case nir_var_mem_ssbo:
281bf215546Sopenharmony_ci   case nir_var_image:
282bf215546Sopenharmony_ci   case nir_var_mem_shared:
283bf215546Sopenharmony_ci   case nir_var_system_value:
284bf215546Sopenharmony_ci   case nir_var_mem_push_const:
285bf215546Sopenharmony_ci   case nir_var_mem_constant:
286bf215546Sopenharmony_ci   case nir_var_shader_call_data:
287bf215546Sopenharmony_ci   case nir_var_ray_hit_attrib:
288bf215546Sopenharmony_ci   case nir_var_mem_task_payload:
289bf215546Sopenharmony_ci   case nir_var_mem_global:
290bf215546Sopenharmony_ci      break;
291bf215546Sopenharmony_ci
292bf215546Sopenharmony_ci   default:
293bf215546Sopenharmony_ci      assert(!"invalid mode");
294bf215546Sopenharmony_ci      return;
295bf215546Sopenharmony_ci   }
296bf215546Sopenharmony_ci
297bf215546Sopenharmony_ci   exec_list_push_tail(&shader->variables, &var->node);
298bf215546Sopenharmony_ci}
299bf215546Sopenharmony_ci
300bf215546Sopenharmony_cinir_variable *
301bf215546Sopenharmony_cinir_variable_create(nir_shader *shader, nir_variable_mode mode,
302bf215546Sopenharmony_ci                    const struct glsl_type *type, const char *name)
303bf215546Sopenharmony_ci{
304bf215546Sopenharmony_ci   nir_variable *var = rzalloc(shader, nir_variable);
305bf215546Sopenharmony_ci   var->name = ralloc_strdup(var, name);
306bf215546Sopenharmony_ci   var->type = type;
307bf215546Sopenharmony_ci   var->data.mode = mode;
308bf215546Sopenharmony_ci   var->data.how_declared = nir_var_declared_normally;
309bf215546Sopenharmony_ci
310bf215546Sopenharmony_ci   if ((mode == nir_var_shader_in &&
311bf215546Sopenharmony_ci        shader->info.stage != MESA_SHADER_VERTEX &&
312bf215546Sopenharmony_ci        shader->info.stage != MESA_SHADER_KERNEL) ||
313bf215546Sopenharmony_ci       (mode == nir_var_shader_out &&
314bf215546Sopenharmony_ci        shader->info.stage != MESA_SHADER_FRAGMENT))
315bf215546Sopenharmony_ci      var->data.interpolation = INTERP_MODE_SMOOTH;
316bf215546Sopenharmony_ci
317bf215546Sopenharmony_ci   if (mode == nir_var_shader_in || mode == nir_var_uniform)
318bf215546Sopenharmony_ci      var->data.read_only = true;
319bf215546Sopenharmony_ci
320bf215546Sopenharmony_ci   nir_shader_add_variable(shader, var);
321bf215546Sopenharmony_ci
322bf215546Sopenharmony_ci   return var;
323bf215546Sopenharmony_ci}
324bf215546Sopenharmony_ci
325bf215546Sopenharmony_cinir_variable *
326bf215546Sopenharmony_cinir_local_variable_create(nir_function_impl *impl,
327bf215546Sopenharmony_ci                          const struct glsl_type *type, const char *name)
328bf215546Sopenharmony_ci{
329bf215546Sopenharmony_ci   nir_variable *var = rzalloc(impl->function->shader, nir_variable);
330bf215546Sopenharmony_ci   var->name = ralloc_strdup(var, name);
331bf215546Sopenharmony_ci   var->type = type;
332bf215546Sopenharmony_ci   var->data.mode = nir_var_function_temp;
333bf215546Sopenharmony_ci
334bf215546Sopenharmony_ci   nir_function_impl_add_variable(impl, var);
335bf215546Sopenharmony_ci
336bf215546Sopenharmony_ci   return var;
337bf215546Sopenharmony_ci}
338bf215546Sopenharmony_ci
339bf215546Sopenharmony_cinir_variable *
340bf215546Sopenharmony_cinir_find_variable_with_location(nir_shader *shader,
341bf215546Sopenharmony_ci                                nir_variable_mode mode,
342bf215546Sopenharmony_ci                                unsigned location)
343bf215546Sopenharmony_ci{
344bf215546Sopenharmony_ci   assert(util_bitcount(mode) == 1 && mode != nir_var_function_temp);
345bf215546Sopenharmony_ci   nir_foreach_variable_with_modes(var, shader, mode) {
346bf215546Sopenharmony_ci      if (var->data.location == location)
347bf215546Sopenharmony_ci         return var;
348bf215546Sopenharmony_ci   }
349bf215546Sopenharmony_ci   return NULL;
350bf215546Sopenharmony_ci}
351bf215546Sopenharmony_ci
352bf215546Sopenharmony_cinir_variable *
353bf215546Sopenharmony_cinir_find_variable_with_driver_location(nir_shader *shader,
354bf215546Sopenharmony_ci                                       nir_variable_mode mode,
355bf215546Sopenharmony_ci                                       unsigned location)
356bf215546Sopenharmony_ci{
357bf215546Sopenharmony_ci   assert(util_bitcount(mode) == 1 && mode != nir_var_function_temp);
358bf215546Sopenharmony_ci   nir_foreach_variable_with_modes(var, shader, mode) {
359bf215546Sopenharmony_ci      if (var->data.driver_location == location)
360bf215546Sopenharmony_ci         return var;
361bf215546Sopenharmony_ci   }
362bf215546Sopenharmony_ci   return NULL;
363bf215546Sopenharmony_ci}
364bf215546Sopenharmony_ci
365bf215546Sopenharmony_ci/* Annoyingly, qsort_r is not in the C standard library and, in particular, we
366bf215546Sopenharmony_ci * can't count on it on MSV and Android.  So we stuff the CMP function into
367bf215546Sopenharmony_ci * each array element.  It's a bit messy and burns more memory but the list of
368bf215546Sopenharmony_ci * variables should hever be all that long.
369bf215546Sopenharmony_ci */
370bf215546Sopenharmony_cistruct var_cmp {
371bf215546Sopenharmony_ci   nir_variable *var;
372bf215546Sopenharmony_ci   int (*cmp)(const nir_variable *, const nir_variable *);
373bf215546Sopenharmony_ci};
374bf215546Sopenharmony_ci
375bf215546Sopenharmony_cistatic int
376bf215546Sopenharmony_civar_sort_cmp(const void *_a, const void *_b, void *_cmp)
377bf215546Sopenharmony_ci{
378bf215546Sopenharmony_ci   const struct var_cmp *a = _a;
379bf215546Sopenharmony_ci   const struct var_cmp *b = _b;
380bf215546Sopenharmony_ci   assert(a->cmp == b->cmp);
381bf215546Sopenharmony_ci   return a->cmp(a->var, b->var);
382bf215546Sopenharmony_ci}
383bf215546Sopenharmony_ci
384bf215546Sopenharmony_civoid
385bf215546Sopenharmony_cinir_sort_variables_with_modes(nir_shader *shader,
386bf215546Sopenharmony_ci                              int (*cmp)(const nir_variable *,
387bf215546Sopenharmony_ci                                         const nir_variable *),
388bf215546Sopenharmony_ci                              nir_variable_mode modes)
389bf215546Sopenharmony_ci{
390bf215546Sopenharmony_ci   unsigned num_vars = 0;
391bf215546Sopenharmony_ci   nir_foreach_variable_with_modes(var, shader, modes) {
392bf215546Sopenharmony_ci      ++num_vars;
393bf215546Sopenharmony_ci   }
394bf215546Sopenharmony_ci   struct var_cmp *vars = ralloc_array(shader, struct var_cmp, num_vars);
395bf215546Sopenharmony_ci   unsigned i = 0;
396bf215546Sopenharmony_ci   nir_foreach_variable_with_modes_safe(var, shader, modes) {
397bf215546Sopenharmony_ci      exec_node_remove(&var->node);
398bf215546Sopenharmony_ci      vars[i++] = (struct var_cmp){
399bf215546Sopenharmony_ci         .var = var,
400bf215546Sopenharmony_ci         .cmp = cmp,
401bf215546Sopenharmony_ci      };
402bf215546Sopenharmony_ci   }
403bf215546Sopenharmony_ci   assert(i == num_vars);
404bf215546Sopenharmony_ci
405bf215546Sopenharmony_ci   util_qsort_r(vars, num_vars, sizeof(*vars), var_sort_cmp, cmp);
406bf215546Sopenharmony_ci
407bf215546Sopenharmony_ci   for (i = 0; i < num_vars; i++)
408bf215546Sopenharmony_ci      exec_list_push_tail(&shader->variables, &vars[i].var->node);
409bf215546Sopenharmony_ci
410bf215546Sopenharmony_ci   ralloc_free(vars);
411bf215546Sopenharmony_ci}
412bf215546Sopenharmony_ci
413bf215546Sopenharmony_cinir_function *
414bf215546Sopenharmony_cinir_function_create(nir_shader *shader, const char *name)
415bf215546Sopenharmony_ci{
416bf215546Sopenharmony_ci   nir_function *func = ralloc(shader, nir_function);
417bf215546Sopenharmony_ci
418bf215546Sopenharmony_ci   exec_list_push_tail(&shader->functions, &func->node);
419bf215546Sopenharmony_ci
420bf215546Sopenharmony_ci   func->name = ralloc_strdup(func, name);
421bf215546Sopenharmony_ci   func->shader = shader;
422bf215546Sopenharmony_ci   func->num_params = 0;
423bf215546Sopenharmony_ci   func->params = NULL;
424bf215546Sopenharmony_ci   func->impl = NULL;
425bf215546Sopenharmony_ci   func->is_entrypoint = false;
426bf215546Sopenharmony_ci   func->is_preamble = false;
427bf215546Sopenharmony_ci
428bf215546Sopenharmony_ci   return func;
429bf215546Sopenharmony_ci}
430bf215546Sopenharmony_ci
431bf215546Sopenharmony_cistatic bool src_has_indirect(nir_src *src)
432bf215546Sopenharmony_ci{
433bf215546Sopenharmony_ci   return !src->is_ssa && src->reg.indirect;
434bf215546Sopenharmony_ci}
435bf215546Sopenharmony_ci
436bf215546Sopenharmony_cistatic void src_free_indirects(nir_src *src)
437bf215546Sopenharmony_ci{
438bf215546Sopenharmony_ci   if (src_has_indirect(src)) {
439bf215546Sopenharmony_ci      assert(src->reg.indirect->is_ssa || !src->reg.indirect->reg.indirect);
440bf215546Sopenharmony_ci      free(src->reg.indirect);
441bf215546Sopenharmony_ci      src->reg.indirect = NULL;
442bf215546Sopenharmony_ci   }
443bf215546Sopenharmony_ci}
444bf215546Sopenharmony_ci
445bf215546Sopenharmony_cistatic void dest_free_indirects(nir_dest *dest)
446bf215546Sopenharmony_ci{
447bf215546Sopenharmony_ci   if (!dest->is_ssa && dest->reg.indirect) {
448bf215546Sopenharmony_ci      assert(dest->reg.indirect->is_ssa || !dest->reg.indirect->reg.indirect);
449bf215546Sopenharmony_ci      free(dest->reg.indirect);
450bf215546Sopenharmony_ci      dest->reg.indirect = NULL;
451bf215546Sopenharmony_ci   }
452bf215546Sopenharmony_ci}
453bf215546Sopenharmony_ci
454bf215546Sopenharmony_ci/* NOTE: if the instruction you are copying a src to is already added
455bf215546Sopenharmony_ci * to the IR, use nir_instr_rewrite_src() instead.
456bf215546Sopenharmony_ci */
457bf215546Sopenharmony_civoid nir_src_copy(nir_src *dest, const nir_src *src)
458bf215546Sopenharmony_ci{
459bf215546Sopenharmony_ci   src_free_indirects(dest);
460bf215546Sopenharmony_ci
461bf215546Sopenharmony_ci   dest->is_ssa = src->is_ssa;
462bf215546Sopenharmony_ci   if (src->is_ssa) {
463bf215546Sopenharmony_ci      dest->ssa = src->ssa;
464bf215546Sopenharmony_ci   } else {
465bf215546Sopenharmony_ci      dest->reg.base_offset = src->reg.base_offset;
466bf215546Sopenharmony_ci      dest->reg.reg = src->reg.reg;
467bf215546Sopenharmony_ci      if (src->reg.indirect) {
468bf215546Sopenharmony_ci         dest->reg.indirect = calloc(1, sizeof(nir_src));
469bf215546Sopenharmony_ci         nir_src_copy(dest->reg.indirect, src->reg.indirect);
470bf215546Sopenharmony_ci      } else {
471bf215546Sopenharmony_ci         dest->reg.indirect = NULL;
472bf215546Sopenharmony_ci      }
473bf215546Sopenharmony_ci   }
474bf215546Sopenharmony_ci}
475bf215546Sopenharmony_ci
476bf215546Sopenharmony_civoid nir_dest_copy(nir_dest *dest, const nir_dest *src)
477bf215546Sopenharmony_ci{
478bf215546Sopenharmony_ci   /* Copying an SSA definition makes no sense whatsoever. */
479bf215546Sopenharmony_ci   assert(!src->is_ssa);
480bf215546Sopenharmony_ci
481bf215546Sopenharmony_ci   dest_free_indirects(dest);
482bf215546Sopenharmony_ci
483bf215546Sopenharmony_ci   dest->is_ssa = false;
484bf215546Sopenharmony_ci
485bf215546Sopenharmony_ci   dest->reg.base_offset = src->reg.base_offset;
486bf215546Sopenharmony_ci   dest->reg.reg = src->reg.reg;
487bf215546Sopenharmony_ci   if (src->reg.indirect) {
488bf215546Sopenharmony_ci      dest->reg.indirect = calloc(1, sizeof(nir_src));
489bf215546Sopenharmony_ci      nir_src_copy(dest->reg.indirect, src->reg.indirect);
490bf215546Sopenharmony_ci   } else {
491bf215546Sopenharmony_ci      dest->reg.indirect = NULL;
492bf215546Sopenharmony_ci   }
493bf215546Sopenharmony_ci}
494bf215546Sopenharmony_ci
495bf215546Sopenharmony_civoid
496bf215546Sopenharmony_cinir_alu_src_copy(nir_alu_src *dest, const nir_alu_src *src)
497bf215546Sopenharmony_ci{
498bf215546Sopenharmony_ci   nir_src_copy(&dest->src, &src->src);
499bf215546Sopenharmony_ci   dest->abs = src->abs;
500bf215546Sopenharmony_ci   dest->negate = src->negate;
501bf215546Sopenharmony_ci   for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++)
502bf215546Sopenharmony_ci      dest->swizzle[i] = src->swizzle[i];
503bf215546Sopenharmony_ci}
504bf215546Sopenharmony_ci
505bf215546Sopenharmony_civoid
506bf215546Sopenharmony_cinir_alu_dest_copy(nir_alu_dest *dest, const nir_alu_dest *src)
507bf215546Sopenharmony_ci{
508bf215546Sopenharmony_ci   nir_dest_copy(&dest->dest, &src->dest);
509bf215546Sopenharmony_ci   dest->write_mask = src->write_mask;
510bf215546Sopenharmony_ci   dest->saturate = src->saturate;
511bf215546Sopenharmony_ci}
512bf215546Sopenharmony_ci
513bf215546Sopenharmony_cibool
514bf215546Sopenharmony_cinir_alu_src_is_trivial_ssa(const nir_alu_instr *alu, unsigned srcn)
515bf215546Sopenharmony_ci{
516bf215546Sopenharmony_ci   static uint8_t trivial_swizzle[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
517bf215546Sopenharmony_ci   STATIC_ASSERT(ARRAY_SIZE(trivial_swizzle) == NIR_MAX_VEC_COMPONENTS);
518bf215546Sopenharmony_ci
519bf215546Sopenharmony_ci   const nir_alu_src *src = &alu->src[srcn];
520bf215546Sopenharmony_ci   unsigned num_components = nir_ssa_alu_instr_src_components(alu, srcn);
521bf215546Sopenharmony_ci
522bf215546Sopenharmony_ci   return src->src.is_ssa && (src->src.ssa->num_components == num_components) &&
523bf215546Sopenharmony_ci          !src->abs && !src->negate &&
524bf215546Sopenharmony_ci          (memcmp(src->swizzle, trivial_swizzle, num_components) == 0);
525bf215546Sopenharmony_ci}
526bf215546Sopenharmony_ci
527bf215546Sopenharmony_ci
528bf215546Sopenharmony_cistatic void
529bf215546Sopenharmony_cicf_init(nir_cf_node *node, nir_cf_node_type type)
530bf215546Sopenharmony_ci{
531bf215546Sopenharmony_ci   exec_node_init(&node->node);
532bf215546Sopenharmony_ci   node->parent = NULL;
533bf215546Sopenharmony_ci   node->type = type;
534bf215546Sopenharmony_ci}
535bf215546Sopenharmony_ci
536bf215546Sopenharmony_cinir_function_impl *
537bf215546Sopenharmony_cinir_function_impl_create_bare(nir_shader *shader)
538bf215546Sopenharmony_ci{
539bf215546Sopenharmony_ci   nir_function_impl *impl = ralloc(shader, nir_function_impl);
540bf215546Sopenharmony_ci
541bf215546Sopenharmony_ci   impl->function = NULL;
542bf215546Sopenharmony_ci   impl->preamble = NULL;
543bf215546Sopenharmony_ci
544bf215546Sopenharmony_ci   cf_init(&impl->cf_node, nir_cf_node_function);
545bf215546Sopenharmony_ci
546bf215546Sopenharmony_ci   exec_list_make_empty(&impl->body);
547bf215546Sopenharmony_ci   exec_list_make_empty(&impl->registers);
548bf215546Sopenharmony_ci   exec_list_make_empty(&impl->locals);
549bf215546Sopenharmony_ci   impl->reg_alloc = 0;
550bf215546Sopenharmony_ci   impl->ssa_alloc = 0;
551bf215546Sopenharmony_ci   impl->num_blocks = 0;
552bf215546Sopenharmony_ci   impl->valid_metadata = nir_metadata_none;
553bf215546Sopenharmony_ci   impl->structured = true;
554bf215546Sopenharmony_ci
555bf215546Sopenharmony_ci   /* create start & end blocks */
556bf215546Sopenharmony_ci   nir_block *start_block = nir_block_create(shader);
557bf215546Sopenharmony_ci   nir_block *end_block = nir_block_create(shader);
558bf215546Sopenharmony_ci   start_block->cf_node.parent = &impl->cf_node;
559bf215546Sopenharmony_ci   end_block->cf_node.parent = &impl->cf_node;
560bf215546Sopenharmony_ci   impl->end_block = end_block;
561bf215546Sopenharmony_ci
562bf215546Sopenharmony_ci   exec_list_push_tail(&impl->body, &start_block->cf_node.node);
563bf215546Sopenharmony_ci
564bf215546Sopenharmony_ci   start_block->successors[0] = end_block;
565bf215546Sopenharmony_ci   _mesa_set_add(end_block->predecessors, start_block);
566bf215546Sopenharmony_ci   return impl;
567bf215546Sopenharmony_ci}
568bf215546Sopenharmony_ci
569bf215546Sopenharmony_cinir_function_impl *
570bf215546Sopenharmony_cinir_function_impl_create(nir_function *function)
571bf215546Sopenharmony_ci{
572bf215546Sopenharmony_ci   assert(function->impl == NULL);
573bf215546Sopenharmony_ci
574bf215546Sopenharmony_ci   nir_function_impl *impl = nir_function_impl_create_bare(function->shader);
575bf215546Sopenharmony_ci
576bf215546Sopenharmony_ci   function->impl = impl;
577bf215546Sopenharmony_ci   impl->function = function;
578bf215546Sopenharmony_ci
579bf215546Sopenharmony_ci   return impl;
580bf215546Sopenharmony_ci}
581bf215546Sopenharmony_ci
582bf215546Sopenharmony_cinir_block *
583bf215546Sopenharmony_cinir_block_create(nir_shader *shader)
584bf215546Sopenharmony_ci{
585bf215546Sopenharmony_ci   nir_block *block = rzalloc(shader, nir_block);
586bf215546Sopenharmony_ci
587bf215546Sopenharmony_ci   cf_init(&block->cf_node, nir_cf_node_block);
588bf215546Sopenharmony_ci
589bf215546Sopenharmony_ci   block->successors[0] = block->successors[1] = NULL;
590bf215546Sopenharmony_ci   block->predecessors = _mesa_pointer_set_create(block);
591bf215546Sopenharmony_ci   block->imm_dom = NULL;
592bf215546Sopenharmony_ci   /* XXX maybe it would be worth it to defer allocation?  This
593bf215546Sopenharmony_ci    * way it doesn't get allocated for shader refs that never run
594bf215546Sopenharmony_ci    * nir_calc_dominance?  For example, state-tracker creates an
595bf215546Sopenharmony_ci    * initial IR, clones that, runs appropriate lowering pass, passes
596bf215546Sopenharmony_ci    * to driver which does common lowering/opt, and then stores ref
597bf215546Sopenharmony_ci    * which is later used to do state specific lowering and futher
598bf215546Sopenharmony_ci    * opt.  Do any of the references not need dominance metadata?
599bf215546Sopenharmony_ci    */
600bf215546Sopenharmony_ci   block->dom_frontier = _mesa_pointer_set_create(block);
601bf215546Sopenharmony_ci
602bf215546Sopenharmony_ci   exec_list_make_empty(&block->instr_list);
603bf215546Sopenharmony_ci
604bf215546Sopenharmony_ci   return block;
605bf215546Sopenharmony_ci}
606bf215546Sopenharmony_ci
607bf215546Sopenharmony_cistatic inline void
608bf215546Sopenharmony_cisrc_init(nir_src *src)
609bf215546Sopenharmony_ci{
610bf215546Sopenharmony_ci   src->is_ssa = false;
611bf215546Sopenharmony_ci   src->reg.reg = NULL;
612bf215546Sopenharmony_ci   src->reg.indirect = NULL;
613bf215546Sopenharmony_ci   src->reg.base_offset = 0;
614bf215546Sopenharmony_ci}
615bf215546Sopenharmony_ci
616bf215546Sopenharmony_cinir_if *
617bf215546Sopenharmony_cinir_if_create(nir_shader *shader)
618bf215546Sopenharmony_ci{
619bf215546Sopenharmony_ci   nir_if *if_stmt = ralloc(shader, nir_if);
620bf215546Sopenharmony_ci
621bf215546Sopenharmony_ci   if_stmt->control = nir_selection_control_none;
622bf215546Sopenharmony_ci
623bf215546Sopenharmony_ci   cf_init(&if_stmt->cf_node, nir_cf_node_if);
624bf215546Sopenharmony_ci   src_init(&if_stmt->condition);
625bf215546Sopenharmony_ci
626bf215546Sopenharmony_ci   nir_block *then = nir_block_create(shader);
627bf215546Sopenharmony_ci   exec_list_make_empty(&if_stmt->then_list);
628bf215546Sopenharmony_ci   exec_list_push_tail(&if_stmt->then_list, &then->cf_node.node);
629bf215546Sopenharmony_ci   then->cf_node.parent = &if_stmt->cf_node;
630bf215546Sopenharmony_ci
631bf215546Sopenharmony_ci   nir_block *else_stmt = nir_block_create(shader);
632bf215546Sopenharmony_ci   exec_list_make_empty(&if_stmt->else_list);
633bf215546Sopenharmony_ci   exec_list_push_tail(&if_stmt->else_list, &else_stmt->cf_node.node);
634bf215546Sopenharmony_ci   else_stmt->cf_node.parent = &if_stmt->cf_node;
635bf215546Sopenharmony_ci
636bf215546Sopenharmony_ci   return if_stmt;
637bf215546Sopenharmony_ci}
638bf215546Sopenharmony_ci
639bf215546Sopenharmony_cinir_loop *
640bf215546Sopenharmony_cinir_loop_create(nir_shader *shader)
641bf215546Sopenharmony_ci{
642bf215546Sopenharmony_ci   nir_loop *loop = rzalloc(shader, nir_loop);
643bf215546Sopenharmony_ci
644bf215546Sopenharmony_ci   cf_init(&loop->cf_node, nir_cf_node_loop);
645bf215546Sopenharmony_ci   /* Assume that loops are divergent until proven otherwise */
646bf215546Sopenharmony_ci   loop->divergent = true;
647bf215546Sopenharmony_ci
648bf215546Sopenharmony_ci   nir_block *body = nir_block_create(shader);
649bf215546Sopenharmony_ci   exec_list_make_empty(&loop->body);
650bf215546Sopenharmony_ci   exec_list_push_tail(&loop->body, &body->cf_node.node);
651bf215546Sopenharmony_ci   body->cf_node.parent = &loop->cf_node;
652bf215546Sopenharmony_ci
653bf215546Sopenharmony_ci   body->successors[0] = body;
654bf215546Sopenharmony_ci   _mesa_set_add(body->predecessors, body);
655bf215546Sopenharmony_ci
656bf215546Sopenharmony_ci   return loop;
657bf215546Sopenharmony_ci}
658bf215546Sopenharmony_ci
659bf215546Sopenharmony_cistatic void
660bf215546Sopenharmony_ciinstr_init(nir_instr *instr, nir_instr_type type)
661bf215546Sopenharmony_ci{
662bf215546Sopenharmony_ci   instr->type = type;
663bf215546Sopenharmony_ci   instr->block = NULL;
664bf215546Sopenharmony_ci   exec_node_init(&instr->node);
665bf215546Sopenharmony_ci}
666bf215546Sopenharmony_ci
667bf215546Sopenharmony_cistatic void
668bf215546Sopenharmony_cidest_init(nir_dest *dest)
669bf215546Sopenharmony_ci{
670bf215546Sopenharmony_ci   dest->is_ssa = false;
671bf215546Sopenharmony_ci   dest->reg.reg = NULL;
672bf215546Sopenharmony_ci   dest->reg.indirect = NULL;
673bf215546Sopenharmony_ci   dest->reg.base_offset = 0;
674bf215546Sopenharmony_ci}
675bf215546Sopenharmony_ci
676bf215546Sopenharmony_cistatic void
677bf215546Sopenharmony_cialu_dest_init(nir_alu_dest *dest)
678bf215546Sopenharmony_ci{
679bf215546Sopenharmony_ci   dest_init(&dest->dest);
680bf215546Sopenharmony_ci   dest->saturate = false;
681bf215546Sopenharmony_ci   dest->write_mask = 0xf;
682bf215546Sopenharmony_ci}
683bf215546Sopenharmony_ci
684bf215546Sopenharmony_cistatic void
685bf215546Sopenharmony_cialu_src_init(nir_alu_src *src)
686bf215546Sopenharmony_ci{
687bf215546Sopenharmony_ci   src_init(&src->src);
688bf215546Sopenharmony_ci   src->abs = src->negate = false;
689bf215546Sopenharmony_ci   for (int i = 0; i < NIR_MAX_VEC_COMPONENTS; ++i)
690bf215546Sopenharmony_ci      src->swizzle[i] = i;
691bf215546Sopenharmony_ci}
692bf215546Sopenharmony_ci
693bf215546Sopenharmony_cinir_alu_instr *
694bf215546Sopenharmony_cinir_alu_instr_create(nir_shader *shader, nir_op op)
695bf215546Sopenharmony_ci{
696bf215546Sopenharmony_ci   unsigned num_srcs = nir_op_infos[op].num_inputs;
697bf215546Sopenharmony_ci   /* TODO: don't use calloc */
698bf215546Sopenharmony_ci   nir_alu_instr *instr = calloc(1, sizeof(nir_alu_instr) + num_srcs * sizeof(nir_alu_src));
699bf215546Sopenharmony_ci
700bf215546Sopenharmony_ci   instr_init(&instr->instr, nir_instr_type_alu);
701bf215546Sopenharmony_ci   instr->op = op;
702bf215546Sopenharmony_ci   alu_dest_init(&instr->dest);
703bf215546Sopenharmony_ci   for (unsigned i = 0; i < num_srcs; i++)
704bf215546Sopenharmony_ci      alu_src_init(&instr->src[i]);
705bf215546Sopenharmony_ci
706bf215546Sopenharmony_ci   list_add(&instr->instr.gc_node, &shader->gc_list);
707bf215546Sopenharmony_ci
708bf215546Sopenharmony_ci   return instr;
709bf215546Sopenharmony_ci}
710bf215546Sopenharmony_ci
711bf215546Sopenharmony_cinir_deref_instr *
712bf215546Sopenharmony_cinir_deref_instr_create(nir_shader *shader, nir_deref_type deref_type)
713bf215546Sopenharmony_ci{
714bf215546Sopenharmony_ci   nir_deref_instr *instr = calloc(1, sizeof(*instr));
715bf215546Sopenharmony_ci
716bf215546Sopenharmony_ci   instr_init(&instr->instr, nir_instr_type_deref);
717bf215546Sopenharmony_ci
718bf215546Sopenharmony_ci   instr->deref_type = deref_type;
719bf215546Sopenharmony_ci   if (deref_type != nir_deref_type_var)
720bf215546Sopenharmony_ci      src_init(&instr->parent);
721bf215546Sopenharmony_ci
722bf215546Sopenharmony_ci   if (deref_type == nir_deref_type_array ||
723bf215546Sopenharmony_ci       deref_type == nir_deref_type_ptr_as_array)
724bf215546Sopenharmony_ci      src_init(&instr->arr.index);
725bf215546Sopenharmony_ci
726bf215546Sopenharmony_ci   dest_init(&instr->dest);
727bf215546Sopenharmony_ci
728bf215546Sopenharmony_ci   list_add(&instr->instr.gc_node, &shader->gc_list);
729bf215546Sopenharmony_ci
730bf215546Sopenharmony_ci   return instr;
731bf215546Sopenharmony_ci}
732bf215546Sopenharmony_ci
733bf215546Sopenharmony_cinir_jump_instr *
734bf215546Sopenharmony_cinir_jump_instr_create(nir_shader *shader, nir_jump_type type)
735bf215546Sopenharmony_ci{
736bf215546Sopenharmony_ci   nir_jump_instr *instr = malloc(sizeof(*instr));
737bf215546Sopenharmony_ci   instr_init(&instr->instr, nir_instr_type_jump);
738bf215546Sopenharmony_ci   src_init(&instr->condition);
739bf215546Sopenharmony_ci   instr->type = type;
740bf215546Sopenharmony_ci   instr->target = NULL;
741bf215546Sopenharmony_ci   instr->else_target = NULL;
742bf215546Sopenharmony_ci
743bf215546Sopenharmony_ci   list_add(&instr->instr.gc_node, &shader->gc_list);
744bf215546Sopenharmony_ci
745bf215546Sopenharmony_ci   return instr;
746bf215546Sopenharmony_ci}
747bf215546Sopenharmony_ci
748bf215546Sopenharmony_cinir_load_const_instr *
749bf215546Sopenharmony_cinir_load_const_instr_create(nir_shader *shader, unsigned num_components,
750bf215546Sopenharmony_ci                            unsigned bit_size)
751bf215546Sopenharmony_ci{
752bf215546Sopenharmony_ci   nir_load_const_instr *instr =
753bf215546Sopenharmony_ci      calloc(1, sizeof(*instr) + num_components * sizeof(*instr->value));
754bf215546Sopenharmony_ci   instr_init(&instr->instr, nir_instr_type_load_const);
755bf215546Sopenharmony_ci
756bf215546Sopenharmony_ci   nir_ssa_def_init(&instr->instr, &instr->def, num_components, bit_size);
757bf215546Sopenharmony_ci
758bf215546Sopenharmony_ci   list_add(&instr->instr.gc_node, &shader->gc_list);
759bf215546Sopenharmony_ci
760bf215546Sopenharmony_ci   return instr;
761bf215546Sopenharmony_ci}
762bf215546Sopenharmony_ci
763bf215546Sopenharmony_cinir_intrinsic_instr *
764bf215546Sopenharmony_cinir_intrinsic_instr_create(nir_shader *shader, nir_intrinsic_op op)
765bf215546Sopenharmony_ci{
766bf215546Sopenharmony_ci   unsigned num_srcs = nir_intrinsic_infos[op].num_srcs;
767bf215546Sopenharmony_ci   /* TODO: don't use calloc */
768bf215546Sopenharmony_ci   nir_intrinsic_instr *instr =
769bf215546Sopenharmony_ci      calloc(1, sizeof(nir_intrinsic_instr) + num_srcs * sizeof(nir_src));
770bf215546Sopenharmony_ci
771bf215546Sopenharmony_ci   instr_init(&instr->instr, nir_instr_type_intrinsic);
772bf215546Sopenharmony_ci   instr->intrinsic = op;
773bf215546Sopenharmony_ci
774bf215546Sopenharmony_ci   if (nir_intrinsic_infos[op].has_dest)
775bf215546Sopenharmony_ci      dest_init(&instr->dest);
776bf215546Sopenharmony_ci
777bf215546Sopenharmony_ci   for (unsigned i = 0; i < num_srcs; i++)
778bf215546Sopenharmony_ci      src_init(&instr->src[i]);
779bf215546Sopenharmony_ci
780bf215546Sopenharmony_ci   list_add(&instr->instr.gc_node, &shader->gc_list);
781bf215546Sopenharmony_ci
782bf215546Sopenharmony_ci   return instr;
783bf215546Sopenharmony_ci}
784bf215546Sopenharmony_ci
785bf215546Sopenharmony_cinir_call_instr *
786bf215546Sopenharmony_cinir_call_instr_create(nir_shader *shader, nir_function *callee)
787bf215546Sopenharmony_ci{
788bf215546Sopenharmony_ci   const unsigned num_params = callee->num_params;
789bf215546Sopenharmony_ci   nir_call_instr *instr =
790bf215546Sopenharmony_ci      calloc(1, sizeof(*instr) + num_params * sizeof(instr->params[0]));
791bf215546Sopenharmony_ci
792bf215546Sopenharmony_ci   instr_init(&instr->instr, nir_instr_type_call);
793bf215546Sopenharmony_ci   instr->callee = callee;
794bf215546Sopenharmony_ci   instr->num_params = num_params;
795bf215546Sopenharmony_ci   for (unsigned i = 0; i < num_params; i++)
796bf215546Sopenharmony_ci      src_init(&instr->params[i]);
797bf215546Sopenharmony_ci
798bf215546Sopenharmony_ci   list_add(&instr->instr.gc_node, &shader->gc_list);
799bf215546Sopenharmony_ci
800bf215546Sopenharmony_ci   return instr;
801bf215546Sopenharmony_ci}
802bf215546Sopenharmony_ci
803bf215546Sopenharmony_cistatic int8_t default_tg4_offsets[4][2] =
804bf215546Sopenharmony_ci{
805bf215546Sopenharmony_ci   { 0, 1 },
806bf215546Sopenharmony_ci   { 1, 1 },
807bf215546Sopenharmony_ci   { 1, 0 },
808bf215546Sopenharmony_ci   { 0, 0 },
809bf215546Sopenharmony_ci};
810bf215546Sopenharmony_ci
811bf215546Sopenharmony_cinir_tex_instr *
812bf215546Sopenharmony_cinir_tex_instr_create(nir_shader *shader, unsigned num_srcs)
813bf215546Sopenharmony_ci{
814bf215546Sopenharmony_ci   nir_tex_instr *instr = calloc(1, sizeof(*instr));
815bf215546Sopenharmony_ci   instr_init(&instr->instr, nir_instr_type_tex);
816bf215546Sopenharmony_ci
817bf215546Sopenharmony_ci   dest_init(&instr->dest);
818bf215546Sopenharmony_ci
819bf215546Sopenharmony_ci   instr->num_srcs = num_srcs;
820bf215546Sopenharmony_ci   instr->src = malloc(sizeof(nir_tex_src) * num_srcs);
821bf215546Sopenharmony_ci   for (unsigned i = 0; i < num_srcs; i++)
822bf215546Sopenharmony_ci      src_init(&instr->src[i].src);
823bf215546Sopenharmony_ci
824bf215546Sopenharmony_ci   instr->texture_index = 0;
825bf215546Sopenharmony_ci   instr->sampler_index = 0;
826bf215546Sopenharmony_ci   memcpy(instr->tg4_offsets, default_tg4_offsets, sizeof(instr->tg4_offsets));
827bf215546Sopenharmony_ci
828bf215546Sopenharmony_ci   list_add(&instr->instr.gc_node, &shader->gc_list);
829bf215546Sopenharmony_ci
830bf215546Sopenharmony_ci   return instr;
831bf215546Sopenharmony_ci}
832bf215546Sopenharmony_ci
833bf215546Sopenharmony_civoid
834bf215546Sopenharmony_cinir_tex_instr_add_src(nir_tex_instr *tex,
835bf215546Sopenharmony_ci                      nir_tex_src_type src_type,
836bf215546Sopenharmony_ci                      nir_src src)
837bf215546Sopenharmony_ci{
838bf215546Sopenharmony_ci   nir_tex_src *new_srcs = calloc(sizeof(*new_srcs),
839bf215546Sopenharmony_ci                                         tex->num_srcs + 1);
840bf215546Sopenharmony_ci
841bf215546Sopenharmony_ci   for (unsigned i = 0; i < tex->num_srcs; i++) {
842bf215546Sopenharmony_ci      new_srcs[i].src_type = tex->src[i].src_type;
843bf215546Sopenharmony_ci      nir_instr_move_src(&tex->instr, &new_srcs[i].src,
844bf215546Sopenharmony_ci                         &tex->src[i].src);
845bf215546Sopenharmony_ci   }
846bf215546Sopenharmony_ci
847bf215546Sopenharmony_ci   free(tex->src);
848bf215546Sopenharmony_ci   tex->src = new_srcs;
849bf215546Sopenharmony_ci
850bf215546Sopenharmony_ci   tex->src[tex->num_srcs].src_type = src_type;
851bf215546Sopenharmony_ci   nir_instr_rewrite_src(&tex->instr, &tex->src[tex->num_srcs].src, src);
852bf215546Sopenharmony_ci   tex->num_srcs++;
853bf215546Sopenharmony_ci}
854bf215546Sopenharmony_ci
855bf215546Sopenharmony_civoid
856bf215546Sopenharmony_cinir_tex_instr_remove_src(nir_tex_instr *tex, unsigned src_idx)
857bf215546Sopenharmony_ci{
858bf215546Sopenharmony_ci   assert(src_idx < tex->num_srcs);
859bf215546Sopenharmony_ci
860bf215546Sopenharmony_ci   /* First rewrite the source to NIR_SRC_INIT */
861bf215546Sopenharmony_ci   nir_instr_rewrite_src(&tex->instr, &tex->src[src_idx].src, NIR_SRC_INIT);
862bf215546Sopenharmony_ci
863bf215546Sopenharmony_ci   /* Now, move all of the other sources down */
864bf215546Sopenharmony_ci   for (unsigned i = src_idx + 1; i < tex->num_srcs; i++) {
865bf215546Sopenharmony_ci      tex->src[i-1].src_type = tex->src[i].src_type;
866bf215546Sopenharmony_ci      nir_instr_move_src(&tex->instr, &tex->src[i-1].src, &tex->src[i].src);
867bf215546Sopenharmony_ci   }
868bf215546Sopenharmony_ci   tex->num_srcs--;
869bf215546Sopenharmony_ci}
870bf215546Sopenharmony_ci
871bf215546Sopenharmony_cibool
872bf215546Sopenharmony_cinir_tex_instr_has_explicit_tg4_offsets(nir_tex_instr *tex)
873bf215546Sopenharmony_ci{
874bf215546Sopenharmony_ci   if (tex->op != nir_texop_tg4)
875bf215546Sopenharmony_ci      return false;
876bf215546Sopenharmony_ci   return memcmp(tex->tg4_offsets, default_tg4_offsets,
877bf215546Sopenharmony_ci                 sizeof(tex->tg4_offsets)) != 0;
878bf215546Sopenharmony_ci}
879bf215546Sopenharmony_ci
880bf215546Sopenharmony_cinir_phi_instr *
881bf215546Sopenharmony_cinir_phi_instr_create(nir_shader *shader)
882bf215546Sopenharmony_ci{
883bf215546Sopenharmony_ci   nir_phi_instr *instr = malloc(sizeof(*instr));
884bf215546Sopenharmony_ci   instr_init(&instr->instr, nir_instr_type_phi);
885bf215546Sopenharmony_ci
886bf215546Sopenharmony_ci   dest_init(&instr->dest);
887bf215546Sopenharmony_ci   exec_list_make_empty(&instr->srcs);
888bf215546Sopenharmony_ci
889bf215546Sopenharmony_ci   list_add(&instr->instr.gc_node, &shader->gc_list);
890bf215546Sopenharmony_ci
891bf215546Sopenharmony_ci   return instr;
892bf215546Sopenharmony_ci}
893bf215546Sopenharmony_ci
894bf215546Sopenharmony_ci/**
895bf215546Sopenharmony_ci * Adds a new source to a NIR instruction.
896bf215546Sopenharmony_ci *
897bf215546Sopenharmony_ci * Note that this does not update the def/use relationship for src, assuming
898bf215546Sopenharmony_ci * that the instr is not in the shader.  If it is, you have to do:
899bf215546Sopenharmony_ci *
900bf215546Sopenharmony_ci * list_addtail(&phi_src->src.use_link, &src.ssa->uses);
901bf215546Sopenharmony_ci */
902bf215546Sopenharmony_cinir_phi_src *
903bf215546Sopenharmony_cinir_phi_instr_add_src(nir_phi_instr *instr, nir_block *pred, nir_src src)
904bf215546Sopenharmony_ci{
905bf215546Sopenharmony_ci   nir_phi_src *phi_src;
906bf215546Sopenharmony_ci
907bf215546Sopenharmony_ci   phi_src = calloc(1, sizeof(nir_phi_src));
908bf215546Sopenharmony_ci   phi_src->pred = pred;
909bf215546Sopenharmony_ci   phi_src->src = src;
910bf215546Sopenharmony_ci   phi_src->src.parent_instr = &instr->instr;
911bf215546Sopenharmony_ci   exec_list_push_tail(&instr->srcs, &phi_src->node);
912bf215546Sopenharmony_ci
913bf215546Sopenharmony_ci   return phi_src;
914bf215546Sopenharmony_ci}
915bf215546Sopenharmony_ci
916bf215546Sopenharmony_cinir_parallel_copy_instr *
917bf215546Sopenharmony_cinir_parallel_copy_instr_create(nir_shader *shader)
918bf215546Sopenharmony_ci{
919bf215546Sopenharmony_ci   nir_parallel_copy_instr *instr = malloc(sizeof(*instr));
920bf215546Sopenharmony_ci   instr_init(&instr->instr, nir_instr_type_parallel_copy);
921bf215546Sopenharmony_ci
922bf215546Sopenharmony_ci   exec_list_make_empty(&instr->entries);
923bf215546Sopenharmony_ci
924bf215546Sopenharmony_ci   list_add(&instr->instr.gc_node, &shader->gc_list);
925bf215546Sopenharmony_ci
926bf215546Sopenharmony_ci   return instr;
927bf215546Sopenharmony_ci}
928bf215546Sopenharmony_ci
929bf215546Sopenharmony_cinir_ssa_undef_instr *
930bf215546Sopenharmony_cinir_ssa_undef_instr_create(nir_shader *shader,
931bf215546Sopenharmony_ci                           unsigned num_components,
932bf215546Sopenharmony_ci                           unsigned bit_size)
933bf215546Sopenharmony_ci{
934bf215546Sopenharmony_ci   nir_ssa_undef_instr *instr = malloc(sizeof(*instr));
935bf215546Sopenharmony_ci   instr_init(&instr->instr, nir_instr_type_ssa_undef);
936bf215546Sopenharmony_ci
937bf215546Sopenharmony_ci   nir_ssa_def_init(&instr->instr, &instr->def, num_components, bit_size);
938bf215546Sopenharmony_ci
939bf215546Sopenharmony_ci   list_add(&instr->instr.gc_node, &shader->gc_list);
940bf215546Sopenharmony_ci
941bf215546Sopenharmony_ci   return instr;
942bf215546Sopenharmony_ci}
943bf215546Sopenharmony_ci
944bf215546Sopenharmony_cistatic nir_const_value
945bf215546Sopenharmony_ciconst_value_float(double d, unsigned bit_size)
946bf215546Sopenharmony_ci{
947bf215546Sopenharmony_ci   nir_const_value v;
948bf215546Sopenharmony_ci   memset(&v, 0, sizeof(v));
949bf215546Sopenharmony_ci   switch (bit_size) {
950bf215546Sopenharmony_ci   case 16: v.u16 = _mesa_float_to_half(d);  break;
951bf215546Sopenharmony_ci   case 32: v.f32 = d;                       break;
952bf215546Sopenharmony_ci   case 64: v.f64 = d;                       break;
953bf215546Sopenharmony_ci   default:
954bf215546Sopenharmony_ci      unreachable("Invalid bit size");
955bf215546Sopenharmony_ci   }
956bf215546Sopenharmony_ci   return v;
957bf215546Sopenharmony_ci}
958bf215546Sopenharmony_ci
959bf215546Sopenharmony_cistatic nir_const_value
960bf215546Sopenharmony_ciconst_value_int(int64_t i, unsigned bit_size)
961bf215546Sopenharmony_ci{
962bf215546Sopenharmony_ci   nir_const_value v;
963bf215546Sopenharmony_ci   memset(&v, 0, sizeof(v));
964bf215546Sopenharmony_ci   switch (bit_size) {
965bf215546Sopenharmony_ci   case 1:  v.b   = i & 1;  break;
966bf215546Sopenharmony_ci   case 8:  v.i8  = i;  break;
967bf215546Sopenharmony_ci   case 16: v.i16 = i;  break;
968bf215546Sopenharmony_ci   case 32: v.i32 = i;  break;
969bf215546Sopenharmony_ci   case 64: v.i64 = i;  break;
970bf215546Sopenharmony_ci   default:
971bf215546Sopenharmony_ci      unreachable("Invalid bit size");
972bf215546Sopenharmony_ci   }
973bf215546Sopenharmony_ci   return v;
974bf215546Sopenharmony_ci}
975bf215546Sopenharmony_ci
976bf215546Sopenharmony_cinir_const_value
977bf215546Sopenharmony_cinir_alu_binop_identity(nir_op binop, unsigned bit_size)
978bf215546Sopenharmony_ci{
979bf215546Sopenharmony_ci   const int64_t max_int = (1ull << (bit_size - 1)) - 1;
980bf215546Sopenharmony_ci   const int64_t min_int = -max_int - 1;
981bf215546Sopenharmony_ci   switch (binop) {
982bf215546Sopenharmony_ci   case nir_op_iadd:
983bf215546Sopenharmony_ci      return const_value_int(0, bit_size);
984bf215546Sopenharmony_ci   case nir_op_fadd:
985bf215546Sopenharmony_ci      return const_value_float(0, bit_size);
986bf215546Sopenharmony_ci   case nir_op_imul:
987bf215546Sopenharmony_ci      return const_value_int(1, bit_size);
988bf215546Sopenharmony_ci   case nir_op_fmul:
989bf215546Sopenharmony_ci      return const_value_float(1, bit_size);
990bf215546Sopenharmony_ci   case nir_op_imin:
991bf215546Sopenharmony_ci      return const_value_int(max_int, bit_size);
992bf215546Sopenharmony_ci   case nir_op_umin:
993bf215546Sopenharmony_ci      return const_value_int(~0ull, bit_size);
994bf215546Sopenharmony_ci   case nir_op_fmin:
995bf215546Sopenharmony_ci      return const_value_float(INFINITY, bit_size);
996bf215546Sopenharmony_ci   case nir_op_imax:
997bf215546Sopenharmony_ci      return const_value_int(min_int, bit_size);
998bf215546Sopenharmony_ci   case nir_op_umax:
999bf215546Sopenharmony_ci      return const_value_int(0, bit_size);
1000bf215546Sopenharmony_ci   case nir_op_fmax:
1001bf215546Sopenharmony_ci      return const_value_float(-INFINITY, bit_size);
1002bf215546Sopenharmony_ci   case nir_op_iand:
1003bf215546Sopenharmony_ci      return const_value_int(~0ull, bit_size);
1004bf215546Sopenharmony_ci   case nir_op_ior:
1005bf215546Sopenharmony_ci      return const_value_int(0, bit_size);
1006bf215546Sopenharmony_ci   case nir_op_ixor:
1007bf215546Sopenharmony_ci      return const_value_int(0, bit_size);
1008bf215546Sopenharmony_ci   default:
1009bf215546Sopenharmony_ci      unreachable("Invalid reduction operation");
1010bf215546Sopenharmony_ci   }
1011bf215546Sopenharmony_ci}
1012bf215546Sopenharmony_ci
1013bf215546Sopenharmony_cinir_function_impl *
1014bf215546Sopenharmony_cinir_cf_node_get_function(nir_cf_node *node)
1015bf215546Sopenharmony_ci{
1016bf215546Sopenharmony_ci   while (node->type != nir_cf_node_function) {
1017bf215546Sopenharmony_ci      node = node->parent;
1018bf215546Sopenharmony_ci   }
1019bf215546Sopenharmony_ci
1020bf215546Sopenharmony_ci   return nir_cf_node_as_function(node);
1021bf215546Sopenharmony_ci}
1022bf215546Sopenharmony_ci
1023bf215546Sopenharmony_ci/* Reduces a cursor by trying to convert everything to after and trying to
1024bf215546Sopenharmony_ci * go up to block granularity when possible.
1025bf215546Sopenharmony_ci */
1026bf215546Sopenharmony_cistatic nir_cursor
1027bf215546Sopenharmony_cireduce_cursor(nir_cursor cursor)
1028bf215546Sopenharmony_ci{
1029bf215546Sopenharmony_ci   switch (cursor.option) {
1030bf215546Sopenharmony_ci   case nir_cursor_before_block:
1031bf215546Sopenharmony_ci      if (exec_list_is_empty(&cursor.block->instr_list)) {
1032bf215546Sopenharmony_ci         /* Empty block.  After is as good as before. */
1033bf215546Sopenharmony_ci         cursor.option = nir_cursor_after_block;
1034bf215546Sopenharmony_ci      }
1035bf215546Sopenharmony_ci      return cursor;
1036bf215546Sopenharmony_ci
1037bf215546Sopenharmony_ci   case nir_cursor_after_block:
1038bf215546Sopenharmony_ci      return cursor;
1039bf215546Sopenharmony_ci
1040bf215546Sopenharmony_ci   case nir_cursor_before_instr: {
1041bf215546Sopenharmony_ci      nir_instr *prev_instr = nir_instr_prev(cursor.instr);
1042bf215546Sopenharmony_ci      if (prev_instr) {
1043bf215546Sopenharmony_ci         /* Before this instruction is after the previous */
1044bf215546Sopenharmony_ci         cursor.instr = prev_instr;
1045bf215546Sopenharmony_ci         cursor.option = nir_cursor_after_instr;
1046bf215546Sopenharmony_ci      } else {
1047bf215546Sopenharmony_ci         /* No previous instruction.  Switch to before block */
1048bf215546Sopenharmony_ci         cursor.block = cursor.instr->block;
1049bf215546Sopenharmony_ci         cursor.option = nir_cursor_before_block;
1050bf215546Sopenharmony_ci      }
1051bf215546Sopenharmony_ci      return reduce_cursor(cursor);
1052bf215546Sopenharmony_ci   }
1053bf215546Sopenharmony_ci
1054bf215546Sopenharmony_ci   case nir_cursor_after_instr:
1055bf215546Sopenharmony_ci      if (nir_instr_next(cursor.instr) == NULL) {
1056bf215546Sopenharmony_ci         /* This is the last instruction, switch to after block */
1057bf215546Sopenharmony_ci         cursor.option = nir_cursor_after_block;
1058bf215546Sopenharmony_ci         cursor.block = cursor.instr->block;
1059bf215546Sopenharmony_ci      }
1060bf215546Sopenharmony_ci      return cursor;
1061bf215546Sopenharmony_ci
1062bf215546Sopenharmony_ci   default:
1063bf215546Sopenharmony_ci      unreachable("Inavlid cursor option");
1064bf215546Sopenharmony_ci   }
1065bf215546Sopenharmony_ci}
1066bf215546Sopenharmony_ci
1067bf215546Sopenharmony_cibool
1068bf215546Sopenharmony_cinir_cursors_equal(nir_cursor a, nir_cursor b)
1069bf215546Sopenharmony_ci{
1070bf215546Sopenharmony_ci   /* Reduced cursors should be unique */
1071bf215546Sopenharmony_ci   a = reduce_cursor(a);
1072bf215546Sopenharmony_ci   b = reduce_cursor(b);
1073bf215546Sopenharmony_ci
1074bf215546Sopenharmony_ci   return a.block == b.block && a.option == b.option;
1075bf215546Sopenharmony_ci}
1076bf215546Sopenharmony_ci
1077bf215546Sopenharmony_cistatic bool
1078bf215546Sopenharmony_ciadd_use_cb(nir_src *src, void *state)
1079bf215546Sopenharmony_ci{
1080bf215546Sopenharmony_ci   nir_instr *instr = state;
1081bf215546Sopenharmony_ci
1082bf215546Sopenharmony_ci   src->parent_instr = instr;
1083bf215546Sopenharmony_ci   list_addtail(&src->use_link,
1084bf215546Sopenharmony_ci                src->is_ssa ? &src->ssa->uses : &src->reg.reg->uses);
1085bf215546Sopenharmony_ci
1086bf215546Sopenharmony_ci   return true;
1087bf215546Sopenharmony_ci}
1088bf215546Sopenharmony_ci
1089bf215546Sopenharmony_cistatic bool
1090bf215546Sopenharmony_ciadd_ssa_def_cb(nir_ssa_def *def, void *state)
1091bf215546Sopenharmony_ci{
1092bf215546Sopenharmony_ci   nir_instr *instr = state;
1093bf215546Sopenharmony_ci
1094bf215546Sopenharmony_ci   if (instr->block && def->index == UINT_MAX) {
1095bf215546Sopenharmony_ci      nir_function_impl *impl =
1096bf215546Sopenharmony_ci         nir_cf_node_get_function(&instr->block->cf_node);
1097bf215546Sopenharmony_ci
1098bf215546Sopenharmony_ci      def->index = impl->ssa_alloc++;
1099bf215546Sopenharmony_ci
1100bf215546Sopenharmony_ci      impl->valid_metadata &= ~nir_metadata_live_ssa_defs;
1101bf215546Sopenharmony_ci   }
1102bf215546Sopenharmony_ci
1103bf215546Sopenharmony_ci   return true;
1104bf215546Sopenharmony_ci}
1105bf215546Sopenharmony_ci
1106bf215546Sopenharmony_cistatic bool
1107bf215546Sopenharmony_ciadd_reg_def_cb(nir_dest *dest, void *state)
1108bf215546Sopenharmony_ci{
1109bf215546Sopenharmony_ci   nir_instr *instr = state;
1110bf215546Sopenharmony_ci
1111bf215546Sopenharmony_ci   if (!dest->is_ssa) {
1112bf215546Sopenharmony_ci      dest->reg.parent_instr = instr;
1113bf215546Sopenharmony_ci      list_addtail(&dest->reg.def_link, &dest->reg.reg->defs);
1114bf215546Sopenharmony_ci   }
1115bf215546Sopenharmony_ci
1116bf215546Sopenharmony_ci   return true;
1117bf215546Sopenharmony_ci}
1118bf215546Sopenharmony_ci
1119bf215546Sopenharmony_cistatic void
1120bf215546Sopenharmony_ciadd_defs_uses(nir_instr *instr)
1121bf215546Sopenharmony_ci{
1122bf215546Sopenharmony_ci   nir_foreach_src(instr, add_use_cb, instr);
1123bf215546Sopenharmony_ci   nir_foreach_dest(instr, add_reg_def_cb, instr);
1124bf215546Sopenharmony_ci   nir_foreach_ssa_def(instr, add_ssa_def_cb, instr);
1125bf215546Sopenharmony_ci}
1126bf215546Sopenharmony_ci
1127bf215546Sopenharmony_civoid
1128bf215546Sopenharmony_cinir_instr_insert(nir_cursor cursor, nir_instr *instr)
1129bf215546Sopenharmony_ci{
1130bf215546Sopenharmony_ci   switch (cursor.option) {
1131bf215546Sopenharmony_ci   case nir_cursor_before_block:
1132bf215546Sopenharmony_ci      /* Only allow inserting jumps into empty blocks. */
1133bf215546Sopenharmony_ci      if (instr->type == nir_instr_type_jump)
1134bf215546Sopenharmony_ci         assert(exec_list_is_empty(&cursor.block->instr_list));
1135bf215546Sopenharmony_ci
1136bf215546Sopenharmony_ci      instr->block = cursor.block;
1137bf215546Sopenharmony_ci      add_defs_uses(instr);
1138bf215546Sopenharmony_ci      exec_list_push_head(&cursor.block->instr_list, &instr->node);
1139bf215546Sopenharmony_ci      break;
1140bf215546Sopenharmony_ci   case nir_cursor_after_block: {
1141bf215546Sopenharmony_ci      /* Inserting instructions after a jump is illegal. */
1142bf215546Sopenharmony_ci      nir_instr *last = nir_block_last_instr(cursor.block);
1143bf215546Sopenharmony_ci      assert(last == NULL || last->type != nir_instr_type_jump);
1144bf215546Sopenharmony_ci      (void) last;
1145bf215546Sopenharmony_ci
1146bf215546Sopenharmony_ci      instr->block = cursor.block;
1147bf215546Sopenharmony_ci      add_defs_uses(instr);
1148bf215546Sopenharmony_ci      exec_list_push_tail(&cursor.block->instr_list, &instr->node);
1149bf215546Sopenharmony_ci      break;
1150bf215546Sopenharmony_ci   }
1151bf215546Sopenharmony_ci   case nir_cursor_before_instr:
1152bf215546Sopenharmony_ci      assert(instr->type != nir_instr_type_jump);
1153bf215546Sopenharmony_ci      instr->block = cursor.instr->block;
1154bf215546Sopenharmony_ci      add_defs_uses(instr);
1155bf215546Sopenharmony_ci      exec_node_insert_node_before(&cursor.instr->node, &instr->node);
1156bf215546Sopenharmony_ci      break;
1157bf215546Sopenharmony_ci   case nir_cursor_after_instr:
1158bf215546Sopenharmony_ci      /* Inserting instructions after a jump is illegal. */
1159bf215546Sopenharmony_ci      assert(cursor.instr->type != nir_instr_type_jump);
1160bf215546Sopenharmony_ci
1161bf215546Sopenharmony_ci      /* Only allow inserting jumps at the end of the block. */
1162bf215546Sopenharmony_ci      if (instr->type == nir_instr_type_jump)
1163bf215546Sopenharmony_ci         assert(cursor.instr == nir_block_last_instr(cursor.instr->block));
1164bf215546Sopenharmony_ci
1165bf215546Sopenharmony_ci      instr->block = cursor.instr->block;
1166bf215546Sopenharmony_ci      add_defs_uses(instr);
1167bf215546Sopenharmony_ci      exec_node_insert_after(&cursor.instr->node, &instr->node);
1168bf215546Sopenharmony_ci      break;
1169bf215546Sopenharmony_ci   }
1170bf215546Sopenharmony_ci
1171bf215546Sopenharmony_ci   if (instr->type == nir_instr_type_jump)
1172bf215546Sopenharmony_ci      nir_handle_add_jump(instr->block);
1173bf215546Sopenharmony_ci
1174bf215546Sopenharmony_ci   nir_function_impl *impl = nir_cf_node_get_function(&instr->block->cf_node);
1175bf215546Sopenharmony_ci   impl->valid_metadata &= ~nir_metadata_instr_index;
1176bf215546Sopenharmony_ci}
1177bf215546Sopenharmony_ci
1178bf215546Sopenharmony_cibool
1179bf215546Sopenharmony_cinir_instr_move(nir_cursor cursor, nir_instr *instr)
1180bf215546Sopenharmony_ci{
1181bf215546Sopenharmony_ci   /* If the cursor happens to refer to this instruction (either before or
1182bf215546Sopenharmony_ci    * after), don't do anything.
1183bf215546Sopenharmony_ci    */
1184bf215546Sopenharmony_ci   if ((cursor.option == nir_cursor_before_instr ||
1185bf215546Sopenharmony_ci        cursor.option == nir_cursor_after_instr) &&
1186bf215546Sopenharmony_ci       cursor.instr == instr)
1187bf215546Sopenharmony_ci      return false;
1188bf215546Sopenharmony_ci
1189bf215546Sopenharmony_ci   nir_instr_remove(instr);
1190bf215546Sopenharmony_ci   nir_instr_insert(cursor, instr);
1191bf215546Sopenharmony_ci   return true;
1192bf215546Sopenharmony_ci}
1193bf215546Sopenharmony_ci
1194bf215546Sopenharmony_cistatic bool
1195bf215546Sopenharmony_cisrc_is_valid(const nir_src *src)
1196bf215546Sopenharmony_ci{
1197bf215546Sopenharmony_ci   return src->is_ssa ? (src->ssa != NULL) : (src->reg.reg != NULL);
1198bf215546Sopenharmony_ci}
1199bf215546Sopenharmony_ci
1200bf215546Sopenharmony_cistatic bool
1201bf215546Sopenharmony_ciremove_use_cb(nir_src *src, void *state)
1202bf215546Sopenharmony_ci{
1203bf215546Sopenharmony_ci   (void) state;
1204bf215546Sopenharmony_ci
1205bf215546Sopenharmony_ci   if (src_is_valid(src))
1206bf215546Sopenharmony_ci      list_del(&src->use_link);
1207bf215546Sopenharmony_ci
1208bf215546Sopenharmony_ci   return true;
1209bf215546Sopenharmony_ci}
1210bf215546Sopenharmony_ci
1211bf215546Sopenharmony_cistatic bool
1212bf215546Sopenharmony_ciremove_def_cb(nir_dest *dest, void *state)
1213bf215546Sopenharmony_ci{
1214bf215546Sopenharmony_ci   (void) state;
1215bf215546Sopenharmony_ci
1216bf215546Sopenharmony_ci   if (!dest->is_ssa)
1217bf215546Sopenharmony_ci      list_del(&dest->reg.def_link);
1218bf215546Sopenharmony_ci
1219bf215546Sopenharmony_ci   return true;
1220bf215546Sopenharmony_ci}
1221bf215546Sopenharmony_ci
1222bf215546Sopenharmony_cistatic void
1223bf215546Sopenharmony_ciremove_defs_uses(nir_instr *instr)
1224bf215546Sopenharmony_ci{
1225bf215546Sopenharmony_ci   nir_foreach_dest(instr, remove_def_cb, instr);
1226bf215546Sopenharmony_ci   nir_foreach_src(instr, remove_use_cb, instr);
1227bf215546Sopenharmony_ci}
1228bf215546Sopenharmony_ci
1229bf215546Sopenharmony_civoid nir_instr_remove_v(nir_instr *instr)
1230bf215546Sopenharmony_ci{
1231bf215546Sopenharmony_ci   remove_defs_uses(instr);
1232bf215546Sopenharmony_ci   exec_node_remove(&instr->node);
1233bf215546Sopenharmony_ci
1234bf215546Sopenharmony_ci   if (instr->type == nir_instr_type_jump) {
1235bf215546Sopenharmony_ci      nir_jump_instr *jump_instr = nir_instr_as_jump(instr);
1236bf215546Sopenharmony_ci      nir_handle_remove_jump(instr->block, jump_instr->type);
1237bf215546Sopenharmony_ci   }
1238bf215546Sopenharmony_ci}
1239bf215546Sopenharmony_ci
1240bf215546Sopenharmony_cistatic bool free_src_indirects_cb(nir_src *src, void *state)
1241bf215546Sopenharmony_ci{
1242bf215546Sopenharmony_ci   src_free_indirects(src);
1243bf215546Sopenharmony_ci   return true;
1244bf215546Sopenharmony_ci}
1245bf215546Sopenharmony_ci
1246bf215546Sopenharmony_cistatic bool free_dest_indirects_cb(nir_dest *dest, void *state)
1247bf215546Sopenharmony_ci{
1248bf215546Sopenharmony_ci   dest_free_indirects(dest);
1249bf215546Sopenharmony_ci   return true;
1250bf215546Sopenharmony_ci}
1251bf215546Sopenharmony_ci
1252bf215546Sopenharmony_civoid nir_instr_free(nir_instr *instr)
1253bf215546Sopenharmony_ci{
1254bf215546Sopenharmony_ci   nir_foreach_src(instr, free_src_indirects_cb, NULL);
1255bf215546Sopenharmony_ci   nir_foreach_dest(instr, free_dest_indirects_cb, NULL);
1256bf215546Sopenharmony_ci
1257bf215546Sopenharmony_ci   switch (instr->type) {
1258bf215546Sopenharmony_ci   case nir_instr_type_tex:
1259bf215546Sopenharmony_ci      free(nir_instr_as_tex(instr)->src);
1260bf215546Sopenharmony_ci      break;
1261bf215546Sopenharmony_ci
1262bf215546Sopenharmony_ci   case nir_instr_type_phi: {
1263bf215546Sopenharmony_ci      nir_phi_instr *phi = nir_instr_as_phi(instr);
1264bf215546Sopenharmony_ci      nir_foreach_phi_src_safe(phi_src, phi) {
1265bf215546Sopenharmony_ci         free(phi_src);
1266bf215546Sopenharmony_ci      }
1267bf215546Sopenharmony_ci      break;
1268bf215546Sopenharmony_ci   }
1269bf215546Sopenharmony_ci
1270bf215546Sopenharmony_ci   default:
1271bf215546Sopenharmony_ci      break;
1272bf215546Sopenharmony_ci   }
1273bf215546Sopenharmony_ci
1274bf215546Sopenharmony_ci   list_del(&instr->gc_node);
1275bf215546Sopenharmony_ci   free(instr);
1276bf215546Sopenharmony_ci}
1277bf215546Sopenharmony_ci
1278bf215546Sopenharmony_civoid
1279bf215546Sopenharmony_cinir_instr_free_list(struct exec_list *list)
1280bf215546Sopenharmony_ci{
1281bf215546Sopenharmony_ci   struct exec_node *node;
1282bf215546Sopenharmony_ci   while ((node = exec_list_pop_head(list))) {
1283bf215546Sopenharmony_ci      nir_instr *removed_instr = exec_node_data(nir_instr, node, node);
1284bf215546Sopenharmony_ci      nir_instr_free(removed_instr);
1285bf215546Sopenharmony_ci   }
1286bf215546Sopenharmony_ci}
1287bf215546Sopenharmony_ci
1288bf215546Sopenharmony_cistatic bool nir_instr_free_and_dce_live_cb(nir_ssa_def *def, void *state)
1289bf215546Sopenharmony_ci{
1290bf215546Sopenharmony_ci   bool *live = state;
1291bf215546Sopenharmony_ci
1292bf215546Sopenharmony_ci   if (!nir_ssa_def_is_unused(def)) {
1293bf215546Sopenharmony_ci      *live = true;
1294bf215546Sopenharmony_ci      return false;
1295bf215546Sopenharmony_ci   } else {
1296bf215546Sopenharmony_ci      return true;
1297bf215546Sopenharmony_ci   }
1298bf215546Sopenharmony_ci}
1299bf215546Sopenharmony_ci
1300bf215546Sopenharmony_cistatic bool nir_instr_free_and_dce_is_live(nir_instr *instr)
1301bf215546Sopenharmony_ci{
1302bf215546Sopenharmony_ci   /* Note: don't have to worry about jumps because they don't have dests to
1303bf215546Sopenharmony_ci    * become unused.
1304bf215546Sopenharmony_ci    */
1305bf215546Sopenharmony_ci   if (instr->type == nir_instr_type_intrinsic) {
1306bf215546Sopenharmony_ci      nir_intrinsic_instr *intr = nir_instr_as_intrinsic(instr);
1307bf215546Sopenharmony_ci      const nir_intrinsic_info *info = &nir_intrinsic_infos[intr->intrinsic];
1308bf215546Sopenharmony_ci      if (!(info->flags & NIR_INTRINSIC_CAN_ELIMINATE))
1309bf215546Sopenharmony_ci         return true;
1310bf215546Sopenharmony_ci   }
1311bf215546Sopenharmony_ci
1312bf215546Sopenharmony_ci   bool live = false;
1313bf215546Sopenharmony_ci   nir_foreach_ssa_def(instr, nir_instr_free_and_dce_live_cb, &live);
1314bf215546Sopenharmony_ci   return live;
1315bf215546Sopenharmony_ci}
1316bf215546Sopenharmony_ci
1317bf215546Sopenharmony_cistatic bool
1318bf215546Sopenharmony_cinir_instr_dce_add_dead_srcs_cb(nir_src *src, void *state)
1319bf215546Sopenharmony_ci{
1320bf215546Sopenharmony_ci   nir_instr_worklist *wl = state;
1321bf215546Sopenharmony_ci
1322bf215546Sopenharmony_ci   if (src->is_ssa) {
1323bf215546Sopenharmony_ci      list_del(&src->use_link);
1324bf215546Sopenharmony_ci      if (!nir_instr_free_and_dce_is_live(src->ssa->parent_instr))
1325bf215546Sopenharmony_ci         nir_instr_worklist_push_tail(wl, src->ssa->parent_instr);
1326bf215546Sopenharmony_ci
1327bf215546Sopenharmony_ci      /* Stop nir_instr_remove from trying to delete the link again. */
1328bf215546Sopenharmony_ci      src->ssa = NULL;
1329bf215546Sopenharmony_ci   }
1330bf215546Sopenharmony_ci
1331bf215546Sopenharmony_ci   return true;
1332bf215546Sopenharmony_ci}
1333bf215546Sopenharmony_ci
1334bf215546Sopenharmony_cistatic void
1335bf215546Sopenharmony_cinir_instr_dce_add_dead_ssa_srcs(nir_instr_worklist *wl, nir_instr *instr)
1336bf215546Sopenharmony_ci{
1337bf215546Sopenharmony_ci   nir_foreach_src(instr, nir_instr_dce_add_dead_srcs_cb, wl);
1338bf215546Sopenharmony_ci}
1339bf215546Sopenharmony_ci
1340bf215546Sopenharmony_ci/**
1341bf215546Sopenharmony_ci * Frees an instruction and any SSA defs that it used that are now dead,
1342bf215546Sopenharmony_ci * returning a nir_cursor where the instruction previously was.
1343bf215546Sopenharmony_ci */
1344bf215546Sopenharmony_cinir_cursor
1345bf215546Sopenharmony_cinir_instr_free_and_dce(nir_instr *instr)
1346bf215546Sopenharmony_ci{
1347bf215546Sopenharmony_ci   nir_instr_worklist *worklist = nir_instr_worklist_create();
1348bf215546Sopenharmony_ci
1349bf215546Sopenharmony_ci   nir_instr_dce_add_dead_ssa_srcs(worklist, instr);
1350bf215546Sopenharmony_ci   nir_cursor c = nir_instr_remove(instr);
1351bf215546Sopenharmony_ci
1352bf215546Sopenharmony_ci   struct exec_list to_free;
1353bf215546Sopenharmony_ci   exec_list_make_empty(&to_free);
1354bf215546Sopenharmony_ci
1355bf215546Sopenharmony_ci   nir_instr *dce_instr;
1356bf215546Sopenharmony_ci   while ((dce_instr = nir_instr_worklist_pop_head(worklist))) {
1357bf215546Sopenharmony_ci      nir_instr_dce_add_dead_ssa_srcs(worklist, dce_instr);
1358bf215546Sopenharmony_ci
1359bf215546Sopenharmony_ci      /* If we're removing the instr where our cursor is, then we have to
1360bf215546Sopenharmony_ci       * point the cursor elsewhere.
1361bf215546Sopenharmony_ci       */
1362bf215546Sopenharmony_ci      if ((c.option == nir_cursor_before_instr ||
1363bf215546Sopenharmony_ci           c.option == nir_cursor_after_instr) &&
1364bf215546Sopenharmony_ci          c.instr == dce_instr)
1365bf215546Sopenharmony_ci         c = nir_instr_remove(dce_instr);
1366bf215546Sopenharmony_ci      else
1367bf215546Sopenharmony_ci         nir_instr_remove(dce_instr);
1368bf215546Sopenharmony_ci      exec_list_push_tail(&to_free, &dce_instr->node);
1369bf215546Sopenharmony_ci   }
1370bf215546Sopenharmony_ci
1371bf215546Sopenharmony_ci   nir_instr_free_list(&to_free);
1372bf215546Sopenharmony_ci
1373bf215546Sopenharmony_ci   nir_instr_worklist_destroy(worklist);
1374bf215546Sopenharmony_ci
1375bf215546Sopenharmony_ci   return c;
1376bf215546Sopenharmony_ci}
1377bf215546Sopenharmony_ci
1378bf215546Sopenharmony_ci/*@}*/
1379bf215546Sopenharmony_ci
1380bf215546Sopenharmony_civoid
1381bf215546Sopenharmony_cinir_index_local_regs(nir_function_impl *impl)
1382bf215546Sopenharmony_ci{
1383bf215546Sopenharmony_ci   unsigned index = 0;
1384bf215546Sopenharmony_ci   foreach_list_typed(nir_register, reg, node, &impl->registers) {
1385bf215546Sopenharmony_ci      reg->index = index++;
1386bf215546Sopenharmony_ci   }
1387bf215546Sopenharmony_ci   impl->reg_alloc = index;
1388bf215546Sopenharmony_ci}
1389bf215546Sopenharmony_ci
1390bf215546Sopenharmony_cistruct foreach_ssa_def_state {
1391bf215546Sopenharmony_ci   nir_foreach_ssa_def_cb cb;
1392bf215546Sopenharmony_ci   void *client_state;
1393bf215546Sopenharmony_ci};
1394bf215546Sopenharmony_ci
1395bf215546Sopenharmony_cistatic inline bool
1396bf215546Sopenharmony_cinir_ssa_def_visitor(nir_dest *dest, void *void_state)
1397bf215546Sopenharmony_ci{
1398bf215546Sopenharmony_ci   struct foreach_ssa_def_state *state = void_state;
1399bf215546Sopenharmony_ci
1400bf215546Sopenharmony_ci   if (dest->is_ssa)
1401bf215546Sopenharmony_ci      return state->cb(&dest->ssa, state->client_state);
1402bf215546Sopenharmony_ci   else
1403bf215546Sopenharmony_ci      return true;
1404bf215546Sopenharmony_ci}
1405bf215546Sopenharmony_ci
1406bf215546Sopenharmony_cibool
1407bf215546Sopenharmony_cinir_foreach_ssa_def(nir_instr *instr, nir_foreach_ssa_def_cb cb, void *state)
1408bf215546Sopenharmony_ci{
1409bf215546Sopenharmony_ci   switch (instr->type) {
1410bf215546Sopenharmony_ci   case nir_instr_type_alu:
1411bf215546Sopenharmony_ci   case nir_instr_type_deref:
1412bf215546Sopenharmony_ci   case nir_instr_type_tex:
1413bf215546Sopenharmony_ci   case nir_instr_type_intrinsic:
1414bf215546Sopenharmony_ci   case nir_instr_type_phi:
1415bf215546Sopenharmony_ci   case nir_instr_type_parallel_copy: {
1416bf215546Sopenharmony_ci      struct foreach_ssa_def_state foreach_state = {cb, state};
1417bf215546Sopenharmony_ci      return nir_foreach_dest(instr, nir_ssa_def_visitor, &foreach_state);
1418bf215546Sopenharmony_ci   }
1419bf215546Sopenharmony_ci
1420bf215546Sopenharmony_ci   case nir_instr_type_load_const:
1421bf215546Sopenharmony_ci      return cb(&nir_instr_as_load_const(instr)->def, state);
1422bf215546Sopenharmony_ci   case nir_instr_type_ssa_undef:
1423bf215546Sopenharmony_ci      return cb(&nir_instr_as_ssa_undef(instr)->def, state);
1424bf215546Sopenharmony_ci   case nir_instr_type_call:
1425bf215546Sopenharmony_ci   case nir_instr_type_jump:
1426bf215546Sopenharmony_ci      return true;
1427bf215546Sopenharmony_ci   default:
1428bf215546Sopenharmony_ci      unreachable("Invalid instruction type");
1429bf215546Sopenharmony_ci   }
1430bf215546Sopenharmony_ci}
1431bf215546Sopenharmony_ci
1432bf215546Sopenharmony_cinir_ssa_def *
1433bf215546Sopenharmony_cinir_instr_ssa_def(nir_instr *instr)
1434bf215546Sopenharmony_ci{
1435bf215546Sopenharmony_ci   switch (instr->type) {
1436bf215546Sopenharmony_ci   case nir_instr_type_alu:
1437bf215546Sopenharmony_ci      assert(nir_instr_as_alu(instr)->dest.dest.is_ssa);
1438bf215546Sopenharmony_ci      return &nir_instr_as_alu(instr)->dest.dest.ssa;
1439bf215546Sopenharmony_ci
1440bf215546Sopenharmony_ci   case nir_instr_type_deref:
1441bf215546Sopenharmony_ci      assert(nir_instr_as_deref(instr)->dest.is_ssa);
1442bf215546Sopenharmony_ci      return &nir_instr_as_deref(instr)->dest.ssa;
1443bf215546Sopenharmony_ci
1444bf215546Sopenharmony_ci   case nir_instr_type_tex:
1445bf215546Sopenharmony_ci      assert(nir_instr_as_tex(instr)->dest.is_ssa);
1446bf215546Sopenharmony_ci      return &nir_instr_as_tex(instr)->dest.ssa;
1447bf215546Sopenharmony_ci
1448bf215546Sopenharmony_ci   case nir_instr_type_intrinsic: {
1449bf215546Sopenharmony_ci      nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
1450bf215546Sopenharmony_ci      if (nir_intrinsic_infos[intrin->intrinsic].has_dest) {
1451bf215546Sopenharmony_ci         assert(intrin->dest.is_ssa);
1452bf215546Sopenharmony_ci         return &intrin->dest.ssa;
1453bf215546Sopenharmony_ci      } else {
1454bf215546Sopenharmony_ci         return NULL;
1455bf215546Sopenharmony_ci      }
1456bf215546Sopenharmony_ci   }
1457bf215546Sopenharmony_ci
1458bf215546Sopenharmony_ci   case nir_instr_type_phi:
1459bf215546Sopenharmony_ci      assert(nir_instr_as_phi(instr)->dest.is_ssa);
1460bf215546Sopenharmony_ci      return &nir_instr_as_phi(instr)->dest.ssa;
1461bf215546Sopenharmony_ci
1462bf215546Sopenharmony_ci   case nir_instr_type_parallel_copy:
1463bf215546Sopenharmony_ci      unreachable("Parallel copies are unsupported by this function");
1464bf215546Sopenharmony_ci
1465bf215546Sopenharmony_ci   case nir_instr_type_load_const:
1466bf215546Sopenharmony_ci      return &nir_instr_as_load_const(instr)->def;
1467bf215546Sopenharmony_ci
1468bf215546Sopenharmony_ci   case nir_instr_type_ssa_undef:
1469bf215546Sopenharmony_ci      return &nir_instr_as_ssa_undef(instr)->def;
1470bf215546Sopenharmony_ci
1471bf215546Sopenharmony_ci   case nir_instr_type_call:
1472bf215546Sopenharmony_ci   case nir_instr_type_jump:
1473bf215546Sopenharmony_ci      return NULL;
1474bf215546Sopenharmony_ci   }
1475bf215546Sopenharmony_ci
1476bf215546Sopenharmony_ci   unreachable("Invalid instruction type");
1477bf215546Sopenharmony_ci}
1478bf215546Sopenharmony_ci
1479bf215546Sopenharmony_cibool
1480bf215546Sopenharmony_cinir_instr_def_is_register(nir_instr *instr)
1481bf215546Sopenharmony_ci{
1482bf215546Sopenharmony_ci   switch (instr->type) {
1483bf215546Sopenharmony_ci   case nir_instr_type_alu:
1484bf215546Sopenharmony_ci      return !nir_instr_as_alu(instr)->dest.dest.is_ssa;
1485bf215546Sopenharmony_ci
1486bf215546Sopenharmony_ci   case nir_instr_type_deref:
1487bf215546Sopenharmony_ci      return !nir_instr_as_deref(instr)->dest.is_ssa;
1488bf215546Sopenharmony_ci
1489bf215546Sopenharmony_ci   case nir_instr_type_tex:
1490bf215546Sopenharmony_ci      return !nir_instr_as_tex(instr)->dest.is_ssa;
1491bf215546Sopenharmony_ci
1492bf215546Sopenharmony_ci   case nir_instr_type_intrinsic: {
1493bf215546Sopenharmony_ci      nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
1494bf215546Sopenharmony_ci      return nir_intrinsic_infos[intrin->intrinsic].has_dest &&
1495bf215546Sopenharmony_ci             !intrin->dest.is_ssa;
1496bf215546Sopenharmony_ci   }
1497bf215546Sopenharmony_ci
1498bf215546Sopenharmony_ci   case nir_instr_type_phi:
1499bf215546Sopenharmony_ci      return !nir_instr_as_phi(instr)->dest.is_ssa;
1500bf215546Sopenharmony_ci
1501bf215546Sopenharmony_ci   case nir_instr_type_parallel_copy:
1502bf215546Sopenharmony_ci      unreachable("Parallel copies are unsupported by this function");
1503bf215546Sopenharmony_ci
1504bf215546Sopenharmony_ci   case nir_instr_type_load_const:
1505bf215546Sopenharmony_ci   case nir_instr_type_ssa_undef:
1506bf215546Sopenharmony_ci      return false;
1507bf215546Sopenharmony_ci
1508bf215546Sopenharmony_ci   case nir_instr_type_call:
1509bf215546Sopenharmony_ci   case nir_instr_type_jump:
1510bf215546Sopenharmony_ci      return false;
1511bf215546Sopenharmony_ci   }
1512bf215546Sopenharmony_ci
1513bf215546Sopenharmony_ci   unreachable("Invalid instruction type");
1514bf215546Sopenharmony_ci}
1515bf215546Sopenharmony_ci
1516bf215546Sopenharmony_cibool
1517bf215546Sopenharmony_cinir_foreach_phi_src_leaving_block(nir_block *block,
1518bf215546Sopenharmony_ci                                  nir_foreach_src_cb cb,
1519bf215546Sopenharmony_ci                                  void *state)
1520bf215546Sopenharmony_ci{
1521bf215546Sopenharmony_ci   for (unsigned i = 0; i < ARRAY_SIZE(block->successors); i++) {
1522bf215546Sopenharmony_ci      if (block->successors[i] == NULL)
1523bf215546Sopenharmony_ci         continue;
1524bf215546Sopenharmony_ci
1525bf215546Sopenharmony_ci      nir_foreach_instr(instr, block->successors[i]) {
1526bf215546Sopenharmony_ci         if (instr->type != nir_instr_type_phi)
1527bf215546Sopenharmony_ci            break;
1528bf215546Sopenharmony_ci
1529bf215546Sopenharmony_ci         nir_phi_instr *phi = nir_instr_as_phi(instr);
1530bf215546Sopenharmony_ci         nir_foreach_phi_src(phi_src, phi) {
1531bf215546Sopenharmony_ci            if (phi_src->pred == block) {
1532bf215546Sopenharmony_ci               if (!cb(&phi_src->src, state))
1533bf215546Sopenharmony_ci                  return false;
1534bf215546Sopenharmony_ci            }
1535bf215546Sopenharmony_ci         }
1536bf215546Sopenharmony_ci      }
1537bf215546Sopenharmony_ci   }
1538bf215546Sopenharmony_ci
1539bf215546Sopenharmony_ci   return true;
1540bf215546Sopenharmony_ci}
1541bf215546Sopenharmony_ci
1542bf215546Sopenharmony_cinir_const_value
1543bf215546Sopenharmony_cinir_const_value_for_float(double f, unsigned bit_size)
1544bf215546Sopenharmony_ci{
1545bf215546Sopenharmony_ci   nir_const_value v;
1546bf215546Sopenharmony_ci   memset(&v, 0, sizeof(v));
1547bf215546Sopenharmony_ci
1548bf215546Sopenharmony_ci   switch (bit_size) {
1549bf215546Sopenharmony_ci   case 16:
1550bf215546Sopenharmony_ci      v.u16 = _mesa_float_to_half(f);
1551bf215546Sopenharmony_ci      break;
1552bf215546Sopenharmony_ci   case 32:
1553bf215546Sopenharmony_ci      v.f32 = f;
1554bf215546Sopenharmony_ci      break;
1555bf215546Sopenharmony_ci   case 64:
1556bf215546Sopenharmony_ci      v.f64 = f;
1557bf215546Sopenharmony_ci      break;
1558bf215546Sopenharmony_ci   default:
1559bf215546Sopenharmony_ci      unreachable("Invalid bit size");
1560bf215546Sopenharmony_ci   }
1561bf215546Sopenharmony_ci
1562bf215546Sopenharmony_ci   return v;
1563bf215546Sopenharmony_ci}
1564bf215546Sopenharmony_ci
1565bf215546Sopenharmony_cidouble
1566bf215546Sopenharmony_cinir_const_value_as_float(nir_const_value value, unsigned bit_size)
1567bf215546Sopenharmony_ci{
1568bf215546Sopenharmony_ci   switch (bit_size) {
1569bf215546Sopenharmony_ci   case 16: return _mesa_half_to_float(value.u16);
1570bf215546Sopenharmony_ci   case 32: return value.f32;
1571bf215546Sopenharmony_ci   case 64: return value.f64;
1572bf215546Sopenharmony_ci   default:
1573bf215546Sopenharmony_ci      unreachable("Invalid bit size");
1574bf215546Sopenharmony_ci   }
1575bf215546Sopenharmony_ci}
1576bf215546Sopenharmony_ci
1577bf215546Sopenharmony_cinir_const_value *
1578bf215546Sopenharmony_cinir_src_as_const_value(nir_src src)
1579bf215546Sopenharmony_ci{
1580bf215546Sopenharmony_ci   if (!src.is_ssa)
1581bf215546Sopenharmony_ci      return NULL;
1582bf215546Sopenharmony_ci
1583bf215546Sopenharmony_ci   if (src.ssa->parent_instr->type != nir_instr_type_load_const)
1584bf215546Sopenharmony_ci      return NULL;
1585bf215546Sopenharmony_ci
1586bf215546Sopenharmony_ci   nir_load_const_instr *load = nir_instr_as_load_const(src.ssa->parent_instr);
1587bf215546Sopenharmony_ci
1588bf215546Sopenharmony_ci   return load->value;
1589bf215546Sopenharmony_ci}
1590bf215546Sopenharmony_ci
1591bf215546Sopenharmony_ci/**
1592bf215546Sopenharmony_ci * Returns true if the source is known to be always uniform. Otherwise it
1593bf215546Sopenharmony_ci * returns false which means it may or may not be uniform but it can't be
1594bf215546Sopenharmony_ci * determined.
1595bf215546Sopenharmony_ci *
1596bf215546Sopenharmony_ci * For a more precise analysis of uniform values, use nir_divergence_analysis.
1597bf215546Sopenharmony_ci */
1598bf215546Sopenharmony_cibool
1599bf215546Sopenharmony_cinir_src_is_always_uniform(nir_src src)
1600bf215546Sopenharmony_ci{
1601bf215546Sopenharmony_ci   if (!src.is_ssa)
1602bf215546Sopenharmony_ci      return false;
1603bf215546Sopenharmony_ci
1604bf215546Sopenharmony_ci   /* Constants are trivially uniform */
1605bf215546Sopenharmony_ci   if (src.ssa->parent_instr->type == nir_instr_type_load_const)
1606bf215546Sopenharmony_ci      return true;
1607bf215546Sopenharmony_ci
1608bf215546Sopenharmony_ci   if (src.ssa->parent_instr->type == nir_instr_type_intrinsic) {
1609bf215546Sopenharmony_ci      nir_intrinsic_instr *intr = nir_instr_as_intrinsic(src.ssa->parent_instr);
1610bf215546Sopenharmony_ci      /* As are uniform variables */
1611bf215546Sopenharmony_ci      if (intr->intrinsic == nir_intrinsic_load_uniform &&
1612bf215546Sopenharmony_ci          nir_src_is_always_uniform(intr->src[0]))
1613bf215546Sopenharmony_ci         return true;
1614bf215546Sopenharmony_ci      /* From the Vulkan specification 15.6.1. Push Constant Interface:
1615bf215546Sopenharmony_ci       * "Any member of a push constant block that is declared as an array must
1616bf215546Sopenharmony_ci       * only be accessed with dynamically uniform indices."
1617bf215546Sopenharmony_ci       */
1618bf215546Sopenharmony_ci      if (intr->intrinsic == nir_intrinsic_load_push_constant)
1619bf215546Sopenharmony_ci         return true;
1620bf215546Sopenharmony_ci      if (intr->intrinsic == nir_intrinsic_load_deref &&
1621bf215546Sopenharmony_ci          nir_deref_mode_is(nir_src_as_deref(intr->src[0]), nir_var_mem_push_const))
1622bf215546Sopenharmony_ci         return true;
1623bf215546Sopenharmony_ci   }
1624bf215546Sopenharmony_ci
1625bf215546Sopenharmony_ci   /* Operating together uniform expressions produces a uniform result */
1626bf215546Sopenharmony_ci   if (src.ssa->parent_instr->type == nir_instr_type_alu) {
1627bf215546Sopenharmony_ci      nir_alu_instr *alu = nir_instr_as_alu(src.ssa->parent_instr);
1628bf215546Sopenharmony_ci      for (int i = 0; i < nir_op_infos[alu->op].num_inputs; i++) {
1629bf215546Sopenharmony_ci         if (!nir_src_is_always_uniform(alu->src[i].src))
1630bf215546Sopenharmony_ci            return false;
1631bf215546Sopenharmony_ci      }
1632bf215546Sopenharmony_ci
1633bf215546Sopenharmony_ci      return true;
1634bf215546Sopenharmony_ci   }
1635bf215546Sopenharmony_ci
1636bf215546Sopenharmony_ci   /* XXX: this could have many more tests, such as when a sampler function is
1637bf215546Sopenharmony_ci    * called with uniform arguments.
1638bf215546Sopenharmony_ci    */
1639bf215546Sopenharmony_ci   return false;
1640bf215546Sopenharmony_ci}
1641bf215546Sopenharmony_ci
1642bf215546Sopenharmony_cistatic void
1643bf215546Sopenharmony_cisrc_remove_all_uses(nir_src *src)
1644bf215546Sopenharmony_ci{
1645bf215546Sopenharmony_ci   for (; src; src = src->is_ssa ? NULL : src->reg.indirect) {
1646bf215546Sopenharmony_ci      if (!src_is_valid(src))
1647bf215546Sopenharmony_ci         continue;
1648bf215546Sopenharmony_ci
1649bf215546Sopenharmony_ci      list_del(&src->use_link);
1650bf215546Sopenharmony_ci   }
1651bf215546Sopenharmony_ci}
1652bf215546Sopenharmony_ci
1653bf215546Sopenharmony_cistatic void
1654bf215546Sopenharmony_cisrc_add_all_uses(nir_src *src, nir_instr *parent_instr, nir_if *parent_if)
1655bf215546Sopenharmony_ci{
1656bf215546Sopenharmony_ci   for (; src; src = src->is_ssa ? NULL : src->reg.indirect) {
1657bf215546Sopenharmony_ci      if (!src_is_valid(src))
1658bf215546Sopenharmony_ci         continue;
1659bf215546Sopenharmony_ci
1660bf215546Sopenharmony_ci      if (parent_instr) {
1661bf215546Sopenharmony_ci         src->parent_instr = parent_instr;
1662bf215546Sopenharmony_ci         if (src->is_ssa)
1663bf215546Sopenharmony_ci            list_addtail(&src->use_link, &src->ssa->uses);
1664bf215546Sopenharmony_ci         else
1665bf215546Sopenharmony_ci            list_addtail(&src->use_link, &src->reg.reg->uses);
1666bf215546Sopenharmony_ci      } else {
1667bf215546Sopenharmony_ci         assert(parent_if);
1668bf215546Sopenharmony_ci         src->parent_if = parent_if;
1669bf215546Sopenharmony_ci         if (src->is_ssa)
1670bf215546Sopenharmony_ci            list_addtail(&src->use_link, &src->ssa->if_uses);
1671bf215546Sopenharmony_ci         else
1672bf215546Sopenharmony_ci            list_addtail(&src->use_link, &src->reg.reg->if_uses);
1673bf215546Sopenharmony_ci      }
1674bf215546Sopenharmony_ci   }
1675bf215546Sopenharmony_ci}
1676bf215546Sopenharmony_ci
1677bf215546Sopenharmony_civoid
1678bf215546Sopenharmony_cinir_instr_rewrite_src(nir_instr *instr, nir_src *src, nir_src new_src)
1679bf215546Sopenharmony_ci{
1680bf215546Sopenharmony_ci   assert(!src_is_valid(src) || src->parent_instr == instr);
1681bf215546Sopenharmony_ci
1682bf215546Sopenharmony_ci   src_remove_all_uses(src);
1683bf215546Sopenharmony_ci   nir_src_copy(src, &new_src);
1684bf215546Sopenharmony_ci   src_add_all_uses(src, instr, NULL);
1685bf215546Sopenharmony_ci}
1686bf215546Sopenharmony_ci
1687bf215546Sopenharmony_civoid
1688bf215546Sopenharmony_cinir_instr_move_src(nir_instr *dest_instr, nir_src *dest, nir_src *src)
1689bf215546Sopenharmony_ci{
1690bf215546Sopenharmony_ci   assert(!src_is_valid(dest) || dest->parent_instr == dest_instr);
1691bf215546Sopenharmony_ci
1692bf215546Sopenharmony_ci   src_remove_all_uses(dest);
1693bf215546Sopenharmony_ci   src_free_indirects(dest);
1694bf215546Sopenharmony_ci   src_remove_all_uses(src);
1695bf215546Sopenharmony_ci   *dest = *src;
1696bf215546Sopenharmony_ci   *src = NIR_SRC_INIT;
1697bf215546Sopenharmony_ci   src_add_all_uses(dest, dest_instr, NULL);
1698bf215546Sopenharmony_ci}
1699bf215546Sopenharmony_ci
1700bf215546Sopenharmony_civoid
1701bf215546Sopenharmony_cinir_if_rewrite_condition(nir_if *if_stmt, nir_src new_src)
1702bf215546Sopenharmony_ci{
1703bf215546Sopenharmony_ci   nir_src *src = &if_stmt->condition;
1704bf215546Sopenharmony_ci   assert(!src_is_valid(src) || src->parent_if == if_stmt);
1705bf215546Sopenharmony_ci
1706bf215546Sopenharmony_ci   src_remove_all_uses(src);
1707bf215546Sopenharmony_ci   nir_src_copy(src, &new_src);
1708bf215546Sopenharmony_ci   src_add_all_uses(src, NULL, if_stmt);
1709bf215546Sopenharmony_ci}
1710bf215546Sopenharmony_ci
1711bf215546Sopenharmony_civoid
1712bf215546Sopenharmony_cinir_instr_rewrite_dest(nir_instr *instr, nir_dest *dest, nir_dest new_dest)
1713bf215546Sopenharmony_ci{
1714bf215546Sopenharmony_ci   if (dest->is_ssa) {
1715bf215546Sopenharmony_ci      /* We can only overwrite an SSA destination if it has no uses. */
1716bf215546Sopenharmony_ci      assert(nir_ssa_def_is_unused(&dest->ssa));
1717bf215546Sopenharmony_ci   } else {
1718bf215546Sopenharmony_ci      list_del(&dest->reg.def_link);
1719bf215546Sopenharmony_ci      if (dest->reg.indirect)
1720bf215546Sopenharmony_ci         src_remove_all_uses(dest->reg.indirect);
1721bf215546Sopenharmony_ci   }
1722bf215546Sopenharmony_ci
1723bf215546Sopenharmony_ci   /* We can't re-write with an SSA def */
1724bf215546Sopenharmony_ci   assert(!new_dest.is_ssa);
1725bf215546Sopenharmony_ci
1726bf215546Sopenharmony_ci   nir_dest_copy(dest, &new_dest);
1727bf215546Sopenharmony_ci
1728bf215546Sopenharmony_ci   dest->reg.parent_instr = instr;
1729bf215546Sopenharmony_ci   list_addtail(&dest->reg.def_link, &new_dest.reg.reg->defs);
1730bf215546Sopenharmony_ci
1731bf215546Sopenharmony_ci   if (dest->reg.indirect)
1732bf215546Sopenharmony_ci      src_add_all_uses(dest->reg.indirect, instr, NULL);
1733bf215546Sopenharmony_ci}
1734bf215546Sopenharmony_ci
1735bf215546Sopenharmony_ci/* note: does *not* take ownership of 'name' */
1736bf215546Sopenharmony_civoid
1737bf215546Sopenharmony_cinir_ssa_def_init(nir_instr *instr, nir_ssa_def *def,
1738bf215546Sopenharmony_ci                 unsigned num_components,
1739bf215546Sopenharmony_ci                 unsigned bit_size)
1740bf215546Sopenharmony_ci{
1741bf215546Sopenharmony_ci   def->parent_instr = instr;
1742bf215546Sopenharmony_ci   list_inithead(&def->uses);
1743bf215546Sopenharmony_ci   list_inithead(&def->if_uses);
1744bf215546Sopenharmony_ci   def->num_components = num_components;
1745bf215546Sopenharmony_ci   def->bit_size = bit_size;
1746bf215546Sopenharmony_ci   def->divergent = true; /* This is the safer default */
1747bf215546Sopenharmony_ci
1748bf215546Sopenharmony_ci   if (instr->block) {
1749bf215546Sopenharmony_ci      nir_function_impl *impl =
1750bf215546Sopenharmony_ci         nir_cf_node_get_function(&instr->block->cf_node);
1751bf215546Sopenharmony_ci
1752bf215546Sopenharmony_ci      def->index = impl->ssa_alloc++;
1753bf215546Sopenharmony_ci
1754bf215546Sopenharmony_ci      impl->valid_metadata &= ~nir_metadata_live_ssa_defs;
1755bf215546Sopenharmony_ci   } else {
1756bf215546Sopenharmony_ci      def->index = UINT_MAX;
1757bf215546Sopenharmony_ci   }
1758bf215546Sopenharmony_ci}
1759bf215546Sopenharmony_ci
1760bf215546Sopenharmony_ci/* note: does *not* take ownership of 'name' */
1761bf215546Sopenharmony_civoid
1762bf215546Sopenharmony_cinir_ssa_dest_init(nir_instr *instr, nir_dest *dest,
1763bf215546Sopenharmony_ci                 unsigned num_components, unsigned bit_size,
1764bf215546Sopenharmony_ci                 const char *name)
1765bf215546Sopenharmony_ci{
1766bf215546Sopenharmony_ci   dest->is_ssa = true;
1767bf215546Sopenharmony_ci   nir_ssa_def_init(instr, &dest->ssa, num_components, bit_size);
1768bf215546Sopenharmony_ci}
1769bf215546Sopenharmony_ci
1770bf215546Sopenharmony_civoid
1771bf215546Sopenharmony_cinir_ssa_def_rewrite_uses(nir_ssa_def *def, nir_ssa_def *new_ssa)
1772bf215546Sopenharmony_ci{
1773bf215546Sopenharmony_ci   assert(def != new_ssa);
1774bf215546Sopenharmony_ci   nir_foreach_use_safe(use_src, def)
1775bf215546Sopenharmony_ci      nir_instr_rewrite_src_ssa(use_src->parent_instr, use_src, new_ssa);
1776bf215546Sopenharmony_ci
1777bf215546Sopenharmony_ci   nir_foreach_if_use_safe(use_src, def)
1778bf215546Sopenharmony_ci      nir_if_rewrite_condition_ssa(use_src->parent_if, use_src, new_ssa);
1779bf215546Sopenharmony_ci}
1780bf215546Sopenharmony_ci
1781bf215546Sopenharmony_civoid
1782bf215546Sopenharmony_cinir_ssa_def_rewrite_uses_src(nir_ssa_def *def, nir_src new_src)
1783bf215546Sopenharmony_ci{
1784bf215546Sopenharmony_ci   if (new_src.is_ssa) {
1785bf215546Sopenharmony_ci      nir_ssa_def_rewrite_uses(def, new_src.ssa);
1786bf215546Sopenharmony_ci   } else {
1787bf215546Sopenharmony_ci      nir_foreach_use_safe(use_src, def)
1788bf215546Sopenharmony_ci         nir_instr_rewrite_src(use_src->parent_instr, use_src, new_src);
1789bf215546Sopenharmony_ci
1790bf215546Sopenharmony_ci      nir_foreach_if_use_safe(use_src, def)
1791bf215546Sopenharmony_ci         nir_if_rewrite_condition(use_src->parent_if, new_src);
1792bf215546Sopenharmony_ci   }
1793bf215546Sopenharmony_ci}
1794bf215546Sopenharmony_ci
1795bf215546Sopenharmony_cistatic bool
1796bf215546Sopenharmony_ciis_instr_between(nir_instr *start, nir_instr *end, nir_instr *between)
1797bf215546Sopenharmony_ci{
1798bf215546Sopenharmony_ci   assert(start->block == end->block);
1799bf215546Sopenharmony_ci
1800bf215546Sopenharmony_ci   if (between->block != start->block)
1801bf215546Sopenharmony_ci      return false;
1802bf215546Sopenharmony_ci
1803bf215546Sopenharmony_ci   /* Search backwards looking for "between" */
1804bf215546Sopenharmony_ci   while (start != end) {
1805bf215546Sopenharmony_ci      if (between == end)
1806bf215546Sopenharmony_ci         return true;
1807bf215546Sopenharmony_ci
1808bf215546Sopenharmony_ci      end = nir_instr_prev(end);
1809bf215546Sopenharmony_ci      assert(end);
1810bf215546Sopenharmony_ci   }
1811bf215546Sopenharmony_ci
1812bf215546Sopenharmony_ci   return false;
1813bf215546Sopenharmony_ci}
1814bf215546Sopenharmony_ci
1815bf215546Sopenharmony_ci/* Replaces all uses of the given SSA def with the given source but only if
1816bf215546Sopenharmony_ci * the use comes after the after_me instruction.  This can be useful if you
1817bf215546Sopenharmony_ci * are emitting code to fix up the result of some instruction: you can freely
1818bf215546Sopenharmony_ci * use the result in that code and then call rewrite_uses_after and pass the
1819bf215546Sopenharmony_ci * last fixup instruction as after_me and it will replace all of the uses you
1820bf215546Sopenharmony_ci * want without touching the fixup code.
1821bf215546Sopenharmony_ci *
1822bf215546Sopenharmony_ci * This function assumes that after_me is in the same block as
1823bf215546Sopenharmony_ci * def->parent_instr and that after_me comes after def->parent_instr.
1824bf215546Sopenharmony_ci */
1825bf215546Sopenharmony_civoid
1826bf215546Sopenharmony_cinir_ssa_def_rewrite_uses_after(nir_ssa_def *def, nir_ssa_def *new_ssa,
1827bf215546Sopenharmony_ci                               nir_instr *after_me)
1828bf215546Sopenharmony_ci{
1829bf215546Sopenharmony_ci   if (def == new_ssa)
1830bf215546Sopenharmony_ci      return;
1831bf215546Sopenharmony_ci
1832bf215546Sopenharmony_ci   nir_foreach_use_safe(use_src, def) {
1833bf215546Sopenharmony_ci      assert(use_src->parent_instr != def->parent_instr);
1834bf215546Sopenharmony_ci      /* Since def already dominates all of its uses, the only way a use can
1835bf215546Sopenharmony_ci       * not be dominated by after_me is if it is between def and after_me in
1836bf215546Sopenharmony_ci       * the instruction list.
1837bf215546Sopenharmony_ci       */
1838bf215546Sopenharmony_ci      if (!is_instr_between(def->parent_instr, after_me, use_src->parent_instr))
1839bf215546Sopenharmony_ci         nir_instr_rewrite_src_ssa(use_src->parent_instr, use_src, new_ssa);
1840bf215546Sopenharmony_ci   }
1841bf215546Sopenharmony_ci
1842bf215546Sopenharmony_ci   nir_foreach_if_use_safe(use_src, def) {
1843bf215546Sopenharmony_ci      nir_if_rewrite_condition_ssa(use_src->parent_if,
1844bf215546Sopenharmony_ci                                   &use_src->parent_if->condition,
1845bf215546Sopenharmony_ci                                   new_ssa);
1846bf215546Sopenharmony_ci   }
1847bf215546Sopenharmony_ci}
1848bf215546Sopenharmony_ci
1849bf215546Sopenharmony_cistatic nir_ssa_def *
1850bf215546Sopenharmony_ciget_store_value(nir_intrinsic_instr *intrin)
1851bf215546Sopenharmony_ci{
1852bf215546Sopenharmony_ci   assert(nir_intrinsic_has_write_mask(intrin));
1853bf215546Sopenharmony_ci   /* deref stores have the deref in src[0] and the store value in src[1] */
1854bf215546Sopenharmony_ci   if (intrin->intrinsic == nir_intrinsic_store_deref ||
1855bf215546Sopenharmony_ci       intrin->intrinsic == nir_intrinsic_store_deref_block_intel)
1856bf215546Sopenharmony_ci      return intrin->src[1].ssa;
1857bf215546Sopenharmony_ci
1858bf215546Sopenharmony_ci   /* all other stores have the store value in src[0] */
1859bf215546Sopenharmony_ci   return intrin->src[0].ssa;
1860bf215546Sopenharmony_ci}
1861bf215546Sopenharmony_ci
1862bf215546Sopenharmony_cinir_component_mask_t
1863bf215546Sopenharmony_cinir_src_components_read(const nir_src *src)
1864bf215546Sopenharmony_ci{
1865bf215546Sopenharmony_ci   assert(src->is_ssa && src->parent_instr);
1866bf215546Sopenharmony_ci
1867bf215546Sopenharmony_ci   if (src->parent_instr->type == nir_instr_type_alu) {
1868bf215546Sopenharmony_ci      nir_alu_instr *alu = nir_instr_as_alu(src->parent_instr);
1869bf215546Sopenharmony_ci      nir_alu_src *alu_src = exec_node_data(nir_alu_src, src, src);
1870bf215546Sopenharmony_ci      int src_idx = alu_src - &alu->src[0];
1871bf215546Sopenharmony_ci      assert(src_idx >= 0 && src_idx < nir_op_infos[alu->op].num_inputs);
1872bf215546Sopenharmony_ci      return nir_alu_instr_src_read_mask(alu, src_idx);
1873bf215546Sopenharmony_ci   } else if (src->parent_instr->type == nir_instr_type_intrinsic) {
1874bf215546Sopenharmony_ci      nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(src->parent_instr);
1875bf215546Sopenharmony_ci      if (nir_intrinsic_has_write_mask(intrin) && src->ssa == get_store_value(intrin))
1876bf215546Sopenharmony_ci         return nir_intrinsic_write_mask(intrin);
1877bf215546Sopenharmony_ci      else
1878bf215546Sopenharmony_ci         return (1 << src->ssa->num_components) - 1;
1879bf215546Sopenharmony_ci   } else {
1880bf215546Sopenharmony_ci      return (1 << src->ssa->num_components) - 1;
1881bf215546Sopenharmony_ci   }
1882bf215546Sopenharmony_ci}
1883bf215546Sopenharmony_ci
1884bf215546Sopenharmony_cinir_component_mask_t
1885bf215546Sopenharmony_cinir_ssa_def_components_read(const nir_ssa_def *def)
1886bf215546Sopenharmony_ci{
1887bf215546Sopenharmony_ci   nir_component_mask_t read_mask = 0;
1888bf215546Sopenharmony_ci
1889bf215546Sopenharmony_ci   if (!list_is_empty(&def->if_uses))
1890bf215546Sopenharmony_ci      read_mask |= 1;
1891bf215546Sopenharmony_ci
1892bf215546Sopenharmony_ci   nir_foreach_use(use, def) {
1893bf215546Sopenharmony_ci      read_mask |= nir_src_components_read(use);
1894bf215546Sopenharmony_ci      if (read_mask == (1 << def->num_components) - 1)
1895bf215546Sopenharmony_ci         return read_mask;
1896bf215546Sopenharmony_ci   }
1897bf215546Sopenharmony_ci
1898bf215546Sopenharmony_ci   return read_mask;
1899bf215546Sopenharmony_ci}
1900bf215546Sopenharmony_ci
1901bf215546Sopenharmony_cinir_block *
1902bf215546Sopenharmony_cinir_block_unstructured_next(nir_block *block)
1903bf215546Sopenharmony_ci{
1904bf215546Sopenharmony_ci   if (block == NULL) {
1905bf215546Sopenharmony_ci      /* nir_foreach_block_unstructured_safe() will call this function on a
1906bf215546Sopenharmony_ci       * NULL block after the last iteration, but it won't use the result so
1907bf215546Sopenharmony_ci       * just return NULL here.
1908bf215546Sopenharmony_ci       */
1909bf215546Sopenharmony_ci      return NULL;
1910bf215546Sopenharmony_ci   }
1911bf215546Sopenharmony_ci
1912bf215546Sopenharmony_ci   nir_cf_node *cf_next = nir_cf_node_next(&block->cf_node);
1913bf215546Sopenharmony_ci   if (cf_next == NULL && block->cf_node.parent->type == nir_cf_node_function)
1914bf215546Sopenharmony_ci      return NULL;
1915bf215546Sopenharmony_ci
1916bf215546Sopenharmony_ci   if (cf_next && cf_next->type == nir_cf_node_block)
1917bf215546Sopenharmony_ci      return nir_cf_node_as_block(cf_next);
1918bf215546Sopenharmony_ci
1919bf215546Sopenharmony_ci   return nir_block_cf_tree_next(block);
1920bf215546Sopenharmony_ci}
1921bf215546Sopenharmony_ci
1922bf215546Sopenharmony_cinir_block *
1923bf215546Sopenharmony_cinir_unstructured_start_block(nir_function_impl *impl)
1924bf215546Sopenharmony_ci{
1925bf215546Sopenharmony_ci   return nir_start_block(impl);
1926bf215546Sopenharmony_ci}
1927bf215546Sopenharmony_ci
1928bf215546Sopenharmony_cinir_block *
1929bf215546Sopenharmony_cinir_block_cf_tree_next(nir_block *block)
1930bf215546Sopenharmony_ci{
1931bf215546Sopenharmony_ci   if (block == NULL) {
1932bf215546Sopenharmony_ci      /* nir_foreach_block_safe() will call this function on a NULL block
1933bf215546Sopenharmony_ci       * after the last iteration, but it won't use the result so just return
1934bf215546Sopenharmony_ci       * NULL here.
1935bf215546Sopenharmony_ci       */
1936bf215546Sopenharmony_ci      return NULL;
1937bf215546Sopenharmony_ci   }
1938bf215546Sopenharmony_ci
1939bf215546Sopenharmony_ci   assert(nir_cf_node_get_function(&block->cf_node)->structured);
1940bf215546Sopenharmony_ci
1941bf215546Sopenharmony_ci   nir_cf_node *cf_next = nir_cf_node_next(&block->cf_node);
1942bf215546Sopenharmony_ci   if (cf_next)
1943bf215546Sopenharmony_ci      return nir_cf_node_cf_tree_first(cf_next);
1944bf215546Sopenharmony_ci
1945bf215546Sopenharmony_ci   nir_cf_node *parent = block->cf_node.parent;
1946bf215546Sopenharmony_ci
1947bf215546Sopenharmony_ci   switch (parent->type) {
1948bf215546Sopenharmony_ci   case nir_cf_node_if: {
1949bf215546Sopenharmony_ci      /* Are we at the end of the if? Go to the beginning of the else */
1950bf215546Sopenharmony_ci      nir_if *if_stmt = nir_cf_node_as_if(parent);
1951bf215546Sopenharmony_ci      if (block == nir_if_last_then_block(if_stmt))
1952bf215546Sopenharmony_ci         return nir_if_first_else_block(if_stmt);
1953bf215546Sopenharmony_ci
1954bf215546Sopenharmony_ci      assert(block == nir_if_last_else_block(if_stmt));
1955bf215546Sopenharmony_ci   }
1956bf215546Sopenharmony_ci   FALLTHROUGH;
1957bf215546Sopenharmony_ci
1958bf215546Sopenharmony_ci   case nir_cf_node_loop:
1959bf215546Sopenharmony_ci      return nir_cf_node_as_block(nir_cf_node_next(parent));
1960bf215546Sopenharmony_ci
1961bf215546Sopenharmony_ci   case nir_cf_node_function:
1962bf215546Sopenharmony_ci      return NULL;
1963bf215546Sopenharmony_ci
1964bf215546Sopenharmony_ci   default:
1965bf215546Sopenharmony_ci      unreachable("unknown cf node type");
1966bf215546Sopenharmony_ci   }
1967bf215546Sopenharmony_ci}
1968bf215546Sopenharmony_ci
1969bf215546Sopenharmony_cinir_block *
1970bf215546Sopenharmony_cinir_block_cf_tree_prev(nir_block *block)
1971bf215546Sopenharmony_ci{
1972bf215546Sopenharmony_ci   if (block == NULL) {
1973bf215546Sopenharmony_ci      /* do this for consistency with nir_block_cf_tree_next() */
1974bf215546Sopenharmony_ci      return NULL;
1975bf215546Sopenharmony_ci   }
1976bf215546Sopenharmony_ci
1977bf215546Sopenharmony_ci   assert(nir_cf_node_get_function(&block->cf_node)->structured);
1978bf215546Sopenharmony_ci
1979bf215546Sopenharmony_ci   nir_cf_node *cf_prev = nir_cf_node_prev(&block->cf_node);
1980bf215546Sopenharmony_ci   if (cf_prev)
1981bf215546Sopenharmony_ci      return nir_cf_node_cf_tree_last(cf_prev);
1982bf215546Sopenharmony_ci
1983bf215546Sopenharmony_ci   nir_cf_node *parent = block->cf_node.parent;
1984bf215546Sopenharmony_ci
1985bf215546Sopenharmony_ci   switch (parent->type) {
1986bf215546Sopenharmony_ci   case nir_cf_node_if: {
1987bf215546Sopenharmony_ci      /* Are we at the beginning of the else? Go to the end of the if */
1988bf215546Sopenharmony_ci      nir_if *if_stmt = nir_cf_node_as_if(parent);
1989bf215546Sopenharmony_ci      if (block == nir_if_first_else_block(if_stmt))
1990bf215546Sopenharmony_ci         return nir_if_last_then_block(if_stmt);
1991bf215546Sopenharmony_ci
1992bf215546Sopenharmony_ci      assert(block == nir_if_first_then_block(if_stmt));
1993bf215546Sopenharmony_ci   }
1994bf215546Sopenharmony_ci   FALLTHROUGH;
1995bf215546Sopenharmony_ci
1996bf215546Sopenharmony_ci   case nir_cf_node_loop:
1997bf215546Sopenharmony_ci      return nir_cf_node_as_block(nir_cf_node_prev(parent));
1998bf215546Sopenharmony_ci
1999bf215546Sopenharmony_ci   case nir_cf_node_function:
2000bf215546Sopenharmony_ci      return NULL;
2001bf215546Sopenharmony_ci
2002bf215546Sopenharmony_ci   default:
2003bf215546Sopenharmony_ci      unreachable("unknown cf node type");
2004bf215546Sopenharmony_ci   }
2005bf215546Sopenharmony_ci}
2006bf215546Sopenharmony_ci
2007bf215546Sopenharmony_cinir_block *nir_cf_node_cf_tree_first(nir_cf_node *node)
2008bf215546Sopenharmony_ci{
2009bf215546Sopenharmony_ci   switch (node->type) {
2010bf215546Sopenharmony_ci   case nir_cf_node_function: {
2011bf215546Sopenharmony_ci      nir_function_impl *impl = nir_cf_node_as_function(node);
2012bf215546Sopenharmony_ci      return nir_start_block(impl);
2013bf215546Sopenharmony_ci   }
2014bf215546Sopenharmony_ci
2015bf215546Sopenharmony_ci   case nir_cf_node_if: {
2016bf215546Sopenharmony_ci      nir_if *if_stmt = nir_cf_node_as_if(node);
2017bf215546Sopenharmony_ci      return nir_if_first_then_block(if_stmt);
2018bf215546Sopenharmony_ci   }
2019bf215546Sopenharmony_ci
2020bf215546Sopenharmony_ci   case nir_cf_node_loop: {
2021bf215546Sopenharmony_ci      nir_loop *loop = nir_cf_node_as_loop(node);
2022bf215546Sopenharmony_ci      return nir_loop_first_block(loop);
2023bf215546Sopenharmony_ci   }
2024bf215546Sopenharmony_ci
2025bf215546Sopenharmony_ci   case nir_cf_node_block: {
2026bf215546Sopenharmony_ci      return nir_cf_node_as_block(node);
2027bf215546Sopenharmony_ci   }
2028bf215546Sopenharmony_ci
2029bf215546Sopenharmony_ci   default:
2030bf215546Sopenharmony_ci      unreachable("unknown node type");
2031bf215546Sopenharmony_ci   }
2032bf215546Sopenharmony_ci}
2033bf215546Sopenharmony_ci
2034bf215546Sopenharmony_cinir_block *nir_cf_node_cf_tree_last(nir_cf_node *node)
2035bf215546Sopenharmony_ci{
2036bf215546Sopenharmony_ci   switch (node->type) {
2037bf215546Sopenharmony_ci   case nir_cf_node_function: {
2038bf215546Sopenharmony_ci      nir_function_impl *impl = nir_cf_node_as_function(node);
2039bf215546Sopenharmony_ci      return nir_impl_last_block(impl);
2040bf215546Sopenharmony_ci   }
2041bf215546Sopenharmony_ci
2042bf215546Sopenharmony_ci   case nir_cf_node_if: {
2043bf215546Sopenharmony_ci      nir_if *if_stmt = nir_cf_node_as_if(node);
2044bf215546Sopenharmony_ci      return nir_if_last_else_block(if_stmt);
2045bf215546Sopenharmony_ci   }
2046bf215546Sopenharmony_ci
2047bf215546Sopenharmony_ci   case nir_cf_node_loop: {
2048bf215546Sopenharmony_ci      nir_loop *loop = nir_cf_node_as_loop(node);
2049bf215546Sopenharmony_ci      return nir_loop_last_block(loop);
2050bf215546Sopenharmony_ci   }
2051bf215546Sopenharmony_ci
2052bf215546Sopenharmony_ci   case nir_cf_node_block: {
2053bf215546Sopenharmony_ci      return nir_cf_node_as_block(node);
2054bf215546Sopenharmony_ci   }
2055bf215546Sopenharmony_ci
2056bf215546Sopenharmony_ci   default:
2057bf215546Sopenharmony_ci      unreachable("unknown node type");
2058bf215546Sopenharmony_ci   }
2059bf215546Sopenharmony_ci}
2060bf215546Sopenharmony_ci
2061bf215546Sopenharmony_cinir_block *nir_cf_node_cf_tree_next(nir_cf_node *node)
2062bf215546Sopenharmony_ci{
2063bf215546Sopenharmony_ci   if (node->type == nir_cf_node_block)
2064bf215546Sopenharmony_ci      return nir_block_cf_tree_next(nir_cf_node_as_block(node));
2065bf215546Sopenharmony_ci   else if (node->type == nir_cf_node_function)
2066bf215546Sopenharmony_ci      return NULL;
2067bf215546Sopenharmony_ci   else
2068bf215546Sopenharmony_ci      return nir_cf_node_as_block(nir_cf_node_next(node));
2069bf215546Sopenharmony_ci}
2070bf215546Sopenharmony_ci
2071bf215546Sopenharmony_cinir_if *
2072bf215546Sopenharmony_cinir_block_get_following_if(nir_block *block)
2073bf215546Sopenharmony_ci{
2074bf215546Sopenharmony_ci   if (exec_node_is_tail_sentinel(&block->cf_node.node))
2075bf215546Sopenharmony_ci      return NULL;
2076bf215546Sopenharmony_ci
2077bf215546Sopenharmony_ci   if (nir_cf_node_is_last(&block->cf_node))
2078bf215546Sopenharmony_ci      return NULL;
2079bf215546Sopenharmony_ci
2080bf215546Sopenharmony_ci   nir_cf_node *next_node = nir_cf_node_next(&block->cf_node);
2081bf215546Sopenharmony_ci
2082bf215546Sopenharmony_ci   if (next_node->type != nir_cf_node_if)
2083bf215546Sopenharmony_ci      return NULL;
2084bf215546Sopenharmony_ci
2085bf215546Sopenharmony_ci   return nir_cf_node_as_if(next_node);
2086bf215546Sopenharmony_ci}
2087bf215546Sopenharmony_ci
2088bf215546Sopenharmony_cinir_loop *
2089bf215546Sopenharmony_cinir_block_get_following_loop(nir_block *block)
2090bf215546Sopenharmony_ci{
2091bf215546Sopenharmony_ci   if (exec_node_is_tail_sentinel(&block->cf_node.node))
2092bf215546Sopenharmony_ci      return NULL;
2093bf215546Sopenharmony_ci
2094bf215546Sopenharmony_ci   if (nir_cf_node_is_last(&block->cf_node))
2095bf215546Sopenharmony_ci      return NULL;
2096bf215546Sopenharmony_ci
2097bf215546Sopenharmony_ci   nir_cf_node *next_node = nir_cf_node_next(&block->cf_node);
2098bf215546Sopenharmony_ci
2099bf215546Sopenharmony_ci   if (next_node->type != nir_cf_node_loop)
2100bf215546Sopenharmony_ci      return NULL;
2101bf215546Sopenharmony_ci
2102bf215546Sopenharmony_ci   return nir_cf_node_as_loop(next_node);
2103bf215546Sopenharmony_ci}
2104bf215546Sopenharmony_ci
2105bf215546Sopenharmony_cistatic int
2106bf215546Sopenharmony_cicompare_block_index(const void *p1, const void *p2)
2107bf215546Sopenharmony_ci{
2108bf215546Sopenharmony_ci   const nir_block *block1 = *((const nir_block **) p1);
2109bf215546Sopenharmony_ci   const nir_block *block2 = *((const nir_block **) p2);
2110bf215546Sopenharmony_ci
2111bf215546Sopenharmony_ci   return (int) block1->index - (int) block2->index;
2112bf215546Sopenharmony_ci}
2113bf215546Sopenharmony_ci
2114bf215546Sopenharmony_cinir_block **
2115bf215546Sopenharmony_cinir_block_get_predecessors_sorted(const nir_block *block, void *mem_ctx)
2116bf215546Sopenharmony_ci{
2117bf215546Sopenharmony_ci   nir_block **preds =
2118bf215546Sopenharmony_ci      ralloc_array(mem_ctx, nir_block *, block->predecessors->entries);
2119bf215546Sopenharmony_ci
2120bf215546Sopenharmony_ci   unsigned i = 0;
2121bf215546Sopenharmony_ci   set_foreach(block->predecessors, entry)
2122bf215546Sopenharmony_ci      preds[i++] = (nir_block *) entry->key;
2123bf215546Sopenharmony_ci   assert(i == block->predecessors->entries);
2124bf215546Sopenharmony_ci
2125bf215546Sopenharmony_ci   qsort(preds, block->predecessors->entries, sizeof(nir_block *),
2126bf215546Sopenharmony_ci         compare_block_index);
2127bf215546Sopenharmony_ci
2128bf215546Sopenharmony_ci   return preds;
2129bf215546Sopenharmony_ci}
2130bf215546Sopenharmony_ci
2131bf215546Sopenharmony_civoid
2132bf215546Sopenharmony_cinir_index_blocks(nir_function_impl *impl)
2133bf215546Sopenharmony_ci{
2134bf215546Sopenharmony_ci   unsigned index = 0;
2135bf215546Sopenharmony_ci
2136bf215546Sopenharmony_ci   if (impl->valid_metadata & nir_metadata_block_index)
2137bf215546Sopenharmony_ci      return;
2138bf215546Sopenharmony_ci
2139bf215546Sopenharmony_ci   nir_foreach_block_unstructured(block, impl) {
2140bf215546Sopenharmony_ci      block->index = index++;
2141bf215546Sopenharmony_ci   }
2142bf215546Sopenharmony_ci
2143bf215546Sopenharmony_ci   /* The end_block isn't really part of the program, which is why its index
2144bf215546Sopenharmony_ci    * is >= num_blocks.
2145bf215546Sopenharmony_ci    */
2146bf215546Sopenharmony_ci   impl->num_blocks = impl->end_block->index = index;
2147bf215546Sopenharmony_ci}
2148bf215546Sopenharmony_ci
2149bf215546Sopenharmony_cistatic bool
2150bf215546Sopenharmony_ciindex_ssa_def_cb(nir_ssa_def *def, void *state)
2151bf215546Sopenharmony_ci{
2152bf215546Sopenharmony_ci   unsigned *index = (unsigned *) state;
2153bf215546Sopenharmony_ci   def->index = (*index)++;
2154bf215546Sopenharmony_ci
2155bf215546Sopenharmony_ci   return true;
2156bf215546Sopenharmony_ci}
2157bf215546Sopenharmony_ci
2158bf215546Sopenharmony_ci/**
2159bf215546Sopenharmony_ci * The indices are applied top-to-bottom which has the very nice property
2160bf215546Sopenharmony_ci * that, if A dominates B, then A->index <= B->index.
2161bf215546Sopenharmony_ci */
2162bf215546Sopenharmony_civoid
2163bf215546Sopenharmony_cinir_index_ssa_defs(nir_function_impl *impl)
2164bf215546Sopenharmony_ci{
2165bf215546Sopenharmony_ci   unsigned index = 0;
2166bf215546Sopenharmony_ci
2167bf215546Sopenharmony_ci   impl->valid_metadata &= ~nir_metadata_live_ssa_defs;
2168bf215546Sopenharmony_ci
2169bf215546Sopenharmony_ci   nir_foreach_block_unstructured(block, impl) {
2170bf215546Sopenharmony_ci      nir_foreach_instr(instr, block)
2171bf215546Sopenharmony_ci         nir_foreach_ssa_def(instr, index_ssa_def_cb, &index);
2172bf215546Sopenharmony_ci   }
2173bf215546Sopenharmony_ci
2174bf215546Sopenharmony_ci   impl->ssa_alloc = index;
2175bf215546Sopenharmony_ci}
2176bf215546Sopenharmony_ci
2177bf215546Sopenharmony_ci/**
2178bf215546Sopenharmony_ci * The indices are applied top-to-bottom which has the very nice property
2179bf215546Sopenharmony_ci * that, if A dominates B, then A->index <= B->index.
2180bf215546Sopenharmony_ci */
2181bf215546Sopenharmony_ciunsigned
2182bf215546Sopenharmony_cinir_index_instrs(nir_function_impl *impl)
2183bf215546Sopenharmony_ci{
2184bf215546Sopenharmony_ci   unsigned index = 0;
2185bf215546Sopenharmony_ci
2186bf215546Sopenharmony_ci   nir_foreach_block(block, impl) {
2187bf215546Sopenharmony_ci      block->start_ip = index++;
2188bf215546Sopenharmony_ci
2189bf215546Sopenharmony_ci      nir_foreach_instr(instr, block)
2190bf215546Sopenharmony_ci         instr->index = index++;
2191bf215546Sopenharmony_ci
2192bf215546Sopenharmony_ci      block->end_ip = index++;
2193bf215546Sopenharmony_ci   }
2194bf215546Sopenharmony_ci
2195bf215546Sopenharmony_ci   return index;
2196bf215546Sopenharmony_ci}
2197bf215546Sopenharmony_ci
2198bf215546Sopenharmony_ciunsigned
2199bf215546Sopenharmony_cinir_shader_index_vars(nir_shader *shader, nir_variable_mode modes)
2200bf215546Sopenharmony_ci{
2201bf215546Sopenharmony_ci   unsigned count = 0;
2202bf215546Sopenharmony_ci   nir_foreach_variable_with_modes(var, shader, modes)
2203bf215546Sopenharmony_ci      var->index = count++;
2204bf215546Sopenharmony_ci   return count;
2205bf215546Sopenharmony_ci}
2206bf215546Sopenharmony_ci
2207bf215546Sopenharmony_ciunsigned
2208bf215546Sopenharmony_cinir_function_impl_index_vars(nir_function_impl *impl)
2209bf215546Sopenharmony_ci{
2210bf215546Sopenharmony_ci   unsigned count = 0;
2211bf215546Sopenharmony_ci   nir_foreach_function_temp_variable(var, impl)
2212bf215546Sopenharmony_ci      var->index = count++;
2213bf215546Sopenharmony_ci   return count;
2214bf215546Sopenharmony_ci}
2215bf215546Sopenharmony_ci
2216bf215546Sopenharmony_cistatic nir_instr *
2217bf215546Sopenharmony_cicursor_next_instr(nir_cursor cursor)
2218bf215546Sopenharmony_ci{
2219bf215546Sopenharmony_ci   switch (cursor.option) {
2220bf215546Sopenharmony_ci   case nir_cursor_before_block:
2221bf215546Sopenharmony_ci      for (nir_block *block = cursor.block; block;
2222bf215546Sopenharmony_ci           block = nir_block_cf_tree_next(block)) {
2223bf215546Sopenharmony_ci         nir_instr *instr = nir_block_first_instr(block);
2224bf215546Sopenharmony_ci         if (instr)
2225bf215546Sopenharmony_ci            return instr;
2226bf215546Sopenharmony_ci      }
2227bf215546Sopenharmony_ci      return NULL;
2228bf215546Sopenharmony_ci
2229bf215546Sopenharmony_ci   case nir_cursor_after_block:
2230bf215546Sopenharmony_ci      cursor.block = nir_block_cf_tree_next(cursor.block);
2231bf215546Sopenharmony_ci      if (cursor.block == NULL)
2232bf215546Sopenharmony_ci         return NULL;
2233bf215546Sopenharmony_ci
2234bf215546Sopenharmony_ci      cursor.option = nir_cursor_before_block;
2235bf215546Sopenharmony_ci      return cursor_next_instr(cursor);
2236bf215546Sopenharmony_ci
2237bf215546Sopenharmony_ci   case nir_cursor_before_instr:
2238bf215546Sopenharmony_ci      return cursor.instr;
2239bf215546Sopenharmony_ci
2240bf215546Sopenharmony_ci   case nir_cursor_after_instr:
2241bf215546Sopenharmony_ci      if (nir_instr_next(cursor.instr))
2242bf215546Sopenharmony_ci         return nir_instr_next(cursor.instr);
2243bf215546Sopenharmony_ci
2244bf215546Sopenharmony_ci      cursor.option = nir_cursor_after_block;
2245bf215546Sopenharmony_ci      cursor.block = cursor.instr->block;
2246bf215546Sopenharmony_ci      return cursor_next_instr(cursor);
2247bf215546Sopenharmony_ci   }
2248bf215546Sopenharmony_ci
2249bf215546Sopenharmony_ci   unreachable("Inavlid cursor option");
2250bf215546Sopenharmony_ci}
2251bf215546Sopenharmony_ci
2252bf215546Sopenharmony_ciASSERTED static bool
2253bf215546Sopenharmony_cidest_is_ssa(nir_dest *dest, void *_state)
2254bf215546Sopenharmony_ci{
2255bf215546Sopenharmony_ci   (void) _state;
2256bf215546Sopenharmony_ci   return dest->is_ssa;
2257bf215546Sopenharmony_ci}
2258bf215546Sopenharmony_ci
2259bf215546Sopenharmony_cibool
2260bf215546Sopenharmony_cinir_function_impl_lower_instructions(nir_function_impl *impl,
2261bf215546Sopenharmony_ci                                     nir_instr_filter_cb filter,
2262bf215546Sopenharmony_ci                                     nir_lower_instr_cb lower,
2263bf215546Sopenharmony_ci                                     void *cb_data)
2264bf215546Sopenharmony_ci{
2265bf215546Sopenharmony_ci   nir_builder b;
2266bf215546Sopenharmony_ci   nir_builder_init(&b, impl);
2267bf215546Sopenharmony_ci
2268bf215546Sopenharmony_ci   nir_metadata preserved = nir_metadata_block_index |
2269bf215546Sopenharmony_ci                            nir_metadata_dominance;
2270bf215546Sopenharmony_ci
2271bf215546Sopenharmony_ci   bool progress = false;
2272bf215546Sopenharmony_ci   nir_cursor iter = nir_before_cf_list(&impl->body);
2273bf215546Sopenharmony_ci   nir_instr *instr;
2274bf215546Sopenharmony_ci   while ((instr = cursor_next_instr(iter)) != NULL) {
2275bf215546Sopenharmony_ci      if (filter && !filter(instr, cb_data)) {
2276bf215546Sopenharmony_ci         iter = nir_after_instr(instr);
2277bf215546Sopenharmony_ci         continue;
2278bf215546Sopenharmony_ci      }
2279bf215546Sopenharmony_ci
2280bf215546Sopenharmony_ci      assert(nir_foreach_dest(instr, dest_is_ssa, NULL));
2281bf215546Sopenharmony_ci      nir_ssa_def *old_def = nir_instr_ssa_def(instr);
2282bf215546Sopenharmony_ci      struct list_head old_uses, old_if_uses;
2283bf215546Sopenharmony_ci      if (old_def != NULL) {
2284bf215546Sopenharmony_ci         /* We're about to ask the callback to generate a replacement for instr.
2285bf215546Sopenharmony_ci          * Save off the uses from instr's SSA def so we know what uses to
2286bf215546Sopenharmony_ci          * rewrite later.  If we use nir_ssa_def_rewrite_uses, it fails in the
2287bf215546Sopenharmony_ci          * case where the generated replacement code uses the result of instr
2288bf215546Sopenharmony_ci          * itself.  If we use nir_ssa_def_rewrite_uses_after (which is the
2289bf215546Sopenharmony_ci          * normal solution to this problem), it doesn't work well if control-
2290bf215546Sopenharmony_ci          * flow is inserted as part of the replacement, doesn't handle cases
2291bf215546Sopenharmony_ci          * where the replacement is something consumed by instr, and suffers
2292bf215546Sopenharmony_ci          * from performance issues.  This is the only way to 100% guarantee
2293bf215546Sopenharmony_ci          * that we rewrite the correct set efficiently.
2294bf215546Sopenharmony_ci          */
2295bf215546Sopenharmony_ci
2296bf215546Sopenharmony_ci         list_replace(&old_def->uses, &old_uses);
2297bf215546Sopenharmony_ci         list_inithead(&old_def->uses);
2298bf215546Sopenharmony_ci         list_replace(&old_def->if_uses, &old_if_uses);
2299bf215546Sopenharmony_ci         list_inithead(&old_def->if_uses);
2300bf215546Sopenharmony_ci      }
2301bf215546Sopenharmony_ci
2302bf215546Sopenharmony_ci      b.cursor = nir_after_instr(instr);
2303bf215546Sopenharmony_ci      nir_ssa_def *new_def = lower(&b, instr, cb_data);
2304bf215546Sopenharmony_ci      if (new_def && new_def != NIR_LOWER_INSTR_PROGRESS &&
2305bf215546Sopenharmony_ci          new_def != NIR_LOWER_INSTR_PROGRESS_REPLACE) {
2306bf215546Sopenharmony_ci         assert(old_def != NULL);
2307bf215546Sopenharmony_ci         if (new_def->parent_instr->block != instr->block)
2308bf215546Sopenharmony_ci            preserved = nir_metadata_none;
2309bf215546Sopenharmony_ci
2310bf215546Sopenharmony_ci         nir_src new_src = nir_src_for_ssa(new_def);
2311bf215546Sopenharmony_ci         list_for_each_entry_safe(nir_src, use_src, &old_uses, use_link)
2312bf215546Sopenharmony_ci            nir_instr_rewrite_src(use_src->parent_instr, use_src, new_src);
2313bf215546Sopenharmony_ci
2314bf215546Sopenharmony_ci         list_for_each_entry_safe(nir_src, use_src, &old_if_uses, use_link)
2315bf215546Sopenharmony_ci            nir_if_rewrite_condition(use_src->parent_if, new_src);
2316bf215546Sopenharmony_ci
2317bf215546Sopenharmony_ci         if (nir_ssa_def_is_unused(old_def)) {
2318bf215546Sopenharmony_ci            iter = nir_instr_free_and_dce(instr);
2319bf215546Sopenharmony_ci         } else {
2320bf215546Sopenharmony_ci            iter = nir_after_instr(instr);
2321bf215546Sopenharmony_ci         }
2322bf215546Sopenharmony_ci         progress = true;
2323bf215546Sopenharmony_ci      } else {
2324bf215546Sopenharmony_ci         /* We didn't end up lowering after all.  Put the uses back */
2325bf215546Sopenharmony_ci         if (old_def) {
2326bf215546Sopenharmony_ci            list_replace(&old_uses, &old_def->uses);
2327bf215546Sopenharmony_ci            list_replace(&old_if_uses, &old_def->if_uses);
2328bf215546Sopenharmony_ci         }
2329bf215546Sopenharmony_ci         if (new_def == NIR_LOWER_INSTR_PROGRESS_REPLACE) {
2330bf215546Sopenharmony_ci            /* Only instructions without a return value can be removed like this */
2331bf215546Sopenharmony_ci            assert(!old_def);
2332bf215546Sopenharmony_ci            iter = nir_instr_free_and_dce(instr);
2333bf215546Sopenharmony_ci            progress = true;
2334bf215546Sopenharmony_ci         } else
2335bf215546Sopenharmony_ci            iter = nir_after_instr(instr);
2336bf215546Sopenharmony_ci
2337bf215546Sopenharmony_ci         if (new_def == NIR_LOWER_INSTR_PROGRESS)
2338bf215546Sopenharmony_ci            progress = true;
2339bf215546Sopenharmony_ci      }
2340bf215546Sopenharmony_ci   }
2341bf215546Sopenharmony_ci
2342bf215546Sopenharmony_ci   if (progress) {
2343bf215546Sopenharmony_ci      nir_metadata_preserve(impl, preserved);
2344bf215546Sopenharmony_ci   } else {
2345bf215546Sopenharmony_ci      nir_metadata_preserve(impl, nir_metadata_all);
2346bf215546Sopenharmony_ci   }
2347bf215546Sopenharmony_ci
2348bf215546Sopenharmony_ci   return progress;
2349bf215546Sopenharmony_ci}
2350bf215546Sopenharmony_ci
2351bf215546Sopenharmony_cibool
2352bf215546Sopenharmony_cinir_shader_lower_instructions(nir_shader *shader,
2353bf215546Sopenharmony_ci                              nir_instr_filter_cb filter,
2354bf215546Sopenharmony_ci                              nir_lower_instr_cb lower,
2355bf215546Sopenharmony_ci                              void *cb_data)
2356bf215546Sopenharmony_ci{
2357bf215546Sopenharmony_ci   bool progress = false;
2358bf215546Sopenharmony_ci
2359bf215546Sopenharmony_ci   nir_foreach_function(function, shader) {
2360bf215546Sopenharmony_ci      if (function->impl &&
2361bf215546Sopenharmony_ci          nir_function_impl_lower_instructions(function->impl,
2362bf215546Sopenharmony_ci                                               filter, lower, cb_data))
2363bf215546Sopenharmony_ci         progress = true;
2364bf215546Sopenharmony_ci   }
2365bf215546Sopenharmony_ci
2366bf215546Sopenharmony_ci   return progress;
2367bf215546Sopenharmony_ci}
2368bf215546Sopenharmony_ci
2369bf215546Sopenharmony_ci/**
2370bf215546Sopenharmony_ci * Returns true if the shader supports quad-based implicit derivatives on
2371bf215546Sopenharmony_ci * texture sampling.
2372bf215546Sopenharmony_ci */
2373bf215546Sopenharmony_cibool nir_shader_supports_implicit_lod(nir_shader *shader)
2374bf215546Sopenharmony_ci{
2375bf215546Sopenharmony_ci   return (shader->info.stage == MESA_SHADER_FRAGMENT ||
2376bf215546Sopenharmony_ci           (shader->info.stage == MESA_SHADER_COMPUTE &&
2377bf215546Sopenharmony_ci            shader->info.cs.derivative_group != DERIVATIVE_GROUP_NONE));
2378bf215546Sopenharmony_ci}
2379bf215546Sopenharmony_ci
2380bf215546Sopenharmony_cinir_intrinsic_op
2381bf215546Sopenharmony_cinir_intrinsic_from_system_value(gl_system_value val)
2382bf215546Sopenharmony_ci{
2383bf215546Sopenharmony_ci   switch (val) {
2384bf215546Sopenharmony_ci   case SYSTEM_VALUE_VERTEX_ID:
2385bf215546Sopenharmony_ci      return nir_intrinsic_load_vertex_id;
2386bf215546Sopenharmony_ci   case SYSTEM_VALUE_INSTANCE_ID:
2387bf215546Sopenharmony_ci      return nir_intrinsic_load_instance_id;
2388bf215546Sopenharmony_ci   case SYSTEM_VALUE_DRAW_ID:
2389bf215546Sopenharmony_ci      return nir_intrinsic_load_draw_id;
2390bf215546Sopenharmony_ci   case SYSTEM_VALUE_BASE_INSTANCE:
2391bf215546Sopenharmony_ci      return nir_intrinsic_load_base_instance;
2392bf215546Sopenharmony_ci   case SYSTEM_VALUE_VERTEX_ID_ZERO_BASE:
2393bf215546Sopenharmony_ci      return nir_intrinsic_load_vertex_id_zero_base;
2394bf215546Sopenharmony_ci   case SYSTEM_VALUE_IS_INDEXED_DRAW:
2395bf215546Sopenharmony_ci      return nir_intrinsic_load_is_indexed_draw;
2396bf215546Sopenharmony_ci   case SYSTEM_VALUE_FIRST_VERTEX:
2397bf215546Sopenharmony_ci      return nir_intrinsic_load_first_vertex;
2398bf215546Sopenharmony_ci   case SYSTEM_VALUE_BASE_VERTEX:
2399bf215546Sopenharmony_ci      return nir_intrinsic_load_base_vertex;
2400bf215546Sopenharmony_ci   case SYSTEM_VALUE_INVOCATION_ID:
2401bf215546Sopenharmony_ci      return nir_intrinsic_load_invocation_id;
2402bf215546Sopenharmony_ci   case SYSTEM_VALUE_FRAG_COORD:
2403bf215546Sopenharmony_ci      return nir_intrinsic_load_frag_coord;
2404bf215546Sopenharmony_ci   case SYSTEM_VALUE_POINT_COORD:
2405bf215546Sopenharmony_ci      return nir_intrinsic_load_point_coord;
2406bf215546Sopenharmony_ci   case SYSTEM_VALUE_LINE_COORD:
2407bf215546Sopenharmony_ci      return nir_intrinsic_load_line_coord;
2408bf215546Sopenharmony_ci   case SYSTEM_VALUE_FRONT_FACE:
2409bf215546Sopenharmony_ci      return nir_intrinsic_load_front_face;
2410bf215546Sopenharmony_ci   case SYSTEM_VALUE_SAMPLE_ID:
2411bf215546Sopenharmony_ci      return nir_intrinsic_load_sample_id;
2412bf215546Sopenharmony_ci   case SYSTEM_VALUE_SAMPLE_POS:
2413bf215546Sopenharmony_ci      return nir_intrinsic_load_sample_pos;
2414bf215546Sopenharmony_ci   case SYSTEM_VALUE_SAMPLE_POS_OR_CENTER:
2415bf215546Sopenharmony_ci      return nir_intrinsic_load_sample_pos_or_center;
2416bf215546Sopenharmony_ci   case SYSTEM_VALUE_SAMPLE_MASK_IN:
2417bf215546Sopenharmony_ci      return nir_intrinsic_load_sample_mask_in;
2418bf215546Sopenharmony_ci   case SYSTEM_VALUE_LOCAL_INVOCATION_ID:
2419bf215546Sopenharmony_ci      return nir_intrinsic_load_local_invocation_id;
2420bf215546Sopenharmony_ci   case SYSTEM_VALUE_LOCAL_INVOCATION_INDEX:
2421bf215546Sopenharmony_ci      return nir_intrinsic_load_local_invocation_index;
2422bf215546Sopenharmony_ci   case SYSTEM_VALUE_WORKGROUP_ID:
2423bf215546Sopenharmony_ci      return nir_intrinsic_load_workgroup_id;
2424bf215546Sopenharmony_ci   case SYSTEM_VALUE_WORKGROUP_INDEX:
2425bf215546Sopenharmony_ci      return nir_intrinsic_load_workgroup_index;
2426bf215546Sopenharmony_ci   case SYSTEM_VALUE_NUM_WORKGROUPS:
2427bf215546Sopenharmony_ci      return nir_intrinsic_load_num_workgroups;
2428bf215546Sopenharmony_ci   case SYSTEM_VALUE_PRIMITIVE_ID:
2429bf215546Sopenharmony_ci      return nir_intrinsic_load_primitive_id;
2430bf215546Sopenharmony_ci   case SYSTEM_VALUE_TESS_COORD:
2431bf215546Sopenharmony_ci      return nir_intrinsic_load_tess_coord;
2432bf215546Sopenharmony_ci   case SYSTEM_VALUE_TESS_LEVEL_OUTER:
2433bf215546Sopenharmony_ci      return nir_intrinsic_load_tess_level_outer;
2434bf215546Sopenharmony_ci   case SYSTEM_VALUE_TESS_LEVEL_INNER:
2435bf215546Sopenharmony_ci      return nir_intrinsic_load_tess_level_inner;
2436bf215546Sopenharmony_ci   case SYSTEM_VALUE_TESS_LEVEL_OUTER_DEFAULT:
2437bf215546Sopenharmony_ci      return nir_intrinsic_load_tess_level_outer_default;
2438bf215546Sopenharmony_ci   case SYSTEM_VALUE_TESS_LEVEL_INNER_DEFAULT:
2439bf215546Sopenharmony_ci      return nir_intrinsic_load_tess_level_inner_default;
2440bf215546Sopenharmony_ci   case SYSTEM_VALUE_VERTICES_IN:
2441bf215546Sopenharmony_ci      return nir_intrinsic_load_patch_vertices_in;
2442bf215546Sopenharmony_ci   case SYSTEM_VALUE_HELPER_INVOCATION:
2443bf215546Sopenharmony_ci      return nir_intrinsic_load_helper_invocation;
2444bf215546Sopenharmony_ci   case SYSTEM_VALUE_COLOR0:
2445bf215546Sopenharmony_ci      return nir_intrinsic_load_color0;
2446bf215546Sopenharmony_ci   case SYSTEM_VALUE_COLOR1:
2447bf215546Sopenharmony_ci      return nir_intrinsic_load_color1;
2448bf215546Sopenharmony_ci   case SYSTEM_VALUE_VIEW_INDEX:
2449bf215546Sopenharmony_ci      return nir_intrinsic_load_view_index;
2450bf215546Sopenharmony_ci   case SYSTEM_VALUE_SUBGROUP_SIZE:
2451bf215546Sopenharmony_ci      return nir_intrinsic_load_subgroup_size;
2452bf215546Sopenharmony_ci   case SYSTEM_VALUE_SUBGROUP_INVOCATION:
2453bf215546Sopenharmony_ci      return nir_intrinsic_load_subgroup_invocation;
2454bf215546Sopenharmony_ci   case SYSTEM_VALUE_SUBGROUP_EQ_MASK:
2455bf215546Sopenharmony_ci      return nir_intrinsic_load_subgroup_eq_mask;
2456bf215546Sopenharmony_ci   case SYSTEM_VALUE_SUBGROUP_GE_MASK:
2457bf215546Sopenharmony_ci      return nir_intrinsic_load_subgroup_ge_mask;
2458bf215546Sopenharmony_ci   case SYSTEM_VALUE_SUBGROUP_GT_MASK:
2459bf215546Sopenharmony_ci      return nir_intrinsic_load_subgroup_gt_mask;
2460bf215546Sopenharmony_ci   case SYSTEM_VALUE_SUBGROUP_LE_MASK:
2461bf215546Sopenharmony_ci      return nir_intrinsic_load_subgroup_le_mask;
2462bf215546Sopenharmony_ci   case SYSTEM_VALUE_SUBGROUP_LT_MASK:
2463bf215546Sopenharmony_ci      return nir_intrinsic_load_subgroup_lt_mask;
2464bf215546Sopenharmony_ci   case SYSTEM_VALUE_NUM_SUBGROUPS:
2465bf215546Sopenharmony_ci      return nir_intrinsic_load_num_subgroups;
2466bf215546Sopenharmony_ci   case SYSTEM_VALUE_SUBGROUP_ID:
2467bf215546Sopenharmony_ci      return nir_intrinsic_load_subgroup_id;
2468bf215546Sopenharmony_ci   case SYSTEM_VALUE_WORKGROUP_SIZE:
2469bf215546Sopenharmony_ci      return nir_intrinsic_load_workgroup_size;
2470bf215546Sopenharmony_ci   case SYSTEM_VALUE_GLOBAL_INVOCATION_ID:
2471bf215546Sopenharmony_ci      return nir_intrinsic_load_global_invocation_id;
2472bf215546Sopenharmony_ci   case SYSTEM_VALUE_BASE_GLOBAL_INVOCATION_ID:
2473bf215546Sopenharmony_ci      return nir_intrinsic_load_base_global_invocation_id;
2474bf215546Sopenharmony_ci   case SYSTEM_VALUE_GLOBAL_INVOCATION_INDEX:
2475bf215546Sopenharmony_ci      return nir_intrinsic_load_global_invocation_index;
2476bf215546Sopenharmony_ci   case SYSTEM_VALUE_WORK_DIM:
2477bf215546Sopenharmony_ci      return nir_intrinsic_load_work_dim;
2478bf215546Sopenharmony_ci   case SYSTEM_VALUE_USER_DATA_AMD:
2479bf215546Sopenharmony_ci      return nir_intrinsic_load_user_data_amd;
2480bf215546Sopenharmony_ci   case SYSTEM_VALUE_RAY_LAUNCH_ID:
2481bf215546Sopenharmony_ci      return nir_intrinsic_load_ray_launch_id;
2482bf215546Sopenharmony_ci   case SYSTEM_VALUE_RAY_LAUNCH_SIZE:
2483bf215546Sopenharmony_ci      return nir_intrinsic_load_ray_launch_size;
2484bf215546Sopenharmony_ci   case SYSTEM_VALUE_RAY_LAUNCH_SIZE_ADDR_AMD:
2485bf215546Sopenharmony_ci      return nir_intrinsic_load_ray_launch_size_addr_amd;
2486bf215546Sopenharmony_ci   case SYSTEM_VALUE_RAY_WORLD_ORIGIN:
2487bf215546Sopenharmony_ci      return nir_intrinsic_load_ray_world_origin;
2488bf215546Sopenharmony_ci   case SYSTEM_VALUE_RAY_WORLD_DIRECTION:
2489bf215546Sopenharmony_ci      return nir_intrinsic_load_ray_world_direction;
2490bf215546Sopenharmony_ci   case SYSTEM_VALUE_RAY_OBJECT_ORIGIN:
2491bf215546Sopenharmony_ci      return nir_intrinsic_load_ray_object_origin;
2492bf215546Sopenharmony_ci   case SYSTEM_VALUE_RAY_OBJECT_DIRECTION:
2493bf215546Sopenharmony_ci      return nir_intrinsic_load_ray_object_direction;
2494bf215546Sopenharmony_ci   case SYSTEM_VALUE_RAY_T_MIN:
2495bf215546Sopenharmony_ci      return nir_intrinsic_load_ray_t_min;
2496bf215546Sopenharmony_ci   case SYSTEM_VALUE_RAY_T_MAX:
2497bf215546Sopenharmony_ci      return nir_intrinsic_load_ray_t_max;
2498bf215546Sopenharmony_ci   case SYSTEM_VALUE_RAY_OBJECT_TO_WORLD:
2499bf215546Sopenharmony_ci      return nir_intrinsic_load_ray_object_to_world;
2500bf215546Sopenharmony_ci   case SYSTEM_VALUE_RAY_WORLD_TO_OBJECT:
2501bf215546Sopenharmony_ci      return nir_intrinsic_load_ray_world_to_object;
2502bf215546Sopenharmony_ci   case SYSTEM_VALUE_RAY_HIT_KIND:
2503bf215546Sopenharmony_ci      return nir_intrinsic_load_ray_hit_kind;
2504bf215546Sopenharmony_ci   case SYSTEM_VALUE_RAY_FLAGS:
2505bf215546Sopenharmony_ci      return nir_intrinsic_load_ray_flags;
2506bf215546Sopenharmony_ci   case SYSTEM_VALUE_RAY_GEOMETRY_INDEX:
2507bf215546Sopenharmony_ci      return nir_intrinsic_load_ray_geometry_index;
2508bf215546Sopenharmony_ci   case SYSTEM_VALUE_RAY_INSTANCE_CUSTOM_INDEX:
2509bf215546Sopenharmony_ci      return nir_intrinsic_load_ray_instance_custom_index;
2510bf215546Sopenharmony_ci   case SYSTEM_VALUE_CULL_MASK:
2511bf215546Sopenharmony_ci      return nir_intrinsic_load_cull_mask;
2512bf215546Sopenharmony_ci   case SYSTEM_VALUE_MESH_VIEW_COUNT:
2513bf215546Sopenharmony_ci      return nir_intrinsic_load_mesh_view_count;
2514bf215546Sopenharmony_ci   case SYSTEM_VALUE_FRAG_SHADING_RATE:
2515bf215546Sopenharmony_ci      return nir_intrinsic_load_frag_shading_rate;
2516bf215546Sopenharmony_ci   default:
2517bf215546Sopenharmony_ci      unreachable("system value does not directly correspond to intrinsic");
2518bf215546Sopenharmony_ci   }
2519bf215546Sopenharmony_ci}
2520bf215546Sopenharmony_ci
2521bf215546Sopenharmony_cigl_system_value
2522bf215546Sopenharmony_cinir_system_value_from_intrinsic(nir_intrinsic_op intrin)
2523bf215546Sopenharmony_ci{
2524bf215546Sopenharmony_ci   switch (intrin) {
2525bf215546Sopenharmony_ci   case nir_intrinsic_load_vertex_id:
2526bf215546Sopenharmony_ci      return SYSTEM_VALUE_VERTEX_ID;
2527bf215546Sopenharmony_ci   case nir_intrinsic_load_instance_id:
2528bf215546Sopenharmony_ci      return SYSTEM_VALUE_INSTANCE_ID;
2529bf215546Sopenharmony_ci   case nir_intrinsic_load_draw_id:
2530bf215546Sopenharmony_ci      return SYSTEM_VALUE_DRAW_ID;
2531bf215546Sopenharmony_ci   case nir_intrinsic_load_base_instance:
2532bf215546Sopenharmony_ci      return SYSTEM_VALUE_BASE_INSTANCE;
2533bf215546Sopenharmony_ci   case nir_intrinsic_load_vertex_id_zero_base:
2534bf215546Sopenharmony_ci      return SYSTEM_VALUE_VERTEX_ID_ZERO_BASE;
2535bf215546Sopenharmony_ci   case nir_intrinsic_load_first_vertex:
2536bf215546Sopenharmony_ci      return SYSTEM_VALUE_FIRST_VERTEX;
2537bf215546Sopenharmony_ci   case nir_intrinsic_load_is_indexed_draw:
2538bf215546Sopenharmony_ci      return SYSTEM_VALUE_IS_INDEXED_DRAW;
2539bf215546Sopenharmony_ci   case nir_intrinsic_load_base_vertex:
2540bf215546Sopenharmony_ci      return SYSTEM_VALUE_BASE_VERTEX;
2541bf215546Sopenharmony_ci   case nir_intrinsic_load_invocation_id:
2542bf215546Sopenharmony_ci      return SYSTEM_VALUE_INVOCATION_ID;
2543bf215546Sopenharmony_ci   case nir_intrinsic_load_frag_coord:
2544bf215546Sopenharmony_ci      return SYSTEM_VALUE_FRAG_COORD;
2545bf215546Sopenharmony_ci   case nir_intrinsic_load_point_coord:
2546bf215546Sopenharmony_ci      return SYSTEM_VALUE_POINT_COORD;
2547bf215546Sopenharmony_ci   case nir_intrinsic_load_line_coord:
2548bf215546Sopenharmony_ci      return SYSTEM_VALUE_LINE_COORD;
2549bf215546Sopenharmony_ci   case nir_intrinsic_load_front_face:
2550bf215546Sopenharmony_ci      return SYSTEM_VALUE_FRONT_FACE;
2551bf215546Sopenharmony_ci   case nir_intrinsic_load_sample_id:
2552bf215546Sopenharmony_ci      return SYSTEM_VALUE_SAMPLE_ID;
2553bf215546Sopenharmony_ci   case nir_intrinsic_load_sample_pos:
2554bf215546Sopenharmony_ci      return SYSTEM_VALUE_SAMPLE_POS;
2555bf215546Sopenharmony_ci   case nir_intrinsic_load_sample_pos_or_center:
2556bf215546Sopenharmony_ci      return SYSTEM_VALUE_SAMPLE_POS_OR_CENTER;
2557bf215546Sopenharmony_ci   case nir_intrinsic_load_sample_mask_in:
2558bf215546Sopenharmony_ci      return SYSTEM_VALUE_SAMPLE_MASK_IN;
2559bf215546Sopenharmony_ci   case nir_intrinsic_load_local_invocation_id:
2560bf215546Sopenharmony_ci      return SYSTEM_VALUE_LOCAL_INVOCATION_ID;
2561bf215546Sopenharmony_ci   case nir_intrinsic_load_local_invocation_index:
2562bf215546Sopenharmony_ci      return SYSTEM_VALUE_LOCAL_INVOCATION_INDEX;
2563bf215546Sopenharmony_ci   case nir_intrinsic_load_num_workgroups:
2564bf215546Sopenharmony_ci      return SYSTEM_VALUE_NUM_WORKGROUPS;
2565bf215546Sopenharmony_ci   case nir_intrinsic_load_workgroup_id:
2566bf215546Sopenharmony_ci      return SYSTEM_VALUE_WORKGROUP_ID;
2567bf215546Sopenharmony_ci   case nir_intrinsic_load_workgroup_index:
2568bf215546Sopenharmony_ci      return SYSTEM_VALUE_WORKGROUP_INDEX;
2569bf215546Sopenharmony_ci   case nir_intrinsic_load_primitive_id:
2570bf215546Sopenharmony_ci      return SYSTEM_VALUE_PRIMITIVE_ID;
2571bf215546Sopenharmony_ci   case nir_intrinsic_load_tess_coord:
2572bf215546Sopenharmony_ci      return SYSTEM_VALUE_TESS_COORD;
2573bf215546Sopenharmony_ci   case nir_intrinsic_load_tess_level_outer:
2574bf215546Sopenharmony_ci      return SYSTEM_VALUE_TESS_LEVEL_OUTER;
2575bf215546Sopenharmony_ci   case nir_intrinsic_load_tess_level_inner:
2576bf215546Sopenharmony_ci      return SYSTEM_VALUE_TESS_LEVEL_INNER;
2577bf215546Sopenharmony_ci   case nir_intrinsic_load_tess_level_outer_default:
2578bf215546Sopenharmony_ci      return SYSTEM_VALUE_TESS_LEVEL_OUTER_DEFAULT;
2579bf215546Sopenharmony_ci   case nir_intrinsic_load_tess_level_inner_default:
2580bf215546Sopenharmony_ci      return SYSTEM_VALUE_TESS_LEVEL_INNER_DEFAULT;
2581bf215546Sopenharmony_ci   case nir_intrinsic_load_patch_vertices_in:
2582bf215546Sopenharmony_ci      return SYSTEM_VALUE_VERTICES_IN;
2583bf215546Sopenharmony_ci   case nir_intrinsic_load_helper_invocation:
2584bf215546Sopenharmony_ci      return SYSTEM_VALUE_HELPER_INVOCATION;
2585bf215546Sopenharmony_ci   case nir_intrinsic_load_color0:
2586bf215546Sopenharmony_ci      return SYSTEM_VALUE_COLOR0;
2587bf215546Sopenharmony_ci   case nir_intrinsic_load_color1:
2588bf215546Sopenharmony_ci      return SYSTEM_VALUE_COLOR1;
2589bf215546Sopenharmony_ci   case nir_intrinsic_load_view_index:
2590bf215546Sopenharmony_ci      return SYSTEM_VALUE_VIEW_INDEX;
2591bf215546Sopenharmony_ci   case nir_intrinsic_load_subgroup_size:
2592bf215546Sopenharmony_ci      return SYSTEM_VALUE_SUBGROUP_SIZE;
2593bf215546Sopenharmony_ci   case nir_intrinsic_load_subgroup_invocation:
2594bf215546Sopenharmony_ci      return SYSTEM_VALUE_SUBGROUP_INVOCATION;
2595bf215546Sopenharmony_ci   case nir_intrinsic_load_subgroup_eq_mask:
2596bf215546Sopenharmony_ci      return SYSTEM_VALUE_SUBGROUP_EQ_MASK;
2597bf215546Sopenharmony_ci   case nir_intrinsic_load_subgroup_ge_mask:
2598bf215546Sopenharmony_ci      return SYSTEM_VALUE_SUBGROUP_GE_MASK;
2599bf215546Sopenharmony_ci   case nir_intrinsic_load_subgroup_gt_mask:
2600bf215546Sopenharmony_ci      return SYSTEM_VALUE_SUBGROUP_GT_MASK;
2601bf215546Sopenharmony_ci   case nir_intrinsic_load_subgroup_le_mask:
2602bf215546Sopenharmony_ci      return SYSTEM_VALUE_SUBGROUP_LE_MASK;
2603bf215546Sopenharmony_ci   case nir_intrinsic_load_subgroup_lt_mask:
2604bf215546Sopenharmony_ci      return SYSTEM_VALUE_SUBGROUP_LT_MASK;
2605bf215546Sopenharmony_ci   case nir_intrinsic_load_num_subgroups:
2606bf215546Sopenharmony_ci      return SYSTEM_VALUE_NUM_SUBGROUPS;
2607bf215546Sopenharmony_ci   case nir_intrinsic_load_subgroup_id:
2608bf215546Sopenharmony_ci      return SYSTEM_VALUE_SUBGROUP_ID;
2609bf215546Sopenharmony_ci   case nir_intrinsic_load_workgroup_size:
2610bf215546Sopenharmony_ci      return SYSTEM_VALUE_WORKGROUP_SIZE;
2611bf215546Sopenharmony_ci   case nir_intrinsic_load_global_invocation_id:
2612bf215546Sopenharmony_ci      return SYSTEM_VALUE_GLOBAL_INVOCATION_ID;
2613bf215546Sopenharmony_ci   case nir_intrinsic_load_base_global_invocation_id:
2614bf215546Sopenharmony_ci      return SYSTEM_VALUE_BASE_GLOBAL_INVOCATION_ID;
2615bf215546Sopenharmony_ci   case nir_intrinsic_load_global_invocation_index:
2616bf215546Sopenharmony_ci      return SYSTEM_VALUE_GLOBAL_INVOCATION_INDEX;
2617bf215546Sopenharmony_ci   case nir_intrinsic_load_work_dim:
2618bf215546Sopenharmony_ci      return SYSTEM_VALUE_WORK_DIM;
2619bf215546Sopenharmony_ci   case nir_intrinsic_load_user_data_amd:
2620bf215546Sopenharmony_ci      return SYSTEM_VALUE_USER_DATA_AMD;
2621bf215546Sopenharmony_ci   case nir_intrinsic_load_barycentric_model:
2622bf215546Sopenharmony_ci      return SYSTEM_VALUE_BARYCENTRIC_PULL_MODEL;
2623bf215546Sopenharmony_ci   case nir_intrinsic_load_gs_header_ir3:
2624bf215546Sopenharmony_ci      return SYSTEM_VALUE_GS_HEADER_IR3;
2625bf215546Sopenharmony_ci   case nir_intrinsic_load_tcs_header_ir3:
2626bf215546Sopenharmony_ci      return SYSTEM_VALUE_TCS_HEADER_IR3;
2627bf215546Sopenharmony_ci   case nir_intrinsic_load_ray_launch_id:
2628bf215546Sopenharmony_ci      return SYSTEM_VALUE_RAY_LAUNCH_ID;
2629bf215546Sopenharmony_ci   case nir_intrinsic_load_ray_launch_size:
2630bf215546Sopenharmony_ci      return SYSTEM_VALUE_RAY_LAUNCH_SIZE;
2631bf215546Sopenharmony_ci   case nir_intrinsic_load_ray_launch_size_addr_amd:
2632bf215546Sopenharmony_ci      return SYSTEM_VALUE_RAY_LAUNCH_SIZE_ADDR_AMD;
2633bf215546Sopenharmony_ci   case nir_intrinsic_load_ray_world_origin:
2634bf215546Sopenharmony_ci      return SYSTEM_VALUE_RAY_WORLD_ORIGIN;
2635bf215546Sopenharmony_ci   case nir_intrinsic_load_ray_world_direction:
2636bf215546Sopenharmony_ci      return SYSTEM_VALUE_RAY_WORLD_DIRECTION;
2637bf215546Sopenharmony_ci   case nir_intrinsic_load_ray_object_origin:
2638bf215546Sopenharmony_ci      return SYSTEM_VALUE_RAY_OBJECT_ORIGIN;
2639bf215546Sopenharmony_ci   case nir_intrinsic_load_ray_object_direction:
2640bf215546Sopenharmony_ci      return SYSTEM_VALUE_RAY_OBJECT_DIRECTION;
2641bf215546Sopenharmony_ci   case nir_intrinsic_load_ray_t_min:
2642bf215546Sopenharmony_ci      return SYSTEM_VALUE_RAY_T_MIN;
2643bf215546Sopenharmony_ci   case nir_intrinsic_load_ray_t_max:
2644bf215546Sopenharmony_ci      return SYSTEM_VALUE_RAY_T_MAX;
2645bf215546Sopenharmony_ci   case nir_intrinsic_load_ray_object_to_world:
2646bf215546Sopenharmony_ci      return SYSTEM_VALUE_RAY_OBJECT_TO_WORLD;
2647bf215546Sopenharmony_ci   case nir_intrinsic_load_ray_world_to_object:
2648bf215546Sopenharmony_ci      return SYSTEM_VALUE_RAY_WORLD_TO_OBJECT;
2649bf215546Sopenharmony_ci   case nir_intrinsic_load_ray_hit_kind:
2650bf215546Sopenharmony_ci      return SYSTEM_VALUE_RAY_HIT_KIND;
2651bf215546Sopenharmony_ci   case nir_intrinsic_load_ray_flags:
2652bf215546Sopenharmony_ci      return SYSTEM_VALUE_RAY_FLAGS;
2653bf215546Sopenharmony_ci   case nir_intrinsic_load_ray_geometry_index:
2654bf215546Sopenharmony_ci      return SYSTEM_VALUE_RAY_GEOMETRY_INDEX;
2655bf215546Sopenharmony_ci   case nir_intrinsic_load_ray_instance_custom_index:
2656bf215546Sopenharmony_ci      return SYSTEM_VALUE_RAY_INSTANCE_CUSTOM_INDEX;
2657bf215546Sopenharmony_ci   case nir_intrinsic_load_cull_mask:
2658bf215546Sopenharmony_ci      return SYSTEM_VALUE_CULL_MASK;
2659bf215546Sopenharmony_ci   case nir_intrinsic_load_frag_shading_rate:
2660bf215546Sopenharmony_ci      return SYSTEM_VALUE_FRAG_SHADING_RATE;
2661bf215546Sopenharmony_ci   case nir_intrinsic_load_mesh_view_count:
2662bf215546Sopenharmony_ci      return SYSTEM_VALUE_MESH_VIEW_COUNT;
2663bf215546Sopenharmony_ci   default:
2664bf215546Sopenharmony_ci      unreachable("intrinsic doesn't produce a system value");
2665bf215546Sopenharmony_ci   }
2666bf215546Sopenharmony_ci}
2667bf215546Sopenharmony_ci
2668bf215546Sopenharmony_ci/* OpenGL utility method that remaps the location attributes if they are
2669bf215546Sopenharmony_ci * doubles. Not needed for vulkan due the differences on the input location
2670bf215546Sopenharmony_ci * count for doubles on vulkan vs OpenGL
2671bf215546Sopenharmony_ci *
2672bf215546Sopenharmony_ci * The bitfield returned in dual_slot is one bit for each double input slot in
2673bf215546Sopenharmony_ci * the original OpenGL single-slot input numbering.  The mapping from old
2674bf215546Sopenharmony_ci * locations to new locations is as follows:
2675bf215546Sopenharmony_ci *
2676bf215546Sopenharmony_ci *    new_loc = loc + util_bitcount(dual_slot & BITFIELD64_MASK(loc))
2677bf215546Sopenharmony_ci */
2678bf215546Sopenharmony_civoid
2679bf215546Sopenharmony_cinir_remap_dual_slot_attributes(nir_shader *shader, uint64_t *dual_slot)
2680bf215546Sopenharmony_ci{
2681bf215546Sopenharmony_ci   assert(shader->info.stage == MESA_SHADER_VERTEX);
2682bf215546Sopenharmony_ci
2683bf215546Sopenharmony_ci   *dual_slot = 0;
2684bf215546Sopenharmony_ci   nir_foreach_shader_in_variable(var, shader) {
2685bf215546Sopenharmony_ci      if (glsl_type_is_dual_slot(glsl_without_array(var->type))) {
2686bf215546Sopenharmony_ci         unsigned slots = glsl_count_attribute_slots(var->type, true);
2687bf215546Sopenharmony_ci         *dual_slot |= BITFIELD64_MASK(slots) << var->data.location;
2688bf215546Sopenharmony_ci      }
2689bf215546Sopenharmony_ci   }
2690bf215546Sopenharmony_ci
2691bf215546Sopenharmony_ci   nir_foreach_shader_in_variable(var, shader) {
2692bf215546Sopenharmony_ci      var->data.location +=
2693bf215546Sopenharmony_ci         util_bitcount64(*dual_slot & BITFIELD64_MASK(var->data.location));
2694bf215546Sopenharmony_ci   }
2695bf215546Sopenharmony_ci}
2696bf215546Sopenharmony_ci
2697bf215546Sopenharmony_ci/* Returns an attribute mask that has been re-compacted using the given
2698bf215546Sopenharmony_ci * dual_slot mask.
2699bf215546Sopenharmony_ci */
2700bf215546Sopenharmony_ciuint64_t
2701bf215546Sopenharmony_cinir_get_single_slot_attribs_mask(uint64_t attribs, uint64_t dual_slot)
2702bf215546Sopenharmony_ci{
2703bf215546Sopenharmony_ci   while (dual_slot) {
2704bf215546Sopenharmony_ci      unsigned loc = u_bit_scan64(&dual_slot);
2705bf215546Sopenharmony_ci      /* mask of all bits up to and including loc */
2706bf215546Sopenharmony_ci      uint64_t mask = BITFIELD64_MASK(loc + 1);
2707bf215546Sopenharmony_ci      attribs = (attribs & mask) | ((attribs & ~mask) >> 1);
2708bf215546Sopenharmony_ci   }
2709bf215546Sopenharmony_ci   return attribs;
2710bf215546Sopenharmony_ci}
2711bf215546Sopenharmony_ci
2712bf215546Sopenharmony_civoid
2713bf215546Sopenharmony_cinir_rewrite_image_intrinsic(nir_intrinsic_instr *intrin, nir_ssa_def *src,
2714bf215546Sopenharmony_ci                            bool bindless)
2715bf215546Sopenharmony_ci{
2716bf215546Sopenharmony_ci   enum gl_access_qualifier access = nir_intrinsic_access(intrin);
2717bf215546Sopenharmony_ci
2718bf215546Sopenharmony_ci   /* Image intrinsics only have one of these */
2719bf215546Sopenharmony_ci   assert(!nir_intrinsic_has_src_type(intrin) ||
2720bf215546Sopenharmony_ci          !nir_intrinsic_has_dest_type(intrin));
2721bf215546Sopenharmony_ci
2722bf215546Sopenharmony_ci   nir_alu_type data_type = nir_type_invalid;
2723bf215546Sopenharmony_ci   if (nir_intrinsic_has_src_type(intrin))
2724bf215546Sopenharmony_ci      data_type = nir_intrinsic_src_type(intrin);
2725bf215546Sopenharmony_ci   if (nir_intrinsic_has_dest_type(intrin))
2726bf215546Sopenharmony_ci      data_type = nir_intrinsic_dest_type(intrin);
2727bf215546Sopenharmony_ci
2728bf215546Sopenharmony_ci   switch (intrin->intrinsic) {
2729bf215546Sopenharmony_ci#define CASE(op) \
2730bf215546Sopenharmony_ci   case nir_intrinsic_image_deref_##op: \
2731bf215546Sopenharmony_ci      intrin->intrinsic = bindless ? nir_intrinsic_bindless_image_##op \
2732bf215546Sopenharmony_ci                                   : nir_intrinsic_image_##op; \
2733bf215546Sopenharmony_ci      break;
2734bf215546Sopenharmony_ci   CASE(load)
2735bf215546Sopenharmony_ci   CASE(sparse_load)
2736bf215546Sopenharmony_ci   CASE(store)
2737bf215546Sopenharmony_ci   CASE(atomic_add)
2738bf215546Sopenharmony_ci   CASE(atomic_imin)
2739bf215546Sopenharmony_ci   CASE(atomic_umin)
2740bf215546Sopenharmony_ci   CASE(atomic_imax)
2741bf215546Sopenharmony_ci   CASE(atomic_umax)
2742bf215546Sopenharmony_ci   CASE(atomic_and)
2743bf215546Sopenharmony_ci   CASE(atomic_or)
2744bf215546Sopenharmony_ci   CASE(atomic_xor)
2745bf215546Sopenharmony_ci   CASE(atomic_exchange)
2746bf215546Sopenharmony_ci   CASE(atomic_comp_swap)
2747bf215546Sopenharmony_ci   CASE(atomic_fadd)
2748bf215546Sopenharmony_ci   CASE(atomic_fmin)
2749bf215546Sopenharmony_ci   CASE(atomic_fmax)
2750bf215546Sopenharmony_ci   CASE(atomic_inc_wrap)
2751bf215546Sopenharmony_ci   CASE(atomic_dec_wrap)
2752bf215546Sopenharmony_ci   CASE(size)
2753bf215546Sopenharmony_ci   CASE(samples)
2754bf215546Sopenharmony_ci   CASE(load_raw_intel)
2755bf215546Sopenharmony_ci   CASE(store_raw_intel)
2756bf215546Sopenharmony_ci#undef CASE
2757bf215546Sopenharmony_ci   default:
2758bf215546Sopenharmony_ci      unreachable("Unhanded image intrinsic");
2759bf215546Sopenharmony_ci   }
2760bf215546Sopenharmony_ci
2761bf215546Sopenharmony_ci   nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
2762bf215546Sopenharmony_ci   nir_variable *var = nir_deref_instr_get_variable(deref);
2763bf215546Sopenharmony_ci
2764bf215546Sopenharmony_ci   /* Only update the format if the intrinsic doesn't have one set */
2765bf215546Sopenharmony_ci   if (nir_intrinsic_format(intrin) == PIPE_FORMAT_NONE)
2766bf215546Sopenharmony_ci      nir_intrinsic_set_format(intrin, var->data.image.format);
2767bf215546Sopenharmony_ci
2768bf215546Sopenharmony_ci   nir_intrinsic_set_access(intrin, access | var->data.access);
2769bf215546Sopenharmony_ci   if (nir_intrinsic_has_src_type(intrin))
2770bf215546Sopenharmony_ci      nir_intrinsic_set_src_type(intrin, data_type);
2771bf215546Sopenharmony_ci   if (nir_intrinsic_has_dest_type(intrin))
2772bf215546Sopenharmony_ci      nir_intrinsic_set_dest_type(intrin, data_type);
2773bf215546Sopenharmony_ci
2774bf215546Sopenharmony_ci   nir_instr_rewrite_src(&intrin->instr, &intrin->src[0],
2775bf215546Sopenharmony_ci                         nir_src_for_ssa(src));
2776bf215546Sopenharmony_ci}
2777bf215546Sopenharmony_ci
2778bf215546Sopenharmony_ciunsigned
2779bf215546Sopenharmony_cinir_image_intrinsic_coord_components(const nir_intrinsic_instr *instr)
2780bf215546Sopenharmony_ci{
2781bf215546Sopenharmony_ci   enum glsl_sampler_dim dim = nir_intrinsic_image_dim(instr);
2782bf215546Sopenharmony_ci   int coords = glsl_get_sampler_dim_coordinate_components(dim);
2783bf215546Sopenharmony_ci   if (dim == GLSL_SAMPLER_DIM_CUBE)
2784bf215546Sopenharmony_ci      return coords;
2785bf215546Sopenharmony_ci   else
2786bf215546Sopenharmony_ci      return coords + nir_intrinsic_image_array(instr);
2787bf215546Sopenharmony_ci}
2788bf215546Sopenharmony_ci
2789bf215546Sopenharmony_cinir_src *
2790bf215546Sopenharmony_cinir_get_shader_call_payload_src(nir_intrinsic_instr *call)
2791bf215546Sopenharmony_ci{
2792bf215546Sopenharmony_ci   switch (call->intrinsic) {
2793bf215546Sopenharmony_ci   case nir_intrinsic_trace_ray:
2794bf215546Sopenharmony_ci   case nir_intrinsic_rt_trace_ray:
2795bf215546Sopenharmony_ci      return &call->src[10];
2796bf215546Sopenharmony_ci   case nir_intrinsic_execute_callable:
2797bf215546Sopenharmony_ci   case nir_intrinsic_rt_execute_callable:
2798bf215546Sopenharmony_ci      return &call->src[1];
2799bf215546Sopenharmony_ci   default:
2800bf215546Sopenharmony_ci      unreachable("Not a call intrinsic");
2801bf215546Sopenharmony_ci      return NULL;
2802bf215546Sopenharmony_ci   }
2803bf215546Sopenharmony_ci}
2804bf215546Sopenharmony_ci
2805bf215546Sopenharmony_cinir_binding nir_chase_binding(nir_src rsrc)
2806bf215546Sopenharmony_ci{
2807bf215546Sopenharmony_ci   nir_binding res = {0};
2808bf215546Sopenharmony_ci   if (rsrc.ssa->parent_instr->type == nir_instr_type_deref) {
2809bf215546Sopenharmony_ci      const struct glsl_type *type = glsl_without_array(nir_src_as_deref(rsrc)->type);
2810bf215546Sopenharmony_ci      bool is_image = glsl_type_is_image(type) || glsl_type_is_sampler(type);
2811bf215546Sopenharmony_ci      while (rsrc.ssa->parent_instr->type == nir_instr_type_deref) {
2812bf215546Sopenharmony_ci         nir_deref_instr *deref = nir_src_as_deref(rsrc);
2813bf215546Sopenharmony_ci
2814bf215546Sopenharmony_ci         if (deref->deref_type == nir_deref_type_var) {
2815bf215546Sopenharmony_ci            res.success = true;
2816bf215546Sopenharmony_ci            res.var = deref->var;
2817bf215546Sopenharmony_ci            res.desc_set = deref->var->data.descriptor_set;
2818bf215546Sopenharmony_ci            res.binding = deref->var->data.binding;
2819bf215546Sopenharmony_ci            return res;
2820bf215546Sopenharmony_ci         } else if (deref->deref_type == nir_deref_type_array && is_image) {
2821bf215546Sopenharmony_ci            if (res.num_indices == ARRAY_SIZE(res.indices))
2822bf215546Sopenharmony_ci               return (nir_binding){0};
2823bf215546Sopenharmony_ci            res.indices[res.num_indices++] = deref->arr.index;
2824bf215546Sopenharmony_ci         }
2825bf215546Sopenharmony_ci
2826bf215546Sopenharmony_ci         rsrc = deref->parent;
2827bf215546Sopenharmony_ci      }
2828bf215546Sopenharmony_ci   }
2829bf215546Sopenharmony_ci
2830bf215546Sopenharmony_ci   /* Skip copies and trimming. Trimming can appear as nir_op_mov instructions
2831bf215546Sopenharmony_ci    * when removing the offset from addresses. We also consider nir_op_is_vec()
2832bf215546Sopenharmony_ci    * instructions to skip trimming of vec2_index_32bit_offset addresses after
2833bf215546Sopenharmony_ci    * lowering ALU to scalar.
2834bf215546Sopenharmony_ci    */
2835bf215546Sopenharmony_ci   while (true) {
2836bf215546Sopenharmony_ci      nir_alu_instr *alu = nir_src_as_alu_instr(rsrc);
2837bf215546Sopenharmony_ci      nir_intrinsic_instr *intrin = nir_src_as_intrinsic(rsrc);
2838bf215546Sopenharmony_ci      if (alu && alu->op == nir_op_mov) {
2839bf215546Sopenharmony_ci         for (unsigned i = 0; i < alu->dest.dest.ssa.num_components; i++) {
2840bf215546Sopenharmony_ci            if (alu->src[0].swizzle[i] != i)
2841bf215546Sopenharmony_ci               return (nir_binding){0};
2842bf215546Sopenharmony_ci         }
2843bf215546Sopenharmony_ci         rsrc = alu->src[0].src;
2844bf215546Sopenharmony_ci      } else if (alu && nir_op_is_vec(alu->op)) {
2845bf215546Sopenharmony_ci         for (unsigned i = 0; i < nir_op_infos[alu->op].num_inputs; i++) {
2846bf215546Sopenharmony_ci            if (alu->src[i].swizzle[0] != i || alu->src[i].src.ssa != alu->src[0].src.ssa)
2847bf215546Sopenharmony_ci               return (nir_binding){0};
2848bf215546Sopenharmony_ci         }
2849bf215546Sopenharmony_ci         rsrc = alu->src[0].src;
2850bf215546Sopenharmony_ci      } else if (intrin && intrin->intrinsic == nir_intrinsic_read_first_invocation) {
2851bf215546Sopenharmony_ci         /* The caller might want to be aware if only the first invocation of
2852bf215546Sopenharmony_ci          * the indices are used.
2853bf215546Sopenharmony_ci          */
2854bf215546Sopenharmony_ci         res.read_first_invocation = true;
2855bf215546Sopenharmony_ci         rsrc = intrin->src[0];
2856bf215546Sopenharmony_ci      } else {
2857bf215546Sopenharmony_ci         break;
2858bf215546Sopenharmony_ci      }
2859bf215546Sopenharmony_ci   }
2860bf215546Sopenharmony_ci
2861bf215546Sopenharmony_ci   if (nir_src_is_const(rsrc)) {
2862bf215546Sopenharmony_ci      /* GL binding model after deref lowering */
2863bf215546Sopenharmony_ci      res.success = true;
2864bf215546Sopenharmony_ci      /* Can't use just nir_src_as_uint. Vulkan resource index produces a
2865bf215546Sopenharmony_ci       * vec2. Some drivers lower it to vec1 (to handle get_ssbo_size for
2866bf215546Sopenharmony_ci       * example) but others just keep it around as a vec2 (v3dv).
2867bf215546Sopenharmony_ci       */
2868bf215546Sopenharmony_ci      res.binding = nir_src_comp_as_uint(rsrc, 0);
2869bf215546Sopenharmony_ci      return res;
2870bf215546Sopenharmony_ci   }
2871bf215546Sopenharmony_ci
2872bf215546Sopenharmony_ci   /* otherwise, must be Vulkan binding model after deref lowering or GL bindless */
2873bf215546Sopenharmony_ci
2874bf215546Sopenharmony_ci   nir_intrinsic_instr *intrin = nir_src_as_intrinsic(rsrc);
2875bf215546Sopenharmony_ci   if (!intrin)
2876bf215546Sopenharmony_ci      return (nir_binding){0};
2877bf215546Sopenharmony_ci
2878bf215546Sopenharmony_ci   /* skip load_vulkan_descriptor */
2879bf215546Sopenharmony_ci   if (intrin->intrinsic == nir_intrinsic_load_vulkan_descriptor) {
2880bf215546Sopenharmony_ci      intrin = nir_src_as_intrinsic(intrin->src[0]);
2881bf215546Sopenharmony_ci      if (!intrin)
2882bf215546Sopenharmony_ci         return (nir_binding){0};
2883bf215546Sopenharmony_ci   }
2884bf215546Sopenharmony_ci
2885bf215546Sopenharmony_ci   if (intrin->intrinsic != nir_intrinsic_vulkan_resource_index)
2886bf215546Sopenharmony_ci      return (nir_binding){0};
2887bf215546Sopenharmony_ci
2888bf215546Sopenharmony_ci   assert(res.num_indices == 0);
2889bf215546Sopenharmony_ci   res.success = true;
2890bf215546Sopenharmony_ci   res.desc_set = nir_intrinsic_desc_set(intrin);
2891bf215546Sopenharmony_ci   res.binding = nir_intrinsic_binding(intrin);
2892bf215546Sopenharmony_ci   res.num_indices = 1;
2893bf215546Sopenharmony_ci   res.indices[0] = intrin->src[0];
2894bf215546Sopenharmony_ci   return res;
2895bf215546Sopenharmony_ci}
2896bf215546Sopenharmony_ci
2897bf215546Sopenharmony_cinir_variable *nir_get_binding_variable(nir_shader *shader, nir_binding binding)
2898bf215546Sopenharmony_ci{
2899bf215546Sopenharmony_ci   nir_variable *binding_var = NULL;
2900bf215546Sopenharmony_ci   unsigned count = 0;
2901bf215546Sopenharmony_ci
2902bf215546Sopenharmony_ci   if (!binding.success)
2903bf215546Sopenharmony_ci      return NULL;
2904bf215546Sopenharmony_ci
2905bf215546Sopenharmony_ci   if (binding.var)
2906bf215546Sopenharmony_ci      return binding.var;
2907bf215546Sopenharmony_ci
2908bf215546Sopenharmony_ci   nir_foreach_variable_with_modes(var, shader, nir_var_mem_ubo | nir_var_mem_ssbo) {
2909bf215546Sopenharmony_ci      if (var->data.descriptor_set == binding.desc_set && var->data.binding == binding.binding) {
2910bf215546Sopenharmony_ci         binding_var = var;
2911bf215546Sopenharmony_ci         count++;
2912bf215546Sopenharmony_ci      }
2913bf215546Sopenharmony_ci   }
2914bf215546Sopenharmony_ci
2915bf215546Sopenharmony_ci   /* Be conservative if another variable is using the same binding/desc_set
2916bf215546Sopenharmony_ci    * because the access mask might be different and we can't get it reliably.
2917bf215546Sopenharmony_ci    */
2918bf215546Sopenharmony_ci   if (count > 1)
2919bf215546Sopenharmony_ci      return NULL;
2920bf215546Sopenharmony_ci
2921bf215546Sopenharmony_ci   return binding_var;
2922bf215546Sopenharmony_ci}
2923bf215546Sopenharmony_ci
2924bf215546Sopenharmony_cibool
2925bf215546Sopenharmony_cinir_alu_instr_is_copy(nir_alu_instr *instr)
2926bf215546Sopenharmony_ci{
2927bf215546Sopenharmony_ci   assert(instr->src[0].src.is_ssa);
2928bf215546Sopenharmony_ci
2929bf215546Sopenharmony_ci   if (instr->op == nir_op_mov) {
2930bf215546Sopenharmony_ci      return !instr->dest.saturate &&
2931bf215546Sopenharmony_ci             !instr->src[0].abs &&
2932bf215546Sopenharmony_ci             !instr->src[0].negate;
2933bf215546Sopenharmony_ci   } else if (nir_op_is_vec(instr->op)) {
2934bf215546Sopenharmony_ci      for (unsigned i = 0; i < instr->dest.dest.ssa.num_components; i++) {
2935bf215546Sopenharmony_ci         if (instr->src[i].abs || instr->src[i].negate)
2936bf215546Sopenharmony_ci            return false;
2937bf215546Sopenharmony_ci      }
2938bf215546Sopenharmony_ci      return !instr->dest.saturate;
2939bf215546Sopenharmony_ci   } else {
2940bf215546Sopenharmony_ci      return false;
2941bf215546Sopenharmony_ci   }
2942bf215546Sopenharmony_ci}
2943bf215546Sopenharmony_ci
2944bf215546Sopenharmony_cinir_ssa_scalar
2945bf215546Sopenharmony_cinir_ssa_scalar_chase_movs(nir_ssa_scalar s)
2946bf215546Sopenharmony_ci{
2947bf215546Sopenharmony_ci   while (nir_ssa_scalar_is_alu(s)) {
2948bf215546Sopenharmony_ci      nir_alu_instr *alu = nir_instr_as_alu(s.def->parent_instr);
2949bf215546Sopenharmony_ci      if (!nir_alu_instr_is_copy(alu))
2950bf215546Sopenharmony_ci         break;
2951bf215546Sopenharmony_ci
2952bf215546Sopenharmony_ci      if (alu->op == nir_op_mov) {
2953bf215546Sopenharmony_ci         s.def = alu->src[0].src.ssa;
2954bf215546Sopenharmony_ci         s.comp = alu->src[0].swizzle[s.comp];
2955bf215546Sopenharmony_ci      } else {
2956bf215546Sopenharmony_ci         assert(nir_op_is_vec(alu->op));
2957bf215546Sopenharmony_ci         s.def = alu->src[s.comp].src.ssa;
2958bf215546Sopenharmony_ci         s.comp = alu->src[s.comp].swizzle[0];
2959bf215546Sopenharmony_ci      }
2960bf215546Sopenharmony_ci   }
2961bf215546Sopenharmony_ci
2962bf215546Sopenharmony_ci   return s;
2963bf215546Sopenharmony_ci}
2964bf215546Sopenharmony_ci
2965bf215546Sopenharmony_cinir_alu_type
2966bf215546Sopenharmony_cinir_get_nir_type_for_glsl_base_type(enum glsl_base_type base_type)
2967bf215546Sopenharmony_ci{
2968bf215546Sopenharmony_ci   switch (base_type) {
2969bf215546Sopenharmony_ci   case GLSL_TYPE_BOOL:
2970bf215546Sopenharmony_ci      return nir_type_bool1;
2971bf215546Sopenharmony_ci      break;
2972bf215546Sopenharmony_ci   case GLSL_TYPE_UINT:
2973bf215546Sopenharmony_ci      return nir_type_uint32;
2974bf215546Sopenharmony_ci      break;
2975bf215546Sopenharmony_ci   case GLSL_TYPE_INT:
2976bf215546Sopenharmony_ci      return nir_type_int32;
2977bf215546Sopenharmony_ci      break;
2978bf215546Sopenharmony_ci   case GLSL_TYPE_UINT16:
2979bf215546Sopenharmony_ci      return nir_type_uint16;
2980bf215546Sopenharmony_ci      break;
2981bf215546Sopenharmony_ci   case GLSL_TYPE_INT16:
2982bf215546Sopenharmony_ci      return nir_type_int16;
2983bf215546Sopenharmony_ci      break;
2984bf215546Sopenharmony_ci   case GLSL_TYPE_UINT8:
2985bf215546Sopenharmony_ci      return nir_type_uint8;
2986bf215546Sopenharmony_ci   case GLSL_TYPE_INT8:
2987bf215546Sopenharmony_ci      return nir_type_int8;
2988bf215546Sopenharmony_ci   case GLSL_TYPE_UINT64:
2989bf215546Sopenharmony_ci      return nir_type_uint64;
2990bf215546Sopenharmony_ci      break;
2991bf215546Sopenharmony_ci   case GLSL_TYPE_INT64:
2992bf215546Sopenharmony_ci      return nir_type_int64;
2993bf215546Sopenharmony_ci      break;
2994bf215546Sopenharmony_ci   case GLSL_TYPE_FLOAT:
2995bf215546Sopenharmony_ci      return nir_type_float32;
2996bf215546Sopenharmony_ci      break;
2997bf215546Sopenharmony_ci   case GLSL_TYPE_FLOAT16:
2998bf215546Sopenharmony_ci      return nir_type_float16;
2999bf215546Sopenharmony_ci      break;
3000bf215546Sopenharmony_ci   case GLSL_TYPE_DOUBLE:
3001bf215546Sopenharmony_ci      return nir_type_float64;
3002bf215546Sopenharmony_ci      break;
3003bf215546Sopenharmony_ci
3004bf215546Sopenharmony_ci   case GLSL_TYPE_SAMPLER:
3005bf215546Sopenharmony_ci   case GLSL_TYPE_TEXTURE:
3006bf215546Sopenharmony_ci   case GLSL_TYPE_IMAGE:
3007bf215546Sopenharmony_ci   case GLSL_TYPE_ATOMIC_UINT:
3008bf215546Sopenharmony_ci   case GLSL_TYPE_STRUCT:
3009bf215546Sopenharmony_ci   case GLSL_TYPE_INTERFACE:
3010bf215546Sopenharmony_ci   case GLSL_TYPE_ARRAY:
3011bf215546Sopenharmony_ci   case GLSL_TYPE_VOID:
3012bf215546Sopenharmony_ci   case GLSL_TYPE_SUBROUTINE:
3013bf215546Sopenharmony_ci   case GLSL_TYPE_FUNCTION:
3014bf215546Sopenharmony_ci   case GLSL_TYPE_ERROR:
3015bf215546Sopenharmony_ci      return nir_type_invalid;
3016bf215546Sopenharmony_ci   }
3017bf215546Sopenharmony_ci
3018bf215546Sopenharmony_ci   unreachable("unknown type");
3019bf215546Sopenharmony_ci}
3020bf215546Sopenharmony_ci
3021bf215546Sopenharmony_cienum glsl_base_type
3022bf215546Sopenharmony_cinir_get_glsl_base_type_for_nir_type(nir_alu_type base_type)
3023bf215546Sopenharmony_ci{
3024bf215546Sopenharmony_ci   switch (base_type) {
3025bf215546Sopenharmony_ci   case nir_type_bool1:
3026bf215546Sopenharmony_ci      return GLSL_TYPE_BOOL;
3027bf215546Sopenharmony_ci   case nir_type_uint32:
3028bf215546Sopenharmony_ci      return GLSL_TYPE_UINT;
3029bf215546Sopenharmony_ci   case nir_type_int32:
3030bf215546Sopenharmony_ci      return GLSL_TYPE_INT;
3031bf215546Sopenharmony_ci   case nir_type_uint16:
3032bf215546Sopenharmony_ci      return GLSL_TYPE_UINT16;
3033bf215546Sopenharmony_ci   case nir_type_int16:
3034bf215546Sopenharmony_ci      return GLSL_TYPE_INT16;
3035bf215546Sopenharmony_ci   case nir_type_uint8:
3036bf215546Sopenharmony_ci      return GLSL_TYPE_UINT8;
3037bf215546Sopenharmony_ci   case nir_type_int8:
3038bf215546Sopenharmony_ci      return GLSL_TYPE_INT8;
3039bf215546Sopenharmony_ci   case nir_type_uint64:
3040bf215546Sopenharmony_ci      return GLSL_TYPE_UINT64;
3041bf215546Sopenharmony_ci   case nir_type_int64:
3042bf215546Sopenharmony_ci      return GLSL_TYPE_INT64;
3043bf215546Sopenharmony_ci   case nir_type_float32:
3044bf215546Sopenharmony_ci      return GLSL_TYPE_FLOAT;
3045bf215546Sopenharmony_ci   case nir_type_float16:
3046bf215546Sopenharmony_ci      return GLSL_TYPE_FLOAT16;
3047bf215546Sopenharmony_ci   case nir_type_float64:
3048bf215546Sopenharmony_ci      return GLSL_TYPE_DOUBLE;
3049bf215546Sopenharmony_ci
3050bf215546Sopenharmony_ci   default: unreachable("Not a sized nir_alu_type");
3051bf215546Sopenharmony_ci   }
3052bf215546Sopenharmony_ci}
3053bf215546Sopenharmony_ci
3054bf215546Sopenharmony_cinir_op
3055bf215546Sopenharmony_cinir_op_vec(unsigned components)
3056bf215546Sopenharmony_ci{
3057bf215546Sopenharmony_ci   switch (components) {
3058bf215546Sopenharmony_ci   case  1: return nir_op_mov;
3059bf215546Sopenharmony_ci   case  2: return nir_op_vec2;
3060bf215546Sopenharmony_ci   case  3: return nir_op_vec3;
3061bf215546Sopenharmony_ci   case  4: return nir_op_vec4;
3062bf215546Sopenharmony_ci   case  5: return nir_op_vec5;
3063bf215546Sopenharmony_ci   case  8: return nir_op_vec8;
3064bf215546Sopenharmony_ci   case 16: return nir_op_vec16;
3065bf215546Sopenharmony_ci   default: unreachable("bad component count");
3066bf215546Sopenharmony_ci   }
3067bf215546Sopenharmony_ci}
3068bf215546Sopenharmony_ci
3069bf215546Sopenharmony_cibool
3070bf215546Sopenharmony_cinir_op_is_vec(nir_op op)
3071bf215546Sopenharmony_ci{
3072bf215546Sopenharmony_ci   switch (op) {
3073bf215546Sopenharmony_ci   case nir_op_mov:
3074bf215546Sopenharmony_ci   case nir_op_vec2:
3075bf215546Sopenharmony_ci   case nir_op_vec3:
3076bf215546Sopenharmony_ci   case nir_op_vec4:
3077bf215546Sopenharmony_ci   case nir_op_vec5:
3078bf215546Sopenharmony_ci   case nir_op_vec8:
3079bf215546Sopenharmony_ci   case nir_op_vec16:
3080bf215546Sopenharmony_ci      return true;
3081bf215546Sopenharmony_ci   default:
3082bf215546Sopenharmony_ci      return false;
3083bf215546Sopenharmony_ci   }
3084bf215546Sopenharmony_ci}
3085bf215546Sopenharmony_ci
3086bf215546Sopenharmony_cibool
3087bf215546Sopenharmony_cinir_alu_instr_channel_used(const nir_alu_instr *instr, unsigned src,
3088bf215546Sopenharmony_ci                           unsigned channel)
3089bf215546Sopenharmony_ci{
3090bf215546Sopenharmony_ci   if (nir_op_infos[instr->op].input_sizes[src] > 0)
3091bf215546Sopenharmony_ci      return channel < nir_op_infos[instr->op].input_sizes[src];
3092bf215546Sopenharmony_ci
3093bf215546Sopenharmony_ci   return (instr->dest.write_mask >> channel) & 1;
3094bf215546Sopenharmony_ci}
3095bf215546Sopenharmony_ci
3096bf215546Sopenharmony_cinir_component_mask_t
3097bf215546Sopenharmony_cinir_alu_instr_src_read_mask(const nir_alu_instr *instr, unsigned src)
3098bf215546Sopenharmony_ci{
3099bf215546Sopenharmony_ci   nir_component_mask_t read_mask = 0;
3100bf215546Sopenharmony_ci   for (unsigned c = 0; c < NIR_MAX_VEC_COMPONENTS; c++) {
3101bf215546Sopenharmony_ci      if (!nir_alu_instr_channel_used(instr, src, c))
3102bf215546Sopenharmony_ci         continue;
3103bf215546Sopenharmony_ci
3104bf215546Sopenharmony_ci      read_mask |= (1 << instr->src[src].swizzle[c]);
3105bf215546Sopenharmony_ci   }
3106bf215546Sopenharmony_ci   return read_mask;
3107bf215546Sopenharmony_ci}
3108bf215546Sopenharmony_ci
3109bf215546Sopenharmony_ciunsigned
3110bf215546Sopenharmony_cinir_ssa_alu_instr_src_components(const nir_alu_instr *instr, unsigned src)
3111bf215546Sopenharmony_ci{
3112bf215546Sopenharmony_ci   if (nir_op_infos[instr->op].input_sizes[src] > 0)
3113bf215546Sopenharmony_ci      return nir_op_infos[instr->op].input_sizes[src];
3114bf215546Sopenharmony_ci
3115bf215546Sopenharmony_ci   return nir_dest_num_components(instr->dest.dest);
3116bf215546Sopenharmony_ci}
3117bf215546Sopenharmony_ci
3118bf215546Sopenharmony_ci#define CASE_ALL_SIZES(op) \
3119bf215546Sopenharmony_ci   case op: \
3120bf215546Sopenharmony_ci   case op ## 8: \
3121bf215546Sopenharmony_ci   case op ## 16: \
3122bf215546Sopenharmony_ci   case op ## 32: \
3123bf215546Sopenharmony_ci
3124bf215546Sopenharmony_cibool
3125bf215546Sopenharmony_cinir_alu_instr_is_comparison(const nir_alu_instr *instr)
3126bf215546Sopenharmony_ci{
3127bf215546Sopenharmony_ci   switch (instr->op) {
3128bf215546Sopenharmony_ci   CASE_ALL_SIZES(nir_op_flt)
3129bf215546Sopenharmony_ci   CASE_ALL_SIZES(nir_op_fge)
3130bf215546Sopenharmony_ci   CASE_ALL_SIZES(nir_op_feq)
3131bf215546Sopenharmony_ci   CASE_ALL_SIZES(nir_op_fneu)
3132bf215546Sopenharmony_ci   CASE_ALL_SIZES(nir_op_ilt)
3133bf215546Sopenharmony_ci   CASE_ALL_SIZES(nir_op_ult)
3134bf215546Sopenharmony_ci   CASE_ALL_SIZES(nir_op_ige)
3135bf215546Sopenharmony_ci   CASE_ALL_SIZES(nir_op_uge)
3136bf215546Sopenharmony_ci   CASE_ALL_SIZES(nir_op_ieq)
3137bf215546Sopenharmony_ci   CASE_ALL_SIZES(nir_op_ine)
3138bf215546Sopenharmony_ci   case nir_op_i2b1:
3139bf215546Sopenharmony_ci   case nir_op_i2b8:
3140bf215546Sopenharmony_ci   case nir_op_i2b16:
3141bf215546Sopenharmony_ci   case nir_op_i2b32:
3142bf215546Sopenharmony_ci   case nir_op_f2b1:
3143bf215546Sopenharmony_ci   case nir_op_f2b8:
3144bf215546Sopenharmony_ci   case nir_op_f2b16:
3145bf215546Sopenharmony_ci   case nir_op_f2b32:
3146bf215546Sopenharmony_ci   case nir_op_inot:
3147bf215546Sopenharmony_ci      return true;
3148bf215546Sopenharmony_ci   default:
3149bf215546Sopenharmony_ci      return false;
3150bf215546Sopenharmony_ci   }
3151bf215546Sopenharmony_ci}
3152bf215546Sopenharmony_ci
3153bf215546Sopenharmony_ci#undef CASE_ALL_SIZES
3154bf215546Sopenharmony_ci
3155bf215546Sopenharmony_ciunsigned
3156bf215546Sopenharmony_cinir_intrinsic_src_components(const nir_intrinsic_instr *intr, unsigned srcn)
3157bf215546Sopenharmony_ci{
3158bf215546Sopenharmony_ci   const nir_intrinsic_info *info = &nir_intrinsic_infos[intr->intrinsic];
3159bf215546Sopenharmony_ci   assert(srcn < info->num_srcs);
3160bf215546Sopenharmony_ci   if (info->src_components[srcn] > 0)
3161bf215546Sopenharmony_ci      return info->src_components[srcn];
3162bf215546Sopenharmony_ci   else if (info->src_components[srcn] == 0)
3163bf215546Sopenharmony_ci      return intr->num_components;
3164bf215546Sopenharmony_ci   else
3165bf215546Sopenharmony_ci      return nir_src_num_components(intr->src[srcn]);
3166bf215546Sopenharmony_ci}
3167bf215546Sopenharmony_ci
3168bf215546Sopenharmony_ciunsigned
3169bf215546Sopenharmony_cinir_intrinsic_dest_components(nir_intrinsic_instr *intr)
3170bf215546Sopenharmony_ci{
3171bf215546Sopenharmony_ci   const nir_intrinsic_info *info = &nir_intrinsic_infos[intr->intrinsic];
3172bf215546Sopenharmony_ci   if (!info->has_dest)
3173bf215546Sopenharmony_ci      return 0;
3174bf215546Sopenharmony_ci   else if (info->dest_components)
3175bf215546Sopenharmony_ci      return info->dest_components;
3176bf215546Sopenharmony_ci   else
3177bf215546Sopenharmony_ci      return intr->num_components;
3178bf215546Sopenharmony_ci}
3179bf215546Sopenharmony_ci
3180bf215546Sopenharmony_ci/**
3181bf215546Sopenharmony_ci * Helper to copy const_index[] from src to dst, without assuming they
3182bf215546Sopenharmony_ci * match in order.
3183bf215546Sopenharmony_ci */
3184bf215546Sopenharmony_civoid
3185bf215546Sopenharmony_cinir_intrinsic_copy_const_indices(nir_intrinsic_instr *dst, nir_intrinsic_instr *src)
3186bf215546Sopenharmony_ci{
3187bf215546Sopenharmony_ci   if (src->intrinsic == dst->intrinsic) {
3188bf215546Sopenharmony_ci      memcpy(dst->const_index, src->const_index, sizeof(dst->const_index));
3189bf215546Sopenharmony_ci      return;
3190bf215546Sopenharmony_ci   }
3191bf215546Sopenharmony_ci
3192bf215546Sopenharmony_ci   const nir_intrinsic_info *src_info = &nir_intrinsic_infos[src->intrinsic];
3193bf215546Sopenharmony_ci   const nir_intrinsic_info *dst_info = &nir_intrinsic_infos[dst->intrinsic];
3194bf215546Sopenharmony_ci
3195bf215546Sopenharmony_ci   for (unsigned i = 0; i < NIR_INTRINSIC_NUM_INDEX_FLAGS; i++) {
3196bf215546Sopenharmony_ci      if (src_info->index_map[i] == 0)
3197bf215546Sopenharmony_ci         continue;
3198bf215546Sopenharmony_ci
3199bf215546Sopenharmony_ci      /* require that dst instruction also uses the same const_index[]: */
3200bf215546Sopenharmony_ci      assert(dst_info->index_map[i] > 0);
3201bf215546Sopenharmony_ci
3202bf215546Sopenharmony_ci      dst->const_index[dst_info->index_map[i] - 1] =
3203bf215546Sopenharmony_ci            src->const_index[src_info->index_map[i] - 1];
3204bf215546Sopenharmony_ci   }
3205bf215546Sopenharmony_ci}
3206bf215546Sopenharmony_ci
3207bf215546Sopenharmony_ci
3208bf215546Sopenharmony_cibool
3209bf215546Sopenharmony_cinir_tex_instr_need_sampler(const nir_tex_instr *instr)
3210bf215546Sopenharmony_ci{
3211bf215546Sopenharmony_ci   switch (instr->op) {
3212bf215546Sopenharmony_ci   case nir_texop_txf:
3213bf215546Sopenharmony_ci   case nir_texop_txf_ms:
3214bf215546Sopenharmony_ci   case nir_texop_txs:
3215bf215546Sopenharmony_ci   case nir_texop_query_levels:
3216bf215546Sopenharmony_ci   case nir_texop_texture_samples:
3217bf215546Sopenharmony_ci   case nir_texop_samples_identical:
3218bf215546Sopenharmony_ci      return false;
3219bf215546Sopenharmony_ci   default:
3220bf215546Sopenharmony_ci      return true;
3221bf215546Sopenharmony_ci   }
3222bf215546Sopenharmony_ci}
3223bf215546Sopenharmony_ci
3224bf215546Sopenharmony_ciunsigned
3225bf215546Sopenharmony_cinir_tex_instr_result_size(const nir_tex_instr *instr)
3226bf215546Sopenharmony_ci{
3227bf215546Sopenharmony_ci   switch (instr->op) {
3228bf215546Sopenharmony_ci   case nir_texop_txs: {
3229bf215546Sopenharmony_ci      unsigned ret;
3230bf215546Sopenharmony_ci      switch (instr->sampler_dim) {
3231bf215546Sopenharmony_ci         case GLSL_SAMPLER_DIM_1D:
3232bf215546Sopenharmony_ci         case GLSL_SAMPLER_DIM_BUF:
3233bf215546Sopenharmony_ci            ret = 1;
3234bf215546Sopenharmony_ci            break;
3235bf215546Sopenharmony_ci         case GLSL_SAMPLER_DIM_2D:
3236bf215546Sopenharmony_ci         case GLSL_SAMPLER_DIM_CUBE:
3237bf215546Sopenharmony_ci         case GLSL_SAMPLER_DIM_MS:
3238bf215546Sopenharmony_ci         case GLSL_SAMPLER_DIM_RECT:
3239bf215546Sopenharmony_ci         case GLSL_SAMPLER_DIM_EXTERNAL:
3240bf215546Sopenharmony_ci         case GLSL_SAMPLER_DIM_SUBPASS:
3241bf215546Sopenharmony_ci            ret = 2;
3242bf215546Sopenharmony_ci            break;
3243bf215546Sopenharmony_ci         case GLSL_SAMPLER_DIM_3D:
3244bf215546Sopenharmony_ci            ret = 3;
3245bf215546Sopenharmony_ci            break;
3246bf215546Sopenharmony_ci         default:
3247bf215546Sopenharmony_ci            unreachable("not reached");
3248bf215546Sopenharmony_ci      }
3249bf215546Sopenharmony_ci      if (instr->is_array)
3250bf215546Sopenharmony_ci         ret++;
3251bf215546Sopenharmony_ci      return ret;
3252bf215546Sopenharmony_ci   }
3253bf215546Sopenharmony_ci
3254bf215546Sopenharmony_ci   case nir_texop_lod:
3255bf215546Sopenharmony_ci      return 2;
3256bf215546Sopenharmony_ci
3257bf215546Sopenharmony_ci   case nir_texop_texture_samples:
3258bf215546Sopenharmony_ci   case nir_texop_query_levels:
3259bf215546Sopenharmony_ci   case nir_texop_samples_identical:
3260bf215546Sopenharmony_ci   case nir_texop_fragment_mask_fetch_amd:
3261bf215546Sopenharmony_ci      return 1;
3262bf215546Sopenharmony_ci
3263bf215546Sopenharmony_ci   default:
3264bf215546Sopenharmony_ci      if (instr->is_shadow && instr->is_new_style_shadow)
3265bf215546Sopenharmony_ci         return 1;
3266bf215546Sopenharmony_ci
3267bf215546Sopenharmony_ci      return 4;
3268bf215546Sopenharmony_ci   }
3269bf215546Sopenharmony_ci}
3270bf215546Sopenharmony_ci
3271bf215546Sopenharmony_cibool
3272bf215546Sopenharmony_cinir_tex_instr_is_query(const nir_tex_instr *instr)
3273bf215546Sopenharmony_ci{
3274bf215546Sopenharmony_ci   switch (instr->op) {
3275bf215546Sopenharmony_ci   case nir_texop_txs:
3276bf215546Sopenharmony_ci   case nir_texop_lod:
3277bf215546Sopenharmony_ci   case nir_texop_texture_samples:
3278bf215546Sopenharmony_ci   case nir_texop_query_levels:
3279bf215546Sopenharmony_ci      return true;
3280bf215546Sopenharmony_ci   case nir_texop_tex:
3281bf215546Sopenharmony_ci   case nir_texop_txb:
3282bf215546Sopenharmony_ci   case nir_texop_txl:
3283bf215546Sopenharmony_ci   case nir_texop_txd:
3284bf215546Sopenharmony_ci   case nir_texop_txf:
3285bf215546Sopenharmony_ci   case nir_texop_txf_ms:
3286bf215546Sopenharmony_ci   case nir_texop_txf_ms_fb:
3287bf215546Sopenharmony_ci   case nir_texop_txf_ms_mcs_intel:
3288bf215546Sopenharmony_ci   case nir_texop_tg4:
3289bf215546Sopenharmony_ci      return false;
3290bf215546Sopenharmony_ci   default:
3291bf215546Sopenharmony_ci      unreachable("Invalid texture opcode");
3292bf215546Sopenharmony_ci   }
3293bf215546Sopenharmony_ci}
3294bf215546Sopenharmony_ci
3295bf215546Sopenharmony_cibool
3296bf215546Sopenharmony_cinir_tex_instr_has_implicit_derivative(const nir_tex_instr *instr)
3297bf215546Sopenharmony_ci{
3298bf215546Sopenharmony_ci   switch (instr->op) {
3299bf215546Sopenharmony_ci   case nir_texop_tex:
3300bf215546Sopenharmony_ci   case nir_texop_txb:
3301bf215546Sopenharmony_ci   case nir_texop_lod:
3302bf215546Sopenharmony_ci      return true;
3303bf215546Sopenharmony_ci   default:
3304bf215546Sopenharmony_ci      return false;
3305bf215546Sopenharmony_ci   }
3306bf215546Sopenharmony_ci}
3307bf215546Sopenharmony_ci
3308bf215546Sopenharmony_cinir_alu_type
3309bf215546Sopenharmony_cinir_tex_instr_src_type(const nir_tex_instr *instr, unsigned src)
3310bf215546Sopenharmony_ci{
3311bf215546Sopenharmony_ci   switch (instr->src[src].src_type) {
3312bf215546Sopenharmony_ci   case nir_tex_src_coord:
3313bf215546Sopenharmony_ci      switch (instr->op) {
3314bf215546Sopenharmony_ci      case nir_texop_txf:
3315bf215546Sopenharmony_ci      case nir_texop_txf_ms:
3316bf215546Sopenharmony_ci      case nir_texop_txf_ms_fb:
3317bf215546Sopenharmony_ci      case nir_texop_txf_ms_mcs_intel:
3318bf215546Sopenharmony_ci      case nir_texop_samples_identical:
3319bf215546Sopenharmony_ci      case nir_texop_fragment_fetch_amd:
3320bf215546Sopenharmony_ci      case nir_texop_fragment_mask_fetch_amd:
3321bf215546Sopenharmony_ci         return nir_type_int;
3322bf215546Sopenharmony_ci
3323bf215546Sopenharmony_ci      default:
3324bf215546Sopenharmony_ci         return nir_type_float;
3325bf215546Sopenharmony_ci      }
3326bf215546Sopenharmony_ci
3327bf215546Sopenharmony_ci   case nir_tex_src_lod:
3328bf215546Sopenharmony_ci      switch (instr->op) {
3329bf215546Sopenharmony_ci      case nir_texop_txs:
3330bf215546Sopenharmony_ci      case nir_texop_txf:
3331bf215546Sopenharmony_ci      case nir_texop_txf_ms:
3332bf215546Sopenharmony_ci      case nir_texop_fragment_fetch_amd:
3333bf215546Sopenharmony_ci      case nir_texop_fragment_mask_fetch_amd:
3334bf215546Sopenharmony_ci         return nir_type_int;
3335bf215546Sopenharmony_ci
3336bf215546Sopenharmony_ci      default:
3337bf215546Sopenharmony_ci         return nir_type_float;
3338bf215546Sopenharmony_ci      }
3339bf215546Sopenharmony_ci
3340bf215546Sopenharmony_ci   case nir_tex_src_projector:
3341bf215546Sopenharmony_ci   case nir_tex_src_comparator:
3342bf215546Sopenharmony_ci   case nir_tex_src_bias:
3343bf215546Sopenharmony_ci   case nir_tex_src_min_lod:
3344bf215546Sopenharmony_ci   case nir_tex_src_ddx:
3345bf215546Sopenharmony_ci   case nir_tex_src_ddy:
3346bf215546Sopenharmony_ci   case nir_tex_src_backend1:
3347bf215546Sopenharmony_ci   case nir_tex_src_backend2:
3348bf215546Sopenharmony_ci      return nir_type_float;
3349bf215546Sopenharmony_ci
3350bf215546Sopenharmony_ci   case nir_tex_src_offset:
3351bf215546Sopenharmony_ci   case nir_tex_src_ms_index:
3352bf215546Sopenharmony_ci   case nir_tex_src_plane:
3353bf215546Sopenharmony_ci      return nir_type_int;
3354bf215546Sopenharmony_ci
3355bf215546Sopenharmony_ci   case nir_tex_src_ms_mcs_intel:
3356bf215546Sopenharmony_ci   case nir_tex_src_texture_deref:
3357bf215546Sopenharmony_ci   case nir_tex_src_sampler_deref:
3358bf215546Sopenharmony_ci   case nir_tex_src_texture_offset:
3359bf215546Sopenharmony_ci   case nir_tex_src_sampler_offset:
3360bf215546Sopenharmony_ci   case nir_tex_src_texture_handle:
3361bf215546Sopenharmony_ci   case nir_tex_src_sampler_handle:
3362bf215546Sopenharmony_ci      return nir_type_uint;
3363bf215546Sopenharmony_ci
3364bf215546Sopenharmony_ci   case nir_num_tex_src_types:
3365bf215546Sopenharmony_ci      unreachable("nir_num_tex_src_types is not a valid source type");
3366bf215546Sopenharmony_ci   }
3367bf215546Sopenharmony_ci
3368bf215546Sopenharmony_ci   unreachable("Invalid texture source type");
3369bf215546Sopenharmony_ci}
3370bf215546Sopenharmony_ci
3371bf215546Sopenharmony_ciunsigned
3372bf215546Sopenharmony_cinir_tex_instr_src_size(const nir_tex_instr *instr, unsigned src)
3373bf215546Sopenharmony_ci{
3374bf215546Sopenharmony_ci   if (instr->src[src].src_type == nir_tex_src_coord)
3375bf215546Sopenharmony_ci      return instr->coord_components;
3376bf215546Sopenharmony_ci
3377bf215546Sopenharmony_ci   /* The MCS value is expected to be a vec4 returned by a txf_ms_mcs_intel */
3378bf215546Sopenharmony_ci   if (instr->src[src].src_type == nir_tex_src_ms_mcs_intel)
3379bf215546Sopenharmony_ci      return 4;
3380bf215546Sopenharmony_ci
3381bf215546Sopenharmony_ci   if (instr->src[src].src_type == nir_tex_src_ddx ||
3382bf215546Sopenharmony_ci       instr->src[src].src_type == nir_tex_src_ddy) {
3383bf215546Sopenharmony_ci
3384bf215546Sopenharmony_ci      if (instr->is_array && !instr->array_is_lowered_cube)
3385bf215546Sopenharmony_ci         return instr->coord_components - 1;
3386bf215546Sopenharmony_ci      else
3387bf215546Sopenharmony_ci         return instr->coord_components;
3388bf215546Sopenharmony_ci   }
3389bf215546Sopenharmony_ci
3390bf215546Sopenharmony_ci   if (instr->src[src].src_type == nir_tex_src_offset) {
3391bf215546Sopenharmony_ci      if (instr->is_array)
3392bf215546Sopenharmony_ci         return instr->coord_components - 1;
3393bf215546Sopenharmony_ci      else
3394bf215546Sopenharmony_ci         return instr->coord_components;
3395bf215546Sopenharmony_ci   }
3396bf215546Sopenharmony_ci
3397bf215546Sopenharmony_ci   if (instr->src[src].src_type == nir_tex_src_backend1 ||
3398bf215546Sopenharmony_ci       instr->src[src].src_type == nir_tex_src_backend2)
3399bf215546Sopenharmony_ci      return nir_src_num_components(instr->src[src].src);
3400bf215546Sopenharmony_ci
3401bf215546Sopenharmony_ci   /* For AMD, this can be a vec8/vec4 image/sampler descriptor. */
3402bf215546Sopenharmony_ci   if (instr->src[src].src_type == nir_tex_src_texture_handle ||
3403bf215546Sopenharmony_ci       instr->src[src].src_type == nir_tex_src_sampler_handle)
3404bf215546Sopenharmony_ci      return 0;
3405bf215546Sopenharmony_ci
3406bf215546Sopenharmony_ci   return 1;
3407bf215546Sopenharmony_ci}
3408bf215546Sopenharmony_ci
3409bf215546Sopenharmony_ci/**
3410bf215546Sopenharmony_ci * Return which components are written into transform feedback buffers.
3411bf215546Sopenharmony_ci * The result is relative to 0, not "component".
3412bf215546Sopenharmony_ci */
3413bf215546Sopenharmony_ciunsigned
3414bf215546Sopenharmony_cinir_instr_xfb_write_mask(nir_intrinsic_instr *instr)
3415bf215546Sopenharmony_ci{
3416bf215546Sopenharmony_ci   unsigned mask = 0;
3417bf215546Sopenharmony_ci
3418bf215546Sopenharmony_ci   if (nir_intrinsic_has_io_xfb(instr)) {
3419bf215546Sopenharmony_ci      unsigned wr_mask = nir_intrinsic_write_mask(instr) <<
3420bf215546Sopenharmony_ci                         nir_intrinsic_component(instr);
3421bf215546Sopenharmony_ci      assert((wr_mask & ~0xf) == 0); /* only 4 components allowed */
3422bf215546Sopenharmony_ci
3423bf215546Sopenharmony_ci      unsigned iter_mask = wr_mask;
3424bf215546Sopenharmony_ci      while (iter_mask) {
3425bf215546Sopenharmony_ci         unsigned i = u_bit_scan(&iter_mask);
3426bf215546Sopenharmony_ci         nir_io_xfb xfb = i < 2 ? nir_intrinsic_io_xfb(instr) :
3427bf215546Sopenharmony_ci                                  nir_intrinsic_io_xfb2(instr);
3428bf215546Sopenharmony_ci         if (xfb.out[i % 2].num_components)
3429bf215546Sopenharmony_ci            mask |= BITFIELD_RANGE(i, xfb.out[i % 2].num_components) & wr_mask;
3430bf215546Sopenharmony_ci      }
3431bf215546Sopenharmony_ci   }
3432bf215546Sopenharmony_ci
3433bf215546Sopenharmony_ci   return mask;
3434bf215546Sopenharmony_ci}
3435bf215546Sopenharmony_ci
3436bf215546Sopenharmony_ci/**
3437bf215546Sopenharmony_ci * Whether an output slot is consumed by fixed-function logic.
3438bf215546Sopenharmony_ci */
3439bf215546Sopenharmony_cibool
3440bf215546Sopenharmony_cinir_slot_is_sysval_output(gl_varying_slot slot)
3441bf215546Sopenharmony_ci{
3442bf215546Sopenharmony_ci   return slot == VARYING_SLOT_POS ||
3443bf215546Sopenharmony_ci          slot == VARYING_SLOT_PSIZ ||
3444bf215546Sopenharmony_ci          slot == VARYING_SLOT_EDGE ||
3445bf215546Sopenharmony_ci          slot == VARYING_SLOT_CLIP_VERTEX ||
3446bf215546Sopenharmony_ci          slot == VARYING_SLOT_CLIP_DIST0 ||
3447bf215546Sopenharmony_ci          slot == VARYING_SLOT_CLIP_DIST1 ||
3448bf215546Sopenharmony_ci          slot == VARYING_SLOT_CULL_DIST0 ||
3449bf215546Sopenharmony_ci          slot == VARYING_SLOT_CULL_DIST1 ||
3450bf215546Sopenharmony_ci          slot == VARYING_SLOT_LAYER ||
3451bf215546Sopenharmony_ci          slot == VARYING_SLOT_VIEWPORT ||
3452bf215546Sopenharmony_ci          slot == VARYING_SLOT_TESS_LEVEL_OUTER ||
3453bf215546Sopenharmony_ci          slot == VARYING_SLOT_TESS_LEVEL_INNER ||
3454bf215546Sopenharmony_ci          slot == VARYING_SLOT_BOUNDING_BOX0 ||
3455bf215546Sopenharmony_ci          slot == VARYING_SLOT_BOUNDING_BOX1 ||
3456bf215546Sopenharmony_ci          slot == VARYING_SLOT_VIEW_INDEX ||
3457bf215546Sopenharmony_ci          slot == VARYING_SLOT_VIEWPORT_MASK ||
3458bf215546Sopenharmony_ci          slot == VARYING_SLOT_PRIMITIVE_SHADING_RATE ||
3459bf215546Sopenharmony_ci          slot == VARYING_SLOT_PRIMITIVE_COUNT ||
3460bf215546Sopenharmony_ci          slot == VARYING_SLOT_PRIMITIVE_INDICES ||
3461bf215546Sopenharmony_ci          slot == VARYING_SLOT_TASK_COUNT;
3462bf215546Sopenharmony_ci}
3463bf215546Sopenharmony_ci
3464bf215546Sopenharmony_ci/**
3465bf215546Sopenharmony_ci * Whether an input/output slot is consumed by the next shader stage,
3466bf215546Sopenharmony_ci * or written by the previous shader stage.
3467bf215546Sopenharmony_ci */
3468bf215546Sopenharmony_cibool
3469bf215546Sopenharmony_cinir_slot_is_varying(gl_varying_slot slot)
3470bf215546Sopenharmony_ci{
3471bf215546Sopenharmony_ci   return slot >= VARYING_SLOT_VAR0 ||
3472bf215546Sopenharmony_ci          slot == VARYING_SLOT_COL0 ||
3473bf215546Sopenharmony_ci          slot == VARYING_SLOT_COL1 ||
3474bf215546Sopenharmony_ci          slot == VARYING_SLOT_BFC0 ||
3475bf215546Sopenharmony_ci          slot == VARYING_SLOT_BFC1 ||
3476bf215546Sopenharmony_ci          slot == VARYING_SLOT_FOGC ||
3477bf215546Sopenharmony_ci          (slot >= VARYING_SLOT_TEX0 && slot <= VARYING_SLOT_TEX7) ||
3478bf215546Sopenharmony_ci          slot == VARYING_SLOT_PNTC ||
3479bf215546Sopenharmony_ci          slot == VARYING_SLOT_CLIP_DIST0 ||
3480bf215546Sopenharmony_ci          slot == VARYING_SLOT_CLIP_DIST1 ||
3481bf215546Sopenharmony_ci          slot == VARYING_SLOT_CULL_DIST0 ||
3482bf215546Sopenharmony_ci          slot == VARYING_SLOT_CULL_DIST1 ||
3483bf215546Sopenharmony_ci          slot == VARYING_SLOT_PRIMITIVE_ID ||
3484bf215546Sopenharmony_ci          slot == VARYING_SLOT_LAYER ||
3485bf215546Sopenharmony_ci          slot == VARYING_SLOT_VIEWPORT ||
3486bf215546Sopenharmony_ci          slot == VARYING_SLOT_TESS_LEVEL_OUTER ||
3487bf215546Sopenharmony_ci          slot == VARYING_SLOT_TESS_LEVEL_INNER;
3488bf215546Sopenharmony_ci}
3489bf215546Sopenharmony_ci
3490bf215546Sopenharmony_cibool
3491bf215546Sopenharmony_cinir_slot_is_sysval_output_and_varying(gl_varying_slot slot)
3492bf215546Sopenharmony_ci{
3493bf215546Sopenharmony_ci   return nir_slot_is_sysval_output(slot) &&
3494bf215546Sopenharmony_ci          nir_slot_is_varying(slot);
3495bf215546Sopenharmony_ci}
3496bf215546Sopenharmony_ci
3497bf215546Sopenharmony_ci/**
3498bf215546Sopenharmony_ci * This marks the output store instruction as not feeding the next shader
3499bf215546Sopenharmony_ci * stage. If the instruction has no other use, it's removed.
3500bf215546Sopenharmony_ci */
3501bf215546Sopenharmony_civoid nir_remove_varying(nir_intrinsic_instr *intr)
3502bf215546Sopenharmony_ci{
3503bf215546Sopenharmony_ci   nir_io_semantics sem = nir_intrinsic_io_semantics(intr);
3504bf215546Sopenharmony_ci
3505bf215546Sopenharmony_ci   if ((!sem.no_sysval_output && nir_slot_is_sysval_output(sem.location)) ||
3506bf215546Sopenharmony_ci       nir_instr_xfb_write_mask(intr)) {
3507bf215546Sopenharmony_ci      /* Demote the store instruction. */
3508bf215546Sopenharmony_ci      sem.no_varying = true;
3509bf215546Sopenharmony_ci      nir_intrinsic_set_io_semantics(intr, sem);
3510bf215546Sopenharmony_ci   } else {
3511bf215546Sopenharmony_ci      nir_instr_remove(&intr->instr);
3512bf215546Sopenharmony_ci   }
3513bf215546Sopenharmony_ci}
3514bf215546Sopenharmony_ci
3515bf215546Sopenharmony_ci/**
3516bf215546Sopenharmony_ci * This marks the output store instruction as not feeding fixed-function
3517bf215546Sopenharmony_ci * logic. If the instruction has no other use, it's removed.
3518bf215546Sopenharmony_ci */
3519bf215546Sopenharmony_civoid nir_remove_sysval_output(nir_intrinsic_instr *intr)
3520bf215546Sopenharmony_ci{
3521bf215546Sopenharmony_ci   nir_io_semantics sem = nir_intrinsic_io_semantics(intr);
3522bf215546Sopenharmony_ci
3523bf215546Sopenharmony_ci   if ((!sem.no_varying && nir_slot_is_varying(sem.location)) ||
3524bf215546Sopenharmony_ci       nir_instr_xfb_write_mask(intr)) {
3525bf215546Sopenharmony_ci      /* Demote the store instruction. */
3526bf215546Sopenharmony_ci      sem.no_sysval_output = true;
3527bf215546Sopenharmony_ci      nir_intrinsic_set_io_semantics(intr, sem);
3528bf215546Sopenharmony_ci   } else {
3529bf215546Sopenharmony_ci      nir_instr_remove(&intr->instr);
3530bf215546Sopenharmony_ci   }
3531bf215546Sopenharmony_ci}
3532bf215546Sopenharmony_ci
3533bf215546Sopenharmony_civoid nir_remove_non_entrypoints(nir_shader *nir)
3534bf215546Sopenharmony_ci{
3535bf215546Sopenharmony_ci   foreach_list_typed_safe(nir_function, func, node, &nir->functions) {
3536bf215546Sopenharmony_ci      if (!func->is_entrypoint)
3537bf215546Sopenharmony_ci         exec_node_remove(&func->node);
3538bf215546Sopenharmony_ci   }
3539bf215546Sopenharmony_ci   assert(exec_list_length(&nir->functions) == 1);
3540bf215546Sopenharmony_ci}
3541