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