blob: e89b5bc7685304c56ce5119edacaeb188a869e7e [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;
172
Eric Anholt43ad37a2010-05-12 14:42:21 -0700173 case ir_unop_neg:
174 type = ir->type;
175 for (c = 0; c < ir->operands[0]->type->components(); c++) {
176 switch (type->base_type) {
177 case GLSL_TYPE_UINT:
178 u[c] = -op[0]->value.u[c];
179 break;
180 case GLSL_TYPE_INT:
181 i[c] = -op[0]->value.i[c];
182 break;
183 case GLSL_TYPE_FLOAT:
184 f[c] = -op[0]->value.f[c];
185 break;
186 default:
187 assert(0);
188 }
189 }
190 break;
191
192 case ir_unop_abs:
193 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
194 type = ir->type;
195 for (c = 0; c < ir->operands[0]->type->components(); c++) {
196 switch (type->base_type) {
197 case GLSL_TYPE_UINT:
198 u[c] = op[0]->value.u[c];
199 break;
200 case GLSL_TYPE_INT:
201 i[c] = op[0]->value.i[c];
202 if (i[c] < 0)
203 i[c] = -i[c];
204 break;
205 case GLSL_TYPE_FLOAT:
206 f[c] = fabs(op[0]->value.f[c]);
207 break;
208 default:
209 assert(0);
210 }
211 }
212 break;
213
214 case ir_unop_rcp:
215 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
216 type = ir->type;
217 for (c = 0; c < ir->operands[0]->type->components(); c++) {
218 switch (type->base_type) {
219 case GLSL_TYPE_UINT:
220 if (op[0]->value.u[c] != 0.0)
221 u[c] = 1 / op[0]->value.u[c];
222 break;
223 case GLSL_TYPE_INT:
224 if (op[0]->value.i[c] != 0.0)
225 i[c] = 1 / op[0]->value.i[c];
226 break;
227 case GLSL_TYPE_FLOAT:
228 if (op[0]->value.f[c] != 0.0)
229 f[c] = 1.0 / op[0]->value.f[c];
230 break;
231 default:
232 assert(0);
233 }
234 }
235 break;
236
237 case ir_unop_rsq:
238 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
239 type = ir->type;
240 for (c = 0; c < ir->operands[0]->type->components(); c++) {
241 f[c] = 1.0 / sqrtf(op[0]->value.f[c]);
242 }
243 break;
244
245 case ir_unop_sqrt:
246 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
247 type = ir->type;
248 for (c = 0; c < ir->operands[0]->type->components(); c++) {
249 f[c] = sqrtf(op[0]->value.f[c]);
250 }
251 break;
252
253 case ir_unop_exp:
254 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
255 type = ir->type;
256 for (c = 0; c < ir->operands[0]->type->components(); c++) {
257 f[c] = expf(op[0]->value.f[c]);
258 }
259 break;
260
261 case ir_unop_log:
262 assert(op[0]->type->base_type == GLSL_TYPE_FLOAT);
263 type = ir->type;
264 for (c = 0; c < ir->operands[0]->type->components(); c++) {
265 f[c] = logf(op[0]->value.f[c]);
266 }
267 break;
268
Eric Anholtd251b922010-04-01 18:35:42 -1000269 case ir_binop_add:
270 if (ir->operands[0]->type == ir->operands[1]->type) {
271 type = ir->operands[0]->type;
272 for (c = 0; c < ir->operands[0]->type->components(); c++) {
273 switch (ir->operands[0]->type->base_type) {
274 case GLSL_TYPE_UINT:
275 u[c] = op[0]->value.u[c] + op[1]->value.u[c];
276 break;
277 case GLSL_TYPE_INT:
278 i[c] = op[0]->value.i[c] + op[1]->value.i[c];
279 break;
280 case GLSL_TYPE_FLOAT:
281 f[c] = op[0]->value.f[c] + op[1]->value.f[c];
282 break;
283 default:
284 assert(0);
285 }
286 }
287 }
288 break;
289 case ir_binop_sub:
290 if (ir->operands[0]->type == ir->operands[1]->type) {
291 type = ir->operands[0]->type;
292 for (c = 0; c < ir->operands[0]->type->components(); c++) {
293 switch (ir->operands[0]->type->base_type) {
294 case GLSL_TYPE_UINT:
295 u[c] = op[0]->value.u[c] - op[1]->value.u[c];
296 break;
297 case GLSL_TYPE_INT:
298 i[c] = op[0]->value.i[c] - op[1]->value.i[c];
299 break;
300 case GLSL_TYPE_FLOAT:
301 f[c] = op[0]->value.f[c] - op[1]->value.f[c];
302 break;
303 default:
304 assert(0);
305 }
306 }
307 }
308 break;
Eric Anholta576f9d2010-03-31 16:25:12 -1000309 case ir_binop_mul:
Eric Anholtd98da972010-04-01 18:25:11 -1000310 if (ir->operands[0]->type == ir->operands[1]->type &&
311 !ir->operands[0]->type->is_matrix()) {
312 type = ir->operands[0]->type;
313 for (c = 0; c < ir->operands[0]->type->components(); c++) {
314 switch (ir->operands[0]->type->base_type) {
315 case GLSL_TYPE_UINT:
316 u[c] = op[0]->value.u[c] * op[1]->value.u[c];
317 break;
318 case GLSL_TYPE_INT:
319 i[c] = op[0]->value.i[c] * op[1]->value.i[c];
320 break;
321 case GLSL_TYPE_FLOAT:
322 f[c] = op[0]->value.f[c] * op[1]->value.f[c];
323 break;
324 default:
325 assert(0);
326 }
Eric Anholta576f9d2010-03-31 16:25:12 -1000327 }
328 }
329 break;
Eric Anholtd251b922010-04-01 18:35:42 -1000330 case ir_binop_div:
331 if (ir->operands[0]->type == ir->operands[1]->type) {
332 type = ir->operands[0]->type;
333 for (c = 0; c < ir->operands[0]->type->components(); c++) {
334 switch (ir->operands[0]->type->base_type) {
335 case GLSL_TYPE_UINT:
336 u[c] = op[0]->value.u[c] / op[1]->value.u[c];
337 break;
338 case GLSL_TYPE_INT:
339 i[c] = op[0]->value.i[c] / op[1]->value.i[c];
340 break;
341 case GLSL_TYPE_FLOAT:
342 f[c] = op[0]->value.f[c] / op[1]->value.f[c];
343 break;
344 default:
345 assert(0);
346 }
347 }
348 }
349 break;
Eric Anholta576f9d2010-03-31 16:25:12 -1000350 case ir_binop_logic_and:
Eric Anholtd98da972010-04-01 18:25:11 -1000351 type = ir->operands[0]->type;
352 assert(type->base_type == GLSL_TYPE_BOOL);
353 for (c = 0; c < ir->operands[0]->type->components(); c++)
354 b[c] = op[0]->value.b[c] && op[1]->value.b[c];
Eric Anholta576f9d2010-03-31 16:25:12 -1000355 break;
Eric Anholtd251b922010-04-01 18:35:42 -1000356 case ir_binop_logic_xor:
357 type = ir->operands[0]->type;
358 assert(type->base_type == GLSL_TYPE_BOOL);
359 for (c = 0; c < ir->operands[0]->type->components(); c++)
360 b[c] = op[0]->value.b[c] ^ op[1]->value.b[c];
361 break;
Eric Anholta576f9d2010-03-31 16:25:12 -1000362 case ir_binop_logic_or:
Eric Anholtd98da972010-04-01 18:25:11 -1000363 type = ir->operands[0]->type;
364 assert(type->base_type == GLSL_TYPE_BOOL);
365 for (c = 0; c < ir->operands[0]->type->components(); c++)
366 b[c] = op[0]->value.b[c] || op[1]->value.b[c];
Eric Anholta576f9d2010-03-31 16:25:12 -1000367 break;
Eric Anholt85171c22010-04-06 09:55:45 -0700368
369 case ir_binop_less:
370 type = glsl_type::bool_type;
371 switch (ir->operands[0]->type->base_type) {
372 case GLSL_TYPE_UINT:
373 b[0] = op[0]->value.u[0] < op[1]->value.u[0];
374 break;
375 case GLSL_TYPE_INT:
376 b[0] = op[0]->value.i[0] < op[1]->value.i[0];
377 break;
378 case GLSL_TYPE_FLOAT:
379 b[0] = op[0]->value.f[0] < op[1]->value.f[0];
380 break;
381 default:
382 assert(0);
383 }
384 break;
385 case ir_binop_greater:
386 type = glsl_type::bool_type;
387 switch (ir->operands[0]->type->base_type) {
388 case GLSL_TYPE_UINT:
389 b[0] = op[0]->value.u[0] > op[1]->value.u[0];
390 break;
391 case GLSL_TYPE_INT:
392 b[0] = op[0]->value.i[0] > op[1]->value.i[0];
393 break;
394 case GLSL_TYPE_FLOAT:
395 b[0] = op[0]->value.f[0] > op[1]->value.f[0];
396 break;
397 default:
398 assert(0);
399 }
400 break;
401 case ir_binop_lequal:
402 type = glsl_type::bool_type;
403 switch (ir->operands[0]->type->base_type) {
404 case GLSL_TYPE_UINT:
405 b[0] = op[0]->value.u[0] <= op[1]->value.u[0];
406 break;
407 case GLSL_TYPE_INT:
408 b[0] = op[0]->value.i[0] <= op[1]->value.i[0];
409 break;
410 case GLSL_TYPE_FLOAT:
411 b[0] = op[0]->value.f[0] <= op[1]->value.f[0];
412 break;
413 default:
414 assert(0);
415 }
416 break;
417 case ir_binop_gequal:
418 type = glsl_type::bool_type;
419 switch (ir->operands[0]->type->base_type) {
420 case GLSL_TYPE_UINT:
421 b[0] = op[0]->value.u[0] >= op[1]->value.u[0];
422 break;
423 case GLSL_TYPE_INT:
424 b[0] = op[0]->value.i[0] >= op[1]->value.i[0];
425 break;
426 case GLSL_TYPE_FLOAT:
427 b[0] = op[0]->value.f[0] >= op[1]->value.f[0];
428 break;
429 default:
430 assert(0);
431 }
432 break;
433
Eric Anholtec1949e2010-04-06 10:02:27 -0700434 case ir_binop_equal:
435 if (ir->operands[0]->type == ir->operands[1]->type) {
436 type = glsl_type::bool_type;
437 b[0] = true;
438 for (c = 0; c < ir->operands[0]->type->components(); c++) {
439 switch (ir->operands[0]->type->base_type) {
440 case GLSL_TYPE_UINT:
441 b[0] = b[0] && op[0]->value.u[c] == op[1]->value.u[c];
442 break;
443 case GLSL_TYPE_INT:
444 b[0] = b[0] && op[0]->value.i[c] == op[1]->value.i[c];
445 break;
446 case GLSL_TYPE_FLOAT:
447 b[0] = b[0] && op[0]->value.f[c] == op[1]->value.f[c];
448 break;
Ian Romanick77cce642010-04-07 16:48:42 -0700449 case GLSL_TYPE_BOOL:
450 b[0] = b[0] && op[0]->value.b[c] == op[1]->value.b[c];
451 break;
Eric Anholtec1949e2010-04-06 10:02:27 -0700452 default:
453 assert(0);
454 }
455 }
456 }
457 break;
458 case ir_binop_nequal:
459 if (ir->operands[0]->type == ir->operands[1]->type) {
460 type = glsl_type::bool_type;
461 b[0] = false;
462 for (c = 0; c < ir->operands[0]->type->components(); c++) {
463 switch (ir->operands[0]->type->base_type) {
464 case GLSL_TYPE_UINT:
465 b[0] = b[0] || op[0]->value.u[c] != op[1]->value.u[c];
466 break;
467 case GLSL_TYPE_INT:
468 b[0] = b[0] || op[0]->value.i[c] != op[1]->value.i[c];
469 break;
470 case GLSL_TYPE_FLOAT:
471 b[0] = b[0] || op[0]->value.f[c] != op[1]->value.f[c];
472 break;
Ian Romanick77cce642010-04-07 16:48:42 -0700473 case GLSL_TYPE_BOOL:
474 b[0] = b[0] || op[0]->value.b[c] != op[1]->value.b[c];
475 break;
Eric Anholtec1949e2010-04-06 10:02:27 -0700476 default:
477 assert(0);
478 }
479 }
480 }
481 break;
482
Eric Anholta576f9d2010-03-31 16:25:12 -1000483 default:
484 break;
485 }
Eric Anholtd98da972010-04-01 18:25:11 -1000486
487 if (type) {
488 switch (type->base_type) {
489 case GLSL_TYPE_UINT:
490 value = new ir_constant(type, u);
491 break;
492 case GLSL_TYPE_INT:
493 value = new ir_constant(type, i);
494 break;
495 case GLSL_TYPE_FLOAT:
496 value = new ir_constant(type, f);
497 break;
498 case GLSL_TYPE_BOOL:
499 value = new ir_constant(type, b);
500 break;
501 }
502 }
Ian Romanick1cf43a42010-03-30 16:56:50 -0700503}
504
505
506void
Kenneth Graunke26d74cd2010-05-26 17:42:03 -0700507ir_constant_visitor::visit(ir_texture *ir)
508{
509 // FINISHME: Do stuff with texture lookups
510 (void) ir;
511 value = NULL;
512}
513
514
515void
Ian Romanick1cf43a42010-03-30 16:56:50 -0700516ir_constant_visitor::visit(ir_swizzle *ir)
517{
518 (void) ir;
519 value = NULL;
520}
521
522
523void
Ian Romanickc7b10462010-05-19 13:20:12 +0200524ir_constant_visitor::visit(ir_dereference_variable *ir)
Ian Romanick1cf43a42010-03-30 16:56:50 -0700525{
Ian Romanick1cf43a42010-03-30 16:56:50 -0700526 value = NULL;
Eric Anholt326c6762010-04-06 10:30:54 -0700527
Ian Romanickc7b10462010-05-19 13:20:12 +0200528 ir_variable *var = ir->variable_referenced();
529 if (var && var->constant_value)
530 value = new ir_constant(ir->type, &var->constant_value->value);
531}
532
533
534void
535ir_constant_visitor::visit(ir_dereference_array *ir)
536{
Ian Romanick36ea2862010-05-19 13:52:29 +0200537 (void) ir;
Ian Romanickc7b10462010-05-19 13:20:12 +0200538 value = NULL;
539 /* FINISHME: Other dereference modes. */
540}
541
542
543void
544ir_constant_visitor::visit(ir_dereference_record *ir)
545{
Ian Romanick36ea2862010-05-19 13:52:29 +0200546 (void) ir;
Ian Romanickc7b10462010-05-19 13:20:12 +0200547 value = NULL;
Eric Anholt326c6762010-04-06 10:30:54 -0700548 /* FINISHME: Other dereference modes. */
Ian Romanick1cf43a42010-03-30 16:56:50 -0700549}
550
551
552void
553ir_constant_visitor::visit(ir_assignment *ir)
554{
555 (void) ir;
556 value = NULL;
557}
558
559
560void
561ir_constant_visitor::visit(ir_constant *ir)
562{
563 value = ir;
564}
565
566
567void
568ir_constant_visitor::visit(ir_call *ir)
569{
570 (void) ir;
571 value = NULL;
572}
573
574
575void
576ir_constant_visitor::visit(ir_return *ir)
577{
578 (void) ir;
579 value = NULL;
580}
581
582
583void
584ir_constant_visitor::visit(ir_if *ir)
585{
586 (void) ir;
587 value = NULL;
588}
Ian Romanickfad607a2010-04-05 16:16:07 -0700589
590
591void
592ir_constant_visitor::visit(ir_loop *ir)
593{
594 (void) ir;
595 value = NULL;
596}
Ian Romanickf8e31e02010-04-05 16:28:15 -0700597
598
599void
600ir_constant_visitor::visit(ir_loop_jump *ir)
601{
602 (void) ir;
603 value = NULL;
604}