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"
31
nir_block_worklist_init(nir_block_worklist *w, unsigned num_blocks,
38
w->blocks_present = rzalloc_array(mem_ctx, BITSET_WORD,
39
BITSET_WORDS(num_blocks));
40
w->blocks = rzalloc_array(mem_ctx, nir_block *, num_blocks);
44
nir_block_worklist_fini(nir_block_worklist *w)
46
ralloc_free(w->blocks_present);
47
ralloc_free(w->blocks);
51
nir_block_worklist_add_all(nir_block_worklist *w, nir_function_impl *impl)
53
nir_foreach_block(block, impl) {
54
nir_block_worklist_push_tail(w, block);
59
nir_block_worklist_push_head(nir_block_worklist *w, nir_block *block)
61
/* Pushing a block we already have is a no-op */
62
if (BITSET_TEST(w->blocks_present, block->index))
65
assert(w->count < w->size);
68
w->start = w->size - 1;
74
w->blocks[w->start] = block;
75
BITSET_SET(w->blocks_present, block->index);
79
nir_block_worklist_peek_head(const nir_block_worklist *w)
83
return w->blocks[w->start];
87
nir_block_worklist_pop_head(nir_block_worklist *w)
91
unsigned head = w->start;
93
w->start = (w->start + 1) % w->size;
96
BITSET_CLEAR(w->blocks_present, w->blocks[head]->index);
97
return w->blocks[head];
101
nir_block_worklist_push_tail(nir_block_worklist *w, nir_block *block)
103
/* Pushing a block we already have is a no-op */
104
if (BITSET_TEST(w->blocks_present, block->index))
107
assert(w->count < w->size);
111
unsigned tail = (w->start + w->count - 1) % w->size;
113
w->blocks[tail] = block;
114
BITSET_SET(w->blocks_present, block->index);
118
nir_block_worklist_peek_tail(const nir_block_worklist *w)
120
assert(w->count > 0);
122
unsigned tail = (w->start + w->count - 1) % w->size;
124
return w->blocks[tail];
128
nir_block_worklist_pop_tail(nir_block_worklist *w)
130
assert(w->count > 0);
132
unsigned tail = (w->start + w->count - 1) % w->size;
136
BITSET_CLEAR(w->blocks_present, w->blocks[tail]->index);
137
return w->blocks[tail];
141
nir_instr_worklist_add_srcs_cb(nir_src *src, void *state)
143
nir_instr_worklist *wl = state;
146
nir_instr_worklist_push_tail(wl, src->ssa->parent_instr);
152
nir_instr_worklist_add_ssa_srcs(nir_instr_worklist *wl, nir_instr *instr)
154
nir_foreach_src(instr, nir_instr_worklist_add_srcs_cb, wl);