| 1 |
/* |
| 2 |
* Copyright (C) 2018-2019 Alyssa Rosenzweig |
| 3 |
* Copyright (C) 2019-2020 Collabora, Ltd. |
| 4 |
* SPDX-License-Identifier: MIT |
| 5 |
*/ |
| 6 |
|
| 7 |
#include "compiler.h" |
| 8 |
|
| 9 |
/* Routines for liveness analysis. Liveness is tracked per byte per node. Per |
| 10 |
* byte granularity is necessary for proper handling of int8 */ |
| 11 |
|
| 12 |
static void |
| 13 |
mir_liveness_gen(uint16_t *live, unsigned node, unsigned max, uint16_t mask) |
| 14 |
{ |
| 15 |
if (node >= max) |
| 16 |
return; |
| 17 |
|
| 18 |
live[node] |= mask; |
| 19 |
} |
| 20 |
|
| 21 |
static void |
| 22 |
mir_liveness_kill(uint16_t *live, unsigned node, unsigned max, uint16_t mask) |
| 23 |
{ |
| 24 |
if (node >= max) |
| 25 |
return; |
| 26 |
|
| 27 |
live[node] &= ~mask; |
| 28 |
} |
| 29 |
|
| 30 |
static bool |
| 31 |
mir_liveness_get(uint16_t *live, unsigned node, uint16_t max) |
| 32 |
{ |
| 33 |
if (node >= max) |
| 34 |
return false; |
| 35 |
|
| 36 |
return live[node]; |
| 37 |
} |
| 38 |
|
| 39 |
void |
| 40 |
mir_liveness_ins_update(uint16_t *live, const midgard_instruction *ins, |
| 41 |
unsigned max) |
| 42 |
{ |
| 43 |
/* live_in[s] = GEN[s] + (live_out[s] - KILL[s]) */ |
| 44 |
|
| 45 |
mir_liveness_kill(live, ins->dest, max, mir_bytemask(ins)); |
| 46 |
|
| 47 |
mir_foreach_src(ins, src) { |
| 48 |
unsigned node = ins->src[src]; |
| 49 |
unsigned bytemask = mir_bytemask_of_read_components(ins, node); |
| 50 |
|
| 51 |
mir_liveness_gen(live, node, max, bytemask); |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
/* live_out[s] = sum { p in succ[s] } ( live_in[p] ) */ |
| 56 |
|
| 57 |
static void |
| 58 |
liveness_block_live_out(midgard_block *blk, unsigned temp_count) |
| 59 |
{ |
| 60 |
mir_foreach_successor(blk, succ) { |
| 61 |
for (unsigned i = 0; i < temp_count; ++i) |
| 62 |
blk->live_out[i] |= succ->live_in[i]; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
/* Liveness analysis is a backwards-may dataflow analysis pass. Within a block, |
| 67 |
* we compute live_out from live_in. The intrablock pass is linear-time. It |
| 68 |
* returns whether progress was made. */ |
| 69 |
|
| 70 |
static bool |
| 71 |
liveness_block_update(midgard_block *blk, unsigned temp_count) |
| 72 |
{ |
| 73 |
bool progress = false; |
| 74 |
|
| 75 |
liveness_block_live_out(blk, temp_count); |
| 76 |
|
| 77 |
uint16_t *live = ralloc_array(blk, uint16_t, temp_count); |
| 78 |
memcpy(live, blk->live_out, temp_count * sizeof(uint16_t)); |
| 79 |
|
| 80 |
mir_foreach_instr_in_block_rev(blk, ins) |
| 81 |
mir_liveness_ins_update(live, (midgard_instruction *)ins, temp_count); |
| 82 |
|
| 83 |
/* To figure out progress, diff live_in */ |
| 84 |
|
| 85 |
for (unsigned i = 0; (i < temp_count) && !progress; ++i) |
| 86 |
progress |= (blk->live_in[i] != live[i]); |
| 87 |
|
| 88 |
ralloc_free(blk->live_in); |
| 89 |
blk->live_in = live; |
| 90 |
|
| 91 |
return progress; |
| 92 |
} |
| 93 |
|
| 94 |
/* Once liveness data is no longer valid, call this */ |
| 95 |
|
| 96 |
static void |
| 97 |
mir_free_liveness(compiler_context *ctx) |
| 98 |
{ |
| 99 |
mir_foreach_block(ctx, _block) { |
| 100 |
midgard_block *block = (midgard_block *)_block; |
| 101 |
|
| 102 |
ralloc_free(block->live_in); |
| 103 |
ralloc_free(block->live_out); |
| 104 |
|
| 105 |
block->live_in = NULL; |
| 106 |
block->live_out = NULL; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
/* Globally, liveness analysis uses a fixed-point algorithm based on a |
| 111 |
* worklist. We initialize a work list with the exit block. We iterate the work |
| 112 |
* list to compute live_in from live_out for each block on the work list, |
| 113 |
* adding the predecessors of the block to the work list if we made progress. |
| 114 |
*/ |
| 115 |
|
| 116 |
void |
| 117 |
mir_compute_liveness(compiler_context *ctx) |
| 118 |
{ |
| 119 |
/* If we already have fresh liveness, nothing to do */ |
| 120 |
if (ctx->metadata & MIDGARD_METADATA_LIVENESS) |
| 121 |
return; |
| 122 |
|
| 123 |
mir_compute_temp_count(ctx); |
| 124 |
|
| 125 |
/* Set of midgard_block */ |
| 126 |
struct set *work_list = |
| 127 |
_mesa_set_create(NULL, _mesa_hash_pointer, _mesa_key_pointer_equal); |
| 128 |
|
| 129 |
struct set *visited = |
| 130 |
_mesa_set_create(NULL, _mesa_hash_pointer, _mesa_key_pointer_equal); |
| 131 |
|
| 132 |
/* Free any previous liveness, and allocate */ |
| 133 |
|
| 134 |
mir_free_liveness(ctx); |
| 135 |
|
| 136 |
mir_foreach_block(ctx, _block) { |
| 137 |
midgard_block *block = (midgard_block *)_block; |
| 138 |
block->live_in = rzalloc_array(block, uint16_t, ctx->temp_count); |
| 139 |
block->live_out = rzalloc_array(block, uint16_t, ctx->temp_count); |
| 140 |
} |
| 141 |
|
| 142 |
/* Initialize the work list with the exit block */ |
| 143 |
struct set_entry *cur; |
| 144 |
|
| 145 |
cur = _mesa_set_add(work_list, mir_exit_block(&ctx->blocks)); |
| 146 |
|
| 147 |
/* Iterate the work list */ |
| 148 |
|
| 149 |
do { |
| 150 |
/* Pop off a block */ |
| 151 |
midgard_block *blk = (struct midgard_block *)cur->key; |
| 152 |
_mesa_set_remove(work_list, cur); |
| 153 |
|
| 154 |
/* Update its liveness information */ |
| 155 |
bool progress = liveness_block_update(blk, ctx->temp_count); |
| 156 |
|
| 157 |
/* If we made progress, we need to process the predecessors */ |
| 158 |
|
| 159 |
if (progress || !_mesa_set_search(visited, blk)) { |
| 160 |
mir_foreach_predecessor(blk, pred) |
| 161 |
_mesa_set_add(work_list, pred); |
| 162 |
} |
| 163 |
|
| 164 |
_mesa_set_add(visited, blk); |
| 165 |
} while ((cur = _mesa_set_next_entry(work_list, NULL)) != NULL); |
| 166 |
|
| 167 |
_mesa_set_destroy(visited, NULL); |
| 168 |
_mesa_set_destroy(work_list, NULL); |
| 169 |
|
| 170 |
/* Liveness is now valid */ |
| 171 |
ctx->metadata |= MIDGARD_METADATA_LIVENESS; |
| 172 |
} |
| 173 |
|
| 174 |
void |
| 175 |
mir_invalidate_liveness(compiler_context *ctx) |
| 176 |
{ |
| 177 |
/* If we didn't already compute liveness, there's nothing to do */ |
| 178 |
if (!(ctx->metadata & MIDGARD_METADATA_LIVENESS)) |
| 179 |
return; |
| 180 |
|
| 181 |
mir_free_liveness(ctx); |
| 182 |
|
| 183 |
/* It's now invalid regardless */ |
| 184 |
ctx->metadata &= ~MIDGARD_METADATA_LIVENESS; |
| 185 |
} |
| 186 |
|
| 187 |
bool |
| 188 |
mir_is_live_after(compiler_context *ctx, const midgard_block *block, |
| 189 |
const midgard_instruction *start, int src) |
| 190 |
{ |
| 191 |
mir_compute_liveness(ctx); |
| 192 |
|
| 193 |
/* Check whether we're live in the successors */ |
| 194 |
|
| 195 |
if (mir_liveness_get(block->live_out, src, ctx->temp_count)) |
| 196 |
return true; |
| 197 |
|
| 198 |
/* Check the rest of the block for liveness */ |
| 199 |
|
| 200 |
mir_foreach_instr_in_block_from(block, ins, mir_next_op(start)) { |
| 201 |
if (mir_has_arg(ins, src)) |
| 202 |
return true; |
| 203 |
} |
| 204 |
|
| 205 |
return false; |
| 206 |
} |