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>
31 /** @file brw_cfg_t.cpp
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
;
53 children
.make_empty();
57 bblock_t::add_successor(void *mem_ctx
, bblock_t
*successor
)
59 successor
->parents
.push_tail(this->make_list(mem_ctx
));
60 children
.push_tail(successor
->make_list(mem_ctx
));
64 bblock_t::make_list(void *mem_ctx
)
66 return new(mem_ctx
) bblock_link(this);
69 cfg_t::cfg_t(backend_visitor
*v
)
71 create(v
->mem_ctx
, &v
->instructions
);
74 cfg_t::cfg_t(void *mem_ctx
, exec_list
*instructions
)
76 create(mem_ctx
, instructions
);
80 cfg_t::create(void *parent_mem_ctx
, exec_list
*instructions
)
82 mem_ctx
= ralloc_context(parent_mem_ctx
);
83 block_list
.make_empty();
88 bblock_t
*entry
= new_block();
89 bblock_t
*cur_if
= NULL
, *cur_else
= NULL
, *cur_endif
= NULL
;
90 bblock_t
*cur_do
= NULL
, *cur_while
= NULL
;
91 exec_list if_stack
, else_stack
, endif_stack
, do_stack
, while_stack
;
94 set_next_block(entry
);
96 entry
->start
= (backend_instruction
*) instructions
->get_head();
98 foreach_list(node
, instructions
) {
99 backend_instruction
*inst
= (backend_instruction
*)node
;
103 /* set_next_block wants the post-incremented ip */
106 switch (inst
->opcode
) {
108 /* Push our information onto a stack so we can recover from
111 if_stack
.push_tail(cur_if
->make_list(mem_ctx
));
112 else_stack
.push_tail(cur_else
->make_list(mem_ctx
));
113 endif_stack
.push_tail(cur_endif
->make_list(mem_ctx
));
117 /* Set up the block just after the endif. Don't know when exactly
118 * it will start, yet.
120 cur_endif
= new_block();
122 /* Set up our immediately following block, full of "then"
126 next
->start
= (backend_instruction
*)inst
->next
;
127 cur_if
->add_successor(mem_ctx
, next
);
129 set_next_block(next
);
132 case BRW_OPCODE_ELSE
:
133 cur
->add_successor(mem_ctx
, cur_endif
);
136 next
->start
= (backend_instruction
*)inst
->next
;
137 cur_if
->add_successor(mem_ctx
, next
);
140 set_next_block(next
);
143 case BRW_OPCODE_ENDIF
:
144 cur_endif
->start
= (backend_instruction
*)inst
->next
;
145 cur
->add_successor(mem_ctx
, cur_endif
);
146 set_next_block(cur_endif
);
149 cur_if
->add_successor(mem_ctx
, cur_endif
);
151 /* Pop the stack so we're in the previous if/else/endif */
152 cur_if
= pop_stack(&if_stack
);
153 cur_else
= pop_stack(&else_stack
);
154 cur_endif
= pop_stack(&endif_stack
);
158 /* Push our information onto a stack so we can recover from
161 do_stack
.push_tail(cur_do
->make_list(mem_ctx
));
162 while_stack
.push_tail(cur_while
->make_list(mem_ctx
));
164 /* Set up the block just after the while. Don't know when exactly
165 * it will start, yet.
167 cur_while
= new_block();
169 /* Set up our immediately following block, full of "then"
173 next
->start
= (backend_instruction
*)inst
->next
;
174 cur
->add_successor(mem_ctx
, next
);
177 set_next_block(next
);
180 case BRW_OPCODE_CONTINUE
:
181 cur
->add_successor(mem_ctx
, cur_do
);
184 next
->start
= (backend_instruction
*)inst
->next
;
186 cur
->add_successor(mem_ctx
, next
);
188 set_next_block(next
);
191 case BRW_OPCODE_BREAK
:
192 cur
->add_successor(mem_ctx
, cur_while
);
195 next
->start
= (backend_instruction
*)inst
->next
;
197 cur
->add_successor(mem_ctx
, next
);
199 set_next_block(next
);
202 case BRW_OPCODE_WHILE
:
203 cur_while
->start
= (backend_instruction
*)inst
->next
;
205 cur
->add_successor(mem_ctx
, cur_do
);
206 set_next_block(cur_while
);
208 /* Pop the stack so we're in the previous loop */
209 cur_do
= pop_stack(&do_stack
);
210 cur_while
= pop_stack(&while_stack
);
225 ralloc_free(mem_ctx
);
231 bblock_t
*block
= new(mem_ctx
) bblock_t();
237 cfg_t::set_next_block(bblock_t
*block
)
240 assert(cur
->end
->next
== block
->start
);
241 cur
->end_ip
= ip
- 1;
244 block
->start_ip
= ip
;
245 block
->block_num
= num_blocks
++;
246 block_list
.push_tail(block
->make_list(mem_ctx
));
251 cfg_t::make_block_array()
253 blocks
= ralloc_array(mem_ctx
, bblock_t
*, num_blocks
);
256 foreach_list(block_node
, &block_list
) {
257 bblock_link
*link
= (bblock_link
*)block_node
;
258 blocks
[i
++] = link
->block
;
260 assert(i
== num_blocks
);