targets/egl-static: automake: don't export local symbols
[mesa.git] / src / glsl / opt_vectorize.cpp
1 /*
2 * Copyright © 2013 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_vectorize.cpp
26 *
27 * Combines scalar assignments of the same expression (modulo swizzle) to
28 * multiple channels of the same variable into a single vectorized expression
29 * and assignment.
30 *
31 * Many generated shaders contain scalarized code. That is, they contain
32 *
33 * r1.x = log2(v0.x);
34 * r1.y = log2(v0.y);
35 * r1.z = log2(v0.z);
36 *
37 * rather than
38 *
39 * r1.xyz = log2(v0.xyz);
40 *
41 * We look for consecutive assignments of the same expression (modulo swizzle)
42 * to each channel of the same variable.
43 *
44 * For instance, we want to convert these three scalar operations
45 *
46 * (assign (x) (var_ref r1) (expression float log2 (swiz x (var_ref v0))))
47 * (assign (y) (var_ref r1) (expression float log2 (swiz y (var_ref v0))))
48 * (assign (z) (var_ref r1) (expression float log2 (swiz z (var_ref v0))))
49 *
50 * into a single vector operation
51 *
52 * (assign (xyz) (var_ref r1) (expression vec3 log2 (swiz xyz (var_ref v0))))
53 */
54
55 #include "ir.h"
56 #include "ir_visitor.h"
57 #include "ir_optimization.h"
58 #include "glsl_types.h"
59 #include "program/prog_instruction.h"
60
61 namespace {
62
63 class ir_vectorize_visitor : public ir_hierarchical_visitor {
64 public:
65 void clear()
66 {
67 assignment[0] = NULL;
68 assignment[1] = NULL;
69 assignment[2] = NULL;
70 assignment[3] = NULL;
71 current_assignment = NULL;
72 last_assignment = NULL;
73 channels = 0;
74 has_swizzle = false;
75 }
76
77 ir_vectorize_visitor()
78 {
79 clear();
80 progress = false;
81 }
82
83 virtual ir_visitor_status visit_enter(ir_assignment *);
84 virtual ir_visitor_status visit_enter(ir_swizzle *);
85 virtual ir_visitor_status visit_enter(ir_if *);
86 virtual ir_visitor_status visit_enter(ir_loop *);
87
88 virtual ir_visitor_status visit_leave(ir_assignment *);
89
90 void try_vectorize();
91
92 ir_assignment *assignment[4];
93 ir_assignment *current_assignment, *last_assignment;
94 unsigned channels;
95 bool has_swizzle;
96
97 bool progress;
98 };
99
100 } /* unnamed namespace */
101
102 /**
103 * Rewrites the swizzles and types of a right-hand side of an assignment.
104 *
105 * From the example above, this function would be called (by visit_tree()) on
106 * the nodes of the tree (expression float log2 (swiz z (var_ref v0))),
107 * rewriting it into (expression vec3 log2 (swiz xyz (var_ref v0))).
108 *
109 * The function operates on ir_expressions (and its operands) and ir_swizzles.
110 * For expressions it sets a new type and swizzles any non-expression and non-
111 * swizzle scalar operands into appropriately sized vector arguments. For
112 * example, if combining
113 *
114 * (assign (x) (var_ref r1) (expression float + (swiz x (var_ref v0) (var_ref v1))))
115 * (assign (y) (var_ref r1) (expression float + (swiz y (var_ref v0) (var_ref v1))))
116 *
117 * where v1 is a scalar, rewrite_swizzle() would insert a swizzle on
118 * (var_ref v1) such that the final result was
119 *
120 * (assign (xy) (var_ref r1) (expression vec2 + (swiz xy (var_ref v0))
121 * (swiz xx (var_ref v1))))
122 *
123 * For swizzles, it sets a new type, and if the variable being swizzled is a
124 * vector it overwrites the swizzle mask with the ir_swizzle_mask passed as the
125 * data parameter. If the swizzled variable is scalar, then the swizzle was
126 * added by an earlier call to rewrite_swizzle() on an expression, so the
127 * mask should not be modified.
128 */
129 static void
130 rewrite_swizzle(ir_instruction *ir, void *data)
131 {
132 ir_swizzle_mask *mask = (ir_swizzle_mask *)data;
133
134 switch (ir->ir_type) {
135 case ir_type_swizzle: {
136 ir_swizzle *swz = (ir_swizzle *)ir;
137 if (swz->val->type->is_vector()) {
138 swz->mask = *mask;
139 }
140 swz->type = glsl_type::get_instance(swz->type->base_type,
141 mask->num_components, 1);
142 break;
143 }
144 case ir_type_expression: {
145 ir_expression *expr = (ir_expression *)ir;
146 expr->type = glsl_type::get_instance(expr->type->base_type,
147 mask->num_components, 1);
148 for (unsigned i = 0; i < 4; i++) {
149 if (expr->operands[i]) {
150 ir_rvalue *rval = expr->operands[i]->as_rvalue();
151 if (rval && rval->type->is_scalar() &&
152 !rval->as_expression() && !rval->as_swizzle()) {
153 expr->operands[i] = new(ir) ir_swizzle(rval, 0, 0, 0, 0,
154 mask->num_components);
155 }
156 }
157 }
158 break;
159 }
160 default:
161 break;
162 }
163 }
164
165 /**
166 * Attempt to vectorize the previously saved assignments, and clear them from
167 * consideration.
168 *
169 * If the assignments are able to be combined, it modifies in-place the last
170 * assignment seen to be an equivalent vector form of the scalar assignments.
171 * It then removes the other now obsolete scalar assignments.
172 */
173 void
174 ir_vectorize_visitor::try_vectorize()
175 {
176 if (this->last_assignment && this->channels > 1) {
177 ir_swizzle_mask mask = {0, 0, 0, 0, channels, 0};
178
179 this->last_assignment->write_mask = 0;
180
181 for (unsigned i = 0, j = 0; i < 4; i++) {
182 if (this->assignment[i]) {
183 this->last_assignment->write_mask |= 1 << i;
184
185 if (this->assignment[i] != this->last_assignment) {
186 this->assignment[i]->remove();
187 }
188
189 switch (j) {
190 case 0: mask.x = i; break;
191 case 1: mask.y = i; break;
192 case 2: mask.z = i; break;
193 case 3: mask.w = i; break;
194 }
195
196 j++;
197 }
198 }
199
200 visit_tree(this->last_assignment->rhs, rewrite_swizzle, &mask);
201
202 this->progress = true;
203 }
204 clear();
205 }
206
207 /**
208 * Returns whether the write mask is a single channel.
209 */
210 static bool
211 single_channel_write_mask(unsigned write_mask)
212 {
213 return write_mask != 0 && (write_mask & (write_mask - 1)) == 0;
214 }
215
216 /**
217 * Translates single-channeled write mask to single-channeled swizzle.
218 */
219 static unsigned
220 write_mask_to_swizzle(unsigned write_mask)
221 {
222 switch (write_mask) {
223 case WRITEMASK_X: return SWIZZLE_X;
224 case WRITEMASK_Y: return SWIZZLE_Y;
225 case WRITEMASK_Z: return SWIZZLE_Z;
226 case WRITEMASK_W: return SWIZZLE_W;
227 }
228 assert(!"not reached");
229 unreachable();
230 }
231
232 /**
233 * Returns whether a single-channeled write mask matches a swizzle.
234 */
235 static bool
236 write_mask_matches_swizzle(unsigned write_mask,
237 const ir_swizzle *swz)
238 {
239 return ((write_mask == WRITEMASK_X && swz->mask.x == SWIZZLE_X) ||
240 (write_mask == WRITEMASK_Y && swz->mask.x == SWIZZLE_Y) ||
241 (write_mask == WRITEMASK_Z && swz->mask.x == SWIZZLE_Z) ||
242 (write_mask == WRITEMASK_W && swz->mask.x == SWIZZLE_W));
243 }
244
245 /**
246 * Upon entering an ir_assignment, attempt to vectorize the currently tracked
247 * assignments if the current assignment is not suitable. Keep a pointer to
248 * the current assignment.
249 */
250 ir_visitor_status
251 ir_vectorize_visitor::visit_enter(ir_assignment *ir)
252 {
253 ir_dereference *lhs = this->last_assignment != NULL ?
254 this->last_assignment->lhs : NULL;
255 ir_rvalue *rhs = this->last_assignment != NULL ?
256 this->last_assignment->rhs : NULL;
257
258 if (ir->condition ||
259 this->channels >= 4 ||
260 !single_channel_write_mask(ir->write_mask) ||
261 (lhs && !ir->lhs->equals(lhs)) ||
262 (rhs && !ir->rhs->equals(rhs, ir_type_swizzle))) {
263 try_vectorize();
264 }
265
266 this->current_assignment = ir;
267
268 return visit_continue;
269 }
270
271 /**
272 * Upon entering an ir_swizzle, set ::has_swizzle if we're visiting from an
273 * ir_assignment (i.e., that ::current_assignment is set) and the swizzle mask
274 * matches the current assignment's write mask.
275 *
276 * If the write mask doesn't match the swizzle mask, remove the current
277 * assignment from further consideration.
278 */
279 ir_visitor_status
280 ir_vectorize_visitor::visit_enter(ir_swizzle *ir)
281 {
282 if (this->current_assignment) {
283 if (write_mask_matches_swizzle(this->current_assignment->write_mask, ir)) {
284 this->has_swizzle = true;
285 } else {
286 this->current_assignment = NULL;
287 }
288 }
289 return visit_continue;
290 }
291
292 /* Since there is no statement to visit between the "then" and "else"
293 * instructions try to vectorize before, in between, and after them to avoid
294 * combining statements from different basic blocks.
295 */
296 ir_visitor_status
297 ir_vectorize_visitor::visit_enter(ir_if *ir)
298 {
299 try_vectorize();
300
301 visit_list_elements(this, &ir->then_instructions);
302 try_vectorize();
303
304 visit_list_elements(this, &ir->else_instructions);
305 try_vectorize();
306
307 return visit_continue_with_parent;
308 }
309
310 /* Since there is no statement to visit between the instructions in the body of
311 * the loop and the instructions after it try to vectorize before and after the
312 * body to avoid combining statements from different basic blocks.
313 */
314 ir_visitor_status
315 ir_vectorize_visitor::visit_enter(ir_loop *ir)
316 {
317 try_vectorize();
318
319 visit_list_elements(this, &ir->body_instructions);
320 try_vectorize();
321
322 return visit_continue_with_parent;
323 }
324
325 /**
326 * Upon leaving an ir_assignment, save a pointer to it in ::assignment[] if
327 * the swizzle mask(s) found were appropriate. Also save a pointer in
328 * ::last_assignment so that we can compare future assignments with it.
329 *
330 * Finally, clear ::current_assignment and ::has_swizzle.
331 */
332 ir_visitor_status
333 ir_vectorize_visitor::visit_leave(ir_assignment *ir)
334 {
335 if (this->has_swizzle && this->current_assignment) {
336 assert(this->current_assignment == ir);
337
338 unsigned channel = write_mask_to_swizzle(this->current_assignment->write_mask);
339 this->assignment[channel] = ir;
340 this->channels++;
341
342 this->last_assignment = this->current_assignment;
343 }
344 this->current_assignment = NULL;
345 this->has_swizzle = false;
346 return visit_continue;
347 }
348
349 /**
350 * Combines scalar assignments of the same expression (modulo swizzle) to
351 * multiple channels of the same variable into a single vectorized expression
352 * and assignment.
353 */
354 bool
355 do_vectorize(exec_list *instructions)
356 {
357 ir_vectorize_visitor v;
358
359 v.run(instructions);
360
361 /* Try to vectorize the last assignments seen. */
362 v.try_vectorize();
363
364 return v.progress;
365 }