87eafaee2476f9f939619479c4a06584423e00c7
[mesa.git] / src / gallium / drivers / r600 / sb / sb_ssa_builder.cpp
1 /*
2 * Copyright 2013 Vadim Girlin <vadimgirlin@gmail.com>
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * on the rights to use, copy, modify, merge, publish, distribute, sub
8 * license, and/or sell copies of the Software, and to permit persons to whom
9 * the Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
21 * USE OR OTHER DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Vadim Girlin
25 */
26
27 #include <stack>
28 #include <map>
29
30 #include "sb_shader.h"
31 #include "sb_pass.h"
32
33 namespace r600_sb {
34
35 container_node* ssa_prepare::create_phi_nodes(int count) {
36 container_node *p = sh.create_container();
37 val_set &vars = cur_set();
38 node *nn;
39
40 for (val_set::iterator I = vars.begin(sh), E = vars.end(sh); I != E; ++I) {
41 nn = sh.create_node(NT_OP, NST_PHI);
42 nn->dst.assign(1, *I);
43 nn->src.assign(count, *I);
44 p->push_back(nn);
45 }
46 return p;
47 }
48
49 void ssa_prepare::add_defs(node &n) {
50 val_set &s = cur_set();
51 for (vvec::iterator I = n.dst.begin(), E = n.dst.end(); I != E; ++I) {
52 value *v = *I;
53 if (!v)
54 continue;
55
56 if (v->is_rel()) {
57 s.add_vec(v->mdef);
58 } else
59 s.add_val(v);
60 }
61 }
62
63 bool ssa_prepare::visit(cf_node& n, bool enter) {
64 if (enter) {
65 push_stk();
66 } else {
67 add_defs(n);
68 pop_stk();
69 }
70 return true;
71 }
72
73 bool ssa_prepare::visit(alu_node& n, bool enter) {
74 if (enter) {
75 } else {
76 add_defs(n);
77 }
78 return true;
79 }
80
81 bool ssa_prepare::visit(fetch_node& n, bool enter) {
82 if (enter) {
83 } else {
84 add_defs(n);
85 }
86 return true;
87 }
88
89 bool ssa_prepare::visit(region_node& n, bool enter) {
90 if (enter) {
91
92 push_stk();
93 } else {
94 cur_set().add_set(n.vars_defined);
95 if (n.dep_count() > 0)
96 n.phi = create_phi_nodes(n.dep_count());
97 if (n.rep_count() > 1) {
98 n.loop_phi = create_phi_nodes(n.rep_count());
99 n.loop_phi->subtype = NST_LOOP_PHI_CONTAINER;
100 }
101 n.vars_defined.clear();
102 pop_stk();
103 }
104 return true;
105 }
106
107 bool ssa_prepare::visit(repeat_node& n, bool enter) {
108 if (enter) {
109 push_stk();
110 } else {
111 assert(n.target);
112 n.target->vars_defined.add_set(cur_set());
113 cur_set().clear();
114 pop_stk();
115 }
116 return true;
117 }
118
119 bool ssa_prepare::visit(depart_node& n, bool enter) {
120 if (enter) {
121 push_stk();
122 } else {
123 assert(n.target);
124 n.target->vars_defined.add_set(cur_set());
125 cur_set().clear();
126 pop_stk();
127 }
128 return true;
129 }
130
131 // ===============================
132
133 int ssa_rename::init() {
134 rename_stack.push(def_map());
135 return 0;
136 }
137
138 bool ssa_rename::visit(alu_group_node& n, bool enter) {
139 // taking into account parallel execution of the alu group
140 if (enter) {
141 for (node_iterator I = n.begin(), E = n.end(); I != E; ++I) {
142 I->accept(*this, true);
143 }
144 } else {
145 for (node_iterator I = n.begin(), E = n.end(); I != E; ++I) {
146 I->accept(*this, false);
147 }
148 }
149 return false;
150 }
151
152 bool ssa_rename::visit(cf_node& n, bool enter) {
153 if (enter) {
154 rename_src(&n);
155 } else {
156 rename_dst(&n);
157 }
158 return true;
159 }
160
161 bool ssa_rename::visit(alu_node& n, bool enter) {
162 if (enter) {
163 rename_src(&n);
164 } else {
165
166 node *psi = NULL;
167
168 if (n.pred && n.dst[0]) {
169
170 value *d = n.dst[0];
171 unsigned index = get_index(rename_stack.top(), d);
172 value *p = sh.get_value_version(d, index);
173
174 psi = sh.create_node(NT_OP, NST_PSI);
175
176 container_node *parent;
177 if (n.parent->subtype == NST_ALU_GROUP)
178 parent = n.parent;
179 else {
180 assert (n.parent->parent->subtype == NST_ALU_GROUP);
181 parent = n.parent->parent;
182 }
183 parent->insert_after(psi);
184
185 assert(n.bc.pred_sel);
186
187 psi->src.resize(6);
188 psi->src[2] = p;
189 psi->src[3] = n.pred;
190 psi->src[4] = sh.get_pred_sel(n.bc.pred_sel - PRED_SEL_0);
191 psi->src[5] = d;
192 psi->dst.push_back(d);
193 }
194
195 rename_dst(&n);
196
197 if (psi) {
198 rename_src(psi);
199 rename_dst(psi);
200 }
201
202 if (!n.dst.empty() && n.dst[0]) {
203 // FIXME probably use separate pass for such things
204 if ((n.bc.op_ptr->flags & AF_INTERP) || n.bc.op == ALU_OP2_CUBE)
205 n.dst[0]->flags |= VLF_PIN_CHAN;
206 }
207 }
208 return true;
209 }
210
211 bool ssa_rename::visit(alu_packed_node& n, bool enter) {
212 if (enter) {
213 for (node_iterator I = n.begin(), E = n.end(); I != E; ++I) {
214 I->accept(*this, true);
215 }
216 } else {
217 for (node_iterator I = n.begin(), E = n.end(); I != E; ++I) {
218 I->accept(*this, false);
219 }
220
221 n.init_args();
222 }
223 return false;
224 }
225
226 bool ssa_rename::visit(fetch_node& n, bool enter) {
227 if (enter) {
228 rename_src(&n);
229 rename_dst(&n);
230 } else {
231 }
232 return true;
233 }
234
235 bool ssa_rename::visit(region_node& n, bool enter) {
236 if (enter) {
237 if (n.loop_phi)
238 rename_phi_args(n.loop_phi, 0, true);
239 } else {
240 if (n.phi)
241 rename_phi_args(n.phi, ~0u, true);
242 }
243 return true;
244 }
245
246 bool ssa_rename::visit(repeat_node& n, bool enter) {
247 if (enter) {
248 push(n.target->loop_phi);
249 } else {
250 if (n.target->loop_phi)
251 rename_phi_args(n.target->loop_phi, n.rep_id, false);
252 pop();
253 }
254 return true;
255 }
256
257 bool ssa_rename::visit(depart_node& n, bool enter) {
258 if (enter) {
259 push(n.target->phi);
260 } else {
261 if (n.target->phi)
262 rename_phi_args(n.target->phi, n.dep_id, false);
263 pop();
264 }
265 return true;
266 }
267
268 bool ssa_rename::visit(if_node& n, bool enter) {
269 if (enter) {
270 } else {
271 n.cond = rename_use(&n, n.cond);
272 }
273 return true;
274 }
275
276 void ssa_rename::push(node* phi) {
277 rename_stack.push(rename_stack.top());
278 }
279
280 void ssa_rename::pop() {
281 rename_stack.pop();
282 }
283
284 value* ssa_rename::rename_use(node *n, value* v) {
285 if (v->version)
286 return v;
287
288 unsigned index = get_index(rename_stack.top(), v);
289 v = sh.get_value_version(v, index);
290
291 // if (alu) instruction is predicated and source arg comes from psi node
292 // (that is, from another predicated instruction through its psi node),
293 // we can try to select the corresponding source value directly
294 if (n->pred && v->def && v->def->subtype == NST_PSI) {
295 assert(n->subtype == NST_ALU_INST);
296 alu_node *an = static_cast<alu_node*>(n);
297 node *pn = v->def;
298 // FIXME make it more generic ???
299 if (pn->src.size() == 6) {
300 if (pn->src[3] == n->pred) {
301 value* ps = sh.get_pred_sel(an->bc.pred_sel - PRED_SEL_0);
302 if (pn->src[4] == ps)
303 return pn->src[5];
304 else
305 return pn->src[2];
306 }
307 }
308 }
309 return v;
310 }
311
312 value* ssa_rename::rename_def(node *n, value* v) {
313 unsigned index = new_index(def_count, v);
314 set_index(rename_stack.top(), v, index);
315 value *r = sh.get_value_version(v, index);
316 return r;
317 }
318
319 void ssa_rename::rename_src_vec(node *n, vvec &vv, bool src) {
320 for(vvec::iterator I = vv.begin(), E = vv.end(); I != E; ++I) {
321 value* &v = *I;
322 if (!v || v->is_readonly())
323 continue;
324
325 if (v->is_rel()) {
326 if (!v->rel->is_readonly())
327 v->rel = rename_use(n, v->rel);
328 rename_src_vec(n, v->muse, true);
329 } else if (src)
330 v = rename_use(n, v);
331 }
332 }
333
334 void ssa_rename::rename_src(node* n) {
335 if (n->pred)
336 n->pred = rename_use(n, n->pred);
337
338 rename_src_vec(n, n->src, true);
339 rename_src_vec(n, n->dst, false);
340
341 }
342
343 void ssa_rename::rename_dst_vec(node *n, vvec &vv, bool set_def) {
344
345 for(vvec::iterator I = vv.begin(), E = vv.end(); I != E; ++I) {
346 value* &v = *I;
347 if (!v)
348 continue;
349
350 if (v->is_rel()) {
351 rename_dst_vec(n, v->mdef, false);
352 } else {
353 v = rename_def(n, v);
354 if (set_def)
355 v->def = n;
356 }
357 }
358 }
359
360 void ssa_rename::rename_dst(node* n) {
361 rename_dst_vec(n, n->dst, true);
362 }
363
364 unsigned ssa_rename::get_index(def_map& m, value* v) {
365 def_map::iterator I = m.find(v);
366 if (I != m.end())
367 return I->second;
368 return 0;
369 }
370
371 void ssa_rename::set_index(def_map& m, value* v, unsigned index) {
372 std::pair<def_map::iterator,bool> r = m.insert(std::make_pair(v, index));
373 if (!r.second)
374 r.first->second = index;
375 }
376
377 unsigned ssa_rename::new_index(def_map& m, value* v) {
378 unsigned index = 1;
379 def_map::iterator I = m.find(v);
380 if (I != m.end())
381 index = ++I->second;
382 else
383 m.insert(std::make_pair(v, index));
384 return index;
385 }
386
387 bool ssa_rename::visit(node& n, bool enter) {
388 if (enter) {
389 assert(n.subtype == NST_PSI);
390 rename_src(&n);
391 rename_dst(&n);
392 }
393 return false;
394 }
395
396 bool ssa_rename::visit(container_node& n, bool enter) {
397 if (enter) {
398 } else {
399 // should be root container node
400 assert(n.parent == NULL);
401 rename_src_vec(&n, n.src, true);
402 }
403 return true;
404 }
405
406 void ssa_rename::rename_phi_args(container_node* phi, unsigned op, bool def) {
407 for (node_iterator I = phi->begin(), E = phi->end(); I != E; ++I) {
408 node *o = *I;
409 if (op != ~0u)
410 o->src[op] = rename_use(o, o->src[op]);
411 if (def) {
412 o->dst[0] = rename_def(o, o->dst[0]);
413 o->dst[0]->def = o;
414 }
415 }
416 }
417
418 } // namespace r600_sb