i965/cfg: Point to bblock_t containing associated control flow
[mesa.git] / src / mesa / drivers / dri / i965 / brw_cfg.h
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 <eric@anholt.net>
25 *
26 */
27
28 #pragma once
29 #ifndef BRW_CFG_H
30 #define BRW_CFG_H
31
32 #include "brw_shader.h"
33
34 struct bblock_t;
35
36 struct bblock_link {
37 #ifdef __cplusplus
38 DECLARE_RALLOC_CXX_OPERATORS(bblock_link)
39
40 bblock_link(bblock_t *block)
41 : block(block)
42 {
43 }
44 #endif
45
46 struct exec_node link;
47 struct bblock_t *block;
48 };
49
50 #ifndef __cplusplus
51 struct backend_instruction;
52 #endif
53
54 struct bblock_t {
55 #ifdef __cplusplus
56 DECLARE_RALLOC_CXX_OPERATORS(bblock_t)
57
58 explicit bblock_t(cfg_t *cfg);
59
60 void add_successor(void *mem_ctx, bblock_t *successor);
61 bool is_predecessor_of(const bblock_t *block) const;
62 bool is_successor_of(const bblock_t *block) const;
63 void dump(backend_visitor *v);
64 #endif
65
66 struct exec_node link;
67 struct cfg_t *cfg;
68
69 struct backend_instruction *start;
70 struct backend_instruction *end;
71
72 int start_ip;
73 int end_ip;
74
75 struct exec_list parents;
76 struct exec_list children;
77 int num;
78
79 /* If the current basic block ends in an IF or ELSE instruction, these will
80 * point to the basic blocks containing the other associated instruction.
81 *
82 * Otherwise they are NULL.
83 */
84 struct bblock_t *if_block;
85 struct bblock_t *else_block;
86 };
87
88 struct cfg_t {
89 #ifdef __cplusplus
90 DECLARE_RALLOC_CXX_OPERATORS(cfg_t)
91
92 cfg_t(exec_list *instructions);
93 ~cfg_t();
94
95 void remove_block(bblock_t *block);
96
97 bblock_t *new_block();
98 void set_next_block(bblock_t **cur, bblock_t *block, int ip);
99 void make_block_array();
100
101 void dump(backend_visitor *v);
102 #endif
103 void *mem_ctx;
104
105 /** Ordered list (by ip) of basic blocks */
106 struct exec_list block_list;
107 struct bblock_t **blocks;
108 int num_blocks;
109 };
110
111 #define foreach_block_and_inst(__block, __type, __inst, __cfg) \
112 foreach_block (__block, __cfg) \
113 foreach_inst_in_block (__type, __inst, __block)
114
115 #define foreach_block_and_inst_safe(__block, __type, __inst, __cfg) \
116 foreach_block_safe (__block, __cfg) \
117 foreach_inst_in_block_safe (__type, __inst, __block)
118
119 #define foreach_block(__block, __cfg) \
120 foreach_list_typed (bblock_t, __block, link, &(__cfg)->block_list)
121
122 #define foreach_block_safe(__block, __cfg) \
123 foreach_list_typed_safe (bblock_t, __block, link, &(__cfg)->block_list)
124
125 #define foreach_inst_in_block(__type, __inst, __block) \
126 for (__type *__inst = (__type *)__block->start; \
127 __inst != __block->end->next; \
128 __inst = (__type *)__inst->next)
129
130 #define foreach_inst_in_block_safe(__type, __inst, __block) \
131 for (__type *__inst = (__type *)__block->start, \
132 *__next = (__type *)__inst->next, \
133 *__end = (__type *)__block->end->next->next; \
134 __next != __end; \
135 __inst = __next, \
136 __next = (__type *)__next->next)
137
138 #define foreach_inst_in_block_reverse(__type, __inst, __block) \
139 for (__type *__inst = (__type *)__block->end; \
140 __inst != __block->start->prev; \
141 __inst = (__type *)__inst->prev)
142
143 #endif /* BRW_CFG_H */