nir: Fix a bit of broken indentation.
[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 <assert.h>
30
31 nir_shader *
32 nir_shader_create(void *mem_ctx)
33 {
34 nir_shader *shader = ralloc(mem_ctx, nir_shader);
35
36 shader->uniforms = _mesa_hash_table_create(shader, _mesa_key_hash_string,
37 _mesa_key_string_equal);
38 shader->inputs = _mesa_hash_table_create(shader, _mesa_key_hash_string,
39 _mesa_key_string_equal);
40 shader->outputs = _mesa_hash_table_create(shader, _mesa_key_hash_string,
41 _mesa_key_string_equal);
42
43 shader->num_user_structures = 0;
44 shader->user_structures = NULL;
45
46 exec_list_make_empty(&shader->functions);
47 exec_list_make_empty(&shader->registers);
48 exec_list_make_empty(&shader->globals);
49 exec_list_make_empty(&shader->system_values);
50 shader->reg_alloc = 0;
51
52 shader->num_inputs = 0;
53 shader->num_outputs = 0;
54 shader->num_uniforms = 0;
55
56 return shader;
57 }
58
59 static nir_register *
60 reg_create(void *mem_ctx, struct exec_list *list)
61 {
62 nir_register *reg = ralloc(mem_ctx, nir_register);
63
64 reg->uses = _mesa_set_create(mem_ctx, _mesa_hash_pointer,
65 _mesa_key_pointer_equal);
66 reg->defs = _mesa_set_create(mem_ctx, _mesa_hash_pointer,
67 _mesa_key_pointer_equal);
68 reg->if_uses = _mesa_set_create(mem_ctx, _mesa_hash_pointer,
69 _mesa_key_pointer_equal);
70
71 reg->num_components = 0;
72 reg->num_array_elems = 0;
73 reg->is_packed = false;
74 reg->name = NULL;
75
76 exec_list_push_tail(list, &reg->node);
77
78 return reg;
79 }
80
81 nir_register *
82 nir_global_reg_create(nir_shader *shader)
83 {
84 nir_register *reg = reg_create(shader, &shader->registers);
85 reg->index = shader->reg_alloc++;
86 reg->is_global = true;
87
88 return reg;
89 }
90
91 nir_register *
92 nir_local_reg_create(nir_function_impl *impl)
93 {
94 nir_register *reg = reg_create(ralloc_parent(impl), &impl->registers);
95 reg->index = impl->reg_alloc++;
96 reg->is_global = false;
97
98 return reg;
99 }
100
101 void
102 nir_reg_remove(nir_register *reg)
103 {
104 exec_node_remove(&reg->node);
105 }
106
107 nir_function *
108 nir_function_create(nir_shader *shader, const char *name)
109 {
110 nir_function *func = ralloc(shader, nir_function);
111
112 exec_list_push_tail(&shader->functions, &func->node);
113 exec_list_make_empty(&func->overload_list);
114 func->name = name;
115 func->shader = shader;
116
117 return func;
118 }
119
120 nir_function_overload *
121 nir_function_overload_create(nir_function *func)
122 {
123 void *mem_ctx = ralloc_parent(func);
124
125 nir_function_overload *overload = ralloc(mem_ctx, nir_function_overload);
126
127 overload->num_params = 0;
128 overload->params = NULL;
129 overload->return_type = glsl_void_type();
130 overload->impl = NULL;
131
132 exec_list_push_tail(&func->overload_list, &overload->node);
133 overload->function = func;
134
135 return overload;
136 }
137
138 void nir_src_copy(nir_src *dest, const nir_src *src, void *mem_ctx)
139 {
140 dest->is_ssa = src->is_ssa;
141 if (src->is_ssa) {
142 dest->ssa = src->ssa;
143 } else {
144 dest->reg.base_offset = src->reg.base_offset;
145 dest->reg.reg = src->reg.reg;
146 if (src->reg.indirect) {
147 dest->reg.indirect = ralloc(mem_ctx, nir_src);
148 nir_src_copy(dest->reg.indirect, src->reg.indirect, mem_ctx);
149 } else {
150 dest->reg.indirect = NULL;
151 }
152 }
153 }
154
155 void nir_dest_copy(nir_dest *dest, const nir_dest *src, void *mem_ctx)
156 {
157 dest->is_ssa = src->is_ssa;
158 if (src->is_ssa) {
159 dest->ssa = src->ssa;
160 } else {
161 dest->reg.base_offset = src->reg.base_offset;
162 dest->reg.reg = src->reg.reg;
163 if (src->reg.indirect) {
164 dest->reg.indirect = ralloc(mem_ctx, nir_src);
165 nir_src_copy(dest->reg.indirect, src->reg.indirect, mem_ctx);
166 } else {
167 dest->reg.indirect = NULL;
168 }
169 }
170 }
171
172 void
173 nir_alu_src_copy(nir_alu_src *dest, const nir_alu_src *src, void *mem_ctx)
174 {
175 nir_src_copy(&dest->src, &src->src, mem_ctx);
176 dest->abs = src->abs;
177 dest->negate = src->negate;
178 for (unsigned i = 0; i < 4; i++)
179 dest->swizzle[i] = src->swizzle[i];
180 }
181
182 void
183 nir_alu_dest_copy(nir_alu_dest *dest, const nir_alu_dest *src, void *mem_ctx)
184 {
185 nir_dest_copy(&dest->dest, &src->dest, mem_ctx);
186 dest->write_mask = src->write_mask;
187 dest->saturate = src->saturate;
188 }
189
190 static inline void
191 block_add_pred(nir_block *block, nir_block *pred)
192 {
193 _mesa_set_add(block->predecessors, pred);
194 }
195
196 static void
197 cf_init(nir_cf_node *node, nir_cf_node_type type)
198 {
199 exec_node_init(&node->node);
200 node->parent = NULL;
201 node->type = type;
202 }
203
204 static void
205 link_blocks(nir_block *pred, nir_block *succ1, nir_block *succ2)
206 {
207 pred->successors[0] = succ1;
208 block_add_pred(succ1, pred);
209
210 pred->successors[1] = succ2;
211 if (succ2 != NULL)
212 block_add_pred(succ2, pred);
213 }
214
215 static void
216 unlink_blocks(nir_block *pred, nir_block *succ)
217 {
218 if (pred->successors[0] == succ) {
219 pred->successors[0] = pred->successors[1];
220 pred->successors[1] = NULL;
221 } else {
222 assert(pred->successors[1] == succ);
223 pred->successors[1] = NULL;
224 }
225
226 struct set_entry *entry = _mesa_set_search(succ->predecessors, pred);
227
228 assert(entry);
229
230 _mesa_set_remove(succ->predecessors, entry);
231 }
232
233 static void
234 unlink_block_successors(nir_block *block)
235 {
236 if (block->successors[0] != NULL)
237 unlink_blocks(block, block->successors[0]);
238 if (block->successors[1] != NULL)
239 unlink_blocks(block, block->successors[1]);
240 }
241
242
243 nir_function_impl *
244 nir_function_impl_create(nir_function_overload *overload)
245 {
246 assert(overload->impl == NULL);
247
248 void *mem_ctx = ralloc_parent(overload);
249
250 nir_function_impl *impl = ralloc(mem_ctx, nir_function_impl);
251
252 overload->impl = impl;
253 impl->overload = overload;
254
255 cf_init(&impl->cf_node, nir_cf_node_function);
256
257 exec_list_make_empty(&impl->body);
258 exec_list_make_empty(&impl->registers);
259 exec_list_make_empty(&impl->locals);
260 impl->num_params = 0;
261 impl->params = NULL;
262 impl->return_var = NULL;
263 impl->reg_alloc = 0;
264 impl->ssa_alloc = 0;
265 impl->valid_metadata = nir_metadata_none;
266
267 /* create start & end blocks */
268 nir_block *start_block = nir_block_create(mem_ctx);
269 nir_block *end_block = nir_block_create(mem_ctx);
270 start_block->cf_node.parent = &impl->cf_node;
271 end_block->cf_node.parent = &impl->cf_node;
272 impl->start_block = start_block;
273 impl->end_block = end_block;
274
275 exec_list_push_tail(&impl->body, &start_block->cf_node.node);
276
277 start_block->successors[0] = end_block;
278 block_add_pred(end_block, start_block);
279
280 return impl;
281 }
282
283 nir_block *
284 nir_block_create(void *mem_ctx)
285 {
286 nir_block *block = ralloc(mem_ctx, nir_block);
287
288 cf_init(&block->cf_node, nir_cf_node_block);
289
290 block->successors[0] = block->successors[1] = NULL;
291 block->predecessors = _mesa_set_create(mem_ctx, _mesa_hash_pointer,
292 _mesa_key_pointer_equal);
293 block->imm_dom = NULL;
294 block->dom_frontier = _mesa_set_create(mem_ctx, _mesa_hash_pointer,
295 _mesa_key_pointer_equal);
296
297 exec_list_make_empty(&block->instr_list);
298
299 return block;
300 }
301
302 static inline void
303 src_init(nir_src *src)
304 {
305 src->is_ssa = false;
306 src->reg.reg = NULL;
307 src->reg.indirect = NULL;
308 src->reg.base_offset = 0;
309 }
310
311 nir_if *
312 nir_if_create(void *mem_ctx)
313 {
314 nir_if *if_stmt = ralloc(mem_ctx, nir_if);
315
316 cf_init(&if_stmt->cf_node, nir_cf_node_if);
317 src_init(&if_stmt->condition);
318
319 nir_block *then = nir_block_create(mem_ctx);
320 exec_list_make_empty(&if_stmt->then_list);
321 exec_list_push_tail(&if_stmt->then_list, &then->cf_node.node);
322 then->cf_node.parent = &if_stmt->cf_node;
323
324 nir_block *else_stmt = nir_block_create(mem_ctx);
325 exec_list_make_empty(&if_stmt->else_list);
326 exec_list_push_tail(&if_stmt->else_list, &else_stmt->cf_node.node);
327 else_stmt->cf_node.parent = &if_stmt->cf_node;
328
329 return if_stmt;
330 }
331
332 nir_loop *
333 nir_loop_create(void *mem_ctx)
334 {
335 nir_loop *loop = ralloc(mem_ctx, nir_loop);
336
337 cf_init(&loop->cf_node, nir_cf_node_loop);
338
339 nir_block *body = nir_block_create(mem_ctx);
340 exec_list_make_empty(&loop->body);
341 exec_list_push_tail(&loop->body, &body->cf_node.node);
342 body->cf_node.parent = &loop->cf_node;
343
344 body->successors[0] = body;
345 block_add_pred(body, body);
346
347 return loop;
348 }
349
350 static void
351 instr_init(nir_instr *instr, nir_instr_type type)
352 {
353 instr->type = type;
354 instr->block = NULL;
355 exec_node_init(&instr->node);
356 }
357
358 static void
359 dest_init(nir_dest *dest)
360 {
361 dest->is_ssa = false;
362 dest->reg.reg = NULL;
363 dest->reg.indirect = NULL;
364 dest->reg.base_offset = 0;
365 }
366
367 static void
368 alu_dest_init(nir_alu_dest *dest)
369 {
370 dest_init(&dest->dest);
371 dest->saturate = false;
372 dest->write_mask = 0xf;
373 }
374
375 static void
376 alu_src_init(nir_alu_src *src)
377 {
378 src_init(&src->src);
379 src->abs = src->negate = false;
380 src->swizzle[0] = 0;
381 src->swizzle[1] = 1;
382 src->swizzle[2] = 2;
383 src->swizzle[3] = 3;
384 }
385
386 nir_alu_instr *
387 nir_alu_instr_create(void *mem_ctx, nir_op op)
388 {
389 unsigned num_srcs = nir_op_infos[op].num_inputs;
390 nir_alu_instr *instr =
391 ralloc_size(mem_ctx,
392 sizeof(nir_alu_instr) + num_srcs * sizeof(nir_alu_src));
393
394 instr_init(&instr->instr, nir_instr_type_alu);
395 instr->op = op;
396 alu_dest_init(&instr->dest);
397 for (unsigned i = 0; i < num_srcs; i++)
398 alu_src_init(&instr->src[i]);
399
400 return instr;
401 }
402
403 nir_jump_instr *
404 nir_jump_instr_create(void *mem_ctx, nir_jump_type type)
405 {
406 nir_jump_instr *instr = ralloc(mem_ctx, nir_jump_instr);
407 instr_init(&instr->instr, nir_instr_type_jump);
408 instr->type = type;
409 return instr;
410 }
411
412 nir_load_const_instr *
413 nir_load_const_instr_create(void *mem_ctx, unsigned num_components)
414 {
415 nir_load_const_instr *instr = ralloc(mem_ctx, nir_load_const_instr);
416 instr_init(&instr->instr, nir_instr_type_load_const);
417
418 nir_ssa_def_init(&instr->instr, &instr->def, num_components, NULL);
419
420 return instr;
421 }
422
423 nir_intrinsic_instr *
424 nir_intrinsic_instr_create(void *mem_ctx, nir_intrinsic_op op)
425 {
426 unsigned num_srcs = nir_intrinsic_infos[op].num_srcs;
427 nir_intrinsic_instr *instr =
428 ralloc_size(mem_ctx,
429 sizeof(nir_intrinsic_instr) + num_srcs * sizeof(nir_src));
430
431 instr_init(&instr->instr, nir_instr_type_intrinsic);
432 instr->intrinsic = op;
433
434 if (nir_intrinsic_infos[op].has_dest)
435 dest_init(&instr->dest);
436
437 for (unsigned i = 0; i < num_srcs; i++)
438 src_init(&instr->src[i]);
439
440 return instr;
441 }
442
443 nir_call_instr *
444 nir_call_instr_create(void *mem_ctx, nir_function_overload *callee)
445 {
446 nir_call_instr *instr = ralloc(mem_ctx, nir_call_instr);
447 instr_init(&instr->instr, nir_instr_type_call);
448
449 instr->callee = callee;
450 instr->num_params = callee->num_params;
451 instr->params = ralloc_array(mem_ctx, nir_deref_var *, instr->num_params);
452 instr->return_deref = NULL;
453
454 return instr;
455 }
456
457 nir_tex_instr *
458 nir_tex_instr_create(void *mem_ctx, unsigned num_srcs)
459 {
460 nir_tex_instr *instr = ralloc(mem_ctx, nir_tex_instr);
461 instr_init(&instr->instr, nir_instr_type_tex);
462
463 dest_init(&instr->dest);
464
465 instr->num_srcs = num_srcs;
466 instr->src = ralloc_array(mem_ctx, nir_tex_src, num_srcs);
467 for (unsigned i = 0; i < num_srcs; i++)
468 src_init(&instr->src[i].src);
469
470 instr->sampler_index = 0;
471 instr->sampler_array_size = 0;
472 instr->sampler = NULL;
473
474 return instr;
475 }
476
477 nir_phi_instr *
478 nir_phi_instr_create(void *mem_ctx)
479 {
480 nir_phi_instr *instr = ralloc(mem_ctx, nir_phi_instr);
481 instr_init(&instr->instr, nir_instr_type_phi);
482
483 dest_init(&instr->dest);
484 exec_list_make_empty(&instr->srcs);
485 return instr;
486 }
487
488 nir_parallel_copy_instr *
489 nir_parallel_copy_instr_create(void *mem_ctx)
490 {
491 nir_parallel_copy_instr *instr = ralloc(mem_ctx, nir_parallel_copy_instr);
492 instr_init(&instr->instr, nir_instr_type_parallel_copy);
493
494 exec_list_make_empty(&instr->entries);
495
496 return instr;
497 }
498
499 nir_ssa_undef_instr *
500 nir_ssa_undef_instr_create(void *mem_ctx, unsigned num_components)
501 {
502 nir_ssa_undef_instr *instr = ralloc(mem_ctx, nir_ssa_undef_instr);
503 instr_init(&instr->instr, nir_instr_type_ssa_undef);
504
505 nir_ssa_def_init(&instr->instr, &instr->def, num_components, NULL);
506
507 return instr;
508 }
509
510 nir_deref_var *
511 nir_deref_var_create(void *mem_ctx, nir_variable *var)
512 {
513 nir_deref_var *deref = ralloc(mem_ctx, nir_deref_var);
514 deref->deref.deref_type = nir_deref_type_var;
515 deref->deref.child = NULL;
516 deref->deref.type = var->type;
517 deref->var = var;
518 return deref;
519 }
520
521 nir_deref_array *
522 nir_deref_array_create(void *mem_ctx)
523 {
524 nir_deref_array *deref = ralloc(mem_ctx, nir_deref_array);
525 deref->deref.deref_type = nir_deref_type_array;
526 deref->deref.child = NULL;
527 deref->deref_array_type = nir_deref_array_type_direct;
528 src_init(&deref->indirect);
529 deref->base_offset = 0;
530 return deref;
531 }
532
533 nir_deref_struct *
534 nir_deref_struct_create(void *mem_ctx, unsigned field_index)
535 {
536 nir_deref_struct *deref = ralloc(mem_ctx, nir_deref_struct);
537 deref->deref.deref_type = nir_deref_type_struct;
538 deref->deref.child = NULL;
539 deref->index = field_index;
540 return deref;
541 }
542
543 static nir_deref_var *
544 copy_deref_var(void *mem_ctx, nir_deref_var *deref)
545 {
546 nir_deref_var *ret = nir_deref_var_create(mem_ctx, deref->var);
547 ret->deref.type = deref->deref.type;
548 if (deref->deref.child)
549 ret->deref.child = nir_copy_deref(mem_ctx, deref->deref.child);
550 return ret;
551 }
552
553 static nir_deref_array *
554 copy_deref_array(void *mem_ctx, nir_deref_array *deref)
555 {
556 nir_deref_array *ret = nir_deref_array_create(mem_ctx);
557 ret->base_offset = deref->base_offset;
558 ret->deref_array_type = deref->deref_array_type;
559 if (deref->deref_array_type == nir_deref_array_type_indirect) {
560 nir_src_copy(&ret->indirect, &deref->indirect, mem_ctx);
561 }
562 ret->deref.type = deref->deref.type;
563 if (deref->deref.child)
564 ret->deref.child = nir_copy_deref(mem_ctx, deref->deref.child);
565 return ret;
566 }
567
568 static nir_deref_struct *
569 copy_deref_struct(void *mem_ctx, nir_deref_struct *deref)
570 {
571 nir_deref_struct *ret = nir_deref_struct_create(mem_ctx, deref->index);
572 ret->deref.type = deref->deref.type;
573 if (deref->deref.child)
574 ret->deref.child = nir_copy_deref(mem_ctx, deref->deref.child);
575 return ret;
576 }
577
578 nir_deref *
579 nir_copy_deref(void *mem_ctx, nir_deref *deref)
580 {
581 switch (deref->deref_type) {
582 case nir_deref_type_var:
583 return &copy_deref_var(mem_ctx, nir_deref_as_var(deref))->deref;
584 case nir_deref_type_array:
585 return &copy_deref_array(mem_ctx, nir_deref_as_array(deref))->deref;
586 case nir_deref_type_struct:
587 return &copy_deref_struct(mem_ctx, nir_deref_as_struct(deref))->deref;
588 default:
589 unreachable("Invalid dereference type");
590 }
591
592 return NULL;
593 }
594
595
596 /**
597 * \name Control flow modification
598 *
599 * These functions modify the control flow tree while keeping the control flow
600 * graph up-to-date. The invariants respected are:
601 * 1. Each then statement, else statement, or loop body must have at least one
602 * control flow node.
603 * 2. Each if-statement and loop must have one basic block before it and one
604 * after.
605 * 3. Two basic blocks cannot be directly next to each other.
606 * 4. If a basic block has a jump instruction, there must be only one and it
607 * must be at the end of the block.
608 * 5. The CFG must always be connected - this means that we must insert a fake
609 * CFG edge for loops with no break statement.
610 *
611 * The purpose of the second one is so that we have places to insert code during
612 * GCM, as well as eliminating the possibility of critical edges.
613 */
614 /*@{*/
615
616 static void
617 link_non_block_to_block(nir_cf_node *node, nir_block *block)
618 {
619 if (node->type == nir_cf_node_if) {
620 /*
621 * We're trying to link an if to a block after it; this just means linking
622 * the last block of the then and else branches.
623 */
624
625 nir_if *if_stmt = nir_cf_node_as_if(node);
626
627 nir_cf_node *last_then = nir_if_last_then_node(if_stmt);
628 assert(last_then->type == nir_cf_node_block);
629 nir_block *last_then_block = nir_cf_node_as_block(last_then);
630
631 nir_cf_node *last_else = nir_if_last_else_node(if_stmt);
632 assert(last_else->type == nir_cf_node_block);
633 nir_block *last_else_block = nir_cf_node_as_block(last_else);
634
635 if (exec_list_is_empty(&last_then_block->instr_list) ||
636 nir_block_last_instr(last_then_block)->type != nir_instr_type_jump) {
637 unlink_block_successors(last_then_block);
638 link_blocks(last_then_block, block, NULL);
639 }
640
641 if (exec_list_is_empty(&last_else_block->instr_list) ||
642 nir_block_last_instr(last_else_block)->type != nir_instr_type_jump) {
643 unlink_block_successors(last_else_block);
644 link_blocks(last_else_block, block, NULL);
645 }
646 } else {
647 assert(node->type == nir_cf_node_loop);
648
649 /*
650 * We can only get to this codepath if we're inserting a new loop, or
651 * at least a loop with no break statements; we can't insert break
652 * statements into a loop when we haven't inserted it into the CFG
653 * because we wouldn't know which block comes after the loop
654 * and therefore, which block should be the successor of the block with
655 * the break). Therefore, we need to insert a fake edge (see invariant
656 * #5).
657 */
658
659 nir_loop *loop = nir_cf_node_as_loop(node);
660
661 nir_cf_node *last = nir_loop_last_cf_node(loop);
662 assert(last->type == nir_cf_node_block);
663 nir_block *last_block = nir_cf_node_as_block(last);
664
665 last_block->successors[1] = block;
666 block_add_pred(block, last_block);
667 }
668 }
669
670 static void
671 link_block_to_non_block(nir_block *block, nir_cf_node *node)
672 {
673 if (node->type == nir_cf_node_if) {
674 /*
675 * We're trying to link a block to an if after it; this just means linking
676 * the block to the first block of the then and else branches.
677 */
678
679 nir_if *if_stmt = nir_cf_node_as_if(node);
680
681 nir_cf_node *first_then = nir_if_first_then_node(if_stmt);
682 assert(first_then->type == nir_cf_node_block);
683 nir_block *first_then_block = nir_cf_node_as_block(first_then);
684
685 nir_cf_node *first_else = nir_if_first_else_node(if_stmt);
686 assert(first_else->type == nir_cf_node_block);
687 nir_block *first_else_block = nir_cf_node_as_block(first_else);
688
689 unlink_block_successors(block);
690 link_blocks(block, first_then_block, first_else_block);
691 } else {
692 /*
693 * For similar reasons as the corresponding case in
694 * link_non_block_to_block(), don't worry about if the loop header has
695 * any predecessors that need to be unlinked.
696 */
697
698 assert(node->type == nir_cf_node_loop);
699
700 nir_loop *loop = nir_cf_node_as_loop(node);
701
702 nir_cf_node *loop_header = nir_loop_first_cf_node(loop);
703 assert(loop_header->type == nir_cf_node_block);
704 nir_block *loop_header_block = nir_cf_node_as_block(loop_header);
705
706 unlink_block_successors(block);
707 link_blocks(block, loop_header_block, NULL);
708 }
709
710 }
711
712 /**
713 * Takes a basic block and inserts a new empty basic block before it, making its
714 * predecessors point to the new block. This essentially splits the block into
715 * an empty header and a body so that another non-block CF node can be inserted
716 * between the two. Note that this does *not* link the two basic blocks, so
717 * some kind of cleanup *must* be performed after this call.
718 */
719
720 static nir_block *
721 split_block_beginning(nir_block *block)
722 {
723 nir_block *new_block = nir_block_create(ralloc_parent(block));
724 new_block->cf_node.parent = block->cf_node.parent;
725 exec_node_insert_node_before(&block->cf_node.node, &new_block->cf_node.node);
726
727 struct set_entry *entry;
728 set_foreach(block->predecessors, entry) {
729 nir_block *pred = (nir_block *) entry->key;
730
731 unlink_blocks(pred, block);
732 link_blocks(pred, new_block, NULL);
733 }
734
735 return new_block;
736 }
737
738 static void
739 rewrite_phi_preds(nir_block *block, nir_block *old_pred, nir_block *new_pred)
740 {
741 nir_foreach_instr_safe(block, instr) {
742 if (instr->type != nir_instr_type_phi)
743 break;
744
745 nir_phi_instr *phi = nir_instr_as_phi(instr);
746 nir_foreach_phi_src(phi, src) {
747 if (src->pred == old_pred) {
748 src->pred = new_pred;
749 break;
750 }
751 }
752 }
753 }
754
755 /**
756 * Moves the successors of source to the successors of dest, leaving both
757 * successors of source NULL.
758 */
759
760 static void
761 move_successors(nir_block *source, nir_block *dest)
762 {
763 nir_block *succ1 = source->successors[0];
764 nir_block *succ2 = source->successors[1];
765
766 if (succ1) {
767 unlink_blocks(source, succ1);
768 rewrite_phi_preds(succ1, source, dest);
769 }
770
771 if (succ2) {
772 unlink_blocks(source, succ2);
773 rewrite_phi_preds(succ2, source, dest);
774 }
775
776 unlink_block_successors(dest);
777 link_blocks(dest, succ1, succ2);
778 }
779
780 static nir_block *
781 split_block_end(nir_block *block)
782 {
783 nir_block *new_block = nir_block_create(ralloc_parent(block));
784 new_block->cf_node.parent = block->cf_node.parent;
785 exec_node_insert_after(&block->cf_node.node, &new_block->cf_node.node);
786
787 move_successors(block, new_block);
788
789 return new_block;
790 }
791
792 /**
793 * Inserts a non-basic block between two basic blocks and links them together.
794 */
795
796 static void
797 insert_non_block(nir_block *before, nir_cf_node *node, nir_block *after)
798 {
799 node->parent = before->cf_node.parent;
800 exec_node_insert_after(&before->cf_node.node, &node->node);
801 link_block_to_non_block(before, node);
802 link_non_block_to_block(node, after);
803 }
804
805 /**
806 * Inserts a non-basic block before a basic block.
807 */
808
809 static void
810 insert_non_block_before_block(nir_cf_node *node, nir_block *block)
811 {
812 /* split off the beginning of block into new_block */
813 nir_block *new_block = split_block_beginning(block);
814
815 /* insert our node in between new_block and block */
816 insert_non_block(new_block, node, block);
817 }
818
819 static void
820 insert_non_block_after_block(nir_block *block, nir_cf_node *node)
821 {
822 /* split off the end of block into new_block */
823 nir_block *new_block = split_block_end(block);
824
825 /* insert our node in between block and new_block */
826 insert_non_block(block, node, new_block);
827 }
828
829 /* walk up the control flow tree to find the innermost enclosed loop */
830 static nir_loop *
831 nearest_loop(nir_cf_node *node)
832 {
833 while (node->type != nir_cf_node_loop) {
834 node = node->parent;
835 }
836
837 return nir_cf_node_as_loop(node);
838 }
839
840 nir_function_impl *
841 nir_cf_node_get_function(nir_cf_node *node)
842 {
843 while (node->type != nir_cf_node_function) {
844 node = node->parent;
845 }
846
847 return nir_cf_node_as_function(node);
848 }
849
850 /*
851 * update the CFG after a jump instruction has been added to the end of a block
852 */
853
854 static void
855 handle_jump(nir_block *block)
856 {
857 nir_instr *instr = nir_block_last_instr(block);
858 nir_jump_instr *jump_instr = nir_instr_as_jump(instr);
859
860 unlink_block_successors(block);
861
862 nir_function_impl *impl = nir_cf_node_get_function(&block->cf_node);
863 nir_metadata_preserve(impl, nir_metadata_none);
864
865 if (jump_instr->type == nir_jump_break ||
866 jump_instr->type == nir_jump_continue) {
867 nir_loop *loop = nearest_loop(&block->cf_node);
868
869 if (jump_instr->type == nir_jump_continue) {
870 nir_cf_node *first_node = nir_loop_first_cf_node(loop);
871 assert(first_node->type == nir_cf_node_block);
872 nir_block *first_block = nir_cf_node_as_block(first_node);
873 link_blocks(block, first_block, NULL);
874 } else {
875 nir_cf_node *after = nir_cf_node_next(&loop->cf_node);
876 assert(after->type == nir_cf_node_block);
877 nir_block *after_block = nir_cf_node_as_block(after);
878 link_blocks(block, after_block, NULL);
879
880 /* If we inserted a fake link, remove it */
881 nir_cf_node *last = nir_loop_last_cf_node(loop);
882 assert(last->type == nir_cf_node_block);
883 nir_block *last_block = nir_cf_node_as_block(last);
884 if (last_block->successors[1] != NULL)
885 unlink_blocks(last_block, after_block);
886 }
887 } else {
888 assert(jump_instr->type == nir_jump_return);
889 link_blocks(block, impl->end_block, NULL);
890 }
891 }
892
893 static void
894 handle_remove_jump(nir_block *block, nir_jump_type type)
895 {
896 unlink_block_successors(block);
897
898 if (exec_node_is_tail_sentinel(block->cf_node.node.next)) {
899 nir_cf_node *parent = block->cf_node.parent;
900 if (parent->type == nir_cf_node_if) {
901 nir_cf_node *next = nir_cf_node_next(parent);
902 assert(next->type == nir_cf_node_block);
903 nir_block *next_block = nir_cf_node_as_block(next);
904
905 link_blocks(block, next_block, NULL);
906 } else {
907 assert(parent->type == nir_cf_node_loop);
908 nir_loop *loop = nir_cf_node_as_loop(parent);
909
910 nir_cf_node *head = nir_loop_first_cf_node(loop);
911 assert(head->type == nir_cf_node_block);
912 nir_block *head_block = nir_cf_node_as_block(head);
913
914 link_blocks(block, head_block, NULL);
915 }
916 } else {
917 nir_cf_node *next = nir_cf_node_next(&block->cf_node);
918 if (next->type == nir_cf_node_if) {
919 nir_if *next_if = nir_cf_node_as_if(next);
920
921 nir_cf_node *first_then = nir_if_first_then_node(next_if);
922 assert(first_then->type == nir_cf_node_block);
923 nir_block *first_then_block = nir_cf_node_as_block(first_then);
924
925 nir_cf_node *first_else = nir_if_first_else_node(next_if);
926 assert(first_else->type == nir_cf_node_block);
927 nir_block *first_else_block = nir_cf_node_as_block(first_else);
928
929 link_blocks(block, first_then_block, first_else_block);
930 } else {
931 assert(next->type == nir_cf_node_loop);
932 nir_loop *next_loop = nir_cf_node_as_loop(next);
933
934 nir_cf_node *first = nir_loop_first_cf_node(next_loop);
935 assert(first->type == nir_cf_node_block);
936 nir_block *first_block = nir_cf_node_as_block(first);
937
938 link_blocks(block, first_block, NULL);
939 }
940 }
941
942 if (type == nir_jump_break) {
943 nir_loop *loop = nearest_loop(&block->cf_node);
944
945 nir_cf_node *next = nir_cf_node_next(&loop->cf_node);
946 assert(next->type == nir_cf_node_block);
947 nir_block *next_block = nir_cf_node_as_block(next);
948
949 if (next_block->predecessors->entries == 0) {
950 /* insert fake link */
951 nir_cf_node *last = nir_loop_last_cf_node(loop);
952 assert(last->type == nir_cf_node_block);
953 nir_block *last_block = nir_cf_node_as_block(last);
954
955 last_block->successors[1] = next_block;
956 block_add_pred(next_block, last_block);
957 }
958 }
959
960 nir_function_impl *impl = nir_cf_node_get_function(&block->cf_node);
961 nir_metadata_preserve(impl, nir_metadata_none);
962 }
963
964 /**
965 * Inserts a basic block before another by merging the instructions.
966 *
967 * @param block the target of the insertion
968 * @param before the block to be inserted - must not have been inserted before
969 * @param has_jump whether \before has a jump instruction at the end
970 */
971
972 static void
973 insert_block_before_block(nir_block *block, nir_block *before, bool has_jump)
974 {
975 assert(!has_jump || exec_list_is_empty(&block->instr_list));
976
977 foreach_list_typed(nir_instr, instr, node, &before->instr_list) {
978 instr->block = block;
979 }
980
981 exec_list_prepend(&block->instr_list, &before->instr_list);
982
983 if (has_jump)
984 handle_jump(block);
985 }
986
987 /**
988 * Inserts a basic block after another by merging the instructions.
989 *
990 * @param block the target of the insertion
991 * @param after the block to be inserted - must not have been inserted before
992 * @param has_jump whether \after has a jump instruction at the end
993 */
994
995 static void
996 insert_block_after_block(nir_block *block, nir_block *after, bool has_jump)
997 {
998 foreach_list_typed(nir_instr, instr, node, &after->instr_list) {
999 instr->block = block;
1000 }
1001
1002 exec_list_append(&block->instr_list, &after->instr_list);
1003
1004 if (has_jump)
1005 handle_jump(block);
1006 }
1007
1008 static void
1009 update_if_uses(nir_cf_node *node)
1010 {
1011 if (node->type != nir_cf_node_if)
1012 return;
1013
1014 nir_if *if_stmt = nir_cf_node_as_if(node);
1015
1016 struct set *if_uses_set = if_stmt->condition.is_ssa ?
1017 if_stmt->condition.ssa->if_uses :
1018 if_stmt->condition.reg.reg->uses;
1019
1020 _mesa_set_add(if_uses_set, if_stmt);
1021 }
1022
1023 void
1024 nir_cf_node_insert_after(nir_cf_node *node, nir_cf_node *after)
1025 {
1026 update_if_uses(after);
1027
1028 if (after->type == nir_cf_node_block) {
1029 /*
1030 * either node or the one after it must be a basic block, by invariant #2;
1031 * in either case, just merge the blocks together.
1032 */
1033 nir_block *after_block = nir_cf_node_as_block(after);
1034
1035 bool has_jump = !exec_list_is_empty(&after_block->instr_list) &&
1036 nir_block_last_instr(after_block)->type == nir_instr_type_jump;
1037
1038 if (node->type == nir_cf_node_block) {
1039 insert_block_after_block(nir_cf_node_as_block(node), after_block,
1040 has_jump);
1041 } else {
1042 nir_cf_node *next = nir_cf_node_next(node);
1043 assert(next->type == nir_cf_node_block);
1044 nir_block *next_block = nir_cf_node_as_block(next);
1045
1046 insert_block_before_block(next_block, after_block, has_jump);
1047 }
1048 } else {
1049 if (node->type == nir_cf_node_block) {
1050 insert_non_block_after_block(nir_cf_node_as_block(node), after);
1051 } else {
1052 /*
1053 * We have to insert a non-basic block after a non-basic block. Since
1054 * every non-basic block has a basic block after it, this is equivalent
1055 * to inserting a non-basic block before a basic block.
1056 */
1057
1058 nir_cf_node *next = nir_cf_node_next(node);
1059 assert(next->type == nir_cf_node_block);
1060 nir_block *next_block = nir_cf_node_as_block(next);
1061
1062 insert_non_block_before_block(after, next_block);
1063 }
1064 }
1065
1066 nir_function_impl *impl = nir_cf_node_get_function(node);
1067 nir_metadata_preserve(impl, nir_metadata_none);
1068 }
1069
1070 void
1071 nir_cf_node_insert_before(nir_cf_node *node, nir_cf_node *before)
1072 {
1073 update_if_uses(before);
1074
1075 if (before->type == nir_cf_node_block) {
1076 nir_block *before_block = nir_cf_node_as_block(before);
1077
1078 bool has_jump = !exec_list_is_empty(&before_block->instr_list) &&
1079 nir_block_last_instr(before_block)->type == nir_instr_type_jump;
1080
1081 if (node->type == nir_cf_node_block) {
1082 insert_block_before_block(nir_cf_node_as_block(node), before_block,
1083 has_jump);
1084 } else {
1085 nir_cf_node *prev = nir_cf_node_prev(node);
1086 assert(prev->type == nir_cf_node_block);
1087 nir_block *prev_block = nir_cf_node_as_block(prev);
1088
1089 insert_block_after_block(prev_block, before_block, has_jump);
1090 }
1091 } else {
1092 if (node->type == nir_cf_node_block) {
1093 insert_non_block_before_block(before, nir_cf_node_as_block(node));
1094 } else {
1095 /*
1096 * We have to insert a non-basic block before a non-basic block. This
1097 * is equivalent to inserting a non-basic block after a basic block.
1098 */
1099
1100 nir_cf_node *prev_node = nir_cf_node_prev(node);
1101 assert(prev_node->type == nir_cf_node_block);
1102 nir_block *prev_block = nir_cf_node_as_block(prev_node);
1103
1104 insert_non_block_after_block(prev_block, before);
1105 }
1106 }
1107
1108 nir_function_impl *impl = nir_cf_node_get_function(node);
1109 nir_metadata_preserve(impl, nir_metadata_none);
1110 }
1111
1112 void
1113 nir_cf_node_insert_begin(struct exec_list *list, nir_cf_node *node)
1114 {
1115 nir_cf_node *begin = exec_node_data(nir_cf_node, list->head, node);
1116 nir_cf_node_insert_before(begin, node);
1117 }
1118
1119 void
1120 nir_cf_node_insert_end(struct exec_list *list, nir_cf_node *node)
1121 {
1122 nir_cf_node *end = exec_node_data(nir_cf_node, list->tail_pred, node);
1123 nir_cf_node_insert_after(end, node);
1124 }
1125
1126 /**
1127 * Stitch two basic blocks together into one. The aggregate must have the same
1128 * predecessors as the first and the same successors as the second.
1129 */
1130
1131 static void
1132 stitch_blocks(nir_block *before, nir_block *after)
1133 {
1134 /*
1135 * We move after into before, so we have to deal with up to 2 successors vs.
1136 * possibly a large number of predecessors.
1137 *
1138 * TODO: special case when before is empty and after isn't?
1139 */
1140
1141 move_successors(after, before);
1142
1143 foreach_list_typed(nir_instr, instr, node, &after->instr_list) {
1144 instr->block = before;
1145 }
1146
1147 exec_list_append(&before->instr_list, &after->instr_list);
1148 exec_node_remove(&after->cf_node.node);
1149 }
1150
1151 void
1152 nir_cf_node_remove(nir_cf_node *node)
1153 {
1154 nir_function_impl *impl = nir_cf_node_get_function(node);
1155 nir_metadata_preserve(impl, nir_metadata_none);
1156
1157 if (node->type == nir_cf_node_block) {
1158 /*
1159 * Basic blocks can't really be removed by themselves, since they act as
1160 * padding between the non-basic blocks. So all we do here is empty the
1161 * block of instructions.
1162 *
1163 * TODO: could we assert here?
1164 */
1165 exec_list_make_empty(&nir_cf_node_as_block(node)->instr_list);
1166 } else {
1167 nir_cf_node *before = nir_cf_node_prev(node);
1168 assert(before->type == nir_cf_node_block);
1169 nir_block *before_block = nir_cf_node_as_block(before);
1170
1171 nir_cf_node *after = nir_cf_node_next(node);
1172 assert(after->type == nir_cf_node_block);
1173 nir_block *after_block = nir_cf_node_as_block(after);
1174
1175 exec_node_remove(&node->node);
1176 stitch_blocks(before_block, after_block);
1177 }
1178 }
1179
1180 static bool
1181 add_use_cb(nir_src *src, void *state)
1182 {
1183 nir_instr *instr = (nir_instr *) state;
1184
1185 struct set *uses_set = src->is_ssa ? src->ssa->uses : src->reg.reg->uses;
1186
1187 _mesa_set_add(uses_set, instr);
1188
1189 return true;
1190 }
1191
1192 static bool
1193 add_ssa_def_cb(nir_ssa_def *def, void *state)
1194 {
1195 nir_instr *instr = (nir_instr *) state;
1196
1197 if (instr->block && def->index == UINT_MAX) {
1198 nir_function_impl *impl =
1199 nir_cf_node_get_function(&instr->block->cf_node);
1200
1201 def->index = impl->ssa_alloc++;
1202 }
1203
1204 return true;
1205 }
1206
1207 static bool
1208 add_reg_def_cb(nir_dest *dest, void *state)
1209 {
1210 nir_instr *instr = (nir_instr *) state;
1211
1212 if (!dest->is_ssa)
1213 _mesa_set_add(dest->reg.reg->defs, instr);
1214
1215 return true;
1216 }
1217
1218 static void
1219 add_defs_uses(nir_instr *instr)
1220 {
1221 nir_foreach_src(instr, add_use_cb, instr);
1222 nir_foreach_dest(instr, add_reg_def_cb, instr);
1223 nir_foreach_ssa_def(instr, add_ssa_def_cb, instr);
1224 }
1225
1226 void
1227 nir_instr_insert_before(nir_instr *instr, nir_instr *before)
1228 {
1229 assert(before->type != nir_instr_type_jump);
1230 before->block = instr->block;
1231 add_defs_uses(before);
1232 exec_node_insert_node_before(&instr->node, &before->node);
1233 }
1234
1235 void
1236 nir_instr_insert_after(nir_instr *instr, nir_instr *after)
1237 {
1238 if (after->type == nir_instr_type_jump) {
1239 assert(instr == nir_block_last_instr(instr->block));
1240 assert(instr->type != nir_instr_type_jump);
1241 }
1242
1243 after->block = instr->block;
1244 add_defs_uses(after);
1245 exec_node_insert_after(&instr->node, &after->node);
1246
1247 if (after->type == nir_instr_type_jump)
1248 handle_jump(after->block);
1249 }
1250
1251 void
1252 nir_instr_insert_before_block(nir_block *block, nir_instr *before)
1253 {
1254 if (before->type == nir_instr_type_jump)
1255 assert(exec_list_is_empty(&block->instr_list));
1256
1257 before->block = block;
1258 add_defs_uses(before);
1259 exec_list_push_head(&block->instr_list, &before->node);
1260
1261 if (before->type == nir_instr_type_jump)
1262 handle_jump(block);
1263 }
1264
1265 void
1266 nir_instr_insert_after_block(nir_block *block, nir_instr *after)
1267 {
1268 if (after->type == nir_instr_type_jump) {
1269 assert(exec_list_is_empty(&block->instr_list) ||
1270 nir_block_last_instr(block)->type != nir_instr_type_jump);
1271 }
1272
1273 after->block = block;
1274 add_defs_uses(after);
1275 exec_list_push_tail(&block->instr_list, &after->node);
1276
1277 if (after->type == nir_instr_type_jump)
1278 handle_jump(block);
1279 }
1280
1281 void
1282 nir_instr_insert_before_cf(nir_cf_node *node, nir_instr *before)
1283 {
1284 if (node->type == nir_cf_node_block) {
1285 nir_instr_insert_before_block(nir_cf_node_as_block(node), before);
1286 } else {
1287 nir_cf_node *prev = nir_cf_node_prev(node);
1288 assert(prev->type == nir_cf_node_block);
1289 nir_block *prev_block = nir_cf_node_as_block(prev);
1290
1291 nir_instr_insert_before_block(prev_block, before);
1292 }
1293 }
1294
1295 void
1296 nir_instr_insert_after_cf(nir_cf_node *node, nir_instr *after)
1297 {
1298 if (node->type == nir_cf_node_block) {
1299 nir_instr_insert_after_block(nir_cf_node_as_block(node), after);
1300 } else {
1301 nir_cf_node *next = nir_cf_node_next(node);
1302 assert(next->type == nir_cf_node_block);
1303 nir_block *next_block = nir_cf_node_as_block(next);
1304
1305 nir_instr_insert_before_block(next_block, after);
1306 }
1307 }
1308
1309 void
1310 nir_instr_insert_before_cf_list(struct exec_list *list, nir_instr *before)
1311 {
1312 nir_cf_node *first_node = exec_node_data(nir_cf_node,
1313 exec_list_get_head(list), node);
1314 nir_instr_insert_before_cf(first_node, before);
1315 }
1316
1317 void
1318 nir_instr_insert_after_cf_list(struct exec_list *list, nir_instr *after)
1319 {
1320 nir_cf_node *last_node = exec_node_data(nir_cf_node,
1321 exec_list_get_tail(list), node);
1322 nir_instr_insert_after_cf(last_node, after);
1323 }
1324
1325 static bool
1326 remove_use_cb(nir_src *src, void *state)
1327 {
1328 nir_instr *instr = (nir_instr *) state;
1329
1330 struct set *uses_set = src->is_ssa ? src->ssa->uses : src->reg.reg->uses;
1331
1332 struct set_entry *entry = _mesa_set_search(uses_set, instr);
1333 if (entry)
1334 _mesa_set_remove(uses_set, entry);
1335
1336 return true;
1337 }
1338
1339 static bool
1340 remove_def_cb(nir_dest *dest, void *state)
1341 {
1342 nir_instr *instr = (nir_instr *) state;
1343
1344 if (dest->is_ssa)
1345 return true;
1346
1347 nir_register *reg = dest->reg.reg;
1348
1349 struct set_entry *entry = _mesa_set_search(reg->defs, instr);
1350 if (entry)
1351 _mesa_set_remove(reg->defs, entry);
1352
1353 return true;
1354 }
1355
1356 static void
1357 remove_defs_uses(nir_instr *instr)
1358 {
1359 nir_foreach_dest(instr, remove_def_cb, instr);
1360 nir_foreach_src(instr, remove_use_cb, instr);
1361 }
1362
1363 void nir_instr_remove(nir_instr *instr)
1364 {
1365 remove_defs_uses(instr);
1366 exec_node_remove(&instr->node);
1367
1368 if (instr->type == nir_instr_type_jump) {
1369 nir_jump_instr *jump_instr = nir_instr_as_jump(instr);
1370 handle_remove_jump(instr->block, jump_instr->type);
1371 }
1372 }
1373
1374 /*@}*/
1375
1376 void
1377 nir_index_local_regs(nir_function_impl *impl)
1378 {
1379 unsigned index = 0;
1380 foreach_list_typed(nir_register, reg, node, &impl->registers) {
1381 reg->index = index++;
1382 }
1383 impl->reg_alloc = index;
1384 }
1385
1386 void
1387 nir_index_global_regs(nir_shader *shader)
1388 {
1389 unsigned index = 0;
1390 foreach_list_typed(nir_register, reg, node, &shader->registers) {
1391 reg->index = index++;
1392 }
1393 shader->reg_alloc = index;
1394 }
1395
1396 static bool
1397 visit_alu_dest(nir_alu_instr *instr, nir_foreach_dest_cb cb, void *state)
1398 {
1399 return cb(&instr->dest.dest, state);
1400 }
1401
1402 static bool
1403 visit_intrinsic_dest(nir_intrinsic_instr *instr, nir_foreach_dest_cb cb,
1404 void *state)
1405 {
1406 if (nir_intrinsic_infos[instr->intrinsic].has_dest)
1407 return cb(&instr->dest, state);
1408
1409 return true;
1410 }
1411
1412 static bool
1413 visit_texture_dest(nir_tex_instr *instr, nir_foreach_dest_cb cb,
1414 void *state)
1415 {
1416 return cb(&instr->dest, state);
1417 }
1418
1419 static bool
1420 visit_phi_dest(nir_phi_instr *instr, nir_foreach_dest_cb cb, void *state)
1421 {
1422 return cb(&instr->dest, state);
1423 }
1424
1425 static bool
1426 visit_parallel_copy_dest(nir_parallel_copy_instr *instr,
1427 nir_foreach_dest_cb cb, void *state)
1428 {
1429 nir_foreach_parallel_copy_entry(instr, entry) {
1430 if (!cb(&entry->dest, state))
1431 return false;
1432 }
1433
1434 return true;
1435 }
1436
1437 bool
1438 nir_foreach_dest(nir_instr *instr, nir_foreach_dest_cb cb, void *state)
1439 {
1440 switch (instr->type) {
1441 case nir_instr_type_alu:
1442 return visit_alu_dest(nir_instr_as_alu(instr), cb, state);
1443 case nir_instr_type_intrinsic:
1444 return visit_intrinsic_dest(nir_instr_as_intrinsic(instr), cb, state);
1445 case nir_instr_type_tex:
1446 return visit_texture_dest(nir_instr_as_tex(instr), cb, state);
1447 case nir_instr_type_phi:
1448 return visit_phi_dest(nir_instr_as_phi(instr), cb, state);
1449 case nir_instr_type_parallel_copy:
1450 return visit_parallel_copy_dest(nir_instr_as_parallel_copy(instr),
1451 cb, state);
1452
1453 case nir_instr_type_load_const:
1454 case nir_instr_type_ssa_undef:
1455 case nir_instr_type_call:
1456 case nir_instr_type_jump:
1457 break;
1458
1459 default:
1460 unreachable("Invalid instruction type");
1461 break;
1462 }
1463
1464 return true;
1465 }
1466
1467 struct foreach_ssa_def_state {
1468 nir_foreach_ssa_def_cb cb;
1469 void *client_state;
1470 };
1471
1472 static inline bool
1473 nir_ssa_def_visitor(nir_dest *dest, void *void_state)
1474 {
1475 struct foreach_ssa_def_state *state = void_state;
1476
1477 if (dest->is_ssa)
1478 return state->cb(&dest->ssa, state->client_state);
1479 else
1480 return true;
1481 }
1482
1483 bool
1484 nir_foreach_ssa_def(nir_instr *instr, nir_foreach_ssa_def_cb cb, void *state)
1485 {
1486 switch (instr->type) {
1487 case nir_instr_type_alu:
1488 case nir_instr_type_tex:
1489 case nir_instr_type_intrinsic:
1490 case nir_instr_type_phi:
1491 case nir_instr_type_parallel_copy: {
1492 struct foreach_ssa_def_state foreach_state = {cb, state};
1493 return nir_foreach_dest(instr, nir_ssa_def_visitor, &foreach_state);
1494 }
1495
1496 case nir_instr_type_load_const:
1497 return cb(&nir_instr_as_load_const(instr)->def, state);
1498 case nir_instr_type_ssa_undef:
1499 return cb(&nir_instr_as_ssa_undef(instr)->def, state);
1500 case nir_instr_type_call:
1501 case nir_instr_type_jump:
1502 return true;
1503 default:
1504 unreachable("Invalid instruction type");
1505 }
1506 }
1507
1508 static bool
1509 visit_src(nir_src *src, nir_foreach_src_cb cb, void *state)
1510 {
1511 if (!cb(src, state))
1512 return false;
1513 if (!src->is_ssa && src->reg.indirect)
1514 return cb(src->reg.indirect, state);
1515 return true;
1516 }
1517
1518 static bool
1519 visit_deref_array_src(nir_deref_array *deref, nir_foreach_src_cb cb,
1520 void *state)
1521 {
1522 if (deref->deref_array_type == nir_deref_array_type_indirect)
1523 return visit_src(&deref->indirect, cb, state);
1524 return true;
1525 }
1526
1527 static bool
1528 visit_deref_src(nir_deref_var *deref, nir_foreach_src_cb cb, void *state)
1529 {
1530 nir_deref *cur = &deref->deref;
1531 while (cur != NULL) {
1532 if (cur->deref_type == nir_deref_type_array)
1533 if (!visit_deref_array_src(nir_deref_as_array(cur), cb, state))
1534 return false;
1535
1536 cur = cur->child;
1537 }
1538
1539 return true;
1540 }
1541
1542 static bool
1543 visit_alu_src(nir_alu_instr *instr, nir_foreach_src_cb cb, void *state)
1544 {
1545 for (unsigned i = 0; i < nir_op_infos[instr->op].num_inputs; i++)
1546 if (!visit_src(&instr->src[i].src, cb, state))
1547 return false;
1548
1549 return true;
1550 }
1551
1552 static bool
1553 visit_tex_src(nir_tex_instr *instr, nir_foreach_src_cb cb, void *state)
1554 {
1555 for (unsigned i = 0; i < instr->num_srcs; i++)
1556 if (!visit_src(&instr->src[i].src, cb, state))
1557 return false;
1558
1559 if (instr->sampler != NULL)
1560 if (!visit_deref_src(instr->sampler, cb, state))
1561 return false;
1562
1563 return true;
1564 }
1565
1566 static bool
1567 visit_intrinsic_src(nir_intrinsic_instr *instr, nir_foreach_src_cb cb,
1568 void *state)
1569 {
1570 unsigned num_srcs = nir_intrinsic_infos[instr->intrinsic].num_srcs;
1571 for (unsigned i = 0; i < num_srcs; i++)
1572 if (!visit_src(&instr->src[i], cb, state))
1573 return false;
1574
1575 unsigned num_vars =
1576 nir_intrinsic_infos[instr->intrinsic].num_variables;
1577 for (unsigned i = 0; i < num_vars; i++)
1578 if (!visit_deref_src(instr->variables[i], cb, state))
1579 return false;
1580
1581 return true;
1582 }
1583
1584 static bool
1585 visit_call_src(nir_call_instr *instr, nir_foreach_src_cb cb, void *state)
1586 {
1587 return true;
1588 }
1589
1590 static bool
1591 visit_load_const_src(nir_load_const_instr *instr, nir_foreach_src_cb cb,
1592 void *state)
1593 {
1594 return true;
1595 }
1596
1597 static bool
1598 visit_phi_src(nir_phi_instr *instr, nir_foreach_src_cb cb, void *state)
1599 {
1600 nir_foreach_phi_src(instr, src) {
1601 if (!visit_src(&src->src, cb, state))
1602 return false;
1603 }
1604
1605 return true;
1606 }
1607
1608 static bool
1609 visit_parallel_copy_src(nir_parallel_copy_instr *instr,
1610 nir_foreach_src_cb cb, void *state)
1611 {
1612 nir_foreach_parallel_copy_entry(instr, entry) {
1613 if (!visit_src(&entry->src, cb, state))
1614 return false;
1615 }
1616
1617 return true;
1618 }
1619
1620 typedef struct {
1621 void *state;
1622 nir_foreach_src_cb cb;
1623 } visit_dest_indirect_state;
1624
1625 static bool
1626 visit_dest_indirect(nir_dest *dest, void *_state)
1627 {
1628 visit_dest_indirect_state *state = (visit_dest_indirect_state *) _state;
1629
1630 if (!dest->is_ssa && dest->reg.indirect)
1631 return state->cb(dest->reg.indirect, state->state);
1632
1633 return true;
1634 }
1635
1636 bool
1637 nir_foreach_src(nir_instr *instr, nir_foreach_src_cb cb, void *state)
1638 {
1639 switch (instr->type) {
1640 case nir_instr_type_alu:
1641 if (!visit_alu_src(nir_instr_as_alu(instr), cb, state))
1642 return false;
1643 break;
1644 case nir_instr_type_intrinsic:
1645 if (!visit_intrinsic_src(nir_instr_as_intrinsic(instr), cb, state))
1646 return false;
1647 break;
1648 case nir_instr_type_tex:
1649 if (!visit_tex_src(nir_instr_as_tex(instr), cb, state))
1650 return false;
1651 break;
1652 case nir_instr_type_call:
1653 if (!visit_call_src(nir_instr_as_call(instr), cb, state))
1654 return false;
1655 break;
1656 case nir_instr_type_load_const:
1657 if (!visit_load_const_src(nir_instr_as_load_const(instr), cb, state))
1658 return false;
1659 break;
1660 case nir_instr_type_phi:
1661 if (!visit_phi_src(nir_instr_as_phi(instr), cb, state))
1662 return false;
1663 break;
1664 case nir_instr_type_parallel_copy:
1665 if (!visit_parallel_copy_src(nir_instr_as_parallel_copy(instr),
1666 cb, state))
1667 return false;
1668 break;
1669 case nir_instr_type_jump:
1670 case nir_instr_type_ssa_undef:
1671 return true;
1672
1673 default:
1674 unreachable("Invalid instruction type");
1675 break;
1676 }
1677
1678 visit_dest_indirect_state dest_state;
1679 dest_state.state = state;
1680 dest_state.cb = cb;
1681 return nir_foreach_dest(instr, visit_dest_indirect, &dest_state);
1682 }
1683
1684 nir_const_value *
1685 nir_src_as_const_value(nir_src src)
1686 {
1687 if (!src.is_ssa)
1688 return NULL;
1689
1690 if (src.ssa->parent_instr->type != nir_instr_type_load_const)
1691 return NULL;
1692
1693 nir_load_const_instr *load = nir_instr_as_load_const(src.ssa->parent_instr);
1694
1695 return &load->value;
1696 }
1697
1698 bool
1699 nir_srcs_equal(nir_src src1, nir_src src2)
1700 {
1701 if (src1.is_ssa) {
1702 if (src2.is_ssa) {
1703 return src1.ssa == src2.ssa;
1704 } else {
1705 return false;
1706 }
1707 } else {
1708 if (src2.is_ssa) {
1709 return false;
1710 } else {
1711 if ((src1.reg.indirect == NULL) != (src2.reg.indirect == NULL))
1712 return false;
1713
1714 if (src1.reg.indirect) {
1715 if (!nir_srcs_equal(*src1.reg.indirect, *src2.reg.indirect))
1716 return false;
1717 }
1718
1719 return src1.reg.reg == src2.reg.reg &&
1720 src1.reg.base_offset == src2.reg.base_offset;
1721 }
1722 }
1723 }
1724
1725 static bool
1726 src_does_not_use_def(nir_src *src, void *void_def)
1727 {
1728 nir_ssa_def *def = void_def;
1729
1730 if (src->is_ssa) {
1731 return src->ssa != def;
1732 } else {
1733 return true;
1734 }
1735 }
1736
1737 static bool
1738 src_does_not_use_reg(nir_src *src, void *void_reg)
1739 {
1740 nir_register *reg = void_reg;
1741
1742 if (src->is_ssa) {
1743 return true;
1744 } else {
1745 return src->reg.reg != reg;
1746 }
1747 }
1748
1749 void
1750 nir_instr_rewrite_src(nir_instr *instr, nir_src *src, nir_src new_src)
1751 {
1752 if (src->is_ssa) {
1753 nir_ssa_def *old_ssa = src->ssa;
1754 *src = new_src;
1755 if (old_ssa && nir_foreach_src(instr, src_does_not_use_def, old_ssa)) {
1756 struct set_entry *entry = _mesa_set_search(old_ssa->uses, instr);
1757 assert(entry);
1758 _mesa_set_remove(old_ssa->uses, entry);
1759 }
1760 } else {
1761 if (src->reg.indirect)
1762 nir_instr_rewrite_src(instr, src->reg.indirect, new_src);
1763
1764 nir_register *old_reg = src->reg.reg;
1765 *src = new_src;
1766 if (old_reg && nir_foreach_src(instr, src_does_not_use_reg, old_reg)) {
1767 struct set_entry *entry = _mesa_set_search(old_reg->uses, instr);
1768 assert(entry);
1769 _mesa_set_remove(old_reg->uses, entry);
1770 }
1771 }
1772
1773 if (new_src.is_ssa) {
1774 if (new_src.ssa)
1775 _mesa_set_add(new_src.ssa->uses, instr);
1776 } else {
1777 if (new_src.reg.reg)
1778 _mesa_set_add(new_src.reg.reg->uses, instr);
1779 }
1780 }
1781
1782 void
1783 nir_ssa_def_init(nir_instr *instr, nir_ssa_def *def,
1784 unsigned num_components, const char *name)
1785 {
1786 void *mem_ctx = ralloc_parent(instr);
1787
1788 def->name = name;
1789 def->parent_instr = instr;
1790 def->uses = _mesa_set_create(mem_ctx, _mesa_hash_pointer,
1791 _mesa_key_pointer_equal);
1792 def->if_uses = _mesa_set_create(mem_ctx, _mesa_hash_pointer,
1793 _mesa_key_pointer_equal);
1794 def->num_components = num_components;
1795
1796 if (instr->block) {
1797 nir_function_impl *impl =
1798 nir_cf_node_get_function(&instr->block->cf_node);
1799
1800 def->index = impl->ssa_alloc++;
1801 } else {
1802 def->index = UINT_MAX;
1803 }
1804 }
1805
1806 void
1807 nir_ssa_dest_init(nir_instr *instr, nir_dest *dest,
1808 unsigned num_components, const char *name)
1809 {
1810 dest->is_ssa = true;
1811 nir_ssa_def_init(instr, &dest->ssa, num_components, name);
1812 }
1813
1814 struct ssa_def_rewrite_state {
1815 void *mem_ctx;
1816 nir_ssa_def *old;
1817 nir_src new_src;
1818 };
1819
1820 static bool
1821 ssa_def_rewrite_uses_src(nir_src *src, void *void_state)
1822 {
1823 struct ssa_def_rewrite_state *state = void_state;
1824
1825 if (src->is_ssa && src->ssa == state->old)
1826 nir_src_copy(src, &state->new_src, state->mem_ctx);
1827
1828 return true;
1829 }
1830
1831 void
1832 nir_ssa_def_rewrite_uses(nir_ssa_def *def, nir_src new_src, void *mem_ctx)
1833 {
1834 struct ssa_def_rewrite_state state;
1835 state.mem_ctx = mem_ctx;
1836 state.old = def;
1837 state.new_src = new_src;
1838
1839 assert(!new_src.is_ssa || def != new_src.ssa);
1840
1841 struct set *new_uses, *new_if_uses;
1842 if (new_src.is_ssa) {
1843 new_uses = new_src.ssa->uses;
1844 new_if_uses = new_src.ssa->if_uses;
1845 } else {
1846 new_uses = new_src.reg.reg->uses;
1847 new_if_uses = new_src.reg.reg->if_uses;
1848 }
1849
1850 struct set_entry *entry;
1851 set_foreach(def->uses, entry) {
1852 nir_instr *instr = (nir_instr *)entry->key;
1853
1854 _mesa_set_remove(def->uses, entry);
1855 nir_foreach_src(instr, ssa_def_rewrite_uses_src, &state);
1856 _mesa_set_add(new_uses, instr);
1857 }
1858
1859 set_foreach(def->if_uses, entry) {
1860 nir_if *if_use = (nir_if *)entry->key;
1861
1862 _mesa_set_remove(def->if_uses, entry);
1863 nir_src_copy(&if_use->condition, &new_src, mem_ctx);
1864 _mesa_set_add(new_if_uses, if_use);
1865 }
1866 }
1867
1868
1869 static bool foreach_cf_node(nir_cf_node *node, nir_foreach_block_cb cb,
1870 bool reverse, void *state);
1871
1872 static inline bool
1873 foreach_if(nir_if *if_stmt, nir_foreach_block_cb cb, bool reverse, void *state)
1874 {
1875 if (reverse) {
1876 foreach_list_typed_safe_reverse(nir_cf_node, node, node,
1877 &if_stmt->else_list) {
1878 if (!foreach_cf_node(node, cb, reverse, state))
1879 return false;
1880 }
1881
1882 foreach_list_typed_safe_reverse(nir_cf_node, node, node,
1883 &if_stmt->then_list) {
1884 if (!foreach_cf_node(node, cb, reverse, state))
1885 return false;
1886 }
1887 } else {
1888 foreach_list_typed_safe(nir_cf_node, node, node, &if_stmt->then_list) {
1889 if (!foreach_cf_node(node, cb, reverse, state))
1890 return false;
1891 }
1892
1893 foreach_list_typed_safe(nir_cf_node, node, node, &if_stmt->else_list) {
1894 if (!foreach_cf_node(node, cb, reverse, state))
1895 return false;
1896 }
1897 }
1898
1899 return true;
1900 }
1901
1902 static inline bool
1903 foreach_loop(nir_loop *loop, nir_foreach_block_cb cb, bool reverse, void *state)
1904 {
1905 if (reverse) {
1906 foreach_list_typed_safe_reverse(nir_cf_node, node, node, &loop->body) {
1907 if (!foreach_cf_node(node, cb, reverse, state))
1908 return false;
1909 }
1910 } else {
1911 foreach_list_typed_safe(nir_cf_node, node, node, &loop->body) {
1912 if (!foreach_cf_node(node, cb, reverse, state))
1913 return false;
1914 }
1915 }
1916
1917 return true;
1918 }
1919
1920 static bool
1921 foreach_cf_node(nir_cf_node *node, nir_foreach_block_cb cb,
1922 bool reverse, void *state)
1923 {
1924 switch (node->type) {
1925 case nir_cf_node_block:
1926 return cb(nir_cf_node_as_block(node), state);
1927 case nir_cf_node_if:
1928 return foreach_if(nir_cf_node_as_if(node), cb, reverse, state);
1929 case nir_cf_node_loop:
1930 return foreach_loop(nir_cf_node_as_loop(node), cb, reverse, state);
1931 break;
1932
1933 default:
1934 unreachable("Invalid CFG node type");
1935 break;
1936 }
1937
1938 return false;
1939 }
1940
1941 bool
1942 nir_foreach_block(nir_function_impl *impl, nir_foreach_block_cb cb, void *state)
1943 {
1944 foreach_list_typed_safe(nir_cf_node, node, node, &impl->body) {
1945 if (!foreach_cf_node(node, cb, false, state))
1946 return false;
1947 }
1948
1949 return cb(impl->end_block, state);
1950 }
1951
1952 bool
1953 nir_foreach_block_reverse(nir_function_impl *impl, nir_foreach_block_cb cb,
1954 void *state)
1955 {
1956 if (!cb(impl->end_block, state))
1957 return false;
1958
1959 foreach_list_typed_safe_reverse(nir_cf_node, node, node, &impl->body) {
1960 if (!foreach_cf_node(node, cb, true, state))
1961 return false;
1962 }
1963
1964 return true;
1965 }
1966
1967 nir_if *
1968 nir_block_get_following_if(nir_block *block)
1969 {
1970 if (exec_node_is_tail_sentinel(&block->cf_node.node))
1971 return NULL;
1972
1973 if (nir_cf_node_is_last(&block->cf_node))
1974 return NULL;
1975
1976 nir_cf_node *next_node = nir_cf_node_next(&block->cf_node);
1977
1978 if (next_node->type != nir_cf_node_if)
1979 return NULL;
1980
1981 return nir_cf_node_as_if(next_node);
1982 }
1983
1984 static bool
1985 index_block(nir_block *block, void *state)
1986 {
1987 unsigned *index = (unsigned *) state;
1988 block->index = (*index)++;
1989 return true;
1990 }
1991
1992 void
1993 nir_index_blocks(nir_function_impl *impl)
1994 {
1995 unsigned index = 0;
1996
1997 if (impl->valid_metadata & nir_metadata_block_index)
1998 return;
1999
2000 nir_foreach_block(impl, index_block, &index);
2001
2002 impl->num_blocks = index;
2003 }
2004
2005 static void
2006 index_ssa_def(nir_ssa_def *def, unsigned *index)
2007 {
2008 def->index = (*index)++;
2009 }
2010
2011 static bool
2012 index_ssa_def_cb(nir_dest *dest, void *state)
2013 {
2014 unsigned *index = (unsigned *) state;
2015 if (dest->is_ssa)
2016 index_ssa_def(&dest->ssa, index);
2017 return true;
2018 }
2019
2020 static void
2021 index_ssa_undef(nir_ssa_undef_instr *instr, unsigned *index)
2022 {
2023 index_ssa_def(&instr->def, index);
2024 }
2025
2026 static bool
2027 index_ssa_block(nir_block *block, void *state)
2028 {
2029 unsigned *index = (unsigned *) state;
2030
2031 nir_foreach_instr(block, instr) {
2032 if (instr->type == nir_instr_type_ssa_undef)
2033 index_ssa_undef(nir_instr_as_ssa_undef(instr), index);
2034 else
2035 nir_foreach_dest(instr, index_ssa_def_cb, state);
2036 }
2037
2038 return true;
2039 }
2040
2041 void
2042 nir_index_ssa_defs(nir_function_impl *impl)
2043 {
2044 unsigned index = 0;
2045 nir_foreach_block(impl, index_ssa_block, &index);
2046 impl->ssa_alloc = index;
2047 }