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