i965: Ensure that we end instruction streams properly.
[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_list(node, instructions) {
111 backend_instruction *inst = (backend_instruction *)node;
112
113 cur->end = inst;
114
115 /* set_next_block wants the post-incremented ip */
116 ip++;
117
118 switch (inst->opcode) {
119 case BRW_OPCODE_IF:
120 /* Push our information onto a stack so we can recover from
121 * nested ifs.
122 */
123 if_stack.push_tail(link(mem_ctx, cur_if));
124 else_stack.push_tail(link(mem_ctx, cur_else));
125
126 cur_if = cur;
127 cur_else = NULL;
128 cur_endif = NULL;
129
130 /* Set up our immediately following block, full of "then"
131 * instructions.
132 */
133 next = new_block();
134 next->start = (backend_instruction *)inst->next;
135 cur_if->add_successor(mem_ctx, next);
136
137 set_next_block(&cur, next, ip);
138 break;
139
140 case BRW_OPCODE_ELSE:
141 cur_else = cur;
142
143 next = new_block();
144 next->start = (backend_instruction *)inst->next;
145 cur_if->add_successor(mem_ctx, next);
146
147 set_next_block(&cur, next, ip);
148 break;
149
150 case BRW_OPCODE_ENDIF: {
151 if (cur->start == inst) {
152 /* New block was just created; use it. */
153 cur_endif = cur;
154 } else {
155 cur_endif = new_block();
156 cur_endif->start = inst;
157
158 cur->end = (backend_instruction *)inst->prev;
159 cur->add_successor(mem_ctx, cur_endif);
160
161 set_next_block(&cur, cur_endif, ip - 1);
162 }
163
164 backend_instruction *else_inst = NULL;
165 if (cur_else) {
166 else_inst = (backend_instruction *)cur_else->end;
167
168 cur_else->add_successor(mem_ctx, cur_endif);
169 } else {
170 cur_if->add_successor(mem_ctx, cur_endif);
171 }
172
173 assert(cur_if->end->opcode == BRW_OPCODE_IF);
174 assert(!else_inst || else_inst->opcode == BRW_OPCODE_ELSE);
175 assert(inst->opcode == BRW_OPCODE_ENDIF);
176
177 cur_if->if_inst = cur_if->end;
178 cur_if->else_inst = else_inst;
179 cur_if->endif_inst = inst;
180
181 if (cur_else) {
182 cur_else->if_inst = cur_if->end;
183 cur_else->else_inst = else_inst;
184 cur_else->endif_inst = inst;
185 }
186
187 cur->if_inst = cur_if->end;
188 cur->else_inst = else_inst;
189 cur->endif_inst = inst;
190
191 /* Pop the stack so we're in the previous if/else/endif */
192 cur_if = pop_stack(&if_stack);
193 cur_else = pop_stack(&else_stack);
194 break;
195 }
196 case BRW_OPCODE_DO:
197 /* Push our information onto a stack so we can recover from
198 * nested loops.
199 */
200 do_stack.push_tail(link(mem_ctx, cur_do));
201 while_stack.push_tail(link(mem_ctx, cur_while));
202
203 /* Set up the block just after the while. Don't know when exactly
204 * it will start, yet.
205 */
206 cur_while = new_block();
207
208 if (cur->start == inst) {
209 /* New block was just created; use it. */
210 cur_do = cur;
211 } else {
212 cur_do = new_block();
213 cur_do->start = inst;
214
215 cur->end = (backend_instruction *)inst->prev;
216 cur->add_successor(mem_ctx, cur_do);
217
218 set_next_block(&cur, cur_do, ip - 1);
219 }
220 break;
221
222 case BRW_OPCODE_CONTINUE:
223 cur->add_successor(mem_ctx, cur_do);
224
225 next = new_block();
226 next->start = (backend_instruction *)inst->next;
227 if (inst->predicate)
228 cur->add_successor(mem_ctx, next);
229
230 set_next_block(&cur, next, ip);
231 break;
232
233 case BRW_OPCODE_BREAK:
234 cur->add_successor(mem_ctx, cur_while);
235
236 next = new_block();
237 next->start = (backend_instruction *)inst->next;
238 if (inst->predicate)
239 cur->add_successor(mem_ctx, next);
240
241 set_next_block(&cur, next, ip);
242 break;
243
244 case BRW_OPCODE_WHILE:
245 cur_while->start = (backend_instruction *)inst->next;
246
247 cur->add_successor(mem_ctx, cur_do);
248 set_next_block(&cur, cur_while, ip);
249
250 /* Pop the stack so we're in the previous loop */
251 cur_do = pop_stack(&do_stack);
252 cur_while = pop_stack(&while_stack);
253 break;
254
255 default:
256 break;
257 }
258 }
259
260 assert(cur->end);
261
262 cur->end_ip = ip;
263
264 make_block_array();
265 }
266
267 cfg_t::~cfg_t()
268 {
269 ralloc_free(mem_ctx);
270 }
271
272 bblock_t *
273 cfg_t::new_block()
274 {
275 bblock_t *block = new(mem_ctx) bblock_t();
276
277 return block;
278 }
279
280 void
281 cfg_t::set_next_block(bblock_t **cur, bblock_t *block, int ip)
282 {
283 if (*cur) {
284 assert((*cur)->end->next == block->start);
285 (*cur)->end_ip = ip - 1;
286 }
287
288 block->start_ip = ip;
289 block->block_num = num_blocks++;
290 block_list.push_tail(link(mem_ctx, block));
291 *cur = block;
292 }
293
294 void
295 cfg_t::make_block_array()
296 {
297 blocks = ralloc_array(mem_ctx, bblock_t *, num_blocks);
298
299 int i = 0;
300 foreach_list_typed(bblock_link, link, link, &block_list) {
301 blocks[i++] = link->block;
302 }
303 assert(i == num_blocks);
304 }
305
306 void
307 cfg_t::dump(backend_visitor *v)
308 {
309 for (int b = 0; b < this->num_blocks; b++) {
310 bblock_t *block = this->blocks[b];
311 fprintf(stderr, "START B%d", b);
312 foreach_list_typed(bblock_link, link, link, &block->parents) {
313 fprintf(stderr, " <-B%d",
314 link->block->block_num);
315 }
316 fprintf(stderr, "\n");
317 block->dump(v);
318 fprintf(stderr, "END B%d", b);
319 foreach_list_typed(bblock_link, link, link, &block->children) {
320 fprintf(stderr, " ->B%d",
321 link->block->block_num);
322 }
323 fprintf(stderr, "\n");
324 }
325 }