523866efe19e6887effa3be2989a5e3d635e07f2
[mesa.git] / src / mesa / drivers / dri / i965 / brw_vec4_copy_propagation.cpp
1 /*
2 * Copyright © 2011 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 /**
25 * @file brw_vec4_copy_propagation.cpp
26 *
27 * Implements tracking of values copied between registers, and
28 * optimizations based on that: copy propagation and constant
29 * propagation.
30 */
31
32 #include "brw_vec4.h"
33 #include "brw_cfg.h"
34 extern "C" {
35 #include "main/macros.h"
36 }
37
38 namespace brw {
39
40 struct copy_entry {
41 src_reg *value[4];
42 int saturatemask;
43 };
44
45 static bool
46 is_direct_copy(vec4_instruction *inst)
47 {
48 return (inst->opcode == BRW_OPCODE_MOV &&
49 !inst->predicate &&
50 inst->dst.file == GRF &&
51 !inst->dst.reladdr &&
52 !inst->src[0].reladdr &&
53 (inst->dst.type == inst->src[0].type ||
54 (inst->dst.type == BRW_REGISTER_TYPE_F &&
55 inst->src[0].type == BRW_REGISTER_TYPE_VF)));
56 }
57
58 static bool
59 is_dominated_by_previous_instruction(vec4_instruction *inst)
60 {
61 return (inst->opcode != BRW_OPCODE_DO &&
62 inst->opcode != BRW_OPCODE_WHILE &&
63 inst->opcode != BRW_OPCODE_ELSE &&
64 inst->opcode != BRW_OPCODE_ENDIF);
65 }
66
67 static bool
68 is_channel_updated(vec4_instruction *inst, src_reg *values[4], int ch)
69 {
70 const src_reg *src = values[ch];
71
72 /* consider GRF only */
73 assert(inst->dst.file == GRF);
74 if (!src || src->file != GRF)
75 return false;
76
77 return (src->in_range(inst->dst, inst->regs_written) &&
78 inst->dst.writemask & (1 << BRW_GET_SWZ(src->swizzle, ch)));
79 }
80
81 static unsigned
82 swizzle_vf_imm(unsigned vf4, unsigned swizzle)
83 {
84 union {
85 unsigned vf4;
86 uint8_t vf[4];
87 } v = { vf4 }, ret;
88
89 ret.vf[0] = v.vf[BRW_GET_SWZ(swizzle, 0)];
90 ret.vf[1] = v.vf[BRW_GET_SWZ(swizzle, 1)];
91 ret.vf[2] = v.vf[BRW_GET_SWZ(swizzle, 2)];
92 ret.vf[3] = v.vf[BRW_GET_SWZ(swizzle, 3)];
93
94 return ret.vf4;
95 }
96
97 static bool
98 is_logic_op(enum opcode opcode)
99 {
100 return (opcode == BRW_OPCODE_AND ||
101 opcode == BRW_OPCODE_OR ||
102 opcode == BRW_OPCODE_XOR ||
103 opcode == BRW_OPCODE_NOT);
104 }
105
106 static bool
107 try_constant_propagate(const struct brw_device_info *devinfo,
108 vec4_instruction *inst,
109 int arg, struct copy_entry *entry)
110 {
111 /* For constant propagation, we only handle the same constant
112 * across all 4 channels. Some day, we should handle the 8-bit
113 * float vector format, which would let us constant propagate
114 * vectors better.
115 */
116 src_reg value = *entry->value[0];
117 for (int i = 1; i < 4; i++) {
118 if (!value.equals(*entry->value[i]))
119 return false;
120 }
121
122 if (value.file != IMM)
123 return false;
124
125 if (value.type == BRW_REGISTER_TYPE_VF) {
126 /* The result of bit-casting the component values of a vector float
127 * cannot in general be represented as an immediate.
128 */
129 if (inst->src[arg].type != BRW_REGISTER_TYPE_F)
130 return false;
131 } else {
132 value.type = inst->src[arg].type;
133 }
134
135 if (inst->src[arg].abs) {
136 if ((devinfo->gen >= 8 && is_logic_op(inst->opcode)) ||
137 !brw_abs_immediate(value.type, &value)) {
138 return false;
139 }
140 }
141
142 if (inst->src[arg].negate) {
143 if ((devinfo->gen >= 8 && is_logic_op(inst->opcode)) ||
144 !brw_negate_immediate(value.type, &value)) {
145 return false;
146 }
147 }
148
149 if (value.type == BRW_REGISTER_TYPE_VF)
150 value.ud = swizzle_vf_imm(value.ud,
151 inst->src[arg].swizzle);
152
153 switch (inst->opcode) {
154 case BRW_OPCODE_MOV:
155 case SHADER_OPCODE_BROADCAST:
156 inst->src[arg] = value;
157 return true;
158
159 case SHADER_OPCODE_POW:
160 case SHADER_OPCODE_INT_QUOTIENT:
161 case SHADER_OPCODE_INT_REMAINDER:
162 if (devinfo->gen < 8)
163 break;
164 /* fallthrough */
165 case BRW_OPCODE_DP2:
166 case BRW_OPCODE_DP3:
167 case BRW_OPCODE_DP4:
168 case BRW_OPCODE_DPH:
169 case BRW_OPCODE_BFI1:
170 case BRW_OPCODE_ASR:
171 case BRW_OPCODE_SHL:
172 case BRW_OPCODE_SHR:
173 case BRW_OPCODE_SUBB:
174 if (arg == 1) {
175 inst->src[arg] = value;
176 return true;
177 }
178 break;
179
180 case BRW_OPCODE_MACH:
181 case BRW_OPCODE_MUL:
182 case SHADER_OPCODE_MULH:
183 case BRW_OPCODE_ADD:
184 case BRW_OPCODE_OR:
185 case BRW_OPCODE_AND:
186 case BRW_OPCODE_XOR:
187 case BRW_OPCODE_ADDC:
188 if (arg == 1) {
189 inst->src[arg] = value;
190 return true;
191 } else if (arg == 0 && inst->src[1].file != IMM) {
192 /* Fit this constant in by commuting the operands. Exception: we
193 * can't do this for 32-bit integer MUL/MACH because it's asymmetric.
194 */
195 if ((inst->opcode == BRW_OPCODE_MUL ||
196 inst->opcode == BRW_OPCODE_MACH) &&
197 (inst->src[1].type == BRW_REGISTER_TYPE_D ||
198 inst->src[1].type == BRW_REGISTER_TYPE_UD))
199 break;
200 inst->src[0] = inst->src[1];
201 inst->src[1] = value;
202 return true;
203 }
204 break;
205 case GS_OPCODE_SET_WRITE_OFFSET:
206 /* This is just a multiply by a constant with special strides.
207 * The generator will handle immediates in both arguments (generating
208 * a single MOV of the product). So feel free to propagate in src0.
209 */
210 inst->src[arg] = value;
211 return true;
212
213 case BRW_OPCODE_CMP:
214 if (arg == 1) {
215 inst->src[arg] = value;
216 return true;
217 } else if (arg == 0 && inst->src[1].file != IMM) {
218 enum brw_conditional_mod new_cmod;
219
220 new_cmod = brw_swap_cmod(inst->conditional_mod);
221 if (new_cmod != BRW_CONDITIONAL_NONE) {
222 /* Fit this constant in by swapping the operands and
223 * flipping the test.
224 */
225 inst->src[0] = inst->src[1];
226 inst->src[1] = value;
227 inst->conditional_mod = new_cmod;
228 return true;
229 }
230 }
231 break;
232
233 case BRW_OPCODE_SEL:
234 if (arg == 1) {
235 inst->src[arg] = value;
236 return true;
237 } else if (arg == 0 && inst->src[1].file != IMM) {
238 inst->src[0] = inst->src[1];
239 inst->src[1] = value;
240
241 /* If this was predicated, flipping operands means
242 * we also need to flip the predicate.
243 */
244 if (inst->conditional_mod == BRW_CONDITIONAL_NONE) {
245 inst->predicate_inverse = !inst->predicate_inverse;
246 }
247 return true;
248 }
249 break;
250
251 default:
252 break;
253 }
254
255 return false;
256 }
257
258 static bool
259 try_copy_propagate(const struct brw_device_info *devinfo,
260 vec4_instruction *inst,
261 int arg, struct copy_entry *entry)
262 {
263 /* Build up the value we are propagating as if it were the source of a
264 * single MOV
265 */
266 /* For constant propagation, we only handle the same constant
267 * across all 4 channels. Some day, we should handle the 8-bit
268 * float vector format, which would let us constant propagate
269 * vectors better.
270 */
271 src_reg value = *entry->value[0];
272 for (int i = 1; i < 4; i++) {
273 /* This is equals() except we don't care about the swizzle. */
274 if (value.file != entry->value[i]->file ||
275 value.reg != entry->value[i]->reg ||
276 value.reg_offset != entry->value[i]->reg_offset ||
277 value.type != entry->value[i]->type ||
278 value.negate != entry->value[i]->negate ||
279 value.abs != entry->value[i]->abs) {
280 return false;
281 }
282 }
283
284 /* Compute the swizzle of the original register by swizzling the
285 * component loaded from each value according to the swizzle of
286 * operand we're going to change.
287 */
288 int s[4];
289 for (int i = 0; i < 4; i++) {
290 s[i] = BRW_GET_SWZ(entry->value[i]->swizzle, i);
291 }
292 value.swizzle = BRW_SWIZZLE4(s[0], s[1], s[2], s[3]);
293
294 /* Check that we can propagate that value */
295 if (value.file != UNIFORM &&
296 value.file != GRF &&
297 value.file != ATTR)
298 return false;
299
300 if (devinfo->gen >= 8 && (value.negate || value.abs) &&
301 is_logic_op(inst->opcode)) {
302 return false;
303 }
304
305 bool has_source_modifiers = value.negate || value.abs;
306
307 /* gen6 math and gen7+ SENDs from GRFs ignore source modifiers on
308 * instructions.
309 */
310 if ((has_source_modifiers || value.file == UNIFORM ||
311 value.swizzle != BRW_SWIZZLE_XYZW) && !inst->can_do_source_mods(devinfo))
312 return false;
313
314 if (has_source_modifiers &&
315 value.type != inst->src[arg].type &&
316 !inst->can_change_types())
317 return false;
318
319 if (has_source_modifiers &&
320 inst->opcode == SHADER_OPCODE_GEN4_SCRATCH_WRITE)
321 return false;
322
323 unsigned composed_swizzle = brw_compose_swizzle(inst->src[arg].swizzle,
324 value.swizzle);
325 if (inst->is_3src() &&
326 value.file == UNIFORM &&
327 !brw_is_single_value_swizzle(composed_swizzle))
328 return false;
329
330 if (inst->is_send_from_grf())
331 return false;
332
333 /* we can't generally copy-propagate UD negations becuse we
334 * end up accessing the resulting values as signed integers
335 * instead. See also resolve_ud_negate().
336 */
337 if (value.negate &&
338 value.type == BRW_REGISTER_TYPE_UD)
339 return false;
340
341 /* Don't report progress if this is a noop. */
342 if (value.equals(inst->src[arg]))
343 return false;
344
345 const unsigned dst_saturate_mask = inst->dst.writemask &
346 brw_apply_swizzle_to_mask(inst->src[arg].swizzle, entry->saturatemask);
347
348 if (dst_saturate_mask) {
349 /* We either saturate all or nothing. */
350 if (dst_saturate_mask != inst->dst.writemask)
351 return false;
352
353 /* Limit saturate propagation only to SEL with src1 bounded within 0.0
354 * and 1.0, otherwise skip copy propagate altogether.
355 */
356 switch(inst->opcode) {
357 case BRW_OPCODE_SEL:
358 if (arg != 0 ||
359 inst->src[0].type != BRW_REGISTER_TYPE_F ||
360 inst->src[1].file != IMM ||
361 inst->src[1].type != BRW_REGISTER_TYPE_F ||
362 inst->src[1].f < 0.0 ||
363 inst->src[1].f > 1.0) {
364 return false;
365 }
366 if (!inst->saturate)
367 inst->saturate = true;
368 break;
369 default:
370 return false;
371 }
372 }
373
374 /* Build the final value */
375 if (inst->src[arg].abs) {
376 value.negate = false;
377 value.abs = true;
378 }
379 if (inst->src[arg].negate)
380 value.negate = !value.negate;
381
382 value.swizzle = composed_swizzle;
383 if (has_source_modifiers &&
384 value.type != inst->src[arg].type) {
385 assert(inst->can_change_types());
386 for (int i = 0; i < 3; i++) {
387 inst->src[i].type = value.type;
388 }
389 inst->dst.type = value.type;
390 } else {
391 value.type = inst->src[arg].type;
392 }
393
394 inst->src[arg] = value;
395 return true;
396 }
397
398 bool
399 vec4_visitor::opt_copy_propagation(bool do_constant_prop)
400 {
401 bool progress = false;
402 struct copy_entry entries[alloc.total_size];
403
404 memset(&entries, 0, sizeof(entries));
405
406 foreach_block_and_inst(block, vec4_instruction, inst, cfg) {
407 /* This pass only works on basic blocks. If there's flow
408 * control, throw out all our information and start from
409 * scratch.
410 *
411 * This should really be fixed by using a structure like in
412 * src/glsl/opt_copy_propagation.cpp to track available copies.
413 */
414 if (!is_dominated_by_previous_instruction(inst)) {
415 memset(&entries, 0, sizeof(entries));
416 continue;
417 }
418
419 /* For each source arg, see if each component comes from a copy
420 * from the same type file (IMM, GRF, UNIFORM), and try
421 * optimizing out access to the copy result
422 */
423 for (int i = 2; i >= 0; i--) {
424 /* Copied values end up in GRFs, and we don't track reladdr
425 * accesses.
426 */
427 if (inst->src[i].file != GRF ||
428 inst->src[i].reladdr)
429 continue;
430
431 /* We only handle single-register copies. */
432 if (inst->regs_read(i) != 1)
433 continue;
434
435 int reg = (alloc.offsets[inst->src[i].reg] +
436 inst->src[i].reg_offset);
437
438 /* Find the regs that each swizzle component came from.
439 */
440 struct copy_entry entry;
441 memset(&entry, 0, sizeof(copy_entry));
442 int c;
443 for (c = 0; c < 4; c++) {
444 int channel = BRW_GET_SWZ(inst->src[i].swizzle, c);
445 entry.value[c] = entries[reg].value[channel];
446
447 /* If there's no available copy for this channel, bail.
448 * We could be more aggressive here -- some channels might
449 * not get used based on the destination writemask.
450 */
451 if (!entry.value[c])
452 break;
453
454 entry.saturatemask |=
455 (entries[reg].saturatemask & (1 << channel) ? 1 : 0) << c;
456
457 /* We'll only be able to copy propagate if the sources are
458 * all from the same file -- there's no ability to swizzle
459 * 0 or 1 constants in with source registers like in i915.
460 */
461 if (c > 0 && entry.value[c - 1]->file != entry.value[c]->file)
462 break;
463 }
464
465 if (c != 4)
466 continue;
467
468 if (do_constant_prop && try_constant_propagate(devinfo, inst, i, &entry))
469 progress = true;
470
471 if (try_copy_propagate(devinfo, inst, i, &entry))
472 progress = true;
473 }
474
475 /* Track available source registers. */
476 if (inst->dst.file == GRF) {
477 const int reg =
478 alloc.offsets[inst->dst.reg] + inst->dst.reg_offset;
479
480 /* Update our destination's current channel values. For a direct copy,
481 * the value is the newly propagated source. Otherwise, we don't know
482 * the new value, so clear it.
483 */
484 bool direct_copy = is_direct_copy(inst);
485 entries[reg].saturatemask &= ~inst->dst.writemask;
486 for (int i = 0; i < 4; i++) {
487 if (inst->dst.writemask & (1 << i)) {
488 entries[reg].value[i] = direct_copy ? &inst->src[0] : NULL;
489 entries[reg].saturatemask |=
490 inst->saturate && direct_copy ? 1 << i : 0;
491 }
492 }
493
494 /* Clear the records for any registers whose current value came from
495 * our destination's updated channels, as the two are no longer equal.
496 */
497 if (inst->dst.reladdr)
498 memset(&entries, 0, sizeof(entries));
499 else {
500 for (unsigned i = 0; i < alloc.total_size; i++) {
501 for (int j = 0; j < 4; j++) {
502 if (is_channel_updated(inst, entries[i].value, j)) {
503 entries[i].value[j] = NULL;
504 entries[i].saturatemask &= ~(1 << j);
505 }
506 }
507 }
508 }
509 }
510 }
511
512 if (progress)
513 invalidate_live_intervals();
514
515 return progress;
516 }
517
518 } /* namespace brw */