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
;
46 bblock_t::bblock_t() :
47 start_ip(0), end_ip(0), block_num(0)
53 children
.make_empty();
61 bblock_t::add_successor(void *mem_ctx
, bblock_t
*successor
)
63 successor
->parents
.push_tail(new(mem_ctx
) bblock_link(this));
64 children
.push_tail(new(mem_ctx
) bblock_link(successor
));
68 bblock_t::dump(backend_visitor
*v
)
70 int ip
= this->start_ip
;
71 for (backend_instruction
*inst
= (backend_instruction
*)this->start
;
72 inst
!= this->end
->next
;
73 inst
= (backend_instruction
*) inst
->next
) {
74 fprintf(stderr
, "%5d: ", ip
);
75 v
->dump_instruction(inst
);
80 cfg_t::cfg_t(exec_list
*instructions
)
82 mem_ctx
= ralloc_context(NULL
);
83 block_list
.make_empty();
90 bblock_t
*entry
= new_block();
91 bblock_t
*cur_if
= NULL
; /**< BB ending with IF. */
92 bblock_t
*cur_else
= NULL
; /**< BB ending with ELSE. */
93 bblock_t
*cur_endif
= NULL
; /**< BB starting with ENDIF. */
94 bblock_t
*cur_do
= NULL
; /**< BB ending with DO. */
95 bblock_t
*cur_while
= NULL
; /**< BB immediately following WHILE. */
96 exec_list if_stack
, else_stack
, do_stack
, while_stack
;
99 set_next_block(&cur
, entry
, ip
);
101 entry
->start
= (backend_instruction
*) instructions
->get_head();
103 foreach_list(node
, instructions
) {
104 backend_instruction
*inst
= (backend_instruction
*)node
;
108 /* set_next_block wants the post-incremented ip */
111 switch (inst
->opcode
) {
113 /* Push our information onto a stack so we can recover from
116 if_stack
.push_tail(new(mem_ctx
) bblock_link(cur_if
));
117 else_stack
.push_tail(new(mem_ctx
) bblock_link(cur_else
));
123 /* Set up our immediately following block, full of "then"
127 next
->start
= (backend_instruction
*)inst
->next
;
128 cur_if
->add_successor(mem_ctx
, next
);
130 set_next_block(&cur
, next
, ip
);
133 case BRW_OPCODE_ELSE
:
137 next
->start
= (backend_instruction
*)inst
->next
;
138 cur_if
->add_successor(mem_ctx
, next
);
140 set_next_block(&cur
, next
, ip
);
143 case BRW_OPCODE_ENDIF
: {
144 if (cur
->start
== inst
) {
145 /* New block was just created; use it. */
148 cur_endif
= new_block();
149 cur_endif
->start
= inst
;
151 cur
->end
= (backend_instruction
*)inst
->prev
;
152 cur
->add_successor(mem_ctx
, cur_endif
);
154 set_next_block(&cur
, cur_endif
, ip
- 1);
157 backend_instruction
*else_inst
= NULL
;
159 else_inst
= (backend_instruction
*)cur_else
->end
;
161 cur_else
->add_successor(mem_ctx
, cur_endif
);
163 cur_if
->add_successor(mem_ctx
, cur_endif
);
166 assert(cur_if
->end
->opcode
== BRW_OPCODE_IF
);
167 assert(!else_inst
|| else_inst
->opcode
== BRW_OPCODE_ELSE
);
168 assert(inst
->opcode
== BRW_OPCODE_ENDIF
);
170 cur_if
->if_inst
= cur_if
->end
;
171 cur_if
->else_inst
= else_inst
;
172 cur_if
->endif_inst
= inst
;
175 cur_else
->if_inst
= cur_if
->end
;
176 cur_else
->else_inst
= else_inst
;
177 cur_else
->endif_inst
= inst
;
180 cur
->if_inst
= cur_if
->end
;
181 cur
->else_inst
= else_inst
;
182 cur
->endif_inst
= inst
;
184 /* Pop the stack so we're in the previous if/else/endif */
185 cur_if
= pop_stack(&if_stack
);
186 cur_else
= pop_stack(&else_stack
);
190 /* Push our information onto a stack so we can recover from
193 do_stack
.push_tail(new(mem_ctx
) bblock_link(cur_do
));
194 while_stack
.push_tail(new(mem_ctx
) bblock_link(cur_while
));
196 /* Set up the block just after the while. Don't know when exactly
197 * it will start, yet.
199 cur_while
= new_block();
201 /* Set up our immediately following block, full of "then"
205 next
->start
= (backend_instruction
*)inst
->next
;
206 cur
->add_successor(mem_ctx
, next
);
209 set_next_block(&cur
, next
, ip
);
212 case BRW_OPCODE_CONTINUE
:
213 cur
->add_successor(mem_ctx
, cur_do
);
216 next
->start
= (backend_instruction
*)inst
->next
;
218 cur
->add_successor(mem_ctx
, next
);
220 set_next_block(&cur
, next
, ip
);
223 case BRW_OPCODE_BREAK
:
224 cur
->add_successor(mem_ctx
, cur_while
);
227 next
->start
= (backend_instruction
*)inst
->next
;
229 cur
->add_successor(mem_ctx
, next
);
231 set_next_block(&cur
, next
, ip
);
234 case BRW_OPCODE_WHILE
:
235 cur_while
->start
= (backend_instruction
*)inst
->next
;
237 cur
->add_successor(mem_ctx
, cur_do
);
238 set_next_block(&cur
, cur_while
, ip
);
240 /* Pop the stack so we're in the previous loop */
241 cur_do
= pop_stack(&do_stack
);
242 cur_while
= pop_stack(&while_stack
);
257 ralloc_free(mem_ctx
);
263 bblock_t
*block
= new(mem_ctx
) bblock_t();
269 cfg_t::set_next_block(bblock_t
**cur
, bblock_t
*block
, int ip
)
272 assert((*cur
)->end
->next
== block
->start
);
273 (*cur
)->end_ip
= ip
- 1;
276 block
->start_ip
= ip
;
277 block
->block_num
= num_blocks
++;
278 block_list
.push_tail(new(mem_ctx
) bblock_link(block
));
283 cfg_t::make_block_array()
285 blocks
= ralloc_array(mem_ctx
, bblock_t
*, num_blocks
);
288 foreach_list(block_node
, &block_list
) {
289 bblock_link
*link
= (bblock_link
*)block_node
;
290 blocks
[i
++] = link
->block
;
292 assert(i
== num_blocks
);
296 cfg_t::dump(backend_visitor
*v
)
298 for (int b
= 0; b
< this->num_blocks
; b
++) {
299 bblock_t
*block
= this->blocks
[b
];
300 fprintf(stderr
, "START B%d", b
);
301 foreach_list(node
, &block
->parents
) {
302 bblock_link
*link
= (bblock_link
*)node
;
303 fprintf(stderr
, " <-B%d",
304 link
->block
->block_num
);
306 fprintf(stderr
, "\n");
308 fprintf(stderr
, "END B%d", b
);
309 foreach_list(node
, &block
->children
) {
310 bblock_link
*link
= (bblock_link
*)node
;
311 fprintf(stderr
, " ->B%d",
312 link
->block
->block_num
);
314 fprintf(stderr
, "\n");