1 /*
2 * Copyright © 2012 Intel Corporation
3 *
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:
10 *
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
13 * Software.
14 *
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
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <[email protected]>
25 *
26 */
27
28 #ifndef ELK_CFG_H
29 #define ELK_CFG_H
30
31 #include "elk_ir.h"
32 #ifdef __cplusplus
33 #include "elk_ir_analysis.h"
34 #endif
35
36 struct elk_bblock_t;
37
38 /**
39 * CFG edge types.
40 *
41 * A logical edge represents a potential control flow path of the original
42 * scalar program, while a physical edge represents a control flow path that
43 * may not have existed in the original program but was introduced during
44 * vectorization in order to implement divergent control flow of different
45 * shader invocations within the same SIMD thread.
46 *
47 * All logical edges in the CFG are considered to be physical edges but not
48 * the other way around -- I.e. the logical CFG is a subset of the physical
49 * one.
50 */
51 enum bblock_link_kind {
52 bblock_link_logical = 0,
53 bblock_link_physical
54 };
55
56 struct elk_bblock_link {
57 #ifdef __cplusplus
58 DECLARE_RALLOC_CXX_OPERATORS(elk_bblock_link)
59
elk_bblock_linkelk_bblock_link60 elk_bblock_link(elk_bblock_t *block, enum bblock_link_kind kind)
61 : block(block), kind(kind)
62 {
63 }
64 #endif
65
66 struct exec_node link;
67 struct elk_bblock_t *block;
68
69 /* Type of this CFG edge. Because bblock_link_logical also implies
70 * bblock_link_physical, the proper way to test for membership of edge 'l'
71 * in CFG kind 'k' is 'l.kind <= k'.
72 */
73 enum bblock_link_kind kind;
74 };
75
76 struct elk_backend_shader;
77 struct elk_cfg_t;
78
79 struct elk_bblock_t {
80 #ifdef __cplusplus
81 DECLARE_RALLOC_CXX_OPERATORS(elk_bblock_t)
82
83 explicit elk_bblock_t(elk_cfg_t *cfg);
84
85 void add_successor(void *mem_ctx, elk_bblock_t *successor,
86 enum bblock_link_kind kind);
87 bool is_predecessor_of(const elk_bblock_t *block,
88 enum bblock_link_kind kind) const;
89 bool is_successor_of(const elk_bblock_t *block,
90 enum bblock_link_kind kind) const;
91 bool can_combine_with(const elk_bblock_t *that) const;
92 void combine_with(elk_bblock_t *that);
93 void dump(FILE *file = stderr) const;
94
95 elk_backend_instruction *start();
96 const elk_backend_instruction *start() const;
97 elk_backend_instruction *end();
98 const elk_backend_instruction *end() const;
99
100 elk_bblock_t *next();
101 const elk_bblock_t *next() const;
102 elk_bblock_t *prev();
103 const elk_bblock_t *prev() const;
104
105 bool starts_with_control_flow() const;
106 bool ends_with_control_flow() const;
107
108 elk_backend_instruction *first_non_control_flow_inst();
109 elk_backend_instruction *last_non_control_flow_inst();
110
111 private:
112 /**
113 * \sa unlink_parents, unlink_children
114 */
115 void unlink_list(exec_list *);
116
117 public:
unlink_parentselk_bblock_t118 void unlink_parents()
119 {
120 unlink_list(&parents);
121 }
122
unlink_childrenelk_bblock_t123 void unlink_children()
124 {
125 unlink_list(&children);
126 }
127 #endif
128
129 struct exec_node link;
130 struct elk_cfg_t *cfg;
131
132 int start_ip;
133 int end_ip;
134
135 /**
136 * Change in end_ip since the last time IPs of later blocks were updated.
137 */
138 int end_ip_delta;
139
140 struct exec_list instructions;
141 struct exec_list parents;
142 struct exec_list children;
143 int num;
144 };
145
146 static inline struct elk_backend_instruction *
bblock_start(struct elk_bblock_t * block)147 bblock_start(struct elk_bblock_t *block)
148 {
149 return (struct elk_backend_instruction *)exec_list_get_head(&block->instructions);
150 }
151
152 static inline const struct elk_backend_instruction *
bblock_start_const(const struct elk_bblock_t * block)153 bblock_start_const(const struct elk_bblock_t *block)
154 {
155 return (const struct elk_backend_instruction *)exec_list_get_head_const(&block->instructions);
156 }
157
158 static inline struct elk_backend_instruction *
bblock_end(struct elk_bblock_t * block)159 bblock_end(struct elk_bblock_t *block)
160 {
161 return (struct elk_backend_instruction *)exec_list_get_tail(&block->instructions);
162 }
163
164 static inline const struct elk_backend_instruction *
bblock_end_const(const struct elk_bblock_t * block)165 bblock_end_const(const struct elk_bblock_t *block)
166 {
167 return (const struct elk_backend_instruction *)exec_list_get_tail_const(&block->instructions);
168 }
169
170 static inline struct elk_bblock_t *
bblock_next(struct elk_bblock_t * block)171 bblock_next(struct elk_bblock_t *block)
172 {
173 if (exec_node_is_tail_sentinel(block->link.next))
174 return NULL;
175
176 return (struct elk_bblock_t *)block->link.next;
177 }
178
179 static inline const struct elk_bblock_t *
bblock_next_const(const struct elk_bblock_t * block)180 bblock_next_const(const struct elk_bblock_t *block)
181 {
182 if (exec_node_is_tail_sentinel(block->link.next))
183 return NULL;
184
185 return (const struct elk_bblock_t *)block->link.next;
186 }
187
188 static inline struct elk_bblock_t *
bblock_prev(struct elk_bblock_t * block)189 bblock_prev(struct elk_bblock_t *block)
190 {
191 if (exec_node_is_head_sentinel(block->link.prev))
192 return NULL;
193
194 return (struct elk_bblock_t *)block->link.prev;
195 }
196
197 static inline const struct elk_bblock_t *
bblock_prev_const(const struct elk_bblock_t * block)198 bblock_prev_const(const struct elk_bblock_t *block)
199 {
200 if (exec_node_is_head_sentinel(block->link.prev))
201 return NULL;
202
203 return (const struct elk_bblock_t *)block->link.prev;
204 }
205
206 static inline bool
bblock_starts_with_control_flow(const struct elk_bblock_t * block)207 bblock_starts_with_control_flow(const struct elk_bblock_t *block)
208 {
209 enum elk_opcode op = bblock_start_const(block)->opcode;
210 return op == ELK_OPCODE_DO || op == ELK_OPCODE_ENDIF;
211 }
212
213 static inline bool
bblock_ends_with_control_flow(const struct elk_bblock_t * block)214 bblock_ends_with_control_flow(const struct elk_bblock_t *block)
215 {
216 enum elk_opcode op = bblock_end_const(block)->opcode;
217 return op == ELK_OPCODE_IF ||
218 op == ELK_OPCODE_ELSE ||
219 op == ELK_OPCODE_WHILE ||
220 op == ELK_OPCODE_BREAK ||
221 op == ELK_OPCODE_CONTINUE;
222 }
223
224 static inline struct elk_backend_instruction *
bblock_first_non_control_flow_inst(struct elk_bblock_t * block)225 bblock_first_non_control_flow_inst(struct elk_bblock_t *block)
226 {
227 struct elk_backend_instruction *inst = bblock_start(block);
228 if (bblock_starts_with_control_flow(block))
229 #ifdef __cplusplus
230 inst = (struct elk_backend_instruction *)inst->next;
231 #else
232 inst = (struct elk_backend_instruction *)inst->link.next;
233 #endif
234 return inst;
235 }
236
237 static inline struct elk_backend_instruction *
bblock_last_non_control_flow_inst(struct elk_bblock_t * block)238 bblock_last_non_control_flow_inst(struct elk_bblock_t *block)
239 {
240 struct elk_backend_instruction *inst = bblock_end(block);
241 if (bblock_ends_with_control_flow(block))
242 #ifdef __cplusplus
243 inst = (struct elk_backend_instruction *)inst->prev;
244 #else
245 inst = (struct elk_backend_instruction *)inst->link.prev;
246 #endif
247 return inst;
248 }
249
250 #ifdef __cplusplus
251 inline elk_backend_instruction *
start()252 elk_bblock_t::start()
253 {
254 return bblock_start(this);
255 }
256
257 inline const elk_backend_instruction *
start()258 elk_bblock_t::start() const
259 {
260 return bblock_start_const(this);
261 }
262
263 inline elk_backend_instruction *
end()264 elk_bblock_t::end()
265 {
266 return bblock_end(this);
267 }
268
269 inline const elk_backend_instruction *
end()270 elk_bblock_t::end() const
271 {
272 return bblock_end_const(this);
273 }
274
275 inline elk_bblock_t *
next()276 elk_bblock_t::next()
277 {
278 return bblock_next(this);
279 }
280
281 inline const elk_bblock_t *
next()282 elk_bblock_t::next() const
283 {
284 return bblock_next_const(this);
285 }
286
287 inline elk_bblock_t *
prev()288 elk_bblock_t::prev()
289 {
290 return bblock_prev(this);
291 }
292
293 inline const elk_bblock_t *
prev()294 elk_bblock_t::prev() const
295 {
296 return bblock_prev_const(this);
297 }
298
299 inline bool
starts_with_control_flow()300 elk_bblock_t::starts_with_control_flow() const
301 {
302 return bblock_starts_with_control_flow(this);
303 }
304
305 inline bool
ends_with_control_flow()306 elk_bblock_t::ends_with_control_flow() const
307 {
308 return bblock_ends_with_control_flow(this);
309 }
310
311 inline elk_backend_instruction *
first_non_control_flow_inst()312 elk_bblock_t::first_non_control_flow_inst()
313 {
314 return bblock_first_non_control_flow_inst(this);
315 }
316
317 inline elk_backend_instruction *
last_non_control_flow_inst()318 elk_bblock_t::last_non_control_flow_inst()
319 {
320 return bblock_last_non_control_flow_inst(this);
321 }
322 #endif
323
324 struct elk_cfg_t {
325 #ifdef __cplusplus
326 DECLARE_RALLOC_CXX_OPERATORS(elk_cfg_t)
327
328 elk_cfg_t(const elk_backend_shader *s, exec_list *instructions);
329 elk_cfg_t(const elk_cfg_t &) = delete;
330 ~elk_cfg_t();
331
332 elk_cfg_t & operator=(const elk_cfg_t &) = delete;
333
334 void remove_block(elk_bblock_t *block);
335
336 elk_bblock_t *first_block();
337 const elk_bblock_t *first_block() const;
338 elk_bblock_t *last_block();
339 const elk_bblock_t *last_block() const;
340
341 elk_bblock_t *new_block();
342 void set_next_block(elk_bblock_t **cur, elk_bblock_t *block, int ip);
343 void make_block_array();
344
345 void dump(FILE *file = stderr);
346 void dump_cfg();
347
348 #ifdef NDEBUG
validateelk_cfg_t349 void validate(UNUSED const char *stage_abbrev) { }
350 #else
351 void validate(const char *stage_abbrev);
352 #endif
353
354 /**
355 * Propagate elk_bblock_t::end_ip_delta data through the CFG.
356 */
357 inline void adjust_block_ips();
358
359 #endif
360 const struct elk_backend_shader *s;
361 void *mem_ctx;
362
363 /** Ordered list (by ip) of basic blocks */
364 struct exec_list block_list;
365 struct elk_bblock_t **blocks;
366 int num_blocks;
367 };
368
369 static inline struct elk_bblock_t *
cfg_first_block(struct elk_cfg_t * cfg)370 cfg_first_block(struct elk_cfg_t *cfg)
371 {
372 return (struct elk_bblock_t *)exec_list_get_head(&cfg->block_list);
373 }
374
375 static inline const struct elk_bblock_t *
cfg_first_block_const(const struct elk_cfg_t * cfg)376 cfg_first_block_const(const struct elk_cfg_t *cfg)
377 {
378 return (const struct elk_bblock_t *)exec_list_get_head_const(&cfg->block_list);
379 }
380
381 static inline struct elk_bblock_t *
cfg_last_block(struct elk_cfg_t * cfg)382 cfg_last_block(struct elk_cfg_t *cfg)
383 {
384 return (struct elk_bblock_t *)exec_list_get_tail(&cfg->block_list);
385 }
386
387 static inline const struct elk_bblock_t *
cfg_last_block_const(const struct elk_cfg_t * cfg)388 cfg_last_block_const(const struct elk_cfg_t *cfg)
389 {
390 return (const struct elk_bblock_t *)exec_list_get_tail_const(&cfg->block_list);
391 }
392
393 #ifdef __cplusplus
394 inline elk_bblock_t *
first_block()395 elk_cfg_t::first_block()
396 {
397 return cfg_first_block(this);
398 }
399
400 const inline elk_bblock_t *
first_block()401 elk_cfg_t::first_block() const
402 {
403 return cfg_first_block_const(this);
404 }
405
406 inline elk_bblock_t *
last_block()407 elk_cfg_t::last_block()
408 {
409 return cfg_last_block(this);
410 }
411
412 const inline elk_bblock_t *
last_block()413 elk_cfg_t::last_block() const
414 {
415 return cfg_last_block_const(this);
416 }
417 #endif
418
419 /* Note that this is implemented with a double for loop -- break will
420 * break from the inner loop only!
421 */
422 #define foreach_block_and_inst(__block, __type, __inst, __cfg) \
423 foreach_block (__block, __cfg) \
424 foreach_inst_in_block (__type, __inst, __block)
425
426 /* Note that this is implemented with a double for loop -- break will
427 * break from the inner loop only!
428 */
429 #define foreach_block_and_inst_safe(__block, __type, __inst, __cfg) \
430 foreach_block_safe (__block, __cfg) \
431 foreach_inst_in_block_safe (__type, __inst, __block)
432
433 #define foreach_block(__block, __cfg) \
434 foreach_list_typed (elk_bblock_t, __block, link, &(__cfg)->block_list)
435
436 #define foreach_block_reverse(__block, __cfg) \
437 foreach_list_typed_reverse (elk_bblock_t, __block, link, &(__cfg)->block_list)
438
439 #define foreach_block_safe(__block, __cfg) \
440 foreach_list_typed_safe (elk_bblock_t, __block, link, &(__cfg)->block_list)
441
442 #define foreach_block_reverse_safe(__block, __cfg) \
443 foreach_list_typed_reverse_safe (elk_bblock_t, __block, link, &(__cfg)->block_list)
444
445 #define foreach_inst_in_block(__type, __inst, __block) \
446 foreach_in_list(__type, __inst, &(__block)->instructions)
447
448 #define foreach_inst_in_block_safe(__type, __inst, __block) \
449 for (__type *__inst = (__type *)__block->instructions.head_sentinel.next, \
450 *__next = (__type *)__inst->next; \
451 __next != NULL; \
452 __inst = __next, \
453 __next = (__type *)__next->next)
454
455 #define foreach_inst_in_block_reverse(__type, __inst, __block) \
456 foreach_in_list_reverse(__type, __inst, &(__block)->instructions)
457
458 #define foreach_inst_in_block_reverse_safe(__type, __inst, __block) \
459 foreach_in_list_reverse_safe(__type, __inst, &(__block)->instructions)
460
461 #define foreach_inst_in_block_starting_from(__type, __scan_inst, __inst) \
462 for (__type *__scan_inst = (__type *)__inst->next; \
463 !__scan_inst->is_tail_sentinel(); \
464 __scan_inst = (__type *)__scan_inst->next)
465
466 #define foreach_inst_in_block_reverse_starting_from(__type, __scan_inst, __inst) \
467 for (__type *__scan_inst = (__type *)__inst->prev; \
468 !__scan_inst->is_head_sentinel(); \
469 __scan_inst = (__type *)__scan_inst->prev)
470
471 #ifdef __cplusplus
472 inline void
adjust_block_ips()473 elk_cfg_t::adjust_block_ips()
474 {
475 int delta = 0;
476
477 foreach_block(block, this) {
478 block->start_ip += delta;
479 block->end_ip += delta;
480
481 delta += block->end_ip_delta;
482
483 block->end_ip_delta = 0;
484 }
485 }
486
487 namespace elk {
488 /**
489 * Immediate dominator tree analysis of a shader.
490 */
491 struct idom_tree {
492 idom_tree(const elk_backend_shader *s);
493 idom_tree(const idom_tree &) = delete;
494 ~idom_tree();
495 idom_tree & operator=(const idom_tree &) = delete;
496
497 bool
validateidom_tree498 validate(const elk_backend_shader *) const
499 {
500 /* FINISHME */
501 return true;
502 }
503
504 analysis_dependency_class
dependency_classidom_tree505 dependency_class() const
506 {
507 return DEPENDENCY_BLOCKS;
508 }
509
510 const elk_bblock_t *
parentidom_tree511 parent(const elk_bblock_t *b) const
512 {
513 assert(unsigned(b->num) < num_parents);
514 return parents[b->num];
515 }
516
517 elk_bblock_t *
parentidom_tree518 parent(elk_bblock_t *b) const
519 {
520 assert(unsigned(b->num) < num_parents);
521 return parents[b->num];
522 }
523
524 elk_bblock_t *
525 intersect(elk_bblock_t *b1, elk_bblock_t *b2) const;
526
527 void
528 dump() const;
529
530 private:
531 unsigned num_parents;
532 elk_bblock_t **parents;
533 };
534 }
535 #endif
536
537 #endif /* ELK_CFG_H */
538