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_fs_live_variables.h"
33 /** @file brw_fs_live_variables.cpp
35 * Support for calculating liveness information about virtual GRFs.
37 * This produces a live interval for each whole virtual GRF. We could
38 * choose to expose per-component live intervals for VGRFs of size > 1,
39 * but we currently do not. It is easier for the consumers of this
40 * information to work with whole VGRFs.
42 * However, we internally track use/def information at the per-component
43 * (reg_offset) level for greater accuracy. Large VGRFs may be accessed
44 * piecemeal over many (possibly non-adjacent) instructions. In this case,
45 * examining a single instruction is insufficient to decide whether a whole
46 * VGRF is ultimately used or defined. Tracking individual components
47 * allows us to easily assemble this information.
49 * See Muchnik's Advanced Compiler Design and Implementation, section
54 fs_live_variables::setup_one_read(bblock_t
*block
, fs_inst
*inst
,
57 int var
= var_from_vgrf
[reg
.reg
] + reg
.reg_offset
;
59 /* The use[] bitset marks when the block makes use of a variable (VGRF
60 * channel) without having completely defined that variable within the
63 if (!BITSET_TEST(bd
[block
->block_num
].def
, var
))
64 BITSET_SET(bd
[block
->block_num
].use
, var
);
68 fs_live_variables::setup_one_write(bblock_t
*block
, fs_inst
*inst
,
71 int var
= var_from_vgrf
[reg
.reg
] + reg
.reg_offset
;
73 /* The def[] bitset marks when an initialization in a block completely
74 * screens off previous updates of that variable (VGRF channel).
76 if (inst
->dst
.file
== GRF
&& !inst
->is_partial_write()) {
77 if (!BITSET_TEST(bd
[block
->block_num
].use
, var
))
78 BITSET_SET(bd
[block
->block_num
].def
, var
);
83 * Sets up the use[] and def[] bitsets.
85 * The basic-block-level live variable analysis needs to know which
86 * variables get used before they're completely defined, and which
87 * variables are completely defined before they're used.
89 * These are tracked at the per-component level, rather than whole VGRFs.
92 fs_live_variables::setup_def_use()
96 for (int b
= 0; b
< cfg
->num_blocks
; b
++) {
97 bblock_t
*block
= cfg
->blocks
[b
];
99 assert(ip
== block
->start_ip
);
101 assert(cfg
->blocks
[b
- 1]->end_ip
== ip
- 1);
103 for (fs_inst
*inst
= (fs_inst
*)block
->start
;
104 inst
!= block
->end
->next
;
105 inst
= (fs_inst
*)inst
->next
) {
107 /* Set use[] for this instruction */
108 for (unsigned int i
= 0; i
< 3; i
++) {
109 fs_reg reg
= inst
->src
[i
];
115 /* We don't know how many components are read in a send-from-grf,
116 * so just assume "all of them."
118 if (inst
->is_send_from_grf())
119 regs_read
= v
->virtual_grf_sizes
[reg
.reg
];
121 for (int i
= 0; i
< regs_read
; i
++) {
122 setup_one_read(block
, inst
, ip
, reg
);
127 /* Set def[] for this instruction */
128 if (inst
->dst
.file
== GRF
) {
129 fs_reg reg
= inst
->dst
;
130 for (int j
= 0; j
< inst
->regs_written
; j
++) {
131 setup_one_write(block
, inst
, ip
, reg
);
142 * The algorithm incrementally sets bits in liveout and livein,
143 * propagating it through control flow. It will eventually terminate
144 * because it only ever adds bits, and stops when no bits are added in
148 fs_live_variables::compute_live_variables()
155 for (int b
= 0; b
< cfg
->num_blocks
; b
++) {
157 for (int i
= 0; i
< bitset_words
; i
++) {
158 BITSET_WORD new_livein
= (bd
[b
].use
[i
] |
159 (bd
[b
].liveout
[i
] & ~bd
[b
].def
[i
]));
160 if (new_livein
& ~bd
[b
].livein
[i
]) {
161 bd
[b
].livein
[i
] |= new_livein
;
167 foreach_list(block_node
, &cfg
->blocks
[b
]->children
) {
168 bblock_link
*link
= (bblock_link
*)block_node
;
169 bblock_t
*block
= link
->block
;
171 for (int i
= 0; i
< bitset_words
; i
++) {
172 BITSET_WORD new_liveout
= (bd
[block
->block_num
].livein
[i
] &
175 bd
[b
].liveout
[i
] |= new_liveout
;
184 fs_live_variables::fs_live_variables(fs_visitor
*v
, cfg_t
*cfg
)
187 mem_ctx
= ralloc_context(cfg
->mem_ctx
);
189 num_vgrfs
= v
->virtual_grf_count
;
191 var_from_vgrf
= rzalloc_array(mem_ctx
, int, num_vgrfs
);
192 for (int i
= 0; i
< num_vgrfs
; i
++) {
193 var_from_vgrf
[i
] = num_vars
;
194 num_vars
+= v
->virtual_grf_sizes
[i
];
197 vgrf_from_var
= rzalloc_array(mem_ctx
, int, num_vars
);
198 for (int i
= 0; i
< num_vgrfs
; i
++) {
199 for (int j
= 0; j
< v
->virtual_grf_sizes
[i
]; j
++) {
200 vgrf_from_var
[var_from_vgrf
[i
] + j
] = i
;
204 bd
= rzalloc_array(mem_ctx
, struct block_data
, cfg
->num_blocks
);
206 bitset_words
= BITSET_WORDS(num_vars
);
207 for (int i
= 0; i
< cfg
->num_blocks
; i
++) {
208 bd
[i
].def
= rzalloc_array(mem_ctx
, BITSET_WORD
, bitset_words
);
209 bd
[i
].use
= rzalloc_array(mem_ctx
, BITSET_WORD
, bitset_words
);
210 bd
[i
].livein
= rzalloc_array(mem_ctx
, BITSET_WORD
, bitset_words
);
211 bd
[i
].liveout
= rzalloc_array(mem_ctx
, BITSET_WORD
, bitset_words
);
215 compute_live_variables();
218 fs_live_variables::~fs_live_variables()
220 ralloc_free(mem_ctx
);
223 #define MAX_INSTRUCTION (1 << 30)
226 fs_visitor::invalidate_live_intervals()
228 this->live_intervals_valid
= false;
232 * Compute the live intervals for each virtual GRF.
234 * This uses the per-component use/def data, but combines it to produce
235 * information about whole VGRFs.
238 fs_visitor::calculate_live_intervals()
240 if (this->live_intervals_valid
)
243 int num_vgrfs
= this->virtual_grf_count
;
244 int *start
= ralloc_array(mem_ctx
, int, num_vgrfs
);
245 int *end
= ralloc_array(mem_ctx
, int, num_vgrfs
);
246 ralloc_free(this->virtual_grf_start
);
247 ralloc_free(this->virtual_grf_end
);
248 this->virtual_grf_start
= start
;
249 this->virtual_grf_end
= end
;
251 for (int i
= 0; i
< num_vgrfs
; i
++) {
252 start
[i
] = MAX_INSTRUCTION
;
256 /* Start by setting up the intervals with no knowledge of control
260 foreach_list(node
, &this->instructions
) {
261 fs_inst
*inst
= (fs_inst
*)node
;
263 for (unsigned int i
= 0; i
< 3; i
++) {
264 if (inst
->src
[i
].file
== GRF
) {
265 int reg
= inst
->src
[i
].reg
;
268 /* In most cases, a register can be written over safely by the
269 * same instruction that is its last use. For a single
270 * instruction, the sources are dereferenced before writing of the
271 * destination starts (naturally). This gets more complicated for
272 * simd16, because the instruction:
274 * mov(16) g4<1>F g4<8,8,1>F g6<8,8,1>F
276 * is actually decoded in hardware as:
278 * mov(8) g4<1>F g4<8,8,1>F g6<8,8,1>F
279 * mov(8) g5<1>F g5<8,8,1>F g7<8,8,1>F
281 * Which is safe. However, if we have uniform accesses
282 * happening, we get into trouble:
284 * mov(8) g4<1>F g4<0,1,0>F g6<8,8,1>F
285 * mov(8) g5<1>F g4<0,1,0>F g7<8,8,1>F
287 * Now our destination for the first instruction overwrote the
288 * second instruction's src0, and we get garbage for those 8
289 * pixels. There's a similar issue for the pre-gen6
290 * pixel_x/pixel_y, which are registers of 16-bit values and thus
291 * would get stomped by the first decode as well.
293 if (dispatch_width
== 16 && (inst
->src
[i
].smear
>= 0 ||
294 (this->pixel_x
.reg
== reg
||
295 this->pixel_y
.reg
== reg
))) {
299 start
[reg
] = MIN2(start
[reg
], ip
);
300 end
[reg
] = MAX2(end
[reg
], end_ip
);
304 if (inst
->dst
.file
== GRF
) {
305 int reg
= inst
->dst
.reg
;
307 start
[reg
] = MIN2(start
[reg
], ip
);
308 end
[reg
] = MAX2(end
[reg
], ip
);
314 /* Now, extend those intervals using our analysis of control flow. */
316 fs_live_variables
livevars(this, &cfg
);
318 for (int b
= 0; b
< cfg
.num_blocks
; b
++) {
319 for (int i
= 0; i
< livevars
.num_vars
; i
++) {
320 int vgrf
= livevars
.vgrf_from_var
[i
];
321 if (BITSET_TEST(livevars
.bd
[b
].livein
, i
)) {
322 start
[vgrf
] = MIN2(start
[vgrf
], cfg
.blocks
[b
]->start_ip
);
323 end
[vgrf
] = MAX2(end
[vgrf
], cfg
.blocks
[b
]->start_ip
);
326 if (BITSET_TEST(livevars
.bd
[b
].liveout
, i
)) {
327 start
[vgrf
] = MIN2(start
[vgrf
], cfg
.blocks
[b
]->end_ip
);
328 end
[vgrf
] = MAX2(end
[vgrf
], cfg
.blocks
[b
]->end_ip
);
333 this->live_intervals_valid
= true;
337 fs_visitor::virtual_grf_interferes(int a
, int b
)
339 return !(virtual_grf_end
[a
] <= virtual_grf_start
[b
] ||
340 virtual_grf_end
[b
] <= virtual_grf_start
[a
]);