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
= ralloc_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 worklist_add_block(nir_block
*block
, void *w
)
53 nir_block_worklist_push_tail(w
, block
);
59 nir_block_worklist_add_all(nir_block_worklist
*w
, nir_function_impl
*impl
)
61 nir_foreach_block(impl
, worklist_add_block
, w
);
65 nir_block_worklist_push_head(nir_block_worklist
*w
, nir_block
*block
)
67 /* Pushing a block we already have is a no-op */
68 if (BITSET_TEST(w
->blocks_present
, block
->index
))
71 assert(w
->count
< w
->size
);
74 w
->start
= w
->size
- 1;
80 w
->blocks
[w
->start
] = block
;
81 BITSET_SET(w
->blocks_present
, block
->index
);
85 nir_block_worklist_peek_head(nir_block_worklist
*w
)
89 return w
->blocks
[w
->start
];
93 nir_block_worklist_pop_head(nir_block_worklist
*w
)
97 unsigned head
= w
->start
;
99 w
->start
= (w
->start
+ 1) % w
->size
;
102 BITSET_CLEAR(w
->blocks_present
, w
->blocks
[head
]->index
);
103 return w
->blocks
[head
];
107 nir_block_worklist_push_tail(nir_block_worklist
*w
, nir_block
*block
)
109 /* Pushing a block we already have is a no-op */
110 if (BITSET_TEST(w
->blocks_present
, block
->index
))
113 assert(w
->count
< w
->size
);
117 unsigned tail
= w
->start
= (w
->start
+ w
->count
- 1) % w
->size
;
119 w
->blocks
[tail
] = block
;
120 BITSET_SET(w
->blocks_present
, block
->index
);
124 nir_block_worklist_peek_tail(nir_block_worklist
*w
)
126 assert(w
->count
> 0);
128 unsigned tail
= w
->start
= (w
->start
+ w
->count
- 1) % w
->size
;
130 return w
->blocks
[tail
];
134 nir_block_worklist_pop_tail(nir_block_worklist
*w
)
136 assert(w
->count
> 0);
138 unsigned tail
= w
->start
= (w
->start
+ w
->count
- 1) % w
->size
;
142 BITSET_CLEAR(w
->blocks_present
, w
->blocks
[tail
]->index
);
143 return w
->blocks
[tail
];