glsl: move to compiler/
[mesa.git] / src / mesa / drivers / dri / i965 / brw_fs_vector_splitting.cpp
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 brw_wm_vector_splitting.cpp
26 *
27 * If a vector 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 vectors in uniforms and varyings, which need to be
32 * accessible as vectors for their access by the GL. Also, vector
33 * results of non-variable-derefs in assignments aren't handled
34 * because to do so we would have to store the vector result to a
35 * temporary in order to unload each channel, and to do so would just
36 * loop us back to where we started. For the 965, this is exactly the
37 * behavior we want for the results of texture lookups, but probably not for
38 */
39
40 #include "main/imports.h"
41 #include "compiler/glsl/ir.h"
42 #include "compiler/glsl/ir_rvalue_visitor.h"
43 #include "compiler/glsl_types.h"
44 #include "util/hash_table.h"
45
46 static bool debug = false;
47
48 class variable_entry : public exec_node
49 {
50 public:
51 variable_entry(ir_variable *var)
52 {
53 this->var = var;
54 this->whole_vector_access = 0;
55 this->mem_ctx = NULL;
56 }
57
58 ir_variable *var; /* The key: the variable's pointer. */
59
60 /** Number of times the variable is referenced, including assignments. */
61 unsigned whole_vector_access;
62
63 ir_variable *components[4];
64
65 /** ralloc_parent(this->var) -- the shader's ralloc context. */
66 void *mem_ctx;
67 };
68
69 class ir_vector_reference_visitor : public ir_hierarchical_visitor {
70 public:
71 ir_vector_reference_visitor(void)
72 {
73 this->mem_ctx = ralloc_context(NULL);
74 this->ht = _mesa_hash_table_create(mem_ctx, _mesa_hash_pointer,
75 _mesa_key_pointer_equal);
76 }
77
78 ~ir_vector_reference_visitor(void)
79 {
80 ralloc_free(mem_ctx);
81 }
82
83 virtual ir_visitor_status visit(ir_variable *);
84 virtual ir_visitor_status visit(ir_dereference_variable *);
85 virtual ir_visitor_status visit_enter(ir_swizzle *);
86 virtual ir_visitor_status visit_enter(ir_assignment *);
87 virtual ir_visitor_status visit_enter(ir_function_signature *);
88
89 variable_entry *get_variable_entry(ir_variable *var);
90
91 /* List of variable_entry */
92 struct hash_table *ht;
93
94 void *mem_ctx;
95 };
96
97 variable_entry *
98 ir_vector_reference_visitor::get_variable_entry(ir_variable *var)
99 {
100 assert(var);
101
102 if (!var->type->is_vector())
103 return NULL;
104
105 switch (var->data.mode) {
106 case ir_var_uniform:
107 case ir_var_shader_storage:
108 case ir_var_shader_shared:
109 case ir_var_shader_in:
110 case ir_var_shader_out:
111 case ir_var_system_value:
112 case ir_var_function_in:
113 case ir_var_function_out:
114 case ir_var_function_inout:
115 /* Can't split varyings or uniforms. Function in/outs won't get split
116 * either.
117 */
118 return NULL;
119 case ir_var_auto:
120 case ir_var_temporary:
121 break;
122 }
123
124 struct hash_entry *hte = _mesa_hash_table_search(ht, var);
125 if (hte)
126 return (struct variable_entry *) hte->data;
127
128 variable_entry *entry = new(mem_ctx) variable_entry(var);
129 _mesa_hash_table_insert(ht, var, entry);
130 return entry;
131 }
132
133
134 ir_visitor_status
135 ir_vector_reference_visitor::visit(ir_variable *ir)
136 {
137 /* Make sure splitting looks at splitting this variable */
138 (void)this->get_variable_entry(ir);
139
140 return visit_continue;
141 }
142
143 ir_visitor_status
144 ir_vector_reference_visitor::visit(ir_dereference_variable *ir)
145 {
146 ir_variable *const var = ir->var;
147 variable_entry *entry = this->get_variable_entry(var);
148
149 if (entry)
150 entry->whole_vector_access++;
151
152 return visit_continue;
153 }
154
155 ir_visitor_status
156 ir_vector_reference_visitor::visit_enter(ir_swizzle *ir)
157 {
158 /* Don't descend into a vector ir_dereference_variable below. */
159 if (ir->val->as_dereference_variable() && ir->type->is_scalar())
160 return visit_continue_with_parent;
161
162 return visit_continue;
163 }
164
165 ir_visitor_status
166 ir_vector_reference_visitor::visit_enter(ir_assignment *ir)
167 {
168 if (ir->lhs->as_dereference_variable() &&
169 ir->rhs->as_dereference_variable() &&
170 !ir->condition) {
171 /* We'll split copies of a vector to copies of channels, so don't
172 * descend to the ir_dereference_variables.
173 */
174 return visit_continue_with_parent;
175 }
176 if (ir->lhs->as_dereference_variable() &&
177 _mesa_is_pow_two(ir->write_mask) &&
178 !ir->condition) {
179 /* If we're writing just a channel, then channel-splitting the LHS is OK.
180 */
181 ir->rhs->accept(this);
182 return visit_continue_with_parent;
183 }
184 return visit_continue;
185 }
186
187 ir_visitor_status
188 ir_vector_reference_visitor::visit_enter(ir_function_signature *ir)
189 {
190 /* We don't want to descend into the function parameters and
191 * split them, so just accept the body here.
192 */
193 visit_list_elements(this, &ir->body);
194 return visit_continue_with_parent;
195 }
196
197 class ir_vector_splitting_visitor : public ir_rvalue_visitor {
198 public:
199 ir_vector_splitting_visitor(struct hash_table *vars)
200 {
201 this->ht = vars;
202 }
203
204 virtual ir_visitor_status visit_leave(ir_assignment *);
205
206 void handle_rvalue(ir_rvalue **rvalue);
207 variable_entry *get_splitting_entry(ir_variable *var);
208
209 struct hash_table *ht;
210 };
211
212 variable_entry *
213 ir_vector_splitting_visitor::get_splitting_entry(ir_variable *var)
214 {
215 assert(var);
216
217 if (!var->type->is_vector())
218 return NULL;
219
220 struct hash_entry *hte = _mesa_hash_table_search(ht, var);
221 return hte ? (struct variable_entry *) hte->data : NULL;
222 }
223
224 void
225 ir_vector_splitting_visitor::handle_rvalue(ir_rvalue **rvalue)
226 {
227 if (!*rvalue)
228 return;
229
230 ir_swizzle *swiz = (*rvalue)->as_swizzle();
231 if (!swiz || !swiz->type->is_scalar())
232 return;
233
234 ir_dereference_variable *deref_var = swiz->val->as_dereference_variable();
235 if (!deref_var)
236 return;
237
238 variable_entry *entry = get_splitting_entry(deref_var->var);
239 if (!entry)
240 return;
241
242 ir_variable *var = entry->components[swiz->mask.x];
243 *rvalue = new(entry->mem_ctx) ir_dereference_variable(var);
244 }
245
246 ir_visitor_status
247 ir_vector_splitting_visitor::visit_leave(ir_assignment *ir)
248 {
249 ir_dereference_variable *lhs_deref = ir->lhs->as_dereference_variable();
250 ir_dereference_variable *rhs_deref = ir->rhs->as_dereference_variable();
251 variable_entry *lhs = lhs_deref ? get_splitting_entry(lhs_deref->var) : NULL;
252 variable_entry *rhs = rhs_deref ? get_splitting_entry(rhs_deref->var) : NULL;
253
254 if (lhs_deref && rhs_deref && (lhs || rhs) && !ir->condition) {
255 unsigned int rhs_chan = 0;
256
257 /* Straight assignment of vector variables. */
258 for (unsigned int i = 0; i < ir->lhs->type->vector_elements; i++) {
259 ir_dereference *new_lhs;
260 ir_rvalue *new_rhs;
261 void *mem_ctx = lhs ? lhs->mem_ctx : rhs->mem_ctx;
262 unsigned int writemask;
263
264 if (!(ir->write_mask & (1 << i)))
265 continue;
266
267 if (lhs) {
268 new_lhs = new(mem_ctx) ir_dereference_variable(lhs->components[i]);
269 writemask = 1;
270 } else {
271 new_lhs = ir->lhs->clone(mem_ctx, NULL);
272 writemask = 1 << i;
273 }
274
275 if (rhs) {
276 new_rhs =
277 new(mem_ctx) ir_dereference_variable(rhs->components[rhs_chan]);
278 } else {
279 new_rhs = new(mem_ctx) ir_swizzle(ir->rhs->clone(mem_ctx, NULL),
280 rhs_chan, 0, 0, 0, 1);
281 }
282
283 ir->insert_before(new(mem_ctx) ir_assignment(new_lhs,
284 new_rhs,
285 NULL, writemask));
286
287 rhs_chan++;
288 }
289 ir->remove();
290 } else if (lhs) {
291 void *mem_ctx = lhs->mem_ctx;
292 int elem = -1;
293
294 switch (ir->write_mask) {
295 case (1 << 0):
296 elem = 0;
297 break;
298 case (1 << 1):
299 elem = 1;
300 break;
301 case (1 << 2):
302 elem = 2;
303 break;
304 case (1 << 3):
305 elem = 3;
306 break;
307 default:
308 ir->fprint(stderr);
309 unreachable("not reached: non-channelwise dereference of LHS.");
310 }
311
312 ir->lhs = new(mem_ctx) ir_dereference_variable(lhs->components[elem]);
313 ir->write_mask = (1 << 0);
314
315 handle_rvalue(&ir->rhs);
316 } else {
317 handle_rvalue(&ir->rhs);
318 }
319
320 handle_rvalue(&ir->condition);
321
322 return visit_continue;
323 }
324
325 bool
326 brw_do_vector_splitting(exec_list *instructions)
327 {
328 struct hash_entry *hte;
329
330 ir_vector_reference_visitor refs;
331
332 visit_list_elements(&refs, instructions);
333
334 /* Trim out variables we can't split. */
335 hash_table_foreach(refs.ht, hte) {
336 struct variable_entry *entry = (struct variable_entry *) hte->data;
337 if (debug) {
338 fprintf(stderr, "vector %s@%p: whole_access %d\n",
339 entry->var->name, (void *) entry->var,
340 entry->whole_vector_access);
341 }
342
343 if (entry->whole_vector_access) {
344 _mesa_hash_table_remove(refs.ht, hte);
345 }
346 }
347
348 if (refs.ht->entries == 0)
349 return false;
350
351 void *mem_ctx = ralloc_context(NULL);
352
353 /* Replace the decls of the vectors to be split with their split
354 * components.
355 */
356 hash_table_foreach(refs.ht, hte) {
357 struct variable_entry *entry = (struct variable_entry *) hte->data;
358 const struct glsl_type *type;
359 type = glsl_type::get_instance(entry->var->type->base_type, 1, 1);
360
361 entry->mem_ctx = ralloc_parent(entry->var);
362
363 for (unsigned int i = 0; i < entry->var->type->vector_elements; i++) {
364 char *const name = ir_variable::temporaries_allocate_names
365 ? ralloc_asprintf(mem_ctx, "%s_%c",
366 entry->var->name,
367 "xyzw"[i])
368 : NULL;
369
370 entry->components[i] = new(entry->mem_ctx) ir_variable(type, name,
371 ir_var_temporary);
372
373 ralloc_free(name);
374
375 entry->var->insert_before(entry->components[i]);
376 }
377
378 entry->var->remove();
379 }
380
381 ir_vector_splitting_visitor split(refs.ht);
382 visit_list_elements(&split, instructions);
383
384 ralloc_free(mem_ctx);
385
386 return true;
387 }