2 * Copyright © 2014 Intel Corporation
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:
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
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
24 * Jason Ekstrand (jason@jlekstrand.net)
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
37 struct from_ssa_state
{
41 struct hash_table
*merge_node_table
;
43 nir_function_impl
*impl
;
46 /* Returns true if a dominates b */
48 ssa_def_dominates(nir_ssa_def
*a
, nir_ssa_def
*b
)
50 if (a
->live_index
== 0) {
51 /* SSA undefs always dominate */
53 } else if (b
->live_index
< a
->live_index
) {
55 } else if (a
->parent_instr
->block
== b
->parent_instr
->block
) {
56 return a
->live_index
<= b
->live_index
;
58 return nir_block_dominates(a
->parent_instr
->block
,
59 b
->parent_instr
->block
);
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..
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
84 struct exec_node node
;
85 struct merge_set
*set
;
89 typedef struct merge_set
{
90 struct exec_list nodes
;
97 merge_set_dump(merge_set
*set
, FILE *fp
)
99 nir_ssa_def
*dom
[set
->size
];
102 foreach_list_typed(merge_node
, node
, node
, &set
->nodes
) {
103 while (dom_idx
>= 0 && !ssa_def_dominates(dom
[dom_idx
], node
->def
))
106 for (int i
= 0; i
<= dom_idx
; i
++)
110 fprintf(fp
, "ssa_%d /* %s */\n", node
->def
->index
, node
->def
->name
);
112 fprintf(fp
, "ssa_%d\n", node
->def
->index
);
114 dom
[++dom_idx
] = node
->def
;
120 get_merge_node(nir_ssa_def
*def
, struct from_ssa_state
*state
)
122 struct hash_entry
*entry
=
123 _mesa_hash_table_search(state
->merge_node_table
, def
);
127 merge_set
*set
= ralloc(state
->dead_ctx
, merge_set
);
128 exec_list_make_empty(&set
->nodes
);
132 merge_node
*node
= ralloc(state
->dead_ctx
, merge_node
);
135 exec_list_push_head(&set
->nodes
, &node
->node
);
137 _mesa_hash_table_insert(state
->merge_node_table
, def
, node
);
143 merge_nodes_interfere(merge_node
*a
, merge_node
*b
)
145 return nir_ssa_defs_interfere(a
->def
, b
->def
);
148 /* Merges b into a */
150 merge_merge_sets(merge_set
*a
, merge_set
*b
)
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
);
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
;
176 /* Checks for any interference between two merge sets
178 * This is an implementation of Algorithm 2 in "Revisiting Out-of-SSA
179 * Translation for Correctness, Code Quality, and Efficiency" by
183 merge_sets_interfere(merge_set
*a
, merge_set
*b
)
185 NIR_VLA(merge_node
*, dom
, a
->size
+ b
->size
);
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
)) {
194 if (exec_node_is_tail_sentinel(an
)) {
195 current
= exec_node_data(merge_node
, bn
, node
);
197 } else if (exec_node_is_tail_sentinel(bn
)) {
198 current
= exec_node_data(merge_node
, an
, node
);
201 merge_node
*a_node
= exec_node_data(merge_node
, an
, node
);
202 merge_node
*b_node
= exec_node_data(merge_node
, bn
, node
);
204 if (a_node
->def
->live_index
<= b_node
->def
->live_index
) {
213 while (dom_idx
>= 0 &&
214 !ssa_def_dominates(dom
[dom_idx
]->def
, current
->def
))
217 if (dom_idx
>= 0 && merge_nodes_interfere(current
, dom
[dom_idx
]))
220 dom
[++dom_idx
] = current
;
227 add_parallel_copy_to_end_of_block(nir_block
*block
, void *void_state
)
229 struct from_ssa_state
*state
= void_state
;
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;
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;
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
249 nir_parallel_copy_instr
*pcopy
=
250 nir_parallel_copy_instr_create(state
->dead_ctx
);
252 nir_instr_insert(nir_after_block_before_jump(block
), &pcopy
->instr
);
258 static nir_parallel_copy_instr
*
259 get_parallel_copy_at_end_of_block(nir_block
*block
)
261 nir_instr
*last_instr
= nir_block_last_instr(block
);
262 if (last_instr
== NULL
)
265 /* The last instruction may be a jump in which case the parallel copy is
268 if (last_instr
->type
== nir_instr_type_jump
)
269 last_instr
= nir_instr_prev(last_instr
);
271 if (last_instr
&& last_instr
->type
== nir_instr_type_parallel_copy
)
272 return nir_instr_as_parallel_copy(last_instr
);
277 /** Isolate phi nodes with parallel copies
279 * In order to solve the dependency problems with the sources and
280 * destinations of phi nodes, we first isolate them by adding parallel
281 * copies to the beginnings and ends of basic blocks. For every block with
282 * phi nodes, we add a parallel copy immediately following the last phi
283 * node that copies the destinations of all of the phi nodes to new SSA
284 * values. We also add a parallel copy to the end of every block that has
285 * a successor with phi nodes that, for each phi node in each successor,
286 * copies the corresponding sorce of the phi node and adjust the phi to
287 * used the destination of the parallel copy.
289 * In SSA form, each value has exactly one definition. What this does is
290 * ensure that each value used in a phi also has exactly one use. The
291 * destinations of phis are only used by the parallel copy immediately
292 * following the phi nodes and. Thanks to the parallel copy at the end of
293 * the predecessor block, the sources of phi nodes are are the only use of
294 * that value. This allows us to immediately assign all the sources and
295 * destinations of any given phi node to the same register without worrying
296 * about interference at all. We do coalescing to get rid of the parallel
297 * copies where possible.
299 * Before this pass can be run, we have to iterate over the blocks with
300 * add_parallel_copy_to_end_of_block to ensure that the parallel copies at
301 * the ends of blocks exist. We can create the ones at the beginnings as
302 * we go, but the ones at the ends of blocks need to be created ahead of
303 * time because of potential back-edges in the CFG.
306 isolate_phi_nodes_block(nir_block
*block
, void *void_state
)
308 struct from_ssa_state
*state
= void_state
;
310 nir_instr
*last_phi_instr
= NULL
;
311 nir_foreach_instr(block
, instr
) {
312 /* Phi nodes only ever come at the start of a block */
313 if (instr
->type
!= nir_instr_type_phi
)
316 last_phi_instr
= instr
;
319 /* If we don't have any phi's, then there's nothing for us to do. */
320 if (last_phi_instr
== NULL
)
323 /* If we have phi nodes, we need to create a parallel copy at the
324 * start of this block but after the phi nodes.
326 nir_parallel_copy_instr
*block_pcopy
=
327 nir_parallel_copy_instr_create(state
->dead_ctx
);
328 nir_instr_insert_after(last_phi_instr
, &block_pcopy
->instr
);
330 nir_foreach_instr(block
, instr
) {
331 /* Phi nodes only ever come at the start of a block */
332 if (instr
->type
!= nir_instr_type_phi
)
335 nir_phi_instr
*phi
= nir_instr_as_phi(instr
);
336 assert(phi
->dest
.is_ssa
);
337 nir_foreach_phi_src(phi
, src
) {
338 nir_parallel_copy_instr
*pcopy
=
339 get_parallel_copy_at_end_of_block(src
->pred
);
342 nir_parallel_copy_entry
*entry
= rzalloc(state
->dead_ctx
,
343 nir_parallel_copy_entry
);
344 nir_ssa_dest_init(&pcopy
->instr
, &entry
->dest
,
345 phi
->dest
.ssa
.num_components
, src
->src
.ssa
->name
);
346 exec_list_push_tail(&pcopy
->entries
, &entry
->node
);
348 assert(src
->src
.is_ssa
);
349 nir_instr_rewrite_src(&pcopy
->instr
, &entry
->src
, src
->src
);
351 nir_instr_rewrite_src(&phi
->instr
, &src
->src
,
352 nir_src_for_ssa(&entry
->dest
.ssa
));
355 nir_parallel_copy_entry
*entry
= rzalloc(state
->dead_ctx
,
356 nir_parallel_copy_entry
);
357 nir_ssa_dest_init(&block_pcopy
->instr
, &entry
->dest
,
358 phi
->dest
.ssa
.num_components
, phi
->dest
.ssa
.name
);
359 exec_list_push_tail(&block_pcopy
->entries
, &entry
->node
);
361 nir_ssa_def_rewrite_uses(&phi
->dest
.ssa
,
362 nir_src_for_ssa(&entry
->dest
.ssa
));
364 nir_instr_rewrite_src(&block_pcopy
->instr
, &entry
->src
,
365 nir_src_for_ssa(&phi
->dest
.ssa
));
372 coalesce_phi_nodes_block(nir_block
*block
, void *void_state
)
374 struct from_ssa_state
*state
= void_state
;
376 nir_foreach_instr(block
, instr
) {
377 /* Phi nodes only ever come at the start of a block */
378 if (instr
->type
!= nir_instr_type_phi
)
381 nir_phi_instr
*phi
= nir_instr_as_phi(instr
);
383 assert(phi
->dest
.is_ssa
);
384 merge_node
*dest_node
= get_merge_node(&phi
->dest
.ssa
, state
);
386 nir_foreach_phi_src(phi
, src
) {
387 assert(src
->src
.is_ssa
);
388 merge_node
*src_node
= get_merge_node(src
->src
.ssa
, state
);
389 if (src_node
->set
!= dest_node
->set
)
390 merge_merge_sets(dest_node
->set
, src_node
->set
);
398 aggressive_coalesce_parallel_copy(nir_parallel_copy_instr
*pcopy
,
399 struct from_ssa_state
*state
)
401 nir_foreach_parallel_copy_entry(pcopy
, entry
) {
402 if (!entry
->src
.is_ssa
)
405 /* Since load_const instructions are SSA only, we can't replace their
406 * destinations with registers and, therefore, can't coalesce them.
408 if (entry
->src
.ssa
->parent_instr
->type
== nir_instr_type_load_const
)
411 /* Don't try and coalesce these */
412 if (entry
->dest
.ssa
.num_components
!= entry
->src
.ssa
->num_components
)
415 merge_node
*src_node
= get_merge_node(entry
->src
.ssa
, state
);
416 merge_node
*dest_node
= get_merge_node(&entry
->dest
.ssa
, state
);
418 if (src_node
->set
== dest_node
->set
)
421 if (!merge_sets_interfere(src_node
->set
, dest_node
->set
))
422 merge_merge_sets(src_node
->set
, dest_node
->set
);
427 aggressive_coalesce_block(nir_block
*block
, void *void_state
)
429 struct from_ssa_state
*state
= void_state
;
431 nir_parallel_copy_instr
*start_pcopy
= NULL
;
432 nir_foreach_instr(block
, instr
) {
433 /* Phi nodes only ever come at the start of a block */
434 if (instr
->type
!= nir_instr_type_phi
) {
435 if (instr
->type
!= nir_instr_type_parallel_copy
)
436 break; /* The parallel copy must be right after the phis */
438 start_pcopy
= nir_instr_as_parallel_copy(instr
);
440 aggressive_coalesce_parallel_copy(start_pcopy
, state
);
446 nir_parallel_copy_instr
*end_pcopy
=
447 get_parallel_copy_at_end_of_block(block
);
449 if (end_pcopy
&& end_pcopy
!= start_pcopy
)
450 aggressive_coalesce_parallel_copy(end_pcopy
, state
);
456 rewrite_ssa_def(nir_ssa_def
*def
, void *void_state
)
458 struct from_ssa_state
*state
= void_state
;
461 struct hash_entry
*entry
=
462 _mesa_hash_table_search(state
->merge_node_table
, def
);
464 /* In this case, we're part of a phi web. Use the web's register. */
465 merge_node
*node
= (merge_node
*)entry
->data
;
467 /* If it doesn't have a register yet, create one. Note that all of
468 * the things in the merge set should be the same so it doesn't
469 * matter which node's definition we use.
471 if (node
->set
->reg
== NULL
) {
472 node
->set
->reg
= nir_local_reg_create(state
->impl
);
473 node
->set
->reg
->name
= def
->name
;
474 node
->set
->reg
->num_components
= def
->num_components
;
475 node
->set
->reg
->num_array_elems
= 0;
478 reg
= node
->set
->reg
;
480 if (state
->phi_webs_only
)
483 /* We leave load_const SSA values alone. They act as immediates to
484 * the backend. If it got coalesced into a phi, that's ok.
486 if (def
->parent_instr
->type
== nir_instr_type_load_const
)
489 reg
= nir_local_reg_create(state
->impl
);
490 reg
->name
= def
->name
;
491 reg
->num_components
= def
->num_components
;
492 reg
->num_array_elems
= 0;
495 nir_ssa_def_rewrite_uses(def
, nir_src_for_reg(reg
));
496 assert(list_empty(&def
->uses
) && list_empty(&def
->if_uses
));
498 if (def
->parent_instr
->type
== nir_instr_type_ssa_undef
) {
499 /* If it's an ssa_undef instruction, remove it since we know we just got
500 * rid of all its uses.
502 nir_instr
*parent_instr
= def
->parent_instr
;
503 nir_instr_remove(parent_instr
);
504 ralloc_steal(state
->dead_ctx
, parent_instr
);
508 assert(def
->parent_instr
->type
!= nir_instr_type_load_const
);
510 /* At this point we know a priori that this SSA def is part of a
511 * nir_dest. We can use exec_node_data to get the dest pointer.
513 nir_dest
*dest
= exec_node_data(nir_dest
, def
, ssa
);
515 nir_instr_rewrite_dest(state
->instr
, dest
, nir_dest_for_reg(reg
));
520 /* Resolves ssa definitions to registers. While we're at it, we also
524 resolve_registers_block(nir_block
*block
, void *void_state
)
526 struct from_ssa_state
*state
= void_state
;
528 nir_foreach_instr_safe(block
, instr
) {
529 state
->instr
= instr
;
530 nir_foreach_ssa_def(instr
, rewrite_ssa_def
, state
);
532 if (instr
->type
== nir_instr_type_phi
) {
533 nir_instr_remove(instr
);
534 ralloc_steal(state
->dead_ctx
, instr
);
543 emit_copy(nir_parallel_copy_instr
*pcopy
, nir_src src
, nir_src dest_src
,
546 assert(!dest_src
.is_ssa
&&
547 dest_src
.reg
.indirect
== NULL
&&
548 dest_src
.reg
.base_offset
== 0);
551 assert(src
.ssa
->num_components
>= dest_src
.reg
.reg
->num_components
);
553 assert(src
.reg
.reg
->num_components
>= dest_src
.reg
.reg
->num_components
);
555 nir_alu_instr
*mov
= nir_alu_instr_create(mem_ctx
, nir_op_imov
);
556 nir_src_copy(&mov
->src
[0].src
, &src
, mov
);
557 mov
->dest
.dest
= nir_dest_for_reg(dest_src
.reg
.reg
);
558 mov
->dest
.write_mask
= (1 << dest_src
.reg
.reg
->num_components
) - 1;
560 nir_instr_insert_before(&pcopy
->instr
, &mov
->instr
);
563 /* Resolves a single parallel copy operation into a sequence of mov's
565 * This is based on Algorithm 1 from "Revisiting Out-of-SSA Translation for
566 * Correctness, Code Quality, and Efficiency" by Boissinot et. al..
567 * However, I never got the algorithm to work as written, so this version
568 * is slightly modified.
570 * The algorithm works by playing this little shell game with the values.
571 * We start by recording where every source value is and which source value
572 * each destination value should receive. We then grab any copy whose
573 * destination is "empty", i.e. not used as a source, and do the following:
574 * - Find where its source value currently lives
575 * - Emit the move instruction
576 * - Set the location of the source value to the destination
577 * - Mark the location containing the source value
578 * - Mark the destination as no longer needing to be copied
580 * When we run out of "empty" destinations, we have a cycle and so we
581 * create a temporary register, copy to that register, and mark the value
582 * we copied as living in that temporary. Now, the cycle is broken, so we
583 * can continue with the above steps.
586 resolve_parallel_copy(nir_parallel_copy_instr
*pcopy
,
587 struct from_ssa_state
*state
)
589 unsigned num_copies
= 0;
590 nir_foreach_parallel_copy_entry(pcopy
, entry
) {
591 /* Sources may be SSA */
592 if (!entry
->src
.is_ssa
&& entry
->src
.reg
.reg
== entry
->dest
.reg
.reg
)
598 if (num_copies
== 0) {
599 /* Hooray, we don't need any copies! */
600 nir_instr_remove(&pcopy
->instr
);
604 /* The register/source corresponding to the given index */
605 NIR_VLA_ZERO(nir_src
, values
, num_copies
* 2);
607 /* The current location of a given piece of data. We will use -1 for "null" */
608 NIR_VLA_FILL(int, loc
, num_copies
* 2, -1);
610 /* The piece of data that the given piece of data is to be copied from. We will use -1 for "null" */
611 NIR_VLA_FILL(int, pred
, num_copies
* 2, -1);
613 /* The destinations we have yet to properly fill */
614 NIR_VLA(int, to_do
, num_copies
* 2);
617 /* Now we set everything up:
618 * - All values get assigned a temporary index
619 * - Current locations are set from sources
620 * - Predicessors are recorded from sources and destinations
623 nir_foreach_parallel_copy_entry(pcopy
, entry
) {
624 /* Sources may be SSA */
625 if (!entry
->src
.is_ssa
&& entry
->src
.reg
.reg
== entry
->dest
.reg
.reg
)
629 for (int i
= 0; i
< num_vals
; ++i
) {
630 if (nir_srcs_equal(values
[i
], entry
->src
))
634 src_idx
= num_vals
++;
635 values
[src_idx
] = entry
->src
;
638 nir_src dest_src
= nir_src_for_reg(entry
->dest
.reg
.reg
);
641 for (int i
= 0; i
< num_vals
; ++i
) {
642 if (nir_srcs_equal(values
[i
], dest_src
)) {
643 /* Each destination of a parallel copy instruction should be
644 * unique. A destination may get used as a source, so we still
645 * have to walk the list. However, the predecessor should not,
646 * at this point, be set yet, so we should have -1 here.
648 assert(pred
[i
] == -1);
653 dest_idx
= num_vals
++;
654 values
[dest_idx
] = dest_src
;
657 loc
[src_idx
] = src_idx
;
658 pred
[dest_idx
] = src_idx
;
660 to_do
[++to_do_idx
] = dest_idx
;
663 /* Currently empty destinations we can go ahead and fill */
664 NIR_VLA(int, ready
, num_copies
* 2);
667 /* Mark the ones that are ready for copying. We know an index is a
668 * destination if it has a predecessor and it's ready for copying if
669 * it's not marked as containing data.
671 for (int i
= 0; i
< num_vals
; i
++) {
672 if (pred
[i
] != -1 && loc
[i
] == -1)
673 ready
[++ready_idx
] = i
;
676 while (to_do_idx
>= 0) {
677 while (ready_idx
>= 0) {
678 int b
= ready
[ready_idx
--];
680 emit_copy(pcopy
, values
[loc
[a
]], values
[b
], state
->mem_ctx
);
682 /* If any other copies want a they can find it at b */
685 /* b has been filled, mark it as not needing to be copied */
688 /* If a needs to be filled, it's ready for copying now */
690 ready
[++ready_idx
] = a
;
692 int b
= to_do
[to_do_idx
--];
696 /* If we got here, then we don't have any more trivial copies that we
697 * can do. We have to break a cycle, so we create a new temporary
698 * register for that purpose. Normally, if going out of SSA after
699 * register allocation, you would want to avoid creating temporary
700 * registers. However, we are going out of SSA before register
701 * allocation, so we would rather not create extra register
702 * dependencies for the backend to deal with. If it wants, the
703 * backend can coalesce the (possibly multiple) temporaries.
705 assert(num_vals
< num_copies
* 2);
706 nir_register
*reg
= nir_local_reg_create(state
->impl
);
707 reg
->name
= "copy_temp";
708 reg
->num_array_elems
= 0;
709 if (values
[b
].is_ssa
)
710 reg
->num_components
= values
[b
].ssa
->num_components
;
712 reg
->num_components
= values
[b
].reg
.reg
->num_components
;
713 values
[num_vals
].is_ssa
= false;
714 values
[num_vals
].reg
.reg
= reg
;
716 emit_copy(pcopy
, values
[b
], values
[num_vals
], state
->mem_ctx
);
718 ready
[++ready_idx
] = b
;
722 nir_instr_remove(&pcopy
->instr
);
725 /* Resolves the parallel copies in a block. Each block can have at most
726 * two: One at the beginning, right after all the phi noces, and one at
727 * the end (or right before the final jump if it exists).
730 resolve_parallel_copies_block(nir_block
*block
, void *void_state
)
732 struct from_ssa_state
*state
= void_state
;
734 /* At this point, we have removed all of the phi nodes. If a parallel
735 * copy existed right after the phi nodes in this block, it is now the
738 nir_instr
*first_instr
= nir_block_first_instr(block
);
739 if (first_instr
== NULL
)
740 return true; /* Empty, nothing to do. */
742 if (first_instr
->type
== nir_instr_type_parallel_copy
) {
743 nir_parallel_copy_instr
*pcopy
= nir_instr_as_parallel_copy(first_instr
);
745 resolve_parallel_copy(pcopy
, state
);
748 /* It's possible that the above code already cleaned up the end parallel
749 * copy. However, doing so removed it form the instructions list so we
750 * won't find it here. Therefore, it's safe to go ahead and just look
751 * for one and clean it up if it exists.
753 nir_parallel_copy_instr
*end_pcopy
=
754 get_parallel_copy_at_end_of_block(block
);
756 resolve_parallel_copy(end_pcopy
, state
);
762 nir_convert_from_ssa_impl(nir_function_impl
*impl
, bool phi_webs_only
)
764 struct from_ssa_state state
;
766 state
.mem_ctx
= ralloc_parent(impl
);
767 state
.dead_ctx
= ralloc_context(NULL
);
769 state
.phi_webs_only
= phi_webs_only
;
770 state
.merge_node_table
= _mesa_hash_table_create(NULL
, _mesa_hash_pointer
,
771 _mesa_key_pointer_equal
);
773 nir_foreach_block(impl
, add_parallel_copy_to_end_of_block
, &state
);
774 nir_foreach_block(impl
, isolate_phi_nodes_block
, &state
);
776 /* Mark metadata as dirty before we ask for liveness analysis */
777 nir_metadata_preserve(impl
, nir_metadata_block_index
|
778 nir_metadata_dominance
);
780 nir_metadata_require(impl
, nir_metadata_live_ssa_defs
|
781 nir_metadata_dominance
);
783 nir_foreach_block(impl
, coalesce_phi_nodes_block
, &state
);
784 nir_foreach_block(impl
, aggressive_coalesce_block
, &state
);
786 nir_foreach_block(impl
, resolve_registers_block
, &state
);
788 nir_foreach_block(impl
, resolve_parallel_copies_block
, &state
);
790 nir_metadata_preserve(impl
, nir_metadata_block_index
|
791 nir_metadata_dominance
);
793 /* Clean up dead instructions and the hash tables */
794 _mesa_hash_table_destroy(state
.merge_node_table
, NULL
);
795 ralloc_free(state
.dead_ctx
);
799 nir_convert_from_ssa(nir_shader
*shader
, bool phi_webs_only
)
801 nir_foreach_function(shader
, function
) {
803 nir_convert_from_ssa_impl(function
->impl
, phi_webs_only
);