Merge remote-tracking branch 'mesa-public/master' into vulkan
[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 struct exec_list;
28
29 typedef struct nir_builder {
30 struct exec_list *cf_node_list;
31
32 nir_block *before_block;
33 nir_block *after_block;
34
35 nir_instr *before_instr;
36 nir_instr *after_instr;
37
38 nir_shader *shader;
39 nir_function_impl *impl;
40 } nir_builder;
41
42 static inline void
43 nir_builder_init(nir_builder *build, nir_function_impl *impl)
44 {
45 memset(build, 0, sizeof(*build));
46 build->impl = impl;
47 build->shader = impl->overload->function->shader;
48 }
49
50 static inline void
51 nir_builder_insert_after_cf_list(nir_builder *build,
52 struct exec_list *cf_node_list)
53 {
54 build->cf_node_list = cf_node_list;
55 build->before_block = NULL;
56 build->after_block = NULL;
57 build->before_instr = NULL;
58 build->after_instr = NULL;
59 }
60
61 static inline void
62 nir_builder_insert_before_block(nir_builder *build,
63 nir_block *block)
64 {
65 build->cf_node_list = NULL;
66 build->before_block = block;
67 build->after_block = NULL;
68 build->before_instr = NULL;
69 build->after_instr = NULL;
70 }
71
72 static inline void
73 nir_builder_insert_after_block(nir_builder *build,
74 nir_block *block)
75 {
76 build->cf_node_list = NULL;
77 build->before_block = NULL;
78 build->after_block = block;
79 build->before_instr = NULL;
80 build->after_instr = NULL;
81 }
82
83 static inline void
84 nir_builder_insert_before_instr(nir_builder *build, nir_instr *before_instr)
85 {
86 build->cf_node_list = NULL;
87 build->before_block = NULL;
88 build->after_block = NULL;
89 build->before_instr = before_instr;
90 build->after_instr = NULL;
91 }
92
93 static inline void
94 nir_builder_insert_after_instr(nir_builder *build, nir_instr *after_instr)
95 {
96 build->cf_node_list = NULL;
97 build->before_block = NULL;
98 build->after_block = NULL;
99 build->before_instr = NULL;
100 build->after_instr = after_instr;
101 }
102
103 static inline void
104 nir_builder_instr_insert(nir_builder *build, nir_instr *instr)
105 {
106 if (build->cf_node_list) {
107 nir_instr_insert_after_cf_list(build->cf_node_list, instr);
108 } else if (build->before_block) {
109 nir_instr_insert_before_block(build->before_block, instr);
110 } else if (build->after_block) {
111 nir_instr_insert_after_block(build->after_block, instr);
112 } else if (build->before_instr) {
113 nir_instr_insert_before(build->before_instr, instr);
114 } else {
115 assert(build->after_instr);
116 nir_instr_insert_after(build->after_instr, instr);
117 build->after_instr = instr;
118 }
119 }
120
121 static inline nir_ssa_def *
122 nir_build_imm(nir_builder *build, unsigned num_components, nir_const_value value)
123 {
124 nir_load_const_instr *load_const =
125 nir_load_const_instr_create(build->shader, num_components);
126 if (!load_const)
127 return NULL;
128
129 load_const->value = value;
130
131 nir_builder_instr_insert(build, &load_const->instr);
132
133 return &load_const->def;
134 }
135
136 static inline nir_ssa_def *
137 nir_imm_float(nir_builder *build, float x)
138 {
139 nir_const_value v = { { .f = {x, 0, 0, 0} } };
140 return nir_build_imm(build, 1, v);
141 }
142
143 static inline nir_ssa_def *
144 nir_imm_vec4(nir_builder *build, float x, float y, float z, float w)
145 {
146 nir_const_value v = { { .f = {x, y, z, w} } };
147 return nir_build_imm(build, 4, v);
148 }
149
150 static inline nir_ssa_def *
151 nir_imm_int(nir_builder *build, int x)
152 {
153 nir_const_value v = { { .i = {x, 0, 0, 0} } };
154 return nir_build_imm(build, 1, v);
155 }
156
157 static inline nir_ssa_def *
158 nir_build_alu(nir_builder *build, nir_op op, nir_ssa_def *src0,
159 nir_ssa_def *src1, nir_ssa_def *src2, nir_ssa_def *src3)
160 {
161 const nir_op_info *op_info = &nir_op_infos[op];
162 nir_alu_instr *instr = nir_alu_instr_create(build->shader, op);
163 if (!instr)
164 return NULL;
165
166 instr->src[0].src = nir_src_for_ssa(src0);
167 if (src1)
168 instr->src[1].src = nir_src_for_ssa(src1);
169 if (src2)
170 instr->src[2].src = nir_src_for_ssa(src2);
171 if (src3)
172 instr->src[3].src = nir_src_for_ssa(src3);
173
174 /* Guess the number of components the destination temporary should have
175 * based on our input sizes, if it's not fixed for the op.
176 */
177 unsigned num_components = op_info->output_size;
178 if (num_components == 0) {
179 for (unsigned i = 0; i < op_info->num_inputs; i++) {
180 if (op_info->input_sizes[i] == 0)
181 num_components = MAX2(num_components,
182 instr->src[i].src.ssa->num_components);
183 }
184 }
185 assert(num_components != 0);
186
187 /* Make sure we don't swizzle from outside of our source vector (like if a
188 * scalar value was passed into a multiply with a vector).
189 */
190 for (unsigned i = 0; i < op_info->num_inputs; i++) {
191 for (unsigned j = instr->src[i].src.ssa->num_components; j < 4; j++) {
192 instr->src[i].swizzle[j] = instr->src[i].src.ssa->num_components - 1;
193 }
194 }
195
196 nir_ssa_dest_init(&instr->instr, &instr->dest.dest, num_components, NULL);
197 instr->dest.write_mask = (1 << num_components) - 1;
198
199 nir_builder_instr_insert(build, &instr->instr);
200
201 return &instr->dest.dest.ssa;
202 }
203
204 #define ALU1(op) \
205 static inline nir_ssa_def * \
206 nir_##op(nir_builder *build, nir_ssa_def *src0) \
207 { \
208 return nir_build_alu(build, nir_op_##op, src0, NULL, NULL, NULL); \
209 }
210
211 #define ALU2(op) \
212 static inline nir_ssa_def * \
213 nir_##op(nir_builder *build, nir_ssa_def *src0, nir_ssa_def *src1) \
214 { \
215 return nir_build_alu(build, nir_op_##op, src0, src1, NULL, NULL); \
216 }
217
218 #define ALU3(op) \
219 static inline nir_ssa_def * \
220 nir_##op(nir_builder *build, nir_ssa_def *src0, \
221 nir_ssa_def *src1, nir_ssa_def *src2) \
222 { \
223 return nir_build_alu(build, nir_op_##op, src0, src1, src2, NULL); \
224 }
225
226 #define ALU4(op) \
227 static inline nir_ssa_def * \
228 nir_##op(nir_builder *build, nir_ssa_def *src0, \
229 nir_ssa_def *src1, nir_ssa_def *src2, nir_ssa_def *src3) \
230 { \
231 return nir_build_alu(build, nir_op_##op, src0, src1, src2, src3); \
232 }
233
234 #include "nir_builder_opcodes.h"
235
236 /**
237 * Similar to nir_fmov, but takes a nir_alu_src instead of a nir_ssa_def.
238 */
239 static inline nir_ssa_def *
240 nir_fmov_alu(nir_builder *build, nir_alu_src src, unsigned num_components)
241 {
242 nir_alu_instr *mov = nir_alu_instr_create(build->shader, nir_op_fmov);
243 nir_ssa_dest_init(&mov->instr, &mov->dest.dest, num_components, NULL);
244 mov->dest.write_mask = (1 << num_components) - 1;
245 mov->src[0] = src;
246 nir_builder_instr_insert(build, &mov->instr);
247
248 return &mov->dest.dest.ssa;
249 }
250
251 static inline nir_ssa_def *
252 nir_imov_alu(nir_builder *build, nir_alu_src src, unsigned num_components)
253 {
254 nir_alu_instr *mov = nir_alu_instr_create(build->shader, nir_op_imov);
255 nir_ssa_dest_init(&mov->instr, &mov->dest.dest, num_components, NULL);
256 mov->dest.write_mask = (1 << num_components) - 1;
257 mov->src[0] = src;
258 nir_builder_instr_insert(build, &mov->instr);
259
260 return &mov->dest.dest.ssa;
261 }
262
263 /**
264 * Construct an fmov or imov that reswizzles the source's components.
265 */
266 static inline nir_ssa_def *
267 nir_swizzle(nir_builder *build, nir_ssa_def *src, unsigned swiz[4],
268 unsigned num_components, bool use_fmov)
269 {
270 nir_alu_src alu_src = { NIR_SRC_INIT };
271 alu_src.src = nir_src_for_ssa(src);
272 for (int i = 0; i < 4; i++)
273 alu_src.swizzle[i] = swiz[i];
274
275 return use_fmov ? nir_fmov_alu(build, alu_src, num_components) :
276 nir_imov_alu(build, alu_src, num_components);
277 }
278
279 /* Selects the right fdot given the number of components in each source. */
280 static inline nir_ssa_def *
281 nir_fdot(nir_builder *build, nir_ssa_def *src0, nir_ssa_def *src1)
282 {
283 assert(src0->num_components == src1->num_components);
284 switch (src0->num_components) {
285 case 1: return nir_fmul(build, src0, src1);
286 case 2: return nir_fdot2(build, src0, src1);
287 case 3: return nir_fdot3(build, src0, src1);
288 case 4: return nir_fdot4(build, src0, src1);
289 default:
290 unreachable("bad component size");
291 }
292
293 return NULL;
294 }
295
296 /**
297 * Turns a nir_src into a nir_ssa_def * so it can be passed to
298 * nir_build_alu()-based builder calls.
299 */
300 static inline nir_ssa_def *
301 nir_ssa_for_src(nir_builder *build, nir_src src, int num_components)
302 {
303 if (src.is_ssa && src.ssa->num_components == num_components)
304 return src.ssa;
305
306 nir_alu_src alu = { NIR_SRC_INIT };
307 alu.src = src;
308 for (int j = 0; j < 4; j++)
309 alu.swizzle[j] = j;
310
311 return nir_imov_alu(build, alu, num_components);
312 }
313
314 #endif /* NIR_BUILDER_H */