17ec681f3Smrg/*
27ec681f3Smrg * Copyright (C) 2018-2019 Alyssa Rosenzweig <alyssa@rosenzweig.io>
37ec681f3Smrg * Copyright (C) 2019-2020 Collabora, Ltd.
47ec681f3Smrg *
57ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
67ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
77ec681f3Smrg * to deal in the Software without restriction, including without limitation
87ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
97ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the
107ec681f3Smrg * Software is furnished to do so, subject to the following conditions:
117ec681f3Smrg *
127ec681f3Smrg * The above copyright notice and this permission notice (including the next
137ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the
147ec681f3Smrg * Software.
157ec681f3Smrg *
167ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
177ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
187ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
197ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
207ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
217ec681f3Smrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
227ec681f3Smrg * SOFTWARE.
237ec681f3Smrg */
247ec681f3Smrg
257ec681f3Smrg#include "compiler.h"
267ec681f3Smrg
277ec681f3Smrgvoid
287ec681f3Smrgmir_liveness_ins_update(uint16_t *live, midgard_instruction *ins, unsigned max)
297ec681f3Smrg{
307ec681f3Smrg        /* live_in[s] = GEN[s] + (live_out[s] - KILL[s]) */
317ec681f3Smrg
327ec681f3Smrg        pan_liveness_kill(live, ins->dest, max, mir_bytemask(ins));
337ec681f3Smrg
347ec681f3Smrg        mir_foreach_src(ins, src) {
357ec681f3Smrg                unsigned node = ins->src[src];
367ec681f3Smrg                unsigned bytemask = mir_bytemask_of_read_components(ins, node);
377ec681f3Smrg
387ec681f3Smrg                pan_liveness_gen(live, node, max, bytemask);
397ec681f3Smrg        }
407ec681f3Smrg}
417ec681f3Smrg
427ec681f3Smrgstatic void
437ec681f3Smrgmir_liveness_ins_update_wrap(uint16_t *live, void *ins, unsigned max)
447ec681f3Smrg{
457ec681f3Smrg        mir_liveness_ins_update(live, (midgard_instruction *) ins, max);
467ec681f3Smrg}
477ec681f3Smrg
487ec681f3Smrgvoid
497ec681f3Smrgmir_compute_liveness(compiler_context *ctx)
507ec681f3Smrg{
517ec681f3Smrg        /* If we already have fresh liveness, nothing to do */
527ec681f3Smrg        if (ctx->metadata & MIDGARD_METADATA_LIVENESS)
537ec681f3Smrg                return;
547ec681f3Smrg
557ec681f3Smrg        mir_compute_temp_count(ctx);
567ec681f3Smrg        pan_compute_liveness(&ctx->blocks, ctx->temp_count, mir_liveness_ins_update_wrap);
577ec681f3Smrg
587ec681f3Smrg        /* Liveness is now valid */
597ec681f3Smrg        ctx->metadata |= MIDGARD_METADATA_LIVENESS;
607ec681f3Smrg}
617ec681f3Smrg
627ec681f3Smrg/* Once liveness data is no longer valid, call this */
637ec681f3Smrg
647ec681f3Smrgvoid
657ec681f3Smrgmir_invalidate_liveness(compiler_context *ctx)
667ec681f3Smrg{
677ec681f3Smrg        /* If we didn't already compute liveness, there's nothing to do */
687ec681f3Smrg        if (!(ctx->metadata & MIDGARD_METADATA_LIVENESS))
697ec681f3Smrg                return;
707ec681f3Smrg
717ec681f3Smrg        pan_free_liveness(&ctx->blocks);
727ec681f3Smrg
737ec681f3Smrg        /* It's now invalid regardless */
747ec681f3Smrg        ctx->metadata &= ~MIDGARD_METADATA_LIVENESS;
757ec681f3Smrg}
767ec681f3Smrg
777ec681f3Smrgbool
787ec681f3Smrgmir_is_live_after(compiler_context *ctx, midgard_block *block, midgard_instruction *start, int src)
797ec681f3Smrg{
807ec681f3Smrg        mir_compute_liveness(ctx);
817ec681f3Smrg
827ec681f3Smrg        /* Check whether we're live in the successors */
837ec681f3Smrg
847ec681f3Smrg        if (pan_liveness_get(block->base.live_out, src, ctx->temp_count))
857ec681f3Smrg                return true;
867ec681f3Smrg
877ec681f3Smrg        /* Check the rest of the block for liveness */
887ec681f3Smrg
897ec681f3Smrg        mir_foreach_instr_in_block_from(block, ins, mir_next_op(start)) {
907ec681f3Smrg                if (mir_has_arg(ins, src))
917ec681f3Smrg                        return true;
927ec681f3Smrg        }
937ec681f3Smrg
947ec681f3Smrg        return false;
957ec681f3Smrg}
96