nir.c revision 7e102996
101e04c3fSmrg/*
201e04c3fSmrg * Copyright © 2014 Intel Corporation
301e04c3fSmrg *
401e04c3fSmrg * Permission is hereby granted, free of charge, to any person obtaining a
501e04c3fSmrg * copy of this software and associated documentation files (the "Software"),
601e04c3fSmrg * to deal in the Software without restriction, including without limitation
701e04c3fSmrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
801e04c3fSmrg * and/or sell copies of the Software, and to permit persons to whom the
901e04c3fSmrg * Software is furnished to do so, subject to the following conditions:
1001e04c3fSmrg *
1101e04c3fSmrg * The above copyright notice and this permission notice (including the next
1201e04c3fSmrg * paragraph) shall be included in all copies or substantial portions of the
1301e04c3fSmrg * Software.
1401e04c3fSmrg *
1501e04c3fSmrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1601e04c3fSmrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1701e04c3fSmrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
1801e04c3fSmrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1901e04c3fSmrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
2001e04c3fSmrg * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
2101e04c3fSmrg * IN THE SOFTWARE.
2201e04c3fSmrg *
2301e04c3fSmrg * Authors:
2401e04c3fSmrg *    Connor Abbott (cwabbott0@gmail.com)
2501e04c3fSmrg *
2601e04c3fSmrg */
2701e04c3fSmrg
2801e04c3fSmrg#include "nir.h"
2901e04c3fSmrg#include "nir_control_flow_private.h"
3001e04c3fSmrg#include "util/half_float.h"
3101e04c3fSmrg#include <limits.h>
3201e04c3fSmrg#include <assert.h>
3301e04c3fSmrg#include <math.h>
3401e04c3fSmrg#include "util/u_math.h"
3501e04c3fSmrg
3601e04c3fSmrg#include "main/menums.h" /* BITFIELD64_MASK */
3701e04c3fSmrg
3801e04c3fSmrgnir_shader *
3901e04c3fSmrgnir_shader_create(void *mem_ctx,
4001e04c3fSmrg                  gl_shader_stage stage,
4101e04c3fSmrg                  const nir_shader_compiler_options *options,
4201e04c3fSmrg                  shader_info *si)
4301e04c3fSmrg{
4401e04c3fSmrg   nir_shader *shader = rzalloc(mem_ctx, nir_shader);
4501e04c3fSmrg
4601e04c3fSmrg   exec_list_make_empty(&shader->uniforms);
4701e04c3fSmrg   exec_list_make_empty(&shader->inputs);
4801e04c3fSmrg   exec_list_make_empty(&shader->outputs);
4901e04c3fSmrg   exec_list_make_empty(&shader->shared);
5001e04c3fSmrg
5101e04c3fSmrg   shader->options = options;
5201e04c3fSmrg
5301e04c3fSmrg   if (si) {
5401e04c3fSmrg      assert(si->stage == stage);
5501e04c3fSmrg      shader->info = *si;
5601e04c3fSmrg   } else {
5701e04c3fSmrg      shader->info.stage = stage;
5801e04c3fSmrg   }
5901e04c3fSmrg
6001e04c3fSmrg   exec_list_make_empty(&shader->functions);
6101e04c3fSmrg   exec_list_make_empty(&shader->globals);
6201e04c3fSmrg   exec_list_make_empty(&shader->system_values);
6301e04c3fSmrg
6401e04c3fSmrg   shader->num_inputs = 0;
6501e04c3fSmrg   shader->num_outputs = 0;
6601e04c3fSmrg   shader->num_uniforms = 0;
6701e04c3fSmrg   shader->num_shared = 0;
6801e04c3fSmrg
6901e04c3fSmrg   return shader;
7001e04c3fSmrg}
7101e04c3fSmrg
7201e04c3fSmrgstatic nir_register *
7301e04c3fSmrgreg_create(void *mem_ctx, struct exec_list *list)
7401e04c3fSmrg{
7501e04c3fSmrg   nir_register *reg = ralloc(mem_ctx, nir_register);
7601e04c3fSmrg
7701e04c3fSmrg   list_inithead(&reg->uses);
7801e04c3fSmrg   list_inithead(&reg->defs);
7901e04c3fSmrg   list_inithead(&reg->if_uses);
8001e04c3fSmrg
8101e04c3fSmrg   reg->num_components = 0;
8201e04c3fSmrg   reg->bit_size = 32;
8301e04c3fSmrg   reg->num_array_elems = 0;
8401e04c3fSmrg   reg->name = NULL;
8501e04c3fSmrg
8601e04c3fSmrg   exec_list_push_tail(list, &reg->node);
8701e04c3fSmrg
8801e04c3fSmrg   return reg;
8901e04c3fSmrg}
9001e04c3fSmrg
9101e04c3fSmrgnir_register *
9201e04c3fSmrgnir_local_reg_create(nir_function_impl *impl)
9301e04c3fSmrg{
9401e04c3fSmrg   nir_register *reg = reg_create(ralloc_parent(impl), &impl->registers);
9501e04c3fSmrg   reg->index = impl->reg_alloc++;
9601e04c3fSmrg
9701e04c3fSmrg   return reg;
9801e04c3fSmrg}
9901e04c3fSmrg
10001e04c3fSmrgvoid
10101e04c3fSmrgnir_reg_remove(nir_register *reg)
10201e04c3fSmrg{
10301e04c3fSmrg   exec_node_remove(&reg->node);
10401e04c3fSmrg}
10501e04c3fSmrg
10601e04c3fSmrgvoid
10701e04c3fSmrgnir_shader_add_variable(nir_shader *shader, nir_variable *var)
10801e04c3fSmrg{
10901e04c3fSmrg   switch (var->data.mode) {
11001e04c3fSmrg   case nir_var_all:
11101e04c3fSmrg      assert(!"invalid mode");
11201e04c3fSmrg      break;
11301e04c3fSmrg
1147e102996Smaya   case nir_var_function_temp:
11501e04c3fSmrg      assert(!"nir_shader_add_variable cannot be used for local variables");
11601e04c3fSmrg      break;
11701e04c3fSmrg
1187e102996Smaya   case nir_var_shader_temp:
11901e04c3fSmrg      exec_list_push_tail(&shader->globals, &var->node);
12001e04c3fSmrg      break;
12101e04c3fSmrg
12201e04c3fSmrg   case nir_var_shader_in:
12301e04c3fSmrg      exec_list_push_tail(&shader->inputs, &var->node);
12401e04c3fSmrg      break;
12501e04c3fSmrg
12601e04c3fSmrg   case nir_var_shader_out:
12701e04c3fSmrg      exec_list_push_tail(&shader->outputs, &var->node);
12801e04c3fSmrg      break;
12901e04c3fSmrg
13001e04c3fSmrg   case nir_var_uniform:
1317e102996Smaya   case nir_var_mem_ubo:
1327e102996Smaya   case nir_var_mem_ssbo:
13301e04c3fSmrg      exec_list_push_tail(&shader->uniforms, &var->node);
13401e04c3fSmrg      break;
13501e04c3fSmrg
1367e102996Smaya   case nir_var_mem_shared:
1377e102996Smaya      assert(gl_shader_stage_is_compute(shader->info.stage));
13801e04c3fSmrg      exec_list_push_tail(&shader->shared, &var->node);
13901e04c3fSmrg      break;
14001e04c3fSmrg
1417e102996Smaya   case nir_var_mem_global:
1427e102996Smaya      assert(!"nir_shader_add_variable cannot be used for global memory");
1437e102996Smaya      break;
1447e102996Smaya
14501e04c3fSmrg   case nir_var_system_value:
14601e04c3fSmrg      exec_list_push_tail(&shader->system_values, &var->node);
14701e04c3fSmrg      break;
14801e04c3fSmrg   }
14901e04c3fSmrg}
15001e04c3fSmrg
15101e04c3fSmrgnir_variable *
15201e04c3fSmrgnir_variable_create(nir_shader *shader, nir_variable_mode mode,
15301e04c3fSmrg                    const struct glsl_type *type, const char *name)
15401e04c3fSmrg{
15501e04c3fSmrg   nir_variable *var = rzalloc(shader, nir_variable);
15601e04c3fSmrg   var->name = ralloc_strdup(var, name);
15701e04c3fSmrg   var->type = type;
15801e04c3fSmrg   var->data.mode = mode;
15901e04c3fSmrg   var->data.how_declared = nir_var_declared_normally;
16001e04c3fSmrg
16101e04c3fSmrg   if ((mode == nir_var_shader_in &&
16201e04c3fSmrg        shader->info.stage != MESA_SHADER_VERTEX) ||
16301e04c3fSmrg       (mode == nir_var_shader_out &&
16401e04c3fSmrg        shader->info.stage != MESA_SHADER_FRAGMENT))
16501e04c3fSmrg      var->data.interpolation = INTERP_MODE_SMOOTH;
16601e04c3fSmrg
16701e04c3fSmrg   if (mode == nir_var_shader_in || mode == nir_var_uniform)
16801e04c3fSmrg      var->data.read_only = true;
16901e04c3fSmrg
17001e04c3fSmrg   nir_shader_add_variable(shader, var);
17101e04c3fSmrg
17201e04c3fSmrg   return var;
17301e04c3fSmrg}
17401e04c3fSmrg
17501e04c3fSmrgnir_variable *
17601e04c3fSmrgnir_local_variable_create(nir_function_impl *impl,
17701e04c3fSmrg                          const struct glsl_type *type, const char *name)
17801e04c3fSmrg{
17901e04c3fSmrg   nir_variable *var = rzalloc(impl->function->shader, nir_variable);
18001e04c3fSmrg   var->name = ralloc_strdup(var, name);
18101e04c3fSmrg   var->type = type;
1827e102996Smaya   var->data.mode = nir_var_function_temp;
18301e04c3fSmrg
18401e04c3fSmrg   nir_function_impl_add_variable(impl, var);
18501e04c3fSmrg
18601e04c3fSmrg   return var;
18701e04c3fSmrg}
18801e04c3fSmrg
18901e04c3fSmrgnir_function *
19001e04c3fSmrgnir_function_create(nir_shader *shader, const char *name)
19101e04c3fSmrg{
19201e04c3fSmrg   nir_function *func = ralloc(shader, nir_function);
19301e04c3fSmrg
19401e04c3fSmrg   exec_list_push_tail(&shader->functions, &func->node);
19501e04c3fSmrg
19601e04c3fSmrg   func->name = ralloc_strdup(func, name);
19701e04c3fSmrg   func->shader = shader;
19801e04c3fSmrg   func->num_params = 0;
19901e04c3fSmrg   func->params = NULL;
20001e04c3fSmrg   func->impl = NULL;
2017e102996Smaya   func->is_entrypoint = false;
20201e04c3fSmrg
20301e04c3fSmrg   return func;
20401e04c3fSmrg}
20501e04c3fSmrg
20601e04c3fSmrg/* NOTE: if the instruction you are copying a src to is already added
20701e04c3fSmrg * to the IR, use nir_instr_rewrite_src() instead.
20801e04c3fSmrg */
20901e04c3fSmrgvoid nir_src_copy(nir_src *dest, const nir_src *src, void *mem_ctx)
21001e04c3fSmrg{
21101e04c3fSmrg   dest->is_ssa = src->is_ssa;
21201e04c3fSmrg   if (src->is_ssa) {
21301e04c3fSmrg      dest->ssa = src->ssa;
21401e04c3fSmrg   } else {
21501e04c3fSmrg      dest->reg.base_offset = src->reg.base_offset;
21601e04c3fSmrg      dest->reg.reg = src->reg.reg;
21701e04c3fSmrg      if (src->reg.indirect) {
21801e04c3fSmrg         dest->reg.indirect = ralloc(mem_ctx, nir_src);
21901e04c3fSmrg         nir_src_copy(dest->reg.indirect, src->reg.indirect, mem_ctx);
22001e04c3fSmrg      } else {
22101e04c3fSmrg         dest->reg.indirect = NULL;
22201e04c3fSmrg      }
22301e04c3fSmrg   }
22401e04c3fSmrg}
22501e04c3fSmrg
22601e04c3fSmrgvoid nir_dest_copy(nir_dest *dest, const nir_dest *src, nir_instr *instr)
22701e04c3fSmrg{
22801e04c3fSmrg   /* Copying an SSA definition makes no sense whatsoever. */
22901e04c3fSmrg   assert(!src->is_ssa);
23001e04c3fSmrg
23101e04c3fSmrg   dest->is_ssa = false;
23201e04c3fSmrg
23301e04c3fSmrg   dest->reg.base_offset = src->reg.base_offset;
23401e04c3fSmrg   dest->reg.reg = src->reg.reg;
23501e04c3fSmrg   if (src->reg.indirect) {
23601e04c3fSmrg      dest->reg.indirect = ralloc(instr, nir_src);
23701e04c3fSmrg      nir_src_copy(dest->reg.indirect, src->reg.indirect, instr);
23801e04c3fSmrg   } else {
23901e04c3fSmrg      dest->reg.indirect = NULL;
24001e04c3fSmrg   }
24101e04c3fSmrg}
24201e04c3fSmrg
24301e04c3fSmrgvoid
24401e04c3fSmrgnir_alu_src_copy(nir_alu_src *dest, const nir_alu_src *src,
24501e04c3fSmrg                 nir_alu_instr *instr)
24601e04c3fSmrg{
24701e04c3fSmrg   nir_src_copy(&dest->src, &src->src, &instr->instr);
24801e04c3fSmrg   dest->abs = src->abs;
24901e04c3fSmrg   dest->negate = src->negate;
25001e04c3fSmrg   for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++)
25101e04c3fSmrg      dest->swizzle[i] = src->swizzle[i];
25201e04c3fSmrg}
25301e04c3fSmrg
25401e04c3fSmrgvoid
25501e04c3fSmrgnir_alu_dest_copy(nir_alu_dest *dest, const nir_alu_dest *src,
25601e04c3fSmrg                  nir_alu_instr *instr)
25701e04c3fSmrg{
25801e04c3fSmrg   nir_dest_copy(&dest->dest, &src->dest, &instr->instr);
25901e04c3fSmrg   dest->write_mask = src->write_mask;
26001e04c3fSmrg   dest->saturate = src->saturate;
26101e04c3fSmrg}
26201e04c3fSmrg
26301e04c3fSmrg
26401e04c3fSmrgstatic void
26501e04c3fSmrgcf_init(nir_cf_node *node, nir_cf_node_type type)
26601e04c3fSmrg{
26701e04c3fSmrg   exec_node_init(&node->node);
26801e04c3fSmrg   node->parent = NULL;
26901e04c3fSmrg   node->type = type;
27001e04c3fSmrg}
27101e04c3fSmrg
27201e04c3fSmrgnir_function_impl *
27301e04c3fSmrgnir_function_impl_create_bare(nir_shader *shader)
27401e04c3fSmrg{
27501e04c3fSmrg   nir_function_impl *impl = ralloc(shader, nir_function_impl);
27601e04c3fSmrg
27701e04c3fSmrg   impl->function = NULL;
27801e04c3fSmrg
27901e04c3fSmrg   cf_init(&impl->cf_node, nir_cf_node_function);
28001e04c3fSmrg
28101e04c3fSmrg   exec_list_make_empty(&impl->body);
28201e04c3fSmrg   exec_list_make_empty(&impl->registers);
28301e04c3fSmrg   exec_list_make_empty(&impl->locals);
28401e04c3fSmrg   impl->reg_alloc = 0;
28501e04c3fSmrg   impl->ssa_alloc = 0;
28601e04c3fSmrg   impl->valid_metadata = nir_metadata_none;
28701e04c3fSmrg
28801e04c3fSmrg   /* create start & end blocks */
28901e04c3fSmrg   nir_block *start_block = nir_block_create(shader);
29001e04c3fSmrg   nir_block *end_block = nir_block_create(shader);
29101e04c3fSmrg   start_block->cf_node.parent = &impl->cf_node;
29201e04c3fSmrg   end_block->cf_node.parent = &impl->cf_node;
29301e04c3fSmrg   impl->end_block = end_block;
29401e04c3fSmrg
29501e04c3fSmrg   exec_list_push_tail(&impl->body, &start_block->cf_node.node);
29601e04c3fSmrg
29701e04c3fSmrg   start_block->successors[0] = end_block;
29801e04c3fSmrg   _mesa_set_add(end_block->predecessors, start_block);
29901e04c3fSmrg   return impl;
30001e04c3fSmrg}
30101e04c3fSmrg
30201e04c3fSmrgnir_function_impl *
30301e04c3fSmrgnir_function_impl_create(nir_function *function)
30401e04c3fSmrg{
30501e04c3fSmrg   assert(function->impl == NULL);
30601e04c3fSmrg
30701e04c3fSmrg   nir_function_impl *impl = nir_function_impl_create_bare(function->shader);
30801e04c3fSmrg
30901e04c3fSmrg   function->impl = impl;
31001e04c3fSmrg   impl->function = function;
31101e04c3fSmrg
31201e04c3fSmrg   return impl;
31301e04c3fSmrg}
31401e04c3fSmrg
31501e04c3fSmrgnir_block *
31601e04c3fSmrgnir_block_create(nir_shader *shader)
31701e04c3fSmrg{
31801e04c3fSmrg   nir_block *block = rzalloc(shader, nir_block);
31901e04c3fSmrg
32001e04c3fSmrg   cf_init(&block->cf_node, nir_cf_node_block);
32101e04c3fSmrg
32201e04c3fSmrg   block->successors[0] = block->successors[1] = NULL;
3237e102996Smaya   block->predecessors = _mesa_pointer_set_create(block);
32401e04c3fSmrg   block->imm_dom = NULL;
32501e04c3fSmrg   /* XXX maybe it would be worth it to defer allocation?  This
32601e04c3fSmrg    * way it doesn't get allocated for shader refs that never run
32701e04c3fSmrg    * nir_calc_dominance?  For example, state-tracker creates an
32801e04c3fSmrg    * initial IR, clones that, runs appropriate lowering pass, passes
32901e04c3fSmrg    * to driver which does common lowering/opt, and then stores ref
33001e04c3fSmrg    * which is later used to do state specific lowering and futher
33101e04c3fSmrg    * opt.  Do any of the references not need dominance metadata?
33201e04c3fSmrg    */
3337e102996Smaya   block->dom_frontier = _mesa_pointer_set_create(block);
33401e04c3fSmrg
33501e04c3fSmrg   exec_list_make_empty(&block->instr_list);
33601e04c3fSmrg
33701e04c3fSmrg   return block;
33801e04c3fSmrg}
33901e04c3fSmrg
34001e04c3fSmrgstatic inline void
34101e04c3fSmrgsrc_init(nir_src *src)
34201e04c3fSmrg{
34301e04c3fSmrg   src->is_ssa = false;
34401e04c3fSmrg   src->reg.reg = NULL;
34501e04c3fSmrg   src->reg.indirect = NULL;
34601e04c3fSmrg   src->reg.base_offset = 0;
34701e04c3fSmrg}
34801e04c3fSmrg
34901e04c3fSmrgnir_if *
35001e04c3fSmrgnir_if_create(nir_shader *shader)
35101e04c3fSmrg{
35201e04c3fSmrg   nir_if *if_stmt = ralloc(shader, nir_if);
35301e04c3fSmrg
3547e102996Smaya   if_stmt->control = nir_selection_control_none;
3557e102996Smaya
35601e04c3fSmrg   cf_init(&if_stmt->cf_node, nir_cf_node_if);
35701e04c3fSmrg   src_init(&if_stmt->condition);
35801e04c3fSmrg
35901e04c3fSmrg   nir_block *then = nir_block_create(shader);
36001e04c3fSmrg   exec_list_make_empty(&if_stmt->then_list);
36101e04c3fSmrg   exec_list_push_tail(&if_stmt->then_list, &then->cf_node.node);
36201e04c3fSmrg   then->cf_node.parent = &if_stmt->cf_node;
36301e04c3fSmrg
36401e04c3fSmrg   nir_block *else_stmt = nir_block_create(shader);
36501e04c3fSmrg   exec_list_make_empty(&if_stmt->else_list);
36601e04c3fSmrg   exec_list_push_tail(&if_stmt->else_list, &else_stmt->cf_node.node);
36701e04c3fSmrg   else_stmt->cf_node.parent = &if_stmt->cf_node;
36801e04c3fSmrg
36901e04c3fSmrg   return if_stmt;
37001e04c3fSmrg}
37101e04c3fSmrg
37201e04c3fSmrgnir_loop *
37301e04c3fSmrgnir_loop_create(nir_shader *shader)
37401e04c3fSmrg{
37501e04c3fSmrg   nir_loop *loop = rzalloc(shader, nir_loop);
37601e04c3fSmrg
37701e04c3fSmrg   cf_init(&loop->cf_node, nir_cf_node_loop);
37801e04c3fSmrg
37901e04c3fSmrg   nir_block *body = nir_block_create(shader);
38001e04c3fSmrg   exec_list_make_empty(&loop->body);
38101e04c3fSmrg   exec_list_push_tail(&loop->body, &body->cf_node.node);
38201e04c3fSmrg   body->cf_node.parent = &loop->cf_node;
38301e04c3fSmrg
38401e04c3fSmrg   body->successors[0] = body;
38501e04c3fSmrg   _mesa_set_add(body->predecessors, body);
38601e04c3fSmrg
38701e04c3fSmrg   return loop;
38801e04c3fSmrg}
38901e04c3fSmrg
39001e04c3fSmrgstatic void
39101e04c3fSmrginstr_init(nir_instr *instr, nir_instr_type type)
39201e04c3fSmrg{
39301e04c3fSmrg   instr->type = type;
39401e04c3fSmrg   instr->block = NULL;
39501e04c3fSmrg   exec_node_init(&instr->node);
39601e04c3fSmrg}
39701e04c3fSmrg
39801e04c3fSmrgstatic void
39901e04c3fSmrgdest_init(nir_dest *dest)
40001e04c3fSmrg{
40101e04c3fSmrg   dest->is_ssa = false;
40201e04c3fSmrg   dest->reg.reg = NULL;
40301e04c3fSmrg   dest->reg.indirect = NULL;
40401e04c3fSmrg   dest->reg.base_offset = 0;
40501e04c3fSmrg}
40601e04c3fSmrg
40701e04c3fSmrgstatic void
40801e04c3fSmrgalu_dest_init(nir_alu_dest *dest)
40901e04c3fSmrg{
41001e04c3fSmrg   dest_init(&dest->dest);
41101e04c3fSmrg   dest->saturate = false;
41201e04c3fSmrg   dest->write_mask = 0xf;
41301e04c3fSmrg}
41401e04c3fSmrg
41501e04c3fSmrgstatic void
41601e04c3fSmrgalu_src_init(nir_alu_src *src)
41701e04c3fSmrg{
41801e04c3fSmrg   src_init(&src->src);
41901e04c3fSmrg   src->abs = src->negate = false;
42001e04c3fSmrg   for (int i = 0; i < NIR_MAX_VEC_COMPONENTS; ++i)
42101e04c3fSmrg      src->swizzle[i] = i;
42201e04c3fSmrg}
42301e04c3fSmrg
42401e04c3fSmrgnir_alu_instr *
42501e04c3fSmrgnir_alu_instr_create(nir_shader *shader, nir_op op)
42601e04c3fSmrg{
42701e04c3fSmrg   unsigned num_srcs = nir_op_infos[op].num_inputs;
42801e04c3fSmrg   /* TODO: don't use rzalloc */
42901e04c3fSmrg   nir_alu_instr *instr =
43001e04c3fSmrg      rzalloc_size(shader,
43101e04c3fSmrg                   sizeof(nir_alu_instr) + num_srcs * sizeof(nir_alu_src));
43201e04c3fSmrg
43301e04c3fSmrg   instr_init(&instr->instr, nir_instr_type_alu);
43401e04c3fSmrg   instr->op = op;
43501e04c3fSmrg   alu_dest_init(&instr->dest);
43601e04c3fSmrg   for (unsigned i = 0; i < num_srcs; i++)
43701e04c3fSmrg      alu_src_init(&instr->src[i]);
43801e04c3fSmrg
43901e04c3fSmrg   return instr;
44001e04c3fSmrg}
44101e04c3fSmrg
44201e04c3fSmrgnir_deref_instr *
44301e04c3fSmrgnir_deref_instr_create(nir_shader *shader, nir_deref_type deref_type)
44401e04c3fSmrg{
44501e04c3fSmrg   nir_deref_instr *instr =
44601e04c3fSmrg      rzalloc_size(shader, sizeof(nir_deref_instr));
44701e04c3fSmrg
44801e04c3fSmrg   instr_init(&instr->instr, nir_instr_type_deref);
44901e04c3fSmrg
45001e04c3fSmrg   instr->deref_type = deref_type;
45101e04c3fSmrg   if (deref_type != nir_deref_type_var)
45201e04c3fSmrg      src_init(&instr->parent);
45301e04c3fSmrg
4547e102996Smaya   if (deref_type == nir_deref_type_array ||
4557e102996Smaya       deref_type == nir_deref_type_ptr_as_array)
45601e04c3fSmrg      src_init(&instr->arr.index);
45701e04c3fSmrg
45801e04c3fSmrg   dest_init(&instr->dest);
45901e04c3fSmrg
46001e04c3fSmrg   return instr;
46101e04c3fSmrg}
46201e04c3fSmrg
46301e04c3fSmrgnir_jump_instr *
46401e04c3fSmrgnir_jump_instr_create(nir_shader *shader, nir_jump_type type)
46501e04c3fSmrg{
46601e04c3fSmrg   nir_jump_instr *instr = ralloc(shader, nir_jump_instr);
46701e04c3fSmrg   instr_init(&instr->instr, nir_instr_type_jump);
46801e04c3fSmrg   instr->type = type;
46901e04c3fSmrg   return instr;
47001e04c3fSmrg}
47101e04c3fSmrg
47201e04c3fSmrgnir_load_const_instr *
47301e04c3fSmrgnir_load_const_instr_create(nir_shader *shader, unsigned num_components,
47401e04c3fSmrg                            unsigned bit_size)
47501e04c3fSmrg{
4767e102996Smaya   nir_load_const_instr *instr =
4777e102996Smaya      rzalloc_size(shader, sizeof(*instr) + num_components * sizeof(*instr->value));
47801e04c3fSmrg   instr_init(&instr->instr, nir_instr_type_load_const);
47901e04c3fSmrg
48001e04c3fSmrg   nir_ssa_def_init(&instr->instr, &instr->def, num_components, bit_size, NULL);
48101e04c3fSmrg
48201e04c3fSmrg   return instr;
48301e04c3fSmrg}
48401e04c3fSmrg
48501e04c3fSmrgnir_intrinsic_instr *
48601e04c3fSmrgnir_intrinsic_instr_create(nir_shader *shader, nir_intrinsic_op op)
48701e04c3fSmrg{
48801e04c3fSmrg   unsigned num_srcs = nir_intrinsic_infos[op].num_srcs;
48901e04c3fSmrg   /* TODO: don't use rzalloc */
49001e04c3fSmrg   nir_intrinsic_instr *instr =
49101e04c3fSmrg      rzalloc_size(shader,
49201e04c3fSmrg                  sizeof(nir_intrinsic_instr) + num_srcs * sizeof(nir_src));
49301e04c3fSmrg
49401e04c3fSmrg   instr_init(&instr->instr, nir_instr_type_intrinsic);
49501e04c3fSmrg   instr->intrinsic = op;
49601e04c3fSmrg
49701e04c3fSmrg   if (nir_intrinsic_infos[op].has_dest)
49801e04c3fSmrg      dest_init(&instr->dest);
49901e04c3fSmrg
50001e04c3fSmrg   for (unsigned i = 0; i < num_srcs; i++)
50101e04c3fSmrg      src_init(&instr->src[i]);
50201e04c3fSmrg
50301e04c3fSmrg   return instr;
50401e04c3fSmrg}
50501e04c3fSmrg
50601e04c3fSmrgnir_call_instr *
50701e04c3fSmrgnir_call_instr_create(nir_shader *shader, nir_function *callee)
50801e04c3fSmrg{
50901e04c3fSmrg   const unsigned num_params = callee->num_params;
51001e04c3fSmrg   nir_call_instr *instr =
51101e04c3fSmrg      rzalloc_size(shader, sizeof(*instr) +
51201e04c3fSmrg                   num_params * sizeof(instr->params[0]));
51301e04c3fSmrg
51401e04c3fSmrg   instr_init(&instr->instr, nir_instr_type_call);
51501e04c3fSmrg   instr->callee = callee;
51601e04c3fSmrg   instr->num_params = num_params;
51701e04c3fSmrg   for (unsigned i = 0; i < num_params; i++)
51801e04c3fSmrg      src_init(&instr->params[i]);
51901e04c3fSmrg
52001e04c3fSmrg   return instr;
52101e04c3fSmrg}
52201e04c3fSmrg
5237e102996Smayastatic int8_t default_tg4_offsets[4][2] =
5247e102996Smaya{
5257e102996Smaya   { 0, 1 },
5267e102996Smaya   { 1, 1 },
5277e102996Smaya   { 1, 0 },
5287e102996Smaya   { 0, 0 },
5297e102996Smaya};
5307e102996Smaya
53101e04c3fSmrgnir_tex_instr *
53201e04c3fSmrgnir_tex_instr_create(nir_shader *shader, unsigned num_srcs)
53301e04c3fSmrg{
53401e04c3fSmrg   nir_tex_instr *instr = rzalloc(shader, nir_tex_instr);
53501e04c3fSmrg   instr_init(&instr->instr, nir_instr_type_tex);
53601e04c3fSmrg
53701e04c3fSmrg   dest_init(&instr->dest);
53801e04c3fSmrg
53901e04c3fSmrg   instr->num_srcs = num_srcs;
54001e04c3fSmrg   instr->src = ralloc_array(instr, nir_tex_src, num_srcs);
54101e04c3fSmrg   for (unsigned i = 0; i < num_srcs; i++)
54201e04c3fSmrg      src_init(&instr->src[i].src);
54301e04c3fSmrg
54401e04c3fSmrg   instr->texture_index = 0;
54501e04c3fSmrg   instr->texture_array_size = 0;
54601e04c3fSmrg   instr->sampler_index = 0;
5477e102996Smaya   memcpy(instr->tg4_offsets, default_tg4_offsets, sizeof(instr->tg4_offsets));
54801e04c3fSmrg
54901e04c3fSmrg   return instr;
55001e04c3fSmrg}
55101e04c3fSmrg
55201e04c3fSmrgvoid
55301e04c3fSmrgnir_tex_instr_add_src(nir_tex_instr *tex,
55401e04c3fSmrg                      nir_tex_src_type src_type,
55501e04c3fSmrg                      nir_src src)
55601e04c3fSmrg{
55701e04c3fSmrg   nir_tex_src *new_srcs = rzalloc_array(tex, nir_tex_src,
55801e04c3fSmrg                                         tex->num_srcs + 1);
55901e04c3fSmrg
56001e04c3fSmrg   for (unsigned i = 0; i < tex->num_srcs; i++) {
56101e04c3fSmrg      new_srcs[i].src_type = tex->src[i].src_type;
56201e04c3fSmrg      nir_instr_move_src(&tex->instr, &new_srcs[i].src,
56301e04c3fSmrg                         &tex->src[i].src);
56401e04c3fSmrg   }
56501e04c3fSmrg
56601e04c3fSmrg   ralloc_free(tex->src);
56701e04c3fSmrg   tex->src = new_srcs;
56801e04c3fSmrg
56901e04c3fSmrg   tex->src[tex->num_srcs].src_type = src_type;
57001e04c3fSmrg   nir_instr_rewrite_src(&tex->instr, &tex->src[tex->num_srcs].src, src);
57101e04c3fSmrg   tex->num_srcs++;
57201e04c3fSmrg}
57301e04c3fSmrg
57401e04c3fSmrgvoid
57501e04c3fSmrgnir_tex_instr_remove_src(nir_tex_instr *tex, unsigned src_idx)
57601e04c3fSmrg{
57701e04c3fSmrg   assert(src_idx < tex->num_srcs);
57801e04c3fSmrg
57901e04c3fSmrg   /* First rewrite the source to NIR_SRC_INIT */
58001e04c3fSmrg   nir_instr_rewrite_src(&tex->instr, &tex->src[src_idx].src, NIR_SRC_INIT);
58101e04c3fSmrg
58201e04c3fSmrg   /* Now, move all of the other sources down */
58301e04c3fSmrg   for (unsigned i = src_idx + 1; i < tex->num_srcs; i++) {
58401e04c3fSmrg      tex->src[i-1].src_type = tex->src[i].src_type;
58501e04c3fSmrg      nir_instr_move_src(&tex->instr, &tex->src[i-1].src, &tex->src[i].src);
58601e04c3fSmrg   }
58701e04c3fSmrg   tex->num_srcs--;
58801e04c3fSmrg}
58901e04c3fSmrg
5907e102996Smayabool
5917e102996Smayanir_tex_instr_has_explicit_tg4_offsets(nir_tex_instr *tex)
5927e102996Smaya{
5937e102996Smaya   if (tex->op != nir_texop_tg4)
5947e102996Smaya      return false;
5957e102996Smaya   return memcmp(tex->tg4_offsets, default_tg4_offsets,
5967e102996Smaya                 sizeof(tex->tg4_offsets)) != 0;
5977e102996Smaya}
5987e102996Smaya
59901e04c3fSmrgnir_phi_instr *
60001e04c3fSmrgnir_phi_instr_create(nir_shader *shader)
60101e04c3fSmrg{
60201e04c3fSmrg   nir_phi_instr *instr = ralloc(shader, nir_phi_instr);
60301e04c3fSmrg   instr_init(&instr->instr, nir_instr_type_phi);
60401e04c3fSmrg
60501e04c3fSmrg   dest_init(&instr->dest);
60601e04c3fSmrg   exec_list_make_empty(&instr->srcs);
60701e04c3fSmrg   return instr;
60801e04c3fSmrg}
60901e04c3fSmrg
61001e04c3fSmrgnir_parallel_copy_instr *
61101e04c3fSmrgnir_parallel_copy_instr_create(nir_shader *shader)
61201e04c3fSmrg{
61301e04c3fSmrg   nir_parallel_copy_instr *instr = ralloc(shader, nir_parallel_copy_instr);
61401e04c3fSmrg   instr_init(&instr->instr, nir_instr_type_parallel_copy);
61501e04c3fSmrg
61601e04c3fSmrg   exec_list_make_empty(&instr->entries);
61701e04c3fSmrg
61801e04c3fSmrg   return instr;
61901e04c3fSmrg}
62001e04c3fSmrg
62101e04c3fSmrgnir_ssa_undef_instr *
62201e04c3fSmrgnir_ssa_undef_instr_create(nir_shader *shader,
62301e04c3fSmrg                           unsigned num_components,
62401e04c3fSmrg                           unsigned bit_size)
62501e04c3fSmrg{
62601e04c3fSmrg   nir_ssa_undef_instr *instr = ralloc(shader, nir_ssa_undef_instr);
62701e04c3fSmrg   instr_init(&instr->instr, nir_instr_type_ssa_undef);
62801e04c3fSmrg
62901e04c3fSmrg   nir_ssa_def_init(&instr->instr, &instr->def, num_components, bit_size, NULL);
63001e04c3fSmrg
63101e04c3fSmrg   return instr;
63201e04c3fSmrg}
63301e04c3fSmrg
63401e04c3fSmrgstatic nir_const_value
63501e04c3fSmrgconst_value_float(double d, unsigned bit_size)
63601e04c3fSmrg{
63701e04c3fSmrg   nir_const_value v;
6387e102996Smaya   memset(&v, 0, sizeof(v));
63901e04c3fSmrg   switch (bit_size) {
6407e102996Smaya   case 16: v.u16 = _mesa_float_to_half(d);  break;
6417e102996Smaya   case 32: v.f32 = d;                       break;
6427e102996Smaya   case 64: v.f64 = d;                       break;
64301e04c3fSmrg   default:
64401e04c3fSmrg      unreachable("Invalid bit size");
64501e04c3fSmrg   }
64601e04c3fSmrg   return v;
64701e04c3fSmrg}
64801e04c3fSmrg
64901e04c3fSmrgstatic nir_const_value
65001e04c3fSmrgconst_value_int(int64_t i, unsigned bit_size)
65101e04c3fSmrg{
65201e04c3fSmrg   nir_const_value v;
6537e102996Smaya   memset(&v, 0, sizeof(v));
65401e04c3fSmrg   switch (bit_size) {
6557e102996Smaya   case 1:  v.b   = i & 1;  break;
6567e102996Smaya   case 8:  v.i8  = i;  break;
6577e102996Smaya   case 16: v.i16 = i;  break;
6587e102996Smaya   case 32: v.i32 = i;  break;
6597e102996Smaya   case 64: v.i64 = i;  break;
66001e04c3fSmrg   default:
66101e04c3fSmrg      unreachable("Invalid bit size");
66201e04c3fSmrg   }
66301e04c3fSmrg   return v;
66401e04c3fSmrg}
66501e04c3fSmrg
66601e04c3fSmrgnir_const_value
66701e04c3fSmrgnir_alu_binop_identity(nir_op binop, unsigned bit_size)
66801e04c3fSmrg{
66901e04c3fSmrg   const int64_t max_int = (1ull << (bit_size - 1)) - 1;
67001e04c3fSmrg   const int64_t min_int = -max_int - 1;
67101e04c3fSmrg   switch (binop) {
67201e04c3fSmrg   case nir_op_iadd:
67301e04c3fSmrg      return const_value_int(0, bit_size);
67401e04c3fSmrg   case nir_op_fadd:
67501e04c3fSmrg      return const_value_float(0, bit_size);
67601e04c3fSmrg   case nir_op_imul:
67701e04c3fSmrg      return const_value_int(1, bit_size);
67801e04c3fSmrg   case nir_op_fmul:
67901e04c3fSmrg      return const_value_float(1, bit_size);
68001e04c3fSmrg   case nir_op_imin:
68101e04c3fSmrg      return const_value_int(max_int, bit_size);
68201e04c3fSmrg   case nir_op_umin:
68301e04c3fSmrg      return const_value_int(~0ull, bit_size);
68401e04c3fSmrg   case nir_op_fmin:
68501e04c3fSmrg      return const_value_float(INFINITY, bit_size);
68601e04c3fSmrg   case nir_op_imax:
68701e04c3fSmrg      return const_value_int(min_int, bit_size);
68801e04c3fSmrg   case nir_op_umax:
68901e04c3fSmrg      return const_value_int(0, bit_size);
69001e04c3fSmrg   case nir_op_fmax:
69101e04c3fSmrg      return const_value_float(-INFINITY, bit_size);
69201e04c3fSmrg   case nir_op_iand:
69301e04c3fSmrg      return const_value_int(~0ull, bit_size);
69401e04c3fSmrg   case nir_op_ior:
69501e04c3fSmrg      return const_value_int(0, bit_size);
69601e04c3fSmrg   case nir_op_ixor:
69701e04c3fSmrg      return const_value_int(0, bit_size);
69801e04c3fSmrg   default:
69901e04c3fSmrg      unreachable("Invalid reduction operation");
70001e04c3fSmrg   }
70101e04c3fSmrg}
70201e04c3fSmrg
70301e04c3fSmrgnir_function_impl *
70401e04c3fSmrgnir_cf_node_get_function(nir_cf_node *node)
70501e04c3fSmrg{
70601e04c3fSmrg   while (node->type != nir_cf_node_function) {
70701e04c3fSmrg      node = node->parent;
70801e04c3fSmrg   }
70901e04c3fSmrg
71001e04c3fSmrg   return nir_cf_node_as_function(node);
71101e04c3fSmrg}
71201e04c3fSmrg
71301e04c3fSmrg/* Reduces a cursor by trying to convert everything to after and trying to
71401e04c3fSmrg * go up to block granularity when possible.
71501e04c3fSmrg */
71601e04c3fSmrgstatic nir_cursor
71701e04c3fSmrgreduce_cursor(nir_cursor cursor)
71801e04c3fSmrg{
71901e04c3fSmrg   switch (cursor.option) {
72001e04c3fSmrg   case nir_cursor_before_block:
72101e04c3fSmrg      assert(nir_cf_node_prev(&cursor.block->cf_node) == NULL ||
72201e04c3fSmrg             nir_cf_node_prev(&cursor.block->cf_node)->type != nir_cf_node_block);
72301e04c3fSmrg      if (exec_list_is_empty(&cursor.block->instr_list)) {
72401e04c3fSmrg         /* Empty block.  After is as good as before. */
72501e04c3fSmrg         cursor.option = nir_cursor_after_block;
72601e04c3fSmrg      }
72701e04c3fSmrg      return cursor;
72801e04c3fSmrg
72901e04c3fSmrg   case nir_cursor_after_block:
73001e04c3fSmrg      return cursor;
73101e04c3fSmrg
73201e04c3fSmrg   case nir_cursor_before_instr: {
73301e04c3fSmrg      nir_instr *prev_instr = nir_instr_prev(cursor.instr);
73401e04c3fSmrg      if (prev_instr) {
73501e04c3fSmrg         /* Before this instruction is after the previous */
73601e04c3fSmrg         cursor.instr = prev_instr;
73701e04c3fSmrg         cursor.option = nir_cursor_after_instr;
73801e04c3fSmrg      } else {
73901e04c3fSmrg         /* No previous instruction.  Switch to before block */
74001e04c3fSmrg         cursor.block = cursor.instr->block;
74101e04c3fSmrg         cursor.option = nir_cursor_before_block;
74201e04c3fSmrg      }
74301e04c3fSmrg      return reduce_cursor(cursor);
74401e04c3fSmrg   }
74501e04c3fSmrg
74601e04c3fSmrg   case nir_cursor_after_instr:
74701e04c3fSmrg      if (nir_instr_next(cursor.instr) == NULL) {
74801e04c3fSmrg         /* This is the last instruction, switch to after block */
74901e04c3fSmrg         cursor.option = nir_cursor_after_block;
75001e04c3fSmrg         cursor.block = cursor.instr->block;
75101e04c3fSmrg      }
75201e04c3fSmrg      return cursor;
75301e04c3fSmrg
75401e04c3fSmrg   default:
75501e04c3fSmrg      unreachable("Inavlid cursor option");
75601e04c3fSmrg   }
75701e04c3fSmrg}
75801e04c3fSmrg
75901e04c3fSmrgbool
76001e04c3fSmrgnir_cursors_equal(nir_cursor a, nir_cursor b)
76101e04c3fSmrg{
76201e04c3fSmrg   /* Reduced cursors should be unique */
76301e04c3fSmrg   a = reduce_cursor(a);
76401e04c3fSmrg   b = reduce_cursor(b);
76501e04c3fSmrg
76601e04c3fSmrg   return a.block == b.block && a.option == b.option;
76701e04c3fSmrg}
76801e04c3fSmrg
76901e04c3fSmrgstatic bool
77001e04c3fSmrgadd_use_cb(nir_src *src, void *state)
77101e04c3fSmrg{
77201e04c3fSmrg   nir_instr *instr = state;
77301e04c3fSmrg
77401e04c3fSmrg   src->parent_instr = instr;
77501e04c3fSmrg   list_addtail(&src->use_link,
77601e04c3fSmrg                src->is_ssa ? &src->ssa->uses : &src->reg.reg->uses);
77701e04c3fSmrg
77801e04c3fSmrg   return true;
77901e04c3fSmrg}
78001e04c3fSmrg
78101e04c3fSmrgstatic bool
78201e04c3fSmrgadd_ssa_def_cb(nir_ssa_def *def, void *state)
78301e04c3fSmrg{
78401e04c3fSmrg   nir_instr *instr = state;
78501e04c3fSmrg
78601e04c3fSmrg   if (instr->block && def->index == UINT_MAX) {
78701e04c3fSmrg      nir_function_impl *impl =
78801e04c3fSmrg         nir_cf_node_get_function(&instr->block->cf_node);
78901e04c3fSmrg
79001e04c3fSmrg      def->index = impl->ssa_alloc++;
79101e04c3fSmrg   }
79201e04c3fSmrg
79301e04c3fSmrg   return true;
79401e04c3fSmrg}
79501e04c3fSmrg
79601e04c3fSmrgstatic bool
79701e04c3fSmrgadd_reg_def_cb(nir_dest *dest, void *state)
79801e04c3fSmrg{
79901e04c3fSmrg   nir_instr *instr = state;
80001e04c3fSmrg
80101e04c3fSmrg   if (!dest->is_ssa) {
80201e04c3fSmrg      dest->reg.parent_instr = instr;
80301e04c3fSmrg      list_addtail(&dest->reg.def_link, &dest->reg.reg->defs);
80401e04c3fSmrg   }
80501e04c3fSmrg
80601e04c3fSmrg   return true;
80701e04c3fSmrg}
80801e04c3fSmrg
80901e04c3fSmrgstatic void
81001e04c3fSmrgadd_defs_uses(nir_instr *instr)
81101e04c3fSmrg{
81201e04c3fSmrg   nir_foreach_src(instr, add_use_cb, instr);
81301e04c3fSmrg   nir_foreach_dest(instr, add_reg_def_cb, instr);
81401e04c3fSmrg   nir_foreach_ssa_def(instr, add_ssa_def_cb, instr);
81501e04c3fSmrg}
81601e04c3fSmrg
81701e04c3fSmrgvoid
81801e04c3fSmrgnir_instr_insert(nir_cursor cursor, nir_instr *instr)
81901e04c3fSmrg{
82001e04c3fSmrg   switch (cursor.option) {
82101e04c3fSmrg   case nir_cursor_before_block:
82201e04c3fSmrg      /* Only allow inserting jumps into empty blocks. */
82301e04c3fSmrg      if (instr->type == nir_instr_type_jump)
82401e04c3fSmrg         assert(exec_list_is_empty(&cursor.block->instr_list));
82501e04c3fSmrg
82601e04c3fSmrg      instr->block = cursor.block;
82701e04c3fSmrg      add_defs_uses(instr);
82801e04c3fSmrg      exec_list_push_head(&cursor.block->instr_list, &instr->node);
82901e04c3fSmrg      break;
83001e04c3fSmrg   case nir_cursor_after_block: {
83101e04c3fSmrg      /* Inserting instructions after a jump is illegal. */
83201e04c3fSmrg      nir_instr *last = nir_block_last_instr(cursor.block);
83301e04c3fSmrg      assert(last == NULL || last->type != nir_instr_type_jump);
83401e04c3fSmrg      (void) last;
83501e04c3fSmrg
83601e04c3fSmrg      instr->block = cursor.block;
83701e04c3fSmrg      add_defs_uses(instr);
83801e04c3fSmrg      exec_list_push_tail(&cursor.block->instr_list, &instr->node);
83901e04c3fSmrg      break;
84001e04c3fSmrg   }
84101e04c3fSmrg   case nir_cursor_before_instr:
84201e04c3fSmrg      assert(instr->type != nir_instr_type_jump);
84301e04c3fSmrg      instr->block = cursor.instr->block;
84401e04c3fSmrg      add_defs_uses(instr);
84501e04c3fSmrg      exec_node_insert_node_before(&cursor.instr->node, &instr->node);
84601e04c3fSmrg      break;
84701e04c3fSmrg   case nir_cursor_after_instr:
84801e04c3fSmrg      /* Inserting instructions after a jump is illegal. */
84901e04c3fSmrg      assert(cursor.instr->type != nir_instr_type_jump);
85001e04c3fSmrg
85101e04c3fSmrg      /* Only allow inserting jumps at the end of the block. */
85201e04c3fSmrg      if (instr->type == nir_instr_type_jump)
85301e04c3fSmrg         assert(cursor.instr == nir_block_last_instr(cursor.instr->block));
85401e04c3fSmrg
85501e04c3fSmrg      instr->block = cursor.instr->block;
85601e04c3fSmrg      add_defs_uses(instr);
85701e04c3fSmrg      exec_node_insert_after(&cursor.instr->node, &instr->node);
85801e04c3fSmrg      break;
85901e04c3fSmrg   }
86001e04c3fSmrg
86101e04c3fSmrg   if (instr->type == nir_instr_type_jump)
86201e04c3fSmrg      nir_handle_add_jump(instr->block);
86301e04c3fSmrg}
86401e04c3fSmrg
86501e04c3fSmrgstatic bool
86601e04c3fSmrgsrc_is_valid(const nir_src *src)
86701e04c3fSmrg{
86801e04c3fSmrg   return src->is_ssa ? (src->ssa != NULL) : (src->reg.reg != NULL);
86901e04c3fSmrg}
87001e04c3fSmrg
87101e04c3fSmrgstatic bool
87201e04c3fSmrgremove_use_cb(nir_src *src, void *state)
87301e04c3fSmrg{
87401e04c3fSmrg   (void) state;
87501e04c3fSmrg
87601e04c3fSmrg   if (src_is_valid(src))
87701e04c3fSmrg      list_del(&src->use_link);
87801e04c3fSmrg
87901e04c3fSmrg   return true;
88001e04c3fSmrg}
88101e04c3fSmrg
88201e04c3fSmrgstatic bool
88301e04c3fSmrgremove_def_cb(nir_dest *dest, void *state)
88401e04c3fSmrg{
88501e04c3fSmrg   (void) state;
88601e04c3fSmrg
88701e04c3fSmrg   if (!dest->is_ssa)
88801e04c3fSmrg      list_del(&dest->reg.def_link);
88901e04c3fSmrg
89001e04c3fSmrg   return true;
89101e04c3fSmrg}
89201e04c3fSmrg
89301e04c3fSmrgstatic void
89401e04c3fSmrgremove_defs_uses(nir_instr *instr)
89501e04c3fSmrg{
89601e04c3fSmrg   nir_foreach_dest(instr, remove_def_cb, instr);
89701e04c3fSmrg   nir_foreach_src(instr, remove_use_cb, instr);
89801e04c3fSmrg}
89901e04c3fSmrg
90001e04c3fSmrgvoid nir_instr_remove_v(nir_instr *instr)
90101e04c3fSmrg{
90201e04c3fSmrg   remove_defs_uses(instr);
90301e04c3fSmrg   exec_node_remove(&instr->node);
90401e04c3fSmrg
90501e04c3fSmrg   if (instr->type == nir_instr_type_jump) {
90601e04c3fSmrg      nir_jump_instr *jump_instr = nir_instr_as_jump(instr);
90701e04c3fSmrg      nir_handle_remove_jump(instr->block, jump_instr->type);
90801e04c3fSmrg   }
90901e04c3fSmrg}
91001e04c3fSmrg
91101e04c3fSmrg/*@}*/
91201e04c3fSmrg
91301e04c3fSmrgvoid
91401e04c3fSmrgnir_index_local_regs(nir_function_impl *impl)
91501e04c3fSmrg{
91601e04c3fSmrg   unsigned index = 0;
91701e04c3fSmrg   foreach_list_typed(nir_register, reg, node, &impl->registers) {
91801e04c3fSmrg      reg->index = index++;
91901e04c3fSmrg   }
92001e04c3fSmrg   impl->reg_alloc = index;
92101e04c3fSmrg}
92201e04c3fSmrg
92301e04c3fSmrgstatic bool
92401e04c3fSmrgvisit_alu_dest(nir_alu_instr *instr, nir_foreach_dest_cb cb, void *state)
92501e04c3fSmrg{
92601e04c3fSmrg   return cb(&instr->dest.dest, state);
92701e04c3fSmrg}
92801e04c3fSmrg
92901e04c3fSmrgstatic bool
93001e04c3fSmrgvisit_deref_dest(nir_deref_instr *instr, nir_foreach_dest_cb cb, void *state)
93101e04c3fSmrg{
93201e04c3fSmrg   return cb(&instr->dest, state);
93301e04c3fSmrg}
93401e04c3fSmrg
93501e04c3fSmrgstatic bool
93601e04c3fSmrgvisit_intrinsic_dest(nir_intrinsic_instr *instr, nir_foreach_dest_cb cb,
93701e04c3fSmrg                     void *state)
93801e04c3fSmrg{
93901e04c3fSmrg   if (nir_intrinsic_infos[instr->intrinsic].has_dest)
94001e04c3fSmrg      return cb(&instr->dest, state);
94101e04c3fSmrg
94201e04c3fSmrg   return true;
94301e04c3fSmrg}
94401e04c3fSmrg
94501e04c3fSmrgstatic bool
94601e04c3fSmrgvisit_texture_dest(nir_tex_instr *instr, nir_foreach_dest_cb cb,
94701e04c3fSmrg                   void *state)
94801e04c3fSmrg{
94901e04c3fSmrg   return cb(&instr->dest, state);
95001e04c3fSmrg}
95101e04c3fSmrg
95201e04c3fSmrgstatic bool
95301e04c3fSmrgvisit_phi_dest(nir_phi_instr *instr, nir_foreach_dest_cb cb, void *state)
95401e04c3fSmrg{
95501e04c3fSmrg   return cb(&instr->dest, state);
95601e04c3fSmrg}
95701e04c3fSmrg
95801e04c3fSmrgstatic bool
95901e04c3fSmrgvisit_parallel_copy_dest(nir_parallel_copy_instr *instr,
96001e04c3fSmrg                         nir_foreach_dest_cb cb, void *state)
96101e04c3fSmrg{
96201e04c3fSmrg   nir_foreach_parallel_copy_entry(entry, instr) {
96301e04c3fSmrg      if (!cb(&entry->dest, state))
96401e04c3fSmrg         return false;
96501e04c3fSmrg   }
96601e04c3fSmrg
96701e04c3fSmrg   return true;
96801e04c3fSmrg}
96901e04c3fSmrg
97001e04c3fSmrgbool
97101e04c3fSmrgnir_foreach_dest(nir_instr *instr, nir_foreach_dest_cb cb, void *state)
97201e04c3fSmrg{
97301e04c3fSmrg   switch (instr->type) {
97401e04c3fSmrg   case nir_instr_type_alu:
97501e04c3fSmrg      return visit_alu_dest(nir_instr_as_alu(instr), cb, state);
97601e04c3fSmrg   case nir_instr_type_deref:
97701e04c3fSmrg      return visit_deref_dest(nir_instr_as_deref(instr), cb, state);
97801e04c3fSmrg   case nir_instr_type_intrinsic:
97901e04c3fSmrg      return visit_intrinsic_dest(nir_instr_as_intrinsic(instr), cb, state);
98001e04c3fSmrg   case nir_instr_type_tex:
98101e04c3fSmrg      return visit_texture_dest(nir_instr_as_tex(instr), cb, state);
98201e04c3fSmrg   case nir_instr_type_phi:
98301e04c3fSmrg      return visit_phi_dest(nir_instr_as_phi(instr), cb, state);
98401e04c3fSmrg   case nir_instr_type_parallel_copy:
98501e04c3fSmrg      return visit_parallel_copy_dest(nir_instr_as_parallel_copy(instr),
98601e04c3fSmrg                                      cb, state);
98701e04c3fSmrg
98801e04c3fSmrg   case nir_instr_type_load_const:
98901e04c3fSmrg   case nir_instr_type_ssa_undef:
99001e04c3fSmrg   case nir_instr_type_call:
99101e04c3fSmrg   case nir_instr_type_jump:
99201e04c3fSmrg      break;
99301e04c3fSmrg
99401e04c3fSmrg   default:
99501e04c3fSmrg      unreachable("Invalid instruction type");
99601e04c3fSmrg      break;
99701e04c3fSmrg   }
99801e04c3fSmrg
99901e04c3fSmrg   return true;
100001e04c3fSmrg}
100101e04c3fSmrg
100201e04c3fSmrgstruct foreach_ssa_def_state {
100301e04c3fSmrg   nir_foreach_ssa_def_cb cb;
100401e04c3fSmrg   void *client_state;
100501e04c3fSmrg};
100601e04c3fSmrg
100701e04c3fSmrgstatic inline bool
100801e04c3fSmrgnir_ssa_def_visitor(nir_dest *dest, void *void_state)
100901e04c3fSmrg{
101001e04c3fSmrg   struct foreach_ssa_def_state *state = void_state;
101101e04c3fSmrg
101201e04c3fSmrg   if (dest->is_ssa)
101301e04c3fSmrg      return state->cb(&dest->ssa, state->client_state);
101401e04c3fSmrg   else
101501e04c3fSmrg      return true;
101601e04c3fSmrg}
101701e04c3fSmrg
101801e04c3fSmrgbool
101901e04c3fSmrgnir_foreach_ssa_def(nir_instr *instr, nir_foreach_ssa_def_cb cb, void *state)
102001e04c3fSmrg{
102101e04c3fSmrg   switch (instr->type) {
102201e04c3fSmrg   case nir_instr_type_alu:
102301e04c3fSmrg   case nir_instr_type_deref:
102401e04c3fSmrg   case nir_instr_type_tex:
102501e04c3fSmrg   case nir_instr_type_intrinsic:
102601e04c3fSmrg   case nir_instr_type_phi:
102701e04c3fSmrg   case nir_instr_type_parallel_copy: {
102801e04c3fSmrg      struct foreach_ssa_def_state foreach_state = {cb, state};
102901e04c3fSmrg      return nir_foreach_dest(instr, nir_ssa_def_visitor, &foreach_state);
103001e04c3fSmrg   }
103101e04c3fSmrg
103201e04c3fSmrg   case nir_instr_type_load_const:
103301e04c3fSmrg      return cb(&nir_instr_as_load_const(instr)->def, state);
103401e04c3fSmrg   case nir_instr_type_ssa_undef:
103501e04c3fSmrg      return cb(&nir_instr_as_ssa_undef(instr)->def, state);
103601e04c3fSmrg   case nir_instr_type_call:
103701e04c3fSmrg   case nir_instr_type_jump:
103801e04c3fSmrg      return true;
103901e04c3fSmrg   default:
104001e04c3fSmrg      unreachable("Invalid instruction type");
104101e04c3fSmrg   }
104201e04c3fSmrg}
104301e04c3fSmrg
104401e04c3fSmrgstatic bool
104501e04c3fSmrgvisit_src(nir_src *src, nir_foreach_src_cb cb, void *state)
104601e04c3fSmrg{
104701e04c3fSmrg   if (!cb(src, state))
104801e04c3fSmrg      return false;
104901e04c3fSmrg   if (!src->is_ssa && src->reg.indirect)
105001e04c3fSmrg      return cb(src->reg.indirect, state);
105101e04c3fSmrg   return true;
105201e04c3fSmrg}
105301e04c3fSmrg
105401e04c3fSmrgstatic bool
105501e04c3fSmrgvisit_alu_src(nir_alu_instr *instr, nir_foreach_src_cb cb, void *state)
105601e04c3fSmrg{
105701e04c3fSmrg   for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++)
105801e04c3fSmrg      if (!visit_src(&instr->src[i].src, cb, state))
105901e04c3fSmrg         return false;
106001e04c3fSmrg
106101e04c3fSmrg   return true;
106201e04c3fSmrg}
106301e04c3fSmrg
106401e04c3fSmrgstatic bool
106501e04c3fSmrgvisit_deref_instr_src(nir_deref_instr *instr,
106601e04c3fSmrg                      nir_foreach_src_cb cb, void *state)
106701e04c3fSmrg{
106801e04c3fSmrg   if (instr->deref_type != nir_deref_type_var) {
106901e04c3fSmrg      if (!visit_src(&instr->parent, cb, state))
107001e04c3fSmrg         return false;
107101e04c3fSmrg   }
107201e04c3fSmrg
10737e102996Smaya   if (instr->deref_type == nir_deref_type_array ||
10747e102996Smaya       instr->deref_type == nir_deref_type_ptr_as_array) {
107501e04c3fSmrg      if (!visit_src(&instr->arr.index, cb, state))
107601e04c3fSmrg         return false;
107701e04c3fSmrg   }
107801e04c3fSmrg
107901e04c3fSmrg   return true;
108001e04c3fSmrg}
108101e04c3fSmrg
108201e04c3fSmrgstatic bool
108301e04c3fSmrgvisit_tex_src(nir_tex_instr *instr, nir_foreach_src_cb cb, void *state)
108401e04c3fSmrg{
108501e04c3fSmrg   for (unsigned i = 0; i < instr->num_srcs; i++) {
108601e04c3fSmrg      if (!visit_src(&instr->src[i].src, cb, state))
108701e04c3fSmrg         return false;
108801e04c3fSmrg   }
108901e04c3fSmrg
109001e04c3fSmrg   return true;
109101e04c3fSmrg}
109201e04c3fSmrg
109301e04c3fSmrgstatic bool
109401e04c3fSmrgvisit_intrinsic_src(nir_intrinsic_instr *instr, nir_foreach_src_cb cb,
109501e04c3fSmrg                    void *state)
109601e04c3fSmrg{
109701e04c3fSmrg   unsigned num_srcs = nir_intrinsic_infos[instr->intrinsic].num_srcs;
109801e04c3fSmrg   for (unsigned i = 0; i < num_srcs; i++) {
109901e04c3fSmrg      if (!visit_src(&instr->src[i], cb, state))
110001e04c3fSmrg         return false;
110101e04c3fSmrg   }
110201e04c3fSmrg
110301e04c3fSmrg   return true;
110401e04c3fSmrg}
110501e04c3fSmrg
110601e04c3fSmrgstatic bool
110701e04c3fSmrgvisit_call_src(nir_call_instr *instr, nir_foreach_src_cb cb, void *state)
110801e04c3fSmrg{
110901e04c3fSmrg   for (unsigned i = 0; i < instr->num_params; i++) {
111001e04c3fSmrg      if (!visit_src(&instr->params[i], cb, state))
111101e04c3fSmrg         return false;
111201e04c3fSmrg   }
111301e04c3fSmrg
111401e04c3fSmrg   return true;
111501e04c3fSmrg}
111601e04c3fSmrg
111701e04c3fSmrgstatic bool
111801e04c3fSmrgvisit_phi_src(nir_phi_instr *instr, nir_foreach_src_cb cb, void *state)
111901e04c3fSmrg{
112001e04c3fSmrg   nir_foreach_phi_src(src, instr) {
112101e04c3fSmrg      if (!visit_src(&src->src, cb, state))
112201e04c3fSmrg         return false;
112301e04c3fSmrg   }
112401e04c3fSmrg
112501e04c3fSmrg   return true;
112601e04c3fSmrg}
112701e04c3fSmrg
112801e04c3fSmrgstatic bool
112901e04c3fSmrgvisit_parallel_copy_src(nir_parallel_copy_instr *instr,
113001e04c3fSmrg                        nir_foreach_src_cb cb, void *state)
113101e04c3fSmrg{
113201e04c3fSmrg   nir_foreach_parallel_copy_entry(entry, instr) {
113301e04c3fSmrg      if (!visit_src(&entry->src, cb, state))
113401e04c3fSmrg         return false;
113501e04c3fSmrg   }
113601e04c3fSmrg
113701e04c3fSmrg   return true;
113801e04c3fSmrg}
113901e04c3fSmrg
114001e04c3fSmrgtypedef struct {
114101e04c3fSmrg   void *state;
114201e04c3fSmrg   nir_foreach_src_cb cb;
114301e04c3fSmrg} visit_dest_indirect_state;
114401e04c3fSmrg
114501e04c3fSmrgstatic bool
114601e04c3fSmrgvisit_dest_indirect(nir_dest *dest, void *_state)
114701e04c3fSmrg{
114801e04c3fSmrg   visit_dest_indirect_state *state = (visit_dest_indirect_state *) _state;
114901e04c3fSmrg
115001e04c3fSmrg   if (!dest->is_ssa && dest->reg.indirect)
115101e04c3fSmrg      return state->cb(dest->reg.indirect, state->state);
115201e04c3fSmrg
115301e04c3fSmrg   return true;
115401e04c3fSmrg}
115501e04c3fSmrg
115601e04c3fSmrgbool
115701e04c3fSmrgnir_foreach_src(nir_instr *instr, nir_foreach_src_cb cb, void *state)
115801e04c3fSmrg{
115901e04c3fSmrg   switch (instr->type) {
116001e04c3fSmrg   case nir_instr_type_alu:
116101e04c3fSmrg      if (!visit_alu_src(nir_instr_as_alu(instr), cb, state))
116201e04c3fSmrg         return false;
116301e04c3fSmrg      break;
116401e04c3fSmrg   case nir_instr_type_deref:
116501e04c3fSmrg      if (!visit_deref_instr_src(nir_instr_as_deref(instr), cb, state))
116601e04c3fSmrg         return false;
116701e04c3fSmrg      break;
116801e04c3fSmrg   case nir_instr_type_intrinsic:
116901e04c3fSmrg      if (!visit_intrinsic_src(nir_instr_as_intrinsic(instr), cb, state))
117001e04c3fSmrg         return false;
117101e04c3fSmrg      break;
117201e04c3fSmrg   case nir_instr_type_tex:
117301e04c3fSmrg      if (!visit_tex_src(nir_instr_as_tex(instr), cb, state))
117401e04c3fSmrg         return false;
117501e04c3fSmrg      break;
117601e04c3fSmrg   case nir_instr_type_call:
117701e04c3fSmrg      if (!visit_call_src(nir_instr_as_call(instr), cb, state))
117801e04c3fSmrg         return false;
117901e04c3fSmrg      break;
118001e04c3fSmrg   case nir_instr_type_load_const:
118101e04c3fSmrg      /* Constant load instructions have no regular sources */
118201e04c3fSmrg      break;
118301e04c3fSmrg   case nir_instr_type_phi:
118401e04c3fSmrg      if (!visit_phi_src(nir_instr_as_phi(instr), cb, state))
118501e04c3fSmrg         return false;
118601e04c3fSmrg      break;
118701e04c3fSmrg   case nir_instr_type_parallel_copy:
118801e04c3fSmrg      if (!visit_parallel_copy_src(nir_instr_as_parallel_copy(instr),
118901e04c3fSmrg                                   cb, state))
119001e04c3fSmrg         return false;
119101e04c3fSmrg      break;
119201e04c3fSmrg   case nir_instr_type_jump:
119301e04c3fSmrg   case nir_instr_type_ssa_undef:
119401e04c3fSmrg      return true;
119501e04c3fSmrg
119601e04c3fSmrg   default:
119701e04c3fSmrg      unreachable("Invalid instruction type");
119801e04c3fSmrg      break;
119901e04c3fSmrg   }
120001e04c3fSmrg
120101e04c3fSmrg   visit_dest_indirect_state dest_state;
120201e04c3fSmrg   dest_state.state = state;
120301e04c3fSmrg   dest_state.cb = cb;
120401e04c3fSmrg   return nir_foreach_dest(instr, visit_dest_indirect, &dest_state);
120501e04c3fSmrg}
120601e04c3fSmrg
12077e102996Smayanir_const_value
12087e102996Smayanir_const_value_for_float(double f, unsigned bit_size)
12097e102996Smaya{
12107e102996Smaya   nir_const_value v;
12117e102996Smaya   memset(&v, 0, sizeof(v));
12127e102996Smaya
12137e102996Smaya   switch (bit_size) {
12147e102996Smaya   case 16:
12157e102996Smaya      v.u16 = _mesa_float_to_half(f);
12167e102996Smaya      break;
12177e102996Smaya   case 32:
12187e102996Smaya      v.f32 = f;
12197e102996Smaya      break;
12207e102996Smaya   case 64:
12217e102996Smaya      v.f64 = f;
12227e102996Smaya      break;
12237e102996Smaya   default:
12247e102996Smaya      unreachable("Invalid bit size");
12257e102996Smaya   }
12267e102996Smaya
12277e102996Smaya   return v;
12287e102996Smaya}
12297e102996Smaya
12307e102996Smayadouble
12317e102996Smayanir_const_value_as_float(nir_const_value value, unsigned bit_size)
12327e102996Smaya{
12337e102996Smaya   switch (bit_size) {
12347e102996Smaya   case 16: return _mesa_half_to_float(value.u16);
12357e102996Smaya   case 32: return value.f32;
12367e102996Smaya   case 64: return value.f64;
12377e102996Smaya   default:
12387e102996Smaya      unreachable("Invalid bit size");
12397e102996Smaya   }
12407e102996Smaya}
12417e102996Smaya
124201e04c3fSmrgint64_t
124301e04c3fSmrgnir_src_comp_as_int(nir_src src, unsigned comp)
124401e04c3fSmrg{
124501e04c3fSmrg   assert(nir_src_is_const(src));
124601e04c3fSmrg   nir_load_const_instr *load = nir_instr_as_load_const(src.ssa->parent_instr);
124701e04c3fSmrg
124801e04c3fSmrg   assert(comp < load->def.num_components);
124901e04c3fSmrg   switch (load->def.bit_size) {
12507e102996Smaya   /* int1_t uses 0/-1 convention */
12517e102996Smaya   case 1:  return -(int)load->value[comp].b;
12527e102996Smaya   case 8:  return load->value[comp].i8;
12537e102996Smaya   case 16: return load->value[comp].i16;
12547e102996Smaya   case 32: return load->value[comp].i32;
12557e102996Smaya   case 64: return load->value[comp].i64;
125601e04c3fSmrg   default:
125701e04c3fSmrg      unreachable("Invalid bit size");
125801e04c3fSmrg   }
125901e04c3fSmrg}
126001e04c3fSmrg
126101e04c3fSmrguint64_t
126201e04c3fSmrgnir_src_comp_as_uint(nir_src src, unsigned comp)
126301e04c3fSmrg{
126401e04c3fSmrg   assert(nir_src_is_const(src));
126501e04c3fSmrg   nir_load_const_instr *load = nir_instr_as_load_const(src.ssa->parent_instr);
126601e04c3fSmrg
126701e04c3fSmrg   assert(comp < load->def.num_components);
126801e04c3fSmrg   switch (load->def.bit_size) {
12697e102996Smaya   case 1:  return load->value[comp].b;
12707e102996Smaya   case 8:  return load->value[comp].u8;
12717e102996Smaya   case 16: return load->value[comp].u16;
12727e102996Smaya   case 32: return load->value[comp].u32;
12737e102996Smaya   case 64: return load->value[comp].u64;
127401e04c3fSmrg   default:
127501e04c3fSmrg      unreachable("Invalid bit size");
127601e04c3fSmrg   }
127701e04c3fSmrg}
127801e04c3fSmrg
127901e04c3fSmrgbool
128001e04c3fSmrgnir_src_comp_as_bool(nir_src src, unsigned comp)
128101e04c3fSmrg{
12827e102996Smaya   int64_t i = nir_src_comp_as_int(src, comp);
128301e04c3fSmrg
12847e102996Smaya   /* Booleans of any size use 0/-1 convention */
12857e102996Smaya   assert(i == 0 || i == -1);
128601e04c3fSmrg
12877e102996Smaya   return i;
128801e04c3fSmrg}
128901e04c3fSmrg
129001e04c3fSmrgdouble
129101e04c3fSmrgnir_src_comp_as_float(nir_src src, unsigned comp)
129201e04c3fSmrg{
129301e04c3fSmrg   assert(nir_src_is_const(src));
129401e04c3fSmrg   nir_load_const_instr *load = nir_instr_as_load_const(src.ssa->parent_instr);
129501e04c3fSmrg
129601e04c3fSmrg   assert(comp < load->def.num_components);
129701e04c3fSmrg   switch (load->def.bit_size) {
12987e102996Smaya   case 16: return _mesa_half_to_float(load->value[comp].u16);
12997e102996Smaya   case 32: return load->value[comp].f32;
13007e102996Smaya   case 64: return load->value[comp].f64;
130101e04c3fSmrg   default:
130201e04c3fSmrg      unreachable("Invalid bit size");
130301e04c3fSmrg   }
130401e04c3fSmrg}
130501e04c3fSmrg
130601e04c3fSmrgint64_t
130701e04c3fSmrgnir_src_as_int(nir_src src)
130801e04c3fSmrg{
130901e04c3fSmrg   assert(nir_src_num_components(src) == 1);
131001e04c3fSmrg   return nir_src_comp_as_int(src, 0);
131101e04c3fSmrg}
131201e04c3fSmrg
131301e04c3fSmrguint64_t
131401e04c3fSmrgnir_src_as_uint(nir_src src)
131501e04c3fSmrg{
131601e04c3fSmrg   assert(nir_src_num_components(src) == 1);
131701e04c3fSmrg   return nir_src_comp_as_uint(src, 0);
131801e04c3fSmrg}
131901e04c3fSmrg
132001e04c3fSmrgbool
132101e04c3fSmrgnir_src_as_bool(nir_src src)
132201e04c3fSmrg{
132301e04c3fSmrg   assert(nir_src_num_components(src) == 1);
132401e04c3fSmrg   return nir_src_comp_as_bool(src, 0);
132501e04c3fSmrg}
132601e04c3fSmrg
132701e04c3fSmrgdouble
132801e04c3fSmrgnir_src_as_float(nir_src src)
132901e04c3fSmrg{
133001e04c3fSmrg   assert(nir_src_num_components(src) == 1);
133101e04c3fSmrg   return nir_src_comp_as_float(src, 0);
133201e04c3fSmrg}
133301e04c3fSmrg
133401e04c3fSmrgnir_const_value *
133501e04c3fSmrgnir_src_as_const_value(nir_src src)
133601e04c3fSmrg{
133701e04c3fSmrg   if (!src.is_ssa)
133801e04c3fSmrg      return NULL;
133901e04c3fSmrg
134001e04c3fSmrg   if (src.ssa->parent_instr->type != nir_instr_type_load_const)
134101e04c3fSmrg      return NULL;
134201e04c3fSmrg
134301e04c3fSmrg   nir_load_const_instr *load = nir_instr_as_load_const(src.ssa->parent_instr);
134401e04c3fSmrg
13457e102996Smaya   return load->value;
134601e04c3fSmrg}
134701e04c3fSmrg
134801e04c3fSmrg/**
134901e04c3fSmrg * Returns true if the source is known to be dynamically uniform. Otherwise it
135001e04c3fSmrg * returns false which means it may or may not be dynamically uniform but it
135101e04c3fSmrg * can't be determined.
135201e04c3fSmrg */
135301e04c3fSmrgbool
135401e04c3fSmrgnir_src_is_dynamically_uniform(nir_src src)
135501e04c3fSmrg{
135601e04c3fSmrg   if (!src.is_ssa)
135701e04c3fSmrg      return false;
135801e04c3fSmrg
135901e04c3fSmrg   /* Constants are trivially dynamically uniform */
136001e04c3fSmrg   if (src.ssa->parent_instr->type == nir_instr_type_load_const)
136101e04c3fSmrg      return true;
136201e04c3fSmrg
136301e04c3fSmrg   /* As are uniform variables */
136401e04c3fSmrg   if (src.ssa->parent_instr->type == nir_instr_type_intrinsic) {
136501e04c3fSmrg      nir_intrinsic_instr *intr = nir_instr_as_intrinsic(src.ssa->parent_instr);
136601e04c3fSmrg
136701e04c3fSmrg      if (intr->intrinsic == nir_intrinsic_load_uniform)
136801e04c3fSmrg         return true;
136901e04c3fSmrg   }
137001e04c3fSmrg
137101e04c3fSmrg   /* XXX: this could have many more tests, such as when a sampler function is
137201e04c3fSmrg    * called with dynamically uniform arguments.
137301e04c3fSmrg    */
137401e04c3fSmrg   return false;
137501e04c3fSmrg}
137601e04c3fSmrg
137701e04c3fSmrgstatic void
137801e04c3fSmrgsrc_remove_all_uses(nir_src *src)
137901e04c3fSmrg{
138001e04c3fSmrg   for (; src; src = src->is_ssa ? NULL : src->reg.indirect) {
138101e04c3fSmrg      if (!src_is_valid(src))
138201e04c3fSmrg         continue;
138301e04c3fSmrg
138401e04c3fSmrg      list_del(&src->use_link);
138501e04c3fSmrg   }
138601e04c3fSmrg}
138701e04c3fSmrg
138801e04c3fSmrgstatic void
138901e04c3fSmrgsrc_add_all_uses(nir_src *src, nir_instr *parent_instr, nir_if *parent_if)
139001e04c3fSmrg{
139101e04c3fSmrg   for (; src; src = src->is_ssa ? NULL : src->reg.indirect) {
139201e04c3fSmrg      if (!src_is_valid(src))
139301e04c3fSmrg         continue;
139401e04c3fSmrg
139501e04c3fSmrg      if (parent_instr) {
139601e04c3fSmrg         src->parent_instr = parent_instr;
139701e04c3fSmrg         if (src->is_ssa)
139801e04c3fSmrg            list_addtail(&src->use_link, &src->ssa->uses);
139901e04c3fSmrg         else
140001e04c3fSmrg            list_addtail(&src->use_link, &src->reg.reg->uses);
140101e04c3fSmrg      } else {
140201e04c3fSmrg         assert(parent_if);
140301e04c3fSmrg         src->parent_if = parent_if;
140401e04c3fSmrg         if (src->is_ssa)
140501e04c3fSmrg            list_addtail(&src->use_link, &src->ssa->if_uses);
140601e04c3fSmrg         else
140701e04c3fSmrg            list_addtail(&src->use_link, &src->reg.reg->if_uses);
140801e04c3fSmrg      }
140901e04c3fSmrg   }
141001e04c3fSmrg}
141101e04c3fSmrg
141201e04c3fSmrgvoid
141301e04c3fSmrgnir_instr_rewrite_src(nir_instr *instr, nir_src *src, nir_src new_src)
141401e04c3fSmrg{
141501e04c3fSmrg   assert(!src_is_valid(src) || src->parent_instr == instr);
141601e04c3fSmrg
141701e04c3fSmrg   src_remove_all_uses(src);
141801e04c3fSmrg   *src = new_src;
141901e04c3fSmrg   src_add_all_uses(src, instr, NULL);
142001e04c3fSmrg}
142101e04c3fSmrg
142201e04c3fSmrgvoid
142301e04c3fSmrgnir_instr_move_src(nir_instr *dest_instr, nir_src *dest, nir_src *src)
142401e04c3fSmrg{
142501e04c3fSmrg   assert(!src_is_valid(dest) || dest->parent_instr == dest_instr);
142601e04c3fSmrg
142701e04c3fSmrg   src_remove_all_uses(dest);
142801e04c3fSmrg   src_remove_all_uses(src);
142901e04c3fSmrg   *dest = *src;
143001e04c3fSmrg   *src = NIR_SRC_INIT;
143101e04c3fSmrg   src_add_all_uses(dest, dest_instr, NULL);
143201e04c3fSmrg}
143301e04c3fSmrg
143401e04c3fSmrgvoid
143501e04c3fSmrgnir_if_rewrite_condition(nir_if *if_stmt, nir_src new_src)
143601e04c3fSmrg{
143701e04c3fSmrg   nir_src *src = &if_stmt->condition;
143801e04c3fSmrg   assert(!src_is_valid(src) || src->parent_if == if_stmt);
143901e04c3fSmrg
144001e04c3fSmrg   src_remove_all_uses(src);
144101e04c3fSmrg   *src = new_src;
144201e04c3fSmrg   src_add_all_uses(src, NULL, if_stmt);
144301e04c3fSmrg}
144401e04c3fSmrg
144501e04c3fSmrgvoid
144601e04c3fSmrgnir_instr_rewrite_dest(nir_instr *instr, nir_dest *dest, nir_dest new_dest)
144701e04c3fSmrg{
144801e04c3fSmrg   if (dest->is_ssa) {
144901e04c3fSmrg      /* We can only overwrite an SSA destination if it has no uses. */
145001e04c3fSmrg      assert(list_empty(&dest->ssa.uses) && list_empty(&dest->ssa.if_uses));
145101e04c3fSmrg   } else {
145201e04c3fSmrg      list_del(&dest->reg.def_link);
145301e04c3fSmrg      if (dest->reg.indirect)
145401e04c3fSmrg         src_remove_all_uses(dest->reg.indirect);
145501e04c3fSmrg   }
145601e04c3fSmrg
145701e04c3fSmrg   /* We can't re-write with an SSA def */
145801e04c3fSmrg   assert(!new_dest.is_ssa);
145901e04c3fSmrg
146001e04c3fSmrg   nir_dest_copy(dest, &new_dest, instr);
146101e04c3fSmrg
146201e04c3fSmrg   dest->reg.parent_instr = instr;
146301e04c3fSmrg   list_addtail(&dest->reg.def_link, &new_dest.reg.reg->defs);
146401e04c3fSmrg
146501e04c3fSmrg   if (dest->reg.indirect)
146601e04c3fSmrg      src_add_all_uses(dest->reg.indirect, instr, NULL);
146701e04c3fSmrg}
146801e04c3fSmrg
146901e04c3fSmrg/* note: does *not* take ownership of 'name' */
147001e04c3fSmrgvoid
147101e04c3fSmrgnir_ssa_def_init(nir_instr *instr, nir_ssa_def *def,
147201e04c3fSmrg                 unsigned num_components,
147301e04c3fSmrg                 unsigned bit_size, const char *name)
147401e04c3fSmrg{
147501e04c3fSmrg   def->name = ralloc_strdup(instr, name);
147601e04c3fSmrg   def->parent_instr = instr;
147701e04c3fSmrg   list_inithead(&def->uses);
147801e04c3fSmrg   list_inithead(&def->if_uses);
147901e04c3fSmrg   def->num_components = num_components;
148001e04c3fSmrg   def->bit_size = bit_size;
148101e04c3fSmrg
148201e04c3fSmrg   if (instr->block) {
148301e04c3fSmrg      nir_function_impl *impl =
148401e04c3fSmrg         nir_cf_node_get_function(&instr->block->cf_node);
148501e04c3fSmrg
148601e04c3fSmrg      def->index = impl->ssa_alloc++;
148701e04c3fSmrg   } else {
148801e04c3fSmrg      def->index = UINT_MAX;
148901e04c3fSmrg   }
149001e04c3fSmrg}
149101e04c3fSmrg
149201e04c3fSmrg/* note: does *not* take ownership of 'name' */
149301e04c3fSmrgvoid
149401e04c3fSmrgnir_ssa_dest_init(nir_instr *instr, nir_dest *dest,
149501e04c3fSmrg                 unsigned num_components, unsigned bit_size,
149601e04c3fSmrg                 const char *name)
149701e04c3fSmrg{
149801e04c3fSmrg   dest->is_ssa = true;
149901e04c3fSmrg   nir_ssa_def_init(instr, &dest->ssa, num_components, bit_size, name);
150001e04c3fSmrg}
150101e04c3fSmrg
150201e04c3fSmrgvoid
150301e04c3fSmrgnir_ssa_def_rewrite_uses(nir_ssa_def *def, nir_src new_src)
150401e04c3fSmrg{
150501e04c3fSmrg   assert(!new_src.is_ssa || def != new_src.ssa);
150601e04c3fSmrg
150701e04c3fSmrg   nir_foreach_use_safe(use_src, def)
150801e04c3fSmrg      nir_instr_rewrite_src(use_src->parent_instr, use_src, new_src);
150901e04c3fSmrg
151001e04c3fSmrg   nir_foreach_if_use_safe(use_src, def)
151101e04c3fSmrg      nir_if_rewrite_condition(use_src->parent_if, new_src);
151201e04c3fSmrg}
151301e04c3fSmrg
151401e04c3fSmrgstatic bool
151501e04c3fSmrgis_instr_between(nir_instr *start, nir_instr *end, nir_instr *between)
151601e04c3fSmrg{
151701e04c3fSmrg   assert(start->block == end->block);
151801e04c3fSmrg
151901e04c3fSmrg   if (between->block != start->block)
152001e04c3fSmrg      return false;
152101e04c3fSmrg
152201e04c3fSmrg   /* Search backwards looking for "between" */
152301e04c3fSmrg   while (start != end) {
152401e04c3fSmrg      if (between == end)
152501e04c3fSmrg         return true;
152601e04c3fSmrg
152701e04c3fSmrg      end = nir_instr_prev(end);
152801e04c3fSmrg      assert(end);
152901e04c3fSmrg   }
153001e04c3fSmrg
153101e04c3fSmrg   return false;
153201e04c3fSmrg}
153301e04c3fSmrg
153401e04c3fSmrg/* Replaces all uses of the given SSA def with the given source but only if
153501e04c3fSmrg * the use comes after the after_me instruction.  This can be useful if you
153601e04c3fSmrg * are emitting code to fix up the result of some instruction: you can freely
153701e04c3fSmrg * use the result in that code and then call rewrite_uses_after and pass the
153801e04c3fSmrg * last fixup instruction as after_me and it will replace all of the uses you
153901e04c3fSmrg * want without touching the fixup code.
154001e04c3fSmrg *
154101e04c3fSmrg * This function assumes that after_me is in the same block as
154201e04c3fSmrg * def->parent_instr and that after_me comes after def->parent_instr.
154301e04c3fSmrg */
154401e04c3fSmrgvoid
154501e04c3fSmrgnir_ssa_def_rewrite_uses_after(nir_ssa_def *def, nir_src new_src,
154601e04c3fSmrg                               nir_instr *after_me)
154701e04c3fSmrg{
15487e102996Smaya   if (new_src.is_ssa && def == new_src.ssa)
15497e102996Smaya      return;
155001e04c3fSmrg
155101e04c3fSmrg   nir_foreach_use_safe(use_src, def) {
155201e04c3fSmrg      assert(use_src->parent_instr != def->parent_instr);
155301e04c3fSmrg      /* Since def already dominates all of its uses, the only way a use can
155401e04c3fSmrg       * not be dominated by after_me is if it is between def and after_me in
155501e04c3fSmrg       * the instruction list.
155601e04c3fSmrg       */
155701e04c3fSmrg      if (!is_instr_between(def->parent_instr, after_me, use_src->parent_instr))
155801e04c3fSmrg         nir_instr_rewrite_src(use_src->parent_instr, use_src, new_src);
155901e04c3fSmrg   }
156001e04c3fSmrg
156101e04c3fSmrg   nir_foreach_if_use_safe(use_src, def)
156201e04c3fSmrg      nir_if_rewrite_condition(use_src->parent_if, new_src);
156301e04c3fSmrg}
156401e04c3fSmrg
156501e04c3fSmrgnir_component_mask_t
156601e04c3fSmrgnir_ssa_def_components_read(const nir_ssa_def *def)
156701e04c3fSmrg{
156801e04c3fSmrg   nir_component_mask_t read_mask = 0;
156901e04c3fSmrg   nir_foreach_use(use, def) {
157001e04c3fSmrg      if (use->parent_instr->type == nir_instr_type_alu) {
157101e04c3fSmrg         nir_alu_instr *alu = nir_instr_as_alu(use->parent_instr);
157201e04c3fSmrg         nir_alu_src *alu_src = exec_node_data(nir_alu_src, use, src);
157301e04c3fSmrg         int src_idx = alu_src - &alu->src[0];
157401e04c3fSmrg         assert(src_idx >= 0 && src_idx < nir_op_infos[alu->op].num_inputs);
15757e102996Smaya         read_mask |= nir_alu_instr_src_read_mask(alu, src_idx);
157601e04c3fSmrg      } else {
157701e04c3fSmrg         return (1 << def->num_components) - 1;
157801e04c3fSmrg      }
157901e04c3fSmrg   }
158001e04c3fSmrg
158101e04c3fSmrg   if (!list_empty(&def->if_uses))
158201e04c3fSmrg      read_mask |= 1;
158301e04c3fSmrg
158401e04c3fSmrg   return read_mask;
158501e04c3fSmrg}
158601e04c3fSmrg
158701e04c3fSmrgnir_block *
158801e04c3fSmrgnir_block_cf_tree_next(nir_block *block)
158901e04c3fSmrg{
159001e04c3fSmrg   if (block == NULL) {
159101e04c3fSmrg      /* nir_foreach_block_safe() will call this function on a NULL block
159201e04c3fSmrg       * after the last iteration, but it won't use the result so just return
159301e04c3fSmrg       * NULL here.
159401e04c3fSmrg       */
159501e04c3fSmrg      return NULL;
159601e04c3fSmrg   }
159701e04c3fSmrg
159801e04c3fSmrg   nir_cf_node *cf_next = nir_cf_node_next(&block->cf_node);
159901e04c3fSmrg   if (cf_next)
160001e04c3fSmrg      return nir_cf_node_cf_tree_first(cf_next);
160101e04c3fSmrg
160201e04c3fSmrg   nir_cf_node *parent = block->cf_node.parent;
160301e04c3fSmrg
160401e04c3fSmrg   switch (parent->type) {
160501e04c3fSmrg   case nir_cf_node_if: {
160601e04c3fSmrg      /* Are we at the end of the if? Go to the beginning of the else */
160701e04c3fSmrg      nir_if *if_stmt = nir_cf_node_as_if(parent);
160801e04c3fSmrg      if (block == nir_if_last_then_block(if_stmt))
160901e04c3fSmrg         return nir_if_first_else_block(if_stmt);
161001e04c3fSmrg
161101e04c3fSmrg      assert(block == nir_if_last_else_block(if_stmt));
161201e04c3fSmrg      /* fall through */
161301e04c3fSmrg   }
161401e04c3fSmrg
161501e04c3fSmrg   case nir_cf_node_loop:
161601e04c3fSmrg      return nir_cf_node_as_block(nir_cf_node_next(parent));
161701e04c3fSmrg
161801e04c3fSmrg   case nir_cf_node_function:
161901e04c3fSmrg      return NULL;
162001e04c3fSmrg
162101e04c3fSmrg   default:
162201e04c3fSmrg      unreachable("unknown cf node type");
162301e04c3fSmrg   }
162401e04c3fSmrg}
162501e04c3fSmrg
162601e04c3fSmrgnir_block *
162701e04c3fSmrgnir_block_cf_tree_prev(nir_block *block)
162801e04c3fSmrg{
162901e04c3fSmrg   if (block == NULL) {
163001e04c3fSmrg      /* do this for consistency with nir_block_cf_tree_next() */
163101e04c3fSmrg      return NULL;
163201e04c3fSmrg   }
163301e04c3fSmrg
163401e04c3fSmrg   nir_cf_node *cf_prev = nir_cf_node_prev(&block->cf_node);
163501e04c3fSmrg   if (cf_prev)
163601e04c3fSmrg      return nir_cf_node_cf_tree_last(cf_prev);
163701e04c3fSmrg
163801e04c3fSmrg   nir_cf_node *parent = block->cf_node.parent;
163901e04c3fSmrg
164001e04c3fSmrg   switch (parent->type) {
164101e04c3fSmrg   case nir_cf_node_if: {
164201e04c3fSmrg      /* Are we at the beginning of the else? Go to the end of the if */
164301e04c3fSmrg      nir_if *if_stmt = nir_cf_node_as_if(parent);
164401e04c3fSmrg      if (block == nir_if_first_else_block(if_stmt))
164501e04c3fSmrg         return nir_if_last_then_block(if_stmt);
164601e04c3fSmrg
164701e04c3fSmrg      assert(block == nir_if_first_then_block(if_stmt));
164801e04c3fSmrg      /* fall through */
164901e04c3fSmrg   }
165001e04c3fSmrg
165101e04c3fSmrg   case nir_cf_node_loop:
165201e04c3fSmrg      return nir_cf_node_as_block(nir_cf_node_prev(parent));
165301e04c3fSmrg
165401e04c3fSmrg   case nir_cf_node_function:
165501e04c3fSmrg      return NULL;
165601e04c3fSmrg
165701e04c3fSmrg   default:
165801e04c3fSmrg      unreachable("unknown cf node type");
165901e04c3fSmrg   }
166001e04c3fSmrg}
166101e04c3fSmrg
166201e04c3fSmrgnir_block *nir_cf_node_cf_tree_first(nir_cf_node *node)
166301e04c3fSmrg{
166401e04c3fSmrg   switch (node->type) {
166501e04c3fSmrg   case nir_cf_node_function: {
166601e04c3fSmrg      nir_function_impl *impl = nir_cf_node_as_function(node);
166701e04c3fSmrg      return nir_start_block(impl);
166801e04c3fSmrg   }
166901e04c3fSmrg
167001e04c3fSmrg   case nir_cf_node_if: {
167101e04c3fSmrg      nir_if *if_stmt = nir_cf_node_as_if(node);
167201e04c3fSmrg      return nir_if_first_then_block(if_stmt);
167301e04c3fSmrg   }
167401e04c3fSmrg
167501e04c3fSmrg   case nir_cf_node_loop: {
167601e04c3fSmrg      nir_loop *loop = nir_cf_node_as_loop(node);
167701e04c3fSmrg      return nir_loop_first_block(loop);
167801e04c3fSmrg   }
167901e04c3fSmrg
168001e04c3fSmrg   case nir_cf_node_block: {
168101e04c3fSmrg      return nir_cf_node_as_block(node);
168201e04c3fSmrg   }
168301e04c3fSmrg
168401e04c3fSmrg   default:
168501e04c3fSmrg      unreachable("unknown node type");
168601e04c3fSmrg   }
168701e04c3fSmrg}
168801e04c3fSmrg
168901e04c3fSmrgnir_block *nir_cf_node_cf_tree_last(nir_cf_node *node)
169001e04c3fSmrg{
169101e04c3fSmrg   switch (node->type) {
169201e04c3fSmrg   case nir_cf_node_function: {
169301e04c3fSmrg      nir_function_impl *impl = nir_cf_node_as_function(node);
169401e04c3fSmrg      return nir_impl_last_block(impl);
169501e04c3fSmrg   }
169601e04c3fSmrg
169701e04c3fSmrg   case nir_cf_node_if: {
169801e04c3fSmrg      nir_if *if_stmt = nir_cf_node_as_if(node);
169901e04c3fSmrg      return nir_if_last_else_block(if_stmt);
170001e04c3fSmrg   }
170101e04c3fSmrg
170201e04c3fSmrg   case nir_cf_node_loop: {
170301e04c3fSmrg      nir_loop *loop = nir_cf_node_as_loop(node);
170401e04c3fSmrg      return nir_loop_last_block(loop);
170501e04c3fSmrg   }
170601e04c3fSmrg
170701e04c3fSmrg   case nir_cf_node_block: {
170801e04c3fSmrg      return nir_cf_node_as_block(node);
170901e04c3fSmrg   }
171001e04c3fSmrg
171101e04c3fSmrg   default:
171201e04c3fSmrg      unreachable("unknown node type");
171301e04c3fSmrg   }
171401e04c3fSmrg}
171501e04c3fSmrg
171601e04c3fSmrgnir_block *nir_cf_node_cf_tree_next(nir_cf_node *node)
171701e04c3fSmrg{
171801e04c3fSmrg   if (node->type == nir_cf_node_block)
171901e04c3fSmrg      return nir_block_cf_tree_next(nir_cf_node_as_block(node));
172001e04c3fSmrg   else if (node->type == nir_cf_node_function)
172101e04c3fSmrg      return NULL;
172201e04c3fSmrg   else
172301e04c3fSmrg      return nir_cf_node_as_block(nir_cf_node_next(node));
172401e04c3fSmrg}
172501e04c3fSmrg
172601e04c3fSmrgnir_if *
172701e04c3fSmrgnir_block_get_following_if(nir_block *block)
172801e04c3fSmrg{
172901e04c3fSmrg   if (exec_node_is_tail_sentinel(&block->cf_node.node))
173001e04c3fSmrg      return NULL;
173101e04c3fSmrg
173201e04c3fSmrg   if (nir_cf_node_is_last(&block->cf_node))
173301e04c3fSmrg      return NULL;
173401e04c3fSmrg
173501e04c3fSmrg   nir_cf_node *next_node = nir_cf_node_next(&block->cf_node);
173601e04c3fSmrg
173701e04c3fSmrg   if (next_node->type != nir_cf_node_if)
173801e04c3fSmrg      return NULL;
173901e04c3fSmrg
174001e04c3fSmrg   return nir_cf_node_as_if(next_node);
174101e04c3fSmrg}
174201e04c3fSmrg
174301e04c3fSmrgnir_loop *
174401e04c3fSmrgnir_block_get_following_loop(nir_block *block)
174501e04c3fSmrg{
174601e04c3fSmrg   if (exec_node_is_tail_sentinel(&block->cf_node.node))
174701e04c3fSmrg      return NULL;
174801e04c3fSmrg
174901e04c3fSmrg   if (nir_cf_node_is_last(&block->cf_node))
175001e04c3fSmrg      return NULL;
175101e04c3fSmrg
175201e04c3fSmrg   nir_cf_node *next_node = nir_cf_node_next(&block->cf_node);
175301e04c3fSmrg
175401e04c3fSmrg   if (next_node->type != nir_cf_node_loop)
175501e04c3fSmrg      return NULL;
175601e04c3fSmrg
175701e04c3fSmrg   return nir_cf_node_as_loop(next_node);
175801e04c3fSmrg}
175901e04c3fSmrg
176001e04c3fSmrgvoid
176101e04c3fSmrgnir_index_blocks(nir_function_impl *impl)
176201e04c3fSmrg{
176301e04c3fSmrg   unsigned index = 0;
176401e04c3fSmrg
176501e04c3fSmrg   if (impl->valid_metadata & nir_metadata_block_index)
176601e04c3fSmrg      return;
176701e04c3fSmrg
176801e04c3fSmrg   nir_foreach_block(block, impl) {
176901e04c3fSmrg      block->index = index++;
177001e04c3fSmrg   }
177101e04c3fSmrg
177201e04c3fSmrg   /* The end_block isn't really part of the program, which is why its index
177301e04c3fSmrg    * is >= num_blocks.
177401e04c3fSmrg    */
177501e04c3fSmrg   impl->num_blocks = impl->end_block->index = index;
177601e04c3fSmrg}
177701e04c3fSmrg
177801e04c3fSmrgstatic bool
177901e04c3fSmrgindex_ssa_def_cb(nir_ssa_def *def, void *state)
178001e04c3fSmrg{
178101e04c3fSmrg   unsigned *index = (unsigned *) state;
178201e04c3fSmrg   def->index = (*index)++;
178301e04c3fSmrg
178401e04c3fSmrg   return true;
178501e04c3fSmrg}
178601e04c3fSmrg
178701e04c3fSmrg/**
178801e04c3fSmrg * The indices are applied top-to-bottom which has the very nice property
178901e04c3fSmrg * that, if A dominates B, then A->index <= B->index.
179001e04c3fSmrg */
179101e04c3fSmrgvoid
179201e04c3fSmrgnir_index_ssa_defs(nir_function_impl *impl)
179301e04c3fSmrg{
179401e04c3fSmrg   unsigned index = 0;
179501e04c3fSmrg
179601e04c3fSmrg   nir_foreach_block(block, impl) {
179701e04c3fSmrg      nir_foreach_instr(instr, block)
179801e04c3fSmrg         nir_foreach_ssa_def(instr, index_ssa_def_cb, &index);
179901e04c3fSmrg   }
180001e04c3fSmrg
180101e04c3fSmrg   impl->ssa_alloc = index;
180201e04c3fSmrg}
180301e04c3fSmrg
180401e04c3fSmrg/**
180501e04c3fSmrg * The indices are applied top-to-bottom which has the very nice property
180601e04c3fSmrg * that, if A dominates B, then A->index <= B->index.
180701e04c3fSmrg */
180801e04c3fSmrgunsigned
180901e04c3fSmrgnir_index_instrs(nir_function_impl *impl)
181001e04c3fSmrg{
181101e04c3fSmrg   unsigned index = 0;
181201e04c3fSmrg
181301e04c3fSmrg   nir_foreach_block(block, impl) {
181401e04c3fSmrg      nir_foreach_instr(instr, block)
181501e04c3fSmrg         instr->index = index++;
181601e04c3fSmrg   }
181701e04c3fSmrg
181801e04c3fSmrg   return index;
181901e04c3fSmrg}
182001e04c3fSmrg
182101e04c3fSmrgnir_intrinsic_op
182201e04c3fSmrgnir_intrinsic_from_system_value(gl_system_value val)
182301e04c3fSmrg{
182401e04c3fSmrg   switch (val) {
182501e04c3fSmrg   case SYSTEM_VALUE_VERTEX_ID:
182601e04c3fSmrg      return nir_intrinsic_load_vertex_id;
182701e04c3fSmrg   case SYSTEM_VALUE_INSTANCE_ID:
182801e04c3fSmrg      return nir_intrinsic_load_instance_id;
182901e04c3fSmrg   case SYSTEM_VALUE_DRAW_ID:
183001e04c3fSmrg      return nir_intrinsic_load_draw_id;
183101e04c3fSmrg   case SYSTEM_VALUE_BASE_INSTANCE:
183201e04c3fSmrg      return nir_intrinsic_load_base_instance;
183301e04c3fSmrg   case SYSTEM_VALUE_VERTEX_ID_ZERO_BASE:
183401e04c3fSmrg      return nir_intrinsic_load_vertex_id_zero_base;
183501e04c3fSmrg   case SYSTEM_VALUE_IS_INDEXED_DRAW:
183601e04c3fSmrg      return nir_intrinsic_load_is_indexed_draw;
183701e04c3fSmrg   case SYSTEM_VALUE_FIRST_VERTEX:
183801e04c3fSmrg      return nir_intrinsic_load_first_vertex;
183901e04c3fSmrg   case SYSTEM_VALUE_BASE_VERTEX:
184001e04c3fSmrg      return nir_intrinsic_load_base_vertex;
184101e04c3fSmrg   case SYSTEM_VALUE_INVOCATION_ID:
184201e04c3fSmrg      return nir_intrinsic_load_invocation_id;
184301e04c3fSmrg   case SYSTEM_VALUE_FRAG_COORD:
184401e04c3fSmrg      return nir_intrinsic_load_frag_coord;
184501e04c3fSmrg   case SYSTEM_VALUE_FRONT_FACE:
184601e04c3fSmrg      return nir_intrinsic_load_front_face;
184701e04c3fSmrg   case SYSTEM_VALUE_SAMPLE_ID:
184801e04c3fSmrg      return nir_intrinsic_load_sample_id;
184901e04c3fSmrg   case SYSTEM_VALUE_SAMPLE_POS:
185001e04c3fSmrg      return nir_intrinsic_load_sample_pos;
185101e04c3fSmrg   case SYSTEM_VALUE_SAMPLE_MASK_IN:
185201e04c3fSmrg      return nir_intrinsic_load_sample_mask_in;
185301e04c3fSmrg   case SYSTEM_VALUE_LOCAL_INVOCATION_ID:
185401e04c3fSmrg      return nir_intrinsic_load_local_invocation_id;
185501e04c3fSmrg   case SYSTEM_VALUE_LOCAL_INVOCATION_INDEX:
185601e04c3fSmrg      return nir_intrinsic_load_local_invocation_index;
185701e04c3fSmrg   case SYSTEM_VALUE_WORK_GROUP_ID:
185801e04c3fSmrg      return nir_intrinsic_load_work_group_id;
185901e04c3fSmrg   case SYSTEM_VALUE_NUM_WORK_GROUPS:
186001e04c3fSmrg      return nir_intrinsic_load_num_work_groups;
186101e04c3fSmrg   case SYSTEM_VALUE_PRIMITIVE_ID:
186201e04c3fSmrg      return nir_intrinsic_load_primitive_id;
186301e04c3fSmrg   case SYSTEM_VALUE_TESS_COORD:
186401e04c3fSmrg      return nir_intrinsic_load_tess_coord;
186501e04c3fSmrg   case SYSTEM_VALUE_TESS_LEVEL_OUTER:
186601e04c3fSmrg      return nir_intrinsic_load_tess_level_outer;
186701e04c3fSmrg   case SYSTEM_VALUE_TESS_LEVEL_INNER:
186801e04c3fSmrg      return nir_intrinsic_load_tess_level_inner;
186901e04c3fSmrg   case SYSTEM_VALUE_VERTICES_IN:
187001e04c3fSmrg      return nir_intrinsic_load_patch_vertices_in;
187101e04c3fSmrg   case SYSTEM_VALUE_HELPER_INVOCATION:
187201e04c3fSmrg      return nir_intrinsic_load_helper_invocation;
187301e04c3fSmrg   case SYSTEM_VALUE_VIEW_INDEX:
187401e04c3fSmrg      return nir_intrinsic_load_view_index;
187501e04c3fSmrg   case SYSTEM_VALUE_SUBGROUP_SIZE:
187601e04c3fSmrg      return nir_intrinsic_load_subgroup_size;
187701e04c3fSmrg   case SYSTEM_VALUE_SUBGROUP_INVOCATION:
187801e04c3fSmrg      return nir_intrinsic_load_subgroup_invocation;
187901e04c3fSmrg   case SYSTEM_VALUE_SUBGROUP_EQ_MASK:
188001e04c3fSmrg      return nir_intrinsic_load_subgroup_eq_mask;
188101e04c3fSmrg   case SYSTEM_VALUE_SUBGROUP_GE_MASK:
188201e04c3fSmrg      return nir_intrinsic_load_subgroup_ge_mask;
188301e04c3fSmrg   case SYSTEM_VALUE_SUBGROUP_GT_MASK:
188401e04c3fSmrg      return nir_intrinsic_load_subgroup_gt_mask;
188501e04c3fSmrg   case SYSTEM_VALUE_SUBGROUP_LE_MASK:
188601e04c3fSmrg      return nir_intrinsic_load_subgroup_le_mask;
188701e04c3fSmrg   case SYSTEM_VALUE_SUBGROUP_LT_MASK:
188801e04c3fSmrg      return nir_intrinsic_load_subgroup_lt_mask;
188901e04c3fSmrg   case SYSTEM_VALUE_NUM_SUBGROUPS:
189001e04c3fSmrg      return nir_intrinsic_load_num_subgroups;
189101e04c3fSmrg   case SYSTEM_VALUE_SUBGROUP_ID:
189201e04c3fSmrg      return nir_intrinsic_load_subgroup_id;
189301e04c3fSmrg   case SYSTEM_VALUE_LOCAL_GROUP_SIZE:
189401e04c3fSmrg      return nir_intrinsic_load_local_group_size;
189501e04c3fSmrg   case SYSTEM_VALUE_GLOBAL_INVOCATION_ID:
189601e04c3fSmrg      return nir_intrinsic_load_global_invocation_id;
18977e102996Smaya   case SYSTEM_VALUE_GLOBAL_INVOCATION_INDEX:
18987e102996Smaya      return nir_intrinsic_load_global_invocation_index;
189901e04c3fSmrg   case SYSTEM_VALUE_WORK_DIM:
190001e04c3fSmrg      return nir_intrinsic_load_work_dim;
190101e04c3fSmrg   default:
190201e04c3fSmrg      unreachable("system value does not directly correspond to intrinsic");
190301e04c3fSmrg   }
190401e04c3fSmrg}
190501e04c3fSmrg
190601e04c3fSmrggl_system_value
190701e04c3fSmrgnir_system_value_from_intrinsic(nir_intrinsic_op intrin)
190801e04c3fSmrg{
190901e04c3fSmrg   switch (intrin) {
191001e04c3fSmrg   case nir_intrinsic_load_vertex_id:
191101e04c3fSmrg      return SYSTEM_VALUE_VERTEX_ID;
191201e04c3fSmrg   case nir_intrinsic_load_instance_id:
191301e04c3fSmrg      return SYSTEM_VALUE_INSTANCE_ID;
191401e04c3fSmrg   case nir_intrinsic_load_draw_id:
191501e04c3fSmrg      return SYSTEM_VALUE_DRAW_ID;
191601e04c3fSmrg   case nir_intrinsic_load_base_instance:
191701e04c3fSmrg      return SYSTEM_VALUE_BASE_INSTANCE;
191801e04c3fSmrg   case nir_intrinsic_load_vertex_id_zero_base:
191901e04c3fSmrg      return SYSTEM_VALUE_VERTEX_ID_ZERO_BASE;
192001e04c3fSmrg   case nir_intrinsic_load_first_vertex:
192101e04c3fSmrg      return SYSTEM_VALUE_FIRST_VERTEX;
192201e04c3fSmrg   case nir_intrinsic_load_is_indexed_draw:
192301e04c3fSmrg      return SYSTEM_VALUE_IS_INDEXED_DRAW;
192401e04c3fSmrg   case nir_intrinsic_load_base_vertex:
192501e04c3fSmrg      return SYSTEM_VALUE_BASE_VERTEX;
192601e04c3fSmrg   case nir_intrinsic_load_invocation_id:
192701e04c3fSmrg      return SYSTEM_VALUE_INVOCATION_ID;
192801e04c3fSmrg   case nir_intrinsic_load_frag_coord:
192901e04c3fSmrg      return SYSTEM_VALUE_FRAG_COORD;
193001e04c3fSmrg   case nir_intrinsic_load_front_face:
193101e04c3fSmrg      return SYSTEM_VALUE_FRONT_FACE;
193201e04c3fSmrg   case nir_intrinsic_load_sample_id:
193301e04c3fSmrg      return SYSTEM_VALUE_SAMPLE_ID;
193401e04c3fSmrg   case nir_intrinsic_load_sample_pos:
193501e04c3fSmrg      return SYSTEM_VALUE_SAMPLE_POS;
193601e04c3fSmrg   case nir_intrinsic_load_sample_mask_in:
193701e04c3fSmrg      return SYSTEM_VALUE_SAMPLE_MASK_IN;
193801e04c3fSmrg   case nir_intrinsic_load_local_invocation_id:
193901e04c3fSmrg      return SYSTEM_VALUE_LOCAL_INVOCATION_ID;
194001e04c3fSmrg   case nir_intrinsic_load_local_invocation_index:
194101e04c3fSmrg      return SYSTEM_VALUE_LOCAL_INVOCATION_INDEX;
194201e04c3fSmrg   case nir_intrinsic_load_num_work_groups:
194301e04c3fSmrg      return SYSTEM_VALUE_NUM_WORK_GROUPS;
194401e04c3fSmrg   case nir_intrinsic_load_work_group_id:
194501e04c3fSmrg      return SYSTEM_VALUE_WORK_GROUP_ID;
194601e04c3fSmrg   case nir_intrinsic_load_primitive_id:
194701e04c3fSmrg      return SYSTEM_VALUE_PRIMITIVE_ID;
194801e04c3fSmrg   case nir_intrinsic_load_tess_coord:
194901e04c3fSmrg      return SYSTEM_VALUE_TESS_COORD;
195001e04c3fSmrg   case nir_intrinsic_load_tess_level_outer:
195101e04c3fSmrg      return SYSTEM_VALUE_TESS_LEVEL_OUTER;
195201e04c3fSmrg   case nir_intrinsic_load_tess_level_inner:
195301e04c3fSmrg      return SYSTEM_VALUE_TESS_LEVEL_INNER;
195401e04c3fSmrg   case nir_intrinsic_load_patch_vertices_in:
195501e04c3fSmrg      return SYSTEM_VALUE_VERTICES_IN;
195601e04c3fSmrg   case nir_intrinsic_load_helper_invocation:
195701e04c3fSmrg      return SYSTEM_VALUE_HELPER_INVOCATION;
195801e04c3fSmrg   case nir_intrinsic_load_view_index:
195901e04c3fSmrg      return SYSTEM_VALUE_VIEW_INDEX;
196001e04c3fSmrg   case nir_intrinsic_load_subgroup_size:
196101e04c3fSmrg      return SYSTEM_VALUE_SUBGROUP_SIZE;
196201e04c3fSmrg   case nir_intrinsic_load_subgroup_invocation:
196301e04c3fSmrg      return SYSTEM_VALUE_SUBGROUP_INVOCATION;
196401e04c3fSmrg   case nir_intrinsic_load_subgroup_eq_mask:
196501e04c3fSmrg      return SYSTEM_VALUE_SUBGROUP_EQ_MASK;
196601e04c3fSmrg   case nir_intrinsic_load_subgroup_ge_mask:
196701e04c3fSmrg      return SYSTEM_VALUE_SUBGROUP_GE_MASK;
196801e04c3fSmrg   case nir_intrinsic_load_subgroup_gt_mask:
196901e04c3fSmrg      return SYSTEM_VALUE_SUBGROUP_GT_MASK;
197001e04c3fSmrg   case nir_intrinsic_load_subgroup_le_mask:
197101e04c3fSmrg      return SYSTEM_VALUE_SUBGROUP_LE_MASK;
197201e04c3fSmrg   case nir_intrinsic_load_subgroup_lt_mask:
197301e04c3fSmrg      return SYSTEM_VALUE_SUBGROUP_LT_MASK;
197401e04c3fSmrg   case nir_intrinsic_load_num_subgroups:
197501e04c3fSmrg      return SYSTEM_VALUE_NUM_SUBGROUPS;
197601e04c3fSmrg   case nir_intrinsic_load_subgroup_id:
197701e04c3fSmrg      return SYSTEM_VALUE_SUBGROUP_ID;
197801e04c3fSmrg   case nir_intrinsic_load_local_group_size:
197901e04c3fSmrg      return SYSTEM_VALUE_LOCAL_GROUP_SIZE;
198001e04c3fSmrg   case nir_intrinsic_load_global_invocation_id:
198101e04c3fSmrg      return SYSTEM_VALUE_GLOBAL_INVOCATION_ID;
198201e04c3fSmrg   default:
198301e04c3fSmrg      unreachable("intrinsic doesn't produce a system value");
198401e04c3fSmrg   }
198501e04c3fSmrg}
198601e04c3fSmrg
198701e04c3fSmrg/* OpenGL utility method that remaps the location attributes if they are
198801e04c3fSmrg * doubles. Not needed for vulkan due the differences on the input location
198901e04c3fSmrg * count for doubles on vulkan vs OpenGL
199001e04c3fSmrg *
199101e04c3fSmrg * The bitfield returned in dual_slot is one bit for each double input slot in
199201e04c3fSmrg * the original OpenGL single-slot input numbering.  The mapping from old
199301e04c3fSmrg * locations to new locations is as follows:
199401e04c3fSmrg *
199501e04c3fSmrg *    new_loc = loc + util_bitcount(dual_slot & BITFIELD64_MASK(loc))
199601e04c3fSmrg */
199701e04c3fSmrgvoid
199801e04c3fSmrgnir_remap_dual_slot_attributes(nir_shader *shader, uint64_t *dual_slot)
199901e04c3fSmrg{
200001e04c3fSmrg   assert(shader->info.stage == MESA_SHADER_VERTEX);
200101e04c3fSmrg
200201e04c3fSmrg   *dual_slot = 0;
200301e04c3fSmrg   nir_foreach_variable(var, &shader->inputs) {
200401e04c3fSmrg      if (glsl_type_is_dual_slot(glsl_without_array(var->type))) {
200501e04c3fSmrg         unsigned slots = glsl_count_attribute_slots(var->type, true);
200601e04c3fSmrg         *dual_slot |= BITFIELD64_MASK(slots) << var->data.location;
200701e04c3fSmrg      }
200801e04c3fSmrg   }
200901e04c3fSmrg
201001e04c3fSmrg   nir_foreach_variable(var, &shader->inputs) {
201101e04c3fSmrg      var->data.location +=
201201e04c3fSmrg         util_bitcount64(*dual_slot & BITFIELD64_MASK(var->data.location));
201301e04c3fSmrg   }
201401e04c3fSmrg}
201501e04c3fSmrg
201601e04c3fSmrg/* Returns an attribute mask that has been re-compacted using the given
201701e04c3fSmrg * dual_slot mask.
201801e04c3fSmrg */
201901e04c3fSmrguint64_t
202001e04c3fSmrgnir_get_single_slot_attribs_mask(uint64_t attribs, uint64_t dual_slot)
202101e04c3fSmrg{
202201e04c3fSmrg   while (dual_slot) {
202301e04c3fSmrg      unsigned loc = u_bit_scan64(&dual_slot);
202401e04c3fSmrg      /* mask of all bits up to and including loc */
202501e04c3fSmrg      uint64_t mask = BITFIELD64_MASK(loc + 1);
202601e04c3fSmrg      attribs = (attribs & mask) | ((attribs & ~mask) >> 1);
202701e04c3fSmrg   }
202801e04c3fSmrg   return attribs;
202901e04c3fSmrg}
20307e102996Smaya
20317e102996Smayavoid
20327e102996Smayanir_rewrite_image_intrinsic(nir_intrinsic_instr *intrin, nir_ssa_def *src,
20337e102996Smaya                            bool bindless)
20347e102996Smaya{
20357e102996Smaya   enum gl_access_qualifier access = nir_intrinsic_access(intrin);
20367e102996Smaya
20377e102996Smaya   switch (intrin->intrinsic) {
20387e102996Smaya#define CASE(op) \
20397e102996Smaya   case nir_intrinsic_image_deref_##op: \
20407e102996Smaya      intrin->intrinsic = bindless ? nir_intrinsic_bindless_image_##op \
20417e102996Smaya                                   : nir_intrinsic_image_##op; \
20427e102996Smaya      break;
20437e102996Smaya   CASE(load)
20447e102996Smaya   CASE(store)
20457e102996Smaya   CASE(atomic_add)
20467e102996Smaya   CASE(atomic_min)
20477e102996Smaya   CASE(atomic_max)
20487e102996Smaya   CASE(atomic_and)
20497e102996Smaya   CASE(atomic_or)
20507e102996Smaya   CASE(atomic_xor)
20517e102996Smaya   CASE(atomic_exchange)
20527e102996Smaya   CASE(atomic_comp_swap)
20537e102996Smaya   CASE(atomic_fadd)
20547e102996Smaya   CASE(size)
20557e102996Smaya   CASE(samples)
20567e102996Smaya   CASE(load_raw_intel)
20577e102996Smaya   CASE(store_raw_intel)
20587e102996Smaya#undef CASE
20597e102996Smaya   default:
20607e102996Smaya      unreachable("Unhanded image intrinsic");
20617e102996Smaya   }
20627e102996Smaya
20637e102996Smaya   nir_deref_instr *deref = nir_src_as_deref(intrin->src[0]);
20647e102996Smaya   nir_variable *var = nir_deref_instr_get_variable(deref);
20657e102996Smaya
20667e102996Smaya   nir_intrinsic_set_image_dim(intrin, glsl_get_sampler_dim(deref->type));
20677e102996Smaya   nir_intrinsic_set_image_array(intrin, glsl_sampler_type_is_array(deref->type));
20687e102996Smaya   nir_intrinsic_set_access(intrin, access | var->data.image.access);
20697e102996Smaya   nir_intrinsic_set_format(intrin, var->data.image.format);
20707e102996Smaya
20717e102996Smaya   nir_instr_rewrite_src(&intrin->instr, &intrin->src[0],
20727e102996Smaya                         nir_src_for_ssa(src));
20737e102996Smaya}
2074