blob: d6191002c2f05d0fccc22ee6126cb11137504e08 [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/**
Chad Versacedf883eb2010-11-17 10:43:10 -080025 * \file opt_structure_splitting.cpp
Eric Anholt7f7eaf02010-08-05 11:01:09 -070026 *
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
Brian Paul81137622010-08-24 10:21:25 -060043// 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?
47class variable_entry2 : public exec_node
Eric Anholt7f7eaf02010-08-05 11:01:09 -070048{
49public:
Brian Paul81137622010-08-24 10:21:25 -060050 variable_entry2(ir_variable *var)
Eric Anholt7f7eaf02010-08-05 11:01:09 -070051 {
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 /** talloc_parent(this->var) -- the shader's talloc context. */
69 void *mem_ctx;
70};
71
Brian Paul81137622010-08-24 10:21:25 -060072
Eric Anholt7f7eaf02010-08-05 11:01:09 -070073class ir_structure_reference_visitor : public ir_hierarchical_visitor {
74public:
75 ir_structure_reference_visitor(void)
76 {
77 this->mem_ctx = talloc_new(NULL);
78 this->variable_list.make_empty();
79 }
80
81 ~ir_structure_reference_visitor(void)
82 {
83 talloc_free(mem_ctx);
84 }
85
86 virtual ir_visitor_status visit(ir_variable *);
87 virtual ir_visitor_status visit(ir_dereference_variable *);
Eric Anholt0a0ab122010-08-05 13:25:39 -070088 virtual ir_visitor_status visit_enter(ir_dereference_record *);
Eric Anholtf8d2cfe2010-08-08 23:29:54 -070089 virtual ir_visitor_status visit_enter(ir_assignment *);
Eric Anholt7f7eaf02010-08-05 11:01:09 -070090 virtual ir_visitor_status visit_enter(ir_function_signature *);
91
Brian Paul81137622010-08-24 10:21:25 -060092 variable_entry2 *get_variable_entry2(ir_variable *var);
Eric Anholt7f7eaf02010-08-05 11:01:09 -070093
94 /* List of variable_entry */
95 exec_list variable_list;
96
97 void *mem_ctx;
98};
99
Brian Paul81137622010-08-24 10:21:25 -0600100variable_entry2 *
101ir_structure_reference_visitor::get_variable_entry2(ir_variable *var)
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700102{
103 assert(var);
104
Eric Anholt0a0ab122010-08-05 13:25:39 -0700105 if (!var->type->is_record() || var->mode == ir_var_uniform)
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700106 return NULL;
107
108 foreach_iter(exec_list_iterator, iter, this->variable_list) {
Brian Paul81137622010-08-24 10:21:25 -0600109 variable_entry2 *entry = (variable_entry2 *)iter.get();
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700110 if (entry->var == var)
111 return entry;
112 }
113
Brian Paul81137622010-08-24 10:21:25 -0600114 variable_entry2 *entry = new(mem_ctx) variable_entry2(var);
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700115 this->variable_list.push_tail(entry);
116 return entry;
117}
118
119
120ir_visitor_status
121ir_structure_reference_visitor::visit(ir_variable *ir)
122{
Brian Paul81137622010-08-24 10:21:25 -0600123 variable_entry2 *entry = this->get_variable_entry2(ir);
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700124
125 if (entry)
126 entry->declaration = true;
127
128 return visit_continue;
129}
130
131ir_visitor_status
132ir_structure_reference_visitor::visit(ir_dereference_variable *ir)
133{
134 ir_variable *const var = ir->variable_referenced();
Brian Paul81137622010-08-24 10:21:25 -0600135 variable_entry2 *entry = this->get_variable_entry2(var);
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700136
137 if (entry)
138 entry->whole_structure_access++;
139
140 return visit_continue;
141}
142
143ir_visitor_status
Eric Anholt0a0ab122010-08-05 13:25:39 -0700144ir_structure_reference_visitor::visit_enter(ir_dereference_record *ir)
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700145{
Ian Romanickd0a9cbd2010-08-16 13:59:01 -0700146 (void) ir;
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700147 /* Don't descend into the ir_dereference_variable below. */
Eric Anholt0a0ab122010-08-05 13:25:39 -0700148 return visit_continue_with_parent;
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700149}
150
151ir_visitor_status
Eric Anholtf8d2cfe2010-08-08 23:29:54 -0700152ir_structure_reference_visitor::visit_enter(ir_assignment *ir)
153{
154 if (ir->lhs->as_dereference_variable() &&
155 ir->rhs->as_dereference_variable() &&
156 !ir->condition) {
157 /* We'll split copies of a structure to copies of components, so don't
158 * descend to the ir_dereference_variables.
159 */
160 return visit_continue_with_parent;
161 }
Aras Pranckevicius5023eda2010-08-09 11:50:24 +0300162 return visit_continue;
Eric Anholtf8d2cfe2010-08-08 23:29:54 -0700163}
164
165ir_visitor_status
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700166ir_structure_reference_visitor::visit_enter(ir_function_signature *ir)
167{
168 /* We don't want to descend into the function parameters and
169 * dead-code eliminate them, so just accept the body here.
170 */
171 visit_list_elements(this, &ir->body);
172 return visit_continue_with_parent;
173}
174
Eric Anholtb3b0cf62010-08-13 20:39:24 -0700175class ir_structure_splitting_visitor : public ir_rvalue_visitor {
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700176public:
177 ir_structure_splitting_visitor(exec_list *vars)
178 {
179 this->variable_list = vars;
180 }
181
182 virtual ~ir_structure_splitting_visitor()
183 {
184 }
185
186 virtual ir_visitor_status visit_leave(ir_assignment *);
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700187
188 void split_deref(ir_dereference **deref);
Eric Anholtb3b0cf62010-08-13 20:39:24 -0700189 void handle_rvalue(ir_rvalue **rvalue);
Brian Paul81137622010-08-24 10:21:25 -0600190 variable_entry2 *get_splitting_entry(ir_variable *var);
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700191
192 exec_list *variable_list;
193 void *mem_ctx;
194};
195
Brian Paul81137622010-08-24 10:21:25 -0600196variable_entry2 *
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700197ir_structure_splitting_visitor::get_splitting_entry(ir_variable *var)
198{
199 assert(var);
200
201 if (!var->type->is_record())
202 return NULL;
203
204 foreach_iter(exec_list_iterator, iter, *this->variable_list) {
Brian Paul81137622010-08-24 10:21:25 -0600205 variable_entry2 *entry = (variable_entry2 *)iter.get();
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700206 if (entry->var == var) {
207 return entry;
208 }
209 }
210
211 return NULL;
212}
213
214void
215ir_structure_splitting_visitor::split_deref(ir_dereference **deref)
216{
217 if ((*deref)->ir_type != ir_type_dereference_record)
218 return;
219
Eric Anholt0a0ab122010-08-05 13:25:39 -0700220 ir_dereference_record *deref_record = (ir_dereference_record *)*deref;
221 ir_dereference_variable *deref_var = deref_record->record->as_dereference_variable();
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700222 if (!deref_var)
223 return;
224
Brian Paul81137622010-08-24 10:21:25 -0600225 variable_entry2 *entry = get_splitting_entry(deref_var->var);
Eric Anholt0a0ab122010-08-05 13:25:39 -0700226 if (!entry)
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700227 return;
228
229 unsigned int i;
230 for (i = 0; i < entry->var->type->length; i++) {
231 if (strcmp(deref_record->field,
232 entry->var->type->fields.structure[i].name) == 0)
233 break;
234 }
235 assert(i != entry->var->type->length);
236
237 *deref = new(entry->mem_ctx) ir_dereference_variable(entry->components[i]);
238}
239
240void
Eric Anholtb3b0cf62010-08-13 20:39:24 -0700241ir_structure_splitting_visitor::handle_rvalue(ir_rvalue **rvalue)
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700242{
Eric Anholt0a0ab122010-08-05 13:25:39 -0700243 if (!*rvalue)
244 return;
245
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700246 ir_dereference *deref = (*rvalue)->as_dereference();
247
248 if (!deref)
249 return;
250
251 split_deref(&deref);
252 *rvalue = deref;
253}
254
255ir_visitor_status
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700256ir_structure_splitting_visitor::visit_leave(ir_assignment *ir)
257{
Eric Anholtf8d2cfe2010-08-08 23:29:54 -0700258 ir_dereference_variable *lhs_deref = ir->lhs->as_dereference_variable();
259 ir_dereference_variable *rhs_deref = ir->rhs->as_dereference_variable();
Brian Paul81137622010-08-24 10:21:25 -0600260 variable_entry2 *lhs_entry = lhs_deref ? get_splitting_entry(lhs_deref->var) : NULL;
261 variable_entry2 *rhs_entry = rhs_deref ? get_splitting_entry(rhs_deref->var) : NULL;
Eric Anholtf8d2cfe2010-08-08 23:29:54 -0700262 const glsl_type *type = ir->rhs->type;
263
264 if ((lhs_entry || rhs_entry) && !ir->condition) {
265 for (unsigned int i = 0; i < type->length; i++) {
266 ir_dereference *new_lhs, *new_rhs;
267 void *mem_ctx = lhs_entry ? lhs_entry->mem_ctx : rhs_entry->mem_ctx;
268
269 if (lhs_entry) {
270 new_lhs = new(mem_ctx) ir_dereference_variable(lhs_entry->components[i]);
271 } else {
272 new_lhs = new(mem_ctx)
273 ir_dereference_record(ir->lhs->clone(mem_ctx, NULL),
274 type->fields.structure[i].name);
275 }
276
277 if (rhs_entry) {
278 new_rhs = new(mem_ctx) ir_dereference_variable(rhs_entry->components[i]);
279 } else {
280 new_rhs = new(mem_ctx)
281 ir_dereference_record(ir->rhs->clone(mem_ctx, NULL),
282 type->fields.structure[i].name);
283 }
284
285 ir->insert_before(new(mem_ctx) ir_assignment(new_lhs,
286 new_rhs,
287 NULL));
288 }
289 ir->remove();
290 } else {
Eric Anholtb3b0cf62010-08-13 20:39:24 -0700291 handle_rvalue(&ir->rhs);
Eric Anholtf8d2cfe2010-08-08 23:29:54 -0700292 split_deref(&ir->lhs);
293 }
294
Eric Anholtb3b0cf62010-08-13 20:39:24 -0700295 handle_rvalue(&ir->condition);
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700296
297 return visit_continue;
298}
299
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700300bool
301do_structure_splitting(exec_list *instructions)
302{
303 ir_structure_reference_visitor refs;
Eric Anholt0a0ab122010-08-05 13:25:39 -0700304
305 visit_list_elements(&refs, instructions);
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700306
307 /* Trim out variables we can't split. */
308 foreach_iter(exec_list_iterator, iter, refs.variable_list) {
Brian Paul81137622010-08-24 10:21:25 -0600309 variable_entry2 *entry = (variable_entry2 *)iter.get();
Eric Anholt0a0ab122010-08-05 13:25:39 -0700310
311 if (debug) {
312 printf("structure %s@%p: decl %d, whole_access %d\n",
Brian Paulffd3f152010-08-11 14:00:15 -0600313 entry->var->name, (void *) entry->var, entry->declaration,
Eric Anholt0a0ab122010-08-05 13:25:39 -0700314 entry->whole_structure_access);
315 }
316
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700317 if (!entry->declaration || entry->whole_structure_access) {
318 entry->remove();
319 }
320 }
321
322 if (refs.variable_list.is_empty())
323 return false;
324
Eric Anholt0a0ab122010-08-05 13:25:39 -0700325 void *mem_ctx = talloc_new(NULL);
326
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700327 /* Replace the decls of the structures to be split with their split
328 * components.
329 */
330 foreach_iter(exec_list_iterator, iter, refs.variable_list) {
Brian Paul81137622010-08-24 10:21:25 -0600331 variable_entry2 *entry = (variable_entry2 *)iter.get();
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700332 const struct glsl_type *type = entry->var->type;
333
334 entry->mem_ctx = talloc_parent(entry->var);
335
336 entry->components = talloc_array(mem_ctx,
337 ir_variable *,
338 type->length);
339
340 for (unsigned int i = 0; i < entry->var->type->length; i++) {
341 const char *name = talloc_asprintf(mem_ctx, "%s_%s",
Eric Anholt0a0ab122010-08-05 13:25:39 -0700342 entry->var->name,
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700343 type->fields.structure[i].name);
344
345 entry->components[i] =
346 new(entry->mem_ctx) ir_variable(type->fields.structure[i].type,
347 name,
348 ir_var_temporary);
349 entry->var->insert_before(entry->components[i]);
350 }
351
352 entry->var->remove();
353 }
354
355 ir_structure_splitting_visitor split(&refs.variable_list);
356 visit_list_elements(&split, instructions);
357
358 talloc_free(mem_ctx);
359
360 return true;
361}