2 * Copyright © 2014 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Connor Abbott (cwabbott0@gmail.com)
29 #include "nir_control_flow_private.h"
33 nir_shader_create(void *mem_ctx
,
34 gl_shader_stage stage
,
35 const nir_shader_compiler_options
*options
)
37 nir_shader
*shader
= ralloc(mem_ctx
, nir_shader
);
39 exec_list_make_empty(&shader
->uniforms
);
40 exec_list_make_empty(&shader
->inputs
);
41 exec_list_make_empty(&shader
->outputs
);
43 shader
->options
= options
;
45 exec_list_make_empty(&shader
->functions
);
46 exec_list_make_empty(&shader
->registers
);
47 exec_list_make_empty(&shader
->globals
);
48 exec_list_make_empty(&shader
->system_values
);
49 shader
->reg_alloc
= 0;
51 shader
->num_inputs
= 0;
52 shader
->num_outputs
= 0;
53 shader
->num_uniforms
= 0;
55 shader
->stage
= stage
;
57 shader
->gs
.vertices_out
= 0;
58 shader
->gs
.invocations
= 0;
64 reg_create(void *mem_ctx
, struct exec_list
*list
)
66 nir_register
*reg
= ralloc(mem_ctx
, nir_register
);
68 list_inithead(®
->uses
);
69 list_inithead(®
->defs
);
70 list_inithead(®
->if_uses
);
72 reg
->num_components
= 0;
73 reg
->num_array_elems
= 0;
74 reg
->is_packed
= false;
77 exec_list_push_tail(list
, ®
->node
);
83 nir_global_reg_create(nir_shader
*shader
)
85 nir_register
*reg
= reg_create(shader
, &shader
->registers
);
86 reg
->index
= shader
->reg_alloc
++;
87 reg
->is_global
= true;
93 nir_local_reg_create(nir_function_impl
*impl
)
95 nir_register
*reg
= reg_create(ralloc_parent(impl
), &impl
->registers
);
96 reg
->index
= impl
->reg_alloc
++;
97 reg
->is_global
= false;
103 nir_reg_remove(nir_register
*reg
)
105 exec_node_remove(®
->node
);
109 nir_function_create(nir_shader
*shader
, const char *name
)
111 nir_function
*func
= ralloc(shader
, nir_function
);
113 exec_list_push_tail(&shader
->functions
, &func
->node
);
114 exec_list_make_empty(&func
->overload_list
);
115 func
->name
= ralloc_strdup(func
, name
);
116 func
->shader
= shader
;
121 nir_function_overload
*
122 nir_function_overload_create(nir_function
*func
)
124 void *mem_ctx
= ralloc_parent(func
);
126 nir_function_overload
*overload
= ralloc(mem_ctx
, nir_function_overload
);
128 overload
->num_params
= 0;
129 overload
->params
= NULL
;
130 overload
->return_type
= glsl_void_type();
131 overload
->impl
= NULL
;
133 exec_list_push_tail(&func
->overload_list
, &overload
->node
);
134 overload
->function
= func
;
139 void nir_src_copy(nir_src
*dest
, const nir_src
*src
, void *mem_ctx
)
141 dest
->is_ssa
= src
->is_ssa
;
143 dest
->ssa
= src
->ssa
;
145 dest
->reg
.base_offset
= src
->reg
.base_offset
;
146 dest
->reg
.reg
= src
->reg
.reg
;
147 if (src
->reg
.indirect
) {
148 dest
->reg
.indirect
= ralloc(mem_ctx
, nir_src
);
149 nir_src_copy(dest
->reg
.indirect
, src
->reg
.indirect
, mem_ctx
);
151 dest
->reg
.indirect
= NULL
;
156 void nir_dest_copy(nir_dest
*dest
, const nir_dest
*src
, nir_instr
*instr
)
158 /* Copying an SSA definition makes no sense whatsoever. */
159 assert(!src
->is_ssa
);
161 dest
->is_ssa
= false;
163 dest
->reg
.base_offset
= src
->reg
.base_offset
;
164 dest
->reg
.reg
= src
->reg
.reg
;
165 if (src
->reg
.indirect
) {
166 dest
->reg
.indirect
= ralloc(instr
, nir_src
);
167 nir_src_copy(dest
->reg
.indirect
, src
->reg
.indirect
, instr
);
169 dest
->reg
.indirect
= NULL
;
174 nir_alu_src_copy(nir_alu_src
*dest
, const nir_alu_src
*src
,
175 nir_alu_instr
*instr
)
177 nir_src_copy(&dest
->src
, &src
->src
, &instr
->instr
);
178 dest
->abs
= src
->abs
;
179 dest
->negate
= src
->negate
;
180 for (unsigned i
= 0; i
< 4; i
++)
181 dest
->swizzle
[i
] = src
->swizzle
[i
];
185 nir_alu_dest_copy(nir_alu_dest
*dest
, const nir_alu_dest
*src
,
186 nir_alu_instr
*instr
)
188 nir_dest_copy(&dest
->dest
, &src
->dest
, &instr
->instr
);
189 dest
->write_mask
= src
->write_mask
;
190 dest
->saturate
= src
->saturate
;
195 cf_init(nir_cf_node
*node
, nir_cf_node_type type
)
197 exec_node_init(&node
->node
);
203 nir_function_impl_create(nir_function_overload
*overload
)
205 assert(overload
->impl
== NULL
);
207 void *mem_ctx
= ralloc_parent(overload
);
209 nir_function_impl
*impl
= ralloc(mem_ctx
, nir_function_impl
);
211 overload
->impl
= impl
;
212 impl
->overload
= overload
;
214 cf_init(&impl
->cf_node
, nir_cf_node_function
);
216 exec_list_make_empty(&impl
->body
);
217 exec_list_make_empty(&impl
->registers
);
218 exec_list_make_empty(&impl
->locals
);
219 impl
->num_params
= 0;
221 impl
->return_var
= NULL
;
224 impl
->valid_metadata
= nir_metadata_none
;
226 /* create start & end blocks */
227 nir_block
*start_block
= nir_block_create(mem_ctx
);
228 nir_block
*end_block
= nir_block_create(mem_ctx
);
229 start_block
->cf_node
.parent
= &impl
->cf_node
;
230 end_block
->cf_node
.parent
= &impl
->cf_node
;
231 impl
->end_block
= end_block
;
233 exec_list_push_tail(&impl
->body
, &start_block
->cf_node
.node
);
235 start_block
->successors
[0] = end_block
;
236 _mesa_set_add(end_block
->predecessors
, start_block
);
241 nir_block_create(void *mem_ctx
)
243 nir_block
*block
= ralloc(mem_ctx
, nir_block
);
245 cf_init(&block
->cf_node
, nir_cf_node_block
);
247 block
->successors
[0] = block
->successors
[1] = NULL
;
248 block
->predecessors
= _mesa_set_create(block
, _mesa_hash_pointer
,
249 _mesa_key_pointer_equal
);
250 block
->imm_dom
= NULL
;
251 block
->dom_frontier
= _mesa_set_create(block
, _mesa_hash_pointer
,
252 _mesa_key_pointer_equal
);
254 exec_list_make_empty(&block
->instr_list
);
260 src_init(nir_src
*src
)
264 src
->reg
.indirect
= NULL
;
265 src
->reg
.base_offset
= 0;
269 nir_if_create(void *mem_ctx
)
271 nir_if
*if_stmt
= ralloc(mem_ctx
, nir_if
);
273 cf_init(&if_stmt
->cf_node
, nir_cf_node_if
);
274 src_init(&if_stmt
->condition
);
276 nir_block
*then
= nir_block_create(mem_ctx
);
277 exec_list_make_empty(&if_stmt
->then_list
);
278 exec_list_push_tail(&if_stmt
->then_list
, &then
->cf_node
.node
);
279 then
->cf_node
.parent
= &if_stmt
->cf_node
;
281 nir_block
*else_stmt
= nir_block_create(mem_ctx
);
282 exec_list_make_empty(&if_stmt
->else_list
);
283 exec_list_push_tail(&if_stmt
->else_list
, &else_stmt
->cf_node
.node
);
284 else_stmt
->cf_node
.parent
= &if_stmt
->cf_node
;
290 nir_loop_create(void *mem_ctx
)
292 nir_loop
*loop
= ralloc(mem_ctx
, nir_loop
);
294 cf_init(&loop
->cf_node
, nir_cf_node_loop
);
296 nir_block
*body
= nir_block_create(mem_ctx
);
297 exec_list_make_empty(&loop
->body
);
298 exec_list_push_tail(&loop
->body
, &body
->cf_node
.node
);
299 body
->cf_node
.parent
= &loop
->cf_node
;
301 body
->successors
[0] = body
;
302 _mesa_set_add(body
->predecessors
, body
);
308 instr_init(nir_instr
*instr
, nir_instr_type type
)
312 exec_node_init(&instr
->node
);
316 dest_init(nir_dest
*dest
)
318 dest
->is_ssa
= false;
319 dest
->reg
.reg
= NULL
;
320 dest
->reg
.indirect
= NULL
;
321 dest
->reg
.base_offset
= 0;
325 alu_dest_init(nir_alu_dest
*dest
)
327 dest_init(&dest
->dest
);
328 dest
->saturate
= false;
329 dest
->write_mask
= 0xf;
333 alu_src_init(nir_alu_src
*src
)
336 src
->abs
= src
->negate
= false;
344 nir_alu_instr_create(nir_shader
*shader
, nir_op op
)
346 unsigned num_srcs
= nir_op_infos
[op
].num_inputs
;
347 nir_alu_instr
*instr
=
349 sizeof(nir_alu_instr
) + num_srcs
* sizeof(nir_alu_src
));
351 instr_init(&instr
->instr
, nir_instr_type_alu
);
353 alu_dest_init(&instr
->dest
);
354 for (unsigned i
= 0; i
< num_srcs
; i
++)
355 alu_src_init(&instr
->src
[i
]);
361 nir_jump_instr_create(nir_shader
*shader
, nir_jump_type type
)
363 nir_jump_instr
*instr
= ralloc(shader
, nir_jump_instr
);
364 instr_init(&instr
->instr
, nir_instr_type_jump
);
369 nir_load_const_instr
*
370 nir_load_const_instr_create(nir_shader
*shader
, unsigned num_components
)
372 nir_load_const_instr
*instr
= ralloc(shader
, nir_load_const_instr
);
373 instr_init(&instr
->instr
, nir_instr_type_load_const
);
375 nir_ssa_def_init(&instr
->instr
, &instr
->def
, num_components
, NULL
);
380 nir_intrinsic_instr
*
381 nir_intrinsic_instr_create(nir_shader
*shader
, nir_intrinsic_op op
)
383 unsigned num_srcs
= nir_intrinsic_infos
[op
].num_srcs
;
384 nir_intrinsic_instr
*instr
=
386 sizeof(nir_intrinsic_instr
) + num_srcs
* sizeof(nir_src
));
388 instr_init(&instr
->instr
, nir_instr_type_intrinsic
);
389 instr
->intrinsic
= op
;
391 if (nir_intrinsic_infos
[op
].has_dest
)
392 dest_init(&instr
->dest
);
394 for (unsigned i
= 0; i
< num_srcs
; i
++)
395 src_init(&instr
->src
[i
]);
401 nir_call_instr_create(nir_shader
*shader
, nir_function_overload
*callee
)
403 nir_call_instr
*instr
= ralloc(shader
, nir_call_instr
);
404 instr_init(&instr
->instr
, nir_instr_type_call
);
406 instr
->callee
= callee
;
407 instr
->num_params
= callee
->num_params
;
408 instr
->params
= ralloc_array(instr
, nir_deref_var
*, instr
->num_params
);
409 instr
->return_deref
= NULL
;
415 nir_tex_instr_create(nir_shader
*shader
, unsigned num_srcs
)
417 nir_tex_instr
*instr
= rzalloc(shader
, nir_tex_instr
);
418 instr_init(&instr
->instr
, nir_instr_type_tex
);
420 dest_init(&instr
->dest
);
422 instr
->num_srcs
= num_srcs
;
423 instr
->src
= ralloc_array(instr
, nir_tex_src
, num_srcs
);
424 for (unsigned i
= 0; i
< num_srcs
; i
++)
425 src_init(&instr
->src
[i
].src
);
427 instr
->sampler_index
= 0;
428 instr
->sampler_array_size
= 0;
429 instr
->sampler
= NULL
;
435 nir_phi_instr_create(nir_shader
*shader
)
437 nir_phi_instr
*instr
= ralloc(shader
, nir_phi_instr
);
438 instr_init(&instr
->instr
, nir_instr_type_phi
);
440 dest_init(&instr
->dest
);
441 exec_list_make_empty(&instr
->srcs
);
445 nir_parallel_copy_instr
*
446 nir_parallel_copy_instr_create(nir_shader
*shader
)
448 nir_parallel_copy_instr
*instr
= ralloc(shader
, nir_parallel_copy_instr
);
449 instr_init(&instr
->instr
, nir_instr_type_parallel_copy
);
451 exec_list_make_empty(&instr
->entries
);
456 nir_ssa_undef_instr
*
457 nir_ssa_undef_instr_create(nir_shader
*shader
, unsigned num_components
)
459 nir_ssa_undef_instr
*instr
= ralloc(shader
, nir_ssa_undef_instr
);
460 instr_init(&instr
->instr
, nir_instr_type_ssa_undef
);
462 nir_ssa_def_init(&instr
->instr
, &instr
->def
, num_components
, NULL
);
468 nir_deref_var_create(void *mem_ctx
, nir_variable
*var
)
470 nir_deref_var
*deref
= ralloc(mem_ctx
, nir_deref_var
);
471 deref
->deref
.deref_type
= nir_deref_type_var
;
472 deref
->deref
.child
= NULL
;
473 deref
->deref
.type
= var
->type
;
479 nir_deref_array_create(void *mem_ctx
)
481 nir_deref_array
*deref
= ralloc(mem_ctx
, nir_deref_array
);
482 deref
->deref
.deref_type
= nir_deref_type_array
;
483 deref
->deref
.child
= NULL
;
484 deref
->deref_array_type
= nir_deref_array_type_direct
;
485 src_init(&deref
->indirect
);
486 deref
->base_offset
= 0;
491 nir_deref_struct_create(void *mem_ctx
, unsigned field_index
)
493 nir_deref_struct
*deref
= ralloc(mem_ctx
, nir_deref_struct
);
494 deref
->deref
.deref_type
= nir_deref_type_struct
;
495 deref
->deref
.child
= NULL
;
496 deref
->index
= field_index
;
500 static nir_deref_var
*
501 copy_deref_var(void *mem_ctx
, nir_deref_var
*deref
)
503 nir_deref_var
*ret
= nir_deref_var_create(mem_ctx
, deref
->var
);
504 ret
->deref
.type
= deref
->deref
.type
;
505 if (deref
->deref
.child
)
506 ret
->deref
.child
= nir_copy_deref(ret
, deref
->deref
.child
);
510 static nir_deref_array
*
511 copy_deref_array(void *mem_ctx
, nir_deref_array
*deref
)
513 nir_deref_array
*ret
= nir_deref_array_create(mem_ctx
);
514 ret
->base_offset
= deref
->base_offset
;
515 ret
->deref_array_type
= deref
->deref_array_type
;
516 if (deref
->deref_array_type
== nir_deref_array_type_indirect
) {
517 nir_src_copy(&ret
->indirect
, &deref
->indirect
, mem_ctx
);
519 ret
->deref
.type
= deref
->deref
.type
;
520 if (deref
->deref
.child
)
521 ret
->deref
.child
= nir_copy_deref(ret
, deref
->deref
.child
);
525 static nir_deref_struct
*
526 copy_deref_struct(void *mem_ctx
, nir_deref_struct
*deref
)
528 nir_deref_struct
*ret
= nir_deref_struct_create(mem_ctx
, deref
->index
);
529 ret
->deref
.type
= deref
->deref
.type
;
530 if (deref
->deref
.child
)
531 ret
->deref
.child
= nir_copy_deref(ret
, deref
->deref
.child
);
536 nir_copy_deref(void *mem_ctx
, nir_deref
*deref
)
538 switch (deref
->deref_type
) {
539 case nir_deref_type_var
:
540 return ©_deref_var(mem_ctx
, nir_deref_as_var(deref
))->deref
;
541 case nir_deref_type_array
:
542 return ©_deref_array(mem_ctx
, nir_deref_as_array(deref
))->deref
;
543 case nir_deref_type_struct
:
544 return ©_deref_struct(mem_ctx
, nir_deref_as_struct(deref
))->deref
;
546 unreachable("Invalid dereference type");
552 /* Returns a load_const instruction that represents the constant
553 * initializer for the given deref chain. The caller is responsible for
554 * ensuring that there actually is a constant initializer.
556 nir_load_const_instr
*
557 nir_deref_get_const_initializer_load(nir_shader
*shader
, nir_deref_var
*deref
)
559 nir_constant
*constant
= deref
->var
->constant_initializer
;
562 const nir_deref
*tail
= &deref
->deref
;
563 unsigned matrix_offset
= 0;
564 while (tail
->child
) {
565 switch (tail
->child
->deref_type
) {
566 case nir_deref_type_array
: {
567 nir_deref_array
*arr
= nir_deref_as_array(tail
->child
);
568 assert(arr
->deref_array_type
== nir_deref_array_type_direct
);
569 if (glsl_type_is_matrix(tail
->type
)) {
570 assert(arr
->deref
.child
== NULL
);
571 matrix_offset
= arr
->base_offset
;
573 constant
= constant
->elements
[arr
->base_offset
];
578 case nir_deref_type_struct
: {
579 constant
= constant
->elements
[nir_deref_as_struct(tail
->child
)->index
];
584 unreachable("Invalid deref child type");
590 nir_load_const_instr
*load
=
591 nir_load_const_instr_create(shader
, glsl_get_vector_elements(tail
->type
));
593 matrix_offset
*= load
->def
.num_components
;
594 for (unsigned i
= 0; i
< load
->def
.num_components
; i
++) {
595 switch (glsl_get_base_type(tail
->type
)) {
596 case GLSL_TYPE_FLOAT
:
599 load
->value
.u
[i
] = constant
->value
.u
[matrix_offset
+ i
];
602 load
->value
.u
[i
] = constant
->value
.b
[matrix_offset
+ i
] ?
603 NIR_TRUE
: NIR_FALSE
;
606 unreachable("Invalid immediate type");
614 nir_cf_node_get_function(nir_cf_node
*node
)
616 while (node
->type
!= nir_cf_node_function
) {
620 return nir_cf_node_as_function(node
);
624 add_use_cb(nir_src
*src
, void *state
)
626 nir_instr
*instr
= state
;
628 src
->parent_instr
= instr
;
629 list_addtail(&src
->use_link
,
630 src
->is_ssa
? &src
->ssa
->uses
: &src
->reg
.reg
->uses
);
636 add_ssa_def_cb(nir_ssa_def
*def
, void *state
)
638 nir_instr
*instr
= state
;
640 if (instr
->block
&& def
->index
== UINT_MAX
) {
641 nir_function_impl
*impl
=
642 nir_cf_node_get_function(&instr
->block
->cf_node
);
644 def
->index
= impl
->ssa_alloc
++;
651 add_reg_def_cb(nir_dest
*dest
, void *state
)
653 nir_instr
*instr
= state
;
656 dest
->reg
.parent_instr
= instr
;
657 list_addtail(&dest
->reg
.def_link
, &dest
->reg
.reg
->defs
);
664 add_defs_uses(nir_instr
*instr
)
666 nir_foreach_src(instr
, add_use_cb
, instr
);
667 nir_foreach_dest(instr
, add_reg_def_cb
, instr
);
668 nir_foreach_ssa_def(instr
, add_ssa_def_cb
, instr
);
672 nir_instr_insert(nir_cursor cursor
, nir_instr
*instr
)
674 switch (cursor
.option
) {
675 case nir_cursor_before_block
:
676 /* Only allow inserting jumps into empty blocks. */
677 if (instr
->type
== nir_instr_type_jump
)
678 assert(exec_list_is_empty(&cursor
.block
->instr_list
));
680 instr
->block
= cursor
.block
;
681 add_defs_uses(instr
);
682 exec_list_push_head(&cursor
.block
->instr_list
, &instr
->node
);
684 case nir_cursor_after_block
: {
685 /* Inserting instructions after a jump is illegal. */
686 nir_instr
*last
= nir_block_last_instr(cursor
.block
);
687 assert(last
== NULL
|| last
->type
!= nir_instr_type_jump
);
690 instr
->block
= cursor
.block
;
691 add_defs_uses(instr
);
692 exec_list_push_tail(&cursor
.block
->instr_list
, &instr
->node
);
695 case nir_cursor_before_instr
:
696 assert(instr
->type
!= nir_instr_type_jump
);
697 instr
->block
= cursor
.instr
->block
;
698 add_defs_uses(instr
);
699 exec_node_insert_node_before(&cursor
.instr
->node
, &instr
->node
);
701 case nir_cursor_after_instr
:
702 /* Inserting instructions after a jump is illegal. */
703 assert(cursor
.instr
->type
!= nir_instr_type_jump
);
705 /* Only allow inserting jumps at the end of the block. */
706 if (instr
->type
== nir_instr_type_jump
)
707 assert(cursor
.instr
== nir_block_last_instr(cursor
.instr
->block
));
709 instr
->block
= cursor
.instr
->block
;
710 add_defs_uses(instr
);
711 exec_node_insert_after(&cursor
.instr
->node
, &instr
->node
);
715 if (instr
->type
== nir_instr_type_jump
)
716 nir_handle_add_jump(instr
->block
);
720 remove_use_cb(nir_src
*src
, void *state
)
722 list_del(&src
->use_link
);
728 remove_def_cb(nir_dest
*dest
, void *state
)
731 list_del(&dest
->reg
.def_link
);
737 remove_defs_uses(nir_instr
*instr
)
739 nir_foreach_dest(instr
, remove_def_cb
, instr
);
740 nir_foreach_src(instr
, remove_use_cb
, instr
);
743 void nir_instr_remove(nir_instr
*instr
)
745 remove_defs_uses(instr
);
746 exec_node_remove(&instr
->node
);
748 if (instr
->type
== nir_instr_type_jump
) {
749 nir_jump_instr
*jump_instr
= nir_instr_as_jump(instr
);
750 nir_handle_remove_jump(instr
->block
, jump_instr
->type
);
757 nir_index_local_regs(nir_function_impl
*impl
)
760 foreach_list_typed(nir_register
, reg
, node
, &impl
->registers
) {
761 reg
->index
= index
++;
763 impl
->reg_alloc
= index
;
767 nir_index_global_regs(nir_shader
*shader
)
770 foreach_list_typed(nir_register
, reg
, node
, &shader
->registers
) {
771 reg
->index
= index
++;
773 shader
->reg_alloc
= index
;
777 visit_alu_dest(nir_alu_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
779 return cb(&instr
->dest
.dest
, state
);
783 visit_intrinsic_dest(nir_intrinsic_instr
*instr
, nir_foreach_dest_cb cb
,
786 if (nir_intrinsic_infos
[instr
->intrinsic
].has_dest
)
787 return cb(&instr
->dest
, state
);
793 visit_texture_dest(nir_tex_instr
*instr
, nir_foreach_dest_cb cb
,
796 return cb(&instr
->dest
, state
);
800 visit_phi_dest(nir_phi_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
802 return cb(&instr
->dest
, state
);
806 visit_parallel_copy_dest(nir_parallel_copy_instr
*instr
,
807 nir_foreach_dest_cb cb
, void *state
)
809 nir_foreach_parallel_copy_entry(instr
, entry
) {
810 if (!cb(&entry
->dest
, state
))
818 nir_foreach_dest(nir_instr
*instr
, nir_foreach_dest_cb cb
, void *state
)
820 switch (instr
->type
) {
821 case nir_instr_type_alu
:
822 return visit_alu_dest(nir_instr_as_alu(instr
), cb
, state
);
823 case nir_instr_type_intrinsic
:
824 return visit_intrinsic_dest(nir_instr_as_intrinsic(instr
), cb
, state
);
825 case nir_instr_type_tex
:
826 return visit_texture_dest(nir_instr_as_tex(instr
), cb
, state
);
827 case nir_instr_type_phi
:
828 return visit_phi_dest(nir_instr_as_phi(instr
), cb
, state
);
829 case nir_instr_type_parallel_copy
:
830 return visit_parallel_copy_dest(nir_instr_as_parallel_copy(instr
),
833 case nir_instr_type_load_const
:
834 case nir_instr_type_ssa_undef
:
835 case nir_instr_type_call
:
836 case nir_instr_type_jump
:
840 unreachable("Invalid instruction type");
847 struct foreach_ssa_def_state
{
848 nir_foreach_ssa_def_cb cb
;
853 nir_ssa_def_visitor(nir_dest
*dest
, void *void_state
)
855 struct foreach_ssa_def_state
*state
= void_state
;
858 return state
->cb(&dest
->ssa
, state
->client_state
);
864 nir_foreach_ssa_def(nir_instr
*instr
, nir_foreach_ssa_def_cb cb
, void *state
)
866 switch (instr
->type
) {
867 case nir_instr_type_alu
:
868 case nir_instr_type_tex
:
869 case nir_instr_type_intrinsic
:
870 case nir_instr_type_phi
:
871 case nir_instr_type_parallel_copy
: {
872 struct foreach_ssa_def_state foreach_state
= {cb
, state
};
873 return nir_foreach_dest(instr
, nir_ssa_def_visitor
, &foreach_state
);
876 case nir_instr_type_load_const
:
877 return cb(&nir_instr_as_load_const(instr
)->def
, state
);
878 case nir_instr_type_ssa_undef
:
879 return cb(&nir_instr_as_ssa_undef(instr
)->def
, state
);
880 case nir_instr_type_call
:
881 case nir_instr_type_jump
:
884 unreachable("Invalid instruction type");
889 visit_src(nir_src
*src
, nir_foreach_src_cb cb
, void *state
)
893 if (!src
->is_ssa
&& src
->reg
.indirect
)
894 return cb(src
->reg
.indirect
, state
);
899 visit_deref_array_src(nir_deref_array
*deref
, nir_foreach_src_cb cb
,
902 if (deref
->deref_array_type
== nir_deref_array_type_indirect
)
903 return visit_src(&deref
->indirect
, cb
, state
);
908 visit_deref_src(nir_deref_var
*deref
, nir_foreach_src_cb cb
, void *state
)
910 nir_deref
*cur
= &deref
->deref
;
911 while (cur
!= NULL
) {
912 if (cur
->deref_type
== nir_deref_type_array
)
913 if (!visit_deref_array_src(nir_deref_as_array(cur
), cb
, state
))
923 visit_alu_src(nir_alu_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
925 for (unsigned i
= 0; i
< nir_op_infos
[instr
->op
].num_inputs
; i
++)
926 if (!visit_src(&instr
->src
[i
].src
, cb
, state
))
933 visit_tex_src(nir_tex_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
935 for (unsigned i
= 0; i
< instr
->num_srcs
; i
++)
936 if (!visit_src(&instr
->src
[i
].src
, cb
, state
))
939 if (instr
->sampler
!= NULL
)
940 if (!visit_deref_src(instr
->sampler
, cb
, state
))
947 visit_intrinsic_src(nir_intrinsic_instr
*instr
, nir_foreach_src_cb cb
,
950 unsigned num_srcs
= nir_intrinsic_infos
[instr
->intrinsic
].num_srcs
;
951 for (unsigned i
= 0; i
< num_srcs
; i
++)
952 if (!visit_src(&instr
->src
[i
], cb
, state
))
956 nir_intrinsic_infos
[instr
->intrinsic
].num_variables
;
957 for (unsigned i
= 0; i
< num_vars
; i
++)
958 if (!visit_deref_src(instr
->variables
[i
], cb
, state
))
965 visit_call_src(nir_call_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
971 visit_load_const_src(nir_load_const_instr
*instr
, nir_foreach_src_cb cb
,
978 visit_phi_src(nir_phi_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
980 nir_foreach_phi_src(instr
, src
) {
981 if (!visit_src(&src
->src
, cb
, state
))
989 visit_parallel_copy_src(nir_parallel_copy_instr
*instr
,
990 nir_foreach_src_cb cb
, void *state
)
992 nir_foreach_parallel_copy_entry(instr
, entry
) {
993 if (!visit_src(&entry
->src
, cb
, state
))
1002 nir_foreach_src_cb cb
;
1003 } visit_dest_indirect_state
;
1006 visit_dest_indirect(nir_dest
*dest
, void *_state
)
1008 visit_dest_indirect_state
*state
= (visit_dest_indirect_state
*) _state
;
1010 if (!dest
->is_ssa
&& dest
->reg
.indirect
)
1011 return state
->cb(dest
->reg
.indirect
, state
->state
);
1017 nir_foreach_src(nir_instr
*instr
, nir_foreach_src_cb cb
, void *state
)
1019 switch (instr
->type
) {
1020 case nir_instr_type_alu
:
1021 if (!visit_alu_src(nir_instr_as_alu(instr
), cb
, state
))
1024 case nir_instr_type_intrinsic
:
1025 if (!visit_intrinsic_src(nir_instr_as_intrinsic(instr
), cb
, state
))
1028 case nir_instr_type_tex
:
1029 if (!visit_tex_src(nir_instr_as_tex(instr
), cb
, state
))
1032 case nir_instr_type_call
:
1033 if (!visit_call_src(nir_instr_as_call(instr
), cb
, state
))
1036 case nir_instr_type_load_const
:
1037 if (!visit_load_const_src(nir_instr_as_load_const(instr
), cb
, state
))
1040 case nir_instr_type_phi
:
1041 if (!visit_phi_src(nir_instr_as_phi(instr
), cb
, state
))
1044 case nir_instr_type_parallel_copy
:
1045 if (!visit_parallel_copy_src(nir_instr_as_parallel_copy(instr
),
1049 case nir_instr_type_jump
:
1050 case nir_instr_type_ssa_undef
:
1054 unreachable("Invalid instruction type");
1058 visit_dest_indirect_state dest_state
;
1059 dest_state
.state
= state
;
1061 return nir_foreach_dest(instr
, visit_dest_indirect
, &dest_state
);
1065 nir_src_as_const_value(nir_src src
)
1070 if (src
.ssa
->parent_instr
->type
!= nir_instr_type_load_const
)
1073 nir_load_const_instr
*load
= nir_instr_as_load_const(src
.ssa
->parent_instr
);
1075 return &load
->value
;
1079 nir_srcs_equal(nir_src src1
, nir_src src2
)
1083 return src1
.ssa
== src2
.ssa
;
1091 if ((src1
.reg
.indirect
== NULL
) != (src2
.reg
.indirect
== NULL
))
1094 if (src1
.reg
.indirect
) {
1095 if (!nir_srcs_equal(*src1
.reg
.indirect
, *src2
.reg
.indirect
))
1099 return src1
.reg
.reg
== src2
.reg
.reg
&&
1100 src1
.reg
.base_offset
== src2
.reg
.base_offset
;
1106 src_is_valid(const nir_src
*src
)
1108 return src
->is_ssa
? (src
->ssa
!= NULL
) : (src
->reg
.reg
!= NULL
);
1112 src_remove_all_uses(nir_src
*src
)
1114 for (; src
; src
= src
->is_ssa
? NULL
: src
->reg
.indirect
) {
1115 if (!src_is_valid(src
))
1118 list_del(&src
->use_link
);
1123 src_add_all_uses(nir_src
*src
, nir_instr
*parent_instr
, nir_if
*parent_if
)
1125 for (; src
; src
= src
->is_ssa
? NULL
: src
->reg
.indirect
) {
1126 if (!src_is_valid(src
))
1130 src
->parent_instr
= parent_instr
;
1132 list_addtail(&src
->use_link
, &src
->ssa
->uses
);
1134 list_addtail(&src
->use_link
, &src
->reg
.reg
->uses
);
1137 src
->parent_if
= parent_if
;
1139 list_addtail(&src
->use_link
, &src
->ssa
->if_uses
);
1141 list_addtail(&src
->use_link
, &src
->reg
.reg
->if_uses
);
1147 nir_instr_rewrite_src(nir_instr
*instr
, nir_src
*src
, nir_src new_src
)
1149 assert(!src_is_valid(src
) || src
->parent_instr
== instr
);
1151 src_remove_all_uses(src
);
1153 src_add_all_uses(src
, instr
, NULL
);
1157 nir_instr_move_src(nir_instr
*dest_instr
, nir_src
*dest
, nir_src
*src
)
1159 assert(!src_is_valid(dest
) || dest
->parent_instr
== dest_instr
);
1161 src_remove_all_uses(dest
);
1162 src_remove_all_uses(src
);
1164 *src
= NIR_SRC_INIT
;
1165 src_add_all_uses(dest
, dest_instr
, NULL
);
1169 nir_if_rewrite_condition(nir_if
*if_stmt
, nir_src new_src
)
1171 nir_src
*src
= &if_stmt
->condition
;
1172 assert(!src_is_valid(src
) || src
->parent_if
== if_stmt
);
1174 src_remove_all_uses(src
);
1176 src_add_all_uses(src
, NULL
, if_stmt
);
1180 nir_ssa_def_init(nir_instr
*instr
, nir_ssa_def
*def
,
1181 unsigned num_components
, const char *name
)
1184 def
->parent_instr
= instr
;
1185 list_inithead(&def
->uses
);
1186 list_inithead(&def
->if_uses
);
1187 def
->num_components
= num_components
;
1190 nir_function_impl
*impl
=
1191 nir_cf_node_get_function(&instr
->block
->cf_node
);
1193 def
->index
= impl
->ssa_alloc
++;
1195 def
->index
= UINT_MAX
;
1200 nir_ssa_dest_init(nir_instr
*instr
, nir_dest
*dest
,
1201 unsigned num_components
, const char *name
)
1203 dest
->is_ssa
= true;
1204 nir_ssa_def_init(instr
, &dest
->ssa
, num_components
, name
);
1208 nir_ssa_def_rewrite_uses(nir_ssa_def
*def
, nir_src new_src
)
1210 assert(!new_src
.is_ssa
|| def
!= new_src
.ssa
);
1212 nir_foreach_use_safe(def
, use_src
) {
1213 nir_instr
*src_parent_instr
= use_src
->parent_instr
;
1214 list_del(&use_src
->use_link
);
1215 nir_src_copy(use_src
, &new_src
, src_parent_instr
);
1216 src_add_all_uses(use_src
, src_parent_instr
, NULL
);
1219 nir_foreach_if_use_safe(def
, use_src
) {
1220 nir_if
*src_parent_if
= use_src
->parent_if
;
1221 list_del(&use_src
->use_link
);
1222 nir_src_copy(use_src
, &new_src
, src_parent_if
);
1223 src_add_all_uses(use_src
, NULL
, src_parent_if
);
1228 static bool foreach_cf_node(nir_cf_node
*node
, nir_foreach_block_cb cb
,
1229 bool reverse
, void *state
);
1232 foreach_if(nir_if
*if_stmt
, nir_foreach_block_cb cb
, bool reverse
, void *state
)
1235 foreach_list_typed_safe_reverse(nir_cf_node
, node
, node
,
1236 &if_stmt
->else_list
) {
1237 if (!foreach_cf_node(node
, cb
, reverse
, state
))
1241 foreach_list_typed_safe_reverse(nir_cf_node
, node
, node
,
1242 &if_stmt
->then_list
) {
1243 if (!foreach_cf_node(node
, cb
, reverse
, state
))
1247 foreach_list_typed_safe(nir_cf_node
, node
, node
, &if_stmt
->then_list
) {
1248 if (!foreach_cf_node(node
, cb
, reverse
, state
))
1252 foreach_list_typed_safe(nir_cf_node
, node
, node
, &if_stmt
->else_list
) {
1253 if (!foreach_cf_node(node
, cb
, reverse
, state
))
1262 foreach_loop(nir_loop
*loop
, nir_foreach_block_cb cb
, bool reverse
, void *state
)
1265 foreach_list_typed_safe_reverse(nir_cf_node
, node
, node
, &loop
->body
) {
1266 if (!foreach_cf_node(node
, cb
, reverse
, state
))
1270 foreach_list_typed_safe(nir_cf_node
, node
, node
, &loop
->body
) {
1271 if (!foreach_cf_node(node
, cb
, reverse
, state
))
1280 foreach_cf_node(nir_cf_node
*node
, nir_foreach_block_cb cb
,
1281 bool reverse
, void *state
)
1283 switch (node
->type
) {
1284 case nir_cf_node_block
:
1285 return cb(nir_cf_node_as_block(node
), state
);
1286 case nir_cf_node_if
:
1287 return foreach_if(nir_cf_node_as_if(node
), cb
, reverse
, state
);
1288 case nir_cf_node_loop
:
1289 return foreach_loop(nir_cf_node_as_loop(node
), cb
, reverse
, state
);
1293 unreachable("Invalid CFG node type");
1301 nir_foreach_block_in_cf_node(nir_cf_node
*node
, nir_foreach_block_cb cb
,
1304 return foreach_cf_node(node
, cb
, false, state
);
1308 nir_foreach_block(nir_function_impl
*impl
, nir_foreach_block_cb cb
, void *state
)
1310 foreach_list_typed_safe(nir_cf_node
, node
, node
, &impl
->body
) {
1311 if (!foreach_cf_node(node
, cb
, false, state
))
1315 return cb(impl
->end_block
, state
);
1319 nir_foreach_block_reverse(nir_function_impl
*impl
, nir_foreach_block_cb cb
,
1322 if (!cb(impl
->end_block
, state
))
1325 foreach_list_typed_safe_reverse(nir_cf_node
, node
, node
, &impl
->body
) {
1326 if (!foreach_cf_node(node
, cb
, true, state
))
1334 nir_block_get_following_if(nir_block
*block
)
1336 if (exec_node_is_tail_sentinel(&block
->cf_node
.node
))
1339 if (nir_cf_node_is_last(&block
->cf_node
))
1342 nir_cf_node
*next_node
= nir_cf_node_next(&block
->cf_node
);
1344 if (next_node
->type
!= nir_cf_node_if
)
1347 return nir_cf_node_as_if(next_node
);
1351 nir_block_get_following_loop(nir_block
*block
)
1353 if (exec_node_is_tail_sentinel(&block
->cf_node
.node
))
1356 if (nir_cf_node_is_last(&block
->cf_node
))
1359 nir_cf_node
*next_node
= nir_cf_node_next(&block
->cf_node
);
1361 if (next_node
->type
!= nir_cf_node_loop
)
1364 return nir_cf_node_as_loop(next_node
);
1367 index_block(nir_block
*block
, void *state
)
1369 unsigned *index
= state
;
1370 block
->index
= (*index
)++;
1375 nir_index_blocks(nir_function_impl
*impl
)
1379 if (impl
->valid_metadata
& nir_metadata_block_index
)
1382 nir_foreach_block(impl
, index_block
, &index
);
1384 impl
->num_blocks
= index
;
1388 index_ssa_def_cb(nir_ssa_def
*def
, void *state
)
1390 unsigned *index
= (unsigned *) state
;
1391 def
->index
= (*index
)++;
1397 index_ssa_block(nir_block
*block
, void *state
)
1399 nir_foreach_instr(block
, instr
)
1400 nir_foreach_ssa_def(instr
, index_ssa_def_cb
, state
);
1406 nir_index_ssa_defs(nir_function_impl
*impl
)
1409 nir_foreach_block(impl
, index_ssa_block
, &index
);
1410 impl
->ssa_alloc
= index
;
1414 nir_system_value_from_intrinsic(nir_intrinsic_op intrin
)
1417 case nir_intrinsic_load_vertex_id
:
1418 return SYSTEM_VALUE_VERTEX_ID
;
1419 case nir_intrinsic_load_instance_id
:
1420 return SYSTEM_VALUE_INSTANCE_ID
;
1421 case nir_intrinsic_load_vertex_id_zero_base
:
1422 return SYSTEM_VALUE_VERTEX_ID_ZERO_BASE
;
1423 case nir_intrinsic_load_base_vertex
:
1424 return SYSTEM_VALUE_BASE_VERTEX
;
1425 case nir_intrinsic_load_invocation_id
:
1426 return SYSTEM_VALUE_INVOCATION_ID
;
1427 case nir_intrinsic_load_front_face
:
1428 return SYSTEM_VALUE_FRONT_FACE
;
1429 case nir_intrinsic_load_sample_id
:
1430 return SYSTEM_VALUE_SAMPLE_ID
;
1431 case nir_intrinsic_load_sample_pos
:
1432 return SYSTEM_VALUE_SAMPLE_POS
;
1433 case nir_intrinsic_load_sample_mask_in
:
1434 return SYSTEM_VALUE_SAMPLE_MASK_IN
;
1435 /* FINISHME: Add tessellation intrinsics.
1436 return SYSTEM_VALUE_TESS_COORD;
1437 return SYSTEM_VALUE_VERTICES_IN;
1438 return SYSTEM_VALUE_PRIMITIVE_ID;
1439 return SYSTEM_VALUE_TESS_LEVEL_OUTER;
1440 return SYSTEM_VALUE_TESS_LEVEL_INNER;
1443 unreachable("intrinsic doesn't produce a system value");