i965: Use unreachable() instead of unconditional assert().
[mesa.git] / src / mesa / drivers / dri / i965 / brw_cfg.cpp
1 /*
2 * Copyright © 2012 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 * Eric Anholt <eric@anholt.net>
25 *
26 */
27
28 #include "brw_cfg.h"
29
30 /** @file brw_cfg.cpp
31 *
32 * Walks the shader instructions generated and creates a set of basic
33 * blocks with successor/predecessor edges connecting them.
34 */
35
36 static bblock_t *
37 pop_stack(exec_list *list)
38 {
39 bblock_link *link = (bblock_link *)list->get_tail();
40 bblock_t *block = link->block;
41 link->link.remove();
42
43 return block;
44 }
45
46 static exec_node *
47 link(void *mem_ctx, bblock_t *block)
48 {
49 bblock_link *l = new(mem_ctx) bblock_link(block);
50 return &l->link;
51 }
52
53 bblock_t::bblock_t() :
54 start_ip(0), end_ip(0), block_num(0)
55 {
56 start = NULL;
57 end = NULL;
58
59 parents.make_empty();
60 children.make_empty();
61
62 if_inst = NULL;
63 else_inst = NULL;
64 endif_inst = NULL;
65 }
66
67 void
68 bblock_t::add_successor(void *mem_ctx, bblock_t *successor)
69 {
70 successor->parents.push_tail(link(mem_ctx, this));
71 children.push_tail(link(mem_ctx, successor));
72 }
73
74 void
75 bblock_t::dump(backend_visitor *v)
76 {
77 int ip = this->start_ip;
78 for (backend_instruction *inst = (backend_instruction *)this->start;
79 inst != this->end->next;
80 inst = (backend_instruction *) inst->next) {
81 fprintf(stderr, "%5d: ", ip);
82 v->dump_instruction(inst);
83 ip++;
84 }
85 }
86
87 cfg_t::cfg_t(exec_list *instructions)
88 {
89 mem_ctx = ralloc_context(NULL);
90 block_list.make_empty();
91 blocks = NULL;
92 num_blocks = 0;
93
94 bblock_t *cur = NULL;
95 int ip = 0;
96
97 bblock_t *entry = new_block();
98 bblock_t *cur_if = NULL; /**< BB ending with IF. */
99 bblock_t *cur_else = NULL; /**< BB ending with ELSE. */
100 bblock_t *cur_endif = NULL; /**< BB starting with ENDIF. */
101 bblock_t *cur_do = NULL; /**< BB starting with DO. */
102 bblock_t *cur_while = NULL; /**< BB immediately following WHILE. */
103 exec_list if_stack, else_stack, do_stack, while_stack;
104 bblock_t *next;
105
106 set_next_block(&cur, entry, ip);
107
108 entry->start = (backend_instruction *) instructions->get_head();
109
110 foreach_in_list(backend_instruction, inst, instructions) {
111 cur->end = inst;
112
113 /* set_next_block wants the post-incremented ip */
114 ip++;
115
116 switch (inst->opcode) {
117 case BRW_OPCODE_IF:
118 /* Push our information onto a stack so we can recover from
119 * nested ifs.
120 */
121 if_stack.push_tail(link(mem_ctx, cur_if));
122 else_stack.push_tail(link(mem_ctx, cur_else));
123
124 cur_if = cur;
125 cur_else = NULL;
126 cur_endif = NULL;
127
128 /* Set up our immediately following block, full of "then"
129 * instructions.
130 */
131 next = new_block();
132 next->start = (backend_instruction *)inst->next;
133 cur_if->add_successor(mem_ctx, next);
134
135 set_next_block(&cur, next, ip);
136 break;
137
138 case BRW_OPCODE_ELSE:
139 cur_else = cur;
140
141 next = new_block();
142 next->start = (backend_instruction *)inst->next;
143 cur_if->add_successor(mem_ctx, next);
144
145 set_next_block(&cur, next, ip);
146 break;
147
148 case BRW_OPCODE_ENDIF: {
149 if (cur->start == inst) {
150 /* New block was just created; use it. */
151 cur_endif = cur;
152 } else {
153 cur_endif = new_block();
154 cur_endif->start = inst;
155
156 cur->end = (backend_instruction *)inst->prev;
157 cur->add_successor(mem_ctx, cur_endif);
158
159 set_next_block(&cur, cur_endif, ip - 1);
160 }
161
162 backend_instruction *else_inst = NULL;
163 if (cur_else) {
164 else_inst = (backend_instruction *)cur_else->end;
165
166 cur_else->add_successor(mem_ctx, cur_endif);
167 } else {
168 cur_if->add_successor(mem_ctx, cur_endif);
169 }
170
171 assert(cur_if->end->opcode == BRW_OPCODE_IF);
172 assert(!else_inst || else_inst->opcode == BRW_OPCODE_ELSE);
173 assert(inst->opcode == BRW_OPCODE_ENDIF);
174
175 cur_if->if_inst = cur_if->end;
176 cur_if->else_inst = else_inst;
177 cur_if->endif_inst = inst;
178
179 if (cur_else) {
180 cur_else->if_inst = cur_if->end;
181 cur_else->else_inst = else_inst;
182 cur_else->endif_inst = inst;
183 }
184
185 cur->if_inst = cur_if->end;
186 cur->else_inst = else_inst;
187 cur->endif_inst = inst;
188
189 /* Pop the stack so we're in the previous if/else/endif */
190 cur_if = pop_stack(&if_stack);
191 cur_else = pop_stack(&else_stack);
192 break;
193 }
194 case BRW_OPCODE_DO:
195 /* Push our information onto a stack so we can recover from
196 * nested loops.
197 */
198 do_stack.push_tail(link(mem_ctx, cur_do));
199 while_stack.push_tail(link(mem_ctx, cur_while));
200
201 /* Set up the block just after the while. Don't know when exactly
202 * it will start, yet.
203 */
204 cur_while = new_block();
205
206 if (cur->start == inst) {
207 /* New block was just created; use it. */
208 cur_do = cur;
209 } else {
210 cur_do = new_block();
211 cur_do->start = inst;
212
213 cur->end = (backend_instruction *)inst->prev;
214 cur->add_successor(mem_ctx, cur_do);
215
216 set_next_block(&cur, cur_do, ip - 1);
217 }
218 break;
219
220 case BRW_OPCODE_CONTINUE:
221 cur->add_successor(mem_ctx, cur_do);
222
223 next = new_block();
224 next->start = (backend_instruction *)inst->next;
225 if (inst->predicate)
226 cur->add_successor(mem_ctx, next);
227
228 set_next_block(&cur, next, ip);
229 break;
230
231 case BRW_OPCODE_BREAK:
232 cur->add_successor(mem_ctx, cur_while);
233
234 next = new_block();
235 next->start = (backend_instruction *)inst->next;
236 if (inst->predicate)
237 cur->add_successor(mem_ctx, next);
238
239 set_next_block(&cur, next, ip);
240 break;
241
242 case BRW_OPCODE_WHILE:
243 cur_while->start = (backend_instruction *)inst->next;
244
245 cur->add_successor(mem_ctx, cur_do);
246 set_next_block(&cur, cur_while, ip);
247
248 /* Pop the stack so we're in the previous loop */
249 cur_do = pop_stack(&do_stack);
250 cur_while = pop_stack(&while_stack);
251 break;
252
253 default:
254 break;
255 }
256 }
257
258 assert(cur->end);
259
260 cur->end_ip = ip;
261
262 make_block_array();
263 }
264
265 cfg_t::~cfg_t()
266 {
267 ralloc_free(mem_ctx);
268 }
269
270 bblock_t *
271 cfg_t::new_block()
272 {
273 bblock_t *block = new(mem_ctx) bblock_t();
274
275 return block;
276 }
277
278 void
279 cfg_t::set_next_block(bblock_t **cur, bblock_t *block, int ip)
280 {
281 if (*cur) {
282 assert((*cur)->end->next == block->start);
283 (*cur)->end_ip = ip - 1;
284 }
285
286 block->start_ip = ip;
287 block->block_num = num_blocks++;
288 block_list.push_tail(link(mem_ctx, block));
289 *cur = block;
290 }
291
292 void
293 cfg_t::make_block_array()
294 {
295 blocks = ralloc_array(mem_ctx, bblock_t *, num_blocks);
296
297 int i = 0;
298 foreach_list_typed(bblock_link, link, link, &block_list) {
299 blocks[i++] = link->block;
300 }
301 assert(i == num_blocks);
302 }
303
304 void
305 cfg_t::dump(backend_visitor *v)
306 {
307 for (int b = 0; b < this->num_blocks; b++) {
308 bblock_t *block = this->blocks[b];
309 fprintf(stderr, "START B%d", b);
310 foreach_list_typed(bblock_link, link, link, &block->parents) {
311 fprintf(stderr, " <-B%d",
312 link->block->block_num);
313 }
314 fprintf(stderr, "\n");
315 block->dump(v);
316 fprintf(stderr, "END B%d", b);
317 foreach_list_typed(bblock_link, link, link, &block->children) {
318 fprintf(stderr, " ->B%d",
319 link->block->block_num);
320 }
321 fprintf(stderr, "\n");
322 }
323 }