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
)
70 /* TODO: we currently don't handle left/right neighbors
71 * very well when inserting parallel-copies into phi..
72 * to avoid problems don't eliminate a mov coming out
75 if (src_instr
->opc
== OPC_META_PHI
)
82 static unsigned cp_flags(unsigned flags
)
84 /* only considering these flags (at least for now): */
85 flags
&= (IR3_REG_CONST
| IR3_REG_IMMED
|
86 IR3_REG_FNEG
| IR3_REG_FABS
|
87 IR3_REG_SNEG
| IR3_REG_SABS
|
88 IR3_REG_BNOT
| IR3_REG_RELATIV
);
92 static bool valid_flags(struct ir3_instruction
*instr
, unsigned n
,
96 flags
= cp_flags(flags
);
98 /* If destination is indirect, then source cannot be.. at least
101 if ((instr
->regs
[0]->flags
& IR3_REG_RELATIV
) &&
102 (flags
& IR3_REG_RELATIV
))
105 /* clear flags that are 'ok' */
106 switch (opc_cat(instr
->opc
)) {
108 valid_flags
= IR3_REG_IMMED
| IR3_REG_CONST
| IR3_REG_RELATIV
;
109 if (flags
& ~valid_flags
)
113 /* no flags allowed */
118 valid_flags
= IR3_REG_IMMED
;
119 if (flags
& ~valid_flags
)
122 if (flags
& IR3_REG_IMMED
) {
123 /* doesn't seem like we can have immediate src for store
126 * TODO this restriction could also apply to load instructions,
127 * but for load instructions this arg is the address (and not
128 * really sure any good way to test a hard-coded immed addr src)
130 if (is_store(instr
) && (n
== 1))
136 valid_flags
= ir3_cat2_absneg(instr
->opc
) |
137 IR3_REG_CONST
| IR3_REG_RELATIV
;
139 if (ir3_cat2_int(instr
->opc
))
140 valid_flags
|= IR3_REG_IMMED
;
142 if (flags
& ~valid_flags
)
145 if (flags
& (IR3_REG_CONST
| IR3_REG_IMMED
)) {
146 unsigned m
= (n
^ 1) + 1;
147 /* cannot deal w/ const in both srcs:
148 * (note that some cat2 actually only have a single src)
150 if (m
< instr
->regs_count
) {
151 struct ir3_register
*reg
= instr
->regs
[m
];
152 if ((flags
& IR3_REG_CONST
) && (reg
->flags
& IR3_REG_CONST
))
154 if ((flags
& IR3_REG_IMMED
) && (reg
->flags
& IR3_REG_IMMED
))
157 /* cannot be const + ABS|NEG: */
158 if (flags
& (IR3_REG_FABS
| IR3_REG_FNEG
|
159 IR3_REG_SABS
| IR3_REG_SNEG
| IR3_REG_BNOT
))
164 valid_flags
= ir3_cat3_absneg(instr
->opc
) |
165 IR3_REG_CONST
| IR3_REG_RELATIV
;
167 if (flags
& ~valid_flags
)
170 if (flags
& (IR3_REG_CONST
| IR3_REG_RELATIV
)) {
171 /* cannot deal w/ const/relativ in 2nd src: */
176 if (flags
& IR3_REG_CONST
) {
177 /* cannot be const + ABS|NEG: */
178 if (flags
& (IR3_REG_FABS
| IR3_REG_FNEG
|
179 IR3_REG_SABS
| IR3_REG_SNEG
| IR3_REG_BNOT
))
184 /* seems like blob compiler avoids const as src.. */
185 /* TODO double check if this is still the case on a4xx */
186 if (flags
& (IR3_REG_CONST
| IR3_REG_IMMED
))
188 if (flags
& (IR3_REG_SABS
| IR3_REG_SNEG
))
196 /* propagate register flags from src to dst.. negates need special
197 * handling to cancel each other out.
199 static void combine_flags(unsigned *dstflags
, struct ir3_instruction
*src
)
201 unsigned srcflags
= src
->regs
[1]->flags
;
203 /* if what we are combining into already has (abs) flags,
204 * we can drop (neg) from src:
206 if (*dstflags
& IR3_REG_FABS
)
207 srcflags
&= ~IR3_REG_FNEG
;
208 if (*dstflags
& IR3_REG_SABS
)
209 srcflags
&= ~IR3_REG_SNEG
;
211 if (srcflags
& IR3_REG_FABS
)
212 *dstflags
|= IR3_REG_FABS
;
213 if (srcflags
& IR3_REG_SABS
)
214 *dstflags
|= IR3_REG_SABS
;
215 if (srcflags
& IR3_REG_FNEG
)
216 *dstflags
^= IR3_REG_FNEG
;
217 if (srcflags
& IR3_REG_SNEG
)
218 *dstflags
^= IR3_REG_SNEG
;
219 if (srcflags
& IR3_REG_BNOT
)
220 *dstflags
^= IR3_REG_BNOT
;
222 *dstflags
&= ~IR3_REG_SSA
;
223 *dstflags
|= srcflags
& IR3_REG_SSA
;
224 *dstflags
|= srcflags
& IR3_REG_CONST
;
225 *dstflags
|= srcflags
& IR3_REG_IMMED
;
226 *dstflags
|= srcflags
& IR3_REG_RELATIV
;
227 *dstflags
|= srcflags
& IR3_REG_ARRAY
;
229 /* if src of the src is boolean we can drop the (abs) since we know
230 * the source value is already a postitive integer. This cleans
231 * up the absnegs that get inserted when converting between nir and
232 * native boolean (see ir3_b2n/n2b)
234 struct ir3_instruction
*srcsrc
= ssa(src
->regs
[1]);
235 if (srcsrc
&& is_bool(srcsrc
))
236 *dstflags
&= ~IR3_REG_SABS
;
239 static struct ir3_register
*
240 lower_immed(struct ir3_cp_ctx
*ctx
, struct ir3_register
*reg
, unsigned new_flags
)
242 unsigned swiz
, idx
, i
;
244 reg
= ir3_reg_clone(ctx
->shader
, reg
);
246 /* in some cases, there are restrictions on (abs)/(neg) plus const..
247 * so just evaluate those and clear the flags:
249 if (new_flags
& IR3_REG_SABS
) {
250 reg
->iim_val
= abs(reg
->iim_val
);
251 new_flags
&= ~IR3_REG_SABS
;
254 if (new_flags
& IR3_REG_FABS
) {
255 reg
->fim_val
= fabs(reg
->fim_val
);
256 new_flags
&= ~IR3_REG_FABS
;
259 if (new_flags
& IR3_REG_SNEG
) {
260 reg
->iim_val
= -reg
->iim_val
;
261 new_flags
&= ~IR3_REG_SNEG
;
264 if (new_flags
& IR3_REG_FNEG
) {
265 reg
->fim_val
= -reg
->fim_val
;
266 new_flags
&= ~IR3_REG_FNEG
;
269 for (i
= 0; i
< ctx
->immediate_idx
; i
++) {
273 if (ctx
->so
->immediates
[idx
].val
[swiz
] == reg
->uim_val
) {
278 if (i
== ctx
->immediate_idx
) {
279 /* need to generate a new immediate: */
282 ctx
->so
->immediates
[idx
].val
[swiz
] = reg
->uim_val
;
283 ctx
->so
->immediates_count
= idx
+ 1;
284 ctx
->immediate_idx
++;
287 new_flags
&= ~IR3_REG_IMMED
;
288 new_flags
|= IR3_REG_CONST
;
289 reg
->flags
= new_flags
;
290 reg
->num
= i
+ (4 * ctx
->so
->first_immediate
);
296 * Handle cp for a given src register. This additionally handles
297 * the cases of collapsing immedate/const (which replace the src
298 * register with a non-ssa src) or collapsing mov's from relative
299 * src (which needs to also fixup the address src reference by the
303 reg_cp(struct ir3_cp_ctx
*ctx
, struct ir3_instruction
*instr
,
304 struct ir3_register
*reg
, unsigned n
)
306 struct ir3_instruction
*src
= ssa(reg
);
308 /* don't propagate copies into a PHI, since we don't know if the
309 * src block executed:
311 if (instr
->opc
== OPC_META_PHI
)
314 if (is_eligible_mov(src
, true)) {
315 /* simple case, no immed/const/relativ, only mov's w/ ssa src: */
316 struct ir3_register
*src_reg
= src
->regs
[1];
317 unsigned new_flags
= reg
->flags
;
319 combine_flags(&new_flags
, src
);
321 if (valid_flags(instr
, n
, new_flags
)) {
322 if (new_flags
& IR3_REG_ARRAY
) {
323 debug_assert(!(reg
->flags
& IR3_REG_ARRAY
));
324 reg
->array
= src_reg
->array
;
326 reg
->flags
= new_flags
;
327 reg
->instr
= ssa(src_reg
);
330 src
= ssa(reg
); /* could be null for IR3_REG_ARRAY case */
333 } else if (is_same_type_mov(src
) &&
334 /* cannot collapse const/immed/etc into meta instrs: */
336 /* immed/const/etc cases, which require some special handling: */
337 struct ir3_register
*src_reg
= src
->regs
[1];
338 unsigned new_flags
= reg
->flags
;
340 combine_flags(&new_flags
, src
);
342 if (!valid_flags(instr
, n
, new_flags
)) {
343 /* See if lowering an immediate to const would help. */
344 if (valid_flags(instr
, n
, (new_flags
& ~IR3_REG_IMMED
) | IR3_REG_CONST
)) {
345 debug_assert(new_flags
& IR3_REG_IMMED
);
346 instr
->regs
[n
+ 1] = lower_immed(ctx
, src_reg
, new_flags
);
350 /* special case for "normal" mad instructions, we can
351 * try swapping the first two args if that fits better.
353 * the "plain" MAD's (ie. the ones that don't shift first
354 * src prior to multiply) can swap their first two srcs if
355 * src[0] is !CONST and src[1] is CONST:
357 if ((n
== 1) && is_mad(instr
->opc
) &&
358 !(instr
->regs
[0 + 1]->flags
& (IR3_REG_CONST
| IR3_REG_RELATIV
)) &&
359 valid_flags(instr
, 0, new_flags
)) {
360 /* swap src[0] and src[1]: */
361 struct ir3_register
*tmp
;
362 tmp
= instr
->regs
[0 + 1];
363 instr
->regs
[0 + 1] = instr
->regs
[1 + 1];
364 instr
->regs
[1 + 1] = tmp
;
371 /* Here we handle the special case of mov from
372 * CONST and/or RELATIV. These need to be handled
373 * specially, because in the case of move from CONST
374 * there is no src ir3_instruction so we need to
375 * replace the ir3_register. And in the case of
376 * RELATIV we need to handle the address register
379 if (src_reg
->flags
& IR3_REG_CONST
) {
380 /* an instruction cannot reference two different
383 if ((src_reg
->flags
& IR3_REG_RELATIV
) &&
384 conflicts(instr
->address
, reg
->instr
->address
))
387 /* This seems to be a hw bug, or something where the timings
388 * just somehow don't work out. This restriction may only
389 * apply if the first src is also CONST.
391 if ((opc_cat(instr
->opc
) == 3) && (n
== 2) &&
392 (src_reg
->flags
& IR3_REG_RELATIV
) &&
393 (src_reg
->array
.offset
== 0))
396 src_reg
= ir3_reg_clone(instr
->block
->shader
, src_reg
);
397 src_reg
->flags
= new_flags
;
398 instr
->regs
[n
+1] = src_reg
;
400 if (src_reg
->flags
& IR3_REG_RELATIV
)
401 ir3_instr_set_address(instr
, reg
->instr
->address
);
406 if ((src_reg
->flags
& IR3_REG_RELATIV
) &&
407 !conflicts(instr
->address
, reg
->instr
->address
)) {
408 src_reg
= ir3_reg_clone(instr
->block
->shader
, src_reg
);
409 src_reg
->flags
= new_flags
;
410 instr
->regs
[n
+1] = src_reg
;
411 ir3_instr_set_address(instr
, reg
->instr
->address
);
416 /* NOTE: seems we can only do immed integers, so don't
417 * need to care about float. But we do need to handle
418 * abs/neg *before* checking that the immediate requires
419 * few enough bits to encode:
421 * TODO: do we need to do something to avoid accidentally
422 * catching a float immed?
424 if (src_reg
->flags
& IR3_REG_IMMED
) {
425 int32_t iim_val
= src_reg
->iim_val
;
427 debug_assert((opc_cat(instr
->opc
) == 1) ||
428 (opc_cat(instr
->opc
) == 6) ||
429 ir3_cat2_int(instr
->opc
));
431 if (new_flags
& IR3_REG_SABS
)
432 iim_val
= abs(iim_val
);
434 if (new_flags
& IR3_REG_SNEG
)
437 if (new_flags
& IR3_REG_BNOT
)
440 /* other than category 1 (mov) we can only encode up to 10 bits: */
441 if ((instr
->opc
== OPC_MOV
) ||
442 !((iim_val
& ~0x3ff) && (-iim_val
& ~0x3ff))) {
443 new_flags
&= ~(IR3_REG_SABS
| IR3_REG_SNEG
| IR3_REG_BNOT
);
444 src_reg
= ir3_reg_clone(instr
->block
->shader
, src_reg
);
445 src_reg
->flags
= new_flags
;
446 src_reg
->iim_val
= iim_val
;
447 instr
->regs
[n
+1] = src_reg
;
448 } else if (valid_flags(instr
, n
, (new_flags
& ~IR3_REG_IMMED
) | IR3_REG_CONST
)) {
449 /* See if lowering an immediate to const would help. */
450 instr
->regs
[n
+1] = lower_immed(ctx
, src_reg
, new_flags
);
458 /* Handle special case of eliminating output mov, and similar cases where
459 * there isn't a normal "consuming" instruction. In this case we cannot
460 * collapse flags (ie. output mov from const, or w/ abs/neg flags, cannot
463 static struct ir3_instruction
*
464 eliminate_output_mov(struct ir3_instruction
*instr
)
466 if (is_eligible_mov(instr
, false)) {
467 struct ir3_register
*reg
= instr
->regs
[1];
468 if (!(reg
->flags
& IR3_REG_ARRAY
)) {
469 struct ir3_instruction
*src_instr
= ssa(reg
);
470 debug_assert(src_instr
);
478 * Find instruction src's which are mov's that can be collapsed, replacing
479 * the mov dst with the mov src
482 instr_cp(struct ir3_cp_ctx
*ctx
, struct ir3_instruction
*instr
)
484 struct ir3_register
*reg
;
486 if (instr
->regs_count
== 0)
489 if (ir3_instr_check_mark(instr
))
492 /* walk down the graph from each src: */
493 foreach_src_n(reg
, n
, instr
) {
494 struct ir3_instruction
*src
= ssa(reg
);
501 /* TODO non-indirect access we could figure out which register
502 * we actually want and allow cp..
504 if (reg
->flags
& IR3_REG_ARRAY
)
507 reg_cp(ctx
, instr
, reg
, n
);
510 if (instr
->regs
[0]->flags
& IR3_REG_ARRAY
) {
511 struct ir3_instruction
*src
= ssa(instr
->regs
[0]);
516 if (instr
->address
) {
517 instr_cp(ctx
, instr
->address
);
518 ir3_instr_set_address(instr
, eliminate_output_mov(instr
->address
));
521 /* we can end up with extra cmps.s from frontend, which uses a
523 * cmps.s p0.x, cond, 0
525 * as a way to mov into the predicate register. But frequently 'cond'
526 * is itself a cmps.s/cmps.f/cmps.u. So detect this special case and
527 * just re-write the instruction writing predicate register to get rid
528 * of the double cmps.
530 if ((instr
->opc
== OPC_CMPS_S
) &&
531 (instr
->regs
[0]->num
== regid(REG_P0
, 0)) &&
532 ssa(instr
->regs
[1]) &&
533 (instr
->regs
[2]->flags
& IR3_REG_IMMED
) &&
534 (instr
->regs
[2]->iim_val
== 0)) {
535 struct ir3_instruction
*cond
= ssa(instr
->regs
[1]);
540 instr
->opc
= cond
->opc
;
541 instr
->flags
= cond
->flags
;
542 instr
->cat2
= cond
->cat2
;
543 instr
->address
= cond
->address
;
544 instr
->regs
[1] = cond
->regs
[1];
545 instr
->regs
[2] = cond
->regs
[2];
554 ir3_cp(struct ir3
*ir
, struct ir3_shader_variant
*so
)
556 struct ir3_cp_ctx ctx
= {
563 for (unsigned i
= 0; i
< ir
->noutputs
; i
++) {
564 if (ir
->outputs
[i
]) {
565 instr_cp(&ctx
, ir
->outputs
[i
]);
566 ir
->outputs
[i
] = eliminate_output_mov(ir
->outputs
[i
]);
570 for (unsigned i
= 0; i
< ir
->keeps_count
; i
++) {
571 instr_cp(&ctx
, ir
->keeps
[i
]);
572 ir
->keeps
[i
] = eliminate_output_mov(ir
->keeps
[i
]);
575 list_for_each_entry (struct ir3_block
, block
, &ir
->block_list
, node
) {
576 if (block
->condition
) {
577 instr_cp(&ctx
, block
->condition
);
578 block
->condition
= eliminate_output_mov(block
->condition
);