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(fs_visitor
*v
)
71 mem_ctx
= ralloc_context(v
->mem_ctx
);
72 block_list
.make_empty();
77 bblock_t
*entry
= new_block();
78 bblock_t
*cur_if
= NULL
, *cur_else
= NULL
, *cur_endif
= NULL
;
79 bblock_t
*cur_do
= NULL
, *cur_while
= NULL
;
80 exec_list if_stack
, else_stack
, endif_stack
, do_stack
, while_stack
;
83 set_next_block(entry
);
85 entry
->start
= (fs_inst
*)v
->instructions
.get_head();
87 foreach_list(node
, &v
->instructions
) {
88 fs_inst
*inst
= (fs_inst
*)node
;
92 /* set_next_block wants the post-incremented ip */
95 switch (inst
->opcode
) {
97 /* Push our information onto a stack so we can recover from
100 if_stack
.push_tail(cur_if
->make_list(mem_ctx
));
101 else_stack
.push_tail(cur_else
->make_list(mem_ctx
));
102 endif_stack
.push_tail(cur_endif
->make_list(mem_ctx
));
106 /* Set up the block just after the endif. Don't know when exactly
107 * it will start, yet.
109 cur_endif
= new_block();
111 /* Set up our immediately following block, full of "then"
115 next
->start
= (fs_inst
*)inst
->next
;
116 cur_if
->add_successor(mem_ctx
, next
);
118 set_next_block(next
);
121 case BRW_OPCODE_ELSE
:
122 cur
->add_successor(mem_ctx
, cur_endif
);
125 next
->start
= (fs_inst
*)inst
->next
;
126 cur_if
->add_successor(mem_ctx
, next
);
129 set_next_block(next
);
132 case BRW_OPCODE_ENDIF
:
133 cur_endif
->start
= (fs_inst
*)inst
->next
;
134 cur
->add_successor(mem_ctx
, cur_endif
);
135 set_next_block(cur_endif
);
138 cur_if
->add_successor(mem_ctx
, cur_endif
);
140 /* Pop the stack so we're in the previous if/else/endif */
141 cur_if
= pop_stack(&if_stack
);
142 cur_else
= pop_stack(&else_stack
);
143 cur_endif
= pop_stack(&endif_stack
);
147 /* Push our information onto a stack so we can recover from
150 do_stack
.push_tail(cur_do
->make_list(mem_ctx
));
151 while_stack
.push_tail(cur_while
->make_list(mem_ctx
));
153 /* Set up the block just after the while. Don't know when exactly
154 * it will start, yet.
156 cur_while
= new_block();
158 /* Set up our immediately following block, full of "then"
162 next
->start
= (fs_inst
*)inst
->next
;
163 cur
->add_successor(mem_ctx
, next
);
166 set_next_block(next
);
169 case BRW_OPCODE_CONTINUE
:
170 cur
->add_successor(mem_ctx
, cur_do
);
173 next
->start
= (fs_inst
*)inst
->next
;
174 if (inst
->predicated
)
175 cur
->add_successor(mem_ctx
, next
);
177 set_next_block(next
);
180 case BRW_OPCODE_BREAK
:
181 cur
->add_successor(mem_ctx
, cur_while
);
184 next
->start
= (fs_inst
*)inst
->next
;
185 if (inst
->predicated
)
186 cur
->add_successor(mem_ctx
, next
);
188 set_next_block(next
);
191 case BRW_OPCODE_WHILE
:
192 cur_while
->start
= (fs_inst
*)inst
->next
;
194 cur
->add_successor(mem_ctx
, cur_do
);
195 set_next_block(cur_while
);
197 /* Pop the stack so we're in the previous loop */
198 cur_do
= pop_stack(&do_stack
);
199 cur_while
= pop_stack(&while_stack
);
214 ralloc_free(mem_ctx
);
220 bblock_t
*block
= new(mem_ctx
) bblock_t();
226 cfg_t::set_next_block(bblock_t
*block
)
229 assert(cur
->end
->next
== block
->start
);
230 cur
->end_ip
= ip
- 1;
233 block
->start_ip
= ip
;
234 block
->block_num
= num_blocks
++;
235 block_list
.push_tail(block
->make_list(mem_ctx
));
240 cfg_t::make_block_array()
242 blocks
= ralloc_array(mem_ctx
, bblock_t
*, num_blocks
);
245 foreach_list(block_node
, &block_list
) {
246 bblock_link
*link
= (bblock_link
*)block_node
;
247 blocks
[i
++] = link
->block
;
249 assert(i
== num_blocks
);