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