util: use C99 declaration in the for-loop set_foreach() macro
[mesa.git] / src / compiler / nir / nir_control_flow.c
1 /*
2 * Copyright © 2014 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 * Connor Abbott (cwabbott0@gmail.com)
25 *
26 */
27
28 #include "nir_control_flow_private.h"
29
30 /**
31 * \name Control flow modification
32 *
33 * These functions modify the control flow tree while keeping the control flow
34 * graph up-to-date. The invariants respected are:
35 * 1. Each then statement, else statement, or loop body must have at least one
36 * control flow node.
37 * 2. Each if-statement and loop must have one basic block before it and one
38 * after.
39 * 3. Two basic blocks cannot be directly next to each other.
40 * 4. If a basic block has a jump instruction, there must be only one and it
41 * must be at the end of the block.
42 *
43 * The purpose of the second one is so that we have places to insert code during
44 * GCM, as well as eliminating the possibility of critical edges.
45 */
46 /*@{*/
47
48 static inline void
49 block_add_pred(nir_block *block, nir_block *pred)
50 {
51 _mesa_set_add(block->predecessors, pred);
52 }
53
54 static inline void
55 block_remove_pred(nir_block *block, nir_block *pred)
56 {
57 struct set_entry *entry = _mesa_set_search(block->predecessors, pred);
58
59 assert(entry);
60
61 _mesa_set_remove(block->predecessors, entry);
62 }
63
64 static void
65 link_blocks(nir_block *pred, nir_block *succ1, nir_block *succ2)
66 {
67 pred->successors[0] = succ1;
68 if (succ1 != NULL)
69 block_add_pred(succ1, pred);
70
71 pred->successors[1] = succ2;
72 if (succ2 != NULL)
73 block_add_pred(succ2, pred);
74 }
75
76 static void
77 unlink_blocks(nir_block *pred, nir_block *succ)
78 {
79 if (pred->successors[0] == succ) {
80 pred->successors[0] = pred->successors[1];
81 pred->successors[1] = NULL;
82 } else {
83 assert(pred->successors[1] == succ);
84 pred->successors[1] = NULL;
85 }
86
87 block_remove_pred(succ, pred);
88 }
89
90 static void
91 unlink_block_successors(nir_block *block)
92 {
93 if (block->successors[1] != NULL)
94 unlink_blocks(block, block->successors[1]);
95 if (block->successors[0] != NULL)
96 unlink_blocks(block, block->successors[0]);
97 }
98
99 static void
100 link_non_block_to_block(nir_cf_node *node, nir_block *block)
101 {
102 if (node->type == nir_cf_node_if) {
103 /*
104 * We're trying to link an if to a block after it; this just means linking
105 * the last block of the then and else branches.
106 */
107
108 nir_if *if_stmt = nir_cf_node_as_if(node);
109
110 nir_block *last_then_block = nir_if_last_then_block(if_stmt);
111 nir_block *last_else_block = nir_if_last_else_block(if_stmt);
112
113 if (!nir_block_ends_in_jump(last_then_block)) {
114 unlink_block_successors(last_then_block);
115 link_blocks(last_then_block, block, NULL);
116 }
117
118 if (!nir_block_ends_in_jump(last_else_block)) {
119 unlink_block_successors(last_else_block);
120 link_blocks(last_else_block, block, NULL);
121 }
122 } else {
123 assert(node->type == nir_cf_node_loop);
124 }
125 }
126
127 static void
128 link_block_to_non_block(nir_block *block, nir_cf_node *node)
129 {
130 if (node->type == nir_cf_node_if) {
131 /*
132 * We're trying to link a block to an if after it; this just means linking
133 * the block to the first block of the then and else branches.
134 */
135
136 nir_if *if_stmt = nir_cf_node_as_if(node);
137
138 nir_block *first_then_block = nir_if_first_then_block(if_stmt);
139 nir_block *first_else_block = nir_if_first_else_block(if_stmt);
140
141 unlink_block_successors(block);
142 link_blocks(block, first_then_block, first_else_block);
143 } else {
144 /*
145 * For similar reasons as the corresponding case in
146 * link_non_block_to_block(), don't worry about if the loop header has
147 * any predecessors that need to be unlinked.
148 */
149
150 nir_loop *loop = nir_cf_node_as_loop(node);
151
152 nir_block *loop_header_block = nir_loop_first_block(loop);
153
154 unlink_block_successors(block);
155 link_blocks(block, loop_header_block, NULL);
156 }
157
158 }
159
160 /**
161 * Replace a block's successor with a different one.
162 */
163 static void
164 replace_successor(nir_block *block, nir_block *old_succ, nir_block *new_succ)
165 {
166 if (block->successors[0] == old_succ) {
167 block->successors[0] = new_succ;
168 } else {
169 assert(block->successors[1] == old_succ);
170 block->successors[1] = new_succ;
171 }
172
173 block_remove_pred(old_succ, block);
174 block_add_pred(new_succ, block);
175 }
176
177 /**
178 * Takes a basic block and inserts a new empty basic block before it, making its
179 * predecessors point to the new block. This essentially splits the block into
180 * an empty header and a body so that another non-block CF node can be inserted
181 * between the two. Note that this does *not* link the two basic blocks, so
182 * some kind of cleanup *must* be performed after this call.
183 */
184
185 static nir_block *
186 split_block_beginning(nir_block *block)
187 {
188 nir_block *new_block = nir_block_create(ralloc_parent(block));
189 new_block->cf_node.parent = block->cf_node.parent;
190 exec_node_insert_node_before(&block->cf_node.node, &new_block->cf_node.node);
191
192 set_foreach(block->predecessors, entry) {
193 nir_block *pred = (nir_block *) entry->key;
194 replace_successor(pred, block, new_block);
195 }
196
197 /* Any phi nodes must stay part of the new block, or else their
198 * sourcse will be messed up. This will reverse the order of the phis, but
199 * order shouldn't matter.
200 */
201 nir_foreach_instr_safe(instr, block) {
202 if (instr->type != nir_instr_type_phi)
203 break;
204
205 exec_node_remove(&instr->node);
206 instr->block = new_block;
207 exec_list_push_head(&new_block->instr_list, &instr->node);
208 }
209
210 return new_block;
211 }
212
213 static void
214 rewrite_phi_preds(nir_block *block, nir_block *old_pred, nir_block *new_pred)
215 {
216 nir_foreach_instr_safe(instr, block) {
217 if (instr->type != nir_instr_type_phi)
218 break;
219
220 nir_phi_instr *phi = nir_instr_as_phi(instr);
221 nir_foreach_phi_src(src, phi) {
222 if (src->pred == old_pred) {
223 src->pred = new_pred;
224 break;
225 }
226 }
227 }
228 }
229
230 static void
231 insert_phi_undef(nir_block *block, nir_block *pred)
232 {
233 nir_function_impl *impl = nir_cf_node_get_function(&block->cf_node);
234 nir_foreach_instr(instr, block) {
235 if (instr->type != nir_instr_type_phi)
236 break;
237
238 nir_phi_instr *phi = nir_instr_as_phi(instr);
239 nir_ssa_undef_instr *undef =
240 nir_ssa_undef_instr_create(ralloc_parent(phi),
241 phi->dest.ssa.num_components,
242 phi->dest.ssa.bit_size);
243 nir_instr_insert_before_cf_list(&impl->body, &undef->instr);
244 nir_phi_src *src = ralloc(phi, nir_phi_src);
245 src->pred = pred;
246 src->src.parent_instr = &phi->instr;
247 src->src.is_ssa = true;
248 src->src.ssa = &undef->def;
249
250 list_addtail(&src->src.use_link, &undef->def.uses);
251
252 exec_list_push_tail(&phi->srcs, &src->node);
253 }
254 }
255
256 /**
257 * Moves the successors of source to the successors of dest, leaving both
258 * successors of source NULL.
259 */
260
261 static void
262 move_successors(nir_block *source, nir_block *dest)
263 {
264 nir_block *succ1 = source->successors[0];
265 nir_block *succ2 = source->successors[1];
266
267 if (succ1) {
268 unlink_blocks(source, succ1);
269 rewrite_phi_preds(succ1, source, dest);
270 }
271
272 if (succ2) {
273 unlink_blocks(source, succ2);
274 rewrite_phi_preds(succ2, source, dest);
275 }
276
277 unlink_block_successors(dest);
278 link_blocks(dest, succ1, succ2);
279 }
280
281 /* Given a basic block with no successors that has been inserted into the
282 * control flow tree, gives it the successors it would normally have assuming
283 * it doesn't end in a jump instruction. Also inserts phi sources with undefs
284 * if necessary.
285 */
286 static void
287 block_add_normal_succs(nir_block *block)
288 {
289 if (exec_node_is_tail_sentinel(block->cf_node.node.next)) {
290 nir_cf_node *parent = block->cf_node.parent;
291 if (parent->type == nir_cf_node_if) {
292 nir_cf_node *next = nir_cf_node_next(parent);
293 nir_block *next_block = nir_cf_node_as_block(next);
294
295 link_blocks(block, next_block, NULL);
296 } else if (parent->type == nir_cf_node_loop) {
297 nir_loop *loop = nir_cf_node_as_loop(parent);
298
299 nir_block *head_block = nir_loop_first_block(loop);
300
301 link_blocks(block, head_block, NULL);
302 insert_phi_undef(head_block, block);
303 } else {
304 nir_function_impl *impl = nir_cf_node_as_function(parent);
305 link_blocks(block, impl->end_block, NULL);
306 }
307 } else {
308 nir_cf_node *next = nir_cf_node_next(&block->cf_node);
309 if (next->type == nir_cf_node_if) {
310 nir_if *next_if = nir_cf_node_as_if(next);
311
312 nir_block *first_then_block = nir_if_first_then_block(next_if);
313 nir_block *first_else_block = nir_if_first_else_block(next_if);
314
315 link_blocks(block, first_then_block, first_else_block);
316 } else {
317 nir_loop *next_loop = nir_cf_node_as_loop(next);
318
319 nir_block *first_block = nir_loop_first_block(next_loop);
320
321 link_blocks(block, first_block, NULL);
322 insert_phi_undef(first_block, block);
323 }
324 }
325 }
326
327 static nir_block *
328 split_block_end(nir_block *block)
329 {
330 nir_block *new_block = nir_block_create(ralloc_parent(block));
331 new_block->cf_node.parent = block->cf_node.parent;
332 exec_node_insert_after(&block->cf_node.node, &new_block->cf_node.node);
333
334 if (nir_block_ends_in_jump(block)) {
335 /* Figure out what successor block would've had if it didn't have a jump
336 * instruction, and make new_block have that successor.
337 */
338 block_add_normal_succs(new_block);
339 } else {
340 move_successors(block, new_block);
341 }
342
343 return new_block;
344 }
345
346 static nir_block *
347 split_block_before_instr(nir_instr *instr)
348 {
349 assert(instr->type != nir_instr_type_phi);
350 nir_block *new_block = split_block_beginning(instr->block);
351
352 nir_foreach_instr_safe(cur_instr, instr->block) {
353 if (cur_instr == instr)
354 break;
355
356 exec_node_remove(&cur_instr->node);
357 cur_instr->block = new_block;
358 exec_list_push_tail(&new_block->instr_list, &cur_instr->node);
359 }
360
361 return new_block;
362 }
363
364 /* Splits a basic block at the point specified by the cursor. The "before" and
365 * "after" arguments are filled out with the blocks resulting from the split
366 * if non-NULL. Note that the "beginning" of the block is actually interpreted
367 * as before the first non-phi instruction, and it's illegal to split a block
368 * before a phi instruction.
369 */
370
371 static void
372 split_block_cursor(nir_cursor cursor,
373 nir_block **_before, nir_block **_after)
374 {
375 nir_block *before, *after;
376 switch (cursor.option) {
377 case nir_cursor_before_block:
378 after = cursor.block;
379 before = split_block_beginning(cursor.block);
380 break;
381
382 case nir_cursor_after_block:
383 before = cursor.block;
384 after = split_block_end(cursor.block);
385 break;
386
387 case nir_cursor_before_instr:
388 after = cursor.instr->block;
389 before = split_block_before_instr(cursor.instr);
390 break;
391
392 case nir_cursor_after_instr:
393 /* We lower this to split_block_before_instr() so that we can keep the
394 * after-a-jump-instr case contained to split_block_end().
395 */
396 if (nir_instr_is_last(cursor.instr)) {
397 before = cursor.instr->block;
398 after = split_block_end(cursor.instr->block);
399 } else {
400 after = cursor.instr->block;
401 before = split_block_before_instr(nir_instr_next(cursor.instr));
402 }
403 break;
404
405 default:
406 unreachable("not reached");
407 }
408
409 if (_before)
410 *_before = before;
411 if (_after)
412 *_after = after;
413 }
414
415 /**
416 * Inserts a non-basic block between two basic blocks and links them together.
417 */
418
419 static void
420 insert_non_block(nir_block *before, nir_cf_node *node, nir_block *after)
421 {
422 node->parent = before->cf_node.parent;
423 exec_node_insert_after(&before->cf_node.node, &node->node);
424 link_block_to_non_block(before, node);
425 link_non_block_to_block(node, after);
426 }
427
428 /* walk up the control flow tree to find the innermost enclosed loop */
429 static nir_loop *
430 nearest_loop(nir_cf_node *node)
431 {
432 while (node->type != nir_cf_node_loop) {
433 node = node->parent;
434 }
435
436 return nir_cf_node_as_loop(node);
437 }
438
439 static void
440 remove_phi_src(nir_block *block, nir_block *pred)
441 {
442 nir_foreach_instr(instr, block) {
443 if (instr->type != nir_instr_type_phi)
444 break;
445
446 nir_phi_instr *phi = nir_instr_as_phi(instr);
447 nir_foreach_phi_src_safe(src, phi) {
448 if (src->pred == pred) {
449 list_del(&src->src.use_link);
450 exec_node_remove(&src->node);
451 }
452 }
453 }
454 }
455
456 /*
457 * update the CFG after a jump instruction has been added to the end of a block
458 */
459
460 void
461 nir_handle_add_jump(nir_block *block)
462 {
463 nir_instr *instr = nir_block_last_instr(block);
464 nir_jump_instr *jump_instr = nir_instr_as_jump(instr);
465
466 if (block->successors[0])
467 remove_phi_src(block->successors[0], block);
468 if (block->successors[1])
469 remove_phi_src(block->successors[1], block);
470 unlink_block_successors(block);
471
472 nir_function_impl *impl = nir_cf_node_get_function(&block->cf_node);
473 nir_metadata_preserve(impl, nir_metadata_none);
474
475 if (jump_instr->type == nir_jump_break ||
476 jump_instr->type == nir_jump_continue) {
477 nir_loop *loop = nearest_loop(&block->cf_node);
478
479 if (jump_instr->type == nir_jump_continue) {
480 nir_block *first_block = nir_loop_first_block(loop);
481 link_blocks(block, first_block, NULL);
482 } else {
483 nir_cf_node *after = nir_cf_node_next(&loop->cf_node);
484 nir_block *after_block = nir_cf_node_as_block(after);
485 link_blocks(block, after_block, NULL);
486 }
487 } else {
488 assert(jump_instr->type == nir_jump_return);
489 link_blocks(block, impl->end_block, NULL);
490 }
491 }
492
493 /* Removes the successor of a block with a jump. Note that the jump to be
494 * eliminated may be free-floating.
495 */
496
497 static void
498 unlink_jump(nir_block *block, nir_jump_type type, bool add_normal_successors)
499 {
500 if (block->successors[0])
501 remove_phi_src(block->successors[0], block);
502 if (block->successors[1])
503 remove_phi_src(block->successors[1], block);
504
505 unlink_block_successors(block);
506 if (add_normal_successors)
507 block_add_normal_succs(block);
508 }
509
510 void
511 nir_handle_remove_jump(nir_block *block, nir_jump_type type)
512 {
513 unlink_jump(block, type, true);
514
515 nir_function_impl *impl = nir_cf_node_get_function(&block->cf_node);
516 nir_metadata_preserve(impl, nir_metadata_none);
517 }
518
519 static void
520 update_if_uses(nir_cf_node *node)
521 {
522 if (node->type != nir_cf_node_if)
523 return;
524
525 nir_if *if_stmt = nir_cf_node_as_if(node);
526
527 if_stmt->condition.parent_if = if_stmt;
528 if (if_stmt->condition.is_ssa) {
529 list_addtail(&if_stmt->condition.use_link,
530 &if_stmt->condition.ssa->if_uses);
531 } else {
532 list_addtail(&if_stmt->condition.use_link,
533 &if_stmt->condition.reg.reg->if_uses);
534 }
535 }
536
537 /**
538 * Stitch two basic blocks together into one. The aggregate must have the same
539 * predecessors as the first and the same successors as the second.
540 */
541
542 static void
543 stitch_blocks(nir_block *before, nir_block *after)
544 {
545 /*
546 * We move after into before, so we have to deal with up to 2 successors vs.
547 * possibly a large number of predecessors.
548 *
549 * TODO: special case when before is empty and after isn't?
550 */
551
552 if (nir_block_ends_in_jump(before)) {
553 assert(exec_list_is_empty(&after->instr_list));
554 if (after->successors[0])
555 remove_phi_src(after->successors[0], after);
556 if (after->successors[1])
557 remove_phi_src(after->successors[1], after);
558 unlink_block_successors(after);
559 exec_node_remove(&after->cf_node.node);
560 } else {
561 move_successors(after, before);
562
563 foreach_list_typed(nir_instr, instr, node, &after->instr_list) {
564 instr->block = before;
565 }
566
567 exec_list_append(&before->instr_list, &after->instr_list);
568 exec_node_remove(&after->cf_node.node);
569 }
570 }
571
572 void
573 nir_cf_node_insert(nir_cursor cursor, nir_cf_node *node)
574 {
575 nir_block *before, *after;
576
577 split_block_cursor(cursor, &before, &after);
578
579 if (node->type == nir_cf_node_block) {
580 nir_block *block = nir_cf_node_as_block(node);
581 exec_node_insert_after(&before->cf_node.node, &block->cf_node.node);
582 block->cf_node.parent = before->cf_node.parent;
583 /* stitch_blocks() assumes that any block that ends with a jump has
584 * already been setup with the correct successors, so we need to set
585 * up jumps here as the block is being inserted.
586 */
587 if (nir_block_ends_in_jump(block))
588 nir_handle_add_jump(block);
589
590 stitch_blocks(block, after);
591 stitch_blocks(before, block);
592 } else {
593 update_if_uses(node);
594 insert_non_block(before, node, after);
595 }
596 }
597
598 static bool
599 replace_ssa_def_uses(nir_ssa_def *def, void *void_impl)
600 {
601 nir_function_impl *impl = void_impl;
602 void *mem_ctx = ralloc_parent(impl);
603
604 nir_ssa_undef_instr *undef =
605 nir_ssa_undef_instr_create(mem_ctx, def->num_components,
606 def->bit_size);
607 nir_instr_insert_before_cf_list(&impl->body, &undef->instr);
608 nir_ssa_def_rewrite_uses(def, nir_src_for_ssa(&undef->def));
609 return true;
610 }
611
612 static void
613 cleanup_cf_node(nir_cf_node *node, nir_function_impl *impl)
614 {
615 switch (node->type) {
616 case nir_cf_node_block: {
617 nir_block *block = nir_cf_node_as_block(node);
618 /* We need to walk the instructions and clean up defs/uses */
619 nir_foreach_instr_safe(instr, block) {
620 if (instr->type == nir_instr_type_jump) {
621 nir_jump_type jump_type = nir_instr_as_jump(instr)->type;
622 unlink_jump(block, jump_type, false);
623 } else {
624 nir_foreach_ssa_def(instr, replace_ssa_def_uses, impl);
625 nir_instr_remove(instr);
626 }
627 }
628 break;
629 }
630
631 case nir_cf_node_if: {
632 nir_if *if_stmt = nir_cf_node_as_if(node);
633 foreach_list_typed(nir_cf_node, child, node, &if_stmt->then_list)
634 cleanup_cf_node(child, impl);
635 foreach_list_typed(nir_cf_node, child, node, &if_stmt->else_list)
636 cleanup_cf_node(child, impl);
637
638 list_del(&if_stmt->condition.use_link);
639 break;
640 }
641
642 case nir_cf_node_loop: {
643 nir_loop *loop = nir_cf_node_as_loop(node);
644 foreach_list_typed(nir_cf_node, child, node, &loop->body)
645 cleanup_cf_node(child, impl);
646 break;
647 }
648 case nir_cf_node_function: {
649 nir_function_impl *impl = nir_cf_node_as_function(node);
650 foreach_list_typed(nir_cf_node, child, node, &impl->body)
651 cleanup_cf_node(child, impl);
652 break;
653 }
654 default:
655 unreachable("Invalid CF node type");
656 }
657 }
658
659 void
660 nir_cf_extract(nir_cf_list *extracted, nir_cursor begin, nir_cursor end)
661 {
662 nir_block *block_begin, *block_end, *block_before, *block_after;
663
664 if (nir_cursors_equal(begin, end)) {
665 exec_list_make_empty(&extracted->list);
666 extracted->impl = NULL; /* we shouldn't need this */
667 return;
668 }
669
670 /* In the case where begin points to an instruction in some basic block and
671 * end points to the end of the same basic block, we rely on the fact that
672 * splitting on an instruction moves earlier instructions into a new basic
673 * block. If the later instructions were moved instead, then the end cursor
674 * would be pointing to the same place that begin used to point to, which
675 * is obviously not what we want.
676 */
677 split_block_cursor(begin, &block_before, &block_begin);
678 split_block_cursor(end, &block_end, &block_after);
679
680 extracted->impl = nir_cf_node_get_function(&block_begin->cf_node);
681 exec_list_make_empty(&extracted->list);
682
683 /* Dominance and other block-related information is toast. */
684 nir_metadata_preserve(extracted->impl, nir_metadata_none);
685
686 nir_cf_node *cf_node = &block_begin->cf_node;
687 nir_cf_node *cf_node_end = &block_end->cf_node;
688 while (true) {
689 nir_cf_node *next = nir_cf_node_next(cf_node);
690
691 exec_node_remove(&cf_node->node);
692 cf_node->parent = NULL;
693 exec_list_push_tail(&extracted->list, &cf_node->node);
694
695 if (cf_node == cf_node_end)
696 break;
697
698 cf_node = next;
699 }
700
701 stitch_blocks(block_before, block_after);
702 }
703
704 void
705 nir_cf_reinsert(nir_cf_list *cf_list, nir_cursor cursor)
706 {
707 nir_block *before, *after;
708
709 if (exec_list_is_empty(&cf_list->list))
710 return;
711
712 split_block_cursor(cursor, &before, &after);
713
714 foreach_list_typed_safe(nir_cf_node, node, node, &cf_list->list) {
715 exec_node_remove(&node->node);
716 node->parent = before->cf_node.parent;
717 exec_node_insert_node_before(&after->cf_node.node, &node->node);
718 }
719
720 stitch_blocks(before,
721 nir_cf_node_as_block(nir_cf_node_next(&before->cf_node)));
722 stitch_blocks(nir_cf_node_as_block(nir_cf_node_prev(&after->cf_node)),
723 after);
724 }
725
726 void
727 nir_cf_delete(nir_cf_list *cf_list)
728 {
729 foreach_list_typed(nir_cf_node, node, node, &cf_list->list) {
730 cleanup_cf_node(node, cf_list->impl);
731 }
732 }