nir: Add a helper for getting binop identities
[mesa.git] / src / compiler / nir / nir.c
1 /*
2 * Copyright © 2014 Intel Corporation
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 * Authors:
24 * Connor Abbott (cwabbott0@gmail.com)
25 *
26 */
27
28 #include "nir.h"
29 #include "nir_control_flow_private.h"
30 #include "util/half_float.h"
31 #include <assert.h>
32 #include <math.h>
33
34 nir_shader *
35 nir_shader_create(void *mem_ctx,
36 gl_shader_stage stage,
37 const nir_shader_compiler_options *options,
38 shader_info *si)
39 {
40 nir_shader *shader = rzalloc(mem_ctx, nir_shader);
41
42 exec_list_make_empty(&shader->uniforms);
43 exec_list_make_empty(&shader->inputs);
44 exec_list_make_empty(&shader->outputs);
45 exec_list_make_empty(&shader->shared);
46
47 shader->options = options;
48
49 if (si) {
50 assert(si->stage == stage);
51 shader->info = *si;
52 } else {
53 shader->info.stage = stage;
54 }
55
56 exec_list_make_empty(&shader->functions);
57 exec_list_make_empty(&shader->registers);
58 exec_list_make_empty(&shader->globals);
59 exec_list_make_empty(&shader->system_values);
60 shader->reg_alloc = 0;
61
62 shader->num_inputs = 0;
63 shader->num_outputs = 0;
64 shader->num_uniforms = 0;
65 shader->num_shared = 0;
66
67 return shader;
68 }
69
70 static nir_register *
71 reg_create(void *mem_ctx, struct exec_list *list)
72 {
73 nir_register *reg = ralloc(mem_ctx, nir_register);
74
75 list_inithead(&reg->uses);
76 list_inithead(&reg->defs);
77 list_inithead(&reg->if_uses);
78
79 reg->num_components = 0;
80 reg->bit_size = 32;
81 reg->num_array_elems = 0;
82 reg->is_packed = false;
83 reg->name = NULL;
84
85 exec_list_push_tail(list, &reg->node);
86
87 return reg;
88 }
89
90 nir_register *
91 nir_global_reg_create(nir_shader *shader)
92 {
93 nir_register *reg = reg_create(shader, &shader->registers);
94 reg->index = shader->reg_alloc++;
95 reg->is_global = true;
96
97 return reg;
98 }
99
100 nir_register *
101 nir_local_reg_create(nir_function_impl *impl)
102 {
103 nir_register *reg = reg_create(ralloc_parent(impl), &impl->registers);
104 reg->index = impl->reg_alloc++;
105 reg->is_global = false;
106
107 return reg;
108 }
109
110 void
111 nir_reg_remove(nir_register *reg)
112 {
113 exec_node_remove(&reg->node);
114 }
115
116 void
117 nir_shader_add_variable(nir_shader *shader, nir_variable *var)
118 {
119 switch (var->data.mode) {
120 case nir_var_all:
121 assert(!"invalid mode");
122 break;
123
124 case nir_var_local:
125 assert(!"nir_shader_add_variable cannot be used for local variables");
126 break;
127
128 case nir_var_param:
129 assert(!"nir_shader_add_variable cannot be used for function parameters");
130 break;
131
132 case nir_var_global:
133 exec_list_push_tail(&shader->globals, &var->node);
134 break;
135
136 case nir_var_shader_in:
137 exec_list_push_tail(&shader->inputs, &var->node);
138 break;
139
140 case nir_var_shader_out:
141 exec_list_push_tail(&shader->outputs, &var->node);
142 break;
143
144 case nir_var_uniform:
145 case nir_var_shader_storage:
146 exec_list_push_tail(&shader->uniforms, &var->node);
147 break;
148
149 case nir_var_shared:
150 assert(shader->info.stage == MESA_SHADER_COMPUTE);
151 exec_list_push_tail(&shader->shared, &var->node);
152 break;
153
154 case nir_var_system_value:
155 exec_list_push_tail(&shader->system_values, &var->node);
156 break;
157 }
158 }
159
160 nir_variable *
161 nir_variable_create(nir_shader *shader, nir_variable_mode mode,
162 const struct glsl_type *type, const char *name)
163 {
164 nir_variable *var = rzalloc(shader, nir_variable);
165 var->name = ralloc_strdup(var, name);
166 var->type = type;
167 var->data.mode = mode;
168
169 if ((mode == nir_var_shader_in &&
170 shader->info.stage != MESA_SHADER_VERTEX) ||
171 (mode == nir_var_shader_out &&
172 shader->info.stage != MESA_SHADER_FRAGMENT))
173 var->data.interpolation = INTERP_MODE_SMOOTH;
174
175 if (mode == nir_var_shader_in || mode == nir_var_uniform)
176 var->data.read_only = true;
177
178 nir_shader_add_variable(shader, var);
179
180 return var;
181 }
182
183 nir_variable *
184 nir_local_variable_create(nir_function_impl *impl,
185 const struct glsl_type *type, const char *name)
186 {
187 nir_variable *var = rzalloc(impl->function->shader, nir_variable);
188 var->name = ralloc_strdup(var, name);
189 var->type = type;
190 var->data.mode = nir_var_local;
191
192 nir_function_impl_add_variable(impl, var);
193
194 return var;
195 }
196
197 nir_function *
198 nir_function_create(nir_shader *shader, const char *name)
199 {
200 nir_function *func = ralloc(shader, nir_function);
201
202 exec_list_push_tail(&shader->functions, &func->node);
203
204 func->name = ralloc_strdup(func, name);
205 func->shader = shader;
206 func->num_params = 0;
207 func->params = NULL;
208 func->return_type = glsl_void_type();
209 func->impl = NULL;
210
211 return func;
212 }
213
214 void nir_src_copy(nir_src *dest, const nir_src *src, void *mem_ctx)
215 {
216 dest->is_ssa = src->is_ssa;
217 if (src->is_ssa) {
218 dest->ssa = src->ssa;
219 } else {
220 dest->reg.base_offset = src->reg.base_offset;
221 dest->reg.reg = src->reg.reg;
222 if (src->reg.indirect) {
223 dest->reg.indirect = ralloc(mem_ctx, nir_src);
224 nir_src_copy(dest->reg.indirect, src->reg.indirect, mem_ctx);
225 } else {
226 dest->reg.indirect = NULL;
227 }
228 }
229 }
230
231 void nir_dest_copy(nir_dest *dest, const nir_dest *src, nir_instr *instr)
232 {
233 /* Copying an SSA definition makes no sense whatsoever. */
234 assert(!src->is_ssa);
235
236 dest->is_ssa = false;
237
238 dest->reg.base_offset = src->reg.base_offset;
239 dest->reg.reg = src->reg.reg;
240 if (src->reg.indirect) {
241 dest->reg.indirect = ralloc(instr, nir_src);
242 nir_src_copy(dest->reg.indirect, src->reg.indirect, instr);
243 } else {
244 dest->reg.indirect = NULL;
245 }
246 }
247
248 void
249 nir_alu_src_copy(nir_alu_src *dest, const nir_alu_src *src,
250 nir_alu_instr *instr)
251 {
252 nir_src_copy(&dest->src, &src->src, &instr->instr);
253 dest->abs = src->abs;
254 dest->negate = src->negate;
255 for (unsigned i = 0; i < 4; i++)
256 dest->swizzle[i] = src->swizzle[i];
257 }
258
259 void
260 nir_alu_dest_copy(nir_alu_dest *dest, const nir_alu_dest *src,
261 nir_alu_instr *instr)
262 {
263 nir_dest_copy(&dest->dest, &src->dest, &instr->instr);
264 dest->write_mask = src->write_mask;
265 dest->saturate = src->saturate;
266 }
267
268
269 static void
270 cf_init(nir_cf_node *node, nir_cf_node_type type)
271 {
272 exec_node_init(&node->node);
273 node->parent = NULL;
274 node->type = type;
275 }
276
277 nir_function_impl *
278 nir_function_impl_create_bare(nir_shader *shader)
279 {
280 nir_function_impl *impl = ralloc(shader, nir_function_impl);
281
282 impl->function = NULL;
283
284 cf_init(&impl->cf_node, nir_cf_node_function);
285
286 exec_list_make_empty(&impl->body);
287 exec_list_make_empty(&impl->registers);
288 exec_list_make_empty(&impl->locals);
289 impl->num_params = 0;
290 impl->params = NULL;
291 impl->return_var = NULL;
292 impl->reg_alloc = 0;
293 impl->ssa_alloc = 0;
294 impl->valid_metadata = nir_metadata_none;
295
296 /* create start & end blocks */
297 nir_block *start_block = nir_block_create(shader);
298 nir_block *end_block = nir_block_create(shader);
299 start_block->cf_node.parent = &impl->cf_node;
300 end_block->cf_node.parent = &impl->cf_node;
301 impl->end_block = end_block;
302
303 exec_list_push_tail(&impl->body, &start_block->cf_node.node);
304
305 start_block->successors[0] = end_block;
306 _mesa_set_add(end_block->predecessors, start_block);
307 return impl;
308 }
309
310 nir_function_impl *
311 nir_function_impl_create(nir_function *function)
312 {
313 assert(function->impl == NULL);
314
315 nir_function_impl *impl = nir_function_impl_create_bare(function->shader);
316
317 function->impl = impl;
318 impl->function = function;
319
320 impl->num_params = function->num_params;
321 impl->params = ralloc_array(function->shader,
322 nir_variable *, impl->num_params);
323
324 for (unsigned i = 0; i < impl->num_params; i++) {
325 impl->params[i] = rzalloc(function->shader, nir_variable);
326 impl->params[i]->type = function->params[i].type;
327 impl->params[i]->data.mode = nir_var_param;
328 impl->params[i]->data.location = i;
329 }
330
331 if (!glsl_type_is_void(function->return_type)) {
332 impl->return_var = rzalloc(function->shader, nir_variable);
333 impl->return_var->type = function->return_type;
334 impl->return_var->data.mode = nir_var_param;
335 impl->return_var->data.location = -1;
336 } else {
337 impl->return_var = NULL;
338 }
339
340 return impl;
341 }
342
343 nir_block *
344 nir_block_create(nir_shader *shader)
345 {
346 nir_block *block = rzalloc(shader, nir_block);
347
348 cf_init(&block->cf_node, nir_cf_node_block);
349
350 block->successors[0] = block->successors[1] = NULL;
351 block->predecessors = _mesa_set_create(block, _mesa_hash_pointer,
352 _mesa_key_pointer_equal);
353 block->imm_dom = NULL;
354 /* XXX maybe it would be worth it to defer allocation? This
355 * way it doesn't get allocated for shader refs that never run
356 * nir_calc_dominance? For example, state-tracker creates an
357 * initial IR, clones that, runs appropriate lowering pass, passes
358 * to driver which does common lowering/opt, and then stores ref
359 * which is later used to do state specific lowering and futher
360 * opt. Do any of the references not need dominance metadata?
361 */
362 block->dom_frontier = _mesa_set_create(block, _mesa_hash_pointer,
363 _mesa_key_pointer_equal);
364
365 exec_list_make_empty(&block->instr_list);
366
367 return block;
368 }
369
370 static inline void
371 src_init(nir_src *src)
372 {
373 src->is_ssa = false;
374 src->reg.reg = NULL;
375 src->reg.indirect = NULL;
376 src->reg.base_offset = 0;
377 }
378
379 nir_if *
380 nir_if_create(nir_shader *shader)
381 {
382 nir_if *if_stmt = ralloc(shader, nir_if);
383
384 cf_init(&if_stmt->cf_node, nir_cf_node_if);
385 src_init(&if_stmt->condition);
386
387 nir_block *then = nir_block_create(shader);
388 exec_list_make_empty(&if_stmt->then_list);
389 exec_list_push_tail(&if_stmt->then_list, &then->cf_node.node);
390 then->cf_node.parent = &if_stmt->cf_node;
391
392 nir_block *else_stmt = nir_block_create(shader);
393 exec_list_make_empty(&if_stmt->else_list);
394 exec_list_push_tail(&if_stmt->else_list, &else_stmt->cf_node.node);
395 else_stmt->cf_node.parent = &if_stmt->cf_node;
396
397 return if_stmt;
398 }
399
400 nir_loop *
401 nir_loop_create(nir_shader *shader)
402 {
403 nir_loop *loop = rzalloc(shader, nir_loop);
404
405 cf_init(&loop->cf_node, nir_cf_node_loop);
406
407 nir_block *body = nir_block_create(shader);
408 exec_list_make_empty(&loop->body);
409 exec_list_push_tail(&loop->body, &body->cf_node.node);
410 body->cf_node.parent = &loop->cf_node;
411
412 body->successors[0] = body;
413 _mesa_set_add(body->predecessors, body);
414
415 return loop;
416 }
417
418 static void
419 instr_init(nir_instr *instr, nir_instr_type type)
420 {
421 instr->type = type;
422 instr->block = NULL;
423 exec_node_init(&instr->node);
424 }
425
426 static void
427 dest_init(nir_dest *dest)
428 {
429 dest->is_ssa = false;
430 dest->reg.reg = NULL;
431 dest->reg.indirect = NULL;
432 dest->reg.base_offset = 0;
433 }
434
435 static void
436 alu_dest_init(nir_alu_dest *dest)
437 {
438 dest_init(&dest->dest);
439 dest->saturate = false;
440 dest->write_mask = 0xf;
441 }
442
443 static void
444 alu_src_init(nir_alu_src *src)
445 {
446 src_init(&src->src);
447 src->abs = src->negate = false;
448 src->swizzle[0] = 0;
449 src->swizzle[1] = 1;
450 src->swizzle[2] = 2;
451 src->swizzle[3] = 3;
452 }
453
454 nir_alu_instr *
455 nir_alu_instr_create(nir_shader *shader, nir_op op)
456 {
457 unsigned num_srcs = nir_op_infos[op].num_inputs;
458 /* TODO: don't use rzalloc */
459 nir_alu_instr *instr =
460 rzalloc_size(shader,
461 sizeof(nir_alu_instr) + num_srcs * sizeof(nir_alu_src));
462
463 instr_init(&instr->instr, nir_instr_type_alu);
464 instr->op = op;
465 alu_dest_init(&instr->dest);
466 for (unsigned i = 0; i < num_srcs; i++)
467 alu_src_init(&instr->src[i]);
468
469 return instr;
470 }
471
472 nir_jump_instr *
473 nir_jump_instr_create(nir_shader *shader, nir_jump_type type)
474 {
475 nir_jump_instr *instr = ralloc(shader, nir_jump_instr);
476 instr_init(&instr->instr, nir_instr_type_jump);
477 instr->type = type;
478 return instr;
479 }
480
481 nir_load_const_instr *
482 nir_load_const_instr_create(nir_shader *shader, unsigned num_components,
483 unsigned bit_size)
484 {
485 nir_load_const_instr *instr = rzalloc(shader, nir_load_const_instr);
486 instr_init(&instr->instr, nir_instr_type_load_const);
487
488 nir_ssa_def_init(&instr->instr, &instr->def, num_components, bit_size, NULL);
489
490 return instr;
491 }
492
493 nir_intrinsic_instr *
494 nir_intrinsic_instr_create(nir_shader *shader, nir_intrinsic_op op)
495 {
496 unsigned num_srcs = nir_intrinsic_infos[op].num_srcs;
497 /* TODO: don't use rzalloc */
498 nir_intrinsic_instr *instr =
499 rzalloc_size(shader,
500 sizeof(nir_intrinsic_instr) + num_srcs * sizeof(nir_src));
501
502 instr_init(&instr->instr, nir_instr_type_intrinsic);
503 instr->intrinsic = op;
504
505 if (nir_intrinsic_infos[op].has_dest)
506 dest_init(&instr->dest);
507
508 for (unsigned i = 0; i < num_srcs; i++)
509 src_init(&instr->src[i]);
510
511 return instr;
512 }
513
514 nir_call_instr *
515 nir_call_instr_create(nir_shader *shader, nir_function *callee)
516 {
517 nir_call_instr *instr = ralloc(shader, nir_call_instr);
518 instr_init(&instr->instr, nir_instr_type_call);
519
520 instr->callee = callee;
521 instr->num_params = callee->num_params;
522 instr->params = ralloc_array(instr, nir_deref_var *, instr->num_params);
523 instr->return_deref = NULL;
524
525 return instr;
526 }
527
528 nir_tex_instr *
529 nir_tex_instr_create(nir_shader *shader, unsigned num_srcs)
530 {
531 nir_tex_instr *instr = rzalloc(shader, nir_tex_instr);
532 instr_init(&instr->instr, nir_instr_type_tex);
533
534 dest_init(&instr->dest);
535
536 instr->num_srcs = num_srcs;
537 instr->src = ralloc_array(instr, nir_tex_src, num_srcs);
538 for (unsigned i = 0; i < num_srcs; i++)
539 src_init(&instr->src[i].src);
540
541 instr->texture_index = 0;
542 instr->texture_array_size = 0;
543 instr->texture = NULL;
544 instr->sampler_index = 0;
545 instr->sampler = NULL;
546
547 return instr;
548 }
549
550 void
551 nir_tex_instr_add_src(nir_tex_instr *tex,
552 nir_tex_src_type src_type,
553 nir_src src)
554 {
555 nir_tex_src *new_srcs = rzalloc_array(tex, nir_tex_src,
556 tex->num_srcs + 1);
557
558 for (unsigned i = 0; i < tex->num_srcs; i++) {
559 new_srcs[i].src_type = tex->src[i].src_type;
560 nir_instr_move_src(&tex->instr, &new_srcs[i].src,
561 &tex->src[i].src);
562 }
563
564 ralloc_free(tex->src);
565 tex->src = new_srcs;
566
567 tex->src[tex->num_srcs].src_type = src_type;
568 nir_instr_rewrite_src(&tex->instr, &tex->src[tex->num_srcs].src, src);
569 tex->num_srcs++;
570 }
571
572 void
573 nir_tex_instr_remove_src(nir_tex_instr *tex, unsigned src_idx)
574 {
575 assert(src_idx < tex->num_srcs);
576
577 /* First rewrite the source to NIR_SRC_INIT */
578 nir_instr_rewrite_src(&tex->instr, &tex->src[src_idx].src, NIR_SRC_INIT);
579
580 /* Now, move all of the other sources down */
581 for (unsigned i = src_idx + 1; i < tex->num_srcs; i++) {
582 tex->src[i-1].src_type = tex->src[i].src_type;
583 nir_instr_move_src(&tex->instr, &tex->src[i-1].src, &tex->src[i].src);
584 }
585 tex->num_srcs--;
586 }
587
588 nir_phi_instr *
589 nir_phi_instr_create(nir_shader *shader)
590 {
591 nir_phi_instr *instr = ralloc(shader, nir_phi_instr);
592 instr_init(&instr->instr, nir_instr_type_phi);
593
594 dest_init(&instr->dest);
595 exec_list_make_empty(&instr->srcs);
596 return instr;
597 }
598
599 nir_parallel_copy_instr *
600 nir_parallel_copy_instr_create(nir_shader *shader)
601 {
602 nir_parallel_copy_instr *instr = ralloc(shader, nir_parallel_copy_instr);
603 instr_init(&instr->instr, nir_instr_type_parallel_copy);
604
605 exec_list_make_empty(&instr->entries);
606
607 return instr;
608 }
609
610 nir_ssa_undef_instr *
611 nir_ssa_undef_instr_create(nir_shader *shader,
612 unsigned num_components,
613 unsigned bit_size)
614 {
615 nir_ssa_undef_instr *instr = ralloc(shader, nir_ssa_undef_instr);
616 instr_init(&instr->instr, nir_instr_type_ssa_undef);
617
618 nir_ssa_def_init(&instr->instr, &instr->def, num_components, bit_size, NULL);
619
620 return instr;
621 }
622
623 nir_deref_var *
624 nir_deref_var_create(void *mem_ctx, nir_variable *var)
625 {
626 nir_deref_var *deref = ralloc(mem_ctx, nir_deref_var);
627 deref->deref.deref_type = nir_deref_type_var;
628 deref->deref.child = NULL;
629 deref->deref.type = var->type;
630 deref->var = var;
631 return deref;
632 }
633
634 nir_deref_array *
635 nir_deref_array_create(void *mem_ctx)
636 {
637 nir_deref_array *deref = ralloc(mem_ctx, nir_deref_array);
638 deref->deref.deref_type = nir_deref_type_array;
639 deref->deref.child = NULL;
640 deref->deref_array_type = nir_deref_array_type_direct;
641 src_init(&deref->indirect);
642 deref->base_offset = 0;
643 return deref;
644 }
645
646 nir_deref_struct *
647 nir_deref_struct_create(void *mem_ctx, unsigned field_index)
648 {
649 nir_deref_struct *deref = ralloc(mem_ctx, nir_deref_struct);
650 deref->deref.deref_type = nir_deref_type_struct;
651 deref->deref.child = NULL;
652 deref->index = field_index;
653 return deref;
654 }
655
656 nir_deref_var *
657 nir_deref_var_clone(const nir_deref_var *deref, void *mem_ctx)
658 {
659 if (deref == NULL)
660 return NULL;
661
662 nir_deref_var *ret = nir_deref_var_create(mem_ctx, deref->var);
663 ret->deref.type = deref->deref.type;
664 if (deref->deref.child)
665 ret->deref.child = nir_deref_clone(deref->deref.child, ret);
666 return ret;
667 }
668
669 static nir_deref_array *
670 deref_array_clone(const nir_deref_array *deref, void *mem_ctx)
671 {
672 nir_deref_array *ret = nir_deref_array_create(mem_ctx);
673 ret->base_offset = deref->base_offset;
674 ret->deref_array_type = deref->deref_array_type;
675 if (deref->deref_array_type == nir_deref_array_type_indirect) {
676 nir_src_copy(&ret->indirect, &deref->indirect, mem_ctx);
677 }
678 ret->deref.type = deref->deref.type;
679 if (deref->deref.child)
680 ret->deref.child = nir_deref_clone(deref->deref.child, ret);
681 return ret;
682 }
683
684 static nir_deref_struct *
685 deref_struct_clone(const nir_deref_struct *deref, void *mem_ctx)
686 {
687 nir_deref_struct *ret = nir_deref_struct_create(mem_ctx, deref->index);
688 ret->deref.type = deref->deref.type;
689 if (deref->deref.child)
690 ret->deref.child = nir_deref_clone(deref->deref.child, ret);
691 return ret;
692 }
693
694 nir_deref *
695 nir_deref_clone(const nir_deref *deref, void *mem_ctx)
696 {
697 if (deref == NULL)
698 return NULL;
699
700 switch (deref->deref_type) {
701 case nir_deref_type_var:
702 return &nir_deref_var_clone(nir_deref_as_var(deref), mem_ctx)->deref;
703 case nir_deref_type_array:
704 return &deref_array_clone(nir_deref_as_array(deref), mem_ctx)->deref;
705 case nir_deref_type_struct:
706 return &deref_struct_clone(nir_deref_as_struct(deref), mem_ctx)->deref;
707 default:
708 unreachable("Invalid dereference type");
709 }
710
711 return NULL;
712 }
713
714 /* This is the second step in the recursion. We've found the tail and made a
715 * copy. Now we need to iterate over all possible leaves and call the
716 * callback on each one.
717 */
718 static bool
719 deref_foreach_leaf_build_recur(nir_deref_var *deref, nir_deref *tail,
720 nir_deref_foreach_leaf_cb cb, void *state)
721 {
722 unsigned length;
723 union {
724 nir_deref_array arr;
725 nir_deref_struct str;
726 } tmp;
727
728 assert(tail->child == NULL);
729 switch (glsl_get_base_type(tail->type)) {
730 case GLSL_TYPE_UINT:
731 case GLSL_TYPE_UINT16:
732 case GLSL_TYPE_UINT64:
733 case GLSL_TYPE_INT:
734 case GLSL_TYPE_INT16:
735 case GLSL_TYPE_INT64:
736 case GLSL_TYPE_FLOAT:
737 case GLSL_TYPE_FLOAT16:
738 case GLSL_TYPE_DOUBLE:
739 case GLSL_TYPE_BOOL:
740 if (glsl_type_is_vector_or_scalar(tail->type))
741 return cb(deref, state);
742 /* Fall Through */
743
744 case GLSL_TYPE_ARRAY:
745 tmp.arr.deref.deref_type = nir_deref_type_array;
746 tmp.arr.deref.type = glsl_get_array_element(tail->type);
747 tmp.arr.deref_array_type = nir_deref_array_type_direct;
748 tmp.arr.indirect = NIR_SRC_INIT;
749 tail->child = &tmp.arr.deref;
750
751 length = glsl_get_length(tail->type);
752 for (unsigned i = 0; i < length; i++) {
753 tmp.arr.deref.child = NULL;
754 tmp.arr.base_offset = i;
755 if (!deref_foreach_leaf_build_recur(deref, &tmp.arr.deref, cb, state))
756 return false;
757 }
758 return true;
759
760 case GLSL_TYPE_STRUCT:
761 tmp.str.deref.deref_type = nir_deref_type_struct;
762 tail->child = &tmp.str.deref;
763
764 length = glsl_get_length(tail->type);
765 for (unsigned i = 0; i < length; i++) {
766 tmp.arr.deref.child = NULL;
767 tmp.str.deref.type = glsl_get_struct_field(tail->type, i);
768 tmp.str.index = i;
769 if (!deref_foreach_leaf_build_recur(deref, &tmp.arr.deref, cb, state))
770 return false;
771 }
772 return true;
773
774 default:
775 unreachable("Invalid type for dereference");
776 }
777 }
778
779 /* This is the first step of the foreach_leaf recursion. In this step we are
780 * walking to the end of the deref chain and making a copy in the stack as we
781 * go. This is because we don't want to mutate the deref chain that was
782 * passed in by the caller. The downside is that this deref chain is on the
783 * stack and , if the caller wants to do anything with it, they will have to
784 * make their own copy because this one will go away.
785 */
786 static bool
787 deref_foreach_leaf_copy_recur(nir_deref_var *deref, nir_deref *tail,
788 nir_deref_foreach_leaf_cb cb, void *state)
789 {
790 union {
791 nir_deref_array arr;
792 nir_deref_struct str;
793 } c;
794
795 if (tail->child) {
796 switch (tail->child->deref_type) {
797 case nir_deref_type_array:
798 c.arr = *nir_deref_as_array(tail->child);
799 tail->child = &c.arr.deref;
800 return deref_foreach_leaf_copy_recur(deref, &c.arr.deref, cb, state);
801
802 case nir_deref_type_struct:
803 c.str = *nir_deref_as_struct(tail->child);
804 tail->child = &c.str.deref;
805 return deref_foreach_leaf_copy_recur(deref, &c.str.deref, cb, state);
806
807 case nir_deref_type_var:
808 default:
809 unreachable("Invalid deref type for a child");
810 }
811 } else {
812 /* We've gotten to the end of the original deref. Time to start
813 * building our own derefs.
814 */
815 return deref_foreach_leaf_build_recur(deref, tail, cb, state);
816 }
817 }
818
819 /**
820 * This function iterates over all of the possible derefs that can be created
821 * with the given deref as the head. It then calls the provided callback with
822 * a full deref for each one.
823 *
824 * The deref passed to the callback will be allocated on the stack. You will
825 * need to make a copy if you want it to hang around.
826 */
827 bool
828 nir_deref_foreach_leaf(nir_deref_var *deref,
829 nir_deref_foreach_leaf_cb cb, void *state)
830 {
831 nir_deref_var copy = *deref;
832 return deref_foreach_leaf_copy_recur(&copy, &copy.deref, cb, state);
833 }
834
835 /* Returns a load_const instruction that represents the constant
836 * initializer for the given deref chain. The caller is responsible for
837 * ensuring that there actually is a constant initializer.
838 */
839 nir_load_const_instr *
840 nir_deref_get_const_initializer_load(nir_shader *shader, nir_deref_var *deref)
841 {
842 nir_constant *constant = deref->var->constant_initializer;
843 assert(constant);
844
845 const nir_deref *tail = &deref->deref;
846 unsigned matrix_col = 0;
847 while (tail->child) {
848 switch (tail->child->deref_type) {
849 case nir_deref_type_array: {
850 nir_deref_array *arr = nir_deref_as_array(tail->child);
851 assert(arr->deref_array_type == nir_deref_array_type_direct);
852 if (glsl_type_is_matrix(tail->type)) {
853 assert(arr->deref.child == NULL);
854 matrix_col = arr->base_offset;
855 } else {
856 constant = constant->elements[arr->base_offset];
857 }
858 break;
859 }
860
861 case nir_deref_type_struct: {
862 constant = constant->elements[nir_deref_as_struct(tail->child)->index];
863 break;
864 }
865
866 default:
867 unreachable("Invalid deref child type");
868 }
869
870 tail = tail->child;
871 }
872
873 unsigned bit_size = glsl_get_bit_size(tail->type);
874 nir_load_const_instr *load =
875 nir_load_const_instr_create(shader, glsl_get_vector_elements(tail->type),
876 bit_size);
877
878 switch (glsl_get_base_type(tail->type)) {
879 case GLSL_TYPE_FLOAT:
880 case GLSL_TYPE_INT:
881 case GLSL_TYPE_UINT:
882 case GLSL_TYPE_FLOAT16:
883 case GLSL_TYPE_DOUBLE:
884 case GLSL_TYPE_INT16:
885 case GLSL_TYPE_UINT16:
886 case GLSL_TYPE_UINT64:
887 case GLSL_TYPE_INT64:
888 case GLSL_TYPE_BOOL:
889 load->value = constant->values[matrix_col];
890 break;
891 default:
892 unreachable("Invalid immediate type");
893 }
894
895 return load;
896 }
897
898 static nir_const_value
899 const_value_float(double d, unsigned bit_size)
900 {
901 nir_const_value v;
902 switch (bit_size) {
903 case 16: v.u16[0] = _mesa_float_to_half(d); break;
904 case 32: v.f32[0] = d; break;
905 case 64: v.f64[0] = d; break;
906 default:
907 unreachable("Invalid bit size");
908 }
909 return v;
910 }
911
912 static nir_const_value
913 const_value_int(int64_t i, unsigned bit_size)
914 {
915 nir_const_value v;
916 switch (bit_size) {
917 case 8: v.i8[0] = i; break;
918 case 16: v.i16[0] = i; break;
919 case 32: v.i32[0] = i; break;
920 case 64: v.i64[0] = i; break;
921 default:
922 unreachable("Invalid bit size");
923 }
924 return v;
925 }
926
927 nir_const_value
928 nir_alu_binop_identity(nir_op binop, unsigned bit_size)
929 {
930 const int64_t max_int = (1ull << (bit_size - 1)) - 1;
931 const int64_t min_int = -max_int - 1;
932 switch (binop) {
933 case nir_op_iadd:
934 return const_value_int(0, bit_size);
935 case nir_op_fadd:
936 return const_value_float(0, bit_size);
937 case nir_op_imul:
938 return const_value_int(1, bit_size);
939 case nir_op_fmul:
940 return const_value_float(1, bit_size);
941 case nir_op_imin:
942 return const_value_int(max_int, bit_size);
943 case nir_op_umin:
944 return const_value_int(~0ull, bit_size);
945 case nir_op_fmin:
946 return const_value_float(INFINITY, bit_size);
947 case nir_op_imax:
948 return const_value_int(min_int, bit_size);
949 case nir_op_umax:
950 return const_value_int(0, bit_size);
951 case nir_op_fmax:
952 return const_value_float(-INFINITY, bit_size);
953 case nir_op_iand:
954 return const_value_int(~0ull, bit_size);
955 case nir_op_ior:
956 return const_value_int(0, bit_size);
957 case nir_op_ixor:
958 return const_value_int(0, bit_size);
959 default:
960 unreachable("Invalid reduction operation");
961 }
962 }
963
964 nir_function_impl *
965 nir_cf_node_get_function(nir_cf_node *node)
966 {
967 while (node->type != nir_cf_node_function) {
968 node = node->parent;
969 }
970
971 return nir_cf_node_as_function(node);
972 }
973
974 /* Reduces a cursor by trying to convert everything to after and trying to
975 * go up to block granularity when possible.
976 */
977 static nir_cursor
978 reduce_cursor(nir_cursor cursor)
979 {
980 switch (cursor.option) {
981 case nir_cursor_before_block:
982 assert(nir_cf_node_prev(&cursor.block->cf_node) == NULL ||
983 nir_cf_node_prev(&cursor.block->cf_node)->type != nir_cf_node_block);
984 if (exec_list_is_empty(&cursor.block->instr_list)) {
985 /* Empty block. After is as good as before. */
986 cursor.option = nir_cursor_after_block;
987 }
988 return cursor;
989
990 case nir_cursor_after_block:
991 return cursor;
992
993 case nir_cursor_before_instr: {
994 nir_instr *prev_instr = nir_instr_prev(cursor.instr);
995 if (prev_instr) {
996 /* Before this instruction is after the previous */
997 cursor.instr = prev_instr;
998 cursor.option = nir_cursor_after_instr;
999 } else {
1000 /* No previous instruction. Switch to before block */
1001 cursor.block = cursor.instr->block;
1002 cursor.option = nir_cursor_before_block;
1003 }
1004 return reduce_cursor(cursor);
1005 }
1006
1007 case nir_cursor_after_instr:
1008 if (nir_instr_next(cursor.instr) == NULL) {
1009 /* This is the last instruction, switch to after block */
1010 cursor.option = nir_cursor_after_block;
1011 cursor.block = cursor.instr->block;
1012 }
1013 return cursor;
1014
1015 default:
1016 unreachable("Inavlid cursor option");
1017 }
1018 }
1019
1020 bool
1021 nir_cursors_equal(nir_cursor a, nir_cursor b)
1022 {
1023 /* Reduced cursors should be unique */
1024 a = reduce_cursor(a);
1025 b = reduce_cursor(b);
1026
1027 return a.block == b.block && a.option == b.option;
1028 }
1029
1030 static bool
1031 add_use_cb(nir_src *src, void *state)
1032 {
1033 nir_instr *instr = state;
1034
1035 src->parent_instr = instr;
1036 list_addtail(&src->use_link,
1037 src->is_ssa ? &src->ssa->uses : &src->reg.reg->uses);
1038
1039 return true;
1040 }
1041
1042 static bool
1043 add_ssa_def_cb(nir_ssa_def *def, void *state)
1044 {
1045 nir_instr *instr = state;
1046
1047 if (instr->block && def->index == UINT_MAX) {
1048 nir_function_impl *impl =
1049 nir_cf_node_get_function(&instr->block->cf_node);
1050
1051 def->index = impl->ssa_alloc++;
1052 }
1053
1054 return true;
1055 }
1056
1057 static bool
1058 add_reg_def_cb(nir_dest *dest, void *state)
1059 {
1060 nir_instr *instr = state;
1061
1062 if (!dest->is_ssa) {
1063 dest->reg.parent_instr = instr;
1064 list_addtail(&dest->reg.def_link, &dest->reg.reg->defs);
1065 }
1066
1067 return true;
1068 }
1069
1070 static void
1071 add_defs_uses(nir_instr *instr)
1072 {
1073 nir_foreach_src(instr, add_use_cb, instr);
1074 nir_foreach_dest(instr, add_reg_def_cb, instr);
1075 nir_foreach_ssa_def(instr, add_ssa_def_cb, instr);
1076 }
1077
1078 void
1079 nir_instr_insert(nir_cursor cursor, nir_instr *instr)
1080 {
1081 switch (cursor.option) {
1082 case nir_cursor_before_block:
1083 /* Only allow inserting jumps into empty blocks. */
1084 if (instr->type == nir_instr_type_jump)
1085 assert(exec_list_is_empty(&cursor.block->instr_list));
1086
1087 instr->block = cursor.block;
1088 add_defs_uses(instr);
1089 exec_list_push_head(&cursor.block->instr_list, &instr->node);
1090 break;
1091 case nir_cursor_after_block: {
1092 /* Inserting instructions after a jump is illegal. */
1093 nir_instr *last = nir_block_last_instr(cursor.block);
1094 assert(last == NULL || last->type != nir_instr_type_jump);
1095 (void) last;
1096
1097 instr->block = cursor.block;
1098 add_defs_uses(instr);
1099 exec_list_push_tail(&cursor.block->instr_list, &instr->node);
1100 break;
1101 }
1102 case nir_cursor_before_instr:
1103 assert(instr->type != nir_instr_type_jump);
1104 instr->block = cursor.instr->block;
1105 add_defs_uses(instr);
1106 exec_node_insert_node_before(&cursor.instr->node, &instr->node);
1107 break;
1108 case nir_cursor_after_instr:
1109 /* Inserting instructions after a jump is illegal. */
1110 assert(cursor.instr->type != nir_instr_type_jump);
1111
1112 /* Only allow inserting jumps at the end of the block. */
1113 if (instr->type == nir_instr_type_jump)
1114 assert(cursor.instr == nir_block_last_instr(cursor.instr->block));
1115
1116 instr->block = cursor.instr->block;
1117 add_defs_uses(instr);
1118 exec_node_insert_after(&cursor.instr->node, &instr->node);
1119 break;
1120 }
1121
1122 if (instr->type == nir_instr_type_jump)
1123 nir_handle_add_jump(instr->block);
1124 }
1125
1126 static bool
1127 src_is_valid(const nir_src *src)
1128 {
1129 return src->is_ssa ? (src->ssa != NULL) : (src->reg.reg != NULL);
1130 }
1131
1132 static bool
1133 remove_use_cb(nir_src *src, void *state)
1134 {
1135 (void) state;
1136
1137 if (src_is_valid(src))
1138 list_del(&src->use_link);
1139
1140 return true;
1141 }
1142
1143 static bool
1144 remove_def_cb(nir_dest *dest, void *state)
1145 {
1146 (void) state;
1147
1148 if (!dest->is_ssa)
1149 list_del(&dest->reg.def_link);
1150
1151 return true;
1152 }
1153
1154 static void
1155 remove_defs_uses(nir_instr *instr)
1156 {
1157 nir_foreach_dest(instr, remove_def_cb, instr);
1158 nir_foreach_src(instr, remove_use_cb, instr);
1159 }
1160
1161 void nir_instr_remove(nir_instr *instr)
1162 {
1163 remove_defs_uses(instr);
1164 exec_node_remove(&instr->node);
1165
1166 if (instr->type == nir_instr_type_jump) {
1167 nir_jump_instr *jump_instr = nir_instr_as_jump(instr);
1168 nir_handle_remove_jump(instr->block, jump_instr->type);
1169 }
1170 }
1171
1172 /*@}*/
1173
1174 void
1175 nir_index_local_regs(nir_function_impl *impl)
1176 {
1177 unsigned index = 0;
1178 foreach_list_typed(nir_register, reg, node, &impl->registers) {
1179 reg->index = index++;
1180 }
1181 impl->reg_alloc = index;
1182 }
1183
1184 void
1185 nir_index_global_regs(nir_shader *shader)
1186 {
1187 unsigned index = 0;
1188 foreach_list_typed(nir_register, reg, node, &shader->registers) {
1189 reg->index = index++;
1190 }
1191 shader->reg_alloc = index;
1192 }
1193
1194 static bool
1195 visit_alu_dest(nir_alu_instr *instr, nir_foreach_dest_cb cb, void *state)
1196 {
1197 return cb(&instr->dest.dest, state);
1198 }
1199
1200 static bool
1201 visit_intrinsic_dest(nir_intrinsic_instr *instr, nir_foreach_dest_cb cb,
1202 void *state)
1203 {
1204 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
1205 return cb(&instr->dest, state);
1206
1207 return true;
1208 }
1209
1210 static bool
1211 visit_texture_dest(nir_tex_instr *instr, nir_foreach_dest_cb cb,
1212 void *state)
1213 {
1214 return cb(&instr->dest, state);
1215 }
1216
1217 static bool
1218 visit_phi_dest(nir_phi_instr *instr, nir_foreach_dest_cb cb, void *state)
1219 {
1220 return cb(&instr->dest, state);
1221 }
1222
1223 static bool
1224 visit_parallel_copy_dest(nir_parallel_copy_instr *instr,
1225 nir_foreach_dest_cb cb, void *state)
1226 {
1227 nir_foreach_parallel_copy_entry(entry, instr) {
1228 if (!cb(&entry->dest, state))
1229 return false;
1230 }
1231
1232 return true;
1233 }
1234
1235 bool
1236 nir_foreach_dest(nir_instr *instr, nir_foreach_dest_cb cb, void *state)
1237 {
1238 switch (instr->type) {
1239 case nir_instr_type_alu:
1240 return visit_alu_dest(nir_instr_as_alu(instr), cb, state);
1241 case nir_instr_type_intrinsic:
1242 return visit_intrinsic_dest(nir_instr_as_intrinsic(instr), cb, state);
1243 case nir_instr_type_tex:
1244 return visit_texture_dest(nir_instr_as_tex(instr), cb, state);
1245 case nir_instr_type_phi:
1246 return visit_phi_dest(nir_instr_as_phi(instr), cb, state);
1247 case nir_instr_type_parallel_copy:
1248 return visit_parallel_copy_dest(nir_instr_as_parallel_copy(instr),
1249 cb, state);
1250
1251 case nir_instr_type_load_const:
1252 case nir_instr_type_ssa_undef:
1253 case nir_instr_type_call:
1254 case nir_instr_type_jump:
1255 break;
1256
1257 default:
1258 unreachable("Invalid instruction type");
1259 break;
1260 }
1261
1262 return true;
1263 }
1264
1265 struct foreach_ssa_def_state {
1266 nir_foreach_ssa_def_cb cb;
1267 void *client_state;
1268 };
1269
1270 static inline bool
1271 nir_ssa_def_visitor(nir_dest *dest, void *void_state)
1272 {
1273 struct foreach_ssa_def_state *state = void_state;
1274
1275 if (dest->is_ssa)
1276 return state->cb(&dest->ssa, state->client_state);
1277 else
1278 return true;
1279 }
1280
1281 bool
1282 nir_foreach_ssa_def(nir_instr *instr, nir_foreach_ssa_def_cb cb, void *state)
1283 {
1284 switch (instr->type) {
1285 case nir_instr_type_alu:
1286 case nir_instr_type_tex:
1287 case nir_instr_type_intrinsic:
1288 case nir_instr_type_phi:
1289 case nir_instr_type_parallel_copy: {
1290 struct foreach_ssa_def_state foreach_state = {cb, state};
1291 return nir_foreach_dest(instr, nir_ssa_def_visitor, &foreach_state);
1292 }
1293
1294 case nir_instr_type_load_const:
1295 return cb(&nir_instr_as_load_const(instr)->def, state);
1296 case nir_instr_type_ssa_undef:
1297 return cb(&nir_instr_as_ssa_undef(instr)->def, state);
1298 case nir_instr_type_call:
1299 case nir_instr_type_jump:
1300 return true;
1301 default:
1302 unreachable("Invalid instruction type");
1303 }
1304 }
1305
1306 static bool
1307 visit_src(nir_src *src, nir_foreach_src_cb cb, void *state)
1308 {
1309 if (!cb(src, state))
1310 return false;
1311 if (!src->is_ssa && src->reg.indirect)
1312 return cb(src->reg.indirect, state);
1313 return true;
1314 }
1315
1316 static bool
1317 visit_deref_array_src(nir_deref_array *deref, nir_foreach_src_cb cb,
1318 void *state)
1319 {
1320 if (deref->deref_array_type == nir_deref_array_type_indirect)
1321 return visit_src(&deref->indirect, cb, state);
1322 return true;
1323 }
1324
1325 static bool
1326 visit_deref_src(nir_deref_var *deref, nir_foreach_src_cb cb, void *state)
1327 {
1328 nir_deref *cur = &deref->deref;
1329 while (cur != NULL) {
1330 if (cur->deref_type == nir_deref_type_array) {
1331 if (!visit_deref_array_src(nir_deref_as_array(cur), cb, state))
1332 return false;
1333 }
1334
1335 cur = cur->child;
1336 }
1337
1338 return true;
1339 }
1340
1341 static bool
1342 visit_alu_src(nir_alu_instr *instr, nir_foreach_src_cb cb, void *state)
1343 {
1344 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++)
1345 if (!visit_src(&instr->src[i].src, cb, state))
1346 return false;
1347
1348 return true;
1349 }
1350
1351 static bool
1352 visit_tex_src(nir_tex_instr *instr, nir_foreach_src_cb cb, void *state)
1353 {
1354 for (unsigned i = 0; i < instr->num_srcs; i++) {
1355 if (!visit_src(&instr->src[i].src, cb, state))
1356 return false;
1357 }
1358
1359 if (instr->texture != NULL) {
1360 if (!visit_deref_src(instr->texture, cb, state))
1361 return false;
1362 }
1363
1364 if (instr->sampler != NULL) {
1365 if (!visit_deref_src(instr->sampler, cb, state))
1366 return false;
1367 }
1368
1369 return true;
1370 }
1371
1372 static bool
1373 visit_intrinsic_src(nir_intrinsic_instr *instr, nir_foreach_src_cb cb,
1374 void *state)
1375 {
1376 unsigned num_srcs = nir_intrinsic_infos[instr->intrinsic].num_srcs;
1377 for (unsigned i = 0; i < num_srcs; i++) {
1378 if (!visit_src(&instr->src[i], cb, state))
1379 return false;
1380 }
1381
1382 unsigned num_vars =
1383 nir_intrinsic_infos[instr->intrinsic].num_variables;
1384 for (unsigned i = 0; i < num_vars; i++) {
1385 if (!visit_deref_src(instr->variables[i], cb, state))
1386 return false;
1387 }
1388
1389 return true;
1390 }
1391
1392 static bool
1393 visit_phi_src(nir_phi_instr *instr, nir_foreach_src_cb cb, void *state)
1394 {
1395 nir_foreach_phi_src(src, instr) {
1396 if (!visit_src(&src->src, cb, state))
1397 return false;
1398 }
1399
1400 return true;
1401 }
1402
1403 static bool
1404 visit_parallel_copy_src(nir_parallel_copy_instr *instr,
1405 nir_foreach_src_cb cb, void *state)
1406 {
1407 nir_foreach_parallel_copy_entry(entry, instr) {
1408 if (!visit_src(&entry->src, cb, state))
1409 return false;
1410 }
1411
1412 return true;
1413 }
1414
1415 typedef struct {
1416 void *state;
1417 nir_foreach_src_cb cb;
1418 } visit_dest_indirect_state;
1419
1420 static bool
1421 visit_dest_indirect(nir_dest *dest, void *_state)
1422 {
1423 visit_dest_indirect_state *state = (visit_dest_indirect_state *) _state;
1424
1425 if (!dest->is_ssa && dest->reg.indirect)
1426 return state->cb(dest->reg.indirect, state->state);
1427
1428 return true;
1429 }
1430
1431 bool
1432 nir_foreach_src(nir_instr *instr, nir_foreach_src_cb cb, void *state)
1433 {
1434 switch (instr->type) {
1435 case nir_instr_type_alu:
1436 if (!visit_alu_src(nir_instr_as_alu(instr), cb, state))
1437 return false;
1438 break;
1439 case nir_instr_type_intrinsic:
1440 if (!visit_intrinsic_src(nir_instr_as_intrinsic(instr), cb, state))
1441 return false;
1442 break;
1443 case nir_instr_type_tex:
1444 if (!visit_tex_src(nir_instr_as_tex(instr), cb, state))
1445 return false;
1446 break;
1447 case nir_instr_type_call:
1448 /* Call instructions have no regular sources */
1449 break;
1450 case nir_instr_type_load_const:
1451 /* Constant load instructions have no regular sources */
1452 break;
1453 case nir_instr_type_phi:
1454 if (!visit_phi_src(nir_instr_as_phi(instr), cb, state))
1455 return false;
1456 break;
1457 case nir_instr_type_parallel_copy:
1458 if (!visit_parallel_copy_src(nir_instr_as_parallel_copy(instr),
1459 cb, state))
1460 return false;
1461 break;
1462 case nir_instr_type_jump:
1463 case nir_instr_type_ssa_undef:
1464 return true;
1465
1466 default:
1467 unreachable("Invalid instruction type");
1468 break;
1469 }
1470
1471 visit_dest_indirect_state dest_state;
1472 dest_state.state = state;
1473 dest_state.cb = cb;
1474 return nir_foreach_dest(instr, visit_dest_indirect, &dest_state);
1475 }
1476
1477 nir_const_value *
1478 nir_src_as_const_value(nir_src src)
1479 {
1480 if (!src.is_ssa)
1481 return NULL;
1482
1483 if (src.ssa->parent_instr->type != nir_instr_type_load_const)
1484 return NULL;
1485
1486 nir_load_const_instr *load = nir_instr_as_load_const(src.ssa->parent_instr);
1487
1488 return &load->value;
1489 }
1490
1491 /**
1492 * Returns true if the source is known to be dynamically uniform. Otherwise it
1493 * returns false which means it may or may not be dynamically uniform but it
1494 * can't be determined.
1495 */
1496 bool
1497 nir_src_is_dynamically_uniform(nir_src src)
1498 {
1499 if (!src.is_ssa)
1500 return false;
1501
1502 /* Constants are trivially dynamically uniform */
1503 if (src.ssa->parent_instr->type == nir_instr_type_load_const)
1504 return true;
1505
1506 /* As are uniform variables */
1507 if (src.ssa->parent_instr->type == nir_instr_type_intrinsic) {
1508 nir_intrinsic_instr *intr = nir_instr_as_intrinsic(src.ssa->parent_instr);
1509
1510 if (intr->intrinsic == nir_intrinsic_load_uniform)
1511 return true;
1512 }
1513
1514 /* XXX: this could have many more tests, such as when a sampler function is
1515 * called with dynamically uniform arguments.
1516 */
1517 return false;
1518 }
1519
1520 static void
1521 src_remove_all_uses(nir_src *src)
1522 {
1523 for (; src; src = src->is_ssa ? NULL : src->reg.indirect) {
1524 if (!src_is_valid(src))
1525 continue;
1526
1527 list_del(&src->use_link);
1528 }
1529 }
1530
1531 static void
1532 src_add_all_uses(nir_src *src, nir_instr *parent_instr, nir_if *parent_if)
1533 {
1534 for (; src; src = src->is_ssa ? NULL : src->reg.indirect) {
1535 if (!src_is_valid(src))
1536 continue;
1537
1538 if (parent_instr) {
1539 src->parent_instr = parent_instr;
1540 if (src->is_ssa)
1541 list_addtail(&src->use_link, &src->ssa->uses);
1542 else
1543 list_addtail(&src->use_link, &src->reg.reg->uses);
1544 } else {
1545 assert(parent_if);
1546 src->parent_if = parent_if;
1547 if (src->is_ssa)
1548 list_addtail(&src->use_link, &src->ssa->if_uses);
1549 else
1550 list_addtail(&src->use_link, &src->reg.reg->if_uses);
1551 }
1552 }
1553 }
1554
1555 void
1556 nir_instr_rewrite_src(nir_instr *instr, nir_src *src, nir_src new_src)
1557 {
1558 assert(!src_is_valid(src) || src->parent_instr == instr);
1559
1560 src_remove_all_uses(src);
1561 *src = new_src;
1562 src_add_all_uses(src, instr, NULL);
1563 }
1564
1565 void
1566 nir_instr_move_src(nir_instr *dest_instr, nir_src *dest, nir_src *src)
1567 {
1568 assert(!src_is_valid(dest) || dest->parent_instr == dest_instr);
1569
1570 src_remove_all_uses(dest);
1571 src_remove_all_uses(src);
1572 *dest = *src;
1573 *src = NIR_SRC_INIT;
1574 src_add_all_uses(dest, dest_instr, NULL);
1575 }
1576
1577 void
1578 nir_if_rewrite_condition(nir_if *if_stmt, nir_src new_src)
1579 {
1580 nir_src *src = &if_stmt->condition;
1581 assert(!src_is_valid(src) || src->parent_if == if_stmt);
1582
1583 src_remove_all_uses(src);
1584 *src = new_src;
1585 src_add_all_uses(src, NULL, if_stmt);
1586 }
1587
1588 void
1589 nir_instr_rewrite_dest(nir_instr *instr, nir_dest *dest, nir_dest new_dest)
1590 {
1591 if (dest->is_ssa) {
1592 /* We can only overwrite an SSA destination if it has no uses. */
1593 assert(list_empty(&dest->ssa.uses) && list_empty(&dest->ssa.if_uses));
1594 } else {
1595 list_del(&dest->reg.def_link);
1596 if (dest->reg.indirect)
1597 src_remove_all_uses(dest->reg.indirect);
1598 }
1599
1600 /* We can't re-write with an SSA def */
1601 assert(!new_dest.is_ssa);
1602
1603 nir_dest_copy(dest, &new_dest, instr);
1604
1605 dest->reg.parent_instr = instr;
1606 list_addtail(&dest->reg.def_link, &new_dest.reg.reg->defs);
1607
1608 if (dest->reg.indirect)
1609 src_add_all_uses(dest->reg.indirect, instr, NULL);
1610 }
1611
1612 void
1613 nir_instr_rewrite_deref(nir_instr *instr, nir_deref_var **deref,
1614 nir_deref_var *new_deref)
1615 {
1616 if (*deref)
1617 visit_deref_src(*deref, remove_use_cb, NULL);
1618
1619 *deref = new_deref;
1620
1621 if (*deref)
1622 visit_deref_src(*deref, add_use_cb, instr);
1623 }
1624
1625 /* note: does *not* take ownership of 'name' */
1626 void
1627 nir_ssa_def_init(nir_instr *instr, nir_ssa_def *def,
1628 unsigned num_components,
1629 unsigned bit_size, const char *name)
1630 {
1631 def->name = ralloc_strdup(instr, name);
1632 def->parent_instr = instr;
1633 list_inithead(&def->uses);
1634 list_inithead(&def->if_uses);
1635 def->num_components = num_components;
1636 def->bit_size = bit_size;
1637
1638 if (instr->block) {
1639 nir_function_impl *impl =
1640 nir_cf_node_get_function(&instr->block->cf_node);
1641
1642 def->index = impl->ssa_alloc++;
1643 } else {
1644 def->index = UINT_MAX;
1645 }
1646 }
1647
1648 /* note: does *not* take ownership of 'name' */
1649 void
1650 nir_ssa_dest_init(nir_instr *instr, nir_dest *dest,
1651 unsigned num_components, unsigned bit_size,
1652 const char *name)
1653 {
1654 dest->is_ssa = true;
1655 nir_ssa_def_init(instr, &dest->ssa, num_components, bit_size, name);
1656 }
1657
1658 void
1659 nir_ssa_def_rewrite_uses(nir_ssa_def *def, nir_src new_src)
1660 {
1661 assert(!new_src.is_ssa || def != new_src.ssa);
1662
1663 nir_foreach_use_safe(use_src, def)
1664 nir_instr_rewrite_src(use_src->parent_instr, use_src, new_src);
1665
1666 nir_foreach_if_use_safe(use_src, def)
1667 nir_if_rewrite_condition(use_src->parent_if, new_src);
1668 }
1669
1670 static bool
1671 is_instr_between(nir_instr *start, nir_instr *end, nir_instr *between)
1672 {
1673 assert(start->block == end->block);
1674
1675 if (between->block != start->block)
1676 return false;
1677
1678 /* Search backwards looking for "between" */
1679 while (start != end) {
1680 if (between == end)
1681 return true;
1682
1683 end = nir_instr_prev(end);
1684 assert(end);
1685 }
1686
1687 return false;
1688 }
1689
1690 /* Replaces all uses of the given SSA def with the given source but only if
1691 * the use comes after the after_me instruction. This can be useful if you
1692 * are emitting code to fix up the result of some instruction: you can freely
1693 * use the result in that code and then call rewrite_uses_after and pass the
1694 * last fixup instruction as after_me and it will replace all of the uses you
1695 * want without touching the fixup code.
1696 *
1697 * This function assumes that after_me is in the same block as
1698 * def->parent_instr and that after_me comes after def->parent_instr.
1699 */
1700 void
1701 nir_ssa_def_rewrite_uses_after(nir_ssa_def *def, nir_src new_src,
1702 nir_instr *after_me)
1703 {
1704 assert(!new_src.is_ssa || def != new_src.ssa);
1705
1706 nir_foreach_use_safe(use_src, def) {
1707 assert(use_src->parent_instr != def->parent_instr);
1708 /* Since def already dominates all of its uses, the only way a use can
1709 * not be dominated by after_me is if it is between def and after_me in
1710 * the instruction list.
1711 */
1712 if (!is_instr_between(def->parent_instr, after_me, use_src->parent_instr))
1713 nir_instr_rewrite_src(use_src->parent_instr, use_src, new_src);
1714 }
1715
1716 nir_foreach_if_use_safe(use_src, def)
1717 nir_if_rewrite_condition(use_src->parent_if, new_src);
1718 }
1719
1720 uint8_t
1721 nir_ssa_def_components_read(const nir_ssa_def *def)
1722 {
1723 uint8_t read_mask = 0;
1724 nir_foreach_use(use, def) {
1725 if (use->parent_instr->type == nir_instr_type_alu) {
1726 nir_alu_instr *alu = nir_instr_as_alu(use->parent_instr);
1727 nir_alu_src *alu_src = exec_node_data(nir_alu_src, use, src);
1728 int src_idx = alu_src - &alu->src[0];
1729 assert(src_idx >= 0 && src_idx < nir_op_infos[alu->op].num_inputs);
1730
1731 for (unsigned c = 0; c < 4; c++) {
1732 if (!nir_alu_instr_channel_used(alu, src_idx, c))
1733 continue;
1734
1735 read_mask |= (1 << alu_src->swizzle[c]);
1736 }
1737 } else {
1738 return (1 << def->num_components) - 1;
1739 }
1740 }
1741
1742 return read_mask;
1743 }
1744
1745 nir_block *
1746 nir_block_cf_tree_next(nir_block *block)
1747 {
1748 if (block == NULL) {
1749 /* nir_foreach_block_safe() will call this function on a NULL block
1750 * after the last iteration, but it won't use the result so just return
1751 * NULL here.
1752 */
1753 return NULL;
1754 }
1755
1756 nir_cf_node *cf_next = nir_cf_node_next(&block->cf_node);
1757 if (cf_next)
1758 return nir_cf_node_cf_tree_first(cf_next);
1759
1760 nir_cf_node *parent = block->cf_node.parent;
1761
1762 switch (parent->type) {
1763 case nir_cf_node_if: {
1764 /* Are we at the end of the if? Go to the beginning of the else */
1765 nir_if *if_stmt = nir_cf_node_as_if(parent);
1766 if (block == nir_if_last_then_block(if_stmt))
1767 return nir_if_first_else_block(if_stmt);
1768
1769 assert(block == nir_if_last_else_block(if_stmt));
1770 /* fall through */
1771 }
1772
1773 case nir_cf_node_loop:
1774 return nir_cf_node_as_block(nir_cf_node_next(parent));
1775
1776 case nir_cf_node_function:
1777 return NULL;
1778
1779 default:
1780 unreachable("unknown cf node type");
1781 }
1782 }
1783
1784 nir_block *
1785 nir_block_cf_tree_prev(nir_block *block)
1786 {
1787 if (block == NULL) {
1788 /* do this for consistency with nir_block_cf_tree_next() */
1789 return NULL;
1790 }
1791
1792 nir_cf_node *cf_prev = nir_cf_node_prev(&block->cf_node);
1793 if (cf_prev)
1794 return nir_cf_node_cf_tree_last(cf_prev);
1795
1796 nir_cf_node *parent = block->cf_node.parent;
1797
1798 switch (parent->type) {
1799 case nir_cf_node_if: {
1800 /* Are we at the beginning of the else? Go to the end of the if */
1801 nir_if *if_stmt = nir_cf_node_as_if(parent);
1802 if (block == nir_if_first_else_block(if_stmt))
1803 return nir_if_last_then_block(if_stmt);
1804
1805 assert(block == nir_if_first_then_block(if_stmt));
1806 /* fall through */
1807 }
1808
1809 case nir_cf_node_loop:
1810 return nir_cf_node_as_block(nir_cf_node_prev(parent));
1811
1812 case nir_cf_node_function:
1813 return NULL;
1814
1815 default:
1816 unreachable("unknown cf node type");
1817 }
1818 }
1819
1820 nir_block *nir_cf_node_cf_tree_first(nir_cf_node *node)
1821 {
1822 switch (node->type) {
1823 case nir_cf_node_function: {
1824 nir_function_impl *impl = nir_cf_node_as_function(node);
1825 return nir_start_block(impl);
1826 }
1827
1828 case nir_cf_node_if: {
1829 nir_if *if_stmt = nir_cf_node_as_if(node);
1830 return nir_if_first_then_block(if_stmt);
1831 }
1832
1833 case nir_cf_node_loop: {
1834 nir_loop *loop = nir_cf_node_as_loop(node);
1835 return nir_loop_first_block(loop);
1836 }
1837
1838 case nir_cf_node_block: {
1839 return nir_cf_node_as_block(node);
1840 }
1841
1842 default:
1843 unreachable("unknown node type");
1844 }
1845 }
1846
1847 nir_block *nir_cf_node_cf_tree_last(nir_cf_node *node)
1848 {
1849 switch (node->type) {
1850 case nir_cf_node_function: {
1851 nir_function_impl *impl = nir_cf_node_as_function(node);
1852 return nir_impl_last_block(impl);
1853 }
1854
1855 case nir_cf_node_if: {
1856 nir_if *if_stmt = nir_cf_node_as_if(node);
1857 return nir_if_last_else_block(if_stmt);
1858 }
1859
1860 case nir_cf_node_loop: {
1861 nir_loop *loop = nir_cf_node_as_loop(node);
1862 return nir_loop_last_block(loop);
1863 }
1864
1865 case nir_cf_node_block: {
1866 return nir_cf_node_as_block(node);
1867 }
1868
1869 default:
1870 unreachable("unknown node type");
1871 }
1872 }
1873
1874 nir_block *nir_cf_node_cf_tree_next(nir_cf_node *node)
1875 {
1876 if (node->type == nir_cf_node_block)
1877 return nir_block_cf_tree_next(nir_cf_node_as_block(node));
1878 else if (node->type == nir_cf_node_function)
1879 return NULL;
1880 else
1881 return nir_cf_node_as_block(nir_cf_node_next(node));
1882 }
1883
1884 nir_if *
1885 nir_block_get_following_if(nir_block *block)
1886 {
1887 if (exec_node_is_tail_sentinel(&block->cf_node.node))
1888 return NULL;
1889
1890 if (nir_cf_node_is_last(&block->cf_node))
1891 return NULL;
1892
1893 nir_cf_node *next_node = nir_cf_node_next(&block->cf_node);
1894
1895 if (next_node->type != nir_cf_node_if)
1896 return NULL;
1897
1898 return nir_cf_node_as_if(next_node);
1899 }
1900
1901 nir_loop *
1902 nir_block_get_following_loop(nir_block *block)
1903 {
1904 if (exec_node_is_tail_sentinel(&block->cf_node.node))
1905 return NULL;
1906
1907 if (nir_cf_node_is_last(&block->cf_node))
1908 return NULL;
1909
1910 nir_cf_node *next_node = nir_cf_node_next(&block->cf_node);
1911
1912 if (next_node->type != nir_cf_node_loop)
1913 return NULL;
1914
1915 return nir_cf_node_as_loop(next_node);
1916 }
1917
1918 void
1919 nir_index_blocks(nir_function_impl *impl)
1920 {
1921 unsigned index = 0;
1922
1923 if (impl->valid_metadata & nir_metadata_block_index)
1924 return;
1925
1926 nir_foreach_block(block, impl) {
1927 block->index = index++;
1928 }
1929
1930 impl->num_blocks = index;
1931 }
1932
1933 static bool
1934 index_ssa_def_cb(nir_ssa_def *def, void *state)
1935 {
1936 unsigned *index = (unsigned *) state;
1937 def->index = (*index)++;
1938
1939 return true;
1940 }
1941
1942 /**
1943 * The indices are applied top-to-bottom which has the very nice property
1944 * that, if A dominates B, then A->index <= B->index.
1945 */
1946 void
1947 nir_index_ssa_defs(nir_function_impl *impl)
1948 {
1949 unsigned index = 0;
1950
1951 nir_foreach_block(block, impl) {
1952 nir_foreach_instr(instr, block)
1953 nir_foreach_ssa_def(instr, index_ssa_def_cb, &index);
1954 }
1955
1956 impl->ssa_alloc = index;
1957 }
1958
1959 /**
1960 * The indices are applied top-to-bottom which has the very nice property
1961 * that, if A dominates B, then A->index <= B->index.
1962 */
1963 unsigned
1964 nir_index_instrs(nir_function_impl *impl)
1965 {
1966 unsigned index = 0;
1967
1968 nir_foreach_block(block, impl) {
1969 nir_foreach_instr(instr, block)
1970 instr->index = index++;
1971 }
1972
1973 return index;
1974 }
1975
1976 nir_intrinsic_op
1977 nir_intrinsic_from_system_value(gl_system_value val)
1978 {
1979 switch (val) {
1980 case SYSTEM_VALUE_VERTEX_ID:
1981 return nir_intrinsic_load_vertex_id;
1982 case SYSTEM_VALUE_INSTANCE_ID:
1983 return nir_intrinsic_load_instance_id;
1984 case SYSTEM_VALUE_DRAW_ID:
1985 return nir_intrinsic_load_draw_id;
1986 case SYSTEM_VALUE_BASE_INSTANCE:
1987 return nir_intrinsic_load_base_instance;
1988 case SYSTEM_VALUE_VERTEX_ID_ZERO_BASE:
1989 return nir_intrinsic_load_vertex_id_zero_base;
1990 case SYSTEM_VALUE_BASE_VERTEX:
1991 return nir_intrinsic_load_base_vertex;
1992 case SYSTEM_VALUE_INVOCATION_ID:
1993 return nir_intrinsic_load_invocation_id;
1994 case SYSTEM_VALUE_FRAG_COORD:
1995 return nir_intrinsic_load_frag_coord;
1996 case SYSTEM_VALUE_FRONT_FACE:
1997 return nir_intrinsic_load_front_face;
1998 case SYSTEM_VALUE_SAMPLE_ID:
1999 return nir_intrinsic_load_sample_id;
2000 case SYSTEM_VALUE_SAMPLE_POS:
2001 return nir_intrinsic_load_sample_pos;
2002 case SYSTEM_VALUE_SAMPLE_MASK_IN:
2003 return nir_intrinsic_load_sample_mask_in;
2004 case SYSTEM_VALUE_LOCAL_INVOCATION_ID:
2005 return nir_intrinsic_load_local_invocation_id;
2006 case SYSTEM_VALUE_LOCAL_INVOCATION_INDEX:
2007 return nir_intrinsic_load_local_invocation_index;
2008 case SYSTEM_VALUE_WORK_GROUP_ID:
2009 return nir_intrinsic_load_work_group_id;
2010 case SYSTEM_VALUE_NUM_WORK_GROUPS:
2011 return nir_intrinsic_load_num_work_groups;
2012 case SYSTEM_VALUE_PRIMITIVE_ID:
2013 return nir_intrinsic_load_primitive_id;
2014 case SYSTEM_VALUE_TESS_COORD:
2015 return nir_intrinsic_load_tess_coord;
2016 case SYSTEM_VALUE_TESS_LEVEL_OUTER:
2017 return nir_intrinsic_load_tess_level_outer;
2018 case SYSTEM_VALUE_TESS_LEVEL_INNER:
2019 return nir_intrinsic_load_tess_level_inner;
2020 case SYSTEM_VALUE_VERTICES_IN:
2021 return nir_intrinsic_load_patch_vertices_in;
2022 case SYSTEM_VALUE_HELPER_INVOCATION:
2023 return nir_intrinsic_load_helper_invocation;
2024 case SYSTEM_VALUE_VIEW_INDEX:
2025 return nir_intrinsic_load_view_index;
2026 case SYSTEM_VALUE_SUBGROUP_SIZE:
2027 return nir_intrinsic_load_subgroup_size;
2028 case SYSTEM_VALUE_SUBGROUP_INVOCATION:
2029 return nir_intrinsic_load_subgroup_invocation;
2030 case SYSTEM_VALUE_SUBGROUP_EQ_MASK:
2031 return nir_intrinsic_load_subgroup_eq_mask;
2032 case SYSTEM_VALUE_SUBGROUP_GE_MASK:
2033 return nir_intrinsic_load_subgroup_ge_mask;
2034 case SYSTEM_VALUE_SUBGROUP_GT_MASK:
2035 return nir_intrinsic_load_subgroup_gt_mask;
2036 case SYSTEM_VALUE_SUBGROUP_LE_MASK:
2037 return nir_intrinsic_load_subgroup_le_mask;
2038 case SYSTEM_VALUE_SUBGROUP_LT_MASK:
2039 return nir_intrinsic_load_subgroup_lt_mask;
2040 case SYSTEM_VALUE_NUM_SUBGROUPS:
2041 return nir_intrinsic_load_num_subgroups;
2042 case SYSTEM_VALUE_SUBGROUP_ID:
2043 return nir_intrinsic_load_subgroup_id;
2044 case SYSTEM_VALUE_LOCAL_GROUP_SIZE:
2045 return nir_intrinsic_load_local_group_size;
2046 default:
2047 unreachable("system value does not directly correspond to intrinsic");
2048 }
2049 }
2050
2051 gl_system_value
2052 nir_system_value_from_intrinsic(nir_intrinsic_op intrin)
2053 {
2054 switch (intrin) {
2055 case nir_intrinsic_load_vertex_id:
2056 return SYSTEM_VALUE_VERTEX_ID;
2057 case nir_intrinsic_load_instance_id:
2058 return SYSTEM_VALUE_INSTANCE_ID;
2059 case nir_intrinsic_load_draw_id:
2060 return SYSTEM_VALUE_DRAW_ID;
2061 case nir_intrinsic_load_base_instance:
2062 return SYSTEM_VALUE_BASE_INSTANCE;
2063 case nir_intrinsic_load_vertex_id_zero_base:
2064 return SYSTEM_VALUE_VERTEX_ID_ZERO_BASE;
2065 case nir_intrinsic_load_base_vertex:
2066 return SYSTEM_VALUE_BASE_VERTEX;
2067 case nir_intrinsic_load_invocation_id:
2068 return SYSTEM_VALUE_INVOCATION_ID;
2069 case nir_intrinsic_load_frag_coord:
2070 return SYSTEM_VALUE_FRAG_COORD;
2071 case nir_intrinsic_load_front_face:
2072 return SYSTEM_VALUE_FRONT_FACE;
2073 case nir_intrinsic_load_sample_id:
2074 return SYSTEM_VALUE_SAMPLE_ID;
2075 case nir_intrinsic_load_sample_pos:
2076 return SYSTEM_VALUE_SAMPLE_POS;
2077 case nir_intrinsic_load_sample_mask_in:
2078 return SYSTEM_VALUE_SAMPLE_MASK_IN;
2079 case nir_intrinsic_load_local_invocation_id:
2080 return SYSTEM_VALUE_LOCAL_INVOCATION_ID;
2081 case nir_intrinsic_load_local_invocation_index:
2082 return SYSTEM_VALUE_LOCAL_INVOCATION_INDEX;
2083 case nir_intrinsic_load_num_work_groups:
2084 return SYSTEM_VALUE_NUM_WORK_GROUPS;
2085 case nir_intrinsic_load_work_group_id:
2086 return SYSTEM_VALUE_WORK_GROUP_ID;
2087 case nir_intrinsic_load_primitive_id:
2088 return SYSTEM_VALUE_PRIMITIVE_ID;
2089 case nir_intrinsic_load_tess_coord:
2090 return SYSTEM_VALUE_TESS_COORD;
2091 case nir_intrinsic_load_tess_level_outer:
2092 return SYSTEM_VALUE_TESS_LEVEL_OUTER;
2093 case nir_intrinsic_load_tess_level_inner:
2094 return SYSTEM_VALUE_TESS_LEVEL_INNER;
2095 case nir_intrinsic_load_patch_vertices_in:
2096 return SYSTEM_VALUE_VERTICES_IN;
2097 case nir_intrinsic_load_helper_invocation:
2098 return SYSTEM_VALUE_HELPER_INVOCATION;
2099 case nir_intrinsic_load_view_index:
2100 return SYSTEM_VALUE_VIEW_INDEX;
2101 case nir_intrinsic_load_subgroup_size:
2102 return SYSTEM_VALUE_SUBGROUP_SIZE;
2103 case nir_intrinsic_load_subgroup_invocation:
2104 return SYSTEM_VALUE_SUBGROUP_INVOCATION;
2105 case nir_intrinsic_load_subgroup_eq_mask:
2106 return SYSTEM_VALUE_SUBGROUP_EQ_MASK;
2107 case nir_intrinsic_load_subgroup_ge_mask:
2108 return SYSTEM_VALUE_SUBGROUP_GE_MASK;
2109 case nir_intrinsic_load_subgroup_gt_mask:
2110 return SYSTEM_VALUE_SUBGROUP_GT_MASK;
2111 case nir_intrinsic_load_subgroup_le_mask:
2112 return SYSTEM_VALUE_SUBGROUP_LE_MASK;
2113 case nir_intrinsic_load_subgroup_lt_mask:
2114 return SYSTEM_VALUE_SUBGROUP_LT_MASK;
2115 case nir_intrinsic_load_num_subgroups:
2116 return SYSTEM_VALUE_NUM_SUBGROUPS;
2117 case nir_intrinsic_load_subgroup_id:
2118 return SYSTEM_VALUE_SUBGROUP_ID;
2119 case nir_intrinsic_load_local_group_size:
2120 return SYSTEM_VALUE_LOCAL_GROUP_SIZE;
2121 default:
2122 unreachable("intrinsic doesn't produce a system value");
2123 }
2124 }