nir: reword code comment
[mesa.git] / src / compiler / nir / nir_loop_analyze.c
1 /*
2 * Copyright © 2015 Thomas Helland
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 */
23
24 #include "nir.h"
25 #include "nir_constant_expressions.h"
26 #include "nir_loop_analyze.h"
27
28 typedef enum {
29 undefined,
30 invariant,
31 not_invariant,
32 basic_induction
33 } nir_loop_variable_type;
34
35 struct nir_basic_induction_var;
36
37 typedef struct {
38 /* A link for the work list */
39 struct list_head process_link;
40
41 bool in_loop;
42
43 /* The ssa_def associated with this info */
44 nir_ssa_def *def;
45
46 /* The type of this ssa_def */
47 nir_loop_variable_type type;
48
49 /* If this is of type basic_induction */
50 struct nir_basic_induction_var *ind;
51
52 /* True if variable is in an if branch */
53 bool in_if_branch;
54
55 /* True if variable is in a nested loop */
56 bool in_nested_loop;
57
58 } nir_loop_variable;
59
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;
66
67 typedef struct {
68 /* The loop we store information for */
69 nir_loop *loop;
70
71 /* Loop_variable for all ssa_defs in function */
72 nir_loop_variable *loop_vars;
73
74 /* A list of the loop_vars to analyze */
75 struct list_head process_list;
76
77 nir_variable_mode indirect_mask;
78
79 } loop_info_state;
80
81 static nir_loop_variable *
82 get_loop_var(nir_ssa_def *value, loop_info_state *state)
83 {
84 return &(state->loop_vars[value->index]);
85 }
86
87 typedef struct {
88 loop_info_state *state;
89 bool in_if_branch;
90 bool in_nested_loop;
91 } init_loop_state;
92
93 static bool
94 init_loop_def(nir_ssa_def *def, void *void_init_loop_state)
95 {
96 init_loop_state *loop_init_state = void_init_loop_state;
97 nir_loop_variable *var = get_loop_var(def, loop_init_state->state);
98
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;
103 } else {
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.
108 */
109 list_addtail(&var->process_link, &loop_init_state->state->process_list);
110 }
111
112 var->in_loop = true;
113
114 return true;
115 }
116
117 static bool
118 init_loop_block(nir_block *block, loop_info_state *state,
119 bool in_if_branch, bool in_nested_loop)
120 {
121 init_loop_state init_state = {.in_if_branch = in_if_branch,
122 .in_nested_loop = in_nested_loop,
123 .state = state };
124
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++;
130 }
131
132 nir_foreach_ssa_def(instr, init_loop_def, &init_state);
133 }
134
135 return true;
136 }
137
138 static inline bool
139 is_var_alu(nir_loop_variable *var)
140 {
141 return var->def->parent_instr->type == nir_instr_type_alu;
142 }
143
144 static inline bool
145 is_var_constant(nir_loop_variable *var)
146 {
147 return var->def->parent_instr->type == nir_instr_type_load_const;
148 }
149
150 static inline bool
151 is_var_phi(nir_loop_variable *var)
152 {
153 return var->def->parent_instr->type == nir_instr_type_phi;
154 }
155
156 static inline bool
157 mark_invariant(nir_ssa_def *def, loop_info_state *state)
158 {
159 nir_loop_variable *var = get_loop_var(def, state);
160
161 if (var->type == invariant)
162 return true;
163
164 if (!var->in_loop) {
165 var->type = invariant;
166 return true;
167 }
168
169 if (var->type == not_invariant)
170 return false;
171
172 if (is_var_alu(var)) {
173 nir_alu_instr *alu = nir_instr_as_alu(def->parent_instr);
174
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;
178 return false;
179 }
180 }
181 var->type = invariant;
182 return true;
183 }
184
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.
189 */
190 var->type = not_invariant;
191 return false;
192 }
193
194 static void
195 compute_invariance_information(loop_info_state *state)
196 {
197 /* An expression is invariant in a loop L if:
198 * (base cases)
199 * – it’s a constant
200 * – it’s a variable use, all of whose single defs are outside of L
201 * (inductive cases)
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
205 */
206 list_for_each_entry_safe(nir_loop_variable, var, &state->process_list,
207 process_link) {
208 assert(!var->in_if_branch && !var->in_nested_loop);
209
210 if (mark_invariant(var->def, state))
211 list_del(&var->process_link);
212 }
213 }
214
215 static bool
216 compute_induction_information(loop_info_state *state)
217 {
218 bool found_induction_var = false;
219 list_for_each_entry_safe(nir_loop_variable, var, &state->process_list,
220 process_link) {
221
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().
225 */
226 assert(!var->in_if_branch && !var->in_nested_loop &&
227 var->type != invariant);
228
229 /* We are only interested in checking phis for the basic induction
230 * variable case as its simple to detect. All basic induction variables
231 * have a phi node
232 */
233 if (!is_var_phi(var))
234 continue;
235
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);
238
239 nir_foreach_phi_src(src, phi) {
240 nir_loop_variable *src_var = get_loop_var(src->src.ssa, state);
241
242 /* If one of the sources is in an if branch or nested loop then don't
243 * attempt to go any further.
244 */
245 if (src_var->in_if_branch || src_var->in_nested_loop)
246 break;
247
248 if (!src_var->in_loop) {
249 biv->def_outside_loop = src_var;
250 } else if (is_var_alu(src_var)) {
251 nir_alu_instr *alu = nir_instr_as_alu(src_var->def->parent_instr);
252
253 if (nir_op_infos[alu->op].num_inputs == 2) {
254 biv->alu_def = src_var;
255 biv->alu_op = alu->op;
256
257 for (unsigned i = 0; i < 2; i++) {
258 /* Is one of the operands const, and the other the phi */
259 if (alu->src[i].src.ssa->parent_instr->type == nir_instr_type_load_const &&
260 alu->src[1-i].src.ssa == &phi->dest.ssa)
261 biv->invariant = get_loop_var(alu->src[i].src.ssa, state);
262 }
263 }
264 }
265 }
266
267 if (biv->alu_def && biv->def_outside_loop && biv->invariant &&
268 is_var_constant(biv->def_outside_loop)) {
269 assert(is_var_constant(biv->invariant));
270 biv->alu_def->type = basic_induction;
271 biv->alu_def->ind = biv;
272 var->type = basic_induction;
273 var->ind = biv;
274
275 found_induction_var = true;
276 } else {
277 ralloc_free(biv);
278 }
279 }
280 return found_induction_var;
281 }
282
283 static bool
284 initialize_ssa_def(nir_ssa_def *def, void *void_state)
285 {
286 loop_info_state *state = void_state;
287 nir_loop_variable *var = get_loop_var(def, state);
288
289 var->in_loop = false;
290 var->def = def;
291
292 if (def->parent_instr->type == nir_instr_type_load_const) {
293 var->type = invariant;
294 } else {
295 var->type = undefined;
296 }
297
298 return true;
299 }
300
301 static bool
302 find_loop_terminators(loop_info_state *state)
303 {
304 bool success = false;
305 foreach_list_typed_safe(nir_cf_node, node, node, &state->loop->body) {
306 if (node->type == nir_cf_node_if) {
307 nir_if *nif = nir_cf_node_as_if(node);
308
309 nir_block *break_blk = NULL;
310 nir_block *continue_from_blk = NULL;
311 bool continue_from_then = true;
312
313 nir_block *last_then = nir_if_last_then_block(nif);
314 nir_block *last_else = nir_if_last_else_block(nif);
315 if (nir_block_ends_in_break(last_then)) {
316 break_blk = last_then;
317 continue_from_blk = last_else;
318 continue_from_then = false;
319 } else if (nir_block_ends_in_break(last_else)) {
320 break_blk = last_else;
321 continue_from_blk = last_then;
322 }
323
324 /* If there is a break then we should find a terminator. If we can
325 * not find a loop terminator, but there is a break-statement then
326 * we should return false so that we do not try to find trip-count
327 */
328 if (!nir_is_trivial_loop_if(nif, break_blk)) {
329 state->loop->info->complex_loop = true;
330 return false;
331 }
332
333 /* Continue if the if contained no jumps at all */
334 if (!break_blk)
335 continue;
336
337 if (nif->condition.ssa->parent_instr->type == nir_instr_type_phi) {
338 state->loop->info->complex_loop = true;
339 return false;
340 }
341
342 nir_loop_terminator *terminator =
343 rzalloc(state->loop->info, nir_loop_terminator);
344
345 list_addtail(&terminator->loop_terminator_link,
346 &state->loop->info->loop_terminator_list);
347
348 terminator->nif = nif;
349 terminator->break_block = break_blk;
350 terminator->continue_from_block = continue_from_blk;
351 terminator->continue_from_then = continue_from_then;
352 terminator->conditional_instr = nif->condition.ssa->parent_instr;
353
354 success = true;
355 }
356 }
357
358 return success;
359 }
360
361 /* This function looks for an array access within a loop that uses an
362 * induction variable for the array index. If found it returns the size of the
363 * array, otherwise 0 is returned. If we find an induction var we pass it back
364 * to the caller via array_index_out.
365 */
366 static unsigned
367 find_array_access_via_induction(loop_info_state *state,
368 nir_deref_instr *deref,
369 nir_loop_variable **array_index_out)
370 {
371 for (nir_deref_instr *d = deref; d; d = nir_deref_instr_parent(d)) {
372 if (d->deref_type != nir_deref_type_array)
373 continue;
374
375 assert(d->arr.index.is_ssa);
376 nir_loop_variable *array_index = get_loop_var(d->arr.index.ssa, state);
377
378 if (array_index->type != basic_induction)
379 continue;
380
381 if (array_index_out)
382 *array_index_out = array_index;
383
384 nir_deref_instr *parent = nir_deref_instr_parent(d);
385 assert(glsl_type_is_array_or_matrix(parent->type));
386
387 return glsl_get_length(parent->type);
388 }
389
390 return 0;
391 }
392
393 static int32_t
394 get_iteration(nir_op cond_op, nir_const_value *initial, nir_const_value *step,
395 nir_const_value *limit)
396 {
397 int32_t iter;
398
399 switch (cond_op) {
400 case nir_op_ige:
401 case nir_op_ilt:
402 case nir_op_ieq:
403 case nir_op_ine: {
404 int32_t initial_val = initial->i32[0];
405 int32_t span = limit->i32[0] - initial_val;
406 iter = span / step->i32[0];
407 break;
408 }
409 case nir_op_uge:
410 case nir_op_ult: {
411 uint32_t initial_val = initial->u32[0];
412 uint32_t span = limit->u32[0] - initial_val;
413 iter = span / step->u32[0];
414 break;
415 }
416 case nir_op_fge:
417 case nir_op_flt:
418 case nir_op_feq:
419 case nir_op_fne: {
420 float initial_val = initial->f32[0];
421 float span = limit->f32[0] - initial_val;
422 iter = span / step->f32[0];
423 break;
424 }
425 default:
426 return -1;
427 }
428
429 return iter;
430 }
431
432 static bool
433 test_iterations(int32_t iter_int, nir_const_value *step,
434 nir_const_value *limit, nir_op cond_op, unsigned bit_size,
435 nir_alu_type induction_base_type,
436 nir_const_value *initial, bool limit_rhs, bool invert_cond)
437 {
438 assert(nir_op_infos[cond_op].num_inputs == 2);
439
440 nir_const_value iter_src = { {0, } };
441 nir_op mul_op;
442 nir_op add_op;
443 switch (induction_base_type) {
444 case nir_type_float:
445 iter_src.f32[0] = (float) iter_int;
446 mul_op = nir_op_fmul;
447 add_op = nir_op_fadd;
448 break;
449 case nir_type_int:
450 case nir_type_uint:
451 iter_src.i32[0] = iter_int;
452 mul_op = nir_op_imul;
453 add_op = nir_op_iadd;
454 break;
455 default:
456 unreachable("Unhandled induction variable base type!");
457 }
458
459 /* Multiple the iteration count we are testing by the number of times we
460 * step the induction variable each iteration.
461 */
462 nir_const_value mul_src[2] = { iter_src, *step };
463 nir_const_value mul_result =
464 nir_eval_const_opcode(mul_op, 1, bit_size, mul_src);
465
466 /* Add the initial value to the accumulated induction variable total */
467 nir_const_value add_src[2] = { mul_result, *initial };
468 nir_const_value add_result =
469 nir_eval_const_opcode(add_op, 1, bit_size, add_src);
470
471 nir_const_value src[2] = { { {0, } }, { {0, } } };
472 src[limit_rhs ? 0 : 1] = add_result;
473 src[limit_rhs ? 1 : 0] = *limit;
474
475 /* Evaluate the loop exit condition */
476 nir_const_value result = nir_eval_const_opcode(cond_op, 1, bit_size, src);
477
478 return invert_cond ? (result.u32[0] == 0) : (result.u32[0] != 0);
479 }
480
481 static int
482 calculate_iterations(nir_const_value *initial, nir_const_value *step,
483 nir_const_value *limit, nir_loop_variable *alu_def,
484 nir_alu_instr *cond_alu, bool limit_rhs, bool invert_cond)
485 {
486 assert(initial != NULL && step != NULL && limit != NULL);
487
488 nir_alu_instr *alu = nir_instr_as_alu(alu_def->def->parent_instr);
489
490 /* nir_op_isub should have been lowered away by this point */
491 assert(alu->op != nir_op_isub);
492
493 /* Make sure the alu type for our induction variable is compatible with the
494 * conditional alus input type. If its not something has gone really wrong.
495 */
496 nir_alu_type induction_base_type =
497 nir_alu_type_get_base_type(nir_op_infos[alu->op].output_type);
498 if (induction_base_type == nir_type_int || induction_base_type == nir_type_uint) {
499 assert(nir_alu_type_get_base_type(nir_op_infos[cond_alu->op].input_types[1]) == nir_type_int ||
500 nir_alu_type_get_base_type(nir_op_infos[cond_alu->op].input_types[1]) == nir_type_uint);
501 } else {
502 assert(nir_alu_type_get_base_type(nir_op_infos[cond_alu->op].input_types[0]) ==
503 induction_base_type);
504 }
505
506 /* Check for nsupported alu operations */
507 if (alu->op != nir_op_iadd && alu->op != nir_op_fadd)
508 return -1;
509
510 /* do-while loops can increment the starting value before the condition is
511 * checked. e.g.
512 *
513 * do {
514 * ndx++;
515 * } while (ndx < 3);
516 *
517 * Here we check if the induction variable is used directly by the loop
518 * condition and if so we assume we need to step the initial value.
519 */
520 unsigned trip_offset = 0;
521 if (cond_alu->src[0].src.ssa == alu_def->def ||
522 cond_alu->src[1].src.ssa == alu_def->def) {
523 trip_offset = 1;
524 }
525
526 int iter_int = get_iteration(cond_alu->op, initial, step, limit);
527
528 /* If iter_int is negative the loop is ill-formed or is the conditional is
529 * unsigned with a huge iteration count so don't bother going any further.
530 */
531 if (iter_int < 0)
532 return -1;
533
534 /* An explanation from the GLSL unrolling pass:
535 *
536 * Make sure that the calculated number of iterations satisfies the exit
537 * condition. This is needed to catch off-by-one errors and some types of
538 * ill-formed loops. For example, we need to detect that the following
539 * loop does not have a maximum iteration count.
540 *
541 * for (float x = 0.0; x != 0.9; x += 0.2);
542 */
543 assert(nir_src_bit_size(alu->src[0].src) ==
544 nir_src_bit_size(alu->src[1].src));
545 unsigned bit_size = nir_src_bit_size(alu->src[0].src);
546 for (int bias = -1; bias <= 1; bias++) {
547 const int iter_bias = iter_int + bias;
548
549 if (test_iterations(iter_bias, step, limit, cond_alu->op, bit_size,
550 induction_base_type, initial,
551 limit_rhs, invert_cond)) {
552 return iter_bias > 0 ? iter_bias - trip_offset : iter_bias;
553 }
554 }
555
556 return -1;
557 }
558
559 /* Run through each of the terminators of the loop and try to infer a possible
560 * trip-count. We need to check them all, and set the lowest trip-count as the
561 * trip-count of our loop. If one of the terminators has an undecidable
562 * trip-count we can not safely assume anything about the duration of the
563 * loop.
564 */
565 static void
566 find_trip_count(loop_info_state *state)
567 {
568 bool trip_count_known = true;
569 nir_loop_terminator *limiting_terminator = NULL;
570 int max_trip_count = -1;
571
572 list_for_each_entry(nir_loop_terminator, terminator,
573 &state->loop->info->loop_terminator_list,
574 loop_terminator_link) {
575
576 if (terminator->conditional_instr->type != nir_instr_type_alu) {
577 /* If we get here the loop is dead and will get cleaned up by the
578 * nir_opt_dead_cf pass.
579 */
580 trip_count_known = false;
581 continue;
582 }
583
584 nir_alu_instr *alu = nir_instr_as_alu(terminator->conditional_instr);
585 nir_loop_variable *basic_ind = NULL;
586 nir_loop_variable *limit = NULL;
587 bool limit_rhs = true;
588
589 switch (alu->op) {
590 case nir_op_fge: case nir_op_ige: case nir_op_uge:
591 case nir_op_flt: case nir_op_ilt: case nir_op_ult:
592 case nir_op_feq: case nir_op_ieq:
593 case nir_op_fne: case nir_op_ine:
594
595 /* We assume that the limit is the "right" operand */
596 basic_ind = get_loop_var(alu->src[0].src.ssa, state);
597 limit = get_loop_var(alu->src[1].src.ssa, state);
598
599 if (basic_ind->type != basic_induction) {
600 /* We had it the wrong way, flip things around */
601 basic_ind = get_loop_var(alu->src[1].src.ssa, state);
602 limit = get_loop_var(alu->src[0].src.ssa, state);
603 limit_rhs = false;
604 }
605
606 /* The comparison has to have a basic induction variable
607 * and a constant for us to be able to find trip counts
608 */
609 if (basic_ind->type != basic_induction || !is_var_constant(limit)) {
610 trip_count_known = false;
611 continue;
612 }
613
614 /* We have determined that we have the following constants:
615 * (With the typical int i = 0; i < x; i++; as an example)
616 * - Upper limit.
617 * - Starting value
618 * - Step / iteration size
619 * Thats all thats needed to calculate the trip-count
620 */
621
622 nir_const_value initial_val =
623 nir_instr_as_load_const(basic_ind->ind->def_outside_loop->
624 def->parent_instr)->value;
625
626 nir_const_value step_val =
627 nir_instr_as_load_const(basic_ind->ind->invariant->def->
628 parent_instr)->value;
629
630 nir_const_value limit_val =
631 nir_instr_as_load_const(limit->def->parent_instr)->value;
632
633 int iterations = calculate_iterations(&initial_val, &step_val,
634 &limit_val,
635 basic_ind->ind->alu_def, alu,
636 limit_rhs,
637 terminator->continue_from_then);
638
639 /* Where we not able to calculate the iteration count */
640 if (iterations == -1) {
641 trip_count_known = false;
642 continue;
643 }
644
645 /* If this is the first run or we have found a smaller amount of
646 * iterations than previously (we have identified a more limiting
647 * terminator) set the trip count and limiting terminator.
648 */
649 if (max_trip_count == -1 || iterations < max_trip_count) {
650 max_trip_count = iterations;
651 limiting_terminator = terminator;
652 }
653 break;
654
655 default:
656 trip_count_known = false;
657 }
658 }
659
660 state->loop->info->exact_trip_count_known = trip_count_known;
661 if (max_trip_count > -1)
662 state->loop->info->max_trip_count = max_trip_count;
663 state->loop->info->limiting_terminator = limiting_terminator;
664 }
665
666 static bool
667 force_unroll_array_access(loop_info_state *state, nir_deref_instr *deref)
668 {
669 unsigned array_size = find_array_access_via_induction(state, deref, NULL);
670 if (array_size) {
671 if (array_size == state->loop->info->max_trip_count)
672 return true;
673
674 if (deref->mode & state->indirect_mask)
675 return true;
676 }
677
678 return false;
679 }
680
681 static bool
682 force_unroll_heuristics(loop_info_state *state, nir_block *block)
683 {
684 nir_foreach_instr(instr, block) {
685 if (instr->type != nir_instr_type_intrinsic)
686 continue;
687
688 nir_intrinsic_instr *intrin = nir_instr_as_intrinsic(instr);
689
690 /* Check for arrays variably-indexed by a loop induction variable.
691 * Unrolling the loop may convert that access into constant-indexing.
692 */
693 if (intrin->intrinsic == nir_intrinsic_load_deref ||
694 intrin->intrinsic == nir_intrinsic_store_deref ||
695 intrin->intrinsic == nir_intrinsic_copy_deref) {
696 if (force_unroll_array_access(state,
697 nir_src_as_deref(intrin->src[0])))
698 return true;
699
700 if (intrin->intrinsic == nir_intrinsic_copy_deref &&
701 force_unroll_array_access(state,
702 nir_src_as_deref(intrin->src[1])))
703 return true;
704 }
705 }
706
707 return false;
708 }
709
710 static void
711 get_loop_info(loop_info_state *state, nir_function_impl *impl)
712 {
713 /* Initialize all variables to "outside_loop". This also marks defs
714 * invariant and constant if they are nir_instr_type_load_consts
715 */
716 nir_foreach_block(block, impl) {
717 nir_foreach_instr(instr, block)
718 nir_foreach_ssa_def(instr, initialize_ssa_def, state);
719 }
720
721 /* Add all entries in the outermost part of the loop to the processing list
722 * Mark the entries in conditionals or in nested loops accordingly
723 */
724 foreach_list_typed_safe(nir_cf_node, node, node, &state->loop->body) {
725 switch (node->type) {
726
727 case nir_cf_node_block:
728 init_loop_block(nir_cf_node_as_block(node), state, false, false);
729 break;
730
731 case nir_cf_node_if:
732 nir_foreach_block_in_cf_node(block, node)
733 init_loop_block(block, state, true, false);
734 break;
735
736 case nir_cf_node_loop:
737 nir_foreach_block_in_cf_node(block, node) {
738 init_loop_block(block, state, false, true);
739 }
740 break;
741
742 case nir_cf_node_function:
743 break;
744 }
745 }
746
747 /* Try to find all simple terminators of the loop. If we can't find any,
748 * or we find possible terminators that have side effects then bail.
749 */
750 if (!find_loop_terminators(state)) {
751 list_for_each_entry_safe(nir_loop_terminator, terminator,
752 &state->loop->info->loop_terminator_list,
753 loop_terminator_link) {
754 list_del(&terminator->loop_terminator_link);
755 ralloc_free(terminator);
756 }
757 return;
758 }
759
760 /* Induction analysis needs invariance information so get that first */
761 compute_invariance_information(state);
762
763 /* We have invariance information so try to find induction variables */
764 if (!compute_induction_information(state))
765 return;
766
767 /* Run through each of the terminators and try to compute a trip-count */
768 find_trip_count(state);
769
770 nir_shader *ns = impl->function->shader;
771 nir_foreach_block_in_cf_node(block, &state->loop->cf_node) {
772 if (force_unroll_heuristics(state, block)) {
773 state->loop->info->force_unroll = true;
774 break;
775 }
776 }
777 }
778
779 static loop_info_state *
780 initialize_loop_info_state(nir_loop *loop, void *mem_ctx,
781 nir_function_impl *impl)
782 {
783 loop_info_state *state = rzalloc(mem_ctx, loop_info_state);
784 state->loop_vars = rzalloc_array(mem_ctx, nir_loop_variable,
785 impl->ssa_alloc);
786 state->loop = loop;
787
788 list_inithead(&state->process_list);
789
790 if (loop->info)
791 ralloc_free(loop->info);
792
793 loop->info = rzalloc(loop, nir_loop_info);
794
795 list_inithead(&loop->info->loop_terminator_list);
796
797 return state;
798 }
799
800 static void
801 process_loops(nir_cf_node *cf_node, nir_variable_mode indirect_mask)
802 {
803 switch (cf_node->type) {
804 case nir_cf_node_block:
805 return;
806 case nir_cf_node_if: {
807 nir_if *if_stmt = nir_cf_node_as_if(cf_node);
808 foreach_list_typed(nir_cf_node, nested_node, node, &if_stmt->then_list)
809 process_loops(nested_node, indirect_mask);
810 foreach_list_typed(nir_cf_node, nested_node, node, &if_stmt->else_list)
811 process_loops(nested_node, indirect_mask);
812 return;
813 }
814 case nir_cf_node_loop: {
815 nir_loop *loop = nir_cf_node_as_loop(cf_node);
816 foreach_list_typed(nir_cf_node, nested_node, node, &loop->body)
817 process_loops(nested_node, indirect_mask);
818 break;
819 }
820 default:
821 unreachable("unknown cf node type");
822 }
823
824 nir_loop *loop = nir_cf_node_as_loop(cf_node);
825 nir_function_impl *impl = nir_cf_node_get_function(cf_node);
826 void *mem_ctx = ralloc_context(NULL);
827
828 loop_info_state *state = initialize_loop_info_state(loop, mem_ctx, impl);
829 state->indirect_mask = indirect_mask;
830
831 get_loop_info(state, impl);
832
833 ralloc_free(mem_ctx);
834 }
835
836 void
837 nir_loop_analyze_impl(nir_function_impl *impl,
838 nir_variable_mode indirect_mask)
839 {
840 nir_index_ssa_defs(impl);
841 foreach_list_typed(nir_cf_node, node, node, &impl->body)
842 process_loops(node, indirect_mask);
843 }