replace _mesa_logbase2 with util_logbase2
[mesa.git] / src / intel / compiler / brw_cfg.cpp
1 /*
2 * Copyright © 2012 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 DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
28 #include "brw_cfg.h"
29 #include "brw_shader.h"
30
31 /** @file brw_cfg.cpp
32 *
33 * Walks the shader instructions generated and creates a set of basic
34 * blocks with successor/predecessor edges connecting them.
35 */
36
37 using namespace brw;
38
39 static bblock_t *
40 pop_stack(exec_list *list)
41 {
42 bblock_link *link = (bblock_link *)list->get_tail();
43 bblock_t *block = link->block;
44 link->link.remove();
45
46 return block;
47 }
48
49 static exec_node *
50 link(void *mem_ctx, bblock_t *block, enum bblock_link_kind kind)
51 {
52 bblock_link *l = new(mem_ctx) bblock_link(block, kind);
53 return &l->link;
54 }
55
56 void
57 push_stack(exec_list *list, void *mem_ctx, bblock_t *block)
58 {
59 /* The kind of the link is immaterial, but we need to provide one since
60 * this is (ab)using the edge data structure in order to implement a stack.
61 */
62 list->push_tail(link(mem_ctx, block, bblock_link_logical));
63 }
64
65 bblock_t::bblock_t(cfg_t *cfg) :
66 cfg(cfg), start_ip(0), end_ip(0), num(0), cycle_count(0)
67 {
68 instructions.make_empty();
69 parents.make_empty();
70 children.make_empty();
71 }
72
73 void
74 bblock_t::add_successor(void *mem_ctx, bblock_t *successor,
75 enum bblock_link_kind kind)
76 {
77 successor->parents.push_tail(::link(mem_ctx, this, kind));
78 children.push_tail(::link(mem_ctx, successor, kind));
79 }
80
81 bool
82 bblock_t::is_predecessor_of(const bblock_t *block,
83 enum bblock_link_kind kind) const
84 {
85 foreach_list_typed_safe (bblock_link, parent, link, &block->parents) {
86 if (parent->block == this && parent->kind <= kind) {
87 return true;
88 }
89 }
90
91 return false;
92 }
93
94 bool
95 bblock_t::is_successor_of(const bblock_t *block,
96 enum bblock_link_kind kind) const
97 {
98 foreach_list_typed_safe (bblock_link, child, link, &block->children) {
99 if (child->block == this && child->kind <= kind) {
100 return true;
101 }
102 }
103
104 return false;
105 }
106
107 static bool
108 ends_block(const backend_instruction *inst)
109 {
110 enum opcode op = inst->opcode;
111
112 return op == BRW_OPCODE_IF ||
113 op == BRW_OPCODE_ELSE ||
114 op == BRW_OPCODE_CONTINUE ||
115 op == BRW_OPCODE_BREAK ||
116 op == BRW_OPCODE_DO ||
117 op == BRW_OPCODE_WHILE;
118 }
119
120 static bool
121 starts_block(const backend_instruction *inst)
122 {
123 enum opcode op = inst->opcode;
124
125 return op == BRW_OPCODE_DO ||
126 op == BRW_OPCODE_ENDIF;
127 }
128
129 bool
130 bblock_t::can_combine_with(const bblock_t *that) const
131 {
132 if ((const bblock_t *)this->link.next != that)
133 return false;
134
135 if (ends_block(this->end()) ||
136 starts_block(that->start()))
137 return false;
138
139 return true;
140 }
141
142 void
143 bblock_t::combine_with(bblock_t *that)
144 {
145 assert(this->can_combine_with(that));
146 foreach_list_typed (bblock_link, link, link, &that->parents) {
147 assert(link->block == this);
148 }
149
150 this->end_ip = that->end_ip;
151 this->instructions.append_list(&that->instructions);
152
153 this->cfg->remove_block(that);
154 }
155
156 void
157 bblock_t::dump() const
158 {
159 const backend_shader *s = this->cfg->s;
160
161 int ip = this->start_ip;
162 foreach_inst_in_block(backend_instruction, inst, this) {
163 fprintf(stderr, "%5d: ", ip);
164 s->dump_instruction(inst);
165 ip++;
166 }
167 }
168
169 cfg_t::cfg_t(const backend_shader *s, exec_list *instructions) :
170 s(s)
171 {
172 mem_ctx = ralloc_context(NULL);
173 block_list.make_empty();
174 blocks = NULL;
175 num_blocks = 0;
176 cycle_count = 0;
177
178 bblock_t *cur = NULL;
179 int ip = 0;
180
181 bblock_t *entry = new_block();
182 bblock_t *cur_if = NULL; /**< BB ending with IF. */
183 bblock_t *cur_else = NULL; /**< BB ending with ELSE. */
184 bblock_t *cur_endif = NULL; /**< BB starting with ENDIF. */
185 bblock_t *cur_do = NULL; /**< BB starting with DO. */
186 bblock_t *cur_while = NULL; /**< BB immediately following WHILE. */
187 exec_list if_stack, else_stack, do_stack, while_stack;
188 bblock_t *next;
189
190 set_next_block(&cur, entry, ip);
191
192 foreach_in_list_safe(backend_instruction, inst, instructions) {
193 /* set_next_block wants the post-incremented ip */
194 ip++;
195
196 inst->exec_node::remove();
197
198 switch (inst->opcode) {
199 case BRW_OPCODE_IF:
200 cur->instructions.push_tail(inst);
201
202 /* Push our information onto a stack so we can recover from
203 * nested ifs.
204 */
205 push_stack(&if_stack, mem_ctx, cur_if);
206 push_stack(&else_stack, mem_ctx, cur_else);
207
208 cur_if = cur;
209 cur_else = NULL;
210 cur_endif = NULL;
211
212 /* Set up our immediately following block, full of "then"
213 * instructions.
214 */
215 next = new_block();
216 cur_if->add_successor(mem_ctx, next, bblock_link_logical);
217
218 set_next_block(&cur, next, ip);
219 break;
220
221 case BRW_OPCODE_ELSE:
222 cur->instructions.push_tail(inst);
223
224 cur_else = cur;
225
226 next = new_block();
227 assert(cur_if != NULL);
228 cur_if->add_successor(mem_ctx, next, bblock_link_logical);
229 cur_else->add_successor(mem_ctx, next, bblock_link_physical);
230
231 set_next_block(&cur, next, ip);
232 break;
233
234 case BRW_OPCODE_ENDIF: {
235 if (cur->instructions.is_empty()) {
236 /* New block was just created; use it. */
237 cur_endif = cur;
238 } else {
239 cur_endif = new_block();
240
241 cur->add_successor(mem_ctx, cur_endif, bblock_link_logical);
242
243 set_next_block(&cur, cur_endif, ip - 1);
244 }
245
246 cur->instructions.push_tail(inst);
247
248 if (cur_else) {
249 cur_else->add_successor(mem_ctx, cur_endif, bblock_link_logical);
250 } else {
251 assert(cur_if != NULL);
252 cur_if->add_successor(mem_ctx, cur_endif, bblock_link_logical);
253 }
254
255 assert(cur_if->end()->opcode == BRW_OPCODE_IF);
256 assert(!cur_else || cur_else->end()->opcode == BRW_OPCODE_ELSE);
257
258 /* Pop the stack so we're in the previous if/else/endif */
259 cur_if = pop_stack(&if_stack);
260 cur_else = pop_stack(&else_stack);
261 break;
262 }
263 case BRW_OPCODE_DO:
264 /* Push our information onto a stack so we can recover from
265 * nested loops.
266 */
267 push_stack(&do_stack, mem_ctx, cur_do);
268 push_stack(&while_stack, mem_ctx, cur_while);
269
270 /* Set up the block just after the while. Don't know when exactly
271 * it will start, yet.
272 */
273 cur_while = new_block();
274
275 if (cur->instructions.is_empty()) {
276 /* New block was just created; use it. */
277 cur_do = cur;
278 } else {
279 cur_do = new_block();
280
281 cur->add_successor(mem_ctx, cur_do, bblock_link_logical);
282
283 set_next_block(&cur, cur_do, ip - 1);
284 }
285
286 cur->instructions.push_tail(inst);
287
288 /* Represent divergent execution of the loop as a pair of alternative
289 * edges coming out of the DO instruction: For any physical iteration
290 * of the loop a given logical thread can either start off enabled
291 * (which is represented as the "next" successor), or disabled (if it
292 * has reached a non-uniform exit of the loop during a previous
293 * iteration, which is represented as the "cur_while" successor).
294 *
295 * The disabled edge will be taken by the logical thread anytime we
296 * arrive at the DO instruction through a back-edge coming from a
297 * conditional exit of the loop where divergent control flow started.
298 *
299 * This guarantees that there is a control-flow path from any
300 * divergence point of the loop into the convergence point
301 * (immediately past the WHILE instruction) such that it overlaps the
302 * whole IP region of divergent control flow (potentially the whole
303 * loop) *and* doesn't imply the execution of any instructions part
304 * of the loop (since the corresponding execution mask bit will be
305 * disabled for a diverging thread).
306 *
307 * This way we make sure that any variables that are live throughout
308 * the region of divergence for an inactive logical thread are also
309 * considered to interfere with any other variables assigned by
310 * active logical threads within the same physical region of the
311 * program, since otherwise we would risk cross-channel data
312 * corruption.
313 */
314 next = new_block();
315 cur->add_successor(mem_ctx, next, bblock_link_logical);
316 cur->add_successor(mem_ctx, cur_while, bblock_link_physical);
317 set_next_block(&cur, next, ip);
318 break;
319
320 case BRW_OPCODE_CONTINUE:
321 cur->instructions.push_tail(inst);
322
323 /* A conditional CONTINUE may start a region of divergent control
324 * flow until the start of the next loop iteration (*not* until the
325 * end of the loop which is why the successor is not the top-level
326 * divergence point at cur_do). The live interval of any variable
327 * extending through a CONTINUE edge is guaranteed to overlap the
328 * whole region of divergent execution, because any variable live-out
329 * at the CONTINUE instruction will also be live-in at the top of the
330 * loop, and therefore also live-out at the bottom-most point of the
331 * loop which is reachable from the top (since a control flow path
332 * exists from a definition of the variable through this CONTINUE
333 * instruction, the top of the loop, the (reachable) bottom of the
334 * loop, the top of the loop again, into a use of the variable).
335 */
336 assert(cur_do != NULL);
337 cur->add_successor(mem_ctx, cur_do->next(), bblock_link_logical);
338
339 next = new_block();
340 if (inst->predicate)
341 cur->add_successor(mem_ctx, next, bblock_link_logical);
342 else
343 cur->add_successor(mem_ctx, next, bblock_link_physical);
344
345 set_next_block(&cur, next, ip);
346 break;
347
348 case BRW_OPCODE_BREAK:
349 cur->instructions.push_tail(inst);
350
351 /* A conditional BREAK instruction may start a region of divergent
352 * control flow until the end of the loop if the condition is
353 * non-uniform, in which case the loop will execute additional
354 * iterations with the present channel disabled. We model this as a
355 * control flow path from the divergence point to the convergence
356 * point that overlaps the whole IP range of the loop and skips over
357 * the execution of any other instructions part of the loop.
358 *
359 * See the DO case for additional explanation.
360 */
361 assert(cur_do != NULL);
362 cur->add_successor(mem_ctx, cur_do, bblock_link_physical);
363 cur->add_successor(mem_ctx, cur_while, bblock_link_logical);
364
365 next = new_block();
366 if (inst->predicate)
367 cur->add_successor(mem_ctx, next, bblock_link_logical);
368
369 set_next_block(&cur, next, ip);
370 break;
371
372 case BRW_OPCODE_WHILE:
373 cur->instructions.push_tail(inst);
374
375 assert(cur_do != NULL && cur_while != NULL);
376
377 /* A conditional WHILE instruction may start a region of divergent
378 * control flow until the end of the loop, just like the BREAK
379 * instruction. See the BREAK case for more details. OTOH an
380 * unconditional WHILE instruction is non-divergent (just like an
381 * unconditional CONTINUE), and will necessarily lead to the
382 * execution of an additional iteration of the loop for all enabled
383 * channels, so we may skip over the divergence point at the top of
384 * the loop to keep the CFG as unambiguous as possible.
385 */
386 if (inst->predicate) {
387 cur->add_successor(mem_ctx, cur_do, bblock_link_logical);
388 } else {
389 cur->add_successor(mem_ctx, cur_do->next(), bblock_link_logical);
390 }
391
392 set_next_block(&cur, cur_while, ip);
393
394 /* Pop the stack so we're in the previous loop */
395 cur_do = pop_stack(&do_stack);
396 cur_while = pop_stack(&while_stack);
397 break;
398
399 default:
400 cur->instructions.push_tail(inst);
401 break;
402 }
403 }
404
405 cur->end_ip = ip - 1;
406
407 make_block_array();
408 }
409
410 cfg_t::~cfg_t()
411 {
412 ralloc_free(mem_ctx);
413 }
414
415 void
416 cfg_t::remove_block(bblock_t *block)
417 {
418 foreach_list_typed_safe (bblock_link, predecessor, link, &block->parents) {
419 /* Remove block from all of its predecessors' successor lists. */
420 foreach_list_typed_safe (bblock_link, successor, link,
421 &predecessor->block->children) {
422 if (block == successor->block) {
423 successor->link.remove();
424 ralloc_free(successor);
425 }
426 }
427
428 /* Add removed-block's successors to its predecessors' successor lists. */
429 foreach_list_typed (bblock_link, successor, link, &block->children) {
430 if (!successor->block->is_successor_of(predecessor->block,
431 successor->kind)) {
432 predecessor->block->children.push_tail(link(mem_ctx,
433 successor->block,
434 successor->kind));
435 }
436 }
437 }
438
439 foreach_list_typed_safe (bblock_link, successor, link, &block->children) {
440 /* Remove block from all of its childrens' parents lists. */
441 foreach_list_typed_safe (bblock_link, predecessor, link,
442 &successor->block->parents) {
443 if (block == predecessor->block) {
444 predecessor->link.remove();
445 ralloc_free(predecessor);
446 }
447 }
448
449 /* Add removed-block's predecessors to its successors' predecessor lists. */
450 foreach_list_typed (bblock_link, predecessor, link, &block->parents) {
451 if (!predecessor->block->is_predecessor_of(successor->block,
452 predecessor->kind)) {
453 successor->block->parents.push_tail(link(mem_ctx,
454 predecessor->block,
455 predecessor->kind));
456 }
457 }
458 }
459
460 block->link.remove();
461
462 for (int b = block->num; b < this->num_blocks - 1; b++) {
463 this->blocks[b] = this->blocks[b + 1];
464 this->blocks[b]->num = b;
465 }
466
467 this->blocks[this->num_blocks - 1]->num = this->num_blocks - 2;
468 this->num_blocks--;
469 }
470
471 bblock_t *
472 cfg_t::new_block()
473 {
474 bblock_t *block = new(mem_ctx) bblock_t(this);
475
476 return block;
477 }
478
479 void
480 cfg_t::set_next_block(bblock_t **cur, bblock_t *block, int ip)
481 {
482 if (*cur) {
483 (*cur)->end_ip = ip - 1;
484 }
485
486 block->start_ip = ip;
487 block->num = num_blocks++;
488 block_list.push_tail(&block->link);
489 *cur = block;
490 }
491
492 void
493 cfg_t::make_block_array()
494 {
495 blocks = ralloc_array(mem_ctx, bblock_t *, num_blocks);
496
497 int i = 0;
498 foreach_block (block, this) {
499 blocks[i++] = block;
500 }
501 assert(i == num_blocks);
502 }
503
504 void
505 cfg_t::dump()
506 {
507 const idom_tree *idom = (s ? &s->idom_analysis.require() : NULL);
508
509 foreach_block (block, this) {
510 if (idom && idom->parent(block))
511 fprintf(stderr, "START B%d IDOM(B%d)", block->num,
512 idom->parent(block)->num);
513 else
514 fprintf(stderr, "START B%d IDOM(none)", block->num);
515
516 foreach_list_typed(bblock_link, link, link, &block->parents) {
517 fprintf(stderr, " <%cB%d",
518 link->kind == bblock_link_logical ? '-' : '~',
519 link->block->num);
520 }
521 fprintf(stderr, "\n");
522 if (s != NULL)
523 block->dump();
524 fprintf(stderr, "END B%d", block->num);
525 foreach_list_typed(bblock_link, link, link, &block->children) {
526 fprintf(stderr, " %c>B%d",
527 link->kind == bblock_link_logical ? '-' : '~',
528 link->block->num);
529 }
530 fprintf(stderr, "\n");
531 }
532 }
533
534 /* Calculates the immediate dominator of each block, according to "A Simple,
535 * Fast Dominance Algorithm" by Keith D. Cooper, Timothy J. Harvey, and Ken
536 * Kennedy.
537 *
538 * The authors claim that for control flow graphs of sizes normally encountered
539 * (less than 1000 nodes) that this algorithm is significantly faster than
540 * others like Lengauer-Tarjan.
541 */
542 idom_tree::idom_tree(const backend_shader *s) :
543 num_parents(s->cfg->num_blocks),
544 parents(new bblock_t *[num_parents]())
545 {
546 bool changed;
547
548 parents[0] = s->cfg->blocks[0];
549
550 do {
551 changed = false;
552
553 foreach_block(block, s->cfg) {
554 if (block->num == 0)
555 continue;
556
557 bblock_t *new_idom = NULL;
558 foreach_list_typed(bblock_link, parent_link, link, &block->parents) {
559 if (parent(parent_link->block)) {
560 new_idom = (new_idom ? intersect(new_idom, parent_link->block) :
561 parent_link->block);
562 }
563 }
564
565 if (parent(block) != new_idom) {
566 parents[block->num] = new_idom;
567 changed = true;
568 }
569 }
570 } while (changed);
571 }
572
573 idom_tree::~idom_tree()
574 {
575 delete[] parents;
576 }
577
578 bblock_t *
579 idom_tree::intersect(bblock_t *b1, bblock_t *b2) const
580 {
581 /* Note, the comparisons here are the opposite of what the paper says
582 * because we index blocks from beginning -> end (i.e. reverse post-order)
583 * instead of post-order like they assume.
584 */
585 while (b1->num != b2->num) {
586 while (b1->num > b2->num)
587 b1 = parent(b1);
588 while (b2->num > b1->num)
589 b2 = parent(b2);
590 }
591 assert(b1);
592 return b1;
593 }
594
595 void
596 idom_tree::dump() const
597 {
598 printf("digraph DominanceTree {\n");
599 for (unsigned i = 0; i < num_parents; i++)
600 printf("\t%d -> %d\n", parents[i]->num, i);
601 printf("}\n");
602 }
603
604 void
605 cfg_t::dump_cfg()
606 {
607 printf("digraph CFG {\n");
608 for (int b = 0; b < num_blocks; b++) {
609 bblock_t *block = this->blocks[b];
610
611 foreach_list_typed_safe (bblock_link, child, link, &block->children) {
612 printf("\t%d -> %d\n", b, child->block->num);
613 }
614 }
615 printf("}\n");
616 }