xref: /aosp_15_r20/external/mesa3d/src/freedreno/ir3/ir3_cse.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2014 Valve Corporation
3  * SPDX-License-Identifier: MIT
4  */
5 
6 #include "ir3.h"
7 
8 #define XXH_INLINE_ALL
9 #include "util/xxhash.h"
10 
11 /* This pass handles CSE'ing repeated expressions created in the process of
12  * translating from NIR. Currently this is just collect's. Also, currently
13  * this is intra-block only, to make it work over multiple block we'd need to
14  * bring forward dominance calculation.
15  */
16 
17 #define HASH(hash, data) XXH32(&(data), sizeof(data), hash)
18 
19 static uint32_t
hash_instr(const void * data)20 hash_instr(const void *data)
21 {
22    const struct ir3_instruction *instr = data;
23    uint32_t hash = 0;
24 
25    hash = HASH(hash, instr->opc);
26    hash = HASH(hash, instr->dsts[0]->flags);
27    foreach_src (src, (struct ir3_instruction *)instr) {
28       if (src->flags & IR3_REG_CONST) {
29          if (src->flags & IR3_REG_RELATIV)
30             hash = HASH(hash, src->array.offset);
31          else
32             hash = HASH(hash, src->num);
33       } else if (src->flags & IR3_REG_IMMED) {
34          hash = HASH(hash, src->uim_val);
35       } else {
36          if (src->flags & IR3_REG_ARRAY)
37             hash = HASH(hash, src->array.offset);
38          hash = HASH(hash, src->def);
39       }
40    }
41 
42    if (opc_cat(instr->opc) == 1) {
43       hash = HASH(hash, instr->cat1.dst_type);
44       hash = HASH(hash, instr->cat1.src_type);
45       hash = HASH(hash, instr->cat1.round);
46    }
47 
48    return hash;
49 }
50 
51 static bool
instrs_equal(const struct ir3_instruction * i1,const struct ir3_instruction * i2)52 instrs_equal(const struct ir3_instruction *i1, const struct ir3_instruction *i2)
53 {
54    if (i1->opc != i2->opc)
55       return false;
56 
57    if (i1->dsts_count != i2->dsts_count)
58       return false;
59 
60    if (i1->srcs_count != i2->srcs_count)
61       return false;
62 
63    if (i1->dsts[0]->flags != i2->dsts[0]->flags)
64       return false;
65 
66    for (unsigned i = 0; i < i1->srcs_count; i++) {
67       const struct ir3_register *i1_reg = i1->srcs[i], *i2_reg = i2->srcs[i];
68 
69       if (i1_reg->flags != i2_reg->flags)
70          return false;
71 
72       if (i1_reg->flags & IR3_REG_CONST) {
73          if (i1_reg->flags & IR3_REG_RELATIV) {
74             if (i1_reg->array.offset != i2_reg->array.offset)
75                return false;
76          } else {
77             if (i1_reg->num != i2_reg->num)
78                return false;
79          }
80       } else if (i1_reg->flags & IR3_REG_IMMED) {
81          if (i1_reg->uim_val != i2_reg->uim_val)
82             return false;
83       } else {
84          if (i1_reg->flags & IR3_REG_ARRAY) {
85             if (i1_reg->array.offset != i2_reg->array.offset)
86                return false;
87          }
88          if (i1_reg->def != i2_reg->def)
89             return false;
90       }
91    }
92 
93    if (opc_cat(i1->opc) == 1) {
94       if (i1->cat1.dst_type != i2->cat1.dst_type ||
95           i1->cat1.src_type != i2->cat1.src_type ||
96           i1->cat1.round != i2->cat1.round)
97          return false;
98    }
99 
100    return true;
101 }
102 
103 static bool
instr_can_cse(const struct ir3_instruction * instr)104 instr_can_cse(const struct ir3_instruction *instr)
105 {
106    if (instr->opc != OPC_META_COLLECT && instr->opc != OPC_MOV)
107       return false;
108 
109    if (!is_dest_gpr(instr->dsts[0]) || (instr->dsts[0]->flags & IR3_REG_ARRAY))
110       return false;
111 
112    return true;
113 }
114 
115 static bool
cmp_func(const void * data1,const void * data2)116 cmp_func(const void *data1, const void *data2)
117 {
118    return instrs_equal(data1, data2);
119 }
120 
121 bool
ir3_cse(struct ir3 * ir)122 ir3_cse(struct ir3 *ir)
123 {
124    struct set *instr_set = _mesa_set_create(NULL, hash_instr, cmp_func);
125    foreach_block (block, &ir->block_list) {
126       _mesa_set_clear(instr_set, NULL);
127 
128       foreach_instr (instr, &block->instr_list) {
129          instr->data = NULL;
130 
131          if (!instr_can_cse(instr))
132             continue;
133 
134          bool found;
135          struct set_entry *entry =
136             _mesa_set_search_or_add(instr_set, instr, &found);
137          if (found)
138             instr->data = (void *)entry->key;
139       }
140    }
141 
142    bool progress = false;
143    foreach_block (block, &ir->block_list) {
144       foreach_instr (instr, &block->instr_list) {
145          foreach_src (src, instr) {
146             if ((src->flags & IR3_REG_SSA) && src->def &&
147                 src->def->instr->data) {
148                progress = true;
149                struct ir3_instruction *instr = src->def->instr->data;
150                src->def = instr->dsts[0];
151             }
152          }
153       }
154    }
155 
156    _mesa_set_destroy(instr_set, NULL);
157    return progress;
158 }
159