1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
|
/*
* Copyright © 2010 Intel Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice (including the next
* paragraph) shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*/
/**
* \file ir_structure_splitting.cpp
*
* If a structure is only ever referenced by its components, then
* split those components out to individual variables so they can be
* handled normally by other optimization passes.
*
* This skips structures like uniforms, which need to be accessible as
* structures for their access by the GL.
*/
#include "ir.h"
#include "ir_visitor.h"
#include "ir_print_visitor.h"
#include "ir_rvalue_visitor.h"
#include "glsl_types.h"
static bool debug = false;
class variable_entry : public exec_node
{
public:
variable_entry(ir_variable *var)
{
this->var = var;
this->whole_structure_access = 0;
this->declaration = false;
this->components = NULL;
this->mem_ctx = NULL;
}
ir_variable *var; /* The key: the variable's pointer. */
/** Number of times the variable is referenced, including assignments. */
unsigned whole_structure_access;
bool declaration; /* If the variable had a decl in the instruction stream */
ir_variable **components;
/** talloc_parent(this->var) -- the shader's talloc context. */
void *mem_ctx;
};
class ir_structure_reference_visitor : public ir_hierarchical_visitor {
public:
ir_structure_reference_visitor(void)
{
this->mem_ctx = talloc_new(NULL);
this->variable_list.make_empty();
}
~ir_structure_reference_visitor(void)
{
talloc_free(mem_ctx);
}
virtual ir_visitor_status visit(ir_variable *);
virtual ir_visitor_status visit(ir_dereference_variable *);
virtual ir_visitor_status visit_enter(ir_dereference_record *);
virtual ir_visitor_status visit_enter(ir_assignment *);
virtual ir_visitor_status visit_enter(ir_function_signature *);
variable_entry *get_variable_entry(ir_variable *var);
/* List of variable_entry */
exec_list variable_list;
void *mem_ctx;
};
variable_entry *
ir_structure_reference_visitor::get_variable_entry(ir_variable *var)
{
assert(var);
if (!var->type->is_record() || var->mode == ir_var_uniform)
return NULL;
foreach_iter(exec_list_iterator, iter, this->variable_list) {
variable_entry *entry = (variable_entry *)iter.get();
if (entry->var == var)
return entry;
}
variable_entry *entry = new(mem_ctx) variable_entry(var);
this->variable_list.push_tail(entry);
return entry;
}
ir_visitor_status
ir_structure_reference_visitor::visit(ir_variable *ir)
{
variable_entry *entry = this->get_variable_entry(ir);
if (entry)
entry->declaration = true;
return visit_continue;
}
ir_visitor_status
ir_structure_reference_visitor::visit(ir_dereference_variable *ir)
{
ir_variable *const var = ir->variable_referenced();
variable_entry *entry = this->get_variable_entry(var);
if (entry)
entry->whole_structure_access++;
return visit_continue;
}
ir_visitor_status
ir_structure_reference_visitor::visit_enter(ir_dereference_record *ir)
{
/* Don't descend into the ir_dereference_variable below. */
return visit_continue_with_parent;
}
ir_visitor_status
ir_structure_reference_visitor::visit_enter(ir_assignment *ir)
{
if (ir->lhs->as_dereference_variable() &&
ir->rhs->as_dereference_variable() &&
!ir->condition) {
/* We'll split copies of a structure to copies of components, so don't
* descend to the ir_dereference_variables.
*/
return visit_continue_with_parent;
}
return visit_continue;
}
ir_visitor_status
ir_structure_reference_visitor::visit_enter(ir_function_signature *ir)
{
/* We don't want to descend into the function parameters and
* dead-code eliminate them, so just accept the body here.
*/
visit_list_elements(this, &ir->body);
return visit_continue_with_parent;
}
class ir_structure_splitting_visitor : public ir_rvalue_visitor {
public:
ir_structure_splitting_visitor(exec_list *vars)
{
this->variable_list = vars;
}
virtual ~ir_structure_splitting_visitor()
{
}
virtual ir_visitor_status visit_leave(ir_assignment *);
void split_deref(ir_dereference **deref);
void handle_rvalue(ir_rvalue **rvalue);
struct variable_entry *get_splitting_entry(ir_variable *var);
exec_list *variable_list;
void *mem_ctx;
};
struct variable_entry *
ir_structure_splitting_visitor::get_splitting_entry(ir_variable *var)
{
assert(var);
if (!var->type->is_record())
return NULL;
foreach_iter(exec_list_iterator, iter, *this->variable_list) {
variable_entry *entry = (variable_entry *)iter.get();
if (entry->var == var) {
return entry;
}
}
return NULL;
}
void
ir_structure_splitting_visitor::split_deref(ir_dereference **deref)
{
if ((*deref)->ir_type != ir_type_dereference_record)
return;
ir_dereference_record *deref_record = (ir_dereference_record *)*deref;
ir_dereference_variable *deref_var = deref_record->record->as_dereference_variable();
if (!deref_var)
return;
variable_entry *entry = get_splitting_entry(deref_var->var);
if (!entry)
return;
unsigned int i;
for (i = 0; i < entry->var->type->length; i++) {
if (strcmp(deref_record->field,
entry->var->type->fields.structure[i].name) == 0)
break;
}
assert(i != entry->var->type->length);
*deref = new(entry->mem_ctx) ir_dereference_variable(entry->components[i]);
}
void
ir_structure_splitting_visitor::handle_rvalue(ir_rvalue **rvalue)
{
if (!*rvalue)
return;
ir_dereference *deref = (*rvalue)->as_dereference();
if (!deref)
return;
split_deref(&deref);
*rvalue = deref;
}
ir_visitor_status
ir_structure_splitting_visitor::visit_leave(ir_assignment *ir)
{
ir_dereference_variable *lhs_deref = ir->lhs->as_dereference_variable();
ir_dereference_variable *rhs_deref = ir->rhs->as_dereference_variable();
variable_entry *lhs_entry = lhs_deref ? get_splitting_entry(lhs_deref->var) : NULL;
variable_entry *rhs_entry = rhs_deref ? get_splitting_entry(rhs_deref->var) : NULL;
const glsl_type *type = ir->rhs->type;
if ((lhs_entry || rhs_entry) && !ir->condition) {
for (unsigned int i = 0; i < type->length; i++) {
ir_dereference *new_lhs, *new_rhs;
void *mem_ctx = lhs_entry ? lhs_entry->mem_ctx : rhs_entry->mem_ctx;
if (lhs_entry) {
new_lhs = new(mem_ctx) ir_dereference_variable(lhs_entry->components[i]);
} else {
new_lhs = new(mem_ctx)
ir_dereference_record(ir->lhs->clone(mem_ctx, NULL),
type->fields.structure[i].name);
}
if (rhs_entry) {
new_rhs = new(mem_ctx) ir_dereference_variable(rhs_entry->components[i]);
} else {
new_rhs = new(mem_ctx)
ir_dereference_record(ir->rhs->clone(mem_ctx, NULL),
type->fields.structure[i].name);
}
ir->insert_before(new(mem_ctx) ir_assignment(new_lhs,
new_rhs,
NULL));
}
ir->remove();
} else {
handle_rvalue(&ir->rhs);
split_deref(&ir->lhs);
}
handle_rvalue(&ir->condition);
return visit_continue;
}
bool
do_structure_splitting(exec_list *instructions)
{
ir_structure_reference_visitor refs;
visit_list_elements(&refs, instructions);
/* Trim out variables we can't split. */
foreach_iter(exec_list_iterator, iter, refs.variable_list) {
variable_entry *entry = (variable_entry *)iter.get();
if (debug) {
printf("structure %s@%p: decl %d, whole_access %d\n",
entry->var->name, (void *) entry->var, entry->declaration,
entry->whole_structure_access);
}
if (!entry->declaration || entry->whole_structure_access) {
entry->remove();
}
}
if (refs.variable_list.is_empty())
return false;
void *mem_ctx = talloc_new(NULL);
/* Replace the decls of the structures to be split with their split
* components.
*/
foreach_iter(exec_list_iterator, iter, refs.variable_list) {
variable_entry *entry = (variable_entry *)iter.get();
const struct glsl_type *type = entry->var->type;
entry->mem_ctx = talloc_parent(entry->var);
entry->components = talloc_array(mem_ctx,
ir_variable *,
type->length);
for (unsigned int i = 0; i < entry->var->type->length; i++) {
const char *name = talloc_asprintf(mem_ctx, "%s_%s",
entry->var->name,
type->fields.structure[i].name);
entry->components[i] =
new(entry->mem_ctx) ir_variable(type->fields.structure[i].type,
name,
ir_var_temporary);
entry->var->insert_before(entry->components[i]);
}
entry->var->remove();
}
ir_structure_splitting_visitor split(&refs.variable_list);
visit_list_elements(&split, instructions);
talloc_free(mem_ctx);
return true;
}
|