i965/fs: Lower 32x32 bit multiplication on BXT.
[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(cfg_t *cfg) :
54 cfg(cfg), idom(NULL), start_ip(0), end_ip(0), num(0)
55 {
56 instructions.make_empty();
57 parents.make_empty();
58 children.make_empty();
59 }
60
61 void
62 bblock_t::add_successor(void *mem_ctx, bblock_t *successor)
63 {
64 successor->parents.push_tail(::link(mem_ctx, this));
65 children.push_tail(::link(mem_ctx, successor));
66 }
67
68 bool
69 bblock_t::is_predecessor_of(const bblock_t *block) const
70 {
71 foreach_list_typed_safe (bblock_link, parent, link, &block->parents) {
72 if (parent->block == this) {
73 return true;
74 }
75 }
76
77 return false;
78 }
79
80 bool
81 bblock_t::is_successor_of(const bblock_t *block) const
82 {
83 foreach_list_typed_safe (bblock_link, child, link, &block->children) {
84 if (child->block == this) {
85 return true;
86 }
87 }
88
89 return false;
90 }
91
92 static bool
93 ends_block(const backend_instruction *inst)
94 {
95 enum opcode op = inst->opcode;
96
97 return op == BRW_OPCODE_IF ||
98 op == BRW_OPCODE_ELSE ||
99 op == BRW_OPCODE_CONTINUE ||
100 op == BRW_OPCODE_BREAK ||
101 op == BRW_OPCODE_WHILE;
102 }
103
104 static bool
105 starts_block(const backend_instruction *inst)
106 {
107 enum opcode op = inst->opcode;
108
109 return op == BRW_OPCODE_DO ||
110 op == BRW_OPCODE_ENDIF;
111 }
112
113 bool
114 bblock_t::can_combine_with(const bblock_t *that) const
115 {
116 if ((const bblock_t *)this->link.next != that)
117 return false;
118
119 if (ends_block(this->end()) ||
120 starts_block(that->start()))
121 return false;
122
123 return true;
124 }
125
126 void
127 bblock_t::combine_with(bblock_t *that)
128 {
129 assert(this->can_combine_with(that));
130 foreach_list_typed (bblock_link, link, link, &this->children) {
131 assert(link->block == that);
132 }
133 foreach_list_typed (bblock_link, link, link, &that->parents) {
134 assert(link->block == this);
135 }
136
137 this->end_ip = that->end_ip;
138 this->instructions.append_list(&that->instructions);
139
140 this->cfg->remove_block(that);
141 }
142
143 void
144 bblock_t::dump(backend_shader *s) const
145 {
146 int ip = this->start_ip;
147 foreach_inst_in_block(backend_instruction, inst, this) {
148 fprintf(stderr, "%5d: ", ip);
149 s->dump_instruction(inst);
150 ip++;
151 }
152 }
153
154 cfg_t::cfg_t(exec_list *instructions)
155 {
156 mem_ctx = ralloc_context(NULL);
157 block_list.make_empty();
158 blocks = NULL;
159 num_blocks = 0;
160 idom_dirty = true;
161
162 bblock_t *cur = NULL;
163 int ip = 0;
164
165 bblock_t *entry = new_block();
166 bblock_t *cur_if = NULL; /**< BB ending with IF. */
167 bblock_t *cur_else = NULL; /**< BB ending with ELSE. */
168 bblock_t *cur_endif = NULL; /**< BB starting with ENDIF. */
169 bblock_t *cur_do = NULL; /**< BB starting with DO. */
170 bblock_t *cur_while = NULL; /**< BB immediately following WHILE. */
171 exec_list if_stack, else_stack, do_stack, while_stack;
172 bblock_t *next;
173
174 set_next_block(&cur, entry, ip);
175
176 foreach_in_list_safe(backend_instruction, inst, instructions) {
177 /* set_next_block wants the post-incremented ip */
178 ip++;
179
180 switch (inst->opcode) {
181 case BRW_OPCODE_IF:
182 inst->exec_node::remove();
183 cur->instructions.push_tail(inst);
184
185 /* Push our information onto a stack so we can recover from
186 * nested ifs.
187 */
188 if_stack.push_tail(link(mem_ctx, cur_if));
189 else_stack.push_tail(link(mem_ctx, cur_else));
190
191 cur_if = cur;
192 cur_else = NULL;
193 cur_endif = NULL;
194
195 /* Set up our immediately following block, full of "then"
196 * instructions.
197 */
198 next = new_block();
199 cur_if->add_successor(mem_ctx, next);
200
201 set_next_block(&cur, next, ip);
202 break;
203
204 case BRW_OPCODE_ELSE:
205 inst->exec_node::remove();
206 cur->instructions.push_tail(inst);
207
208 cur_else = cur;
209
210 next = new_block();
211 assert(cur_if != NULL);
212 cur_if->add_successor(mem_ctx, next);
213
214 set_next_block(&cur, next, ip);
215 break;
216
217 case BRW_OPCODE_ENDIF: {
218 if (cur->instructions.is_empty()) {
219 /* New block was just created; use it. */
220 cur_endif = cur;
221 } else {
222 cur_endif = new_block();
223
224 cur->add_successor(mem_ctx, cur_endif);
225
226 set_next_block(&cur, cur_endif, ip - 1);
227 }
228
229 inst->exec_node::remove();
230 cur->instructions.push_tail(inst);
231
232 if (cur_else) {
233 cur_else->add_successor(mem_ctx, cur_endif);
234 } else {
235 assert(cur_if != NULL);
236 cur_if->add_successor(mem_ctx, cur_endif);
237 }
238
239 assert(cur_if->end()->opcode == BRW_OPCODE_IF);
240 assert(!cur_else || cur_else->end()->opcode == BRW_OPCODE_ELSE);
241
242 /* Pop the stack so we're in the previous if/else/endif */
243 cur_if = pop_stack(&if_stack);
244 cur_else = pop_stack(&else_stack);
245 break;
246 }
247 case BRW_OPCODE_DO:
248 /* Push our information onto a stack so we can recover from
249 * nested loops.
250 */
251 do_stack.push_tail(link(mem_ctx, cur_do));
252 while_stack.push_tail(link(mem_ctx, cur_while));
253
254 /* Set up the block just after the while. Don't know when exactly
255 * it will start, yet.
256 */
257 cur_while = new_block();
258
259 if (cur->instructions.is_empty()) {
260 /* New block was just created; use it. */
261 cur_do = cur;
262 } else {
263 cur_do = new_block();
264
265 cur->add_successor(mem_ctx, cur_do);
266
267 set_next_block(&cur, cur_do, ip - 1);
268 }
269
270 inst->exec_node::remove();
271 cur->instructions.push_tail(inst);
272 break;
273
274 case BRW_OPCODE_CONTINUE:
275 inst->exec_node::remove();
276 cur->instructions.push_tail(inst);
277
278 assert(cur_do != NULL);
279 cur->add_successor(mem_ctx, cur_do);
280
281 next = new_block();
282 if (inst->predicate)
283 cur->add_successor(mem_ctx, next);
284
285 set_next_block(&cur, next, ip);
286 break;
287
288 case BRW_OPCODE_BREAK:
289 inst->exec_node::remove();
290 cur->instructions.push_tail(inst);
291
292 assert(cur_while != NULL);
293 cur->add_successor(mem_ctx, cur_while);
294
295 next = new_block();
296 if (inst->predicate)
297 cur->add_successor(mem_ctx, next);
298
299 set_next_block(&cur, next, ip);
300 break;
301
302 case BRW_OPCODE_WHILE:
303 inst->exec_node::remove();
304 cur->instructions.push_tail(inst);
305
306 assert(cur_do != NULL && cur_while != NULL);
307 cur->add_successor(mem_ctx, cur_do);
308 set_next_block(&cur, cur_while, ip);
309
310 /* Pop the stack so we're in the previous loop */
311 cur_do = pop_stack(&do_stack);
312 cur_while = pop_stack(&while_stack);
313 break;
314
315 default:
316 inst->exec_node::remove();
317 cur->instructions.push_tail(inst);
318 break;
319 }
320 }
321
322 cur->end_ip = ip - 1;
323
324 make_block_array();
325 }
326
327 cfg_t::~cfg_t()
328 {
329 ralloc_free(mem_ctx);
330 }
331
332 void
333 cfg_t::remove_block(bblock_t *block)
334 {
335 foreach_list_typed_safe (bblock_link, predecessor, link, &block->parents) {
336 /* Remove block from all of its predecessors' successor lists. */
337 foreach_list_typed_safe (bblock_link, successor, link,
338 &predecessor->block->children) {
339 if (block == successor->block) {
340 successor->link.remove();
341 ralloc_free(successor);
342 }
343 }
344
345 /* Add removed-block's successors to its predecessors' successor lists. */
346 foreach_list_typed (bblock_link, successor, link, &block->children) {
347 if (!successor->block->is_successor_of(predecessor->block)) {
348 predecessor->block->children.push_tail(link(mem_ctx,
349 successor->block));
350 }
351 }
352 }
353
354 foreach_list_typed_safe (bblock_link, successor, link, &block->children) {
355 /* Remove block from all of its childrens' parents lists. */
356 foreach_list_typed_safe (bblock_link, predecessor, link,
357 &successor->block->parents) {
358 if (block == predecessor->block) {
359 predecessor->link.remove();
360 ralloc_free(predecessor);
361 }
362 }
363
364 /* Add removed-block's predecessors to its successors' predecessor lists. */
365 foreach_list_typed (bblock_link, predecessor, link, &block->parents) {
366 if (!predecessor->block->is_predecessor_of(successor->block)) {
367 successor->block->parents.push_tail(link(mem_ctx,
368 predecessor->block));
369 }
370 }
371 }
372
373 block->link.remove();
374
375 for (int b = block->num; b < this->num_blocks - 1; b++) {
376 this->blocks[b] = this->blocks[b + 1];
377 this->blocks[b]->num = b;
378 }
379
380 this->blocks[this->num_blocks - 1]->num = this->num_blocks - 2;
381 this->num_blocks--;
382 idom_dirty = true;
383 }
384
385 bblock_t *
386 cfg_t::new_block()
387 {
388 bblock_t *block = new(mem_ctx) bblock_t(this);
389
390 return block;
391 }
392
393 void
394 cfg_t::set_next_block(bblock_t **cur, bblock_t *block, int ip)
395 {
396 if (*cur) {
397 (*cur)->end_ip = ip - 1;
398 }
399
400 block->start_ip = ip;
401 block->num = num_blocks++;
402 block_list.push_tail(&block->link);
403 *cur = block;
404 }
405
406 void
407 cfg_t::make_block_array()
408 {
409 blocks = ralloc_array(mem_ctx, bblock_t *, num_blocks);
410
411 int i = 0;
412 foreach_block (block, this) {
413 blocks[i++] = block;
414 }
415 assert(i == num_blocks);
416 }
417
418 void
419 cfg_t::dump(backend_shader *s)
420 {
421 if (idom_dirty)
422 calculate_idom();
423
424 foreach_block (block, this) {
425 fprintf(stderr, "START B%d IDOM(B%d)", block->num, block->idom->num);
426 foreach_list_typed(bblock_link, link, link, &block->parents) {
427 fprintf(stderr, " <-B%d",
428 link->block->num);
429 }
430 fprintf(stderr, "\n");
431 if (s != NULL)
432 block->dump(s);
433 fprintf(stderr, "END B%d", block->num);
434 foreach_list_typed(bblock_link, link, link, &block->children) {
435 fprintf(stderr, " ->B%d",
436 link->block->num);
437 }
438 fprintf(stderr, "\n");
439 }
440 }
441
442 /* Calculates the immediate dominator of each block, according to "A Simple,
443 * Fast Dominance Algorithm" by Keith D. Cooper, Timothy J. Harvey, and Ken
444 * Kennedy.
445 *
446 * The authors claim that for control flow graphs of sizes normally encountered
447 * (less than 1000 nodes) that this algorithm is significantly faster than
448 * others like Lengauer-Tarjan.
449 */
450 void
451 cfg_t::calculate_idom()
452 {
453 foreach_block(block, this) {
454 block->idom = NULL;
455 }
456 blocks[0]->idom = blocks[0];
457
458 bool changed;
459 do {
460 changed = false;
461
462 foreach_block(block, this) {
463 if (block->num == 0)
464 continue;
465
466 bblock_t *new_idom = NULL;
467 foreach_list_typed(bblock_link, parent, link, &block->parents) {
468 if (parent->block->idom) {
469 if (new_idom == NULL) {
470 new_idom = parent->block;
471 } else if (parent->block->idom != NULL) {
472 new_idom = intersect(parent->block, new_idom);
473 }
474 }
475 }
476
477 if (block->idom != new_idom) {
478 block->idom = new_idom;
479 changed = true;
480 }
481 }
482 } while (changed);
483
484 idom_dirty = false;
485 }
486
487 bblock_t *
488 cfg_t::intersect(bblock_t *b1, bblock_t *b2)
489 {
490 /* Note, the comparisons here are the opposite of what the paper says
491 * because we index blocks from beginning -> end (i.e. reverse post-order)
492 * instead of post-order like they assume.
493 */
494 while (b1->num != b2->num) {
495 while (b1->num > b2->num)
496 b1 = b1->idom;
497 while (b2->num > b1->num)
498 b2 = b2->idom;
499 }
500 assert(b1);
501 return b1;
502 }
503
504 void
505 cfg_t::dump_cfg()
506 {
507 printf("digraph CFG {\n");
508 for (int b = 0; b < num_blocks; b++) {
509 bblock_t *block = this->blocks[b];
510
511 foreach_list_typed_safe (bblock_link, child, link, &block->children) {
512 printf("\t%d -> %d\n", b, child->block->num);
513 }
514 }
515 printf("}\n");
516 }
517
518 void
519 cfg_t::dump_domtree()
520 {
521 printf("digraph DominanceTree {\n");
522 foreach_block(block, this) {
523 printf("\t%d -> %d\n", block->idom->num, block->num);
524 }
525 printf("}\n");
526 }