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