e739f792cf80ac3e3872157e72b199d77c16b94d
[mesa.git] / src / freedreno / ir3 / ir3_cp.c
1 /*
2 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
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 * Authors:
24 * Rob Clark <robclark@freedesktop.org>
25 */
26
27 #include <math.h>
28 #include "util/half_float.h"
29 #include "util/u_math.h"
30
31 #include "ir3.h"
32 #include "ir3_compiler.h"
33 #include "ir3_shader.h"
34
35 #define swap(a, b) \
36 do { __typeof(a) __tmp = (a); (a) = (b); (b) = __tmp; } while (0)
37
38 /*
39 * Copy Propagate:
40 */
41
42 struct ir3_cp_ctx {
43 struct ir3 *shader;
44 struct ir3_shader_variant *so;
45 bool progress;
46 };
47
48 /* is it a type preserving mov, with ok flags?
49 *
50 * @instr: the mov to consider removing
51 * @dst_instr: the instruction consuming the mov (instr)
52 *
53 * TODO maybe drop allow_flags since this is only false when dst is
54 * NULL (ie. outputs)
55 */
56 static bool is_eligible_mov(struct ir3_instruction *instr,
57 struct ir3_instruction *dst_instr, bool allow_flags)
58 {
59 if (is_same_type_mov(instr)) {
60 struct ir3_register *dst = instr->regs[0];
61 struct ir3_register *src = instr->regs[1];
62 struct ir3_instruction *src_instr = ssa(src);
63
64 /* only if mov src is SSA (not const/immed): */
65 if (!src_instr)
66 return false;
67
68 /* no indirect: */
69 if (dst->flags & IR3_REG_RELATIV)
70 return false;
71 if (src->flags & IR3_REG_RELATIV)
72 return false;
73
74 if (src->flags & IR3_REG_ARRAY)
75 return false;
76
77 if (!allow_flags)
78 if (src->flags & (IR3_REG_FABS | IR3_REG_FNEG |
79 IR3_REG_SABS | IR3_REG_SNEG | IR3_REG_BNOT))
80 return false;
81
82 /* If src is coming from fanout/split (ie. one component of a
83 * texture fetch, etc) and we have constraints on swizzle of
84 * destination, then skip it.
85 *
86 * We could possibly do a bit better, and copy-propagation if
87 * we can CP all components that are being fanned out.
88 */
89 if (src_instr->opc == OPC_META_SPLIT) {
90 if (!dst_instr)
91 return false;
92 if (dst_instr->opc == OPC_META_COLLECT)
93 return false;
94 if (dst_instr->cp.left || dst_instr->cp.right)
95 return false;
96 }
97
98 return true;
99 }
100 return false;
101 }
102
103 /* propagate register flags from src to dst.. negates need special
104 * handling to cancel each other out.
105 */
106 static void combine_flags(unsigned *dstflags, struct ir3_instruction *src)
107 {
108 unsigned srcflags = src->regs[1]->flags;
109
110 /* if what we are combining into already has (abs) flags,
111 * we can drop (neg) from src:
112 */
113 if (*dstflags & IR3_REG_FABS)
114 srcflags &= ~IR3_REG_FNEG;
115 if (*dstflags & IR3_REG_SABS)
116 srcflags &= ~IR3_REG_SNEG;
117
118 if (srcflags & IR3_REG_FABS)
119 *dstflags |= IR3_REG_FABS;
120 if (srcflags & IR3_REG_SABS)
121 *dstflags |= IR3_REG_SABS;
122 if (srcflags & IR3_REG_FNEG)
123 *dstflags ^= IR3_REG_FNEG;
124 if (srcflags & IR3_REG_SNEG)
125 *dstflags ^= IR3_REG_SNEG;
126 if (srcflags & IR3_REG_BNOT)
127 *dstflags ^= IR3_REG_BNOT;
128
129 *dstflags &= ~IR3_REG_SSA;
130 *dstflags |= srcflags & IR3_REG_SSA;
131 *dstflags |= srcflags & IR3_REG_CONST;
132 *dstflags |= srcflags & IR3_REG_IMMED;
133 *dstflags |= srcflags & IR3_REG_RELATIV;
134 *dstflags |= srcflags & IR3_REG_ARRAY;
135 *dstflags |= srcflags & IR3_REG_HIGH;
136
137 /* if src of the src is boolean we can drop the (abs) since we know
138 * the source value is already a postitive integer. This cleans
139 * up the absnegs that get inserted when converting between nir and
140 * native boolean (see ir3_b2n/n2b)
141 */
142 struct ir3_instruction *srcsrc = ssa(src->regs[1]);
143 if (srcsrc && is_bool(srcsrc))
144 *dstflags &= ~IR3_REG_SABS;
145 }
146
147 /* Tries lowering an immediate register argument to a const buffer access by
148 * adding to the list of immediates to be pushed to the const buffer when
149 * switching to this shader.
150 */
151 static bool
152 lower_immed(struct ir3_cp_ctx *ctx, struct ir3_instruction *instr, unsigned n,
153 struct ir3_register *reg, unsigned new_flags)
154 {
155 if (!(new_flags & IR3_REG_IMMED))
156 return false;
157
158 new_flags &= ~IR3_REG_IMMED;
159 new_flags |= IR3_REG_CONST;
160
161 if (!ir3_valid_flags(instr, n, new_flags))
162 return false;
163
164 unsigned swiz, idx, i;
165
166 reg = ir3_reg_clone(ctx->shader, reg);
167
168 /* Half constant registers seems to handle only 32-bit values
169 * within floating-point opcodes. So convert back to 32-bit values.
170 */
171 bool f_opcode = (is_cat2_float(instr->opc) ||
172 is_cat3_float(instr->opc)) ? true : false;
173 if (f_opcode && (new_flags & IR3_REG_HALF))
174 reg->uim_val = fui(_mesa_half_to_float(reg->uim_val));
175
176 /* in some cases, there are restrictions on (abs)/(neg) plus const..
177 * so just evaluate those and clear the flags:
178 */
179 if (new_flags & IR3_REG_SABS) {
180 reg->iim_val = abs(reg->iim_val);
181 new_flags &= ~IR3_REG_SABS;
182 }
183
184 if (new_flags & IR3_REG_FABS) {
185 reg->fim_val = fabs(reg->fim_val);
186 new_flags &= ~IR3_REG_FABS;
187 }
188
189 if (new_flags & IR3_REG_SNEG) {
190 reg->iim_val = -reg->iim_val;
191 new_flags &= ~IR3_REG_SNEG;
192 }
193
194 if (new_flags & IR3_REG_FNEG) {
195 reg->fim_val = -reg->fim_val;
196 new_flags &= ~IR3_REG_FNEG;
197 }
198
199 /* Reallocate for 4 more elements whenever it's necessary */
200 struct ir3_const_state *const_state = ir3_const_state(ctx->so);
201 if (const_state->immediate_idx == const_state->immediates_size * 4) {
202 const_state->immediates = rerzalloc(const_state,
203 const_state->immediates,
204 __typeof__(const_state->immediates[0]),
205 const_state->immediates_size,
206 const_state->immediates_size + 4);
207 const_state->immediates_size += 4;
208
209 for (int i = const_state->immediate_idx; i < const_state->immediates_size * 4; i++)
210 const_state->immediates[i / 4].val[i % 4] = 0xd0d0d0d0;
211 }
212
213 for (i = 0; i < const_state->immediate_idx; i++) {
214 swiz = i % 4;
215 idx = i / 4;
216
217 if (const_state->immediates[idx].val[swiz] == reg->uim_val) {
218 break;
219 }
220 }
221
222 if (i == const_state->immediate_idx) {
223 struct ir3_compiler *compiler = instr->block->shader->compiler;
224 /* Add on a new immediate to be pushed, if we have space left in the
225 * constbuf.
226 */
227 if (const_state->offsets.immediate + const_state->immediate_idx / 4 >=
228 compiler->max_const)
229 return false;
230
231 swiz = i % 4;
232 idx = i / 4;
233
234 const_state->immediates[idx].val[swiz] = reg->uim_val;
235 const_state->immediates_count = idx + 1;
236 const_state->immediate_idx++;
237 }
238
239 reg->flags = new_flags;
240 reg->num = i + (4 * const_state->offsets.immediate);
241
242 instr->regs[n + 1] = reg;
243
244 return true;
245 }
246
247 static void
248 unuse(struct ir3_instruction *instr)
249 {
250 debug_assert(instr->use_count > 0);
251
252 if (--instr->use_count == 0) {
253 struct ir3_block *block = instr->block;
254
255 instr->barrier_class = 0;
256 instr->barrier_conflict = 0;
257
258 /* we don't want to remove anything in keeps (which could
259 * be things like array store's)
260 */
261 for (unsigned i = 0; i < block->keeps_count; i++) {
262 debug_assert(block->keeps[i] != instr);
263 }
264 }
265 }
266
267 /**
268 * Handles the special case of the 2nd src (n == 1) to "normal" mad
269 * instructions, which cannot reference a constant. See if it is
270 * possible to swap the 1st and 2nd sources.
271 */
272 static bool
273 try_swap_mad_two_srcs(struct ir3_instruction *instr, unsigned new_flags)
274 {
275 if (!is_mad(instr->opc))
276 return false;
277
278 /* NOTE: pre-swap first two src's before valid_flags(),
279 * which might try to dereference the n'th src:
280 */
281 swap(instr->regs[0 + 1], instr->regs[1 + 1]);
282
283 /* cat3 doesn't encode immediate, but we can lower immediate
284 * to const if that helps:
285 */
286 if (new_flags & IR3_REG_IMMED) {
287 new_flags &= ~IR3_REG_IMMED;
288 new_flags |= IR3_REG_CONST;
289 }
290
291 bool valid_swap =
292 /* can we propagate mov if we move 2nd src to first? */
293 ir3_valid_flags(instr, 0, new_flags) &&
294 /* and does first src fit in second slot? */
295 ir3_valid_flags(instr, 1, instr->regs[1 + 1]->flags);
296
297 if (!valid_swap) {
298 /* put things back the way they were: */
299 swap(instr->regs[0 + 1], instr->regs[1 + 1]);
300 } /* otherwise leave things swapped */
301
302 return valid_swap;
303 }
304
305 /**
306 * Handle cp for a given src register. This additionally handles
307 * the cases of collapsing immedate/const (which replace the src
308 * register with a non-ssa src) or collapsing mov's from relative
309 * src (which needs to also fixup the address src reference by the
310 * instruction).
311 */
312 static bool
313 reg_cp(struct ir3_cp_ctx *ctx, struct ir3_instruction *instr,
314 struct ir3_register *reg, unsigned n)
315 {
316 struct ir3_instruction *src = ssa(reg);
317
318 if (is_eligible_mov(src, instr, true)) {
319 /* simple case, no immed/const/relativ, only mov's w/ ssa src: */
320 struct ir3_register *src_reg = src->regs[1];
321 unsigned new_flags = reg->flags;
322
323 combine_flags(&new_flags, src);
324
325 if (ir3_valid_flags(instr, n, new_flags)) {
326 if (new_flags & IR3_REG_ARRAY) {
327 debug_assert(!(reg->flags & IR3_REG_ARRAY));
328 reg->array = src_reg->array;
329 }
330 reg->flags = new_flags;
331 reg->instr = ssa(src_reg);
332
333 instr->barrier_class |= src->barrier_class;
334 instr->barrier_conflict |= src->barrier_conflict;
335
336 unuse(src);
337 reg->instr->use_count++;
338
339 return true;
340 }
341 } else if ((is_same_type_mov(src) || is_const_mov(src)) &&
342 /* cannot collapse const/immed/etc into meta instrs: */
343 !is_meta(instr)) {
344 /* immed/const/etc cases, which require some special handling: */
345 struct ir3_register *src_reg = src->regs[1];
346 unsigned new_flags = reg->flags;
347
348 combine_flags(&new_flags, src);
349
350 if (!ir3_valid_flags(instr, n, new_flags)) {
351 /* See if lowering an immediate to const would help. */
352 if (lower_immed(ctx, instr, n, src_reg, new_flags))
353 return true;
354
355 /* special case for "normal" mad instructions, we can
356 * try swapping the first two args if that fits better.
357 *
358 * the "plain" MAD's (ie. the ones that don't shift first
359 * src prior to multiply) can swap their first two srcs if
360 * src[0] is !CONST and src[1] is CONST:
361 */
362 if ((n == 1) && try_swap_mad_two_srcs(instr, new_flags)) {
363 return true;
364 } else {
365 return false;
366 }
367 }
368
369 /* Here we handle the special case of mov from
370 * CONST and/or RELATIV. These need to be handled
371 * specially, because in the case of move from CONST
372 * there is no src ir3_instruction so we need to
373 * replace the ir3_register. And in the case of
374 * RELATIV we need to handle the address register
375 * dependency.
376 */
377 if (src_reg->flags & IR3_REG_CONST) {
378 /* an instruction cannot reference two different
379 * address registers:
380 */
381 if ((src_reg->flags & IR3_REG_RELATIV) &&
382 conflicts(instr->address, reg->instr->address))
383 return false;
384
385 /* This seems to be a hw bug, or something where the timings
386 * just somehow don't work out. This restriction may only
387 * apply if the first src is also CONST.
388 */
389 if ((opc_cat(instr->opc) == 3) && (n == 2) &&
390 (src_reg->flags & IR3_REG_RELATIV) &&
391 (src_reg->array.offset == 0))
392 return false;
393
394 /* When narrowing constant from 32b to 16b, it seems
395 * to work only for float. So we should do this only with
396 * float opcodes.
397 */
398 if (src->cat1.dst_type == TYPE_F16) {
399 if (instr->opc == OPC_MOV && !type_float(instr->cat1.src_type))
400 return false;
401 if (!is_cat2_float(instr->opc) && !is_cat3_float(instr->opc))
402 return false;
403 }
404
405 src_reg = ir3_reg_clone(instr->block->shader, src_reg);
406 src_reg->flags = new_flags;
407 instr->regs[n+1] = src_reg;
408
409 if (src_reg->flags & IR3_REG_RELATIV)
410 ir3_instr_set_address(instr, reg->instr->address);
411
412 return true;
413 }
414
415 if ((src_reg->flags & IR3_REG_RELATIV) &&
416 !conflicts(instr->address, reg->instr->address)) {
417 src_reg = ir3_reg_clone(instr->block->shader, src_reg);
418 src_reg->flags = new_flags;
419 instr->regs[n+1] = src_reg;
420 ir3_instr_set_address(instr, reg->instr->address);
421
422 return true;
423 }
424
425 /* NOTE: seems we can only do immed integers, so don't
426 * need to care about float. But we do need to handle
427 * abs/neg *before* checking that the immediate requires
428 * few enough bits to encode:
429 *
430 * TODO: do we need to do something to avoid accidentally
431 * catching a float immed?
432 */
433 if (src_reg->flags & IR3_REG_IMMED) {
434 int32_t iim_val = src_reg->iim_val;
435
436 debug_assert((opc_cat(instr->opc) == 1) ||
437 (opc_cat(instr->opc) == 6) ||
438 ir3_cat2_int(instr->opc) ||
439 (is_mad(instr->opc) && (n == 0)));
440
441 if (new_flags & IR3_REG_SABS)
442 iim_val = abs(iim_val);
443
444 if (new_flags & IR3_REG_SNEG)
445 iim_val = -iim_val;
446
447 if (new_flags & IR3_REG_BNOT)
448 iim_val = ~iim_val;
449
450 /* other than category 1 (mov) we can only encode up to 10 bits: */
451 if (ir3_valid_flags(instr, n, new_flags) &&
452 ((instr->opc == OPC_MOV) ||
453 !((iim_val & ~0x3ff) && (-iim_val & ~0x3ff)))) {
454 new_flags &= ~(IR3_REG_SABS | IR3_REG_SNEG | IR3_REG_BNOT);
455 src_reg = ir3_reg_clone(instr->block->shader, src_reg);
456 src_reg->flags = new_flags;
457 src_reg->iim_val = iim_val;
458 instr->regs[n+1] = src_reg;
459
460 return true;
461 } else if (lower_immed(ctx, instr, n, src_reg, new_flags)) {
462 /* Fell back to loading the immediate as a const */
463 return true;
464 }
465 }
466 }
467
468 return false;
469 }
470
471 /* Handle special case of eliminating output mov, and similar cases where
472 * there isn't a normal "consuming" instruction. In this case we cannot
473 * collapse flags (ie. output mov from const, or w/ abs/neg flags, cannot
474 * be eliminated)
475 */
476 static struct ir3_instruction *
477 eliminate_output_mov(struct ir3_cp_ctx *ctx, struct ir3_instruction *instr)
478 {
479 if (is_eligible_mov(instr, NULL, false)) {
480 struct ir3_register *reg = instr->regs[1];
481 if (!(reg->flags & IR3_REG_ARRAY)) {
482 struct ir3_instruction *src_instr = ssa(reg);
483 debug_assert(src_instr);
484 ctx->progress = true;
485 return src_instr;
486 }
487 }
488 return instr;
489 }
490
491 /**
492 * Find instruction src's which are mov's that can be collapsed, replacing
493 * the mov dst with the mov src
494 */
495 static void
496 instr_cp(struct ir3_cp_ctx *ctx, struct ir3_instruction *instr)
497 {
498 if (instr->regs_count == 0)
499 return;
500
501 if (ir3_instr_check_mark(instr))
502 return;
503
504 /* walk down the graph from each src: */
505 bool progress;
506 do {
507 progress = false;
508 foreach_src_n (reg, n, instr) {
509 struct ir3_instruction *src = ssa(reg);
510
511 if (!src)
512 continue;
513
514 instr_cp(ctx, src);
515
516 /* TODO non-indirect access we could figure out which register
517 * we actually want and allow cp..
518 */
519 if (reg->flags & IR3_REG_ARRAY)
520 continue;
521
522 /* Don't CP absneg into meta instructions, that won't end well: */
523 if (is_meta(instr) && (src->opc != OPC_MOV))
524 continue;
525
526 progress |= reg_cp(ctx, instr, reg, n);
527 ctx->progress |= progress;
528 }
529 } while (progress);
530
531 if (instr->regs[0]->flags & IR3_REG_ARRAY) {
532 struct ir3_instruction *src = ssa(instr->regs[0]);
533 if (src)
534 instr_cp(ctx, src);
535 }
536
537 if (instr->address) {
538 instr_cp(ctx, instr->address);
539 ir3_instr_set_address(instr, eliminate_output_mov(ctx, instr->address));
540 }
541
542 /* we can end up with extra cmps.s from frontend, which uses a
543 *
544 * cmps.s p0.x, cond, 0
545 *
546 * as a way to mov into the predicate register. But frequently 'cond'
547 * is itself a cmps.s/cmps.f/cmps.u. So detect this special case and
548 * just re-write the instruction writing predicate register to get rid
549 * of the double cmps.
550 */
551 if ((instr->opc == OPC_CMPS_S) &&
552 (instr->regs[0]->num == regid(REG_P0, 0)) &&
553 ssa(instr->regs[1]) &&
554 (instr->regs[2]->flags & IR3_REG_IMMED) &&
555 (instr->regs[2]->iim_val == 0) &&
556 (instr->cat2.condition == IR3_COND_NE)) {
557 struct ir3_instruction *cond = ssa(instr->regs[1]);
558 switch (cond->opc) {
559 case OPC_CMPS_S:
560 case OPC_CMPS_F:
561 case OPC_CMPS_U:
562 instr->opc = cond->opc;
563 instr->flags = cond->flags;
564 instr->cat2 = cond->cat2;
565 ir3_instr_set_address(instr, cond->address);
566 instr->regs[1] = cond->regs[1];
567 instr->regs[2] = cond->regs[2];
568 instr->barrier_class |= cond->barrier_class;
569 instr->barrier_conflict |= cond->barrier_conflict;
570 unuse(cond);
571 ctx->progress = true;
572 break;
573 default:
574 break;
575 }
576 }
577
578 /* Handle converting a sam.s2en (taking samp/tex idx params via register)
579 * into a normal sam (encoding immediate samp/tex idx) if they are
580 * immediate. This saves some instructions and regs in the common case
581 * where we know samp/tex at compile time. This needs to be done in the
582 * frontend for bindless tex, though, so don't replicate it here.
583 */
584 if (is_tex(instr) && (instr->flags & IR3_INSTR_S2EN) &&
585 !(instr->flags & IR3_INSTR_B) &&
586 !(ir3_shader_debug & IR3_DBG_FORCES2EN)) {
587 /* The first src will be a collect, if both of it's
588 * two sources are mov from imm, then we can
589 */
590 struct ir3_instruction *samp_tex = ssa(instr->regs[1]);
591
592 debug_assert(samp_tex->opc == OPC_META_COLLECT);
593
594 struct ir3_instruction *samp = ssa(samp_tex->regs[1]);
595 struct ir3_instruction *tex = ssa(samp_tex->regs[2]);
596
597 if ((samp->opc == OPC_MOV) &&
598 (samp->regs[1]->flags & IR3_REG_IMMED) &&
599 (tex->opc == OPC_MOV) &&
600 (tex->regs[1]->flags & IR3_REG_IMMED)) {
601 instr->flags &= ~IR3_INSTR_S2EN;
602 instr->cat5.samp = samp->regs[1]->iim_val;
603 instr->cat5.tex = tex->regs[1]->iim_val;
604
605 /* shuffle around the regs to remove the first src: */
606 instr->regs_count--;
607 for (unsigned i = 1; i < instr->regs_count; i++) {
608 instr->regs[i] = instr->regs[i + 1];
609 }
610
611 ctx->progress = true;
612 }
613 }
614 }
615
616 bool
617 ir3_cp(struct ir3 *ir, struct ir3_shader_variant *so)
618 {
619 struct ir3_cp_ctx ctx = {
620 .shader = ir,
621 .so = so,
622 };
623
624 /* This is a bit annoying, and probably wouldn't be necessary if we
625 * tracked a reverse link from producing instruction to consumer.
626 * But we need to know when we've eliminated the last consumer of
627 * a mov, so we need to do a pass to first count consumers of a
628 * mov.
629 */
630 foreach_block (block, &ir->block_list) {
631 foreach_instr (instr, &block->instr_list) {
632
633 /* by the way, we don't account for false-dep's, so the CP
634 * pass should always happen before false-dep's are inserted
635 */
636 debug_assert(instr->deps_count == 0);
637
638 foreach_ssa_src (src, instr) {
639 src->use_count++;
640 }
641 }
642 }
643
644 ir3_clear_mark(ir);
645
646 foreach_output_n (out, n, ir) {
647 instr_cp(&ctx, out);
648 ir->outputs[n] = eliminate_output_mov(&ctx, out);
649 }
650
651 foreach_block (block, &ir->block_list) {
652 if (block->condition) {
653 instr_cp(&ctx, block->condition);
654 block->condition = eliminate_output_mov(&ctx, block->condition);
655 }
656
657 for (unsigned i = 0; i < block->keeps_count; i++) {
658 instr_cp(&ctx, block->keeps[i]);
659 block->keeps[i] = eliminate_output_mov(&ctx, block->keeps[i]);
660 }
661 }
662
663 return ctx.progress;
664 }