ir_to_mesa: Support texture rectangle targets
[mesa/nouveau-pmpeg.git] / src / glsl / ir_structure_splitting.cpp
blobc0244071ee2a738626667cc668d7f9ca4cb7b040
1 /*
2 * Copyright © 2010 Intel Corporation
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:
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.
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.
24 /**
25 * \file ir_structure_splitting.cpp
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.
31 * This skips structures like uniforms, which need to be accessible as
32 * structures for their access by the GL.
35 #include "ir.h"
36 #include "ir_visitor.h"
37 #include "ir_print_visitor.h"
38 #include "ir_rvalue_visitor.h"
39 #include "glsl_types.h"
41 static bool debug = false;
43 class variable_entry : public exec_node
45 public:
46 variable_entry(ir_variable *var)
48 this->var = var;
49 this->whole_structure_access = 0;
50 this->declaration = false;
51 this->components = NULL;
52 this->mem_ctx = NULL;
55 ir_variable *var; /* The key: the variable's pointer. */
57 /** Number of times the variable is referenced, including assignments. */
58 unsigned whole_structure_access;
60 bool declaration; /* If the variable had a decl in the instruction stream */
62 ir_variable **components;
64 /** talloc_parent(this->var) -- the shader's talloc context. */
65 void *mem_ctx;
68 class ir_structure_reference_visitor : public ir_hierarchical_visitor {
69 public:
70 ir_structure_reference_visitor(void)
72 this->mem_ctx = talloc_new(NULL);
73 this->variable_list.make_empty();
76 ~ir_structure_reference_visitor(void)
78 talloc_free(mem_ctx);
81 virtual ir_visitor_status visit(ir_variable *);
82 virtual ir_visitor_status visit(ir_dereference_variable *);
83 virtual ir_visitor_status visit_enter(ir_dereference_record *);
84 virtual ir_visitor_status visit_enter(ir_assignment *);
85 virtual ir_visitor_status visit_enter(ir_function_signature *);
87 variable_entry *get_variable_entry(ir_variable *var);
89 /* List of variable_entry */
90 exec_list variable_list;
92 void *mem_ctx;
95 variable_entry *
96 ir_structure_reference_visitor::get_variable_entry(ir_variable *var)
98 assert(var);
100 if (!var->type->is_record() || var->mode == ir_var_uniform)
101 return NULL;
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;
109 variable_entry *entry = new(mem_ctx) variable_entry(var);
110 this->variable_list.push_tail(entry);
111 return entry;
115 ir_visitor_status
116 ir_structure_reference_visitor::visit(ir_variable *ir)
118 variable_entry *entry = this->get_variable_entry(ir);
120 if (entry)
121 entry->declaration = true;
123 return visit_continue;
126 ir_visitor_status
127 ir_structure_reference_visitor::visit(ir_dereference_variable *ir)
129 ir_variable *const var = ir->variable_referenced();
130 variable_entry *entry = this->get_variable_entry(var);
132 if (entry)
133 entry->whole_structure_access++;
135 return visit_continue;
138 ir_visitor_status
139 ir_structure_reference_visitor::visit_enter(ir_dereference_record *ir)
141 /* Don't descend into the ir_dereference_variable below. */
142 return visit_continue_with_parent;
145 ir_visitor_status
146 ir_structure_reference_visitor::visit_enter(ir_assignment *ir)
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.
154 return visit_continue_with_parent;
156 return visit_continue;
159 ir_visitor_status
160 ir_structure_reference_visitor::visit_enter(ir_function_signature *ir)
162 /* We don't want to descend into the function parameters and
163 * dead-code eliminate them, so just accept the body here.
165 visit_list_elements(this, &ir->body);
166 return visit_continue_with_parent;
169 class ir_structure_splitting_visitor : public ir_rvalue_visitor {
170 public:
171 ir_structure_splitting_visitor(exec_list *vars)
173 this->variable_list = vars;
176 virtual ~ir_structure_splitting_visitor()
180 virtual ir_visitor_status visit_leave(ir_assignment *);
182 void split_deref(ir_dereference **deref);
183 void handle_rvalue(ir_rvalue **rvalue);
184 variable_entry *get_splitting_entry(ir_variable *var);
186 exec_list *variable_list;
187 void *mem_ctx;
190 variable_entry *
191 ir_structure_splitting_visitor::get_splitting_entry(ir_variable *var)
193 assert(var);
195 if (!var->type->is_record())
196 return NULL;
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;
205 return NULL;
208 void
209 ir_structure_splitting_visitor::split_deref(ir_dereference **deref)
211 if ((*deref)->ir_type != ir_type_dereference_record)
212 return;
214 ir_dereference_record *deref_record = (ir_dereference_record *)*deref;
215 ir_dereference_variable *deref_var = deref_record->record->as_dereference_variable();
216 if (!deref_var)
217 return;
219 variable_entry *entry = get_splitting_entry(deref_var->var);
220 if (!entry)
221 return;
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;
229 assert(i != entry->var->type->length);
231 *deref = new(entry->mem_ctx) ir_dereference_variable(entry->components[i]);
234 void
235 ir_structure_splitting_visitor::handle_rvalue(ir_rvalue **rvalue)
237 if (!*rvalue)
238 return;
240 ir_dereference *deref = (*rvalue)->as_dereference();
242 if (!deref)
243 return;
245 split_deref(&deref);
246 *rvalue = deref;
249 ir_visitor_status
250 ir_structure_splitting_visitor::visit_leave(ir_assignment *ir)
252 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;
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;
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);
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);
279 ir->insert_before(new(mem_ctx) ir_assignment(new_lhs,
280 new_rhs,
281 NULL));
283 ir->remove();
284 } else {
285 handle_rvalue(&ir->rhs);
286 split_deref(&ir->lhs);
289 handle_rvalue(&ir->condition);
291 return visit_continue;
294 bool
295 do_structure_splitting(exec_list *instructions)
297 ir_structure_reference_visitor refs;
299 visit_list_elements(&refs, instructions);
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();
305 if (debug) {
306 printf("structure %s@%p: decl %d, whole_access %d\n",
307 entry->var->name, (void *) entry->var, entry->declaration,
308 entry->whole_structure_access);
311 if (!entry->declaration || entry->whole_structure_access) {
312 entry->remove();
316 if (refs.variable_list.is_empty())
317 return false;
319 void *mem_ctx = talloc_new(NULL);
321 /* Replace the decls of the structures to be split with their split
322 * components.
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;
328 entry->mem_ctx = talloc_parent(entry->var);
330 entry->components = talloc_array(mem_ctx,
331 ir_variable *,
332 type->length);
334 for (unsigned int i = 0; i < entry->var->type->length; i++) {
335 const char *name = talloc_asprintf(mem_ctx, "%s_%s",
336 entry->var->name,
337 type->fields.structure[i].name);
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]);
346 entry->var->remove();
349 ir_structure_splitting_visitor split(&refs.variable_list);
350 visit_list_elements(&split, instructions);
352 talloc_free(mem_ctx);
354 return true;