nir: Add nir_imm_ivec2 helper
[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, NULL);
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 nir_instr *
72 nir_builder_last_instr(nir_builder *build)
73 {
74 assert(build->cursor.option == nir_cursor_after_instr);
75 return build->cursor.instr;
76 }
77
78 static inline void
79 nir_builder_cf_insert(nir_builder *build, nir_cf_node *cf)
80 {
81 nir_cf_node_insert(build->cursor, cf);
82 }
83
84 static inline bool
85 nir_builder_is_inside_cf(nir_builder *build, nir_cf_node *cf_node)
86 {
87 nir_block *block = nir_cursor_current_block(build->cursor);
88 for (nir_cf_node *n = &block->cf_node; n; n = n->parent) {
89 if (n == cf_node)
90 return true;
91 }
92 return false;
93 }
94
95 static inline nir_if *
96 nir_push_if(nir_builder *build, nir_ssa_def *condition)
97 {
98 nir_if *nif = nir_if_create(build->shader);
99 nif->condition = nir_src_for_ssa(condition);
100 nir_builder_cf_insert(build, &nif->cf_node);
101 build->cursor = nir_before_cf_list(&nif->then_list);
102 return nif;
103 }
104
105 static inline nir_if *
106 nir_push_else(nir_builder *build, nir_if *nif)
107 {
108 if (nif) {
109 assert(nir_builder_is_inside_cf(build, &nif->cf_node));
110 } else {
111 nir_block *block = nir_cursor_current_block(build->cursor);
112 nif = nir_cf_node_as_if(block->cf_node.parent);
113 }
114 build->cursor = nir_before_cf_list(&nif->else_list);
115 return nif;
116 }
117
118 static inline void
119 nir_pop_if(nir_builder *build, nir_if *nif)
120 {
121 if (nif) {
122 assert(nir_builder_is_inside_cf(build, &nif->cf_node));
123 } else {
124 nir_block *block = nir_cursor_current_block(build->cursor);
125 nif = nir_cf_node_as_if(block->cf_node.parent);
126 }
127 build->cursor = nir_after_cf_node(&nif->cf_node);
128 }
129
130 static inline nir_ssa_def *
131 nir_if_phi(nir_builder *build, nir_ssa_def *then_def, nir_ssa_def *else_def)
132 {
133 nir_block *block = nir_cursor_current_block(build->cursor);
134 nir_if *nif = nir_cf_node_as_if(nir_cf_node_prev(&block->cf_node));
135
136 nir_phi_instr *phi = nir_phi_instr_create(build->shader);
137
138 nir_phi_src *src = ralloc(phi, nir_phi_src);
139 src->pred = nir_if_last_then_block(nif);
140 src->src = nir_src_for_ssa(then_def);
141 exec_list_push_tail(&phi->srcs, &src->node);
142
143 src = ralloc(phi, nir_phi_src);
144 src->pred = nir_if_last_else_block(nif);
145 src->src = nir_src_for_ssa(else_def);
146 exec_list_push_tail(&phi->srcs, &src->node);
147
148 assert(then_def->num_components == else_def->num_components);
149 assert(then_def->bit_size == else_def->bit_size);
150 nir_ssa_dest_init(&phi->instr, &phi->dest,
151 then_def->num_components, then_def->bit_size, NULL);
152
153 nir_builder_instr_insert(build, &phi->instr);
154
155 return &phi->dest.ssa;
156 }
157
158 static inline nir_loop *
159 nir_push_loop(nir_builder *build)
160 {
161 nir_loop *loop = nir_loop_create(build->shader);
162 nir_builder_cf_insert(build, &loop->cf_node);
163 build->cursor = nir_before_cf_list(&loop->body);
164 return loop;
165 }
166
167 static inline void
168 nir_pop_loop(nir_builder *build, nir_loop *loop)
169 {
170 if (loop) {
171 assert(nir_builder_is_inside_cf(build, &loop->cf_node));
172 } else {
173 nir_block *block = nir_cursor_current_block(build->cursor);
174 loop = nir_cf_node_as_loop(block->cf_node.parent);
175 }
176 build->cursor = nir_after_cf_node(&loop->cf_node);
177 }
178
179 static inline nir_ssa_def *
180 nir_ssa_undef(nir_builder *build, unsigned num_components, unsigned bit_size)
181 {
182 nir_ssa_undef_instr *undef =
183 nir_ssa_undef_instr_create(build->shader, num_components, bit_size);
184 if (!undef)
185 return NULL;
186
187 nir_instr_insert(nir_before_cf_list(&build->impl->body), &undef->instr);
188
189 return &undef->def;
190 }
191
192 static inline nir_ssa_def *
193 nir_build_imm(nir_builder *build, unsigned num_components,
194 unsigned bit_size, nir_const_value value)
195 {
196 nir_load_const_instr *load_const =
197 nir_load_const_instr_create(build->shader, num_components, bit_size);
198 if (!load_const)
199 return NULL;
200
201 load_const->value = value;
202
203 nir_builder_instr_insert(build, &load_const->instr);
204
205 return &load_const->def;
206 }
207
208 static inline nir_ssa_def *
209 nir_imm_float(nir_builder *build, float x)
210 {
211 nir_const_value v;
212
213 memset(&v, 0, sizeof(v));
214 v.f32[0] = x;
215
216 return nir_build_imm(build, 1, 32, v);
217 }
218
219 static inline nir_ssa_def *
220 nir_imm_double(nir_builder *build, double x)
221 {
222 nir_const_value v;
223
224 memset(&v, 0, sizeof(v));
225 v.f64[0] = x;
226
227 return nir_build_imm(build, 1, 64, v);
228 }
229
230 static inline nir_ssa_def *
231 nir_imm_floatN_t(nir_builder *build, double x, unsigned bit_size)
232 {
233 switch (bit_size) {
234 case 32:
235 return nir_imm_float(build, x);
236 case 64:
237 return nir_imm_double(build, x);
238 }
239
240 unreachable("unknown float immediate bit size");
241 }
242
243 static inline nir_ssa_def *
244 nir_imm_vec4(nir_builder *build, float x, float y, float z, float w)
245 {
246 nir_const_value v;
247
248 memset(&v, 0, sizeof(v));
249 v.f32[0] = x;
250 v.f32[1] = y;
251 v.f32[2] = z;
252 v.f32[3] = w;
253
254 return nir_build_imm(build, 4, 32, v);
255 }
256
257 static inline nir_ssa_def *
258 nir_imm_ivec2(nir_builder *build, int x, int y)
259 {
260 nir_const_value v;
261
262 memset(&v, 0, sizeof(v));
263 v.i32[0] = x;
264 v.i32[1] = y;
265
266 return nir_build_imm(build, 2, 32, v);
267 }
268
269 static inline nir_ssa_def *
270 nir_imm_int(nir_builder *build, int x)
271 {
272 nir_const_value v;
273
274 memset(&v, 0, sizeof(v));
275 v.i32[0] = x;
276
277 return nir_build_imm(build, 1, 32, v);
278 }
279
280 static inline nir_ssa_def *
281 nir_imm_int64(nir_builder *build, int64_t x)
282 {
283 nir_const_value v;
284
285 memset(&v, 0, sizeof(v));
286 v.i64[0] = x;
287
288 return nir_build_imm(build, 1, 64, v);
289 }
290
291 static inline nir_ssa_def *
292 nir_imm_intN_t(nir_builder *build, uint64_t x, unsigned bit_size)
293 {
294 nir_const_value v;
295
296 memset(&v, 0, sizeof(v));
297 assert(bit_size <= 64);
298 v.i64[0] = x & (~0ull >> (64 - bit_size));
299
300 return nir_build_imm(build, 1, bit_size, v);
301 }
302
303 static inline nir_ssa_def *
304 nir_imm_ivec4(nir_builder *build, int x, int y, int z, int w)
305 {
306 nir_const_value v;
307
308 memset(&v, 0, sizeof(v));
309 v.i32[0] = x;
310 v.i32[1] = y;
311 v.i32[2] = z;
312 v.i32[3] = w;
313
314 return nir_build_imm(build, 4, 32, v);
315 }
316
317 static inline nir_ssa_def *
318 nir_build_alu(nir_builder *build, nir_op op, nir_ssa_def *src0,
319 nir_ssa_def *src1, nir_ssa_def *src2, nir_ssa_def *src3)
320 {
321 const nir_op_info *op_info = &nir_op_infos[op];
322 nir_alu_instr *instr = nir_alu_instr_create(build->shader, op);
323 if (!instr)
324 return NULL;
325
326 instr->exact = build->exact;
327
328 instr->src[0].src = nir_src_for_ssa(src0);
329 if (src1)
330 instr->src[1].src = nir_src_for_ssa(src1);
331 if (src2)
332 instr->src[2].src = nir_src_for_ssa(src2);
333 if (src3)
334 instr->src[3].src = nir_src_for_ssa(src3);
335
336 /* Guess the number of components the destination temporary should have
337 * based on our input sizes, if it's not fixed for the op.
338 */
339 unsigned num_components = op_info->output_size;
340 if (num_components == 0) {
341 for (unsigned i = 0; i < op_info->num_inputs; i++) {
342 if (op_info->input_sizes[i] == 0)
343 num_components = MAX2(num_components,
344 instr->src[i].src.ssa->num_components);
345 }
346 }
347 assert(num_components != 0);
348
349 /* Figure out the bitwidth based on the source bitwidth if the instruction
350 * is variable-width.
351 */
352 unsigned bit_size = nir_alu_type_get_type_size(op_info->output_type);
353 if (bit_size == 0) {
354 for (unsigned i = 0; i < op_info->num_inputs; i++) {
355 unsigned src_bit_size = instr->src[i].src.ssa->bit_size;
356 if (nir_alu_type_get_type_size(op_info->input_types[i]) == 0) {
357 if (bit_size)
358 assert(src_bit_size == bit_size);
359 else
360 bit_size = src_bit_size;
361 } else {
362 assert(src_bit_size ==
363 nir_alu_type_get_type_size(op_info->input_types[i]));
364 }
365 }
366 }
367
368 /* When in doubt, assume 32. */
369 if (bit_size == 0)
370 bit_size = 32;
371
372 /* Make sure we don't swizzle from outside of our source vector (like if a
373 * scalar value was passed into a multiply with a vector).
374 */
375 for (unsigned i = 0; i < op_info->num_inputs; i++) {
376 for (unsigned j = instr->src[i].src.ssa->num_components;
377 j < NIR_MAX_VEC_COMPONENTS; j++) {
378 instr->src[i].swizzle[j] = instr->src[i].src.ssa->num_components - 1;
379 }
380 }
381
382 nir_ssa_dest_init(&instr->instr, &instr->dest.dest, num_components,
383 bit_size, NULL);
384 instr->dest.write_mask = (1 << num_components) - 1;
385
386 nir_builder_instr_insert(build, &instr->instr);
387
388 return &instr->dest.dest.ssa;
389 }
390
391 #include "nir_builder_opcodes.h"
392
393 static inline nir_ssa_def *
394 nir_vec(nir_builder *build, nir_ssa_def **comp, unsigned num_components)
395 {
396 switch (num_components) {
397 case 4:
398 return nir_vec4(build, comp[0], comp[1], comp[2], comp[3]);
399 case 3:
400 return nir_vec3(build, comp[0], comp[1], comp[2]);
401 case 2:
402 return nir_vec2(build, comp[0], comp[1]);
403 case 1:
404 return comp[0];
405 default:
406 unreachable("bad component count");
407 return NULL;
408 }
409 }
410
411 /**
412 * Similar to nir_fmov, but takes a nir_alu_src instead of a nir_ssa_def.
413 */
414 static inline nir_ssa_def *
415 nir_fmov_alu(nir_builder *build, nir_alu_src src, unsigned num_components)
416 {
417 nir_alu_instr *mov = nir_alu_instr_create(build->shader, nir_op_fmov);
418 nir_ssa_dest_init(&mov->instr, &mov->dest.dest, num_components,
419 nir_src_bit_size(src.src), NULL);
420 mov->exact = build->exact;
421 mov->dest.write_mask = (1 << num_components) - 1;
422 mov->src[0] = src;
423 nir_builder_instr_insert(build, &mov->instr);
424
425 return &mov->dest.dest.ssa;
426 }
427
428 static inline nir_ssa_def *
429 nir_imov_alu(nir_builder *build, nir_alu_src src, unsigned num_components)
430 {
431 nir_alu_instr *mov = nir_alu_instr_create(build->shader, nir_op_imov);
432 nir_ssa_dest_init(&mov->instr, &mov->dest.dest, num_components,
433 nir_src_bit_size(src.src), NULL);
434 mov->exact = build->exact;
435 mov->dest.write_mask = (1 << num_components) - 1;
436 mov->src[0] = src;
437 nir_builder_instr_insert(build, &mov->instr);
438
439 return &mov->dest.dest.ssa;
440 }
441
442 /**
443 * Construct an fmov or imov that reswizzles the source's components.
444 */
445 static inline nir_ssa_def *
446 nir_swizzle(nir_builder *build, nir_ssa_def *src, const unsigned *swiz,
447 unsigned num_components, bool use_fmov)
448 {
449 assert(num_components <= NIR_MAX_VEC_COMPONENTS);
450 nir_alu_src alu_src = { NIR_SRC_INIT };
451 alu_src.src = nir_src_for_ssa(src);
452 for (unsigned i = 0; i < num_components && i < NIR_MAX_VEC_COMPONENTS; i++)
453 alu_src.swizzle[i] = swiz[i];
454
455 return use_fmov ? nir_fmov_alu(build, alu_src, num_components) :
456 nir_imov_alu(build, alu_src, num_components);
457 }
458
459 /* Selects the right fdot given the number of components in each source. */
460 static inline nir_ssa_def *
461 nir_fdot(nir_builder *build, nir_ssa_def *src0, nir_ssa_def *src1)
462 {
463 assert(src0->num_components == src1->num_components);
464 switch (src0->num_components) {
465 case 1: return nir_fmul(build, src0, src1);
466 case 2: return nir_fdot2(build, src0, src1);
467 case 3: return nir_fdot3(build, src0, src1);
468 case 4: return nir_fdot4(build, src0, src1);
469 default:
470 unreachable("bad component size");
471 }
472
473 return NULL;
474 }
475
476 static inline nir_ssa_def *
477 nir_bany_inequal(nir_builder *b, nir_ssa_def *src0, nir_ssa_def *src1)
478 {
479 switch (src0->num_components) {
480 case 1: return nir_ine(b, src0, src1);
481 case 2: return nir_bany_inequal2(b, src0, src1);
482 case 3: return nir_bany_inequal3(b, src0, src1);
483 case 4: return nir_bany_inequal4(b, src0, src1);
484 default:
485 unreachable("bad component size");
486 }
487 }
488
489 static inline nir_ssa_def *
490 nir_bany(nir_builder *b, nir_ssa_def *src)
491 {
492 return nir_bany_inequal(b, src, nir_imm_int(b, 0));
493 }
494
495 static inline nir_ssa_def *
496 nir_channel(nir_builder *b, nir_ssa_def *def, unsigned c)
497 {
498 return nir_swizzle(b, def, &c, 1, false);
499 }
500
501 static inline nir_ssa_def *
502 nir_channels(nir_builder *b, nir_ssa_def *def, nir_component_mask_t mask)
503 {
504 unsigned num_channels = 0, swizzle[NIR_MAX_VEC_COMPONENTS] = { 0 };
505
506 for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++) {
507 if ((mask & (1 << i)) == 0)
508 continue;
509 swizzle[num_channels++] = i;
510 }
511
512 return nir_swizzle(b, def, swizzle, num_channels, false);
513 }
514
515 /**
516 * Turns a nir_src into a nir_ssa_def * so it can be passed to
517 * nir_build_alu()-based builder calls.
518 *
519 * See nir_ssa_for_alu_src() for alu instructions.
520 */
521 static inline nir_ssa_def *
522 nir_ssa_for_src(nir_builder *build, nir_src src, int num_components)
523 {
524 if (src.is_ssa && src.ssa->num_components == num_components)
525 return src.ssa;
526
527 nir_alu_src alu = { NIR_SRC_INIT };
528 alu.src = src;
529 for (int j = 0; j < 4; j++)
530 alu.swizzle[j] = j;
531
532 return nir_imov_alu(build, alu, num_components);
533 }
534
535 /**
536 * Similar to nir_ssa_for_src(), but for alu srcs, respecting the
537 * nir_alu_src's swizzle.
538 */
539 static inline nir_ssa_def *
540 nir_ssa_for_alu_src(nir_builder *build, nir_alu_instr *instr, unsigned srcn)
541 {
542 static uint8_t trivial_swizzle[NIR_MAX_VEC_COMPONENTS];
543 for (int i = 0; i < NIR_MAX_VEC_COMPONENTS; ++i)
544 trivial_swizzle[i] = i;
545 nir_alu_src *src = &instr->src[srcn];
546 unsigned num_components = nir_ssa_alu_instr_src_components(instr, srcn);
547
548 if (src->src.is_ssa && (src->src.ssa->num_components == num_components) &&
549 !src->abs && !src->negate &&
550 (memcmp(src->swizzle, trivial_swizzle, num_components) == 0))
551 return src->src.ssa;
552
553 return nir_imov_alu(build, *src, num_components);
554 }
555
556 static inline nir_deref_instr *
557 nir_build_deref_var(nir_builder *build, nir_variable *var)
558 {
559 nir_deref_instr *deref =
560 nir_deref_instr_create(build->shader, nir_deref_type_var);
561
562 deref->mode = var->data.mode;
563 deref->type = var->type;
564 deref->var = var;
565
566 nir_ssa_dest_init(&deref->instr, &deref->dest, 1, 32, NULL);
567
568 nir_builder_instr_insert(build, &deref->instr);
569
570 return deref;
571 }
572
573 static inline nir_deref_instr *
574 nir_build_deref_array(nir_builder *build, nir_deref_instr *parent,
575 nir_ssa_def *index)
576 {
577 assert(glsl_type_is_array(parent->type) ||
578 glsl_type_is_matrix(parent->type) ||
579 glsl_type_is_vector(parent->type));
580
581 nir_deref_instr *deref =
582 nir_deref_instr_create(build->shader, nir_deref_type_array);
583
584 deref->mode = parent->mode;
585 deref->type = glsl_get_array_element(parent->type);
586 deref->parent = nir_src_for_ssa(&parent->dest.ssa);
587 deref->arr.index = nir_src_for_ssa(index);
588
589 nir_ssa_dest_init(&deref->instr, &deref->dest,
590 parent->dest.ssa.num_components,
591 parent->dest.ssa.bit_size, NULL);
592
593 nir_builder_instr_insert(build, &deref->instr);
594
595 return deref;
596 }
597
598 static inline nir_deref_instr *
599 nir_build_deref_array_wildcard(nir_builder *build, nir_deref_instr *parent)
600 {
601 assert(glsl_type_is_array(parent->type) ||
602 glsl_type_is_matrix(parent->type));
603
604 nir_deref_instr *deref =
605 nir_deref_instr_create(build->shader, nir_deref_type_array_wildcard);
606
607 deref->mode = parent->mode;
608 deref->type = glsl_get_array_element(parent->type);
609 deref->parent = nir_src_for_ssa(&parent->dest.ssa);
610
611 nir_ssa_dest_init(&deref->instr, &deref->dest,
612 parent->dest.ssa.num_components,
613 parent->dest.ssa.bit_size, NULL);
614
615 nir_builder_instr_insert(build, &deref->instr);
616
617 return deref;
618 }
619
620 static inline nir_deref_instr *
621 nir_build_deref_struct(nir_builder *build, nir_deref_instr *parent,
622 unsigned index)
623 {
624 assert(glsl_type_is_struct(parent->type));
625
626 nir_deref_instr *deref =
627 nir_deref_instr_create(build->shader, nir_deref_type_struct);
628
629 deref->mode = parent->mode;
630 deref->type = glsl_get_struct_field(parent->type, index);
631 deref->parent = nir_src_for_ssa(&parent->dest.ssa);
632 deref->strct.index = index;
633
634 nir_ssa_dest_init(&deref->instr, &deref->dest,
635 parent->dest.ssa.num_components,
636 parent->dest.ssa.bit_size, NULL);
637
638 nir_builder_instr_insert(build, &deref->instr);
639
640 return deref;
641 }
642
643 static inline nir_deref_instr *
644 nir_build_deref_cast(nir_builder *build, nir_ssa_def *parent,
645 nir_variable_mode mode, const struct glsl_type *type)
646 {
647 nir_deref_instr *deref =
648 nir_deref_instr_create(build->shader, nir_deref_type_cast);
649
650 deref->mode = mode;
651 deref->type = type;
652 deref->parent = nir_src_for_ssa(parent);
653
654 nir_ssa_dest_init(&deref->instr, &deref->dest,
655 parent->num_components, parent->bit_size, NULL);
656
657 nir_builder_instr_insert(build, &deref->instr);
658
659 return deref;
660 }
661
662 /** Returns a deref that follows another but starting from the given parent
663 *
664 * The new deref will be the same type and take the same array or struct index
665 * as the leader deref but it may have a different parent. This is very
666 * useful for walking deref paths.
667 */
668 static inline nir_deref_instr *
669 nir_build_deref_follower(nir_builder *b, nir_deref_instr *parent,
670 nir_deref_instr *leader)
671 {
672 /* If the derefs would have the same parent, don't make a new one */
673 assert(leader->parent.is_ssa);
674 if (leader->parent.ssa == &parent->dest.ssa)
675 return leader;
676
677 UNUSED nir_deref_instr *leader_parent = nir_src_as_deref(leader->parent);
678
679 switch (leader->deref_type) {
680 case nir_deref_type_var:
681 unreachable("A var dereference cannot have a parent");
682 break;
683
684 case nir_deref_type_array:
685 case nir_deref_type_array_wildcard:
686 assert(glsl_type_is_matrix(parent->type) ||
687 glsl_type_is_array(parent->type));
688 assert(glsl_get_length(parent->type) ==
689 glsl_get_length(leader_parent->type));
690
691 if (leader->deref_type == nir_deref_type_array) {
692 assert(leader->arr.index.is_ssa);
693 return nir_build_deref_array(b, parent, leader->arr.index.ssa);
694 } else {
695 return nir_build_deref_array_wildcard(b, parent);
696 }
697
698 case nir_deref_type_struct:
699 assert(glsl_type_is_struct(parent->type));
700 assert(glsl_get_length(parent->type) ==
701 glsl_get_length(leader_parent->type));
702
703 return nir_build_deref_struct(b, parent, leader->strct.index);
704
705 default:
706 unreachable("Invalid deref instruction type");
707 }
708 }
709
710 static inline nir_ssa_def *
711 nir_load_reg(nir_builder *build, nir_register *reg)
712 {
713 return nir_ssa_for_src(build, nir_src_for_reg(reg), reg->num_components);
714 }
715
716 static inline nir_ssa_def *
717 nir_load_deref(nir_builder *build, nir_deref_instr *deref)
718 {
719 nir_intrinsic_instr *load =
720 nir_intrinsic_instr_create(build->shader, nir_intrinsic_load_deref);
721 load->num_components = glsl_get_vector_elements(deref->type);
722 load->src[0] = nir_src_for_ssa(&deref->dest.ssa);
723 nir_ssa_dest_init(&load->instr, &load->dest, load->num_components,
724 glsl_get_bit_size(deref->type), NULL);
725 nir_builder_instr_insert(build, &load->instr);
726 return &load->dest.ssa;
727 }
728
729 static inline void
730 nir_store_deref(nir_builder *build, nir_deref_instr *deref,
731 nir_ssa_def *value, unsigned writemask)
732 {
733 nir_intrinsic_instr *store =
734 nir_intrinsic_instr_create(build->shader, nir_intrinsic_store_deref);
735 store->num_components = glsl_get_vector_elements(deref->type);
736 store->src[0] = nir_src_for_ssa(&deref->dest.ssa);
737 store->src[1] = nir_src_for_ssa(value);
738 nir_intrinsic_set_write_mask(store,
739 writemask & ((1 << store->num_components) - 1));
740 nir_builder_instr_insert(build, &store->instr);
741 }
742
743 static inline void
744 nir_copy_deref(nir_builder *build, nir_deref_instr *dest, nir_deref_instr *src)
745 {
746 nir_intrinsic_instr *copy =
747 nir_intrinsic_instr_create(build->shader, nir_intrinsic_copy_deref);
748 copy->src[0] = nir_src_for_ssa(&dest->dest.ssa);
749 copy->src[1] = nir_src_for_ssa(&src->dest.ssa);
750 nir_builder_instr_insert(build, &copy->instr);
751 }
752
753 static inline nir_ssa_def *
754 nir_load_var(nir_builder *build, nir_variable *var)
755 {
756 return nir_load_deref(build, nir_build_deref_var(build, var));
757 }
758
759 static inline void
760 nir_store_var(nir_builder *build, nir_variable *var, nir_ssa_def *value,
761 unsigned writemask)
762 {
763 nir_store_deref(build, nir_build_deref_var(build, var), value, writemask);
764 }
765
766 static inline void
767 nir_copy_var(nir_builder *build, nir_variable *dest, nir_variable *src)
768 {
769 nir_copy_deref(build, nir_build_deref_var(build, dest),
770 nir_build_deref_var(build, src));
771 }
772
773 static inline nir_ssa_def *
774 nir_load_param(nir_builder *build, uint32_t param_idx)
775 {
776 assert(param_idx < build->impl->function->num_params);
777 nir_parameter *param = &build->impl->function->params[param_idx];
778
779 nir_intrinsic_instr *load =
780 nir_intrinsic_instr_create(build->shader, nir_intrinsic_load_param);
781 nir_intrinsic_set_param_idx(load, param_idx);
782 load->num_components = param->num_components;
783 nir_ssa_dest_init(&load->instr, &load->dest,
784 param->num_components, param->bit_size, NULL);
785 nir_builder_instr_insert(build, &load->instr);
786 return &load->dest.ssa;
787 }
788
789 #include "nir_builder_opcodes.h"
790
791 static inline nir_ssa_def *
792 nir_load_barycentric(nir_builder *build, nir_intrinsic_op op,
793 unsigned interp_mode)
794 {
795 nir_intrinsic_instr *bary = nir_intrinsic_instr_create(build->shader, op);
796 nir_ssa_dest_init(&bary->instr, &bary->dest, 2, 32, NULL);
797 nir_intrinsic_set_interp_mode(bary, interp_mode);
798 nir_builder_instr_insert(build, &bary->instr);
799 return &bary->dest.ssa;
800 }
801
802 static inline void
803 nir_jump(nir_builder *build, nir_jump_type jump_type)
804 {
805 nir_jump_instr *jump = nir_jump_instr_create(build->shader, jump_type);
806 nir_builder_instr_insert(build, &jump->instr);
807 }
808
809 static inline nir_ssa_def *
810 nir_compare_func(nir_builder *b, enum compare_func func,
811 nir_ssa_def *src0, nir_ssa_def *src1)
812 {
813 switch (func) {
814 case COMPARE_FUNC_NEVER:
815 return nir_imm_int(b, 0);
816 case COMPARE_FUNC_ALWAYS:
817 return nir_imm_int(b, ~0);
818 case COMPARE_FUNC_EQUAL:
819 return nir_feq(b, src0, src1);
820 case COMPARE_FUNC_NOTEQUAL:
821 return nir_fne(b, src0, src1);
822 case COMPARE_FUNC_GREATER:
823 return nir_flt(b, src1, src0);
824 case COMPARE_FUNC_GEQUAL:
825 return nir_fge(b, src0, src1);
826 case COMPARE_FUNC_LESS:
827 return nir_flt(b, src0, src1);
828 case COMPARE_FUNC_LEQUAL:
829 return nir_fge(b, src1, src0);
830 }
831 unreachable("bad compare func");
832 }
833
834 #endif /* NIR_BUILDER_H */