nir/algebraic: Simplify fsat of fsign
[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_int(nir_builder *build, int x)
259 {
260 nir_const_value v;
261
262 memset(&v, 0, sizeof(v));
263 v.i32[0] = x;
264
265 return nir_build_imm(build, 1, 32, v);
266 }
267
268 static inline nir_ssa_def *
269 nir_imm_int64(nir_builder *build, int64_t x)
270 {
271 nir_const_value v;
272
273 memset(&v, 0, sizeof(v));
274 v.i64[0] = x;
275
276 return nir_build_imm(build, 1, 64, v);
277 }
278
279 static inline nir_ssa_def *
280 nir_imm_intN_t(nir_builder *build, uint64_t x, unsigned bit_size)
281 {
282 nir_const_value v;
283
284 memset(&v, 0, sizeof(v));
285 assert(bit_size <= 64);
286 v.i64[0] = x & (~0ull >> (64 - bit_size));
287
288 return nir_build_imm(build, 1, bit_size, v);
289 }
290
291 static inline nir_ssa_def *
292 nir_imm_ivec4(nir_builder *build, int x, int y, int z, int w)
293 {
294 nir_const_value v;
295
296 memset(&v, 0, sizeof(v));
297 v.i32[0] = x;
298 v.i32[1] = y;
299 v.i32[2] = z;
300 v.i32[3] = w;
301
302 return nir_build_imm(build, 4, 32, v);
303 }
304
305 static inline nir_ssa_def *
306 nir_build_alu(nir_builder *build, nir_op op, nir_ssa_def *src0,
307 nir_ssa_def *src1, nir_ssa_def *src2, nir_ssa_def *src3)
308 {
309 const nir_op_info *op_info = &nir_op_infos[op];
310 nir_alu_instr *instr = nir_alu_instr_create(build->shader, op);
311 if (!instr)
312 return NULL;
313
314 instr->exact = build->exact;
315
316 instr->src[0].src = nir_src_for_ssa(src0);
317 if (src1)
318 instr->src[1].src = nir_src_for_ssa(src1);
319 if (src2)
320 instr->src[2].src = nir_src_for_ssa(src2);
321 if (src3)
322 instr->src[3].src = nir_src_for_ssa(src3);
323
324 /* Guess the number of components the destination temporary should have
325 * based on our input sizes, if it's not fixed for the op.
326 */
327 unsigned num_components = op_info->output_size;
328 if (num_components == 0) {
329 for (unsigned i = 0; i < op_info->num_inputs; i++) {
330 if (op_info->input_sizes[i] == 0)
331 num_components = MAX2(num_components,
332 instr->src[i].src.ssa->num_components);
333 }
334 }
335 assert(num_components != 0);
336
337 /* Figure out the bitwidth based on the source bitwidth if the instruction
338 * is variable-width.
339 */
340 unsigned bit_size = nir_alu_type_get_type_size(op_info->output_type);
341 if (bit_size == 0) {
342 for (unsigned i = 0; i < op_info->num_inputs; i++) {
343 unsigned src_bit_size = instr->src[i].src.ssa->bit_size;
344 if (nir_alu_type_get_type_size(op_info->input_types[i]) == 0) {
345 if (bit_size)
346 assert(src_bit_size == bit_size);
347 else
348 bit_size = src_bit_size;
349 } else {
350 assert(src_bit_size ==
351 nir_alu_type_get_type_size(op_info->input_types[i]));
352 }
353 }
354 }
355
356 /* When in doubt, assume 32. */
357 if (bit_size == 0)
358 bit_size = 32;
359
360 /* Make sure we don't swizzle from outside of our source vector (like if a
361 * scalar value was passed into a multiply with a vector).
362 */
363 for (unsigned i = 0; i < op_info->num_inputs; i++) {
364 for (unsigned j = instr->src[i].src.ssa->num_components;
365 j < NIR_MAX_VEC_COMPONENTS; j++) {
366 instr->src[i].swizzle[j] = instr->src[i].src.ssa->num_components - 1;
367 }
368 }
369
370 nir_ssa_dest_init(&instr->instr, &instr->dest.dest, num_components,
371 bit_size, NULL);
372 instr->dest.write_mask = (1 << num_components) - 1;
373
374 nir_builder_instr_insert(build, &instr->instr);
375
376 return &instr->dest.dest.ssa;
377 }
378
379 #include "nir_builder_opcodes.h"
380
381 static inline nir_ssa_def *
382 nir_vec(nir_builder *build, nir_ssa_def **comp, unsigned num_components)
383 {
384 switch (num_components) {
385 case 4:
386 return nir_vec4(build, comp[0], comp[1], comp[2], comp[3]);
387 case 3:
388 return nir_vec3(build, comp[0], comp[1], comp[2]);
389 case 2:
390 return nir_vec2(build, comp[0], comp[1]);
391 case 1:
392 return comp[0];
393 default:
394 unreachable("bad component count");
395 return NULL;
396 }
397 }
398
399 /**
400 * Similar to nir_fmov, but takes a nir_alu_src instead of a nir_ssa_def.
401 */
402 static inline nir_ssa_def *
403 nir_fmov_alu(nir_builder *build, nir_alu_src src, unsigned num_components)
404 {
405 nir_alu_instr *mov = nir_alu_instr_create(build->shader, nir_op_fmov);
406 nir_ssa_dest_init(&mov->instr, &mov->dest.dest, num_components,
407 nir_src_bit_size(src.src), NULL);
408 mov->exact = build->exact;
409 mov->dest.write_mask = (1 << num_components) - 1;
410 mov->src[0] = src;
411 nir_builder_instr_insert(build, &mov->instr);
412
413 return &mov->dest.dest.ssa;
414 }
415
416 static inline nir_ssa_def *
417 nir_imov_alu(nir_builder *build, nir_alu_src src, unsigned num_components)
418 {
419 nir_alu_instr *mov = nir_alu_instr_create(build->shader, nir_op_imov);
420 nir_ssa_dest_init(&mov->instr, &mov->dest.dest, num_components,
421 nir_src_bit_size(src.src), NULL);
422 mov->exact = build->exact;
423 mov->dest.write_mask = (1 << num_components) - 1;
424 mov->src[0] = src;
425 nir_builder_instr_insert(build, &mov->instr);
426
427 return &mov->dest.dest.ssa;
428 }
429
430 /**
431 * Construct an fmov or imov that reswizzles the source's components.
432 */
433 static inline nir_ssa_def *
434 nir_swizzle(nir_builder *build, nir_ssa_def *src, const unsigned *swiz,
435 unsigned num_components, bool use_fmov)
436 {
437 assert(num_components <= NIR_MAX_VEC_COMPONENTS);
438 nir_alu_src alu_src = { NIR_SRC_INIT };
439 alu_src.src = nir_src_for_ssa(src);
440 for (unsigned i = 0; i < num_components && i < NIR_MAX_VEC_COMPONENTS; i++)
441 alu_src.swizzle[i] = swiz[i];
442
443 return use_fmov ? nir_fmov_alu(build, alu_src, num_components) :
444 nir_imov_alu(build, alu_src, num_components);
445 }
446
447 /* Selects the right fdot given the number of components in each source. */
448 static inline nir_ssa_def *
449 nir_fdot(nir_builder *build, nir_ssa_def *src0, nir_ssa_def *src1)
450 {
451 assert(src0->num_components == src1->num_components);
452 switch (src0->num_components) {
453 case 1: return nir_fmul(build, src0, src1);
454 case 2: return nir_fdot2(build, src0, src1);
455 case 3: return nir_fdot3(build, src0, src1);
456 case 4: return nir_fdot4(build, src0, src1);
457 default:
458 unreachable("bad component size");
459 }
460
461 return NULL;
462 }
463
464 static inline nir_ssa_def *
465 nir_bany_inequal(nir_builder *b, nir_ssa_def *src0, nir_ssa_def *src1)
466 {
467 switch (src0->num_components) {
468 case 1: return nir_ine(b, src0, src1);
469 case 2: return nir_bany_inequal2(b, src0, src1);
470 case 3: return nir_bany_inequal3(b, src0, src1);
471 case 4: return nir_bany_inequal4(b, src0, src1);
472 default:
473 unreachable("bad component size");
474 }
475 }
476
477 static inline nir_ssa_def *
478 nir_bany(nir_builder *b, nir_ssa_def *src)
479 {
480 return nir_bany_inequal(b, src, nir_imm_int(b, 0));
481 }
482
483 static inline nir_ssa_def *
484 nir_channel(nir_builder *b, nir_ssa_def *def, unsigned c)
485 {
486 return nir_swizzle(b, def, &c, 1, false);
487 }
488
489 static inline nir_ssa_def *
490 nir_channels(nir_builder *b, nir_ssa_def *def, nir_component_mask_t mask)
491 {
492 unsigned num_channels = 0, swizzle[NIR_MAX_VEC_COMPONENTS] = { 0 };
493
494 for (unsigned i = 0; i < NIR_MAX_VEC_COMPONENTS; i++) {
495 if ((mask & (1 << i)) == 0)
496 continue;
497 swizzle[num_channels++] = i;
498 }
499
500 return nir_swizzle(b, def, swizzle, num_channels, false);
501 }
502
503 /**
504 * Turns a nir_src into a nir_ssa_def * so it can be passed to
505 * nir_build_alu()-based builder calls.
506 *
507 * See nir_ssa_for_alu_src() for alu instructions.
508 */
509 static inline nir_ssa_def *
510 nir_ssa_for_src(nir_builder *build, nir_src src, int num_components)
511 {
512 if (src.is_ssa && src.ssa->num_components == num_components)
513 return src.ssa;
514
515 nir_alu_src alu = { NIR_SRC_INIT };
516 alu.src = src;
517 for (int j = 0; j < 4; j++)
518 alu.swizzle[j] = j;
519
520 return nir_imov_alu(build, alu, num_components);
521 }
522
523 /**
524 * Similar to nir_ssa_for_src(), but for alu srcs, respecting the
525 * nir_alu_src's swizzle.
526 */
527 static inline nir_ssa_def *
528 nir_ssa_for_alu_src(nir_builder *build, nir_alu_instr *instr, unsigned srcn)
529 {
530 static uint8_t trivial_swizzle[NIR_MAX_VEC_COMPONENTS];
531 for (int i = 0; i < NIR_MAX_VEC_COMPONENTS; ++i)
532 trivial_swizzle[i] = i;
533 nir_alu_src *src = &instr->src[srcn];
534 unsigned num_components = nir_ssa_alu_instr_src_components(instr, srcn);
535
536 if (src->src.is_ssa && (src->src.ssa->num_components == num_components) &&
537 !src->abs && !src->negate &&
538 (memcmp(src->swizzle, trivial_swizzle, num_components) == 0))
539 return src->src.ssa;
540
541 return nir_imov_alu(build, *src, num_components);
542 }
543
544 static inline nir_deref_instr *
545 nir_build_deref_var(nir_builder *build, nir_variable *var)
546 {
547 nir_deref_instr *deref =
548 nir_deref_instr_create(build->shader, nir_deref_type_var);
549
550 deref->mode = var->data.mode;
551 deref->type = var->type;
552 deref->var = var;
553
554 nir_ssa_dest_init(&deref->instr, &deref->dest, 1, 32, NULL);
555
556 nir_builder_instr_insert(build, &deref->instr);
557
558 return deref;
559 }
560
561 static inline nir_deref_instr *
562 nir_build_deref_array(nir_builder *build, nir_deref_instr *parent,
563 nir_ssa_def *index)
564 {
565 assert(glsl_type_is_array(parent->type) ||
566 glsl_type_is_matrix(parent->type) ||
567 glsl_type_is_vector(parent->type));
568
569 nir_deref_instr *deref =
570 nir_deref_instr_create(build->shader, nir_deref_type_array);
571
572 deref->mode = parent->mode;
573 deref->type = glsl_get_array_element(parent->type);
574 deref->parent = nir_src_for_ssa(&parent->dest.ssa);
575 deref->arr.index = nir_src_for_ssa(index);
576
577 nir_ssa_dest_init(&deref->instr, &deref->dest,
578 parent->dest.ssa.num_components,
579 parent->dest.ssa.bit_size, NULL);
580
581 nir_builder_instr_insert(build, &deref->instr);
582
583 return deref;
584 }
585
586 static inline nir_deref_instr *
587 nir_build_deref_array_wildcard(nir_builder *build, nir_deref_instr *parent)
588 {
589 assert(glsl_type_is_array(parent->type) ||
590 glsl_type_is_matrix(parent->type));
591
592 nir_deref_instr *deref =
593 nir_deref_instr_create(build->shader, nir_deref_type_array_wildcard);
594
595 deref->mode = parent->mode;
596 deref->type = glsl_get_array_element(parent->type);
597 deref->parent = nir_src_for_ssa(&parent->dest.ssa);
598
599 nir_ssa_dest_init(&deref->instr, &deref->dest,
600 parent->dest.ssa.num_components,
601 parent->dest.ssa.bit_size, NULL);
602
603 nir_builder_instr_insert(build, &deref->instr);
604
605 return deref;
606 }
607
608 static inline nir_deref_instr *
609 nir_build_deref_struct(nir_builder *build, nir_deref_instr *parent,
610 unsigned index)
611 {
612 assert(glsl_type_is_struct(parent->type));
613
614 nir_deref_instr *deref =
615 nir_deref_instr_create(build->shader, nir_deref_type_struct);
616
617 deref->mode = parent->mode;
618 deref->type = glsl_get_struct_field(parent->type, index);
619 deref->parent = nir_src_for_ssa(&parent->dest.ssa);
620 deref->strct.index = index;
621
622 nir_ssa_dest_init(&deref->instr, &deref->dest,
623 parent->dest.ssa.num_components,
624 parent->dest.ssa.bit_size, NULL);
625
626 nir_builder_instr_insert(build, &deref->instr);
627
628 return deref;
629 }
630
631 static inline nir_deref_instr *
632 nir_build_deref_cast(nir_builder *build, nir_ssa_def *parent,
633 nir_variable_mode mode, const struct glsl_type *type)
634 {
635 nir_deref_instr *deref =
636 nir_deref_instr_create(build->shader, nir_deref_type_cast);
637
638 deref->mode = mode;
639 deref->type = type;
640 deref->parent = nir_src_for_ssa(parent);
641
642 nir_ssa_dest_init(&deref->instr, &deref->dest,
643 parent->num_components, parent->bit_size, NULL);
644
645 nir_builder_instr_insert(build, &deref->instr);
646
647 return deref;
648 }
649
650 /** Returns a deref that follows another but starting from the given parent
651 *
652 * The new deref will be the same type and take the same array or struct index
653 * as the leader deref but it may have a different parent. This is very
654 * useful for walking deref paths.
655 */
656 static inline nir_deref_instr *
657 nir_build_deref_follower(nir_builder *b, nir_deref_instr *parent,
658 nir_deref_instr *leader)
659 {
660 /* If the derefs would have the same parent, don't make a new one */
661 assert(leader->parent.is_ssa);
662 if (leader->parent.ssa == &parent->dest.ssa)
663 return leader;
664
665 UNUSED nir_deref_instr *leader_parent = nir_src_as_deref(leader->parent);
666
667 switch (leader->deref_type) {
668 case nir_deref_type_var:
669 unreachable("A var dereference cannot have a parent");
670 break;
671
672 case nir_deref_type_array:
673 case nir_deref_type_array_wildcard:
674 assert(glsl_type_is_matrix(parent->type) ||
675 glsl_type_is_array(parent->type));
676 assert(glsl_get_length(parent->type) ==
677 glsl_get_length(leader_parent->type));
678
679 if (leader->deref_type == nir_deref_type_array) {
680 assert(leader->arr.index.is_ssa);
681 return nir_build_deref_array(b, parent, leader->arr.index.ssa);
682 } else {
683 return nir_build_deref_array_wildcard(b, parent);
684 }
685
686 case nir_deref_type_struct:
687 assert(glsl_type_is_struct(parent->type));
688 assert(glsl_get_length(parent->type) ==
689 glsl_get_length(leader_parent->type));
690
691 return nir_build_deref_struct(b, parent, leader->strct.index);
692
693 default:
694 unreachable("Invalid deref instruction type");
695 }
696 }
697
698 static inline nir_ssa_def *
699 nir_load_reg(nir_builder *build, nir_register *reg)
700 {
701 return nir_ssa_for_src(build, nir_src_for_reg(reg), reg->num_components);
702 }
703
704 static inline nir_ssa_def *
705 nir_load_deref(nir_builder *build, nir_deref_instr *deref)
706 {
707 nir_intrinsic_instr *load =
708 nir_intrinsic_instr_create(build->shader, nir_intrinsic_load_deref);
709 load->num_components = glsl_get_vector_elements(deref->type);
710 load->src[0] = nir_src_for_ssa(&deref->dest.ssa);
711 nir_ssa_dest_init(&load->instr, &load->dest, load->num_components,
712 glsl_get_bit_size(deref->type), NULL);
713 nir_builder_instr_insert(build, &load->instr);
714 return &load->dest.ssa;
715 }
716
717 static inline void
718 nir_store_deref(nir_builder *build, nir_deref_instr *deref,
719 nir_ssa_def *value, unsigned writemask)
720 {
721 nir_intrinsic_instr *store =
722 nir_intrinsic_instr_create(build->shader, nir_intrinsic_store_deref);
723 store->num_components = glsl_get_vector_elements(deref->type);
724 store->src[0] = nir_src_for_ssa(&deref->dest.ssa);
725 store->src[1] = nir_src_for_ssa(value);
726 nir_intrinsic_set_write_mask(store,
727 writemask & ((1 << store->num_components) - 1));
728 nir_builder_instr_insert(build, &store->instr);
729 }
730
731 static inline void
732 nir_copy_deref(nir_builder *build, nir_deref_instr *dest, nir_deref_instr *src)
733 {
734 nir_intrinsic_instr *copy =
735 nir_intrinsic_instr_create(build->shader, nir_intrinsic_copy_deref);
736 copy->src[0] = nir_src_for_ssa(&dest->dest.ssa);
737 copy->src[1] = nir_src_for_ssa(&src->dest.ssa);
738 nir_builder_instr_insert(build, &copy->instr);
739 }
740
741 static inline nir_ssa_def *
742 nir_load_var(nir_builder *build, nir_variable *var)
743 {
744 return nir_load_deref(build, nir_build_deref_var(build, var));
745 }
746
747 static inline void
748 nir_store_var(nir_builder *build, nir_variable *var, nir_ssa_def *value,
749 unsigned writemask)
750 {
751 nir_store_deref(build, nir_build_deref_var(build, var), value, writemask);
752 }
753
754 static inline void
755 nir_copy_var(nir_builder *build, nir_variable *dest, nir_variable *src)
756 {
757 nir_copy_deref(build, nir_build_deref_var(build, dest),
758 nir_build_deref_var(build, src));
759 }
760
761 static inline nir_ssa_def *
762 nir_load_param(nir_builder *build, uint32_t param_idx)
763 {
764 assert(param_idx < build->impl->function->num_params);
765 nir_parameter *param = &build->impl->function->params[param_idx];
766
767 nir_intrinsic_instr *load =
768 nir_intrinsic_instr_create(build->shader, nir_intrinsic_load_param);
769 nir_intrinsic_set_param_idx(load, param_idx);
770 load->num_components = param->num_components;
771 nir_ssa_dest_init(&load->instr, &load->dest,
772 param->num_components, param->bit_size, NULL);
773 nir_builder_instr_insert(build, &load->instr);
774 return &load->dest.ssa;
775 }
776
777 #include "nir_builder_opcodes.h"
778
779 static inline nir_ssa_def *
780 nir_load_barycentric(nir_builder *build, nir_intrinsic_op op,
781 unsigned interp_mode)
782 {
783 nir_intrinsic_instr *bary = nir_intrinsic_instr_create(build->shader, op);
784 nir_ssa_dest_init(&bary->instr, &bary->dest, 2, 32, NULL);
785 nir_intrinsic_set_interp_mode(bary, interp_mode);
786 nir_builder_instr_insert(build, &bary->instr);
787 return &bary->dest.ssa;
788 }
789
790 static inline void
791 nir_jump(nir_builder *build, nir_jump_type jump_type)
792 {
793 nir_jump_instr *jump = nir_jump_instr_create(build->shader, jump_type);
794 nir_builder_instr_insert(build, &jump->instr);
795 }
796
797 static inline nir_ssa_def *
798 nir_compare_func(nir_builder *b, enum compare_func func,
799 nir_ssa_def *src0, nir_ssa_def *src1)
800 {
801 switch (func) {
802 case COMPARE_FUNC_NEVER:
803 return nir_imm_int(b, 0);
804 case COMPARE_FUNC_ALWAYS:
805 return nir_imm_int(b, ~0);
806 case COMPARE_FUNC_EQUAL:
807 return nir_feq(b, src0, src1);
808 case COMPARE_FUNC_NOTEQUAL:
809 return nir_fne(b, src0, src1);
810 case COMPARE_FUNC_GREATER:
811 return nir_flt(b, src1, src0);
812 case COMPARE_FUNC_GEQUAL:
813 return nir_fge(b, src0, src1);
814 case COMPARE_FUNC_LESS:
815 return nir_flt(b, src0, src1);
816 case COMPARE_FUNC_LEQUAL:
817 return nir_fge(b, src1, src0);
818 }
819 unreachable("bad compare func");
820 }
821
822 #endif /* NIR_BUILDER_H */