nir: Convert the builder to use the new NIR cursor API.
[mesa.git] / src / glsl / nir / nir_builder.h
1 /*
2 * Copyright © 2014-2015 Broadcom
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
24 #ifndef NIR_BUILDER_H
25 #define NIR_BUILDER_H
26
27 #include "nir_control_flow.h"
28
29 struct exec_list;
30
31 typedef struct nir_builder {
32 nir_cursor cursor;
33
34 nir_shader *shader;
35 nir_function_impl *impl;
36 } nir_builder;
37
38 static inline void
39 nir_builder_init(nir_builder *build, nir_function_impl *impl)
40 {
41 memset(build, 0, sizeof(*build));
42 build->impl = impl;
43 build->shader = impl->overload->function->shader;
44 }
45
46 static inline void
47 nir_builder_instr_insert(nir_builder *build, nir_instr *instr)
48 {
49 nir_instr_insert(build->cursor, instr);
50
51 /* Move the cursor forward. */
52 if (build->cursor.option == nir_cursor_after_instr)
53 build->cursor.instr = instr;
54 }
55
56 static inline void
57 nir_builder_cf_insert(nir_builder *build, nir_cf_node *cf)
58 {
59 nir_cf_node_insert(build->cursor, cf);
60 }
61
62 static inline nir_ssa_def *
63 nir_build_imm(nir_builder *build, unsigned num_components, nir_const_value value)
64 {
65 nir_load_const_instr *load_const =
66 nir_load_const_instr_create(build->shader, num_components);
67 if (!load_const)
68 return NULL;
69
70 load_const->value = value;
71
72 nir_builder_instr_insert(build, &load_const->instr);
73
74 return &load_const->def;
75 }
76
77 static inline nir_ssa_def *
78 nir_imm_float(nir_builder *build, float x)
79 {
80 nir_const_value v = { { .f = {x, 0, 0, 0} } };
81 return nir_build_imm(build, 1, v);
82 }
83
84 static inline nir_ssa_def *
85 nir_imm_vec4(nir_builder *build, float x, float y, float z, float w)
86 {
87 nir_const_value v = { { .f = {x, y, z, w} } };
88 return nir_build_imm(build, 4, v);
89 }
90
91 static inline nir_ssa_def *
92 nir_imm_int(nir_builder *build, int x)
93 {
94 nir_const_value v = { { .i = {x, 0, 0, 0} } };
95 return nir_build_imm(build, 1, v);
96 }
97
98 static inline nir_ssa_def *
99 nir_build_alu(nir_builder *build, nir_op op, nir_ssa_def *src0,
100 nir_ssa_def *src1, nir_ssa_def *src2, nir_ssa_def *src3)
101 {
102 const nir_op_info *op_info = &nir_op_infos[op];
103 nir_alu_instr *instr = nir_alu_instr_create(build->shader, op);
104 if (!instr)
105 return NULL;
106
107 instr->src[0].src = nir_src_for_ssa(src0);
108 if (src1)
109 instr->src[1].src = nir_src_for_ssa(src1);
110 if (src2)
111 instr->src[2].src = nir_src_for_ssa(src2);
112 if (src3)
113 instr->src[3].src = nir_src_for_ssa(src3);
114
115 /* Guess the number of components the destination temporary should have
116 * based on our input sizes, if it's not fixed for the op.
117 */
118 unsigned num_components = op_info->output_size;
119 if (num_components == 0) {
120 for (unsigned i = 0; i < op_info->num_inputs; i++) {
121 if (op_info->input_sizes[i] == 0)
122 num_components = MAX2(num_components,
123 instr->src[i].src.ssa->num_components);
124 }
125 }
126 assert(num_components != 0);
127
128 /* Make sure we don't swizzle from outside of our source vector (like if a
129 * scalar value was passed into a multiply with a vector).
130 */
131 for (unsigned i = 0; i < op_info->num_inputs; i++) {
132 for (unsigned j = instr->src[i].src.ssa->num_components; j < 4; j++) {
133 instr->src[i].swizzle[j] = instr->src[i].src.ssa->num_components - 1;
134 }
135 }
136
137 nir_ssa_dest_init(&instr->instr, &instr->dest.dest, num_components, NULL);
138 instr->dest.write_mask = (1 << num_components) - 1;
139
140 nir_builder_instr_insert(build, &instr->instr);
141
142 return &instr->dest.dest.ssa;
143 }
144
145 #define ALU1(op) \
146 static inline nir_ssa_def * \
147 nir_##op(nir_builder *build, nir_ssa_def *src0) \
148 { \
149 return nir_build_alu(build, nir_op_##op, src0, NULL, NULL, NULL); \
150 }
151
152 #define ALU2(op) \
153 static inline nir_ssa_def * \
154 nir_##op(nir_builder *build, nir_ssa_def *src0, nir_ssa_def *src1) \
155 { \
156 return nir_build_alu(build, nir_op_##op, src0, src1, NULL, NULL); \
157 }
158
159 #define ALU3(op) \
160 static inline nir_ssa_def * \
161 nir_##op(nir_builder *build, nir_ssa_def *src0, \
162 nir_ssa_def *src1, nir_ssa_def *src2) \
163 { \
164 return nir_build_alu(build, nir_op_##op, src0, src1, src2, NULL); \
165 }
166
167 #define ALU4(op) \
168 static inline nir_ssa_def * \
169 nir_##op(nir_builder *build, nir_ssa_def *src0, \
170 nir_ssa_def *src1, nir_ssa_def *src2, nir_ssa_def *src3) \
171 { \
172 return nir_build_alu(build, nir_op_##op, src0, src1, src2, src3); \
173 }
174
175 #include "nir_builder_opcodes.h"
176
177 /**
178 * Similar to nir_fmov, but takes a nir_alu_src instead of a nir_ssa_def.
179 */
180 static inline nir_ssa_def *
181 nir_fmov_alu(nir_builder *build, nir_alu_src src, unsigned num_components)
182 {
183 nir_alu_instr *mov = nir_alu_instr_create(build->shader, nir_op_fmov);
184 nir_ssa_dest_init(&mov->instr, &mov->dest.dest, num_components, NULL);
185 mov->dest.write_mask = (1 << num_components) - 1;
186 mov->src[0] = src;
187 nir_builder_instr_insert(build, &mov->instr);
188
189 return &mov->dest.dest.ssa;
190 }
191
192 static inline nir_ssa_def *
193 nir_imov_alu(nir_builder *build, nir_alu_src src, unsigned num_components)
194 {
195 nir_alu_instr *mov = nir_alu_instr_create(build->shader, nir_op_imov);
196 nir_ssa_dest_init(&mov->instr, &mov->dest.dest, num_components, NULL);
197 mov->dest.write_mask = (1 << num_components) - 1;
198 mov->src[0] = src;
199 nir_builder_instr_insert(build, &mov->instr);
200
201 return &mov->dest.dest.ssa;
202 }
203
204 /**
205 * Construct an fmov or imov that reswizzles the source's components.
206 */
207 static inline nir_ssa_def *
208 nir_swizzle(nir_builder *build, nir_ssa_def *src, unsigned swiz[4],
209 unsigned num_components, bool use_fmov)
210 {
211 nir_alu_src alu_src = { NIR_SRC_INIT };
212 alu_src.src = nir_src_for_ssa(src);
213 for (int i = 0; i < 4; i++)
214 alu_src.swizzle[i] = swiz[i];
215
216 return use_fmov ? nir_fmov_alu(build, alu_src, num_components) :
217 nir_imov_alu(build, alu_src, num_components);
218 }
219
220 /**
221 * Turns a nir_src into a nir_ssa_def * so it can be passed to
222 * nir_build_alu()-based builder calls.
223 */
224 static inline nir_ssa_def *
225 nir_ssa_for_src(nir_builder *build, nir_src src, int num_components)
226 {
227 if (src.is_ssa && src.ssa->num_components == num_components)
228 return src.ssa;
229
230 nir_alu_src alu = { NIR_SRC_INIT };
231 alu.src = src;
232 for (int j = 0; j < 4; j++)
233 alu.swizzle[j] = j;
234
235 return nir_imov_alu(build, alu, num_components);
236 }
237
238 #endif /* NIR_BUILDER_H */