blob: e257defb1a1d15d5c3e9c4206a881c6b14198b05 [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{
141 /* Don't descend into the ir_dereference_variable below. */
Eric Anholt0a0ab122010-08-05 13:25:39 -0700142 return visit_continue_with_parent;
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700143}
144
145ir_visitor_status
Eric Anholtf8d2cfe2010-08-08 23:29:54 -0700146ir_structure_reference_visitor::visit_enter(ir_assignment *ir)
147{
148 if (ir->lhs->as_dereference_variable() &&
149 ir->rhs->as_dereference_variable() &&
150 !ir->condition) {
151 /* We'll split copies of a structure to copies of components, so don't
152 * descend to the ir_dereference_variables.
153 */
154 return visit_continue_with_parent;
155 }
Aras Pranckevicius5023eda2010-08-09 11:50:24 +0300156 return visit_continue;
Eric Anholtf8d2cfe2010-08-08 23:29:54 -0700157}
158
159ir_visitor_status
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700160ir_structure_reference_visitor::visit_enter(ir_function_signature *ir)
161{
162 /* We don't want to descend into the function parameters and
163 * dead-code eliminate them, so just accept the body here.
164 */
165 visit_list_elements(this, &ir->body);
166 return visit_continue_with_parent;
167}
168
Eric Anholtb3b0cf62010-08-13 20:39:24 -0700169class ir_structure_splitting_visitor : public ir_rvalue_visitor {
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700170public:
171 ir_structure_splitting_visitor(exec_list *vars)
172 {
173 this->variable_list = vars;
174 }
175
176 virtual ~ir_structure_splitting_visitor()
177 {
178 }
179
180 virtual ir_visitor_status visit_leave(ir_assignment *);
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700181
182 void split_deref(ir_dereference **deref);
Eric Anholtb3b0cf62010-08-13 20:39:24 -0700183 void handle_rvalue(ir_rvalue **rvalue);
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700184 struct variable_entry *get_splitting_entry(ir_variable *var);
185
186 exec_list *variable_list;
187 void *mem_ctx;
188};
189
190struct variable_entry *
191ir_structure_splitting_visitor::get_splitting_entry(ir_variable *var)
192{
193 assert(var);
194
195 if (!var->type->is_record())
196 return NULL;
197
198 foreach_iter(exec_list_iterator, iter, *this->variable_list) {
199 variable_entry *entry = (variable_entry *)iter.get();
200 if (entry->var == var) {
201 return entry;
202 }
203 }
204
205 return NULL;
206}
207
208void
209ir_structure_splitting_visitor::split_deref(ir_dereference **deref)
210{
211 if ((*deref)->ir_type != ir_type_dereference_record)
212 return;
213
Eric Anholt0a0ab122010-08-05 13:25:39 -0700214 ir_dereference_record *deref_record = (ir_dereference_record *)*deref;
215 ir_dereference_variable *deref_var = deref_record->record->as_dereference_variable();
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700216 if (!deref_var)
217 return;
218
219 variable_entry *entry = get_splitting_entry(deref_var->var);
Eric Anholt0a0ab122010-08-05 13:25:39 -0700220 if (!entry)
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700221 return;
222
223 unsigned int i;
224 for (i = 0; i < entry->var->type->length; i++) {
225 if (strcmp(deref_record->field,
226 entry->var->type->fields.structure[i].name) == 0)
227 break;
228 }
229 assert(i != entry->var->type->length);
230
231 *deref = new(entry->mem_ctx) ir_dereference_variable(entry->components[i]);
232}
233
234void
Eric Anholtb3b0cf62010-08-13 20:39:24 -0700235ir_structure_splitting_visitor::handle_rvalue(ir_rvalue **rvalue)
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700236{
Eric Anholt0a0ab122010-08-05 13:25:39 -0700237 if (!*rvalue)
238 return;
239
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700240 ir_dereference *deref = (*rvalue)->as_dereference();
241
242 if (!deref)
243 return;
244
245 split_deref(&deref);
246 *rvalue = deref;
247}
248
249ir_visitor_status
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700250ir_structure_splitting_visitor::visit_leave(ir_assignment *ir)
251{
Eric Anholtf8d2cfe2010-08-08 23:29:54 -0700252 ir_dereference_variable *lhs_deref = ir->lhs->as_dereference_variable();
253 ir_dereference_variable *rhs_deref = ir->rhs->as_dereference_variable();
254 variable_entry *lhs_entry = lhs_deref ? get_splitting_entry(lhs_deref->var) : NULL;
255 variable_entry *rhs_entry = rhs_deref ? get_splitting_entry(rhs_deref->var) : NULL;
256 const glsl_type *type = ir->rhs->type;
257
258 if ((lhs_entry || rhs_entry) && !ir->condition) {
259 for (unsigned int i = 0; i < type->length; i++) {
260 ir_dereference *new_lhs, *new_rhs;
261 void *mem_ctx = lhs_entry ? lhs_entry->mem_ctx : rhs_entry->mem_ctx;
262
263 if (lhs_entry) {
264 new_lhs = new(mem_ctx) ir_dereference_variable(lhs_entry->components[i]);
265 } else {
266 new_lhs = new(mem_ctx)
267 ir_dereference_record(ir->lhs->clone(mem_ctx, NULL),
268 type->fields.structure[i].name);
269 }
270
271 if (rhs_entry) {
272 new_rhs = new(mem_ctx) ir_dereference_variable(rhs_entry->components[i]);
273 } else {
274 new_rhs = new(mem_ctx)
275 ir_dereference_record(ir->rhs->clone(mem_ctx, NULL),
276 type->fields.structure[i].name);
277 }
278
279 ir->insert_before(new(mem_ctx) ir_assignment(new_lhs,
280 new_rhs,
281 NULL));
282 }
283 ir->remove();
284 } else {
Eric Anholtb3b0cf62010-08-13 20:39:24 -0700285 handle_rvalue(&ir->rhs);
Eric Anholtf8d2cfe2010-08-08 23:29:54 -0700286 split_deref(&ir->lhs);
287 }
288
Eric Anholtb3b0cf62010-08-13 20:39:24 -0700289 handle_rvalue(&ir->condition);
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700290
291 return visit_continue;
292}
293
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700294bool
295do_structure_splitting(exec_list *instructions)
296{
297 ir_structure_reference_visitor refs;
Eric Anholt0a0ab122010-08-05 13:25:39 -0700298
299 visit_list_elements(&refs, instructions);
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700300
301 /* Trim out variables we can't split. */
302 foreach_iter(exec_list_iterator, iter, refs.variable_list) {
303 variable_entry *entry = (variable_entry *)iter.get();
Eric Anholt0a0ab122010-08-05 13:25:39 -0700304
305 if (debug) {
306 printf("structure %s@%p: decl %d, whole_access %d\n",
Brian Paulffd3f152010-08-11 14:00:15 -0600307 entry->var->name, (void *) entry->var, entry->declaration,
Eric Anholt0a0ab122010-08-05 13:25:39 -0700308 entry->whole_structure_access);
309 }
310
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700311 if (!entry->declaration || entry->whole_structure_access) {
312 entry->remove();
313 }
314 }
315
316 if (refs.variable_list.is_empty())
317 return false;
318
Eric Anholt0a0ab122010-08-05 13:25:39 -0700319 void *mem_ctx = talloc_new(NULL);
320
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700321 /* Replace the decls of the structures to be split with their split
322 * components.
323 */
324 foreach_iter(exec_list_iterator, iter, refs.variable_list) {
325 variable_entry *entry = (variable_entry *)iter.get();
326 const struct glsl_type *type = entry->var->type;
327
328 entry->mem_ctx = talloc_parent(entry->var);
329
330 entry->components = talloc_array(mem_ctx,
331 ir_variable *,
332 type->length);
333
334 for (unsigned int i = 0; i < entry->var->type->length; i++) {
335 const char *name = talloc_asprintf(mem_ctx, "%s_%s",
Eric Anholt0a0ab122010-08-05 13:25:39 -0700336 entry->var->name,
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700337 type->fields.structure[i].name);
338
339 entry->components[i] =
340 new(entry->mem_ctx) ir_variable(type->fields.structure[i].type,
341 name,
342 ir_var_temporary);
343 entry->var->insert_before(entry->components[i]);
344 }
345
346 entry->var->remove();
347 }
348
349 ir_structure_splitting_visitor split(&refs.variable_list);
350 visit_list_elements(&split, instructions);
351
352 talloc_free(mem_ctx);
353
354 return true;
355}