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