17ec681f3Smrg/*
27ec681f3Smrg * Copyright (C) 2014 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.h"
257ec681f3Smrg
267ec681f3Smrg#define XXH_INLINE_ALL
277ec681f3Smrg#include "xxhash.h"
287ec681f3Smrg
297ec681f3Smrg/* This pass handles CSE'ing repeated expressions created in the process of
307ec681f3Smrg * translating from NIR. Currently this is just collect's. Also, currently
317ec681f3Smrg * this is intra-block only, to make it work over multiple block we'd need to
327ec681f3Smrg * bring forward dominance calculation.
337ec681f3Smrg */
347ec681f3Smrg
357ec681f3Smrg#define HASH(hash, data) XXH32(&(data), sizeof(data), hash)
367ec681f3Smrg
377ec681f3Smrgstatic uint32_t
387ec681f3Smrghash_instr(const void *data)
397ec681f3Smrg{
407ec681f3Smrg   const struct ir3_instruction *instr = data;
417ec681f3Smrg   uint32_t hash = 0;
427ec681f3Smrg
437ec681f3Smrg   hash = HASH(hash, instr->opc);
447ec681f3Smrg   hash = HASH(hash, instr->dsts[0]->flags);
457ec681f3Smrg   foreach_src (src, (struct ir3_instruction *)instr) {
467ec681f3Smrg      if (src->flags & IR3_REG_CONST)
477ec681f3Smrg         hash = HASH(hash, src->num);
487ec681f3Smrg      else if (src->flags & IR3_REG_IMMED)
497ec681f3Smrg         hash = HASH(hash, src->uim_val);
507ec681f3Smrg      else
517ec681f3Smrg         hash = HASH(hash, src->def);
527ec681f3Smrg   }
537ec681f3Smrg
547ec681f3Smrg   return hash;
557ec681f3Smrg}
567ec681f3Smrg
577ec681f3Smrgstatic bool
587ec681f3Smrginstrs_equal(const struct ir3_instruction *i1, const struct ir3_instruction *i2)
597ec681f3Smrg{
607ec681f3Smrg   if (i1->opc != i2->opc)
617ec681f3Smrg      return false;
627ec681f3Smrg
637ec681f3Smrg   if (i1->dsts_count != i2->dsts_count)
647ec681f3Smrg      return false;
657ec681f3Smrg
667ec681f3Smrg   if (i1->srcs_count != i2->srcs_count)
677ec681f3Smrg      return false;
687ec681f3Smrg
697ec681f3Smrg   if (i1->dsts[0]->flags != i2->dsts[0]->flags)
707ec681f3Smrg      return false;
717ec681f3Smrg
727ec681f3Smrg   for (unsigned i = 0; i < i1->srcs_count; i++) {
737ec681f3Smrg      const struct ir3_register *i1_reg = i1->srcs[i], *i2_reg = i2->srcs[i];
747ec681f3Smrg
757ec681f3Smrg      if (i1_reg->flags != i2_reg->flags)
767ec681f3Smrg         return false;
777ec681f3Smrg
787ec681f3Smrg      if (i1_reg->flags & IR3_REG_CONST) {
797ec681f3Smrg         if (i1_reg->num != i2_reg->num)
807ec681f3Smrg            return false;
817ec681f3Smrg      } else if (i1_reg->flags & IR3_REG_IMMED) {
827ec681f3Smrg         if (i1_reg->uim_val != i2_reg->uim_val)
837ec681f3Smrg            return false;
847ec681f3Smrg      } else {
857ec681f3Smrg         if (i1_reg->def != i2_reg->def)
867ec681f3Smrg            return false;
877ec681f3Smrg      }
887ec681f3Smrg   }
897ec681f3Smrg
907ec681f3Smrg   return true;
917ec681f3Smrg}
927ec681f3Smrg
937ec681f3Smrgstatic bool
947ec681f3Smrginstr_can_cse(const struct ir3_instruction *instr)
957ec681f3Smrg{
967ec681f3Smrg   if (instr->opc != OPC_META_COLLECT)
977ec681f3Smrg      return false;
987ec681f3Smrg
997ec681f3Smrg   return true;
1007ec681f3Smrg}
1017ec681f3Smrg
1027ec681f3Smrgstatic bool
1037ec681f3Smrgcmp_func(const void *data1, const void *data2)
1047ec681f3Smrg{
1057ec681f3Smrg   return instrs_equal(data1, data2);
1067ec681f3Smrg}
1077ec681f3Smrg
1087ec681f3Smrgbool
1097ec681f3Smrgir3_cse(struct ir3 *ir)
1107ec681f3Smrg{
1117ec681f3Smrg   struct set *instr_set = _mesa_set_create(NULL, hash_instr, cmp_func);
1127ec681f3Smrg   foreach_block (block, &ir->block_list) {
1137ec681f3Smrg      _mesa_set_clear(instr_set, NULL);
1147ec681f3Smrg
1157ec681f3Smrg      foreach_instr (instr, &block->instr_list) {
1167ec681f3Smrg         instr->data = NULL;
1177ec681f3Smrg
1187ec681f3Smrg         if (!instr_can_cse(instr))
1197ec681f3Smrg            continue;
1207ec681f3Smrg
1217ec681f3Smrg         bool found;
1227ec681f3Smrg         struct set_entry *entry =
1237ec681f3Smrg            _mesa_set_search_or_add(instr_set, instr, &found);
1247ec681f3Smrg         if (found)
1257ec681f3Smrg            instr->data = (void *)entry->key;
1267ec681f3Smrg      }
1277ec681f3Smrg   }
1287ec681f3Smrg
1297ec681f3Smrg   bool progress = false;
1307ec681f3Smrg   foreach_block (block, &ir->block_list) {
1317ec681f3Smrg      foreach_instr (instr, &block->instr_list) {
1327ec681f3Smrg         foreach_src (src, instr) {
1337ec681f3Smrg            if ((src->flags & IR3_REG_SSA) && src->def &&
1347ec681f3Smrg                src->def->instr->data) {
1357ec681f3Smrg               progress = true;
1367ec681f3Smrg               struct ir3_instruction *instr = src->def->instr->data;
1377ec681f3Smrg               src->def = instr->dsts[0];
1387ec681f3Smrg            }
1397ec681f3Smrg         }
1407ec681f3Smrg      }
1417ec681f3Smrg   }
1427ec681f3Smrg
1437ec681f3Smrg   _mesa_set_destroy(instr_set, NULL);
1447ec681f3Smrg   return progress;
1457ec681f3Smrg}
146