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 #include "brw_shader.h"
38 DECLARE_RALLOC_CXX_OPERATORS(bblock_link
)
40 bblock_link(bblock_t
*block
)
46 struct exec_node link
;
47 struct bblock_t
*block
;
50 struct backend_instruction
;
54 DECLARE_RALLOC_CXX_OPERATORS(bblock_t
)
56 explicit bblock_t(cfg_t
*cfg
);
58 void add_successor(void *mem_ctx
, bblock_t
*successor
);
59 bool is_predecessor_of(const bblock_t
*block
) const;
60 bool is_successor_of(const bblock_t
*block
) const;
61 bool can_combine_with(const bblock_t
*that
) const;
62 void combine_with(bblock_t
*that
);
63 void dump(backend_shader
*s
) const;
65 backend_instruction
*start();
66 const backend_instruction
*start() const;
67 backend_instruction
*end();
68 const backend_instruction
*end() const;
71 const bblock_t
*next() const;
73 const bblock_t
*prev() const;
75 bool starts_with_control_flow() const;
76 bool ends_with_control_flow() const;
78 backend_instruction
*first_non_control_flow_inst();
79 backend_instruction
*last_non_control_flow_inst();
82 struct exec_node link
;
84 struct bblock_t
*idom
;
89 struct exec_list instructions
;
90 struct exec_list parents
;
91 struct exec_list children
;
97 static inline struct backend_instruction
*
98 bblock_start(struct bblock_t
*block
)
100 return (struct backend_instruction
*)exec_list_get_head(&block
->instructions
);
103 static inline const struct backend_instruction
*
104 bblock_start_const(const struct bblock_t
*block
)
106 return (const struct backend_instruction
*)exec_list_get_head_const(&block
->instructions
);
109 static inline struct backend_instruction
*
110 bblock_end(struct bblock_t
*block
)
112 return (struct backend_instruction
*)exec_list_get_tail(&block
->instructions
);
115 static inline const struct backend_instruction
*
116 bblock_end_const(const struct bblock_t
*block
)
118 return (const struct backend_instruction
*)exec_list_get_tail_const(&block
->instructions
);
121 static inline struct bblock_t
*
122 bblock_next(struct bblock_t
*block
)
124 return (struct bblock_t
*)block
->link
.next
;
127 static inline const struct bblock_t
*
128 bblock_next_const(const struct bblock_t
*block
)
130 return (const struct bblock_t
*)block
->link
.next
;
133 static inline struct bblock_t
*
134 bblock_prev(struct bblock_t
*block
)
136 return (struct bblock_t
*)block
->link
.prev
;
139 static inline const struct bblock_t
*
140 bblock_prev_const(const struct bblock_t
*block
)
142 return (const struct bblock_t
*)block
->link
.prev
;
146 bblock_starts_with_control_flow(const struct bblock_t
*block
)
148 enum opcode op
= bblock_start_const(block
)->opcode
;
149 return op
== BRW_OPCODE_DO
|| op
== BRW_OPCODE_ENDIF
;
153 bblock_ends_with_control_flow(const struct bblock_t
*block
)
155 enum opcode op
= bblock_end_const(block
)->opcode
;
156 return op
== BRW_OPCODE_IF
||
157 op
== BRW_OPCODE_ELSE
||
158 op
== BRW_OPCODE_WHILE
||
159 op
== BRW_OPCODE_BREAK
||
160 op
== BRW_OPCODE_CONTINUE
;
163 static inline struct backend_instruction
*
164 bblock_first_non_control_flow_inst(struct bblock_t
*block
)
166 struct backend_instruction
*inst
= bblock_start(block
);
167 if (bblock_starts_with_control_flow(block
))
169 inst
= (struct backend_instruction
*)inst
->next
;
171 inst
= (struct backend_instruction
*)inst
->link
.next
;
176 static inline struct backend_instruction
*
177 bblock_last_non_control_flow_inst(struct bblock_t
*block
)
179 struct backend_instruction
*inst
= bblock_end(block
);
180 if (bblock_ends_with_control_flow(block
))
182 inst
= (struct backend_instruction
*)inst
->prev
;
184 inst
= (struct backend_instruction
*)inst
->link
.prev
;
190 inline backend_instruction
*
193 return bblock_start(this);
196 inline const backend_instruction
*
197 bblock_t::start() const
199 return bblock_start_const(this);
202 inline backend_instruction
*
205 return bblock_end(this);
208 inline const backend_instruction
*
209 bblock_t::end() const
211 return bblock_end_const(this);
217 return bblock_next(this);
220 inline const bblock_t
*
221 bblock_t::next() const
223 return bblock_next_const(this);
229 return bblock_prev(this);
232 inline const bblock_t
*
233 bblock_t::prev() const
235 return bblock_prev_const(this);
239 bblock_t::starts_with_control_flow() const
241 return bblock_starts_with_control_flow(this);
245 bblock_t::ends_with_control_flow() const
247 return bblock_ends_with_control_flow(this);
250 inline backend_instruction
*
251 bblock_t::first_non_control_flow_inst()
253 return bblock_first_non_control_flow_inst(this);
256 inline backend_instruction
*
257 bblock_t::last_non_control_flow_inst()
259 return bblock_last_non_control_flow_inst(this);
265 DECLARE_RALLOC_CXX_OPERATORS(cfg_t
)
267 cfg_t(exec_list
*instructions
);
270 void remove_block(bblock_t
*block
);
272 bblock_t
*new_block();
273 void set_next_block(bblock_t
**cur
, bblock_t
*block
, int ip
);
274 void make_block_array();
275 void calculate_idom();
276 static bblock_t
*intersect(bblock_t
*b1
, bblock_t
*b2
);
278 void dump(backend_shader
*s
);
284 /** Ordered list (by ip) of basic blocks */
285 struct exec_list block_list
;
286 struct bblock_t
**blocks
;
291 unsigned cycle_count
;
294 /* Note that this is implemented with a double for loop -- break will
295 * break from the inner loop only!
297 #define foreach_block_and_inst(__block, __type, __inst, __cfg) \
298 foreach_block (__block, __cfg) \
299 foreach_inst_in_block (__type, __inst, __block)
301 /* Note that this is implemented with a double for loop -- break will
302 * break from the inner loop only!
304 #define foreach_block_and_inst_safe(__block, __type, __inst, __cfg) \
305 foreach_block_safe (__block, __cfg) \
306 foreach_inst_in_block_safe (__type, __inst, __block)
308 #define foreach_block(__block, __cfg) \
309 foreach_list_typed (bblock_t, __block, link, &(__cfg)->block_list)
311 #define foreach_block_reverse(__block, __cfg) \
312 foreach_list_typed_reverse (bblock_t, __block, link, &(__cfg)->block_list)
314 #define foreach_block_safe(__block, __cfg) \
315 foreach_list_typed_safe (bblock_t, __block, link, &(__cfg)->block_list)
317 #define foreach_block_reverse_safe(__block, __cfg) \
318 foreach_list_typed_reverse_safe (bblock_t, __block, link, &(__cfg)->block_list)
320 #define foreach_inst_in_block(__type, __inst, __block) \
321 foreach_in_list(__type, __inst, &(__block)->instructions)
323 #define foreach_inst_in_block_safe(__type, __inst, __block) \
324 for (__type *__inst = (__type *)__block->instructions.head, \
325 *__next = (__type *)__inst->next, \
326 *__end = (__type *)__block->instructions.tail; \
329 __next = (__type *)__next->next)
331 #define foreach_inst_in_block_reverse(__type, __inst, __block) \
332 foreach_in_list_reverse(__type, __inst, &(__block)->instructions)
334 #define foreach_inst_in_block_reverse_safe(__type, __inst, __block) \
335 foreach_in_list_reverse_safe(__type, __inst, &(__block)->instructions)
337 #define foreach_inst_in_block_starting_from(__type, __scan_inst, __inst) \
338 for (__type *__scan_inst = (__type *)__inst->next; \
339 !__scan_inst->is_tail_sentinel(); \
340 __scan_inst = (__type *)__scan_inst->next)
342 #define foreach_inst_in_block_reverse_starting_from(__type, __scan_inst, __inst) \
343 for (__type *__scan_inst = (__type *)__inst->prev; \
344 !__scan_inst->is_head_sentinel(); \
345 __scan_inst = (__type *)__scan_inst->prev)
347 #endif /* BRW_CFG_H */