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
block_add_pred(nir_block * block,nir_block * pred)49 block_add_pred(nir_block *block, nir_block *pred)
50 {
51    _mesa_set_add(block->predecessors, pred);
52 }
53 
54 static inline void
block_remove_pred(nir_block * block,nir_block * pred)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
link_blocks(nir_block * pred,nir_block * succ1,nir_block * succ2)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
unlink_blocks(nir_block * pred,nir_block * succ)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
unlink_block_successors(nir_block * block)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
link_non_block_to_block(nir_cf_node * node,nir_block * block)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
link_block_to_non_block(nir_block * block,nir_cf_node * node)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 if (node->type == nir_cf_node_loop) {
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
replace_successor(nir_block * block,nir_block * old_succ,nir_block * new_succ)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 *
split_block_beginning(nir_block * 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     * sources will be messed up.
199     */
200    nir_foreach_instr_safe(instr, block) {
201       if (instr->type != nir_instr_type_phi)
202          break;
203 
204       exec_node_remove(&instr->node);
205       instr->block = new_block;
206       exec_list_push_tail(&new_block->instr_list, &instr->node);
207    }
208 
209    return new_block;
210 }
211 
212 static void
rewrite_phi_preds(nir_block * block,nir_block * old_pred,nir_block * new_pred)213 rewrite_phi_preds(nir_block *block, nir_block *old_pred, nir_block *new_pred)
214 {
215    nir_foreach_instr_safe(instr, block) {
216       if (instr->type != nir_instr_type_phi)
217          break;
218 
219       nir_phi_instr *phi = nir_instr_as_phi(instr);
220       nir_foreach_phi_src(src, phi) {
221          if (src->pred == old_pred) {
222             src->pred = new_pred;
223             break;
224          }
225       }
226    }
227 }
228 
229 void
nir_insert_phi_undef(nir_block * block,nir_block * pred)230 nir_insert_phi_undef(nir_block *block, nir_block *pred)
231 {
232    nir_function_impl *impl = nir_cf_node_get_function(&block->cf_node);
233    nir_foreach_instr(instr, block) {
234       if (instr->type != nir_instr_type_phi)
235          break;
236 
237       nir_phi_instr *phi = nir_instr_as_phi(instr);
238       nir_ssa_undef_instr *undef =
239          nir_ssa_undef_instr_create(impl->function->shader,
240                                     phi->dest.ssa.num_components,
241                                     phi->dest.ssa.bit_size);
242       nir_instr_insert_before_cf_list(&impl->body, &undef->instr);
243       nir_phi_src *src = nir_phi_instr_add_src(phi, pred, nir_src_for_ssa(&undef->def));
244       list_addtail(&src->src.use_link, &undef->def.uses);
245    }
246 }
247 
248 /**
249  * Moves the successors of source to the successors of dest, leaving both
250  * successors of source NULL.
251  */
252 
253 static void
move_successors(nir_block * source,nir_block * dest)254 move_successors(nir_block *source, nir_block *dest)
255 {
256    nir_block *succ1 = source->successors[0];
257    nir_block *succ2 = source->successors[1];
258 
259    if (succ1) {
260       unlink_blocks(source, succ1);
261       rewrite_phi_preds(succ1, source, dest);
262    }
263 
264    if (succ2) {
265       unlink_blocks(source, succ2);
266       rewrite_phi_preds(succ2, source, dest);
267    }
268 
269    unlink_block_successors(dest);
270    link_blocks(dest, succ1, succ2);
271 }
272 
273 /* Given a basic block with no successors that has been inserted into the
274  * control flow tree, gives it the successors it would normally have assuming
275  * it doesn't end in a jump instruction. Also inserts phi sources with undefs
276  * if necessary.
277  */
278 static void
block_add_normal_succs(nir_block * block)279 block_add_normal_succs(nir_block *block)
280 {
281    if (exec_node_is_tail_sentinel(block->cf_node.node.next)) {
282       nir_cf_node *parent = block->cf_node.parent;
283       if (parent->type == nir_cf_node_if) {
284          nir_cf_node *next = nir_cf_node_next(parent);
285          nir_block *next_block = nir_cf_node_as_block(next);
286 
287          link_blocks(block, next_block, NULL);
288       } else if (parent->type == nir_cf_node_loop) {
289          nir_loop *loop = nir_cf_node_as_loop(parent);
290 
291          nir_block *head_block = nir_loop_first_block(loop);
292 
293          link_blocks(block, head_block, NULL);
294          nir_insert_phi_undef(head_block, block);
295       } else {
296          nir_function_impl *impl = nir_cf_node_as_function(parent);
297          link_blocks(block, impl->end_block, NULL);
298       }
299    } else {
300       nir_cf_node *next = nir_cf_node_next(&block->cf_node);
301       if (next->type == nir_cf_node_if) {
302          nir_if *next_if = nir_cf_node_as_if(next);
303 
304          nir_block *first_then_block = nir_if_first_then_block(next_if);
305          nir_block *first_else_block = nir_if_first_else_block(next_if);
306 
307          link_blocks(block, first_then_block, first_else_block);
308       } else if (next->type == nir_cf_node_loop) {
309          nir_loop *next_loop = nir_cf_node_as_loop(next);
310 
311          nir_block *first_block = nir_loop_first_block(next_loop);
312 
313          link_blocks(block, first_block, NULL);
314          nir_insert_phi_undef(first_block, block);
315       }
316    }
317 }
318 
319 static nir_block *
split_block_end(nir_block * block)320 split_block_end(nir_block *block)
321 {
322    nir_block *new_block = nir_block_create(ralloc_parent(block));
323    new_block->cf_node.parent = block->cf_node.parent;
324    exec_node_insert_after(&block->cf_node.node, &new_block->cf_node.node);
325 
326    if (nir_block_ends_in_jump(block)) {
327       /* Figure out what successor block would've had if it didn't have a jump
328        * instruction, and make new_block have that successor.
329        */
330       block_add_normal_succs(new_block);
331    } else {
332       move_successors(block, new_block);
333    }
334 
335    return new_block;
336 }
337 
338 static nir_block *
split_block_before_instr(nir_instr * instr)339 split_block_before_instr(nir_instr *instr)
340 {
341    assert(instr->type != nir_instr_type_phi);
342    nir_block *new_block = split_block_beginning(instr->block);
343 
344    nir_foreach_instr_safe(cur_instr, instr->block) {
345       if (cur_instr == instr)
346          break;
347 
348       exec_node_remove(&cur_instr->node);
349       cur_instr->block = new_block;
350       exec_list_push_tail(&new_block->instr_list, &cur_instr->node);
351    }
352 
353    return new_block;
354 }
355 
356 /* Splits a basic block at the point specified by the cursor. The "before" and
357  * "after" arguments are filled out with the blocks resulting from the split
358  * if non-NULL. Note that the "beginning" of the block is actually interpreted
359  * as before the first non-phi instruction, and it's illegal to split a block
360  * before a phi instruction.
361  */
362 
363 static void
split_block_cursor(nir_cursor cursor,nir_block ** _before,nir_block ** _after)364 split_block_cursor(nir_cursor cursor,
365                    nir_block **_before, nir_block **_after)
366 {
367    nir_block *before, *after;
368    switch (cursor.option) {
369    case nir_cursor_before_block:
370       after = cursor.block;
371       before = split_block_beginning(cursor.block);
372       break;
373 
374    case nir_cursor_after_block:
375       before = cursor.block;
376       after = split_block_end(cursor.block);
377       break;
378 
379    case nir_cursor_before_instr:
380       after = cursor.instr->block;
381       before = split_block_before_instr(cursor.instr);
382       break;
383 
384    case nir_cursor_after_instr:
385       /* We lower this to split_block_before_instr() so that we can keep the
386        * after-a-jump-instr case contained to split_block_end().
387        */
388       if (nir_instr_is_last(cursor.instr)) {
389          before = cursor.instr->block;
390          after = split_block_end(cursor.instr->block);
391       } else {
392          after = cursor.instr->block;
393          before = split_block_before_instr(nir_instr_next(cursor.instr));
394       }
395       break;
396 
397    default:
398       unreachable("not reached");
399    }
400 
401    if (_before)
402       *_before = before;
403    if (_after)
404       *_after = after;
405 }
406 
407 /**
408  * Inserts a non-basic block between two basic blocks and links them together.
409  */
410 
411 static void
insert_non_block(nir_block * before,nir_cf_node * node,nir_block * after)412 insert_non_block(nir_block *before, nir_cf_node *node, nir_block *after)
413 {
414    node->parent = before->cf_node.parent;
415    exec_node_insert_after(&before->cf_node.node, &node->node);
416    link_block_to_non_block(before, node);
417    link_non_block_to_block(node, after);
418 }
419 
420 /* walk up the control flow tree to find the innermost enclosed loop */
421 static nir_loop *
nearest_loop(nir_cf_node * node)422 nearest_loop(nir_cf_node *node)
423 {
424    while (node->type != nir_cf_node_loop) {
425       node = node->parent;
426    }
427 
428    return nir_cf_node_as_loop(node);
429 }
430 
431 static void
remove_phi_src(nir_block * block,nir_block * pred)432 remove_phi_src(nir_block *block, nir_block *pred)
433 {
434    nir_foreach_instr(instr, block) {
435       if (instr->type != nir_instr_type_phi)
436          break;
437 
438       nir_phi_instr *phi = nir_instr_as_phi(instr);
439       nir_foreach_phi_src_safe(src, phi) {
440          if (src->pred == pred) {
441             list_del(&src->src.use_link);
442             exec_node_remove(&src->node);
443             free(src);
444          }
445       }
446    }
447 }
448 
449 /*
450  * update the CFG after a jump instruction has been added to the end of a block
451  */
452 
453 void
nir_handle_add_jump(nir_block * block)454 nir_handle_add_jump(nir_block *block)
455 {
456    nir_instr *instr = nir_block_last_instr(block);
457    nir_jump_instr *jump_instr = nir_instr_as_jump(instr);
458 
459    if (block->successors[0])
460       remove_phi_src(block->successors[0], block);
461    if (block->successors[1])
462       remove_phi_src(block->successors[1], block);
463    unlink_block_successors(block);
464 
465    nir_function_impl *impl = nir_cf_node_get_function(&block->cf_node);
466    nir_metadata_preserve(impl, nir_metadata_none);
467 
468    switch (jump_instr->type) {
469    case nir_jump_return:
470    case nir_jump_halt:
471       link_blocks(block, impl->end_block, NULL);
472       break;
473 
474    case nir_jump_break: {
475       nir_loop *loop = nearest_loop(&block->cf_node);
476       nir_cf_node *after = nir_cf_node_next(&loop->cf_node);
477       nir_block *after_block = nir_cf_node_as_block(after);
478       link_blocks(block, after_block, NULL);
479       break;
480    }
481 
482    case nir_jump_continue: {
483       nir_loop *loop = nearest_loop(&block->cf_node);
484       nir_block *first_block = nir_loop_first_block(loop);
485       link_blocks(block, first_block, NULL);
486       break;
487    }
488 
489    case nir_jump_goto:
490       link_blocks(block, jump_instr->target, NULL);
491       break;
492 
493    case nir_jump_goto_if:
494       link_blocks(block, jump_instr->else_target, jump_instr->target);
495       break;
496 
497    default:
498       unreachable("Invalid jump type");
499    }
500 }
501 
502 /* Removes the successor of a block with a jump. Note that the jump to be
503  * eliminated may be free-floating.
504  */
505 
506 static void
unlink_jump(nir_block * block,nir_jump_type type,bool add_normal_successors)507 unlink_jump(nir_block *block, nir_jump_type type, bool add_normal_successors)
508 {
509    if (block->successors[0])
510       remove_phi_src(block->successors[0], block);
511    if (block->successors[1])
512       remove_phi_src(block->successors[1], block);
513 
514    unlink_block_successors(block);
515    if (add_normal_successors)
516       block_add_normal_succs(block);
517 }
518 
519 void
nir_handle_remove_jump(nir_block * block,nir_jump_type type)520 nir_handle_remove_jump(nir_block *block, nir_jump_type type)
521 {
522    unlink_jump(block, type, true);
523 
524    nir_function_impl *impl = nir_cf_node_get_function(&block->cf_node);
525    nir_metadata_preserve(impl, nir_metadata_none);
526 }
527 
528 static void
update_if_uses(nir_cf_node * node)529 update_if_uses(nir_cf_node *node)
530 {
531    if (node->type != nir_cf_node_if)
532       return;
533 
534    nir_if *if_stmt = nir_cf_node_as_if(node);
535 
536    if_stmt->condition.parent_if = if_stmt;
537    if (if_stmt->condition.is_ssa) {
538       list_addtail(&if_stmt->condition.use_link,
539                    &if_stmt->condition.ssa->if_uses);
540    } else {
541       list_addtail(&if_stmt->condition.use_link,
542                    &if_stmt->condition.reg.reg->if_uses);
543    }
544 }
545 
546 /**
547  * Stitch two basic blocks together into one. The aggregate must have the same
548  * predecessors as the first and the same successors as the second.
549  */
550 
551 static void
stitch_blocks(nir_block * before,nir_block * after)552 stitch_blocks(nir_block *before, nir_block *after)
553 {
554    /*
555     * We move after into before, so we have to deal with up to 2 successors vs.
556     * possibly a large number of predecessors.
557     *
558     * TODO: special case when before is empty and after isn't?
559     */
560 
561    if (nir_block_ends_in_jump(before)) {
562       assert(exec_list_is_empty(&after->instr_list));
563       if (after->successors[0])
564          remove_phi_src(after->successors[0], after);
565       if (after->successors[1])
566          remove_phi_src(after->successors[1], after);
567       unlink_block_successors(after);
568       exec_node_remove(&after->cf_node.node);
569    } else {
570       move_successors(after, before);
571 
572       foreach_list_typed(nir_instr, instr, node, &after->instr_list) {
573          instr->block = before;
574       }
575 
576       exec_list_append(&before->instr_list, &after->instr_list);
577       exec_node_remove(&after->cf_node.node);
578    }
579 }
580 
581 void
nir_cf_node_insert(nir_cursor cursor,nir_cf_node * node)582 nir_cf_node_insert(nir_cursor cursor, nir_cf_node *node)
583 {
584    nir_block *before, *after;
585 
586    split_block_cursor(cursor, &before, &after);
587 
588    if (node->type == nir_cf_node_block) {
589       nir_block *block = nir_cf_node_as_block(node);
590       exec_node_insert_after(&before->cf_node.node, &block->cf_node.node);
591       block->cf_node.parent = before->cf_node.parent;
592       /* stitch_blocks() assumes that any block that ends with a jump has
593        * already been setup with the correct successors, so we need to set
594        * up jumps here as the block is being inserted.
595        */
596       if (nir_block_ends_in_jump(block))
597          nir_handle_add_jump(block);
598 
599       stitch_blocks(block, after);
600       stitch_blocks(before, block);
601    } else {
602       update_if_uses(node);
603       insert_non_block(before, node, after);
604    }
605 }
606 
607 static bool
replace_ssa_def_uses(nir_ssa_def * def,void * void_impl)608 replace_ssa_def_uses(nir_ssa_def *def, void *void_impl)
609 {
610    nir_function_impl *impl = void_impl;
611 
612    nir_ssa_undef_instr *undef =
613       nir_ssa_undef_instr_create(impl->function->shader,
614                                  def->num_components,
615                                  def->bit_size);
616    nir_instr_insert_before_cf_list(&impl->body, &undef->instr);
617    nir_ssa_def_rewrite_uses(def, &undef->def);
618    return true;
619 }
620 
621 static void
cleanup_cf_node(nir_cf_node * node,nir_function_impl * impl)622 cleanup_cf_node(nir_cf_node *node, nir_function_impl *impl)
623 {
624    switch (node->type) {
625    case nir_cf_node_block: {
626       nir_block *block = nir_cf_node_as_block(node);
627       /* We need to walk the instructions and clean up defs/uses */
628       nir_foreach_instr_safe(instr, block) {
629          if (instr->type == nir_instr_type_jump) {
630             nir_jump_instr *jump = nir_instr_as_jump(instr);
631             unlink_jump(block, jump->type, false);
632             if (jump->type == nir_jump_goto_if)
633                nir_instr_rewrite_src(instr, &jump->condition, NIR_SRC_INIT);
634          } else {
635             nir_foreach_ssa_def(instr, replace_ssa_def_uses, impl);
636             nir_instr_remove(instr);
637          }
638       }
639       break;
640    }
641 
642    case nir_cf_node_if: {
643       nir_if *if_stmt = nir_cf_node_as_if(node);
644       foreach_list_typed(nir_cf_node, child, node, &if_stmt->then_list)
645          cleanup_cf_node(child, impl);
646       foreach_list_typed(nir_cf_node, child, node, &if_stmt->else_list)
647          cleanup_cf_node(child, impl);
648 
649       list_del(&if_stmt->condition.use_link);
650       break;
651    }
652 
653    case nir_cf_node_loop: {
654       nir_loop *loop = nir_cf_node_as_loop(node);
655       foreach_list_typed(nir_cf_node, child, node, &loop->body)
656          cleanup_cf_node(child, impl);
657       break;
658    }
659    case nir_cf_node_function: {
660       nir_function_impl *impl = nir_cf_node_as_function(node);
661       foreach_list_typed(nir_cf_node, child, node, &impl->body)
662          cleanup_cf_node(child, impl);
663       break;
664    }
665    default:
666       unreachable("Invalid CF node type");
667    }
668 }
669 
670 void
nir_cf_extract(nir_cf_list * extracted,nir_cursor begin,nir_cursor end)671 nir_cf_extract(nir_cf_list *extracted, nir_cursor begin, nir_cursor end)
672 {
673    nir_block *block_begin, *block_end, *block_before, *block_after;
674 
675    if (nir_cursors_equal(begin, end)) {
676       exec_list_make_empty(&extracted->list);
677       extracted->impl = NULL; /* we shouldn't need this */
678       return;
679    }
680 
681    split_block_cursor(begin, &block_before, &block_begin);
682 
683    /* Splitting a block twice with two cursors created before either split is
684     * tricky and there are a couple of places it can go wrong if both cursors
685     * point to the same block.  One is if the second cursor is an block-based
686     * cursor and, thanks to the split above, it ends up pointing to the wrong
687     * block.  If it's a before_block cursor and it's in the same block as
688     * begin, then begin must also be a before_block cursor and it should be
689     * caught by the nir_cursors_equal check above and we won't get here.  If
690     * it's an after_block cursor, we need to re-adjust to ensure that it
691     * points to the second one of the split blocks, regardless of which it is.
692     */
693    if (end.option == nir_cursor_after_block && end.block == block_before)
694       end.block = block_begin;
695 
696    split_block_cursor(end, &block_end, &block_after);
697 
698    /* The second place this can all go wrong is that it could be that the
699     * second split places the original block after the new block in which case
700     * the block_begin pointer that we saved off above is pointing to the block
701     * at the end rather than the block in the middle like it's supposed to be.
702     * In this case, we have to re-adjust begin_block to point to the middle
703     * one.
704     */
705    if (block_begin == block_after)
706       block_begin = block_end;
707 
708    extracted->impl = nir_cf_node_get_function(&block_begin->cf_node);
709    exec_list_make_empty(&extracted->list);
710 
711    /* Dominance and other block-related information is toast. */
712    nir_metadata_preserve(extracted->impl, nir_metadata_none);
713 
714    nir_cf_node *cf_node = &block_begin->cf_node;
715    nir_cf_node *cf_node_end = &block_end->cf_node;
716    while (true) {
717       nir_cf_node *next = nir_cf_node_next(cf_node);
718 
719       exec_node_remove(&cf_node->node);
720       cf_node->parent = NULL;
721       exec_list_push_tail(&extracted->list, &cf_node->node);
722 
723       if (cf_node == cf_node_end)
724          break;
725 
726       cf_node = next;
727    }
728 
729    stitch_blocks(block_before, block_after);
730 }
731 
732 static void
relink_jump_halt_cf_node(nir_cf_node * node,nir_block * end_block)733 relink_jump_halt_cf_node(nir_cf_node *node, nir_block *end_block)
734 {
735    switch (node->type) {
736    case nir_cf_node_block: {
737       nir_block *block = nir_cf_node_as_block(node);
738       nir_instr *last_instr = nir_block_last_instr(block);
739       if (last_instr == NULL || last_instr->type != nir_instr_type_jump)
740          break;
741 
742       nir_jump_instr *jump = nir_instr_as_jump(last_instr);
743       /* We can't move a CF list from one function to another while we still
744        * have returns.
745        */
746       assert(jump->type != nir_jump_return);
747 
748       if (jump->type == nir_jump_halt) {
749          unlink_block_successors(block);
750          link_blocks(block, end_block, NULL);
751       }
752       break;
753    }
754 
755    case nir_cf_node_if: {
756       nir_if *if_stmt = nir_cf_node_as_if(node);
757       foreach_list_typed(nir_cf_node, child, node, &if_stmt->then_list)
758          relink_jump_halt_cf_node(child, end_block);
759       foreach_list_typed(nir_cf_node, child, node, &if_stmt->else_list)
760          relink_jump_halt_cf_node(child, end_block);
761       break;
762    }
763 
764    case nir_cf_node_loop: {
765       nir_loop *loop = nir_cf_node_as_loop(node);
766       foreach_list_typed(nir_cf_node, child, node, &loop->body)
767          relink_jump_halt_cf_node(child, end_block);
768       break;
769    }
770 
771    case nir_cf_node_function:
772       unreachable("Cannot insert a function in a function");
773 
774    default:
775       unreachable("Invalid CF node type");
776    }
777 }
778 
779 void
nir_cf_reinsert(nir_cf_list * cf_list,nir_cursor cursor)780 nir_cf_reinsert(nir_cf_list *cf_list, nir_cursor cursor)
781 {
782    nir_block *before, *after;
783 
784    if (exec_list_is_empty(&cf_list->list))
785       return;
786 
787    nir_function_impl *cursor_impl =
788       nir_cf_node_get_function(&nir_cursor_current_block(cursor)->cf_node);
789    if (cf_list->impl != cursor_impl) {
790       foreach_list_typed(nir_cf_node, node, node, &cf_list->list)
791          relink_jump_halt_cf_node(node, cursor_impl->end_block);
792    }
793 
794    split_block_cursor(cursor, &before, &after);
795 
796    foreach_list_typed_safe(nir_cf_node, node, node, &cf_list->list) {
797       exec_node_remove(&node->node);
798       node->parent = before->cf_node.parent;
799       exec_node_insert_node_before(&after->cf_node.node, &node->node);
800    }
801 
802    stitch_blocks(before,
803                  nir_cf_node_as_block(nir_cf_node_next(&before->cf_node)));
804    stitch_blocks(nir_cf_node_as_block(nir_cf_node_prev(&after->cf_node)),
805                  after);
806 }
807 
808 void
nir_cf_delete(nir_cf_list * cf_list)809 nir_cf_delete(nir_cf_list *cf_list)
810 {
811    foreach_list_typed(nir_cf_node, node, node, &cf_list->list) {
812       cleanup_cf_node(node, cf_list->impl);
813    }
814 }
815