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