5d46615bc7b98b28229aebcb02c87d028e51e7fc
2 * Copyright © 2012 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 * Eric Anholt <eric@anholt.net>
32 * Walks the shader instructions generated and creates a set of basic
33 * blocks with successor/predecessor edges connecting them.
37 pop_stack(exec_list
*list
)
39 bblock_link
*link
= (bblock_link
*)list
->get_tail();
40 bblock_t
*block
= link
->block
;
47 link(void *mem_ctx
, bblock_t
*block
)
49 bblock_link
*l
= new(mem_ctx
) bblock_link(block
);
53 bblock_t::bblock_t(cfg_t
*cfg
) :
54 cfg(cfg
), idom(NULL
), start_ip(0), end_ip(0), num(0)
56 instructions
.make_empty();
58 children
.make_empty();
62 bblock_t::add_successor(void *mem_ctx
, bblock_t
*successor
)
64 successor
->parents
.push_tail(::link(mem_ctx
, this));
65 children
.push_tail(::link(mem_ctx
, successor
));
69 bblock_t::is_predecessor_of(const bblock_t
*block
) const
71 foreach_list_typed_safe (bblock_link
, parent
, link
, &block
->parents
) {
72 if (parent
->block
== this) {
81 bblock_t::is_successor_of(const bblock_t
*block
) const
83 foreach_list_typed_safe (bblock_link
, child
, link
, &block
->children
) {
84 if (child
->block
== this) {
93 ends_block(const backend_instruction
*inst
)
95 enum opcode op
= inst
->opcode
;
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
;
105 starts_block(const backend_instruction
*inst
)
107 enum opcode op
= inst
->opcode
;
109 return op
== BRW_OPCODE_DO
||
110 op
== BRW_OPCODE_ENDIF
;
114 bblock_t::can_combine_with(const bblock_t
*that
) const
116 if ((const bblock_t
*)this->link
.next
!= that
)
119 if (ends_block(this->end()) ||
120 starts_block(that
->start()))
127 bblock_t::combine_with(bblock_t
*that
)
129 assert(this->can_combine_with(that
));
130 foreach_list_typed (bblock_link
, link
, link
, &this->children
) {
131 assert(link
->block
== that
);
133 foreach_list_typed (bblock_link
, link
, link
, &that
->parents
) {
134 assert(link
->block
== this);
137 this->end_ip
= that
->end_ip
;
138 this->instructions
.append_list(&that
->instructions
);
140 this->cfg
->remove_block(that
);
144 bblock_t::dump(backend_shader
*s
) const
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
);
154 cfg_t::cfg_t(exec_list
*instructions
)
156 mem_ctx
= ralloc_context(NULL
);
157 block_list
.make_empty();
162 bblock_t
*cur
= NULL
;
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
;
174 set_next_block(&cur
, entry
, ip
);
176 foreach_in_list_safe(backend_instruction
, inst
, instructions
) {
177 /* set_next_block wants the post-incremented ip */
180 switch (inst
->opcode
) {
182 inst
->exec_node::remove();
183 cur
->instructions
.push_tail(inst
);
185 /* Push our information onto a stack so we can recover from
188 if_stack
.push_tail(link(mem_ctx
, cur_if
));
189 else_stack
.push_tail(link(mem_ctx
, cur_else
));
195 /* Set up our immediately following block, full of "then"
199 cur_if
->add_successor(mem_ctx
, next
);
201 set_next_block(&cur
, next
, ip
);
204 case BRW_OPCODE_ELSE
:
205 inst
->exec_node::remove();
206 cur
->instructions
.push_tail(inst
);
211 assert(cur_if
!= NULL
);
212 cur_if
->add_successor(mem_ctx
, next
);
214 set_next_block(&cur
, next
, ip
);
217 case BRW_OPCODE_ENDIF
: {
218 if (cur
->instructions
.is_empty()) {
219 /* New block was just created; use it. */
222 cur_endif
= new_block();
224 cur
->add_successor(mem_ctx
, cur_endif
);
226 set_next_block(&cur
, cur_endif
, ip
- 1);
229 inst
->exec_node::remove();
230 cur
->instructions
.push_tail(inst
);
233 cur_else
->add_successor(mem_ctx
, cur_endif
);
235 assert(cur_if
!= NULL
);
236 cur_if
->add_successor(mem_ctx
, cur_endif
);
239 assert(cur_if
->end()->opcode
== BRW_OPCODE_IF
);
240 assert(!cur_else
|| cur_else
->end()->opcode
== BRW_OPCODE_ELSE
);
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
);
248 /* Push our information onto a stack so we can recover from
251 do_stack
.push_tail(link(mem_ctx
, cur_do
));
252 while_stack
.push_tail(link(mem_ctx
, cur_while
));
254 /* Set up the block just after the while. Don't know when exactly
255 * it will start, yet.
257 cur_while
= new_block();
259 if (cur
->instructions
.is_empty()) {
260 /* New block was just created; use it. */
263 cur_do
= new_block();
265 cur
->add_successor(mem_ctx
, cur_do
);
267 set_next_block(&cur
, cur_do
, ip
- 1);
270 inst
->exec_node::remove();
271 cur
->instructions
.push_tail(inst
);
274 case BRW_OPCODE_CONTINUE
:
275 inst
->exec_node::remove();
276 cur
->instructions
.push_tail(inst
);
278 assert(cur_do
!= NULL
);
279 cur
->add_successor(mem_ctx
, cur_do
);
283 cur
->add_successor(mem_ctx
, next
);
285 set_next_block(&cur
, next
, ip
);
288 case BRW_OPCODE_BREAK
:
289 inst
->exec_node::remove();
290 cur
->instructions
.push_tail(inst
);
292 assert(cur_while
!= NULL
);
293 cur
->add_successor(mem_ctx
, cur_while
);
297 cur
->add_successor(mem_ctx
, next
);
299 set_next_block(&cur
, next
, ip
);
302 case BRW_OPCODE_WHILE
:
303 inst
->exec_node::remove();
304 cur
->instructions
.push_tail(inst
);
306 assert(cur_do
!= NULL
&& cur_while
!= NULL
);
307 cur
->add_successor(mem_ctx
, cur_do
);
310 cur
->add_successor(mem_ctx
, cur_while
);
312 set_next_block(&cur
, cur_while
, ip
);
314 /* Pop the stack so we're in the previous loop */
315 cur_do
= pop_stack(&do_stack
);
316 cur_while
= pop_stack(&while_stack
);
320 inst
->exec_node::remove();
321 cur
->instructions
.push_tail(inst
);
326 cur
->end_ip
= ip
- 1;
333 ralloc_free(mem_ctx
);
337 cfg_t::remove_block(bblock_t
*block
)
339 foreach_list_typed_safe (bblock_link
, predecessor
, link
, &block
->parents
) {
340 /* Remove block from all of its predecessors' successor lists. */
341 foreach_list_typed_safe (bblock_link
, successor
, link
,
342 &predecessor
->block
->children
) {
343 if (block
== successor
->block
) {
344 successor
->link
.remove();
345 ralloc_free(successor
);
349 /* Add removed-block's successors to its predecessors' successor lists. */
350 foreach_list_typed (bblock_link
, successor
, link
, &block
->children
) {
351 if (!successor
->block
->is_successor_of(predecessor
->block
)) {
352 predecessor
->block
->children
.push_tail(link(mem_ctx
,
358 foreach_list_typed_safe (bblock_link
, successor
, link
, &block
->children
) {
359 /* Remove block from all of its childrens' parents lists. */
360 foreach_list_typed_safe (bblock_link
, predecessor
, link
,
361 &successor
->block
->parents
) {
362 if (block
== predecessor
->block
) {
363 predecessor
->link
.remove();
364 ralloc_free(predecessor
);
368 /* Add removed-block's predecessors to its successors' predecessor lists. */
369 foreach_list_typed (bblock_link
, predecessor
, link
, &block
->parents
) {
370 if (!predecessor
->block
->is_predecessor_of(successor
->block
)) {
371 successor
->block
->parents
.push_tail(link(mem_ctx
,
372 predecessor
->block
));
377 block
->link
.remove();
379 for (int b
= block
->num
; b
< this->num_blocks
- 1; b
++) {
380 this->blocks
[b
] = this->blocks
[b
+ 1];
381 this->blocks
[b
]->num
= b
;
384 this->blocks
[this->num_blocks
- 1]->num
= this->num_blocks
- 2;
392 bblock_t
*block
= new(mem_ctx
) bblock_t(this);
398 cfg_t::set_next_block(bblock_t
**cur
, bblock_t
*block
, int ip
)
401 (*cur
)->end_ip
= ip
- 1;
404 block
->start_ip
= ip
;
405 block
->num
= num_blocks
++;
406 block_list
.push_tail(&block
->link
);
411 cfg_t::make_block_array()
413 blocks
= ralloc_array(mem_ctx
, bblock_t
*, num_blocks
);
416 foreach_block (block
, this) {
419 assert(i
== num_blocks
);
423 cfg_t::dump(backend_shader
*s
)
428 foreach_block (block
, this) {
430 fprintf(stderr
, "START B%d IDOM(B%d)", block
->num
, block
->idom
->num
);
432 fprintf(stderr
, "START B%d IDOM(none)", block
->num
);
434 foreach_list_typed(bblock_link
, link
, link
, &block
->parents
) {
435 fprintf(stderr
, " <-B%d",
438 fprintf(stderr
, "\n");
441 fprintf(stderr
, "END B%d", block
->num
);
442 foreach_list_typed(bblock_link
, link
, link
, &block
->children
) {
443 fprintf(stderr
, " ->B%d",
446 fprintf(stderr
, "\n");
450 /* Calculates the immediate dominator of each block, according to "A Simple,
451 * Fast Dominance Algorithm" by Keith D. Cooper, Timothy J. Harvey, and Ken
454 * The authors claim that for control flow graphs of sizes normally encountered
455 * (less than 1000 nodes) that this algorithm is significantly faster than
456 * others like Lengauer-Tarjan.
459 cfg_t::calculate_idom()
461 foreach_block(block
, this) {
464 blocks
[0]->idom
= blocks
[0];
470 foreach_block(block
, this) {
474 bblock_t
*new_idom
= NULL
;
475 foreach_list_typed(bblock_link
, parent
, link
, &block
->parents
) {
476 if (parent
->block
->idom
) {
477 if (new_idom
== NULL
) {
478 new_idom
= parent
->block
;
479 } else if (parent
->block
->idom
!= NULL
) {
480 new_idom
= intersect(parent
->block
, new_idom
);
485 if (block
->idom
!= new_idom
) {
486 block
->idom
= new_idom
;
496 cfg_t::intersect(bblock_t
*b1
, bblock_t
*b2
)
498 /* Note, the comparisons here are the opposite of what the paper says
499 * because we index blocks from beginning -> end (i.e. reverse post-order)
500 * instead of post-order like they assume.
502 while (b1
->num
!= b2
->num
) {
503 while (b1
->num
> b2
->num
)
505 while (b2
->num
> b1
->num
)
515 printf("digraph CFG {\n");
516 for (int b
= 0; b
< num_blocks
; b
++) {
517 bblock_t
*block
= this->blocks
[b
];
519 foreach_list_typed_safe (bblock_link
, child
, link
, &block
->children
) {
520 printf("\t%d -> %d\n", b
, child
->block
->num
);
527 cfg_t::dump_domtree()
529 printf("digraph DominanceTree {\n");
530 foreach_block(block
, this) {
532 printf("\t%d -> %d\n", block
->idom
->num
, block
->num
);