i965/fs: Fix CSE temporary copy for some LOAD_PAYLOAD corner cases.
[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 using namespace brw;
36
37 namespace {
38 struct aeb_entry : public exec_node {
39 /** The instruction that generates the expression value. */
40 fs_inst *generator;
41
42 /** The temporary where the value is stored. */
43 fs_reg tmp;
44 };
45 }
46
47 static bool
48 is_expression(const fs_visitor *v, const fs_inst *const inst)
49 {
50 switch (inst->opcode) {
51 case BRW_OPCODE_MOV:
52 case BRW_OPCODE_SEL:
53 case BRW_OPCODE_NOT:
54 case BRW_OPCODE_AND:
55 case BRW_OPCODE_OR:
56 case BRW_OPCODE_XOR:
57 case BRW_OPCODE_SHR:
58 case BRW_OPCODE_SHL:
59 case BRW_OPCODE_ASR:
60 case BRW_OPCODE_CMP:
61 case BRW_OPCODE_CMPN:
62 case BRW_OPCODE_ADD:
63 case BRW_OPCODE_MUL:
64 case SHADER_OPCODE_MULH:
65 case BRW_OPCODE_FRC:
66 case BRW_OPCODE_RNDU:
67 case BRW_OPCODE_RNDD:
68 case BRW_OPCODE_RNDE:
69 case BRW_OPCODE_RNDZ:
70 case BRW_OPCODE_LINE:
71 case BRW_OPCODE_PLN:
72 case BRW_OPCODE_MAD:
73 case BRW_OPCODE_LRP:
74 case FS_OPCODE_UNIFORM_PULL_CONSTANT_LOAD:
75 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD_GEN7:
76 case FS_OPCODE_VARYING_PULL_CONSTANT_LOAD:
77 case FS_OPCODE_CINTERP:
78 case FS_OPCODE_LINTERP:
79 case SHADER_OPCODE_FIND_LIVE_CHANNEL:
80 case SHADER_OPCODE_BROADCAST:
81 case SHADER_OPCODE_EXTRACT_BYTE:
82 case SHADER_OPCODE_EXTRACT_WORD:
83 case SHADER_OPCODE_MOV_INDIRECT:
84 case FS_OPCODE_PACK:
85 return true;
86 case SHADER_OPCODE_RCP:
87 case SHADER_OPCODE_RSQ:
88 case SHADER_OPCODE_SQRT:
89 case SHADER_OPCODE_EXP2:
90 case SHADER_OPCODE_LOG2:
91 case SHADER_OPCODE_POW:
92 case SHADER_OPCODE_INT_QUOTIENT:
93 case SHADER_OPCODE_INT_REMAINDER:
94 case SHADER_OPCODE_SIN:
95 case SHADER_OPCODE_COS:
96 return inst->mlen < 2;
97 case SHADER_OPCODE_LOAD_PAYLOAD:
98 return !inst->is_copy_payload(v->alloc);
99 default:
100 return inst->is_send_from_grf() && !inst->has_side_effects() &&
101 !inst->is_volatile();
102 }
103 }
104
105 static bool
106 operands_match(const fs_inst *a, const fs_inst *b, bool *negate)
107 {
108 fs_reg *xs = a->src;
109 fs_reg *ys = b->src;
110
111 if (a->opcode == BRW_OPCODE_MAD) {
112 return xs[0].equals(ys[0]) &&
113 ((xs[1].equals(ys[1]) && xs[2].equals(ys[2])) ||
114 (xs[2].equals(ys[1]) && xs[1].equals(ys[2])));
115 } else if (a->opcode == BRW_OPCODE_MUL && a->dst.type == BRW_REGISTER_TYPE_F) {
116 bool xs0_negate = xs[0].negate;
117 bool xs1_negate = xs[1].file == IMM ? xs[1].f < 0.0f
118 : xs[1].negate;
119 bool ys0_negate = ys[0].negate;
120 bool ys1_negate = ys[1].file == IMM ? ys[1].f < 0.0f
121 : ys[1].negate;
122 float xs1_imm = xs[1].f;
123 float ys1_imm = ys[1].f;
124
125 xs[0].negate = false;
126 xs[1].negate = false;
127 ys[0].negate = false;
128 ys[1].negate = false;
129 xs[1].f = fabsf(xs[1].f);
130 ys[1].f = fabsf(ys[1].f);
131
132 bool ret = (xs[0].equals(ys[0]) && xs[1].equals(ys[1])) ||
133 (xs[1].equals(ys[0]) && xs[0].equals(ys[1]));
134
135 xs[0].negate = xs0_negate;
136 xs[1].negate = xs[1].file == IMM ? false : xs1_negate;
137 ys[0].negate = ys0_negate;
138 ys[1].negate = ys[1].file == IMM ? false : ys1_negate;
139 xs[1].f = xs1_imm;
140 ys[1].f = ys1_imm;
141
142 *negate = (xs0_negate != xs1_negate) != (ys0_negate != ys1_negate);
143 if (*negate && (a->saturate || b->saturate))
144 return false;
145 return ret;
146 } else if (!a->is_commutative()) {
147 bool match = true;
148 for (int i = 0; i < a->sources; i++) {
149 if (!xs[i].equals(ys[i])) {
150 match = false;
151 break;
152 }
153 }
154 return match;
155 } else {
156 return (xs[0].equals(ys[0]) && xs[1].equals(ys[1])) ||
157 (xs[1].equals(ys[0]) && xs[0].equals(ys[1]));
158 }
159 }
160
161 static bool
162 instructions_match(fs_inst *a, fs_inst *b, bool *negate)
163 {
164 return a->opcode == b->opcode &&
165 a->force_writemask_all == b->force_writemask_all &&
166 a->exec_size == b->exec_size &&
167 a->force_sechalf == b->force_sechalf &&
168 a->saturate == b->saturate &&
169 a->predicate == b->predicate &&
170 a->predicate_inverse == b->predicate_inverse &&
171 a->conditional_mod == b->conditional_mod &&
172 a->flag_subreg == b->flag_subreg &&
173 a->dst.type == b->dst.type &&
174 a->offset == b->offset &&
175 a->mlen == b->mlen &&
176 a->regs_written == b->regs_written &&
177 a->base_mrf == b->base_mrf &&
178 a->eot == b->eot &&
179 a->header_size == b->header_size &&
180 a->shadow_compare == b->shadow_compare &&
181 a->pi_noperspective == b->pi_noperspective &&
182 a->sources == b->sources &&
183 operands_match(a, b, negate);
184 }
185
186 static void
187 create_copy_instr(const fs_builder &bld, fs_inst *inst, fs_reg src, bool negate)
188 {
189 int written = inst->regs_written;
190 int dst_width =
191 DIV_ROUND_UP(inst->dst.component_size(inst->exec_size), REG_SIZE);
192 fs_inst *copy;
193
194 if (inst->opcode == SHADER_OPCODE_LOAD_PAYLOAD ||
195 written != dst_width) {
196 fs_reg *payload;
197 int sources, header_size;
198 if (inst->opcode == SHADER_OPCODE_LOAD_PAYLOAD) {
199 sources = inst->sources;
200 header_size = inst->header_size;
201 } else {
202 assert(written % dst_width == 0);
203 sources = written / dst_width;
204 header_size = 0;
205 }
206
207 assert(src.file == VGRF);
208 payload = ralloc_array(bld.shader->mem_ctx, fs_reg, sources);
209 for (int i = 0; i < header_size; i++) {
210 payload[i] = src;
211 src.reg_offset++;
212 }
213 for (int i = header_size; i < sources; i++) {
214 payload[i] = src;
215 src = offset(src, bld, 1);
216 }
217 copy = bld.LOAD_PAYLOAD(inst->dst, payload, sources, header_size);
218 } else {
219 copy = bld.MOV(inst->dst, src);
220 copy->force_sechalf = inst->force_sechalf;
221 copy->force_writemask_all = inst->force_writemask_all;
222 copy->src[0].negate = negate;
223 }
224 assert(copy->regs_written == written);
225 }
226
227 bool
228 fs_visitor::opt_cse_local(bblock_t *block)
229 {
230 bool progress = false;
231 exec_list aeb;
232
233 void *cse_ctx = ralloc_context(NULL);
234
235 int ip = block->start_ip;
236 foreach_inst_in_block(fs_inst, inst, block) {
237 /* Skip some cases. */
238 if (is_expression(this, inst) && !inst->is_partial_write() &&
239 ((inst->dst.file != ARF && inst->dst.file != FIXED_GRF) ||
240 inst->dst.is_null()))
241 {
242 bool found = false;
243 bool negate = false;
244
245 foreach_in_list_use_after(aeb_entry, entry, &aeb) {
246 /* Match current instruction's expression against those in AEB. */
247 if (!(entry->generator->dst.is_null() && !inst->dst.is_null()) &&
248 instructions_match(inst, entry->generator, &negate)) {
249 found = true;
250 progress = true;
251 break;
252 }
253 }
254
255 if (!found) {
256 if (inst->opcode != BRW_OPCODE_MOV ||
257 (inst->opcode == BRW_OPCODE_MOV &&
258 inst->src[0].file == IMM &&
259 inst->src[0].type == BRW_REGISTER_TYPE_VF)) {
260 /* Our first sighting of this expression. Create an entry. */
261 aeb_entry *entry = ralloc(cse_ctx, aeb_entry);
262 entry->tmp = reg_undef;
263 entry->generator = inst;
264 aeb.push_tail(entry);
265 }
266 } else {
267 /* This is at least our second sighting of this expression.
268 * If we don't have a temporary already, make one.
269 */
270 bool no_existing_temp = entry->tmp.file == BAD_FILE;
271 if (no_existing_temp && !entry->generator->dst.is_null()) {
272 const fs_builder ibld = fs_builder(this, block, entry->generator)
273 .at(block, entry->generator->next);
274 int written = entry->generator->regs_written;
275
276 entry->tmp = fs_reg(VGRF, alloc.allocate(written),
277 entry->generator->dst.type);
278
279 create_copy_instr(ibld, entry->generator, entry->tmp, false);
280
281 entry->generator->dst = entry->tmp;
282 }
283
284 /* dest <- temp */
285 if (!inst->dst.is_null()) {
286 assert(inst->regs_written == entry->generator->regs_written);
287 assert(inst->dst.type == entry->tmp.type);
288 const fs_builder ibld(this, block, inst);
289
290 create_copy_instr(ibld, inst, entry->tmp, negate);
291 }
292
293 /* Set our iterator so that next time through the loop inst->next
294 * will get the instruction in the basic block after the one we've
295 * removed.
296 */
297 fs_inst *prev = (fs_inst *)inst->prev;
298
299 inst->remove(block);
300 inst = prev;
301 }
302 }
303
304 foreach_in_list_safe(aeb_entry, entry, &aeb) {
305 /* Kill all AEB entries that write a different value to or read from
306 * the flag register if we just wrote it.
307 */
308 if (inst->writes_flag()) {
309 bool negate; /* dummy */
310 if (entry->generator->reads_flag() ||
311 (entry->generator->writes_flag() &&
312 !instructions_match(inst, entry->generator, &negate))) {
313 entry->remove();
314 ralloc_free(entry);
315 continue;
316 }
317 }
318
319 for (int i = 0; i < entry->generator->sources; i++) {
320 fs_reg *src_reg = &entry->generator->src[i];
321
322 /* Kill all AEB entries that use the destination we just
323 * overwrote.
324 */
325 if (inst->overwrites_reg(entry->generator->src[i])) {
326 entry->remove();
327 ralloc_free(entry);
328 break;
329 }
330
331 /* Kill any AEB entries using registers that don't get reused any
332 * more -- a sure sign they'll fail operands_match().
333 */
334 if (src_reg->file == VGRF && virtual_grf_end[src_reg->nr] < ip) {
335 entry->remove();
336 ralloc_free(entry);
337 break;
338 }
339 }
340 }
341
342 ip++;
343 }
344
345 ralloc_free(cse_ctx);
346
347 return progress;
348 }
349
350 bool
351 fs_visitor::opt_cse()
352 {
353 bool progress = false;
354
355 calculate_live_intervals();
356
357 foreach_block (block, cfg) {
358 progress = opt_cse_local(block) || progress;
359 }
360
361 if (progress)
362 invalidate_live_intervals();
363
364 return progress;
365 }