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
|
/*
* 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 IFC_DEBUG 0
#if IFC_DEBUG
#define IFC_DUMP(q) do { q } while (0)
#else
#define IFC_DUMP(q)
#endif
#include "sb_shader.h"
#include "sb_pass.h"
namespace r600_sb {
int if_conversion::run() {
regions_vec &rv = sh.get_regions();
unsigned converted = 0;
for (regions_vec::reverse_iterator N, I = rv.rbegin(), E = rv.rend();
I != E; I = N) {
N = I; ++N;
region_node *r = *I;
if (run_on(r)) {
rv.erase(I.base() - 1);
++converted;
}
}
return 0;
}
unsigned if_conversion::try_convert_kills(region_node* r) {
// handling the simplest (and probably most frequent) case only -
// if - 4 kills - endif
// TODO handle more complex cases
depart_node *d1 = static_cast<depart_node*>(r->front());
if (!d1->is_depart())
return 0;
if_node *f = static_cast<if_node*>(d1->front());
if (!f->is_if())
return 0;
depart_node *d2 = static_cast<depart_node*>(f->front());
if (!d2->is_depart())
return 0;
unsigned cnt = 0;
for (node_iterator I = d2->begin(), E = d2->end(); I != E; ++I) {
alu_node *n = static_cast<alu_node*>(*I);
if (!n->is_alu_inst())
return 0;
if (!(n->bc.op_ptr->flags & AF_KILL))
return 0;
if (n->bc.op_ptr->src_count != 2 || n->src.size() != 2)
return 0;
value *s1 = n->src[0], *s2 = n->src[1];
// assuming that the KILL with constant operands is "always kill"
if (!s1 || !s2 || !s1->is_const() || !s2->is_const())
return 0;
++cnt;
}
if (cnt > 4)
return 0;
value *cond = f->cond;
value *pred = get_select_value_for_em(sh, cond);
if (!pred)
return 0;
for (node_iterator N, I = d2->begin(), E = d2->end(); I != E; I = N) {
N = I; ++N;
alu_node *n = static_cast<alu_node*>(*I);
IFC_DUMP(
sblog << "converting ";
dump::dump_op(n);
sblog << " " << n << "\n";
);
n->remove();
n->bc.set_op(ALU_OP2_KILLE_INT);
n->src[0] = pred;
n->src[1] = sh.get_const_value(0);
// reset src modifiers
memset(&n->bc.src[0], 0, sizeof(bc_alu_src));
memset(&n->bc.src[1], 0, sizeof(bc_alu_src));
r->insert_before(n);
}
return cnt;
}
bool if_conversion::run_on(region_node* r) {
if (r->dep_count() != 2 || r->rep_count() != 1)
return false;
node_stats s;
r->collect_stats(s);
IFC_DUMP(
sblog << "ifcvt: region " << r->region_id << " :\n";
s.dump();
);
if (s.region_count || s.fetch_count ||
s.if_count != 1 || s.repeat_count)
return false;
unsigned real_alu_count = s.alu_count - s.alu_copy_mov_count;
// if_conversion allows to eliminate JUMP-ALU_POP_AFTER or
// JUMP-ALU-ELSE-ALU_POP_AFTER, for now let's assume that 3 CF instructions
// are eliminated. According to the docs, cost of CF instruction is
// equal to ~40 ALU VLIW instructions (instruction groups),
// so we have eliminated cost equal to ~120 groups in total.
// Let's also assume that we have avg 3 ALU instructions per group,
// This means that potential eliminated cost is about 360 single alu inst.
// On the other hand, we are speculatively executing conditional code now,
// so we are increasing the cost in some cases. In the worst case, we'll
// have to execute real_alu_count additional alu instructions instead of
// jumping over them. Let's assume for now that average added cost is
//
// (0.9 * real_alu_count)
//
// So we should perform if_conversion if
//
// (0.9 * real_alu_count) < 360, or
//
// real_alu_count < 400
//
// So if real_alu_count is more than 400, than we think that if_conversion
// doesn't make sense.
// FIXME: We can use more precise heuristic, taking into account sizes of
// the branches and their probability instead of total size.
// Another way to improve this is to consider the number of the groups
// instead of the number of instructions (taking into account actual VLIW
// packing).
// (Currently we don't know anything about packing at this stage, but
// probably we can make some more precise estimations anyway)
if (real_alu_count > 400)
return false;
if (s.alu_kill_count) {
unsigned kcnt = try_convert_kills(r);
if (kcnt < s.alu_kill_count)
return false;
}
IFC_DUMP( sblog << "if_cvt: processing...\n"; );
depart_node *nd1 = static_cast<depart_node*>(r->first);
if (!nd1->is_depart())
return false;
if_node *nif = static_cast<if_node*>(nd1->first);
if (!nif->is_if())
return false;
depart_node *nd2 = static_cast<depart_node*>(nif->first);
if (!nd2->is_depart())
return false;
value *em = nif->cond;
value *select = get_select_value_for_em(sh, em);
if (!select)
return false;
for (node_iterator I = r->phi->begin(), E = r->phi->end(); I != E; ++I) {
node *n = *I;
alu_node *ns = convert_phi(select, n);
if (ns)
r->insert_after(ns);
}
nd2->expand();
nif->expand();
nd1->expand();
r->expand();
return true;
}
alu_node* if_conversion::convert_phi(value* select, node* phi) {
assert(phi->dst.size() == 1 || phi->src.size() == 2);
value *d = phi->dst[0];
value *v1 = phi->src[0];
value *v2 = phi->src[1];
assert(d);
if (!d->is_any_gpr())
return NULL;
if (v1->is_undef()) {
if (v2->is_undef()) {
return NULL;
} else {
return sh.create_mov(d, v2);
}
} else if (v2->is_undef())
return sh.create_mov(d, v1);
alu_node* n = sh.create_alu();
n->bc.set_op(ALU_OP3_CNDE_INT);
n->dst.push_back(d);
n->src.push_back(select);
n->src.push_back(v1);
n->src.push_back(v2);
return n;
}
} // namespace r600_sb
|