| 1 |
/* |
| 2 |
* Copyright (C) 2020 Collabora, Ltd. |
| 3 |
* Copyright (C) 2018-2019 Alyssa Rosenzweig |
| 4 |
* Copyright © 2014 Intel Corporation |
| 5 |
* SPDX-License-Identifier: MIT |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "util/u_memory.h" |
| 9 |
#include "compiler.h" |
| 10 |
|
| 11 |
void |
| 12 |
bi_liveness_ins_update_ssa(BITSET_WORD *live, const bi_instr *I) |
| 13 |
{ |
| 14 |
bi_foreach_dest(I, d) |
| 15 |
BITSET_CLEAR(live, I->dest[d].value); |
| 16 |
|
| 17 |
bi_foreach_ssa_src(I, s) { |
| 18 |
/* If the source is not live after this instruction, but becomes live |
| 19 |
* at this instruction, this is the use that kills the source |
| 20 |
*/ |
| 21 |
I->src[s].kill_ssa = !BITSET_TEST(live, I->src[s].value); |
| 22 |
BITSET_SET(live, I->src[s].value); |
| 23 |
} |
| 24 |
} |
| 25 |
|
| 26 |
void |
| 27 |
bi_compute_liveness_ssa(bi_context *ctx) |
| 28 |
{ |
| 29 |
u_worklist worklist; |
| 30 |
u_worklist_init(&worklist, ctx->num_blocks, NULL); |
| 31 |
|
| 32 |
/* Free any previous liveness, and allocate */ |
| 33 |
unsigned words = BITSET_WORDS(ctx->ssa_alloc); |
| 34 |
|
| 35 |
bi_foreach_block(ctx, block) { |
| 36 |
ralloc_free(block->ssa_live_in); |
| 37 |
ralloc_free(block->ssa_live_out); |
| 38 |
|
| 39 |
block->ssa_live_in = rzalloc_array(block, BITSET_WORD, words); |
| 40 |
block->ssa_live_out = rzalloc_array(block, BITSET_WORD, words); |
| 41 |
|
| 42 |
bi_worklist_push_head(&worklist, block); |
| 43 |
} |
| 44 |
|
| 45 |
/* Iterate the work list */ |
| 46 |
while (!u_worklist_is_empty(&worklist)) { |
| 47 |
/* Pop in reverse order since liveness is a backwards pass */ |
| 48 |
bi_block *blk = bi_worklist_pop_head(&worklist); |
| 49 |
|
| 50 |
/* Update its liveness information */ |
| 51 |
memcpy(blk->ssa_live_in, blk->ssa_live_out, words * sizeof(BITSET_WORD)); |
| 52 |
|
| 53 |
bi_foreach_instr_in_block_rev(blk, I) { |
| 54 |
/* Phi nodes are handled separately, so we skip them. As phi nodes are |
| 55 |
* at the beginning and we're iterating backwards, we stop as soon as |
| 56 |
* we hit a phi node. |
| 57 |
*/ |
| 58 |
if (I->op == BI_OPCODE_PHI) |
| 59 |
break; |
| 60 |
|
| 61 |
blk->ssa_max_live = |
| 62 |
MAX2(__bitset_count(blk->ssa_live_in, words), blk->ssa_max_live); |
| 63 |
bi_liveness_ins_update_ssa(blk->ssa_live_in, I); |
| 64 |
} |
| 65 |
|
| 66 |
/* Propagate the live in of the successor (blk) to the live out of |
| 67 |
* predecessors. |
| 68 |
* |
| 69 |
* Phi nodes are logically on the control flow edge and act in parallel. |
| 70 |
* To handle when propagating, we kill writes from phis and make live the |
| 71 |
* corresponding sources. |
| 72 |
*/ |
| 73 |
bi_foreach_predecessor(blk, pred) { |
| 74 |
BITSET_WORD *live = ralloc_array(blk, BITSET_WORD, words); |
| 75 |
memcpy(live, blk->ssa_live_in, words * sizeof(BITSET_WORD)); |
| 76 |
|
| 77 |
/* Kill write */ |
| 78 |
bi_foreach_phi_in_block(blk, I) { |
| 79 |
BITSET_CLEAR(live, I->dest[0].value); |
| 80 |
} |
| 81 |
|
| 82 |
/* Make live the corresponding source */ |
| 83 |
bi_foreach_phi_in_block(blk, I) { |
| 84 |
bi_index operand = I->src[bi_predecessor_index(blk, *pred)]; |
| 85 |
if (bi_is_ssa(operand)) { |
| 86 |
BITSET_SET(live, operand.value); |
| 87 |
I->src[bi_predecessor_index(blk, *pred)].kill_ssa = false; |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
BITSET_WORD progress = 0; |
| 92 |
|
| 93 |
for (unsigned i = 0; i < words; ++i) { |
| 94 |
progress |= live[i] & ~((*pred)->ssa_live_out[i]); |
| 95 |
(*pred)->ssa_live_out[i] |= live[i]; |
| 96 |
} |
| 97 |
|
| 98 |
if (progress != 0) |
| 99 |
bi_worklist_push_tail(&worklist, *pred); |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
u_worklist_fini(&worklist); |
| 104 |
} |