nir/dead_cf: delete code that's unreachable due to jumps
[mesa.git] / src / glsl / nir / nir_from_ssa.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 * Jason Ekstrand (jason@jlekstrand.net)
25 *
26 */
27
28 #include "nir.h"
29 #include "nir_vla.h"
30
31 /*
32 * This file implements an out-of-SSA pass as described in "Revisiting
33 * Out-of-SSA Translation for Correctness, Code Quality, and Efficiency" by
34 * Boissinot et. al.
35 */
36
37 struct from_ssa_state {
38 void *mem_ctx;
39 void *dead_ctx;
40 bool phi_webs_only;
41 struct hash_table *merge_node_table;
42 nir_instr *instr;
43 nir_function_impl *impl;
44 };
45
46 /* Returns true if a dominates b */
47 static bool
48 ssa_def_dominates(nir_ssa_def *a, nir_ssa_def *b)
49 {
50 if (a->live_index == 0) {
51 /* SSA undefs always dominate */
52 return true;
53 } else if (b->live_index < a->live_index) {
54 return false;
55 } else if (a->parent_instr->block == b->parent_instr->block) {
56 return a->live_index <= b->live_index;
57 } else {
58 return nir_block_dominates(a->parent_instr->block,
59 b->parent_instr->block);
60 }
61 }
62
63
64 /* The following data structure, which I have named merge_set is a way of
65 * representing a set registers of non-interfering registers. This is
66 * based on the concept of a "dominence forest" presented in "Fast Copy
67 * Coalescing and Live-Range Identification" by Budimlic et. al. but the
68 * implementation concept is taken from "Revisiting Out-of-SSA Translation
69 * for Correctness, Code Quality, and Efficiency" by Boissinot et. al..
70 *
71 * Each SSA definition is associated with a merge_node and the association
72 * is represented by a combination of a hash table and the "def" parameter
73 * in the merge_node structure. The merge_set stores a linked list of
74 * merge_node's in dominence order of the ssa definitions. (Since the
75 * liveness analysis pass indexes the SSA values in dominence order for us,
76 * this is an easy thing to keep up.) It is assumed that no pair of the
77 * nodes in a given set interfere. Merging two sets or checking for
78 * interference can be done in a single linear-time merge-sort walk of the
79 * two lists of nodes.
80 */
81 struct merge_set;
82
83 typedef struct {
84 struct exec_node node;
85 struct merge_set *set;
86 nir_ssa_def *def;
87 } merge_node;
88
89 typedef struct merge_set {
90 struct exec_list nodes;
91 unsigned size;
92 nir_register *reg;
93 } merge_set;
94
95 #if 0
96 static void
97 merge_set_dump(merge_set *set, FILE *fp)
98 {
99 nir_ssa_def *dom[set->size];
100 int dom_idx = -1;
101
102 foreach_list_typed(merge_node, node, node, &set->nodes) {
103 while (dom_idx >= 0 && !ssa_def_dominates(dom[dom_idx], node->def))
104 dom_idx--;
105
106 for (int i = 0; i <= dom_idx; i++)
107 fprintf(fp, " ");
108
109 if (node->def->name)
110 fprintf(fp, "ssa_%d /* %s */\n", node->def->index, node->def->name);
111 else
112 fprintf(fp, "ssa_%d\n", node->def->index);
113
114 dom[++dom_idx] = node->def;
115 }
116 }
117 #endif
118
119 static merge_node *
120 get_merge_node(nir_ssa_def *def, struct from_ssa_state *state)
121 {
122 struct hash_entry *entry =
123 _mesa_hash_table_search(state->merge_node_table, def);
124 if (entry)
125 return entry->data;
126
127 merge_set *set = ralloc(state->dead_ctx, merge_set);
128 exec_list_make_empty(&set->nodes);
129 set->size = 1;
130 set->reg = NULL;
131
132 merge_node *node = ralloc(state->dead_ctx, merge_node);
133 node->set = set;
134 node->def = def;
135 exec_list_push_head(&set->nodes, &node->node);
136
137 _mesa_hash_table_insert(state->merge_node_table, def, node);
138
139 return node;
140 }
141
142 static bool
143 merge_nodes_interfere(merge_node *a, merge_node *b)
144 {
145 return nir_ssa_defs_interfere(a->def, b->def);
146 }
147
148 /* Merges b into a */
149 static merge_set *
150 merge_merge_sets(merge_set *a, merge_set *b)
151 {
152 struct exec_node *an = exec_list_get_head(&a->nodes);
153 struct exec_node *bn = exec_list_get_head(&b->nodes);
154 while (!exec_node_is_tail_sentinel(bn)) {
155 merge_node *a_node = exec_node_data(merge_node, an, node);
156 merge_node *b_node = exec_node_data(merge_node, bn, node);
157
158 if (exec_node_is_tail_sentinel(an) ||
159 a_node->def->live_index > b_node->def->live_index) {
160 struct exec_node *next = bn->next;
161 exec_node_remove(bn);
162 exec_node_insert_node_before(an, bn);
163 exec_node_data(merge_node, bn, node)->set = a;
164 bn = next;
165 } else {
166 an = an->next;
167 }
168 }
169
170 a->size += b->size;
171 b->size = 0;
172
173 return a;
174 }
175
176 /* Checks for any interference between two merge sets
177 *
178 * This is an implementation of Algorithm 2 in "Revisiting Out-of-SSA
179 * Translation for Correctness, Code Quality, and Efficiency" by
180 * Boissinot et. al.
181 */
182 static bool
183 merge_sets_interfere(merge_set *a, merge_set *b)
184 {
185 NIR_VLA(merge_node *, dom, a->size + b->size);
186 int dom_idx = -1;
187
188 struct exec_node *an = exec_list_get_head(&a->nodes);
189 struct exec_node *bn = exec_list_get_head(&b->nodes);
190 while (!exec_node_is_tail_sentinel(an) ||
191 !exec_node_is_tail_sentinel(bn)) {
192
193 merge_node *current;
194 if (exec_node_is_tail_sentinel(an)) {
195 current = exec_node_data(merge_node, bn, node);
196 bn = bn->next;
197 } else if (exec_node_is_tail_sentinel(bn)) {
198 current = exec_node_data(merge_node, an, node);
199 an = an->next;
200 } else {
201 merge_node *a_node = exec_node_data(merge_node, an, node);
202 merge_node *b_node = exec_node_data(merge_node, bn, node);
203
204 if (a_node->def->live_index <= b_node->def->live_index) {
205 current = a_node;
206 an = an->next;
207 } else {
208 current = b_node;
209 bn = bn->next;
210 }
211 }
212
213 while (dom_idx >= 0 &&
214 !ssa_def_dominates(dom[dom_idx]->def, current->def))
215 dom_idx--;
216
217 if (dom_idx >= 0 && merge_nodes_interfere(current, dom[dom_idx]))
218 return true;
219
220 dom[++dom_idx] = current;
221 }
222
223 return false;
224 }
225
226 static bool
227 add_parallel_copy_to_end_of_block(nir_block *block, void *void_state)
228 {
229 struct from_ssa_state *state = void_state;
230
231 bool need_end_copy = false;
232 if (block->successors[0]) {
233 nir_instr *instr = nir_block_first_instr(block->successors[0]);
234 if (instr && instr->type == nir_instr_type_phi)
235 need_end_copy = true;
236 }
237
238 if (block->successors[1]) {
239 nir_instr *instr = nir_block_first_instr(block->successors[1]);
240 if (instr && instr->type == nir_instr_type_phi)
241 need_end_copy = true;
242 }
243
244 if (need_end_copy) {
245 /* If one of our successors has at least one phi node, we need to
246 * create a parallel copy at the end of the block but before the jump
247 * (if there is one).
248 */
249 nir_parallel_copy_instr *pcopy =
250 nir_parallel_copy_instr_create(state->dead_ctx);
251
252 nir_instr *last_instr = nir_block_last_instr(block);
253 if (last_instr && last_instr->type == nir_instr_type_jump) {
254 nir_instr_insert_before(last_instr, &pcopy->instr);
255 } else {
256 nir_instr_insert_after_block(block, &pcopy->instr);
257 }
258 }
259
260 return true;
261 }
262
263 static nir_parallel_copy_instr *
264 get_parallel_copy_at_end_of_block(nir_block *block)
265 {
266 nir_instr *last_instr = nir_block_last_instr(block);
267 if (last_instr == NULL)
268 return NULL;
269
270 /* The last instruction may be a jump in which case the parallel copy is
271 * right before it.
272 */
273 if (last_instr->type == nir_instr_type_jump)
274 last_instr = nir_instr_prev(last_instr);
275
276 if (last_instr && last_instr->type == nir_instr_type_parallel_copy)
277 return nir_instr_as_parallel_copy(last_instr);
278 else
279 return NULL;
280 }
281
282 /** Isolate phi nodes with parallel copies
283 *
284 * In order to solve the dependency problems with the sources and
285 * destinations of phi nodes, we first isolate them by adding parallel
286 * copies to the beginnings and ends of basic blocks. For every block with
287 * phi nodes, we add a parallel copy immediately following the last phi
288 * node that copies the destinations of all of the phi nodes to new SSA
289 * values. We also add a parallel copy to the end of every block that has
290 * a successor with phi nodes that, for each phi node in each successor,
291 * copies the corresponding sorce of the phi node and adjust the phi to
292 * used the destination of the parallel copy.
293 *
294 * In SSA form, each value has exactly one definition. What this does is
295 * ensure that each value used in a phi also has exactly one use. The
296 * destinations of phis are only used by the parallel copy immediately
297 * following the phi nodes and. Thanks to the parallel copy at the end of
298 * the predecessor block, the sources of phi nodes are are the only use of
299 * that value. This allows us to immediately assign all the sources and
300 * destinations of any given phi node to the same register without worrying
301 * about interference at all. We do coalescing to get rid of the parallel
302 * copies where possible.
303 *
304 * Before this pass can be run, we have to iterate over the blocks with
305 * add_parallel_copy_to_end_of_block to ensure that the parallel copies at
306 * the ends of blocks exist. We can create the ones at the beginnings as
307 * we go, but the ones at the ends of blocks need to be created ahead of
308 * time because of potential back-edges in the CFG.
309 */
310 static bool
311 isolate_phi_nodes_block(nir_block *block, void *void_state)
312 {
313 struct from_ssa_state *state = void_state;
314
315 nir_instr *last_phi_instr = NULL;
316 nir_foreach_instr(block, instr) {
317 /* Phi nodes only ever come at the start of a block */
318 if (instr->type != nir_instr_type_phi)
319 break;
320
321 last_phi_instr = instr;
322 }
323
324 /* If we don't have any phi's, then there's nothing for us to do. */
325 if (last_phi_instr == NULL)
326 return true;
327
328 /* If we have phi nodes, we need to create a parallel copy at the
329 * start of this block but after the phi nodes.
330 */
331 nir_parallel_copy_instr *block_pcopy =
332 nir_parallel_copy_instr_create(state->dead_ctx);
333 nir_instr_insert_after(last_phi_instr, &block_pcopy->instr);
334
335 nir_foreach_instr(block, instr) {
336 /* Phi nodes only ever come at the start of a block */
337 if (instr->type != nir_instr_type_phi)
338 break;
339
340 nir_phi_instr *phi = nir_instr_as_phi(instr);
341 assert(phi->dest.is_ssa);
342 nir_foreach_phi_src(phi, src) {
343 nir_parallel_copy_instr *pcopy =
344 get_parallel_copy_at_end_of_block(src->pred);
345 assert(pcopy);
346
347 nir_parallel_copy_entry *entry = rzalloc(state->dead_ctx,
348 nir_parallel_copy_entry);
349 nir_ssa_dest_init(&pcopy->instr, &entry->dest,
350 phi->dest.ssa.num_components, src->src.ssa->name);
351 exec_list_push_tail(&pcopy->entries, &entry->node);
352
353 assert(src->src.is_ssa);
354 nir_instr_rewrite_src(&pcopy->instr, &entry->src, src->src);
355
356 nir_instr_rewrite_src(&phi->instr, &src->src,
357 nir_src_for_ssa(&entry->dest.ssa));
358 }
359
360 nir_parallel_copy_entry *entry = rzalloc(state->dead_ctx,
361 nir_parallel_copy_entry);
362 nir_ssa_dest_init(&block_pcopy->instr, &entry->dest,
363 phi->dest.ssa.num_components, phi->dest.ssa.name);
364 exec_list_push_tail(&block_pcopy->entries, &entry->node);
365
366 nir_ssa_def_rewrite_uses(&phi->dest.ssa,
367 nir_src_for_ssa(&entry->dest.ssa),
368 state->mem_ctx);
369
370 nir_instr_rewrite_src(&block_pcopy->instr, &entry->src,
371 nir_src_for_ssa(&phi->dest.ssa));
372 }
373
374 return true;
375 }
376
377 static bool
378 coalesce_phi_nodes_block(nir_block *block, void *void_state)
379 {
380 struct from_ssa_state *state = void_state;
381
382 nir_foreach_instr(block, instr) {
383 /* Phi nodes only ever come at the start of a block */
384 if (instr->type != nir_instr_type_phi)
385 break;
386
387 nir_phi_instr *phi = nir_instr_as_phi(instr);
388
389 assert(phi->dest.is_ssa);
390 merge_node *dest_node = get_merge_node(&phi->dest.ssa, state);
391
392 nir_foreach_phi_src(phi, src) {
393 assert(src->src.is_ssa);
394 merge_node *src_node = get_merge_node(src->src.ssa, state);
395 if (src_node->set != dest_node->set)
396 merge_merge_sets(dest_node->set, src_node->set);
397 }
398 }
399
400 return true;
401 }
402
403 static void
404 aggressive_coalesce_parallel_copy(nir_parallel_copy_instr *pcopy,
405 struct from_ssa_state *state)
406 {
407 nir_foreach_parallel_copy_entry(pcopy, entry) {
408 if (!entry->src.is_ssa)
409 continue;
410
411 /* Since load_const instructions are SSA only, we can't replace their
412 * destinations with registers and, therefore, can't coalesce them.
413 */
414 if (entry->src.ssa->parent_instr->type == nir_instr_type_load_const)
415 continue;
416
417 /* Don't try and coalesce these */
418 if (entry->dest.ssa.num_components != entry->src.ssa->num_components)
419 continue;
420
421 merge_node *src_node = get_merge_node(entry->src.ssa, state);
422 merge_node *dest_node = get_merge_node(&entry->dest.ssa, state);
423
424 if (src_node->set == dest_node->set)
425 continue;
426
427 if (!merge_sets_interfere(src_node->set, dest_node->set))
428 merge_merge_sets(src_node->set, dest_node->set);
429 }
430 }
431
432 static bool
433 aggressive_coalesce_block(nir_block *block, void *void_state)
434 {
435 struct from_ssa_state *state = void_state;
436
437 nir_parallel_copy_instr *start_pcopy = NULL;
438 nir_foreach_instr(block, instr) {
439 /* Phi nodes only ever come at the start of a block */
440 if (instr->type != nir_instr_type_phi) {
441 if (instr->type != nir_instr_type_parallel_copy)
442 break; /* The parallel copy must be right after the phis */
443
444 start_pcopy = nir_instr_as_parallel_copy(instr);
445
446 aggressive_coalesce_parallel_copy(start_pcopy, state);
447
448 break;
449 }
450 }
451
452 nir_parallel_copy_instr *end_pcopy =
453 get_parallel_copy_at_end_of_block(block);
454
455 if (end_pcopy && end_pcopy != start_pcopy)
456 aggressive_coalesce_parallel_copy(end_pcopy, state);
457
458 return true;
459 }
460
461 static bool
462 rewrite_ssa_def(nir_ssa_def *def, void *void_state)
463 {
464 struct from_ssa_state *state = void_state;
465 nir_register *reg;
466
467 struct hash_entry *entry =
468 _mesa_hash_table_search(state->merge_node_table, def);
469 if (entry) {
470 /* In this case, we're part of a phi web. Use the web's register. */
471 merge_node *node = (merge_node *)entry->data;
472
473 /* If it doesn't have a register yet, create one. Note that all of
474 * the things in the merge set should be the same so it doesn't
475 * matter which node's definition we use.
476 */
477 if (node->set->reg == NULL) {
478 node->set->reg = nir_local_reg_create(state->impl);
479 node->set->reg->name = def->name;
480 node->set->reg->num_components = def->num_components;
481 node->set->reg->num_array_elems = 0;
482 }
483
484 reg = node->set->reg;
485 } else {
486 if (state->phi_webs_only)
487 return true;
488
489 /* We leave load_const SSA values alone. They act as immediates to
490 * the backend. If it got coalesced into a phi, that's ok.
491 */
492 if (def->parent_instr->type == nir_instr_type_load_const)
493 return true;
494
495 reg = nir_local_reg_create(state->impl);
496 reg->name = def->name;
497 reg->num_components = def->num_components;
498 reg->num_array_elems = 0;
499 }
500
501 nir_ssa_def_rewrite_uses(def, nir_src_for_reg(reg), state->mem_ctx);
502 assert(list_empty(&def->uses) && list_empty(&def->if_uses));
503
504 if (def->parent_instr->type == nir_instr_type_ssa_undef) {
505 /* If it's an ssa_undef instruction, remove it since we know we just got
506 * rid of all its uses.
507 */
508 nir_instr *parent_instr = def->parent_instr;
509 nir_instr_remove(parent_instr);
510 ralloc_steal(state->dead_ctx, parent_instr);
511 return true;
512 }
513
514 assert(def->parent_instr->type != nir_instr_type_load_const);
515
516 /* At this point we know a priori that this SSA def is part of a
517 * nir_dest. We can use exec_node_data to get the dest pointer.
518 */
519 nir_dest *dest = exec_node_data(nir_dest, def, ssa);
520
521 *dest = nir_dest_for_reg(reg);
522 dest->reg.parent_instr = state->instr;
523 list_addtail(&dest->reg.def_link, &reg->defs);
524
525 return true;
526 }
527
528 /* Resolves ssa definitions to registers. While we're at it, we also
529 * remove phi nodes.
530 */
531 static bool
532 resolve_registers_block(nir_block *block, void *void_state)
533 {
534 struct from_ssa_state *state = void_state;
535
536 nir_foreach_instr_safe(block, instr) {
537 state->instr = instr;
538 nir_foreach_ssa_def(instr, rewrite_ssa_def, state);
539
540 if (instr->type == nir_instr_type_phi) {
541 nir_instr_remove(instr);
542 ralloc_steal(state->dead_ctx, instr);
543 }
544 }
545 state->instr = NULL;
546
547 return true;
548 }
549
550 static void
551 emit_copy(nir_parallel_copy_instr *pcopy, nir_src src, nir_src dest_src,
552 void *mem_ctx)
553 {
554 assert(!dest_src.is_ssa &&
555 dest_src.reg.indirect == NULL &&
556 dest_src.reg.base_offset == 0);
557
558 if (src.is_ssa)
559 assert(src.ssa->num_components >= dest_src.reg.reg->num_components);
560 else
561 assert(src.reg.reg->num_components >= dest_src.reg.reg->num_components);
562
563 nir_alu_instr *mov = nir_alu_instr_create(mem_ctx, nir_op_imov);
564 nir_src_copy(&mov->src[0].src, &src, mem_ctx);
565 mov->dest.dest = nir_dest_for_reg(dest_src.reg.reg);
566 mov->dest.write_mask = (1 << dest_src.reg.reg->num_components) - 1;
567
568 nir_instr_insert_before(&pcopy->instr, &mov->instr);
569 }
570
571 /* Resolves a single parallel copy operation into a sequence of mov's
572 *
573 * This is based on Algorithm 1 from "Revisiting Out-of-SSA Translation for
574 * Correctness, Code Quality, and Efficiency" by Boissinot et. al..
575 * However, I never got the algorithm to work as written, so this version
576 * is slightly modified.
577 *
578 * The algorithm works by playing this little shell game with the values.
579 * We start by recording where every source value is and which source value
580 * each destination value should receive. We then grab any copy whose
581 * destination is "empty", i.e. not used as a source, and do the following:
582 * - Find where its source value currently lives
583 * - Emit the move instruction
584 * - Set the location of the source value to the destination
585 * - Mark the location containing the source value
586 * - Mark the destination as no longer needing to be copied
587 *
588 * When we run out of "empty" destinations, we have a cycle and so we
589 * create a temporary register, copy to that register, and mark the value
590 * we copied as living in that temporary. Now, the cycle is broken, so we
591 * can continue with the above steps.
592 */
593 static void
594 resolve_parallel_copy(nir_parallel_copy_instr *pcopy,
595 struct from_ssa_state *state)
596 {
597 unsigned num_copies = 0;
598 nir_foreach_parallel_copy_entry(pcopy, entry) {
599 /* Sources may be SSA */
600 if (!entry->src.is_ssa && entry->src.reg.reg == entry->dest.reg.reg)
601 continue;
602
603 num_copies++;
604 }
605
606 if (num_copies == 0) {
607 /* Hooray, we don't need any copies! */
608 nir_instr_remove(&pcopy->instr);
609 return;
610 }
611
612 /* The register/source corresponding to the given index */
613 NIR_VLA_ZERO(nir_src, values, num_copies * 2);
614
615 /* The current location of a given piece of data. We will use -1 for "null" */
616 NIR_VLA_FILL(int, loc, num_copies * 2, -1);
617
618 /* The piece of data that the given piece of data is to be copied from. We will use -1 for "null" */
619 NIR_VLA_FILL(int, pred, num_copies * 2, -1);
620
621 /* The destinations we have yet to properly fill */
622 NIR_VLA(int, to_do, num_copies * 2);
623 int to_do_idx = -1;
624
625 /* Now we set everything up:
626 * - All values get assigned a temporary index
627 * - Current locations are set from sources
628 * - Predicessors are recorded from sources and destinations
629 */
630 int num_vals = 0;
631 nir_foreach_parallel_copy_entry(pcopy, entry) {
632 /* Sources may be SSA */
633 if (!entry->src.is_ssa && entry->src.reg.reg == entry->dest.reg.reg)
634 continue;
635
636 int src_idx = -1;
637 for (int i = 0; i < num_vals; ++i) {
638 if (nir_srcs_equal(values[i], entry->src))
639 src_idx = i;
640 }
641 if (src_idx < 0) {
642 src_idx = num_vals++;
643 values[src_idx] = entry->src;
644 }
645
646 nir_src dest_src = nir_src_for_reg(entry->dest.reg.reg);
647
648 int dest_idx = -1;
649 for (int i = 0; i < num_vals; ++i) {
650 if (nir_srcs_equal(values[i], dest_src)) {
651 /* Each destination of a parallel copy instruction should be
652 * unique. A destination may get used as a source, so we still
653 * have to walk the list. However, the predecessor should not,
654 * at this point, be set yet, so we should have -1 here.
655 */
656 assert(pred[i] == -1);
657 dest_idx = i;
658 }
659 }
660 if (dest_idx < 0) {
661 dest_idx = num_vals++;
662 values[dest_idx] = dest_src;
663 }
664
665 loc[src_idx] = src_idx;
666 pred[dest_idx] = src_idx;
667
668 to_do[++to_do_idx] = dest_idx;
669 }
670
671 /* Currently empty destinations we can go ahead and fill */
672 NIR_VLA(int, ready, num_copies * 2);
673 int ready_idx = -1;
674
675 /* Mark the ones that are ready for copying. We know an index is a
676 * destination if it has a predecessor and it's ready for copying if
677 * it's not marked as containing data.
678 */
679 for (int i = 0; i < num_vals; i++) {
680 if (pred[i] != -1 && loc[i] == -1)
681 ready[++ready_idx] = i;
682 }
683
684 while (to_do_idx >= 0) {
685 while (ready_idx >= 0) {
686 int b = ready[ready_idx--];
687 int a = pred[b];
688 emit_copy(pcopy, values[loc[a]], values[b], state->mem_ctx);
689
690 /* If any other copies want a they can find it at b */
691 loc[a] = b;
692
693 /* b has been filled, mark it as not needing to be copied */
694 pred[b] = -1;
695
696 /* If a needs to be filled, it's ready for copying now */
697 if (pred[a] != -1)
698 ready[++ready_idx] = a;
699 }
700 int b = to_do[to_do_idx--];
701 if (pred[b] == -1)
702 continue;
703
704 /* If we got here, then we don't have any more trivial copies that we
705 * can do. We have to break a cycle, so we create a new temporary
706 * register for that purpose. Normally, if going out of SSA after
707 * register allocation, you would want to avoid creating temporary
708 * registers. However, we are going out of SSA before register
709 * allocation, so we would rather not create extra register
710 * dependencies for the backend to deal with. If it wants, the
711 * backend can coalesce the (possibly multiple) temporaries.
712 */
713 assert(num_vals < num_copies * 2);
714 nir_register *reg = nir_local_reg_create(state->impl);
715 reg->name = "copy_temp";
716 reg->num_array_elems = 0;
717 if (values[b].is_ssa)
718 reg->num_components = values[b].ssa->num_components;
719 else
720 reg->num_components = values[b].reg.reg->num_components;
721 values[num_vals].is_ssa = false;
722 values[num_vals].reg.reg = reg;
723
724 emit_copy(pcopy, values[b], values[num_vals], state->mem_ctx);
725 loc[b] = num_vals;
726 ready[++ready_idx] = b;
727 num_vals++;
728 }
729
730 nir_instr_remove(&pcopy->instr);
731 }
732
733 /* Resolves the parallel copies in a block. Each block can have at most
734 * two: One at the beginning, right after all the phi noces, and one at
735 * the end (or right before the final jump if it exists).
736 */
737 static bool
738 resolve_parallel_copies_block(nir_block *block, void *void_state)
739 {
740 struct from_ssa_state *state = void_state;
741
742 /* At this point, we have removed all of the phi nodes. If a parallel
743 * copy existed right after the phi nodes in this block, it is now the
744 * first instruction.
745 */
746 nir_instr *first_instr = nir_block_first_instr(block);
747 if (first_instr == NULL)
748 return true; /* Empty, nothing to do. */
749
750 if (first_instr->type == nir_instr_type_parallel_copy) {
751 nir_parallel_copy_instr *pcopy = nir_instr_as_parallel_copy(first_instr);
752
753 resolve_parallel_copy(pcopy, state);
754 }
755
756 /* It's possible that the above code already cleaned up the end parallel
757 * copy. However, doing so removed it form the instructions list so we
758 * won't find it here. Therefore, it's safe to go ahead and just look
759 * for one and clean it up if it exists.
760 */
761 nir_parallel_copy_instr *end_pcopy =
762 get_parallel_copy_at_end_of_block(block);
763 if (end_pcopy)
764 resolve_parallel_copy(end_pcopy, state);
765
766 return true;
767 }
768
769 static void
770 nir_convert_from_ssa_impl(nir_function_impl *impl, bool phi_webs_only)
771 {
772 struct from_ssa_state state;
773
774 state.mem_ctx = ralloc_parent(impl);
775 state.dead_ctx = ralloc_context(NULL);
776 state.impl = impl;
777 state.phi_webs_only = phi_webs_only;
778 state.merge_node_table = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
779 _mesa_key_pointer_equal);
780
781 nir_foreach_block(impl, add_parallel_copy_to_end_of_block, &state);
782 nir_foreach_block(impl, isolate_phi_nodes_block, &state);
783
784 /* Mark metadata as dirty before we ask for liveness analysis */
785 nir_metadata_preserve(impl, nir_metadata_block_index |
786 nir_metadata_dominance);
787
788 nir_metadata_require(impl, nir_metadata_live_variables |
789 nir_metadata_dominance);
790
791 nir_foreach_block(impl, coalesce_phi_nodes_block, &state);
792 nir_foreach_block(impl, aggressive_coalesce_block, &state);
793
794 nir_foreach_block(impl, resolve_registers_block, &state);
795
796 nir_foreach_block(impl, resolve_parallel_copies_block, &state);
797
798 nir_metadata_preserve(impl, nir_metadata_block_index |
799 nir_metadata_dominance);
800
801 /* Clean up dead instructions and the hash tables */
802 _mesa_hash_table_destroy(state.merge_node_table, NULL);
803 ralloc_free(state.dead_ctx);
804 }
805
806 void
807 nir_convert_from_ssa(nir_shader *shader, bool phi_webs_only)
808 {
809 nir_foreach_overload(shader, overload) {
810 if (overload->impl)
811 nir_convert_from_ssa_impl(overload->impl, phi_webs_only);
812 }
813 }