1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
4 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * Rob Clark <robclark@freedesktop.org>
29 #include "freedreno_util.h"
32 #include "ir3_shader.h"
40 struct ir3_shader_variant
*so
;
41 unsigned immediate_idx
;
44 /* is it a type preserving mov, with ok flags? */
45 static bool is_eligible_mov(struct ir3_instruction
*instr
, bool allow_flags
)
47 if (is_same_type_mov(instr
)) {
48 struct ir3_register
*dst
= instr
->regs
[0];
49 struct ir3_register
*src
= instr
->regs
[1];
50 struct ir3_instruction
*src_instr
= ssa(src
);
52 /* only if mov src is SSA (not const/immed): */
57 if (dst
->flags
& IR3_REG_RELATIV
)
59 if (src
->flags
& IR3_REG_RELATIV
)
63 if (src
->flags
& (IR3_REG_FABS
| IR3_REG_FNEG
|
64 IR3_REG_SABS
| IR3_REG_SNEG
| IR3_REG_BNOT
))
67 /* TODO: remove this hack: */
68 if (src_instr
->opc
== OPC_META_FO
)
76 static unsigned cp_flags(unsigned flags
)
78 /* only considering these flags (at least for now): */
79 flags
&= (IR3_REG_CONST
| IR3_REG_IMMED
|
80 IR3_REG_FNEG
| IR3_REG_FABS
|
81 IR3_REG_SNEG
| IR3_REG_SABS
|
82 IR3_REG_BNOT
| IR3_REG_RELATIV
);
86 static bool valid_flags(struct ir3_instruction
*instr
, unsigned n
,
90 flags
= cp_flags(flags
);
92 /* If destination is indirect, then source cannot be.. at least
95 if ((instr
->regs
[0]->flags
& IR3_REG_RELATIV
) &&
96 (flags
& IR3_REG_RELATIV
))
99 /* TODO it seems to *mostly* work to cp RELATIV, except we get some
100 * intermittent piglit variable-indexing fails. Newer blob driver
101 * doesn't seem to cp these. Possibly this is hw workaround? Not
102 * sure, but until that is understood better, lets just switch off
103 * cp for indirect src's:
105 if (flags
& IR3_REG_RELATIV
)
108 switch (opc_cat(instr
->opc
)) {
110 valid_flags
= IR3_REG_IMMED
| IR3_REG_CONST
| IR3_REG_RELATIV
;
111 if (flags
& ~valid_flags
)
115 valid_flags
= ir3_cat2_absneg(instr
->opc
) |
116 IR3_REG_CONST
| IR3_REG_RELATIV
;
118 if (ir3_cat2_int(instr
->opc
))
119 valid_flags
|= IR3_REG_IMMED
;
121 if (flags
& ~valid_flags
)
124 if (flags
& (IR3_REG_CONST
| IR3_REG_IMMED
)) {
125 unsigned m
= (n
^ 1) + 1;
126 /* cannot deal w/ const in both srcs:
127 * (note that some cat2 actually only have a single src)
129 if (m
< instr
->regs_count
) {
130 struct ir3_register
*reg
= instr
->regs
[m
];
131 if ((flags
& IR3_REG_CONST
) && (reg
->flags
& IR3_REG_CONST
))
133 if ((flags
& IR3_REG_IMMED
) && (reg
->flags
& IR3_REG_IMMED
))
136 /* cannot be const + ABS|NEG: */
137 if (flags
& (IR3_REG_FABS
| IR3_REG_FNEG
|
138 IR3_REG_SABS
| IR3_REG_SNEG
| IR3_REG_BNOT
))
143 valid_flags
= ir3_cat3_absneg(instr
->opc
) |
144 IR3_REG_CONST
| IR3_REG_RELATIV
;
146 if (flags
& ~valid_flags
)
149 if (flags
& (IR3_REG_CONST
| IR3_REG_RELATIV
)) {
150 /* cannot deal w/ const/relativ in 2nd src: */
155 if (flags
& IR3_REG_CONST
) {
156 /* cannot be const + ABS|NEG: */
157 if (flags
& (IR3_REG_FABS
| IR3_REG_FNEG
|
158 IR3_REG_SABS
| IR3_REG_SNEG
| IR3_REG_BNOT
))
163 /* seems like blob compiler avoids const as src.. */
164 /* TODO double check if this is still the case on a4xx */
165 if (flags
& (IR3_REG_CONST
| IR3_REG_IMMED
))
167 if (flags
& (IR3_REG_SABS
| IR3_REG_SNEG
))
171 /* no flags allowed */
176 valid_flags
= IR3_REG_IMMED
;
177 if (flags
& ~valid_flags
)
180 if (flags
& IR3_REG_IMMED
) {
181 /* doesn't seem like we can have immediate src for store
184 * TODO this restriction could also apply to load instructions,
185 * but for load instructions this arg is the address (and not
186 * really sure any good way to test a hard-coded immed addr src)
188 if (is_store(instr
) && (n
== 1))
191 if ((instr
->opc
== OPC_LDL
) && (n
!= 1))
194 if ((instr
->opc
== OPC_STL
) && (n
!= 2))
197 /* disallow CP into anything but the SSBO slot argument for
200 if (is_atomic(instr
->opc
) && (n
!= 0))
203 if (is_atomic(instr
->opc
) && !(instr
->flags
& IR3_INSTR_G
))
213 /* propagate register flags from src to dst.. negates need special
214 * handling to cancel each other out.
216 static void combine_flags(unsigned *dstflags
, struct ir3_instruction
*src
)
218 unsigned srcflags
= src
->regs
[1]->flags
;
220 /* if what we are combining into already has (abs) flags,
221 * we can drop (neg) from src:
223 if (*dstflags
& IR3_REG_FABS
)
224 srcflags
&= ~IR3_REG_FNEG
;
225 if (*dstflags
& IR3_REG_SABS
)
226 srcflags
&= ~IR3_REG_SNEG
;
228 if (srcflags
& IR3_REG_FABS
)
229 *dstflags
|= IR3_REG_FABS
;
230 if (srcflags
& IR3_REG_SABS
)
231 *dstflags
|= IR3_REG_SABS
;
232 if (srcflags
& IR3_REG_FNEG
)
233 *dstflags
^= IR3_REG_FNEG
;
234 if (srcflags
& IR3_REG_SNEG
)
235 *dstflags
^= IR3_REG_SNEG
;
236 if (srcflags
& IR3_REG_BNOT
)
237 *dstflags
^= IR3_REG_BNOT
;
239 *dstflags
&= ~IR3_REG_SSA
;
240 *dstflags
|= srcflags
& IR3_REG_SSA
;
241 *dstflags
|= srcflags
& IR3_REG_CONST
;
242 *dstflags
|= srcflags
& IR3_REG_IMMED
;
243 *dstflags
|= srcflags
& IR3_REG_RELATIV
;
244 *dstflags
|= srcflags
& IR3_REG_ARRAY
;
246 /* if src of the src is boolean we can drop the (abs) since we know
247 * the source value is already a postitive integer. This cleans
248 * up the absnegs that get inserted when converting between nir and
249 * native boolean (see ir3_b2n/n2b)
251 struct ir3_instruction
*srcsrc
= ssa(src
->regs
[1]);
252 if (srcsrc
&& is_bool(srcsrc
))
253 *dstflags
&= ~IR3_REG_SABS
;
256 static struct ir3_register
*
257 lower_immed(struct ir3_cp_ctx
*ctx
, struct ir3_register
*reg
, unsigned new_flags
)
259 unsigned swiz
, idx
, i
;
261 reg
= ir3_reg_clone(ctx
->shader
, reg
);
263 /* in some cases, there are restrictions on (abs)/(neg) plus const..
264 * so just evaluate those and clear the flags:
266 if (new_flags
& IR3_REG_SABS
) {
267 reg
->iim_val
= abs(reg
->iim_val
);
268 new_flags
&= ~IR3_REG_SABS
;
271 if (new_flags
& IR3_REG_FABS
) {
272 reg
->fim_val
= fabs(reg
->fim_val
);
273 new_flags
&= ~IR3_REG_FABS
;
276 if (new_flags
& IR3_REG_SNEG
) {
277 reg
->iim_val
= -reg
->iim_val
;
278 new_flags
&= ~IR3_REG_SNEG
;
281 if (new_flags
& IR3_REG_FNEG
) {
282 reg
->fim_val
= -reg
->fim_val
;
283 new_flags
&= ~IR3_REG_FNEG
;
286 for (i
= 0; i
< ctx
->immediate_idx
; i
++) {
290 if (ctx
->so
->immediates
[idx
].val
[swiz
] == reg
->uim_val
) {
295 if (i
== ctx
->immediate_idx
) {
296 /* need to generate a new immediate: */
299 ctx
->so
->immediates
[idx
].val
[swiz
] = reg
->uim_val
;
300 ctx
->so
->immediates_count
= idx
+ 1;
301 ctx
->immediate_idx
++;
304 new_flags
&= ~IR3_REG_IMMED
;
305 new_flags
|= IR3_REG_CONST
;
306 reg
->flags
= new_flags
;
307 reg
->num
= i
+ (4 * ctx
->so
->constbase
.immediate
);
313 unuse(struct ir3_instruction
*instr
)
315 debug_assert(instr
->use_count
> 0);
317 if (--instr
->use_count
== 0) {
318 struct ir3_block
*block
= instr
->block
;
320 instr
->barrier_class
= 0;
321 instr
->barrier_conflict
= 0;
323 /* we don't want to remove anything in keeps (which could
324 * be things like array store's)
326 for (unsigned i
= 0; i
< block
->keeps_count
; i
++) {
327 debug_assert(block
->keeps
[i
] != instr
);
333 * Handle cp for a given src register. This additionally handles
334 * the cases of collapsing immedate/const (which replace the src
335 * register with a non-ssa src) or collapsing mov's from relative
336 * src (which needs to also fixup the address src reference by the
340 reg_cp(struct ir3_cp_ctx
*ctx
, struct ir3_instruction
*instr
,
341 struct ir3_register
*reg
, unsigned n
)
343 struct ir3_instruction
*src
= ssa(reg
);
345 if (is_eligible_mov(src
, true)) {
346 /* simple case, no immed/const/relativ, only mov's w/ ssa src: */
347 struct ir3_register
*src_reg
= src
->regs
[1];
348 unsigned new_flags
= reg
->flags
;
350 combine_flags(&new_flags
, src
);
352 if (valid_flags(instr
, n
, new_flags
)) {
353 if (new_flags
& IR3_REG_ARRAY
) {
354 debug_assert(!(reg
->flags
& IR3_REG_ARRAY
));
355 reg
->array
= src_reg
->array
;
357 reg
->flags
= new_flags
;
358 reg
->instr
= ssa(src_reg
);
360 instr
->barrier_class
|= src
->barrier_class
;
361 instr
->barrier_conflict
|= src
->barrier_conflict
;
364 reg
->instr
->use_count
++;
367 } else if (is_same_type_mov(src
) &&
368 /* cannot collapse const/immed/etc into meta instrs: */
370 /* immed/const/etc cases, which require some special handling: */
371 struct ir3_register
*src_reg
= src
->regs
[1];
372 unsigned new_flags
= reg
->flags
;
374 combine_flags(&new_flags
, src
);
376 if (!valid_flags(instr
, n
, new_flags
)) {
377 /* See if lowering an immediate to const would help. */
378 if (valid_flags(instr
, n
, (new_flags
& ~IR3_REG_IMMED
) | IR3_REG_CONST
)) {
379 debug_assert(new_flags
& IR3_REG_IMMED
);
380 instr
->regs
[n
+ 1] = lower_immed(ctx
, src_reg
, new_flags
);
384 /* special case for "normal" mad instructions, we can
385 * try swapping the first two args if that fits better.
387 * the "plain" MAD's (ie. the ones that don't shift first
388 * src prior to multiply) can swap their first two srcs if
389 * src[0] is !CONST and src[1] is CONST:
391 if ((n
== 1) && is_mad(instr
->opc
) &&
392 !(instr
->regs
[0 + 1]->flags
& (IR3_REG_CONST
| IR3_REG_RELATIV
)) &&
393 valid_flags(instr
, 0, new_flags
& ~IR3_REG_IMMED
)) {
394 /* swap src[0] and src[1]: */
395 struct ir3_register
*tmp
;
396 tmp
= instr
->regs
[0 + 1];
397 instr
->regs
[0 + 1] = instr
->regs
[1 + 1];
398 instr
->regs
[1 + 1] = tmp
;
406 /* Here we handle the special case of mov from
407 * CONST and/or RELATIV. These need to be handled
408 * specially, because in the case of move from CONST
409 * there is no src ir3_instruction so we need to
410 * replace the ir3_register. And in the case of
411 * RELATIV we need to handle the address register
414 if (src_reg
->flags
& IR3_REG_CONST
) {
415 /* an instruction cannot reference two different
418 if ((src_reg
->flags
& IR3_REG_RELATIV
) &&
419 conflicts(instr
->address
, reg
->instr
->address
))
422 /* This seems to be a hw bug, or something where the timings
423 * just somehow don't work out. This restriction may only
424 * apply if the first src is also CONST.
426 if ((opc_cat(instr
->opc
) == 3) && (n
== 2) &&
427 (src_reg
->flags
& IR3_REG_RELATIV
) &&
428 (src_reg
->array
.offset
== 0))
431 src_reg
= ir3_reg_clone(instr
->block
->shader
, src_reg
);
432 src_reg
->flags
= new_flags
;
433 instr
->regs
[n
+1] = src_reg
;
435 if (src_reg
->flags
& IR3_REG_RELATIV
)
436 ir3_instr_set_address(instr
, reg
->instr
->address
);
441 if ((src_reg
->flags
& IR3_REG_RELATIV
) &&
442 !conflicts(instr
->address
, reg
->instr
->address
)) {
443 src_reg
= ir3_reg_clone(instr
->block
->shader
, src_reg
);
444 src_reg
->flags
= new_flags
;
445 instr
->regs
[n
+1] = src_reg
;
446 ir3_instr_set_address(instr
, reg
->instr
->address
);
451 /* NOTE: seems we can only do immed integers, so don't
452 * need to care about float. But we do need to handle
453 * abs/neg *before* checking that the immediate requires
454 * few enough bits to encode:
456 * TODO: do we need to do something to avoid accidentally
457 * catching a float immed?
459 if (src_reg
->flags
& IR3_REG_IMMED
) {
460 int32_t iim_val
= src_reg
->iim_val
;
462 debug_assert((opc_cat(instr
->opc
) == 1) ||
463 (opc_cat(instr
->opc
) == 6) ||
464 ir3_cat2_int(instr
->opc
) ||
465 (is_mad(instr
->opc
) && (n
== 0)));
467 if (new_flags
& IR3_REG_SABS
)
468 iim_val
= abs(iim_val
);
470 if (new_flags
& IR3_REG_SNEG
)
473 if (new_flags
& IR3_REG_BNOT
)
476 /* other than category 1 (mov) we can only encode up to 10 bits: */
477 if ((instr
->opc
== OPC_MOV
) ||
478 !((iim_val
& ~0x3ff) && (-iim_val
& ~0x3ff))) {
479 new_flags
&= ~(IR3_REG_SABS
| IR3_REG_SNEG
| IR3_REG_BNOT
);
480 src_reg
= ir3_reg_clone(instr
->block
->shader
, src_reg
);
481 src_reg
->flags
= new_flags
;
482 src_reg
->iim_val
= iim_val
;
483 instr
->regs
[n
+1] = src_reg
;
484 } else if (valid_flags(instr
, n
, (new_flags
& ~IR3_REG_IMMED
) | IR3_REG_CONST
)) {
485 /* See if lowering an immediate to const would help. */
486 instr
->regs
[n
+1] = lower_immed(ctx
, src_reg
, new_flags
);
494 /* Handle special case of eliminating output mov, and similar cases where
495 * there isn't a normal "consuming" instruction. In this case we cannot
496 * collapse flags (ie. output mov from const, or w/ abs/neg flags, cannot
499 static struct ir3_instruction
*
500 eliminate_output_mov(struct ir3_instruction
*instr
)
502 if (is_eligible_mov(instr
, false)) {
503 struct ir3_register
*reg
= instr
->regs
[1];
504 if (!(reg
->flags
& IR3_REG_ARRAY
)) {
505 struct ir3_instruction
*src_instr
= ssa(reg
);
506 debug_assert(src_instr
);
514 * Find instruction src's which are mov's that can be collapsed, replacing
515 * the mov dst with the mov src
518 instr_cp(struct ir3_cp_ctx
*ctx
, struct ir3_instruction
*instr
)
520 struct ir3_register
*reg
;
522 if (instr
->regs_count
== 0)
525 if (ir3_instr_check_mark(instr
))
528 /* walk down the graph from each src: */
529 foreach_src_n(reg
, n
, instr
) {
530 struct ir3_instruction
*src
= ssa(reg
);
537 /* TODO non-indirect access we could figure out which register
538 * we actually want and allow cp..
540 if (reg
->flags
& IR3_REG_ARRAY
)
543 reg_cp(ctx
, instr
, reg
, n
);
546 if (instr
->regs
[0]->flags
& IR3_REG_ARRAY
) {
547 struct ir3_instruction
*src
= ssa(instr
->regs
[0]);
552 if (instr
->address
) {
553 instr_cp(ctx
, instr
->address
);
554 ir3_instr_set_address(instr
, eliminate_output_mov(instr
->address
));
557 /* we can end up with extra cmps.s from frontend, which uses a
559 * cmps.s p0.x, cond, 0
561 * as a way to mov into the predicate register. But frequently 'cond'
562 * is itself a cmps.s/cmps.f/cmps.u. So detect this special case and
563 * just re-write the instruction writing predicate register to get rid
564 * of the double cmps.
566 if ((instr
->opc
== OPC_CMPS_S
) &&
567 (instr
->regs
[0]->num
== regid(REG_P0
, 0)) &&
568 ssa(instr
->regs
[1]) &&
569 (instr
->regs
[2]->flags
& IR3_REG_IMMED
) &&
570 (instr
->regs
[2]->iim_val
== 0)) {
571 struct ir3_instruction
*cond
= ssa(instr
->regs
[1]);
576 instr
->opc
= cond
->opc
;
577 instr
->flags
= cond
->flags
;
578 instr
->cat2
= cond
->cat2
;
579 instr
->address
= cond
->address
;
580 instr
->regs
[1] = cond
->regs
[1];
581 instr
->regs
[2] = cond
->regs
[2];
582 instr
->barrier_class
|= cond
->barrier_class
;
583 instr
->barrier_conflict
|= cond
->barrier_conflict
;
593 ir3_cp(struct ir3
*ir
, struct ir3_shader_variant
*so
)
595 struct ir3_cp_ctx ctx
= {
600 /* This is a bit annoying, and probably wouldn't be necessary if we
601 * tracked a reverse link from producing instruction to consumer.
602 * But we need to know when we've eliminated the last consumer of
603 * a mov, so we need to do a pass to first count consumers of a
606 list_for_each_entry (struct ir3_block
, block
, &ir
->block_list
, node
) {
607 list_for_each_entry (struct ir3_instruction
, instr
, &block
->instr_list
, node
) {
608 struct ir3_instruction
*src
;
610 /* by the way, we don't acount for false-dep's, so the CP
611 * pass should always happen before false-dep's are inserted
613 debug_assert(instr
->deps_count
== 0);
615 foreach_ssa_src(src
, instr
) {
623 for (unsigned i
= 0; i
< ir
->noutputs
; i
++) {
624 if (ir
->outputs
[i
]) {
625 instr_cp(&ctx
, ir
->outputs
[i
]);
626 ir
->outputs
[i
] = eliminate_output_mov(ir
->outputs
[i
]);
630 list_for_each_entry (struct ir3_block
, block
, &ir
->block_list
, node
) {
631 if (block
->condition
) {
632 instr_cp(&ctx
, block
->condition
);
633 block
->condition
= eliminate_output_mov(block
->condition
);
636 for (unsigned i
= 0; i
< block
->keeps_count
; i
++) {
637 instr_cp(&ctx
, block
->keeps
[i
]);
638 block
->keeps
[i
] = eliminate_output_mov(block
->keeps
[i
]);