xref: /aosp_15_r20/external/mesa3d/src/panfrost/midgard/midgard_liveness.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright (C) 2018-2019 Alyssa Rosenzweig <[email protected]>
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,midgard_instruction * ins,unsigned max)28 mir_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 
42 static void
mir_liveness_ins_update_wrap(uint16_t * live,void * ins,unsigned max)43 mir_liveness_ins_update_wrap(uint16_t *live, void *ins, unsigned max)
44 {
45    mir_liveness_ins_update(live, (midgard_instruction *)ins, max);
46 }
47 
48 void
mir_compute_liveness(compiler_context * ctx)49 mir_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,
57                         mir_liveness_ins_update_wrap);
58 
59    /* Liveness is now valid */
60    ctx->metadata |= MIDGARD_METADATA_LIVENESS;
61 }
62 
63 /* Once liveness data is no longer valid, call this */
64 
65 void
mir_invalidate_liveness(compiler_context * ctx)66 mir_invalidate_liveness(compiler_context *ctx)
67 {
68    /* If we didn't already compute liveness, there's nothing to do */
69    if (!(ctx->metadata & MIDGARD_METADATA_LIVENESS))
70       return;
71 
72    pan_free_liveness(&ctx->blocks);
73 
74    /* It's now invalid regardless */
75    ctx->metadata &= ~MIDGARD_METADATA_LIVENESS;
76 }
77 
78 bool
mir_is_live_after(compiler_context * ctx,midgard_block * block,midgard_instruction * start,int src)79 mir_is_live_after(compiler_context *ctx, midgard_block *block,
80                   midgard_instruction *start, int src)
81 {
82    mir_compute_liveness(ctx);
83 
84    /* Check whether we're live in the successors */
85 
86    if (pan_liveness_get(block->base.live_out, src, ctx->temp_count))
87       return true;
88 
89    /* Check the rest of the block for liveness */
90 
91    mir_foreach_instr_in_block_from(block, ins, mir_next_op(start)) {
92       if (mir_has_arg(ins, src))
93          return true;
94    }
95 
96    return false;
97 }
98