Path: blob/21.2-virgl/src/compiler/nir/nir_control_flow.c
4546 views
/*1* Copyright © 2014 Intel Corporation2*3* Permission is hereby granted, free of charge, to any person obtaining a4* copy of this software and associated documentation files (the "Software"),5* to deal in the Software without restriction, including without limitation6* the rights to use, copy, modify, merge, publish, distribute, sublicense,7* and/or sell copies of the Software, and to permit persons to whom the8* Software is furnished to do so, subject to the following conditions:9*10* The above copyright notice and this permission notice (including the next11* paragraph) shall be included in all copies or substantial portions of the12* Software.13*14* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR15* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,16* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL17* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER18* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING19* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS20* IN THE SOFTWARE.21*22* Authors:23* Connor Abbott ([email protected])24*25*/2627#include "nir_control_flow_private.h"2829/**30* \name Control flow modification31*32* These functions modify the control flow tree while keeping the control flow33* graph up-to-date. The invariants respected are:34* 1. Each then statement, else statement, or loop body must have at least one35* control flow node.36* 2. Each if-statement and loop must have one basic block before it and one37* after.38* 3. Two basic blocks cannot be directly next to each other.39* 4. If a basic block has a jump instruction, there must be only one and it40* must be at the end of the block.41*42* The purpose of the second one is so that we have places to insert code during43* GCM, as well as eliminating the possibility of critical edges.44*/45/*@{*/4647static inline void48block_add_pred(nir_block *block, nir_block *pred)49{50_mesa_set_add(block->predecessors, pred);51}5253static inline void54block_remove_pred(nir_block *block, nir_block *pred)55{56struct set_entry *entry = _mesa_set_search(block->predecessors, pred);5758assert(entry);5960_mesa_set_remove(block->predecessors, entry);61}6263static void64link_blocks(nir_block *pred, nir_block *succ1, nir_block *succ2)65{66pred->successors[0] = succ1;67if (succ1 != NULL)68block_add_pred(succ1, pred);6970pred->successors[1] = succ2;71if (succ2 != NULL)72block_add_pred(succ2, pred);73}7475static void76unlink_blocks(nir_block *pred, nir_block *succ)77{78if (pred->successors[0] == succ) {79pred->successors[0] = pred->successors[1];80pred->successors[1] = NULL;81} else {82assert(pred->successors[1] == succ);83pred->successors[1] = NULL;84}8586block_remove_pred(succ, pred);87}8889static void90unlink_block_successors(nir_block *block)91{92if (block->successors[1] != NULL)93unlink_blocks(block, block->successors[1]);94if (block->successors[0] != NULL)95unlink_blocks(block, block->successors[0]);96}9798static void99link_non_block_to_block(nir_cf_node *node, nir_block *block)100{101if (node->type == nir_cf_node_if) {102/*103* We're trying to link an if to a block after it; this just means linking104* the last block of the then and else branches.105*/106107nir_if *if_stmt = nir_cf_node_as_if(node);108109nir_block *last_then_block = nir_if_last_then_block(if_stmt);110nir_block *last_else_block = nir_if_last_else_block(if_stmt);111112if (!nir_block_ends_in_jump(last_then_block)) {113unlink_block_successors(last_then_block);114link_blocks(last_then_block, block, NULL);115}116117if (!nir_block_ends_in_jump(last_else_block)) {118unlink_block_successors(last_else_block);119link_blocks(last_else_block, block, NULL);120}121} else {122assert(node->type == nir_cf_node_loop);123}124}125126static void127link_block_to_non_block(nir_block *block, nir_cf_node *node)128{129if (node->type == nir_cf_node_if) {130/*131* We're trying to link a block to an if after it; this just means linking132* the block to the first block of the then and else branches.133*/134135nir_if *if_stmt = nir_cf_node_as_if(node);136137nir_block *first_then_block = nir_if_first_then_block(if_stmt);138nir_block *first_else_block = nir_if_first_else_block(if_stmt);139140unlink_block_successors(block);141link_blocks(block, first_then_block, first_else_block);142} else if (node->type == nir_cf_node_loop) {143/*144* For similar reasons as the corresponding case in145* link_non_block_to_block(), don't worry about if the loop header has146* any predecessors that need to be unlinked.147*/148149nir_loop *loop = nir_cf_node_as_loop(node);150151nir_block *loop_header_block = nir_loop_first_block(loop);152153unlink_block_successors(block);154link_blocks(block, loop_header_block, NULL);155}156157}158159/**160* Replace a block's successor with a different one.161*/162static void163replace_successor(nir_block *block, nir_block *old_succ, nir_block *new_succ)164{165if (block->successors[0] == old_succ) {166block->successors[0] = new_succ;167} else {168assert(block->successors[1] == old_succ);169block->successors[1] = new_succ;170}171172block_remove_pred(old_succ, block);173block_add_pred(new_succ, block);174}175176/**177* Takes a basic block and inserts a new empty basic block before it, making its178* predecessors point to the new block. This essentially splits the block into179* an empty header and a body so that another non-block CF node can be inserted180* between the two. Note that this does *not* link the two basic blocks, so181* some kind of cleanup *must* be performed after this call.182*/183184static nir_block *185split_block_beginning(nir_block *block)186{187nir_block *new_block = nir_block_create(ralloc_parent(block));188new_block->cf_node.parent = block->cf_node.parent;189exec_node_insert_node_before(&block->cf_node.node, &new_block->cf_node.node);190191set_foreach(block->predecessors, entry) {192nir_block *pred = (nir_block *) entry->key;193replace_successor(pred, block, new_block);194}195196/* Any phi nodes must stay part of the new block, or else their197* sources will be messed up.198*/199nir_foreach_instr_safe(instr, block) {200if (instr->type != nir_instr_type_phi)201break;202203exec_node_remove(&instr->node);204instr->block = new_block;205exec_list_push_tail(&new_block->instr_list, &instr->node);206}207208return new_block;209}210211static void212rewrite_phi_preds(nir_block *block, nir_block *old_pred, nir_block *new_pred)213{214nir_foreach_instr_safe(instr, block) {215if (instr->type != nir_instr_type_phi)216break;217218nir_phi_instr *phi = nir_instr_as_phi(instr);219nir_foreach_phi_src(src, phi) {220if (src->pred == old_pred) {221src->pred = new_pred;222break;223}224}225}226}227228void229nir_insert_phi_undef(nir_block *block, nir_block *pred)230{231nir_function_impl *impl = nir_cf_node_get_function(&block->cf_node);232nir_foreach_instr(instr, block) {233if (instr->type != nir_instr_type_phi)234break;235236nir_phi_instr *phi = nir_instr_as_phi(instr);237nir_ssa_undef_instr *undef =238nir_ssa_undef_instr_create(ralloc_parent(phi),239phi->dest.ssa.num_components,240phi->dest.ssa.bit_size);241nir_instr_insert_before_cf_list(&impl->body, &undef->instr);242nir_phi_src *src = ralloc(phi, nir_phi_src);243src->pred = pred;244src->src.parent_instr = &phi->instr;245src->src.is_ssa = true;246src->src.ssa = &undef->def;247248list_addtail(&src->src.use_link, &undef->def.uses);249250exec_list_push_tail(&phi->srcs, &src->node);251}252}253254/**255* Moves the successors of source to the successors of dest, leaving both256* successors of source NULL.257*/258259static void260move_successors(nir_block *source, nir_block *dest)261{262nir_block *succ1 = source->successors[0];263nir_block *succ2 = source->successors[1];264265if (succ1) {266unlink_blocks(source, succ1);267rewrite_phi_preds(succ1, source, dest);268}269270if (succ2) {271unlink_blocks(source, succ2);272rewrite_phi_preds(succ2, source, dest);273}274275unlink_block_successors(dest);276link_blocks(dest, succ1, succ2);277}278279/* Given a basic block with no successors that has been inserted into the280* control flow tree, gives it the successors it would normally have assuming281* it doesn't end in a jump instruction. Also inserts phi sources with undefs282* if necessary.283*/284static void285block_add_normal_succs(nir_block *block)286{287if (exec_node_is_tail_sentinel(block->cf_node.node.next)) {288nir_cf_node *parent = block->cf_node.parent;289if (parent->type == nir_cf_node_if) {290nir_cf_node *next = nir_cf_node_next(parent);291nir_block *next_block = nir_cf_node_as_block(next);292293link_blocks(block, next_block, NULL);294} else if (parent->type == nir_cf_node_loop) {295nir_loop *loop = nir_cf_node_as_loop(parent);296297nir_block *head_block = nir_loop_first_block(loop);298299link_blocks(block, head_block, NULL);300nir_insert_phi_undef(head_block, block);301} else {302nir_function_impl *impl = nir_cf_node_as_function(parent);303link_blocks(block, impl->end_block, NULL);304}305} else {306nir_cf_node *next = nir_cf_node_next(&block->cf_node);307if (next->type == nir_cf_node_if) {308nir_if *next_if = nir_cf_node_as_if(next);309310nir_block *first_then_block = nir_if_first_then_block(next_if);311nir_block *first_else_block = nir_if_first_else_block(next_if);312313link_blocks(block, first_then_block, first_else_block);314} else if (next->type == nir_cf_node_loop) {315nir_loop *next_loop = nir_cf_node_as_loop(next);316317nir_block *first_block = nir_loop_first_block(next_loop);318319link_blocks(block, first_block, NULL);320nir_insert_phi_undef(first_block, block);321}322}323}324325static nir_block *326split_block_end(nir_block *block)327{328nir_block *new_block = nir_block_create(ralloc_parent(block));329new_block->cf_node.parent = block->cf_node.parent;330exec_node_insert_after(&block->cf_node.node, &new_block->cf_node.node);331332if (nir_block_ends_in_jump(block)) {333/* Figure out what successor block would've had if it didn't have a jump334* instruction, and make new_block have that successor.335*/336block_add_normal_succs(new_block);337} else {338move_successors(block, new_block);339}340341return new_block;342}343344static nir_block *345split_block_before_instr(nir_instr *instr)346{347assert(instr->type != nir_instr_type_phi);348nir_block *new_block = split_block_beginning(instr->block);349350nir_foreach_instr_safe(cur_instr, instr->block) {351if (cur_instr == instr)352break;353354exec_node_remove(&cur_instr->node);355cur_instr->block = new_block;356exec_list_push_tail(&new_block->instr_list, &cur_instr->node);357}358359return new_block;360}361362/* Splits a basic block at the point specified by the cursor. The "before" and363* "after" arguments are filled out with the blocks resulting from the split364* if non-NULL. Note that the "beginning" of the block is actually interpreted365* as before the first non-phi instruction, and it's illegal to split a block366* before a phi instruction.367*/368369static void370split_block_cursor(nir_cursor cursor,371nir_block **_before, nir_block **_after)372{373nir_block *before, *after;374switch (cursor.option) {375case nir_cursor_before_block:376after = cursor.block;377before = split_block_beginning(cursor.block);378break;379380case nir_cursor_after_block:381before = cursor.block;382after = split_block_end(cursor.block);383break;384385case nir_cursor_before_instr:386after = cursor.instr->block;387before = split_block_before_instr(cursor.instr);388break;389390case nir_cursor_after_instr:391/* We lower this to split_block_before_instr() so that we can keep the392* after-a-jump-instr case contained to split_block_end().393*/394if (nir_instr_is_last(cursor.instr)) {395before = cursor.instr->block;396after = split_block_end(cursor.instr->block);397} else {398after = cursor.instr->block;399before = split_block_before_instr(nir_instr_next(cursor.instr));400}401break;402403default:404unreachable("not reached");405}406407if (_before)408*_before = before;409if (_after)410*_after = after;411}412413/**414* Inserts a non-basic block between two basic blocks and links them together.415*/416417static void418insert_non_block(nir_block *before, nir_cf_node *node, nir_block *after)419{420node->parent = before->cf_node.parent;421exec_node_insert_after(&before->cf_node.node, &node->node);422link_block_to_non_block(before, node);423link_non_block_to_block(node, after);424}425426/* walk up the control flow tree to find the innermost enclosed loop */427static nir_loop *428nearest_loop(nir_cf_node *node)429{430while (node->type != nir_cf_node_loop) {431node = node->parent;432}433434return nir_cf_node_as_loop(node);435}436437static void438remove_phi_src(nir_block *block, nir_block *pred)439{440nir_foreach_instr(instr, block) {441if (instr->type != nir_instr_type_phi)442break;443444nir_phi_instr *phi = nir_instr_as_phi(instr);445nir_foreach_phi_src_safe(src, phi) {446if (src->pred == pred) {447list_del(&src->src.use_link);448exec_node_remove(&src->node);449}450}451}452}453454/*455* update the CFG after a jump instruction has been added to the end of a block456*/457458void459nir_handle_add_jump(nir_block *block)460{461nir_instr *instr = nir_block_last_instr(block);462nir_jump_instr *jump_instr = nir_instr_as_jump(instr);463464if (block->successors[0])465remove_phi_src(block->successors[0], block);466if (block->successors[1])467remove_phi_src(block->successors[1], block);468unlink_block_successors(block);469470nir_function_impl *impl = nir_cf_node_get_function(&block->cf_node);471nir_metadata_preserve(impl, nir_metadata_none);472473switch (jump_instr->type) {474case nir_jump_return:475case nir_jump_halt:476link_blocks(block, impl->end_block, NULL);477break;478479case nir_jump_break: {480nir_loop *loop = nearest_loop(&block->cf_node);481nir_cf_node *after = nir_cf_node_next(&loop->cf_node);482nir_block *after_block = nir_cf_node_as_block(after);483link_blocks(block, after_block, NULL);484break;485}486487case nir_jump_continue: {488nir_loop *loop = nearest_loop(&block->cf_node);489nir_block *first_block = nir_loop_first_block(loop);490link_blocks(block, first_block, NULL);491break;492}493494case nir_jump_goto:495link_blocks(block, jump_instr->target, NULL);496break;497498case nir_jump_goto_if:499link_blocks(block, jump_instr->else_target, jump_instr->target);500break;501502default:503unreachable("Invalid jump type");504}505}506507/* Removes the successor of a block with a jump. Note that the jump to be508* eliminated may be free-floating.509*/510511static void512unlink_jump(nir_block *block, nir_jump_type type, bool add_normal_successors)513{514if (block->successors[0])515remove_phi_src(block->successors[0], block);516if (block->successors[1])517remove_phi_src(block->successors[1], block);518519unlink_block_successors(block);520if (add_normal_successors)521block_add_normal_succs(block);522}523524void525nir_handle_remove_jump(nir_block *block, nir_jump_type type)526{527unlink_jump(block, type, true);528529nir_function_impl *impl = nir_cf_node_get_function(&block->cf_node);530nir_metadata_preserve(impl, nir_metadata_none);531}532533static void534update_if_uses(nir_cf_node *node)535{536if (node->type != nir_cf_node_if)537return;538539nir_if *if_stmt = nir_cf_node_as_if(node);540541if_stmt->condition.parent_if = if_stmt;542if (if_stmt->condition.is_ssa) {543list_addtail(&if_stmt->condition.use_link,544&if_stmt->condition.ssa->if_uses);545} else {546list_addtail(&if_stmt->condition.use_link,547&if_stmt->condition.reg.reg->if_uses);548}549}550551/**552* Stitch two basic blocks together into one. The aggregate must have the same553* predecessors as the first and the same successors as the second.554*/555556static void557stitch_blocks(nir_block *before, nir_block *after)558{559/*560* We move after into before, so we have to deal with up to 2 successors vs.561* possibly a large number of predecessors.562*563* TODO: special case when before is empty and after isn't?564*/565566if (nir_block_ends_in_jump(before)) {567assert(exec_list_is_empty(&after->instr_list));568if (after->successors[0])569remove_phi_src(after->successors[0], after);570if (after->successors[1])571remove_phi_src(after->successors[1], after);572unlink_block_successors(after);573exec_node_remove(&after->cf_node.node);574} else {575move_successors(after, before);576577foreach_list_typed(nir_instr, instr, node, &after->instr_list) {578instr->block = before;579}580581exec_list_append(&before->instr_list, &after->instr_list);582exec_node_remove(&after->cf_node.node);583}584}585586void587nir_cf_node_insert(nir_cursor cursor, nir_cf_node *node)588{589nir_block *before, *after;590591split_block_cursor(cursor, &before, &after);592593if (node->type == nir_cf_node_block) {594nir_block *block = nir_cf_node_as_block(node);595exec_node_insert_after(&before->cf_node.node, &block->cf_node.node);596block->cf_node.parent = before->cf_node.parent;597/* stitch_blocks() assumes that any block that ends with a jump has598* already been setup with the correct successors, so we need to set599* up jumps here as the block is being inserted.600*/601if (nir_block_ends_in_jump(block))602nir_handle_add_jump(block);603604stitch_blocks(block, after);605stitch_blocks(before, block);606} else {607update_if_uses(node);608insert_non_block(before, node, after);609}610}611612static bool613replace_ssa_def_uses(nir_ssa_def *def, void *void_impl)614{615nir_function_impl *impl = void_impl;616void *mem_ctx = ralloc_parent(impl);617618nir_ssa_undef_instr *undef =619nir_ssa_undef_instr_create(mem_ctx, def->num_components,620def->bit_size);621nir_instr_insert_before_cf_list(&impl->body, &undef->instr);622nir_ssa_def_rewrite_uses(def, &undef->def);623return true;624}625626static void627cleanup_cf_node(nir_cf_node *node, nir_function_impl *impl)628{629switch (node->type) {630case nir_cf_node_block: {631nir_block *block = nir_cf_node_as_block(node);632/* We need to walk the instructions and clean up defs/uses */633nir_foreach_instr_safe(instr, block) {634if (instr->type == nir_instr_type_jump) {635nir_jump_instr *jump = nir_instr_as_jump(instr);636unlink_jump(block, jump->type, false);637if (jump->type == nir_jump_goto_if)638nir_instr_rewrite_src(instr, &jump->condition, NIR_SRC_INIT);639} else {640nir_foreach_ssa_def(instr, replace_ssa_def_uses, impl);641nir_instr_remove(instr);642}643}644break;645}646647case nir_cf_node_if: {648nir_if *if_stmt = nir_cf_node_as_if(node);649foreach_list_typed(nir_cf_node, child, node, &if_stmt->then_list)650cleanup_cf_node(child, impl);651foreach_list_typed(nir_cf_node, child, node, &if_stmt->else_list)652cleanup_cf_node(child, impl);653654list_del(&if_stmt->condition.use_link);655break;656}657658case nir_cf_node_loop: {659nir_loop *loop = nir_cf_node_as_loop(node);660foreach_list_typed(nir_cf_node, child, node, &loop->body)661cleanup_cf_node(child, impl);662break;663}664case nir_cf_node_function: {665nir_function_impl *impl = nir_cf_node_as_function(node);666foreach_list_typed(nir_cf_node, child, node, &impl->body)667cleanup_cf_node(child, impl);668break;669}670default:671unreachable("Invalid CF node type");672}673}674675void676nir_cf_extract(nir_cf_list *extracted, nir_cursor begin, nir_cursor end)677{678nir_block *block_begin, *block_end, *block_before, *block_after;679680if (nir_cursors_equal(begin, end)) {681exec_list_make_empty(&extracted->list);682extracted->impl = NULL; /* we shouldn't need this */683return;684}685686split_block_cursor(begin, &block_before, &block_begin);687688/* Splitting a block twice with two cursors created before either split is689* tricky and there are a couple of places it can go wrong if both cursors690* point to the same block. One is if the second cursor is an block-based691* cursor and, thanks to the split above, it ends up pointing to the wrong692* block. If it's a before_block cursor and it's in the same block as693* begin, then begin must also be a before_block cursor and it should be694* caught by the nir_cursors_equal check above and we won't get here. If695* it's an after_block cursor, we need to re-adjust to ensure that it696* points to the second one of the split blocks, regardless of which it is.697*/698if (end.option == nir_cursor_after_block && end.block == block_before)699end.block = block_begin;700701split_block_cursor(end, &block_end, &block_after);702703/* The second place this can all go wrong is that it could be that the704* second split places the original block after the new block in which case705* the block_begin pointer that we saved off above is pointing to the block706* at the end rather than the block in the middle like it's supposed to be.707* In this case, we have to re-adjust begin_block to point to the middle708* one.709*/710if (block_begin == block_after)711block_begin = block_end;712713extracted->impl = nir_cf_node_get_function(&block_begin->cf_node);714exec_list_make_empty(&extracted->list);715716/* Dominance and other block-related information is toast. */717nir_metadata_preserve(extracted->impl, nir_metadata_none);718719nir_cf_node *cf_node = &block_begin->cf_node;720nir_cf_node *cf_node_end = &block_end->cf_node;721while (true) {722nir_cf_node *next = nir_cf_node_next(cf_node);723724exec_node_remove(&cf_node->node);725cf_node->parent = NULL;726exec_list_push_tail(&extracted->list, &cf_node->node);727728if (cf_node == cf_node_end)729break;730731cf_node = next;732}733734stitch_blocks(block_before, block_after);735}736737static void738relink_jump_halt_cf_node(nir_cf_node *node, nir_block *end_block)739{740switch (node->type) {741case nir_cf_node_block: {742nir_block *block = nir_cf_node_as_block(node);743nir_instr *last_instr = nir_block_last_instr(block);744if (last_instr == NULL || last_instr->type != nir_instr_type_jump)745break;746747nir_jump_instr *jump = nir_instr_as_jump(last_instr);748/* We can't move a CF list from one function to another while we still749* have returns.750*/751assert(jump->type != nir_jump_return);752753if (jump->type == nir_jump_halt) {754unlink_block_successors(block);755link_blocks(block, end_block, NULL);756}757break;758}759760case nir_cf_node_if: {761nir_if *if_stmt = nir_cf_node_as_if(node);762foreach_list_typed(nir_cf_node, child, node, &if_stmt->then_list)763relink_jump_halt_cf_node(child, end_block);764foreach_list_typed(nir_cf_node, child, node, &if_stmt->else_list)765relink_jump_halt_cf_node(child, end_block);766break;767}768769case nir_cf_node_loop: {770nir_loop *loop = nir_cf_node_as_loop(node);771foreach_list_typed(nir_cf_node, child, node, &loop->body)772relink_jump_halt_cf_node(child, end_block);773break;774}775776case nir_cf_node_function:777unreachable("Cannot insert a function in a function");778779default:780unreachable("Invalid CF node type");781}782}783784void785nir_cf_reinsert(nir_cf_list *cf_list, nir_cursor cursor)786{787nir_block *before, *after;788789if (exec_list_is_empty(&cf_list->list))790return;791792nir_function_impl *cursor_impl =793nir_cf_node_get_function(&nir_cursor_current_block(cursor)->cf_node);794if (cf_list->impl != cursor_impl) {795foreach_list_typed(nir_cf_node, node, node, &cf_list->list)796relink_jump_halt_cf_node(node, cursor_impl->end_block);797}798799split_block_cursor(cursor, &before, &after);800801foreach_list_typed_safe(nir_cf_node, node, node, &cf_list->list) {802exec_node_remove(&node->node);803node->parent = before->cf_node.parent;804exec_node_insert_node_before(&after->cf_node.node, &node->node);805}806807stitch_blocks(before,808nir_cf_node_as_block(nir_cf_node_next(&before->cf_node)));809stitch_blocks(nir_cf_node_as_block(nir_cf_node_prev(&after->cf_node)),810after);811}812813void814nir_cf_delete(nir_cf_list *cf_list)815{816foreach_list_typed(nir_cf_node, node, node, &cf_list->list) {817cleanup_cf_node(node, cf_list->impl);818}819}820821822