00fa3538cc040b672b8f47175a707ae758b4ee61
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"
37 /* is it a type preserving mov, with ok flags? */
38 static bool is_eligible_mov(struct ir3_instruction
*instr
, bool allow_flags
)
40 if (is_same_type_mov(instr
)) {
41 struct ir3_register
*dst
= instr
->regs
[0];
42 struct ir3_register
*src
= instr
->regs
[1];
43 struct ir3_instruction
*src_instr
= ssa(src
);
45 /* only if mov src is SSA (not const/immed): */
50 if (dst
->flags
& IR3_REG_RELATIV
)
52 if (src
->flags
& IR3_REG_RELATIV
)
56 if (src
->flags
& (IR3_REG_FABS
| IR3_REG_FNEG
|
57 IR3_REG_SABS
| IR3_REG_SNEG
| IR3_REG_BNOT
))
60 /* TODO: remove this hack: */
61 if (src_instr
->opc
== OPC_META_FO
)
63 /* TODO: we currently don't handle left/right neighbors
64 * very well when inserting parallel-copies into phi..
65 * to avoid problems don't eliminate a mov coming out
68 if (src_instr
->opc
== OPC_META_PHI
)
75 static unsigned cp_flags(unsigned flags
)
77 /* only considering these flags (at least for now): */
78 flags
&= (IR3_REG_CONST
| IR3_REG_IMMED
|
79 IR3_REG_FNEG
| IR3_REG_FABS
|
80 IR3_REG_SNEG
| IR3_REG_SABS
|
81 IR3_REG_BNOT
| IR3_REG_RELATIV
);
85 static bool valid_flags(struct ir3_instruction
*instr
, unsigned n
,
89 flags
= cp_flags(flags
);
91 /* If destination is indirect, then source cannot be.. at least
94 if ((instr
->regs
[0]->flags
& IR3_REG_RELATIV
) &&
95 (flags
& IR3_REG_RELATIV
))
98 /* clear flags that are 'ok' */
99 switch (opc_cat(instr
->opc
)) {
101 valid_flags
= IR3_REG_IMMED
| IR3_REG_CONST
| IR3_REG_RELATIV
;
102 if (flags
& ~valid_flags
)
106 /* no flags allowed */
111 valid_flags
= IR3_REG_IMMED
;
112 if (flags
& ~valid_flags
)
115 if (flags
& IR3_REG_IMMED
) {
116 /* doesn't seem like we can have immediate src for store
119 * TODO this restriction could also apply to load instructions,
120 * but for load instructions this arg is the address (and not
121 * really sure any good way to test a hard-coded immed addr src)
123 if (is_store(instr
) && (n
== 1))
129 valid_flags
= ir3_cat2_absneg(instr
->opc
) |
130 IR3_REG_CONST
| IR3_REG_RELATIV
;
132 if (ir3_cat2_int(instr
->opc
))
133 valid_flags
|= IR3_REG_IMMED
;
135 if (flags
& ~valid_flags
)
138 if (flags
& (IR3_REG_CONST
| IR3_REG_IMMED
)) {
139 unsigned m
= (n
^ 1) + 1;
140 /* cannot deal w/ const in both srcs:
141 * (note that some cat2 actually only have a single src)
143 if (m
< instr
->regs_count
) {
144 struct ir3_register
*reg
= instr
->regs
[m
];
145 if ((flags
& IR3_REG_CONST
) && (reg
->flags
& IR3_REG_CONST
))
147 if ((flags
& IR3_REG_IMMED
) && (reg
->flags
& IR3_REG_IMMED
))
150 /* cannot be const + ABS|NEG: */
151 if (flags
& (IR3_REG_FABS
| IR3_REG_FNEG
|
152 IR3_REG_SABS
| IR3_REG_SNEG
| IR3_REG_BNOT
))
157 valid_flags
= ir3_cat3_absneg(instr
->opc
) |
158 IR3_REG_CONST
| IR3_REG_RELATIV
;
160 if (flags
& ~valid_flags
)
163 if (flags
& (IR3_REG_CONST
| IR3_REG_RELATIV
)) {
164 /* cannot deal w/ const/relativ in 2nd src: */
169 if (flags
& IR3_REG_CONST
) {
170 /* cannot be const + ABS|NEG: */
171 if (flags
& (IR3_REG_FABS
| IR3_REG_FNEG
|
172 IR3_REG_SABS
| IR3_REG_SNEG
| IR3_REG_BNOT
))
177 /* seems like blob compiler avoids const as src.. */
178 /* TODO double check if this is still the case on a4xx */
179 if (flags
& IR3_REG_CONST
)
181 if (flags
& (IR3_REG_SABS
| IR3_REG_SNEG
))
189 /* propagate register flags from src to dst.. negates need special
190 * handling to cancel each other out.
192 static void combine_flags(unsigned *dstflags
, unsigned srcflags
)
194 /* if what we are combining into already has (abs) flags,
195 * we can drop (neg) from src:
197 if (*dstflags
& IR3_REG_FABS
)
198 srcflags
&= ~IR3_REG_FNEG
;
199 if (*dstflags
& IR3_REG_SABS
)
200 srcflags
&= ~IR3_REG_SNEG
;
202 if (srcflags
& IR3_REG_FABS
)
203 *dstflags
|= IR3_REG_FABS
;
204 if (srcflags
& IR3_REG_SABS
)
205 *dstflags
|= IR3_REG_SABS
;
206 if (srcflags
& IR3_REG_FNEG
)
207 *dstflags
^= IR3_REG_FNEG
;
208 if (srcflags
& IR3_REG_SNEG
)
209 *dstflags
^= IR3_REG_SNEG
;
210 if (srcflags
& IR3_REG_BNOT
)
211 *dstflags
^= IR3_REG_BNOT
;
213 *dstflags
&= ~IR3_REG_SSA
;
214 *dstflags
|= srcflags
& IR3_REG_SSA
;
215 *dstflags
|= srcflags
& IR3_REG_CONST
;
216 *dstflags
|= srcflags
& IR3_REG_IMMED
;
217 *dstflags
|= srcflags
& IR3_REG_RELATIV
;
218 *dstflags
|= srcflags
& IR3_REG_ARRAY
;
222 * Handle cp for a given src register. This additionally handles
223 * the cases of collapsing immedate/const (which replace the src
224 * register with a non-ssa src) or collapsing mov's from relative
225 * src (which needs to also fixup the address src reference by the
229 reg_cp(struct ir3_instruction
*instr
, struct ir3_register
*reg
, unsigned n
)
231 struct ir3_instruction
*src
= ssa(reg
);
233 if (is_eligible_mov(src
, true)) {
234 /* simple case, no immed/const/relativ, only mov's w/ ssa src: */
235 struct ir3_register
*src_reg
= src
->regs
[1];
236 unsigned new_flags
= reg
->flags
;
238 combine_flags(&new_flags
, src_reg
->flags
);
240 if (valid_flags(instr
, n
, new_flags
)) {
241 if (new_flags
& IR3_REG_ARRAY
) {
242 debug_assert(!(reg
->flags
& IR3_REG_ARRAY
));
243 reg
->array
= src_reg
->array
;
245 reg
->flags
= new_flags
;
246 reg
->instr
= ssa(src_reg
);
249 src
= ssa(reg
); /* could be null for IR3_REG_ARRAY case */
252 } else if (is_same_type_mov(src
) &&
253 /* cannot collapse const/immed/etc into meta instrs: */
255 /* immed/const/etc cases, which require some special handling: */
256 struct ir3_register
*src_reg
= src
->regs
[1];
257 unsigned new_flags
= reg
->flags
;
259 combine_flags(&new_flags
, src_reg
->flags
);
261 if (!valid_flags(instr
, n
, new_flags
)) {
262 /* special case for "normal" mad instructions, we can
263 * try swapping the first two args if that fits better.
265 * the "plain" MAD's (ie. the ones that don't shift first
266 * src prior to multiply) can swap their first two srcs if
267 * src[0] is !CONST and src[1] is CONST:
269 if ((n
== 1) && is_mad(instr
->opc
) &&
270 !(instr
->regs
[0 + 1]->flags
& (IR3_REG_CONST
| IR3_REG_RELATIV
)) &&
271 valid_flags(instr
, 0, new_flags
)) {
272 /* swap src[0] and src[1]: */
273 struct ir3_register
*tmp
;
274 tmp
= instr
->regs
[0 + 1];
275 instr
->regs
[0 + 1] = instr
->regs
[1 + 1];
276 instr
->regs
[1 + 1] = tmp
;
283 /* Here we handle the special case of mov from
284 * CONST and/or RELATIV. These need to be handled
285 * specially, because in the case of move from CONST
286 * there is no src ir3_instruction so we need to
287 * replace the ir3_register. And in the case of
288 * RELATIV we need to handle the address register
291 if (src_reg
->flags
& IR3_REG_CONST
) {
292 /* an instruction cannot reference two different
295 if ((src_reg
->flags
& IR3_REG_RELATIV
) &&
296 conflicts(instr
->address
, reg
->instr
->address
))
299 /* This seems to be a hw bug, or something where the timings
300 * just somehow don't work out. This restriction may only
301 * apply if the first src is also CONST.
303 if ((opc_cat(instr
->opc
) == 3) && (n
== 2) &&
304 (src_reg
->flags
& IR3_REG_RELATIV
) &&
305 (src_reg
->array
.offset
== 0))
308 src_reg
= ir3_reg_clone(instr
->block
->shader
, src_reg
);
309 src_reg
->flags
= new_flags
;
310 instr
->regs
[n
+1] = src_reg
;
312 if (src_reg
->flags
& IR3_REG_RELATIV
)
313 ir3_instr_set_address(instr
, reg
->instr
->address
);
318 if ((src_reg
->flags
& IR3_REG_RELATIV
) &&
319 !conflicts(instr
->address
, reg
->instr
->address
)) {
320 src_reg
= ir3_reg_clone(instr
->block
->shader
, src_reg
);
321 src_reg
->flags
= new_flags
;
322 instr
->regs
[n
+1] = src_reg
;
323 ir3_instr_set_address(instr
, reg
->instr
->address
);
328 /* NOTE: seems we can only do immed integers, so don't
329 * need to care about float. But we do need to handle
330 * abs/neg *before* checking that the immediate requires
331 * few enough bits to encode:
333 * TODO: do we need to do something to avoid accidentally
334 * catching a float immed?
336 if (src_reg
->flags
& IR3_REG_IMMED
) {
337 int32_t iim_val
= src_reg
->iim_val
;
339 debug_assert((opc_cat(instr
->opc
) == 1) ||
340 (opc_cat(instr
->opc
) == 6) ||
341 ir3_cat2_int(instr
->opc
));
343 if (new_flags
& IR3_REG_SABS
)
344 iim_val
= abs(iim_val
);
346 if (new_flags
& IR3_REG_SNEG
)
349 if (new_flags
& IR3_REG_BNOT
)
352 /* other than category 1 (mov) we can only encode up to 10 bits: */
353 if ((instr
->opc
== OPC_MOV
) || !(iim_val
& ~0x3ff)) {
354 new_flags
&= ~(IR3_REG_SABS
| IR3_REG_SNEG
| IR3_REG_BNOT
);
355 src_reg
= ir3_reg_clone(instr
->block
->shader
, src_reg
);
356 src_reg
->flags
= new_flags
;
357 src_reg
->iim_val
= iim_val
;
358 instr
->regs
[n
+1] = src_reg
;
366 /* Handle special case of eliminating output mov, and similar cases where
367 * there isn't a normal "consuming" instruction. In this case we cannot
368 * collapse flags (ie. output mov from const, or w/ abs/neg flags, cannot
371 static struct ir3_instruction
*
372 eliminate_output_mov(struct ir3_instruction
*instr
)
374 if (is_eligible_mov(instr
, false)) {
375 struct ir3_register
*reg
= instr
->regs
[1];
376 if (!(reg
->flags
& IR3_REG_ARRAY
)) {
377 struct ir3_instruction
*src_instr
= ssa(reg
);
378 debug_assert(src_instr
);
386 * Find instruction src's which are mov's that can be collapsed, replacing
387 * the mov dst with the mov src
390 instr_cp(struct ir3_instruction
*instr
)
392 struct ir3_register
*reg
;
394 if (instr
->regs_count
== 0)
397 if (ir3_instr_check_mark(instr
))
400 /* walk down the graph from each src: */
401 foreach_src_n(reg
, n
, instr
) {
402 struct ir3_instruction
*src
= ssa(reg
);
409 /* TODO non-indirect access we could figure out which register
410 * we actually want and allow cp..
412 if (reg
->flags
& IR3_REG_ARRAY
)
415 reg_cp(instr
, reg
, n
);
418 if (instr
->regs
[0]->flags
& IR3_REG_ARRAY
) {
419 struct ir3_instruction
*src
= ssa(instr
->regs
[0]);
424 if (instr
->address
) {
425 instr_cp(instr
->address
);
426 ir3_instr_set_address(instr
, eliminate_output_mov(instr
->address
));
431 ir3_cp(struct ir3
*ir
)
435 for (unsigned i
= 0; i
< ir
->noutputs
; i
++) {
436 if (ir
->outputs
[i
]) {
437 instr_cp(ir
->outputs
[i
]);
438 ir
->outputs
[i
] = eliminate_output_mov(ir
->outputs
[i
]);
442 for (unsigned i
= 0; i
< ir
->keeps_count
; i
++) {
443 instr_cp(ir
->keeps
[i
]);
444 ir
->keeps
[i
] = eliminate_output_mov(ir
->keeps
[i
]);
447 list_for_each_entry (struct ir3_block
, block
, &ir
->block_list
, node
) {
448 if (block
->condition
) {
449 instr_cp(block
->condition
);
450 block
->condition
= eliminate_output_mov(block
->condition
);