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>
29 #include "brw_vec4_live_variables.h"
33 /** @file brw_vec4_live_variables.cpp
35 * Support for computing at the basic block level which variables
36 * (virtual GRFs in our case) are live at entry and exit.
38 * See Muchnick's Advanced Compiler Design and Implementation, section
43 * Sets up the use[] and def[] arrays.
45 * The basic-block-level live variable analysis needs to know which
46 * variables get used before they're completely defined, and which
47 * variables are completely defined before they're used.
49 * We independently track each channel of a vec4. This is because we need to
50 * be able to recognize a sequence like:
55 * MUL result.xy tmp.xy e.xy
58 * as having tmp live only across that sequence (assuming it's used nowhere
59 * else), because it's a common pattern. A more conservative approach that
60 * doesn't get tmp marked a deffed in this block will tend to result in
64 vec4_live_variables::setup_def_use()
68 foreach_block (block
, cfg
) {
69 assert(ip
== block
->start_ip
);
71 assert(cfg
->blocks
[block
->num
- 1]->end_ip
== ip
- 1);
73 foreach_inst_in_block(vec4_instruction
, inst
, block
) {
74 struct block_data
*bd
= &block_data
[block
->num
];
76 /* Set use[] for this instruction */
77 for (unsigned int i
= 0; i
< 3; i
++) {
78 if (inst
->src
[i
].file
== VGRF
) {
79 for (unsigned j
= 0; j
< regs_read(inst
, i
); j
++) {
80 for (int c
= 0; c
< 4; c
++) {
82 var_from_reg(alloc
, offset(inst
->src
[i
], j
), c
);
83 if (!BITSET_TEST(bd
->def
, v
))
84 BITSET_SET(bd
->use
, v
);
89 for (unsigned c
= 0; c
< 4; c
++) {
90 if (inst
->reads_flag(c
) &&
91 !BITSET_TEST(bd
->flag_def
, c
)) {
92 BITSET_SET(bd
->flag_use
, c
);
96 /* Check for unconditional writes to whole registers. These
97 * are the things that screen off preceding definitions of a
98 * variable, and thus qualify for being in def[].
100 if (inst
->dst
.file
== VGRF
&&
101 (!inst
->predicate
|| inst
->opcode
== BRW_OPCODE_SEL
)) {
102 for (unsigned i
= 0; i
< regs_written(inst
); i
++) {
103 for (int c
= 0; c
< 4; c
++) {
104 if (inst
->dst
.writemask
& (1 << c
)) {
106 var_from_reg(alloc
, offset(inst
->dst
, i
), c
);
107 if (!BITSET_TEST(bd
->use
, v
))
108 BITSET_SET(bd
->def
, v
);
113 if (inst
->writes_flag()) {
114 for (unsigned c
= 0; c
< 4; c
++) {
115 if ((inst
->dst
.writemask
& (1 << c
)) &&
116 !BITSET_TEST(bd
->flag_use
, c
)) {
117 BITSET_SET(bd
->flag_def
, c
);
128 * The algorithm incrementally sets bits in liveout and livein,
129 * propagating it through control flow. It will eventually terminate
130 * because it only ever adds bits, and stops when no bits are added in
134 vec4_live_variables::compute_live_variables()
141 foreach_block_reverse (block
, cfg
) {
142 struct block_data
*bd
= &block_data
[block
->num
];
145 foreach_list_typed(bblock_link
, child_link
, link
, &block
->children
) {
146 struct block_data
*child_bd
= &block_data
[child_link
->block
->num
];
148 for (int i
= 0; i
< bitset_words
; i
++) {
149 BITSET_WORD new_liveout
= (child_bd
->livein
[i
] &
152 bd
->liveout
[i
] |= new_liveout
;
156 BITSET_WORD new_liveout
= (child_bd
->flag_livein
[0] &
157 ~bd
->flag_liveout
[0]);
159 bd
->flag_liveout
[0] |= new_liveout
;
165 for (int i
= 0; i
< bitset_words
; i
++) {
166 BITSET_WORD new_livein
= (bd
->use
[i
] |
169 if (new_livein
& ~bd
->livein
[i
]) {
170 bd
->livein
[i
] |= new_livein
;
174 BITSET_WORD new_livein
= (bd
->flag_use
[0] |
175 (bd
->flag_liveout
[0] &
177 if (new_livein
& ~bd
->flag_livein
[0]) {
178 bd
->flag_livein
[0] |= new_livein
;
185 vec4_live_variables::vec4_live_variables(const simple_allocator
&alloc
,
187 : alloc(alloc
), cfg(cfg
)
189 mem_ctx
= ralloc_context(NULL
);
191 num_vars
= alloc
.total_size
* 4;
192 block_data
= rzalloc_array(mem_ctx
, struct block_data
, cfg
->num_blocks
);
194 bitset_words
= BITSET_WORDS(num_vars
);
195 for (int i
= 0; i
< cfg
->num_blocks
; i
++) {
196 block_data
[i
].def
= rzalloc_array(mem_ctx
, BITSET_WORD
, bitset_words
);
197 block_data
[i
].use
= rzalloc_array(mem_ctx
, BITSET_WORD
, bitset_words
);
198 block_data
[i
].livein
= rzalloc_array(mem_ctx
, BITSET_WORD
, bitset_words
);
199 block_data
[i
].liveout
= rzalloc_array(mem_ctx
, BITSET_WORD
, bitset_words
);
201 block_data
[i
].flag_def
[0] = 0;
202 block_data
[i
].flag_use
[0] = 0;
203 block_data
[i
].flag_livein
[0] = 0;
204 block_data
[i
].flag_liveout
[0] = 0;
208 compute_live_variables();
211 vec4_live_variables::~vec4_live_variables()
213 ralloc_free(mem_ctx
);
216 #define MAX_INSTRUCTION (1 << 30)
219 * Computes a conservative start/end of the live intervals for each virtual GRF.
221 * We could expose per-channel live intervals to the consumer based on the
222 * information we computed in vec4_live_variables, except that our only
223 * current user is virtual_grf_interferes(). So we instead union the
224 * per-channel ranges into a per-vgrf range for virtual_grf_start[] and
227 * We could potentially have virtual_grf_interferes() do the test per-channel,
228 * which would let some interesting register allocation occur (particularly on
229 * code-generated GLSL sequences from the Cg compiler which does register
230 * allocation at the GLSL level and thus reuses components of the variable
231 * with distinct lifetimes). But right now the complexity of doing so doesn't
232 * seem worth it, since having virtual_grf_interferes() be cheap is important
233 * for register allocation performance.
236 vec4_visitor::calculate_live_intervals()
238 if (this->live_intervals
)
241 int *start
= ralloc_array(mem_ctx
, int, this->alloc
.total_size
* 4);
242 int *end
= ralloc_array(mem_ctx
, int, this->alloc
.total_size
* 4);
243 ralloc_free(this->virtual_grf_start
);
244 ralloc_free(this->virtual_grf_end
);
245 this->virtual_grf_start
= start
;
246 this->virtual_grf_end
= end
;
248 for (unsigned i
= 0; i
< this->alloc
.total_size
* 4; i
++) {
249 start
[i
] = MAX_INSTRUCTION
;
253 /* Start by setting up the intervals with no knowledge of control
257 foreach_block_and_inst(block
, vec4_instruction
, inst
, cfg
) {
258 for (unsigned int i
= 0; i
< 3; i
++) {
259 if (inst
->src
[i
].file
== VGRF
) {
260 for (unsigned j
= 0; j
< regs_read(inst
, i
); j
++) {
261 for (int c
= 0; c
< 4; c
++) {
263 var_from_reg(alloc
, offset(inst
->src
[i
], j
), c
);
264 start
[v
] = MIN2(start
[v
], ip
);
271 if (inst
->dst
.file
== VGRF
) {
272 for (unsigned i
= 0; i
< regs_written(inst
); i
++) {
273 for (int c
= 0; c
< 4; c
++) {
274 if (inst
->dst
.writemask
& (1 << c
)) {
276 var_from_reg(alloc
, offset(inst
->dst
, i
), c
);
277 start
[v
] = MIN2(start
[v
], ip
);
287 /* Now, extend those intervals using our analysis of control flow.
289 * The control flow-aware analysis was done at a channel level, while at
290 * this point we're distilling it down to vgrfs.
292 this->live_intervals
= new(mem_ctx
) vec4_live_variables(alloc
, cfg
);
294 foreach_block (block
, cfg
) {
295 struct block_data
*bd
= &live_intervals
->block_data
[block
->num
];
297 for (int i
= 0; i
< live_intervals
->num_vars
; i
++) {
298 if (BITSET_TEST(bd
->livein
, i
)) {
299 start
[i
] = MIN2(start
[i
], block
->start_ip
);
300 end
[i
] = MAX2(end
[i
], block
->start_ip
);
303 if (BITSET_TEST(bd
->liveout
, i
)) {
304 start
[i
] = MIN2(start
[i
], block
->end_ip
);
305 end
[i
] = MAX2(end
[i
], block
->end_ip
);
312 vec4_visitor::invalidate_live_intervals()
314 ralloc_free(live_intervals
);
315 live_intervals
= NULL
;
319 vec4_visitor::var_range_start(unsigned v
, unsigned n
) const
323 for (unsigned i
= 0; i
< n
; i
++)
324 start
= MIN2(start
, virtual_grf_start
[v
+ i
]);
330 vec4_visitor::var_range_end(unsigned v
, unsigned n
) const
334 for (unsigned i
= 0; i
< n
; i
++)
335 end
= MAX2(end
, virtual_grf_end
[v
+ i
]);
341 vec4_visitor::virtual_grf_interferes(int a
, int b
)
343 return !((var_range_end(4 * alloc
.offsets
[a
], 4 * alloc
.sizes
[a
]) <=
344 var_range_start(4 * alloc
.offsets
[b
], 4 * alloc
.sizes
[b
])) ||
345 (var_range_end(4 * alloc
.offsets
[b
], 4 * alloc
.sizes
[b
]) <=
346 var_range_start(4 * alloc
.offsets
[a
], 4 * alloc
.sizes
[a
])));