OSDN Git Service

glsl: Refactor handling of ast_array_index to a separate function
[android-x86/external-mesa.git] / src / glsl / ir_validate.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_validate.cpp
26  *
27  * Attempts to verify that various invariants of the IR tree are true.
28  *
29  * In particular, at the moment it makes sure that no single
30  * ir_instruction node except for ir_variable appears multiple times
31  * in the ir tree.  ir_variable does appear multiple times: Once as a
32  * declaration in an exec_list, and multiple times as the endpoint of
33  * a dereference chain.
34  */
35
36 #include "ir.h"
37 #include "ir_hierarchical_visitor.h"
38 #include "program/hash_table.h"
39 #include "glsl_types.h"
40
41 class ir_validate : public ir_hierarchical_visitor {
42 public:
43    ir_validate()
44    {
45       this->ht = hash_table_ctor(0, hash_table_pointer_hash,
46                                  hash_table_pointer_compare);
47
48       this->current_function = NULL;
49
50       this->callback = ir_validate::validate_ir;
51       this->data = ht;
52    }
53
54    ~ir_validate()
55    {
56       hash_table_dtor(this->ht);
57    }
58
59    virtual ir_visitor_status visit(ir_variable *v);
60    virtual ir_visitor_status visit(ir_dereference_variable *ir);
61
62    virtual ir_visitor_status visit_enter(ir_if *ir);
63
64    virtual ir_visitor_status visit_leave(ir_loop *ir);
65    virtual ir_visitor_status visit_enter(ir_function *ir);
66    virtual ir_visitor_status visit_leave(ir_function *ir);
67    virtual ir_visitor_status visit_enter(ir_function_signature *ir);
68
69    virtual ir_visitor_status visit_leave(ir_expression *ir);
70    virtual ir_visitor_status visit_leave(ir_swizzle *ir);
71
72    virtual ir_visitor_status visit_enter(ir_assignment *ir);
73    virtual ir_visitor_status visit_enter(ir_call *ir);
74
75    static void validate_ir(ir_instruction *ir, void *data);
76
77    ir_function *current_function;
78
79    struct hash_table *ht;
80 };
81
82
83 ir_visitor_status
84 ir_validate::visit(ir_dereference_variable *ir)
85 {
86    if ((ir->var == NULL) || (ir->var->as_variable() == NULL)) {
87       printf("ir_dereference_variable @ %p does not specify a variable %p\n",
88              (void *) ir, (void *) ir->var);
89       abort();
90    }
91
92    if (hash_table_find(ht, ir->var) == NULL) {
93       printf("ir_dereference_variable @ %p specifies undeclared variable "
94              "`%s' @ %p\n",
95              (void *) ir, ir->var->name, (void *) ir->var);
96       abort();
97    }
98
99    this->validate_ir(ir, this->data);
100
101    return visit_continue;
102 }
103
104 ir_visitor_status
105 ir_validate::visit_enter(ir_if *ir)
106 {
107    if (ir->condition->type != glsl_type::bool_type) {
108       printf("ir_if condition %s type instead of bool.\n",
109              ir->condition->type->name);
110       ir->print();
111       printf("\n");
112       abort();
113    }
114
115    return visit_continue;
116 }
117
118
119 ir_visitor_status
120 ir_validate::visit_leave(ir_loop *ir)
121 {
122    if (ir->counter != NULL) {
123       if ((ir->from == NULL) || (ir->to == NULL) || (ir->increment == NULL)) {
124          printf("ir_loop has invalid loop controls:\n"
125                 "    counter:   %p\n"
126                 "    from:      %p\n"
127                 "    to:        %p\n"
128                 "    increment: %p\n",
129                 (void *) ir->counter, (void *) ir->from, (void *) ir->to,
130                 (void *) ir->increment);
131          abort();
132       }
133
134       if ((ir->cmp < ir_binop_less) || (ir->cmp > ir_binop_nequal)) {
135          printf("ir_loop has invalid comparitor %d\n", ir->cmp);
136          abort();
137       }
138    } else {
139       if ((ir->from != NULL) || (ir->to != NULL) || (ir->increment != NULL)) {
140          printf("ir_loop has invalid loop controls:\n"
141                 "    counter:   %p\n"
142                 "    from:      %p\n"
143                 "    to:        %p\n"
144                 "    increment: %p\n",
145                 (void *) ir->counter, (void *) ir->from, (void *) ir->to,
146                 (void *) ir->increment);
147          abort();
148       }
149    }
150
151    return visit_continue;
152 }
153
154
155 ir_visitor_status
156 ir_validate::visit_enter(ir_function *ir)
157 {
158    /* Function definitions cannot be nested.
159     */
160    if (this->current_function != NULL) {
161       printf("Function definition nested inside another function "
162              "definition:\n");
163       printf("%s %p inside %s %p\n",
164              ir->name, (void *) ir,
165              this->current_function->name, (void *) this->current_function);
166       abort();
167    }
168
169    /* Store the current function hierarchy being traversed.  This is used
170     * by the function signature visitor to ensure that the signatures are
171     * linked with the correct functions.
172     */
173    this->current_function = ir;
174
175    this->validate_ir(ir, this->data);
176
177    /* Verify that all of the things stored in the list of signatures are,
178     * in fact, function signatures.
179     */
180    foreach_list(node, &ir->signatures) {
181       ir_instruction *sig = (ir_instruction *) node;
182
183       if (sig->ir_type != ir_type_function_signature) {
184          printf("Non-signature in signature list of function `%s'\n",
185                 ir->name);
186          abort();
187       }
188    }
189
190    return visit_continue;
191 }
192
193 ir_visitor_status
194 ir_validate::visit_leave(ir_function *ir)
195 {
196    assert(ralloc_parent(ir->name) == ir);
197
198    this->current_function = NULL;
199    return visit_continue;
200 }
201
202 ir_visitor_status
203 ir_validate::visit_enter(ir_function_signature *ir)
204 {
205    if (this->current_function != ir->function()) {
206       printf("Function signature nested inside wrong function "
207              "definition:\n");
208       printf("%p inside %s %p instead of %s %p\n",
209              (void *) ir,
210              this->current_function->name, (void *) this->current_function,
211              ir->function_name(), (void *) ir->function());
212       abort();
213    }
214
215    if (ir->return_type == NULL) {
216       printf("Function signature %p for function %s has NULL return type.\n",
217              (void *) ir, ir->function_name());
218       abort();
219    }
220
221    this->validate_ir(ir, this->data);
222
223    return visit_continue;
224 }
225
226 ir_visitor_status
227 ir_validate::visit_leave(ir_expression *ir)
228 {
229    switch (ir->operation) {
230    case ir_unop_bit_not:
231       assert(ir->operands[0]->type == ir->type);
232       break;
233    case ir_unop_logic_not:
234       assert(ir->type->base_type == GLSL_TYPE_BOOL);
235       assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
236       break;
237
238    case ir_unop_neg:
239    case ir_unop_abs:
240    case ir_unop_sign:
241    case ir_unop_rcp:
242    case ir_unop_rsq:
243    case ir_unop_sqrt:
244       assert(ir->type == ir->operands[0]->type);
245       break;
246
247    case ir_unop_exp:
248    case ir_unop_log:
249    case ir_unop_exp2:
250    case ir_unop_log2:
251       assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
252       assert(ir->type == ir->operands[0]->type);
253       break;
254
255    case ir_unop_f2i:
256       assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
257       assert(ir->type->base_type == GLSL_TYPE_INT);
258       break;
259    case ir_unop_f2u:
260       assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
261       assert(ir->type->base_type == GLSL_TYPE_UINT);
262       break;
263    case ir_unop_i2f:
264       assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
265       assert(ir->type->base_type == GLSL_TYPE_FLOAT);
266       break;
267    case ir_unop_f2b:
268       assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
269       assert(ir->type->base_type == GLSL_TYPE_BOOL);
270       break;
271    case ir_unop_b2f:
272       assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
273       assert(ir->type->base_type == GLSL_TYPE_FLOAT);
274       break;
275    case ir_unop_i2b:
276       assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
277       assert(ir->type->base_type == GLSL_TYPE_BOOL);
278       break;
279    case ir_unop_b2i:
280       assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
281       assert(ir->type->base_type == GLSL_TYPE_INT);
282       break;
283    case ir_unop_u2f:
284       assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
285       assert(ir->type->base_type == GLSL_TYPE_FLOAT);
286       break;
287    case ir_unop_i2u:
288       assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
289       assert(ir->type->base_type == GLSL_TYPE_UINT);
290       break;
291    case ir_unop_u2i:
292       assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
293       assert(ir->type->base_type == GLSL_TYPE_INT);
294       break;
295    case ir_unop_bitcast_i2f:
296       assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
297       assert(ir->type->base_type == GLSL_TYPE_FLOAT);
298       break;
299    case ir_unop_bitcast_f2i:
300       assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
301       assert(ir->type->base_type == GLSL_TYPE_INT);
302       break;
303    case ir_unop_bitcast_u2f:
304       assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
305       assert(ir->type->base_type == GLSL_TYPE_FLOAT);
306       break;
307    case ir_unop_bitcast_f2u:
308       assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
309       assert(ir->type->base_type == GLSL_TYPE_UINT);
310       break;
311
312    case ir_unop_any:
313       assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
314       assert(ir->type == glsl_type::bool_type);
315       break;
316
317    case ir_unop_trunc:
318    case ir_unop_round_even:
319    case ir_unop_ceil:
320    case ir_unop_floor:
321    case ir_unop_fract:
322    case ir_unop_sin:
323    case ir_unop_cos:
324    case ir_unop_sin_reduced:
325    case ir_unop_cos_reduced:
326    case ir_unop_dFdx:
327    case ir_unop_dFdy:
328       assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
329       assert(ir->operands[0]->type == ir->type);
330       break;
331
332    case ir_unop_pack_snorm_2x16:
333    case ir_unop_pack_unorm_2x16:
334    case ir_unop_pack_half_2x16:
335       assert(ir->type == glsl_type::uint_type);
336       assert(ir->operands[0]->type == glsl_type::vec2_type);
337       break;
338
339    case ir_unop_pack_snorm_4x8:
340    case ir_unop_pack_unorm_4x8:
341       assert(ir->type == glsl_type::uint_type);
342       assert(ir->operands[0]->type == glsl_type::vec4_type);
343       break;
344
345    case ir_unop_unpack_snorm_2x16:
346    case ir_unop_unpack_unorm_2x16:
347    case ir_unop_unpack_half_2x16:
348       assert(ir->type == glsl_type::vec2_type);
349       assert(ir->operands[0]->type == glsl_type::uint_type);
350       break;
351
352    case ir_unop_unpack_snorm_4x8:
353    case ir_unop_unpack_unorm_4x8:
354       assert(ir->type == glsl_type::vec4_type);
355       assert(ir->operands[0]->type == glsl_type::uint_type);
356       break;
357
358    case ir_unop_unpack_half_2x16_split_x:
359    case ir_unop_unpack_half_2x16_split_y:
360       assert(ir->type == glsl_type::float_type);
361       assert(ir->operands[0]->type == glsl_type::uint_type);
362       break;
363
364    case ir_unop_noise:
365       /* XXX what can we assert here? */
366       break;
367
368    case ir_binop_add:
369    case ir_binop_sub:
370    case ir_binop_mul:
371    case ir_binop_div:
372    case ir_binop_mod:
373    case ir_binop_min:
374    case ir_binop_max:
375    case ir_binop_pow:
376       if (ir->operands[0]->type->is_scalar())
377          assert(ir->operands[1]->type == ir->type);
378       else if (ir->operands[1]->type->is_scalar())
379          assert(ir->operands[0]->type == ir->type);
380       else if (ir->operands[0]->type->is_vector() &&
381                ir->operands[1]->type->is_vector()) {
382          assert(ir->operands[0]->type == ir->operands[1]->type);
383          assert(ir->operands[0]->type == ir->type);
384       }
385       break;
386
387    case ir_binop_less:
388    case ir_binop_greater:
389    case ir_binop_lequal:
390    case ir_binop_gequal:
391    case ir_binop_equal:
392    case ir_binop_nequal:
393       /* The semantics of the IR operators differ from the GLSL <, >, <=, >=,
394        * ==, and != operators.  The IR operators perform a component-wise
395        * comparison on scalar or vector types and return a boolean scalar or
396        * vector type of the same size.
397        */
398       assert(ir->type->base_type == GLSL_TYPE_BOOL);
399       assert(ir->operands[0]->type == ir->operands[1]->type);
400       assert(ir->operands[0]->type->is_vector()
401              || ir->operands[0]->type->is_scalar());
402       assert(ir->operands[0]->type->vector_elements
403              == ir->type->vector_elements);
404       break;
405
406    case ir_binop_all_equal:
407    case ir_binop_any_nequal:
408       /* GLSL == and != operate on scalars, vectors, matrices and arrays, and
409        * return a scalar boolean.  The IR matches that.
410        */
411       assert(ir->type == glsl_type::bool_type);
412       assert(ir->operands[0]->type == ir->operands[1]->type);
413       break;
414
415    case ir_binop_lshift:
416    case ir_binop_rshift:
417       assert(ir->operands[0]->type->is_integer() &&
418              ir->operands[1]->type->is_integer());
419       if (ir->operands[0]->type->is_scalar()) {
420           assert(ir->operands[1]->type->is_scalar());
421       }
422       if (ir->operands[0]->type->is_vector() &&
423           ir->operands[1]->type->is_vector()) {
424           assert(ir->operands[0]->type->components() ==
425                  ir->operands[1]->type->components());
426       }
427       assert(ir->type == ir->operands[0]->type);
428       break;
429
430    case ir_binop_bit_and:
431    case ir_binop_bit_xor:
432    case ir_binop_bit_or:
433        assert(ir->operands[0]->type->base_type ==
434               ir->operands[1]->type->base_type);
435        assert(ir->type->is_integer());
436        if (ir->operands[0]->type->is_vector() &&
437            ir->operands[1]->type->is_vector()) {
438            assert(ir->operands[0]->type->vector_elements ==
439                   ir->operands[1]->type->vector_elements);
440        }
441        break;
442
443    case ir_binop_logic_and:
444    case ir_binop_logic_xor:
445    case ir_binop_logic_or:
446       assert(ir->type == glsl_type::bool_type);
447       assert(ir->operands[0]->type == glsl_type::bool_type);
448       assert(ir->operands[1]->type == glsl_type::bool_type);
449       break;
450
451    case ir_binop_dot:
452       assert(ir->type == glsl_type::float_type);
453       assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
454       assert(ir->operands[0]->type->is_vector());
455       assert(ir->operands[0]->type == ir->operands[1]->type);
456       break;
457
458    case ir_binop_pack_half_2x16_split:
459       assert(ir->type == glsl_type::uint_type);
460       assert(ir->operands[0]->type == glsl_type::float_type);
461       assert(ir->operands[1]->type == glsl_type::float_type);
462       break;
463
464    case ir_binop_ubo_load:
465       assert(ir->operands[0]->as_constant());
466       assert(ir->operands[0]->type == glsl_type::uint_type);
467
468       assert(ir->operands[1]->type == glsl_type::uint_type);
469       break;
470
471    case ir_triop_lrp:
472       assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
473       assert(ir->operands[0]->type == ir->operands[1]->type);
474       assert(ir->operands[2]->type == ir->operands[0]->type || ir->operands[2]->type == glsl_type::float_type);
475       break;
476
477    case ir_quadop_vector:
478       /* The vector operator collects some number of scalars and generates a
479        * vector from them.
480        *
481        *  - All of the operands must be scalar.
482        *  - Number of operands must matche the size of the resulting vector.
483        *  - Base type of the operands must match the base type of the result.
484        */
485       assert(ir->type->is_vector());
486       switch (ir->type->vector_elements) {
487       case 2:
488          assert(ir->operands[0]->type->is_scalar());
489          assert(ir->operands[0]->type->base_type == ir->type->base_type);
490          assert(ir->operands[1]->type->is_scalar());
491          assert(ir->operands[1]->type->base_type == ir->type->base_type);
492          assert(ir->operands[2] == NULL);
493          assert(ir->operands[3] == NULL);
494          break;
495       case 3:
496          assert(ir->operands[0]->type->is_scalar());
497          assert(ir->operands[0]->type->base_type == ir->type->base_type);
498          assert(ir->operands[1]->type->is_scalar());
499          assert(ir->operands[1]->type->base_type == ir->type->base_type);
500          assert(ir->operands[2]->type->is_scalar());
501          assert(ir->operands[2]->type->base_type == ir->type->base_type);
502          assert(ir->operands[3] == NULL);
503          break;
504       case 4:
505          assert(ir->operands[0]->type->is_scalar());
506          assert(ir->operands[0]->type->base_type == ir->type->base_type);
507          assert(ir->operands[1]->type->is_scalar());
508          assert(ir->operands[1]->type->base_type == ir->type->base_type);
509          assert(ir->operands[2]->type->is_scalar());
510          assert(ir->operands[2]->type->base_type == ir->type->base_type);
511          assert(ir->operands[3]->type->is_scalar());
512          assert(ir->operands[3]->type->base_type == ir->type->base_type);
513          break;
514       default:
515          /* The is_vector assertion above should prevent execution from ever
516           * getting here.
517           */
518          assert(!"Should not get here.");
519          break;
520       }
521    }
522
523    return visit_continue;
524 }
525
526 ir_visitor_status
527 ir_validate::visit_leave(ir_swizzle *ir)
528 {
529    unsigned int chans[4] = {ir->mask.x, ir->mask.y, ir->mask.z, ir->mask.w};
530
531    for (unsigned int i = 0; i < ir->type->vector_elements; i++) {
532       if (chans[i] >= ir->val->type->vector_elements) {
533          printf("ir_swizzle @ %p specifies a channel not present "
534                 "in the value.\n", (void *) ir);
535          ir->print();
536          abort();
537       }
538    }
539
540    return visit_continue;
541 }
542
543 ir_visitor_status
544 ir_validate::visit(ir_variable *ir)
545 {
546    /* An ir_variable is the one thing that can (and will) appear multiple times
547     * in an IR tree.  It is added to the hashtable so that it can be used
548     * in the ir_dereference_variable handler to ensure that a variable is
549     * declared before it is dereferenced.
550     */
551    if (ir->name)
552       assert(ralloc_parent(ir->name) == ir);
553
554    hash_table_insert(ht, ir, ir);
555
556
557    /* If a variable is an array, verify that the maximum array index is in
558     * bounds.  There was once an error in AST-to-HIR conversion that set this
559     * to be out of bounds.
560     */
561    if (ir->type->array_size() > 0) {
562       if (ir->max_array_access >= ir->type->length) {
563          printf("ir_variable has maximum access out of bounds (%d vs %d)\n",
564                 ir->max_array_access, ir->type->length - 1);
565          ir->print();
566          abort();
567       }
568    }
569
570    if (ir->constant_initializer != NULL && !ir->has_initializer) {
571       printf("ir_variable didn't have an initializer, but has a constant "
572              "initializer value.\n");
573       ir->print();
574       abort();
575    }
576
577    return visit_continue;
578 }
579
580 ir_visitor_status
581 ir_validate::visit_enter(ir_assignment *ir)
582 {
583    const ir_dereference *const lhs = ir->lhs;
584    if (lhs->type->is_scalar() || lhs->type->is_vector()) {
585       if (ir->write_mask == 0) {
586          printf("Assignment LHS is %s, but write mask is 0:\n",
587                 lhs->type->is_scalar() ? "scalar" : "vector");
588          ir->print();
589          abort();
590       }
591
592       int lhs_components = 0;
593       for (int i = 0; i < 4; i++) {
594          if (ir->write_mask & (1 << i))
595             lhs_components++;
596       }
597
598       if (lhs_components != ir->rhs->type->vector_elements) {
599          printf("Assignment count of LHS write mask channels enabled not\n"
600                 "matching RHS vector size (%d LHS, %d RHS).\n",
601                 lhs_components, ir->rhs->type->vector_elements);
602          ir->print();
603          abort();
604       }
605    }
606
607    this->validate_ir(ir, this->data);
608
609    return visit_continue;
610 }
611
612 ir_visitor_status
613 ir_validate::visit_enter(ir_call *ir)
614 {
615    ir_function_signature *const callee = ir->callee;
616
617    if (callee->ir_type != ir_type_function_signature) {
618       printf("IR called by ir_call is not ir_function_signature!\n");
619       abort();
620    }
621
622    if (ir->return_deref) {
623       if (ir->return_deref->type != callee->return_type) {
624          printf("callee type %s does not match return storage type %s\n",
625                 callee->return_type->name, ir->return_deref->type->name);
626          abort();
627       }
628    } else if (callee->return_type != glsl_type::void_type) {
629       printf("ir_call has non-void callee but no return storage\n");
630       abort();
631    }
632
633    const exec_node *formal_param_node = callee->parameters.head;
634    const exec_node *actual_param_node = ir->actual_parameters.head;
635    while (true) {
636       if (formal_param_node->is_tail_sentinel()
637           != actual_param_node->is_tail_sentinel()) {
638          printf("ir_call has the wrong number of parameters:\n");
639          goto dump_ir;
640       }
641       if (formal_param_node->is_tail_sentinel()) {
642          break;
643       }
644       const ir_variable *formal_param
645          = (const ir_variable *) formal_param_node;
646       const ir_rvalue *actual_param
647          = (const ir_rvalue *) actual_param_node;
648       if (formal_param->type != actual_param->type) {
649          printf("ir_call parameter type mismatch:\n");
650          goto dump_ir;
651       }
652       if (formal_param->mode == ir_var_function_out
653           || formal_param->mode == ir_var_function_inout) {
654          if (!actual_param->is_lvalue()) {
655             printf("ir_call out/inout parameters must be lvalues:\n");
656             goto dump_ir;
657          }
658       }
659       formal_param_node = formal_param_node->next;
660       actual_param_node = actual_param_node->next;
661    }
662
663    return visit_continue;
664
665 dump_ir:
666    ir->print();
667    printf("callee:\n");
668    callee->print();
669    abort();
670    return visit_stop;
671 }
672
673 void
674 ir_validate::validate_ir(ir_instruction *ir, void *data)
675 {
676    struct hash_table *ht = (struct hash_table *) data;
677
678    if (hash_table_find(ht, ir)) {
679       printf("Instruction node present twice in ir tree:\n");
680       ir->print();
681       printf("\n");
682       abort();
683    }
684    hash_table_insert(ht, ir, ir);
685 }
686
687 void
688 check_node_type(ir_instruction *ir, void *data)
689 {
690    (void) data;
691
692    if (ir->ir_type <= ir_type_unset || ir->ir_type >= ir_type_max) {
693       printf("Instruction node with unset type\n");
694       ir->print(); printf("\n");
695    }
696    ir_rvalue *value = ir->as_rvalue();
697    if (value != NULL)
698       assert(value->type != glsl_type::error_type);
699 }
700
701 void
702 validate_ir_tree(exec_list *instructions)
703 {
704    /* We shouldn't have any reason to validate IR in a release build,
705     * and it's half composed of assert()s anyway which wouldn't do
706     * anything.
707     */
708 #ifdef DEBUG
709    ir_validate v;
710
711    v.run(instructions);
712
713    foreach_iter(exec_list_iterator, iter, *instructions) {
714       ir_instruction *ir = (ir_instruction *)iter.get();
715
716       visit_tree(ir, check_node_type, NULL);
717    }
718 #endif
719 }