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>
33 * Walks the shader instructions generated and creates a set of basic
34 * blocks with successor/predecessor edges connecting them.
38 pop_stack(exec_list
*list
)
40 bblock_link
*link
= (bblock_link
*)list
->get_tail();
41 bblock_t
*block
= link
->block
;
47 bblock_t::bblock_t() :
48 start_ip(0), end_ip(0), block_num(0)
54 children
.make_empty();
62 bblock_t::add_successor(void *mem_ctx
, bblock_t
*successor
)
64 successor
->parents
.push_tail(this->make_list(mem_ctx
));
65 children
.push_tail(successor
->make_list(mem_ctx
));
69 bblock_t::make_list(void *mem_ctx
)
71 return new(mem_ctx
) bblock_link(this);
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
) {
82 v
->dump_instruction(inst
);
87 cfg_t::cfg_t(backend_visitor
*v
)
89 create(v
->mem_ctx
, &v
->instructions
);
92 cfg_t::cfg_t(void *mem_ctx
, exec_list
*instructions
)
94 create(mem_ctx
, instructions
);
98 cfg_t::create(void *parent_mem_ctx
, exec_list
*instructions
)
100 mem_ctx
= ralloc_context(NULL
);
101 block_list
.make_empty();
107 bblock_t
*entry
= new_block();
108 bblock_t
*cur_if
= NULL
, *cur_else
= NULL
, *cur_endif
= NULL
;
109 bblock_t
*cur_do
= NULL
, *cur_while
= NULL
;
110 exec_list if_stack
, else_stack
, endif_stack
, do_stack
, while_stack
;
113 set_next_block(entry
);
115 entry
->start
= (backend_instruction
*) instructions
->get_head();
117 foreach_list(node
, instructions
) {
118 backend_instruction
*inst
= (backend_instruction
*)node
;
122 /* set_next_block wants the post-incremented ip */
125 switch (inst
->opcode
) {
127 /* Push our information onto a stack so we can recover from
130 if_stack
.push_tail(cur_if
->make_list(mem_ctx
));
131 else_stack
.push_tail(cur_else
->make_list(mem_ctx
));
132 endif_stack
.push_tail(cur_endif
->make_list(mem_ctx
));
136 /* Set up the block just after the endif. Don't know when exactly
137 * it will start, yet.
139 cur_endif
= new_block();
141 /* Set up our immediately following block, full of "then"
145 next
->start
= (backend_instruction
*)inst
->next
;
146 cur_if
->add_successor(mem_ctx
, next
);
148 set_next_block(next
);
151 case BRW_OPCODE_ELSE
:
152 cur
->add_successor(mem_ctx
, cur_endif
);
155 next
->start
= (backend_instruction
*)inst
->next
;
156 cur_if
->add_successor(mem_ctx
, next
);
159 set_next_block(next
);
162 case BRW_OPCODE_ENDIF
: {
163 cur_endif
->start
= (backend_instruction
*)inst
->next
;
164 cur
->add_successor(mem_ctx
, cur_endif
);
165 set_next_block(cur_endif
);
168 cur_if
->add_successor(mem_ctx
, cur_endif
);
170 backend_instruction
*else_inst
= cur_else
?
171 (backend_instruction
*) cur_else
->start
->prev
: NULL
;
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
);
194 cur_endif
= pop_stack(&endif_stack
);
198 /* Push our information onto a stack so we can recover from
201 do_stack
.push_tail(cur_do
->make_list(mem_ctx
));
202 while_stack
.push_tail(cur_while
->make_list(mem_ctx
));
204 /* Set up the block just after the while. Don't know when exactly
205 * it will start, yet.
207 cur_while
= new_block();
209 /* Set up our immediately following block, full of "then"
213 next
->start
= (backend_instruction
*)inst
->next
;
214 cur
->add_successor(mem_ctx
, next
);
217 set_next_block(next
);
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(next
);
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(next
);
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_while
);
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
);
265 ralloc_free(mem_ctx
);
271 bblock_t
*block
= new(mem_ctx
) bblock_t();
277 cfg_t::set_next_block(bblock_t
*block
)
280 assert(cur
->end
->next
== block
->start
);
281 cur
->end_ip
= ip
- 1;
284 block
->start_ip
= ip
;
285 block
->block_num
= num_blocks
++;
286 block_list
.push_tail(block
->make_list(mem_ctx
));
291 cfg_t::make_block_array()
293 blocks
= ralloc_array(mem_ctx
, bblock_t
*, num_blocks
);
296 foreach_list(block_node
, &block_list
) {
297 bblock_link
*link
= (bblock_link
*)block_node
;
298 blocks
[i
++] = link
->block
;
300 assert(i
== num_blocks
);
304 cfg_t::dump(backend_visitor
*v
)
306 for (int b
= 0; b
< this->num_blocks
; b
++) {
307 bblock_t
*block
= this->blocks
[b
];
308 printf("START B%d", b
);
309 foreach_list(node
, &block
->parents
) {
310 bblock_link
*link
= (bblock_link
*)node
;
311 printf(" <-B%d", link
->block
->block_num
);
315 printf("END B%d", b
);
316 foreach_list(node
, &block
->children
) {
317 bblock_link
*link
= (bblock_link
*)node
;
318 printf(" ->B%d", link
->block
->block_num
);