blob: 96dfc56e31c92a6c47f6b9268de02d2bd499269b [file] [log] [blame]
Ian Romanick1cf43a42010-03-30 16:56:50 -07001/*
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
Eric Anholt43ad37a2010-05-12 14:42:21 -070037#include <math.h>
Ian Romanick1cf43a42010-03-30 16:56:50 -070038#include "ir.h"
39#include "ir_visitor.h"
Eric Anholta576f9d2010-03-31 16:25:12 -100040#include "glsl_types.h"
Ian Romanick1cf43a42010-03-30 16:56:50 -070041
42/**
43 * Visitor class for evaluating constant expressions
44 */
45class ir_constant_visitor : public ir_visitor {
46public:
47 ir_constant_visitor()
48 : value(NULL)
49 {
50 /* empty */
51 }
52
53 virtual ~ir_constant_visitor()
54 {
55 /* empty */
56 }
57
58 /**
59 * \name Visit methods
60 *
61 * As typical for the visitor pattern, there must be one \c visit method for
62 * each concrete subclass of \c ir_instruction. Virtual base classes within
63 * the hierarchy should not have \c visit methods.
64 */
65 /*@{*/
66 virtual void visit(ir_variable *);
Ian Romanick1cf43a42010-03-30 16:56:50 -070067 virtual void visit(ir_function_signature *);
68 virtual void visit(ir_function *);
69 virtual void visit(ir_expression *);
Kenneth Graunke26d74cd2010-05-26 17:42:03 -070070 virtual void visit(ir_texture *);
Ian Romanick1cf43a42010-03-30 16:56:50 -070071 virtual void visit(ir_swizzle *);
Ian Romanickc7b10462010-05-19 13:20:12 +020072 virtual void visit(ir_dereference_variable *);
73 virtual void visit(ir_dereference_array *);
74 virtual void visit(ir_dereference_record *);
Ian Romanick1cf43a42010-03-30 16:56:50 -070075 virtual void visit(ir_assignment *);
76 virtual void visit(ir_constant *);
77 virtual void visit(ir_call *);
78 virtual void visit(ir_return *);
79 virtual void visit(ir_if *);
Ian Romanickfad607a2010-04-05 16:16:07 -070080 virtual void visit(ir_loop *);
Ian Romanickf8e31e02010-04-05 16:28:15 -070081 virtual void visit(ir_loop_jump *);
Ian Romanick1cf43a42010-03-30 16:56:50 -070082 /*@}*/
83
84 /**
85 * Value of the constant expression.
86 *
87 * \note
88 * This field will be \c NULL if the expression is not constant valued.
89 */
90 /* FINIHSME: This cannot hold values for constant arrays or structures. */
91 ir_constant *value;
92};
93
94
95ir_constant *
96ir_instruction::constant_expression_value()
97{
98 ir_constant_visitor visitor;
99
100 this->accept(& visitor);
101 return visitor.value;
102}
103
104
105void
106ir_constant_visitor::visit(ir_variable *ir)
107{
108 (void) ir;
109 value = NULL;
110}
111
112
113void
Ian Romanick1cf43a42010-03-30 16:56:50 -0700114ir_constant_visitor::visit(ir_function_signature *ir)
115{
116 (void) ir;
117 value = NULL;
118}
119
120
121void
122ir_constant_visitor::visit(ir_function *ir)
123{
124 (void) ir;
125 value = NULL;
126}
127
Ian Romanick1cf43a42010-03-30 16:56:50 -0700128void
129ir_constant_visitor::visit(ir_expression *ir)
130{
Ian Romanick1cf43a42010-03-30 16:56:50 -0700131 value = NULL;
Eric Anholta576f9d2010-03-31 16:25:12 -1000132 ir_constant *op[2];
Eric Anholtd98da972010-04-01 18:25:11 -1000133 unsigned int operand, c;
134 unsigned u[16];
135 int i[16];
136 float f[16];
137 bool b[16];
138 const glsl_type *type = NULL;
Eric Anholt160d0922010-04-01 18:07:08 -1000139
Eric Anholtd98da972010-04-01 18:25:11 -1000140 for (operand = 0; operand < ir->get_num_operands(); operand++) {
141 op[operand] = ir->operands[operand]->constant_expression_value();
142 if (!op[operand])
Eric Anholt160d0922010-04-01 18:07:08 -1000143 return;
144 }
Eric Anholta576f9d2010-03-31 16:25:12 -1000145
146 switch (ir->operation) {
Eric Anholt528bb852010-03-31 21:09:02 -1000147 case ir_unop_logic_not:
Eric Anholtd98da972010-04-01 18:25:11 -1000148 type = ir->operands[0]->type;
149 assert(type->base_type == GLSL_TYPE_BOOL);
150 for (c = 0; c < ir->operands[0]->type->components(); c++)
151 b[c] = !op[0]->value.b[c];
Eric Anholt528bb852010-03-31 21:09:02 -1000152 break;
Eric Anholtaf186412010-04-06 10:53:57 -0700153
154 case ir_unop_f2i:
155 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
156 type = ir->type;
157 for (c = 0; c < ir->operands[0]->type->components(); c++) {
158 i[c] = op[0]->value.f[c];
159 }
160 break;
161 case ir_unop_i2f:
162 assert(op[0]->type->base_type == GLSL_TYPE_UINT ||
163 op[0]->type->base_type == GLSL_TYPE_INT);
164 type = ir->type;
165 for (c = 0; c < ir->operands[0]->type->components(); c++) {
166 if (op[0]->type->base_type == GLSL_TYPE_INT)
167 f[c] = op[0]->value.i[c];
168 else
169 f[c] = op[0]->value.u[c];
170 }
171 break;
Ian Romanick7dc2b712010-06-07 15:10:14 -0700172 case ir_unop_b2f:
173 assert(op[0]->type->base_type == GLSL_TYPE_BOOL);
174 type = ir->type;
175 for (c = 0; c < ir->operands[0]->type->components(); c++) {
176 f[c] = op[0]->value.b[c] ? 1.0 : 0.0;
177 }
178 break;
179 case ir_unop_f2b:
180 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
181 type = ir->type;
182 for (c = 0; c < ir->operands[0]->type->components(); c++) {
183 b[c] = bool(op[0]->value.f[c]);
184 }
185 break;
Eric Anholtaf186412010-04-06 10:53:57 -0700186
Eric Anholt43ad37a2010-05-12 14:42:21 -0700187 case ir_unop_neg:
188 type = ir->type;
189 for (c = 0; c < ir->operands[0]->type->components(); c++) {
190 switch (type->base_type) {
191 case GLSL_TYPE_UINT:
192 u[c] = -op[0]->value.u[c];
193 break;
194 case GLSL_TYPE_INT:
195 i[c] = -op[0]->value.i[c];
196 break;
197 case GLSL_TYPE_FLOAT:
198 f[c] = -op[0]->value.f[c];
199 break;
200 default:
201 assert(0);
202 }
203 }
204 break;
205
206 case ir_unop_abs:
207 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
208 type = ir->type;
209 for (c = 0; c < ir->operands[0]->type->components(); c++) {
210 switch (type->base_type) {
211 case GLSL_TYPE_UINT:
212 u[c] = op[0]->value.u[c];
213 break;
214 case GLSL_TYPE_INT:
215 i[c] = op[0]->value.i[c];
216 if (i[c] < 0)
217 i[c] = -i[c];
218 break;
219 case GLSL_TYPE_FLOAT:
220 f[c] = fabs(op[0]->value.f[c]);
221 break;
222 default:
223 assert(0);
224 }
225 }
226 break;
227
228 case ir_unop_rcp:
229 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
230 type = ir->type;
231 for (c = 0; c < ir->operands[0]->type->components(); c++) {
232 switch (type->base_type) {
233 case GLSL_TYPE_UINT:
234 if (op[0]->value.u[c] != 0.0)
235 u[c] = 1 / op[0]->value.u[c];
236 break;
237 case GLSL_TYPE_INT:
238 if (op[0]->value.i[c] != 0.0)
239 i[c] = 1 / op[0]->value.i[c];
240 break;
241 case GLSL_TYPE_FLOAT:
242 if (op[0]->value.f[c] != 0.0)
243 f[c] = 1.0 / op[0]->value.f[c];
244 break;
245 default:
246 assert(0);
247 }
248 }
249 break;
250
251 case ir_unop_rsq:
252 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
253 type = ir->type;
254 for (c = 0; c < ir->operands[0]->type->components(); c++) {
255 f[c] = 1.0 / sqrtf(op[0]->value.f[c]);
256 }
257 break;
258
259 case ir_unop_sqrt:
260 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
261 type = ir->type;
262 for (c = 0; c < ir->operands[0]->type->components(); c++) {
263 f[c] = sqrtf(op[0]->value.f[c]);
264 }
265 break;
266
267 case ir_unop_exp:
268 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
269 type = ir->type;
270 for (c = 0; c < ir->operands[0]->type->components(); c++) {
271 f[c] = expf(op[0]->value.f[c]);
272 }
273 break;
274
275 case ir_unop_log:
276 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
277 type = ir->type;
278 for (c = 0; c < ir->operands[0]->type->components(); c++) {
279 f[c] = logf(op[0]->value.f[c]);
280 }
281 break;
282
Kenneth Graunked6a32d42010-06-09 15:22:35 -0700283 case ir_unop_dFdx:
284 case ir_unop_dFdy:
285 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
286 type = ir->type;
287 for (c = 0; c < ir->operands[0]->type->components(); c++) {
288 f[c] = 0.0;
289 }
290 break;
291
Eric Anholtd251b922010-04-01 18:35:42 -1000292 case ir_binop_add:
293 if (ir->operands[0]->type == ir->operands[1]->type) {
294 type = ir->operands[0]->type;
295 for (c = 0; c < ir->operands[0]->type->components(); c++) {
296 switch (ir->operands[0]->type->base_type) {
297 case GLSL_TYPE_UINT:
298 u[c] = op[0]->value.u[c] + op[1]->value.u[c];
299 break;
300 case GLSL_TYPE_INT:
301 i[c] = op[0]->value.i[c] + op[1]->value.i[c];
302 break;
303 case GLSL_TYPE_FLOAT:
304 f[c] = op[0]->value.f[c] + op[1]->value.f[c];
305 break;
306 default:
307 assert(0);
308 }
309 }
310 }
311 break;
312 case ir_binop_sub:
313 if (ir->operands[0]->type == ir->operands[1]->type) {
314 type = ir->operands[0]->type;
315 for (c = 0; c < ir->operands[0]->type->components(); c++) {
316 switch (ir->operands[0]->type->base_type) {
317 case GLSL_TYPE_UINT:
318 u[c] = op[0]->value.u[c] - op[1]->value.u[c];
319 break;
320 case GLSL_TYPE_INT:
321 i[c] = op[0]->value.i[c] - op[1]->value.i[c];
322 break;
323 case GLSL_TYPE_FLOAT:
324 f[c] = op[0]->value.f[c] - op[1]->value.f[c];
325 break;
326 default:
327 assert(0);
328 }
329 }
330 }
331 break;
Eric Anholta576f9d2010-03-31 16:25:12 -1000332 case ir_binop_mul:
Eric Anholtd98da972010-04-01 18:25:11 -1000333 if (ir->operands[0]->type == ir->operands[1]->type &&
334 !ir->operands[0]->type->is_matrix()) {
335 type = ir->operands[0]->type;
336 for (c = 0; c < ir->operands[0]->type->components(); c++) {
337 switch (ir->operands[0]->type->base_type) {
338 case GLSL_TYPE_UINT:
339 u[c] = op[0]->value.u[c] * op[1]->value.u[c];
340 break;
341 case GLSL_TYPE_INT:
342 i[c] = op[0]->value.i[c] * op[1]->value.i[c];
343 break;
344 case GLSL_TYPE_FLOAT:
345 f[c] = op[0]->value.f[c] * op[1]->value.f[c];
346 break;
347 default:
348 assert(0);
349 }
Eric Anholta576f9d2010-03-31 16:25:12 -1000350 }
351 }
352 break;
Eric Anholtd251b922010-04-01 18:35:42 -1000353 case ir_binop_div:
354 if (ir->operands[0]->type == ir->operands[1]->type) {
355 type = ir->operands[0]->type;
356 for (c = 0; c < ir->operands[0]->type->components(); c++) {
357 switch (ir->operands[0]->type->base_type) {
358 case GLSL_TYPE_UINT:
359 u[c] = op[0]->value.u[c] / op[1]->value.u[c];
360 break;
361 case GLSL_TYPE_INT:
362 i[c] = op[0]->value.i[c] / op[1]->value.i[c];
363 break;
364 case GLSL_TYPE_FLOAT:
365 f[c] = op[0]->value.f[c] / op[1]->value.f[c];
366 break;
367 default:
368 assert(0);
369 }
370 }
371 }
372 break;
Eric Anholta576f9d2010-03-31 16:25:12 -1000373 case ir_binop_logic_and:
Eric Anholtd98da972010-04-01 18:25:11 -1000374 type = ir->operands[0]->type;
375 assert(type->base_type == GLSL_TYPE_BOOL);
376 for (c = 0; c < ir->operands[0]->type->components(); c++)
377 b[c] = op[0]->value.b[c] && op[1]->value.b[c];
Eric Anholta576f9d2010-03-31 16:25:12 -1000378 break;
Eric Anholtd251b922010-04-01 18:35:42 -1000379 case ir_binop_logic_xor:
380 type = ir->operands[0]->type;
381 assert(type->base_type == GLSL_TYPE_BOOL);
382 for (c = 0; c < ir->operands[0]->type->components(); c++)
383 b[c] = op[0]->value.b[c] ^ op[1]->value.b[c];
384 break;
Eric Anholta576f9d2010-03-31 16:25:12 -1000385 case ir_binop_logic_or:
Eric Anholtd98da972010-04-01 18:25:11 -1000386 type = ir->operands[0]->type;
387 assert(type->base_type == GLSL_TYPE_BOOL);
388 for (c = 0; c < ir->operands[0]->type->components(); c++)
389 b[c] = op[0]->value.b[c] || op[1]->value.b[c];
Eric Anholta576f9d2010-03-31 16:25:12 -1000390 break;
Eric Anholt85171c22010-04-06 09:55:45 -0700391
392 case ir_binop_less:
393 type = glsl_type::bool_type;
394 switch (ir->operands[0]->type->base_type) {
395 case GLSL_TYPE_UINT:
396 b[0] = op[0]->value.u[0] < op[1]->value.u[0];
397 break;
398 case GLSL_TYPE_INT:
399 b[0] = op[0]->value.i[0] < op[1]->value.i[0];
400 break;
401 case GLSL_TYPE_FLOAT:
402 b[0] = op[0]->value.f[0] < op[1]->value.f[0];
403 break;
404 default:
405 assert(0);
406 }
407 break;
408 case ir_binop_greater:
409 type = glsl_type::bool_type;
410 switch (ir->operands[0]->type->base_type) {
411 case GLSL_TYPE_UINT:
412 b[0] = op[0]->value.u[0] > op[1]->value.u[0];
413 break;
414 case GLSL_TYPE_INT:
415 b[0] = op[0]->value.i[0] > op[1]->value.i[0];
416 break;
417 case GLSL_TYPE_FLOAT:
418 b[0] = op[0]->value.f[0] > op[1]->value.f[0];
419 break;
420 default:
421 assert(0);
422 }
423 break;
424 case ir_binop_lequal:
425 type = glsl_type::bool_type;
426 switch (ir->operands[0]->type->base_type) {
427 case GLSL_TYPE_UINT:
428 b[0] = op[0]->value.u[0] <= op[1]->value.u[0];
429 break;
430 case GLSL_TYPE_INT:
431 b[0] = op[0]->value.i[0] <= op[1]->value.i[0];
432 break;
433 case GLSL_TYPE_FLOAT:
434 b[0] = op[0]->value.f[0] <= op[1]->value.f[0];
435 break;
436 default:
437 assert(0);
438 }
439 break;
440 case ir_binop_gequal:
441 type = glsl_type::bool_type;
442 switch (ir->operands[0]->type->base_type) {
443 case GLSL_TYPE_UINT:
444 b[0] = op[0]->value.u[0] >= op[1]->value.u[0];
445 break;
446 case GLSL_TYPE_INT:
447 b[0] = op[0]->value.i[0] >= op[1]->value.i[0];
448 break;
449 case GLSL_TYPE_FLOAT:
450 b[0] = op[0]->value.f[0] >= op[1]->value.f[0];
451 break;
452 default:
453 assert(0);
454 }
455 break;
456
Eric Anholtec1949e2010-04-06 10:02:27 -0700457 case ir_binop_equal:
458 if (ir->operands[0]->type == ir->operands[1]->type) {
459 type = glsl_type::bool_type;
460 b[0] = true;
461 for (c = 0; c < ir->operands[0]->type->components(); c++) {
462 switch (ir->operands[0]->type->base_type) {
463 case GLSL_TYPE_UINT:
464 b[0] = b[0] && op[0]->value.u[c] == op[1]->value.u[c];
465 break;
466 case GLSL_TYPE_INT:
467 b[0] = b[0] && op[0]->value.i[c] == op[1]->value.i[c];
468 break;
469 case GLSL_TYPE_FLOAT:
470 b[0] = b[0] && op[0]->value.f[c] == op[1]->value.f[c];
471 break;
Ian Romanick77cce642010-04-07 16:48:42 -0700472 case GLSL_TYPE_BOOL:
473 b[0] = b[0] && op[0]->value.b[c] == op[1]->value.b[c];
474 break;
Eric Anholtec1949e2010-04-06 10:02:27 -0700475 default:
476 assert(0);
477 }
478 }
479 }
480 break;
481 case ir_binop_nequal:
482 if (ir->operands[0]->type == ir->operands[1]->type) {
483 type = glsl_type::bool_type;
484 b[0] = false;
485 for (c = 0; c < ir->operands[0]->type->components(); c++) {
486 switch (ir->operands[0]->type->base_type) {
487 case GLSL_TYPE_UINT:
488 b[0] = b[0] || op[0]->value.u[c] != op[1]->value.u[c];
489 break;
490 case GLSL_TYPE_INT:
491 b[0] = b[0] || op[0]->value.i[c] != op[1]->value.i[c];
492 break;
493 case GLSL_TYPE_FLOAT:
494 b[0] = b[0] || op[0]->value.f[c] != op[1]->value.f[c];
495 break;
Ian Romanick77cce642010-04-07 16:48:42 -0700496 case GLSL_TYPE_BOOL:
497 b[0] = b[0] || op[0]->value.b[c] != op[1]->value.b[c];
498 break;
Eric Anholtec1949e2010-04-06 10:02:27 -0700499 default:
500 assert(0);
501 }
502 }
503 }
504 break;
505
Eric Anholta576f9d2010-03-31 16:25:12 -1000506 default:
507 break;
508 }
Eric Anholtd98da972010-04-01 18:25:11 -1000509
510 if (type) {
511 switch (type->base_type) {
512 case GLSL_TYPE_UINT:
513 value = new ir_constant(type, u);
514 break;
515 case GLSL_TYPE_INT:
516 value = new ir_constant(type, i);
517 break;
518 case GLSL_TYPE_FLOAT:
519 value = new ir_constant(type, f);
520 break;
521 case GLSL_TYPE_BOOL:
522 value = new ir_constant(type, b);
523 break;
524 }
525 }
Ian Romanick1cf43a42010-03-30 16:56:50 -0700526}
527
528
529void
Kenneth Graunke26d74cd2010-05-26 17:42:03 -0700530ir_constant_visitor::visit(ir_texture *ir)
531{
532 // FINISHME: Do stuff with texture lookups
533 (void) ir;
534 value = NULL;
535}
536
537
538void
Ian Romanick1cf43a42010-03-30 16:56:50 -0700539ir_constant_visitor::visit(ir_swizzle *ir)
540{
541 (void) ir;
542 value = NULL;
543}
544
545
546void
Ian Romanickc7b10462010-05-19 13:20:12 +0200547ir_constant_visitor::visit(ir_dereference_variable *ir)
Ian Romanick1cf43a42010-03-30 16:56:50 -0700548{
Ian Romanick1cf43a42010-03-30 16:56:50 -0700549 value = NULL;
Eric Anholt326c6762010-04-06 10:30:54 -0700550
Ian Romanickc7b10462010-05-19 13:20:12 +0200551 ir_variable *var = ir->variable_referenced();
552 if (var && var->constant_value)
553 value = new ir_constant(ir->type, &var->constant_value->value);
554}
555
556
557void
558ir_constant_visitor::visit(ir_dereference_array *ir)
559{
Ian Romanick36ea2862010-05-19 13:52:29 +0200560 (void) ir;
Ian Romanickc7b10462010-05-19 13:20:12 +0200561 value = NULL;
562 /* FINISHME: Other dereference modes. */
563}
564
565
566void
567ir_constant_visitor::visit(ir_dereference_record *ir)
568{
Ian Romanick36ea2862010-05-19 13:52:29 +0200569 (void) ir;
Ian Romanickc7b10462010-05-19 13:20:12 +0200570 value = NULL;
Eric Anholt326c6762010-04-06 10:30:54 -0700571 /* FINISHME: Other dereference modes. */
Ian Romanick1cf43a42010-03-30 16:56:50 -0700572}
573
574
575void
576ir_constant_visitor::visit(ir_assignment *ir)
577{
578 (void) ir;
579 value = NULL;
580}
581
582
583void
584ir_constant_visitor::visit(ir_constant *ir)
585{
586 value = ir;
587}
588
589
590void
591ir_constant_visitor::visit(ir_call *ir)
592{
593 (void) ir;
594 value = NULL;
595}
596
597
598void
599ir_constant_visitor::visit(ir_return *ir)
600{
601 (void) ir;
602 value = NULL;
603}
604
605
606void
607ir_constant_visitor::visit(ir_if *ir)
608{
609 (void) ir;
610 value = NULL;
611}
Ian Romanickfad607a2010-04-05 16:16:07 -0700612
613
614void
615ir_constant_visitor::visit(ir_loop *ir)
616{
617 (void) ir;
618 value = NULL;
619}
Ian Romanickf8e31e02010-04-05 16:28:15 -0700620
621
622void
623ir_constant_visitor::visit(ir_loop_jump *ir)
624{
625 (void) ir;
626 value = NULL;
627}