2 * Copyright © 2015 Thomas Helland
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
25 #include "nir_constant_expressions.h"
26 #include "nir_loop_analyze.h"
33 } nir_loop_variable_type
;
35 struct nir_basic_induction_var
;
38 /* A link for the work list */
39 struct list_head process_link
;
43 /* The ssa_def associated with this info */
46 /* The type of this ssa_def */
47 nir_loop_variable_type type
;
49 /* If this is of type basic_induction */
50 struct nir_basic_induction_var
*ind
;
52 /* True if variable is in an if branch */
55 /* True if variable is in a nested loop */
60 typedef struct nir_basic_induction_var
{
61 nir_op alu_op
; /* The type of alu-operation */
62 nir_loop_variable
*alu_def
; /* The def of the alu-operation */
63 nir_loop_variable
*invariant
; /* The invariant alu-operand */
64 nir_loop_variable
*def_outside_loop
; /* The phi-src outside the loop */
65 } nir_basic_induction_var
;
68 /* The loop we store information for */
71 /* Loop_variable for all ssa_defs in function */
72 nir_loop_variable
*loop_vars
;
74 /* A list of the loop_vars to analyze */
75 struct list_head process_list
;
77 nir_variable_mode indirect_mask
;
81 static nir_loop_variable
*
82 get_loop_var(nir_ssa_def
*value
, loop_info_state
*state
)
84 return &(state
->loop_vars
[value
->index
]);
88 loop_info_state
*state
;
94 init_loop_def(nir_ssa_def
*def
, void *void_init_loop_state
)
96 init_loop_state
*loop_init_state
= void_init_loop_state
;
97 nir_loop_variable
*var
= get_loop_var(def
, loop_init_state
->state
);
99 if (loop_init_state
->in_nested_loop
) {
100 var
->in_nested_loop
= true;
101 } else if (loop_init_state
->in_if_branch
) {
102 var
->in_if_branch
= true;
104 /* Add to the tail of the list. That way we start at the beginning of
105 * the defs in the loop instead of the end when walking the list. This
106 * means less recursive calls. Only add defs that are not in nested
107 * loops or conditional blocks.
109 list_addtail(&var
->process_link
, &loop_init_state
->state
->process_list
);
118 init_loop_block(nir_block
*block
, loop_info_state
*state
,
119 bool in_if_branch
, bool in_nested_loop
)
121 init_loop_state init_state
= {.in_if_branch
= in_if_branch
,
122 .in_nested_loop
= in_nested_loop
,
125 nir_foreach_instr(instr
, block
) {
126 if (instr
->type
== nir_instr_type_intrinsic
||
127 instr
->type
== nir_instr_type_alu
||
128 instr
->type
== nir_instr_type_tex
) {
129 state
->loop
->info
->num_instructions
++;
132 nir_foreach_ssa_def(instr
, init_loop_def
, &init_state
);
139 is_var_alu(nir_loop_variable
*var
)
141 return var
->def
->parent_instr
->type
== nir_instr_type_alu
;
145 is_var_constant(nir_loop_variable
*var
)
147 return var
->def
->parent_instr
->type
== nir_instr_type_load_const
;
151 is_var_phi(nir_loop_variable
*var
)
153 return var
->def
->parent_instr
->type
== nir_instr_type_phi
;
157 mark_invariant(nir_ssa_def
*def
, loop_info_state
*state
)
159 nir_loop_variable
*var
= get_loop_var(def
, state
);
161 if (var
->type
== invariant
)
165 var
->type
= invariant
;
169 if (var
->type
== not_invariant
)
172 if (is_var_alu(var
)) {
173 nir_alu_instr
*alu
= nir_instr_as_alu(def
->parent_instr
);
175 for (unsigned i
= 0; i
< nir_op_infos
[alu
->op
].num_inputs
; i
++) {
176 if (!mark_invariant(alu
->src
[i
].src
.ssa
, state
)) {
177 var
->type
= not_invariant
;
181 var
->type
= invariant
;
185 /* Phis shouldn't be invariant except if one operand is invariant, and the
186 * other is the phi itself. These should be removed by opt_remove_phis.
187 * load_consts are already set to invariant and constant during init,
188 * and so should return earlier. Remaining op_codes are set undefined.
190 var
->type
= not_invariant
;
195 compute_invariance_information(loop_info_state
*state
)
197 /* An expression is invariant in a loop L if:
200 * – it’s a variable use, all of whose single defs are outside of L
202 * – it’s a pure computation all of whose args are loop invariant
203 * – it’s a variable use whose single reaching def, and the
204 * rhs of that def is loop-invariant
206 list_for_each_entry_safe(nir_loop_variable
, var
, &state
->process_list
,
208 assert(!var
->in_if_branch
&& !var
->in_nested_loop
);
210 if (mark_invariant(var
->def
, state
))
211 list_del(&var
->process_link
);
216 compute_induction_information(loop_info_state
*state
)
218 bool found_induction_var
= false;
219 list_for_each_entry_safe(nir_loop_variable
, var
, &state
->process_list
,
222 /* It can't be an induction variable if it is invariant. Invariants and
223 * things in nested loops or conditionals should have been removed from
224 * the list by compute_invariance_information().
226 assert(!var
->in_if_branch
&& !var
->in_nested_loop
&&
227 var
->type
!= invariant
);
229 /* We are only interested in checking phis for the basic induction
230 * variable case as its simple to detect. All basic induction variables
233 if (!is_var_phi(var
))
236 nir_phi_instr
*phi
= nir_instr_as_phi(var
->def
->parent_instr
);
237 nir_basic_induction_var
*biv
= rzalloc(state
, nir_basic_induction_var
);
239 nir_foreach_phi_src(src
, phi
) {
240 nir_loop_variable
*src_var
= get_loop_var(src
->src
.ssa
, state
);
242 /* If one of the sources is in an if branch or nested loop then don't
243 * attempt to go any further.
245 if (src_var
->in_if_branch
|| src_var
->in_nested_loop
)
248 /* Detect inductions variables that are incremented in both branches
249 * of an unnested if rather than in a loop block.
251 if (is_var_phi(src_var
)) {
252 nir_phi_instr
*src_phi
=
253 nir_instr_as_phi(src_var
->def
->parent_instr
);
256 nir_ssa_def
*alu_srcs
[2] = {0};
257 nir_foreach_phi_src(src2
, src_phi
) {
258 nir_loop_variable
*src_var2
=
259 get_loop_var(src2
->src
.ssa
, state
);
261 if (!src_var2
->in_if_branch
|| !is_var_alu(src_var2
))
265 nir_instr_as_alu(src_var2
->def
->parent_instr
);
266 if (nir_op_infos
[alu
->op
].num_inputs
!= 2)
269 if (alu
->src
[0].src
.ssa
== alu_srcs
[0] &&
270 alu
->src
[1].src
.ssa
== alu_srcs
[1] &&
272 /* Both branches perform the same calculation so we can use
273 * one of them to find the induction variable.
277 alu_srcs
[0] = alu
->src
[0].src
.ssa
;
278 alu_srcs
[1] = alu
->src
[1].src
.ssa
;
284 if (!src_var
->in_loop
) {
285 biv
->def_outside_loop
= src_var
;
286 } else if (is_var_alu(src_var
)) {
287 nir_alu_instr
*alu
= nir_instr_as_alu(src_var
->def
->parent_instr
);
289 if (nir_op_infos
[alu
->op
].num_inputs
== 2) {
290 biv
->alu_def
= src_var
;
291 biv
->alu_op
= alu
->op
;
293 for (unsigned i
= 0; i
< 2; i
++) {
294 /* Is one of the operands const, and the other the phi */
295 if (alu
->src
[i
].src
.ssa
->parent_instr
->type
== nir_instr_type_load_const
&&
296 alu
->src
[1-i
].src
.ssa
== &phi
->dest
.ssa
)
297 biv
->invariant
= get_loop_var(alu
->src
[i
].src
.ssa
, state
);
303 if (biv
->alu_def
&& biv
->def_outside_loop
&& biv
->invariant
&&
304 is_var_constant(biv
->def_outside_loop
)) {
305 assert(is_var_constant(biv
->invariant
));
306 biv
->alu_def
->type
= basic_induction
;
307 biv
->alu_def
->ind
= biv
;
308 var
->type
= basic_induction
;
311 found_induction_var
= true;
316 return found_induction_var
;
320 initialize_ssa_def(nir_ssa_def
*def
, void *void_state
)
322 loop_info_state
*state
= void_state
;
323 nir_loop_variable
*var
= get_loop_var(def
, state
);
325 var
->in_loop
= false;
328 if (def
->parent_instr
->type
== nir_instr_type_load_const
) {
329 var
->type
= invariant
;
331 var
->type
= undefined
;
338 find_loop_terminators(loop_info_state
*state
)
340 bool success
= false;
341 foreach_list_typed_safe(nir_cf_node
, node
, node
, &state
->loop
->body
) {
342 if (node
->type
== nir_cf_node_if
) {
343 nir_if
*nif
= nir_cf_node_as_if(node
);
345 nir_block
*break_blk
= NULL
;
346 nir_block
*continue_from_blk
= NULL
;
347 bool continue_from_then
= true;
349 nir_block
*last_then
= nir_if_last_then_block(nif
);
350 nir_block
*last_else
= nir_if_last_else_block(nif
);
351 if (nir_block_ends_in_break(last_then
)) {
352 break_blk
= last_then
;
353 continue_from_blk
= last_else
;
354 continue_from_then
= false;
355 } else if (nir_block_ends_in_break(last_else
)) {
356 break_blk
= last_else
;
357 continue_from_blk
= last_then
;
360 /* If there is a break then we should find a terminator. If we can
361 * not find a loop terminator, but there is a break-statement then
362 * we should return false so that we do not try to find trip-count
364 if (!nir_is_trivial_loop_if(nif
, break_blk
)) {
365 state
->loop
->info
->complex_loop
= true;
369 /* Continue if the if contained no jumps at all */
373 if (nif
->condition
.ssa
->parent_instr
->type
== nir_instr_type_phi
) {
374 state
->loop
->info
->complex_loop
= true;
378 nir_loop_terminator
*terminator
=
379 rzalloc(state
->loop
->info
, nir_loop_terminator
);
381 list_addtail(&terminator
->loop_terminator_link
,
382 &state
->loop
->info
->loop_terminator_list
);
384 terminator
->nif
= nif
;
385 terminator
->break_block
= break_blk
;
386 terminator
->continue_from_block
= continue_from_blk
;
387 terminator
->continue_from_then
= continue_from_then
;
388 terminator
->conditional_instr
= nif
->condition
.ssa
->parent_instr
;
397 /* This function looks for an array access within a loop that uses an
398 * induction variable for the array index. If found it returns the size of the
399 * array, otherwise 0 is returned. If we find an induction var we pass it back
400 * to the caller via array_index_out.
403 find_array_access_via_induction(loop_info_state
*state
,
404 nir_deref_instr
*deref
,
405 nir_loop_variable
**array_index_out
)
407 for (nir_deref_instr
*d
= deref
; d
; d
= nir_deref_instr_parent(d
)) {
408 if (d
->deref_type
!= nir_deref_type_array
)
411 assert(d
->arr
.index
.is_ssa
);
412 nir_loop_variable
*array_index
= get_loop_var(d
->arr
.index
.ssa
, state
);
414 if (array_index
->type
!= basic_induction
)
418 *array_index_out
= array_index
;
420 nir_deref_instr
*parent
= nir_deref_instr_parent(d
);
421 assert(glsl_type_is_array_or_matrix(parent
->type
));
423 return glsl_get_length(parent
->type
);
430 get_iteration(nir_op cond_op
, nir_const_value
*initial
, nir_const_value
*step
,
431 nir_const_value
*limit
)
440 int32_t initial_val
= initial
->i32
[0];
441 int32_t span
= limit
->i32
[0] - initial_val
;
442 iter
= span
/ step
->i32
[0];
447 uint32_t initial_val
= initial
->u32
[0];
448 uint32_t span
= limit
->u32
[0] - initial_val
;
449 iter
= span
/ step
->u32
[0];
456 float initial_val
= initial
->f32
[0];
457 float span
= limit
->f32
[0] - initial_val
;
458 iter
= span
/ step
->f32
[0];
469 test_iterations(int32_t iter_int
, nir_const_value
*step
,
470 nir_const_value
*limit
, nir_op cond_op
, unsigned bit_size
,
471 nir_alu_type induction_base_type
,
472 nir_const_value
*initial
, bool limit_rhs
, bool invert_cond
)
474 assert(nir_op_infos
[cond_op
].num_inputs
== 2);
476 nir_const_value iter_src
= { {0, } };
479 switch (induction_base_type
) {
481 iter_src
.f32
[0] = (float) iter_int
;
482 mul_op
= nir_op_fmul
;
483 add_op
= nir_op_fadd
;
487 iter_src
.i32
[0] = iter_int
;
488 mul_op
= nir_op_imul
;
489 add_op
= nir_op_iadd
;
492 unreachable("Unhandled induction variable base type!");
495 /* Multiple the iteration count we are testing by the number of times we
496 * step the induction variable each iteration.
498 nir_const_value mul_src
[2] = { iter_src
, *step
};
499 nir_const_value mul_result
=
500 nir_eval_const_opcode(mul_op
, 1, bit_size
, mul_src
);
502 /* Add the initial value to the accumulated induction variable total */
503 nir_const_value add_src
[2] = { mul_result
, *initial
};
504 nir_const_value add_result
=
505 nir_eval_const_opcode(add_op
, 1, bit_size
, add_src
);
507 nir_const_value src
[2] = { { {0, } }, { {0, } } };
508 src
[limit_rhs
? 0 : 1] = add_result
;
509 src
[limit_rhs
? 1 : 0] = *limit
;
511 /* Evaluate the loop exit condition */
512 nir_const_value result
= nir_eval_const_opcode(cond_op
, 1, bit_size
, src
);
514 return invert_cond
? (result
.u32
[0] == 0) : (result
.u32
[0] != 0);
518 calculate_iterations(nir_const_value
*initial
, nir_const_value
*step
,
519 nir_const_value
*limit
, nir_loop_variable
*alu_def
,
520 nir_alu_instr
*cond_alu
, bool limit_rhs
, bool invert_cond
)
522 assert(initial
!= NULL
&& step
!= NULL
&& limit
!= NULL
);
524 nir_alu_instr
*alu
= nir_instr_as_alu(alu_def
->def
->parent_instr
);
526 /* nir_op_isub should have been lowered away by this point */
527 assert(alu
->op
!= nir_op_isub
);
529 /* Make sure the alu type for our induction variable is compatible with the
530 * conditional alus input type. If its not something has gone really wrong.
532 nir_alu_type induction_base_type
=
533 nir_alu_type_get_base_type(nir_op_infos
[alu
->op
].output_type
);
534 if (induction_base_type
== nir_type_int
|| induction_base_type
== nir_type_uint
) {
535 assert(nir_alu_type_get_base_type(nir_op_infos
[cond_alu
->op
].input_types
[1]) == nir_type_int
||
536 nir_alu_type_get_base_type(nir_op_infos
[cond_alu
->op
].input_types
[1]) == nir_type_uint
);
538 assert(nir_alu_type_get_base_type(nir_op_infos
[cond_alu
->op
].input_types
[0]) ==
539 induction_base_type
);
542 /* Check for nsupported alu operations */
543 if (alu
->op
!= nir_op_iadd
&& alu
->op
!= nir_op_fadd
)
546 /* do-while loops can increment the starting value before the condition is
553 * Here we check if the induction variable is used directly by the loop
554 * condition and if so we assume we need to step the initial value.
556 unsigned trip_offset
= 0;
557 if (cond_alu
->src
[0].src
.ssa
== alu_def
->def
||
558 cond_alu
->src
[1].src
.ssa
== alu_def
->def
) {
562 int iter_int
= get_iteration(cond_alu
->op
, initial
, step
, limit
);
564 /* If iter_int is negative the loop is ill-formed or is the conditional is
565 * unsigned with a huge iteration count so don't bother going any further.
570 /* An explanation from the GLSL unrolling pass:
572 * Make sure that the calculated number of iterations satisfies the exit
573 * condition. This is needed to catch off-by-one errors and some types of
574 * ill-formed loops. For example, we need to detect that the following
575 * loop does not have a maximum iteration count.
577 * for (float x = 0.0; x != 0.9; x += 0.2);
579 assert(nir_src_bit_size(alu
->src
[0].src
) ==
580 nir_src_bit_size(alu
->src
[1].src
));
581 unsigned bit_size
= nir_src_bit_size(alu
->src
[0].src
);
582 for (int bias
= -1; bias
<= 1; bias
++) {
583 const int iter_bias
= iter_int
+ bias
;
585 if (test_iterations(iter_bias
, step
, limit
, cond_alu
->op
, bit_size
,
586 induction_base_type
, initial
,
587 limit_rhs
, invert_cond
)) {
588 return iter_bias
> 0 ? iter_bias
- trip_offset
: iter_bias
;
595 /* Run through each of the terminators of the loop and try to infer a possible
596 * trip-count. We need to check them all, and set the lowest trip-count as the
597 * trip-count of our loop. If one of the terminators has an undecidable
598 * trip-count we can not safely assume anything about the duration of the
602 find_trip_count(loop_info_state
*state
)
604 bool trip_count_known
= true;
605 nir_loop_terminator
*limiting_terminator
= NULL
;
606 int max_trip_count
= -1;
608 list_for_each_entry(nir_loop_terminator
, terminator
,
609 &state
->loop
->info
->loop_terminator_list
,
610 loop_terminator_link
) {
612 if (terminator
->conditional_instr
->type
!= nir_instr_type_alu
) {
613 /* If we get here the loop is dead and will get cleaned up by the
614 * nir_opt_dead_cf pass.
616 trip_count_known
= false;
620 nir_alu_instr
*alu
= nir_instr_as_alu(terminator
->conditional_instr
);
621 nir_loop_variable
*basic_ind
= NULL
;
622 nir_loop_variable
*limit
= NULL
;
623 bool limit_rhs
= true;
626 case nir_op_fge
: case nir_op_ige
: case nir_op_uge
:
627 case nir_op_flt
: case nir_op_ilt
: case nir_op_ult
:
628 case nir_op_feq
: case nir_op_ieq
:
629 case nir_op_fne
: case nir_op_ine
:
631 /* We assume that the limit is the "right" operand */
632 basic_ind
= get_loop_var(alu
->src
[0].src
.ssa
, state
);
633 limit
= get_loop_var(alu
->src
[1].src
.ssa
, state
);
635 if (basic_ind
->type
!= basic_induction
) {
636 /* We had it the wrong way, flip things around */
637 basic_ind
= get_loop_var(alu
->src
[1].src
.ssa
, state
);
638 limit
= get_loop_var(alu
->src
[0].src
.ssa
, state
);
642 /* The comparison has to have a basic induction variable
643 * and a constant for us to be able to find trip counts
645 if (basic_ind
->type
!= basic_induction
|| !is_var_constant(limit
)) {
646 trip_count_known
= false;
650 /* We have determined that we have the following constants:
651 * (With the typical int i = 0; i < x; i++; as an example)
654 * - Step / iteration size
655 * Thats all thats needed to calculate the trip-count
658 nir_const_value initial_val
=
659 nir_instr_as_load_const(basic_ind
->ind
->def_outside_loop
->
660 def
->parent_instr
)->value
;
662 nir_const_value step_val
=
663 nir_instr_as_load_const(basic_ind
->ind
->invariant
->def
->
664 parent_instr
)->value
;
666 nir_const_value limit_val
=
667 nir_instr_as_load_const(limit
->def
->parent_instr
)->value
;
669 int iterations
= calculate_iterations(&initial_val
, &step_val
,
671 basic_ind
->ind
->alu_def
, alu
,
673 terminator
->continue_from_then
);
675 /* Where we not able to calculate the iteration count */
676 if (iterations
== -1) {
677 trip_count_known
= false;
681 /* If this is the first run or we have found a smaller amount of
682 * iterations than previously (we have identified a more limiting
683 * terminator) set the trip count and limiting terminator.
685 if (max_trip_count
== -1 || iterations
< max_trip_count
) {
686 max_trip_count
= iterations
;
687 limiting_terminator
= terminator
;
692 trip_count_known
= false;
696 state
->loop
->info
->exact_trip_count_known
= trip_count_known
;
697 if (max_trip_count
> -1)
698 state
->loop
->info
->max_trip_count
= max_trip_count
;
699 state
->loop
->info
->limiting_terminator
= limiting_terminator
;
703 force_unroll_array_access(loop_info_state
*state
, nir_deref_instr
*deref
)
705 unsigned array_size
= find_array_access_via_induction(state
, deref
, NULL
);
707 if (array_size
== state
->loop
->info
->max_trip_count
)
710 if (deref
->mode
& state
->indirect_mask
)
718 force_unroll_heuristics(loop_info_state
*state
, nir_block
*block
)
720 nir_foreach_instr(instr
, block
) {
721 if (instr
->type
!= nir_instr_type_intrinsic
)
724 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
726 /* Check for arrays variably-indexed by a loop induction variable.
727 * Unrolling the loop may convert that access into constant-indexing.
729 if (intrin
->intrinsic
== nir_intrinsic_load_deref
||
730 intrin
->intrinsic
== nir_intrinsic_store_deref
||
731 intrin
->intrinsic
== nir_intrinsic_copy_deref
) {
732 if (force_unroll_array_access(state
,
733 nir_src_as_deref(intrin
->src
[0])))
736 if (intrin
->intrinsic
== nir_intrinsic_copy_deref
&&
737 force_unroll_array_access(state
,
738 nir_src_as_deref(intrin
->src
[1])))
747 get_loop_info(loop_info_state
*state
, nir_function_impl
*impl
)
749 /* Initialize all variables to "outside_loop". This also marks defs
750 * invariant and constant if they are nir_instr_type_load_consts
752 nir_foreach_block(block
, impl
) {
753 nir_foreach_instr(instr
, block
)
754 nir_foreach_ssa_def(instr
, initialize_ssa_def
, state
);
757 /* Add all entries in the outermost part of the loop to the processing list
758 * Mark the entries in conditionals or in nested loops accordingly
760 foreach_list_typed_safe(nir_cf_node
, node
, node
, &state
->loop
->body
) {
761 switch (node
->type
) {
763 case nir_cf_node_block
:
764 init_loop_block(nir_cf_node_as_block(node
), state
, false, false);
768 nir_foreach_block_in_cf_node(block
, node
)
769 init_loop_block(block
, state
, true, false);
772 case nir_cf_node_loop
:
773 nir_foreach_block_in_cf_node(block
, node
) {
774 init_loop_block(block
, state
, false, true);
778 case nir_cf_node_function
:
783 /* Try to find all simple terminators of the loop. If we can't find any,
784 * or we find possible terminators that have side effects then bail.
786 if (!find_loop_terminators(state
)) {
787 list_for_each_entry_safe(nir_loop_terminator
, terminator
,
788 &state
->loop
->info
->loop_terminator_list
,
789 loop_terminator_link
) {
790 list_del(&terminator
->loop_terminator_link
);
791 ralloc_free(terminator
);
796 /* Induction analysis needs invariance information so get that first */
797 compute_invariance_information(state
);
799 /* We have invariance information so try to find induction variables */
800 if (!compute_induction_information(state
))
803 /* Run through each of the terminators and try to compute a trip-count */
804 find_trip_count(state
);
806 nir_shader
*ns
= impl
->function
->shader
;
807 nir_foreach_block_in_cf_node(block
, &state
->loop
->cf_node
) {
808 if (force_unroll_heuristics(state
, block
)) {
809 state
->loop
->info
->force_unroll
= true;
815 static loop_info_state
*
816 initialize_loop_info_state(nir_loop
*loop
, void *mem_ctx
,
817 nir_function_impl
*impl
)
819 loop_info_state
*state
= rzalloc(mem_ctx
, loop_info_state
);
820 state
->loop_vars
= rzalloc_array(mem_ctx
, nir_loop_variable
,
824 list_inithead(&state
->process_list
);
827 ralloc_free(loop
->info
);
829 loop
->info
= rzalloc(loop
, nir_loop_info
);
831 list_inithead(&loop
->info
->loop_terminator_list
);
837 process_loops(nir_cf_node
*cf_node
, nir_variable_mode indirect_mask
)
839 switch (cf_node
->type
) {
840 case nir_cf_node_block
:
842 case nir_cf_node_if
: {
843 nir_if
*if_stmt
= nir_cf_node_as_if(cf_node
);
844 foreach_list_typed(nir_cf_node
, nested_node
, node
, &if_stmt
->then_list
)
845 process_loops(nested_node
, indirect_mask
);
846 foreach_list_typed(nir_cf_node
, nested_node
, node
, &if_stmt
->else_list
)
847 process_loops(nested_node
, indirect_mask
);
850 case nir_cf_node_loop
: {
851 nir_loop
*loop
= nir_cf_node_as_loop(cf_node
);
852 foreach_list_typed(nir_cf_node
, nested_node
, node
, &loop
->body
)
853 process_loops(nested_node
, indirect_mask
);
857 unreachable("unknown cf node type");
860 nir_loop
*loop
= nir_cf_node_as_loop(cf_node
);
861 nir_function_impl
*impl
= nir_cf_node_get_function(cf_node
);
862 void *mem_ctx
= ralloc_context(NULL
);
864 loop_info_state
*state
= initialize_loop_info_state(loop
, mem_ctx
, impl
);
865 state
->indirect_mask
= indirect_mask
;
867 get_loop_info(state
, impl
);
869 ralloc_free(mem_ctx
);
873 nir_loop_analyze_impl(nir_function_impl
*impl
,
874 nir_variable_mode indirect_mask
)
876 nir_index_ssa_defs(impl
);
877 foreach_list_typed(nir_cf_node
, node
, node
, &impl
->body
)
878 process_loops(node
, indirect_mask
);