5a3be260f63005bcf5fb535f876377a9302ead1f
2 * Copyright © 2014 Intel Corporation
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:
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
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
24 * Connor Abbott (cwabbott0@gmail.com)
36 /** NIR Control Flow Modification
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.
42 /* Helper struct for representing a point to extract/insert. Helps reduce the
43 * combinatorial explosion of possible points to extract.
47 nir_cursor_before_block
,
48 nir_cursor_after_block
,
49 nir_cursor_before_instr
,
50 nir_cursor_after_instr
,
54 nir_cursor_option option
;
61 static inline nir_cursor
62 nir_before_block(nir_block
*block
)
65 cursor
.option
= nir_cursor_before_block
;
70 static inline nir_cursor
71 nir_after_block(nir_block
*block
)
74 cursor
.option
= nir_cursor_after_block
;
79 static inline nir_cursor
80 nir_before_instr(nir_instr
*instr
)
83 cursor
.option
= nir_cursor_before_instr
;
88 static inline nir_cursor
89 nir_after_instr(nir_instr
*instr
)
92 cursor
.option
= nir_cursor_after_instr
;
97 static inline nir_cursor
98 nir_before_cf_node(nir_cf_node
*node
)
100 if (node
->type
== nir_cf_node_block
)
101 return nir_before_block(nir_cf_node_as_block(node
));
103 return nir_after_block(nir_cf_node_as_block(nir_cf_node_prev(node
)));
106 static inline nir_cursor
107 nir_after_cf_node(nir_cf_node
*node
)
109 if (node
->type
== nir_cf_node_block
)
110 return nir_after_block(nir_cf_node_as_block(node
));
112 return nir_before_block(nir_cf_node_as_block(nir_cf_node_next(node
)));
115 static inline nir_cursor
116 nir_before_cf_list(struct exec_list
*cf_list
)
118 nir_cf_node
*first_node
= exec_node_data(nir_cf_node
,
119 exec_list_get_head(cf_list
), node
);
120 return nir_before_cf_node(first_node
);
123 static inline nir_cursor
124 nir_after_cf_list(struct exec_list
*cf_list
)
126 nir_cf_node
*last_node
= exec_node_data(nir_cf_node
,
127 exec_list_get_tail(cf_list
), node
);
128 return nir_after_cf_node(last_node
);
131 /** Control flow insertion. */
133 /** puts a control flow node where the cursor is */
134 void nir_cf_node_insert(nir_cursor cursor
, nir_cf_node
*node
);
136 /** puts a control flow node immediately after another control flow node */
138 nir_cf_node_insert_after(nir_cf_node
*node
, nir_cf_node
*after
)
140 nir_cf_node_insert(nir_after_cf_node(node
), after
);
143 /** puts a control flow node immediately before another control flow node */
145 nir_cf_node_insert_before(nir_cf_node
*node
, nir_cf_node
*before
)
147 nir_cf_node_insert(nir_before_cf_node(node
), before
);
150 /** puts a control flow node at the beginning of a list from an if, loop, or function */
152 nir_cf_node_insert_begin(struct exec_list
*list
, nir_cf_node
*node
)
154 nir_cf_node_insert(nir_before_cf_list(list
), node
);
157 /** puts a control flow node at the end of a list from an if, loop, or function */
159 nir_cf_node_insert_end(struct exec_list
*list
, nir_cf_node
*node
)
161 nir_cf_node_insert(nir_after_cf_list(list
), node
);
164 /** removes a control flow node, doing any cleanup necessary */
165 void nir_cf_node_remove(nir_cf_node
*node
);