17ec681f3Smrg/*
27ec681f3Smrg * Copyright (C) 2021 Valve Corporation
37ec681f3Smrg *
47ec681f3Smrg * Permission is hereby granted, free of charge, to any person obtaining a
57ec681f3Smrg * copy of this software and associated documentation files (the "Software"),
67ec681f3Smrg * to deal in the Software without restriction, including without limitation
77ec681f3Smrg * the rights to use, copy, modify, merge, publish, distribute, sublicense,
87ec681f3Smrg * and/or sell copies of the Software, and to permit persons to whom the
97ec681f3Smrg * Software is furnished to do so, subject to the following conditions:
107ec681f3Smrg *
117ec681f3Smrg * The above copyright notice and this permission notice (including the next
127ec681f3Smrg * paragraph) shall be included in all copies or substantial portions of the
137ec681f3Smrg * Software.
147ec681f3Smrg *
157ec681f3Smrg * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
167ec681f3Smrg * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
177ec681f3Smrg * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
187ec681f3Smrg * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
197ec681f3Smrg * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
207ec681f3Smrg * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
217ec681f3Smrg * SOFTWARE.
227ec681f3Smrg */
237ec681f3Smrg
247ec681f3Smrg#include "ir3_ra.h"
257ec681f3Smrg#include "ir3_shader.h"
267ec681f3Smrg#include "ralloc.h"
277ec681f3Smrg
287ec681f3Smrg/* A note on how phi node uses are handled:
297ec681f3Smrg *
307ec681f3Smrg * - Phi node sources are considered to happen after the end of the
317ec681f3Smrg *   predecessor block, so the live_out for that block contains phi sources.
327ec681f3Smrg * - On the other hand, phi destinations are considered to happen at the start
337ec681f3Smrg *   of the block, so that live_in does *not* contain phi destinations. This
347ec681f3Smrg *   is mainly because phi destinations and live-through values have to be
357ec681f3Smrg *   treated very differently by RA at the beginning of a block.
367ec681f3Smrg */
377ec681f3Smrg
387ec681f3Smrgstatic bool
397ec681f3Smrgcompute_block_liveness(struct ir3_liveness *live, struct ir3_block *block,
407ec681f3Smrg                       BITSET_WORD *tmp_live, unsigned bitset_words)
417ec681f3Smrg{
427ec681f3Smrg   memcpy(tmp_live, live->live_out[block->index],
437ec681f3Smrg          bitset_words * sizeof(BITSET_WORD));
447ec681f3Smrg
457ec681f3Smrg   /* Process instructions */
467ec681f3Smrg   foreach_instr_rev (instr, &block->instr_list) {
477ec681f3Smrg      ra_foreach_dst (dst, instr) {
487ec681f3Smrg         if (BITSET_TEST(tmp_live, dst->name))
497ec681f3Smrg            dst->flags &= ~IR3_REG_UNUSED;
507ec681f3Smrg         else
517ec681f3Smrg            dst->flags |= IR3_REG_UNUSED;
527ec681f3Smrg         BITSET_CLEAR(tmp_live, dst->name);
537ec681f3Smrg      }
547ec681f3Smrg
557ec681f3Smrg      /* Phi node uses occur after the predecessor block */
567ec681f3Smrg      if (instr->opc != OPC_META_PHI) {
577ec681f3Smrg         ra_foreach_src (src, instr) {
587ec681f3Smrg            if (BITSET_TEST(tmp_live, src->def->name))
597ec681f3Smrg               src->flags &= ~IR3_REG_KILL;
607ec681f3Smrg            else
617ec681f3Smrg               src->flags |= IR3_REG_KILL;
627ec681f3Smrg         }
637ec681f3Smrg
647ec681f3Smrg         ra_foreach_src (src, instr) {
657ec681f3Smrg            if (BITSET_TEST(tmp_live, src->def->name))
667ec681f3Smrg               src->flags &= ~IR3_REG_FIRST_KILL;
677ec681f3Smrg            else
687ec681f3Smrg               src->flags |= IR3_REG_FIRST_KILL;
697ec681f3Smrg            BITSET_SET(tmp_live, src->def->name);
707ec681f3Smrg         }
717ec681f3Smrg      }
727ec681f3Smrg   }
737ec681f3Smrg
747ec681f3Smrg   memcpy(live->live_in[block->index], tmp_live,
757ec681f3Smrg          bitset_words * sizeof(BITSET_WORD));
767ec681f3Smrg
777ec681f3Smrg   bool progress = false;
787ec681f3Smrg   for (unsigned i = 0; i < block->predecessors_count; i++) {
797ec681f3Smrg      const struct ir3_block *pred = block->predecessors[i];
807ec681f3Smrg      for (unsigned j = 0; j < bitset_words; j++) {
817ec681f3Smrg         if (tmp_live[j] & ~live->live_out[pred->index][j])
827ec681f3Smrg            progress = true;
837ec681f3Smrg         live->live_out[pred->index][j] |= tmp_live[j];
847ec681f3Smrg      }
857ec681f3Smrg
867ec681f3Smrg      /* Process phi sources. */
877ec681f3Smrg      foreach_instr (phi, &block->instr_list) {
887ec681f3Smrg         if (phi->opc != OPC_META_PHI)
897ec681f3Smrg            break;
907ec681f3Smrg         if (!phi->srcs[i]->def)
917ec681f3Smrg            continue;
927ec681f3Smrg         unsigned name = phi->srcs[i]->def->name;
937ec681f3Smrg         if (!BITSET_TEST(live->live_out[pred->index], name)) {
947ec681f3Smrg            progress = true;
957ec681f3Smrg            BITSET_SET(live->live_out[pred->index], name);
967ec681f3Smrg         }
977ec681f3Smrg      }
987ec681f3Smrg   }
997ec681f3Smrg
1007ec681f3Smrg   for (unsigned i = 0; i < block->physical_predecessors_count; i++) {
1017ec681f3Smrg      const struct ir3_block *pred = block->physical_predecessors[i];
1027ec681f3Smrg      unsigned name;
1037ec681f3Smrg      BITSET_FOREACH_SET (name, tmp_live, live->definitions_count) {
1047ec681f3Smrg         struct ir3_register *reg = live->definitions[name];
1057ec681f3Smrg         if (!(reg->flags & IR3_REG_SHARED))
1067ec681f3Smrg            continue;
1077ec681f3Smrg         if (!BITSET_TEST(live->live_out[pred->index], name)) {
1087ec681f3Smrg            progress = true;
1097ec681f3Smrg            BITSET_SET(live->live_out[pred->index], name);
1107ec681f3Smrg         }
1117ec681f3Smrg      }
1127ec681f3Smrg   }
1137ec681f3Smrg
1147ec681f3Smrg   return progress;
1157ec681f3Smrg}
1167ec681f3Smrg
1177ec681f3Smrgstruct ir3_liveness *
1187ec681f3Smrgir3_calc_liveness(void *mem_ctx, struct ir3 *ir)
1197ec681f3Smrg{
1207ec681f3Smrg   struct ir3_liveness *live = rzalloc(mem_ctx, struct ir3_liveness);
1217ec681f3Smrg
1227ec681f3Smrg   /* Reserve name 0 to mean "doesn't have a name yet" to make the debug
1237ec681f3Smrg    * output nicer.
1247ec681f3Smrg    */
1257ec681f3Smrg   array_insert(live, live->definitions, NULL);
1267ec681f3Smrg
1277ec681f3Smrg   /* Build definition <-> name mapping */
1287ec681f3Smrg   unsigned block_count = 0;
1297ec681f3Smrg   foreach_block (block, &ir->block_list) {
1307ec681f3Smrg      block->index = block_count++;
1317ec681f3Smrg      foreach_instr (instr, &block->instr_list) {
1327ec681f3Smrg         ra_foreach_dst (dst, instr) {
1337ec681f3Smrg            dst->name = live->definitions_count;
1347ec681f3Smrg            array_insert(live, live->definitions, dst);
1357ec681f3Smrg         }
1367ec681f3Smrg      }
1377ec681f3Smrg   }
1387ec681f3Smrg
1397ec681f3Smrg   live->block_count = block_count;
1407ec681f3Smrg
1417ec681f3Smrg   unsigned bitset_words = BITSET_WORDS(live->definitions_count);
1427ec681f3Smrg   BITSET_WORD *tmp_live = ralloc_array(live, BITSET_WORD, bitset_words);
1437ec681f3Smrg   live->live_in = ralloc_array(live, BITSET_WORD *, block_count);
1447ec681f3Smrg   live->live_out = ralloc_array(live, BITSET_WORD *, block_count);
1457ec681f3Smrg   unsigned i = 0;
1467ec681f3Smrg   foreach_block (block, &ir->block_list) {
1477ec681f3Smrg      block->index = i++;
1487ec681f3Smrg      live->live_in[block->index] =
1497ec681f3Smrg         rzalloc_array(live, BITSET_WORD, bitset_words);
1507ec681f3Smrg      live->live_out[block->index] =
1517ec681f3Smrg         rzalloc_array(live, BITSET_WORD, bitset_words);
1527ec681f3Smrg   }
1537ec681f3Smrg
1547ec681f3Smrg   bool progress = true;
1557ec681f3Smrg   while (progress) {
1567ec681f3Smrg      progress = false;
1577ec681f3Smrg      foreach_block_rev (block, &ir->block_list) {
1587ec681f3Smrg         progress |=
1597ec681f3Smrg            compute_block_liveness(live, block, tmp_live, bitset_words);
1607ec681f3Smrg      }
1617ec681f3Smrg   }
1627ec681f3Smrg
1637ec681f3Smrg   return live;
1647ec681f3Smrg}
1657ec681f3Smrg
1667ec681f3Smrg/* Return true if "def" is live after "instr". It's assumed that "def"
1677ec681f3Smrg * dominates "instr".
1687ec681f3Smrg */
1697ec681f3Smrgbool
1707ec681f3Smrgir3_def_live_after(struct ir3_liveness *live, struct ir3_register *def,
1717ec681f3Smrg                   struct ir3_instruction *instr)
1727ec681f3Smrg{
1737ec681f3Smrg   /* If it's live out then it's definitely live at the instruction. */
1747ec681f3Smrg   if (BITSET_TEST(live->live_out[instr->block->index], def->name))
1757ec681f3Smrg      return true;
1767ec681f3Smrg
1777ec681f3Smrg   /* If it's not live in and not defined in the same block then the live
1787ec681f3Smrg    * range can't extend to the instruction.
1797ec681f3Smrg    */
1807ec681f3Smrg   if (def->instr->block != instr->block &&
1817ec681f3Smrg       !BITSET_TEST(live->live_in[instr->block->index], def->name))
1827ec681f3Smrg      return false;
1837ec681f3Smrg
1847ec681f3Smrg   /* Ok, now comes the tricky case, where "def" is killed somewhere in
1857ec681f3Smrg    * "instr"'s block and we have to check if it's before or after.
1867ec681f3Smrg    */
1877ec681f3Smrg   foreach_instr_rev (test_instr, &instr->block->instr_list) {
1887ec681f3Smrg      if (test_instr == instr)
1897ec681f3Smrg         break;
1907ec681f3Smrg
1917ec681f3Smrg      for (unsigned i = 0; i < test_instr->srcs_count; i++) {
1927ec681f3Smrg         if (test_instr->srcs[i]->def == def)
1937ec681f3Smrg            return true;
1947ec681f3Smrg      }
1957ec681f3Smrg   }
1967ec681f3Smrg
1977ec681f3Smrg   return false;
1987ec681f3Smrg}
199