7468c5bbde94a2a7b68baab6a9f7510b8b024805
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
) {
75 v
->dump_instruction(inst
);
80 cfg_t::cfg_t(backend_visitor
*v
)
82 create(v
->mem_ctx
, &v
->instructions
);
85 cfg_t::cfg_t(void *mem_ctx
, exec_list
*instructions
)
87 create(mem_ctx
, instructions
);
91 cfg_t::create(void *parent_mem_ctx
, exec_list
*instructions
)
93 mem_ctx
= ralloc_context(NULL
);
94 block_list
.make_empty();
100 bblock_t
*entry
= new_block();
101 bblock_t
*cur_if
= NULL
, *cur_else
= NULL
, *cur_endif
= NULL
;
102 bblock_t
*cur_do
= NULL
, *cur_while
= NULL
;
103 exec_list if_stack
, else_stack
, do_stack
, while_stack
;
106 set_next_block(entry
);
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(new(mem_ctx
) bblock_link(cur_if
));
124 else_stack
.push_tail(new(mem_ctx
) bblock_link(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(next
);
140 case BRW_OPCODE_ELSE
:
144 next
->start
= (backend_instruction
*)inst
->next
;
145 cur_if
->add_successor(mem_ctx
, next
);
147 set_next_block(next
);
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
);
162 set_next_block(cur_endif
);
166 backend_instruction
*else_inst
= NULL
;
168 else_inst
= (backend_instruction
*)cur_else
->end
;
170 cur_else
->add_successor(mem_ctx
, cur_endif
);
172 cur_if
->add_successor(mem_ctx
, cur_endif
);
175 assert(cur_if
->end
->opcode
== BRW_OPCODE_IF
);
176 assert(!else_inst
|| else_inst
->opcode
== BRW_OPCODE_ELSE
);
177 assert(inst
->opcode
== BRW_OPCODE_ENDIF
);
179 cur_if
->if_inst
= cur_if
->end
;
180 cur_if
->else_inst
= else_inst
;
181 cur_if
->endif_inst
= inst
;
184 cur_else
->if_inst
= cur_if
->end
;
185 cur_else
->else_inst
= else_inst
;
186 cur_else
->endif_inst
= inst
;
189 cur
->if_inst
= cur_if
->end
;
190 cur
->else_inst
= else_inst
;
191 cur
->endif_inst
= inst
;
193 /* Pop the stack so we're in the previous if/else/endif */
194 cur_if
= pop_stack(&if_stack
);
195 cur_else
= pop_stack(&else_stack
);
199 /* Push our information onto a stack so we can recover from
202 do_stack
.push_tail(new(mem_ctx
) bblock_link(cur_do
));
203 while_stack
.push_tail(new(mem_ctx
) bblock_link(cur_while
));
205 /* Set up the block just after the while. Don't know when exactly
206 * it will start, yet.
208 cur_while
= new_block();
210 /* Set up our immediately following block, full of "then"
214 next
->start
= (backend_instruction
*)inst
->next
;
215 cur
->add_successor(mem_ctx
, next
);
218 set_next_block(next
);
221 case BRW_OPCODE_CONTINUE
:
222 cur
->add_successor(mem_ctx
, cur_do
);
225 next
->start
= (backend_instruction
*)inst
->next
;
227 cur
->add_successor(mem_ctx
, next
);
229 set_next_block(next
);
232 case BRW_OPCODE_BREAK
:
233 cur
->add_successor(mem_ctx
, cur_while
);
236 next
->start
= (backend_instruction
*)inst
->next
;
238 cur
->add_successor(mem_ctx
, next
);
240 set_next_block(next
);
243 case BRW_OPCODE_WHILE
:
244 cur_while
->start
= (backend_instruction
*)inst
->next
;
246 cur
->add_successor(mem_ctx
, cur_do
);
247 set_next_block(cur_while
);
249 /* Pop the stack so we're in the previous loop */
250 cur_do
= pop_stack(&do_stack
);
251 cur_while
= pop_stack(&while_stack
);
266 ralloc_free(mem_ctx
);
272 bblock_t
*block
= new(mem_ctx
) bblock_t();
278 cfg_t::set_next_block(bblock_t
*block
)
281 assert(cur
->end
->next
== block
->start
);
282 cur
->end_ip
= ip
- 1;
285 block
->start_ip
= ip
;
286 block
->block_num
= num_blocks
++;
287 block_list
.push_tail(new(mem_ctx
) bblock_link(block
));
292 cfg_t::make_block_array()
294 blocks
= ralloc_array(mem_ctx
, bblock_t
*, num_blocks
);
297 foreach_list(block_node
, &block_list
) {
298 bblock_link
*link
= (bblock_link
*)block_node
;
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 printf("START B%d", b
);
310 foreach_list(node
, &block
->parents
) {
311 bblock_link
*link
= (bblock_link
*)node
;
312 printf(" <-B%d", link
->block
->block_num
);
316 printf("END B%d", b
);
317 foreach_list(node
, &block
->children
) {
318 bblock_link
*link
= (bblock_link
*)node
;
319 printf(" ->B%d", link
->block
->block_num
);