2 * Copyright (C) 2009 Nicolai Haehnle.
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial
16 * portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
22 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
23 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
24 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 #include "radeon_dataflow.h"
30 #include "radeon_compiler.h"
31 #include "radeon_swizzle.h"
33 struct peephole_state
{
34 struct rc_instruction
* Inst
;
35 /** Stores a bitmask of the components that are still "alive" (i.e.
36 * they have not been written to since Inst was executed.)
38 unsigned int WriteMask
;
41 typedef void (*rc_presub_replace_fn
)(struct peephole_state
*,
42 struct rc_instruction
*,
45 static struct rc_src_register
chain_srcregs(struct rc_src_register outer
, struct rc_src_register inner
)
47 struct rc_src_register combine
;
48 combine
.File
= inner
.File
;
49 combine
.Index
= inner
.Index
;
50 combine
.RelAddr
= inner
.RelAddr
;
53 combine
.Negate
= outer
.Negate
;
55 combine
.Abs
= inner
.Abs
;
57 for(unsigned int chan
= 0; chan
< 4; ++chan
) {
58 unsigned int swz
= GET_SWZ(outer
.Swizzle
, chan
);
60 combine
.Negate
|= GET_BIT(inner
.Negate
, swz
) << chan
;
62 combine
.Negate
^= outer
.Negate
;
64 combine
.Swizzle
= combine_swizzles(inner
.Swizzle
, outer
.Swizzle
);
68 struct copy_propagate_state
{
69 struct radeon_compiler
* C
;
70 struct rc_instruction
* Mov
;
71 unsigned int Conflict
:1;
73 /** Whether Mov's source has been clobbered */
74 unsigned int SourceClobbered
:1;
76 /** Which components of Mov's destination register are still from that Mov? */
77 unsigned int MovMask
:4;
79 /** Which components of Mov's destination register are clearly *not* from that Mov */
80 unsigned int DefinedMask
:4;
82 /** Which components of Mov's source register are sourced */
83 unsigned int SourcedMask
:4;
85 /** Branch depth beyond Mov; negative value indicates we left the Mov's block */
90 * This is a callback function that is meant to be passed to
91 * rc_for_all_reads_mask. This function will be called once for each source
93 * @param inst The instruction that the source register belongs to.
94 * @param file The register file of the source register.
95 * @param index The index of the source register.
96 * @param mask The components of the source register that are being read from.
98 static void copy_propagate_scan_read(void * data
, struct rc_instruction
* inst
,
99 rc_register_file file
, unsigned int index
, unsigned int mask
)
101 struct copy_propagate_state
* s
= data
;
103 /* XXX This could probably be handled better. */
104 if (file
== RC_FILE_ADDRESS
) {
109 if (file
!= RC_FILE_TEMPORARY
|| index
!= s
->Mov
->U
.I
.DstReg
.Index
)
112 /* These instructions cannot read from the constants file.
113 * see radeonTransformTEX()
115 if(s
->Mov
->U
.I
.SrcReg
[0].File
!= RC_FILE_TEMPORARY
&&
116 s
->Mov
->U
.I
.SrcReg
[0].File
!= RC_FILE_INPUT
&&
117 (inst
->U
.I
.Opcode
== RC_OPCODE_TEX
||
118 inst
->U
.I
.Opcode
== RC_OPCODE_TXB
||
119 inst
->U
.I
.Opcode
== RC_OPCODE_TXP
||
120 inst
->U
.I
.Opcode
== RC_OPCODE_KIL
)){
124 if ((mask
& s
->MovMask
) == mask
) {
125 if (s
->SourceClobbered
) {
128 } else if ((mask
& s
->DefinedMask
) == mask
) {
129 /* read from something entirely written by other instruction: this is okay */
131 /* read from component combination that is not well-defined without
132 * the MOV: cannot remove it */
137 static void copy_propagate_scan_write(void * data
, struct rc_instruction
* inst
,
138 rc_register_file file
, unsigned int index
, unsigned int mask
)
140 struct copy_propagate_state
* s
= data
;
142 if (s
->BranchDepth
< 0)
145 if (file
== s
->Mov
->U
.I
.DstReg
.File
&& index
== s
->Mov
->U
.I
.DstReg
.Index
) {
147 if (s
->BranchDepth
== 0)
148 s
->DefinedMask
|= mask
;
150 s
->DefinedMask
&= ~mask
;
152 if (file
== s
->Mov
->U
.I
.SrcReg
[0].File
&& index
== s
->Mov
->U
.I
.SrcReg
[0].Index
) {
153 if (mask
& s
->SourcedMask
)
154 s
->SourceClobbered
= 1;
155 } else if (s
->Mov
->U
.I
.SrcReg
[0].RelAddr
&& file
== RC_FILE_ADDRESS
) {
156 s
->SourceClobbered
= 1;
160 static void copy_propagate(struct radeon_compiler
* c
, struct rc_instruction
* inst_mov
)
162 struct copy_propagate_state s
;
164 if (inst_mov
->U
.I
.DstReg
.File
!= RC_FILE_TEMPORARY
||
165 inst_mov
->U
.I
.DstReg
.RelAddr
||
166 inst_mov
->U
.I
.WriteALUResult
)
169 memset(&s
, 0, sizeof(s
));
172 s
.MovMask
= inst_mov
->U
.I
.DstReg
.WriteMask
;
173 s
.DefinedMask
= RC_MASK_XYZW
& ~s
.MovMask
;
175 for(unsigned int chan
= 0; chan
< 4; ++chan
) {
176 unsigned int swz
= GET_SWZ(inst_mov
->U
.I
.SrcReg
[0].Swizzle
, chan
);
177 s
.SourcedMask
|= (1 << swz
) & RC_MASK_XYZW
;
180 /* 1st pass: Check whether all subsequent readers can be changed */
181 for(struct rc_instruction
* inst
= inst_mov
->Next
;
182 inst
!= &c
->Program
.Instructions
;
184 const struct rc_opcode_info
* info
= rc_get_opcode_info(inst
->U
.I
.Opcode
);
185 /* XXX In the future we might be able to make the optimizer
186 * smart enough to handle loops. */
187 if(inst
->U
.I
.Opcode
== RC_OPCODE_BGNLOOP
188 || inst
->U
.I
.Opcode
== RC_OPCODE_ENDLOOP
){
192 /* It is possible to do copy propigation in this situation,
193 * just not right now, see peephole_add_presub_inv() */
194 if (inst_mov
->U
.I
.PreSub
.Opcode
!= RC_PRESUB_NONE
&&
195 info
->NumSrcRegs
> 2) {
199 rc_for_all_reads_mask(inst
, copy_propagate_scan_read
, &s
);
200 rc_for_all_writes_mask(inst
, copy_propagate_scan_write
, &s
);
204 if (s
.BranchDepth
>= 0) {
205 if (inst
->U
.I
.Opcode
== RC_OPCODE_IF
) {
207 } else if (inst
->U
.I
.Opcode
== RC_OPCODE_ENDIF
208 || inst
->U
.I
.Opcode
== RC_OPCODE_ELSE
) {
210 if (s
.BranchDepth
< 0) {
211 s
.DefinedMask
&= ~s
.MovMask
;
221 /* 2nd pass: We can satisfy all readers, so switch them over all at once */
222 s
.MovMask
= inst_mov
->U
.I
.DstReg
.WriteMask
;
225 for(struct rc_instruction
* inst
= inst_mov
->Next
;
226 inst
!= &c
->Program
.Instructions
;
228 const struct rc_opcode_info
* opcode
= rc_get_opcode_info(inst
->U
.I
.Opcode
);
229 for(unsigned int src
= 0; src
< opcode
->NumSrcRegs
; ++src
) {
230 if (inst
->U
.I
.SrcReg
[src
].File
== RC_FILE_TEMPORARY
&&
231 inst
->U
.I
.SrcReg
[src
].Index
== s
.Mov
->U
.I
.DstReg
.Index
) {
232 unsigned int refmask
= 0;
234 for(unsigned int chan
= 0; chan
< 4; ++chan
) {
235 unsigned int swz
= GET_SWZ(inst
->U
.I
.SrcReg
[src
].Swizzle
, chan
);
236 refmask
|= (1 << swz
) & RC_MASK_XYZW
;
239 if ((refmask
& s
.MovMask
) == refmask
) {
240 inst
->U
.I
.SrcReg
[src
] = chain_srcregs(inst
->U
.I
.SrcReg
[src
], s
.Mov
->U
.I
.SrcReg
[0]);
241 if (s
.Mov
->U
.I
.SrcReg
[0].File
== RC_FILE_PRESUB
)
242 inst
->U
.I
.PreSub
= s
.Mov
->U
.I
.PreSub
;
247 if (opcode
->HasDstReg
) {
248 if (inst
->U
.I
.DstReg
.File
== RC_FILE_TEMPORARY
&&
249 inst
->U
.I
.DstReg
.Index
== s
.Mov
->U
.I
.DstReg
.Index
) {
250 s
.MovMask
&= ~inst
->U
.I
.DstReg
.WriteMask
;
254 if (s
.BranchDepth
>= 0) {
255 if (inst
->U
.I
.Opcode
== RC_OPCODE_IF
) {
257 } else if (inst
->U
.I
.Opcode
== RC_OPCODE_ENDIF
258 || inst
->U
.I
.Opcode
== RC_OPCODE_ELSE
) {
260 if (s
.BranchDepth
< 0)
261 break; /* no more readers after this point */
266 /* Finally, remove the original MOV instruction */
267 rc_remove_instruction(inst_mov
);
271 * Check if a source register is actually always the same
274 static int is_src_uniform_constant(struct rc_src_register src
,
275 rc_swizzle
* pswz
, unsigned int * pnegate
)
279 if (src
.File
!= RC_FILE_NONE
) {
284 for(unsigned int chan
= 0; chan
< 4; ++chan
) {
285 unsigned int swz
= GET_SWZ(src
.Swizzle
, chan
);
290 if (swz
== RC_SWIZZLE_UNUSED
)
295 *pnegate
= GET_BIT(src
.Negate
, chan
);
298 if (swz
!= *pswz
|| *pnegate
!= GET_BIT(src
.Negate
, chan
)) {
308 static void constant_folding_mad(struct rc_instruction
* inst
)
313 if (is_src_uniform_constant(inst
->U
.I
.SrcReg
[2], &swz
, &negate
)) {
314 if (swz
== RC_SWIZZLE_ZERO
) {
315 inst
->U
.I
.Opcode
= RC_OPCODE_MUL
;
320 if (is_src_uniform_constant(inst
->U
.I
.SrcReg
[1], &swz
, &negate
)) {
321 if (swz
== RC_SWIZZLE_ONE
) {
322 inst
->U
.I
.Opcode
= RC_OPCODE_ADD
;
324 inst
->U
.I
.SrcReg
[0].Negate
^= RC_MASK_XYZW
;
325 inst
->U
.I
.SrcReg
[1] = inst
->U
.I
.SrcReg
[2];
327 } else if (swz
== RC_SWIZZLE_ZERO
) {
328 inst
->U
.I
.Opcode
= RC_OPCODE_MOV
;
329 inst
->U
.I
.SrcReg
[0] = inst
->U
.I
.SrcReg
[2];
334 if (is_src_uniform_constant(inst
->U
.I
.SrcReg
[0], &swz
, &negate
)) {
335 if (swz
== RC_SWIZZLE_ONE
) {
336 inst
->U
.I
.Opcode
= RC_OPCODE_ADD
;
338 inst
->U
.I
.SrcReg
[1].Negate
^= RC_MASK_XYZW
;
339 inst
->U
.I
.SrcReg
[0] = inst
->U
.I
.SrcReg
[2];
341 } else if (swz
== RC_SWIZZLE_ZERO
) {
342 inst
->U
.I
.Opcode
= RC_OPCODE_MOV
;
343 inst
->U
.I
.SrcReg
[0] = inst
->U
.I
.SrcReg
[2];
349 static void constant_folding_mul(struct rc_instruction
* inst
)
354 if (is_src_uniform_constant(inst
->U
.I
.SrcReg
[0], &swz
, &negate
)) {
355 if (swz
== RC_SWIZZLE_ONE
) {
356 inst
->U
.I
.Opcode
= RC_OPCODE_MOV
;
357 inst
->U
.I
.SrcReg
[0] = inst
->U
.I
.SrcReg
[1];
359 inst
->U
.I
.SrcReg
[0].Negate
^= RC_MASK_XYZW
;
361 } else if (swz
== RC_SWIZZLE_ZERO
) {
362 inst
->U
.I
.Opcode
= RC_OPCODE_MOV
;
363 inst
->U
.I
.SrcReg
[0].Swizzle
= RC_SWIZZLE_0000
;
368 if (is_src_uniform_constant(inst
->U
.I
.SrcReg
[1], &swz
, &negate
)) {
369 if (swz
== RC_SWIZZLE_ONE
) {
370 inst
->U
.I
.Opcode
= RC_OPCODE_MOV
;
372 inst
->U
.I
.SrcReg
[0].Negate
^= RC_MASK_XYZW
;
374 } else if (swz
== RC_SWIZZLE_ZERO
) {
375 inst
->U
.I
.Opcode
= RC_OPCODE_MOV
;
376 inst
->U
.I
.SrcReg
[0].Swizzle
= RC_SWIZZLE_0000
;
382 static void constant_folding_add(struct rc_instruction
* inst
)
387 if (is_src_uniform_constant(inst
->U
.I
.SrcReg
[0], &swz
, &negate
)) {
388 if (swz
== RC_SWIZZLE_ZERO
) {
389 inst
->U
.I
.Opcode
= RC_OPCODE_MOV
;
390 inst
->U
.I
.SrcReg
[0] = inst
->U
.I
.SrcReg
[1];
395 if (is_src_uniform_constant(inst
->U
.I
.SrcReg
[1], &swz
, &negate
)) {
396 if (swz
== RC_SWIZZLE_ZERO
) {
397 inst
->U
.I
.Opcode
= RC_OPCODE_MOV
;
404 * Replace 0.0, 1.0 and 0.5 immediate constants by their
405 * respective swizzles. Simplify instructions like ADD dst, src, 0;
407 static void constant_folding(struct radeon_compiler
* c
, struct rc_instruction
* inst
)
409 const struct rc_opcode_info
* opcode
= rc_get_opcode_info(inst
->U
.I
.Opcode
);
411 /* Replace 0.0, 1.0 and 0.5 immediates by their explicit swizzles */
412 for(unsigned int src
= 0; src
< opcode
->NumSrcRegs
; ++src
) {
413 if (inst
->U
.I
.SrcReg
[src
].File
!= RC_FILE_CONSTANT
||
414 inst
->U
.I
.SrcReg
[src
].RelAddr
||
415 inst
->U
.I
.SrcReg
[src
].Index
>= c
->Program
.Constants
.Count
)
418 struct rc_constant
* constant
=
419 &c
->Program
.Constants
.Constants
[inst
->U
.I
.SrcReg
[src
].Index
];
421 if (constant
->Type
!= RC_CONSTANT_IMMEDIATE
)
424 struct rc_src_register newsrc
= inst
->U
.I
.SrcReg
[src
];
425 int have_real_reference
= 0;
426 for(unsigned int chan
= 0; chan
< 4; ++chan
) {
427 unsigned int swz
= GET_SWZ(newsrc
.Swizzle
, chan
);
432 float imm
= constant
->u
.Immediate
[swz
];
437 if (baseimm
== 0.0) {
438 newswz
= RC_SWIZZLE_ZERO
;
439 } else if (baseimm
== 1.0) {
440 newswz
= RC_SWIZZLE_ONE
;
441 } else if (baseimm
== 0.5 && c
->has_half_swizzles
) {
442 newswz
= RC_SWIZZLE_HALF
;
444 have_real_reference
= 1;
448 SET_SWZ(newsrc
.Swizzle
, chan
, newswz
);
449 if (imm
< 0.0 && !newsrc
.Abs
)
450 newsrc
.Negate
^= 1 << chan
;
453 if (!have_real_reference
) {
454 newsrc
.File
= RC_FILE_NONE
;
458 /* don't make the swizzle worse */
459 if (!c
->SwizzleCaps
->IsNative(inst
->U
.I
.Opcode
, newsrc
) &&
460 c
->SwizzleCaps
->IsNative(inst
->U
.I
.Opcode
, inst
->U
.I
.SrcReg
[src
]))
463 inst
->U
.I
.SrcReg
[src
] = newsrc
;
466 /* Simplify instructions based on constants */
467 if (inst
->U
.I
.Opcode
== RC_OPCODE_MAD
)
468 constant_folding_mad(inst
);
470 /* note: MAD can simplify to MUL or ADD */
471 if (inst
->U
.I
.Opcode
== RC_OPCODE_MUL
)
472 constant_folding_mul(inst
);
473 else if (inst
->U
.I
.Opcode
== RC_OPCODE_ADD
)
474 constant_folding_add(inst
);
478 * This function returns a writemask that indicates wich components are
479 * read by src and also written by dst.
481 static unsigned int src_reads_dst_mask(struct rc_src_register src
,
482 struct rc_dst_register dst
)
484 unsigned int mask
= 0;
486 if (dst
.File
!= src
.File
|| dst
.Index
!= src
.Index
) {
490 for(i
= 0; i
< 4; i
++) {
491 mask
|= 1 << GET_SWZ(src
.Swizzle
, i
);
493 mask
&= RC_MASK_XYZW
;
498 /* Return 1 if the source registers has a constant swizzle (e.g. 0, 0.5, 1.0)
499 * in any of its channels. Return 0 otherwise. */
500 static int src_has_const_swz(struct rc_src_register src
) {
502 for(chan
= 0; chan
< 4; chan
++) {
503 unsigned int swz
= GET_SWZ(src
.Swizzle
, chan
);
504 if (swz
== RC_SWIZZLE_ZERO
|| swz
== RC_SWIZZLE_HALF
505 || swz
== RC_SWIZZLE_ONE
) {
512 static void peephole_scan_write(void * data
, struct rc_instruction
* inst
,
513 rc_register_file file
, unsigned int index
, unsigned int mask
)
515 struct peephole_state
* s
= data
;
516 if(s
->Inst
->U
.I
.DstReg
.File
== file
517 && s
->Inst
->U
.I
.DstReg
.Index
== index
) {
518 unsigned int common_mask
= s
->WriteMask
& mask
;
519 s
->WriteMask
&= ~common_mask
;
523 static int presub_helper(
524 struct radeon_compiler
* c
,
525 struct peephole_state
* s
,
526 rc_presubtract_op presub_opcode
,
527 rc_presub_replace_fn presub_replace
)
529 struct rc_instruction
* inst
;
530 unsigned int can_remove
= 0;
531 unsigned int cant_sub
= 0;
533 for(inst
= s
->Inst
->Next
; inst
!= &c
->Program
.Instructions
;
536 const struct rc_opcode_info
* info
=
537 rc_get_opcode_info(inst
->U
.I
.Opcode
);
539 for(i
= 0; i
< info
->NumSrcRegs
; i
++) {
540 if(s
->Inst
->U
.I
.DstReg
.WriteMask
!=
541 src_reads_dst_mask(inst
->U
.I
.SrcReg
[i
],
542 s
->Inst
->U
.I
.DstReg
)) {
549 /* XXX: There are some situations where instructions
550 * with more than 2 src registers can use the
551 * presubtract select, but to keep things simple we
552 * will disable presubtract on these instructions for
553 * now. Note: This if statement should not be pulled
554 * outside of the loop, because it only applies to
555 * instructions that could potentially use the
556 * presubtract source. */
557 if (info
->NumSrcRegs
> 2) {
562 /* We can't use more than one presubtract value in an
563 * instruction, unless the two prsubtract operations
564 * are the same and read from the same registers. */
565 if (inst
->U
.I
.PreSub
.Opcode
!= RC_PRESUB_NONE
) {
566 if (inst
->U
.I
.PreSub
.Opcode
!= presub_opcode
567 || inst
->U
.I
.PreSub
.SrcReg
[0].File
!=
568 s
->Inst
->U
.I
.SrcReg
[1].File
569 || inst
->U
.I
.PreSub
.SrcReg
[0].Index
!=
570 s
->Inst
->U
.I
.SrcReg
[1].Index
) {
576 presub_replace(s
, inst
, i
);
581 rc_for_all_writes_mask(inst
, peephole_scan_write
, s
);
582 /* If all components of inst_add's destination register have
583 * been written to by subsequent instructions, the original
584 * value of the destination register is no longer valid and
585 * we can't keep doing substitutions. */
589 /* Make this instruction doesn't write to the presubtract source. */
590 if (inst
->U
.I
.DstReg
.WriteMask
&
591 src_reads_dst_mask(s
->Inst
->U
.I
.SrcReg
[1],
593 || src_reads_dst_mask(s
->Inst
->U
.I
.SrcReg
[0],
595 || info
->IsFlowControl
) {
602 /* This function assumes that s->Inst->U.I.SrcReg[0] and
603 * s->Inst->U.I.SrcReg[1] aren't both negative. */
604 static void presub_replace_add(struct peephole_state
*s
,
605 struct rc_instruction
* inst
,
606 unsigned int src_index
)
608 rc_presubtract_op presub_opcode
;
609 if (s
->Inst
->U
.I
.SrcReg
[1].Negate
|| s
->Inst
->U
.I
.SrcReg
[0].Negate
)
610 presub_opcode
= RC_PRESUB_SUB
;
612 presub_opcode
= RC_PRESUB_ADD
;
614 if (s
->Inst
->U
.I
.SrcReg
[1].Negate
) {
615 inst
->U
.I
.PreSub
.SrcReg
[0] = s
->Inst
->U
.I
.SrcReg
[1];
616 inst
->U
.I
.PreSub
.SrcReg
[1] = s
->Inst
->U
.I
.SrcReg
[0];
618 inst
->U
.I
.PreSub
.SrcReg
[0] = s
->Inst
->U
.I
.SrcReg
[0];
619 inst
->U
.I
.PreSub
.SrcReg
[1] = s
->Inst
->U
.I
.SrcReg
[1];
621 inst
->U
.I
.PreSub
.SrcReg
[0].Negate
= 0;
622 inst
->U
.I
.PreSub
.SrcReg
[1].Negate
= 0;
623 inst
->U
.I
.PreSub
.Opcode
= presub_opcode
;
624 inst
->U
.I
.SrcReg
[src_index
] = chain_srcregs(inst
->U
.I
.SrcReg
[src_index
],
625 inst
->U
.I
.PreSub
.SrcReg
[0]);
626 inst
->U
.I
.SrcReg
[src_index
].File
= RC_FILE_PRESUB
;
627 inst
->U
.I
.SrcReg
[src_index
].Index
= presub_opcode
;
630 static int peephole_add_presub_add(
631 struct radeon_compiler
* c
,
632 struct rc_instruction
* inst_add
)
634 struct rc_src_register
* src0
= NULL
;
635 struct rc_src_register
* src1
= NULL
;
637 struct peephole_state s
;
639 if (inst_add
->U
.I
.PreSub
.Opcode
!= RC_PRESUB_NONE
)
642 if (inst_add
->U
.I
.SaturateMode
)
645 if (inst_add
->U
.I
.SrcReg
[0].Swizzle
!= inst_add
->U
.I
.SrcReg
[1].Swizzle
)
648 /* src0 and src1 can't have absolute values only one can be negative and they must be all negative or all positive. */
649 for (i
= 0; i
< 2; i
++) {
650 if (inst_add
->U
.I
.SrcReg
[i
].Abs
)
652 if ((inst_add
->U
.I
.SrcReg
[i
].Negate
653 & inst_add
->U
.I
.DstReg
.WriteMask
) ==
654 inst_add
->U
.I
.DstReg
.WriteMask
) {
655 src0
= &inst_add
->U
.I
.SrcReg
[i
];
657 src1
= &inst_add
->U
.I
.SrcReg
[i
];
659 src0
= &inst_add
->U
.I
.SrcReg
[i
];
667 s
.WriteMask
= inst_add
->U
.I
.DstReg
.WriteMask
;
668 if (presub_helper(c
, &s
, RC_PRESUB_ADD
, presub_replace_add
)) {
669 rc_remove_instruction(inst_add
);
675 static void presub_replace_inv(struct peephole_state
* s
,
676 struct rc_instruction
* inst
,
677 unsigned int src_index
)
679 /* We must be careful not to modify s->Inst, since it
680 * is possible it will remain part of the program.
681 * XXX Maybe pass a struct instead of a pointer for s->Inst.*/
682 inst
->U
.I
.PreSub
.SrcReg
[0] = s
->Inst
->U
.I
.SrcReg
[1];
683 inst
->U
.I
.PreSub
.SrcReg
[0].Negate
= 0;
684 inst
->U
.I
.PreSub
.Opcode
= RC_PRESUB_INV
;
685 inst
->U
.I
.SrcReg
[src_index
] = chain_srcregs(inst
->U
.I
.SrcReg
[src_index
],
686 inst
->U
.I
.PreSub
.SrcReg
[0]);
688 inst
->U
.I
.SrcReg
[src_index
].File
= RC_FILE_PRESUB
;
689 inst
->U
.I
.SrcReg
[src_index
].Index
= RC_PRESUB_INV
;
693 * PRESUB_INV: ADD TEMP[0], none.1, -TEMP[1]
694 * Use the presubtract 1 - src0 for all readers of TEMP[0]. The first source
695 * of the add instruction must have the constatnt 1 swizzle. This function
696 * does not check const registers to see if their value is 1.0, so it should
697 * be called after the constant_folding optimization.
699 * 0 if the ADD instruction is still part of the program.
700 * 1 if the ADD instruction is no longer part of the program.
702 static int peephole_add_presub_inv(
703 struct radeon_compiler
* c
,
704 struct rc_instruction
* inst_add
)
706 unsigned int i
, swz
, mask
;
707 struct peephole_state s
;
709 if (inst_add
->U
.I
.PreSub
.Opcode
!= RC_PRESUB_NONE
)
712 if (inst_add
->U
.I
.SaturateMode
)
715 mask
= inst_add
->U
.I
.DstReg
.WriteMask
;
717 /* Check if src0 is 1. */
718 /* XXX It would be nice to use is_src_uniform_constant here, but that
719 * function only works if the register's file is RC_FILE_NONE */
720 for(i
= 0; i
< 4; i
++ ) {
721 swz
= GET_SWZ(inst_add
->U
.I
.SrcReg
[0].Swizzle
, i
);
722 if(((1 << i
) & inst_add
->U
.I
.DstReg
.WriteMask
)
723 && swz
!= RC_SWIZZLE_ONE
) {
729 if ((inst_add
->U
.I
.SrcReg
[1].Negate
& inst_add
->U
.I
.DstReg
.WriteMask
) !=
730 inst_add
->U
.I
.DstReg
.WriteMask
731 || inst_add
->U
.I
.SrcReg
[1].Abs
732 || (inst_add
->U
.I
.SrcReg
[1].File
!= RC_FILE_TEMPORARY
733 && inst_add
->U
.I
.SrcReg
[1].File
!= RC_FILE_CONSTANT
)
734 || src_has_const_swz(inst_add
->U
.I
.SrcReg
[1])) {
739 /* Setup the peephole_state information. */
741 s
.WriteMask
= inst_add
->U
.I
.DstReg
.WriteMask
;
743 if (presub_helper(c
, &s
, RC_PRESUB_INV
, presub_replace_inv
)) {
744 rc_remove_instruction(inst_add
);
752 * 0 if inst is still part of the program.
753 * 1 if inst is no longer part of the program.
755 static int peephole(struct radeon_compiler
* c
, struct rc_instruction
* inst
)
757 switch(inst
->U
.I
.Opcode
){
760 if(peephole_add_presub_inv(c
, inst
))
762 if(peephole_add_presub_add(c
, inst
))
772 void rc_optimize(struct radeon_compiler
* c
, void *user
)
774 struct rc_instruction
* inst
= c
->Program
.Instructions
.Next
;
775 while(inst
!= &c
->Program
.Instructions
) {
776 struct rc_instruction
* cur
= inst
;
779 constant_folding(c
, cur
);
784 if (cur
->U
.I
.Opcode
== RC_OPCODE_MOV
) {
785 copy_propagate(c
, cur
);
786 /* cur may no longer be part of the program */