nir: only dereference undef after NULL check. (v2)
[mesa.git] / src / compiler / 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 /* Whether new ALU instructions will be marked "exact" */
35 bool exact;
36
37 nir_shader *shader;
38 nir_function_impl *impl;
39 } nir_builder;
40
41 static inline void
42 nir_builder_init(nir_builder *build, nir_function_impl *impl)
43 {
44 memset(build, 0, sizeof(*build));
45 build->exact = false;
46 build->impl = impl;
47 build->shader = impl->function->shader;
48 }
49
50 static inline void
51 nir_builder_init_simple_shader(nir_builder *build, void *mem_ctx,
52 gl_shader_stage stage,
53 const nir_shader_compiler_options *options)
54 {
55 build->shader = nir_shader_create(mem_ctx, stage, options);
56 nir_function *func = nir_function_create(build->shader, "main");
57 build->exact = false;
58 build->impl = nir_function_impl_create(func);
59 build->cursor = nir_after_cf_list(&build->impl->body);
60 }
61
62 static inline void
63 nir_builder_instr_insert(nir_builder *build, nir_instr *instr)
64 {
65 nir_instr_insert(build->cursor, instr);
66
67 /* Move the cursor forward. */
68 build->cursor = nir_after_instr(instr);
69 }
70
71 static inline void
72 nir_builder_cf_insert(nir_builder *build, nir_cf_node *cf)
73 {
74 nir_cf_node_insert(build->cursor, cf);
75 }
76
77 static inline nir_ssa_def *
78 nir_ssa_undef(nir_builder *build, unsigned num_components, unsigned bit_size)
79 {
80 nir_ssa_undef_instr *undef =
81 nir_ssa_undef_instr_create(build->shader, num_components, bit_size);
82 if (!undef)
83 return NULL;
84
85 nir_instr_insert(nir_before_cf_list(&build->impl->body), &undef->instr);
86
87 return &undef->def;
88 }
89
90 static inline nir_ssa_def *
91 nir_build_imm(nir_builder *build, unsigned num_components, nir_const_value value)
92 {
93 nir_load_const_instr *load_const =
94 nir_load_const_instr_create(build->shader, num_components, 32);
95 if (!load_const)
96 return NULL;
97
98 load_const->value = value;
99
100 nir_builder_instr_insert(build, &load_const->instr);
101
102 return &load_const->def;
103 }
104
105 static inline nir_ssa_def *
106 nir_imm_float(nir_builder *build, float x)
107 {
108 nir_const_value v;
109
110 memset(&v, 0, sizeof(v));
111 v.f32[0] = x;
112
113 return nir_build_imm(build, 1, v);
114 }
115
116 static inline nir_ssa_def *
117 nir_imm_vec4(nir_builder *build, float x, float y, float z, float w)
118 {
119 nir_const_value v;
120
121 memset(&v, 0, sizeof(v));
122 v.f32[0] = x;
123 v.f32[1] = y;
124 v.f32[2] = z;
125 v.f32[3] = w;
126
127 return nir_build_imm(build, 4, v);
128 }
129
130 static inline nir_ssa_def *
131 nir_imm_int(nir_builder *build, int x)
132 {
133 nir_const_value v;
134
135 memset(&v, 0, sizeof(v));
136 v.i32[0] = x;
137
138 return nir_build_imm(build, 1, v);
139 }
140
141 static inline nir_ssa_def *
142 nir_imm_ivec4(nir_builder *build, int x, int y, int z, int w)
143 {
144 nir_const_value v;
145
146 memset(&v, 0, sizeof(v));
147 v.i32[0] = x;
148 v.i32[1] = y;
149 v.i32[2] = z;
150 v.i32[3] = w;
151
152 return nir_build_imm(build, 4, v);
153 }
154
155 static inline nir_ssa_def *
156 nir_build_alu(nir_builder *build, nir_op op, nir_ssa_def *src0,
157 nir_ssa_def *src1, nir_ssa_def *src2, nir_ssa_def *src3)
158 {
159 const nir_op_info *op_info = &nir_op_infos[op];
160 nir_alu_instr *instr = nir_alu_instr_create(build->shader, op);
161 if (!instr)
162 return NULL;
163
164 instr->exact = build->exact;
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 /* Figure out the bitwidth based on the source bitwidth if the instruction
188 * is variable-width.
189 */
190 unsigned bit_size = nir_alu_type_get_type_size(op_info->output_type);
191 if (bit_size == 0) {
192 for (unsigned i = 0; i < op_info->num_inputs; i++) {
193 unsigned src_bit_size = instr->src[i].src.ssa->bit_size;
194 if (nir_alu_type_get_type_size(op_info->input_types[i]) == 0) {
195 if (bit_size)
196 assert(src_bit_size == bit_size);
197 else
198 bit_size = src_bit_size;
199 } else {
200 assert(src_bit_size ==
201 nir_alu_type_get_type_size(op_info->input_types[i]));
202 }
203 }
204 }
205
206 /* Make sure we don't swizzle from outside of our source vector (like if a
207 * scalar value was passed into a multiply with a vector).
208 */
209 for (unsigned i = 0; i < op_info->num_inputs; i++) {
210 for (unsigned j = instr->src[i].src.ssa->num_components; j < 4; j++) {
211 instr->src[i].swizzle[j] = instr->src[i].src.ssa->num_components - 1;
212 }
213 }
214
215 nir_ssa_dest_init(&instr->instr, &instr->dest.dest, num_components,
216 bit_size, NULL);
217 instr->dest.write_mask = (1 << num_components) - 1;
218
219 nir_builder_instr_insert(build, &instr->instr);
220
221 return &instr->dest.dest.ssa;
222 }
223
224 #define ALU1(op) \
225 static inline nir_ssa_def * \
226 nir_##op(nir_builder *build, nir_ssa_def *src0) \
227 { \
228 return nir_build_alu(build, nir_op_##op, src0, NULL, NULL, NULL); \
229 }
230
231 #define ALU2(op) \
232 static inline nir_ssa_def * \
233 nir_##op(nir_builder *build, nir_ssa_def *src0, nir_ssa_def *src1) \
234 { \
235 return nir_build_alu(build, nir_op_##op, src0, src1, NULL, NULL); \
236 }
237
238 #define ALU3(op) \
239 static inline nir_ssa_def * \
240 nir_##op(nir_builder *build, nir_ssa_def *src0, \
241 nir_ssa_def *src1, nir_ssa_def *src2) \
242 { \
243 return nir_build_alu(build, nir_op_##op, src0, src1, src2, NULL); \
244 }
245
246 #define ALU4(op) \
247 static inline nir_ssa_def * \
248 nir_##op(nir_builder *build, nir_ssa_def *src0, \
249 nir_ssa_def *src1, nir_ssa_def *src2, nir_ssa_def *src3) \
250 { \
251 return nir_build_alu(build, nir_op_##op, src0, src1, src2, src3); \
252 }
253
254 #include "nir_builder_opcodes.h"
255
256 static inline nir_ssa_def *
257 nir_vec(nir_builder *build, nir_ssa_def **comp, unsigned num_components)
258 {
259 switch (num_components) {
260 case 4:
261 return nir_vec4(build, comp[0], comp[1], comp[2], comp[3]);
262 case 3:
263 return nir_vec3(build, comp[0], comp[1], comp[2]);
264 case 2:
265 return nir_vec2(build, comp[0], comp[1]);
266 case 1:
267 return comp[0];
268 default:
269 unreachable("bad component count");
270 return NULL;
271 }
272 }
273
274 /**
275 * Similar to nir_fmov, but takes a nir_alu_src instead of a nir_ssa_def.
276 */
277 static inline nir_ssa_def *
278 nir_fmov_alu(nir_builder *build, nir_alu_src src, unsigned num_components)
279 {
280 nir_alu_instr *mov = nir_alu_instr_create(build->shader, nir_op_fmov);
281 nir_ssa_dest_init(&mov->instr, &mov->dest.dest, num_components,
282 nir_src_bit_size(src.src), NULL);
283 mov->exact = build->exact;
284 mov->dest.write_mask = (1 << num_components) - 1;
285 mov->src[0] = src;
286 nir_builder_instr_insert(build, &mov->instr);
287
288 return &mov->dest.dest.ssa;
289 }
290
291 static inline nir_ssa_def *
292 nir_imov_alu(nir_builder *build, nir_alu_src src, unsigned num_components)
293 {
294 nir_alu_instr *mov = nir_alu_instr_create(build->shader, nir_op_imov);
295 nir_ssa_dest_init(&mov->instr, &mov->dest.dest, num_components,
296 nir_src_bit_size(src.src), NULL);
297 mov->exact = build->exact;
298 mov->dest.write_mask = (1 << num_components) - 1;
299 mov->src[0] = src;
300 nir_builder_instr_insert(build, &mov->instr);
301
302 return &mov->dest.dest.ssa;
303 }
304
305 /**
306 * Construct an fmov or imov that reswizzles the source's components.
307 */
308 static inline nir_ssa_def *
309 nir_swizzle(nir_builder *build, nir_ssa_def *src, unsigned swiz[4],
310 unsigned num_components, bool use_fmov)
311 {
312 nir_alu_src alu_src = { NIR_SRC_INIT };
313 alu_src.src = nir_src_for_ssa(src);
314 for (unsigned i = 0; i < num_components; i++)
315 alu_src.swizzle[i] = swiz[i];
316
317 return use_fmov ? nir_fmov_alu(build, alu_src, num_components) :
318 nir_imov_alu(build, alu_src, num_components);
319 }
320
321 /* Selects the right fdot given the number of components in each source. */
322 static inline nir_ssa_def *
323 nir_fdot(nir_builder *build, nir_ssa_def *src0, nir_ssa_def *src1)
324 {
325 assert(src0->num_components == src1->num_components);
326 switch (src0->num_components) {
327 case 1: return nir_fmul(build, src0, src1);
328 case 2: return nir_fdot2(build, src0, src1);
329 case 3: return nir_fdot3(build, src0, src1);
330 case 4: return nir_fdot4(build, src0, src1);
331 default:
332 unreachable("bad component size");
333 }
334
335 return NULL;
336 }
337
338 static inline nir_ssa_def *
339 nir_channel(nir_builder *b, nir_ssa_def *def, unsigned c)
340 {
341 unsigned swizzle[4] = {c, c, c, c};
342 return nir_swizzle(b, def, swizzle, 1, false);
343 }
344
345 /**
346 * Turns a nir_src into a nir_ssa_def * so it can be passed to
347 * nir_build_alu()-based builder calls.
348 *
349 * See nir_ssa_for_alu_src() for alu instructions.
350 */
351 static inline nir_ssa_def *
352 nir_ssa_for_src(nir_builder *build, nir_src src, int num_components)
353 {
354 if (src.is_ssa && src.ssa->num_components == num_components)
355 return src.ssa;
356
357 nir_alu_src alu = { NIR_SRC_INIT };
358 alu.src = src;
359 for (int j = 0; j < 4; j++)
360 alu.swizzle[j] = j;
361
362 return nir_imov_alu(build, alu, num_components);
363 }
364
365 /**
366 * Similar to nir_ssa_for_src(), but for alu src's, respecting the
367 * nir_alu_src's swizzle.
368 */
369 static inline nir_ssa_def *
370 nir_ssa_for_alu_src(nir_builder *build, nir_alu_instr *instr, unsigned srcn)
371 {
372 static uint8_t trivial_swizzle[4] = { 0, 1, 2, 3 };
373 nir_alu_src *src = &instr->src[srcn];
374 unsigned num_components = nir_ssa_alu_instr_src_components(instr, srcn);
375
376 if (src->src.is_ssa && (src->src.ssa->num_components == num_components) &&
377 !src->abs && !src->negate &&
378 (memcmp(src->swizzle, trivial_swizzle, num_components) == 0))
379 return src->src.ssa;
380
381 return nir_imov_alu(build, *src, num_components);
382 }
383
384 static inline nir_ssa_def *
385 nir_load_var(nir_builder *build, nir_variable *var)
386 {
387 const unsigned num_components = glsl_get_vector_elements(var->type);
388
389 nir_intrinsic_instr *load =
390 nir_intrinsic_instr_create(build->shader, nir_intrinsic_load_var);
391 load->num_components = num_components;
392 load->variables[0] = nir_deref_var_create(load, var);
393 nir_ssa_dest_init(&load->instr, &load->dest, num_components,
394 glsl_get_bit_size(glsl_get_base_type(var->type)), NULL);
395 nir_builder_instr_insert(build, &load->instr);
396 return &load->dest.ssa;
397 }
398
399 static inline void
400 nir_store_var(nir_builder *build, nir_variable *var, nir_ssa_def *value,
401 unsigned writemask)
402 {
403 const unsigned num_components = glsl_get_vector_elements(var->type);
404
405 nir_intrinsic_instr *store =
406 nir_intrinsic_instr_create(build->shader, nir_intrinsic_store_var);
407 store->num_components = num_components;
408 nir_intrinsic_set_write_mask(store, writemask);
409 store->variables[0] = nir_deref_var_create(store, var);
410 store->src[0] = nir_src_for_ssa(value);
411 nir_builder_instr_insert(build, &store->instr);
412 }
413
414 static inline void
415 nir_store_deref_var(nir_builder *build, nir_deref_var *deref,
416 nir_ssa_def *value, unsigned writemask)
417 {
418 const unsigned num_components =
419 glsl_get_vector_elements(nir_deref_tail(&deref->deref)->type);
420
421 nir_intrinsic_instr *store =
422 nir_intrinsic_instr_create(build->shader, nir_intrinsic_store_var);
423 store->num_components = num_components;
424 store->const_index[0] = writemask & ((1 << num_components) - 1);
425 store->variables[0] = nir_deref_as_var(nir_copy_deref(store, &deref->deref));
426 store->src[0] = nir_src_for_ssa(value);
427 nir_builder_instr_insert(build, &store->instr);
428 }
429
430 static inline void
431 nir_copy_deref_var(nir_builder *build, nir_deref_var *dest, nir_deref_var *src)
432 {
433 assert(nir_deref_tail(&dest->deref)->type ==
434 nir_deref_tail(&src->deref)->type);
435
436 nir_intrinsic_instr *copy =
437 nir_intrinsic_instr_create(build->shader, nir_intrinsic_copy_var);
438 copy->variables[0] = nir_deref_as_var(nir_copy_deref(copy, &dest->deref));
439 copy->variables[1] = nir_deref_as_var(nir_copy_deref(copy, &src->deref));
440 nir_builder_instr_insert(build, &copy->instr);
441 }
442
443 static inline void
444 nir_copy_var(nir_builder *build, nir_variable *dest, nir_variable *src)
445 {
446 nir_intrinsic_instr *copy =
447 nir_intrinsic_instr_create(build->shader, nir_intrinsic_copy_var);
448 copy->variables[0] = nir_deref_var_create(copy, dest);
449 copy->variables[1] = nir_deref_var_create(copy, src);
450 nir_builder_instr_insert(build, &copy->instr);
451 }
452
453 static inline nir_ssa_def *
454 nir_load_system_value(nir_builder *build, nir_intrinsic_op op, int index)
455 {
456 nir_intrinsic_instr *load = nir_intrinsic_instr_create(build->shader, op);
457 load->num_components = nir_intrinsic_infos[op].dest_components;
458 load->const_index[0] = index;
459 nir_ssa_dest_init(&load->instr, &load->dest,
460 nir_intrinsic_infos[op].dest_components, 32, NULL);
461 nir_builder_instr_insert(build, &load->instr);
462 return &load->dest.ssa;
463 }
464
465 static inline void
466 nir_jump(nir_builder *build, nir_jump_type jump_type)
467 {
468 nir_jump_instr *jump = nir_jump_instr_create(build->shader, jump_type);
469 nir_builder_instr_insert(build, &jump->instr);
470 }
471
472 #endif /* NIR_BUILDER_H */