summaryrefslogtreecommitdiff
path: root/src/mesa/drivers/dri/r300/radeon_nqssadce.c
blob: a083c3d2436321b1f2a49ee40fb8f7deb325b37d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
 * Copyright (C) 2008 Nicolai Haehnle.
 *
 * All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice (including the
 * next paragraph) shall be included in all copies or substantial
 * portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE COPYRIGHT OWNER(S) AND/OR ITS SUPPLIERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */

/**
 * @file
 *
 * "Not-quite SSA" and Dead-Code Elimination.
 *
 * @note This code uses SWIZZLE_NIL in a source register to indicate that
 * the corresponding component is ignored by the corresponding instruction.
 */

#include "radeon_nqssadce.h"


/**
 * Return the @ref register_state for the given register (or 0 for untracked
 * registers, i.e. constants).
 */
static struct register_state *get_reg_state(struct nqssadce_state* s, GLuint file, GLuint index)
{
	switch(file) {
	case PROGRAM_TEMPORARY: return &s->Temps[index];
	case PROGRAM_OUTPUT: return &s->Outputs[index];
	default: return 0;
	}
}


/**
 * Left multiplication of a register with a swizzle
 *
 * @note Works correctly only for X, Y, Z, W swizzles, not for constant swizzles.
 */
static struct prog_src_register lmul_swizzle(GLuint swizzle, struct prog_src_register srcreg)
{
	struct prog_src_register tmp = srcreg;
	int i;
	tmp.Swizzle = 0;
	tmp.NegateBase = 0;
	for(i = 0; i < 4; ++i) {
		GLuint swz = GET_SWZ(swizzle, i);
		if (swz < 4) {
			tmp.Swizzle |= GET_SWZ(srcreg.Swizzle, swz) << (i*3);
			tmp.NegateBase |= GET_BIT(srcreg.NegateBase, swz) << i;
		} else {
			tmp.Swizzle |= swz << (i*3);
		}
	}
	return tmp;
}


static struct prog_instruction* track_used_srcreg(struct nqssadce_state* s,
	struct prog_instruction *inst, GLint src, GLuint sourced)
{
	int i;
	GLuint deswz_source = 0;

	for(i = 0; i < 4; ++i) {
		if (GET_BIT(sourced, i)) {
			GLuint swz = GET_SWZ(inst->SrcReg[src].Swizzle, i);
			deswz_source |= 1 << swz;
		} else {
			inst->SrcReg[src].Swizzle &= ~(7 << (3*i));
			inst->SrcReg[src].Swizzle |= SWIZZLE_NIL << (3*i);
		}
	}

	if (!s->Descr->IsNativeSwizzle(inst->Opcode, inst->SrcReg[src])) {
		struct prog_dst_register dstreg = inst->DstReg;
		dstreg.File = PROGRAM_TEMPORARY;
		dstreg.Index = _mesa_find_free_register(s->Program, PROGRAM_TEMPORARY);
		dstreg.WriteMask = sourced;

		s->Descr->BuildSwizzle(s, dstreg, inst->SrcReg[src]);

		inst = s->Program->Instructions + s->IP;
		inst->SrcReg[src].File = PROGRAM_TEMPORARY;
		inst->SrcReg[src].Index = dstreg.Index;
		inst->SrcReg[src].Swizzle = 0;
		inst->SrcReg[src].NegateBase = 0;
		inst->SrcReg[src].Abs = 0;
		inst->SrcReg[src].NegateAbs = 0;
		for(i = 0; i < 4; ++i) {
			if (GET_BIT(sourced, i))
				inst->SrcReg[src].Swizzle |= i << (3*i);
			else
				inst->SrcReg[src].Swizzle |= SWIZZLE_NIL << (3*i);
		}
		deswz_source = sourced;
	}

	struct register_state *regstate = get_reg_state(s, inst->SrcReg[src].File, inst->SrcReg[src].Index);
	if (regstate)
		regstate->Sourced |= deswz_source & 0xf;

	return inst;
}


static void rewrite_depth_out(struct prog_instruction *inst)
{
	if (inst->DstReg.WriteMask & WRITEMASK_Z) {
		inst->DstReg.WriteMask = WRITEMASK_W;
	} else {
		inst->DstReg.WriteMask = 0;
		return;
	}

	switch (inst->Opcode) {
	case OPCODE_FRC:
	case OPCODE_MOV:
		inst->SrcReg[0] = lmul_swizzle(SWIZZLE_ZZZZ, inst->SrcReg[0]);
		break;
	case OPCODE_ADD:
	case OPCODE_MAX:
	case OPCODE_MIN:
	case OPCODE_MUL:
		inst->SrcReg[0] = lmul_swizzle(SWIZZLE_ZZZZ, inst->SrcReg[0]);
		inst->SrcReg[1] = lmul_swizzle(SWIZZLE_ZZZZ, inst->SrcReg[1]);
		break;
	case OPCODE_CMP:
	case OPCODE_MAD:
		inst->SrcReg[0] = lmul_swizzle(SWIZZLE_ZZZZ, inst->SrcReg[0]);
		inst->SrcReg[1] = lmul_swizzle(SWIZZLE_ZZZZ, inst->SrcReg[1]);
		inst->SrcReg[2] = lmul_swizzle(SWIZZLE_ZZZZ, inst->SrcReg[2]);
		break;
	default:
		// Scalar instructions needn't be reswizzled
		break;
	}
}

static void unalias_srcregs(struct prog_instruction *inst, GLuint oldindex, GLuint newindex)
{
	int nsrc = _mesa_num_inst_src_regs(inst->Opcode);
	int i;
	for(i = 0; i < nsrc; ++i)
		if (inst->SrcReg[i].File == PROGRAM_TEMPORARY && inst->SrcReg[i].Index == oldindex)
			inst->SrcReg[i].Index = newindex;
}

static void unalias_temporary(struct nqssadce_state* s, GLuint oldindex)
{
	GLuint newindex = _mesa_find_free_register(s->Program, PROGRAM_TEMPORARY);
	int ip;
	for(ip = 0; ip < s->IP; ++ip) {
		struct prog_instruction* inst = s->Program->Instructions + ip;
		if (inst->DstReg.File == PROGRAM_TEMPORARY && inst->DstReg.Index == oldindex)
			inst->DstReg.Index = newindex;
		unalias_srcregs(inst, oldindex, newindex);
	}
	unalias_srcregs(s->Program->Instructions + s->IP, oldindex, newindex);
}


/**
 * Handle one instruction.
 */
static void process_instruction(struct nqssadce_state* s)
{
	struct prog_instruction *inst = s->Program->Instructions + s->IP;

	if (inst->Opcode == OPCODE_END)
		return;

	if (inst->Opcode != OPCODE_KIL) {
		if (s->Descr->RewriteDepthOut) {
			if (inst->DstReg.File == PROGRAM_OUTPUT && inst->DstReg.Index == FRAG_RESULT_DEPTH)
				rewrite_depth_out(inst);
		}

		struct register_state *regstate = get_reg_state(s, inst->DstReg.File, inst->DstReg.Index);
		if (!regstate) {
			_mesa_problem(s->Ctx, "NqssaDce: bad destination register (%i[%i])\n",
				inst->DstReg.File, inst->DstReg.Index);
			return;
		}

		inst->DstReg.WriteMask &= regstate->Sourced;
		regstate->Sourced &= ~inst->DstReg.WriteMask;

		if (inst->DstReg.WriteMask == 0) {
			_mesa_delete_instructions(s->Program, s->IP, 1);
			return;
		}

		if (inst->DstReg.File == PROGRAM_TEMPORARY && !regstate->Sourced)
			unalias_temporary(s, inst->DstReg.Index);
	}

	/* Attention: Due to swizzle emulation code, the following
	 * might change the instruction stream under us, so we have
	 * to be careful with the inst pointer. */
	switch (inst->Opcode) {
	case OPCODE_DDX:
	case OPCODE_DDY:
	case OPCODE_FRC:
	case OPCODE_MOV:
		inst = track_used_srcreg(s, inst, 0, inst->DstReg.WriteMask);
		break;
	case OPCODE_ADD:
	case OPCODE_MAX:
	case OPCODE_MIN:
	case OPCODE_MUL:
		inst = track_used_srcreg(s, inst, 0, inst->DstReg.WriteMask);
		inst = track_used_srcreg(s, inst, 1, inst->DstReg.WriteMask);
		break;
	case OPCODE_CMP:
	case OPCODE_MAD:
		inst = track_used_srcreg(s, inst, 0, inst->DstReg.WriteMask);
		inst = track_used_srcreg(s, inst, 1, inst->DstReg.WriteMask);
		inst = track_used_srcreg(s, inst, 2, inst->DstReg.WriteMask);
		break;
	case OPCODE_COS:
	case OPCODE_EX2:
	case OPCODE_LG2:
	case OPCODE_RCP:
	case OPCODE_RSQ:
	case OPCODE_SIN:
		inst = track_used_srcreg(s, inst, 0, 0x1);
		break;
	case OPCODE_DP3:
		inst = track_used_srcreg(s, inst, 0, 0x7);
		inst = track_used_srcreg(s, inst, 1, 0x7);
		break;
	case OPCODE_DP4:
		inst = track_used_srcreg(s, inst, 0, 0xf);
		inst = track_used_srcreg(s, inst, 1, 0xf);
		break;
	case OPCODE_KIL:
	case OPCODE_TEX:
	case OPCODE_TXB:
	case OPCODE_TXP:
		inst = track_used_srcreg(s, inst, 0, 0xf);
		break;
	default:
		_mesa_problem(s->Ctx, "NqssaDce: Unknown opcode %d\n", inst->Opcode);
		return;
	}
}


void radeonNqssaDce(GLcontext *ctx, struct gl_program *p, struct radeon_nqssadce_descr* descr)
{
	struct nqssadce_state s;

	_mesa_bzero(&s, sizeof(s));
	s.Ctx = ctx;
	s.Program = p;
	s.Descr = descr;
	s.Descr->Init(&s);
	s.IP = p->NumInstructions;

	while(s.IP > 0) {
		s.IP--;
		process_instruction(&s);
	}
}