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