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
);
117 /** Calculate an estimated cost in number of instructions
119 * We do this so that we don't unroll loops which will later get massively
120 * inflated due to int64 or fp64 lowering. The estimates provided here don't
121 * have to be massively accurate; they just have to be good enough that loop
122 * unrolling doesn't cause things to blow up too much.
125 instr_cost(nir_instr
*instr
, const nir_shader_compiler_options
*options
)
127 if (instr
->type
== nir_instr_type_intrinsic
||
128 instr
->type
== nir_instr_type_tex
)
131 if (instr
->type
!= nir_instr_type_alu
)
134 nir_alu_instr
*alu
= nir_instr_as_alu(instr
);
135 const nir_op_info
*info
= &nir_op_infos
[alu
->op
];
137 /* Assume everything 16 or 32-bit is cheap.
139 * There are no 64-bit ops that don't have a 64-bit thing as their
140 * destination or first source.
142 if (nir_dest_bit_size(alu
->dest
.dest
) < 64 &&
143 nir_src_bit_size(alu
->src
[0].src
) < 64)
146 bool is_fp64
= nir_dest_bit_size(alu
->dest
.dest
) == 64 &&
147 nir_alu_type_get_base_type(info
->output_type
) == nir_type_float
;
148 for (unsigned i
= 0; i
< info
->num_inputs
; i
++) {
149 if (nir_src_bit_size(alu
->src
[i
].src
) == 64 &&
150 nir_alu_type_get_base_type(info
->input_types
[i
]) == nir_type_float
)
155 /* If it's something lowered normally, it's expensive. */
157 if (options
->lower_doubles_options
&
158 nir_lower_doubles_op_to_options_mask(alu
->op
))
161 /* If it's full software, it's even more expensive */
162 if (options
->lower_doubles_options
& nir_lower_fp64_full_software
)
167 if (options
->lower_int64_options
&
168 nir_lower_int64_op_to_options_mask(alu
->op
)) {
169 /* These require a doing the division algorithm. */
170 if (alu
->op
== nir_op_idiv
|| alu
->op
== nir_op_udiv
||
171 alu
->op
== nir_op_imod
|| alu
->op
== nir_op_umod
||
172 alu
->op
== nir_op_irem
)
175 /* Other int64 lowering isn't usually all that expensive */
184 init_loop_block(nir_block
*block
, loop_info_state
*state
,
185 bool in_if_branch
, bool in_nested_loop
,
186 const nir_shader_compiler_options
*options
)
188 init_loop_state init_state
= {.in_if_branch
= in_if_branch
,
189 .in_nested_loop
= in_nested_loop
,
192 nir_foreach_instr(instr
, block
) {
193 state
->loop
->info
->instr_cost
+= instr_cost(instr
, options
);
194 nir_foreach_ssa_def(instr
, init_loop_def
, &init_state
);
201 is_var_alu(nir_loop_variable
*var
)
203 return var
->def
->parent_instr
->type
== nir_instr_type_alu
;
207 is_var_constant(nir_loop_variable
*var
)
209 return var
->def
->parent_instr
->type
== nir_instr_type_load_const
;
213 is_var_phi(nir_loop_variable
*var
)
215 return var
->def
->parent_instr
->type
== nir_instr_type_phi
;
219 mark_invariant(nir_ssa_def
*def
, loop_info_state
*state
)
221 nir_loop_variable
*var
= get_loop_var(def
, state
);
223 if (var
->type
== invariant
)
227 var
->type
= invariant
;
231 if (var
->type
== not_invariant
)
234 if (is_var_alu(var
)) {
235 nir_alu_instr
*alu
= nir_instr_as_alu(def
->parent_instr
);
237 for (unsigned i
= 0; i
< nir_op_infos
[alu
->op
].num_inputs
; i
++) {
238 if (!mark_invariant(alu
->src
[i
].src
.ssa
, state
)) {
239 var
->type
= not_invariant
;
243 var
->type
= invariant
;
247 /* Phis shouldn't be invariant except if one operand is invariant, and the
248 * other is the phi itself. These should be removed by opt_remove_phis.
249 * load_consts are already set to invariant and constant during init,
250 * and so should return earlier. Remaining op_codes are set undefined.
252 var
->type
= not_invariant
;
257 compute_invariance_information(loop_info_state
*state
)
259 /* An expression is invariant in a loop L if:
262 * – it’s a variable use, all of whose single defs are outside of L
264 * – it’s a pure computation all of whose args are loop invariant
265 * – it’s a variable use whose single reaching def, and the
266 * rhs of that def is loop-invariant
268 list_for_each_entry_safe(nir_loop_variable
, var
, &state
->process_list
,
270 assert(!var
->in_if_branch
&& !var
->in_nested_loop
);
272 if (mark_invariant(var
->def
, state
))
273 list_del(&var
->process_link
);
278 compute_induction_information(loop_info_state
*state
)
280 bool found_induction_var
= false;
281 list_for_each_entry_safe(nir_loop_variable
, var
, &state
->process_list
,
284 /* It can't be an induction variable if it is invariant. Invariants and
285 * things in nested loops or conditionals should have been removed from
286 * the list by compute_invariance_information().
288 assert(!var
->in_if_branch
&& !var
->in_nested_loop
&&
289 var
->type
!= invariant
);
291 /* We are only interested in checking phis for the basic induction
292 * variable case as its simple to detect. All basic induction variables
295 if (!is_var_phi(var
))
298 nir_phi_instr
*phi
= nir_instr_as_phi(var
->def
->parent_instr
);
299 nir_basic_induction_var
*biv
= rzalloc(state
, nir_basic_induction_var
);
301 nir_foreach_phi_src(src
, phi
) {
302 nir_loop_variable
*src_var
= get_loop_var(src
->src
.ssa
, state
);
304 /* If one of the sources is in an if branch or nested loop then don't
305 * attempt to go any further.
307 if (src_var
->in_if_branch
|| src_var
->in_nested_loop
)
310 /* Detect inductions variables that are incremented in both branches
311 * of an unnested if rather than in a loop block.
313 if (is_var_phi(src_var
)) {
314 nir_phi_instr
*src_phi
=
315 nir_instr_as_phi(src_var
->def
->parent_instr
);
317 nir_op alu_op
= nir_num_opcodes
; /* avoid uninitialized warning */
318 nir_ssa_def
*alu_srcs
[2] = {0};
319 nir_foreach_phi_src(src2
, src_phi
) {
320 nir_loop_variable
*src_var2
=
321 get_loop_var(src2
->src
.ssa
, state
);
323 if (!src_var2
->in_if_branch
|| !is_var_alu(src_var2
))
327 nir_instr_as_alu(src_var2
->def
->parent_instr
);
328 if (nir_op_infos
[alu
->op
].num_inputs
!= 2)
331 if (alu
->src
[0].src
.ssa
== alu_srcs
[0] &&
332 alu
->src
[1].src
.ssa
== alu_srcs
[1] &&
334 /* Both branches perform the same calculation so we can use
335 * one of them to find the induction variable.
339 alu_srcs
[0] = alu
->src
[0].src
.ssa
;
340 alu_srcs
[1] = alu
->src
[1].src
.ssa
;
346 if (!src_var
->in_loop
) {
347 biv
->def_outside_loop
= src_var
;
348 } else if (is_var_alu(src_var
)) {
349 nir_alu_instr
*alu
= nir_instr_as_alu(src_var
->def
->parent_instr
);
351 if (nir_op_infos
[alu
->op
].num_inputs
== 2) {
352 biv
->alu_def
= src_var
;
353 biv
->alu_op
= alu
->op
;
355 for (unsigned i
= 0; i
< 2; i
++) {
356 /* Is one of the operands const, and the other the phi */
357 if (alu
->src
[i
].src
.ssa
->parent_instr
->type
== nir_instr_type_load_const
&&
358 alu
->src
[1-i
].src
.ssa
== &phi
->dest
.ssa
)
359 biv
->invariant
= get_loop_var(alu
->src
[i
].src
.ssa
, state
);
365 if (biv
->alu_def
&& biv
->def_outside_loop
&& biv
->invariant
&&
366 is_var_constant(biv
->def_outside_loop
)) {
367 assert(is_var_constant(biv
->invariant
));
368 biv
->alu_def
->type
= basic_induction
;
369 biv
->alu_def
->ind
= biv
;
370 var
->type
= basic_induction
;
373 found_induction_var
= true;
378 return found_induction_var
;
382 initialize_ssa_def(nir_ssa_def
*def
, void *void_state
)
384 loop_info_state
*state
= void_state
;
385 nir_loop_variable
*var
= get_loop_var(def
, state
);
387 var
->in_loop
= false;
390 if (def
->parent_instr
->type
== nir_instr_type_load_const
) {
391 var
->type
= invariant
;
393 var
->type
= undefined
;
400 find_loop_terminators(loop_info_state
*state
)
402 bool success
= false;
403 foreach_list_typed_safe(nir_cf_node
, node
, node
, &state
->loop
->body
) {
404 if (node
->type
== nir_cf_node_if
) {
405 nir_if
*nif
= nir_cf_node_as_if(node
);
407 nir_block
*break_blk
= NULL
;
408 nir_block
*continue_from_blk
= NULL
;
409 bool continue_from_then
= true;
411 nir_block
*last_then
= nir_if_last_then_block(nif
);
412 nir_block
*last_else
= nir_if_last_else_block(nif
);
413 if (nir_block_ends_in_break(last_then
)) {
414 break_blk
= last_then
;
415 continue_from_blk
= last_else
;
416 continue_from_then
= false;
417 } else if (nir_block_ends_in_break(last_else
)) {
418 break_blk
= last_else
;
419 continue_from_blk
= last_then
;
422 /* If there is a break then we should find a terminator. If we can
423 * not find a loop terminator, but there is a break-statement then
424 * we should return false so that we do not try to find trip-count
426 if (!nir_is_trivial_loop_if(nif
, break_blk
)) {
427 state
->loop
->info
->complex_loop
= true;
431 /* Continue if the if contained no jumps at all */
435 if (nif
->condition
.ssa
->parent_instr
->type
== nir_instr_type_phi
) {
436 state
->loop
->info
->complex_loop
= true;
440 nir_loop_terminator
*terminator
=
441 rzalloc(state
->loop
->info
, nir_loop_terminator
);
443 list_addtail(&terminator
->loop_terminator_link
,
444 &state
->loop
->info
->loop_terminator_list
);
446 terminator
->nif
= nif
;
447 terminator
->break_block
= break_blk
;
448 terminator
->continue_from_block
= continue_from_blk
;
449 terminator
->continue_from_then
= continue_from_then
;
450 terminator
->conditional_instr
= nif
->condition
.ssa
->parent_instr
;
459 /* This function looks for an array access within a loop that uses an
460 * induction variable for the array index. If found it returns the size of the
461 * array, otherwise 0 is returned. If we find an induction var we pass it back
462 * to the caller via array_index_out.
465 find_array_access_via_induction(loop_info_state
*state
,
466 nir_deref_instr
*deref
,
467 nir_loop_variable
**array_index_out
)
469 for (nir_deref_instr
*d
= deref
; d
; d
= nir_deref_instr_parent(d
)) {
470 if (d
->deref_type
!= nir_deref_type_array
)
473 assert(d
->arr
.index
.is_ssa
);
474 nir_loop_variable
*array_index
= get_loop_var(d
->arr
.index
.ssa
, state
);
476 if (array_index
->type
!= basic_induction
)
480 *array_index_out
= array_index
;
482 nir_deref_instr
*parent
= nir_deref_instr_parent(d
);
483 assert(glsl_type_is_array_or_matrix(parent
->type
));
485 return glsl_get_length(parent
->type
);
492 guess_loop_limit(loop_info_state
*state
, nir_const_value
*limit_val
,
493 nir_loop_variable
*basic_ind
)
495 unsigned min_array_size
= 0;
497 nir_foreach_block_in_cf_node(block
, &state
->loop
->cf_node
) {
498 nir_foreach_instr(instr
, block
) {
499 if (instr
->type
!= nir_instr_type_intrinsic
)
502 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
504 /* Check for arrays variably-indexed by a loop induction variable. */
505 if (intrin
->intrinsic
== nir_intrinsic_load_deref
||
506 intrin
->intrinsic
== nir_intrinsic_store_deref
||
507 intrin
->intrinsic
== nir_intrinsic_copy_deref
) {
509 nir_loop_variable
*array_idx
= NULL
;
510 unsigned array_size
=
511 find_array_access_via_induction(state
,
512 nir_src_as_deref(intrin
->src
[0]),
514 if (basic_ind
== array_idx
&&
515 (min_array_size
== 0 || min_array_size
> array_size
)) {
516 min_array_size
= array_size
;
519 if (intrin
->intrinsic
!= nir_intrinsic_copy_deref
)
523 find_array_access_via_induction(state
,
524 nir_src_as_deref(intrin
->src
[1]),
526 if (basic_ind
== array_idx
&&
527 (min_array_size
== 0 || min_array_size
> array_size
)) {
528 min_array_size
= array_size
;
534 if (min_array_size
) {
535 limit_val
->i32
[0] = min_array_size
;
543 try_find_limit_of_alu(nir_loop_variable
*limit
, nir_const_value
*limit_val
,
544 nir_loop_terminator
*terminator
, loop_info_state
*state
)
546 if(!is_var_alu(limit
))
549 nir_alu_instr
*limit_alu
= nir_instr_as_alu(limit
->def
->parent_instr
);
551 if (limit_alu
->op
== nir_op_imin
||
552 limit_alu
->op
== nir_op_fmin
) {
553 limit
= get_loop_var(limit_alu
->src
[0].src
.ssa
, state
);
555 if (!is_var_constant(limit
))
556 limit
= get_loop_var(limit_alu
->src
[1].src
.ssa
, state
);
558 if (!is_var_constant(limit
))
561 *limit_val
= nir_instr_as_load_const(limit
->def
->parent_instr
)->value
;
563 terminator
->exact_trip_count_unknown
= true;
572 get_iteration(nir_op cond_op
, nir_const_value
*initial
, nir_const_value
*step
,
573 nir_const_value
*limit
)
582 int32_t initial_val
= initial
->i32
[0];
583 int32_t span
= limit
->i32
[0] - initial_val
;
584 iter
= span
/ step
->i32
[0];
589 uint32_t initial_val
= initial
->u32
[0];
590 uint32_t span
= limit
->u32
[0] - initial_val
;
591 iter
= span
/ step
->u32
[0];
598 float initial_val
= initial
->f32
[0];
599 float span
= limit
->f32
[0] - initial_val
;
600 iter
= span
/ step
->f32
[0];
611 test_iterations(int32_t iter_int
, nir_const_value
*step
,
612 nir_const_value
*limit
, nir_op cond_op
, unsigned bit_size
,
613 nir_alu_type induction_base_type
,
614 nir_const_value
*initial
, bool limit_rhs
, bool invert_cond
)
616 assert(nir_op_infos
[cond_op
].num_inputs
== 2);
618 nir_const_value iter_src
= { {0, } };
621 switch (induction_base_type
) {
623 iter_src
.f32
[0] = (float) iter_int
;
624 mul_op
= nir_op_fmul
;
625 add_op
= nir_op_fadd
;
629 iter_src
.i32
[0] = iter_int
;
630 mul_op
= nir_op_imul
;
631 add_op
= nir_op_iadd
;
634 unreachable("Unhandled induction variable base type!");
637 /* Multiple the iteration count we are testing by the number of times we
638 * step the induction variable each iteration.
640 nir_const_value mul_src
[2] = { iter_src
, *step
};
641 nir_const_value mul_result
=
642 nir_eval_const_opcode(mul_op
, 1, bit_size
, mul_src
);
644 /* Add the initial value to the accumulated induction variable total */
645 nir_const_value add_src
[2] = { mul_result
, *initial
};
646 nir_const_value add_result
=
647 nir_eval_const_opcode(add_op
, 1, bit_size
, add_src
);
649 nir_const_value src
[2] = { { {0, } }, { {0, } } };
650 src
[limit_rhs
? 0 : 1] = add_result
;
651 src
[limit_rhs
? 1 : 0] = *limit
;
653 /* Evaluate the loop exit condition */
654 nir_const_value result
= nir_eval_const_opcode(cond_op
, 1, bit_size
, src
);
656 return invert_cond
? (result
.u32
[0] == 0) : (result
.u32
[0] != 0);
660 calculate_iterations(nir_const_value
*initial
, nir_const_value
*step
,
661 nir_const_value
*limit
, nir_loop_variable
*alu_def
,
662 nir_alu_instr
*cond_alu
, bool limit_rhs
, bool invert_cond
)
664 assert(initial
!= NULL
&& step
!= NULL
&& limit
!= NULL
);
666 nir_alu_instr
*alu
= nir_instr_as_alu(alu_def
->def
->parent_instr
);
668 /* nir_op_isub should have been lowered away by this point */
669 assert(alu
->op
!= nir_op_isub
);
671 /* Make sure the alu type for our induction variable is compatible with the
672 * conditional alus input type. If its not something has gone really wrong.
674 nir_alu_type induction_base_type
=
675 nir_alu_type_get_base_type(nir_op_infos
[alu
->op
].output_type
);
676 if (induction_base_type
== nir_type_int
|| induction_base_type
== nir_type_uint
) {
677 assert(nir_alu_type_get_base_type(nir_op_infos
[cond_alu
->op
].input_types
[1]) == nir_type_int
||
678 nir_alu_type_get_base_type(nir_op_infos
[cond_alu
->op
].input_types
[1]) == nir_type_uint
);
680 assert(nir_alu_type_get_base_type(nir_op_infos
[cond_alu
->op
].input_types
[0]) ==
681 induction_base_type
);
684 /* Check for nsupported alu operations */
685 if (alu
->op
!= nir_op_iadd
&& alu
->op
!= nir_op_fadd
)
688 /* do-while loops can increment the starting value before the condition is
695 * Here we check if the induction variable is used directly by the loop
696 * condition and if so we assume we need to step the initial value.
698 unsigned trip_offset
= 0;
699 if (cond_alu
->src
[0].src
.ssa
== alu_def
->def
||
700 cond_alu
->src
[1].src
.ssa
== alu_def
->def
) {
704 int iter_int
= get_iteration(cond_alu
->op
, initial
, step
, limit
);
706 /* If iter_int is negative the loop is ill-formed or is the conditional is
707 * unsigned with a huge iteration count so don't bother going any further.
712 /* An explanation from the GLSL unrolling pass:
714 * Make sure that the calculated number of iterations satisfies the exit
715 * condition. This is needed to catch off-by-one errors and some types of
716 * ill-formed loops. For example, we need to detect that the following
717 * loop does not have a maximum iteration count.
719 * for (float x = 0.0; x != 0.9; x += 0.2);
721 assert(nir_src_bit_size(alu
->src
[0].src
) ==
722 nir_src_bit_size(alu
->src
[1].src
));
723 unsigned bit_size
= nir_src_bit_size(alu
->src
[0].src
);
724 for (int bias
= -1; bias
<= 1; bias
++) {
725 const int iter_bias
= iter_int
+ bias
;
727 if (test_iterations(iter_bias
, step
, limit
, cond_alu
->op
, bit_size
,
728 induction_base_type
, initial
,
729 limit_rhs
, invert_cond
)) {
730 return iter_bias
> 0 ? iter_bias
- trip_offset
: iter_bias
;
737 /* Run through each of the terminators of the loop and try to infer a possible
738 * trip-count. We need to check them all, and set the lowest trip-count as the
739 * trip-count of our loop. If one of the terminators has an undecidable
740 * trip-count we can not safely assume anything about the duration of the
744 find_trip_count(loop_info_state
*state
)
746 bool trip_count_known
= true;
747 bool guessed_trip_count
= false;
748 nir_loop_terminator
*limiting_terminator
= NULL
;
749 int max_trip_count
= -1;
751 list_for_each_entry(nir_loop_terminator
, terminator
,
752 &state
->loop
->info
->loop_terminator_list
,
753 loop_terminator_link
) {
755 if (terminator
->conditional_instr
->type
!= nir_instr_type_alu
) {
756 /* If we get here the loop is dead and will get cleaned up by the
757 * nir_opt_dead_cf pass.
759 trip_count_known
= false;
763 nir_alu_instr
*alu
= nir_instr_as_alu(terminator
->conditional_instr
);
764 nir_loop_variable
*basic_ind
= NULL
;
765 nir_loop_variable
*limit
= NULL
;
766 bool limit_rhs
= true;
769 case nir_op_fge
: case nir_op_ige
: case nir_op_uge
:
770 case nir_op_flt
: case nir_op_ilt
: case nir_op_ult
:
771 case nir_op_feq
: case nir_op_ieq
:
772 case nir_op_fne
: case nir_op_ine
:
774 /* We assume that the limit is the "right" operand */
775 basic_ind
= get_loop_var(alu
->src
[0].src
.ssa
, state
);
776 limit
= get_loop_var(alu
->src
[1].src
.ssa
, state
);
778 if (basic_ind
->type
!= basic_induction
) {
779 /* We had it the wrong way, flip things around */
780 basic_ind
= get_loop_var(alu
->src
[1].src
.ssa
, state
);
781 limit
= get_loop_var(alu
->src
[0].src
.ssa
, state
);
783 terminator
->induction_rhs
= true;
786 /* The comparison has to have a basic induction variable for us to be
787 * able to find trip counts.
789 if (basic_ind
->type
!= basic_induction
) {
790 trip_count_known
= false;
794 /* Attempt to find a constant limit for the loop */
795 nir_const_value limit_val
;
796 if (is_var_constant(limit
)) {
798 nir_instr_as_load_const(limit
->def
->parent_instr
)->value
;
800 trip_count_known
= false;
802 if (!try_find_limit_of_alu(limit
, &limit_val
, terminator
, state
)) {
803 /* Guess loop limit based on array access */
804 if (!guess_loop_limit(state
, &limit_val
, basic_ind
)) {
808 guessed_trip_count
= true;
812 /* We have determined that we have the following constants:
813 * (With the typical int i = 0; i < x; i++; as an example)
816 * - Step / iteration size
817 * Thats all thats needed to calculate the trip-count
820 nir_const_value initial_val
=
821 nir_instr_as_load_const(basic_ind
->ind
->def_outside_loop
->
822 def
->parent_instr
)->value
;
824 nir_const_value step_val
=
825 nir_instr_as_load_const(basic_ind
->ind
->invariant
->def
->
826 parent_instr
)->value
;
828 int iterations
= calculate_iterations(&initial_val
, &step_val
,
830 basic_ind
->ind
->alu_def
, alu
,
832 terminator
->continue_from_then
);
834 /* Where we not able to calculate the iteration count */
835 if (iterations
== -1) {
836 trip_count_known
= false;
837 guessed_trip_count
= false;
841 if (guessed_trip_count
) {
842 guessed_trip_count
= false;
843 if (state
->loop
->info
->guessed_trip_count
== 0 ||
844 state
->loop
->info
->guessed_trip_count
> iterations
)
845 state
->loop
->info
->guessed_trip_count
= iterations
;
850 /* If this is the first run or we have found a smaller amount of
851 * iterations than previously (we have identified a more limiting
852 * terminator) set the trip count and limiting terminator.
854 if (max_trip_count
== -1 || iterations
< max_trip_count
) {
855 max_trip_count
= iterations
;
856 limiting_terminator
= terminator
;
861 trip_count_known
= false;
865 state
->loop
->info
->exact_trip_count_known
= trip_count_known
;
866 if (max_trip_count
> -1)
867 state
->loop
->info
->max_trip_count
= max_trip_count
;
868 state
->loop
->info
->limiting_terminator
= limiting_terminator
;
872 force_unroll_array_access(loop_info_state
*state
, nir_deref_instr
*deref
)
874 unsigned array_size
= find_array_access_via_induction(state
, deref
, NULL
);
876 if (array_size
== state
->loop
->info
->max_trip_count
)
879 if (deref
->mode
& state
->indirect_mask
)
887 force_unroll_heuristics(loop_info_state
*state
, nir_block
*block
)
889 nir_foreach_instr(instr
, block
) {
890 if (instr
->type
!= nir_instr_type_intrinsic
)
893 nir_intrinsic_instr
*intrin
= nir_instr_as_intrinsic(instr
);
895 /* Check for arrays variably-indexed by a loop induction variable.
896 * Unrolling the loop may convert that access into constant-indexing.
898 if (intrin
->intrinsic
== nir_intrinsic_load_deref
||
899 intrin
->intrinsic
== nir_intrinsic_store_deref
||
900 intrin
->intrinsic
== nir_intrinsic_copy_deref
) {
901 if (force_unroll_array_access(state
,
902 nir_src_as_deref(intrin
->src
[0])))
905 if (intrin
->intrinsic
== nir_intrinsic_copy_deref
&&
906 force_unroll_array_access(state
,
907 nir_src_as_deref(intrin
->src
[1])))
916 get_loop_info(loop_info_state
*state
, nir_function_impl
*impl
)
918 nir_shader
*shader
= impl
->function
->shader
;
919 const nir_shader_compiler_options
*options
= shader
->options
;
921 /* Initialize all variables to "outside_loop". This also marks defs
922 * invariant and constant if they are nir_instr_type_load_consts
924 nir_foreach_block(block
, impl
) {
925 nir_foreach_instr(instr
, block
)
926 nir_foreach_ssa_def(instr
, initialize_ssa_def
, state
);
929 /* Add all entries in the outermost part of the loop to the processing list
930 * Mark the entries in conditionals or in nested loops accordingly
932 foreach_list_typed_safe(nir_cf_node
, node
, node
, &state
->loop
->body
) {
933 switch (node
->type
) {
935 case nir_cf_node_block
:
936 init_loop_block(nir_cf_node_as_block(node
), state
,
937 false, false, options
);
941 nir_foreach_block_in_cf_node(block
, node
)
942 init_loop_block(block
, state
, true, false, options
);
945 case nir_cf_node_loop
:
946 nir_foreach_block_in_cf_node(block
, node
) {
947 init_loop_block(block
, state
, false, true, options
);
951 case nir_cf_node_function
:
956 /* Try to find all simple terminators of the loop. If we can't find any,
957 * or we find possible terminators that have side effects then bail.
959 if (!find_loop_terminators(state
)) {
960 list_for_each_entry_safe(nir_loop_terminator
, terminator
,
961 &state
->loop
->info
->loop_terminator_list
,
962 loop_terminator_link
) {
963 list_del(&terminator
->loop_terminator_link
);
964 ralloc_free(terminator
);
969 /* Induction analysis needs invariance information so get that first */
970 compute_invariance_information(state
);
972 /* We have invariance information so try to find induction variables */
973 if (!compute_induction_information(state
))
976 /* Run through each of the terminators and try to compute a trip-count */
977 find_trip_count(state
);
979 nir_foreach_block_in_cf_node(block
, &state
->loop
->cf_node
) {
980 if (force_unroll_heuristics(state
, block
)) {
981 state
->loop
->info
->force_unroll
= true;
987 static loop_info_state
*
988 initialize_loop_info_state(nir_loop
*loop
, void *mem_ctx
,
989 nir_function_impl
*impl
)
991 loop_info_state
*state
= rzalloc(mem_ctx
, loop_info_state
);
992 state
->loop_vars
= rzalloc_array(mem_ctx
, nir_loop_variable
,
996 list_inithead(&state
->process_list
);
999 ralloc_free(loop
->info
);
1001 loop
->info
= rzalloc(loop
, nir_loop_info
);
1003 list_inithead(&loop
->info
->loop_terminator_list
);
1009 process_loops(nir_cf_node
*cf_node
, nir_variable_mode indirect_mask
)
1011 switch (cf_node
->type
) {
1012 case nir_cf_node_block
:
1014 case nir_cf_node_if
: {
1015 nir_if
*if_stmt
= nir_cf_node_as_if(cf_node
);
1016 foreach_list_typed(nir_cf_node
, nested_node
, node
, &if_stmt
->then_list
)
1017 process_loops(nested_node
, indirect_mask
);
1018 foreach_list_typed(nir_cf_node
, nested_node
, node
, &if_stmt
->else_list
)
1019 process_loops(nested_node
, indirect_mask
);
1022 case nir_cf_node_loop
: {
1023 nir_loop
*loop
= nir_cf_node_as_loop(cf_node
);
1024 foreach_list_typed(nir_cf_node
, nested_node
, node
, &loop
->body
)
1025 process_loops(nested_node
, indirect_mask
);
1029 unreachable("unknown cf node type");
1032 nir_loop
*loop
= nir_cf_node_as_loop(cf_node
);
1033 nir_function_impl
*impl
= nir_cf_node_get_function(cf_node
);
1034 void *mem_ctx
= ralloc_context(NULL
);
1036 loop_info_state
*state
= initialize_loop_info_state(loop
, mem_ctx
, impl
);
1037 state
->indirect_mask
= indirect_mask
;
1039 get_loop_info(state
, impl
);
1041 ralloc_free(mem_ctx
);
1045 nir_loop_analyze_impl(nir_function_impl
*impl
,
1046 nir_variable_mode indirect_mask
)
1048 nir_index_ssa_defs(impl
);
1049 foreach_list_typed(nir_cf_node
, node
, node
, &impl
->body
)
1050 process_loops(node
, indirect_mask
);