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
);
112 func
->shader
= shader
;
117 nir_function_overload
*
118 nir_function_overload_create(nir_function
*func
)
120 void *mem_ctx
= ralloc_parent(func
);
122 nir_function_overload
*overload
= ralloc(mem_ctx
, nir_function_overload
);
124 overload
->num_params
= 0;
125 overload
->params
= NULL
;
126 overload
->return_type
= glsl_void_type();
127 overload
->impl
= NULL
;
129 exec_list_push_tail(&func
->overload_list
, &overload
->node
);
130 overload
->function
= func
;
135 nir_src
nir_src_copy(nir_src src
, void *mem_ctx
)
138 ret
.is_ssa
= src
.is_ssa
;
142 ret
.reg
.base_offset
= src
.reg
.base_offset
;
143 ret
.reg
.reg
= src
.reg
.reg
;
144 if (src
.reg
.indirect
) {
145 ret
.reg
.indirect
= ralloc(mem_ctx
, nir_src
);
146 *ret
.reg
.indirect
= *src
.reg
.indirect
;
148 ret
.reg
.indirect
= NULL
;
155 nir_dest
nir_dest_copy(nir_dest dest
, void *mem_ctx
)
158 ret
.is_ssa
= dest
.is_ssa
;
162 ret
.reg
.base_offset
= dest
.reg
.base_offset
;
163 ret
.reg
.reg
= dest
.reg
.reg
;
164 if (dest
.reg
.indirect
) {
165 ret
.reg
.indirect
= ralloc(mem_ctx
, nir_src
);
166 *ret
.reg
.indirect
= *dest
.reg
.indirect
;
168 ret
.reg
.indirect
= NULL
;
176 block_add_pred(nir_block
*block
, nir_block
*pred
)
178 _mesa_set_add(block
->predecessors
, _mesa_hash_pointer(pred
), pred
);
182 cf_init(nir_cf_node
*node
, nir_cf_node_type type
)
184 exec_node_init(&node
->node
);
190 link_blocks(nir_block
*pred
, nir_block
*succ1
, nir_block
*succ2
)
192 pred
->successors
[0] = succ1
;
193 block_add_pred(succ1
, pred
);
195 pred
->successors
[1] = succ2
;
197 block_add_pred(succ2
, pred
);
201 unlink_blocks(nir_block
*pred
, nir_block
*succ
)
203 if (pred
->successors
[0] == succ
) {
204 pred
->successors
[0] = pred
->successors
[1];
205 pred
->successors
[1] = NULL
;
207 assert(pred
->successors
[1] == succ
);
208 pred
->successors
[1] = NULL
;
211 struct set_entry
*entry
= _mesa_set_search(succ
->predecessors
,
212 _mesa_hash_pointer(pred
), pred
);
216 _mesa_set_remove(succ
->predecessors
, entry
);
220 unlink_block_successors(nir_block
*block
)
222 if (block
->successors
[0] != NULL
)
223 unlink_blocks(block
, block
->successors
[0]);
224 if (block
->successors
[1] != NULL
)
225 unlink_blocks(block
, block
->successors
[1]);
230 nir_function_impl_create(nir_function_overload
*overload
)
232 assert(overload
->impl
== NULL
);
234 void *mem_ctx
= ralloc_parent(overload
);
236 nir_function_impl
*impl
= ralloc(mem_ctx
, nir_function_impl
);
238 overload
->impl
= impl
;
239 impl
->overload
= overload
;
241 cf_init(&impl
->cf_node
, nir_cf_node_function
);
243 exec_list_make_empty(&impl
->body
);
244 exec_list_make_empty(&impl
->registers
);
245 exec_list_make_empty(&impl
->locals
);
246 impl
->num_params
= 0;
248 impl
->return_var
= NULL
;
251 impl
->valid_metadata
= nir_metadata_none
;
253 /* create start & end blocks */
254 nir_block
*start_block
= nir_block_create(mem_ctx
);
255 nir_block
*end_block
= nir_block_create(mem_ctx
);
256 start_block
->cf_node
.parent
= &impl
->cf_node
;
257 end_block
->cf_node
.parent
= &impl
->cf_node
;
258 impl
->start_block
= start_block
;
259 impl
->end_block
= end_block
;
261 exec_list_push_tail(&impl
->body
, &start_block
->cf_node
.node
);
263 start_block
->successors
[0] = end_block
;
264 block_add_pred(end_block
, start_block
);
270 nir_block_create(void *mem_ctx
)
272 nir_block
*block
= ralloc(mem_ctx
, nir_block
);
274 cf_init(&block
->cf_node
, nir_cf_node_block
);
276 block
->successors
[0] = block
->successors
[1] = NULL
;
277 block
->predecessors
= _mesa_set_create(mem_ctx
, _mesa_key_pointer_equal
);
278 block
->imm_dom
= NULL
;
279 block
->dom_frontier
= _mesa_set_create(mem_ctx
, _mesa_key_pointer_equal
);
281 exec_list_make_empty(&block
->instr_list
);
287 src_init(nir_src
*src
)
291 src
->reg
.indirect
= NULL
;
292 src
->reg
.base_offset
= 0;
296 nir_if_create(void *mem_ctx
)
298 nir_if
*if_stmt
= ralloc(mem_ctx
, nir_if
);
300 cf_init(&if_stmt
->cf_node
, nir_cf_node_if
);
301 src_init(&if_stmt
->condition
);
303 nir_block
*then
= nir_block_create(mem_ctx
);
304 exec_list_make_empty(&if_stmt
->then_list
);
305 exec_list_push_tail(&if_stmt
->then_list
, &then
->cf_node
.node
);
306 then
->cf_node
.parent
= &if_stmt
->cf_node
;
308 nir_block
*else_stmt
= nir_block_create(mem_ctx
);
309 exec_list_make_empty(&if_stmt
->else_list
);
310 exec_list_push_tail(&if_stmt
->else_list
, &else_stmt
->cf_node
.node
);
311 else_stmt
->cf_node
.parent
= &if_stmt
->cf_node
;
317 nir_loop_create(void *mem_ctx
)
319 nir_loop
*loop
= ralloc(mem_ctx
, nir_loop
);
321 cf_init(&loop
->cf_node
, nir_cf_node_loop
);
323 nir_block
*body
= nir_block_create(mem_ctx
);
324 exec_list_make_empty(&loop
->body
);
325 exec_list_push_tail(&loop
->body
, &body
->cf_node
.node
);
326 body
->cf_node
.parent
= &loop
->cf_node
;
328 body
->successors
[0] = body
;
329 block_add_pred(body
, body
);
335 instr_init(nir_instr
*instr
, nir_instr_type type
)
339 exec_node_init(&instr
->node
);
343 dest_init(nir_dest
*dest
)
345 dest
->is_ssa
= false;
346 dest
->reg
.reg
= NULL
;
347 dest
->reg
.indirect
= NULL
;
348 dest
->reg
.base_offset
= 0;
352 alu_dest_init(nir_alu_dest
*dest
)
354 dest_init(&dest
->dest
);
355 dest
->saturate
= false;
356 dest
->write_mask
= 0xf;
360 alu_src_init(nir_alu_src
*src
)
363 src
->abs
= src
->negate
= false;
371 nir_alu_instr_create(void *mem_ctx
, nir_op op
)
373 unsigned num_srcs
= nir_op_infos
[op
].num_inputs
;
374 nir_alu_instr
*instr
=
376 sizeof(nir_alu_instr
) + num_srcs
* sizeof(nir_alu_src
));
378 instr_init(&instr
->instr
, nir_instr_type_alu
);
380 alu_dest_init(&instr
->dest
);
381 for (unsigned i
= 0; i
< num_srcs
; i
++)
382 alu_src_init(&instr
->src
[i
]);
384 instr
->has_predicate
= false;
385 src_init(&instr
->predicate
);
391 nir_jump_instr_create(void *mem_ctx
, nir_jump_type type
)
393 nir_jump_instr
*instr
= ralloc(mem_ctx
, nir_jump_instr
);
394 instr_init(&instr
->instr
, nir_instr_type_jump
);
399 nir_load_const_instr
*
400 nir_load_const_instr_create(void *mem_ctx
)
402 nir_load_const_instr
*instr
= ralloc(mem_ctx
, nir_load_const_instr
);
403 instr_init(&instr
->instr
, nir_instr_type_load_const
);
405 dest_init(&instr
->dest
);
406 instr
->num_components
= 0;
407 instr
->array_elems
= 0;
409 instr
->has_predicate
= false;
410 src_init(&instr
->predicate
);
415 nir_intrinsic_instr
*
416 nir_intrinsic_instr_create(void *mem_ctx
, nir_intrinsic_op op
)
418 unsigned num_srcs
= nir_intrinsic_infos
[op
].num_srcs
;
419 nir_intrinsic_instr
*instr
=
421 sizeof(nir_intrinsic_instr
) + num_srcs
* sizeof(nir_src
));
423 instr_init(&instr
->instr
, nir_instr_type_intrinsic
);
424 instr
->intrinsic
= op
;
426 if (nir_intrinsic_infos
[op
].has_dest
)
427 dest_init(&instr
->dest
);
429 for (unsigned i
= 0; i
< num_srcs
; i
++)
430 src_init(&instr
->src
[i
]);
432 instr
->has_predicate
= false;
433 src_init(&instr
->predicate
);
439 nir_call_instr_create(void *mem_ctx
, nir_function_overload
*callee
)
441 nir_call_instr
*instr
= ralloc(mem_ctx
, nir_call_instr
);
442 instr_init(&instr
->instr
, nir_instr_type_call
);
444 instr
->callee
= callee
;
445 instr
->num_params
= callee
->num_params
;
446 instr
->params
= ralloc_array(mem_ctx
, nir_deref_var
*, instr
->num_params
);
447 instr
->return_deref
= NULL
;
449 instr
->has_predicate
= false;
450 src_init(&instr
->predicate
);
456 nir_tex_instr_create(void *mem_ctx
, unsigned num_srcs
)
458 nir_tex_instr
*instr
= ralloc(mem_ctx
, nir_tex_instr
);
459 instr_init(&instr
->instr
, nir_instr_type_texture
);
461 dest_init(&instr
->dest
);
463 instr
->num_srcs
= num_srcs
;
464 for (unsigned i
= 0; i
< num_srcs
; i
++)
465 src_init(&instr
->src
[i
]);
467 instr
->has_predicate
= false;
468 src_init(&instr
->predicate
);
474 nir_phi_instr_create(void *mem_ctx
)
476 nir_phi_instr
*instr
= ralloc(mem_ctx
, nir_phi_instr
);
477 instr_init(&instr
->instr
, nir_instr_type_phi
);
479 dest_init(&instr
->dest
);
480 exec_list_make_empty(&instr
->srcs
);
484 nir_parallel_copy_instr
*
485 nir_parallel_copy_instr_create(void *mem_ctx
)
487 nir_parallel_copy_instr
*instr
= ralloc(mem_ctx
, nir_parallel_copy_instr
);
488 instr_init(&instr
->instr
, nir_instr_type_parallel_copy
);
490 instr
->at_end
= false;
491 exec_list_make_empty(&instr
->copies
);
496 nir_ssa_undef_instr
*
497 nir_ssa_undef_instr_create(void *mem_ctx
)
499 nir_ssa_undef_instr
*instr
= ralloc(mem_ctx
, nir_ssa_undef_instr
);
500 instr_init(&instr
->instr
, nir_instr_type_ssa_undef
);
502 instr
->def
.name
= NULL
;
503 instr
->def
.parent_instr
= &instr
->instr
;
509 nir_deref_var_create(void *mem_ctx
, nir_variable
*var
)
511 nir_deref_var
*deref
= ralloc(mem_ctx
, nir_deref_var
);
512 deref
->deref
.deref_type
= nir_deref_type_var
;
513 deref
->deref
.child
= NULL
;
514 deref
->deref
.type
= var
->type
;
520 nir_deref_array_create(void *mem_ctx
)
522 nir_deref_array
*deref
= ralloc(mem_ctx
, nir_deref_array
);
523 deref
->deref
.deref_type
= nir_deref_type_array
;
524 deref
->deref
.child
= NULL
;
525 deref
->has_indirect
= false;
526 src_init(&deref
->indirect
);
527 deref
->base_offset
= 0;
532 nir_deref_struct_create(void *mem_ctx
, const char *field
)
534 nir_deref_struct
*deref
= ralloc(mem_ctx
, nir_deref_struct
);
535 deref
->deref
.deref_type
= nir_deref_type_struct
;
536 deref
->deref
.child
= NULL
;
537 deref
->elem
= ralloc_strdup(deref
, field
);
541 static nir_deref_var
*
542 copy_deref_var(void *mem_ctx
, nir_deref_var
*deref
)
544 nir_deref_var
*ret
= nir_deref_var_create(mem_ctx
, deref
->var
);
545 if (deref
->deref
.child
)
546 ret
->deref
.child
= nir_copy_deref(mem_ctx
, deref
->deref
.child
);
550 static nir_deref_array
*
551 copy_deref_array(void *mem_ctx
, nir_deref_array
*deref
)
553 nir_deref_array
*ret
= nir_deref_array_create(mem_ctx
);
554 ret
->base_offset
= deref
->base_offset
;
555 if (deref
->has_indirect
)
556 ret
->indirect
= deref
->indirect
;
557 ret
->deref
.type
= deref
->deref
.type
;
558 if (deref
->deref
.child
)
559 ret
->deref
.child
= nir_copy_deref(mem_ctx
, deref
->deref
.child
);
563 static nir_deref_struct
*
564 copy_deref_struct(void *mem_ctx
, nir_deref_struct
*deref
)
566 nir_deref_struct
*ret
= nir_deref_struct_create(mem_ctx
, deref
->elem
);
567 ret
->deref
.type
= deref
->deref
.type
;
568 if (deref
->deref
.child
)
569 ret
->deref
.child
= nir_copy_deref(mem_ctx
, deref
->deref
.child
);
574 nir_copy_deref(void *mem_ctx
, nir_deref
*deref
)
576 switch (deref
->deref_type
) {
577 case nir_deref_type_var
:
578 return ©_deref_var(mem_ctx
, nir_deref_as_var(deref
))->deref
;
579 case nir_deref_type_array
:
580 return ©_deref_array(mem_ctx
, nir_deref_as_array(deref
))->deref
;
581 case nir_deref_type_struct
:
582 return ©_deref_struct(mem_ctx
, nir_deref_as_struct(deref
))->deref
;
584 unreachable("Invalid dereference type");
592 * \name Control flow modification
594 * These functions modify the control flow tree while keeping the control flow
595 * graph up-to-date. The invariants respected are:
596 * 1. Each then statement, else statement, or loop body must have at least one
598 * 2. Each if-statement and loop must have one basic block before it and one
600 * 3. Two basic blocks cannot be directly next to each other.
601 * 4. If a basic block has a jump instruction, there must be only one and it
602 * must be at the end of the block.
603 * 5. The CFG must always be connected - this means that we must insert a fake
604 * CFG edge for loops with no break statement.
606 * The purpose of the second one is so that we have places to insert code during
607 * GCM, as well as eliminating the possibility of critical edges.
612 link_non_block_to_block(nir_cf_node
*node
, nir_block
*block
)
614 if (node
->type
== nir_cf_node_if
) {
616 * We're trying to link an if to a block after it; this just means linking
617 * the last block of the then and else branches.
620 nir_if
*if_stmt
= nir_cf_node_as_if(node
);
622 nir_cf_node
*last_then
= nir_if_last_then_node(if_stmt
);
623 assert(last_then
->type
== nir_cf_node_block
);
624 nir_block
*last_then_block
= nir_cf_node_as_block(last_then
);
626 nir_cf_node
*last_else
= nir_if_last_else_node(if_stmt
);
627 assert(last_else
->type
== nir_cf_node_block
);
628 nir_block
*last_else_block
= nir_cf_node_as_block(last_else
);
630 if (exec_list_is_empty(&last_then_block
->instr_list
) ||
631 nir_block_last_instr(last_then_block
)->type
!= nir_instr_type_jump
) {
632 unlink_block_successors(last_then_block
);
633 link_blocks(last_then_block
, block
, NULL
);
636 if (exec_list_is_empty(&last_else_block
->instr_list
) ||
637 nir_block_last_instr(last_else_block
)->type
!= nir_instr_type_jump
) {
638 unlink_block_successors(last_else_block
);
639 link_blocks(last_else_block
, block
, NULL
);
642 assert(node
->type
== nir_cf_node_loop
);
645 * We can only get to this codepath if we're inserting a new loop, or
646 * at least a loop with no break statements; we can't insert break
647 * statements into a loop when we haven't inserted it into the CFG
648 * because we wouldn't know which block comes after the loop
649 * and therefore, which block should be the successor of the block with
650 * the break). Therefore, we need to insert a fake edge (see invariant
654 nir_loop
*loop
= nir_cf_node_as_loop(node
);
656 nir_cf_node
*last
= nir_loop_last_cf_node(loop
);
657 assert(last
->type
== nir_cf_node_block
);
658 nir_block
*last_block
= nir_cf_node_as_block(last
);
660 last_block
->successors
[1] = block
;
661 block_add_pred(block
, last_block
);
666 link_block_to_non_block(nir_block
*block
, nir_cf_node
*node
)
668 if (node
->type
== nir_cf_node_if
) {
670 * We're trying to link a block to an if after it; this just means linking
671 * the block to the first block of the then and else branches.
674 nir_if
*if_stmt
= nir_cf_node_as_if(node
);
676 nir_cf_node
*first_then
= nir_if_first_then_node(if_stmt
);
677 assert(first_then
->type
== nir_cf_node_block
);
678 nir_block
*first_then_block
= nir_cf_node_as_block(first_then
);
680 nir_cf_node
*first_else
= nir_if_first_else_node(if_stmt
);
681 assert(first_else
->type
== nir_cf_node_block
);
682 nir_block
*first_else_block
= nir_cf_node_as_block(first_else
);
684 unlink_block_successors(block
);
685 link_blocks(block
, first_then_block
, first_else_block
);
688 * For similar reasons as the corresponding case in
689 * link_non_block_to_block(), don't worry about if the loop header has
690 * any predecessors that need to be unlinked.
693 assert(node
->type
== nir_cf_node_loop
);
695 nir_loop
*loop
= nir_cf_node_as_loop(node
);
697 nir_cf_node
*loop_header
= nir_loop_first_cf_node(loop
);
698 assert(loop_header
->type
== nir_cf_node_block
);
699 nir_block
*loop_header_block
= nir_cf_node_as_block(loop_header
);
701 unlink_block_successors(block
);
702 link_blocks(block
, loop_header_block
, NULL
);
708 * Takes a basic block and inserts a new empty basic block before it, making its
709 * predecessors point to the new block. This essentially splits the block into
710 * an empty header and a body so that another non-block CF node can be inserted
711 * between the two. Note that this does *not* link the two basic blocks, so
712 * some kind of cleanup *must* be performed after this call.
716 split_block_beginning(nir_block
*block
)
718 nir_block
*new_block
= nir_block_create(ralloc_parent(block
));
719 new_block
->cf_node
.parent
= block
->cf_node
.parent
;
720 exec_node_insert_node_before(&block
->cf_node
.node
, &new_block
->cf_node
.node
);
722 struct set_entry
*entry
;
723 set_foreach(block
->predecessors
, entry
) {
724 nir_block
*pred
= (nir_block
*) entry
->key
;
726 unlink_blocks(pred
, block
);
727 link_blocks(pred
, new_block
, NULL
);
734 * Moves the successors of source to the successors of dest, leaving both
735 * successors of source NULL.
739 move_successors(nir_block
*source
, nir_block
*dest
)
741 nir_block
*succ1
= source
->successors
[0];
743 unlink_blocks(source
, succ1
);
745 nir_block
*succ2
= source
->successors
[1];
747 unlink_blocks(source
, succ2
);
749 unlink_block_successors(dest
);
750 link_blocks(dest
, succ1
, succ2
);
754 split_block_end(nir_block
*block
)
756 nir_block
*new_block
= nir_block_create(ralloc_parent(block
));
757 new_block
->cf_node
.parent
= block
->cf_node
.parent
;
758 exec_node_insert_after(&block
->cf_node
.node
, &new_block
->cf_node
.node
);
760 move_successors(block
, new_block
);
766 * Inserts a non-basic block between two basic blocks and links them together.
770 insert_non_block(nir_block
*before
, nir_cf_node
*node
, nir_block
*after
)
772 node
->parent
= before
->cf_node
.parent
;
773 exec_node_insert_after(&before
->cf_node
.node
, &node
->node
);
774 link_block_to_non_block(before
, node
);
775 link_non_block_to_block(node
, after
);
779 * Inserts a non-basic block before a basic block.
783 insert_non_block_before_block(nir_cf_node
*node
, nir_block
*block
)
785 /* split off the beginning of block into new_block */
786 nir_block
*new_block
= split_block_beginning(block
);
788 /* insert our node in between new_block and block */
789 insert_non_block(new_block
, node
, block
);
793 insert_non_block_after_block(nir_block
*block
, nir_cf_node
*node
)
795 /* split off the end of block into new_block */
796 nir_block
*new_block
= split_block_end(block
);
798 /* insert our node in between block and new_block */
799 insert_non_block(block
, node
, new_block
);
802 /* walk up the control flow tree to find the innermost enclosed loop */
804 nearest_loop(nir_cf_node
*node
)
806 while (node
->type
!= nir_cf_node_loop
) {
810 return nir_cf_node_as_loop(node
);
814 nir_cf_node_get_function(nir_cf_node
*node
)
816 while (node
->type
!= nir_cf_node_function
) {
820 return nir_cf_node_as_function(node
);
824 * update the CFG after a jump instruction has been added to the end of a block
828 handle_jump(nir_block
*block
)
830 nir_instr
*instr
= nir_block_last_instr(block
);
831 nir_jump_instr
*jump_instr
= nir_instr_as_jump(instr
);
833 unlink_block_successors(block
);
835 nir_function_impl
*impl
= nir_cf_node_get_function(&block
->cf_node
);
836 nir_metadata_dirty(impl
, nir_metadata_none
);
838 if (jump_instr
->type
== nir_jump_break
||
839 jump_instr
->type
== nir_jump_continue
) {
840 nir_loop
*loop
= nearest_loop(&block
->cf_node
);
842 if (jump_instr
->type
== nir_jump_continue
) {
843 nir_cf_node
*first_node
= nir_loop_first_cf_node(loop
);
844 assert(first_node
->type
== nir_cf_node_block
);
845 nir_block
*first_block
= nir_cf_node_as_block(first_node
);
846 link_blocks(block
, first_block
, NULL
);
848 nir_cf_node
*after
= nir_cf_node_next(&loop
->cf_node
);
849 assert(after
->type
== nir_cf_node_block
);
850 nir_block
*after_block
= nir_cf_node_as_block(after
);
851 link_blocks(block
, after_block
, NULL
);
853 /* If we inserted a fake link, remove it */
854 nir_cf_node
*last
= nir_loop_last_cf_node(loop
);
855 assert(last
->type
== nir_cf_node_block
);
856 nir_block
*last_block
= nir_cf_node_as_block(last
);
857 if (last_block
->successors
[1] != NULL
)
858 unlink_blocks(last_block
, after_block
);
861 assert(jump_instr
->type
== nir_jump_return
);
862 link_blocks(block
, impl
->end_block
, NULL
);
867 handle_remove_jump(nir_block
*block
, nir_jump_type type
)
869 unlink_block_successors(block
);
871 if (exec_node_is_tail_sentinel(block
->cf_node
.node
.next
)) {
872 nir_cf_node
*parent
= block
->cf_node
.parent
;
873 if (parent
->type
== nir_cf_node_if
) {
874 nir_cf_node
*next
= nir_cf_node_next(parent
);
875 assert(next
->type
== nir_cf_node_block
);
876 nir_block
*next_block
= nir_cf_node_as_block(next
);
878 link_blocks(block
, next_block
, NULL
);
880 assert(parent
->type
== nir_cf_node_loop
);
881 nir_loop
*loop
= nir_cf_node_as_loop(parent
);
883 nir_cf_node
*head
= nir_loop_first_cf_node(loop
);
884 assert(head
->type
== nir_cf_node_block
);
885 nir_block
*head_block
= nir_cf_node_as_block(head
);
887 link_blocks(block
, head_block
, NULL
);
890 nir_cf_node
*next
= nir_cf_node_next(&block
->cf_node
);
891 if (next
->type
== nir_cf_node_if
) {
892 nir_if
*next_if
= nir_cf_node_as_if(next
);
894 nir_cf_node
*first_then
= nir_if_first_then_node(next_if
);
895 assert(first_then
->type
== nir_cf_node_block
);
896 nir_block
*first_then_block
= nir_cf_node_as_block(first_then
);
898 nir_cf_node
*first_else
= nir_if_first_else_node(next_if
);
899 assert(first_else
->type
== nir_cf_node_block
);
900 nir_block
*first_else_block
= nir_cf_node_as_block(first_else
);
902 link_blocks(block
, first_then_block
, first_else_block
);
904 assert(next
->type
== nir_cf_node_loop
);
905 nir_loop
*next_loop
= nir_cf_node_as_loop(next
);
907 nir_cf_node
*first
= nir_loop_first_cf_node(next_loop
);
908 assert(first
->type
== nir_cf_node_block
);
909 nir_block
*first_block
= nir_cf_node_as_block(first
);
911 link_blocks(block
, first_block
, NULL
);
915 if (type
== nir_jump_break
) {
916 nir_loop
*loop
= nearest_loop(&block
->cf_node
);
918 nir_cf_node
*next
= nir_cf_node_next(&loop
->cf_node
);
919 assert(next
->type
== nir_cf_node_block
);
920 nir_block
*next_block
= nir_cf_node_as_block(next
);
922 if (next_block
->predecessors
->entries
== 0) {
923 /* insert fake link */
924 nir_cf_node
*last
= nir_loop_last_cf_node(loop
);
925 assert(last
->type
== nir_cf_node_block
);
926 nir_block
*last_block
= nir_cf_node_as_block(last
);
928 last_block
->successors
[1] = next_block
;
929 block_add_pred(next_block
, last_block
);
933 nir_function_impl
*impl
= nir_cf_node_get_function(&block
->cf_node
);
934 nir_metadata_dirty(impl
, nir_metadata_none
);
938 * Inserts a basic block before another by merging the instructions.
940 * @param block the target of the insertion
941 * @param before the block to be inserted - must not have been inserted before
942 * @param has_jump whether \before has a jump instruction at the end
946 insert_block_before_block(nir_block
*block
, nir_block
*before
, bool has_jump
)
948 assert(!has_jump
|| exec_list_is_empty(&block
->instr_list
));
950 foreach_list_typed(nir_instr
, instr
, node
, &before
->instr_list
) {
951 instr
->block
= block
;
954 exec_list_prepend(&block
->instr_list
, &before
->instr_list
);
961 * Inserts a basic block after another by merging the instructions.
963 * @param block the target of the insertion
964 * @param after the block to be inserted - must not have been inserted before
965 * @param has_jump whether \after has a jump instruction at the end
969 insert_block_after_block(nir_block
*block
, nir_block
*after
, bool has_jump
)
971 foreach_list_typed(nir_instr
, instr
, node
, &after
->instr_list
) {
972 instr
->block
= block
;
975 exec_list_append(&block
->instr_list
, &after
->instr_list
);
982 update_if_uses(nir_cf_node
*node
)
984 if (node
->type
!= nir_cf_node_if
)
987 nir_if
*if_stmt
= nir_cf_node_as_if(node
);
988 if (if_stmt
->condition
.is_ssa
)
991 nir_register
*reg
= if_stmt
->condition
.reg
.reg
;
994 _mesa_set_add(reg
->if_uses
, _mesa_hash_pointer(if_stmt
), if_stmt
);
998 nir_cf_node_insert_after(nir_cf_node
*node
, nir_cf_node
*after
)
1000 update_if_uses(after
);
1002 if (after
->type
== nir_cf_node_block
) {
1004 * either node or the one after it must be a basic block, by invariant #2;
1005 * in either case, just merge the blocks together.
1007 nir_block
*after_block
= nir_cf_node_as_block(after
);
1009 bool has_jump
= !exec_list_is_empty(&after_block
->instr_list
) &&
1010 nir_block_last_instr(after_block
)->type
== nir_instr_type_jump
;
1012 if (node
->type
== nir_cf_node_block
) {
1013 insert_block_after_block(nir_cf_node_as_block(node
), after_block
,
1016 nir_cf_node
*next
= nir_cf_node_next(node
);
1017 assert(next
->type
== nir_cf_node_block
);
1018 nir_block
*next_block
= nir_cf_node_as_block(next
);
1020 insert_block_before_block(next_block
, after_block
, has_jump
);
1023 if (node
->type
== nir_cf_node_block
) {
1024 insert_non_block_after_block(nir_cf_node_as_block(node
), after
);
1027 * We have to insert a non-basic block after a non-basic block. Since
1028 * every non-basic block has a basic block after it, this is equivalent
1029 * to inserting a non-basic block before a basic block.
1032 nir_cf_node
*next
= nir_cf_node_next(node
);
1033 assert(next
->type
== nir_cf_node_block
);
1034 nir_block
*next_block
= nir_cf_node_as_block(next
);
1036 insert_non_block_before_block(after
, next_block
);
1040 nir_function_impl
*impl
= nir_cf_node_get_function(node
);
1041 nir_metadata_dirty(impl
, nir_metadata_none
);
1045 nir_cf_node_insert_before(nir_cf_node
*node
, nir_cf_node
*before
)
1047 update_if_uses(before
);
1049 if (before
->type
== nir_cf_node_block
) {
1050 nir_block
*before_block
= nir_cf_node_as_block(before
);
1052 bool has_jump
= !exec_list_is_empty(&before_block
->instr_list
) &&
1053 nir_block_last_instr(before_block
)->type
== nir_instr_type_jump
;
1055 if (node
->type
== nir_cf_node_block
) {
1056 insert_block_before_block(nir_cf_node_as_block(node
), before_block
,
1059 nir_cf_node
*prev
= nir_cf_node_prev(node
);
1060 assert(prev
->type
== nir_cf_node_block
);
1061 nir_block
*prev_block
= nir_cf_node_as_block(prev
);
1063 insert_block_after_block(prev_block
, before_block
, has_jump
);
1066 if (node
->type
== nir_cf_node_block
) {
1067 insert_non_block_before_block(before
, nir_cf_node_as_block(node
));
1070 * We have to insert a non-basic block before a non-basic block. This
1071 * is equivalent to inserting a non-basic block after a basic block.
1074 nir_cf_node
*prev_node
= nir_cf_node_prev(node
);
1075 assert(prev_node
->type
== nir_cf_node_block
);
1076 nir_block
*prev_block
= nir_cf_node_as_block(prev_node
);
1078 insert_non_block_after_block(prev_block
, before
);
1082 nir_function_impl
*impl
= nir_cf_node_get_function(node
);
1083 nir_metadata_dirty(impl
, nir_metadata_none
);
1087 nir_cf_node_insert_begin(struct exec_list
*list
, nir_cf_node
*node
)
1089 nir_cf_node
*begin
= exec_node_data(nir_cf_node
, list
->head
, node
);
1090 nir_cf_node_insert_before(begin
, node
);
1094 nir_cf_node_insert_end(struct exec_list
*list
, nir_cf_node
*node
)
1096 nir_cf_node
*end
= exec_node_data(nir_cf_node
, list
->tail_pred
, node
);
1097 nir_cf_node_insert_after(end
, node
);
1101 * Stitch two basic blocks together into one. The aggregate must have the same
1102 * predecessors as the first and the same successors as the second.
1106 stitch_blocks(nir_block
*before
, nir_block
*after
)
1109 * We move after into before, so we have to deal with up to 2 successors vs.
1110 * possibly a large number of predecessors.
1112 * TODO: special case when before is empty and after isn't?
1115 move_successors(after
, before
);
1117 foreach_list_typed(nir_instr
, instr
, node
, &after
->instr_list
) {
1118 instr
->block
= before
;
1121 exec_list_append(&before
->instr_list
, &after
->instr_list
);
1122 exec_node_remove(&after
->cf_node
.node
);
1126 nir_cf_node_remove(nir_cf_node
*node
)
1128 nir_function_impl
*impl
= nir_cf_node_get_function(node
);
1129 nir_metadata_dirty(impl
, nir_metadata_none
);
1131 if (node
->type
== nir_cf_node_block
) {
1133 * Basic blocks can't really be removed by themselves, since they act as
1134 * padding between the non-basic blocks. So all we do here is empty the
1135 * block of instructions.
1137 * TODO: could we assert here?
1139 exec_list_make_empty(&nir_cf_node_as_block(node
)->instr_list
);
1141 nir_cf_node
*before
= nir_cf_node_prev(node
);
1142 assert(before
->type
== nir_cf_node_block
);
1143 nir_block
*before_block
= nir_cf_node_as_block(before
);
1145 nir_cf_node
*after
= nir_cf_node_next(node
);
1146 assert(after
->type
== nir_cf_node_block
);
1147 nir_block
*after_block
= nir_cf_node_as_block(after
);
1149 exec_node_remove(&node
->node
);
1150 stitch_blocks(before_block
, after_block
);
1155 add_use_cb(nir_src
*src
, void *state
)
1157 nir_instr
*instr
= (nir_instr
*) state
;
1159 struct set
*uses_set
= src
->is_ssa
? src
->ssa
->uses
: src
->reg
.reg
->uses
;
1161 _mesa_set_add(uses_set
, _mesa_hash_pointer(instr
), instr
);
1167 add_def_cb(nir_dest
*dest
, void *state
)
1169 nir_instr
*instr
= (nir_instr
*) state
;
1174 nir_register
*reg
= dest
->reg
.reg
;
1176 _mesa_set_add(reg
->defs
, _mesa_hash_pointer(instr
), instr
);
1182 add_defs_uses(nir_instr
*instr
)
1184 nir_foreach_src(instr
, add_use_cb
, instr
);
1185 nir_foreach_dest(instr
, add_def_cb
, instr
);
1189 nir_instr_insert_before(nir_instr
*instr
, nir_instr
*before
)
1191 assert(before
->type
!= nir_instr_type_jump
);
1192 before
->block
= instr
->block
;
1193 add_defs_uses(before
);
1194 exec_node_insert_node_before(&instr
->node
, &before
->node
);
1198 nir_instr_insert_after(nir_instr
*instr
, nir_instr
*after
)
1200 if (after
->type
== nir_instr_type_jump
) {
1201 assert(instr
== nir_block_last_instr(instr
->block
));
1202 assert(instr
->type
!= nir_instr_type_jump
);
1205 after
->block
= instr
->block
;
1206 add_defs_uses(after
);
1207 exec_node_insert_after(&instr
->node
, &after
->node
);
1209 if (after
->type
== nir_instr_type_jump
)
1210 handle_jump(after
->block
);
1214 nir_instr_insert_before_block(nir_block
*block
, nir_instr
*before
)
1216 if (before
->type
== nir_instr_type_jump
)
1217 assert(exec_list_is_empty(&block
->instr_list
));
1219 before
->block
= block
;
1220 add_defs_uses(before
);
1221 exec_list_push_head(&block
->instr_list
, &before
->node
);
1223 if (before
->type
== nir_instr_type_jump
)
1228 nir_instr_insert_after_block(nir_block
*block
, nir_instr
*after
)
1230 if (after
->type
== nir_instr_type_jump
) {
1231 assert(exec_list_is_empty(&block
->instr_list
) ||
1232 nir_block_last_instr(block
)->type
!= nir_instr_type_jump
);
1235 after
->block
= block
;
1236 add_defs_uses(after
);
1237 exec_list_push_tail(&block
->instr_list
, &after
->node
);
1239 if (after
->type
== nir_instr_type_jump
)
1244 nir_instr_insert_before_cf(nir_cf_node
*node
, nir_instr
*before
)
1246 if (node
->type
== nir_cf_node_block
) {
1247 nir_instr_insert_before_block(nir_cf_node_as_block(node
), before
);
1249 nir_cf_node
*prev
= nir_cf_node_prev(node
);
1250 assert(prev
->type
== nir_cf_node_block
);
1251 nir_block
*prev_block
= nir_cf_node_as_block(prev
);
1253 nir_instr_insert_before_block(prev_block
, before
);
1258 nir_instr_insert_after_cf(nir_cf_node
*node
, nir_instr
*after
)
1260 if (node
->type
== nir_cf_node_block
) {
1261 nir_instr_insert_after_block(nir_cf_node_as_block(node
), after
);
1263 nir_cf_node
*next
= nir_cf_node_next(node
);
1264 assert(next
->type
== nir_cf_node_block
);
1265 nir_block
*next_block
= nir_cf_node_as_block(next
);
1267 nir_instr_insert_before_block(next_block
, after
);
1272 nir_instr_insert_before_cf_list(struct exec_list
*list
, nir_instr
*before
)
1274 nir_cf_node
*first_node
= exec_node_data(nir_cf_node
,
1275 exec_list_get_head(list
), node
);
1276 nir_instr_insert_before_cf(first_node
, before
);
1280 nir_instr_insert_after_cf_list(struct exec_list
*list
, nir_instr
*after
)
1282 nir_cf_node
*last_node
= exec_node_data(nir_cf_node
,
1283 exec_list_get_tail(list
), node
);
1284 nir_instr_insert_after_cf(last_node
, after
);
1288 remove_use_cb(nir_src
*src
, void *state
)
1290 nir_instr
*instr
= (nir_instr
*) state
;
1292 struct set
*uses_set
= src
->is_ssa
? src
->ssa
->uses
: src
->reg
.reg
->uses
;
1294 struct set_entry
*entry
= _mesa_set_search(uses_set
,
1295 _mesa_hash_pointer(instr
),
1298 _mesa_set_remove(uses_set
, entry
);
1304 remove_def_cb(nir_dest
*dest
, void *state
)
1306 nir_instr
*instr
= (nir_instr
*) state
;
1311 nir_register
*reg
= dest
->reg
.reg
;
1313 struct set_entry
*entry
= _mesa_set_search(reg
->defs
,
1314 _mesa_hash_pointer(instr
),
1317 _mesa_set_remove(reg
->defs
, entry
);
1323 remove_defs_uses(nir_instr
*instr
)
1325 nir_foreach_dest(instr
, remove_def_cb
, instr
);
1326 nir_foreach_src(instr
, remove_use_cb
, instr
);
1329 void nir_instr_remove(nir_instr
*instr
)
1331 remove_defs_uses(instr
);
1332 exec_node_remove(&instr
->node
);
1334 if (instr
->type
== nir_instr_type_jump
) {
1335 nir_jump_instr
*jump_instr
= nir_instr_as_jump(instr
);
1336 handle_remove_jump(instr
->block
, jump_instr
->type
);
1343 nir_index_local_regs(nir_function_impl
*impl
)
1346 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
1347 reg
->index
= index
++;
1349 impl
->reg_alloc
= index
;
1353 nir_index_global_regs(nir_shader
*shader
)
1356 foreach_list_typed(nir_register
, reg
, node
, &shader
->registers
) {
1357 reg
->index
= index
++;
1359 shader
->reg_alloc
= index
;
1363 visit_alu_dest(nir_alu_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
1365 return cb(&instr
->dest
.dest
, state
);
1369 visit_intrinsic_dest(nir_intrinsic_instr
*instr
, nir_foreach_dest_cb cb
,
1372 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
1373 return cb(&instr
->dest
, state
);
1379 visit_texture_dest(nir_tex_instr
*instr
, nir_foreach_dest_cb cb
,
1382 return cb(&instr
->dest
, state
);
1386 visit_load_const_dest(nir_load_const_instr
*instr
, nir_foreach_dest_cb cb
,
1389 return cb(&instr
->dest
, state
);
1393 visit_phi_dest(nir_phi_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
1395 return cb(&instr
->dest
, state
);
1399 visit_parallel_copy_dest(nir_parallel_copy_instr
*instr
,
1400 nir_foreach_dest_cb cb
, void *state
)
1402 foreach_list_typed(nir_parallel_copy_copy
, copy
, node
, &instr
->copies
) {
1403 if (!cb(©
->dest
, state
))
1411 nir_foreach_dest(nir_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
1413 switch (instr
->type
) {
1414 case nir_instr_type_alu
:
1415 return visit_alu_dest(nir_instr_as_alu(instr
), cb
, state
);
1416 case nir_instr_type_intrinsic
:
1417 return visit_intrinsic_dest(nir_instr_as_intrinsic(instr
), cb
, state
);
1418 case nir_instr_type_texture
:
1419 return visit_texture_dest(nir_instr_as_texture(instr
), cb
, state
);
1420 case nir_instr_type_load_const
:
1421 return visit_load_const_dest(nir_instr_as_load_const(instr
), cb
, state
);
1422 case nir_instr_type_phi
:
1423 return visit_phi_dest(nir_instr_as_phi(instr
), cb
, state
);
1424 case nir_instr_type_parallel_copy
:
1425 return visit_parallel_copy_dest(nir_instr_as_parallel_copy(instr
),
1428 case nir_instr_type_ssa_undef
:
1429 case nir_instr_type_call
:
1430 case nir_instr_type_jump
:
1434 unreachable("Invalid instruction type");
1442 visit_src(nir_src
*src
, nir_foreach_src_cb cb
, void *state
)
1444 if (!cb(src
, state
))
1446 if (!src
->is_ssa
&& src
->reg
.indirect
)
1447 return cb(src
->reg
.indirect
, state
);
1452 visit_deref_array_src(nir_deref_array
*deref
, nir_foreach_src_cb cb
,
1455 if (deref
->has_indirect
)
1456 return visit_src(&deref
->indirect
, cb
, state
);
1461 visit_deref_src(nir_deref_var
*deref
, nir_foreach_src_cb cb
, void *state
)
1463 nir_deref
*cur
= &deref
->deref
;
1464 while (cur
!= NULL
) {
1465 if (cur
->deref_type
== nir_deref_type_array
)
1466 if (!visit_deref_array_src(nir_deref_as_array(cur
), cb
, state
))
1476 visit_alu_src(nir_alu_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1478 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
1479 if (!visit_src(&instr
->src
[i
].src
, cb
, state
))
1482 if (instr
->has_predicate
)
1483 if (!visit_src(&instr
->predicate
, cb
, state
))
1490 visit_tex_src(nir_tex_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1492 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++)
1493 if (!visit_src(&instr
->src
[i
], cb
, state
))
1496 if (instr
->has_predicate
)
1497 if (!visit_src(&instr
->predicate
, cb
, state
))
1500 if (instr
->sampler
!= NULL
)
1501 if (!visit_deref_src(instr
->sampler
, cb
, state
))
1508 visit_intrinsic_src(nir_intrinsic_instr
*instr
, nir_foreach_src_cb cb
,
1511 unsigned num_srcs
= nir_intrinsic_infos
[instr
->intrinsic
].num_srcs
;
1512 for (unsigned i
= 0; i
< num_srcs
; i
++)
1513 if (!visit_src(&instr
->src
[i
], cb
, state
))
1517 nir_intrinsic_infos
[instr
->intrinsic
].num_variables
;
1518 for (unsigned i
= 0; i
< num_vars
; i
++)
1519 if (!visit_deref_src(instr
->variables
[i
], cb
, state
))
1522 if (instr
->has_predicate
)
1523 if (!visit_src(&instr
->predicate
, cb
, state
))
1530 visit_call_src(nir_call_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1532 if (instr
->has_predicate
)
1533 if (!visit_src(&instr
->predicate
, cb
, state
))
1540 visit_load_const_src(nir_load_const_instr
*instr
, nir_foreach_src_cb cb
,
1543 if (instr
->has_predicate
)
1544 if (!visit_src(&instr
->predicate
, cb
, state
))
1551 visit_phi_src(nir_phi_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1553 foreach_list_typed(nir_phi_src
, src
, node
, &instr
->srcs
) {
1554 if (!visit_src(&src
->src
, cb
, state
))
1562 visit_parallel_copy_src(nir_parallel_copy_instr
*instr
,
1563 nir_foreach_src_cb cb
, void *state
)
1565 foreach_list_typed(nir_parallel_copy_copy
, copy
, node
, &instr
->copies
) {
1566 if (!visit_src(©
->src
, cb
, state
))
1575 nir_foreach_src_cb cb
;
1576 } visit_dest_indirect_state
;
1579 visit_dest_indirect(nir_dest
*dest
, void *_state
)
1581 visit_dest_indirect_state
*state
= (visit_dest_indirect_state
*) _state
;
1583 if (!dest
->is_ssa
&& dest
->reg
.indirect
)
1584 return state
->cb(dest
->reg
.indirect
, state
->state
);
1590 nir_foreach_src(nir_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1592 switch (instr
->type
) {
1593 case nir_instr_type_alu
:
1594 if (!visit_alu_src(nir_instr_as_alu(instr
), cb
, state
))
1597 case nir_instr_type_intrinsic
:
1598 if (!visit_intrinsic_src(nir_instr_as_intrinsic(instr
), cb
, state
))
1601 case nir_instr_type_texture
:
1602 if (!visit_tex_src(nir_instr_as_texture(instr
), cb
, state
))
1605 case nir_instr_type_call
:
1606 if (!visit_call_src(nir_instr_as_call(instr
), cb
, state
))
1609 case nir_instr_type_load_const
:
1610 if (!visit_load_const_src(nir_instr_as_load_const(instr
), cb
, state
))
1613 case nir_instr_type_phi
:
1614 if (!visit_phi_src(nir_instr_as_phi(instr
), cb
, state
))
1617 case nir_instr_type_parallel_copy
:
1618 if (!visit_parallel_copy_src(nir_instr_as_parallel_copy(instr
),
1622 case nir_instr_type_jump
:
1623 case nir_instr_type_ssa_undef
:
1627 unreachable("Invalid instruction type");
1631 visit_dest_indirect_state dest_state
;
1632 dest_state
.state
= state
;
1634 return nir_foreach_dest(instr
, visit_dest_indirect
, &dest_state
);
1638 nir_srcs_equal(nir_src src1
, nir_src src2
)
1642 return src1
.ssa
== src2
.ssa
;
1650 if ((src1
.reg
.indirect
== NULL
) != (src2
.reg
.indirect
== NULL
))
1653 if (src1
.reg
.indirect
) {
1654 if (!nir_srcs_equal(*src1
.reg
.indirect
, *src2
.reg
.indirect
))
1658 return src1
.reg
.reg
== src2
.reg
.reg
&&
1659 src1
.reg
.base_offset
== src2
.reg
.base_offset
;
1665 nir_ssa_def_init(nir_function_impl
*impl
, nir_instr
*instr
, nir_ssa_def
*def
,
1666 unsigned num_components
, const char *name
)
1668 void *mem_ctx
= ralloc_parent(instr
);
1671 def
->index
= impl
->ssa_alloc
++;
1672 def
->parent_instr
= instr
;
1673 def
->uses
= _mesa_set_create(mem_ctx
, _mesa_key_pointer_equal
);
1674 def
->if_uses
= _mesa_set_create(mem_ctx
, _mesa_key_pointer_equal
);
1675 def
->num_components
= num_components
;
1678 struct ssa_def_rewrite_state
{
1685 ssa_def_rewrite_uses_src(nir_src
*src
, void *void_state
)
1687 struct ssa_def_rewrite_state
*state
= void_state
;
1689 if (src
->is_ssa
&& src
->ssa
== state
->old
)
1690 *src
= nir_src_copy(state
->new_src
, state
->mem_ctx
);
1696 nir_ssa_def_rewrite_uses(nir_ssa_def
*def
, nir_src new_src
, void *mem_ctx
)
1698 struct ssa_def_rewrite_state state
;
1699 state
.mem_ctx
= mem_ctx
;
1701 state
.new_src
= new_src
;
1703 assert(!new_src
.is_ssa
|| def
!= new_src
.ssa
);
1705 struct set
*new_uses
, *new_if_uses
;
1706 if (new_src
.is_ssa
) {
1707 new_uses
= new_src
.ssa
->uses
;
1708 new_if_uses
= new_src
.ssa
->if_uses
;
1710 new_uses
= new_src
.reg
.reg
->uses
;
1711 new_if_uses
= new_src
.reg
.reg
->if_uses
;
1714 struct set_entry
*entry
;
1715 set_foreach(def
->uses
, entry
) {
1716 nir_instr
*instr
= (nir_instr
*)entry
->key
;
1718 _mesa_set_remove(def
->uses
, entry
);
1719 nir_foreach_src(instr
, ssa_def_rewrite_uses_src
, &state
);
1720 _mesa_set_add(new_uses
, _mesa_hash_pointer(instr
), instr
);
1723 set_foreach(def
->if_uses
, entry
) {
1724 nir_if
*if_use
= (nir_if
*)entry
->key
;
1726 _mesa_set_remove(def
->if_uses
, entry
);
1727 if_use
->condition
= nir_src_copy(new_src
, mem_ctx
);
1728 _mesa_set_add(new_if_uses
, _mesa_hash_pointer(if_use
), if_use
);
1733 static bool foreach_cf_node(nir_cf_node
*node
, nir_foreach_block_cb cb
,
1734 bool reverse
, void *state
);
1737 foreach_if(nir_if
*if_stmt
, nir_foreach_block_cb cb
, bool reverse
, void *state
)
1740 foreach_list_typed_reverse(nir_cf_node
, node
, node
, &if_stmt
->else_list
) {
1741 if (!foreach_cf_node(node
, cb
, reverse
, state
))
1745 foreach_list_typed_reverse(nir_cf_node
, node
, node
, &if_stmt
->then_list
) {
1746 if (!foreach_cf_node(node
, cb
, reverse
, state
))
1750 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->then_list
) {
1751 if (!foreach_cf_node(node
, cb
, reverse
, state
))
1755 foreach_list_typed(nir_cf_node
, node
, node
, &if_stmt
->else_list
) {
1756 if (!foreach_cf_node(node
, cb
, reverse
, state
))
1765 foreach_loop(nir_loop
*loop
, nir_foreach_block_cb cb
, bool reverse
, void *state
)
1768 foreach_list_typed_reverse(nir_cf_node
, node
, node
, &loop
->body
) {
1769 if (!foreach_cf_node(node
, cb
, reverse
, state
))
1773 foreach_list_typed(nir_cf_node
, node
, node
, &loop
->body
) {
1774 if (!foreach_cf_node(node
, cb
, reverse
, state
))
1783 foreach_cf_node(nir_cf_node
*node
, nir_foreach_block_cb cb
,
1784 bool reverse
, void *state
)
1786 switch (node
->type
) {
1787 case nir_cf_node_block
:
1788 return cb(nir_cf_node_as_block(node
), state
);
1789 case nir_cf_node_if
:
1790 return foreach_if(nir_cf_node_as_if(node
), cb
, reverse
, state
);
1791 case nir_cf_node_loop
:
1792 return foreach_loop(nir_cf_node_as_loop(node
), cb
, reverse
, state
);
1796 unreachable("Invalid CFG node type");
1804 nir_foreach_block(nir_function_impl
*impl
, nir_foreach_block_cb cb
, void *state
)
1806 foreach_list_typed(nir_cf_node
, node
, node
, &impl
->body
) {
1807 if (!foreach_cf_node(node
, cb
, false, state
))
1811 return cb(impl
->end_block
, state
);
1815 nir_foreach_block_reverse(nir_function_impl
*impl
, nir_foreach_block_cb cb
,
1818 if (!cb(impl
->end_block
, state
))
1821 foreach_list_typed_reverse(nir_cf_node
, node
, node
, &impl
->body
) {
1822 if (!foreach_cf_node(node
, cb
, true, state
))
1830 nir_block_following_if(nir_block
*block
)
1832 if (exec_node_is_tail_sentinel(&block
->cf_node
.node
))
1835 if (nir_cf_node_is_last(&block
->cf_node
))
1838 nir_cf_node
*next_node
= nir_cf_node_next(&block
->cf_node
);
1840 if (next_node
->type
!= nir_cf_node_if
)
1843 return nir_cf_node_as_if(next_node
);
1847 index_block(nir_block
*block
, void *state
)
1849 unsigned *index
= (unsigned *) state
;
1850 block
->index
= (*index
)++;
1855 nir_index_blocks(nir_function_impl
*impl
)
1859 if (impl
->valid_metadata
& nir_metadata_block_index
)
1862 nir_foreach_block(impl
, index_block
, &index
);
1864 impl
->num_blocks
= index
;
1868 index_ssa_def(nir_ssa_def
*def
, unsigned *index
)
1870 def
->index
= (*index
)++;
1874 index_ssa_def_cb(nir_dest
*dest
, void *state
)
1876 unsigned *index
= (unsigned *) state
;
1878 index_ssa_def(&dest
->ssa
, index
);
1883 index_ssa_undef(nir_ssa_undef_instr
*instr
, unsigned *index
)
1885 index_ssa_def(&instr
->def
, index
);
1889 index_ssa_block(nir_block
*block
, void *state
)
1891 unsigned *index
= (unsigned *) state
;
1893 nir_foreach_instr(block
, instr
) {
1894 if (instr
->type
== nir_instr_type_ssa_undef
)
1895 index_ssa_undef(nir_instr_as_ssa_undef(instr
), index
);
1897 nir_foreach_dest(instr
, index_ssa_def_cb
, state
);
1904 nir_index_ssa_defs(nir_function_impl
*impl
)
1907 nir_foreach_block(impl
, index_ssa_block
, &index
);
1908 impl
->ssa_alloc
= index
;