nir/cf: reimplement nir_cf_node_remove() using the new API
[mesa.git] / src / glsl / nir / nir_control_flow.h
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.h"
29
30 #pragma once
31
32 #ifdef __cplusplus
33 extern "C" {
34 #endif
35
36 /** NIR Control Flow Modification
37 *
38 * This file contains various API's that make modifying control flow in NIR,
39 * while maintaining the invariants checked by the validator, much easier.
40 * There are two parts to this:
41 *
42 * 1. Inserting control flow (if's and loops) in various places, for creating
43 * IR either from scratch or as part of some lowering pass.
44 * 2. Taking existing pieces of the IR and either moving them around or
45 * deleting them.
46 */
47
48 /* Helper struct for representing a point to extract/insert. Helps reduce the
49 * combinatorial explosion of possible points to extract.
50 */
51
52 typedef enum {
53 nir_cursor_before_block,
54 nir_cursor_after_block,
55 nir_cursor_before_instr,
56 nir_cursor_after_instr,
57 } nir_cursor_option;
58
59 typedef struct {
60 nir_cursor_option option;
61 union {
62 nir_block *block;
63 nir_instr *instr;
64 };
65 } nir_cursor;
66
67 static inline nir_cursor
68 nir_before_block(nir_block *block)
69 {
70 nir_cursor cursor;
71 cursor.option = nir_cursor_before_block;
72 cursor.block = block;
73 return cursor;
74 }
75
76 static inline nir_cursor
77 nir_after_block(nir_block *block)
78 {
79 nir_cursor cursor;
80 cursor.option = nir_cursor_after_block;
81 cursor.block = block;
82 return cursor;
83 }
84
85 static inline nir_cursor
86 nir_before_instr(nir_instr *instr)
87 {
88 nir_cursor cursor;
89 cursor.option = nir_cursor_before_instr;
90 cursor.instr = instr;
91 return cursor;
92 }
93
94 static inline nir_cursor
95 nir_after_instr(nir_instr *instr)
96 {
97 nir_cursor cursor;
98 cursor.option = nir_cursor_after_instr;
99 cursor.instr = instr;
100 return cursor;
101 }
102
103 static inline nir_cursor
104 nir_before_cf_node(nir_cf_node *node)
105 {
106 if (node->type == nir_cf_node_block)
107 return nir_before_block(nir_cf_node_as_block(node));
108
109 return nir_after_block(nir_cf_node_as_block(nir_cf_node_prev(node)));
110 }
111
112 static inline nir_cursor
113 nir_after_cf_node(nir_cf_node *node)
114 {
115 if (node->type == nir_cf_node_block)
116 return nir_after_block(nir_cf_node_as_block(node));
117
118 return nir_before_block(nir_cf_node_as_block(nir_cf_node_next(node)));
119 }
120
121 static inline nir_cursor
122 nir_before_cf_list(struct exec_list *cf_list)
123 {
124 nir_cf_node *first_node = exec_node_data(nir_cf_node,
125 exec_list_get_head(cf_list), node);
126 return nir_before_cf_node(first_node);
127 }
128
129 static inline nir_cursor
130 nir_after_cf_list(struct exec_list *cf_list)
131 {
132 nir_cf_node *last_node = exec_node_data(nir_cf_node,
133 exec_list_get_tail(cf_list), node);
134 return nir_after_cf_node(last_node);
135 }
136
137 /** Control flow insertion. */
138
139 /** puts a control flow node where the cursor is */
140 void nir_cf_node_insert(nir_cursor cursor, nir_cf_node *node);
141
142 /** puts a control flow node immediately after another control flow node */
143 static inline void
144 nir_cf_node_insert_after(nir_cf_node *node, nir_cf_node *after)
145 {
146 nir_cf_node_insert(nir_after_cf_node(node), after);
147 }
148
149 /** puts a control flow node immediately before another control flow node */
150 static inline void
151 nir_cf_node_insert_before(nir_cf_node *node, nir_cf_node *before)
152 {
153 nir_cf_node_insert(nir_before_cf_node(node), before);
154 }
155
156 /** puts a control flow node at the beginning of a list from an if, loop, or function */
157 static inline void
158 nir_cf_node_insert_begin(struct exec_list *list, nir_cf_node *node)
159 {
160 nir_cf_node_insert(nir_before_cf_list(list), node);
161 }
162
163 /** puts a control flow node at the end of a list from an if, loop, or function */
164 static inline void
165 nir_cf_node_insert_end(struct exec_list *list, nir_cf_node *node)
166 {
167 nir_cf_node_insert(nir_after_cf_list(list), node);
168 }
169
170
171 /** Control flow motion.
172 *
173 * These functions let you take a part of a control flow list (basically
174 * equivalent to a series of statement in GLSL) and "extract" it from the IR,
175 * so that it's a free-floating piece of IR that can be either re-inserted
176 * somewhere else or deleted entirely. A few notes on using it:
177 *
178 * 1. Phi nodes are considered attached to the piece of control flow that
179 * their sources come from. There are three places where phi nodes can
180 * occur, which are the three places where a block can have multiple
181 * predecessors:
182 *
183 * 1) After an if statement, if neither branch ends in a jump.
184 * 2) After a loop, if there are multiple break's.
185 * 3) At the beginning of a loop.
186 *
187 * For #1, the phi node is considered to be part of the if, and for #2 and
188 * #3 the phi node is considered to be part of the loop. This allows us to
189 * keep phi's intact, but it means that phi nodes cannot be separated from
190 * the control flow they come from. For example, extracting an if without
191 * extracting all the phi nodes after it is not allowed, and neither is
192 * extracting only some of the phi nodes at the beginning of a block. It
193 * also means that extracting from the beginning of a basic block actually
194 * means extracting from the first non-phi instruction, since there's no
195 * situation where extracting phi nodes without extracting what comes
196 * before them makes any sense.
197 *
198 * 2. Phi node sources are guaranteed to remain valid, meaning that they still
199 * correspond one-to-one with the predecessors of the basic block they're
200 * part of. In addition, the original sources will be preserved unless they
201 * correspond to a break or continue that was deleted. However, no attempt
202 * is made to ensure that SSA form is maintained. In particular, it is
203 * *not* guaranteed that definitions of SSA values will dominate all their
204 * uses after all is said and done. Either the caller must ensure that this
205 * is the case, or it must insert extra phi nodes to restore SSA.
206 *
207 * 3. It is invalid to move a piece of IR with a break/continue outside of the
208 * loop it references. Doing this will result in invalid
209 * successors/predecessors and phi node sources.
210 *
211 * 4. It is invalid to move a piece of IR from one function implementation to
212 * another.
213 *
214 * 5. Extracting a control flow list will leave lots of dangling references to
215 * and from other pieces of the IR. It also leaves things in a not 100%
216 * consistent state. This means that some things (e.g. inserting
217 * instructions) might not work reliably on the extracted control flow. It
218 * also means that extracting control flow without re-inserting it or
219 * deleting it is a Bad Thing (tm).
220 */
221
222 typedef struct {
223 struct exec_list list;
224 nir_function_impl *impl; /* for cleaning up if the list is deleted */
225 } nir_cf_list;
226
227 void nir_cf_extract(nir_cf_list *extracted, nir_cursor begin, nir_cursor end);
228
229 void nir_cf_reinsert(nir_cf_list *cf_list, nir_cursor cursor);
230
231 void nir_cf_delete(nir_cf_list *cf_list);
232
233 static inline void
234 nir_cf_list_extract(nir_cf_list *extracted, struct exec_list *cf_list)
235 {
236 nir_cf_extract(extracted, nir_before_cf_list(cf_list),
237 nir_after_cf_list(cf_list));
238 }
239
240 /** removes a control flow node, doing any cleanup necessary */
241 static inline void
242 nir_cf_node_remove(nir_cf_node *node)
243 {
244 nir_cf_list list;
245 nir_cf_extract(&list, nir_before_cf_node(node), nir_after_cf_node(node));
246 nir_cf_delete(&list);
247 }
248
249 #ifdef __cplusplus
250 }
251 #endif