blob: 91f6dac23e0c50e83ecc467cba97f1372858c093 [file] [log] [blame]
Eric Anholt7f7eaf02010-08-05 11:01:09 -07001/*
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 ir_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"
Eric Anholt0a0ab122010-08-05 13:25:39 -070037#include "ir_print_visitor.h"
Eric Anholtb3b0cf62010-08-13 20:39:24 -070038#include "ir_rvalue_visitor.h"
Eric Anholt7f7eaf02010-08-05 11:01:09 -070039#include "glsl_types.h"
40
Eric Anholt0a0ab122010-08-05 13:25:39 -070041static bool debug = false;
42
Eric Anholt7f7eaf02010-08-05 11:01:09 -070043class variable_entry : public exec_node
44{
45public:
46 variable_entry(ir_variable *var)
47 {
48 this->var = var;
49 this->whole_structure_access = 0;
50 this->declaration = false;
51 this->components = NULL;
52 this->mem_ctx = NULL;
53 }
54
55 ir_variable *var; /* The key: the variable's pointer. */
56
57 /** Number of times the variable is referenced, including assignments. */
58 unsigned whole_structure_access;
59
60 bool declaration; /* If the variable had a decl in the instruction stream */
61
62 ir_variable **components;
63
64 /** talloc_parent(this->var) -- the shader's talloc context. */
65 void *mem_ctx;
66};
67
68class ir_structure_reference_visitor : public ir_hierarchical_visitor {
69public:
70 ir_structure_reference_visitor(void)
71 {
72 this->mem_ctx = talloc_new(NULL);
73 this->variable_list.make_empty();
74 }
75
76 ~ir_structure_reference_visitor(void)
77 {
78 talloc_free(mem_ctx);
79 }
80
81 virtual ir_visitor_status visit(ir_variable *);
82 virtual ir_visitor_status visit(ir_dereference_variable *);
Eric Anholt0a0ab122010-08-05 13:25:39 -070083 virtual ir_visitor_status visit_enter(ir_dereference_record *);
Eric Anholtf8d2cfe2010-08-08 23:29:54 -070084 virtual ir_visitor_status visit_enter(ir_assignment *);
Eric Anholt7f7eaf02010-08-05 11:01:09 -070085 virtual ir_visitor_status visit_enter(ir_function_signature *);
86
87 variable_entry *get_variable_entry(ir_variable *var);
88
89 /* List of variable_entry */
90 exec_list variable_list;
91
92 void *mem_ctx;
93};
94
95variable_entry *
96ir_structure_reference_visitor::get_variable_entry(ir_variable *var)
97{
98 assert(var);
99
Eric Anholt0a0ab122010-08-05 13:25:39 -0700100 if (!var->type->is_record() || var->mode == ir_var_uniform)
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700101 return NULL;
102
103 foreach_iter(exec_list_iterator, iter, this->variable_list) {
104 variable_entry *entry = (variable_entry *)iter.get();
105 if (entry->var == var)
106 return entry;
107 }
108
109 variable_entry *entry = new(mem_ctx) variable_entry(var);
110 this->variable_list.push_tail(entry);
111 return entry;
112}
113
114
115ir_visitor_status
116ir_structure_reference_visitor::visit(ir_variable *ir)
117{
118 variable_entry *entry = this->get_variable_entry(ir);
119
120 if (entry)
121 entry->declaration = true;
122
123 return visit_continue;
124}
125
126ir_visitor_status
127ir_structure_reference_visitor::visit(ir_dereference_variable *ir)
128{
129 ir_variable *const var = ir->variable_referenced();
130 variable_entry *entry = this->get_variable_entry(var);
131
132 if (entry)
133 entry->whole_structure_access++;
134
135 return visit_continue;
136}
137
138ir_visitor_status
Eric Anholt0a0ab122010-08-05 13:25:39 -0700139ir_structure_reference_visitor::visit_enter(ir_dereference_record *ir)
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700140{
Ian Romanickd0a9cbd2010-08-16 13:59:01 -0700141 (void) ir;
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700142 /* Don't descend into the ir_dereference_variable below. */
Eric Anholt0a0ab122010-08-05 13:25:39 -0700143 return visit_continue_with_parent;
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700144}
145
146ir_visitor_status
Eric Anholtf8d2cfe2010-08-08 23:29:54 -0700147ir_structure_reference_visitor::visit_enter(ir_assignment *ir)
148{
149 if (ir->lhs->as_dereference_variable() &&
150 ir->rhs->as_dereference_variable() &&
151 !ir->condition) {
152 /* We'll split copies of a structure to copies of components, so don't
153 * descend to the ir_dereference_variables.
154 */
155 return visit_continue_with_parent;
156 }
Aras Pranckevicius5023eda2010-08-09 11:50:24 +0300157 return visit_continue;
Eric Anholtf8d2cfe2010-08-08 23:29:54 -0700158}
159
160ir_visitor_status
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700161ir_structure_reference_visitor::visit_enter(ir_function_signature *ir)
162{
163 /* We don't want to descend into the function parameters and
164 * dead-code eliminate them, so just accept the body here.
165 */
166 visit_list_elements(this, &ir->body);
167 return visit_continue_with_parent;
168}
169
Eric Anholtb3b0cf62010-08-13 20:39:24 -0700170class ir_structure_splitting_visitor : public ir_rvalue_visitor {
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700171public:
172 ir_structure_splitting_visitor(exec_list *vars)
173 {
174 this->variable_list = vars;
175 }
176
177 virtual ~ir_structure_splitting_visitor()
178 {
179 }
180
181 virtual ir_visitor_status visit_leave(ir_assignment *);
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700182
183 void split_deref(ir_dereference **deref);
Eric Anholtb3b0cf62010-08-13 20:39:24 -0700184 void handle_rvalue(ir_rvalue **rvalue);
José Fonseca1cbcf662010-08-14 15:35:57 +0100185 variable_entry *get_splitting_entry(ir_variable *var);
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700186
187 exec_list *variable_list;
188 void *mem_ctx;
189};
190
José Fonseca1cbcf662010-08-14 15:35:57 +0100191variable_entry *
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700192ir_structure_splitting_visitor::get_splitting_entry(ir_variable *var)
193{
194 assert(var);
195
196 if (!var->type->is_record())
197 return NULL;
198
199 foreach_iter(exec_list_iterator, iter, *this->variable_list) {
200 variable_entry *entry = (variable_entry *)iter.get();
201 if (entry->var == var) {
202 return entry;
203 }
204 }
205
206 return NULL;
207}
208
209void
210ir_structure_splitting_visitor::split_deref(ir_dereference **deref)
211{
212 if ((*deref)->ir_type != ir_type_dereference_record)
213 return;
214
Eric Anholt0a0ab122010-08-05 13:25:39 -0700215 ir_dereference_record *deref_record = (ir_dereference_record *)*deref;
216 ir_dereference_variable *deref_var = deref_record->record->as_dereference_variable();
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700217 if (!deref_var)
218 return;
219
220 variable_entry *entry = get_splitting_entry(deref_var->var);
Eric Anholt0a0ab122010-08-05 13:25:39 -0700221 if (!entry)
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700222 return;
223
224 unsigned int i;
225 for (i = 0; i < entry->var->type->length; i++) {
226 if (strcmp(deref_record->field,
227 entry->var->type->fields.structure[i].name) == 0)
228 break;
229 }
230 assert(i != entry->var->type->length);
231
232 *deref = new(entry->mem_ctx) ir_dereference_variable(entry->components[i]);
233}
234
235void
Eric Anholtb3b0cf62010-08-13 20:39:24 -0700236ir_structure_splitting_visitor::handle_rvalue(ir_rvalue **rvalue)
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700237{
Eric Anholt0a0ab122010-08-05 13:25:39 -0700238 if (!*rvalue)
239 return;
240
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700241 ir_dereference *deref = (*rvalue)->as_dereference();
242
243 if (!deref)
244 return;
245
246 split_deref(&deref);
247 *rvalue = deref;
248}
249
250ir_visitor_status
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700251ir_structure_splitting_visitor::visit_leave(ir_assignment *ir)
252{
Eric Anholtf8d2cfe2010-08-08 23:29:54 -0700253 ir_dereference_variable *lhs_deref = ir->lhs->as_dereference_variable();
254 ir_dereference_variable *rhs_deref = ir->rhs->as_dereference_variable();
255 variable_entry *lhs_entry = lhs_deref ? get_splitting_entry(lhs_deref->var) : NULL;
256 variable_entry *rhs_entry = rhs_deref ? get_splitting_entry(rhs_deref->var) : NULL;
257 const glsl_type *type = ir->rhs->type;
258
259 if ((lhs_entry || rhs_entry) && !ir->condition) {
260 for (unsigned int i = 0; i < type->length; i++) {
261 ir_dereference *new_lhs, *new_rhs;
262 void *mem_ctx = lhs_entry ? lhs_entry->mem_ctx : rhs_entry->mem_ctx;
263
264 if (lhs_entry) {
265 new_lhs = new(mem_ctx) ir_dereference_variable(lhs_entry->components[i]);
266 } else {
267 new_lhs = new(mem_ctx)
268 ir_dereference_record(ir->lhs->clone(mem_ctx, NULL),
269 type->fields.structure[i].name);
270 }
271
272 if (rhs_entry) {
273 new_rhs = new(mem_ctx) ir_dereference_variable(rhs_entry->components[i]);
274 } else {
275 new_rhs = new(mem_ctx)
276 ir_dereference_record(ir->rhs->clone(mem_ctx, NULL),
277 type->fields.structure[i].name);
278 }
279
280 ir->insert_before(new(mem_ctx) ir_assignment(new_lhs,
281 new_rhs,
282 NULL));
283 }
284 ir->remove();
285 } else {
Eric Anholtb3b0cf62010-08-13 20:39:24 -0700286 handle_rvalue(&ir->rhs);
Eric Anholtf8d2cfe2010-08-08 23:29:54 -0700287 split_deref(&ir->lhs);
288 }
289
Eric Anholtb3b0cf62010-08-13 20:39:24 -0700290 handle_rvalue(&ir->condition);
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700291
292 return visit_continue;
293}
294
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700295bool
296do_structure_splitting(exec_list *instructions)
297{
298 ir_structure_reference_visitor refs;
Eric Anholt0a0ab122010-08-05 13:25:39 -0700299
300 visit_list_elements(&refs, instructions);
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700301
302 /* Trim out variables we can't split. */
303 foreach_iter(exec_list_iterator, iter, refs.variable_list) {
304 variable_entry *entry = (variable_entry *)iter.get();
Eric Anholt0a0ab122010-08-05 13:25:39 -0700305
306 if (debug) {
307 printf("structure %s@%p: decl %d, whole_access %d\n",
Brian Paulffd3f152010-08-11 14:00:15 -0600308 entry->var->name, (void *) entry->var, entry->declaration,
Eric Anholt0a0ab122010-08-05 13:25:39 -0700309 entry->whole_structure_access);
310 }
311
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700312 if (!entry->declaration || entry->whole_structure_access) {
313 entry->remove();
314 }
315 }
316
317 if (refs.variable_list.is_empty())
318 return false;
319
Eric Anholt0a0ab122010-08-05 13:25:39 -0700320 void *mem_ctx = talloc_new(NULL);
321
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700322 /* Replace the decls of the structures to be split with their split
323 * components.
324 */
325 foreach_iter(exec_list_iterator, iter, refs.variable_list) {
326 variable_entry *entry = (variable_entry *)iter.get();
327 const struct glsl_type *type = entry->var->type;
328
329 entry->mem_ctx = talloc_parent(entry->var);
330
331 entry->components = talloc_array(mem_ctx,
332 ir_variable *,
333 type->length);
334
335 for (unsigned int i = 0; i < entry->var->type->length; i++) {
336 const char *name = talloc_asprintf(mem_ctx, "%s_%s",
Eric Anholt0a0ab122010-08-05 13:25:39 -0700337 entry->var->name,
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700338 type->fields.structure[i].name);
339
340 entry->components[i] =
341 new(entry->mem_ctx) ir_variable(type->fields.structure[i].type,
342 name,
343 ir_var_temporary);
344 entry->var->insert_before(entry->components[i]);
345 }
346
347 entry->var->remove();
348 }
349
350 ir_structure_splitting_visitor split(&refs.variable_list);
351 visit_list_elements(&split, instructions);
352
353 talloc_free(mem_ctx);
354
355 return true;
356}