glsl: Disable structure splitting for shader ins/outs.
[mesa.git] / src / glsl / opt_structure_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 opt_structure_splitting.cpp
26 *
27 * If a structure 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 structures like uniforms, which need to be accessible as
32 * structures for their access by the GL.
33 */
34
35 #include "ir.h"
36 #include "ir_visitor.h"
37 #include "ir_print_visitor.h"
38 #include "ir_rvalue_visitor.h"
39 #include "glsl_types.h"
40
41 namespace {
42
43 static bool debug = false;
44
45 class variable_entry : public exec_node
46 {
47 public:
48 variable_entry(ir_variable *var)
49 {
50 this->var = var;
51 this->whole_structure_access = 0;
52 this->declaration = false;
53 this->components = NULL;
54 this->mem_ctx = NULL;
55 }
56
57 ir_variable *var; /* The key: the variable's pointer. */
58
59 /** Number of times the variable is referenced, including assignments. */
60 unsigned whole_structure_access;
61
62 /* If the variable had a decl we can work with in the instruction
63 * stream. We can't do splitting on function arguments, which
64 * don't get this variable set.
65 */
66 bool declaration;
67
68 ir_variable **components;
69
70 /** ralloc_parent(this->var) -- the shader's ralloc context. */
71 void *mem_ctx;
72 };
73
74
75 class ir_structure_reference_visitor : public ir_hierarchical_visitor {
76 public:
77 ir_structure_reference_visitor(void)
78 {
79 this->mem_ctx = ralloc_context(NULL);
80 this->variable_list.make_empty();
81 }
82
83 ~ir_structure_reference_visitor(void)
84 {
85 ralloc_free(mem_ctx);
86 }
87
88 virtual ir_visitor_status visit(ir_variable *);
89 virtual ir_visitor_status visit(ir_dereference_variable *);
90 virtual ir_visitor_status visit_enter(ir_dereference_record *);
91 virtual ir_visitor_status visit_enter(ir_assignment *);
92 virtual ir_visitor_status visit_enter(ir_function_signature *);
93
94 variable_entry *get_variable_entry(ir_variable *var);
95
96 /* List of variable_entry */
97 exec_list variable_list;
98
99 void *mem_ctx;
100 };
101
102 variable_entry *
103 ir_structure_reference_visitor::get_variable_entry(ir_variable *var)
104 {
105 assert(var);
106
107 if (!var->type->is_record() || var->mode == ir_var_uniform
108 || var->mode == ir_var_shader_in || var->mode == ir_var_shader_out)
109 return NULL;
110
111 foreach_iter(exec_list_iterator, iter, this->variable_list) {
112 variable_entry *entry = (variable_entry *)iter.get();
113 if (entry->var == var)
114 return entry;
115 }
116
117 variable_entry *entry = new(mem_ctx) variable_entry(var);
118 this->variable_list.push_tail(entry);
119 return entry;
120 }
121
122
123 ir_visitor_status
124 ir_structure_reference_visitor::visit(ir_variable *ir)
125 {
126 variable_entry *entry = this->get_variable_entry(ir);
127
128 if (entry)
129 entry->declaration = true;
130
131 return visit_continue;
132 }
133
134 ir_visitor_status
135 ir_structure_reference_visitor::visit(ir_dereference_variable *ir)
136 {
137 ir_variable *const var = ir->variable_referenced();
138 variable_entry *entry = this->get_variable_entry(var);
139
140 if (entry)
141 entry->whole_structure_access++;
142
143 return visit_continue;
144 }
145
146 ir_visitor_status
147 ir_structure_reference_visitor::visit_enter(ir_dereference_record *ir)
148 {
149 (void) ir;
150 /* Don't descend into the ir_dereference_variable below. */
151 return visit_continue_with_parent;
152 }
153
154 ir_visitor_status
155 ir_structure_reference_visitor::visit_enter(ir_assignment *ir)
156 {
157 /* If there are no structure references yet, no need to bother with
158 * processing the expression tree.
159 */
160 if (this->variable_list.is_empty())
161 return visit_continue_with_parent;
162
163 if (ir->lhs->as_dereference_variable() &&
164 ir->rhs->as_dereference_variable() &&
165 !ir->condition) {
166 /* We'll split copies of a structure to copies of components, so don't
167 * descend to the ir_dereference_variables.
168 */
169 return visit_continue_with_parent;
170 }
171 return visit_continue;
172 }
173
174 ir_visitor_status
175 ir_structure_reference_visitor::visit_enter(ir_function_signature *ir)
176 {
177 /* We don't have logic for structure-splitting function arguments,
178 * so just look at the body instructions and not the parameter
179 * declarations.
180 */
181 visit_list_elements(this, &ir->body);
182 return visit_continue_with_parent;
183 }
184
185 class ir_structure_splitting_visitor : public ir_rvalue_visitor {
186 public:
187 ir_structure_splitting_visitor(exec_list *vars)
188 {
189 this->variable_list = vars;
190 }
191
192 virtual ~ir_structure_splitting_visitor()
193 {
194 }
195
196 virtual ir_visitor_status visit_leave(ir_assignment *);
197
198 void split_deref(ir_dereference **deref);
199 void handle_rvalue(ir_rvalue **rvalue);
200 variable_entry *get_splitting_entry(ir_variable *var);
201
202 exec_list *variable_list;
203 };
204
205 variable_entry *
206 ir_structure_splitting_visitor::get_splitting_entry(ir_variable *var)
207 {
208 assert(var);
209
210 if (!var->type->is_record())
211 return NULL;
212
213 foreach_iter(exec_list_iterator, iter, *this->variable_list) {
214 variable_entry *entry = (variable_entry *)iter.get();
215 if (entry->var == var) {
216 return entry;
217 }
218 }
219
220 return NULL;
221 }
222
223 void
224 ir_structure_splitting_visitor::split_deref(ir_dereference **deref)
225 {
226 if ((*deref)->ir_type != ir_type_dereference_record)
227 return;
228
229 ir_dereference_record *deref_record = (ir_dereference_record *)*deref;
230 ir_dereference_variable *deref_var = deref_record->record->as_dereference_variable();
231 if (!deref_var)
232 return;
233
234 variable_entry *entry = get_splitting_entry(deref_var->var);
235 if (!entry)
236 return;
237
238 unsigned int i;
239 for (i = 0; i < entry->var->type->length; i++) {
240 if (strcmp(deref_record->field,
241 entry->var->type->fields.structure[i].name) == 0)
242 break;
243 }
244 assert(i != entry->var->type->length);
245
246 *deref = new(entry->mem_ctx) ir_dereference_variable(entry->components[i]);
247 }
248
249 void
250 ir_structure_splitting_visitor::handle_rvalue(ir_rvalue **rvalue)
251 {
252 if (!*rvalue)
253 return;
254
255 ir_dereference *deref = (*rvalue)->as_dereference();
256
257 if (!deref)
258 return;
259
260 split_deref(&deref);
261 *rvalue = deref;
262 }
263
264 ir_visitor_status
265 ir_structure_splitting_visitor::visit_leave(ir_assignment *ir)
266 {
267 ir_dereference_variable *lhs_deref = ir->lhs->as_dereference_variable();
268 ir_dereference_variable *rhs_deref = ir->rhs->as_dereference_variable();
269 variable_entry *lhs_entry = lhs_deref ? get_splitting_entry(lhs_deref->var) : NULL;
270 variable_entry *rhs_entry = rhs_deref ? get_splitting_entry(rhs_deref->var) : NULL;
271 const glsl_type *type = ir->rhs->type;
272
273 if ((lhs_entry || rhs_entry) && !ir->condition) {
274 for (unsigned int i = 0; i < type->length; i++) {
275 ir_dereference *new_lhs, *new_rhs;
276 void *mem_ctx = lhs_entry ? lhs_entry->mem_ctx : rhs_entry->mem_ctx;
277
278 if (lhs_entry) {
279 new_lhs = new(mem_ctx) ir_dereference_variable(lhs_entry->components[i]);
280 } else {
281 new_lhs = new(mem_ctx)
282 ir_dereference_record(ir->lhs->clone(mem_ctx, NULL),
283 type->fields.structure[i].name);
284 }
285
286 if (rhs_entry) {
287 new_rhs = new(mem_ctx) ir_dereference_variable(rhs_entry->components[i]);
288 } else {
289 new_rhs = new(mem_ctx)
290 ir_dereference_record(ir->rhs->clone(mem_ctx, NULL),
291 type->fields.structure[i].name);
292 }
293
294 ir->insert_before(new(mem_ctx) ir_assignment(new_lhs,
295 new_rhs,
296 NULL));
297 }
298 ir->remove();
299 } else {
300 handle_rvalue(&ir->rhs);
301 split_deref(&ir->lhs);
302 }
303
304 handle_rvalue(&ir->condition);
305
306 return visit_continue;
307 }
308
309 } /* unnamed namespace */
310
311 bool
312 do_structure_splitting(exec_list *instructions)
313 {
314 ir_structure_reference_visitor refs;
315
316 visit_list_elements(&refs, instructions);
317
318 /* Trim out variables we can't split. */
319 foreach_iter(exec_list_iterator, iter, refs.variable_list) {
320 variable_entry *entry = (variable_entry *)iter.get();
321
322 if (debug) {
323 printf("structure %s@%p: decl %d, whole_access %d\n",
324 entry->var->name, (void *) entry->var, entry->declaration,
325 entry->whole_structure_access);
326 }
327
328 if (!entry->declaration || entry->whole_structure_access) {
329 entry->remove();
330 }
331 }
332
333 if (refs.variable_list.is_empty())
334 return false;
335
336 void *mem_ctx = ralloc_context(NULL);
337
338 /* Replace the decls of the structures to be split with their split
339 * components.
340 */
341 foreach_iter(exec_list_iterator, iter, refs.variable_list) {
342 variable_entry *entry = (variable_entry *)iter.get();
343 const struct glsl_type *type = entry->var->type;
344
345 entry->mem_ctx = ralloc_parent(entry->var);
346
347 entry->components = ralloc_array(mem_ctx,
348 ir_variable *,
349 type->length);
350
351 for (unsigned int i = 0; i < entry->var->type->length; i++) {
352 const char *name = ralloc_asprintf(mem_ctx, "%s_%s",
353 entry->var->name,
354 type->fields.structure[i].name);
355
356 entry->components[i] =
357 new(entry->mem_ctx) ir_variable(type->fields.structure[i].type,
358 name,
359 ir_var_temporary);
360 entry->var->insert_before(entry->components[i]);
361 }
362
363 entry->var->remove();
364 }
365
366 ir_structure_splitting_visitor split(&refs.variable_list);
367 visit_list_elements(&split, instructions);
368
369 ralloc_free(mem_ctx);
370
371 return true;
372 }