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)
32 nir_shader_create(void *mem_ctx
)
34 nir_shader
*shader
= ralloc(mem_ctx
, nir_shader
);
36 shader
->uniforms
= _mesa_hash_table_create(shader
, _mesa_key_hash_string
,
37 _mesa_key_string_equal
);
38 shader
->inputs
= _mesa_hash_table_create(shader
, _mesa_key_hash_string
,
39 _mesa_key_string_equal
);
40 shader
->outputs
= _mesa_hash_table_create(shader
, _mesa_key_hash_string
,
41 _mesa_key_string_equal
);
43 shader
->num_user_structures
= 0;
44 shader
->user_structures
= NULL
;
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;
60 reg_create(void *mem_ctx
, struct exec_list
*list
)
62 nir_register
*reg
= ralloc(mem_ctx
, nir_register
);
64 reg
->uses
= _mesa_set_create(mem_ctx
, _mesa_key_pointer_equal
);
65 reg
->defs
= _mesa_set_create(mem_ctx
, _mesa_key_pointer_equal
);
66 reg
->if_uses
= _mesa_set_create(mem_ctx
, _mesa_key_pointer_equal
);
68 reg
->num_components
= 0;
69 reg
->num_array_elems
= 0;
70 reg
->is_packed
= false;
73 exec_list_push_tail(list
, ®
->node
);
79 nir_global_reg_create(nir_shader
*shader
)
81 nir_register
*reg
= reg_create(shader
, &shader
->registers
);
82 reg
->index
= shader
->reg_alloc
++;
83 reg
->is_global
= true;
89 nir_local_reg_create(nir_function_impl
*impl
)
91 nir_register
*reg
= reg_create(ralloc_parent(impl
), &impl
->registers
);
92 reg
->index
= impl
->reg_alloc
++;
93 reg
->is_global
= false;
99 nir_reg_remove(nir_register
*reg
)
101 exec_node_remove(®
->node
);
105 nir_function_create(nir_shader
*shader
, const char *name
)
107 nir_function
*func
= ralloc(shader
, nir_function
);
109 exec_list_push_tail(&shader
->functions
, &func
->node
);
110 exec_list_make_empty(&func
->overload_list
);
116 nir_function_overload
*
117 nir_function_overload_create(nir_function
*func
)
119 void *mem_ctx
= ralloc_parent(func
);
121 nir_function_overload
*overload
= ralloc(mem_ctx
, nir_function_overload
);
123 overload
->num_params
= 0;
124 overload
->params
= NULL
;
125 overload
->return_type
= glsl_void_type();
126 overload
->impl
= NULL
;
128 exec_list_push_tail(&func
->overload_list
, &overload
->node
);
129 overload
->function
= func
;
134 nir_src
nir_src_copy(nir_src src
, void *mem_ctx
)
137 ret
.is_ssa
= src
.is_ssa
;
141 ret
.reg
.base_offset
= src
.reg
.base_offset
;
142 ret
.reg
.reg
= src
.reg
.reg
;
143 if (src
.reg
.indirect
) {
144 ret
.reg
.indirect
= ralloc(mem_ctx
, nir_src
);
145 *ret
.reg
.indirect
= *src
.reg
.indirect
;
147 ret
.reg
.indirect
= NULL
;
154 nir_dest
nir_dest_copy(nir_dest dest
, void *mem_ctx
)
157 ret
.is_ssa
= dest
.is_ssa
;
161 ret
.reg
.base_offset
= dest
.reg
.base_offset
;
162 ret
.reg
.reg
= dest
.reg
.reg
;
163 if (dest
.reg
.indirect
) {
164 ret
.reg
.indirect
= ralloc(mem_ctx
, nir_src
);
165 *ret
.reg
.indirect
= *dest
.reg
.indirect
;
167 ret
.reg
.indirect
= NULL
;
175 block_add_pred(nir_block
*block
, nir_block
*pred
)
177 _mesa_set_add(block
->predecessors
, _mesa_hash_pointer(pred
), pred
);
181 cf_init(nir_cf_node
*node
, nir_cf_node_type type
)
183 exec_node_init(&node
->node
);
189 link_blocks(nir_block
*pred
, nir_block
*succ1
, nir_block
*succ2
)
191 pred
->successors
[0] = succ1
;
192 block_add_pred(succ1
, pred
);
194 pred
->successors
[1] = succ2
;
196 block_add_pred(succ2
, pred
);
200 unlink_blocks(nir_block
*pred
, nir_block
*succ
)
202 if (pred
->successors
[0] == succ
) {
203 pred
->successors
[0] = pred
->successors
[1];
204 pred
->successors
[1] = NULL
;
206 assert(pred
->successors
[1] == succ
);
207 pred
->successors
[1] = NULL
;
210 struct set_entry
*entry
= _mesa_set_search(succ
->predecessors
,
211 _mesa_hash_pointer(pred
), pred
);
215 _mesa_set_remove(succ
->predecessors
, entry
);
219 unlink_block_successors(nir_block
*block
)
221 if (block
->successors
[0] != NULL
)
222 unlink_blocks(block
, block
->successors
[0]);
223 if (block
->successors
[1] != NULL
)
224 unlink_blocks(block
, block
->successors
[1]);
229 nir_function_impl_create(nir_function_overload
*overload
)
231 assert(overload
->impl
== NULL
);
233 void *mem_ctx
= ralloc_parent(overload
);
235 nir_function_impl
*impl
= ralloc(mem_ctx
, nir_function_impl
);
237 overload
->impl
= impl
;
238 impl
->overload
= overload
;
240 cf_init(&impl
->cf_node
, nir_cf_node_function
);
242 exec_list_make_empty(&impl
->body
);
243 exec_list_make_empty(&impl
->registers
);
244 exec_list_make_empty(&impl
->locals
);
245 impl
->num_params
= 0;
247 impl
->return_var
= NULL
;
250 impl
->block_index_dirty
= true;
252 /* create start & end blocks */
253 nir_block
*start_block
= nir_block_create(mem_ctx
);
254 nir_block
*end_block
= nir_block_create(mem_ctx
);
255 start_block
->cf_node
.parent
= &impl
->cf_node
;
256 end_block
->cf_node
.parent
= &impl
->cf_node
;
257 impl
->start_block
= start_block
;
258 impl
->end_block
= end_block
;
260 exec_list_push_tail(&impl
->body
, &start_block
->cf_node
.node
);
262 start_block
->successors
[0] = end_block
;
263 block_add_pred(end_block
, start_block
);
269 nir_block_create(void *mem_ctx
)
271 nir_block
*block
= ralloc(mem_ctx
, nir_block
);
273 cf_init(&block
->cf_node
, nir_cf_node_block
);
275 block
->successors
[0] = block
->successors
[1] = NULL
;
276 block
->predecessors
= _mesa_set_create(mem_ctx
, _mesa_key_pointer_equal
);
278 exec_list_make_empty(&block
->instr_list
);
284 src_init(nir_src
*src
)
288 src
->reg
.indirect
= NULL
;
289 src
->reg
.base_offset
= 0;
293 nir_if_create(void *mem_ctx
)
295 nir_if
*if_stmt
= ralloc(mem_ctx
, nir_if
);
297 cf_init(&if_stmt
->cf_node
, nir_cf_node_if
);
298 src_init(&if_stmt
->condition
);
300 nir_block
*then
= nir_block_create(mem_ctx
);
301 exec_list_make_empty(&if_stmt
->then_list
);
302 exec_list_push_tail(&if_stmt
->then_list
, &then
->cf_node
.node
);
303 then
->cf_node
.parent
= &if_stmt
->cf_node
;
305 nir_block
*else_stmt
= nir_block_create(mem_ctx
);
306 exec_list_make_empty(&if_stmt
->else_list
);
307 exec_list_push_tail(&if_stmt
->else_list
, &else_stmt
->cf_node
.node
);
308 else_stmt
->cf_node
.parent
= &if_stmt
->cf_node
;
314 nir_loop_create(void *mem_ctx
)
316 nir_loop
*loop
= ralloc(mem_ctx
, nir_loop
);
318 cf_init(&loop
->cf_node
, nir_cf_node_loop
);
320 nir_block
*body
= nir_block_create(mem_ctx
);
321 exec_list_make_empty(&loop
->body
);
322 exec_list_push_tail(&loop
->body
, &body
->cf_node
.node
);
323 body
->cf_node
.parent
= &loop
->cf_node
;
325 body
->successors
[0] = body
;
326 block_add_pred(body
, body
);
332 instr_init(nir_instr
*instr
, nir_instr_type type
)
336 exec_node_init(&instr
->node
);
340 dest_init(nir_dest
*dest
)
342 dest
->is_ssa
= false;
343 dest
->reg
.reg
= NULL
;
344 dest
->reg
.indirect
= NULL
;
345 dest
->reg
.base_offset
= 0;
349 alu_dest_init(nir_alu_dest
*dest
)
351 dest_init(&dest
->dest
);
352 dest
->saturate
= false;
353 dest
->write_mask
= 0xf;
357 alu_src_init(nir_alu_src
*src
)
360 src
->abs
= src
->negate
= false;
368 nir_alu_instr_create(void *mem_ctx
, nir_op op
)
370 unsigned num_srcs
= nir_op_infos
[op
].num_inputs
;
371 nir_alu_instr
*instr
=
373 sizeof(nir_alu_instr
) + num_srcs
* sizeof(nir_alu_src
));
375 instr_init(&instr
->instr
, nir_instr_type_alu
);
377 alu_dest_init(&instr
->dest
);
378 for (unsigned i
= 0; i
< num_srcs
; i
++)
379 alu_src_init(&instr
->src
[i
]);
381 instr
->has_predicate
= false;
382 src_init(&instr
->predicate
);
388 nir_jump_instr_create(void *mem_ctx
, nir_jump_type type
)
390 nir_jump_instr
*instr
= ralloc(mem_ctx
, nir_jump_instr
);
391 instr_init(&instr
->instr
, nir_instr_type_jump
);
396 nir_load_const_instr
*
397 nir_load_const_instr_create(void *mem_ctx
)
399 nir_load_const_instr
*instr
= ralloc(mem_ctx
, nir_load_const_instr
);
400 instr_init(&instr
->instr
, nir_instr_type_load_const
);
402 dest_init(&instr
->dest
);
403 instr
->num_components
= 0;
404 instr
->array_elems
= 0;
406 instr
->has_predicate
= false;
407 src_init(&instr
->predicate
);
412 nir_intrinsic_instr
*
413 nir_intrinsic_instr_create(void *mem_ctx
, nir_intrinsic_op op
)
415 unsigned num_srcs
= nir_intrinsic_infos
[op
].num_srcs
;
416 nir_intrinsic_instr
*instr
=
418 sizeof(nir_intrinsic_instr
) + num_srcs
* sizeof(nir_src
));
420 instr_init(&instr
->instr
, nir_instr_type_intrinsic
);
421 instr
->intrinsic
= op
;
423 if (nir_intrinsic_infos
[op
].has_dest
)
424 dest_init(&instr
->dest
);
426 for (unsigned i
= 0; i
< num_srcs
; i
++)
427 src_init(&instr
->src
[i
]);
429 instr
->has_predicate
= false;
430 src_init(&instr
->predicate
);
436 nir_call_instr_create(void *mem_ctx
, nir_function_overload
*callee
)
438 nir_call_instr
*instr
= ralloc(mem_ctx
, nir_call_instr
);
439 instr_init(&instr
->instr
, nir_instr_type_call
);
441 instr
->callee
= callee
;
442 instr
->num_params
= callee
->num_params
;
443 instr
->params
= ralloc_array(mem_ctx
, nir_deref_var
*, instr
->num_params
);
444 instr
->return_deref
= NULL
;
446 instr
->has_predicate
= false;
447 src_init(&instr
->predicate
);
453 nir_tex_instr_create(void *mem_ctx
, unsigned num_srcs
)
455 nir_tex_instr
*instr
= ralloc(mem_ctx
, nir_tex_instr
);
456 instr_init(&instr
->instr
, nir_instr_type_texture
);
458 dest_init(&instr
->dest
);
460 instr
->num_srcs
= num_srcs
;
461 for (unsigned i
= 0; i
< num_srcs
; i
++)
462 src_init(&instr
->src
[i
]);
464 instr
->has_predicate
= false;
465 src_init(&instr
->predicate
);
471 nir_phi_instr_create(void *mem_ctx
)
473 nir_phi_instr
*instr
= ralloc(mem_ctx
, nir_phi_instr
);
474 instr_init(&instr
->instr
, nir_instr_type_phi
);
476 dest_init(&instr
->dest
);
477 exec_list_make_empty(&instr
->srcs
);
481 nir_ssa_undef_instr
*
482 nir_ssa_undef_instr_create(void *mem_ctx
)
484 nir_ssa_undef_instr
*instr
= ralloc(mem_ctx
, nir_ssa_undef_instr
);
485 instr_init(&instr
->instr
, nir_instr_type_ssa_undef
);
487 instr
->def
.name
= NULL
;
488 instr
->def
.parent_instr
= &instr
->instr
;
494 nir_deref_var_create(void *mem_ctx
, nir_variable
*var
)
496 nir_deref_var
*deref
= ralloc(mem_ctx
, nir_deref_var
);
497 deref
->deref
.deref_type
= nir_deref_type_var
;
498 deref
->deref
.child
= NULL
;
499 deref
->deref
.type
= var
->type
;
505 nir_deref_array_create(void *mem_ctx
)
507 nir_deref_array
*deref
= ralloc(mem_ctx
, nir_deref_array
);
508 deref
->deref
.deref_type
= nir_deref_type_array
;
509 deref
->deref
.child
= NULL
;
510 deref
->has_indirect
= false;
511 src_init(&deref
->indirect
);
512 deref
->base_offset
= 0;
517 nir_deref_struct_create(void *mem_ctx
, const char *field
)
519 nir_deref_struct
*deref
= ralloc(mem_ctx
, nir_deref_struct
);
520 deref
->deref
.deref_type
= nir_deref_type_struct
;
521 deref
->deref
.child
= NULL
;
522 deref
->elem
= ralloc_strdup(deref
, field
);
526 static nir_deref_var
*
527 copy_deref_var(void *mem_ctx
, nir_deref_var
*deref
)
529 nir_deref_var
*ret
= nir_deref_var_create(mem_ctx
, deref
->var
);
530 if (deref
->deref
.child
)
531 ret
->deref
.child
= nir_copy_deref(mem_ctx
, deref
->deref
.child
);
535 static nir_deref_array
*
536 copy_deref_array(void *mem_ctx
, nir_deref_array
*deref
)
538 nir_deref_array
*ret
= nir_deref_array_create(mem_ctx
);
539 ret
->base_offset
= deref
->base_offset
;
540 if (deref
->has_indirect
)
541 ret
->indirect
= deref
->indirect
;
542 ret
->deref
.type
= deref
->deref
.type
;
543 if (deref
->deref
.child
)
544 ret
->deref
.child
= nir_copy_deref(mem_ctx
, deref
->deref
.child
);
548 static nir_deref_struct
*
549 copy_deref_struct(void *mem_ctx
, nir_deref_struct
*deref
)
551 nir_deref_struct
*ret
= nir_deref_struct_create(mem_ctx
, deref
->elem
);
552 ret
->deref
.type
= deref
->deref
.type
;
553 if (deref
->deref
.child
)
554 ret
->deref
.child
= nir_copy_deref(mem_ctx
, deref
->deref
.child
);
559 nir_copy_deref(void *mem_ctx
, nir_deref
*deref
)
561 switch (deref
->deref_type
) {
562 case nir_deref_type_var
:
563 return ©_deref_var(mem_ctx
, nir_deref_as_var(deref
))->deref
;
564 case nir_deref_type_array
:
565 return ©_deref_array(mem_ctx
, nir_deref_as_array(deref
))->deref
;
566 case nir_deref_type_struct
:
567 return ©_deref_struct(mem_ctx
, nir_deref_as_struct(deref
))->deref
;
569 unreachable("Invalid dereference type");
577 * \name Control flow modification
579 * These functions modify the control flow tree while keeping the control flow
580 * graph up-to-date. The invariants respected are:
581 * 1. Each then statement, else statement, or loop body must have at least one
583 * 2. Each if-statement and loop must have one basic block before it and one
585 * 3. Two basic blocks cannot be directly next to each other.
586 * 4. If a basic block has a jump instruction, there must be only one and it
587 * must be at the end of the block.
588 * 5. The CFG must always be connected - this means that we must insert a fake
589 * CFG edge for loops with no break statement.
591 * The purpose of the second one is so that we have places to insert code during
592 * GCM, as well as eliminating the possibility of critical edges.
597 link_non_block_to_block(nir_cf_node
*node
, nir_block
*block
)
599 if (node
->type
== nir_cf_node_if
) {
601 * We're trying to link an if to a block after it; this just means linking
602 * the last block of the then and else branches.
605 nir_if
*if_stmt
= nir_cf_node_as_if(node
);
607 nir_cf_node
*last_then
= nir_if_last_then_node(if_stmt
);
608 assert(last_then
->type
== nir_cf_node_block
);
609 nir_block
*last_then_block
= nir_cf_node_as_block(last_then
);
611 nir_cf_node
*last_else
= nir_if_last_else_node(if_stmt
);
612 assert(last_else
->type
== nir_cf_node_block
);
613 nir_block
*last_else_block
= nir_cf_node_as_block(last_else
);
615 if (exec_list_is_empty(&last_then_block
->instr_list
) ||
616 nir_block_last_instr(last_then_block
)->type
!= nir_instr_type_jump
) {
617 unlink_block_successors(last_then_block
);
618 link_blocks(last_then_block
, block
, NULL
);
621 if (exec_list_is_empty(&last_else_block
->instr_list
) ||
622 nir_block_last_instr(last_else_block
)->type
!= nir_instr_type_jump
) {
623 unlink_block_successors(last_else_block
);
624 link_blocks(last_else_block
, block
, NULL
);
627 assert(node
->type
== nir_cf_node_loop
);
630 * We can only get to this codepath if we're inserting a new loop, or
631 * at least a loop with no break statements; we can't insert break
632 * statements into a loop when we haven't inserted it into the CFG
633 * because we wouldn't know which block comes after the loop
634 * and therefore, which block should be the successor of the block with
635 * the break). Therefore, we need to insert a fake edge (see invariant
639 nir_loop
*loop
= nir_cf_node_as_loop(node
);
641 nir_cf_node
*last
= nir_loop_last_cf_node(loop
);
642 assert(last
->type
== nir_cf_node_block
);
643 nir_block
*last_block
= nir_cf_node_as_block(last
);
645 last_block
->successors
[1] = block
;
646 block_add_pred(block
, last_block
);
651 link_block_to_non_block(nir_block
*block
, nir_cf_node
*node
)
653 if (node
->type
== nir_cf_node_if
) {
655 * We're trying to link a block to an if after it; this just means linking
656 * the block to the first block of the then and else branches.
659 nir_if
*if_stmt
= nir_cf_node_as_if(node
);
661 nir_cf_node
*first_then
= nir_if_first_then_node(if_stmt
);
662 assert(first_then
->type
== nir_cf_node_block
);
663 nir_block
*first_then_block
= nir_cf_node_as_block(first_then
);
665 nir_cf_node
*first_else
= nir_if_first_else_node(if_stmt
);
666 assert(first_else
->type
== nir_cf_node_block
);
667 nir_block
*first_else_block
= nir_cf_node_as_block(first_else
);
669 unlink_block_successors(block
);
670 link_blocks(block
, first_then_block
, first_else_block
);
673 * For similar reasons as the corresponding case in
674 * link_non_block_to_block(), don't worry about if the loop header has
675 * any predecessors that need to be unlinked.
678 assert(node
->type
== nir_cf_node_loop
);
680 nir_loop
*loop
= nir_cf_node_as_loop(node
);
682 nir_cf_node
*loop_header
= nir_loop_first_cf_node(loop
);
683 assert(loop_header
->type
== nir_cf_node_block
);
684 nir_block
*loop_header_block
= nir_cf_node_as_block(loop_header
);
686 unlink_block_successors(block
);
687 link_blocks(block
, loop_header_block
, NULL
);
693 * Takes a basic block and inserts a new empty basic block before it, making its
694 * predecessors point to the new block. This essentially splits the block into
695 * an empty header and a body so that another non-block CF node can be inserted
696 * between the two. Note that this does *not* link the two basic blocks, so
697 * some kind of cleanup *must* be performed after this call.
701 split_block_beginning(nir_block
*block
)
703 nir_block
*new_block
= nir_block_create(ralloc_parent(block
));
704 new_block
->cf_node
.parent
= block
->cf_node
.parent
;
705 exec_node_insert_node_before(&block
->cf_node
.node
, &new_block
->cf_node
.node
);
707 struct set_entry
*entry
;
708 set_foreach(block
->predecessors
, entry
) {
709 nir_block
*pred
= (nir_block
*) entry
->key
;
711 unlink_blocks(pred
, block
);
712 link_blocks(pred
, new_block
, NULL
);
719 * Moves the successors of source to the successors of dest, leaving both
720 * successors of source NULL.
724 move_successors(nir_block
*source
, nir_block
*dest
)
726 nir_block
*succ1
= source
->successors
[0];
728 unlink_blocks(source
, succ1
);
730 nir_block
*succ2
= source
->successors
[1];
732 unlink_blocks(source
, succ2
);
734 unlink_block_successors(dest
);
735 link_blocks(dest
, succ1
, succ2
);
739 split_block_end(nir_block
*block
)
741 nir_block
*new_block
= nir_block_create(ralloc_parent(block
));
742 new_block
->cf_node
.parent
= block
->cf_node
.parent
;
743 exec_node_insert_after(&block
->cf_node
.node
, &new_block
->cf_node
.node
);
745 move_successors(block
, new_block
);
751 * Inserts a non-basic block between two basic blocks and links them together.
755 insert_non_block(nir_block
*before
, nir_cf_node
*node
, nir_block
*after
)
757 node
->parent
= before
->cf_node
.parent
;
758 exec_node_insert_after(&before
->cf_node
.node
, &node
->node
);
759 link_block_to_non_block(before
, node
);
760 link_non_block_to_block(node
, after
);
764 * Inserts a non-basic block before a basic block.
768 insert_non_block_before_block(nir_cf_node
*node
, nir_block
*block
)
770 /* split off the beginning of block into new_block */
771 nir_block
*new_block
= split_block_beginning(block
);
773 /* insert our node in between new_block and block */
774 insert_non_block(new_block
, node
, block
);
778 insert_non_block_after_block(nir_block
*block
, nir_cf_node
*node
)
780 /* split off the end of block into new_block */
781 nir_block
*new_block
= split_block_end(block
);
783 /* insert our node in between block and new_block */
784 insert_non_block(block
, node
, new_block
);
787 /* walk up the control flow tree to find the innermost enclosed loop */
789 nearest_loop(nir_cf_node
*node
)
791 while (node
->type
!= nir_cf_node_loop
) {
795 return nir_cf_node_as_loop(node
);
799 nir_cf_node_get_function(nir_cf_node
*node
)
801 while (node
->type
!= nir_cf_node_function
) {
805 return nir_cf_node_as_function(node
);
809 * update the CFG after a jump instruction has been added to the end of a block
813 handle_jump(nir_block
*block
)
815 nir_instr
*instr
= nir_block_last_instr(block
);
816 nir_jump_instr
*jump_instr
= nir_instr_as_jump(instr
);
818 unlink_block_successors(block
);
820 if (jump_instr
->type
== nir_jump_break
||
821 jump_instr
->type
== nir_jump_continue
) {
822 nir_loop
*loop
= nearest_loop(&block
->cf_node
);
824 if (jump_instr
->type
== nir_jump_continue
) {
825 nir_cf_node
*first_node
= nir_loop_first_cf_node(loop
);
826 assert(first_node
->type
== nir_cf_node_block
);
827 nir_block
*first_block
= nir_cf_node_as_block(first_node
);
828 link_blocks(block
, first_block
, NULL
);
830 nir_cf_node
*after
= nir_cf_node_next(&loop
->cf_node
);
831 assert(after
->type
== nir_cf_node_block
);
832 nir_block
*after_block
= nir_cf_node_as_block(after
);
833 link_blocks(block
, after_block
, NULL
);
835 /* If we inserted a fake link, remove it */
836 nir_cf_node
*last
= nir_loop_last_cf_node(loop
);
837 assert(last
->type
== nir_cf_node_block
);
838 nir_block
*last_block
= nir_cf_node_as_block(last
);
839 if (last_block
->successors
[1] != NULL
)
840 unlink_blocks(last_block
, after_block
);
843 assert(jump_instr
->type
== nir_jump_return
);
844 nir_function_impl
*impl
= nir_cf_node_get_function(&block
->cf_node
);
845 link_blocks(block
, impl
->end_block
, NULL
);
850 handle_remove_jump(nir_block
*block
, nir_jump_type type
)
852 unlink_block_successors(block
);
854 if (exec_node_is_tail_sentinel(block
->cf_node
.node
.next
)) {
855 nir_cf_node
*parent
= block
->cf_node
.parent
;
856 if (parent
->type
== nir_cf_node_if
) {
857 nir_cf_node
*next
= nir_cf_node_next(parent
);
858 assert(next
->type
== nir_cf_node_block
);
859 nir_block
*next_block
= nir_cf_node_as_block(next
);
861 link_blocks(block
, next_block
, NULL
);
863 assert(parent
->type
== nir_cf_node_loop
);
864 nir_loop
*loop
= nir_cf_node_as_loop(parent
);
866 nir_cf_node
*head
= nir_loop_first_cf_node(loop
);
867 assert(head
->type
== nir_cf_node_block
);
868 nir_block
*head_block
= nir_cf_node_as_block(head
);
870 link_blocks(block
, head_block
, NULL
);
873 nir_cf_node
*next
= nir_cf_node_next(&block
->cf_node
);
874 if (next
->type
== nir_cf_node_if
) {
875 nir_if
*next_if
= nir_cf_node_as_if(next
);
877 nir_cf_node
*first_then
= nir_if_first_then_node(next_if
);
878 assert(first_then
->type
== nir_cf_node_block
);
879 nir_block
*first_then_block
= nir_cf_node_as_block(first_then
);
881 nir_cf_node
*first_else
= nir_if_first_else_node(next_if
);
882 assert(first_else
->type
== nir_cf_node_block
);
883 nir_block
*first_else_block
= nir_cf_node_as_block(first_else
);
885 link_blocks(block
, first_then_block
, first_else_block
);
887 assert(next
->type
== nir_cf_node_loop
);
888 nir_loop
*next_loop
= nir_cf_node_as_loop(next
);
890 nir_cf_node
*first
= nir_loop_first_cf_node(next_loop
);
891 assert(first
->type
== nir_cf_node_block
);
892 nir_block
*first_block
= nir_cf_node_as_block(first
);
894 link_blocks(block
, first_block
, NULL
);
898 if (type
== nir_jump_break
) {
899 nir_loop
*loop
= nearest_loop(&block
->cf_node
);
901 nir_cf_node
*next
= nir_cf_node_next(&loop
->cf_node
);
902 assert(next
->type
== nir_cf_node_block
);
903 nir_block
*next_block
= nir_cf_node_as_block(next
);
905 if (next_block
->predecessors
->entries
== 0) {
906 /* insert fake link */
907 nir_cf_node
*last
= nir_loop_last_cf_node(loop
);
908 assert(last
->type
== nir_cf_node_block
);
909 nir_block
*last_block
= nir_cf_node_as_block(last
);
911 last_block
->successors
[1] = next_block
;
912 block_add_pred(next_block
, last_block
);
918 * Inserts a basic block before another by merging the instructions.
920 * @param block the target of the insertion
921 * @param before the block to be inserted - must not have been inserted before
922 * @param has_jump whether \before has a jump instruction at the end
926 insert_block_before_block(nir_block
*block
, nir_block
*before
, bool has_jump
)
928 assert(!has_jump
|| exec_list_is_empty(&block
->instr_list
));
930 foreach_list_typed(nir_instr
, instr
, node
, &before
->instr_list
) {
931 instr
->block
= block
;
934 exec_list_prepend(&block
->instr_list
, &before
->instr_list
);
941 * Inserts a basic block after another by merging the instructions.
943 * @param block the target of the insertion
944 * @param after the block to be inserted - must not have been inserted before
945 * @param has_jump whether \after has a jump instruction at the end
949 insert_block_after_block(nir_block
*block
, nir_block
*after
, bool has_jump
)
951 foreach_list_typed(nir_instr
, instr
, node
, &after
->instr_list
) {
952 instr
->block
= block
;
955 exec_list_append(&block
->instr_list
, &after
->instr_list
);
962 update_if_uses(nir_cf_node
*node
)
964 if (node
->type
!= nir_cf_node_if
)
967 nir_if
*if_stmt
= nir_cf_node_as_if(node
);
968 if (if_stmt
->condition
.is_ssa
)
971 nir_register
*reg
= if_stmt
->condition
.reg
.reg
;
974 _mesa_set_add(reg
->if_uses
, _mesa_hash_pointer(if_stmt
), if_stmt
);
978 nir_cf_node_insert_after(nir_cf_node
*node
, nir_cf_node
*after
)
980 update_if_uses(after
);
982 if (after
->type
== nir_cf_node_block
) {
984 * either node or the one after it must be a basic block, by invariant #2;
985 * in either case, just merge the blocks together.
987 nir_block
*after_block
= nir_cf_node_as_block(after
);
989 bool has_jump
= !exec_list_is_empty(&after_block
->instr_list
) &&
990 nir_block_last_instr(after_block
)->type
== nir_instr_type_jump
;
992 if (node
->type
== nir_cf_node_block
) {
993 insert_block_after_block(nir_cf_node_as_block(node
), after_block
,
996 nir_cf_node
*next
= nir_cf_node_next(node
);
997 assert(next
->type
== nir_cf_node_block
);
998 nir_block
*next_block
= nir_cf_node_as_block(next
);
1000 insert_block_before_block(next_block
, after_block
, has_jump
);
1003 if (node
->type
== nir_cf_node_block
) {
1004 insert_non_block_after_block(nir_cf_node_as_block(node
), after
);
1007 * We have to insert a non-basic block after a non-basic block. Since
1008 * every non-basic block has a basic block after it, this is equivalent
1009 * to inserting a non-basic block before a basic block.
1012 nir_cf_node
*next
= nir_cf_node_next(node
);
1013 assert(next
->type
== nir_cf_node_block
);
1014 nir_block
*next_block
= nir_cf_node_as_block(next
);
1016 insert_non_block_before_block(after
, next_block
);
1020 nir_function_impl
*impl
= nir_cf_node_get_function(node
);
1021 impl
->block_index_dirty
= true;
1025 nir_cf_node_insert_before(nir_cf_node
*node
, nir_cf_node
*before
)
1027 update_if_uses(before
);
1029 if (before
->type
== nir_cf_node_block
) {
1030 nir_block
*before_block
= nir_cf_node_as_block(before
);
1032 bool has_jump
= !exec_list_is_empty(&before_block
->instr_list
) &&
1033 nir_block_last_instr(before_block
)->type
== nir_instr_type_jump
;
1035 if (node
->type
== nir_cf_node_block
) {
1036 insert_block_before_block(nir_cf_node_as_block(node
), before_block
,
1039 nir_cf_node
*prev
= nir_cf_node_prev(node
);
1040 assert(prev
->type
== nir_cf_node_block
);
1041 nir_block
*prev_block
= nir_cf_node_as_block(prev
);
1043 insert_block_after_block(prev_block
, before_block
, has_jump
);
1046 if (node
->type
== nir_cf_node_block
) {
1047 insert_non_block_before_block(before
, nir_cf_node_as_block(node
));
1050 * We have to insert a non-basic block before a non-basic block. This
1051 * is equivalent to inserting a non-basic block after a basic block.
1054 nir_cf_node
*prev_node
= nir_cf_node_prev(node
);
1055 assert(prev_node
->type
== nir_cf_node_block
);
1056 nir_block
*prev_block
= nir_cf_node_as_block(prev_node
);
1058 insert_non_block_after_block(prev_block
, before
);
1062 nir_function_impl
*impl
= nir_cf_node_get_function(node
);
1063 impl
->block_index_dirty
= true;
1067 nir_cf_node_insert_begin(struct exec_list
*list
, nir_cf_node
*node
)
1069 nir_cf_node
*begin
= exec_node_data(nir_cf_node
, list
->head
, node
);
1070 nir_cf_node_insert_before(begin
, node
);
1074 nir_cf_node_insert_end(struct exec_list
*list
, nir_cf_node
*node
)
1076 nir_cf_node
*end
= exec_node_data(nir_cf_node
, list
->tail_pred
, node
);
1077 nir_cf_node_insert_after(end
, node
);
1081 * Stitch two basic blocks together into one. The aggregate must have the same
1082 * predecessors as the first and the same successors as the second.
1086 stitch_blocks(nir_block
*before
, nir_block
*after
)
1089 * We move after into before, so we have to deal with up to 2 successors vs.
1090 * possibly a large number of predecessors.
1092 * TODO: special case when before is empty and after isn't?
1095 move_successors(after
, before
);
1097 foreach_list_typed(nir_instr
, instr
, node
, &after
->instr_list
) {
1098 instr
->block
= before
;
1101 exec_list_append(&before
->instr_list
, &after
->instr_list
);
1102 exec_node_remove(&after
->cf_node
.node
);
1106 nir_cf_node_remove(nir_cf_node
*node
)
1108 nir_function_impl
*impl
= nir_cf_node_get_function(node
);
1109 impl
->block_index_dirty
= true;
1111 if (node
->type
== nir_cf_node_block
) {
1113 * Basic blocks can't really be removed by themselves, since they act as
1114 * padding between the non-basic blocks. So all we do here is empty the
1115 * block of instructions.
1117 * TODO: could we assert here?
1119 exec_list_make_empty(&nir_cf_node_as_block(node
)->instr_list
);
1121 nir_cf_node
*before
= nir_cf_node_prev(node
);
1122 assert(before
->type
== nir_cf_node_block
);
1123 nir_block
*before_block
= nir_cf_node_as_block(before
);
1125 nir_cf_node
*after
= nir_cf_node_next(node
);
1126 assert(after
->type
== nir_cf_node_block
);
1127 nir_block
*after_block
= nir_cf_node_as_block(after
);
1129 exec_node_remove(&node
->node
);
1130 stitch_blocks(before_block
, after_block
);
1135 add_use_cb(nir_src
*src
, void *state
)
1137 nir_instr
*instr
= (nir_instr
*) state
;
1142 nir_register
*reg
= src
->reg
.reg
;
1144 _mesa_set_add(reg
->uses
, _mesa_hash_pointer(instr
), instr
);
1150 add_def_cb(nir_dest
*dest
, void *state
)
1152 nir_instr
*instr
= (nir_instr
*) state
;
1157 nir_register
*reg
= dest
->reg
.reg
;
1159 _mesa_set_add(reg
->defs
, _mesa_hash_pointer(instr
), instr
);
1165 add_defs_uses(nir_instr
*instr
)
1167 nir_foreach_src(instr
, add_use_cb
, instr
);
1168 nir_foreach_dest(instr
, add_def_cb
, instr
);
1172 nir_instr_insert_before(nir_instr
*instr
, nir_instr
*before
)
1174 assert(before
->type
!= nir_instr_type_jump
);
1175 before
->block
= instr
->block
;
1176 add_defs_uses(before
);
1177 exec_node_insert_node_before(&instr
->node
, &before
->node
);
1181 nir_instr_insert_after(nir_instr
*instr
, nir_instr
*after
)
1183 if (after
->type
== nir_instr_type_jump
) {
1184 assert(instr
== nir_block_last_instr(instr
->block
));
1185 assert(instr
->type
!= nir_instr_type_jump
);
1188 after
->block
= instr
->block
;
1189 add_defs_uses(after
);
1190 exec_node_insert_after(&instr
->node
, &after
->node
);
1192 if (after
->type
== nir_instr_type_jump
)
1193 handle_jump(after
->block
);
1197 nir_instr_insert_before_block(nir_block
*block
, nir_instr
*before
)
1199 if (before
->type
== nir_instr_type_jump
)
1200 assert(exec_list_is_empty(&block
->instr_list
));
1202 before
->block
= block
;
1203 add_defs_uses(before
);
1204 exec_list_push_head(&block
->instr_list
, &before
->node
);
1206 if (before
->type
== nir_instr_type_jump
)
1211 nir_instr_insert_after_block(nir_block
*block
, nir_instr
*after
)
1213 if (after
->type
== nir_instr_type_jump
) {
1214 assert(exec_list_is_empty(&block
->instr_list
) ||
1215 nir_block_last_instr(block
)->type
!= nir_instr_type_jump
);
1218 after
->block
= block
;
1219 add_defs_uses(after
);
1220 exec_list_push_tail(&block
->instr_list
, &after
->node
);
1222 if (after
->type
== nir_instr_type_jump
)
1227 nir_instr_insert_before_cf(nir_cf_node
*node
, nir_instr
*before
)
1229 if (node
->type
== nir_cf_node_block
) {
1230 nir_instr_insert_before_block(nir_cf_node_as_block(node
), before
);
1232 nir_cf_node
*prev
= nir_cf_node_prev(node
);
1233 assert(prev
->type
== nir_cf_node_block
);
1234 nir_block
*prev_block
= nir_cf_node_as_block(prev
);
1236 nir_instr_insert_before_block(prev_block
, before
);
1241 nir_instr_insert_after_cf(nir_cf_node
*node
, nir_instr
*after
)
1243 if (node
->type
== nir_cf_node_block
) {
1244 nir_instr_insert_after_block(nir_cf_node_as_block(node
), after
);
1246 nir_cf_node
*next
= nir_cf_node_next(node
);
1247 assert(next
->type
== nir_cf_node_block
);
1248 nir_block
*next_block
= nir_cf_node_as_block(next
);
1250 nir_instr_insert_before_block(next_block
, after
);
1255 nir_instr_insert_before_cf_list(struct exec_list
*list
, nir_instr
*before
)
1257 nir_cf_node
*first_node
= exec_node_data(nir_cf_node
,
1258 exec_list_get_head(list
), node
);
1259 nir_instr_insert_before_cf(first_node
, before
);
1263 nir_instr_insert_after_cf_list(struct exec_list
*list
, nir_instr
*after
)
1265 nir_cf_node
*last_node
= exec_node_data(nir_cf_node
,
1266 exec_list_get_tail(list
), node
);
1267 nir_instr_insert_after_cf(last_node
, after
);
1271 remove_use_cb(nir_src
*src
, void *state
)
1273 nir_instr
*instr
= (nir_instr
*) state
;
1278 nir_register
*reg
= src
->reg
.reg
;
1280 struct set_entry
*entry
= _mesa_set_search(reg
->uses
,
1281 _mesa_hash_pointer(instr
),
1284 _mesa_set_remove(reg
->uses
, entry
);
1290 remove_def_cb(nir_dest
*dest
, void *state
)
1292 nir_instr
*instr
= (nir_instr
*) state
;
1297 nir_register
*reg
= dest
->reg
.reg
;
1299 struct set_entry
*entry
= _mesa_set_search(reg
->defs
,
1300 _mesa_hash_pointer(instr
),
1303 _mesa_set_remove(reg
->defs
, entry
);
1309 remove_defs_uses(nir_instr
*instr
)
1311 nir_foreach_dest(instr
, remove_def_cb
, instr
);
1312 nir_foreach_src(instr
, remove_use_cb
, instr
);
1315 void nir_instr_remove(nir_instr
*instr
)
1317 remove_defs_uses(instr
);
1318 exec_node_remove(&instr
->node
);
1320 if (instr
->type
== nir_instr_type_jump
) {
1321 nir_jump_instr
*jump_instr
= nir_instr_as_jump(instr
);
1322 handle_remove_jump(instr
->block
, jump_instr
->type
);
1329 nir_index_local_regs(nir_function_impl
*impl
)
1332 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
1333 reg
->index
= index
++;
1338 nir_index_global_regs(nir_shader
*shader
)
1341 foreach_list_typed(nir_register
, reg
, node
, &shader
->registers
) {
1342 reg
->index
= index
++;
1347 visit_alu_dest(nir_alu_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
1349 return cb(&instr
->dest
.dest
, state
);
1353 visit_intrinsic_dest(nir_intrinsic_instr
*instr
, nir_foreach_dest_cb cb
,
1356 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
1357 return cb(&instr
->dest
, state
);
1363 visit_texture_dest(nir_tex_instr
*instr
, nir_foreach_dest_cb cb
,
1366 return cb(&instr
->dest
, state
);
1370 visit_load_const_dest(nir_load_const_instr
*instr
, nir_foreach_dest_cb cb
,
1373 return cb(&instr
->dest
, state
);
1377 visit_phi_dest(nir_phi_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
1379 return cb(&instr
->dest
, state
);
1383 nir_foreach_dest(nir_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
1385 switch (instr
->type
) {
1386 case nir_instr_type_alu
:
1387 return visit_alu_dest(nir_instr_as_alu(instr
), cb
, state
);
1388 case nir_instr_type_intrinsic
:
1389 return visit_intrinsic_dest(nir_instr_as_intrinsic(instr
), cb
, state
);
1390 case nir_instr_type_texture
:
1391 return visit_texture_dest(nir_instr_as_texture(instr
), cb
, state
);
1392 case nir_instr_type_load_const
:
1393 return visit_load_const_dest(nir_instr_as_load_const(instr
), cb
, state
);
1394 case nir_instr_type_phi
:
1395 return visit_phi_dest(nir_instr_as_phi(instr
), cb
, state
);
1398 case nir_instr_type_ssa_undef
:
1399 case nir_instr_type_call
:
1400 case nir_instr_type_jump
:
1404 unreachable("Invalid instruction type");
1412 visit_src(nir_src
*src
, nir_foreach_src_cb cb
, void *state
)
1414 if (!cb(src
, state
))
1416 if (!src
->is_ssa
&& src
->reg
.indirect
)
1417 return cb(src
->reg
.indirect
, state
);
1422 visit_deref_array_src(nir_deref_array
*deref
, nir_foreach_src_cb cb
,
1425 if (deref
->has_indirect
)
1426 return visit_src(&deref
->indirect
, cb
, state
);
1431 visit_deref_src(nir_deref_var
*deref
, nir_foreach_src_cb cb
, void *state
)
1433 nir_deref
*cur
= &deref
->deref
;
1434 while (cur
!= NULL
) {
1435 if (cur
->deref_type
== nir_deref_type_array
)
1436 if (!visit_deref_array_src(nir_deref_as_array(cur
), cb
, state
))
1446 visit_alu_src(nir_alu_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1448 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1449 if (!visit_src(&instr
->src
[i
].src
, cb
, state
))
1452 if (instr
->has_predicate
)
1453 if (!visit_src(&instr
->predicate
, cb
, state
))
1460 visit_tex_src(nir_tex_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1462 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++)
1463 if (!visit_src(&instr
->src
[i
], cb
, state
))
1466 if (instr
->has_predicate
)
1467 if (!visit_src(&instr
->predicate
, cb
, state
))
1470 if (instr
->sampler
!= NULL
)
1471 if (!visit_deref_src(instr
->sampler
, cb
, state
))
1478 visit_intrinsic_src(nir_intrinsic_instr
*instr
, nir_foreach_src_cb cb
,
1481 unsigned num_srcs
= nir_intrinsic_infos
[instr
->intrinsic
].num_srcs
;
1482 for (unsigned i
= 0; i
< num_srcs
; i
++)
1483 if (!visit_src(&instr
->src
[i
], cb
, state
))
1487 nir_intrinsic_infos
[instr
->intrinsic
].num_variables
;
1488 for (unsigned i
= 0; i
< num_vars
; i
++)
1489 if (!visit_deref_src(instr
->variables
[i
], cb
, state
))
1492 if (instr
->has_predicate
)
1493 if (!visit_src(&instr
->predicate
, cb
, state
))
1500 visit_call_src(nir_call_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1502 if (instr
->has_predicate
)
1503 if (!visit_src(&instr
->predicate
, cb
, state
))
1510 visit_load_const_src(nir_load_const_instr
*instr
, nir_foreach_src_cb cb
,
1513 if (instr
->has_predicate
)
1514 if (!visit_src(&instr
->predicate
, cb
, state
))
1521 visit_phi_src(nir_phi_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1523 foreach_list_typed(nir_phi_src
, src
, node
, &instr
->srcs
) {
1524 if (!visit_src(&src
->src
, cb
, state
))
1533 nir_foreach_src_cb cb
;
1534 } visit_dest_indirect_state
;
1537 visit_dest_indirect(nir_dest
*dest
, void *_state
)
1539 visit_dest_indirect_state
*state
= (visit_dest_indirect_state
*) _state
;
1541 if (!dest
->is_ssa
&& dest
->reg
.indirect
)
1542 return state
->cb(dest
->reg
.indirect
, state
->state
);
1548 nir_foreach_src(nir_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1550 switch (instr
->type
) {
1551 case nir_instr_type_alu
:
1552 if (!visit_alu_src(nir_instr_as_alu(instr
), cb
, state
))
1555 case nir_instr_type_intrinsic
:
1556 if (!visit_intrinsic_src(nir_instr_as_intrinsic(instr
), cb
, state
))
1559 case nir_instr_type_texture
:
1560 if (!visit_tex_src(nir_instr_as_texture(instr
), cb
, state
))
1563 case nir_instr_type_call
:
1564 if (!visit_call_src(nir_instr_as_call(instr
), cb
, state
))
1567 case nir_instr_type_load_const
:
1568 if (!visit_load_const_src(nir_instr_as_load_const(instr
), cb
, state
))
1571 case nir_instr_type_phi
:
1572 if (!visit_phi_src(nir_instr_as_phi(instr
), cb
, state
))
1575 case nir_instr_type_jump
:
1576 case nir_instr_type_ssa_undef
:
1580 unreachable("Invalid instruction type");
1584 visit_dest_indirect_state dest_state
;
1585 dest_state
.state
= state
;
1587 return nir_foreach_dest(instr
, visit_dest_indirect
, &dest_state
);
1591 static bool foreach_cf_node(nir_cf_node
*node
, nir_foreach_block_cb cb
,
1595 foreach_block(nir_block
*block
, nir_foreach_block_cb cb
, void *state
)
1597 return cb(block
, state
);
1601 foreach_if(nir_if
*if_stmt
, nir_foreach_block_cb cb
, void *state
)
1603 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->then_list
) {
1604 if (!foreach_cf_node(node
, cb
, state
))
1608 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->else_list
) {
1609 if (!foreach_cf_node(node
, cb
, state
))
1617 foreach_loop(nir_loop
*loop
, nir_foreach_block_cb cb
, void *state
)
1619 foreach_list_typed(nir_cf_node
, node
, node
, &loop
->body
) {
1620 if (!foreach_cf_node(node
, cb
, state
))
1628 foreach_cf_node(nir_cf_node
*node
, nir_foreach_block_cb cb
, void *state
)
1630 switch (node
->type
) {
1631 case nir_cf_node_block
:
1632 return foreach_block(nir_cf_node_as_block(node
), cb
, state
);
1633 case nir_cf_node_if
:
1634 return foreach_if(nir_cf_node_as_if(node
), cb
, state
);
1635 case nir_cf_node_loop
:
1636 return foreach_loop(nir_cf_node_as_loop(node
), cb
, state
);
1640 unreachable("Invalid CFG node type");
1648 nir_foreach_block(nir_function_impl
*impl
, nir_foreach_block_cb cb
, void *state
)
1650 foreach_list_typed(nir_cf_node
, node
, node
, &impl
->body
) {
1651 if (!foreach_cf_node(node
, cb
, state
))
1655 cb(impl
->end_block
, state
);
1661 index_block(nir_block
*block
, void *state
)
1663 unsigned *index
= (unsigned *) state
;
1664 block
->index
= (*index
)++;
1669 nir_index_blocks(nir_function_impl
*impl
)
1673 if (!impl
->block_index_dirty
)
1676 nir_foreach_block(impl
, index_block
, &index
);
1678 impl
->num_blocks
= index
;
1679 impl
->block_index_dirty
= false;
1683 index_ssa_def(nir_ssa_def
*def
, unsigned *index
)
1685 def
->index
= (*index
)++;
1689 index_ssa_def_cb(nir_dest
*dest
, void *state
)
1691 unsigned *index
= (unsigned *) state
;
1693 index_ssa_def(&dest
->ssa
, index
);
1698 index_ssa_undef(nir_ssa_undef_instr
*instr
, unsigned *index
)
1700 index_ssa_def(&instr
->def
, index
);
1704 index_ssa_block(nir_block
*block
, void *state
)
1706 unsigned *index
= (unsigned *) state
;
1708 nir_foreach_instr(block
, instr
) {
1709 if (instr
->type
== nir_instr_type_ssa_undef
)
1710 index_ssa_undef(nir_instr_as_ssa_undef(instr
), index
);
1712 nir_foreach_dest(instr
, index_ssa_def_cb
, state
);
1719 nir_index_ssa_defs(nir_function_impl
*impl
)
1722 nir_foreach_block(impl
, index_ssa_block
, &index
);