1 /*
2 * Copyright (C) 2014 Valve Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 */
23
24 #include "ir3.h"
25
26 #define XXH_INLINE_ALL
27 #include "xxhash.h"
28
29 /* This pass handles CSE'ing repeated expressions created in the process of
30 * translating from NIR. Currently this is just collect's. Also, currently
31 * this is intra-block only, to make it work over multiple block we'd need to
32 * bring forward dominance calculation.
33 */
34
35 #define HASH(hash, data) XXH32(&(data), sizeof(data), hash)
36
37 static uint32_t
hash_instr(const void * data)38 hash_instr(const void *data)
39 {
40 const struct ir3_instruction *instr = data;
41 uint32_t hash = 0;
42
43 hash = HASH(hash, instr->opc);
44 hash = HASH(hash, instr->dsts[0]->flags);
45 foreach_src (src, (struct ir3_instruction *)instr) {
46 if (src->flags & IR3_REG_CONST)
47 hash = HASH(hash, src->num);
48 else if (src->flags & IR3_REG_IMMED)
49 hash = HASH(hash, src->uim_val);
50 else
51 hash = HASH(hash, src->def);
52 }
53
54 return hash;
55 }
56
57 static bool
instrs_equal(const struct ir3_instruction * i1,const struct ir3_instruction * i2)58 instrs_equal(const struct ir3_instruction *i1, const struct ir3_instruction *i2)
59 {
60 if (i1->opc != i2->opc)
61 return false;
62
63 if (i1->dsts_count != i2->dsts_count)
64 return false;
65
66 if (i1->srcs_count != i2->srcs_count)
67 return false;
68
69 if (i1->dsts[0]->flags != i2->dsts[0]->flags)
70 return false;
71
72 for (unsigned i = 0; i < i1->srcs_count; i++) {
73 const struct ir3_register *i1_reg = i1->srcs[i], *i2_reg = i2->srcs[i];
74
75 if (i1_reg->flags != i2_reg->flags)
76 return false;
77
78 if (i1_reg->flags & IR3_REG_CONST) {
79 if (i1_reg->num != i2_reg->num)
80 return false;
81 } else if (i1_reg->flags & IR3_REG_IMMED) {
82 if (i1_reg->uim_val != i2_reg->uim_val)
83 return false;
84 } else {
85 if (i1_reg->def != i2_reg->def)
86 return false;
87 }
88 }
89
90 return true;
91 }
92
93 static bool
instr_can_cse(const struct ir3_instruction * instr)94 instr_can_cse(const struct ir3_instruction *instr)
95 {
96 if (instr->opc != OPC_META_COLLECT)
97 return false;
98
99 return true;
100 }
101
102 static bool
cmp_func(const void * data1,const void * data2)103 cmp_func(const void *data1, const void *data2)
104 {
105 return instrs_equal(data1, data2);
106 }
107
108 bool
ir3_cse(struct ir3 * ir)109 ir3_cse(struct ir3 *ir)
110 {
111 struct set *instr_set = _mesa_set_create(NULL, hash_instr, cmp_func);
112 foreach_block (block, &ir->block_list) {
113 _mesa_set_clear(instr_set, NULL);
114
115 foreach_instr (instr, &block->instr_list) {
116 instr->data = NULL;
117
118 if (!instr_can_cse(instr))
119 continue;
120
121 bool found;
122 struct set_entry *entry =
123 _mesa_set_search_or_add(instr_set, instr, &found);
124 if (found)
125 instr->data = (void *)entry->key;
126 }
127 }
128
129 bool progress = false;
130 foreach_block (block, &ir->block_list) {
131 foreach_instr (instr, &block->instr_list) {
132 foreach_src (src, instr) {
133 if ((src->flags & IR3_REG_SSA) && src->def &&
134 src->def->instr->data) {
135 progress = true;
136 struct ir3_instruction *instr = src->def->instr->data;
137 src->def = instr->dsts[0];
138 }
139 }
140 }
141 }
142
143 _mesa_set_destroy(instr_set, NULL);
144 return progress;
145 }
146