glsl/loops: Move some analysis from loop_controls to loop_analysis.
[mesa.git] / src / glsl / loop_analysis.cpp
1 /*
2 * Copyright © 2010 Intel Corporation
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
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 #include "glsl_types.h"
25 #include "loop_analysis.h"
26 #include "ir_hierarchical_visitor.h"
27
28 static bool is_loop_terminator(ir_if *ir);
29
30 static bool all_expression_operands_are_loop_constant(ir_rvalue *,
31 hash_table *);
32
33 static ir_rvalue *get_basic_induction_increment(ir_assignment *, hash_table *);
34
35
36 /**
37 * Record the fact that the given loop variable was referenced inside the loop.
38 *
39 * \arg in_assignee is true if the reference was on the LHS of an assignment.
40 *
41 * \arg in_conditional_code_or_nested_loop is true if the reference occurred
42 * inside an if statement or a nested loop.
43 *
44 * \arg current_assignment is the ir_assignment node that the loop variable is
45 * on the LHS of, if any (ignored if \c in_assignee is false).
46 */
47 void
48 loop_variable::record_reference(bool in_assignee,
49 bool in_conditional_code_or_nested_loop,
50 ir_assignment *current_assignment)
51 {
52 if (in_assignee) {
53 assert(current_assignment != NULL);
54
55 if (in_conditional_code_or_nested_loop ||
56 current_assignment->condition != NULL) {
57 this->conditional_or_nested_assignment = true;
58 }
59
60 if (this->first_assignment == NULL) {
61 assert(this->num_assignments == 0);
62
63 this->first_assignment = current_assignment;
64 }
65
66 this->num_assignments++;
67 } else if (this->first_assignment == current_assignment) {
68 /* This catches the case where the variable is used in the RHS of an
69 * assignment where it is also in the LHS.
70 */
71 this->read_before_write = true;
72 }
73 }
74
75
76 loop_state::loop_state()
77 {
78 this->ht = hash_table_ctor(0, hash_table_pointer_hash,
79 hash_table_pointer_compare);
80 this->mem_ctx = ralloc_context(NULL);
81 this->loop_found = false;
82 }
83
84
85 loop_state::~loop_state()
86 {
87 hash_table_dtor(this->ht);
88 ralloc_free(this->mem_ctx);
89 }
90
91
92 loop_variable_state *
93 loop_state::insert(ir_loop *ir)
94 {
95 loop_variable_state *ls = new(this->mem_ctx) loop_variable_state;
96
97 hash_table_insert(this->ht, ls, ir);
98 this->loop_found = true;
99
100 return ls;
101 }
102
103
104 loop_variable_state *
105 loop_state::get(const ir_loop *ir)
106 {
107 return (loop_variable_state *) hash_table_find(this->ht, ir);
108 }
109
110
111 loop_variable *
112 loop_variable_state::get(const ir_variable *ir)
113 {
114 return (loop_variable *) hash_table_find(this->var_hash, ir);
115 }
116
117
118 loop_variable *
119 loop_variable_state::insert(ir_variable *var)
120 {
121 void *mem_ctx = ralloc_parent(this);
122 loop_variable *lv = rzalloc(mem_ctx, loop_variable);
123
124 lv->var = var;
125
126 hash_table_insert(this->var_hash, lv, lv->var);
127 this->variables.push_tail(lv);
128
129 return lv;
130 }
131
132
133 loop_terminator *
134 loop_variable_state::insert(ir_if *if_stmt)
135 {
136 void *mem_ctx = ralloc_parent(this);
137 loop_terminator *t = new(mem_ctx) loop_terminator();
138
139 t->ir = if_stmt;
140 this->terminators.push_tail(t);
141
142 return t;
143 }
144
145
146 /**
147 * If the given variable already is recorded in the state for this loop,
148 * return the corresponding loop_variable object that records information
149 * about it.
150 *
151 * Otherwise, create a new loop_variable object to record information about
152 * the variable, and set its \c read_before_write field appropriately based on
153 * \c in_assignee.
154 *
155 * \arg in_assignee is true if this variable was encountered on the LHS of an
156 * assignment.
157 */
158 loop_variable *
159 loop_variable_state::get_or_insert(ir_variable *var, bool in_assignee)
160 {
161 loop_variable *lv = this->get(var);
162
163 if (lv == NULL) {
164 lv = this->insert(var);
165 lv->read_before_write = !in_assignee;
166 }
167
168 return lv;
169 }
170
171
172 namespace {
173
174 class loop_analysis : public ir_hierarchical_visitor {
175 public:
176 loop_analysis(loop_state *loops);
177
178 virtual ir_visitor_status visit(ir_loop_jump *);
179 virtual ir_visitor_status visit(ir_dereference_variable *);
180
181 virtual ir_visitor_status visit_enter(ir_call *);
182
183 virtual ir_visitor_status visit_enter(ir_loop *);
184 virtual ir_visitor_status visit_leave(ir_loop *);
185 virtual ir_visitor_status visit_enter(ir_assignment *);
186 virtual ir_visitor_status visit_leave(ir_assignment *);
187 virtual ir_visitor_status visit_enter(ir_if *);
188 virtual ir_visitor_status visit_leave(ir_if *);
189
190 loop_state *loops;
191
192 int if_statement_depth;
193
194 ir_assignment *current_assignment;
195
196 exec_list state;
197 };
198
199 } /* anonymous namespace */
200
201 loop_analysis::loop_analysis(loop_state *loops)
202 : loops(loops), if_statement_depth(0), current_assignment(NULL)
203 {
204 /* empty */
205 }
206
207
208 ir_visitor_status
209 loop_analysis::visit(ir_loop_jump *ir)
210 {
211 (void) ir;
212
213 assert(!this->state.is_empty());
214
215 loop_variable_state *const ls =
216 (loop_variable_state *) this->state.get_head();
217
218 ls->num_loop_jumps++;
219
220 return visit_continue;
221 }
222
223
224 ir_visitor_status
225 loop_analysis::visit_enter(ir_call *ir)
226 {
227 /* Mark every loop that we're currently analyzing as containing an ir_call
228 * (even those at outer nesting levels).
229 */
230 foreach_list(node, &this->state) {
231 loop_variable_state *const ls = (loop_variable_state *) node;
232 ls->contains_calls = true;
233 }
234
235 return visit_continue_with_parent;
236 }
237
238
239 ir_visitor_status
240 loop_analysis::visit(ir_dereference_variable *ir)
241 {
242 /* If we're not somewhere inside a loop, there's nothing to do.
243 */
244 if (this->state.is_empty())
245 return visit_continue;
246
247 bool nested = false;
248
249 foreach_list(node, &this->state) {
250 loop_variable_state *const ls = (loop_variable_state *) node;
251
252 ir_variable *var = ir->variable_referenced();
253 loop_variable *lv = ls->get_or_insert(var, this->in_assignee);
254
255 lv->record_reference(this->in_assignee,
256 nested || this->if_statement_depth > 0,
257 this->current_assignment);
258 nested = true;
259 }
260
261 return visit_continue;
262 }
263
264 ir_visitor_status
265 loop_analysis::visit_enter(ir_loop *ir)
266 {
267 loop_variable_state *ls = this->loops->insert(ir);
268 this->state.push_head(ls);
269
270 return visit_continue;
271 }
272
273 ir_visitor_status
274 loop_analysis::visit_leave(ir_loop *ir)
275 {
276 loop_variable_state *const ls =
277 (loop_variable_state *) this->state.pop_head();
278
279 /* Function calls may contain side effects. These could alter any of our
280 * variables in ways that cannot be known, and may even terminate shader
281 * execution (say, calling discard in the fragment shader). So we can't
282 * rely on any of our analysis about assignments to variables.
283 *
284 * We could perform some conservative analysis (prove there's no statically
285 * possible assignment, etc.) but it isn't worth it for now; function
286 * inlining will allow us to unroll loops anyway.
287 */
288 if (ls->contains_calls)
289 return visit_continue;
290
291 foreach_list(node, &ir->body_instructions) {
292 /* Skip over declarations at the start of a loop.
293 */
294 if (((ir_instruction *) node)->as_variable())
295 continue;
296
297 ir_if *if_stmt = ((ir_instruction *) node)->as_if();
298
299 if ((if_stmt != NULL) && is_loop_terminator(if_stmt))
300 ls->insert(if_stmt);
301 else
302 break;
303 }
304
305
306 foreach_list_safe(node, &ls->variables) {
307 loop_variable *lv = (loop_variable *) node;
308
309 /* Move variables that are already marked as being loop constant to
310 * a separate list. These trivially don't need to be tested.
311 */
312 if (lv->is_loop_constant()) {
313 lv->remove();
314 ls->constants.push_tail(lv);
315 }
316 }
317
318 /* Each variable assigned in the loop that isn't already marked as being loop
319 * constant might still be loop constant. The requirements at this point
320 * are:
321 *
322 * - Variable is written before it is read.
323 *
324 * - Only one assignment to the variable.
325 *
326 * - All operands on the RHS of the assignment are also loop constants.
327 *
328 * The last requirement is the reason for the progress loop. A variable
329 * marked as a loop constant on one pass may allow other variables to be
330 * marked as loop constant on following passes.
331 */
332 bool progress;
333 do {
334 progress = false;
335
336 foreach_list_safe(node, &ls->variables) {
337 loop_variable *lv = (loop_variable *) node;
338
339 if (lv->conditional_or_nested_assignment || (lv->num_assignments > 1))
340 continue;
341
342 /* Process the RHS of the assignment. If all of the variables
343 * accessed there are loop constants, then add this
344 */
345 ir_rvalue *const rhs = lv->first_assignment->rhs;
346 if (all_expression_operands_are_loop_constant(rhs, ls->var_hash)) {
347 lv->rhs_clean = true;
348
349 if (lv->is_loop_constant()) {
350 progress = true;
351
352 lv->remove();
353 ls->constants.push_tail(lv);
354 }
355 }
356 }
357 } while (progress);
358
359 /* The remaining variables that are not loop invariant might be loop
360 * induction variables.
361 */
362 foreach_list_safe(node, &ls->variables) {
363 loop_variable *lv = (loop_variable *) node;
364
365 /* If there is more than one assignment to a variable, it cannot be a
366 * loop induction variable. This isn't strictly true, but this is a
367 * very simple induction variable detector, and it can't handle more
368 * complex cases.
369 */
370 if (lv->num_assignments > 1)
371 continue;
372
373 /* All of the variables with zero assignments in the loop are loop
374 * invariant, and they should have already been filtered out.
375 */
376 assert(lv->num_assignments == 1);
377 assert(lv->first_assignment != NULL);
378
379 /* The assignment to the variable in the loop must be unconditional and
380 * not inside a nested loop.
381 */
382 if (lv->conditional_or_nested_assignment)
383 continue;
384
385 /* Basic loop induction variables have a single assignment in the loop
386 * that has the form 'VAR = VAR + i' or 'VAR = VAR - i' where i is a
387 * loop invariant.
388 */
389 ir_rvalue *const inc =
390 get_basic_induction_increment(lv->first_assignment, ls->var_hash);
391 if (inc != NULL) {
392 lv->increment = inc;
393
394 lv->remove();
395 ls->induction_variables.push_tail(lv);
396 }
397 }
398
399 /* Search the loop terminating conditions for those of the form 'i < c'
400 * where i is a loop induction variable, c is a constant, and < is any
401 * relative operator. From each of these we can infer an iteration count.
402 * Also figure out which terminator (if any) produces the smallest
403 * iteration count--this is the limiting terminator.
404 */
405 foreach_list(node, &ls->terminators) {
406 loop_terminator *t = (loop_terminator *) node;
407 ir_if *if_stmt = t->ir;
408
409 /* If-statements can be either 'if (expr)' or 'if (deref)'. We only care
410 * about the former here.
411 */
412 ir_expression *cond = if_stmt->condition->as_expression();
413 if (cond == NULL)
414 continue;
415
416 switch (cond->operation) {
417 case ir_binop_less:
418 case ir_binop_greater:
419 case ir_binop_lequal:
420 case ir_binop_gequal: {
421 /* The expressions that we care about will either be of the form
422 * 'counter < limit' or 'limit < counter'. Figure out which is
423 * which.
424 */
425 ir_rvalue *counter = cond->operands[0]->as_dereference_variable();
426 ir_constant *limit = cond->operands[1]->as_constant();
427 enum ir_expression_operation cmp = cond->operation;
428
429 if (limit == NULL) {
430 counter = cond->operands[1]->as_dereference_variable();
431 limit = cond->operands[0]->as_constant();
432
433 switch (cmp) {
434 case ir_binop_less: cmp = ir_binop_greater; break;
435 case ir_binop_greater: cmp = ir_binop_less; break;
436 case ir_binop_lequal: cmp = ir_binop_gequal; break;
437 case ir_binop_gequal: cmp = ir_binop_lequal; break;
438 default: assert(!"Should not get here.");
439 }
440 }
441
442 if ((counter == NULL) || (limit == NULL))
443 break;
444
445 ir_variable *var = counter->variable_referenced();
446
447 ir_rvalue *init = find_initial_value(ir, var);
448
449 loop_variable *lv = ls->get(var);
450 if (lv != NULL && lv->is_induction_var()) {
451 t->iterations = calculate_iterations(init, limit, lv->increment,
452 cmp);
453
454 if (t->iterations >= 0 &&
455 (ls->limiting_terminator == NULL ||
456 t->iterations < ls->limiting_terminator->iterations)) {
457 ls->limiting_terminator = t;
458 }
459 }
460 break;
461 }
462
463 default:
464 break;
465 }
466 }
467
468 return visit_continue;
469 }
470
471 ir_visitor_status
472 loop_analysis::visit_enter(ir_if *ir)
473 {
474 (void) ir;
475
476 if (!this->state.is_empty())
477 this->if_statement_depth++;
478
479 return visit_continue;
480 }
481
482 ir_visitor_status
483 loop_analysis::visit_leave(ir_if *ir)
484 {
485 (void) ir;
486
487 if (!this->state.is_empty())
488 this->if_statement_depth--;
489
490 return visit_continue;
491 }
492
493 ir_visitor_status
494 loop_analysis::visit_enter(ir_assignment *ir)
495 {
496 /* If we're not somewhere inside a loop, there's nothing to do.
497 */
498 if (this->state.is_empty())
499 return visit_continue_with_parent;
500
501 this->current_assignment = ir;
502
503 return visit_continue;
504 }
505
506 ir_visitor_status
507 loop_analysis::visit_leave(ir_assignment *ir)
508 {
509 /* Since the visit_enter exits with visit_continue_with_parent for this
510 * case, the loop state stack should never be empty here.
511 */
512 assert(!this->state.is_empty());
513
514 assert(this->current_assignment == ir);
515 this->current_assignment = NULL;
516
517 return visit_continue;
518 }
519
520
521 class examine_rhs : public ir_hierarchical_visitor {
522 public:
523 examine_rhs(hash_table *loop_variables)
524 {
525 this->only_uses_loop_constants = true;
526 this->loop_variables = loop_variables;
527 }
528
529 virtual ir_visitor_status visit(ir_dereference_variable *ir)
530 {
531 loop_variable *lv =
532 (loop_variable *) hash_table_find(this->loop_variables, ir->var);
533
534 assert(lv != NULL);
535
536 if (lv->is_loop_constant()) {
537 return visit_continue;
538 } else {
539 this->only_uses_loop_constants = false;
540 return visit_stop;
541 }
542 }
543
544 hash_table *loop_variables;
545 bool only_uses_loop_constants;
546 };
547
548
549 bool
550 all_expression_operands_are_loop_constant(ir_rvalue *ir, hash_table *variables)
551 {
552 examine_rhs v(variables);
553
554 ir->accept(&v);
555
556 return v.only_uses_loop_constants;
557 }
558
559
560 ir_rvalue *
561 get_basic_induction_increment(ir_assignment *ir, hash_table *var_hash)
562 {
563 /* The RHS must be a binary expression.
564 */
565 ir_expression *const rhs = ir->rhs->as_expression();
566 if ((rhs == NULL)
567 || ((rhs->operation != ir_binop_add)
568 && (rhs->operation != ir_binop_sub)))
569 return NULL;
570
571 /* One of the of operands of the expression must be the variable assigned.
572 * If the operation is subtraction, the variable in question must be the
573 * "left" operand.
574 */
575 ir_variable *const var = ir->lhs->variable_referenced();
576
577 ir_variable *const op0 = rhs->operands[0]->variable_referenced();
578 ir_variable *const op1 = rhs->operands[1]->variable_referenced();
579
580 if (((op0 != var) && (op1 != var))
581 || ((op1 == var) && (rhs->operation == ir_binop_sub)))
582 return NULL;
583
584 ir_rvalue *inc = (op0 == var) ? rhs->operands[1] : rhs->operands[0];
585
586 if (inc->as_constant() == NULL) {
587 ir_variable *const inc_var = inc->variable_referenced();
588 if (inc_var != NULL) {
589 loop_variable *lv =
590 (loop_variable *) hash_table_find(var_hash, inc_var);
591
592 if (!lv->is_loop_constant())
593 inc = NULL;
594 } else
595 inc = NULL;
596 }
597
598 if ((inc != NULL) && (rhs->operation == ir_binop_sub)) {
599 void *mem_ctx = ralloc_parent(ir);
600
601 inc = new(mem_ctx) ir_expression(ir_unop_neg,
602 inc->type,
603 inc->clone(mem_ctx, NULL),
604 NULL);
605 }
606
607 return inc;
608 }
609
610
611 /**
612 * Detect whether an if-statement is a loop terminating condition
613 *
614 * Detects if-statements of the form
615 *
616 * (if (expression bool ...) (break))
617 */
618 bool
619 is_loop_terminator(ir_if *ir)
620 {
621 if (!ir->else_instructions.is_empty())
622 return false;
623
624 ir_instruction *const inst =
625 (ir_instruction *) ir->then_instructions.get_head();
626 if (inst == NULL)
627 return false;
628
629 if (inst->ir_type != ir_type_loop_jump)
630 return false;
631
632 ir_loop_jump *const jump = (ir_loop_jump *) inst;
633 if (jump->mode != ir_loop_jump::jump_break)
634 return false;
635
636 return true;
637 }
638
639
640 loop_state *
641 analyze_loop_variables(exec_list *instructions)
642 {
643 loop_state *loops = new loop_state;
644 loop_analysis v(loops);
645
646 v.run(instructions);
647 return v.loops;
648 }