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
27 void
mir_liveness_ins_update(uint16_t * live,const midgard_instruction * ins,unsigned max)28 mir_liveness_ins_update(uint16_t *live, const midgard_instruction *ins,
29 unsigned max)
30 {
31 /* live_in[s] = GEN[s] + (live_out[s] - KILL[s]) */
32
33 pan_liveness_kill(live, ins->dest, max, mir_bytemask(ins));
34
35 mir_foreach_src(ins, src) {
36 unsigned node = ins->src[src];
37 unsigned bytemask = mir_bytemask_of_read_components(ins, node);
38
39 pan_liveness_gen(live, node, max, bytemask);
40 }
41 }
42
43 static void
mir_liveness_ins_update_wrap(uint16_t * live,void * ins,unsigned max)44 mir_liveness_ins_update_wrap(uint16_t *live, void *ins, unsigned max)
45 {
46 mir_liveness_ins_update(live, (midgard_instruction *)ins, max);
47 }
48
49 void
mir_compute_liveness(compiler_context * ctx)50 mir_compute_liveness(compiler_context *ctx)
51 {
52 /* If we already have fresh liveness, nothing to do */
53 if (ctx->metadata & MIDGARD_METADATA_LIVENESS)
54 return;
55
56 mir_compute_temp_count(ctx);
57 pan_compute_liveness(&ctx->blocks, ctx->temp_count,
58 mir_liveness_ins_update_wrap);
59
60 /* Liveness is now valid */
61 ctx->metadata |= MIDGARD_METADATA_LIVENESS;
62 }
63
64 /* Once liveness data is no longer valid, call this */
65
66 void
mir_invalidate_liveness(compiler_context * ctx)67 mir_invalidate_liveness(compiler_context *ctx)
68 {
69 /* If we didn't already compute liveness, there's nothing to do */
70 if (!(ctx->metadata & MIDGARD_METADATA_LIVENESS))
71 return;
72
73 pan_free_liveness(&ctx->blocks);
74
75 /* It's now invalid regardless */
76 ctx->metadata &= ~MIDGARD_METADATA_LIVENESS;
77 }
78
79 bool
mir_is_live_after(compiler_context * ctx,const midgard_block * block,const midgard_instruction * start,int src)80 mir_is_live_after(compiler_context *ctx, const midgard_block *block,
81 const midgard_instruction *start, int src)
82 {
83 mir_compute_liveness(ctx);
84
85 /* Check whether we're live in the successors */
86
87 if (pan_liveness_get(block->base.live_out, src, ctx->temp_count))
88 return true;
89
90 /* Check the rest of the block for liveness */
91
92 mir_foreach_instr_in_block_from(block, ins, mir_next_op(start)) {
93 if (mir_has_arg(ins, src))
94 return true;
95 }
96
97 return false;
98 }
99