5a3be260f63005bcf5fb535f876377a9302ead1f
[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 */
41
42 /* Helper struct for representing a point to extract/insert. Helps reduce the
43 * combinatorial explosion of possible points to extract.
44 */
45
46 typedef enum {
47 nir_cursor_before_block,
48 nir_cursor_after_block,
49 nir_cursor_before_instr,
50 nir_cursor_after_instr,
51 } nir_cursor_option;
52
53 typedef struct {
54 nir_cursor_option option;
55 union {
56 nir_block *block;
57 nir_instr *instr;
58 };
59 } nir_cursor;
60
61 static inline nir_cursor
62 nir_before_block(nir_block *block)
63 {
64 nir_cursor cursor;
65 cursor.option = nir_cursor_before_block;
66 cursor.block = block;
67 return cursor;
68 }
69
70 static inline nir_cursor
71 nir_after_block(nir_block *block)
72 {
73 nir_cursor cursor;
74 cursor.option = nir_cursor_after_block;
75 cursor.block = block;
76 return cursor;
77 }
78
79 static inline nir_cursor
80 nir_before_instr(nir_instr *instr)
81 {
82 nir_cursor cursor;
83 cursor.option = nir_cursor_before_instr;
84 cursor.instr = instr;
85 return cursor;
86 }
87
88 static inline nir_cursor
89 nir_after_instr(nir_instr *instr)
90 {
91 nir_cursor cursor;
92 cursor.option = nir_cursor_after_instr;
93 cursor.instr = instr;
94 return cursor;
95 }
96
97 static inline nir_cursor
98 nir_before_cf_node(nir_cf_node *node)
99 {
100 if (node->type == nir_cf_node_block)
101 return nir_before_block(nir_cf_node_as_block(node));
102
103 return nir_after_block(nir_cf_node_as_block(nir_cf_node_prev(node)));
104 }
105
106 static inline nir_cursor
107 nir_after_cf_node(nir_cf_node *node)
108 {
109 if (node->type == nir_cf_node_block)
110 return nir_after_block(nir_cf_node_as_block(node));
111
112 return nir_before_block(nir_cf_node_as_block(nir_cf_node_next(node)));
113 }
114
115 static inline nir_cursor
116 nir_before_cf_list(struct exec_list *cf_list)
117 {
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);
121 }
122
123 static inline nir_cursor
124 nir_after_cf_list(struct exec_list *cf_list)
125 {
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);
129 }
130
131 /** Control flow insertion. */
132
133 /** puts a control flow node where the cursor is */
134 void nir_cf_node_insert(nir_cursor cursor, nir_cf_node *node);
135
136 /** puts a control flow node immediately after another control flow node */
137 static inline void
138 nir_cf_node_insert_after(nir_cf_node *node, nir_cf_node *after)
139 {
140 nir_cf_node_insert(nir_after_cf_node(node), after);
141 }
142
143 /** puts a control flow node immediately before another control flow node */
144 static inline void
145 nir_cf_node_insert_before(nir_cf_node *node, nir_cf_node *before)
146 {
147 nir_cf_node_insert(nir_before_cf_node(node), before);
148 }
149
150 /** puts a control flow node at the beginning of a list from an if, loop, or function */
151 static inline void
152 nir_cf_node_insert_begin(struct exec_list *list, nir_cf_node *node)
153 {
154 nir_cf_node_insert(nir_before_cf_list(list), node);
155 }
156
157 /** puts a control flow node at the end of a list from an if, loop, or function */
158 static inline void
159 nir_cf_node_insert_end(struct exec_list *list, nir_cf_node *node)
160 {
161 nir_cf_node_insert(nir_after_cf_list(list), node);
162 }
163
164 /** removes a control flow node, doing any cleanup necessary */
165 void nir_cf_node_remove(nir_cf_node *node);
166
167 #ifdef __cplusplus
168 }
169 #endif