radeonsi: move MRTZ export into a separate function
[mesa.git] / src / gallium / drivers / vc4 / vc4_opt_cse.c
1 /*
2 * Copyright © 2014 Broadcom
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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 /**
25 * @file vc4_opt_cse.c
26 *
27 * Implements CSE for QIR without control flow.
28 *
29 * For each operation that writes a destination (and isn't just a MOV), put it
30 * in the hash table of all instructions that do so. When faced with another
31 * one, look it up in the hash table by its opcode and operands. If there's
32 * an entry in the table, then just reuse the entry's destination as the
33 * source of a MOV instead of reproducing the computation. That MOV will then
34 * get cleaned up by copy propagation.
35 */
36
37 #include "vc4_qir.h"
38
39 #include "util/hash_table.h"
40 #include "util/ralloc.h"
41
42 static bool debug;
43
44 struct inst_key {
45 enum qop op;
46 struct qreg src[4];
47 /**
48 * If the instruction depends on the flags, how many SFs have been
49 * seen before this instruction.
50 */
51 uint32_t implicit_arg_update_count;
52 };
53
54 static bool
55 inst_key_equals(const void *a, const void *b)
56 {
57 const struct inst_key *key_a = a;
58 const struct inst_key *key_b = b;
59
60 return memcmp(key_a, key_b, sizeof(*key_a)) == 0;
61 }
62
63 static struct qinst *
64 vc4_find_cse(struct vc4_compile *c, struct hash_table *ht,
65 struct qinst *inst, uint32_t sf_count)
66 {
67 if (inst->dst.file != QFILE_TEMP ||
68 !c->defs[inst->dst.index] ||
69 inst->op == QOP_MOV ||
70 qir_get_op_nsrc(inst->op) > 4) {
71 return NULL;
72 }
73
74 struct inst_key key;
75 memset(&key, 0, sizeof(key));
76 key.op = inst->op;
77 memcpy(key.src, inst->src,
78 qir_get_op_nsrc(inst->op) * sizeof(key.src[0]));
79 if (qir_depends_on_flags(inst))
80 key.implicit_arg_update_count = sf_count;
81
82 uint32_t hash = _mesa_hash_data(&key, sizeof(key));
83 struct hash_entry *entry =
84 _mesa_hash_table_search_pre_hashed(ht, hash, &key);
85
86 if (entry) {
87 if (debug) {
88 fprintf(stderr, "CSE found match:\n");
89
90 fprintf(stderr, " Original inst: ");
91 qir_dump_inst(c, entry->data);
92 fprintf(stderr, "\n");
93
94 fprintf(stderr, " Our inst: ");
95 qir_dump_inst(c, inst);
96 fprintf(stderr, "\n");
97 }
98
99 return entry->data;
100 }
101
102 struct inst_key *alloc_key = ralloc(ht, struct inst_key);
103 if (!alloc_key)
104 return NULL;
105 memcpy(alloc_key, &key, sizeof(*alloc_key));
106 _mesa_hash_table_insert_pre_hashed(ht, hash, alloc_key, inst);
107
108 if (debug) {
109 fprintf(stderr, "Added to CSE HT: ");
110 qir_dump_inst(c, inst);
111 fprintf(stderr, "\n");
112 }
113
114 return NULL;
115 }
116
117 bool
118 qir_opt_cse(struct vc4_compile *c)
119 {
120 bool progress = false;
121 uint32_t sf_count = 0;
122
123 struct hash_table *ht = _mesa_hash_table_create(NULL, NULL,
124 inst_key_equals);
125 if (!ht)
126 return false;
127
128 list_for_each_entry(struct qinst, inst, &c->instructions, link) {
129 if (qir_has_side_effects(c, inst) ||
130 qir_has_side_effect_reads(c, inst) ||
131 inst->op == QOP_TLB_COLOR_READ) {
132 continue;
133 }
134
135 if (inst->sf) {
136 sf_count++;
137 } else {
138 struct qinst *cse = vc4_find_cse(c, ht, inst, sf_count);
139 if (cse) {
140 inst->src[0] = cse->dst;
141 for (int i = 1; i < qir_get_op_nsrc(inst->op);
142 i++)
143 inst->src[i] = c->undef;
144 inst->op = QOP_MOV;
145 progress = true;
146
147 if (debug) {
148 fprintf(stderr, " Turned into: ");
149 qir_dump_inst(c, inst);
150 fprintf(stderr, "\n");
151 }
152 }
153 }
154 }
155
156 ralloc_free(ht);
157
158 return progress;
159 }