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
)
37 nir_shader
*shader
= ralloc(mem_ctx
, nir_shader
);
39 exec_list_make_empty(&shader
->uniforms
);
40 exec_list_make_empty(&shader
->inputs
);
41 exec_list_make_empty(&shader
->outputs
);
43 shader
->options
= options
;
44 memset(&shader
->info
, 0, sizeof(shader
->info
));
46 exec_list_make_empty(&shader
->functions
);
47 exec_list_make_empty(&shader
->registers
);
48 exec_list_make_empty(&shader
->globals
);
49 exec_list_make_empty(&shader
->system_values
);
50 shader
->reg_alloc
= 0;
52 shader
->num_inputs
= 0;
53 shader
->num_outputs
= 0;
54 shader
->num_uniforms
= 0;
56 shader
->stage
= stage
;
62 reg_create(void *mem_ctx
, struct exec_list
*list
)
64 nir_register
*reg
= ralloc(mem_ctx
, nir_register
);
66 list_inithead(®
->uses
);
67 list_inithead(®
->defs
);
68 list_inithead(®
->if_uses
);
70 reg
->num_components
= 0;
71 reg
->num_array_elems
= 0;
72 reg
->is_packed
= false;
75 exec_list_push_tail(list
, ®
->node
);
81 nir_global_reg_create(nir_shader
*shader
)
83 nir_register
*reg
= reg_create(shader
, &shader
->registers
);
84 reg
->index
= shader
->reg_alloc
++;
85 reg
->is_global
= true;
91 nir_local_reg_create(nir_function_impl
*impl
)
93 nir_register
*reg
= reg_create(ralloc_parent(impl
), &impl
->registers
);
94 reg
->index
= impl
->reg_alloc
++;
95 reg
->is_global
= false;
101 nir_reg_remove(nir_register
*reg
)
103 exec_node_remove(®
->node
);
107 nir_shader_add_variable(nir_shader
*shader
, nir_variable
*var
)
109 switch (var
->data
.mode
) {
111 assert(!"nir_shader_add_variable cannot be used for local variables");
115 exec_list_push_tail(&shader
->globals
, &var
->node
);
118 case nir_var_shader_in
:
119 exec_list_push_tail(&shader
->inputs
, &var
->node
);
122 case nir_var_shader_out
:
123 exec_list_push_tail(&shader
->outputs
, &var
->node
);
126 case nir_var_uniform
:
127 case nir_var_shader_storage
:
128 exec_list_push_tail(&shader
->uniforms
, &var
->node
);
131 case nir_var_system_value
:
132 exec_list_push_tail(&shader
->system_values
, &var
->node
);
138 nir_variable_create(nir_shader
*shader
, nir_variable_mode mode
,
139 const struct glsl_type
*type
, const char *name
)
141 nir_variable
*var
= rzalloc(shader
, nir_variable
);
142 var
->name
= ralloc_strdup(var
, name
);
144 var
->data
.mode
= mode
;
146 if ((mode
== nir_var_shader_in
&& shader
->stage
!= MESA_SHADER_VERTEX
) ||
147 (mode
== nir_var_shader_out
&& shader
->stage
!= MESA_SHADER_FRAGMENT
))
148 var
->data
.interpolation
= INTERP_QUALIFIER_SMOOTH
;
150 if (mode
== nir_var_shader_in
|| mode
== nir_var_uniform
)
151 var
->data
.read_only
= true;
153 nir_shader_add_variable(shader
, var
);
159 nir_local_variable_create(nir_function_impl
*impl
,
160 const struct glsl_type
*type
, const char *name
)
162 nir_variable
*var
= rzalloc(impl
->overload
->function
->shader
, nir_variable
);
163 var
->name
= ralloc_strdup(var
, name
);
165 var
->data
.mode
= nir_var_local
;
167 nir_function_impl_add_variable(impl
, var
);
173 nir_function_create(nir_shader
*shader
, const char *name
)
175 nir_function
*func
= ralloc(shader
, nir_function
);
177 exec_list_push_tail(&shader
->functions
, &func
->node
);
178 exec_list_make_empty(&func
->overload_list
);
179 func
->name
= ralloc_strdup(func
, name
);
180 func
->shader
= shader
;
185 nir_function_overload
*
186 nir_function_overload_create(nir_function
*func
)
188 void *mem_ctx
= ralloc_parent(func
);
190 nir_function_overload
*overload
= ralloc(mem_ctx
, nir_function_overload
);
192 overload
->num_params
= 0;
193 overload
->params
= NULL
;
194 overload
->return_type
= glsl_void_type();
195 overload
->impl
= NULL
;
197 exec_list_push_tail(&func
->overload_list
, &overload
->node
);
198 overload
->function
= func
;
203 void nir_src_copy(nir_src
*dest
, const nir_src
*src
, void *mem_ctx
)
205 dest
->is_ssa
= src
->is_ssa
;
207 dest
->ssa
= src
->ssa
;
209 dest
->reg
.base_offset
= src
->reg
.base_offset
;
210 dest
->reg
.reg
= src
->reg
.reg
;
211 if (src
->reg
.indirect
) {
212 dest
->reg
.indirect
= ralloc(mem_ctx
, nir_src
);
213 nir_src_copy(dest
->reg
.indirect
, src
->reg
.indirect
, mem_ctx
);
215 dest
->reg
.indirect
= NULL
;
220 void nir_dest_copy(nir_dest
*dest
, const nir_dest
*src
, nir_instr
*instr
)
222 /* Copying an SSA definition makes no sense whatsoever. */
223 assert(!src
->is_ssa
);
225 dest
->is_ssa
= false;
227 dest
->reg
.base_offset
= src
->reg
.base_offset
;
228 dest
->reg
.reg
= src
->reg
.reg
;
229 if (src
->reg
.indirect
) {
230 dest
->reg
.indirect
= ralloc(instr
, nir_src
);
231 nir_src_copy(dest
->reg
.indirect
, src
->reg
.indirect
, instr
);
233 dest
->reg
.indirect
= NULL
;
238 nir_alu_src_copy(nir_alu_src
*dest
, const nir_alu_src
*src
,
239 nir_alu_instr
*instr
)
241 nir_src_copy(&dest
->src
, &src
->src
, &instr
->instr
);
242 dest
->abs
= src
->abs
;
243 dest
->negate
= src
->negate
;
244 for (unsigned i
= 0; i
< 4; i
++)
245 dest
->swizzle
[i
] = src
->swizzle
[i
];
249 nir_alu_dest_copy(nir_alu_dest
*dest
, const nir_alu_dest
*src
,
250 nir_alu_instr
*instr
)
252 nir_dest_copy(&dest
->dest
, &src
->dest
, &instr
->instr
);
253 dest
->write_mask
= src
->write_mask
;
254 dest
->saturate
= src
->saturate
;
259 cf_init(nir_cf_node
*node
, nir_cf_node_type type
)
261 exec_node_init(&node
->node
);
267 nir_function_impl_create(nir_function_overload
*overload
)
269 assert(overload
->impl
== NULL
);
271 void *mem_ctx
= ralloc_parent(overload
);
273 nir_function_impl
*impl
= ralloc(mem_ctx
, nir_function_impl
);
275 overload
->impl
= impl
;
276 impl
->overload
= overload
;
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(mem_ctx
);
292 nir_block
*end_block
= nir_block_create(mem_ctx
);
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_block_create(nir_shader
*shader
)
307 nir_block
*block
= ralloc(shader
, nir_block
);
309 cf_init(&block
->cf_node
, nir_cf_node_block
);
311 block
->successors
[0] = block
->successors
[1] = NULL
;
312 block
->predecessors
= _mesa_set_create(block
, _mesa_hash_pointer
,
313 _mesa_key_pointer_equal
);
314 block
->imm_dom
= NULL
;
315 block
->dom_frontier
= _mesa_set_create(block
, _mesa_hash_pointer
,
316 _mesa_key_pointer_equal
);
318 exec_list_make_empty(&block
->instr_list
);
324 src_init(nir_src
*src
)
328 src
->reg
.indirect
= NULL
;
329 src
->reg
.base_offset
= 0;
333 nir_if_create(nir_shader
*shader
)
335 nir_if
*if_stmt
= ralloc(shader
, nir_if
);
337 cf_init(&if_stmt
->cf_node
, nir_cf_node_if
);
338 src_init(&if_stmt
->condition
);
340 nir_block
*then
= nir_block_create(shader
);
341 exec_list_make_empty(&if_stmt
->then_list
);
342 exec_list_push_tail(&if_stmt
->then_list
, &then
->cf_node
.node
);
343 then
->cf_node
.parent
= &if_stmt
->cf_node
;
345 nir_block
*else_stmt
= nir_block_create(shader
);
346 exec_list_make_empty(&if_stmt
->else_list
);
347 exec_list_push_tail(&if_stmt
->else_list
, &else_stmt
->cf_node
.node
);
348 else_stmt
->cf_node
.parent
= &if_stmt
->cf_node
;
354 nir_loop_create(nir_shader
*shader
)
356 nir_loop
*loop
= ralloc(shader
, nir_loop
);
358 cf_init(&loop
->cf_node
, nir_cf_node_loop
);
360 nir_block
*body
= nir_block_create(shader
);
361 exec_list_make_empty(&loop
->body
);
362 exec_list_push_tail(&loop
->body
, &body
->cf_node
.node
);
363 body
->cf_node
.parent
= &loop
->cf_node
;
365 body
->successors
[0] = body
;
366 _mesa_set_add(body
->predecessors
, body
);
372 instr_init(nir_instr
*instr
, nir_instr_type type
)
376 exec_node_init(&instr
->node
);
380 dest_init(nir_dest
*dest
)
382 dest
->is_ssa
= false;
383 dest
->reg
.reg
= NULL
;
384 dest
->reg
.indirect
= NULL
;
385 dest
->reg
.base_offset
= 0;
389 alu_dest_init(nir_alu_dest
*dest
)
391 dest_init(&dest
->dest
);
392 dest
->saturate
= false;
393 dest
->write_mask
= 0xf;
397 alu_src_init(nir_alu_src
*src
)
400 src
->abs
= src
->negate
= false;
408 nir_alu_instr_create(nir_shader
*shader
, nir_op op
)
410 unsigned num_srcs
= nir_op_infos
[op
].num_inputs
;
411 nir_alu_instr
*instr
=
413 sizeof(nir_alu_instr
) + num_srcs
* sizeof(nir_alu_src
));
415 instr_init(&instr
->instr
, nir_instr_type_alu
);
417 alu_dest_init(&instr
->dest
);
418 for (unsigned i
= 0; i
< num_srcs
; i
++)
419 alu_src_init(&instr
->src
[i
]);
425 nir_jump_instr_create(nir_shader
*shader
, nir_jump_type type
)
427 nir_jump_instr
*instr
= ralloc(shader
, nir_jump_instr
);
428 instr_init(&instr
->instr
, nir_instr_type_jump
);
433 nir_load_const_instr
*
434 nir_load_const_instr_create(nir_shader
*shader
, unsigned num_components
)
436 nir_load_const_instr
*instr
= ralloc(shader
, nir_load_const_instr
);
437 instr_init(&instr
->instr
, nir_instr_type_load_const
);
439 nir_ssa_def_init(&instr
->instr
, &instr
->def
, num_components
, NULL
);
444 nir_intrinsic_instr
*
445 nir_intrinsic_instr_create(nir_shader
*shader
, nir_intrinsic_op op
)
447 unsigned num_srcs
= nir_intrinsic_infos
[op
].num_srcs
;
448 nir_intrinsic_instr
*instr
=
450 sizeof(nir_intrinsic_instr
) + num_srcs
* sizeof(nir_src
));
452 instr_init(&instr
->instr
, nir_instr_type_intrinsic
);
453 instr
->intrinsic
= op
;
455 if (nir_intrinsic_infos
[op
].has_dest
)
456 dest_init(&instr
->dest
);
458 for (unsigned i
= 0; i
< num_srcs
; i
++)
459 src_init(&instr
->src
[i
]);
465 nir_call_instr_create(nir_shader
*shader
, nir_function_overload
*callee
)
467 nir_call_instr
*instr
= ralloc(shader
, nir_call_instr
);
468 instr_init(&instr
->instr
, nir_instr_type_call
);
470 instr
->callee
= callee
;
471 instr
->num_params
= callee
->num_params
;
472 instr
->params
= ralloc_array(instr
, nir_deref_var
*, instr
->num_params
);
473 instr
->return_deref
= NULL
;
479 nir_tex_instr_create(nir_shader
*shader
, unsigned num_srcs
)
481 nir_tex_instr
*instr
= rzalloc(shader
, nir_tex_instr
);
482 instr_init(&instr
->instr
, nir_instr_type_tex
);
484 dest_init(&instr
->dest
);
486 instr
->num_srcs
= num_srcs
;
487 instr
->src
= ralloc_array(instr
, nir_tex_src
, num_srcs
);
488 for (unsigned i
= 0; i
< num_srcs
; i
++)
489 src_init(&instr
->src
[i
].src
);
491 instr
->sampler_index
= 0;
492 instr
->sampler_array_size
= 0;
493 instr
->sampler
= NULL
;
499 nir_phi_instr_create(nir_shader
*shader
)
501 nir_phi_instr
*instr
= ralloc(shader
, nir_phi_instr
);
502 instr_init(&instr
->instr
, nir_instr_type_phi
);
504 dest_init(&instr
->dest
);
505 exec_list_make_empty(&instr
->srcs
);
509 nir_parallel_copy_instr
*
510 nir_parallel_copy_instr_create(nir_shader
*shader
)
512 nir_parallel_copy_instr
*instr
= ralloc(shader
, nir_parallel_copy_instr
);
513 instr_init(&instr
->instr
, nir_instr_type_parallel_copy
);
515 exec_list_make_empty(&instr
->entries
);
520 nir_ssa_undef_instr
*
521 nir_ssa_undef_instr_create(nir_shader
*shader
, unsigned num_components
)
523 nir_ssa_undef_instr
*instr
= ralloc(shader
, nir_ssa_undef_instr
);
524 instr_init(&instr
->instr
, nir_instr_type_ssa_undef
);
526 nir_ssa_def_init(&instr
->instr
, &instr
->def
, num_components
, NULL
);
532 nir_deref_var_create(void *mem_ctx
, nir_variable
*var
)
534 nir_deref_var
*deref
= ralloc(mem_ctx
, nir_deref_var
);
535 deref
->deref
.deref_type
= nir_deref_type_var
;
536 deref
->deref
.child
= NULL
;
537 deref
->deref
.type
= var
->type
;
543 nir_deref_array_create(void *mem_ctx
)
545 nir_deref_array
*deref
= ralloc(mem_ctx
, nir_deref_array
);
546 deref
->deref
.deref_type
= nir_deref_type_array
;
547 deref
->deref
.child
= NULL
;
548 deref
->deref_array_type
= nir_deref_array_type_direct
;
549 src_init(&deref
->indirect
);
550 deref
->base_offset
= 0;
555 nir_deref_struct_create(void *mem_ctx
, unsigned field_index
)
557 nir_deref_struct
*deref
= ralloc(mem_ctx
, nir_deref_struct
);
558 deref
->deref
.deref_type
= nir_deref_type_struct
;
559 deref
->deref
.child
= NULL
;
560 deref
->index
= field_index
;
564 static nir_deref_var
*
565 copy_deref_var(void *mem_ctx
, nir_deref_var
*deref
)
567 nir_deref_var
*ret
= nir_deref_var_create(mem_ctx
, deref
->var
);
568 ret
->deref
.type
= deref
->deref
.type
;
569 if (deref
->deref
.child
)
570 ret
->deref
.child
= nir_copy_deref(ret
, deref
->deref
.child
);
574 static nir_deref_array
*
575 copy_deref_array(void *mem_ctx
, nir_deref_array
*deref
)
577 nir_deref_array
*ret
= nir_deref_array_create(mem_ctx
);
578 ret
->base_offset
= deref
->base_offset
;
579 ret
->deref_array_type
= deref
->deref_array_type
;
580 if (deref
->deref_array_type
== nir_deref_array_type_indirect
) {
581 nir_src_copy(&ret
->indirect
, &deref
->indirect
, mem_ctx
);
583 ret
->deref
.type
= deref
->deref
.type
;
584 if (deref
->deref
.child
)
585 ret
->deref
.child
= nir_copy_deref(ret
, deref
->deref
.child
);
589 static nir_deref_struct
*
590 copy_deref_struct(void *mem_ctx
, nir_deref_struct
*deref
)
592 nir_deref_struct
*ret
= nir_deref_struct_create(mem_ctx
, deref
->index
);
593 ret
->deref
.type
= deref
->deref
.type
;
594 if (deref
->deref
.child
)
595 ret
->deref
.child
= nir_copy_deref(ret
, deref
->deref
.child
);
600 nir_copy_deref(void *mem_ctx
, nir_deref
*deref
)
602 switch (deref
->deref_type
) {
603 case nir_deref_type_var
:
604 return ©_deref_var(mem_ctx
, nir_deref_as_var(deref
))->deref
;
605 case nir_deref_type_array
:
606 return ©_deref_array(mem_ctx
, nir_deref_as_array(deref
))->deref
;
607 case nir_deref_type_struct
:
608 return ©_deref_struct(mem_ctx
, nir_deref_as_struct(deref
))->deref
;
610 unreachable("Invalid dereference type");
616 /* Returns a load_const instruction that represents the constant
617 * initializer for the given deref chain. The caller is responsible for
618 * ensuring that there actually is a constant initializer.
620 nir_load_const_instr
*
621 nir_deref_get_const_initializer_load(nir_shader
*shader
, nir_deref_var
*deref
)
623 nir_constant
*constant
= deref
->var
->constant_initializer
;
626 const nir_deref
*tail
= &deref
->deref
;
627 unsigned matrix_offset
= 0;
628 while (tail
->child
) {
629 switch (tail
->child
->deref_type
) {
630 case nir_deref_type_array
: {
631 nir_deref_array
*arr
= nir_deref_as_array(tail
->child
);
632 assert(arr
->deref_array_type
== nir_deref_array_type_direct
);
633 if (glsl_type_is_matrix(tail
->type
)) {
634 assert(arr
->deref
.child
== NULL
);
635 matrix_offset
= arr
->base_offset
;
637 constant
= constant
->elements
[arr
->base_offset
];
642 case nir_deref_type_struct
: {
643 constant
= constant
->elements
[nir_deref_as_struct(tail
->child
)->index
];
648 unreachable("Invalid deref child type");
654 nir_load_const_instr
*load
=
655 nir_load_const_instr_create(shader
, glsl_get_vector_elements(tail
->type
));
657 matrix_offset
*= load
->def
.num_components
;
658 for (unsigned i
= 0; i
< load
->def
.num_components
; i
++) {
659 switch (glsl_get_base_type(tail
->type
)) {
660 case GLSL_TYPE_FLOAT
:
663 load
->value
.u
[i
] = constant
->value
.u
[matrix_offset
+ i
];
666 load
->value
.u
[i
] = constant
->value
.b
[matrix_offset
+ i
] ?
667 NIR_TRUE
: NIR_FALSE
;
670 unreachable("Invalid immediate type");
678 nir_cf_node_get_function(nir_cf_node
*node
)
680 while (node
->type
!= nir_cf_node_function
) {
684 return nir_cf_node_as_function(node
);
688 add_use_cb(nir_src
*src
, void *state
)
690 nir_instr
*instr
= state
;
692 src
->parent_instr
= instr
;
693 list_addtail(&src
->use_link
,
694 src
->is_ssa
? &src
->ssa
->uses
: &src
->reg
.reg
->uses
);
700 add_ssa_def_cb(nir_ssa_def
*def
, void *state
)
702 nir_instr
*instr
= state
;
704 if (instr
->block
&& def
->index
== UINT_MAX
) {
705 nir_function_impl
*impl
=
706 nir_cf_node_get_function(&instr
->block
->cf_node
);
708 def
->index
= impl
->ssa_alloc
++;
715 add_reg_def_cb(nir_dest
*dest
, void *state
)
717 nir_instr
*instr
= state
;
720 dest
->reg
.parent_instr
= instr
;
721 list_addtail(&dest
->reg
.def_link
, &dest
->reg
.reg
->defs
);
728 add_defs_uses(nir_instr
*instr
)
730 nir_foreach_src(instr
, add_use_cb
, instr
);
731 nir_foreach_dest(instr
, add_reg_def_cb
, instr
);
732 nir_foreach_ssa_def(instr
, add_ssa_def_cb
, instr
);
736 nir_instr_insert(nir_cursor cursor
, nir_instr
*instr
)
738 switch (cursor
.option
) {
739 case nir_cursor_before_block
:
740 /* Only allow inserting jumps into empty blocks. */
741 if (instr
->type
== nir_instr_type_jump
)
742 assert(exec_list_is_empty(&cursor
.block
->instr_list
));
744 instr
->block
= cursor
.block
;
745 add_defs_uses(instr
);
746 exec_list_push_head(&cursor
.block
->instr_list
, &instr
->node
);
748 case nir_cursor_after_block
: {
749 /* Inserting instructions after a jump is illegal. */
750 nir_instr
*last
= nir_block_last_instr(cursor
.block
);
751 assert(last
== NULL
|| last
->type
!= nir_instr_type_jump
);
754 instr
->block
= cursor
.block
;
755 add_defs_uses(instr
);
756 exec_list_push_tail(&cursor
.block
->instr_list
, &instr
->node
);
759 case nir_cursor_before_instr
:
760 assert(instr
->type
!= nir_instr_type_jump
);
761 instr
->block
= cursor
.instr
->block
;
762 add_defs_uses(instr
);
763 exec_node_insert_node_before(&cursor
.instr
->node
, &instr
->node
);
765 case nir_cursor_after_instr
:
766 /* Inserting instructions after a jump is illegal. */
767 assert(cursor
.instr
->type
!= nir_instr_type_jump
);
769 /* Only allow inserting jumps at the end of the block. */
770 if (instr
->type
== nir_instr_type_jump
)
771 assert(cursor
.instr
== nir_block_last_instr(cursor
.instr
->block
));
773 instr
->block
= cursor
.instr
->block
;
774 add_defs_uses(instr
);
775 exec_node_insert_after(&cursor
.instr
->node
, &instr
->node
);
779 if (instr
->type
== nir_instr_type_jump
)
780 nir_handle_add_jump(instr
->block
);
784 src_is_valid(const nir_src
*src
)
786 return src
->is_ssa
? (src
->ssa
!= NULL
) : (src
->reg
.reg
!= NULL
);
790 remove_use_cb(nir_src
*src
, void *state
)
792 if (src_is_valid(src
))
793 list_del(&src
->use_link
);
799 remove_def_cb(nir_dest
*dest
, void *state
)
802 list_del(&dest
->reg
.def_link
);
808 remove_defs_uses(nir_instr
*instr
)
810 nir_foreach_dest(instr
, remove_def_cb
, instr
);
811 nir_foreach_src(instr
, remove_use_cb
, instr
);
814 void nir_instr_remove(nir_instr
*instr
)
816 remove_defs_uses(instr
);
817 exec_node_remove(&instr
->node
);
819 if (instr
->type
== nir_instr_type_jump
) {
820 nir_jump_instr
*jump_instr
= nir_instr_as_jump(instr
);
821 nir_handle_remove_jump(instr
->block
, jump_instr
->type
);
828 nir_index_local_regs(nir_function_impl
*impl
)
831 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
832 reg
->index
= index
++;
834 impl
->reg_alloc
= index
;
838 nir_index_global_regs(nir_shader
*shader
)
841 foreach_list_typed(nir_register
, reg
, node
, &shader
->registers
) {
842 reg
->index
= index
++;
844 shader
->reg_alloc
= index
;
848 visit_alu_dest(nir_alu_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
850 return cb(&instr
->dest
.dest
, state
);
854 visit_intrinsic_dest(nir_intrinsic_instr
*instr
, nir_foreach_dest_cb cb
,
857 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
858 return cb(&instr
->dest
, state
);
864 visit_texture_dest(nir_tex_instr
*instr
, nir_foreach_dest_cb cb
,
867 return cb(&instr
->dest
, state
);
871 visit_phi_dest(nir_phi_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
873 return cb(&instr
->dest
, state
);
877 visit_parallel_copy_dest(nir_parallel_copy_instr
*instr
,
878 nir_foreach_dest_cb cb
, void *state
)
880 nir_foreach_parallel_copy_entry(instr
, entry
) {
881 if (!cb(&entry
->dest
, state
))
889 nir_foreach_dest(nir_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
891 switch (instr
->type
) {
892 case nir_instr_type_alu
:
893 return visit_alu_dest(nir_instr_as_alu(instr
), cb
, state
);
894 case nir_instr_type_intrinsic
:
895 return visit_intrinsic_dest(nir_instr_as_intrinsic(instr
), cb
, state
);
896 case nir_instr_type_tex
:
897 return visit_texture_dest(nir_instr_as_tex(instr
), cb
, state
);
898 case nir_instr_type_phi
:
899 return visit_phi_dest(nir_instr_as_phi(instr
), cb
, state
);
900 case nir_instr_type_parallel_copy
:
901 return visit_parallel_copy_dest(nir_instr_as_parallel_copy(instr
),
904 case nir_instr_type_load_const
:
905 case nir_instr_type_ssa_undef
:
906 case nir_instr_type_call
:
907 case nir_instr_type_jump
:
911 unreachable("Invalid instruction type");
918 struct foreach_ssa_def_state
{
919 nir_foreach_ssa_def_cb cb
;
924 nir_ssa_def_visitor(nir_dest
*dest
, void *void_state
)
926 struct foreach_ssa_def_state
*state
= void_state
;
929 return state
->cb(&dest
->ssa
, state
->client_state
);
935 nir_foreach_ssa_def(nir_instr
*instr
, nir_foreach_ssa_def_cb cb
, void *state
)
937 switch (instr
->type
) {
938 case nir_instr_type_alu
:
939 case nir_instr_type_tex
:
940 case nir_instr_type_intrinsic
:
941 case nir_instr_type_phi
:
942 case nir_instr_type_parallel_copy
: {
943 struct foreach_ssa_def_state foreach_state
= {cb
, state
};
944 return nir_foreach_dest(instr
, nir_ssa_def_visitor
, &foreach_state
);
947 case nir_instr_type_load_const
:
948 return cb(&nir_instr_as_load_const(instr
)->def
, state
);
949 case nir_instr_type_ssa_undef
:
950 return cb(&nir_instr_as_ssa_undef(instr
)->def
, state
);
951 case nir_instr_type_call
:
952 case nir_instr_type_jump
:
955 unreachable("Invalid instruction type");
960 visit_src(nir_src
*src
, nir_foreach_src_cb cb
, void *state
)
964 if (!src
->is_ssa
&& src
->reg
.indirect
)
965 return cb(src
->reg
.indirect
, state
);
970 visit_deref_array_src(nir_deref_array
*deref
, nir_foreach_src_cb cb
,
973 if (deref
->deref_array_type
== nir_deref_array_type_indirect
)
974 return visit_src(&deref
->indirect
, cb
, state
);
979 visit_deref_src(nir_deref_var
*deref
, nir_foreach_src_cb cb
, void *state
)
981 nir_deref
*cur
= &deref
->deref
;
982 while (cur
!= NULL
) {
983 if (cur
->deref_type
== nir_deref_type_array
)
984 if (!visit_deref_array_src(nir_deref_as_array(cur
), cb
, state
))
994 visit_alu_src(nir_alu_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
996 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
997 if (!visit_src(&instr
->src
[i
].src
, cb
, state
))
1004 visit_tex_src(nir_tex_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1006 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++)
1007 if (!visit_src(&instr
->src
[i
].src
, cb
, state
))
1010 if (instr
->sampler
!= NULL
)
1011 if (!visit_deref_src(instr
->sampler
, cb
, state
))
1018 visit_intrinsic_src(nir_intrinsic_instr
*instr
, nir_foreach_src_cb cb
,
1021 unsigned num_srcs
= nir_intrinsic_infos
[instr
->intrinsic
].num_srcs
;
1022 for (unsigned i
= 0; i
< num_srcs
; i
++)
1023 if (!visit_src(&instr
->src
[i
], cb
, state
))
1027 nir_intrinsic_infos
[instr
->intrinsic
].num_variables
;
1028 for (unsigned i
= 0; i
< num_vars
; i
++)
1029 if (!visit_deref_src(instr
->variables
[i
], cb
, state
))
1036 visit_call_src(nir_call_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1042 visit_load_const_src(nir_load_const_instr
*instr
, nir_foreach_src_cb cb
,
1049 visit_phi_src(nir_phi_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1051 nir_foreach_phi_src(instr
, src
) {
1052 if (!visit_src(&src
->src
, cb
, state
))
1060 visit_parallel_copy_src(nir_parallel_copy_instr
*instr
,
1061 nir_foreach_src_cb cb
, void *state
)
1063 nir_foreach_parallel_copy_entry(instr
, entry
) {
1064 if (!visit_src(&entry
->src
, cb
, state
))
1073 nir_foreach_src_cb cb
;
1074 } visit_dest_indirect_state
;
1077 visit_dest_indirect(nir_dest
*dest
, void *_state
)
1079 visit_dest_indirect_state
*state
= (visit_dest_indirect_state
*) _state
;
1081 if (!dest
->is_ssa
&& dest
->reg
.indirect
)
1082 return state
->cb(dest
->reg
.indirect
, state
->state
);
1088 nir_foreach_src(nir_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1090 switch (instr
->type
) {
1091 case nir_instr_type_alu
:
1092 if (!visit_alu_src(nir_instr_as_alu(instr
), cb
, state
))
1095 case nir_instr_type_intrinsic
:
1096 if (!visit_intrinsic_src(nir_instr_as_intrinsic(instr
), cb
, state
))
1099 case nir_instr_type_tex
:
1100 if (!visit_tex_src(nir_instr_as_tex(instr
), cb
, state
))
1103 case nir_instr_type_call
:
1104 if (!visit_call_src(nir_instr_as_call(instr
), cb
, state
))
1107 case nir_instr_type_load_const
:
1108 if (!visit_load_const_src(nir_instr_as_load_const(instr
), cb
, state
))
1111 case nir_instr_type_phi
:
1112 if (!visit_phi_src(nir_instr_as_phi(instr
), cb
, state
))
1115 case nir_instr_type_parallel_copy
:
1116 if (!visit_parallel_copy_src(nir_instr_as_parallel_copy(instr
),
1120 case nir_instr_type_jump
:
1121 case nir_instr_type_ssa_undef
:
1125 unreachable("Invalid instruction type");
1129 visit_dest_indirect_state dest_state
;
1130 dest_state
.state
= state
;
1132 return nir_foreach_dest(instr
, visit_dest_indirect
, &dest_state
);
1136 nir_src_as_const_value(nir_src src
)
1141 if (src
.ssa
->parent_instr
->type
!= nir_instr_type_load_const
)
1144 nir_load_const_instr
*load
= nir_instr_as_load_const(src
.ssa
->parent_instr
);
1146 return &load
->value
;
1150 * Returns true if the source is known to be dynamically uniform. Otherwise it
1151 * returns false which means it may or may not be dynamically uniform but it
1152 * can't be determined.
1155 nir_src_is_dynamically_uniform(nir_src src
)
1160 /* Constants are trivially dynamically uniform */
1161 if (src
.ssa
->parent_instr
->type
== nir_instr_type_load_const
)
1164 /* As are uniform variables */
1165 if (src
.ssa
->parent_instr
->type
== nir_instr_type_intrinsic
) {
1166 nir_intrinsic_instr
*intr
= nir_instr_as_intrinsic(src
.ssa
->parent_instr
);
1168 if (intr
->intrinsic
== nir_intrinsic_load_uniform
)
1172 /* XXX: this could have many more tests, such as when a sampler function is
1173 * called with dynamically uniform arguments.
1179 src_remove_all_uses(nir_src
*src
)
1181 for (; src
; src
= src
->is_ssa
? NULL
: src
->reg
.indirect
) {
1182 if (!src_is_valid(src
))
1185 list_del(&src
->use_link
);
1190 src_add_all_uses(nir_src
*src
, nir_instr
*parent_instr
, nir_if
*parent_if
)
1192 for (; src
; src
= src
->is_ssa
? NULL
: src
->reg
.indirect
) {
1193 if (!src_is_valid(src
))
1197 src
->parent_instr
= parent_instr
;
1199 list_addtail(&src
->use_link
, &src
->ssa
->uses
);
1201 list_addtail(&src
->use_link
, &src
->reg
.reg
->uses
);
1204 src
->parent_if
= parent_if
;
1206 list_addtail(&src
->use_link
, &src
->ssa
->if_uses
);
1208 list_addtail(&src
->use_link
, &src
->reg
.reg
->if_uses
);
1214 nir_instr_rewrite_src(nir_instr
*instr
, nir_src
*src
, nir_src new_src
)
1216 assert(!src_is_valid(src
) || src
->parent_instr
== instr
);
1218 src_remove_all_uses(src
);
1220 src_add_all_uses(src
, instr
, NULL
);
1224 nir_instr_move_src(nir_instr
*dest_instr
, nir_src
*dest
, nir_src
*src
)
1226 assert(!src_is_valid(dest
) || dest
->parent_instr
== dest_instr
);
1228 src_remove_all_uses(dest
);
1229 src_remove_all_uses(src
);
1231 *src
= NIR_SRC_INIT
;
1232 src_add_all_uses(dest
, dest_instr
, NULL
);
1236 nir_if_rewrite_condition(nir_if
*if_stmt
, nir_src new_src
)
1238 nir_src
*src
= &if_stmt
->condition
;
1239 assert(!src_is_valid(src
) || src
->parent_if
== if_stmt
);
1241 src_remove_all_uses(src
);
1243 src_add_all_uses(src
, NULL
, if_stmt
);
1247 nir_instr_rewrite_dest(nir_instr
*instr
, nir_dest
*dest
, nir_dest new_dest
)
1250 /* We can only overwrite an SSA destination if it has no uses. */
1251 assert(list_empty(&dest
->ssa
.uses
) && list_empty(&dest
->ssa
.if_uses
));
1253 list_del(&dest
->reg
.def_link
);
1254 if (dest
->reg
.indirect
)
1255 src_remove_all_uses(dest
->reg
.indirect
);
1258 /* We can't re-write with an SSA def */
1259 assert(!new_dest
.is_ssa
);
1261 nir_dest_copy(dest
, &new_dest
, instr
);
1263 dest
->reg
.parent_instr
= instr
;
1264 list_addtail(&dest
->reg
.def_link
, &new_dest
.reg
.reg
->defs
);
1266 if (dest
->reg
.indirect
)
1267 src_add_all_uses(dest
->reg
.indirect
, instr
, NULL
);
1271 nir_ssa_def_init(nir_instr
*instr
, nir_ssa_def
*def
,
1272 unsigned num_components
, const char *name
)
1275 def
->parent_instr
= instr
;
1276 list_inithead(&def
->uses
);
1277 list_inithead(&def
->if_uses
);
1278 def
->num_components
= num_components
;
1281 nir_function_impl
*impl
=
1282 nir_cf_node_get_function(&instr
->block
->cf_node
);
1284 def
->index
= impl
->ssa_alloc
++;
1286 def
->index
= UINT_MAX
;
1291 nir_ssa_dest_init(nir_instr
*instr
, nir_dest
*dest
,
1292 unsigned num_components
, const char *name
)
1294 dest
->is_ssa
= true;
1295 nir_ssa_def_init(instr
, &dest
->ssa
, num_components
, name
);
1299 nir_ssa_def_rewrite_uses(nir_ssa_def
*def
, nir_src new_src
)
1301 assert(!new_src
.is_ssa
|| def
!= new_src
.ssa
);
1303 nir_foreach_use_safe(def
, use_src
) {
1304 nir_instr
*src_parent_instr
= use_src
->parent_instr
;
1305 list_del(&use_src
->use_link
);
1306 nir_src_copy(use_src
, &new_src
, src_parent_instr
);
1307 src_add_all_uses(use_src
, src_parent_instr
, NULL
);
1310 nir_foreach_if_use_safe(def
, use_src
) {
1311 nir_if
*src_parent_if
= use_src
->parent_if
;
1312 list_del(&use_src
->use_link
);
1313 nir_src_copy(use_src
, &new_src
, src_parent_if
);
1314 src_add_all_uses(use_src
, NULL
, src_parent_if
);
1319 static bool foreach_cf_node(nir_cf_node
*node
, nir_foreach_block_cb cb
,
1320 bool reverse
, void *state
);
1323 foreach_if(nir_if
*if_stmt
, nir_foreach_block_cb cb
, bool reverse
, void *state
)
1326 foreach_list_typed_safe_reverse(nir_cf_node
, node
, node
,
1327 &if_stmt
->else_list
) {
1328 if (!foreach_cf_node(node
, cb
, reverse
, state
))
1332 foreach_list_typed_safe_reverse(nir_cf_node
, node
, node
,
1333 &if_stmt
->then_list
) {
1334 if (!foreach_cf_node(node
, cb
, reverse
, state
))
1338 foreach_list_typed_safe(nir_cf_node
, node
, node
, &if_stmt
->then_list
) {
1339 if (!foreach_cf_node(node
, cb
, reverse
, state
))
1343 foreach_list_typed_safe(nir_cf_node
, node
, node
, &if_stmt
->else_list
) {
1344 if (!foreach_cf_node(node
, cb
, reverse
, state
))
1353 foreach_loop(nir_loop
*loop
, nir_foreach_block_cb cb
, bool reverse
, void *state
)
1356 foreach_list_typed_safe_reverse(nir_cf_node
, node
, node
, &loop
->body
) {
1357 if (!foreach_cf_node(node
, cb
, reverse
, state
))
1361 foreach_list_typed_safe(nir_cf_node
, node
, node
, &loop
->body
) {
1362 if (!foreach_cf_node(node
, cb
, reverse
, state
))
1371 foreach_cf_node(nir_cf_node
*node
, nir_foreach_block_cb cb
,
1372 bool reverse
, void *state
)
1374 switch (node
->type
) {
1375 case nir_cf_node_block
:
1376 return cb(nir_cf_node_as_block(node
), state
);
1377 case nir_cf_node_if
:
1378 return foreach_if(nir_cf_node_as_if(node
), cb
, reverse
, state
);
1379 case nir_cf_node_loop
:
1380 return foreach_loop(nir_cf_node_as_loop(node
), cb
, reverse
, state
);
1384 unreachable("Invalid CFG node type");
1392 nir_foreach_block_in_cf_node(nir_cf_node
*node
, nir_foreach_block_cb cb
,
1395 return foreach_cf_node(node
, cb
, false, state
);
1399 nir_foreach_block(nir_function_impl
*impl
, nir_foreach_block_cb cb
, void *state
)
1401 foreach_list_typed_safe(nir_cf_node
, node
, node
, &impl
->body
) {
1402 if (!foreach_cf_node(node
, cb
, false, state
))
1406 return cb(impl
->end_block
, state
);
1410 nir_foreach_block_reverse(nir_function_impl
*impl
, nir_foreach_block_cb cb
,
1413 if (!cb(impl
->end_block
, state
))
1416 foreach_list_typed_safe_reverse(nir_cf_node
, node
, node
, &impl
->body
) {
1417 if (!foreach_cf_node(node
, cb
, true, state
))
1425 nir_block_get_following_if(nir_block
*block
)
1427 if (exec_node_is_tail_sentinel(&block
->cf_node
.node
))
1430 if (nir_cf_node_is_last(&block
->cf_node
))
1433 nir_cf_node
*next_node
= nir_cf_node_next(&block
->cf_node
);
1435 if (next_node
->type
!= nir_cf_node_if
)
1438 return nir_cf_node_as_if(next_node
);
1442 nir_block_get_following_loop(nir_block
*block
)
1444 if (exec_node_is_tail_sentinel(&block
->cf_node
.node
))
1447 if (nir_cf_node_is_last(&block
->cf_node
))
1450 nir_cf_node
*next_node
= nir_cf_node_next(&block
->cf_node
);
1452 if (next_node
->type
!= nir_cf_node_loop
)
1455 return nir_cf_node_as_loop(next_node
);
1458 index_block(nir_block
*block
, void *state
)
1460 unsigned *index
= state
;
1461 block
->index
= (*index
)++;
1466 nir_index_blocks(nir_function_impl
*impl
)
1470 if (impl
->valid_metadata
& nir_metadata_block_index
)
1473 nir_foreach_block(impl
, index_block
, &index
);
1475 impl
->num_blocks
= index
;
1479 index_ssa_def_cb(nir_ssa_def
*def
, void *state
)
1481 unsigned *index
= (unsigned *) state
;
1482 def
->index
= (*index
)++;
1488 index_ssa_block(nir_block
*block
, void *state
)
1490 nir_foreach_instr(block
, instr
)
1491 nir_foreach_ssa_def(instr
, index_ssa_def_cb
, state
);
1497 * The indices are applied top-to-bottom which has the very nice property
1498 * that, if A dominates B, then A->index <= B->index.
1501 nir_index_ssa_defs(nir_function_impl
*impl
)
1504 nir_foreach_block(impl
, index_ssa_block
, &index
);
1505 impl
->ssa_alloc
= index
;
1509 index_instrs_block(nir_block
*block
, void *state
)
1511 unsigned *index
= state
;
1512 nir_foreach_instr(block
, instr
)
1513 instr
->index
= (*index
)++;
1519 * The indices are applied top-to-bottom which has the very nice property
1520 * that, if A dominates B, then A->index <= B->index.
1523 nir_index_instrs(nir_function_impl
*impl
)
1526 nir_foreach_block(impl
, index_instrs_block
, &index
);
1531 nir_intrinsic_from_system_value(gl_system_value val
)
1534 case SYSTEM_VALUE_VERTEX_ID
:
1535 return nir_intrinsic_load_vertex_id
;
1536 case SYSTEM_VALUE_INSTANCE_ID
:
1537 return nir_intrinsic_load_instance_id
;
1538 case SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
:
1539 return nir_intrinsic_load_vertex_id_zero_base
;
1540 case SYSTEM_VALUE_BASE_VERTEX
:
1541 return nir_intrinsic_load_base_vertex
;
1542 case SYSTEM_VALUE_INVOCATION_ID
:
1543 return nir_intrinsic_load_invocation_id
;
1544 case SYSTEM_VALUE_FRONT_FACE
:
1545 return nir_intrinsic_load_front_face
;
1546 case SYSTEM_VALUE_SAMPLE_ID
:
1547 return nir_intrinsic_load_sample_id
;
1548 case SYSTEM_VALUE_SAMPLE_POS
:
1549 return nir_intrinsic_load_sample_pos
;
1550 case SYSTEM_VALUE_SAMPLE_MASK_IN
:
1551 return nir_intrinsic_load_sample_mask_in
;
1552 case SYSTEM_VALUE_LOCAL_INVOCATION_ID
:
1553 return nir_intrinsic_load_local_invocation_id
;
1554 case SYSTEM_VALUE_WORK_GROUP_ID
:
1555 return nir_intrinsic_load_work_group_id
;
1556 case SYSTEM_VALUE_NUM_WORK_GROUPS
:
1557 return nir_intrinsic_load_num_work_groups
;
1558 case SYSTEM_VALUE_PRIMITIVE_ID
:
1559 return nir_intrinsic_load_primitive_id
;
1560 case SYSTEM_VALUE_TESS_COORD
:
1561 return nir_intrinsic_load_tess_coord
;
1562 case SYSTEM_VALUE_TESS_LEVEL_OUTER
:
1563 return nir_intrinsic_load_tess_level_outer
;
1564 case SYSTEM_VALUE_TESS_LEVEL_INNER
:
1565 return nir_intrinsic_load_tess_level_inner
;
1566 case SYSTEM_VALUE_VERTICES_IN
:
1567 return nir_intrinsic_load_patch_vertices_in
;
1569 unreachable("system value does not directly correspond to intrinsic");
1574 nir_system_value_from_intrinsic(nir_intrinsic_op intrin
)
1577 case nir_intrinsic_load_vertex_id
:
1578 return SYSTEM_VALUE_VERTEX_ID
;
1579 case nir_intrinsic_load_instance_id
:
1580 return SYSTEM_VALUE_INSTANCE_ID
;
1581 case nir_intrinsic_load_vertex_id_zero_base
:
1582 return SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
;
1583 case nir_intrinsic_load_base_vertex
:
1584 return SYSTEM_VALUE_BASE_VERTEX
;
1585 case nir_intrinsic_load_invocation_id
:
1586 return SYSTEM_VALUE_INVOCATION_ID
;
1587 case nir_intrinsic_load_front_face
:
1588 return SYSTEM_VALUE_FRONT_FACE
;
1589 case nir_intrinsic_load_sample_id
:
1590 return SYSTEM_VALUE_SAMPLE_ID
;
1591 case nir_intrinsic_load_sample_pos
:
1592 return SYSTEM_VALUE_SAMPLE_POS
;
1593 case nir_intrinsic_load_sample_mask_in
:
1594 return SYSTEM_VALUE_SAMPLE_MASK_IN
;
1595 case nir_intrinsic_load_local_invocation_id
:
1596 return SYSTEM_VALUE_LOCAL_INVOCATION_ID
;
1597 case nir_intrinsic_load_num_work_groups
:
1598 return SYSTEM_VALUE_NUM_WORK_GROUPS
;
1599 case nir_intrinsic_load_work_group_id
:
1600 return SYSTEM_VALUE_WORK_GROUP_ID
;
1601 case nir_intrinsic_load_primitive_id
:
1602 return SYSTEM_VALUE_PRIMITIVE_ID
;
1603 case nir_intrinsic_load_tess_coord
:
1604 return SYSTEM_VALUE_TESS_COORD
;
1605 case nir_intrinsic_load_tess_level_outer
:
1606 return SYSTEM_VALUE_TESS_LEVEL_OUTER
;
1607 case nir_intrinsic_load_tess_level_inner
:
1608 return SYSTEM_VALUE_TESS_LEVEL_INNER
;
1609 case nir_intrinsic_load_patch_vertices_in
:
1610 return SYSTEM_VALUE_VERTICES_IN
;
1612 unreachable("intrinsic doesn't produce a system value");