nir: Store gl_shader_stage in nir_shader.
[mesa.git] / src / glsl / 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
43 shader->options = options;
44
45 exec_list_make_empty(&shader->functions);
46 exec_list_make_empty(&shader->registers);
47 exec_list_make_empty(&shader->globals);
48 exec_list_make_empty(&shader->system_values);
49 shader->reg_alloc = 0;
50
51 shader->num_inputs = 0;
52 shader->num_outputs = 0;
53 shader->num_uniforms = 0;
54
55 shader->stage = stage;
56
57 return shader;
58 }
59
60 static nir_register *
61 reg_create(void *mem_ctx, struct exec_list *list)
62 {
63 nir_register *reg = ralloc(mem_ctx, nir_register);
64
65 list_inithead(&reg->uses);
66 list_inithead(&reg->defs);
67 list_inithead(&reg->if_uses);
68
69 reg->num_components = 0;
70 reg->num_array_elems = 0;
71 reg->is_packed = false;
72 reg->name = NULL;
73
74 exec_list_push_tail(list, &reg->node);
75
76 return reg;
77 }
78
79 nir_register *
80 nir_global_reg_create(nir_shader *shader)
81 {
82 nir_register *reg = reg_create(shader, &shader->registers);
83 reg->index = shader->reg_alloc++;
84 reg->is_global = true;
85
86 return reg;
87 }
88
89 nir_register *
90 nir_local_reg_create(nir_function_impl *impl)
91 {
92 nir_register *reg = reg_create(ralloc_parent(impl), &impl->registers);
93 reg->index = impl->reg_alloc++;
94 reg->is_global = false;
95
96 return reg;
97 }
98
99 void
100 nir_reg_remove(nir_register *reg)
101 {
102 exec_node_remove(&reg->node);
103 }
104
105 nir_function *
106 nir_function_create(nir_shader *shader, const char *name)
107 {
108 nir_function *func = ralloc(shader, nir_function);
109
110 exec_list_push_tail(&shader->functions, &func->node);
111 exec_list_make_empty(&func->overload_list);
112 func->name = ralloc_strdup(func, name);
113 func->shader = shader;
114
115 return func;
116 }
117
118 nir_function_overload *
119 nir_function_overload_create(nir_function *func)
120 {
121 void *mem_ctx = ralloc_parent(func);
122
123 nir_function_overload *overload = ralloc(mem_ctx, nir_function_overload);
124
125 overload->num_params = 0;
126 overload->params = NULL;
127 overload->return_type = glsl_void_type();
128 overload->impl = NULL;
129
130 exec_list_push_tail(&func->overload_list, &overload->node);
131 overload->function = func;
132
133 return overload;
134 }
135
136 void nir_src_copy(nir_src *dest, const nir_src *src, void *mem_ctx)
137 {
138 dest->is_ssa = src->is_ssa;
139 if (src->is_ssa) {
140 dest->ssa = src->ssa;
141 } else {
142 dest->reg.base_offset = src->reg.base_offset;
143 dest->reg.reg = src->reg.reg;
144 if (src->reg.indirect) {
145 dest->reg.indirect = ralloc(mem_ctx, nir_src);
146 nir_src_copy(dest->reg.indirect, src->reg.indirect, mem_ctx);
147 } else {
148 dest->reg.indirect = NULL;
149 }
150 }
151 }
152
153 void nir_dest_copy(nir_dest *dest, const nir_dest *src, void *mem_ctx)
154 {
155 /* Copying an SSA definition makes no sense whatsoever. */
156 assert(!src->is_ssa);
157
158 dest->is_ssa = false;
159
160 dest->reg.base_offset = src->reg.base_offset;
161 dest->reg.reg = src->reg.reg;
162 if (src->reg.indirect) {
163 dest->reg.indirect = ralloc(mem_ctx, nir_src);
164 nir_src_copy(dest->reg.indirect, src->reg.indirect, mem_ctx);
165 } else {
166 dest->reg.indirect = NULL;
167 }
168 }
169
170 void
171 nir_alu_src_copy(nir_alu_src *dest, const nir_alu_src *src, void *mem_ctx)
172 {
173 nir_src_copy(&dest->src, &src->src, mem_ctx);
174 dest->abs = src->abs;
175 dest->negate = src->negate;
176 for (unsigned i = 0; i < 4; i++)
177 dest->swizzle[i] = src->swizzle[i];
178 }
179
180 void
181 nir_alu_dest_copy(nir_alu_dest *dest, const nir_alu_dest *src, void *mem_ctx)
182 {
183 nir_dest_copy(&dest->dest, &src->dest, mem_ctx);
184 dest->write_mask = src->write_mask;
185 dest->saturate = src->saturate;
186 }
187
188
189 static void
190 cf_init(nir_cf_node *node, nir_cf_node_type type)
191 {
192 exec_node_init(&node->node);
193 node->parent = NULL;
194 node->type = type;
195 }
196
197 nir_function_impl *
198 nir_function_impl_create(nir_function_overload *overload)
199 {
200 assert(overload->impl == NULL);
201
202 void *mem_ctx = ralloc_parent(overload);
203
204 nir_function_impl *impl = ralloc(mem_ctx, nir_function_impl);
205
206 overload->impl = impl;
207 impl->overload = overload;
208
209 cf_init(&impl->cf_node, nir_cf_node_function);
210
211 exec_list_make_empty(&impl->body);
212 exec_list_make_empty(&impl->registers);
213 exec_list_make_empty(&impl->locals);
214 impl->num_params = 0;
215 impl->params = NULL;
216 impl->return_var = NULL;
217 impl->reg_alloc = 0;
218 impl->ssa_alloc = 0;
219 impl->valid_metadata = nir_metadata_none;
220
221 /* create start & end blocks */
222 nir_block *start_block = nir_block_create(mem_ctx);
223 nir_block *end_block = nir_block_create(mem_ctx);
224 start_block->cf_node.parent = &impl->cf_node;
225 end_block->cf_node.parent = &impl->cf_node;
226 impl->end_block = end_block;
227
228 exec_list_push_tail(&impl->body, &start_block->cf_node.node);
229
230 start_block->successors[0] = end_block;
231 _mesa_set_add(end_block->predecessors, start_block);
232 return impl;
233 }
234
235 nir_block *
236 nir_block_create(void *mem_ctx)
237 {
238 nir_block *block = ralloc(mem_ctx, nir_block);
239
240 cf_init(&block->cf_node, nir_cf_node_block);
241
242 block->successors[0] = block->successors[1] = NULL;
243 block->predecessors = _mesa_set_create(block, _mesa_hash_pointer,
244 _mesa_key_pointer_equal);
245 block->imm_dom = NULL;
246 block->dom_frontier = _mesa_set_create(block, _mesa_hash_pointer,
247 _mesa_key_pointer_equal);
248
249 exec_list_make_empty(&block->instr_list);
250
251 return block;
252 }
253
254 static inline void
255 src_init(nir_src *src)
256 {
257 src->is_ssa = false;
258 src->reg.reg = NULL;
259 src->reg.indirect = NULL;
260 src->reg.base_offset = 0;
261 }
262
263 nir_if *
264 nir_if_create(void *mem_ctx)
265 {
266 nir_if *if_stmt = ralloc(mem_ctx, nir_if);
267
268 cf_init(&if_stmt->cf_node, nir_cf_node_if);
269 src_init(&if_stmt->condition);
270
271 nir_block *then = nir_block_create(mem_ctx);
272 exec_list_make_empty(&if_stmt->then_list);
273 exec_list_push_tail(&if_stmt->then_list, &then->cf_node.node);
274 then->cf_node.parent = &if_stmt->cf_node;
275
276 nir_block *else_stmt = nir_block_create(mem_ctx);
277 exec_list_make_empty(&if_stmt->else_list);
278 exec_list_push_tail(&if_stmt->else_list, &else_stmt->cf_node.node);
279 else_stmt->cf_node.parent = &if_stmt->cf_node;
280
281 return if_stmt;
282 }
283
284 nir_loop *
285 nir_loop_create(void *mem_ctx)
286 {
287 nir_loop *loop = ralloc(mem_ctx, nir_loop);
288
289 cf_init(&loop->cf_node, nir_cf_node_loop);
290
291 nir_block *body = nir_block_create(mem_ctx);
292 exec_list_make_empty(&loop->body);
293 exec_list_push_tail(&loop->body, &body->cf_node.node);
294 body->cf_node.parent = &loop->cf_node;
295
296 body->successors[0] = body;
297 _mesa_set_add(body->predecessors, body);
298
299 return loop;
300 }
301
302 static void
303 instr_init(nir_instr *instr, nir_instr_type type)
304 {
305 instr->type = type;
306 instr->block = NULL;
307 exec_node_init(&instr->node);
308 }
309
310 static void
311 dest_init(nir_dest *dest)
312 {
313 dest->is_ssa = false;
314 dest->reg.reg = NULL;
315 dest->reg.indirect = NULL;
316 dest->reg.base_offset = 0;
317 }
318
319 static void
320 alu_dest_init(nir_alu_dest *dest)
321 {
322 dest_init(&dest->dest);
323 dest->saturate = false;
324 dest->write_mask = 0xf;
325 }
326
327 static void
328 alu_src_init(nir_alu_src *src)
329 {
330 src_init(&src->src);
331 src->abs = src->negate = false;
332 src->swizzle[0] = 0;
333 src->swizzle[1] = 1;
334 src->swizzle[2] = 2;
335 src->swizzle[3] = 3;
336 }
337
338 nir_alu_instr *
339 nir_alu_instr_create(nir_shader *shader, nir_op op)
340 {
341 unsigned num_srcs = nir_op_infos[op].num_inputs;
342 nir_alu_instr *instr =
343 ralloc_size(shader,
344 sizeof(nir_alu_instr) + num_srcs * sizeof(nir_alu_src));
345
346 instr_init(&instr->instr, nir_instr_type_alu);
347 instr->op = op;
348 alu_dest_init(&instr->dest);
349 for (unsigned i = 0; i < num_srcs; i++)
350 alu_src_init(&instr->src[i]);
351
352 return instr;
353 }
354
355 nir_jump_instr *
356 nir_jump_instr_create(nir_shader *shader, nir_jump_type type)
357 {
358 nir_jump_instr *instr = ralloc(shader, nir_jump_instr);
359 instr_init(&instr->instr, nir_instr_type_jump);
360 instr->type = type;
361 return instr;
362 }
363
364 nir_load_const_instr *
365 nir_load_const_instr_create(nir_shader *shader, unsigned num_components)
366 {
367 nir_load_const_instr *instr = ralloc(shader, nir_load_const_instr);
368 instr_init(&instr->instr, nir_instr_type_load_const);
369
370 nir_ssa_def_init(&instr->instr, &instr->def, num_components, NULL);
371
372 return instr;
373 }
374
375 nir_intrinsic_instr *
376 nir_intrinsic_instr_create(nir_shader *shader, nir_intrinsic_op op)
377 {
378 unsigned num_srcs = nir_intrinsic_infos[op].num_srcs;
379 nir_intrinsic_instr *instr =
380 ralloc_size(shader,
381 sizeof(nir_intrinsic_instr) + num_srcs * sizeof(nir_src));
382
383 instr_init(&instr->instr, nir_instr_type_intrinsic);
384 instr->intrinsic = op;
385
386 if (nir_intrinsic_infos[op].has_dest)
387 dest_init(&instr->dest);
388
389 for (unsigned i = 0; i < num_srcs; i++)
390 src_init(&instr->src[i]);
391
392 return instr;
393 }
394
395 nir_call_instr *
396 nir_call_instr_create(nir_shader *shader, nir_function_overload *callee)
397 {
398 nir_call_instr *instr = ralloc(shader, nir_call_instr);
399 instr_init(&instr->instr, nir_instr_type_call);
400
401 instr->callee = callee;
402 instr->num_params = callee->num_params;
403 instr->params = ralloc_array(instr, nir_deref_var *, instr->num_params);
404 instr->return_deref = NULL;
405
406 return instr;
407 }
408
409 nir_tex_instr *
410 nir_tex_instr_create(nir_shader *shader, unsigned num_srcs)
411 {
412 nir_tex_instr *instr = rzalloc(shader, nir_tex_instr);
413 instr_init(&instr->instr, nir_instr_type_tex);
414
415 dest_init(&instr->dest);
416
417 instr->num_srcs = num_srcs;
418 instr->src = ralloc_array(instr, nir_tex_src, num_srcs);
419 for (unsigned i = 0; i < num_srcs; i++)
420 src_init(&instr->src[i].src);
421
422 instr->sampler_index = 0;
423 instr->sampler_array_size = 0;
424 instr->sampler = NULL;
425
426 return instr;
427 }
428
429 nir_phi_instr *
430 nir_phi_instr_create(nir_shader *shader)
431 {
432 nir_phi_instr *instr = ralloc(shader, nir_phi_instr);
433 instr_init(&instr->instr, nir_instr_type_phi);
434
435 dest_init(&instr->dest);
436 exec_list_make_empty(&instr->srcs);
437 return instr;
438 }
439
440 nir_parallel_copy_instr *
441 nir_parallel_copy_instr_create(nir_shader *shader)
442 {
443 nir_parallel_copy_instr *instr = ralloc(shader, nir_parallel_copy_instr);
444 instr_init(&instr->instr, nir_instr_type_parallel_copy);
445
446 exec_list_make_empty(&instr->entries);
447
448 return instr;
449 }
450
451 nir_ssa_undef_instr *
452 nir_ssa_undef_instr_create(nir_shader *shader, unsigned num_components)
453 {
454 nir_ssa_undef_instr *instr = ralloc(shader, nir_ssa_undef_instr);
455 instr_init(&instr->instr, nir_instr_type_ssa_undef);
456
457 nir_ssa_def_init(&instr->instr, &instr->def, num_components, NULL);
458
459 return instr;
460 }
461
462 nir_deref_var *
463 nir_deref_var_create(void *mem_ctx, nir_variable *var)
464 {
465 nir_deref_var *deref = ralloc(mem_ctx, nir_deref_var);
466 deref->deref.deref_type = nir_deref_type_var;
467 deref->deref.child = NULL;
468 deref->deref.type = var->type;
469 deref->var = var;
470 return deref;
471 }
472
473 nir_deref_array *
474 nir_deref_array_create(void *mem_ctx)
475 {
476 nir_deref_array *deref = ralloc(mem_ctx, nir_deref_array);
477 deref->deref.deref_type = nir_deref_type_array;
478 deref->deref.child = NULL;
479 deref->deref_array_type = nir_deref_array_type_direct;
480 src_init(&deref->indirect);
481 deref->base_offset = 0;
482 return deref;
483 }
484
485 nir_deref_struct *
486 nir_deref_struct_create(void *mem_ctx, unsigned field_index)
487 {
488 nir_deref_struct *deref = ralloc(mem_ctx, nir_deref_struct);
489 deref->deref.deref_type = nir_deref_type_struct;
490 deref->deref.child = NULL;
491 deref->index = field_index;
492 return deref;
493 }
494
495 static nir_deref_var *
496 copy_deref_var(void *mem_ctx, nir_deref_var *deref)
497 {
498 nir_deref_var *ret = nir_deref_var_create(mem_ctx, deref->var);
499 ret->deref.type = deref->deref.type;
500 if (deref->deref.child)
501 ret->deref.child = nir_copy_deref(ret, deref->deref.child);
502 return ret;
503 }
504
505 static nir_deref_array *
506 copy_deref_array(void *mem_ctx, nir_deref_array *deref)
507 {
508 nir_deref_array *ret = nir_deref_array_create(mem_ctx);
509 ret->base_offset = deref->base_offset;
510 ret->deref_array_type = deref->deref_array_type;
511 if (deref->deref_array_type == nir_deref_array_type_indirect) {
512 nir_src_copy(&ret->indirect, &deref->indirect, mem_ctx);
513 }
514 ret->deref.type = deref->deref.type;
515 if (deref->deref.child)
516 ret->deref.child = nir_copy_deref(ret, deref->deref.child);
517 return ret;
518 }
519
520 static nir_deref_struct *
521 copy_deref_struct(void *mem_ctx, nir_deref_struct *deref)
522 {
523 nir_deref_struct *ret = nir_deref_struct_create(mem_ctx, deref->index);
524 ret->deref.type = deref->deref.type;
525 if (deref->deref.child)
526 ret->deref.child = nir_copy_deref(ret, deref->deref.child);
527 return ret;
528 }
529
530 nir_deref *
531 nir_copy_deref(void *mem_ctx, nir_deref *deref)
532 {
533 switch (deref->deref_type) {
534 case nir_deref_type_var:
535 return &copy_deref_var(mem_ctx, nir_deref_as_var(deref))->deref;
536 case nir_deref_type_array:
537 return &copy_deref_array(mem_ctx, nir_deref_as_array(deref))->deref;
538 case nir_deref_type_struct:
539 return &copy_deref_struct(mem_ctx, nir_deref_as_struct(deref))->deref;
540 default:
541 unreachable("Invalid dereference type");
542 }
543
544 return NULL;
545 }
546
547 /* Returns a load_const instruction that represents the constant
548 * initializer for the given deref chain. The caller is responsible for
549 * ensuring that there actually is a constant initializer.
550 */
551 nir_load_const_instr *
552 nir_deref_get_const_initializer_load(nir_shader *shader, nir_deref_var *deref)
553 {
554 nir_constant *constant = deref->var->constant_initializer;
555 assert(constant);
556
557 const nir_deref *tail = &deref->deref;
558 unsigned matrix_offset = 0;
559 while (tail->child) {
560 switch (tail->child->deref_type) {
561 case nir_deref_type_array: {
562 nir_deref_array *arr = nir_deref_as_array(tail->child);
563 assert(arr->deref_array_type == nir_deref_array_type_direct);
564 if (glsl_type_is_matrix(tail->type)) {
565 assert(arr->deref.child == NULL);
566 matrix_offset = arr->base_offset;
567 } else {
568 constant = constant->elements[arr->base_offset];
569 }
570 break;
571 }
572
573 case nir_deref_type_struct: {
574 constant = constant->elements[nir_deref_as_struct(tail->child)->index];
575 break;
576 }
577
578 default:
579 unreachable("Invalid deref child type");
580 }
581
582 tail = tail->child;
583 }
584
585 nir_load_const_instr *load =
586 nir_load_const_instr_create(shader, glsl_get_vector_elements(tail->type));
587
588 matrix_offset *= load->def.num_components;
589 for (unsigned i = 0; i < load->def.num_components; i++) {
590 switch (glsl_get_base_type(tail->type)) {
591 case GLSL_TYPE_FLOAT:
592 case GLSL_TYPE_INT:
593 case GLSL_TYPE_UINT:
594 load->value.u[i] = constant->value.u[matrix_offset + i];
595 break;
596 case GLSL_TYPE_BOOL:
597 load->value.u[i] = constant->value.b[matrix_offset + i] ?
598 NIR_TRUE : NIR_FALSE;
599 break;
600 default:
601 unreachable("Invalid immediate type");
602 }
603 }
604
605 return load;
606 }
607
608 nir_function_impl *
609 nir_cf_node_get_function(nir_cf_node *node)
610 {
611 while (node->type != nir_cf_node_function) {
612 node = node->parent;
613 }
614
615 return nir_cf_node_as_function(node);
616 }
617
618 static bool
619 add_use_cb(nir_src *src, void *state)
620 {
621 nir_instr *instr = state;
622
623 src->parent_instr = instr;
624 list_addtail(&src->use_link,
625 src->is_ssa ? &src->ssa->uses : &src->reg.reg->uses);
626
627 return true;
628 }
629
630 static bool
631 add_ssa_def_cb(nir_ssa_def *def, void *state)
632 {
633 nir_instr *instr = state;
634
635 if (instr->block && def->index == UINT_MAX) {
636 nir_function_impl *impl =
637 nir_cf_node_get_function(&instr->block->cf_node);
638
639 def->index = impl->ssa_alloc++;
640 }
641
642 return true;
643 }
644
645 static bool
646 add_reg_def_cb(nir_dest *dest, void *state)
647 {
648 nir_instr *instr = state;
649
650 if (!dest->is_ssa) {
651 dest->reg.parent_instr = instr;
652 list_addtail(&dest->reg.def_link, &dest->reg.reg->defs);
653 }
654
655 return true;
656 }
657
658 static void
659 add_defs_uses(nir_instr *instr)
660 {
661 nir_foreach_src(instr, add_use_cb, instr);
662 nir_foreach_dest(instr, add_reg_def_cb, instr);
663 nir_foreach_ssa_def(instr, add_ssa_def_cb, instr);
664 }
665
666 void
667 nir_instr_insert_before(nir_instr *instr, nir_instr *before)
668 {
669 assert(before->type != nir_instr_type_jump);
670 before->block = instr->block;
671 add_defs_uses(before);
672 exec_node_insert_node_before(&instr->node, &before->node);
673 }
674
675 void
676 nir_instr_insert_after(nir_instr *instr, nir_instr *after)
677 {
678 if (after->type == nir_instr_type_jump) {
679 assert(instr == nir_block_last_instr(instr->block));
680 assert(instr->type != nir_instr_type_jump);
681 }
682
683 after->block = instr->block;
684 add_defs_uses(after);
685 exec_node_insert_after(&instr->node, &after->node);
686
687 if (after->type == nir_instr_type_jump)
688 nir_handle_add_jump(after->block);
689 }
690
691 void
692 nir_instr_insert_before_block(nir_block *block, nir_instr *before)
693 {
694 if (before->type == nir_instr_type_jump)
695 assert(exec_list_is_empty(&block->instr_list));
696
697 before->block = block;
698 add_defs_uses(before);
699 exec_list_push_head(&block->instr_list, &before->node);
700
701 if (before->type == nir_instr_type_jump)
702 nir_handle_add_jump(block);
703 }
704
705 void
706 nir_instr_insert_after_block(nir_block *block, nir_instr *after)
707 {
708 if (after->type == nir_instr_type_jump) {
709 assert(exec_list_is_empty(&block->instr_list) ||
710 nir_block_last_instr(block)->type != nir_instr_type_jump);
711 }
712
713 after->block = block;
714 add_defs_uses(after);
715 exec_list_push_tail(&block->instr_list, &after->node);
716
717 if (after->type == nir_instr_type_jump)
718 nir_handle_add_jump(block);
719 }
720
721 void
722 nir_instr_insert_before_cf(nir_cf_node *node, nir_instr *before)
723 {
724 if (node->type == nir_cf_node_block) {
725 nir_instr_insert_before_block(nir_cf_node_as_block(node), before);
726 } else {
727 nir_cf_node *prev = nir_cf_node_prev(node);
728 assert(prev->type == nir_cf_node_block);
729 nir_block *prev_block = nir_cf_node_as_block(prev);
730
731 nir_instr_insert_before_block(prev_block, before);
732 }
733 }
734
735 void
736 nir_instr_insert_after_cf(nir_cf_node *node, nir_instr *after)
737 {
738 if (node->type == nir_cf_node_block) {
739 nir_instr_insert_after_block(nir_cf_node_as_block(node), after);
740 } else {
741 nir_cf_node *next = nir_cf_node_next(node);
742 assert(next->type == nir_cf_node_block);
743 nir_block *next_block = nir_cf_node_as_block(next);
744
745 nir_instr_insert_before_block(next_block, after);
746 }
747 }
748
749 void
750 nir_instr_insert_before_cf_list(struct exec_list *list, nir_instr *before)
751 {
752 nir_cf_node *first_node = exec_node_data(nir_cf_node,
753 exec_list_get_head(list), node);
754 nir_instr_insert_before_cf(first_node, before);
755 }
756
757 void
758 nir_instr_insert_after_cf_list(struct exec_list *list, nir_instr *after)
759 {
760 nir_cf_node *last_node = exec_node_data(nir_cf_node,
761 exec_list_get_tail(list), node);
762 nir_instr_insert_after_cf(last_node, after);
763 }
764
765 static bool
766 remove_use_cb(nir_src *src, void *state)
767 {
768 list_del(&src->use_link);
769
770 return true;
771 }
772
773 static bool
774 remove_def_cb(nir_dest *dest, void *state)
775 {
776 if (!dest->is_ssa)
777 list_del(&dest->reg.def_link);
778
779 return true;
780 }
781
782 static void
783 remove_defs_uses(nir_instr *instr)
784 {
785 nir_foreach_dest(instr, remove_def_cb, instr);
786 nir_foreach_src(instr, remove_use_cb, instr);
787 }
788
789 void nir_instr_remove(nir_instr *instr)
790 {
791 remove_defs_uses(instr);
792 exec_node_remove(&instr->node);
793
794 if (instr->type == nir_instr_type_jump) {
795 nir_jump_instr *jump_instr = nir_instr_as_jump(instr);
796 nir_handle_remove_jump(instr->block, jump_instr->type);
797 }
798 }
799
800 /*@}*/
801
802 void
803 nir_index_local_regs(nir_function_impl *impl)
804 {
805 unsigned index = 0;
806 foreach_list_typed(nir_register, reg, node, &impl->registers) {
807 reg->index = index++;
808 }
809 impl->reg_alloc = index;
810 }
811
812 void
813 nir_index_global_regs(nir_shader *shader)
814 {
815 unsigned index = 0;
816 foreach_list_typed(nir_register, reg, node, &shader->registers) {
817 reg->index = index++;
818 }
819 shader->reg_alloc = index;
820 }
821
822 static bool
823 visit_alu_dest(nir_alu_instr *instr, nir_foreach_dest_cb cb, void *state)
824 {
825 return cb(&instr->dest.dest, state);
826 }
827
828 static bool
829 visit_intrinsic_dest(nir_intrinsic_instr *instr, nir_foreach_dest_cb cb,
830 void *state)
831 {
832 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
833 return cb(&instr->dest, state);
834
835 return true;
836 }
837
838 static bool
839 visit_texture_dest(nir_tex_instr *instr, nir_foreach_dest_cb cb,
840 void *state)
841 {
842 return cb(&instr->dest, state);
843 }
844
845 static bool
846 visit_phi_dest(nir_phi_instr *instr, nir_foreach_dest_cb cb, void *state)
847 {
848 return cb(&instr->dest, state);
849 }
850
851 static bool
852 visit_parallel_copy_dest(nir_parallel_copy_instr *instr,
853 nir_foreach_dest_cb cb, void *state)
854 {
855 nir_foreach_parallel_copy_entry(instr, entry) {
856 if (!cb(&entry->dest, state))
857 return false;
858 }
859
860 return true;
861 }
862
863 bool
864 nir_foreach_dest(nir_instr *instr, nir_foreach_dest_cb cb, void *state)
865 {
866 switch (instr->type) {
867 case nir_instr_type_alu:
868 return visit_alu_dest(nir_instr_as_alu(instr), cb, state);
869 case nir_instr_type_intrinsic:
870 return visit_intrinsic_dest(nir_instr_as_intrinsic(instr), cb, state);
871 case nir_instr_type_tex:
872 return visit_texture_dest(nir_instr_as_tex(instr), cb, state);
873 case nir_instr_type_phi:
874 return visit_phi_dest(nir_instr_as_phi(instr), cb, state);
875 case nir_instr_type_parallel_copy:
876 return visit_parallel_copy_dest(nir_instr_as_parallel_copy(instr),
877 cb, state);
878
879 case nir_instr_type_load_const:
880 case nir_instr_type_ssa_undef:
881 case nir_instr_type_call:
882 case nir_instr_type_jump:
883 break;
884
885 default:
886 unreachable("Invalid instruction type");
887 break;
888 }
889
890 return true;
891 }
892
893 struct foreach_ssa_def_state {
894 nir_foreach_ssa_def_cb cb;
895 void *client_state;
896 };
897
898 static inline bool
899 nir_ssa_def_visitor(nir_dest *dest, void *void_state)
900 {
901 struct foreach_ssa_def_state *state = void_state;
902
903 if (dest->is_ssa)
904 return state->cb(&dest->ssa, state->client_state);
905 else
906 return true;
907 }
908
909 bool
910 nir_foreach_ssa_def(nir_instr *instr, nir_foreach_ssa_def_cb cb, void *state)
911 {
912 switch (instr->type) {
913 case nir_instr_type_alu:
914 case nir_instr_type_tex:
915 case nir_instr_type_intrinsic:
916 case nir_instr_type_phi:
917 case nir_instr_type_parallel_copy: {
918 struct foreach_ssa_def_state foreach_state = {cb, state};
919 return nir_foreach_dest(instr, nir_ssa_def_visitor, &foreach_state);
920 }
921
922 case nir_instr_type_load_const:
923 return cb(&nir_instr_as_load_const(instr)->def, state);
924 case nir_instr_type_ssa_undef:
925 return cb(&nir_instr_as_ssa_undef(instr)->def, state);
926 case nir_instr_type_call:
927 case nir_instr_type_jump:
928 return true;
929 default:
930 unreachable("Invalid instruction type");
931 }
932 }
933
934 static bool
935 visit_src(nir_src *src, nir_foreach_src_cb cb, void *state)
936 {
937 if (!cb(src, state))
938 return false;
939 if (!src->is_ssa && src->reg.indirect)
940 return cb(src->reg.indirect, state);
941 return true;
942 }
943
944 static bool
945 visit_deref_array_src(nir_deref_array *deref, nir_foreach_src_cb cb,
946 void *state)
947 {
948 if (deref->deref_array_type == nir_deref_array_type_indirect)
949 return visit_src(&deref->indirect, cb, state);
950 return true;
951 }
952
953 static bool
954 visit_deref_src(nir_deref_var *deref, nir_foreach_src_cb cb, void *state)
955 {
956 nir_deref *cur = &deref->deref;
957 while (cur != NULL) {
958 if (cur->deref_type == nir_deref_type_array)
959 if (!visit_deref_array_src(nir_deref_as_array(cur), cb, state))
960 return false;
961
962 cur = cur->child;
963 }
964
965 return true;
966 }
967
968 static bool
969 visit_alu_src(nir_alu_instr *instr, nir_foreach_src_cb cb, void *state)
970 {
971 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++)
972 if (!visit_src(&instr->src[i].src, cb, state))
973 return false;
974
975 return true;
976 }
977
978 static bool
979 visit_tex_src(nir_tex_instr *instr, nir_foreach_src_cb cb, void *state)
980 {
981 for (unsigned i = 0; i < instr->num_srcs; i++)
982 if (!visit_src(&instr->src[i].src, cb, state))
983 return false;
984
985 if (instr->sampler != NULL)
986 if (!visit_deref_src(instr->sampler, cb, state))
987 return false;
988
989 return true;
990 }
991
992 static bool
993 visit_intrinsic_src(nir_intrinsic_instr *instr, nir_foreach_src_cb cb,
994 void *state)
995 {
996 unsigned num_srcs = nir_intrinsic_infos[instr->intrinsic].num_srcs;
997 for (unsigned i = 0; i < num_srcs; i++)
998 if (!visit_src(&instr->src[i], cb, state))
999 return false;
1000
1001 unsigned num_vars =
1002 nir_intrinsic_infos[instr->intrinsic].num_variables;
1003 for (unsigned i = 0; i < num_vars; i++)
1004 if (!visit_deref_src(instr->variables[i], cb, state))
1005 return false;
1006
1007 return true;
1008 }
1009
1010 static bool
1011 visit_call_src(nir_call_instr *instr, nir_foreach_src_cb cb, void *state)
1012 {
1013 return true;
1014 }
1015
1016 static bool
1017 visit_load_const_src(nir_load_const_instr *instr, nir_foreach_src_cb cb,
1018 void *state)
1019 {
1020 return true;
1021 }
1022
1023 static bool
1024 visit_phi_src(nir_phi_instr *instr, nir_foreach_src_cb cb, void *state)
1025 {
1026 nir_foreach_phi_src(instr, src) {
1027 if (!visit_src(&src->src, cb, state))
1028 return false;
1029 }
1030
1031 return true;
1032 }
1033
1034 static bool
1035 visit_parallel_copy_src(nir_parallel_copy_instr *instr,
1036 nir_foreach_src_cb cb, void *state)
1037 {
1038 nir_foreach_parallel_copy_entry(instr, entry) {
1039 if (!visit_src(&entry->src, cb, state))
1040 return false;
1041 }
1042
1043 return true;
1044 }
1045
1046 typedef struct {
1047 void *state;
1048 nir_foreach_src_cb cb;
1049 } visit_dest_indirect_state;
1050
1051 static bool
1052 visit_dest_indirect(nir_dest *dest, void *_state)
1053 {
1054 visit_dest_indirect_state *state = (visit_dest_indirect_state *) _state;
1055
1056 if (!dest->is_ssa && dest->reg.indirect)
1057 return state->cb(dest->reg.indirect, state->state);
1058
1059 return true;
1060 }
1061
1062 bool
1063 nir_foreach_src(nir_instr *instr, nir_foreach_src_cb cb, void *state)
1064 {
1065 switch (instr->type) {
1066 case nir_instr_type_alu:
1067 if (!visit_alu_src(nir_instr_as_alu(instr), cb, state))
1068 return false;
1069 break;
1070 case nir_instr_type_intrinsic:
1071 if (!visit_intrinsic_src(nir_instr_as_intrinsic(instr), cb, state))
1072 return false;
1073 break;
1074 case nir_instr_type_tex:
1075 if (!visit_tex_src(nir_instr_as_tex(instr), cb, state))
1076 return false;
1077 break;
1078 case nir_instr_type_call:
1079 if (!visit_call_src(nir_instr_as_call(instr), cb, state))
1080 return false;
1081 break;
1082 case nir_instr_type_load_const:
1083 if (!visit_load_const_src(nir_instr_as_load_const(instr), cb, state))
1084 return false;
1085 break;
1086 case nir_instr_type_phi:
1087 if (!visit_phi_src(nir_instr_as_phi(instr), cb, state))
1088 return false;
1089 break;
1090 case nir_instr_type_parallel_copy:
1091 if (!visit_parallel_copy_src(nir_instr_as_parallel_copy(instr),
1092 cb, state))
1093 return false;
1094 break;
1095 case nir_instr_type_jump:
1096 case nir_instr_type_ssa_undef:
1097 return true;
1098
1099 default:
1100 unreachable("Invalid instruction type");
1101 break;
1102 }
1103
1104 visit_dest_indirect_state dest_state;
1105 dest_state.state = state;
1106 dest_state.cb = cb;
1107 return nir_foreach_dest(instr, visit_dest_indirect, &dest_state);
1108 }
1109
1110 nir_const_value *
1111 nir_src_as_const_value(nir_src src)
1112 {
1113 if (!src.is_ssa)
1114 return NULL;
1115
1116 if (src.ssa->parent_instr->type != nir_instr_type_load_const)
1117 return NULL;
1118
1119 nir_load_const_instr *load = nir_instr_as_load_const(src.ssa->parent_instr);
1120
1121 return &load->value;
1122 }
1123
1124 bool
1125 nir_srcs_equal(nir_src src1, nir_src src2)
1126 {
1127 if (src1.is_ssa) {
1128 if (src2.is_ssa) {
1129 return src1.ssa == src2.ssa;
1130 } else {
1131 return false;
1132 }
1133 } else {
1134 if (src2.is_ssa) {
1135 return false;
1136 } else {
1137 if ((src1.reg.indirect == NULL) != (src2.reg.indirect == NULL))
1138 return false;
1139
1140 if (src1.reg.indirect) {
1141 if (!nir_srcs_equal(*src1.reg.indirect, *src2.reg.indirect))
1142 return false;
1143 }
1144
1145 return src1.reg.reg == src2.reg.reg &&
1146 src1.reg.base_offset == src2.reg.base_offset;
1147 }
1148 }
1149 }
1150
1151 static bool
1152 src_is_valid(const nir_src *src)
1153 {
1154 return src->is_ssa ? (src->ssa != NULL) : (src->reg.reg != NULL);
1155 }
1156
1157 static void
1158 src_remove_all_uses(nir_src *src)
1159 {
1160 for (; src; src = src->is_ssa ? NULL : src->reg.indirect) {
1161 if (!src_is_valid(src))
1162 continue;
1163
1164 list_del(&src->use_link);
1165 }
1166 }
1167
1168 static void
1169 src_add_all_uses(nir_src *src, nir_instr *parent_instr, nir_if *parent_if)
1170 {
1171 for (; src; src = src->is_ssa ? NULL : src->reg.indirect) {
1172 if (!src_is_valid(src))
1173 continue;
1174
1175 if (parent_instr) {
1176 src->parent_instr = parent_instr;
1177 if (src->is_ssa)
1178 list_addtail(&src->use_link, &src->ssa->uses);
1179 else
1180 list_addtail(&src->use_link, &src->reg.reg->uses);
1181 } else {
1182 assert(parent_if);
1183 src->parent_if = parent_if;
1184 if (src->is_ssa)
1185 list_addtail(&src->use_link, &src->ssa->if_uses);
1186 else
1187 list_addtail(&src->use_link, &src->reg.reg->if_uses);
1188 }
1189 }
1190 }
1191
1192 void
1193 nir_instr_rewrite_src(nir_instr *instr, nir_src *src, nir_src new_src)
1194 {
1195 assert(!src_is_valid(src) || src->parent_instr == instr);
1196
1197 src_remove_all_uses(src);
1198 *src = new_src;
1199 src_add_all_uses(src, instr, NULL);
1200 }
1201
1202 void
1203 nir_instr_move_src(nir_instr *dest_instr, nir_src *dest, nir_src *src)
1204 {
1205 assert(!src_is_valid(dest) || dest->parent_instr == dest_instr);
1206
1207 src_remove_all_uses(dest);
1208 src_remove_all_uses(src);
1209 *dest = *src;
1210 *src = NIR_SRC_INIT;
1211 src_add_all_uses(dest, dest_instr, NULL);
1212 }
1213
1214 void
1215 nir_if_rewrite_condition(nir_if *if_stmt, nir_src new_src)
1216 {
1217 nir_src *src = &if_stmt->condition;
1218 assert(!src_is_valid(src) || src->parent_if == if_stmt);
1219
1220 src_remove_all_uses(src);
1221 *src = new_src;
1222 src_add_all_uses(src, NULL, if_stmt);
1223 }
1224
1225 void
1226 nir_ssa_def_init(nir_instr *instr, nir_ssa_def *def,
1227 unsigned num_components, const char *name)
1228 {
1229 def->name = name;
1230 def->parent_instr = instr;
1231 list_inithead(&def->uses);
1232 list_inithead(&def->if_uses);
1233 def->num_components = num_components;
1234
1235 if (instr->block) {
1236 nir_function_impl *impl =
1237 nir_cf_node_get_function(&instr->block->cf_node);
1238
1239 def->index = impl->ssa_alloc++;
1240 } else {
1241 def->index = UINT_MAX;
1242 }
1243 }
1244
1245 void
1246 nir_ssa_dest_init(nir_instr *instr, nir_dest *dest,
1247 unsigned num_components, const char *name)
1248 {
1249 dest->is_ssa = true;
1250 nir_ssa_def_init(instr, &dest->ssa, num_components, name);
1251 }
1252
1253 void
1254 nir_ssa_def_rewrite_uses(nir_ssa_def *def, nir_src new_src, void *mem_ctx)
1255 {
1256 assert(!new_src.is_ssa || def != new_src.ssa);
1257
1258 nir_foreach_use_safe(def, use_src) {
1259 nir_instr *src_parent_instr = use_src->parent_instr;
1260 list_del(&use_src->use_link);
1261 nir_src_copy(use_src, &new_src, mem_ctx);
1262 src_add_all_uses(use_src, src_parent_instr, NULL);
1263 }
1264
1265 nir_foreach_if_use_safe(def, use_src) {
1266 nir_if *src_parent_if = use_src->parent_if;
1267 list_del(&use_src->use_link);
1268 nir_src_copy(use_src, &new_src, mem_ctx);
1269 src_add_all_uses(use_src, NULL, src_parent_if);
1270 }
1271 }
1272
1273
1274 static bool foreach_cf_node(nir_cf_node *node, nir_foreach_block_cb cb,
1275 bool reverse, void *state);
1276
1277 static inline bool
1278 foreach_if(nir_if *if_stmt, nir_foreach_block_cb cb, bool reverse, void *state)
1279 {
1280 if (reverse) {
1281 foreach_list_typed_safe_reverse(nir_cf_node, node, node,
1282 &if_stmt->else_list) {
1283 if (!foreach_cf_node(node, cb, reverse, state))
1284 return false;
1285 }
1286
1287 foreach_list_typed_safe_reverse(nir_cf_node, node, node,
1288 &if_stmt->then_list) {
1289 if (!foreach_cf_node(node, cb, reverse, state))
1290 return false;
1291 }
1292 } else {
1293 foreach_list_typed_safe(nir_cf_node, node, node, &if_stmt->then_list) {
1294 if (!foreach_cf_node(node, cb, reverse, state))
1295 return false;
1296 }
1297
1298 foreach_list_typed_safe(nir_cf_node, node, node, &if_stmt->else_list) {
1299 if (!foreach_cf_node(node, cb, reverse, state))
1300 return false;
1301 }
1302 }
1303
1304 return true;
1305 }
1306
1307 static inline bool
1308 foreach_loop(nir_loop *loop, nir_foreach_block_cb cb, bool reverse, void *state)
1309 {
1310 if (reverse) {
1311 foreach_list_typed_safe_reverse(nir_cf_node, node, node, &loop->body) {
1312 if (!foreach_cf_node(node, cb, reverse, state))
1313 return false;
1314 }
1315 } else {
1316 foreach_list_typed_safe(nir_cf_node, node, node, &loop->body) {
1317 if (!foreach_cf_node(node, cb, reverse, state))
1318 return false;
1319 }
1320 }
1321
1322 return true;
1323 }
1324
1325 static bool
1326 foreach_cf_node(nir_cf_node *node, nir_foreach_block_cb cb,
1327 bool reverse, void *state)
1328 {
1329 switch (node->type) {
1330 case nir_cf_node_block:
1331 return cb(nir_cf_node_as_block(node), state);
1332 case nir_cf_node_if:
1333 return foreach_if(nir_cf_node_as_if(node), cb, reverse, state);
1334 case nir_cf_node_loop:
1335 return foreach_loop(nir_cf_node_as_loop(node), cb, reverse, state);
1336 break;
1337
1338 default:
1339 unreachable("Invalid CFG node type");
1340 break;
1341 }
1342
1343 return false;
1344 }
1345
1346 bool
1347 nir_foreach_block(nir_function_impl *impl, nir_foreach_block_cb cb, void *state)
1348 {
1349 foreach_list_typed_safe(nir_cf_node, node, node, &impl->body) {
1350 if (!foreach_cf_node(node, cb, false, state))
1351 return false;
1352 }
1353
1354 return cb(impl->end_block, state);
1355 }
1356
1357 bool
1358 nir_foreach_block_reverse(nir_function_impl *impl, nir_foreach_block_cb cb,
1359 void *state)
1360 {
1361 if (!cb(impl->end_block, state))
1362 return false;
1363
1364 foreach_list_typed_safe_reverse(nir_cf_node, node, node, &impl->body) {
1365 if (!foreach_cf_node(node, cb, true, state))
1366 return false;
1367 }
1368
1369 return true;
1370 }
1371
1372 nir_if *
1373 nir_block_get_following_if(nir_block *block)
1374 {
1375 if (exec_node_is_tail_sentinel(&block->cf_node.node))
1376 return NULL;
1377
1378 if (nir_cf_node_is_last(&block->cf_node))
1379 return NULL;
1380
1381 nir_cf_node *next_node = nir_cf_node_next(&block->cf_node);
1382
1383 if (next_node->type != nir_cf_node_if)
1384 return NULL;
1385
1386 return nir_cf_node_as_if(next_node);
1387 }
1388
1389 static bool
1390 index_block(nir_block *block, void *state)
1391 {
1392 unsigned *index = state;
1393 block->index = (*index)++;
1394 return true;
1395 }
1396
1397 void
1398 nir_index_blocks(nir_function_impl *impl)
1399 {
1400 unsigned index = 0;
1401
1402 if (impl->valid_metadata & nir_metadata_block_index)
1403 return;
1404
1405 nir_foreach_block(impl, index_block, &index);
1406
1407 impl->num_blocks = index;
1408 }
1409
1410 static bool
1411 index_ssa_def_cb(nir_ssa_def *def, void *state)
1412 {
1413 unsigned *index = (unsigned *) state;
1414 def->index = (*index)++;
1415
1416 return true;
1417 }
1418
1419 static bool
1420 index_ssa_block(nir_block *block, void *state)
1421 {
1422 nir_foreach_instr(block, instr)
1423 nir_foreach_ssa_def(instr, index_ssa_def_cb, state);
1424
1425 return true;
1426 }
1427
1428 void
1429 nir_index_ssa_defs(nir_function_impl *impl)
1430 {
1431 unsigned index = 0;
1432 nir_foreach_block(impl, index_ssa_block, &index);
1433 impl->ssa_alloc = index;
1434 }