1/*
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 opt_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"
37#include "ir_print_visitor.h"
38#include "ir_rvalue_visitor.h"
39#include "glsl_types.h"
40
41namespace {
42
43static bool debug = false;
44
45// XXX using variable_entry2 here to avoid collision (MSVC multiply-defined
46// function) with the variable_entry class seen in ir_variable_refcount.h
47// Perhaps we can use the one in ir_variable_refcount.h and make this class
48// here go away?
49class variable_entry2 : public exec_node
50{
51public:
52   variable_entry2(ir_variable *var)
53   {
54      this->var = var;
55      this->whole_structure_access = 0;
56      this->declaration = false;
57      this->components = NULL;
58      this->mem_ctx = NULL;
59   }
60
61   ir_variable *var; /* The key: the variable's pointer. */
62
63   /** Number of times the variable is referenced, including assignments. */
64   unsigned whole_structure_access;
65
66   /* If the variable had a decl we can work with in the instruction
67    * stream.  We can't do splitting on function arguments, which
68    * don't get this variable set.
69    */
70   bool declaration;
71
72   ir_variable **components;
73
74   /** ralloc_parent(this->var) -- the shader's ralloc context. */
75   void *mem_ctx;
76};
77
78
79class ir_structure_reference_visitor : public ir_hierarchical_visitor {
80public:
81   ir_structure_reference_visitor(void)
82   {
83      this->mem_ctx = ralloc_context(NULL);
84      this->variable_list.make_empty();
85   }
86
87   ~ir_structure_reference_visitor(void)
88   {
89      ralloc_free(mem_ctx);
90   }
91
92   virtual ir_visitor_status visit(ir_variable *);
93   virtual ir_visitor_status visit(ir_dereference_variable *);
94   virtual ir_visitor_status visit_enter(ir_dereference_record *);
95   virtual ir_visitor_status visit_enter(ir_assignment *);
96   virtual ir_visitor_status visit_enter(ir_function_signature *);
97
98   variable_entry2 *get_variable_entry2(ir_variable *var);
99
100   /* List of variable_entry */
101   exec_list variable_list;
102
103   void *mem_ctx;
104};
105
106variable_entry2 *
107ir_structure_reference_visitor::get_variable_entry2(ir_variable *var)
108{
109   assert(var);
110
111   if (!var->type->is_record() || var->mode == ir_var_uniform)
112      return NULL;
113
114   foreach_iter(exec_list_iterator, iter, this->variable_list) {
115      variable_entry2 *entry = (variable_entry2 *)iter.get();
116      if (entry->var == var)
117	 return entry;
118   }
119
120   variable_entry2 *entry = new(mem_ctx) variable_entry2(var);
121   this->variable_list.push_tail(entry);
122   return entry;
123}
124
125
126ir_visitor_status
127ir_structure_reference_visitor::visit(ir_variable *ir)
128{
129   variable_entry2 *entry = this->get_variable_entry2(ir);
130
131   if (entry)
132      entry->declaration = true;
133
134   return visit_continue;
135}
136
137ir_visitor_status
138ir_structure_reference_visitor::visit(ir_dereference_variable *ir)
139{
140   ir_variable *const var = ir->variable_referenced();
141   variable_entry2 *entry = this->get_variable_entry2(var);
142
143   if (entry)
144      entry->whole_structure_access++;
145
146   return visit_continue;
147}
148
149ir_visitor_status
150ir_structure_reference_visitor::visit_enter(ir_dereference_record *ir)
151{
152   (void) ir;
153   /* Don't descend into the ir_dereference_variable below. */
154   return visit_continue_with_parent;
155}
156
157ir_visitor_status
158ir_structure_reference_visitor::visit_enter(ir_assignment *ir)
159{
160   /* If there are no structure references yet, no need to bother with
161    * processing the expression tree.
162    */
163   if (this->variable_list.is_empty())
164      return visit_continue_with_parent;
165
166   if (ir->lhs->as_dereference_variable() &&
167       ir->rhs->as_dereference_variable() &&
168       !ir->condition) {
169      /* We'll split copies of a structure to copies of components, so don't
170       * descend to the ir_dereference_variables.
171       */
172      return visit_continue_with_parent;
173   }
174   return visit_continue;
175}
176
177ir_visitor_status
178ir_structure_reference_visitor::visit_enter(ir_function_signature *ir)
179{
180   /* We don't have logic for structure-splitting function arguments,
181    * so just look at the body instructions and not the parameter
182    * declarations.
183    */
184   visit_list_elements(this, &ir->body);
185   return visit_continue_with_parent;
186}
187
188class ir_structure_splitting_visitor : public ir_rvalue_visitor {
189public:
190   ir_structure_splitting_visitor(exec_list *vars)
191   {
192      this->variable_list = vars;
193   }
194
195   virtual ~ir_structure_splitting_visitor()
196   {
197   }
198
199   virtual ir_visitor_status visit_leave(ir_assignment *);
200
201   void split_deref(ir_dereference **deref);
202   void handle_rvalue(ir_rvalue **rvalue);
203   variable_entry2 *get_splitting_entry(ir_variable *var);
204
205   exec_list *variable_list;
206};
207
208variable_entry2 *
209ir_structure_splitting_visitor::get_splitting_entry(ir_variable *var)
210{
211   assert(var);
212
213   if (!var->type->is_record())
214      return NULL;
215
216   foreach_iter(exec_list_iterator, iter, *this->variable_list) {
217      variable_entry2 *entry = (variable_entry2 *)iter.get();
218      if (entry->var == var) {
219	 return entry;
220      }
221   }
222
223   return NULL;
224}
225
226void
227ir_structure_splitting_visitor::split_deref(ir_dereference **deref)
228{
229   if ((*deref)->ir_type != ir_type_dereference_record)
230      return;
231
232   ir_dereference_record *deref_record = (ir_dereference_record *)*deref;
233   ir_dereference_variable *deref_var = deref_record->record->as_dereference_variable();
234   if (!deref_var)
235      return;
236
237   variable_entry2 *entry = get_splitting_entry(deref_var->var);
238   if (!entry)
239      return;
240
241   unsigned int i;
242   for (i = 0; i < entry->var->type->length; i++) {
243      if (strcmp(deref_record->field,
244		 entry->var->type->fields.structure[i].name) == 0)
245	 break;
246   }
247   assert(i != entry->var->type->length);
248
249   *deref = new(entry->mem_ctx) ir_dereference_variable(entry->components[i]);
250}
251
252void
253ir_structure_splitting_visitor::handle_rvalue(ir_rvalue **rvalue)
254{
255   if (!*rvalue)
256      return;
257
258   ir_dereference *deref = (*rvalue)->as_dereference();
259
260   if (!deref)
261      return;
262
263   split_deref(&deref);
264   *rvalue = deref;
265}
266
267ir_visitor_status
268ir_structure_splitting_visitor::visit_leave(ir_assignment *ir)
269{
270   ir_dereference_variable *lhs_deref = ir->lhs->as_dereference_variable();
271   ir_dereference_variable *rhs_deref = ir->rhs->as_dereference_variable();
272   variable_entry2 *lhs_entry = lhs_deref ? get_splitting_entry(lhs_deref->var) : NULL;
273   variable_entry2 *rhs_entry = rhs_deref ? get_splitting_entry(rhs_deref->var) : NULL;
274   const glsl_type *type = ir->rhs->type;
275
276   if ((lhs_entry || rhs_entry) && !ir->condition) {
277      for (unsigned int i = 0; i < type->length; i++) {
278	 ir_dereference *new_lhs, *new_rhs;
279	 void *mem_ctx = lhs_entry ? lhs_entry->mem_ctx : rhs_entry->mem_ctx;
280
281	 if (lhs_entry) {
282	    new_lhs = new(mem_ctx) ir_dereference_variable(lhs_entry->components[i]);
283	 } else {
284	    new_lhs = new(mem_ctx)
285	       ir_dereference_record(ir->lhs->clone(mem_ctx, NULL),
286				     type->fields.structure[i].name);
287	 }
288
289	 if (rhs_entry) {
290	    new_rhs = new(mem_ctx) ir_dereference_variable(rhs_entry->components[i]);
291	 } else {
292	    new_rhs = new(mem_ctx)
293	       ir_dereference_record(ir->rhs->clone(mem_ctx, NULL),
294				     type->fields.structure[i].name);
295	 }
296
297	 ir->insert_before(new(mem_ctx) ir_assignment(new_lhs,
298						      new_rhs,
299						      NULL));
300      }
301      ir->remove();
302   } else {
303      handle_rvalue(&ir->rhs);
304      split_deref(&ir->lhs);
305   }
306
307   handle_rvalue(&ir->condition);
308
309   return visit_continue;
310}
311
312} /* unnamed namespace */
313
314bool
315do_structure_splitting(exec_list *instructions)
316{
317   ir_structure_reference_visitor refs;
318
319   visit_list_elements(&refs, instructions);
320
321   /* Trim out variables we can't split. */
322   foreach_iter(exec_list_iterator, iter, refs.variable_list) {
323      variable_entry2 *entry = (variable_entry2 *)iter.get();
324
325      if (debug) {
326	 printf("structure %s@%p: decl %d, whole_access %d\n",
327		entry->var->name, (void *) entry->var, entry->declaration,
328		entry->whole_structure_access);
329      }
330
331      if (!entry->declaration || entry->whole_structure_access) {
332	 entry->remove();
333      }
334   }
335
336   if (refs.variable_list.is_empty())
337      return false;
338
339   void *mem_ctx = ralloc_context(NULL);
340
341   /* Replace the decls of the structures to be split with their split
342    * components.
343    */
344   foreach_iter(exec_list_iterator, iter, refs.variable_list) {
345      variable_entry2 *entry = (variable_entry2 *)iter.get();
346      const struct glsl_type *type = entry->var->type;
347
348      entry->mem_ctx = ralloc_parent(entry->var);
349
350      entry->components = ralloc_array(mem_ctx,
351				       ir_variable *,
352				       type->length);
353
354      for (unsigned int i = 0; i < entry->var->type->length; i++) {
355	 const char *name = ralloc_asprintf(mem_ctx, "%s_%s",
356					    entry->var->name,
357					    type->fields.structure[i].name);
358
359	 entry->components[i] =
360	    new(entry->mem_ctx) ir_variable(type->fields.structure[i].type,
361					    name,
362					    ir_var_temporary);
363	 entry->var->insert_before(entry->components[i]);
364      }
365
366      entry->var->remove();
367   }
368
369   ir_structure_splitting_visitor split(&refs.variable_list);
370   visit_list_elements(&split, instructions);
371
372   ralloc_free(mem_ctx);
373
374   return true;
375}
376