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_ends_in_jump(nir_block
*block
)
51 return !exec_list_is_empty(&block
->instr_list
) &&
52 nir_block_last_instr(block
)->type
== nir_instr_type_jump
;
56 block_add_pred(nir_block
*block
, nir_block
*pred
)
58 _mesa_set_add(block
->predecessors
, pred
);
62 block_remove_pred(nir_block
*block
, nir_block
*pred
)
64 struct set_entry
*entry
= _mesa_set_search(block
->predecessors
, pred
);
68 _mesa_set_remove(block
->predecessors
, entry
);
72 link_blocks(nir_block
*pred
, nir_block
*succ1
, nir_block
*succ2
)
74 pred
->successors
[0] = succ1
;
76 block_add_pred(succ1
, pred
);
78 pred
->successors
[1] = succ2
;
80 block_add_pred(succ2
, pred
);
84 unlink_blocks(nir_block
*pred
, nir_block
*succ
)
86 if (pred
->successors
[0] == succ
) {
87 pred
->successors
[0] = pred
->successors
[1];
88 pred
->successors
[1] = NULL
;
90 assert(pred
->successors
[1] == succ
);
91 pred
->successors
[1] = NULL
;
94 block_remove_pred(succ
, pred
);
98 unlink_block_successors(nir_block
*block
)
100 if (block
->successors
[1] != NULL
)
101 unlink_blocks(block
, block
->successors
[1]);
102 if (block
->successors
[0] != NULL
)
103 unlink_blocks(block
, block
->successors
[0]);
107 link_non_block_to_block(nir_cf_node
*node
, nir_block
*block
)
109 if (node
->type
== nir_cf_node_if
) {
111 * We're trying to link an if to a block after it; this just means linking
112 * the last block of the then and else branches.
115 nir_if
*if_stmt
= nir_cf_node_as_if(node
);
117 nir_block
*last_then_block
= nir_if_last_then_block(if_stmt
);
118 nir_block
*last_else_block
= nir_if_last_else_block(if_stmt
);
120 if (!block_ends_in_jump(last_then_block
)) {
121 unlink_block_successors(last_then_block
);
122 link_blocks(last_then_block
, block
, NULL
);
125 if (!block_ends_in_jump(last_else_block
)) {
126 unlink_block_successors(last_else_block
);
127 link_blocks(last_else_block
, block
, NULL
);
130 assert(node
->type
== nir_cf_node_loop
);
135 link_block_to_non_block(nir_block
*block
, nir_cf_node
*node
)
137 if (node
->type
== nir_cf_node_if
) {
139 * We're trying to link a block to an if after it; this just means linking
140 * the block to the first block of the then and else branches.
143 nir_if
*if_stmt
= nir_cf_node_as_if(node
);
145 nir_block
*first_then_block
= nir_if_first_then_block(if_stmt
);
146 nir_block
*first_else_block
= nir_if_first_else_block(if_stmt
);
148 unlink_block_successors(block
);
149 link_blocks(block
, first_then_block
, first_else_block
);
152 * For similar reasons as the corresponding case in
153 * link_non_block_to_block(), don't worry about if the loop header has
154 * any predecessors that need to be unlinked.
157 assert(node
->type
== nir_cf_node_loop
);
159 nir_loop
*loop
= nir_cf_node_as_loop(node
);
161 nir_block
*loop_header_block
= nir_loop_first_block(loop
);
163 unlink_block_successors(block
);
164 link_blocks(block
, loop_header_block
, NULL
);
170 * Replace a block's successor with a different one.
173 replace_successor(nir_block
*block
, nir_block
*old_succ
, nir_block
*new_succ
)
175 if (block
->successors
[0] == old_succ
) {
176 block
->successors
[0] = new_succ
;
178 assert(block
->successors
[1] == old_succ
);
179 block
->successors
[1] = new_succ
;
182 block_remove_pred(old_succ
, block
);
183 block_add_pred(new_succ
, block
);
187 * Takes a basic block and inserts a new empty basic block before it, making its
188 * predecessors point to the new block. This essentially splits the block into
189 * an empty header and a body so that another non-block CF node can be inserted
190 * between the two. Note that this does *not* link the two basic blocks, so
191 * some kind of cleanup *must* be performed after this call.
195 split_block_beginning(nir_block
*block
)
197 nir_block
*new_block
= nir_block_create(ralloc_parent(block
));
198 new_block
->cf_node
.parent
= block
->cf_node
.parent
;
199 exec_node_insert_node_before(&block
->cf_node
.node
, &new_block
->cf_node
.node
);
201 struct set_entry
*entry
;
202 set_foreach(block
->predecessors
, entry
) {
203 nir_block
*pred
= (nir_block
*) entry
->key
;
204 replace_successor(pred
, block
, new_block
);
207 /* Any phi nodes must stay part of the new block, or else their
208 * sourcse will be messed up. This will reverse the order of the phi's, but
209 * order shouldn't matter.
211 nir_foreach_instr_safe(instr
, block
) {
212 if (instr
->type
!= nir_instr_type_phi
)
215 exec_node_remove(&instr
->node
);
216 instr
->block
= new_block
;
217 exec_list_push_head(&new_block
->instr_list
, &instr
->node
);
224 rewrite_phi_preds(nir_block
*block
, nir_block
*old_pred
, nir_block
*new_pred
)
226 nir_foreach_instr_safe(instr
, block
) {
227 if (instr
->type
!= nir_instr_type_phi
)
230 nir_phi_instr
*phi
= nir_instr_as_phi(instr
);
231 nir_foreach_phi_src(src
, phi
) {
232 if (src
->pred
== old_pred
) {
233 src
->pred
= new_pred
;
241 insert_phi_undef(nir_block
*block
, nir_block
*pred
)
243 nir_function_impl
*impl
= nir_cf_node_get_function(&block
->cf_node
);
244 nir_foreach_instr(instr
, block
) {
245 if (instr
->type
!= nir_instr_type_phi
)
248 nir_phi_instr
*phi
= nir_instr_as_phi(instr
);
249 nir_ssa_undef_instr
*undef
=
250 nir_ssa_undef_instr_create(ralloc_parent(phi
),
251 phi
->dest
.ssa
.num_components
,
252 phi
->dest
.ssa
.bit_size
);
253 nir_instr_insert_before_cf_list(&impl
->body
, &undef
->instr
);
254 nir_phi_src
*src
= ralloc(phi
, nir_phi_src
);
256 src
->src
.parent_instr
= &phi
->instr
;
257 src
->src
.is_ssa
= true;
258 src
->src
.ssa
= &undef
->def
;
260 list_addtail(&src
->src
.use_link
, &undef
->def
.uses
);
262 exec_list_push_tail(&phi
->srcs
, &src
->node
);
267 * Moves the successors of source to the successors of dest, leaving both
268 * successors of source NULL.
272 move_successors(nir_block
*source
, nir_block
*dest
)
274 nir_block
*succ1
= source
->successors
[0];
275 nir_block
*succ2
= source
->successors
[1];
278 unlink_blocks(source
, succ1
);
279 rewrite_phi_preds(succ1
, source
, dest
);
283 unlink_blocks(source
, succ2
);
284 rewrite_phi_preds(succ2
, source
, dest
);
287 unlink_block_successors(dest
);
288 link_blocks(dest
, succ1
, succ2
);
291 /* Given a basic block with no successors that has been inserted into the
292 * control flow tree, gives it the successors it would normally have assuming
293 * it doesn't end in a jump instruction. Also inserts phi sources with undefs
297 block_add_normal_succs(nir_block
*block
)
299 if (exec_node_is_tail_sentinel(block
->cf_node
.node
.next
)) {
300 nir_cf_node
*parent
= block
->cf_node
.parent
;
301 if (parent
->type
== nir_cf_node_if
) {
302 nir_cf_node
*next
= nir_cf_node_next(parent
);
303 assert(next
->type
== nir_cf_node_block
);
304 nir_block
*next_block
= nir_cf_node_as_block(next
);
306 link_blocks(block
, next_block
, NULL
);
307 } else if (parent
->type
== nir_cf_node_loop
) {
308 nir_loop
*loop
= nir_cf_node_as_loop(parent
);
310 nir_block
*head_block
= nir_loop_first_block(loop
);
312 link_blocks(block
, head_block
, NULL
);
313 insert_phi_undef(head_block
, block
);
315 assert(parent
->type
== nir_cf_node_function
);
316 nir_function_impl
*impl
= nir_cf_node_as_function(parent
);
317 link_blocks(block
, impl
->end_block
, NULL
);
320 nir_cf_node
*next
= nir_cf_node_next(&block
->cf_node
);
321 if (next
->type
== nir_cf_node_if
) {
322 nir_if
*next_if
= nir_cf_node_as_if(next
);
324 nir_block
*first_then_block
= nir_if_first_then_block(next_if
);
325 nir_block
*first_else_block
= nir_if_first_else_block(next_if
);
327 link_blocks(block
, first_then_block
, first_else_block
);
329 assert(next
->type
== nir_cf_node_loop
);
330 nir_loop
*next_loop
= nir_cf_node_as_loop(next
);
332 nir_block
*first_block
= nir_loop_first_block(next_loop
);
334 link_blocks(block
, first_block
, NULL
);
335 insert_phi_undef(first_block
, block
);
341 split_block_end(nir_block
*block
)
343 nir_block
*new_block
= nir_block_create(ralloc_parent(block
));
344 new_block
->cf_node
.parent
= block
->cf_node
.parent
;
345 exec_node_insert_after(&block
->cf_node
.node
, &new_block
->cf_node
.node
);
347 if (block_ends_in_jump(block
)) {
348 /* Figure out what successor block would've had if it didn't have a jump
349 * instruction, and make new_block have that successor.
351 block_add_normal_succs(new_block
);
353 move_successors(block
, new_block
);
360 split_block_before_instr(nir_instr
*instr
)
362 assert(instr
->type
!= nir_instr_type_phi
);
363 nir_block
*new_block
= split_block_beginning(instr
->block
);
365 nir_foreach_instr_safe(cur_instr
, instr
->block
) {
366 if (cur_instr
== instr
)
369 exec_node_remove(&cur_instr
->node
);
370 cur_instr
->block
= new_block
;
371 exec_list_push_tail(&new_block
->instr_list
, &cur_instr
->node
);
377 /* Splits a basic block at the point specified by the cursor. The "before" and
378 * "after" arguments are filled out with the blocks resulting from the split
379 * if non-NULL. Note that the "beginning" of the block is actually interpreted
380 * as before the first non-phi instruction, and it's illegal to split a block
381 * before a phi instruction.
385 split_block_cursor(nir_cursor cursor
,
386 nir_block
**_before
, nir_block
**_after
)
388 nir_block
*before
, *after
;
389 switch (cursor
.option
) {
390 case nir_cursor_before_block
:
391 after
= cursor
.block
;
392 before
= split_block_beginning(cursor
.block
);
395 case nir_cursor_after_block
:
396 before
= cursor
.block
;
397 after
= split_block_end(cursor
.block
);
400 case nir_cursor_before_instr
:
401 after
= cursor
.instr
->block
;
402 before
= split_block_before_instr(cursor
.instr
);
405 case nir_cursor_after_instr
:
406 /* We lower this to split_block_before_instr() so that we can keep the
407 * after-a-jump-instr case contained to split_block_end().
409 if (nir_instr_is_last(cursor
.instr
)) {
410 before
= cursor
.instr
->block
;
411 after
= split_block_end(cursor
.instr
->block
);
413 after
= cursor
.instr
->block
;
414 before
= split_block_before_instr(nir_instr_next(cursor
.instr
));
419 unreachable("not reached");
429 * Inserts a non-basic block between two basic blocks and links them together.
433 insert_non_block(nir_block
*before
, nir_cf_node
*node
, nir_block
*after
)
435 node
->parent
= before
->cf_node
.parent
;
436 exec_node_insert_after(&before
->cf_node
.node
, &node
->node
);
437 link_block_to_non_block(before
, node
);
438 link_non_block_to_block(node
, after
);
441 /* walk up the control flow tree to find the innermost enclosed loop */
443 nearest_loop(nir_cf_node
*node
)
445 while (node
->type
!= nir_cf_node_loop
) {
449 return nir_cf_node_as_loop(node
);
453 * update the CFG after a jump instruction has been added to the end of a block
457 nir_handle_add_jump(nir_block
*block
)
459 nir_instr
*instr
= nir_block_last_instr(block
);
460 nir_jump_instr
*jump_instr
= nir_instr_as_jump(instr
);
462 unlink_block_successors(block
);
464 nir_function_impl
*impl
= nir_cf_node_get_function(&block
->cf_node
);
465 nir_metadata_preserve(impl
, nir_metadata_none
);
467 if (jump_instr
->type
== nir_jump_break
||
468 jump_instr
->type
== nir_jump_continue
) {
469 nir_loop
*loop
= nearest_loop(&block
->cf_node
);
471 if (jump_instr
->type
== nir_jump_continue
) {
472 nir_block
*first_block
= nir_loop_first_block(loop
);
473 link_blocks(block
, first_block
, NULL
);
475 nir_cf_node
*after
= nir_cf_node_next(&loop
->cf_node
);
476 assert(after
->type
== nir_cf_node_block
);
477 nir_block
*after_block
= nir_cf_node_as_block(after
);
478 link_blocks(block
, after_block
, NULL
);
481 assert(jump_instr
->type
== nir_jump_return
);
482 link_blocks(block
, impl
->end_block
, NULL
);
487 remove_phi_src(nir_block
*block
, nir_block
*pred
)
489 nir_foreach_instr(instr
, block
) {
490 if (instr
->type
!= nir_instr_type_phi
)
493 nir_phi_instr
*phi
= nir_instr_as_phi(instr
);
494 nir_foreach_phi_src_safe(src
, phi
) {
495 if (src
->pred
== pred
) {
496 list_del(&src
->src
.use_link
);
497 exec_node_remove(&src
->node
);
503 /* Removes the successor of a block with a jump. Note that the jump to be
504 * eliminated may be free-floating.
508 unlink_jump(nir_block
*block
, nir_jump_type type
, bool add_normal_successors
)
510 if (block
->successors
[0])
511 remove_phi_src(block
->successors
[0], block
);
512 if (block
->successors
[1])
513 remove_phi_src(block
->successors
[1], block
);
515 unlink_block_successors(block
);
516 if (add_normal_successors
)
517 block_add_normal_succs(block
);
521 nir_handle_remove_jump(nir_block
*block
, nir_jump_type type
)
523 unlink_jump(block
, type
, true);
525 nir_function_impl
*impl
= nir_cf_node_get_function(&block
->cf_node
);
526 nir_metadata_preserve(impl
, nir_metadata_none
);
530 update_if_uses(nir_cf_node
*node
)
532 if (node
->type
!= nir_cf_node_if
)
535 nir_if
*if_stmt
= nir_cf_node_as_if(node
);
537 if_stmt
->condition
.parent_if
= if_stmt
;
538 if (if_stmt
->condition
.is_ssa
) {
539 list_addtail(&if_stmt
->condition
.use_link
,
540 &if_stmt
->condition
.ssa
->if_uses
);
542 list_addtail(&if_stmt
->condition
.use_link
,
543 &if_stmt
->condition
.reg
.reg
->if_uses
);
548 * Stitch two basic blocks together into one. The aggregate must have the same
549 * predecessors as the first and the same successors as the second.
553 stitch_blocks(nir_block
*before
, nir_block
*after
)
556 * We move after into before, so we have to deal with up to 2 successors vs.
557 * possibly a large number of predecessors.
559 * TODO: special case when before is empty and after isn't?
562 if (block_ends_in_jump(before
)) {
563 assert(exec_list_is_empty(&after
->instr_list
));
564 if (after
->successors
[0])
565 remove_phi_src(after
->successors
[0], after
);
566 if (after
->successors
[1])
567 remove_phi_src(after
->successors
[1], after
);
568 unlink_block_successors(after
);
569 exec_node_remove(&after
->cf_node
.node
);
571 move_successors(after
, before
);
573 foreach_list_typed(nir_instr
, instr
, node
, &after
->instr_list
) {
574 instr
->block
= before
;
577 exec_list_append(&before
->instr_list
, &after
->instr_list
);
578 exec_node_remove(&after
->cf_node
.node
);
583 nir_cf_node_insert(nir_cursor cursor
, nir_cf_node
*node
)
585 nir_block
*before
, *after
;
587 split_block_cursor(cursor
, &before
, &after
);
589 if (node
->type
== nir_cf_node_block
) {
590 nir_block
*block
= nir_cf_node_as_block(node
);
591 exec_node_insert_after(&before
->cf_node
.node
, &block
->cf_node
.node
);
592 block
->cf_node
.parent
= before
->cf_node
.parent
;
593 /* stitch_blocks() assumes that any block that ends with a jump has
594 * already been setup with the correct successors, so we need to set
595 * up jumps here as the block is being inserted.
597 if (block_ends_in_jump(block
))
598 nir_handle_add_jump(block
);
600 stitch_blocks(block
, after
);
601 stitch_blocks(before
, block
);
603 update_if_uses(node
);
604 insert_non_block(before
, node
, after
);
609 replace_ssa_def_uses(nir_ssa_def
*def
, void *void_impl
)
611 nir_function_impl
*impl
= void_impl
;
612 void *mem_ctx
= ralloc_parent(impl
);
614 nir_ssa_undef_instr
*undef
=
615 nir_ssa_undef_instr_create(mem_ctx
, def
->num_components
,
617 nir_instr_insert_before_cf_list(&impl
->body
, &undef
->instr
);
618 nir_ssa_def_rewrite_uses(def
, nir_src_for_ssa(&undef
->def
));
623 cleanup_cf_node(nir_cf_node
*node
, nir_function_impl
*impl
)
625 switch (node
->type
) {
626 case nir_cf_node_block
: {
627 nir_block
*block
= nir_cf_node_as_block(node
);
628 /* We need to walk the instructions and clean up defs/uses */
629 nir_foreach_instr_safe(instr
, block
) {
630 if (instr
->type
== nir_instr_type_jump
) {
631 nir_jump_type jump_type
= nir_instr_as_jump(instr
)->type
;
632 unlink_jump(block
, jump_type
, false);
634 nir_foreach_ssa_def(instr
, replace_ssa_def_uses
, impl
);
635 nir_instr_remove(instr
);
641 case nir_cf_node_if
: {
642 nir_if
*if_stmt
= nir_cf_node_as_if(node
);
643 foreach_list_typed(nir_cf_node
, child
, node
, &if_stmt
->then_list
)
644 cleanup_cf_node(child
, impl
);
645 foreach_list_typed(nir_cf_node
, child
, node
, &if_stmt
->else_list
)
646 cleanup_cf_node(child
, impl
);
648 list_del(&if_stmt
->condition
.use_link
);
652 case nir_cf_node_loop
: {
653 nir_loop
*loop
= nir_cf_node_as_loop(node
);
654 foreach_list_typed(nir_cf_node
, child
, node
, &loop
->body
)
655 cleanup_cf_node(child
, impl
);
658 case nir_cf_node_function
: {
659 nir_function_impl
*impl
= nir_cf_node_as_function(node
);
660 foreach_list_typed(nir_cf_node
, child
, node
, &impl
->body
)
661 cleanup_cf_node(child
, impl
);
665 unreachable("Invalid CF node type");
670 nir_cf_extract(nir_cf_list
*extracted
, nir_cursor begin
, nir_cursor end
)
672 nir_block
*block_begin
, *block_end
, *block_before
, *block_after
;
674 if (nir_cursors_equal(begin
, end
)) {
675 exec_list_make_empty(&extracted
->list
);
676 extracted
->impl
= NULL
; /* we shouldn't need this */
680 /* In the case where begin points to an instruction in some basic block and
681 * end points to the end of the same basic block, we rely on the fact that
682 * splitting on an instruction moves earlier instructions into a new basic
683 * block. If the later instructions were moved instead, then the end cursor
684 * would be pointing to the same place that begin used to point to, which
685 * is obviously not what we want.
687 split_block_cursor(begin
, &block_before
, &block_begin
);
688 split_block_cursor(end
, &block_end
, &block_after
);
690 extracted
->impl
= nir_cf_node_get_function(&block_begin
->cf_node
);
691 exec_list_make_empty(&extracted
->list
);
693 /* Dominance and other block-related information is toast. */
694 nir_metadata_preserve(extracted
->impl
, nir_metadata_none
);
696 nir_cf_node
*cf_node
= &block_begin
->cf_node
;
697 nir_cf_node
*cf_node_end
= &block_end
->cf_node
;
699 nir_cf_node
*next
= nir_cf_node_next(cf_node
);
701 exec_node_remove(&cf_node
->node
);
702 cf_node
->parent
= NULL
;
703 exec_list_push_tail(&extracted
->list
, &cf_node
->node
);
705 if (cf_node
== cf_node_end
)
711 stitch_blocks(block_before
, block_after
);
715 nir_cf_reinsert(nir_cf_list
*cf_list
, nir_cursor cursor
)
717 nir_block
*before
, *after
;
719 if (exec_list_is_empty(&cf_list
->list
))
722 split_block_cursor(cursor
, &before
, &after
);
724 foreach_list_typed_safe(nir_cf_node
, node
, node
, &cf_list
->list
) {
725 exec_node_remove(&node
->node
);
726 node
->parent
= before
->cf_node
.parent
;
727 exec_node_insert_node_before(&after
->cf_node
.node
, &node
->node
);
730 stitch_blocks(before
,
731 nir_cf_node_as_block(nir_cf_node_next(&before
->cf_node
)));
732 stitch_blocks(nir_cf_node_as_block(nir_cf_node_prev(&after
->cf_node
)),
737 nir_cf_delete(nir_cf_list
*cf_list
)
739 foreach_list_typed(nir_cf_node
, node
, node
, &cf_list
->list
) {
740 cleanup_cf_node(node
, cf_list
->impl
);