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
, const nir_shader_compiler_options
*options
)
35 nir_shader
*shader
= ralloc(mem_ctx
, nir_shader
);
37 exec_list_make_empty(&shader
->uniforms
);
38 exec_list_make_empty(&shader
->inputs
);
39 exec_list_make_empty(&shader
->outputs
);
41 shader
->options
= options
;
43 exec_list_make_empty(&shader
->functions
);
44 exec_list_make_empty(&shader
->registers
);
45 exec_list_make_empty(&shader
->globals
);
46 exec_list_make_empty(&shader
->system_values
);
47 shader
->reg_alloc
= 0;
49 shader
->num_inputs
= 0;
50 shader
->num_outputs
= 0;
51 shader
->num_uniforms
= 0;
57 reg_create(void *mem_ctx
, struct exec_list
*list
)
59 nir_register
*reg
= ralloc(mem_ctx
, nir_register
);
61 list_inithead(®
->uses
);
62 list_inithead(®
->defs
);
63 list_inithead(®
->if_uses
);
65 reg
->num_components
= 0;
66 reg
->num_array_elems
= 0;
67 reg
->is_packed
= false;
70 exec_list_push_tail(list
, ®
->node
);
76 nir_global_reg_create(nir_shader
*shader
)
78 nir_register
*reg
= reg_create(shader
, &shader
->registers
);
79 reg
->index
= shader
->reg_alloc
++;
80 reg
->is_global
= true;
86 nir_local_reg_create(nir_function_impl
*impl
)
88 nir_register
*reg
= reg_create(ralloc_parent(impl
), &impl
->registers
);
89 reg
->index
= impl
->reg_alloc
++;
90 reg
->is_global
= false;
96 nir_reg_remove(nir_register
*reg
)
98 exec_node_remove(®
->node
);
102 nir_function_create(nir_shader
*shader
, const char *name
)
104 nir_function
*func
= ralloc(shader
, nir_function
);
106 exec_list_push_tail(&shader
->functions
, &func
->node
);
107 exec_list_make_empty(&func
->overload_list
);
108 func
->name
= ralloc_strdup(func
, name
);
109 func
->shader
= shader
;
114 nir_function_overload
*
115 nir_function_overload_create(nir_function
*func
)
117 void *mem_ctx
= ralloc_parent(func
);
119 nir_function_overload
*overload
= ralloc(mem_ctx
, nir_function_overload
);
121 overload
->num_params
= 0;
122 overload
->params
= NULL
;
123 overload
->return_type
= glsl_void_type();
124 overload
->impl
= NULL
;
126 exec_list_push_tail(&func
->overload_list
, &overload
->node
);
127 overload
->function
= func
;
132 void nir_src_copy(nir_src
*dest
, const nir_src
*src
, void *mem_ctx
)
134 dest
->is_ssa
= src
->is_ssa
;
136 dest
->ssa
= src
->ssa
;
138 dest
->reg
.base_offset
= src
->reg
.base_offset
;
139 dest
->reg
.reg
= src
->reg
.reg
;
140 if (src
->reg
.indirect
) {
141 dest
->reg
.indirect
= ralloc(mem_ctx
, nir_src
);
142 nir_src_copy(dest
->reg
.indirect
, src
->reg
.indirect
, mem_ctx
);
144 dest
->reg
.indirect
= NULL
;
149 void nir_dest_copy(nir_dest
*dest
, const nir_dest
*src
, void *mem_ctx
)
151 /* Copying an SSA definition makes no sense whatsoever. */
152 assert(!src
->is_ssa
);
154 dest
->is_ssa
= false;
156 dest
->reg
.base_offset
= src
->reg
.base_offset
;
157 dest
->reg
.reg
= src
->reg
.reg
;
158 if (src
->reg
.indirect
) {
159 dest
->reg
.indirect
= ralloc(mem_ctx
, nir_src
);
160 nir_src_copy(dest
->reg
.indirect
, src
->reg
.indirect
, mem_ctx
);
162 dest
->reg
.indirect
= NULL
;
167 nir_alu_src_copy(nir_alu_src
*dest
, const nir_alu_src
*src
, void *mem_ctx
)
169 nir_src_copy(&dest
->src
, &src
->src
, mem_ctx
);
170 dest
->abs
= src
->abs
;
171 dest
->negate
= src
->negate
;
172 for (unsigned i
= 0; i
< 4; i
++)
173 dest
->swizzle
[i
] = src
->swizzle
[i
];
177 nir_alu_dest_copy(nir_alu_dest
*dest
, const nir_alu_dest
*src
, void *mem_ctx
)
179 nir_dest_copy(&dest
->dest
, &src
->dest
, mem_ctx
);
180 dest
->write_mask
= src
->write_mask
;
181 dest
->saturate
= src
->saturate
;
186 cf_init(nir_cf_node
*node
, nir_cf_node_type type
)
188 exec_node_init(&node
->node
);
194 nir_function_impl_create(nir_function_overload
*overload
)
196 assert(overload
->impl
== NULL
);
198 void *mem_ctx
= ralloc_parent(overload
);
200 nir_function_impl
*impl
= ralloc(mem_ctx
, nir_function_impl
);
202 overload
->impl
= impl
;
203 impl
->overload
= overload
;
205 cf_init(&impl
->cf_node
, nir_cf_node_function
);
207 exec_list_make_empty(&impl
->body
);
208 exec_list_make_empty(&impl
->registers
);
209 exec_list_make_empty(&impl
->locals
);
210 impl
->num_params
= 0;
212 impl
->return_var
= NULL
;
215 impl
->valid_metadata
= nir_metadata_none
;
217 /* create start & end blocks */
218 nir_block
*start_block
= nir_block_create(mem_ctx
);
219 nir_block
*end_block
= nir_block_create(mem_ctx
);
220 start_block
->cf_node
.parent
= &impl
->cf_node
;
221 end_block
->cf_node
.parent
= &impl
->cf_node
;
222 impl
->end_block
= end_block
;
224 exec_list_push_tail(&impl
->body
, &start_block
->cf_node
.node
);
226 start_block
->successors
[0] = end_block
;
227 _mesa_set_add(end_block
->predecessors
, start_block
);
232 nir_block_create(void *mem_ctx
)
234 nir_block
*block
= ralloc(mem_ctx
, nir_block
);
236 cf_init(&block
->cf_node
, nir_cf_node_block
);
238 block
->successors
[0] = block
->successors
[1] = NULL
;
239 block
->predecessors
= _mesa_set_create(block
, _mesa_hash_pointer
,
240 _mesa_key_pointer_equal
);
241 block
->imm_dom
= NULL
;
242 block
->dom_frontier
= _mesa_set_create(block
, _mesa_hash_pointer
,
243 _mesa_key_pointer_equal
);
245 exec_list_make_empty(&block
->instr_list
);
251 src_init(nir_src
*src
)
255 src
->reg
.indirect
= NULL
;
256 src
->reg
.base_offset
= 0;
260 nir_if_create(void *mem_ctx
)
262 nir_if
*if_stmt
= ralloc(mem_ctx
, nir_if
);
264 cf_init(&if_stmt
->cf_node
, nir_cf_node_if
);
265 src_init(&if_stmt
->condition
);
267 nir_block
*then
= nir_block_create(mem_ctx
);
268 exec_list_make_empty(&if_stmt
->then_list
);
269 exec_list_push_tail(&if_stmt
->then_list
, &then
->cf_node
.node
);
270 then
->cf_node
.parent
= &if_stmt
->cf_node
;
272 nir_block
*else_stmt
= nir_block_create(mem_ctx
);
273 exec_list_make_empty(&if_stmt
->else_list
);
274 exec_list_push_tail(&if_stmt
->else_list
, &else_stmt
->cf_node
.node
);
275 else_stmt
->cf_node
.parent
= &if_stmt
->cf_node
;
281 nir_loop_create(void *mem_ctx
)
283 nir_loop
*loop
= ralloc(mem_ctx
, nir_loop
);
285 cf_init(&loop
->cf_node
, nir_cf_node_loop
);
287 nir_block
*body
= nir_block_create(mem_ctx
);
288 exec_list_make_empty(&loop
->body
);
289 exec_list_push_tail(&loop
->body
, &body
->cf_node
.node
);
290 body
->cf_node
.parent
= &loop
->cf_node
;
292 body
->successors
[0] = body
;
293 _mesa_set_add(body
->predecessors
, body
);
299 instr_init(nir_instr
*instr
, nir_instr_type type
)
303 exec_node_init(&instr
->node
);
307 dest_init(nir_dest
*dest
)
309 dest
->is_ssa
= false;
310 dest
->reg
.reg
= NULL
;
311 dest
->reg
.indirect
= NULL
;
312 dest
->reg
.base_offset
= 0;
316 alu_dest_init(nir_alu_dest
*dest
)
318 dest_init(&dest
->dest
);
319 dest
->saturate
= false;
320 dest
->write_mask
= 0xf;
324 alu_src_init(nir_alu_src
*src
)
327 src
->abs
= src
->negate
= false;
335 nir_alu_instr_create(nir_shader
*shader
, nir_op op
)
337 unsigned num_srcs
= nir_op_infos
[op
].num_inputs
;
338 nir_alu_instr
*instr
=
340 sizeof(nir_alu_instr
) + num_srcs
* sizeof(nir_alu_src
));
342 instr_init(&instr
->instr
, nir_instr_type_alu
);
344 alu_dest_init(&instr
->dest
);
345 for (unsigned i
= 0; i
< num_srcs
; i
++)
346 alu_src_init(&instr
->src
[i
]);
352 nir_jump_instr_create(nir_shader
*shader
, nir_jump_type type
)
354 nir_jump_instr
*instr
= ralloc(shader
, nir_jump_instr
);
355 instr_init(&instr
->instr
, nir_instr_type_jump
);
360 nir_load_const_instr
*
361 nir_load_const_instr_create(nir_shader
*shader
, unsigned num_components
)
363 nir_load_const_instr
*instr
= ralloc(shader
, nir_load_const_instr
);
364 instr_init(&instr
->instr
, nir_instr_type_load_const
);
366 nir_ssa_def_init(&instr
->instr
, &instr
->def
, num_components
, NULL
);
371 nir_intrinsic_instr
*
372 nir_intrinsic_instr_create(nir_shader
*shader
, nir_intrinsic_op op
)
374 unsigned num_srcs
= nir_intrinsic_infos
[op
].num_srcs
;
375 nir_intrinsic_instr
*instr
=
377 sizeof(nir_intrinsic_instr
) + num_srcs
* sizeof(nir_src
));
379 instr_init(&instr
->instr
, nir_instr_type_intrinsic
);
380 instr
->intrinsic
= op
;
382 if (nir_intrinsic_infos
[op
].has_dest
)
383 dest_init(&instr
->dest
);
385 for (unsigned i
= 0; i
< num_srcs
; i
++)
386 src_init(&instr
->src
[i
]);
392 nir_call_instr_create(nir_shader
*shader
, nir_function_overload
*callee
)
394 nir_call_instr
*instr
= ralloc(shader
, nir_call_instr
);
395 instr_init(&instr
->instr
, nir_instr_type_call
);
397 instr
->callee
= callee
;
398 instr
->num_params
= callee
->num_params
;
399 instr
->params
= ralloc_array(instr
, nir_deref_var
*, instr
->num_params
);
400 instr
->return_deref
= NULL
;
406 nir_tex_instr_create(nir_shader
*shader
, unsigned num_srcs
)
408 nir_tex_instr
*instr
= rzalloc(shader
, nir_tex_instr
);
409 instr_init(&instr
->instr
, nir_instr_type_tex
);
411 dest_init(&instr
->dest
);
413 instr
->num_srcs
= num_srcs
;
414 instr
->src
= ralloc_array(instr
, nir_tex_src
, num_srcs
);
415 for (unsigned i
= 0; i
< num_srcs
; i
++)
416 src_init(&instr
->src
[i
].src
);
418 instr
->sampler_index
= 0;
419 instr
->sampler_array_size
= 0;
420 instr
->sampler
= NULL
;
426 nir_phi_instr_create(nir_shader
*shader
)
428 nir_phi_instr
*instr
= ralloc(shader
, nir_phi_instr
);
429 instr_init(&instr
->instr
, nir_instr_type_phi
);
431 dest_init(&instr
->dest
);
432 exec_list_make_empty(&instr
->srcs
);
436 nir_parallel_copy_instr
*
437 nir_parallel_copy_instr_create(nir_shader
*shader
)
439 nir_parallel_copy_instr
*instr
= ralloc(shader
, nir_parallel_copy_instr
);
440 instr_init(&instr
->instr
, nir_instr_type_parallel_copy
);
442 exec_list_make_empty(&instr
->entries
);
447 nir_ssa_undef_instr
*
448 nir_ssa_undef_instr_create(nir_shader
*shader
, unsigned num_components
)
450 nir_ssa_undef_instr
*instr
= ralloc(shader
, nir_ssa_undef_instr
);
451 instr_init(&instr
->instr
, nir_instr_type_ssa_undef
);
453 nir_ssa_def_init(&instr
->instr
, &instr
->def
, num_components
, NULL
);
459 nir_deref_var_create(void *mem_ctx
, nir_variable
*var
)
461 nir_deref_var
*deref
= ralloc(mem_ctx
, nir_deref_var
);
462 deref
->deref
.deref_type
= nir_deref_type_var
;
463 deref
->deref
.child
= NULL
;
464 deref
->deref
.type
= var
->type
;
470 nir_deref_array_create(void *mem_ctx
)
472 nir_deref_array
*deref
= ralloc(mem_ctx
, nir_deref_array
);
473 deref
->deref
.deref_type
= nir_deref_type_array
;
474 deref
->deref
.child
= NULL
;
475 deref
->deref_array_type
= nir_deref_array_type_direct
;
476 src_init(&deref
->indirect
);
477 deref
->base_offset
= 0;
482 nir_deref_struct_create(void *mem_ctx
, unsigned field_index
)
484 nir_deref_struct
*deref
= ralloc(mem_ctx
, nir_deref_struct
);
485 deref
->deref
.deref_type
= nir_deref_type_struct
;
486 deref
->deref
.child
= NULL
;
487 deref
->index
= field_index
;
491 static nir_deref_var
*
492 copy_deref_var(void *mem_ctx
, nir_deref_var
*deref
)
494 nir_deref_var
*ret
= nir_deref_var_create(mem_ctx
, deref
->var
);
495 ret
->deref
.type
= deref
->deref
.type
;
496 if (deref
->deref
.child
)
497 ret
->deref
.child
= nir_copy_deref(ret
, deref
->deref
.child
);
501 static nir_deref_array
*
502 copy_deref_array(void *mem_ctx
, nir_deref_array
*deref
)
504 nir_deref_array
*ret
= nir_deref_array_create(mem_ctx
);
505 ret
->base_offset
= deref
->base_offset
;
506 ret
->deref_array_type
= deref
->deref_array_type
;
507 if (deref
->deref_array_type
== nir_deref_array_type_indirect
) {
508 nir_src_copy(&ret
->indirect
, &deref
->indirect
, mem_ctx
);
510 ret
->deref
.type
= deref
->deref
.type
;
511 if (deref
->deref
.child
)
512 ret
->deref
.child
= nir_copy_deref(ret
, deref
->deref
.child
);
516 static nir_deref_struct
*
517 copy_deref_struct(void *mem_ctx
, nir_deref_struct
*deref
)
519 nir_deref_struct
*ret
= nir_deref_struct_create(mem_ctx
, deref
->index
);
520 ret
->deref
.type
= deref
->deref
.type
;
521 if (deref
->deref
.child
)
522 ret
->deref
.child
= nir_copy_deref(ret
, deref
->deref
.child
);
527 nir_copy_deref(void *mem_ctx
, nir_deref
*deref
)
529 switch (deref
->deref_type
) {
530 case nir_deref_type_var
:
531 return ©_deref_var(mem_ctx
, nir_deref_as_var(deref
))->deref
;
532 case nir_deref_type_array
:
533 return ©_deref_array(mem_ctx
, nir_deref_as_array(deref
))->deref
;
534 case nir_deref_type_struct
:
535 return ©_deref_struct(mem_ctx
, nir_deref_as_struct(deref
))->deref
;
537 unreachable("Invalid dereference type");
543 /* Returns a load_const instruction that represents the constant
544 * initializer for the given deref chain. The caller is responsible for
545 * ensuring that there actually is a constant initializer.
547 nir_load_const_instr
*
548 nir_deref_get_const_initializer_load(nir_shader
*shader
, nir_deref_var
*deref
)
550 nir_constant
*constant
= deref
->var
->constant_initializer
;
553 const nir_deref
*tail
= &deref
->deref
;
554 unsigned matrix_offset
= 0;
555 while (tail
->child
) {
556 switch (tail
->child
->deref_type
) {
557 case nir_deref_type_array
: {
558 nir_deref_array
*arr
= nir_deref_as_array(tail
->child
);
559 assert(arr
->deref_array_type
== nir_deref_array_type_direct
);
560 if (glsl_type_is_matrix(tail
->type
)) {
561 assert(arr
->deref
.child
== NULL
);
562 matrix_offset
= arr
->base_offset
;
564 constant
= constant
->elements
[arr
->base_offset
];
569 case nir_deref_type_struct
: {
570 constant
= constant
->elements
[nir_deref_as_struct(tail
->child
)->index
];
575 unreachable("Invalid deref child type");
581 nir_load_const_instr
*load
=
582 nir_load_const_instr_create(shader
, glsl_get_vector_elements(tail
->type
));
584 matrix_offset
*= load
->def
.num_components
;
585 for (unsigned i
= 0; i
< load
->def
.num_components
; i
++) {
586 switch (glsl_get_base_type(tail
->type
)) {
587 case GLSL_TYPE_FLOAT
:
590 load
->value
.u
[i
] = constant
->value
.u
[matrix_offset
+ i
];
593 load
->value
.u
[i
] = constant
->value
.b
[matrix_offset
+ i
] ?
594 NIR_TRUE
: NIR_FALSE
;
597 unreachable("Invalid immediate type");
605 nir_cf_node_get_function(nir_cf_node
*node
)
607 while (node
->type
!= nir_cf_node_function
) {
611 return nir_cf_node_as_function(node
);
615 add_use_cb(nir_src
*src
, void *state
)
617 nir_instr
*instr
= state
;
619 src
->parent_instr
= instr
;
620 list_addtail(&src
->use_link
,
621 src
->is_ssa
? &src
->ssa
->uses
: &src
->reg
.reg
->uses
);
627 add_ssa_def_cb(nir_ssa_def
*def
, void *state
)
629 nir_instr
*instr
= state
;
631 if (instr
->block
&& def
->index
== UINT_MAX
) {
632 nir_function_impl
*impl
=
633 nir_cf_node_get_function(&instr
->block
->cf_node
);
635 def
->index
= impl
->ssa_alloc
++;
642 add_reg_def_cb(nir_dest
*dest
, void *state
)
644 nir_instr
*instr
= state
;
647 dest
->reg
.parent_instr
= instr
;
648 list_addtail(&dest
->reg
.def_link
, &dest
->reg
.reg
->defs
);
655 add_defs_uses(nir_instr
*instr
)
657 nir_foreach_src(instr
, add_use_cb
, instr
);
658 nir_foreach_dest(instr
, add_reg_def_cb
, instr
);
659 nir_foreach_ssa_def(instr
, add_ssa_def_cb
, instr
);
663 nir_instr_insert_before(nir_instr
*instr
, nir_instr
*before
)
665 assert(before
->type
!= nir_instr_type_jump
);
666 before
->block
= instr
->block
;
667 add_defs_uses(before
);
668 exec_node_insert_node_before(&instr
->node
, &before
->node
);
672 nir_instr_insert_after(nir_instr
*instr
, nir_instr
*after
)
674 if (after
->type
== nir_instr_type_jump
) {
675 assert(instr
== nir_block_last_instr(instr
->block
));
676 assert(instr
->type
!= nir_instr_type_jump
);
679 after
->block
= instr
->block
;
680 add_defs_uses(after
);
681 exec_node_insert_after(&instr
->node
, &after
->node
);
683 if (after
->type
== nir_instr_type_jump
)
684 nir_handle_add_jump(after
->block
);
688 nir_instr_insert_before_block(nir_block
*block
, nir_instr
*before
)
690 if (before
->type
== nir_instr_type_jump
)
691 assert(exec_list_is_empty(&block
->instr_list
));
693 before
->block
= block
;
694 add_defs_uses(before
);
695 exec_list_push_head(&block
->instr_list
, &before
->node
);
697 if (before
->type
== nir_instr_type_jump
)
698 nir_handle_add_jump(block
);
702 nir_instr_insert_after_block(nir_block
*block
, nir_instr
*after
)
704 if (after
->type
== nir_instr_type_jump
) {
705 assert(exec_list_is_empty(&block
->instr_list
) ||
706 nir_block_last_instr(block
)->type
!= nir_instr_type_jump
);
709 after
->block
= block
;
710 add_defs_uses(after
);
711 exec_list_push_tail(&block
->instr_list
, &after
->node
);
713 if (after
->type
== nir_instr_type_jump
)
714 nir_handle_add_jump(block
);
718 nir_instr_insert_before_cf(nir_cf_node
*node
, nir_instr
*before
)
720 if (node
->type
== nir_cf_node_block
) {
721 nir_instr_insert_before_block(nir_cf_node_as_block(node
), before
);
723 nir_cf_node
*prev
= nir_cf_node_prev(node
);
724 assert(prev
->type
== nir_cf_node_block
);
725 nir_block
*prev_block
= nir_cf_node_as_block(prev
);
727 nir_instr_insert_before_block(prev_block
, before
);
732 nir_instr_insert_after_cf(nir_cf_node
*node
, nir_instr
*after
)
734 if (node
->type
== nir_cf_node_block
) {
735 nir_instr_insert_after_block(nir_cf_node_as_block(node
), after
);
737 nir_cf_node
*next
= nir_cf_node_next(node
);
738 assert(next
->type
== nir_cf_node_block
);
739 nir_block
*next_block
= nir_cf_node_as_block(next
);
741 nir_instr_insert_before_block(next_block
, after
);
746 nir_instr_insert_before_cf_list(struct exec_list
*list
, nir_instr
*before
)
748 nir_cf_node
*first_node
= exec_node_data(nir_cf_node
,
749 exec_list_get_head(list
), node
);
750 nir_instr_insert_before_cf(first_node
, before
);
754 nir_instr_insert_after_cf_list(struct exec_list
*list
, nir_instr
*after
)
756 nir_cf_node
*last_node
= exec_node_data(nir_cf_node
,
757 exec_list_get_tail(list
), node
);
758 nir_instr_insert_after_cf(last_node
, after
);
762 remove_use_cb(nir_src
*src
, void *state
)
764 list_del(&src
->use_link
);
770 remove_def_cb(nir_dest
*dest
, void *state
)
773 list_del(&dest
->reg
.def_link
);
779 remove_defs_uses(nir_instr
*instr
)
781 nir_foreach_dest(instr
, remove_def_cb
, instr
);
782 nir_foreach_src(instr
, remove_use_cb
, instr
);
785 void nir_instr_remove(nir_instr
*instr
)
787 remove_defs_uses(instr
);
788 exec_node_remove(&instr
->node
);
790 if (instr
->type
== nir_instr_type_jump
) {
791 nir_jump_instr
*jump_instr
= nir_instr_as_jump(instr
);
792 nir_handle_remove_jump(instr
->block
, jump_instr
->type
);
799 nir_index_local_regs(nir_function_impl
*impl
)
802 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
803 reg
->index
= index
++;
805 impl
->reg_alloc
= index
;
809 nir_index_global_regs(nir_shader
*shader
)
812 foreach_list_typed(nir_register
, reg
, node
, &shader
->registers
) {
813 reg
->index
= index
++;
815 shader
->reg_alloc
= index
;
819 visit_alu_dest(nir_alu_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
821 return cb(&instr
->dest
.dest
, state
);
825 visit_intrinsic_dest(nir_intrinsic_instr
*instr
, nir_foreach_dest_cb cb
,
828 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
829 return cb(&instr
->dest
, state
);
835 visit_texture_dest(nir_tex_instr
*instr
, nir_foreach_dest_cb cb
,
838 return cb(&instr
->dest
, state
);
842 visit_phi_dest(nir_phi_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
844 return cb(&instr
->dest
, state
);
848 visit_parallel_copy_dest(nir_parallel_copy_instr
*instr
,
849 nir_foreach_dest_cb cb
, void *state
)
851 nir_foreach_parallel_copy_entry(instr
, entry
) {
852 if (!cb(&entry
->dest
, state
))
860 nir_foreach_dest(nir_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
862 switch (instr
->type
) {
863 case nir_instr_type_alu
:
864 return visit_alu_dest(nir_instr_as_alu(instr
), cb
, state
);
865 case nir_instr_type_intrinsic
:
866 return visit_intrinsic_dest(nir_instr_as_intrinsic(instr
), cb
, state
);
867 case nir_instr_type_tex
:
868 return visit_texture_dest(nir_instr_as_tex(instr
), cb
, state
);
869 case nir_instr_type_phi
:
870 return visit_phi_dest(nir_instr_as_phi(instr
), cb
, state
);
871 case nir_instr_type_parallel_copy
:
872 return visit_parallel_copy_dest(nir_instr_as_parallel_copy(instr
),
875 case nir_instr_type_load_const
:
876 case nir_instr_type_ssa_undef
:
877 case nir_instr_type_call
:
878 case nir_instr_type_jump
:
882 unreachable("Invalid instruction type");
889 struct foreach_ssa_def_state
{
890 nir_foreach_ssa_def_cb cb
;
895 nir_ssa_def_visitor(nir_dest
*dest
, void *void_state
)
897 struct foreach_ssa_def_state
*state
= void_state
;
900 return state
->cb(&dest
->ssa
, state
->client_state
);
906 nir_foreach_ssa_def(nir_instr
*instr
, nir_foreach_ssa_def_cb cb
, void *state
)
908 switch (instr
->type
) {
909 case nir_instr_type_alu
:
910 case nir_instr_type_tex
:
911 case nir_instr_type_intrinsic
:
912 case nir_instr_type_phi
:
913 case nir_instr_type_parallel_copy
: {
914 struct foreach_ssa_def_state foreach_state
= {cb
, state
};
915 return nir_foreach_dest(instr
, nir_ssa_def_visitor
, &foreach_state
);
918 case nir_instr_type_load_const
:
919 return cb(&nir_instr_as_load_const(instr
)->def
, state
);
920 case nir_instr_type_ssa_undef
:
921 return cb(&nir_instr_as_ssa_undef(instr
)->def
, state
);
922 case nir_instr_type_call
:
923 case nir_instr_type_jump
:
926 unreachable("Invalid instruction type");
931 visit_src(nir_src
*src
, nir_foreach_src_cb cb
, void *state
)
935 if (!src
->is_ssa
&& src
->reg
.indirect
)
936 return cb(src
->reg
.indirect
, state
);
941 visit_deref_array_src(nir_deref_array
*deref
, nir_foreach_src_cb cb
,
944 if (deref
->deref_array_type
== nir_deref_array_type_indirect
)
945 return visit_src(&deref
->indirect
, cb
, state
);
950 visit_deref_src(nir_deref_var
*deref
, nir_foreach_src_cb cb
, void *state
)
952 nir_deref
*cur
= &deref
->deref
;
953 while (cur
!= NULL
) {
954 if (cur
->deref_type
== nir_deref_type_array
)
955 if (!visit_deref_array_src(nir_deref_as_array(cur
), cb
, state
))
965 visit_alu_src(nir_alu_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
967 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
968 if (!visit_src(&instr
->src
[i
].src
, cb
, state
))
975 visit_tex_src(nir_tex_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
977 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++)
978 if (!visit_src(&instr
->src
[i
].src
, cb
, state
))
981 if (instr
->sampler
!= NULL
)
982 if (!visit_deref_src(instr
->sampler
, cb
, state
))
989 visit_intrinsic_src(nir_intrinsic_instr
*instr
, nir_foreach_src_cb cb
,
992 unsigned num_srcs
= nir_intrinsic_infos
[instr
->intrinsic
].num_srcs
;
993 for (unsigned i
= 0; i
< num_srcs
; i
++)
994 if (!visit_src(&instr
->src
[i
], cb
, state
))
998 nir_intrinsic_infos
[instr
->intrinsic
].num_variables
;
999 for (unsigned i
= 0; i
< num_vars
; i
++)
1000 if (!visit_deref_src(instr
->variables
[i
], cb
, state
))
1007 visit_call_src(nir_call_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1013 visit_load_const_src(nir_load_const_instr
*instr
, nir_foreach_src_cb cb
,
1020 visit_phi_src(nir_phi_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1022 nir_foreach_phi_src(instr
, src
) {
1023 if (!visit_src(&src
->src
, cb
, state
))
1031 visit_parallel_copy_src(nir_parallel_copy_instr
*instr
,
1032 nir_foreach_src_cb cb
, void *state
)
1034 nir_foreach_parallel_copy_entry(instr
, entry
) {
1035 if (!visit_src(&entry
->src
, cb
, state
))
1044 nir_foreach_src_cb cb
;
1045 } visit_dest_indirect_state
;
1048 visit_dest_indirect(nir_dest
*dest
, void *_state
)
1050 visit_dest_indirect_state
*state
= (visit_dest_indirect_state
*) _state
;
1052 if (!dest
->is_ssa
&& dest
->reg
.indirect
)
1053 return state
->cb(dest
->reg
.indirect
, state
->state
);
1059 nir_foreach_src(nir_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1061 switch (instr
->type
) {
1062 case nir_instr_type_alu
:
1063 if (!visit_alu_src(nir_instr_as_alu(instr
), cb
, state
))
1066 case nir_instr_type_intrinsic
:
1067 if (!visit_intrinsic_src(nir_instr_as_intrinsic(instr
), cb
, state
))
1070 case nir_instr_type_tex
:
1071 if (!visit_tex_src(nir_instr_as_tex(instr
), cb
, state
))
1074 case nir_instr_type_call
:
1075 if (!visit_call_src(nir_instr_as_call(instr
), cb
, state
))
1078 case nir_instr_type_load_const
:
1079 if (!visit_load_const_src(nir_instr_as_load_const(instr
), cb
, state
))
1082 case nir_instr_type_phi
:
1083 if (!visit_phi_src(nir_instr_as_phi(instr
), cb
, state
))
1086 case nir_instr_type_parallel_copy
:
1087 if (!visit_parallel_copy_src(nir_instr_as_parallel_copy(instr
),
1091 case nir_instr_type_jump
:
1092 case nir_instr_type_ssa_undef
:
1096 unreachable("Invalid instruction type");
1100 visit_dest_indirect_state dest_state
;
1101 dest_state
.state
= state
;
1103 return nir_foreach_dest(instr
, visit_dest_indirect
, &dest_state
);
1107 nir_src_as_const_value(nir_src src
)
1112 if (src
.ssa
->parent_instr
->type
!= nir_instr_type_load_const
)
1115 nir_load_const_instr
*load
= nir_instr_as_load_const(src
.ssa
->parent_instr
);
1117 return &load
->value
;
1121 nir_srcs_equal(nir_src src1
, nir_src src2
)
1125 return src1
.ssa
== src2
.ssa
;
1133 if ((src1
.reg
.indirect
== NULL
) != (src2
.reg
.indirect
== NULL
))
1136 if (src1
.reg
.indirect
) {
1137 if (!nir_srcs_equal(*src1
.reg
.indirect
, *src2
.reg
.indirect
))
1141 return src1
.reg
.reg
== src2
.reg
.reg
&&
1142 src1
.reg
.base_offset
== src2
.reg
.base_offset
;
1148 src_is_valid(const nir_src
*src
)
1150 return src
->is_ssa
? (src
->ssa
!= NULL
) : (src
->reg
.reg
!= NULL
);
1154 src_remove_all_uses(nir_src
*src
)
1156 for (; src
; src
= src
->is_ssa
? NULL
: src
->reg
.indirect
) {
1157 if (!src_is_valid(src
))
1160 list_del(&src
->use_link
);
1165 src_add_all_uses(nir_src
*src
, nir_instr
*parent_instr
, nir_if
*parent_if
)
1167 for (; src
; src
= src
->is_ssa
? NULL
: src
->reg
.indirect
) {
1168 if (!src_is_valid(src
))
1172 src
->parent_instr
= parent_instr
;
1174 list_addtail(&src
->use_link
, &src
->ssa
->uses
);
1176 list_addtail(&src
->use_link
, &src
->reg
.reg
->uses
);
1179 src
->parent_if
= parent_if
;
1181 list_addtail(&src
->use_link
, &src
->ssa
->if_uses
);
1183 list_addtail(&src
->use_link
, &src
->reg
.reg
->if_uses
);
1189 nir_instr_rewrite_src(nir_instr
*instr
, nir_src
*src
, nir_src new_src
)
1191 assert(!src_is_valid(src
) || src
->parent_instr
== instr
);
1193 src_remove_all_uses(src
);
1195 src_add_all_uses(src
, instr
, NULL
);
1199 nir_instr_move_src(nir_instr
*dest_instr
, nir_src
*dest
, nir_src
*src
)
1201 assert(!src_is_valid(dest
) || dest
->parent_instr
== dest_instr
);
1203 src_remove_all_uses(dest
);
1204 src_remove_all_uses(src
);
1206 *src
= NIR_SRC_INIT
;
1207 src_add_all_uses(dest
, dest_instr
, NULL
);
1211 nir_if_rewrite_condition(nir_if
*if_stmt
, nir_src new_src
)
1213 nir_src
*src
= &if_stmt
->condition
;
1214 assert(!src_is_valid(src
) || src
->parent_if
== if_stmt
);
1216 src_remove_all_uses(src
);
1218 src_add_all_uses(src
, NULL
, if_stmt
);
1222 nir_ssa_def_init(nir_instr
*instr
, nir_ssa_def
*def
,
1223 unsigned num_components
, const char *name
)
1226 def
->parent_instr
= instr
;
1227 list_inithead(&def
->uses
);
1228 list_inithead(&def
->if_uses
);
1229 def
->num_components
= num_components
;
1232 nir_function_impl
*impl
=
1233 nir_cf_node_get_function(&instr
->block
->cf_node
);
1235 def
->index
= impl
->ssa_alloc
++;
1237 def
->index
= UINT_MAX
;
1242 nir_ssa_dest_init(nir_instr
*instr
, nir_dest
*dest
,
1243 unsigned num_components
, const char *name
)
1245 dest
->is_ssa
= true;
1246 nir_ssa_def_init(instr
, &dest
->ssa
, num_components
, name
);
1250 nir_ssa_def_rewrite_uses(nir_ssa_def
*def
, nir_src new_src
, void *mem_ctx
)
1252 assert(!new_src
.is_ssa
|| def
!= new_src
.ssa
);
1254 nir_foreach_use_safe(def
, use_src
) {
1255 nir_instr
*src_parent_instr
= use_src
->parent_instr
;
1256 list_del(&use_src
->use_link
);
1257 nir_src_copy(use_src
, &new_src
, mem_ctx
);
1258 src_add_all_uses(use_src
, src_parent_instr
, NULL
);
1261 nir_foreach_if_use_safe(def
, use_src
) {
1262 nir_if
*src_parent_if
= use_src
->parent_if
;
1263 list_del(&use_src
->use_link
);
1264 nir_src_copy(use_src
, &new_src
, mem_ctx
);
1265 src_add_all_uses(use_src
, NULL
, src_parent_if
);
1270 static bool foreach_cf_node(nir_cf_node
*node
, nir_foreach_block_cb cb
,
1271 bool reverse
, void *state
);
1274 foreach_if(nir_if
*if_stmt
, nir_foreach_block_cb cb
, bool reverse
, void *state
)
1277 foreach_list_typed_safe_reverse(nir_cf_node
, node
, node
,
1278 &if_stmt
->else_list
) {
1279 if (!foreach_cf_node(node
, cb
, reverse
, state
))
1283 foreach_list_typed_safe_reverse(nir_cf_node
, node
, node
,
1284 &if_stmt
->then_list
) {
1285 if (!foreach_cf_node(node
, cb
, reverse
, state
))
1289 foreach_list_typed_safe(nir_cf_node
, node
, node
, &if_stmt
->then_list
) {
1290 if (!foreach_cf_node(node
, cb
, reverse
, state
))
1294 foreach_list_typed_safe(nir_cf_node
, node
, node
, &if_stmt
->else_list
) {
1295 if (!foreach_cf_node(node
, cb
, reverse
, state
))
1304 foreach_loop(nir_loop
*loop
, nir_foreach_block_cb cb
, bool reverse
, void *state
)
1307 foreach_list_typed_safe_reverse(nir_cf_node
, node
, node
, &loop
->body
) {
1308 if (!foreach_cf_node(node
, cb
, reverse
, state
))
1312 foreach_list_typed_safe(nir_cf_node
, node
, node
, &loop
->body
) {
1313 if (!foreach_cf_node(node
, cb
, reverse
, state
))
1322 foreach_cf_node(nir_cf_node
*node
, nir_foreach_block_cb cb
,
1323 bool reverse
, void *state
)
1325 switch (node
->type
) {
1326 case nir_cf_node_block
:
1327 return cb(nir_cf_node_as_block(node
), state
);
1328 case nir_cf_node_if
:
1329 return foreach_if(nir_cf_node_as_if(node
), cb
, reverse
, state
);
1330 case nir_cf_node_loop
:
1331 return foreach_loop(nir_cf_node_as_loop(node
), cb
, reverse
, state
);
1335 unreachable("Invalid CFG node type");
1343 nir_foreach_block(nir_function_impl
*impl
, nir_foreach_block_cb cb
, void *state
)
1345 foreach_list_typed_safe(nir_cf_node
, node
, node
, &impl
->body
) {
1346 if (!foreach_cf_node(node
, cb
, false, state
))
1350 return cb(impl
->end_block
, state
);
1354 nir_foreach_block_reverse(nir_function_impl
*impl
, nir_foreach_block_cb cb
,
1357 if (!cb(impl
->end_block
, state
))
1360 foreach_list_typed_safe_reverse(nir_cf_node
, node
, node
, &impl
->body
) {
1361 if (!foreach_cf_node(node
, cb
, true, state
))
1369 nir_block_get_following_if(nir_block
*block
)
1371 if (exec_node_is_tail_sentinel(&block
->cf_node
.node
))
1374 if (nir_cf_node_is_last(&block
->cf_node
))
1377 nir_cf_node
*next_node
= nir_cf_node_next(&block
->cf_node
);
1379 if (next_node
->type
!= nir_cf_node_if
)
1382 return nir_cf_node_as_if(next_node
);
1386 index_block(nir_block
*block
, void *state
)
1388 unsigned *index
= state
;
1389 block
->index
= (*index
)++;
1394 nir_index_blocks(nir_function_impl
*impl
)
1398 if (impl
->valid_metadata
& nir_metadata_block_index
)
1401 nir_foreach_block(impl
, index_block
, &index
);
1403 impl
->num_blocks
= index
;
1407 index_ssa_def_cb(nir_ssa_def
*def
, void *state
)
1409 unsigned *index
= (unsigned *) state
;
1410 def
->index
= (*index
)++;
1416 index_ssa_block(nir_block
*block
, void *state
)
1418 nir_foreach_instr(block
, instr
)
1419 nir_foreach_ssa_def(instr
, index_ssa_def_cb
, state
);
1425 nir_index_ssa_defs(nir_function_impl
*impl
)
1428 nir_foreach_block(impl
, index_ssa_block
, &index
);
1429 impl
->ssa_alloc
= index
;