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_list(node
, instructions
) {
111 backend_instruction
*inst
= (backend_instruction
*)node
;
115 /* set_next_block wants the post-incremented ip */
118 switch (inst
->opcode
) {
120 /* Push our information onto a stack so we can recover from
123 if_stack
.push_tail(link(mem_ctx
, cur_if
));
124 else_stack
.push_tail(link(mem_ctx
, cur_else
));
130 /* Set up our immediately following block, full of "then"
134 next
->start
= (backend_instruction
*)inst
->next
;
135 cur_if
->add_successor(mem_ctx
, next
);
137 set_next_block(&cur
, next
, ip
);
140 case BRW_OPCODE_ELSE
:
144 next
->start
= (backend_instruction
*)inst
->next
;
145 cur_if
->add_successor(mem_ctx
, next
);
147 set_next_block(&cur
, next
, ip
);
150 case BRW_OPCODE_ENDIF
: {
151 if (cur
->start
== inst
) {
152 /* New block was just created; use it. */
155 cur_endif
= new_block();
156 cur_endif
->start
= inst
;
158 cur
->end
= (backend_instruction
*)inst
->prev
;
159 cur
->add_successor(mem_ctx
, cur_endif
);
161 set_next_block(&cur
, cur_endif
, ip
- 1);
164 backend_instruction
*else_inst
= NULL
;
166 else_inst
= (backend_instruction
*)cur_else
->end
;
168 cur_else
->add_successor(mem_ctx
, cur_endif
);
170 cur_if
->add_successor(mem_ctx
, cur_endif
);
173 assert(cur_if
->end
->opcode
== BRW_OPCODE_IF
);
174 assert(!else_inst
|| else_inst
->opcode
== BRW_OPCODE_ELSE
);
175 assert(inst
->opcode
== BRW_OPCODE_ENDIF
);
177 cur_if
->if_inst
= cur_if
->end
;
178 cur_if
->else_inst
= else_inst
;
179 cur_if
->endif_inst
= inst
;
182 cur_else
->if_inst
= cur_if
->end
;
183 cur_else
->else_inst
= else_inst
;
184 cur_else
->endif_inst
= inst
;
187 cur
->if_inst
= cur_if
->end
;
188 cur
->else_inst
= else_inst
;
189 cur
->endif_inst
= inst
;
191 /* Pop the stack so we're in the previous if/else/endif */
192 cur_if
= pop_stack(&if_stack
);
193 cur_else
= pop_stack(&else_stack
);
197 /* Push our information onto a stack so we can recover from
200 do_stack
.push_tail(link(mem_ctx
, cur_do
));
201 while_stack
.push_tail(link(mem_ctx
, cur_while
));
203 /* Set up the block just after the while. Don't know when exactly
204 * it will start, yet.
206 cur_while
= new_block();
208 if (cur
->start
== inst
) {
209 /* New block was just created; use it. */
212 cur_do
= new_block();
213 cur_do
->start
= inst
;
215 cur
->end
= (backend_instruction
*)inst
->prev
;
216 cur
->add_successor(mem_ctx
, cur_do
);
218 set_next_block(&cur
, cur_do
, ip
- 1);
222 case BRW_OPCODE_CONTINUE
:
223 cur
->add_successor(mem_ctx
, cur_do
);
226 next
->start
= (backend_instruction
*)inst
->next
;
228 cur
->add_successor(mem_ctx
, next
);
230 set_next_block(&cur
, next
, ip
);
233 case BRW_OPCODE_BREAK
:
234 cur
->add_successor(mem_ctx
, cur_while
);
237 next
->start
= (backend_instruction
*)inst
->next
;
239 cur
->add_successor(mem_ctx
, next
);
241 set_next_block(&cur
, next
, ip
);
244 case BRW_OPCODE_WHILE
:
245 cur_while
->start
= (backend_instruction
*)inst
->next
;
247 cur
->add_successor(mem_ctx
, cur_do
);
248 set_next_block(&cur
, cur_while
, ip
);
250 /* Pop the stack so we're in the previous loop */
251 cur_do
= pop_stack(&do_stack
);
252 cur_while
= pop_stack(&while_stack
);
269 ralloc_free(mem_ctx
);
275 bblock_t
*block
= new(mem_ctx
) bblock_t();
281 cfg_t::set_next_block(bblock_t
**cur
, bblock_t
*block
, int ip
)
284 assert((*cur
)->end
->next
== block
->start
);
285 (*cur
)->end_ip
= ip
- 1;
288 block
->start_ip
= ip
;
289 block
->block_num
= num_blocks
++;
290 block_list
.push_tail(link(mem_ctx
, block
));
295 cfg_t::make_block_array()
297 blocks
= ralloc_array(mem_ctx
, bblock_t
*, num_blocks
);
300 foreach_list_typed(bblock_link
, link
, link
, &block_list
) {
301 blocks
[i
++] = link
->block
;
303 assert(i
== num_blocks
);
307 cfg_t::dump(backend_visitor
*v
)
309 for (int b
= 0; b
< this->num_blocks
; b
++) {
310 bblock_t
*block
= this->blocks
[b
];
311 fprintf(stderr
, "START B%d", b
);
312 foreach_list_typed(bblock_link
, link
, link
, &block
->parents
) {
313 fprintf(stderr
, " <-B%d",
314 link
->block
->block_num
);
316 fprintf(stderr
, "\n");
318 fprintf(stderr
, "END B%d", b
);
319 foreach_list_typed(bblock_link
, link
, link
, &block
->children
) {
320 fprintf(stderr
, " ->B%d",
321 link
->block
->block_num
);
323 fprintf(stderr
, "\n");