blob: 9dc5941207380d6a4f9ac7ee30f9caafe644ab9f [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 Anholt7f7eaf02010-08-05 11:01:09 -070038#include "glsl_types.h"
39
Eric Anholt0a0ab122010-08-05 13:25:39 -070040static bool debug = false;
41
Eric Anholt7f7eaf02010-08-05 11:01:09 -070042class variable_entry : public exec_node
43{
44public:
45 variable_entry(ir_variable *var)
46 {
47 this->var = var;
48 this->whole_structure_access = 0;
49 this->declaration = false;
50 this->components = NULL;
51 this->mem_ctx = NULL;
52 }
53
54 ir_variable *var; /* The key: the variable's pointer. */
55
56 /** Number of times the variable is referenced, including assignments. */
57 unsigned whole_structure_access;
58
59 bool declaration; /* If the variable had a decl in the instruction stream */
60
61 ir_variable **components;
62
63 /** talloc_parent(this->var) -- the shader's talloc context. */
64 void *mem_ctx;
65};
66
67class ir_structure_reference_visitor : public ir_hierarchical_visitor {
68public:
69 ir_structure_reference_visitor(void)
70 {
71 this->mem_ctx = talloc_new(NULL);
72 this->variable_list.make_empty();
73 }
74
75 ~ir_structure_reference_visitor(void)
76 {
77 talloc_free(mem_ctx);
78 }
79
80 virtual ir_visitor_status visit(ir_variable *);
81 virtual ir_visitor_status visit(ir_dereference_variable *);
Eric Anholt0a0ab122010-08-05 13:25:39 -070082 virtual ir_visitor_status visit_enter(ir_dereference_record *);
Eric Anholtf8d2cfe2010-08-08 23:29:54 -070083 virtual ir_visitor_status visit_enter(ir_assignment *);
Eric Anholt7f7eaf02010-08-05 11:01:09 -070084 virtual ir_visitor_status visit_enter(ir_function_signature *);
85
86 variable_entry *get_variable_entry(ir_variable *var);
87
88 /* List of variable_entry */
89 exec_list variable_list;
90
91 void *mem_ctx;
92};
93
94variable_entry *
95ir_structure_reference_visitor::get_variable_entry(ir_variable *var)
96{
97 assert(var);
98
Eric Anholt0a0ab122010-08-05 13:25:39 -070099 if (!var->type->is_record() || var->mode == ir_var_uniform)
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700100 return NULL;
101
102 foreach_iter(exec_list_iterator, iter, this->variable_list) {
103 variable_entry *entry = (variable_entry *)iter.get();
104 if (entry->var == var)
105 return entry;
106 }
107
108 variable_entry *entry = new(mem_ctx) variable_entry(var);
109 this->variable_list.push_tail(entry);
110 return entry;
111}
112
113
114ir_visitor_status
115ir_structure_reference_visitor::visit(ir_variable *ir)
116{
117 variable_entry *entry = this->get_variable_entry(ir);
118
119 if (entry)
120 entry->declaration = true;
121
122 return visit_continue;
123}
124
125ir_visitor_status
126ir_structure_reference_visitor::visit(ir_dereference_variable *ir)
127{
128 ir_variable *const var = ir->variable_referenced();
129 variable_entry *entry = this->get_variable_entry(var);
130
131 if (entry)
132 entry->whole_structure_access++;
133
134 return visit_continue;
135}
136
137ir_visitor_status
Eric Anholt0a0ab122010-08-05 13:25:39 -0700138ir_structure_reference_visitor::visit_enter(ir_dereference_record *ir)
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700139{
140 /* Don't descend into the ir_dereference_variable below. */
Eric Anholt0a0ab122010-08-05 13:25:39 -0700141 return visit_continue_with_parent;
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700142}
143
144ir_visitor_status
Eric Anholtf8d2cfe2010-08-08 23:29:54 -0700145ir_structure_reference_visitor::visit_enter(ir_assignment *ir)
146{
147 if (ir->lhs->as_dereference_variable() &&
148 ir->rhs->as_dereference_variable() &&
149 !ir->condition) {
150 /* We'll split copies of a structure to copies of components, so don't
151 * descend to the ir_dereference_variables.
152 */
153 return visit_continue_with_parent;
154 }
Aras Pranckevicius5023eda2010-08-09 11:50:24 +0300155 return visit_continue;
Eric Anholtf8d2cfe2010-08-08 23:29:54 -0700156}
157
158ir_visitor_status
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700159ir_structure_reference_visitor::visit_enter(ir_function_signature *ir)
160{
161 /* We don't want to descend into the function parameters and
162 * dead-code eliminate them, so just accept the body here.
163 */
164 visit_list_elements(this, &ir->body);
165 return visit_continue_with_parent;
166}
167
168class ir_structure_splitting_visitor : public ir_hierarchical_visitor {
169public:
170 ir_structure_splitting_visitor(exec_list *vars)
171 {
172 this->variable_list = vars;
173 }
174
175 virtual ~ir_structure_splitting_visitor()
176 {
177 }
178
179 virtual ir_visitor_status visit_leave(ir_assignment *);
180 virtual ir_visitor_status visit_leave(ir_call *);
181 virtual ir_visitor_status visit_leave(ir_dereference_array *);
Eric Anholt0a0ab122010-08-05 13:25:39 -0700182 virtual ir_visitor_status visit_leave(ir_dereference_record *);
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700183 virtual ir_visitor_status visit_leave(ir_expression *);
184 virtual ir_visitor_status visit_leave(ir_if *);
185 virtual ir_visitor_status visit_leave(ir_return *);
186 virtual ir_visitor_status visit_leave(ir_swizzle *);
187 virtual ir_visitor_status visit_leave(ir_texture *);
188
189 void split_deref(ir_dereference **deref);
190 void split_rvalue(ir_rvalue **rvalue);
191 struct variable_entry *get_splitting_entry(ir_variable *var);
192
193 exec_list *variable_list;
194 void *mem_ctx;
195};
196
197struct variable_entry *
198ir_structure_splitting_visitor::get_splitting_entry(ir_variable *var)
199{
200 assert(var);
201
202 if (!var->type->is_record())
203 return NULL;
204
205 foreach_iter(exec_list_iterator, iter, *this->variable_list) {
206 variable_entry *entry = (variable_entry *)iter.get();
207 if (entry->var == var) {
208 return entry;
209 }
210 }
211
212 return NULL;
213}
214
215void
216ir_structure_splitting_visitor::split_deref(ir_dereference **deref)
217{
218 if ((*deref)->ir_type != ir_type_dereference_record)
219 return;
220
Eric Anholt0a0ab122010-08-05 13:25:39 -0700221 ir_dereference_record *deref_record = (ir_dereference_record *)*deref;
222 ir_dereference_variable *deref_var = deref_record->record->as_dereference_variable();
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700223 if (!deref_var)
224 return;
225
226 variable_entry *entry = get_splitting_entry(deref_var->var);
Eric Anholt0a0ab122010-08-05 13:25:39 -0700227 if (!entry)
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700228 return;
229
230 unsigned int i;
231 for (i = 0; i < entry->var->type->length; i++) {
232 if (strcmp(deref_record->field,
233 entry->var->type->fields.structure[i].name) == 0)
234 break;
235 }
236 assert(i != entry->var->type->length);
237
238 *deref = new(entry->mem_ctx) ir_dereference_variable(entry->components[i]);
239}
240
241void
242ir_structure_splitting_visitor::split_rvalue(ir_rvalue **rvalue)
243{
Eric Anholt0a0ab122010-08-05 13:25:39 -0700244 if (!*rvalue)
245 return;
246
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700247 ir_dereference *deref = (*rvalue)->as_dereference();
248
249 if (!deref)
250 return;
251
252 split_deref(&deref);
253 *rvalue = deref;
254}
255
256ir_visitor_status
257ir_structure_splitting_visitor::visit_leave(ir_expression *ir)
258{
259 unsigned int operand;
260
261 for (operand = 0; operand < ir->get_num_operands(); operand++) {
262 split_rvalue(&ir->operands[operand]);
263 }
264
265 return visit_continue;
266}
267
268ir_visitor_status
269ir_structure_splitting_visitor::visit_leave(ir_texture *ir)
270{
271 split_rvalue(&ir->coordinate);
272 split_rvalue(&ir->projector);
273 split_rvalue(&ir->shadow_comparitor);
274
275 switch (ir->op) {
276 case ir_tex:
277 break;
278 case ir_txb:
279 split_rvalue(&ir->lod_info.bias);
280 break;
281 case ir_txf:
282 case ir_txl:
283 split_rvalue(&ir->lod_info.lod);
284 break;
285 case ir_txd:
286 split_rvalue(&ir->lod_info.grad.dPdx);
287 split_rvalue(&ir->lod_info.grad.dPdy);
288 break;
289 }
290
291 return visit_continue;
292}
293
294ir_visitor_status
295ir_structure_splitting_visitor::visit_leave(ir_swizzle *ir)
296{
297 split_rvalue(&ir->val);
298 return visit_continue;
299}
300
301ir_visitor_status
302ir_structure_splitting_visitor::visit_leave(ir_dereference_array *ir)
303{
304 split_rvalue(&ir->array_index);
Eric Anholt0a0ab122010-08-05 13:25:39 -0700305 split_rvalue(&ir->array);
306 return visit_continue;
307}
308
309ir_visitor_status
310ir_structure_splitting_visitor::visit_leave(ir_dereference_record *ir)
311{
312 split_rvalue(&ir->record);
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700313 return visit_continue;
314}
315
316ir_visitor_status
317ir_structure_splitting_visitor::visit_leave(ir_assignment *ir)
318{
Eric Anholtf8d2cfe2010-08-08 23:29:54 -0700319 ir_dereference_variable *lhs_deref = ir->lhs->as_dereference_variable();
320 ir_dereference_variable *rhs_deref = ir->rhs->as_dereference_variable();
321 variable_entry *lhs_entry = lhs_deref ? get_splitting_entry(lhs_deref->var) : NULL;
322 variable_entry *rhs_entry = rhs_deref ? get_splitting_entry(rhs_deref->var) : NULL;
323 const glsl_type *type = ir->rhs->type;
324
325 if ((lhs_entry || rhs_entry) && !ir->condition) {
326 for (unsigned int i = 0; i < type->length; i++) {
327 ir_dereference *new_lhs, *new_rhs;
328 void *mem_ctx = lhs_entry ? lhs_entry->mem_ctx : rhs_entry->mem_ctx;
329
330 if (lhs_entry) {
331 new_lhs = new(mem_ctx) ir_dereference_variable(lhs_entry->components[i]);
332 } else {
333 new_lhs = new(mem_ctx)
334 ir_dereference_record(ir->lhs->clone(mem_ctx, NULL),
335 type->fields.structure[i].name);
336 }
337
338 if (rhs_entry) {
339 new_rhs = new(mem_ctx) ir_dereference_variable(rhs_entry->components[i]);
340 } else {
341 new_rhs = new(mem_ctx)
342 ir_dereference_record(ir->rhs->clone(mem_ctx, NULL),
343 type->fields.structure[i].name);
344 }
345
346 ir->insert_before(new(mem_ctx) ir_assignment(new_lhs,
347 new_rhs,
348 NULL));
349 }
350 ir->remove();
351 } else {
352 split_rvalue(&ir->rhs);
353 split_deref(&ir->lhs);
354 }
355
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700356 split_rvalue(&ir->condition);
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700357
358 return visit_continue;
359}
360
361ir_visitor_status
362ir_structure_splitting_visitor::visit_leave(ir_call *ir)
363{
364 foreach_iter(exec_list_iterator, iter, *ir) {
365 ir_rvalue *param = (ir_rvalue *)iter.get();
366 ir_rvalue *new_param = param;
367 split_rvalue(&new_param);
368
369 if (new_param != param) {
370 param->replace_with(new_param);
371 }
372 }
373 return visit_continue;
374}
375
376ir_visitor_status
377ir_structure_splitting_visitor::visit_leave(ir_return *ir)
378{
379 split_rvalue(&ir->value);;
380 return visit_continue;
381}
382
383ir_visitor_status
384ir_structure_splitting_visitor::visit_leave(ir_if *ir)
385{
386 split_rvalue(&ir->condition);
387 return visit_continue;
388}
389
390
391bool
392do_structure_splitting(exec_list *instructions)
393{
394 ir_structure_reference_visitor refs;
Eric Anholt0a0ab122010-08-05 13:25:39 -0700395
396 visit_list_elements(&refs, instructions);
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700397
398 /* Trim out variables we can't split. */
399 foreach_iter(exec_list_iterator, iter, refs.variable_list) {
400 variable_entry *entry = (variable_entry *)iter.get();
Eric Anholt0a0ab122010-08-05 13:25:39 -0700401
402 if (debug) {
403 printf("structure %s@%p: decl %d, whole_access %d\n",
404 entry->var->name, entry->var, entry->declaration,
405 entry->whole_structure_access);
406 }
407
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700408 if (!entry->declaration || entry->whole_structure_access) {
409 entry->remove();
410 }
411 }
412
413 if (refs.variable_list.is_empty())
414 return false;
415
Eric Anholt0a0ab122010-08-05 13:25:39 -0700416 void *mem_ctx = talloc_new(NULL);
417
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700418 /* Replace the decls of the structures to be split with their split
419 * components.
420 */
421 foreach_iter(exec_list_iterator, iter, refs.variable_list) {
422 variable_entry *entry = (variable_entry *)iter.get();
423 const struct glsl_type *type = entry->var->type;
424
425 entry->mem_ctx = talloc_parent(entry->var);
426
427 entry->components = talloc_array(mem_ctx,
428 ir_variable *,
429 type->length);
430
431 for (unsigned int i = 0; i < entry->var->type->length; i++) {
432 const char *name = talloc_asprintf(mem_ctx, "%s_%s",
Eric Anholt0a0ab122010-08-05 13:25:39 -0700433 entry->var->name,
Eric Anholt7f7eaf02010-08-05 11:01:09 -0700434 type->fields.structure[i].name);
435
436 entry->components[i] =
437 new(entry->mem_ctx) ir_variable(type->fields.structure[i].type,
438 name,
439 ir_var_temporary);
440 entry->var->insert_before(entry->components[i]);
441 }
442
443 entry->var->remove();
444 }
445
446 ir_structure_splitting_visitor split(&refs.variable_list);
447 visit_list_elements(&split, instructions);
448
449 talloc_free(mem_ctx);
450
451 return true;
452}