2 * Copyright © 2014 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Connor Abbott (cwabbott0@gmail.com)
29 #include "nir_control_flow_private.h"
33 nir_shader_create(void *mem_ctx
,
34 gl_shader_stage stage
,
35 const nir_shader_compiler_options
*options
,
38 nir_shader
*shader
= rzalloc(mem_ctx
, nir_shader
);
40 exec_list_make_empty(&shader
->uniforms
);
41 exec_list_make_empty(&shader
->inputs
);
42 exec_list_make_empty(&shader
->outputs
);
43 exec_list_make_empty(&shader
->shared
);
45 shader
->options
= options
;
50 exec_list_make_empty(&shader
->functions
);
51 exec_list_make_empty(&shader
->registers
);
52 exec_list_make_empty(&shader
->globals
);
53 exec_list_make_empty(&shader
->system_values
);
54 shader
->reg_alloc
= 0;
56 shader
->num_inputs
= 0;
57 shader
->num_outputs
= 0;
58 shader
->num_uniforms
= 0;
59 shader
->num_shared
= 0;
61 shader
->stage
= stage
;
67 reg_create(void *mem_ctx
, struct exec_list
*list
)
69 nir_register
*reg
= ralloc(mem_ctx
, nir_register
);
71 list_inithead(®
->uses
);
72 list_inithead(®
->defs
);
73 list_inithead(®
->if_uses
);
75 reg
->num_components
= 0;
77 reg
->num_array_elems
= 0;
78 reg
->is_packed
= false;
81 exec_list_push_tail(list
, ®
->node
);
87 nir_global_reg_create(nir_shader
*shader
)
89 nir_register
*reg
= reg_create(shader
, &shader
->registers
);
90 reg
->index
= shader
->reg_alloc
++;
91 reg
->is_global
= true;
97 nir_local_reg_create(nir_function_impl
*impl
)
99 nir_register
*reg
= reg_create(ralloc_parent(impl
), &impl
->registers
);
100 reg
->index
= impl
->reg_alloc
++;
101 reg
->is_global
= false;
107 nir_reg_remove(nir_register
*reg
)
109 exec_node_remove(®
->node
);
113 nir_shader_add_variable(nir_shader
*shader
, nir_variable
*var
)
115 switch (var
->data
.mode
) {
117 assert(!"invalid mode");
121 assert(!"nir_shader_add_variable cannot be used for local variables");
125 assert(!"nir_shader_add_variable cannot be used for function parameters");
129 exec_list_push_tail(&shader
->globals
, &var
->node
);
132 case nir_var_shader_in
:
133 exec_list_push_tail(&shader
->inputs
, &var
->node
);
136 case nir_var_shader_out
:
137 exec_list_push_tail(&shader
->outputs
, &var
->node
);
140 case nir_var_uniform
:
141 case nir_var_shader_storage
:
142 exec_list_push_tail(&shader
->uniforms
, &var
->node
);
146 assert(shader
->stage
== MESA_SHADER_COMPUTE
);
147 exec_list_push_tail(&shader
->shared
, &var
->node
);
150 case nir_var_system_value
:
151 exec_list_push_tail(&shader
->system_values
, &var
->node
);
157 nir_variable_create(nir_shader
*shader
, nir_variable_mode mode
,
158 const struct glsl_type
*type
, const char *name
)
160 nir_variable
*var
= rzalloc(shader
, nir_variable
);
161 var
->name
= ralloc_strdup(var
, name
);
163 var
->data
.mode
= mode
;
165 if ((mode
== nir_var_shader_in
&& shader
->stage
!= MESA_SHADER_VERTEX
) ||
166 (mode
== nir_var_shader_out
&& shader
->stage
!= MESA_SHADER_FRAGMENT
))
167 var
->data
.interpolation
= INTERP_MODE_SMOOTH
;
169 if (mode
== nir_var_shader_in
|| mode
== nir_var_uniform
)
170 var
->data
.read_only
= true;
172 nir_shader_add_variable(shader
, var
);
178 nir_local_variable_create(nir_function_impl
*impl
,
179 const struct glsl_type
*type
, const char *name
)
181 nir_variable
*var
= rzalloc(impl
->function
->shader
, nir_variable
);
182 var
->name
= ralloc_strdup(var
, name
);
184 var
->data
.mode
= nir_var_local
;
186 nir_function_impl_add_variable(impl
, var
);
192 nir_function_create(nir_shader
*shader
, const char *name
)
194 nir_function
*func
= ralloc(shader
, nir_function
);
196 exec_list_push_tail(&shader
->functions
, &func
->node
);
198 func
->name
= ralloc_strdup(func
, name
);
199 func
->shader
= shader
;
200 func
->num_params
= 0;
202 func
->return_type
= glsl_void_type();
208 void nir_src_copy(nir_src
*dest
, const nir_src
*src
, void *mem_ctx
)
210 dest
->is_ssa
= src
->is_ssa
;
212 dest
->ssa
= src
->ssa
;
214 dest
->reg
.base_offset
= src
->reg
.base_offset
;
215 dest
->reg
.reg
= src
->reg
.reg
;
216 if (src
->reg
.indirect
) {
217 dest
->reg
.indirect
= ralloc(mem_ctx
, nir_src
);
218 nir_src_copy(dest
->reg
.indirect
, src
->reg
.indirect
, mem_ctx
);
220 dest
->reg
.indirect
= NULL
;
225 void nir_dest_copy(nir_dest
*dest
, const nir_dest
*src
, nir_instr
*instr
)
227 /* Copying an SSA definition makes no sense whatsoever. */
228 assert(!src
->is_ssa
);
230 dest
->is_ssa
= false;
232 dest
->reg
.base_offset
= src
->reg
.base_offset
;
233 dest
->reg
.reg
= src
->reg
.reg
;
234 if (src
->reg
.indirect
) {
235 dest
->reg
.indirect
= ralloc(instr
, nir_src
);
236 nir_src_copy(dest
->reg
.indirect
, src
->reg
.indirect
, instr
);
238 dest
->reg
.indirect
= NULL
;
243 nir_alu_src_copy(nir_alu_src
*dest
, const nir_alu_src
*src
,
244 nir_alu_instr
*instr
)
246 nir_src_copy(&dest
->src
, &src
->src
, &instr
->instr
);
247 dest
->abs
= src
->abs
;
248 dest
->negate
= src
->negate
;
249 for (unsigned i
= 0; i
< 4; i
++)
250 dest
->swizzle
[i
] = src
->swizzle
[i
];
254 nir_alu_dest_copy(nir_alu_dest
*dest
, const nir_alu_dest
*src
,
255 nir_alu_instr
*instr
)
257 nir_dest_copy(&dest
->dest
, &src
->dest
, &instr
->instr
);
258 dest
->write_mask
= src
->write_mask
;
259 dest
->saturate
= src
->saturate
;
264 cf_init(nir_cf_node
*node
, nir_cf_node_type type
)
266 exec_node_init(&node
->node
);
272 nir_function_impl_create_bare(nir_shader
*shader
)
274 nir_function_impl
*impl
= ralloc(shader
, nir_function_impl
);
276 impl
->function
= NULL
;
278 cf_init(&impl
->cf_node
, nir_cf_node_function
);
280 exec_list_make_empty(&impl
->body
);
281 exec_list_make_empty(&impl
->registers
);
282 exec_list_make_empty(&impl
->locals
);
283 impl
->num_params
= 0;
285 impl
->return_var
= NULL
;
288 impl
->valid_metadata
= nir_metadata_none
;
290 /* create start & end blocks */
291 nir_block
*start_block
= nir_block_create(shader
);
292 nir_block
*end_block
= nir_block_create(shader
);
293 start_block
->cf_node
.parent
= &impl
->cf_node
;
294 end_block
->cf_node
.parent
= &impl
->cf_node
;
295 impl
->end_block
= end_block
;
297 exec_list_push_tail(&impl
->body
, &start_block
->cf_node
.node
);
299 start_block
->successors
[0] = end_block
;
300 _mesa_set_add(end_block
->predecessors
, start_block
);
305 nir_function_impl_create(nir_function
*function
)
307 assert(function
->impl
== NULL
);
309 nir_function_impl
*impl
= nir_function_impl_create_bare(function
->shader
);
311 function
->impl
= impl
;
312 impl
->function
= function
;
314 impl
->num_params
= function
->num_params
;
315 impl
->params
= ralloc_array(function
->shader
,
316 nir_variable
*, impl
->num_params
);
318 for (unsigned i
= 0; i
< impl
->num_params
; i
++) {
319 impl
->params
[i
] = rzalloc(function
->shader
, nir_variable
);
320 impl
->params
[i
]->type
= function
->params
[i
].type
;
321 impl
->params
[i
]->data
.mode
= nir_var_param
;
322 impl
->params
[i
]->data
.location
= i
;
325 if (!glsl_type_is_void(function
->return_type
)) {
326 impl
->return_var
= rzalloc(function
->shader
, nir_variable
);
327 impl
->return_var
->type
= function
->return_type
;
328 impl
->return_var
->data
.mode
= nir_var_param
;
329 impl
->return_var
->data
.location
= -1;
331 impl
->return_var
= NULL
;
338 nir_block_create(nir_shader
*shader
)
340 nir_block
*block
= rzalloc(shader
, nir_block
);
342 cf_init(&block
->cf_node
, nir_cf_node_block
);
344 block
->successors
[0] = block
->successors
[1] = NULL
;
345 block
->predecessors
= _mesa_set_create(block
, _mesa_hash_pointer
,
346 _mesa_key_pointer_equal
);
347 block
->imm_dom
= NULL
;
348 /* XXX maybe it would be worth it to defer allocation? This
349 * way it doesn't get allocated for shader refs that never run
350 * nir_calc_dominance? For example, state-tracker creates an
351 * initial IR, clones that, runs appropriate lowering pass, passes
352 * to driver which does common lowering/opt, and then stores ref
353 * which is later used to do state specific lowering and futher
354 * opt. Do any of the references not need dominance metadata?
356 block
->dom_frontier
= _mesa_set_create(block
, _mesa_hash_pointer
,
357 _mesa_key_pointer_equal
);
359 exec_list_make_empty(&block
->instr_list
);
365 src_init(nir_src
*src
)
369 src
->reg
.indirect
= NULL
;
370 src
->reg
.base_offset
= 0;
374 nir_if_create(nir_shader
*shader
)
376 nir_if
*if_stmt
= ralloc(shader
, nir_if
);
378 cf_init(&if_stmt
->cf_node
, nir_cf_node_if
);
379 src_init(&if_stmt
->condition
);
381 nir_block
*then
= nir_block_create(shader
);
382 exec_list_make_empty(&if_stmt
->then_list
);
383 exec_list_push_tail(&if_stmt
->then_list
, &then
->cf_node
.node
);
384 then
->cf_node
.parent
= &if_stmt
->cf_node
;
386 nir_block
*else_stmt
= nir_block_create(shader
);
387 exec_list_make_empty(&if_stmt
->else_list
);
388 exec_list_push_tail(&if_stmt
->else_list
, &else_stmt
->cf_node
.node
);
389 else_stmt
->cf_node
.parent
= &if_stmt
->cf_node
;
395 nir_loop_create(nir_shader
*shader
)
397 nir_loop
*loop
= rzalloc(shader
, nir_loop
);
399 cf_init(&loop
->cf_node
, nir_cf_node_loop
);
401 nir_block
*body
= nir_block_create(shader
);
402 exec_list_make_empty(&loop
->body
);
403 exec_list_push_tail(&loop
->body
, &body
->cf_node
.node
);
404 body
->cf_node
.parent
= &loop
->cf_node
;
406 body
->successors
[0] = body
;
407 _mesa_set_add(body
->predecessors
, body
);
413 instr_init(nir_instr
*instr
, nir_instr_type type
)
417 exec_node_init(&instr
->node
);
421 dest_init(nir_dest
*dest
)
423 dest
->is_ssa
= false;
424 dest
->reg
.reg
= NULL
;
425 dest
->reg
.indirect
= NULL
;
426 dest
->reg
.base_offset
= 0;
430 alu_dest_init(nir_alu_dest
*dest
)
432 dest_init(&dest
->dest
);
433 dest
->saturate
= false;
434 dest
->write_mask
= 0xf;
438 alu_src_init(nir_alu_src
*src
)
441 src
->abs
= src
->negate
= false;
449 nir_alu_instr_create(nir_shader
*shader
, nir_op op
)
451 unsigned num_srcs
= nir_op_infos
[op
].num_inputs
;
452 /* TODO: don't use rzalloc */
453 nir_alu_instr
*instr
=
455 sizeof(nir_alu_instr
) + num_srcs
* sizeof(nir_alu_src
));
457 instr_init(&instr
->instr
, nir_instr_type_alu
);
459 alu_dest_init(&instr
->dest
);
460 for (unsigned i
= 0; i
< num_srcs
; i
++)
461 alu_src_init(&instr
->src
[i
]);
467 nir_jump_instr_create(nir_shader
*shader
, nir_jump_type type
)
469 nir_jump_instr
*instr
= ralloc(shader
, nir_jump_instr
);
470 instr_init(&instr
->instr
, nir_instr_type_jump
);
475 nir_load_const_instr
*
476 nir_load_const_instr_create(nir_shader
*shader
, unsigned num_components
,
479 nir_load_const_instr
*instr
= ralloc(shader
, nir_load_const_instr
);
480 instr_init(&instr
->instr
, nir_instr_type_load_const
);
482 nir_ssa_def_init(&instr
->instr
, &instr
->def
, num_components
, bit_size
, NULL
);
487 nir_intrinsic_instr
*
488 nir_intrinsic_instr_create(nir_shader
*shader
, nir_intrinsic_op op
)
490 unsigned num_srcs
= nir_intrinsic_infos
[op
].num_srcs
;
491 /* TODO: don't use rzalloc */
492 nir_intrinsic_instr
*instr
=
494 sizeof(nir_intrinsic_instr
) + num_srcs
* sizeof(nir_src
));
496 instr_init(&instr
->instr
, nir_instr_type_intrinsic
);
497 instr
->intrinsic
= op
;
499 if (nir_intrinsic_infos
[op
].has_dest
)
500 dest_init(&instr
->dest
);
502 for (unsigned i
= 0; i
< num_srcs
; i
++)
503 src_init(&instr
->src
[i
]);
509 nir_call_instr_create(nir_shader
*shader
, nir_function
*callee
)
511 nir_call_instr
*instr
= ralloc(shader
, nir_call_instr
);
512 instr_init(&instr
->instr
, nir_instr_type_call
);
514 instr
->callee
= callee
;
515 instr
->num_params
= callee
->num_params
;
516 instr
->params
= ralloc_array(instr
, nir_deref_var
*, instr
->num_params
);
517 instr
->return_deref
= NULL
;
523 nir_tex_instr_create(nir_shader
*shader
, unsigned num_srcs
)
525 nir_tex_instr
*instr
= rzalloc(shader
, nir_tex_instr
);
526 instr_init(&instr
->instr
, nir_instr_type_tex
);
528 dest_init(&instr
->dest
);
530 instr
->num_srcs
= num_srcs
;
531 instr
->src
= ralloc_array(instr
, nir_tex_src
, num_srcs
);
532 for (unsigned i
= 0; i
< num_srcs
; i
++)
533 src_init(&instr
->src
[i
].src
);
535 instr
->texture_index
= 0;
536 instr
->texture_array_size
= 0;
537 instr
->texture
= NULL
;
538 instr
->sampler_index
= 0;
539 instr
->sampler
= NULL
;
545 nir_tex_instr_add_src(nir_tex_instr
*tex
,
546 nir_tex_src_type src_type
,
549 nir_tex_src
*new_srcs
= rzalloc_array(tex
, nir_tex_src
,
552 for (unsigned i
= 0; i
< tex
->num_srcs
; i
++) {
553 new_srcs
[i
].src_type
= tex
->src
[i
].src_type
;
554 nir_instr_move_src(&tex
->instr
, &new_srcs
[i
].src
,
558 ralloc_free(tex
->src
);
561 tex
->src
[tex
->num_srcs
].src_type
= src_type
;
562 nir_instr_rewrite_src(&tex
->instr
, &tex
->src
[tex
->num_srcs
].src
, src
);
567 nir_tex_instr_remove_src(nir_tex_instr
*tex
, unsigned src_idx
)
569 assert(src_idx
< tex
->num_srcs
);
571 /* First rewrite the source to NIR_SRC_INIT */
572 nir_instr_rewrite_src(&tex
->instr
, &tex
->src
[src_idx
].src
, NIR_SRC_INIT
);
574 /* Now, move all of the other sources down */
575 for (unsigned i
= src_idx
+ 1; i
< tex
->num_srcs
; i
++) {
576 tex
->src
[i
-1].src_type
= tex
->src
[i
].src_type
;
577 nir_instr_move_src(&tex
->instr
, &tex
->src
[i
-1].src
, &tex
->src
[i
].src
);
583 nir_phi_instr_create(nir_shader
*shader
)
585 nir_phi_instr
*instr
= ralloc(shader
, nir_phi_instr
);
586 instr_init(&instr
->instr
, nir_instr_type_phi
);
588 dest_init(&instr
->dest
);
589 exec_list_make_empty(&instr
->srcs
);
593 nir_parallel_copy_instr
*
594 nir_parallel_copy_instr_create(nir_shader
*shader
)
596 nir_parallel_copy_instr
*instr
= ralloc(shader
, nir_parallel_copy_instr
);
597 instr_init(&instr
->instr
, nir_instr_type_parallel_copy
);
599 exec_list_make_empty(&instr
->entries
);
604 nir_ssa_undef_instr
*
605 nir_ssa_undef_instr_create(nir_shader
*shader
,
606 unsigned num_components
,
609 nir_ssa_undef_instr
*instr
= ralloc(shader
, nir_ssa_undef_instr
);
610 instr_init(&instr
->instr
, nir_instr_type_ssa_undef
);
612 nir_ssa_def_init(&instr
->instr
, &instr
->def
, num_components
, bit_size
, NULL
);
618 nir_deref_var_create(void *mem_ctx
, nir_variable
*var
)
620 nir_deref_var
*deref
= ralloc(mem_ctx
, nir_deref_var
);
621 deref
->deref
.deref_type
= nir_deref_type_var
;
622 deref
->deref
.child
= NULL
;
623 deref
->deref
.type
= var
->type
;
629 nir_deref_array_create(void *mem_ctx
)
631 nir_deref_array
*deref
= ralloc(mem_ctx
, nir_deref_array
);
632 deref
->deref
.deref_type
= nir_deref_type_array
;
633 deref
->deref
.child
= NULL
;
634 deref
->deref_array_type
= nir_deref_array_type_direct
;
635 src_init(&deref
->indirect
);
636 deref
->base_offset
= 0;
641 nir_deref_struct_create(void *mem_ctx
, unsigned field_index
)
643 nir_deref_struct
*deref
= ralloc(mem_ctx
, nir_deref_struct
);
644 deref
->deref
.deref_type
= nir_deref_type_struct
;
645 deref
->deref
.child
= NULL
;
646 deref
->index
= field_index
;
651 nir_deref_var_clone(const nir_deref_var
*deref
, void *mem_ctx
)
656 nir_deref_var
*ret
= nir_deref_var_create(mem_ctx
, deref
->var
);
657 ret
->deref
.type
= deref
->deref
.type
;
658 if (deref
->deref
.child
)
659 ret
->deref
.child
= nir_deref_clone(deref
->deref
.child
, ret
);
663 static nir_deref_array
*
664 deref_array_clone(const nir_deref_array
*deref
, void *mem_ctx
)
666 nir_deref_array
*ret
= nir_deref_array_create(mem_ctx
);
667 ret
->base_offset
= deref
->base_offset
;
668 ret
->deref_array_type
= deref
->deref_array_type
;
669 if (deref
->deref_array_type
== nir_deref_array_type_indirect
) {
670 nir_src_copy(&ret
->indirect
, &deref
->indirect
, mem_ctx
);
672 ret
->deref
.type
= deref
->deref
.type
;
673 if (deref
->deref
.child
)
674 ret
->deref
.child
= nir_deref_clone(deref
->deref
.child
, ret
);
678 static nir_deref_struct
*
679 deref_struct_clone(const nir_deref_struct
*deref
, void *mem_ctx
)
681 nir_deref_struct
*ret
= nir_deref_struct_create(mem_ctx
, deref
->index
);
682 ret
->deref
.type
= deref
->deref
.type
;
683 if (deref
->deref
.child
)
684 ret
->deref
.child
= nir_deref_clone(deref
->deref
.child
, ret
);
689 nir_deref_clone(const nir_deref
*deref
, void *mem_ctx
)
694 switch (deref
->deref_type
) {
695 case nir_deref_type_var
:
696 return &nir_deref_var_clone(nir_deref_as_var(deref
), mem_ctx
)->deref
;
697 case nir_deref_type_array
:
698 return &deref_array_clone(nir_deref_as_array(deref
), mem_ctx
)->deref
;
699 case nir_deref_type_struct
:
700 return &deref_struct_clone(nir_deref_as_struct(deref
), mem_ctx
)->deref
;
702 unreachable("Invalid dereference type");
708 /* This is the second step in the recursion. We've found the tail and made a
709 * copy. Now we need to iterate over all possible leaves and call the
710 * callback on each one.
713 deref_foreach_leaf_build_recur(nir_deref_var
*deref
, nir_deref
*tail
,
714 nir_deref_foreach_leaf_cb cb
, void *state
)
719 nir_deref_struct str
;
722 assert(tail
->child
== NULL
);
723 switch (glsl_get_base_type(tail
->type
)) {
725 case GLSL_TYPE_UINT64
:
727 case GLSL_TYPE_INT64
:
728 case GLSL_TYPE_FLOAT
:
729 case GLSL_TYPE_DOUBLE
:
731 if (glsl_type_is_vector_or_scalar(tail
->type
))
732 return cb(deref
, state
);
735 case GLSL_TYPE_ARRAY
:
736 tmp
.arr
.deref
.deref_type
= nir_deref_type_array
;
737 tmp
.arr
.deref
.type
= glsl_get_array_element(tail
->type
);
738 tmp
.arr
.deref_array_type
= nir_deref_array_type_direct
;
739 tmp
.arr
.indirect
= NIR_SRC_INIT
;
740 tail
->child
= &tmp
.arr
.deref
;
742 length
= glsl_get_length(tail
->type
);
743 for (unsigned i
= 0; i
< length
; i
++) {
744 tmp
.arr
.deref
.child
= NULL
;
745 tmp
.arr
.base_offset
= i
;
746 if (!deref_foreach_leaf_build_recur(deref
, &tmp
.arr
.deref
, cb
, state
))
751 case GLSL_TYPE_STRUCT
:
752 tmp
.str
.deref
.deref_type
= nir_deref_type_struct
;
753 tail
->child
= &tmp
.str
.deref
;
755 length
= glsl_get_length(tail
->type
);
756 for (unsigned i
= 0; i
< length
; i
++) {
757 tmp
.arr
.deref
.child
= NULL
;
758 tmp
.str
.deref
.type
= glsl_get_struct_field(tail
->type
, i
);
760 if (!deref_foreach_leaf_build_recur(deref
, &tmp
.arr
.deref
, cb
, state
))
766 unreachable("Invalid type for dereference");
770 /* This is the first step of the foreach_leaf recursion. In this step we are
771 * walking to the end of the deref chain and making a copy in the stack as we
772 * go. This is because we don't want to mutate the deref chain that was
773 * passed in by the caller. The downside is that this deref chain is on the
774 * stack and , if the caller wants to do anything with it, they will have to
775 * make their own copy because this one will go away.
778 deref_foreach_leaf_copy_recur(nir_deref_var
*deref
, nir_deref
*tail
,
779 nir_deref_foreach_leaf_cb cb
, void *state
)
783 nir_deref_struct str
;
787 switch (tail
->child
->deref_type
) {
788 case nir_deref_type_array
:
789 c
.arr
= *nir_deref_as_array(tail
->child
);
790 tail
->child
= &c
.arr
.deref
;
791 return deref_foreach_leaf_copy_recur(deref
, &c
.arr
.deref
, cb
, state
);
793 case nir_deref_type_struct
:
794 c
.str
= *nir_deref_as_struct(tail
->child
);
795 tail
->child
= &c
.str
.deref
;
796 return deref_foreach_leaf_copy_recur(deref
, &c
.str
.deref
, cb
, state
);
798 case nir_deref_type_var
:
800 unreachable("Invalid deref type for a child");
803 /* We've gotten to the end of the original deref. Time to start
804 * building our own derefs.
806 return deref_foreach_leaf_build_recur(deref
, tail
, cb
, state
);
811 * This function iterates over all of the possible derefs that can be created
812 * with the given deref as the head. It then calls the provided callback with
813 * a full deref for each one.
815 * The deref passed to the callback will be allocated on the stack. You will
816 * need to make a copy if you want it to hang around.
819 nir_deref_foreach_leaf(nir_deref_var
*deref
,
820 nir_deref_foreach_leaf_cb cb
, void *state
)
822 nir_deref_var copy
= *deref
;
823 return deref_foreach_leaf_copy_recur(©
, ©
.deref
, cb
, state
);
826 /* Returns a load_const instruction that represents the constant
827 * initializer for the given deref chain. The caller is responsible for
828 * ensuring that there actually is a constant initializer.
830 nir_load_const_instr
*
831 nir_deref_get_const_initializer_load(nir_shader
*shader
, nir_deref_var
*deref
)
833 nir_constant
*constant
= deref
->var
->constant_initializer
;
836 const nir_deref
*tail
= &deref
->deref
;
837 unsigned matrix_col
= 0;
838 while (tail
->child
) {
839 switch (tail
->child
->deref_type
) {
840 case nir_deref_type_array
: {
841 nir_deref_array
*arr
= nir_deref_as_array(tail
->child
);
842 assert(arr
->deref_array_type
== nir_deref_array_type_direct
);
843 if (glsl_type_is_matrix(tail
->type
)) {
844 assert(arr
->deref
.child
== NULL
);
845 matrix_col
= arr
->base_offset
;
847 constant
= constant
->elements
[arr
->base_offset
];
852 case nir_deref_type_struct
: {
853 constant
= constant
->elements
[nir_deref_as_struct(tail
->child
)->index
];
858 unreachable("Invalid deref child type");
864 unsigned bit_size
= glsl_get_bit_size(tail
->type
);
865 nir_load_const_instr
*load
=
866 nir_load_const_instr_create(shader
, glsl_get_vector_elements(tail
->type
),
869 switch (glsl_get_base_type(tail
->type
)) {
870 case GLSL_TYPE_FLOAT
:
873 case GLSL_TYPE_DOUBLE
:
874 case GLSL_TYPE_UINT64
:
875 case GLSL_TYPE_INT64
:
877 load
->value
= constant
->values
[matrix_col
];
880 unreachable("Invalid immediate type");
887 nir_cf_node_get_function(nir_cf_node
*node
)
889 while (node
->type
!= nir_cf_node_function
) {
893 return nir_cf_node_as_function(node
);
896 /* Reduces a cursor by trying to convert everything to after and trying to
897 * go up to block granularity when possible.
900 reduce_cursor(nir_cursor cursor
)
902 switch (cursor
.option
) {
903 case nir_cursor_before_block
:
904 assert(nir_cf_node_prev(&cursor
.block
->cf_node
) == NULL
||
905 nir_cf_node_prev(&cursor
.block
->cf_node
)->type
!= nir_cf_node_block
);
906 if (exec_list_is_empty(&cursor
.block
->instr_list
)) {
907 /* Empty block. After is as good as before. */
908 cursor
.option
= nir_cursor_after_block
;
912 case nir_cursor_after_block
:
915 case nir_cursor_before_instr
: {
916 nir_instr
*prev_instr
= nir_instr_prev(cursor
.instr
);
918 /* Before this instruction is after the previous */
919 cursor
.instr
= prev_instr
;
920 cursor
.option
= nir_cursor_after_instr
;
922 /* No previous instruction. Switch to before block */
923 cursor
.block
= cursor
.instr
->block
;
924 cursor
.option
= nir_cursor_before_block
;
926 return reduce_cursor(cursor
);
929 case nir_cursor_after_instr
:
930 if (nir_instr_next(cursor
.instr
) == NULL
) {
931 /* This is the last instruction, switch to after block */
932 cursor
.option
= nir_cursor_after_block
;
933 cursor
.block
= cursor
.instr
->block
;
938 unreachable("Inavlid cursor option");
943 nir_cursors_equal(nir_cursor a
, nir_cursor b
)
945 /* Reduced cursors should be unique */
946 a
= reduce_cursor(a
);
947 b
= reduce_cursor(b
);
949 return a
.block
== b
.block
&& a
.option
== b
.option
;
953 add_use_cb(nir_src
*src
, void *state
)
955 nir_instr
*instr
= state
;
957 src
->parent_instr
= instr
;
958 list_addtail(&src
->use_link
,
959 src
->is_ssa
? &src
->ssa
->uses
: &src
->reg
.reg
->uses
);
965 add_ssa_def_cb(nir_ssa_def
*def
, void *state
)
967 nir_instr
*instr
= state
;
969 if (instr
->block
&& def
->index
== UINT_MAX
) {
970 nir_function_impl
*impl
=
971 nir_cf_node_get_function(&instr
->block
->cf_node
);
973 def
->index
= impl
->ssa_alloc
++;
980 add_reg_def_cb(nir_dest
*dest
, void *state
)
982 nir_instr
*instr
= state
;
985 dest
->reg
.parent_instr
= instr
;
986 list_addtail(&dest
->reg
.def_link
, &dest
->reg
.reg
->defs
);
993 add_defs_uses(nir_instr
*instr
)
995 nir_foreach_src(instr
, add_use_cb
, instr
);
996 nir_foreach_dest(instr
, add_reg_def_cb
, instr
);
997 nir_foreach_ssa_def(instr
, add_ssa_def_cb
, instr
);
1001 nir_instr_insert(nir_cursor cursor
, nir_instr
*instr
)
1003 switch (cursor
.option
) {
1004 case nir_cursor_before_block
:
1005 /* Only allow inserting jumps into empty blocks. */
1006 if (instr
->type
== nir_instr_type_jump
)
1007 assert(exec_list_is_empty(&cursor
.block
->instr_list
));
1009 instr
->block
= cursor
.block
;
1010 add_defs_uses(instr
);
1011 exec_list_push_head(&cursor
.block
->instr_list
, &instr
->node
);
1013 case nir_cursor_after_block
: {
1014 /* Inserting instructions after a jump is illegal. */
1015 nir_instr
*last
= nir_block_last_instr(cursor
.block
);
1016 assert(last
== NULL
|| last
->type
!= nir_instr_type_jump
);
1019 instr
->block
= cursor
.block
;
1020 add_defs_uses(instr
);
1021 exec_list_push_tail(&cursor
.block
->instr_list
, &instr
->node
);
1024 case nir_cursor_before_instr
:
1025 assert(instr
->type
!= nir_instr_type_jump
);
1026 instr
->block
= cursor
.instr
->block
;
1027 add_defs_uses(instr
);
1028 exec_node_insert_node_before(&cursor
.instr
->node
, &instr
->node
);
1030 case nir_cursor_after_instr
:
1031 /* Inserting instructions after a jump is illegal. */
1032 assert(cursor
.instr
->type
!= nir_instr_type_jump
);
1034 /* Only allow inserting jumps at the end of the block. */
1035 if (instr
->type
== nir_instr_type_jump
)
1036 assert(cursor
.instr
== nir_block_last_instr(cursor
.instr
->block
));
1038 instr
->block
= cursor
.instr
->block
;
1039 add_defs_uses(instr
);
1040 exec_node_insert_after(&cursor
.instr
->node
, &instr
->node
);
1044 if (instr
->type
== nir_instr_type_jump
)
1045 nir_handle_add_jump(instr
->block
);
1049 src_is_valid(const nir_src
*src
)
1051 return src
->is_ssa
? (src
->ssa
!= NULL
) : (src
->reg
.reg
!= NULL
);
1055 remove_use_cb(nir_src
*src
, void *state
)
1059 if (src_is_valid(src
))
1060 list_del(&src
->use_link
);
1066 remove_def_cb(nir_dest
*dest
, void *state
)
1071 list_del(&dest
->reg
.def_link
);
1077 remove_defs_uses(nir_instr
*instr
)
1079 nir_foreach_dest(instr
, remove_def_cb
, instr
);
1080 nir_foreach_src(instr
, remove_use_cb
, instr
);
1083 void nir_instr_remove(nir_instr
*instr
)
1085 remove_defs_uses(instr
);
1086 exec_node_remove(&instr
->node
);
1088 if (instr
->type
== nir_instr_type_jump
) {
1089 nir_jump_instr
*jump_instr
= nir_instr_as_jump(instr
);
1090 nir_handle_remove_jump(instr
->block
, jump_instr
->type
);
1097 nir_index_local_regs(nir_function_impl
*impl
)
1100 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
1101 reg
->index
= index
++;
1103 impl
->reg_alloc
= index
;
1107 nir_index_global_regs(nir_shader
*shader
)
1110 foreach_list_typed(nir_register
, reg
, node
, &shader
->registers
) {
1111 reg
->index
= index
++;
1113 shader
->reg_alloc
= index
;
1117 visit_alu_dest(nir_alu_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
1119 return cb(&instr
->dest
.dest
, state
);
1123 visit_intrinsic_dest(nir_intrinsic_instr
*instr
, nir_foreach_dest_cb cb
,
1126 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
1127 return cb(&instr
->dest
, state
);
1133 visit_texture_dest(nir_tex_instr
*instr
, nir_foreach_dest_cb cb
,
1136 return cb(&instr
->dest
, state
);
1140 visit_phi_dest(nir_phi_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
1142 return cb(&instr
->dest
, state
);
1146 visit_parallel_copy_dest(nir_parallel_copy_instr
*instr
,
1147 nir_foreach_dest_cb cb
, void *state
)
1149 nir_foreach_parallel_copy_entry(entry
, instr
) {
1150 if (!cb(&entry
->dest
, state
))
1158 nir_foreach_dest(nir_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
1160 switch (instr
->type
) {
1161 case nir_instr_type_alu
:
1162 return visit_alu_dest(nir_instr_as_alu(instr
), cb
, state
);
1163 case nir_instr_type_intrinsic
:
1164 return visit_intrinsic_dest(nir_instr_as_intrinsic(instr
), cb
, state
);
1165 case nir_instr_type_tex
:
1166 return visit_texture_dest(nir_instr_as_tex(instr
), cb
, state
);
1167 case nir_instr_type_phi
:
1168 return visit_phi_dest(nir_instr_as_phi(instr
), cb
, state
);
1169 case nir_instr_type_parallel_copy
:
1170 return visit_parallel_copy_dest(nir_instr_as_parallel_copy(instr
),
1173 case nir_instr_type_load_const
:
1174 case nir_instr_type_ssa_undef
:
1175 case nir_instr_type_call
:
1176 case nir_instr_type_jump
:
1180 unreachable("Invalid instruction type");
1187 struct foreach_ssa_def_state
{
1188 nir_foreach_ssa_def_cb cb
;
1193 nir_ssa_def_visitor(nir_dest
*dest
, void *void_state
)
1195 struct foreach_ssa_def_state
*state
= void_state
;
1198 return state
->cb(&dest
->ssa
, state
->client_state
);
1204 nir_foreach_ssa_def(nir_instr
*instr
, nir_foreach_ssa_def_cb cb
, void *state
)
1206 switch (instr
->type
) {
1207 case nir_instr_type_alu
:
1208 case nir_instr_type_tex
:
1209 case nir_instr_type_intrinsic
:
1210 case nir_instr_type_phi
:
1211 case nir_instr_type_parallel_copy
: {
1212 struct foreach_ssa_def_state foreach_state
= {cb
, state
};
1213 return nir_foreach_dest(instr
, nir_ssa_def_visitor
, &foreach_state
);
1216 case nir_instr_type_load_const
:
1217 return cb(&nir_instr_as_load_const(instr
)->def
, state
);
1218 case nir_instr_type_ssa_undef
:
1219 return cb(&nir_instr_as_ssa_undef(instr
)->def
, state
);
1220 case nir_instr_type_call
:
1221 case nir_instr_type_jump
:
1224 unreachable("Invalid instruction type");
1229 visit_src(nir_src
*src
, nir_foreach_src_cb cb
, void *state
)
1231 if (!cb(src
, state
))
1233 if (!src
->is_ssa
&& src
->reg
.indirect
)
1234 return cb(src
->reg
.indirect
, state
);
1239 visit_deref_array_src(nir_deref_array
*deref
, nir_foreach_src_cb cb
,
1242 if (deref
->deref_array_type
== nir_deref_array_type_indirect
)
1243 return visit_src(&deref
->indirect
, cb
, state
);
1248 visit_deref_src(nir_deref_var
*deref
, nir_foreach_src_cb cb
, void *state
)
1250 nir_deref
*cur
= &deref
->deref
;
1251 while (cur
!= NULL
) {
1252 if (cur
->deref_type
== nir_deref_type_array
) {
1253 if (!visit_deref_array_src(nir_deref_as_array(cur
), cb
, state
))
1264 visit_alu_src(nir_alu_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1266 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1267 if (!visit_src(&instr
->src
[i
].src
, cb
, state
))
1274 visit_tex_src(nir_tex_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1276 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++) {
1277 if (!visit_src(&instr
->src
[i
].src
, cb
, state
))
1281 if (instr
->texture
!= NULL
) {
1282 if (!visit_deref_src(instr
->texture
, cb
, state
))
1286 if (instr
->sampler
!= NULL
) {
1287 if (!visit_deref_src(instr
->sampler
, cb
, state
))
1295 visit_intrinsic_src(nir_intrinsic_instr
*instr
, nir_foreach_src_cb cb
,
1298 unsigned num_srcs
= nir_intrinsic_infos
[instr
->intrinsic
].num_srcs
;
1299 for (unsigned i
= 0; i
< num_srcs
; i
++) {
1300 if (!visit_src(&instr
->src
[i
], cb
, state
))
1305 nir_intrinsic_infos
[instr
->intrinsic
].num_variables
;
1306 for (unsigned i
= 0; i
< num_vars
; i
++) {
1307 if (!visit_deref_src(instr
->variables
[i
], cb
, state
))
1315 visit_phi_src(nir_phi_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1317 nir_foreach_phi_src(src
, instr
) {
1318 if (!visit_src(&src
->src
, cb
, state
))
1326 visit_parallel_copy_src(nir_parallel_copy_instr
*instr
,
1327 nir_foreach_src_cb cb
, void *state
)
1329 nir_foreach_parallel_copy_entry(entry
, instr
) {
1330 if (!visit_src(&entry
->src
, cb
, state
))
1339 nir_foreach_src_cb cb
;
1340 } visit_dest_indirect_state
;
1343 visit_dest_indirect(nir_dest
*dest
, void *_state
)
1345 visit_dest_indirect_state
*state
= (visit_dest_indirect_state
*) _state
;
1347 if (!dest
->is_ssa
&& dest
->reg
.indirect
)
1348 return state
->cb(dest
->reg
.indirect
, state
->state
);
1354 nir_foreach_src(nir_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1356 switch (instr
->type
) {
1357 case nir_instr_type_alu
:
1358 if (!visit_alu_src(nir_instr_as_alu(instr
), cb
, state
))
1361 case nir_instr_type_intrinsic
:
1362 if (!visit_intrinsic_src(nir_instr_as_intrinsic(instr
), cb
, state
))
1365 case nir_instr_type_tex
:
1366 if (!visit_tex_src(nir_instr_as_tex(instr
), cb
, state
))
1369 case nir_instr_type_call
:
1370 /* Call instructions have no regular sources */
1372 case nir_instr_type_load_const
:
1373 /* Constant load instructions have no regular sources */
1375 case nir_instr_type_phi
:
1376 if (!visit_phi_src(nir_instr_as_phi(instr
), cb
, state
))
1379 case nir_instr_type_parallel_copy
:
1380 if (!visit_parallel_copy_src(nir_instr_as_parallel_copy(instr
),
1384 case nir_instr_type_jump
:
1385 case nir_instr_type_ssa_undef
:
1389 unreachable("Invalid instruction type");
1393 visit_dest_indirect_state dest_state
;
1394 dest_state
.state
= state
;
1396 return nir_foreach_dest(instr
, visit_dest_indirect
, &dest_state
);
1400 nir_src_as_const_value(nir_src src
)
1405 if (src
.ssa
->parent_instr
->type
!= nir_instr_type_load_const
)
1408 nir_load_const_instr
*load
= nir_instr_as_load_const(src
.ssa
->parent_instr
);
1410 return &load
->value
;
1414 * Returns true if the source is known to be dynamically uniform. Otherwise it
1415 * returns false which means it may or may not be dynamically uniform but it
1416 * can't be determined.
1419 nir_src_is_dynamically_uniform(nir_src src
)
1424 /* Constants are trivially dynamically uniform */
1425 if (src
.ssa
->parent_instr
->type
== nir_instr_type_load_const
)
1428 /* As are uniform variables */
1429 if (src
.ssa
->parent_instr
->type
== nir_instr_type_intrinsic
) {
1430 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(src
.ssa
->parent_instr
);
1432 if (intr
->intrinsic
== nir_intrinsic_load_uniform
)
1436 /* XXX: this could have many more tests, such as when a sampler function is
1437 * called with dynamically uniform arguments.
1443 src_remove_all_uses(nir_src
*src
)
1445 for (; src
; src
= src
->is_ssa
? NULL
: src
->reg
.indirect
) {
1446 if (!src_is_valid(src
))
1449 list_del(&src
->use_link
);
1454 src_add_all_uses(nir_src
*src
, nir_instr
*parent_instr
, nir_if
*parent_if
)
1456 for (; src
; src
= src
->is_ssa
? NULL
: src
->reg
.indirect
) {
1457 if (!src_is_valid(src
))
1461 src
->parent_instr
= parent_instr
;
1463 list_addtail(&src
->use_link
, &src
->ssa
->uses
);
1465 list_addtail(&src
->use_link
, &src
->reg
.reg
->uses
);
1468 src
->parent_if
= parent_if
;
1470 list_addtail(&src
->use_link
, &src
->ssa
->if_uses
);
1472 list_addtail(&src
->use_link
, &src
->reg
.reg
->if_uses
);
1478 nir_instr_rewrite_src(nir_instr
*instr
, nir_src
*src
, nir_src new_src
)
1480 assert(!src_is_valid(src
) || src
->parent_instr
== instr
);
1482 src_remove_all_uses(src
);
1484 src_add_all_uses(src
, instr
, NULL
);
1488 nir_instr_move_src(nir_instr
*dest_instr
, nir_src
*dest
, nir_src
*src
)
1490 assert(!src_is_valid(dest
) || dest
->parent_instr
== dest_instr
);
1492 src_remove_all_uses(dest
);
1493 src_remove_all_uses(src
);
1495 *src
= NIR_SRC_INIT
;
1496 src_add_all_uses(dest
, dest_instr
, NULL
);
1500 nir_if_rewrite_condition(nir_if
*if_stmt
, nir_src new_src
)
1502 nir_src
*src
= &if_stmt
->condition
;
1503 assert(!src_is_valid(src
) || src
->parent_if
== if_stmt
);
1505 src_remove_all_uses(src
);
1507 src_add_all_uses(src
, NULL
, if_stmt
);
1511 nir_instr_rewrite_dest(nir_instr
*instr
, nir_dest
*dest
, nir_dest new_dest
)
1514 /* We can only overwrite an SSA destination if it has no uses. */
1515 assert(list_empty(&dest
->ssa
.uses
) && list_empty(&dest
->ssa
.if_uses
));
1517 list_del(&dest
->reg
.def_link
);
1518 if (dest
->reg
.indirect
)
1519 src_remove_all_uses(dest
->reg
.indirect
);
1522 /* We can't re-write with an SSA def */
1523 assert(!new_dest
.is_ssa
);
1525 nir_dest_copy(dest
, &new_dest
, instr
);
1527 dest
->reg
.parent_instr
= instr
;
1528 list_addtail(&dest
->reg
.def_link
, &new_dest
.reg
.reg
->defs
);
1530 if (dest
->reg
.indirect
)
1531 src_add_all_uses(dest
->reg
.indirect
, instr
, NULL
);
1535 nir_instr_rewrite_deref(nir_instr
*instr
, nir_deref_var
**deref
,
1536 nir_deref_var
*new_deref
)
1539 visit_deref_src(*deref
, remove_use_cb
, NULL
);
1544 visit_deref_src(*deref
, add_use_cb
, instr
);
1547 /* note: does *not* take ownership of 'name' */
1549 nir_ssa_def_init(nir_instr
*instr
, nir_ssa_def
*def
,
1550 unsigned num_components
,
1551 unsigned bit_size
, const char *name
)
1553 def
->name
= ralloc_strdup(instr
, name
);
1554 def
->parent_instr
= instr
;
1555 list_inithead(&def
->uses
);
1556 list_inithead(&def
->if_uses
);
1557 def
->num_components
= num_components
;
1558 def
->bit_size
= bit_size
;
1561 nir_function_impl
*impl
=
1562 nir_cf_node_get_function(&instr
->block
->cf_node
);
1564 def
->index
= impl
->ssa_alloc
++;
1566 def
->index
= UINT_MAX
;
1570 /* note: does *not* take ownership of 'name' */
1572 nir_ssa_dest_init(nir_instr
*instr
, nir_dest
*dest
,
1573 unsigned num_components
, unsigned bit_size
,
1576 dest
->is_ssa
= true;
1577 nir_ssa_def_init(instr
, &dest
->ssa
, num_components
, bit_size
, name
);
1581 nir_ssa_def_rewrite_uses(nir_ssa_def
*def
, nir_src new_src
)
1583 assert(!new_src
.is_ssa
|| def
!= new_src
.ssa
);
1585 nir_foreach_use_safe(use_src
, def
)
1586 nir_instr_rewrite_src(use_src
->parent_instr
, use_src
, new_src
);
1588 nir_foreach_if_use_safe(use_src
, def
)
1589 nir_if_rewrite_condition(use_src
->parent_if
, new_src
);
1593 is_instr_between(nir_instr
*start
, nir_instr
*end
, nir_instr
*between
)
1595 assert(start
->block
== end
->block
);
1597 if (between
->block
!= start
->block
)
1600 /* Search backwards looking for "between" */
1601 while (start
!= end
) {
1605 end
= nir_instr_prev(end
);
1612 /* Replaces all uses of the given SSA def with the given source but only if
1613 * the use comes after the after_me instruction. This can be useful if you
1614 * are emitting code to fix up the result of some instruction: you can freely
1615 * use the result in that code and then call rewrite_uses_after and pass the
1616 * last fixup instruction as after_me and it will replace all of the uses you
1617 * want without touching the fixup code.
1619 * This function assumes that after_me is in the same block as
1620 * def->parent_instr and that after_me comes after def->parent_instr.
1623 nir_ssa_def_rewrite_uses_after(nir_ssa_def
*def
, nir_src new_src
,
1624 nir_instr
*after_me
)
1626 assert(!new_src
.is_ssa
|| def
!= new_src
.ssa
);
1628 nir_foreach_use_safe(use_src
, def
) {
1629 assert(use_src
->parent_instr
!= def
->parent_instr
);
1630 /* Since def already dominates all of its uses, the only way a use can
1631 * not be dominated by after_me is if it is between def and after_me in
1632 * the instruction list.
1634 if (!is_instr_between(def
->parent_instr
, after_me
, use_src
->parent_instr
))
1635 nir_instr_rewrite_src(use_src
->parent_instr
, use_src
, new_src
);
1638 nir_foreach_if_use_safe(use_src
, def
)
1639 nir_if_rewrite_condition(use_src
->parent_if
, new_src
);
1643 nir_ssa_def_components_read(nir_ssa_def
*def
)
1645 uint8_t read_mask
= 0;
1646 nir_foreach_use(use
, def
) {
1647 if (use
->parent_instr
->type
== nir_instr_type_alu
) {
1648 nir_alu_instr
*alu
= nir_instr_as_alu(use
->parent_instr
);
1649 nir_alu_src
*alu_src
= exec_node_data(nir_alu_src
, use
, src
);
1650 int src_idx
= alu_src
- &alu
->src
[0];
1651 assert(src_idx
>= 0 && src_idx
< nir_op_infos
[alu
->op
].num_inputs
);
1653 for (unsigned c
= 0; c
< 4; c
++) {
1654 if (!nir_alu_instr_channel_used(alu
, src_idx
, c
))
1657 read_mask
|= (1 << alu_src
->swizzle
[c
]);
1660 return (1 << def
->num_components
) - 1;
1668 nir_block_cf_tree_next(nir_block
*block
)
1670 if (block
== NULL
) {
1671 /* nir_foreach_block_safe() will call this function on a NULL block
1672 * after the last iteration, but it won't use the result so just return
1678 nir_cf_node
*cf_next
= nir_cf_node_next(&block
->cf_node
);
1680 return nir_cf_node_cf_tree_first(cf_next
);
1682 nir_cf_node
*parent
= block
->cf_node
.parent
;
1684 switch (parent
->type
) {
1685 case nir_cf_node_if
: {
1686 /* Are we at the end of the if? Go to the beginning of the else */
1687 nir_if
*if_stmt
= nir_cf_node_as_if(parent
);
1688 if (block
== nir_if_last_then_block(if_stmt
))
1689 return nir_if_first_else_block(if_stmt
);
1691 assert(block
== nir_if_last_else_block(if_stmt
));
1695 case nir_cf_node_loop
:
1696 return nir_cf_node_as_block(nir_cf_node_next(parent
));
1698 case nir_cf_node_function
:
1702 unreachable("unknown cf node type");
1707 nir_block_cf_tree_prev(nir_block
*block
)
1709 if (block
== NULL
) {
1710 /* do this for consistency with nir_block_cf_tree_next() */
1714 nir_cf_node
*cf_prev
= nir_cf_node_prev(&block
->cf_node
);
1716 return nir_cf_node_cf_tree_last(cf_prev
);
1718 nir_cf_node
*parent
= block
->cf_node
.parent
;
1720 switch (parent
->type
) {
1721 case nir_cf_node_if
: {
1722 /* Are we at the beginning of the else? Go to the end of the if */
1723 nir_if
*if_stmt
= nir_cf_node_as_if(parent
);
1724 if (block
== nir_if_first_else_block(if_stmt
))
1725 return nir_if_last_then_block(if_stmt
);
1727 assert(block
== nir_if_first_then_block(if_stmt
));
1731 case nir_cf_node_loop
:
1732 return nir_cf_node_as_block(nir_cf_node_prev(parent
));
1734 case nir_cf_node_function
:
1738 unreachable("unknown cf node type");
1742 nir_block
*nir_cf_node_cf_tree_first(nir_cf_node
*node
)
1744 switch (node
->type
) {
1745 case nir_cf_node_function
: {
1746 nir_function_impl
*impl
= nir_cf_node_as_function(node
);
1747 return nir_start_block(impl
);
1750 case nir_cf_node_if
: {
1751 nir_if
*if_stmt
= nir_cf_node_as_if(node
);
1752 return nir_if_first_then_block(if_stmt
);
1755 case nir_cf_node_loop
: {
1756 nir_loop
*loop
= nir_cf_node_as_loop(node
);
1757 return nir_loop_first_block(loop
);
1760 case nir_cf_node_block
: {
1761 return nir_cf_node_as_block(node
);
1765 unreachable("unknown node type");
1769 nir_block
*nir_cf_node_cf_tree_last(nir_cf_node
*node
)
1771 switch (node
->type
) {
1772 case nir_cf_node_function
: {
1773 nir_function_impl
*impl
= nir_cf_node_as_function(node
);
1774 return nir_impl_last_block(impl
);
1777 case nir_cf_node_if
: {
1778 nir_if
*if_stmt
= nir_cf_node_as_if(node
);
1779 return nir_if_last_else_block(if_stmt
);
1782 case nir_cf_node_loop
: {
1783 nir_loop
*loop
= nir_cf_node_as_loop(node
);
1784 return nir_loop_last_block(loop
);
1787 case nir_cf_node_block
: {
1788 return nir_cf_node_as_block(node
);
1792 unreachable("unknown node type");
1796 nir_block
*nir_cf_node_cf_tree_next(nir_cf_node
*node
)
1798 if (node
->type
== nir_cf_node_block
)
1799 return nir_block_cf_tree_next(nir_cf_node_as_block(node
));
1800 else if (node
->type
== nir_cf_node_function
)
1803 return nir_cf_node_as_block(nir_cf_node_next(node
));
1807 nir_block_get_following_if(nir_block
*block
)
1809 if (exec_node_is_tail_sentinel(&block
->cf_node
.node
))
1812 if (nir_cf_node_is_last(&block
->cf_node
))
1815 nir_cf_node
*next_node
= nir_cf_node_next(&block
->cf_node
);
1817 if (next_node
->type
!= nir_cf_node_if
)
1820 return nir_cf_node_as_if(next_node
);
1824 nir_block_get_following_loop(nir_block
*block
)
1826 if (exec_node_is_tail_sentinel(&block
->cf_node
.node
))
1829 if (nir_cf_node_is_last(&block
->cf_node
))
1832 nir_cf_node
*next_node
= nir_cf_node_next(&block
->cf_node
);
1834 if (next_node
->type
!= nir_cf_node_loop
)
1837 return nir_cf_node_as_loop(next_node
);
1841 nir_index_blocks(nir_function_impl
*impl
)
1845 if (impl
->valid_metadata
& nir_metadata_block_index
)
1848 nir_foreach_block(block
, impl
) {
1849 block
->index
= index
++;
1852 impl
->num_blocks
= index
;
1856 index_ssa_def_cb(nir_ssa_def
*def
, void *state
)
1858 unsigned *index
= (unsigned *) state
;
1859 def
->index
= (*index
)++;
1865 * The indices are applied top-to-bottom which has the very nice property
1866 * that, if A dominates B, then A->index <= B->index.
1869 nir_index_ssa_defs(nir_function_impl
*impl
)
1873 nir_foreach_block(block
, impl
) {
1874 nir_foreach_instr(instr
, block
)
1875 nir_foreach_ssa_def(instr
, index_ssa_def_cb
, &index
);
1878 impl
->ssa_alloc
= index
;
1882 * The indices are applied top-to-bottom which has the very nice property
1883 * that, if A dominates B, then A->index <= B->index.
1886 nir_index_instrs(nir_function_impl
*impl
)
1890 nir_foreach_block(block
, impl
) {
1891 nir_foreach_instr(instr
, block
)
1892 instr
->index
= index
++;
1899 nir_intrinsic_from_system_value(gl_system_value val
)
1902 case SYSTEM_VALUE_VERTEX_ID
:
1903 return nir_intrinsic_load_vertex_id
;
1904 case SYSTEM_VALUE_INSTANCE_ID
:
1905 return nir_intrinsic_load_instance_id
;
1906 case SYSTEM_VALUE_DRAW_ID
:
1907 return nir_intrinsic_load_draw_id
;
1908 case SYSTEM_VALUE_BASE_INSTANCE
:
1909 return nir_intrinsic_load_base_instance
;
1910 case SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
:
1911 return nir_intrinsic_load_vertex_id_zero_base
;
1912 case SYSTEM_VALUE_BASE_VERTEX
:
1913 return nir_intrinsic_load_base_vertex
;
1914 case SYSTEM_VALUE_INVOCATION_ID
:
1915 return nir_intrinsic_load_invocation_id
;
1916 case SYSTEM_VALUE_FRAG_COORD
:
1917 return nir_intrinsic_load_frag_coord
;
1918 case SYSTEM_VALUE_FRONT_FACE
:
1919 return nir_intrinsic_load_front_face
;
1920 case SYSTEM_VALUE_SAMPLE_ID
:
1921 return nir_intrinsic_load_sample_id
;
1922 case SYSTEM_VALUE_SAMPLE_POS
:
1923 return nir_intrinsic_load_sample_pos
;
1924 case SYSTEM_VALUE_SAMPLE_MASK_IN
:
1925 return nir_intrinsic_load_sample_mask_in
;
1926 case SYSTEM_VALUE_LOCAL_INVOCATION_ID
:
1927 return nir_intrinsic_load_local_invocation_id
;
1928 case SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
:
1929 return nir_intrinsic_load_local_invocation_index
;
1930 case SYSTEM_VALUE_WORK_GROUP_ID
:
1931 return nir_intrinsic_load_work_group_id
;
1932 case SYSTEM_VALUE_NUM_WORK_GROUPS
:
1933 return nir_intrinsic_load_num_work_groups
;
1934 case SYSTEM_VALUE_PRIMITIVE_ID
:
1935 return nir_intrinsic_load_primitive_id
;
1936 case SYSTEM_VALUE_TESS_COORD
:
1937 return nir_intrinsic_load_tess_coord
;
1938 case SYSTEM_VALUE_TESS_LEVEL_OUTER
:
1939 return nir_intrinsic_load_tess_level_outer
;
1940 case SYSTEM_VALUE_TESS_LEVEL_INNER
:
1941 return nir_intrinsic_load_tess_level_inner
;
1942 case SYSTEM_VALUE_VERTICES_IN
:
1943 return nir_intrinsic_load_patch_vertices_in
;
1944 case SYSTEM_VALUE_HELPER_INVOCATION
:
1945 return nir_intrinsic_load_helper_invocation
;
1946 case SYSTEM_VALUE_VIEW_INDEX
:
1947 return nir_intrinsic_load_view_index
;
1948 case SYSTEM_VALUE_SUBGROUP_SIZE
:
1949 return nir_intrinsic_load_subgroup_size
;
1950 case SYSTEM_VALUE_SUBGROUP_INVOCATION
:
1951 return nir_intrinsic_load_subgroup_invocation
;
1952 case SYSTEM_VALUE_SUBGROUP_EQ_MASK
:
1953 return nir_intrinsic_load_subgroup_eq_mask
;
1954 case SYSTEM_VALUE_SUBGROUP_GE_MASK
:
1955 return nir_intrinsic_load_subgroup_ge_mask
;
1956 case SYSTEM_VALUE_SUBGROUP_GT_MASK
:
1957 return nir_intrinsic_load_subgroup_gt_mask
;
1958 case SYSTEM_VALUE_SUBGROUP_LE_MASK
:
1959 return nir_intrinsic_load_subgroup_le_mask
;
1960 case SYSTEM_VALUE_SUBGROUP_LT_MASK
:
1961 return nir_intrinsic_load_subgroup_lt_mask
;
1963 unreachable("system value does not directly correspond to intrinsic");
1968 nir_system_value_from_intrinsic(nir_intrinsic_op intrin
)
1971 case nir_intrinsic_load_vertex_id
:
1972 return SYSTEM_VALUE_VERTEX_ID
;
1973 case nir_intrinsic_load_instance_id
:
1974 return SYSTEM_VALUE_INSTANCE_ID
;
1975 case nir_intrinsic_load_draw_id
:
1976 return SYSTEM_VALUE_DRAW_ID
;
1977 case nir_intrinsic_load_base_instance
:
1978 return SYSTEM_VALUE_BASE_INSTANCE
;
1979 case nir_intrinsic_load_vertex_id_zero_base
:
1980 return SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
;
1981 case nir_intrinsic_load_base_vertex
:
1982 return SYSTEM_VALUE_BASE_VERTEX
;
1983 case nir_intrinsic_load_invocation_id
:
1984 return SYSTEM_VALUE_INVOCATION_ID
;
1985 case nir_intrinsic_load_frag_coord
:
1986 return SYSTEM_VALUE_FRAG_COORD
;
1987 case nir_intrinsic_load_front_face
:
1988 return SYSTEM_VALUE_FRONT_FACE
;
1989 case nir_intrinsic_load_sample_id
:
1990 return SYSTEM_VALUE_SAMPLE_ID
;
1991 case nir_intrinsic_load_sample_pos
:
1992 return SYSTEM_VALUE_SAMPLE_POS
;
1993 case nir_intrinsic_load_sample_mask_in
:
1994 return SYSTEM_VALUE_SAMPLE_MASK_IN
;
1995 case nir_intrinsic_load_local_invocation_id
:
1996 return SYSTEM_VALUE_LOCAL_INVOCATION_ID
;
1997 case nir_intrinsic_load_local_invocation_index
:
1998 return SYSTEM_VALUE_LOCAL_INVOCATION_INDEX
;
1999 case nir_intrinsic_load_num_work_groups
:
2000 return SYSTEM_VALUE_NUM_WORK_GROUPS
;
2001 case nir_intrinsic_load_work_group_id
:
2002 return SYSTEM_VALUE_WORK_GROUP_ID
;
2003 case nir_intrinsic_load_primitive_id
:
2004 return SYSTEM_VALUE_PRIMITIVE_ID
;
2005 case nir_intrinsic_load_tess_coord
:
2006 return SYSTEM_VALUE_TESS_COORD
;
2007 case nir_intrinsic_load_tess_level_outer
:
2008 return SYSTEM_VALUE_TESS_LEVEL_OUTER
;
2009 case nir_intrinsic_load_tess_level_inner
:
2010 return SYSTEM_VALUE_TESS_LEVEL_INNER
;
2011 case nir_intrinsic_load_patch_vertices_in
:
2012 return SYSTEM_VALUE_VERTICES_IN
;
2013 case nir_intrinsic_load_helper_invocation
:
2014 return SYSTEM_VALUE_HELPER_INVOCATION
;
2015 case nir_intrinsic_load_view_index
:
2016 return SYSTEM_VALUE_VIEW_INDEX
;
2017 case nir_intrinsic_load_subgroup_size
:
2018 return SYSTEM_VALUE_SUBGROUP_SIZE
;
2019 case nir_intrinsic_load_subgroup_invocation
:
2020 return SYSTEM_VALUE_SUBGROUP_INVOCATION
;
2021 case nir_intrinsic_load_subgroup_eq_mask
:
2022 return SYSTEM_VALUE_SUBGROUP_EQ_MASK
;
2023 case nir_intrinsic_load_subgroup_ge_mask
:
2024 return SYSTEM_VALUE_SUBGROUP_GE_MASK
;
2025 case nir_intrinsic_load_subgroup_gt_mask
:
2026 return SYSTEM_VALUE_SUBGROUP_GT_MASK
;
2027 case nir_intrinsic_load_subgroup_le_mask
:
2028 return SYSTEM_VALUE_SUBGROUP_LE_MASK
;
2029 case nir_intrinsic_load_subgroup_lt_mask
:
2030 return SYSTEM_VALUE_SUBGROUP_LT_MASK
;
2032 unreachable("intrinsic doesn't produce a system value");