aboutsummaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/r600/sb/sb_gvn.cpp
blob: caea4ec66607b04301571787fad264067a8af495 (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
/*
 * Copyright 2013 Vadim Girlin <vadimgirlin@gmail.com>
 *
 * 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
 * on the rights to use, copy, modify, merge, publish, distribute, sub
 * license, 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 NON-INFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHOR(S) AND/OR THEIR 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.
 *
 * Authors:
 *      Vadim Girlin
 */

#define GVN_DEBUG 0

#if GVN_DEBUG
#define GVN_DUMP(q) do { q } while (0)
#else
#define GVN_DUMP(q)
#endif

#include "sb_shader.h"
#include "sb_pass.h"
#include "sb_sched.h"

namespace r600_sb {

bool gvn::visit(node& n, bool enter) {
	if (enter) {


		bool rewrite = true;

		if (n.dst[0]->is_agpr()) {
			rewrite = false;
		}


		process_op(n, rewrite);

		assert(n.parent);

		if (n.parent->subtype == NST_LOOP_PHI_CONTAINER) {
			// There is a problem - sometimes with nested loops
			// loop counter initialization for inner loop is incorrectly hoisted
			// out of the outer loop

			// FIXME not sure if this is enough to fix a problem completely,
			// possibly more complete fix is needed (anyway, the
			// problem was seen only in relatively complex
			// case involving nested loops and
			// indirect access to loop counters (without proper array info
			// loop counters may be considered as array elements too),
			// was not seen in any tests
			// or real apps when proper array information is available in TGSI).

			// For now just mark the instructions that initialize loop counters
			// with DONT_HOIST flag to prevent the insts like MOV r, 0
			// (initialization of inner loop's counter with const)
			// from being hoisted out of the outer loop

			assert(!n.src.empty());
			value *v = n.src[0];

			if (v->is_any_gpr() && v->def)
				v->def->flags |= NF_DONT_HOIST;
		}

	} else {
	}
	return true;
}

bool gvn::visit(cf_node& n, bool enter) {
	if (enter) {
		process_op(n);
	} else {
	}
	return true;
}

bool gvn::visit(alu_node& n, bool enter) {
	if (enter) {
		process_op(n);
	} else {
	}
	return true;
}

bool gvn::visit(alu_packed_node& n, bool enter) {
	if (enter) {
		process_op(n);
	} else {
	}
	return false;
}

bool gvn::visit(fetch_node& n, bool enter) {
	if (enter) {
		process_op(n);
	} else {
	}
	return true;
}

bool gvn::visit(region_node& n, bool enter) {
	if (enter) {
// FIXME: loop_phi sources are undefined yet (except theone from the preceding
// code), can we handle that somehow?
//		if (n.loop_phi)
//			run_on(*n.loop_phi);
	} else {
		if (n.loop_phi)
			run_on(*n.loop_phi);

		if (n.phi)
			run_on(*n.phi);
	}
	return true;
}

bool gvn::process_src(value* &v, bool rewrite) {
	if (!v->gvn_source)
		sh.vt.add_value(v);

	if (rewrite && !v->gvn_source->is_rel()) {
		v = v->gvn_source;
		return true;
	}
	return false;
}

// FIXME: maybe handle it in the scheduler?
void gvn::process_alu_src_constants(node &n, value* &v) {
	if (n.src.size() < 3) {
		process_src(v, true);
		return;
	}

	if (!v->gvn_source)
		sh.vt.add_value(v);

	rp_kcache_tracker kc(sh);

	if (v->gvn_source->is_kcache())
		kc.try_reserve(v->gvn_source->select);

	// don't propagate 3rd constant to the trans-only instruction
	if (!n.is_alu_packed()) {
		alu_node *a = static_cast<alu_node*>(&n);
		if (a->bc.op_ptr->src_count == 3 && !(a->bc.slot_flags & AF_V)) {
			unsigned const_count = 0;
			for (vvec::iterator I = n.src.begin(), E = n.src.end(); I != E;
					++I) {
				value *c = (*I);
				if (c && c->is_readonly() && ++const_count == 2) {
					process_src(v, false);
					return;
				}
			}
		}
	}

	for (vvec::iterator I = n.src.begin(), E = n.src.end(); I != E; ++I) {
		value *c = (*I);

		if (c->is_kcache() && !kc.try_reserve(c->select)) {
			process_src(v, false);
			return;
		}
	}
	process_src(v, true);
}

void gvn::process_op(node& n, bool rewrite) {

	for(vvec::iterator I = n.src.begin(), E = n.src.end(); I != E; ++I) {
		value* &v = *I;
		if (v) {
			if (v->rel) {
				process_src(v->rel, rewrite);
			}

			if (rewrite && v->gvn_source && v->gvn_source->is_readonly() &&
					n.is_any_alu()) {
				process_alu_src_constants(n, v);
			} else if (rewrite && v->gvn_source && v->gvn_source->is_const() &&
					(n.is_fetch_op(FETCH_OP_VFETCH) ||
							n.is_fetch_op(FETCH_OP_SEMFETCH)))
				process_src(v, false);
			else
				process_src(v, rewrite);
		}
	}
	if (n.pred)
		process_src(n.pred, false);

	if (n.type == NT_IF) {
		if_node &i = (if_node&)n;
		if (i.cond)
			process_src(i.cond, false);
	}

	for(vvec::iterator I = n.dst.begin(), E = n.dst.end(); I != E; ++I) {
		value *v = *I;
		if (v) {
			if (v->rel)
				process_src(v->rel, rewrite);
			sh.vt.add_value(v);
		}
	}
}

} // namespace r600_sb