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
), start_ip(0), end_ip(0), num(0),
55 if_block(NULL
), else_block(NULL
)
61 children
.make_empty();
65 bblock_t::add_successor(void *mem_ctx
, bblock_t
*successor
)
67 successor
->parents
.push_tail(::link(mem_ctx
, this));
68 children
.push_tail(::link(mem_ctx
, successor
));
72 bblock_t::is_predecessor_of(const bblock_t
*block
) const
74 foreach_list_typed_safe (bblock_link
, parent
, link
, &block
->parents
) {
75 if (parent
->block
== this) {
84 bblock_t::is_successor_of(const bblock_t
*block
) const
86 foreach_list_typed_safe (bblock_link
, child
, link
, &block
->children
) {
87 if (child
->block
== this) {
96 ends_block(const backend_instruction
*inst
)
98 enum opcode op
= inst
->opcode
;
100 return op
== BRW_OPCODE_IF
||
101 op
== BRW_OPCODE_ELSE
||
102 op
== BRW_OPCODE_CONTINUE
||
103 op
== BRW_OPCODE_BREAK
||
104 op
== BRW_OPCODE_WHILE
;
108 starts_block(const backend_instruction
*inst
)
110 enum opcode op
= inst
->opcode
;
112 return op
== BRW_OPCODE_DO
||
113 op
== BRW_OPCODE_ENDIF
;
117 bblock_t::can_combine_with(const bblock_t
*that
) const
119 if ((const bblock_t
*)this->link
.next
!= that
)
122 if (ends_block(this->end
) ||
123 starts_block(that
->start
))
130 bblock_t::combine_with(bblock_t
*that
)
132 assert(this->can_combine_with(that
));
133 foreach_list_typed (bblock_link
, link
, link
, &this->children
) {
134 assert(link
->block
== that
);
136 foreach_list_typed (bblock_link
, link
, link
, &that
->parents
) {
137 assert(link
->block
== this);
140 this->end_ip
= that
->end_ip
;
141 this->end
= that
->end
;
142 this->else_block
= that
->else_block
;
144 this->cfg
->remove_block(that
);
148 bblock_t::dump(backend_visitor
*v
) const
150 int ip
= this->start_ip
;
151 for (backend_instruction
*inst
= (backend_instruction
*)this->start
;
152 inst
!= this->end
->next
;
153 inst
= (backend_instruction
*) inst
->next
) {
154 fprintf(stderr
, "%5d: ", ip
);
155 v
->dump_instruction(inst
);
160 cfg_t::cfg_t(exec_list
*instructions
)
162 mem_ctx
= ralloc_context(NULL
);
163 block_list
.make_empty();
167 bblock_t
*cur
= NULL
;
170 bblock_t
*entry
= new_block();
171 bblock_t
*cur_if
= NULL
; /**< BB ending with IF. */
172 bblock_t
*cur_else
= NULL
; /**< BB ending with ELSE. */
173 bblock_t
*cur_endif
= NULL
; /**< BB starting with ENDIF. */
174 bblock_t
*cur_do
= NULL
; /**< BB starting with DO. */
175 bblock_t
*cur_while
= NULL
; /**< BB immediately following WHILE. */
176 exec_list if_stack
, else_stack
, do_stack
, while_stack
;
179 set_next_block(&cur
, entry
, ip
);
181 entry
->start
= (backend_instruction
*) instructions
->get_head();
183 foreach_in_list(backend_instruction
, inst
, instructions
) {
186 /* set_next_block wants the post-incremented ip */
189 switch (inst
->opcode
) {
191 /* Push our information onto a stack so we can recover from
194 if_stack
.push_tail(link(mem_ctx
, cur_if
));
195 else_stack
.push_tail(link(mem_ctx
, cur_else
));
201 /* Set up our immediately following block, full of "then"
205 next
->start
= (backend_instruction
*)inst
->next
;
206 cur_if
->add_successor(mem_ctx
, next
);
208 set_next_block(&cur
, next
, ip
);
211 case BRW_OPCODE_ELSE
:
215 next
->start
= (backend_instruction
*)inst
->next
;
216 cur_if
->add_successor(mem_ctx
, next
);
218 set_next_block(&cur
, next
, ip
);
221 case BRW_OPCODE_ENDIF
: {
222 if (cur
->start
== inst
) {
223 /* New block was just created; use it. */
226 cur_endif
= new_block();
227 cur_endif
->start
= inst
;
229 cur
->end
= (backend_instruction
*)inst
->prev
;
230 cur
->add_successor(mem_ctx
, cur_endif
);
232 set_next_block(&cur
, cur_endif
, ip
- 1);
236 cur_else
->add_successor(mem_ctx
, cur_endif
);
238 cur_if
->add_successor(mem_ctx
, cur_endif
);
241 assert(cur_if
->end
->opcode
== BRW_OPCODE_IF
);
242 assert(!cur_else
|| cur_else
->end
->opcode
== BRW_OPCODE_ELSE
);
244 cur_if
->if_block
= cur_if
;
245 cur_if
->else_block
= cur_else
;
248 cur_else
->if_block
= cur_if
;
249 cur_else
->else_block
= cur_else
;
252 cur
->if_block
= cur_if
;
253 cur
->else_block
= cur_else
;
255 /* Pop the stack so we're in the previous if/else/endif */
256 cur_if
= pop_stack(&if_stack
);
257 cur_else
= pop_stack(&else_stack
);
261 /* Push our information onto a stack so we can recover from
264 do_stack
.push_tail(link(mem_ctx
, cur_do
));
265 while_stack
.push_tail(link(mem_ctx
, cur_while
));
267 /* Set up the block just after the while. Don't know when exactly
268 * it will start, yet.
270 cur_while
= new_block();
272 if (cur
->start
== inst
) {
273 /* New block was just created; use it. */
276 cur_do
= new_block();
277 cur_do
->start
= inst
;
279 cur
->end
= (backend_instruction
*)inst
->prev
;
280 cur
->add_successor(mem_ctx
, cur_do
);
282 set_next_block(&cur
, cur_do
, ip
- 1);
286 case BRW_OPCODE_CONTINUE
:
287 cur
->add_successor(mem_ctx
, cur_do
);
290 next
->start
= (backend_instruction
*)inst
->next
;
292 cur
->add_successor(mem_ctx
, next
);
294 set_next_block(&cur
, next
, ip
);
297 case BRW_OPCODE_BREAK
:
298 cur
->add_successor(mem_ctx
, cur_while
);
301 next
->start
= (backend_instruction
*)inst
->next
;
303 cur
->add_successor(mem_ctx
, next
);
305 set_next_block(&cur
, next
, ip
);
308 case BRW_OPCODE_WHILE
:
309 cur_while
->start
= (backend_instruction
*)inst
->next
;
311 cur
->add_successor(mem_ctx
, cur_do
);
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
);
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;
391 bblock_t
*block
= new(mem_ctx
) bblock_t(this);
397 cfg_t::set_next_block(bblock_t
**cur
, bblock_t
*block
, int ip
)
400 assert((*cur
)->end
->next
== block
->start
);
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_visitor
*v
) const
425 foreach_block (block
, this) {
426 fprintf(stderr
, "START B%d", block
->num
);
427 foreach_list_typed(bblock_link
, link
, link
, &block
->parents
) {
428 fprintf(stderr
, " <-B%d",
431 fprintf(stderr
, "\n");
433 fprintf(stderr
, "END B%d", block
->num
);
434 foreach_list_typed(bblock_link
, link
, link
, &block
->children
) {
435 fprintf(stderr
, " ->B%d",
438 fprintf(stderr
, "\n");