Merge branch 'master' of ../mesa into vulkan
[mesa.git] / src / glsl / nir / nir_worklist.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_worklist.h"
29
30 void
31 nir_block_worklist_init(nir_block_worklist *w, unsigned num_blocks,
32 void *mem_ctx)
33 {
34 w->size = num_blocks;
35 w->count = 0;
36 w->start = 0;
37
38 w->blocks_present = rzalloc_array(mem_ctx, BITSET_WORD,
39 BITSET_WORDS(num_blocks));
40 w->blocks = ralloc_array(mem_ctx, nir_block *, num_blocks);
41 }
42
43 void
44 nir_block_worklist_fini(nir_block_worklist *w)
45 {
46 ralloc_free(w->blocks_present);
47 ralloc_free(w->blocks);
48 }
49
50 static bool
51 worklist_add_block(nir_block *block, void *w)
52 {
53 nir_block_worklist_push_tail(w, block);
54
55 return true;
56 }
57
58 void
59 nir_block_worklist_add_all(nir_block_worklist *w, nir_function_impl *impl)
60 {
61 nir_foreach_block(impl, worklist_add_block, w);
62 }
63
64 void
65 nir_block_worklist_push_head(nir_block_worklist *w, nir_block *block)
66 {
67 /* Pushing a block we already have is a no-op */
68 if (BITSET_TEST(w->blocks_present, block->index))
69 return;
70
71 assert(w->count < w->size);
72
73 if (w->start == 0)
74 w->start = w->size - 1;
75 else
76 w->start--;
77
78 w->count++;
79
80 w->blocks[w->start] = block;
81 BITSET_SET(w->blocks_present, block->index);
82 }
83
84 nir_block *
85 nir_block_worklist_peek_head(const nir_block_worklist *w)
86 {
87 assert(w->count > 0);
88
89 return w->blocks[w->start];
90 }
91
92 nir_block *
93 nir_block_worklist_pop_head(nir_block_worklist *w)
94 {
95 assert(w->count > 0);
96
97 unsigned head = w->start;
98
99 w->start = (w->start + 1) % w->size;
100 w->count--;
101
102 BITSET_CLEAR(w->blocks_present, w->blocks[head]->index);
103 return w->blocks[head];
104 }
105
106 void
107 nir_block_worklist_push_tail(nir_block_worklist *w, nir_block *block)
108 {
109 /* Pushing a block we already have is a no-op */
110 if (BITSET_TEST(w->blocks_present, block->index))
111 return;
112
113 assert(w->count < w->size);
114
115 w->count++;
116
117 unsigned tail = (w->start + w->count - 1) % w->size;
118
119 w->blocks[tail] = block;
120 BITSET_SET(w->blocks_present, block->index);
121 }
122
123 nir_block *
124 nir_block_worklist_peek_tail(const nir_block_worklist *w)
125 {
126 assert(w->count > 0);
127
128 unsigned tail = (w->start + w->count - 1) % w->size;
129
130 return w->blocks[tail];
131 }
132
133 nir_block *
134 nir_block_worklist_pop_tail(nir_block_worklist *w)
135 {
136 assert(w->count > 0);
137
138 unsigned tail = (w->start + w->count - 1) % w->size;
139
140 w->count--;
141
142 BITSET_CLEAR(w->blocks_present, w->blocks[tail]->index);
143 return w->blocks[tail];
144 }