2 * Copyright (C) 2008 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.
31 * "Not-quite SSA" and Dead-Code Elimination.
33 * @note This code uses SWIZZLE_NIL in a source register to indicate that
34 * the corresponding component is ignored by the corresponding instruction.
37 #include "radeon_nqssadce.h"
41 * Return the @ref register_state for the given register (or 0 for untracked
42 * registers, i.e. constants).
44 static struct register_state
*get_reg_state(struct nqssadce_state
* s
, GLuint file
, GLuint index
)
47 case PROGRAM_TEMPORARY
: return &s
->Temps
[index
];
48 case PROGRAM_OUTPUT
: return &s
->Outputs
[index
];
55 * Left multiplication of a register with a swizzle
57 * @note Works correctly only for X, Y, Z, W swizzles, not for constant swizzles.
59 static struct prog_src_register
lmul_swizzle(GLuint swizzle
, struct prog_src_register srcreg
)
61 struct prog_src_register tmp
= srcreg
;
65 for(i
= 0; i
< 4; ++i
) {
66 GLuint swz
= GET_SWZ(swizzle
, i
);
68 tmp
.Swizzle
|= GET_SWZ(srcreg
.Swizzle
, swz
) << (i
*3);
69 tmp
.NegateBase
|= GET_BIT(srcreg
.NegateBase
, swz
) << i
;
71 tmp
.Swizzle
|= swz
<< (i
*3);
78 static struct prog_instruction
* track_used_srcreg(struct nqssadce_state
* s
,
79 struct prog_instruction
*inst
, GLint src
, GLuint sourced
)
82 GLuint deswz_source
= 0;
84 for(i
= 0; i
< 4; ++i
) {
85 if (GET_BIT(sourced
, i
)) {
86 GLuint swz
= GET_SWZ(inst
->SrcReg
[src
].Swizzle
, i
);
87 deswz_source
|= 1 << swz
;
89 inst
->SrcReg
[src
].Swizzle
&= ~(7 << (3*i
));
90 inst
->SrcReg
[src
].Swizzle
|= SWIZZLE_NIL
<< (3*i
);
94 if (!s
->Descr
->IsNativeSwizzle(inst
->Opcode
, inst
->SrcReg
[src
])) {
95 struct prog_dst_register dstreg
= inst
->DstReg
;
96 dstreg
.File
= PROGRAM_TEMPORARY
;
97 dstreg
.Index
= _mesa_find_free_register(s
->Program
, PROGRAM_TEMPORARY
);
98 dstreg
.WriteMask
= sourced
;
100 s
->Descr
->BuildSwizzle(s
, dstreg
, inst
->SrcReg
[src
]);
102 inst
= s
->Program
->Instructions
+ s
->IP
;
103 inst
->SrcReg
[src
].File
= PROGRAM_TEMPORARY
;
104 inst
->SrcReg
[src
].Index
= dstreg
.Index
;
105 inst
->SrcReg
[src
].Swizzle
= 0;
106 inst
->SrcReg
[src
].NegateBase
= 0;
107 inst
->SrcReg
[src
].Abs
= 0;
108 inst
->SrcReg
[src
].NegateAbs
= 0;
109 for(i
= 0; i
< 4; ++i
) {
110 if (GET_BIT(sourced
, i
))
111 inst
->SrcReg
[src
].Swizzle
|= i
<< (3*i
);
113 inst
->SrcReg
[src
].Swizzle
|= SWIZZLE_NIL
<< (3*i
);
115 deswz_source
= sourced
;
118 struct register_state
*regstate
= get_reg_state(s
, inst
->SrcReg
[src
].File
, inst
->SrcReg
[src
].Index
);
120 regstate
->Sourced
|= deswz_source
& 0xf;
126 static void rewrite_depth_out(struct prog_instruction
*inst
)
128 if (inst
->DstReg
.WriteMask
& WRITEMASK_Z
) {
129 inst
->DstReg
.WriteMask
= WRITEMASK_W
;
131 inst
->DstReg
.WriteMask
= 0;
135 switch (inst
->Opcode
) {
138 inst
->SrcReg
[0] = lmul_swizzle(SWIZZLE_ZZZZ
, inst
->SrcReg
[0]);
144 inst
->SrcReg
[0] = lmul_swizzle(SWIZZLE_ZZZZ
, inst
->SrcReg
[0]);
145 inst
->SrcReg
[1] = lmul_swizzle(SWIZZLE_ZZZZ
, inst
->SrcReg
[1]);
149 inst
->SrcReg
[0] = lmul_swizzle(SWIZZLE_ZZZZ
, inst
->SrcReg
[0]);
150 inst
->SrcReg
[1] = lmul_swizzle(SWIZZLE_ZZZZ
, inst
->SrcReg
[1]);
151 inst
->SrcReg
[2] = lmul_swizzle(SWIZZLE_ZZZZ
, inst
->SrcReg
[2]);
154 // Scalar instructions needn't be reswizzled
159 static void unalias_srcregs(struct prog_instruction
*inst
, GLuint oldindex
, GLuint newindex
)
161 int nsrc
= _mesa_num_inst_src_regs(inst
->Opcode
);
163 for(i
= 0; i
< nsrc
; ++i
)
164 if (inst
->SrcReg
[i
].File
== PROGRAM_TEMPORARY
&& inst
->SrcReg
[i
].Index
== oldindex
)
165 inst
->SrcReg
[i
].Index
= newindex
;
168 static void unalias_temporary(struct nqssadce_state
* s
, GLuint oldindex
)
170 GLuint newindex
= _mesa_find_free_register(s
->Program
, PROGRAM_TEMPORARY
);
172 for(ip
= 0; ip
< s
->IP
; ++ip
) {
173 struct prog_instruction
* inst
= s
->Program
->Instructions
+ ip
;
174 if (inst
->DstReg
.File
== PROGRAM_TEMPORARY
&& inst
->DstReg
.Index
== oldindex
)
175 inst
->DstReg
.Index
= newindex
;
176 unalias_srcregs(inst
, oldindex
, newindex
);
178 unalias_srcregs(s
->Program
->Instructions
+ s
->IP
, oldindex
, newindex
);
183 * Handle one instruction.
185 static void process_instruction(struct nqssadce_state
* s
)
187 struct prog_instruction
*inst
= s
->Program
->Instructions
+ s
->IP
;
189 if (inst
->Opcode
== OPCODE_END
)
192 if (inst
->Opcode
!= OPCODE_KIL
) {
193 if (s
->Descr
->RewriteDepthOut
) {
194 if (inst
->DstReg
.File
== PROGRAM_OUTPUT
&& inst
->DstReg
.Index
== FRAG_RESULT_DEPR
)
195 rewrite_depth_out(inst
);
198 struct register_state
*regstate
= get_reg_state(s
, inst
->DstReg
.File
, inst
->DstReg
.Index
);
200 _mesa_problem(s
->Ctx
, "NqssaDce: bad destination register (%i[%i])\n",
201 inst
->DstReg
.File
, inst
->DstReg
.Index
);
205 inst
->DstReg
.WriteMask
&= regstate
->Sourced
;
206 regstate
->Sourced
&= ~inst
->DstReg
.WriteMask
;
208 if (inst
->DstReg
.WriteMask
== 0) {
209 _mesa_delete_instructions(s
->Program
, s
->IP
, 1);
213 if (inst
->DstReg
.File
== PROGRAM_TEMPORARY
&& !regstate
->Sourced
)
214 unalias_temporary(s
, inst
->DstReg
.Index
);
217 /* Attention: Due to swizzle emulation code, the following
218 * might change the instruction stream under us, so we have
219 * to be careful with the inst pointer. */
220 switch (inst
->Opcode
) {
225 inst
= track_used_srcreg(s
, inst
, 0, inst
->DstReg
.WriteMask
);
231 inst
= track_used_srcreg(s
, inst
, 0, inst
->DstReg
.WriteMask
);
232 inst
= track_used_srcreg(s
, inst
, 1, inst
->DstReg
.WriteMask
);
236 inst
= track_used_srcreg(s
, inst
, 0, inst
->DstReg
.WriteMask
);
237 inst
= track_used_srcreg(s
, inst
, 1, inst
->DstReg
.WriteMask
);
238 inst
= track_used_srcreg(s
, inst
, 2, inst
->DstReg
.WriteMask
);
246 inst
= track_used_srcreg(s
, inst
, 0, 0x1);
249 inst
= track_used_srcreg(s
, inst
, 0, 0x7);
250 inst
= track_used_srcreg(s
, inst
, 1, 0x7);
253 inst
= track_used_srcreg(s
, inst
, 0, 0xf);
254 inst
= track_used_srcreg(s
, inst
, 1, 0xf);
260 inst
= track_used_srcreg(s
, inst
, 0, 0xf);
263 _mesa_problem(s
->Ctx
, "NqssaDce: Unknown opcode %d\n", inst
->Opcode
);
269 void radeonNqssaDce(GLcontext
*ctx
, struct gl_program
*p
, struct radeon_nqssadce_descr
* descr
)
271 struct nqssadce_state s
;
273 _mesa_bzero(&s
, sizeof(s
));
278 s
.IP
= p
->NumInstructions
;
282 process_instruction(&s
);