1/*
2 * Copyright (C) 2018-2019 Alyssa Rosenzweig <alyssa@rosenzweig.io>
3 * Copyright (C) 2019-2020 Collabora, Ltd.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24
25#include "compiler.h"
26
27void
28mir_liveness_ins_update(uint16_t *live, midgard_instruction *ins, unsigned max)
29{
30        /* live_in[s] = GEN[s] + (live_out[s] - KILL[s]) */
31
32        pan_liveness_kill(live, ins->dest, max, mir_bytemask(ins));
33
34        mir_foreach_src(ins, src) {
35                unsigned node = ins->src[src];
36                unsigned bytemask = mir_bytemask_of_read_components(ins, node);
37
38                pan_liveness_gen(live, node, max, bytemask);
39        }
40}
41
42static void
43mir_liveness_ins_update_wrap(uint16_t *live, void *ins, unsigned max)
44{
45        mir_liveness_ins_update(live, (midgard_instruction *) ins, max);
46}
47
48void
49mir_compute_liveness(compiler_context *ctx)
50{
51        /* If we already have fresh liveness, nothing to do */
52        if (ctx->metadata & MIDGARD_METADATA_LIVENESS)
53                return;
54
55        mir_compute_temp_count(ctx);
56        pan_compute_liveness(&ctx->blocks, ctx->temp_count, mir_liveness_ins_update_wrap);
57
58        /* Liveness is now valid */
59        ctx->metadata |= MIDGARD_METADATA_LIVENESS;
60}
61
62/* Once liveness data is no longer valid, call this */
63
64void
65mir_invalidate_liveness(compiler_context *ctx)
66{
67        /* If we didn't already compute liveness, there's nothing to do */
68        if (!(ctx->metadata & MIDGARD_METADATA_LIVENESS))
69                return;
70
71        pan_free_liveness(&ctx->blocks);
72
73        /* It's now invalid regardless */
74        ctx->metadata &= ~MIDGARD_METADATA_LIVENESS;
75}
76
77bool
78mir_is_live_after(compiler_context *ctx, midgard_block *block, midgard_instruction *start, int src)
79{
80        mir_compute_liveness(ctx);
81
82        /* Check whether we're live in the successors */
83
84        if (pan_liveness_get(block->base.live_out, src, ctx->temp_count))
85                return true;
86
87        /* Check the rest of the block for liveness */
88
89        mir_foreach_instr_in_block_from(block, ins, mir_next_op(start)) {
90                if (mir_has_arg(ins, src))
91                        return true;
92        }
93
94        return false;
95}
96