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)
28 #include "nir_worklist.h"
32 * Basic liveness analysis. This works only in SSA form.
34 * This liveness pass treats phi nodes as being melded to the space between
35 * blocks so that the destinations of a phi are in the livein of the block
36 * in which it resides and the sources are in the liveout of the
37 * corresponding block. By formulating the liveness information in this
38 * way, we ensure that the definition of any variable dominates its entire
39 * live range. This is true because the only way that the definition of an
40 * SSA value may not dominate a use is if the use is in a phi node and the
41 * uses in phi no are in the live-out of the corresponding predecessor
42 * block but not in the live-in of the block containing the phi node.
45 struct live_ssa_defs_state
{
46 unsigned num_ssa_defs
;
47 unsigned bitset_words
;
49 /* Used in propagate_across_edge() */
50 BITSET_WORD
*tmp_live
;
52 nir_block_worklist worklist
;
56 index_ssa_def(nir_ssa_def
*def
, void *void_state
)
58 struct live_ssa_defs_state
*state
= void_state
;
60 if (def
->parent_instr
->type
== nir_instr_type_ssa_undef
)
63 def
->live_index
= state
->num_ssa_defs
++;
68 /* Initialize the liveness data to zero and add the given block to the
72 init_liveness_block(nir_block
*block
,
73 struct live_ssa_defs_state
*state
)
75 block
->live_in
= reralloc(block
, block
->live_in
, BITSET_WORD
,
77 memset(block
->live_in
, 0, state
->bitset_words
* sizeof(BITSET_WORD
));
79 block
->live_out
= reralloc(block
, block
->live_out
, BITSET_WORD
,
81 memset(block
->live_out
, 0, state
->bitset_words
* sizeof(BITSET_WORD
));
83 nir_block_worklist_push_head(&state
->worklist
, block
);
89 set_src_live(nir_src
*src
, void *void_live
)
91 BITSET_WORD
*live
= void_live
;
96 if (src
->ssa
->live_index
== 0)
97 return true; /* undefined variables are never live */
99 BITSET_SET(live
, src
->ssa
->live_index
);
105 set_ssa_def_dead(nir_ssa_def
*def
, void *void_live
)
107 BITSET_WORD
*live
= void_live
;
109 BITSET_CLEAR(live
, def
->live_index
);
114 /** Propagates the live in of succ across the edge to the live out of pred
116 * Phi nodes exist "between" blocks and all the phi nodes at the start of a
117 * block act "in parallel". When we propagate from the live_in of one
118 * block to the live out of the other, we have to kill any writes from phis
119 * and make live any sources.
121 * Returns true if updating live out of pred added anything
124 propagate_across_edge(nir_block
*pred
, nir_block
*succ
,
125 struct live_ssa_defs_state
*state
)
127 BITSET_WORD
*live
= state
->tmp_live
;
128 memcpy(live
, succ
->live_in
, state
->bitset_words
* sizeof *live
);
130 nir_foreach_instr(instr
, succ
) {
131 if (instr
->type
!= nir_instr_type_phi
)
133 nir_phi_instr
*phi
= nir_instr_as_phi(instr
);
135 assert(phi
->dest
.is_ssa
);
136 set_ssa_def_dead(&phi
->dest
.ssa
, live
);
139 nir_foreach_instr(instr
, succ
) {
140 if (instr
->type
!= nir_instr_type_phi
)
142 nir_phi_instr
*phi
= nir_instr_as_phi(instr
);
144 nir_foreach_phi_src(src
, phi
) {
145 if (src
->pred
== pred
) {
146 set_src_live(&src
->src
, live
);
152 BITSET_WORD progress
= 0;
153 for (unsigned i
= 0; i
< state
->bitset_words
; ++i
) {
154 progress
|= live
[i
] & ~pred
->live_out
[i
];
155 pred
->live_out
[i
] |= live
[i
];
157 return progress
!= 0;
161 nir_live_ssa_defs_impl(nir_function_impl
*impl
)
163 struct live_ssa_defs_state state
;
165 /* We start at 1 because we reserve the index value of 0 for ssa_undef
166 * instructions. Those are never live, so their liveness information
167 * can be compacted into a single bit.
169 state
.num_ssa_defs
= 1;
170 nir_foreach_block(block
, impl
) {
171 nir_foreach_instr(instr
, block
)
172 nir_foreach_ssa_def(instr
, index_ssa_def
, &state
);
175 nir_block_worklist_init(&state
.worklist
, impl
->num_blocks
, NULL
);
177 /* We now know how many unique ssa definitions we have and we can go
178 * ahead and allocate live_in and live_out sets and add all of the
179 * blocks to the worklist.
181 state
.bitset_words
= BITSET_WORDS(state
.num_ssa_defs
);
182 state
.tmp_live
= rzalloc_array(impl
, BITSET_WORD
, state
.bitset_words
);
183 nir_foreach_block(block
, impl
) {
184 init_liveness_block(block
, &state
);
188 /* We're now ready to work through the worklist and update the liveness
189 * sets of each of the blocks. By the time we get to this point, every
190 * block in the function implementation has been pushed onto the
191 * worklist in reverse order. As long as we keep the worklist
192 * up-to-date as we go, everything will get covered.
194 while (!nir_block_worklist_is_empty(&state
.worklist
)) {
195 /* We pop them off in the reverse order we pushed them on. This way
196 * the first walk of the instructions is backwards so we only walk
197 * once in the case of no control flow.
199 nir_block
*block
= nir_block_worklist_pop_head(&state
.worklist
);
201 memcpy(block
->live_in
, block
->live_out
,
202 state
.bitset_words
* sizeof(BITSET_WORD
));
204 nir_if
*following_if
= nir_block_get_following_if(block
);
206 set_src_live(&following_if
->condition
, block
->live_in
);
208 nir_foreach_instr_reverse(instr
, block
) {
209 /* Phi nodes are handled seperately so we want to skip them. Since
210 * we are going backwards and they are at the beginning, we can just
211 * break as soon as we see one.
213 if (instr
->type
== nir_instr_type_phi
)
216 nir_foreach_ssa_def(instr
, set_ssa_def_dead
, block
->live_in
);
217 nir_foreach_src(instr
, set_src_live
, block
->live_in
);
220 /* Walk over all of the predecessors of the current block updating
221 * their live in with the live out of this one. If anything has
222 * changed, add the predecessor to the work list so that we ensure
223 * that the new information is used.
225 set_foreach(block
->predecessors
, entry
) {
226 nir_block
*pred
= (nir_block
*)entry
->key
;
227 if (propagate_across_edge(pred
, block
, &state
))
228 nir_block_worklist_push_tail(&state
.worklist
, pred
);
232 ralloc_free(state
.tmp_live
);
233 nir_block_worklist_fini(&state
.worklist
);
237 src_does_not_use_def(nir_src
*src
, void *def
)
239 return !src
->is_ssa
|| src
->ssa
!= (nir_ssa_def
*)def
;
243 search_for_use_after_instr(nir_instr
*start
, nir_ssa_def
*def
)
245 /* Only look for a use strictly after the given instruction */
246 struct exec_node
*node
= start
->node
.next
;
247 while (!exec_node_is_tail_sentinel(node
)) {
248 nir_instr
*instr
= exec_node_data(nir_instr
, node
, node
);
249 if (!nir_foreach_src(instr
, src_does_not_use_def
, def
))
256 /* Returns true if def is live at instr assuming that def comes before
257 * instr in a pre DFS search of the dominance tree.
260 nir_ssa_def_is_live_at(nir_ssa_def
*def
, nir_instr
*instr
)
262 if (BITSET_TEST(instr
->block
->live_out
, def
->live_index
)) {
263 /* Since def dominates instr, if def is in the liveout of the block,
268 if (BITSET_TEST(instr
->block
->live_in
, def
->live_index
) ||
269 def
->parent_instr
->block
== instr
->block
) {
270 /* In this case it is either live coming into instr's block or it
271 * is defined in the same block. In this case, we simply need to
272 * see if it is used after instr.
274 return search_for_use_after_instr(instr
, def
);
282 nir_ssa_defs_interfere(nir_ssa_def
*a
, nir_ssa_def
*b
)
284 if (a
->parent_instr
== b
->parent_instr
) {
285 /* Two variables defined at the same time interfere assuming at
286 * least one isn't dead.
289 } else if (a
->live_index
== 0 || b
->live_index
== 0) {
290 /* If either variable is an ssa_undef, then there's no interference */
292 } else if (a
->live_index
< b
->live_index
) {
293 return nir_ssa_def_is_live_at(a
, b
->parent_instr
);
295 return nir_ssa_def_is_live_at(b
, a
->parent_instr
);