Descend down the tree in more locations in constant folding.
[mesa.git] / ir_constant_expression.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 ir_constant_expression.cpp
26 * Evaluate and process constant valued expressions
27 *
28 * In GLSL, constant valued expressions are used in several places. These
29 * must be processed and evaluated very early in the compilation process.
30 *
31 * * Sizes of arrays
32 * * Initializers for uniforms
33 * * Initializers for \c const variables
34 */
35
36 #define NULL 0
37 #include "ir.h"
38 #include "ir_visitor.h"
39 #include "glsl_types.h"
40
41 /**
42 * Visitor class for evaluating constant expressions
43 */
44 class ir_constant_visitor : public ir_visitor {
45 public:
46 ir_constant_visitor()
47 : value(NULL)
48 {
49 /* empty */
50 }
51
52 virtual ~ir_constant_visitor()
53 {
54 /* empty */
55 }
56
57 /**
58 * \name Visit methods
59 *
60 * As typical for the visitor pattern, there must be one \c visit method for
61 * each concrete subclass of \c ir_instruction. Virtual base classes within
62 * the hierarchy should not have \c visit methods.
63 */
64 /*@{*/
65 virtual void visit(ir_variable *);
66 virtual void visit(ir_label *);
67 virtual void visit(ir_function_signature *);
68 virtual void visit(ir_function *);
69 virtual void visit(ir_expression *);
70 virtual void visit(ir_swizzle *);
71 virtual void visit(ir_dereference *);
72 virtual void visit(ir_assignment *);
73 virtual void visit(ir_constant *);
74 virtual void visit(ir_call *);
75 virtual void visit(ir_return *);
76 virtual void visit(ir_if *);
77 /*@}*/
78
79 /**
80 * Value of the constant expression.
81 *
82 * \note
83 * This field will be \c NULL if the expression is not constant valued.
84 */
85 /* FINIHSME: This cannot hold values for constant arrays or structures. */
86 ir_constant *value;
87 };
88
89
90 ir_constant *
91 ir_instruction::constant_expression_value()
92 {
93 ir_constant_visitor visitor;
94
95 this->accept(& visitor);
96 return visitor.value;
97 }
98
99
100 void
101 ir_constant_visitor::visit(ir_variable *ir)
102 {
103 (void) ir;
104 value = NULL;
105 }
106
107
108 void
109 ir_constant_visitor::visit(ir_label *ir)
110 {
111 (void) ir;
112 value = NULL;
113 }
114
115
116 void
117 ir_constant_visitor::visit(ir_function_signature *ir)
118 {
119 (void) ir;
120 value = NULL;
121 }
122
123
124 void
125 ir_constant_visitor::visit(ir_function *ir)
126 {
127 (void) ir;
128 value = NULL;
129 }
130
131 void
132 ir_constant_visitor::visit(ir_expression *ir)
133 {
134 value = NULL;
135 ir_constant *op[2];
136 unsigned int operand, c;
137 unsigned u[16];
138 int i[16];
139 float f[16];
140 bool b[16];
141 const glsl_type *type = NULL;
142
143 for (operand = 0; operand < ir->get_num_operands(); operand++) {
144 op[operand] = ir->operands[operand]->constant_expression_value();
145 if (!op[operand])
146 return;
147 }
148
149 switch (ir->operation) {
150 case ir_unop_logic_not:
151 type = ir->operands[0]->type;
152 assert(type->base_type == GLSL_TYPE_BOOL);
153 for (c = 0; c < ir->operands[0]->type->components(); c++)
154 b[c] = !op[0]->value.b[c];
155 break;
156
157 case ir_unop_f2i:
158 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
159 type = ir->type;
160 for (c = 0; c < ir->operands[0]->type->components(); c++) {
161 i[c] = op[0]->value.f[c];
162 }
163 break;
164 case ir_unop_i2f:
165 assert(op[0]->type->base_type == GLSL_TYPE_UINT ||
166 op[0]->type->base_type == GLSL_TYPE_INT);
167 type = ir->type;
168 for (c = 0; c < ir->operands[0]->type->components(); c++) {
169 if (op[0]->type->base_type == GLSL_TYPE_INT)
170 f[c] = op[0]->value.i[c];
171 else
172 f[c] = op[0]->value.u[c];
173 }
174 break;
175
176 case ir_binop_add:
177 if (ir->operands[0]->type == ir->operands[1]->type) {
178 type = ir->operands[0]->type;
179 for (c = 0; c < ir->operands[0]->type->components(); c++) {
180 switch (ir->operands[0]->type->base_type) {
181 case GLSL_TYPE_UINT:
182 u[c] = op[0]->value.u[c] + op[1]->value.u[c];
183 break;
184 case GLSL_TYPE_INT:
185 i[c] = op[0]->value.i[c] + op[1]->value.i[c];
186 break;
187 case GLSL_TYPE_FLOAT:
188 f[c] = op[0]->value.f[c] + op[1]->value.f[c];
189 break;
190 default:
191 assert(0);
192 }
193 }
194 }
195 break;
196 case ir_binop_sub:
197 if (ir->operands[0]->type == ir->operands[1]->type) {
198 type = ir->operands[0]->type;
199 for (c = 0; c < ir->operands[0]->type->components(); c++) {
200 switch (ir->operands[0]->type->base_type) {
201 case GLSL_TYPE_UINT:
202 u[c] = op[0]->value.u[c] - op[1]->value.u[c];
203 break;
204 case GLSL_TYPE_INT:
205 i[c] = op[0]->value.i[c] - op[1]->value.i[c];
206 break;
207 case GLSL_TYPE_FLOAT:
208 f[c] = op[0]->value.f[c] - op[1]->value.f[c];
209 break;
210 default:
211 assert(0);
212 }
213 }
214 }
215 break;
216 case ir_binop_mul:
217 if (ir->operands[0]->type == ir->operands[1]->type &&
218 !ir->operands[0]->type->is_matrix()) {
219 type = ir->operands[0]->type;
220 for (c = 0; c < ir->operands[0]->type->components(); c++) {
221 switch (ir->operands[0]->type->base_type) {
222 case GLSL_TYPE_UINT:
223 u[c] = op[0]->value.u[c] * op[1]->value.u[c];
224 break;
225 case GLSL_TYPE_INT:
226 i[c] = op[0]->value.i[c] * op[1]->value.i[c];
227 break;
228 case GLSL_TYPE_FLOAT:
229 f[c] = op[0]->value.f[c] * op[1]->value.f[c];
230 break;
231 default:
232 assert(0);
233 }
234 }
235 }
236 break;
237 case ir_binop_div:
238 if (ir->operands[0]->type == ir->operands[1]->type) {
239 type = ir->operands[0]->type;
240 for (c = 0; c < ir->operands[0]->type->components(); c++) {
241 switch (ir->operands[0]->type->base_type) {
242 case GLSL_TYPE_UINT:
243 u[c] = op[0]->value.u[c] / op[1]->value.u[c];
244 break;
245 case GLSL_TYPE_INT:
246 i[c] = op[0]->value.i[c] / op[1]->value.i[c];
247 break;
248 case GLSL_TYPE_FLOAT:
249 f[c] = op[0]->value.f[c] / op[1]->value.f[c];
250 break;
251 default:
252 assert(0);
253 }
254 }
255 }
256 break;
257 case ir_binop_logic_and:
258 type = ir->operands[0]->type;
259 assert(type->base_type == GLSL_TYPE_BOOL);
260 for (c = 0; c < ir->operands[0]->type->components(); c++)
261 b[c] = op[0]->value.b[c] && op[1]->value.b[c];
262 break;
263 case ir_binop_logic_xor:
264 type = ir->operands[0]->type;
265 assert(type->base_type == GLSL_TYPE_BOOL);
266 for (c = 0; c < ir->operands[0]->type->components(); c++)
267 b[c] = op[0]->value.b[c] ^ op[1]->value.b[c];
268 break;
269 case ir_binop_logic_or:
270 type = ir->operands[0]->type;
271 assert(type->base_type == GLSL_TYPE_BOOL);
272 for (c = 0; c < ir->operands[0]->type->components(); c++)
273 b[c] = op[0]->value.b[c] || op[1]->value.b[c];
274 break;
275
276 case ir_binop_less:
277 type = glsl_type::bool_type;
278 switch (ir->operands[0]->type->base_type) {
279 case GLSL_TYPE_UINT:
280 b[0] = op[0]->value.u[0] < op[1]->value.u[0];
281 break;
282 case GLSL_TYPE_INT:
283 b[0] = op[0]->value.i[0] < op[1]->value.i[0];
284 break;
285 case GLSL_TYPE_FLOAT:
286 b[0] = op[0]->value.f[0] < op[1]->value.f[0];
287 break;
288 default:
289 assert(0);
290 }
291 break;
292 case ir_binop_greater:
293 type = glsl_type::bool_type;
294 switch (ir->operands[0]->type->base_type) {
295 case GLSL_TYPE_UINT:
296 b[0] = op[0]->value.u[0] > op[1]->value.u[0];
297 break;
298 case GLSL_TYPE_INT:
299 b[0] = op[0]->value.i[0] > op[1]->value.i[0];
300 break;
301 case GLSL_TYPE_FLOAT:
302 b[0] = op[0]->value.f[0] > op[1]->value.f[0];
303 break;
304 default:
305 assert(0);
306 }
307 break;
308 case ir_binop_lequal:
309 type = glsl_type::bool_type;
310 switch (ir->operands[0]->type->base_type) {
311 case GLSL_TYPE_UINT:
312 b[0] = op[0]->value.u[0] <= op[1]->value.u[0];
313 break;
314 case GLSL_TYPE_INT:
315 b[0] = op[0]->value.i[0] <= op[1]->value.i[0];
316 break;
317 case GLSL_TYPE_FLOAT:
318 b[0] = op[0]->value.f[0] <= op[1]->value.f[0];
319 break;
320 default:
321 assert(0);
322 }
323 break;
324 case ir_binop_gequal:
325 type = glsl_type::bool_type;
326 switch (ir->operands[0]->type->base_type) {
327 case GLSL_TYPE_UINT:
328 b[0] = op[0]->value.u[0] >= op[1]->value.u[0];
329 break;
330 case GLSL_TYPE_INT:
331 b[0] = op[0]->value.i[0] >= op[1]->value.i[0];
332 break;
333 case GLSL_TYPE_FLOAT:
334 b[0] = op[0]->value.f[0] >= op[1]->value.f[0];
335 break;
336 default:
337 assert(0);
338 }
339 break;
340
341 case ir_binop_equal:
342 if (ir->operands[0]->type == ir->operands[1]->type) {
343 type = glsl_type::bool_type;
344 b[0] = true;
345 for (c = 0; c < ir->operands[0]->type->components(); c++) {
346 switch (ir->operands[0]->type->base_type) {
347 case GLSL_TYPE_UINT:
348 b[0] = b[0] && op[0]->value.u[c] == op[1]->value.u[c];
349 break;
350 case GLSL_TYPE_INT:
351 b[0] = b[0] && op[0]->value.i[c] == op[1]->value.i[c];
352 break;
353 case GLSL_TYPE_FLOAT:
354 b[0] = b[0] && op[0]->value.f[c] == op[1]->value.f[c];
355 break;
356 default:
357 assert(0);
358 }
359 }
360 }
361 break;
362 case ir_binop_nequal:
363 if (ir->operands[0]->type == ir->operands[1]->type) {
364 type = glsl_type::bool_type;
365 b[0] = false;
366 for (c = 0; c < ir->operands[0]->type->components(); c++) {
367 switch (ir->operands[0]->type->base_type) {
368 case GLSL_TYPE_UINT:
369 b[0] = b[0] || op[0]->value.u[c] != op[1]->value.u[c];
370 break;
371 case GLSL_TYPE_INT:
372 b[0] = b[0] || op[0]->value.i[c] != op[1]->value.i[c];
373 break;
374 case GLSL_TYPE_FLOAT:
375 b[0] = b[0] || op[0]->value.f[c] != op[1]->value.f[c];
376 break;
377 default:
378 assert(0);
379 }
380 }
381 }
382 break;
383
384 default:
385 break;
386 }
387
388 if (type) {
389 switch (type->base_type) {
390 case GLSL_TYPE_UINT:
391 value = new ir_constant(type, u);
392 break;
393 case GLSL_TYPE_INT:
394 value = new ir_constant(type, i);
395 break;
396 case GLSL_TYPE_FLOAT:
397 value = new ir_constant(type, f);
398 break;
399 case GLSL_TYPE_BOOL:
400 value = new ir_constant(type, b);
401 break;
402 }
403 }
404 }
405
406
407 void
408 ir_constant_visitor::visit(ir_swizzle *ir)
409 {
410 (void) ir;
411 value = NULL;
412 }
413
414
415 void
416 ir_constant_visitor::visit(ir_dereference *ir)
417 {
418 value = NULL;
419
420 if (ir->mode == ir_dereference::ir_reference_variable) {
421 ir_variable *var = ir->var->as_variable();
422 if (var && var->constant_value) {
423 value = new ir_constant(ir->type, &var->constant_value->value);
424 }
425 }
426 /* FINISHME: Other dereference modes. */
427 }
428
429
430 void
431 ir_constant_visitor::visit(ir_assignment *ir)
432 {
433 (void) ir;
434 value = NULL;
435 }
436
437
438 void
439 ir_constant_visitor::visit(ir_constant *ir)
440 {
441 value = ir;
442 }
443
444
445 void
446 ir_constant_visitor::visit(ir_call *ir)
447 {
448 (void) ir;
449 value = NULL;
450 }
451
452
453 void
454 ir_constant_visitor::visit(ir_return *ir)
455 {
456 (void) ir;
457 value = NULL;
458 }
459
460
461 void
462 ir_constant_visitor::visit(ir_if *ir)
463 {
464 (void) ir;
465 value = NULL;
466 }