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