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() :
54 start_ip(0), end_ip(0), block_num(0)
60 children
.make_empty();
68 bblock_t::add_successor(void *mem_ctx
, bblock_t
*successor
)
70 successor
->parents
.push_tail(link(mem_ctx
, this));
71 children
.push_tail(link(mem_ctx
, successor
));
75 bblock_t::dump(backend_visitor
*v
)
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
);
87 cfg_t::cfg_t(exec_list
*instructions
)
89 mem_ctx
= ralloc_context(NULL
);
90 block_list
.make_empty();
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
;
106 set_next_block(&cur
, entry
, ip
);
108 entry
->start
= (backend_instruction
*) instructions
->get_head();
110 foreach_in_list(backend_instruction
, inst
, instructions
) {
113 /* set_next_block wants the post-incremented ip */
116 switch (inst
->opcode
) {
118 /* Push our information onto a stack so we can recover from
121 if_stack
.push_tail(link(mem_ctx
, cur_if
));
122 else_stack
.push_tail(link(mem_ctx
, cur_else
));
128 /* Set up our immediately following block, full of "then"
132 next
->start
= (backend_instruction
*)inst
->next
;
133 cur_if
->add_successor(mem_ctx
, next
);
135 set_next_block(&cur
, next
, ip
);
138 case BRW_OPCODE_ELSE
:
142 next
->start
= (backend_instruction
*)inst
->next
;
143 cur_if
->add_successor(mem_ctx
, next
);
145 set_next_block(&cur
, next
, ip
);
148 case BRW_OPCODE_ENDIF
: {
149 if (cur
->start
== inst
) {
150 /* New block was just created; use it. */
153 cur_endif
= new_block();
154 cur_endif
->start
= inst
;
156 cur
->end
= (backend_instruction
*)inst
->prev
;
157 cur
->add_successor(mem_ctx
, cur_endif
);
159 set_next_block(&cur
, cur_endif
, ip
- 1);
162 backend_instruction
*else_inst
= NULL
;
164 else_inst
= (backend_instruction
*)cur_else
->end
;
166 cur_else
->add_successor(mem_ctx
, cur_endif
);
168 cur_if
->add_successor(mem_ctx
, cur_endif
);
171 assert(cur_if
->end
->opcode
== BRW_OPCODE_IF
);
172 assert(!else_inst
|| else_inst
->opcode
== BRW_OPCODE_ELSE
);
173 assert(inst
->opcode
== BRW_OPCODE_ENDIF
);
175 cur_if
->if_inst
= cur_if
->end
;
176 cur_if
->else_inst
= else_inst
;
177 cur_if
->endif_inst
= inst
;
180 cur_else
->if_inst
= cur_if
->end
;
181 cur_else
->else_inst
= else_inst
;
182 cur_else
->endif_inst
= inst
;
185 cur
->if_inst
= cur_if
->end
;
186 cur
->else_inst
= else_inst
;
187 cur
->endif_inst
= inst
;
189 /* Pop the stack so we're in the previous if/else/endif */
190 cur_if
= pop_stack(&if_stack
);
191 cur_else
= pop_stack(&else_stack
);
195 /* Push our information onto a stack so we can recover from
198 do_stack
.push_tail(link(mem_ctx
, cur_do
));
199 while_stack
.push_tail(link(mem_ctx
, cur_while
));
201 /* Set up the block just after the while. Don't know when exactly
202 * it will start, yet.
204 cur_while
= new_block();
206 if (cur
->start
== inst
) {
207 /* New block was just created; use it. */
210 cur_do
= new_block();
211 cur_do
->start
= inst
;
213 cur
->end
= (backend_instruction
*)inst
->prev
;
214 cur
->add_successor(mem_ctx
, cur_do
);
216 set_next_block(&cur
, cur_do
, ip
- 1);
220 case BRW_OPCODE_CONTINUE
:
221 cur
->add_successor(mem_ctx
, cur_do
);
224 next
->start
= (backend_instruction
*)inst
->next
;
226 cur
->add_successor(mem_ctx
, next
);
228 set_next_block(&cur
, next
, ip
);
231 case BRW_OPCODE_BREAK
:
232 cur
->add_successor(mem_ctx
, cur_while
);
235 next
->start
= (backend_instruction
*)inst
->next
;
237 cur
->add_successor(mem_ctx
, next
);
239 set_next_block(&cur
, next
, ip
);
242 case BRW_OPCODE_WHILE
:
243 cur_while
->start
= (backend_instruction
*)inst
->next
;
245 cur
->add_successor(mem_ctx
, cur_do
);
246 set_next_block(&cur
, cur_while
, ip
);
248 /* Pop the stack so we're in the previous loop */
249 cur_do
= pop_stack(&do_stack
);
250 cur_while
= pop_stack(&while_stack
);
267 ralloc_free(mem_ctx
);
273 bblock_t
*block
= new(mem_ctx
) bblock_t();
279 cfg_t::set_next_block(bblock_t
**cur
, bblock_t
*block
, int ip
)
282 assert((*cur
)->end
->next
== block
->start
);
283 (*cur
)->end_ip
= ip
- 1;
286 block
->start_ip
= ip
;
287 block
->block_num
= num_blocks
++;
288 block_list
.push_tail(link(mem_ctx
, block
));
293 cfg_t::make_block_array()
295 blocks
= ralloc_array(mem_ctx
, bblock_t
*, num_blocks
);
298 foreach_list_typed(bblock_link
, link
, link
, &block_list
) {
299 blocks
[i
++] = link
->block
;
301 assert(i
== num_blocks
);
305 cfg_t::dump(backend_visitor
*v
)
307 for (int b
= 0; b
< this->num_blocks
; b
++) {
308 bblock_t
*block
= this->blocks
[b
];
309 fprintf(stderr
, "START B%d", b
);
310 foreach_list_typed(bblock_link
, link
, link
, &block
->parents
) {
311 fprintf(stderr
, " <-B%d",
312 link
->block
->block_num
);
314 fprintf(stderr
, "\n");
316 fprintf(stderr
, "END B%d", b
);
317 foreach_list_typed(bblock_link
, link
, link
, &block
->children
) {
318 fprintf(stderr
, " ->B%d",
319 link
->block
->block_num
);
321 fprintf(stderr
, "\n");