aboutsummaryrefslogtreecommitdiffstats
path: root/src/mesa/drivers/dri/r300/compiler/radeon_dataflow_dealias.c
blob: 4596636970a82bb0f907150eb8f8bd9be06e0131 (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
/*
 * Copyright (C) 2009 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.
 *
 */

#include "radeon_dataflow.h"

#include "radeon_compiler.h"


#define DEALIAS_LIST_SIZE 128

struct dealias_state {
	struct radeon_compiler * C;

	unsigned int OldIndex:RC_REGISTER_INDEX_BITS;
	unsigned int NewIndex:RC_REGISTER_INDEX_BITS;
	unsigned int DealiasFail:1;

	struct rc_dataflow_vector * List[DEALIAS_LIST_SIZE];
	unsigned int Length;
};

static void push_dealias_vector(struct dealias_state * s, struct rc_dataflow_vector * vec)
{
	if (s->Length >= DEALIAS_LIST_SIZE) {
		rc_debug(s->C, "%s: list size exceeded\n", __FUNCTION__);
		s->DealiasFail = 1;
		return;
	}

	if (rc_assert(s->C, vec->File == RC_FILE_TEMPORARY && vec->Index == s->OldIndex))
		return;

	s->List[s->Length++] = vec;
}

static void run_dealias(struct dealias_state * s)
{
	unsigned int i;

	for(i = 0; i < s->Length && !s->DealiasFail; ++i) {
		struct rc_dataflow_vector * vec = s->List[i];
		struct rc_dataflow_ref * ref;

		for(ref = vec->Refs.Next; ref != &vec->Refs; ref = ref->Next) {
			if (ref->ReadInstruction->Dataflow.DstRegPrev == ref)
				push_dealias_vector(s, ref->ReadInstruction->Dataflow.DstReg);
		}
	}

	if (s->DealiasFail)
		return;

	for(i = 0; i < s->Length; ++i) {
		struct rc_dataflow_vector * vec = s->List[i];
		struct rc_dataflow_ref * ref;

		vec->Index = s->NewIndex;
		vec->WriteInstruction->I.DstReg.Index = s->NewIndex;

		for(ref = vec->Refs.Next; ref != &vec->Refs; ref = ref->Next) {
			struct rc_instruction * inst = ref->ReadInstruction;
			unsigned int i;

			for(i = 0; i < 3; ++i) {
				if (inst->Dataflow.SrcReg[i] == ref) {
					if (rc_assert(s->C, inst->I.SrcReg[i].File == RC_FILE_TEMPORARY &&
					                    inst->I.SrcReg[i].Index == s->OldIndex))
						return;

					inst->I.SrcReg[i].Index = s->NewIndex;
				}
			}
		}
	}
}

/**
 * Breaks register aliasing to reduce multiple assignments to a single register.
 *
 * This affects sequences like:
 *  MUL r0, ...;
 *  MAD r0, r1, r2, r0;
 * In this example, a new register will be used for the destination of the
 * second MAD.
 *
 * The purpose of this dealiasing is to make the resulting code more SSA-like
 * and therefore make it easier to move instructions around.
 * This is of crucial importance for R300 fragment programs, where de-aliasing
 * can help to reduce texture indirections, but other targets can benefit from
 * it as well.
 *
 * \note When compiling GLSL, there may be some benefit gained from breaking
 * up vectors whose components are unrelated. This is not done yet and should
 * be investigated at some point (of course, a matching pass to re-merge
 * components would be required).
 */
void rc_dataflow_dealias(struct radeon_compiler * c)
{
	struct dealias_state s;

	memset(&s, 0, sizeof(s));
	s.C = c;

	struct rc_instruction * inst;
	for(inst = c->Program.Instructions.Prev; inst != &c->Program.Instructions; inst = inst->Prev) {
		if (!inst->Dataflow.DstRegAliased || inst->Dataflow.DstReg->File != RC_FILE_TEMPORARY)
			continue;

		if (inst->Dataflow.DstReg->UseMask & ~inst->I.DstReg.WriteMask)
			continue;

		s.OldIndex = inst->I.DstReg.Index;
		s.NewIndex = rc_find_free_temporary(c);
		s.DealiasFail = 0;
		s.Length = 0;

		inst->Dataflow.DstRegAliased = 0;
		if (inst->Dataflow.DstRegPrev) {
			rc_dataflow_remove_ref(inst->Dataflow.DstRegPrev);
			inst->Dataflow.DstRegPrev = 0;
		}

		push_dealias_vector(&s, inst->Dataflow.DstReg);
		run_dealias(&s);
	}
}