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#ifndef NIR_CONTROL_FLOW_H 29#define NIR_CONTROL_FLOW_H 30 31#include "nir.h" 32 33#ifdef __cplusplus 34extern "C" { 35#endif 36 37/** NIR Control Flow Modification 38 * 39 * This file contains various APIs that make modifying control flow in NIR, 40 * while maintaining the invariants checked by the validator, much easier. 41 * There are two parts to this: 42 * 43 * 1. Inserting control flow (ifs and loops) in various places, for creating 44 * IR either from scratch or as part of some lowering pass. 45 * 2. Taking existing pieces of the IR and either moving them around or 46 * deleting them. 47 */ 48 49/** Control flow insertion. */ 50 51/** puts a control flow node where the cursor is */ 52void nir_cf_node_insert(nir_cursor cursor, nir_cf_node *node); 53 54/** puts a control flow node immediately after another control flow node */ 55static inline void 56nir_cf_node_insert_after(nir_cf_node *node, nir_cf_node *after) 57{ 58 nir_cf_node_insert(nir_after_cf_node(node), after); 59} 60 61/** puts a control flow node immediately before another control flow node */ 62static inline void 63nir_cf_node_insert_before(nir_cf_node *node, nir_cf_node *before) 64{ 65 nir_cf_node_insert(nir_before_cf_node(node), before); 66} 67 68/** puts a control flow node at the beginning of a list from an if, loop, or function */ 69static inline void 70nir_cf_node_insert_begin(struct exec_list *list, nir_cf_node *node) 71{ 72 nir_cf_node_insert(nir_before_cf_list(list), node); 73} 74 75/** puts a control flow node at the end of a list from an if, loop, or function */ 76static inline void 77nir_cf_node_insert_end(struct exec_list *list, nir_cf_node *node) 78{ 79 nir_cf_node_insert(nir_after_cf_list(list), node); 80} 81 82 83/** Control flow motion. 84 * 85 * These functions let you take a part of a control flow list (basically 86 * equivalent to a series of statement in GLSL) and "extract" it from the IR, 87 * so that it's a free-floating piece of IR that can be either re-inserted 88 * somewhere else or deleted entirely. A few notes on using it: 89 * 90 * 1. Phi nodes are considered attached to the piece of control flow that 91 * their sources come from. There are three places where phi nodes can 92 * occur, which are the three places where a block can have multiple 93 * predecessors: 94 * 95 * 1) After an if statement, if neither branch ends in a jump. 96 * 2) After a loop, if there are multiple breaks. 97 * 3) At the beginning of a loop. 98 * 99 * For #1, the phi node is considered to be part of the if, and for #2 and 100 * #3 the phi node is considered to be part of the loop. This allows us to 101 * keep phis intact, but it means that phi nodes cannot be separated from 102 * the control flow they come from. For example, extracting an if without 103 * extracting all the phi nodes after it is not allowed, and neither is 104 * extracting only some of the phi nodes at the beginning of a block. It 105 * also means that extracting from the beginning of a basic block actually 106 * means extracting from the first non-phi instruction, since there's no 107 * situation where extracting phi nodes without extracting what comes 108 * before them makes any sense. 109 * 110 * 2. Phi node sources are guaranteed to remain valid, meaning that they still 111 * correspond one-to-one with the predecessors of the basic block they're 112 * part of. In addition, the original sources will be preserved unless they 113 * correspond to a break or continue that was deleted. However, no attempt 114 * is made to ensure that SSA form is maintained. In particular, it is 115 * *not* guaranteed that definitions of SSA values will dominate all their 116 * uses after all is said and done. Either the caller must ensure that this 117 * is the case, or it must insert extra phi nodes to restore SSA. 118 * 119 * 3. It is invalid to move a piece of IR with a break/continue outside of the 120 * loop it references. Doing this will result in invalid 121 * successors/predecessors and phi node sources. 122 * 123 * 4. It is invalid to move a piece of IR from one function implementation to 124 * another. 125 * 126 * 5. Extracting a control flow list will leave lots of dangling references to 127 * and from other pieces of the IR. It also leaves things in a not 100% 128 * consistent state. This means that some things (e.g. inserting 129 * instructions) might not work reliably on the extracted control flow. It 130 * also means that extracting control flow without re-inserting it or 131 * deleting it is a Bad Thing (tm). 132 */ 133 134typedef struct { 135 struct exec_list list; 136 nir_function_impl *impl; /* for cleaning up if the list is deleted */ 137} nir_cf_list; 138 139nir_cursor nir_cf_extract(nir_cf_list *extracted, nir_cursor begin, 140 nir_cursor end); 141 142nir_cursor nir_cf_reinsert(nir_cf_list *cf_list, nir_cursor cursor); 143 144void nir_cf_delete(nir_cf_list *cf_list); 145 146void nir_cf_list_clone(nir_cf_list *dst, nir_cf_list *src, nir_cf_node *parent, 147 struct hash_table *remap_table); 148 149static inline void 150nir_cf_list_clone_and_reinsert(nir_cf_list *src_list, nir_cf_node *parent, 151 nir_cursor cursor, 152 struct hash_table *remap_table) 153{ 154 nir_cf_list list; 155 nir_cf_list_clone(&list, src_list, parent, remap_table); 156 nir_cf_reinsert(&list, cursor); 157} 158 159static inline void 160nir_cf_list_extract(nir_cf_list *extracted, struct exec_list *cf_list) 161{ 162 nir_cf_extract(extracted, nir_before_cf_list(cf_list), 163 nir_after_cf_list(cf_list)); 164} 165 166/** removes a control flow node, doing any cleanup necessary */ 167static inline void 168nir_cf_node_remove(nir_cf_node *node) 169{ 170 nir_cf_list list; 171 nir_cf_extract(&list, nir_before_cf_node(node), nir_after_cf_node(node)); 172 nir_cf_delete(&list); 173} 174 175/** inserts undef phi sources from predcessor into phis of the block */ 176void nir_insert_phi_undef(nir_block *block, nir_block *pred); 177 178#ifdef __cplusplus 179} 180#endif 181 182#endif /* NIR_CONTROL_FLOW_H */ 183