2 * Copyright © 2014 Intel Corporation
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:
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
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
24 * Connor Abbott (cwabbott0@gmail.com)
28 #include "nir_control_flow_private.h"
31 * \name Control flow modification
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
37 * 2. Each if-statement and loop must have one basic block before it and one
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.
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.
49 block_add_pred(nir_block
*block
, nir_block
*pred
)
51 _mesa_set_add(block
->predecessors
, pred
);
55 block_remove_pred(nir_block
*block
, nir_block
*pred
)
57 struct set_entry
*entry
= _mesa_set_search(block
->predecessors
, pred
);
61 _mesa_set_remove(block
->predecessors
, entry
);
65 link_blocks(nir_block
*pred
, nir_block
*succ1
, nir_block
*succ2
)
67 pred
->successors
[0] = succ1
;
69 block_add_pred(succ1
, pred
);
71 pred
->successors
[1] = succ2
;
73 block_add_pred(succ2
, pred
);
77 unlink_blocks(nir_block
*pred
, nir_block
*succ
)
79 if (pred
->successors
[0] == succ
) {
80 pred
->successors
[0] = pred
->successors
[1];
81 pred
->successors
[1] = NULL
;
83 assert(pred
->successors
[1] == succ
);
84 pred
->successors
[1] = NULL
;
87 block_remove_pred(succ
, pred
);
91 unlink_block_successors(nir_block
*block
)
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]);
100 link_non_block_to_block(nir_cf_node
*node
, nir_block
*block
)
102 if (node
->type
== nir_cf_node_if
) {
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.
108 nir_if
*if_stmt
= nir_cf_node_as_if(node
);
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
);
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
);
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
);
123 assert(node
->type
== nir_cf_node_loop
);
128 link_block_to_non_block(nir_block
*block
, nir_cf_node
*node
)
130 if (node
->type
== nir_cf_node_if
) {
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.
136 nir_if
*if_stmt
= nir_cf_node_as_if(node
);
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
);
141 unlink_block_successors(block
);
142 link_blocks(block
, first_then_block
, first_else_block
);
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.
150 nir_loop
*loop
= nir_cf_node_as_loop(node
);
152 nir_block
*loop_header_block
= nir_loop_first_block(loop
);
154 unlink_block_successors(block
);
155 link_blocks(block
, loop_header_block
, NULL
);
161 * Replace a block's successor with a different one.
164 replace_successor(nir_block
*block
, nir_block
*old_succ
, nir_block
*new_succ
)
166 if (block
->successors
[0] == old_succ
) {
167 block
->successors
[0] = new_succ
;
169 assert(block
->successors
[1] == old_succ
);
170 block
->successors
[1] = new_succ
;
173 block_remove_pred(old_succ
, block
);
174 block_add_pred(new_succ
, block
);
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.
186 split_block_beginning(nir_block
*block
)
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
);
192 struct set_entry
*entry
;
193 set_foreach(block
->predecessors
, entry
) {
194 nir_block
*pred
= (nir_block
*) entry
->key
;
195 replace_successor(pred
, block
, new_block
);
198 /* Any phi nodes must stay part of the new block, or else their
199 * sourcse will be messed up. This will reverse the order of the phis, but
200 * order shouldn't matter.
202 nir_foreach_instr_safe(instr
, block
) {
203 if (instr
->type
!= nir_instr_type_phi
)
206 exec_node_remove(&instr
->node
);
207 instr
->block
= new_block
;
208 exec_list_push_head(&new_block
->instr_list
, &instr
->node
);
215 rewrite_phi_preds(nir_block
*block
, nir_block
*old_pred
, nir_block
*new_pred
)
217 nir_foreach_instr_safe(instr
, block
) {
218 if (instr
->type
!= nir_instr_type_phi
)
221 nir_phi_instr
*phi
= nir_instr_as_phi(instr
);
222 nir_foreach_phi_src(src
, phi
) {
223 if (src
->pred
== old_pred
) {
224 src
->pred
= new_pred
;
232 insert_phi_undef(nir_block
*block
, nir_block
*pred
)
234 nir_function_impl
*impl
= nir_cf_node_get_function(&block
->cf_node
);
235 nir_foreach_instr(instr
, block
) {
236 if (instr
->type
!= nir_instr_type_phi
)
239 nir_phi_instr
*phi
= nir_instr_as_phi(instr
);
240 nir_ssa_undef_instr
*undef
=
241 nir_ssa_undef_instr_create(ralloc_parent(phi
),
242 phi
->dest
.ssa
.num_components
,
243 phi
->dest
.ssa
.bit_size
);
244 nir_instr_insert_before_cf_list(&impl
->body
, &undef
->instr
);
245 nir_phi_src
*src
= ralloc(phi
, nir_phi_src
);
247 src
->src
.parent_instr
= &phi
->instr
;
248 src
->src
.is_ssa
= true;
249 src
->src
.ssa
= &undef
->def
;
251 list_addtail(&src
->src
.use_link
, &undef
->def
.uses
);
253 exec_list_push_tail(&phi
->srcs
, &src
->node
);
258 * Moves the successors of source to the successors of dest, leaving both
259 * successors of source NULL.
263 move_successors(nir_block
*source
, nir_block
*dest
)
265 nir_block
*succ1
= source
->successors
[0];
266 nir_block
*succ2
= source
->successors
[1];
269 unlink_blocks(source
, succ1
);
270 rewrite_phi_preds(succ1
, source
, dest
);
274 unlink_blocks(source
, succ2
);
275 rewrite_phi_preds(succ2
, source
, dest
);
278 unlink_block_successors(dest
);
279 link_blocks(dest
, succ1
, succ2
);
282 /* Given a basic block with no successors that has been inserted into the
283 * control flow tree, gives it the successors it would normally have assuming
284 * it doesn't end in a jump instruction. Also inserts phi sources with undefs
288 block_add_normal_succs(nir_block
*block
)
290 if (exec_node_is_tail_sentinel(block
->cf_node
.node
.next
)) {
291 nir_cf_node
*parent
= block
->cf_node
.parent
;
292 if (parent
->type
== nir_cf_node_if
) {
293 nir_cf_node
*next
= nir_cf_node_next(parent
);
294 nir_block
*next_block
= nir_cf_node_as_block(next
);
296 link_blocks(block
, next_block
, NULL
);
297 } else if (parent
->type
== nir_cf_node_loop
) {
298 nir_loop
*loop
= nir_cf_node_as_loop(parent
);
300 nir_block
*head_block
= nir_loop_first_block(loop
);
302 link_blocks(block
, head_block
, NULL
);
303 insert_phi_undef(head_block
, block
);
305 nir_function_impl
*impl
= nir_cf_node_as_function(parent
);
306 link_blocks(block
, impl
->end_block
, NULL
);
309 nir_cf_node
*next
= nir_cf_node_next(&block
->cf_node
);
310 if (next
->type
== nir_cf_node_if
) {
311 nir_if
*next_if
= nir_cf_node_as_if(next
);
313 nir_block
*first_then_block
= nir_if_first_then_block(next_if
);
314 nir_block
*first_else_block
= nir_if_first_else_block(next_if
);
316 link_blocks(block
, first_then_block
, first_else_block
);
318 nir_loop
*next_loop
= nir_cf_node_as_loop(next
);
320 nir_block
*first_block
= nir_loop_first_block(next_loop
);
322 link_blocks(block
, first_block
, NULL
);
323 insert_phi_undef(first_block
, block
);
329 split_block_end(nir_block
*block
)
331 nir_block
*new_block
= nir_block_create(ralloc_parent(block
));
332 new_block
->cf_node
.parent
= block
->cf_node
.parent
;
333 exec_node_insert_after(&block
->cf_node
.node
, &new_block
->cf_node
.node
);
335 if (nir_block_ends_in_jump(block
)) {
336 /* Figure out what successor block would've had if it didn't have a jump
337 * instruction, and make new_block have that successor.
339 block_add_normal_succs(new_block
);
341 move_successors(block
, new_block
);
348 split_block_before_instr(nir_instr
*instr
)
350 assert(instr
->type
!= nir_instr_type_phi
);
351 nir_block
*new_block
= split_block_beginning(instr
->block
);
353 nir_foreach_instr_safe(cur_instr
, instr
->block
) {
354 if (cur_instr
== instr
)
357 exec_node_remove(&cur_instr
->node
);
358 cur_instr
->block
= new_block
;
359 exec_list_push_tail(&new_block
->instr_list
, &cur_instr
->node
);
365 /* Splits a basic block at the point specified by the cursor. The "before" and
366 * "after" arguments are filled out with the blocks resulting from the split
367 * if non-NULL. Note that the "beginning" of the block is actually interpreted
368 * as before the first non-phi instruction, and it's illegal to split a block
369 * before a phi instruction.
373 split_block_cursor(nir_cursor cursor
,
374 nir_block
**_before
, nir_block
**_after
)
376 nir_block
*before
, *after
;
377 switch (cursor
.option
) {
378 case nir_cursor_before_block
:
379 after
= cursor
.block
;
380 before
= split_block_beginning(cursor
.block
);
383 case nir_cursor_after_block
:
384 before
= cursor
.block
;
385 after
= split_block_end(cursor
.block
);
388 case nir_cursor_before_instr
:
389 after
= cursor
.instr
->block
;
390 before
= split_block_before_instr(cursor
.instr
);
393 case nir_cursor_after_instr
:
394 /* We lower this to split_block_before_instr() so that we can keep the
395 * after-a-jump-instr case contained to split_block_end().
397 if (nir_instr_is_last(cursor
.instr
)) {
398 before
= cursor
.instr
->block
;
399 after
= split_block_end(cursor
.instr
->block
);
401 after
= cursor
.instr
->block
;
402 before
= split_block_before_instr(nir_instr_next(cursor
.instr
));
407 unreachable("not reached");
417 * Inserts a non-basic block between two basic blocks and links them together.
421 insert_non_block(nir_block
*before
, nir_cf_node
*node
, nir_block
*after
)
423 node
->parent
= before
->cf_node
.parent
;
424 exec_node_insert_after(&before
->cf_node
.node
, &node
->node
);
425 link_block_to_non_block(before
, node
);
426 link_non_block_to_block(node
, after
);
429 /* walk up the control flow tree to find the innermost enclosed loop */
431 nearest_loop(nir_cf_node
*node
)
433 while (node
->type
!= nir_cf_node_loop
) {
437 return nir_cf_node_as_loop(node
);
441 remove_phi_src(nir_block
*block
, nir_block
*pred
)
443 nir_foreach_instr(instr
, block
) {
444 if (instr
->type
!= nir_instr_type_phi
)
447 nir_phi_instr
*phi
= nir_instr_as_phi(instr
);
448 nir_foreach_phi_src_safe(src
, phi
) {
449 if (src
->pred
== pred
) {
450 list_del(&src
->src
.use_link
);
451 exec_node_remove(&src
->node
);
458 * update the CFG after a jump instruction has been added to the end of a block
462 nir_handle_add_jump(nir_block
*block
)
464 nir_instr
*instr
= nir_block_last_instr(block
);
465 nir_jump_instr
*jump_instr
= nir_instr_as_jump(instr
);
467 if (block
->successors
[0])
468 remove_phi_src(block
->successors
[0], block
);
469 if (block
->successors
[1])
470 remove_phi_src(block
->successors
[1], block
);
471 unlink_block_successors(block
);
473 nir_function_impl
*impl
= nir_cf_node_get_function(&block
->cf_node
);
474 nir_metadata_preserve(impl
, nir_metadata_none
);
476 if (jump_instr
->type
== nir_jump_break
||
477 jump_instr
->type
== nir_jump_continue
) {
478 nir_loop
*loop
= nearest_loop(&block
->cf_node
);
480 if (jump_instr
->type
== nir_jump_continue
) {
481 nir_block
*first_block
= nir_loop_first_block(loop
);
482 link_blocks(block
, first_block
, NULL
);
484 nir_cf_node
*after
= nir_cf_node_next(&loop
->cf_node
);
485 nir_block
*after_block
= nir_cf_node_as_block(after
);
486 link_blocks(block
, after_block
, NULL
);
489 assert(jump_instr
->type
== nir_jump_return
);
490 link_blocks(block
, impl
->end_block
, NULL
);
494 /* Removes the successor of a block with a jump. Note that the jump to be
495 * eliminated may be free-floating.
499 unlink_jump(nir_block
*block
, nir_jump_type type
, bool add_normal_successors
)
501 if (block
->successors
[0])
502 remove_phi_src(block
->successors
[0], block
);
503 if (block
->successors
[1])
504 remove_phi_src(block
->successors
[1], block
);
506 unlink_block_successors(block
);
507 if (add_normal_successors
)
508 block_add_normal_succs(block
);
512 nir_handle_remove_jump(nir_block
*block
, nir_jump_type type
)
514 unlink_jump(block
, type
, true);
516 nir_function_impl
*impl
= nir_cf_node_get_function(&block
->cf_node
);
517 nir_metadata_preserve(impl
, nir_metadata_none
);
521 update_if_uses(nir_cf_node
*node
)
523 if (node
->type
!= nir_cf_node_if
)
526 nir_if
*if_stmt
= nir_cf_node_as_if(node
);
528 if_stmt
->condition
.parent_if
= if_stmt
;
529 if (if_stmt
->condition
.is_ssa
) {
530 list_addtail(&if_stmt
->condition
.use_link
,
531 &if_stmt
->condition
.ssa
->if_uses
);
533 list_addtail(&if_stmt
->condition
.use_link
,
534 &if_stmt
->condition
.reg
.reg
->if_uses
);
539 * Stitch two basic blocks together into one. The aggregate must have the same
540 * predecessors as the first and the same successors as the second.
544 stitch_blocks(nir_block
*before
, nir_block
*after
)
547 * We move after into before, so we have to deal with up to 2 successors vs.
548 * possibly a large number of predecessors.
550 * TODO: special case when before is empty and after isn't?
553 if (nir_block_ends_in_jump(before
)) {
554 assert(exec_list_is_empty(&after
->instr_list
));
555 if (after
->successors
[0])
556 remove_phi_src(after
->successors
[0], after
);
557 if (after
->successors
[1])
558 remove_phi_src(after
->successors
[1], after
);
559 unlink_block_successors(after
);
560 exec_node_remove(&after
->cf_node
.node
);
562 move_successors(after
, before
);
564 foreach_list_typed(nir_instr
, instr
, node
, &after
->instr_list
) {
565 instr
->block
= before
;
568 exec_list_append(&before
->instr_list
, &after
->instr_list
);
569 exec_node_remove(&after
->cf_node
.node
);
574 nir_cf_node_insert(nir_cursor cursor
, nir_cf_node
*node
)
576 nir_block
*before
, *after
;
578 split_block_cursor(cursor
, &before
, &after
);
580 if (node
->type
== nir_cf_node_block
) {
581 nir_block
*block
= nir_cf_node_as_block(node
);
582 exec_node_insert_after(&before
->cf_node
.node
, &block
->cf_node
.node
);
583 block
->cf_node
.parent
= before
->cf_node
.parent
;
584 /* stitch_blocks() assumes that any block that ends with a jump has
585 * already been setup with the correct successors, so we need to set
586 * up jumps here as the block is being inserted.
588 if (nir_block_ends_in_jump(block
))
589 nir_handle_add_jump(block
);
591 stitch_blocks(block
, after
);
592 stitch_blocks(before
, block
);
594 update_if_uses(node
);
595 insert_non_block(before
, node
, after
);
600 replace_ssa_def_uses(nir_ssa_def
*def
, void *void_impl
)
602 nir_function_impl
*impl
= void_impl
;
603 void *mem_ctx
= ralloc_parent(impl
);
605 nir_ssa_undef_instr
*undef
=
606 nir_ssa_undef_instr_create(mem_ctx
, def
->num_components
,
608 nir_instr_insert_before_cf_list(&impl
->body
, &undef
->instr
);
609 nir_ssa_def_rewrite_uses(def
, nir_src_for_ssa(&undef
->def
));
614 cleanup_cf_node(nir_cf_node
*node
, nir_function_impl
*impl
)
616 switch (node
->type
) {
617 case nir_cf_node_block
: {
618 nir_block
*block
= nir_cf_node_as_block(node
);
619 /* We need to walk the instructions and clean up defs/uses */
620 nir_foreach_instr_safe(instr
, block
) {
621 if (instr
->type
== nir_instr_type_jump
) {
622 nir_jump_type jump_type
= nir_instr_as_jump(instr
)->type
;
623 unlink_jump(block
, jump_type
, false);
625 nir_foreach_ssa_def(instr
, replace_ssa_def_uses
, impl
);
626 nir_instr_remove(instr
);
632 case nir_cf_node_if
: {
633 nir_if
*if_stmt
= nir_cf_node_as_if(node
);
634 foreach_list_typed(nir_cf_node
, child
, node
, &if_stmt
->then_list
)
635 cleanup_cf_node(child
, impl
);
636 foreach_list_typed(nir_cf_node
, child
, node
, &if_stmt
->else_list
)
637 cleanup_cf_node(child
, impl
);
639 list_del(&if_stmt
->condition
.use_link
);
643 case nir_cf_node_loop
: {
644 nir_loop
*loop
= nir_cf_node_as_loop(node
);
645 foreach_list_typed(nir_cf_node
, child
, node
, &loop
->body
)
646 cleanup_cf_node(child
, impl
);
649 case nir_cf_node_function
: {
650 nir_function_impl
*impl
= nir_cf_node_as_function(node
);
651 foreach_list_typed(nir_cf_node
, child
, node
, &impl
->body
)
652 cleanup_cf_node(child
, impl
);
656 unreachable("Invalid CF node type");
661 nir_cf_extract(nir_cf_list
*extracted
, nir_cursor begin
, nir_cursor end
)
663 nir_block
*block_begin
, *block_end
, *block_before
, *block_after
;
665 if (nir_cursors_equal(begin
, end
)) {
666 exec_list_make_empty(&extracted
->list
);
667 extracted
->impl
= NULL
; /* we shouldn't need this */
671 /* In the case where begin points to an instruction in some basic block and
672 * end points to the end of the same basic block, we rely on the fact that
673 * splitting on an instruction moves earlier instructions into a new basic
674 * block. If the later instructions were moved instead, then the end cursor
675 * would be pointing to the same place that begin used to point to, which
676 * is obviously not what we want.
678 split_block_cursor(begin
, &block_before
, &block_begin
);
679 split_block_cursor(end
, &block_end
, &block_after
);
681 extracted
->impl
= nir_cf_node_get_function(&block_begin
->cf_node
);
682 exec_list_make_empty(&extracted
->list
);
684 /* Dominance and other block-related information is toast. */
685 nir_metadata_preserve(extracted
->impl
, nir_metadata_none
);
687 nir_cf_node
*cf_node
= &block_begin
->cf_node
;
688 nir_cf_node
*cf_node_end
= &block_end
->cf_node
;
690 nir_cf_node
*next
= nir_cf_node_next(cf_node
);
692 exec_node_remove(&cf_node
->node
);
693 cf_node
->parent
= NULL
;
694 exec_list_push_tail(&extracted
->list
, &cf_node
->node
);
696 if (cf_node
== cf_node_end
)
702 stitch_blocks(block_before
, block_after
);
706 nir_cf_reinsert(nir_cf_list
*cf_list
, nir_cursor cursor
)
708 nir_block
*before
, *after
;
710 if (exec_list_is_empty(&cf_list
->list
))
713 split_block_cursor(cursor
, &before
, &after
);
715 foreach_list_typed_safe(nir_cf_node
, node
, node
, &cf_list
->list
) {
716 exec_node_remove(&node
->node
);
717 node
->parent
= before
->cf_node
.parent
;
718 exec_node_insert_node_before(&after
->cf_node
.node
, &node
->node
);
721 stitch_blocks(before
,
722 nir_cf_node_as_block(nir_cf_node_next(&before
->cf_node
)));
723 stitch_blocks(nir_cf_node_as_block(nir_cf_node_prev(&after
->cf_node
)),
728 nir_cf_delete(nir_cf_list
*cf_list
)
730 foreach_list_typed(nir_cf_node
, node
, node
, &cf_list
->list
) {
731 cleanup_cf_node(node
, cf_list
->impl
);