i965: Drop pointless check for variable declarations in splitting.
[mesa.git] / src / mesa / drivers / dri / i965 / brw_fs_vector_splitting.cpp
1 /*
2 * Copyright © 2010 Intel Corporation
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 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * 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 NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 */
23
24 /**
25 * \file brw_wm_vector_splitting.cpp
26 *
27 * If a vector is only ever referenced by its components, then
28 * split those components out to individual variables so they can be
29 * handled normally by other optimization passes.
30 *
31 * This skips vectors in uniforms and varyings, which need to be
32 * accessible as vectors for their access by the GL. Also, vector
33 * results of non-variable-derefs in assignments aren't handled
34 * because to do so we would have to store the vector result to a
35 * temporary in order to unload each channel, and to do so would just
36 * loop us back to where we started. For the 965, this is exactly the
37 * behavior we want for the results of texture lookups, but probably not for
38 */
39
40 extern "C" {
41 #include "main/core.h"
42 #include "brw_context.h"
43 }
44 #include "glsl/ir.h"
45 #include "glsl/ir_visitor.h"
46 #include "glsl/ir_rvalue_visitor.h"
47 #include "glsl/glsl_types.h"
48
49 static bool debug = false;
50
51 class variable_entry : public exec_node
52 {
53 public:
54 variable_entry(ir_variable *var)
55 {
56 this->var = var;
57 this->whole_vector_access = 0;
58 this->mem_ctx = NULL;
59 }
60
61 ir_variable *var; /* The key: the variable's pointer. */
62
63 /** Number of times the variable is referenced, including assignments. */
64 unsigned whole_vector_access;
65
66 ir_variable *components[4];
67
68 /** ralloc_parent(this->var) -- the shader's ralloc context. */
69 void *mem_ctx;
70 };
71
72 class ir_vector_reference_visitor : public ir_hierarchical_visitor {
73 public:
74 ir_vector_reference_visitor(void)
75 {
76 this->mem_ctx = ralloc_context(NULL);
77 this->variable_list.make_empty();
78 }
79
80 ~ir_vector_reference_visitor(void)
81 {
82 ralloc_free(mem_ctx);
83 }
84
85 virtual ir_visitor_status visit(ir_variable *);
86 virtual ir_visitor_status visit(ir_dereference_variable *);
87 virtual ir_visitor_status visit_enter(ir_swizzle *);
88 virtual ir_visitor_status visit_enter(ir_assignment *);
89 virtual ir_visitor_status visit_enter(ir_function_signature *);
90
91 variable_entry *get_variable_entry(ir_variable *var);
92
93 /* List of variable_entry */
94 exec_list variable_list;
95
96 void *mem_ctx;
97 };
98
99 variable_entry *
100 ir_vector_reference_visitor::get_variable_entry(ir_variable *var)
101 {
102 assert(var);
103
104 if (!var->type->is_vector())
105 return NULL;
106
107 switch (var->data.mode) {
108 case ir_var_uniform:
109 case ir_var_shader_in:
110 case ir_var_shader_out:
111 case ir_var_system_value:
112 case ir_var_function_in:
113 case ir_var_function_out:
114 case ir_var_function_inout:
115 /* Can't split varyings or uniforms. Function in/outs won't get split
116 * either.
117 */
118 return NULL;
119 case ir_var_auto:
120 case ir_var_temporary:
121 break;
122 }
123
124 foreach_list(node, &this->variable_list) {
125 variable_entry *entry = (variable_entry *)node;
126 if (entry->var == var)
127 return entry;
128 }
129
130 variable_entry *entry = new(mem_ctx) variable_entry(var);
131 this->variable_list.push_tail(entry);
132 return entry;
133 }
134
135
136 ir_visitor_status
137 ir_vector_reference_visitor::visit(ir_variable *ir)
138 {
139 /* Make sure splitting looks at splitting this variable */
140 (void)this->get_variable_entry(ir);
141
142 return visit_continue;
143 }
144
145 ir_visitor_status
146 ir_vector_reference_visitor::visit(ir_dereference_variable *ir)
147 {
148 ir_variable *const var = ir->var;
149 variable_entry *entry = this->get_variable_entry(var);
150
151 if (entry)
152 entry->whole_vector_access++;
153
154 return visit_continue;
155 }
156
157 ir_visitor_status
158 ir_vector_reference_visitor::visit_enter(ir_swizzle *ir)
159 {
160 /* Don't descend into a vector ir_dereference_variable below. */
161 if (ir->val->as_dereference_variable() && ir->type->is_scalar())
162 return visit_continue_with_parent;
163
164 return visit_continue;
165 }
166
167 ir_visitor_status
168 ir_vector_reference_visitor::visit_enter(ir_assignment *ir)
169 {
170 if (ir->lhs->as_dereference_variable() &&
171 ir->rhs->as_dereference_variable() &&
172 !ir->condition) {
173 /* We'll split copies of a vector to copies of channels, so don't
174 * descend to the ir_dereference_variables.
175 */
176 return visit_continue_with_parent;
177 }
178 if (ir->lhs->as_dereference_variable() &&
179 is_power_of_two(ir->write_mask) &&
180 !ir->condition) {
181 /* If we're writing just a channel, then channel-splitting the LHS is OK.
182 */
183 ir->rhs->accept(this);
184 return visit_continue_with_parent;
185 }
186 return visit_continue;
187 }
188
189 ir_visitor_status
190 ir_vector_reference_visitor::visit_enter(ir_function_signature *ir)
191 {
192 /* We don't want to descend into the function parameters and
193 * split them, so just accept the body here.
194 */
195 visit_list_elements(this, &ir->body);
196 return visit_continue_with_parent;
197 }
198
199 class ir_vector_splitting_visitor : public ir_rvalue_visitor {
200 public:
201 ir_vector_splitting_visitor(exec_list *vars)
202 {
203 this->variable_list = vars;
204 }
205
206 virtual ir_visitor_status visit_leave(ir_assignment *);
207
208 void handle_rvalue(ir_rvalue **rvalue);
209 variable_entry *get_splitting_entry(ir_variable *var);
210
211 exec_list *variable_list;
212 };
213
214 variable_entry *
215 ir_vector_splitting_visitor::get_splitting_entry(ir_variable *var)
216 {
217 assert(var);
218
219 if (!var->type->is_vector())
220 return NULL;
221
222 foreach_list(node, &*this->variable_list) {
223 variable_entry *entry = (variable_entry *)node;
224 if (entry->var == var) {
225 return entry;
226 }
227 }
228
229 return NULL;
230 }
231
232 void
233 ir_vector_splitting_visitor::handle_rvalue(ir_rvalue **rvalue)
234 {
235 if (!*rvalue)
236 return;
237
238 ir_swizzle *swiz = (*rvalue)->as_swizzle();
239 if (!swiz || !swiz->type->is_scalar())
240 return;
241
242 ir_dereference_variable *deref_var = swiz->val->as_dereference_variable();
243 if (!deref_var)
244 return;
245
246 variable_entry *entry = get_splitting_entry(deref_var->var);
247 if (!entry)
248 return;
249
250 ir_variable *var = entry->components[swiz->mask.x];
251 *rvalue = new(entry->mem_ctx) ir_dereference_variable(var);
252 }
253
254 ir_visitor_status
255 ir_vector_splitting_visitor::visit_leave(ir_assignment *ir)
256 {
257 ir_dereference_variable *lhs_deref = ir->lhs->as_dereference_variable();
258 ir_dereference_variable *rhs_deref = ir->rhs->as_dereference_variable();
259 variable_entry *lhs = lhs_deref ? get_splitting_entry(lhs_deref->var) : NULL;
260 variable_entry *rhs = rhs_deref ? get_splitting_entry(rhs_deref->var) : NULL;
261
262 if (lhs_deref && rhs_deref && (lhs || rhs) && !ir->condition) {
263 unsigned int rhs_chan = 0;
264
265 /* Straight assignment of vector variables. */
266 for (unsigned int i = 0; i < ir->lhs->type->vector_elements; i++) {
267 ir_dereference *new_lhs;
268 ir_rvalue *new_rhs;
269 void *mem_ctx = lhs ? lhs->mem_ctx : rhs->mem_ctx;
270 unsigned int writemask;
271
272 if (!(ir->write_mask & (1 << i)))
273 continue;
274
275 if (lhs) {
276 new_lhs = new(mem_ctx) ir_dereference_variable(lhs->components[i]);
277 writemask = 1;
278 } else {
279 new_lhs = ir->lhs->clone(mem_ctx, NULL);
280 writemask = 1 << i;
281 }
282
283 if (rhs) {
284 new_rhs =
285 new(mem_ctx) ir_dereference_variable(rhs->components[rhs_chan]);
286 } else {
287 new_rhs = new(mem_ctx) ir_swizzle(ir->rhs->clone(mem_ctx, NULL),
288 rhs_chan, 0, 0, 0, 1);
289 }
290
291 ir->insert_before(new(mem_ctx) ir_assignment(new_lhs,
292 new_rhs,
293 NULL, writemask));
294
295 rhs_chan++;
296 }
297 ir->remove();
298 } else if (lhs) {
299 void *mem_ctx = lhs->mem_ctx;
300 int elem = -1;
301
302 switch (ir->write_mask) {
303 case (1 << 0):
304 elem = 0;
305 break;
306 case (1 << 1):
307 elem = 1;
308 break;
309 case (1 << 2):
310 elem = 2;
311 break;
312 case (1 << 3):
313 elem = 3;
314 break;
315 default:
316 ir->fprint(stderr);
317 assert(!"not reached: non-channelwise dereference of LHS.");
318 }
319
320 ir->lhs = new(mem_ctx) ir_dereference_variable(lhs->components[elem]);
321 ir->write_mask = (1 << 0);
322
323 handle_rvalue(&ir->rhs);
324 } else {
325 handle_rvalue(&ir->rhs);
326 }
327
328 handle_rvalue(&ir->condition);
329
330 return visit_continue;
331 }
332
333 bool
334 brw_do_vector_splitting(exec_list *instructions)
335 {
336 ir_vector_reference_visitor refs;
337
338 visit_list_elements(&refs, instructions);
339
340 /* Trim out variables we can't split. */
341 foreach_list_safe(node, &refs.variable_list) {
342 variable_entry *entry = (variable_entry *)node;
343
344 if (debug) {
345 fprintf(stderr, "vector %s@%p: whole_access %d\n",
346 entry->var->name, (void *) entry->var,
347 entry->whole_vector_access);
348 }
349
350 if (entry->whole_vector_access) {
351 entry->remove();
352 }
353 }
354
355 if (refs.variable_list.is_empty())
356 return false;
357
358 void *mem_ctx = ralloc_context(NULL);
359
360 /* Replace the decls of the vectors to be split with their split
361 * components.
362 */
363 foreach_list(node, &refs.variable_list) {
364 variable_entry *entry = (variable_entry *)node;
365 const struct glsl_type *type;
366 type = glsl_type::get_instance(entry->var->type->base_type, 1, 1);
367
368 entry->mem_ctx = ralloc_parent(entry->var);
369
370 for (unsigned int i = 0; i < entry->var->type->vector_elements; i++) {
371 const char *name = ralloc_asprintf(mem_ctx, "%s_%c",
372 entry->var->name,
373 "xyzw"[i]);
374
375 entry->components[i] = new(entry->mem_ctx) ir_variable(type, name,
376 ir_var_temporary);
377 entry->var->insert_before(entry->components[i]);
378 }
379
380 entry->var->remove();
381 }
382
383 ir_vector_splitting_visitor split(&refs.variable_list);
384 visit_list_elements(&split, instructions);
385
386 ralloc_free(mem_ctx);
387
388 return true;
389 }