f87601ce0ecd48263607cf2fd989fe739cf31a76
[mesa.git] / src / mesa / drivers / dri / i965 / brw_fs_cse.cpp
1 /*
2 * Copyright © 2012 Intel 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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "brw_fs.h"
25 #include "brw_cfg.h"
26
27 /** @file brw_fs_cse.cpp
28 *
29 * Support for local common subexpression elimination.
30 *
31 * See Muchnick's Advanced Compiler Design and Implementation, section
32 * 13.1 (p378).
33 */
34
35 namespace {
36 struct aeb_entry : public exec_node {
37 /** The instruction that generates the expression value. */
38 fs_inst *generator;
39
40 /** The temporary where the value is stored. */
41 fs_reg tmp;
42 };
43 }
44
45 static bool
46 is_copy_payload(const fs_inst *inst)
47 {
48 const int reg = inst->src[0].reg;
49 if (inst->src[0].reg_offset != 0)
50 return false;
51
52 for (int i = 1; i < inst->sources; i++) {
53 if (inst->src[i].reg != reg ||
54 inst->src[i].reg_offset != i) {
55 return false;
56 }
57 }
58 return true;
59 }
60
61 static bool
62 is_expression(const fs_inst *const inst)
63 {
64 switch (inst->opcode) {
65 case BRW_OPCODE_MOV:
66 case BRW_OPCODE_SEL:
67 case BRW_OPCODE_NOT:
68 case BRW_OPCODE_AND:
69 case BRW_OPCODE_OR:
70 case BRW_OPCODE_XOR:
71 case BRW_OPCODE_SHR:
72 case BRW_OPCODE_SHL:
73 case BRW_OPCODE_ASR:
74 case BRW_OPCODE_CMP:
75 case BRW_OPCODE_CMPN:
76 case BRW_OPCODE_ADD:
77 case BRW_OPCODE_MUL:
78 case BRW_OPCODE_FRC:
79 case BRW_OPCODE_RNDU:
80 case BRW_OPCODE_RNDD:
81 case BRW_OPCODE_RNDE:
82 case BRW_OPCODE_RNDZ:
83 case BRW_OPCODE_LINE:
84 case BRW_OPCODE_PLN:
85 case BRW_OPCODE_MAD:
86 case BRW_OPCODE_LRP:
87 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD:
88 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN7:
89 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD:
90 case FS_OPCODE_CINTERP:
91 case FS_OPCODE_LINTERP:
92 return true;
93 case SHADER_OPCODE_RCP:
94 case SHADER_OPCODE_RSQ:
95 case SHADER_OPCODE_SQRT:
96 case SHADER_OPCODE_EXP2:
97 case SHADER_OPCODE_LOG2:
98 case SHADER_OPCODE_POW:
99 case SHADER_OPCODE_INT_QUOTIENT:
100 case SHADER_OPCODE_INT_REMAINDER:
101 case SHADER_OPCODE_SIN:
102 case SHADER_OPCODE_COS:
103 return inst->mlen < 2;
104 case SHADER_OPCODE_LOAD_PAYLOAD:
105 return !is_copy_payload(inst);
106 default:
107 return inst->is_send_from_grf() && !inst->has_side_effects();
108 }
109 }
110
111 static bool
112 is_expression_commutative(const fs_inst *inst)
113 {
114 switch (inst->opcode) {
115 case BRW_OPCODE_AND:
116 case BRW_OPCODE_OR:
117 case BRW_OPCODE_XOR:
118 case BRW_OPCODE_ADD:
119 case BRW_OPCODE_MUL:
120 return true;
121 case BRW_OPCODE_SEL:
122 /* MIN and MAX are commutative. */
123 if (inst->conditional_mod == BRW_CONDITIONAL_GE ||
124 inst->conditional_mod == BRW_CONDITIONAL_L) {
125 return true;
126 }
127 /* fallthrough */
128 default:
129 return false;
130 }
131 }
132
133 static bool
134 operands_match(const fs_inst *a, const fs_inst *b)
135 {
136 fs_reg *xs = a->src;
137 fs_reg *ys = b->src;
138
139 if (a->opcode == BRW_OPCODE_MAD) {
140 return xs[0].equals(ys[0]) &&
141 ((xs[1].equals(ys[1]) && xs[2].equals(ys[2])) ||
142 (xs[2].equals(ys[1]) && xs[1].equals(ys[2])));
143 } else if (!is_expression_commutative(a)) {
144 bool match = true;
145 for (int i = 0; i < a->sources; i++) {
146 if (!xs[i].equals(ys[i])) {
147 match = false;
148 break;
149 }
150 }
151 return match;
152 } else {
153 return (xs[0].equals(ys[0]) && xs[1].equals(ys[1])) ||
154 (xs[1].equals(ys[0]) && xs[0].equals(ys[1]));
155 }
156 }
157
158 static bool
159 instructions_match(fs_inst *a, fs_inst *b)
160 {
161 return a->opcode == b->opcode &&
162 a->saturate == b->saturate &&
163 a->predicate == b->predicate &&
164 a->predicate_inverse == b->predicate_inverse &&
165 a->conditional_mod == b->conditional_mod &&
166 a->dst.type == b->dst.type &&
167 a->sources == b->sources &&
168 (a->is_tex() ? (a->offset == b->offset &&
169 a->mlen == b->mlen &&
170 a->regs_written == b->regs_written &&
171 a->base_mrf == b->base_mrf &&
172 a->eot == b->eot &&
173 a->header_present == b->header_present &&
174 a->shadow_compare == b->shadow_compare)
175 : true) &&
176 operands_match(a, b);
177 }
178
179 bool
180 fs_visitor::opt_cse_local(bblock_t *block)
181 {
182 bool progress = false;
183 exec_list aeb;
184
185 void *cse_ctx = ralloc_context(NULL);
186
187 int ip = block->start_ip;
188 foreach_inst_in_block(fs_inst, inst, block) {
189 /* Skip some cases. */
190 if (is_expression(inst) && !inst->is_partial_write() &&
191 (inst->dst.file != HW_REG || inst->dst.is_null()))
192 {
193 bool found = false;
194
195 foreach_in_list_use_after(aeb_entry, entry, &aeb) {
196 /* Match current instruction's expression against those in AEB. */
197 if (instructions_match(inst, entry->generator)) {
198 found = true;
199 progress = true;
200 break;
201 }
202 }
203
204 if (!found) {
205 if (inst->opcode != BRW_OPCODE_MOV ||
206 (inst->opcode == BRW_OPCODE_MOV &&
207 inst->src[0].file == IMM &&
208 inst->src[0].type == BRW_REGISTER_TYPE_VF)) {
209 /* Our first sighting of this expression. Create an entry. */
210 aeb_entry *entry = ralloc(cse_ctx, aeb_entry);
211 entry->tmp = reg_undef;
212 entry->generator = inst;
213 aeb.push_tail(entry);
214 }
215 } else {
216 /* This is at least our second sighting of this expression.
217 * If we don't have a temporary already, make one.
218 */
219 bool no_existing_temp = entry->tmp.file == BAD_FILE;
220 if (no_existing_temp && !entry->generator->dst.is_null()) {
221 int written = entry->generator->regs_written;
222 int dst_width = entry->generator->dst.width / 8;
223 assert(written % dst_width == 0);
224
225 fs_reg orig_dst = entry->generator->dst;
226 fs_reg tmp = fs_reg(GRF, virtual_grf_alloc(written),
227 orig_dst.type, orig_dst.width);
228 entry->tmp = tmp;
229 entry->generator->dst = tmp;
230
231 fs_inst *copy;
232 if (written > dst_width) {
233 fs_reg *sources = ralloc_array(mem_ctx, fs_reg, written / dst_width);
234 for (int i = 0; i < written / dst_width; i++)
235 sources[i] = offset(tmp, i);
236 copy = LOAD_PAYLOAD(orig_dst, sources, written / dst_width);
237 } else {
238 copy = MOV(orig_dst, tmp);
239 copy->force_writemask_all =
240 entry->generator->force_writemask_all;
241 }
242 entry->generator->insert_after(block, copy);
243 }
244
245 /* dest <- temp */
246 if (!inst->dst.is_null()) {
247 int written = inst->regs_written;
248 int dst_width = inst->dst.width / 8;
249 assert(written == entry->generator->regs_written);
250 assert(dst_width == entry->generator->dst.width / 8);
251 assert(inst->dst.type == entry->tmp.type);
252 fs_reg dst = inst->dst;
253 fs_reg tmp = entry->tmp;
254 fs_inst *copy;
255 if (written > dst_width) {
256 fs_reg *sources = ralloc_array(mem_ctx, fs_reg, written / dst_width);
257 for (int i = 0; i < written / dst_width; i++)
258 sources[i] = offset(tmp, i);
259 copy = LOAD_PAYLOAD(dst, sources, written / dst_width);
260 } else {
261 copy = MOV(dst, tmp);
262 copy->force_writemask_all = inst->force_writemask_all;
263 }
264 inst->insert_before(block, copy);
265 }
266
267 /* Set our iterator so that next time through the loop inst->next
268 * will get the instruction in the basic block after the one we've
269 * removed.
270 */
271 fs_inst *prev = (fs_inst *)inst->prev;
272
273 inst->remove(block);
274 inst = prev;
275 }
276 }
277
278 foreach_in_list_safe(aeb_entry, entry, &aeb) {
279 /* Kill all AEB entries that write a different value to or read from
280 * the flag register if we just wrote it.
281 */
282 if (inst->writes_flag()) {
283 if (entry->generator->reads_flag() ||
284 (entry->generator->writes_flag() &&
285 !instructions_match(inst, entry->generator))) {
286 entry->remove();
287 ralloc_free(entry);
288 continue;
289 }
290 }
291
292 for (int i = 0; i < entry->generator->sources; i++) {
293 fs_reg *src_reg = &entry->generator->src[i];
294
295 /* Kill all AEB entries that use the destination we just
296 * overwrote.
297 */
298 if (inst->overwrites_reg(entry->generator->src[i])) {
299 entry->remove();
300 ralloc_free(entry);
301 break;
302 }
303
304 /* Kill any AEB entries using registers that don't get reused any
305 * more -- a sure sign they'll fail operands_match().
306 */
307 if (src_reg->file == GRF && virtual_grf_end[src_reg->reg] < ip) {
308 entry->remove();
309 ralloc_free(entry);
310 break;
311 }
312 }
313 }
314
315 ip++;
316 }
317
318 ralloc_free(cse_ctx);
319
320 return progress;
321 }
322
323 bool
324 fs_visitor::opt_cse()
325 {
326 bool progress = false;
327
328 calculate_live_intervals();
329
330 foreach_block (block, cfg) {
331 progress = opt_cse_local(block) || progress;
332 }
333
334 if (progress)
335 invalidate_live_intervals();
336
337 return progress;
338 }