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 "util/hash_table.h"
39#include "util/set.h"
40#include "compiler/glsl_types.h"
41
42namespace {
43
44class ir_validate : public ir_hierarchical_visitor {
45public:
46   ir_validate()
47   {
48      this->ir_set = _mesa_set_create(NULL, _mesa_hash_pointer,
49                                      _mesa_key_pointer_equal);
50
51      this->current_function = NULL;
52
53      this->callback_enter = ir_validate::validate_ir;
54      this->data_enter = ir_set;
55   }
56
57   ~ir_validate()
58   {
59      _mesa_set_destroy(this->ir_set, NULL);
60   }
61
62   virtual ir_visitor_status visit(ir_variable *v);
63   virtual ir_visitor_status visit(ir_dereference_variable *ir);
64
65   virtual ir_visitor_status visit_enter(ir_discard *ir);
66   virtual ir_visitor_status visit_enter(ir_if *ir);
67
68   virtual ir_visitor_status visit_enter(ir_function *ir);
69   virtual ir_visitor_status visit_leave(ir_function *ir);
70   virtual ir_visitor_status visit_enter(ir_function_signature *ir);
71
72   virtual ir_visitor_status visit_leave(ir_expression *ir);
73   virtual ir_visitor_status visit_leave(ir_swizzle *ir);
74
75   virtual ir_visitor_status visit_enter(class ir_dereference_array *);
76
77   virtual ir_visitor_status visit_enter(ir_assignment *ir);
78   virtual ir_visitor_status visit_enter(ir_call *ir);
79
80   static void validate_ir(ir_instruction *ir, void *data);
81
82   ir_function *current_function;
83
84   struct set *ir_set;
85};
86
87} /* anonymous namespace */
88
89ir_visitor_status
90ir_validate::visit(ir_dereference_variable *ir)
91{
92   if ((ir->var == NULL) || (ir->var->as_variable() == NULL)) {
93      printf("ir_dereference_variable @ %p does not specify a variable %p\n",
94	     (void *) ir, (void *) ir->var);
95      abort();
96   }
97
98   if (_mesa_set_search(ir_set, ir->var) == NULL) {
99      printf("ir_dereference_variable @ %p specifies undeclared variable "
100	     "`%s' @ %p\n",
101	     (void *) ir, ir->var->name, (void *) ir->var);
102      abort();
103   }
104
105   this->validate_ir(ir, this->data_enter);
106
107   return visit_continue;
108}
109
110ir_visitor_status
111ir_validate::visit_enter(class ir_dereference_array *ir)
112{
113   if (!ir->array->type->is_array() && !ir->array->type->is_matrix() &&
114      !ir->array->type->is_vector()) {
115      printf("ir_dereference_array @ %p does not specify an array, a vector "
116             "or a matrix\n",
117             (void *) ir);
118      ir->print();
119      printf("\n");
120      abort();
121   }
122
123   if (!ir->array_index->type->is_scalar()) {
124      printf("ir_dereference_array @ %p does not have scalar index: %s\n",
125             (void *) ir, ir->array_index->type->name);
126      abort();
127   }
128
129   if (!ir->array_index->type->is_integer()) {
130      printf("ir_dereference_array @ %p does not have integer index: %s\n",
131             (void *) ir, ir->array_index->type->name);
132      abort();
133   }
134
135   return visit_continue;
136}
137
138ir_visitor_status
139ir_validate::visit_enter(ir_discard *ir)
140{
141   if (ir->condition && ir->condition->type != glsl_type::bool_type) {
142      printf("ir_discard condition %s type instead of bool.\n",
143	     ir->condition->type->name);
144      ir->print();
145      printf("\n");
146      abort();
147   }
148
149   return visit_continue;
150}
151
152ir_visitor_status
153ir_validate::visit_enter(ir_if *ir)
154{
155   if (ir->condition->type != glsl_type::bool_type) {
156      printf("ir_if condition %s type instead of bool.\n",
157	     ir->condition->type->name);
158      ir->print();
159      printf("\n");
160      abort();
161   }
162
163   return visit_continue;
164}
165
166
167ir_visitor_status
168ir_validate::visit_enter(ir_function *ir)
169{
170   /* Function definitions cannot be nested.
171    */
172   if (this->current_function != NULL) {
173      printf("Function definition nested inside another function "
174	     "definition:\n");
175      printf("%s %p inside %s %p\n",
176	     ir->name, (void *) ir,
177	     this->current_function->name, (void *) this->current_function);
178      abort();
179   }
180
181   /* Store the current function hierarchy being traversed.  This is used
182    * by the function signature visitor to ensure that the signatures are
183    * linked with the correct functions.
184    */
185   this->current_function = ir;
186
187   this->validate_ir(ir, this->data_enter);
188
189   /* Verify that all of the things stored in the list of signatures are,
190    * in fact, function signatures.
191    */
192   foreach_in_list(ir_instruction, sig, &ir->signatures) {
193      if (sig->ir_type != ir_type_function_signature) {
194	 printf("Non-signature in signature list of function `%s'\n",
195		ir->name);
196	 abort();
197      }
198   }
199
200   return visit_continue;
201}
202
203ir_visitor_status
204ir_validate::visit_leave(ir_function *ir)
205{
206   assert(ralloc_parent(ir->name) == ir);
207
208   this->current_function = NULL;
209   return visit_continue;
210}
211
212ir_visitor_status
213ir_validate::visit_enter(ir_function_signature *ir)
214{
215   if (this->current_function != ir->function()) {
216      printf("Function signature nested inside wrong function "
217	     "definition:\n");
218      printf("%p inside %s %p instead of %s %p\n",
219	     (void *) ir,
220	     this->current_function->name, (void *) this->current_function,
221	     ir->function_name(), (void *) ir->function());
222      abort();
223   }
224
225   if (ir->return_type == NULL) {
226      printf("Function signature %p for function %s has NULL return type.\n",
227	     (void *) ir, ir->function_name());
228      abort();
229   }
230
231   this->validate_ir(ir, this->data_enter);
232
233   return visit_continue;
234}
235
236ir_visitor_status
237ir_validate::visit_leave(ir_expression *ir)
238{
239   switch (ir->operation) {
240   case ir_unop_bit_not:
241      assert(ir->operands[0]->type == ir->type);
242      break;
243   case ir_unop_logic_not:
244      assert(ir->type->base_type == GLSL_TYPE_BOOL);
245      assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
246      break;
247
248   case ir_unop_neg:
249      assert(ir->type == ir->operands[0]->type);
250      break;
251
252   case ir_unop_abs:
253   case ir_unop_sign:
254      assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT ||
255             ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT ||
256             ir->operands[0]->type->base_type == GLSL_TYPE_DOUBLE);
257      assert(ir->type == ir->operands[0]->type);
258      break;
259
260   case ir_unop_rcp:
261   case ir_unop_rsq:
262   case ir_unop_sqrt:
263      assert(ir->type->base_type == GLSL_TYPE_FLOAT ||
264             ir->type->base_type == GLSL_TYPE_DOUBLE);
265      assert(ir->type == ir->operands[0]->type);
266      break;
267
268   case ir_unop_exp:
269   case ir_unop_log:
270   case ir_unop_exp2:
271   case ir_unop_log2:
272   case ir_unop_saturate:
273      assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
274      assert(ir->type == ir->operands[0]->type);
275      break;
276
277   case ir_unop_f2i:
278      assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
279      assert(ir->type->base_type == GLSL_TYPE_INT);
280      break;
281   case ir_unop_f2u:
282      assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
283      assert(ir->type->base_type == GLSL_TYPE_UINT);
284      break;
285   case ir_unop_i2f:
286      assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
287      assert(ir->type->base_type == GLSL_TYPE_FLOAT);
288      break;
289   case ir_unop_f2b:
290      assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
291      assert(ir->type->base_type == GLSL_TYPE_BOOL);
292      break;
293   case ir_unop_b2f:
294      assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
295      assert(ir->type->base_type == GLSL_TYPE_FLOAT);
296      break;
297   case ir_unop_i2b:
298      assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
299      assert(ir->type->base_type == GLSL_TYPE_BOOL);
300      break;
301   case ir_unop_b2i:
302      assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
303      assert(ir->type->base_type == GLSL_TYPE_INT);
304      break;
305   case ir_unop_u2f:
306      assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
307      assert(ir->type->base_type == GLSL_TYPE_FLOAT);
308      break;
309   case ir_unop_i2u:
310      assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
311      assert(ir->type->base_type == GLSL_TYPE_UINT);
312      break;
313   case ir_unop_u2i:
314      assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
315      assert(ir->type->base_type == GLSL_TYPE_INT);
316      break;
317   case ir_unop_bitcast_i2f:
318      assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
319      assert(ir->type->base_type == GLSL_TYPE_FLOAT);
320      break;
321   case ir_unop_bitcast_f2i:
322      assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
323      assert(ir->type->base_type == GLSL_TYPE_INT);
324      break;
325   case ir_unop_bitcast_u2f:
326      assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
327      assert(ir->type->base_type == GLSL_TYPE_FLOAT);
328      break;
329   case ir_unop_bitcast_f2u:
330      assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
331      assert(ir->type->base_type == GLSL_TYPE_UINT);
332      break;
333
334   case ir_unop_trunc:
335   case ir_unop_round_even:
336   case ir_unop_ceil:
337   case ir_unop_floor:
338   case ir_unop_fract:
339      assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT ||
340             ir->operands[0]->type->base_type == GLSL_TYPE_DOUBLE);
341      assert(ir->operands[0]->type == ir->type);
342      break;
343   case ir_unop_sin:
344   case ir_unop_cos:
345   case ir_unop_dFdx:
346   case ir_unop_dFdx_coarse:
347   case ir_unop_dFdx_fine:
348   case ir_unop_dFdy:
349   case ir_unop_dFdy_coarse:
350   case ir_unop_dFdy_fine:
351      assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
352      assert(ir->operands[0]->type == ir->type);
353      break;
354
355   case ir_unop_pack_snorm_2x16:
356   case ir_unop_pack_unorm_2x16:
357   case ir_unop_pack_half_2x16:
358      assert(ir->type == glsl_type::uint_type);
359      assert(ir->operands[0]->type == glsl_type::vec2_type);
360      break;
361
362   case ir_unop_pack_snorm_4x8:
363   case ir_unop_pack_unorm_4x8:
364      assert(ir->type == glsl_type::uint_type);
365      assert(ir->operands[0]->type == glsl_type::vec4_type);
366      break;
367
368   case ir_unop_pack_double_2x32:
369      assert(ir->type == glsl_type::double_type);
370      assert(ir->operands[0]->type == glsl_type::uvec2_type);
371      break;
372
373   case ir_unop_unpack_snorm_2x16:
374   case ir_unop_unpack_unorm_2x16:
375   case ir_unop_unpack_half_2x16:
376      assert(ir->type == glsl_type::vec2_type);
377      assert(ir->operands[0]->type == glsl_type::uint_type);
378      break;
379
380   case ir_unop_unpack_snorm_4x8:
381   case ir_unop_unpack_unorm_4x8:
382      assert(ir->type == glsl_type::vec4_type);
383      assert(ir->operands[0]->type == glsl_type::uint_type);
384      break;
385
386   case ir_unop_unpack_double_2x32:
387      assert(ir->type == glsl_type::uvec2_type);
388      assert(ir->operands[0]->type == glsl_type::double_type);
389      break;
390
391   case ir_unop_bitfield_reverse:
392      assert(ir->operands[0]->type == ir->type);
393      assert(ir->type->is_integer());
394      break;
395
396   case ir_unop_bit_count:
397   case ir_unop_find_msb:
398   case ir_unop_find_lsb:
399      assert(ir->operands[0]->type->vector_elements == ir->type->vector_elements);
400      assert(ir->operands[0]->type->is_integer());
401      assert(ir->type->base_type == GLSL_TYPE_INT);
402      break;
403
404   case ir_unop_noise:
405      /* XXX what can we assert here? */
406      break;
407
408   case ir_unop_interpolate_at_centroid:
409      assert(ir->operands[0]->type == ir->type);
410      assert(ir->operands[0]->type->is_float());
411      break;
412
413   case ir_unop_get_buffer_size:
414      assert(ir->type == glsl_type::int_type);
415      assert(ir->operands[0]->type == glsl_type::uint_type);
416      break;
417
418   case ir_unop_ssbo_unsized_array_length:
419      assert(ir->type == glsl_type::int_type);
420      assert(ir->operands[0]->type->is_array());
421      assert(ir->operands[0]->type->is_unsized_array());
422      break;
423
424   case ir_unop_d2f:
425      assert(ir->operands[0]->type->base_type == GLSL_TYPE_DOUBLE);
426      assert(ir->type->base_type == GLSL_TYPE_FLOAT);
427      break;
428   case ir_unop_f2d:
429      assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT);
430      assert(ir->type->base_type == GLSL_TYPE_DOUBLE);
431      break;
432   case ir_unop_d2i:
433      assert(ir->operands[0]->type->base_type == GLSL_TYPE_DOUBLE);
434      assert(ir->type->base_type == GLSL_TYPE_INT);
435      break;
436   case ir_unop_i2d:
437      assert(ir->operands[0]->type->base_type == GLSL_TYPE_INT);
438      assert(ir->type->base_type == GLSL_TYPE_DOUBLE);
439      break;
440   case ir_unop_d2u:
441      assert(ir->operands[0]->type->base_type == GLSL_TYPE_DOUBLE);
442      assert(ir->type->base_type == GLSL_TYPE_UINT);
443      break;
444   case ir_unop_u2d:
445      assert(ir->operands[0]->type->base_type == GLSL_TYPE_UINT);
446      assert(ir->type->base_type == GLSL_TYPE_DOUBLE);
447      break;
448   case ir_unop_d2b:
449      assert(ir->operands[0]->type->base_type == GLSL_TYPE_DOUBLE);
450      assert(ir->type->base_type == GLSL_TYPE_BOOL);
451      break;
452
453   case ir_unop_frexp_sig:
454      assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT ||
455             ir->operands[0]->type->base_type == GLSL_TYPE_DOUBLE);
456      assert(ir->type->base_type == GLSL_TYPE_DOUBLE);
457      break;
458   case ir_unop_frexp_exp:
459      assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT ||
460             ir->operands[0]->type->base_type == GLSL_TYPE_DOUBLE);
461      assert(ir->type->base_type == GLSL_TYPE_INT);
462      break;
463   case ir_unop_subroutine_to_int:
464      assert(ir->operands[0]->type->base_type == GLSL_TYPE_SUBROUTINE);
465      assert(ir->type->base_type == GLSL_TYPE_INT);
466      break;
467
468   case ir_unop_vote_any:
469   case ir_unop_vote_all:
470   case ir_unop_vote_eq:
471      assert(ir->type == glsl_type::bool_type);
472      assert(ir->operands[0]->type == glsl_type::bool_type);
473      break;
474
475   case ir_binop_add:
476   case ir_binop_sub:
477   case ir_binop_mul:
478   case ir_binop_div:
479   case ir_binop_mod:
480   case ir_binop_min:
481   case ir_binop_max:
482   case ir_binop_pow:
483      assert(ir->operands[0]->type->base_type ==
484             ir->operands[1]->type->base_type);
485
486      if (ir->operands[0]->type->is_scalar())
487	 assert(ir->operands[1]->type == ir->type);
488      else if (ir->operands[1]->type->is_scalar())
489	 assert(ir->operands[0]->type == ir->type);
490      else if (ir->operands[0]->type->is_vector() &&
491	       ir->operands[1]->type->is_vector()) {
492	 assert(ir->operands[0]->type == ir->operands[1]->type);
493	 assert(ir->operands[0]->type == ir->type);
494      }
495      break;
496
497   case ir_binop_imul_high:
498      assert(ir->type == ir->operands[0]->type);
499      assert(ir->type == ir->operands[1]->type);
500      assert(ir->type->is_integer());
501      break;
502
503   case ir_binop_carry:
504   case ir_binop_borrow:
505      assert(ir->type == ir->operands[0]->type);
506      assert(ir->type == ir->operands[1]->type);
507      assert(ir->type->base_type == GLSL_TYPE_UINT);
508      break;
509
510   case ir_binop_less:
511   case ir_binop_greater:
512   case ir_binop_lequal:
513   case ir_binop_gequal:
514   case ir_binop_equal:
515   case ir_binop_nequal:
516      /* The semantics of the IR operators differ from the GLSL <, >, <=, >=,
517       * ==, and != operators.  The IR operators perform a component-wise
518       * comparison on scalar or vector types and return a boolean scalar or
519       * vector type of the same size.
520       */
521      assert(ir->type->base_type == GLSL_TYPE_BOOL);
522      assert(ir->operands[0]->type == ir->operands[1]->type);
523      assert(ir->operands[0]->type->is_vector()
524	     || ir->operands[0]->type->is_scalar());
525      assert(ir->operands[0]->type->vector_elements
526	     == ir->type->vector_elements);
527      break;
528
529   case ir_binop_all_equal:
530   case ir_binop_any_nequal:
531      /* GLSL == and != operate on scalars, vectors, matrices and arrays, and
532       * return a scalar boolean.  The IR matches that.
533       */
534      assert(ir->type == glsl_type::bool_type);
535      assert(ir->operands[0]->type == ir->operands[1]->type);
536      break;
537
538   case ir_binop_lshift:
539   case ir_binop_rshift:
540      assert(ir->operands[0]->type->is_integer() &&
541             ir->operands[1]->type->is_integer());
542      if (ir->operands[0]->type->is_scalar()) {
543          assert(ir->operands[1]->type->is_scalar());
544      }
545      if (ir->operands[0]->type->is_vector() &&
546          ir->operands[1]->type->is_vector()) {
547          assert(ir->operands[0]->type->components() ==
548                 ir->operands[1]->type->components());
549      }
550      assert(ir->type == ir->operands[0]->type);
551      break;
552
553   case ir_binop_bit_and:
554   case ir_binop_bit_xor:
555   case ir_binop_bit_or:
556       assert(ir->operands[0]->type->base_type ==
557              ir->operands[1]->type->base_type);
558       assert(ir->type->is_integer());
559       if (ir->operands[0]->type->is_vector() &&
560           ir->operands[1]->type->is_vector()) {
561           assert(ir->operands[0]->type->vector_elements ==
562                  ir->operands[1]->type->vector_elements);
563       }
564       break;
565
566   case ir_binop_logic_and:
567   case ir_binop_logic_xor:
568   case ir_binop_logic_or:
569      assert(ir->type->base_type == GLSL_TYPE_BOOL);
570      assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
571      assert(ir->operands[1]->type->base_type == GLSL_TYPE_BOOL);
572      break;
573
574   case ir_binop_dot:
575      assert(ir->type == glsl_type::float_type ||
576             ir->type == glsl_type::double_type);
577      assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT ||
578             ir->operands[0]->type->base_type == GLSL_TYPE_DOUBLE);
579      assert(ir->operands[0]->type->is_vector());
580      assert(ir->operands[0]->type == ir->operands[1]->type);
581      break;
582
583   case ir_binop_ubo_load:
584      assert(ir->operands[0]->type == glsl_type::uint_type);
585
586      assert(ir->operands[1]->type == glsl_type::uint_type);
587      break;
588
589   case ir_binop_ldexp:
590      assert(ir->operands[0]->type == ir->type);
591      assert(ir->operands[0]->type->is_float() ||
592             ir->operands[0]->type->is_double());
593      assert(ir->operands[1]->type->base_type == GLSL_TYPE_INT);
594      assert(ir->operands[0]->type->components() ==
595             ir->operands[1]->type->components());
596      break;
597
598   case ir_binop_vector_extract:
599      assert(ir->operands[0]->type->is_vector());
600      assert(ir->operands[1]->type->is_scalar()
601             && ir->operands[1]->type->is_integer());
602      break;
603
604   case ir_binop_interpolate_at_offset:
605      assert(ir->operands[0]->type == ir->type);
606      assert(ir->operands[0]->type->is_float());
607      assert(ir->operands[1]->type->components() == 2);
608      assert(ir->operands[1]->type->is_float());
609      break;
610
611   case ir_binop_interpolate_at_sample:
612      assert(ir->operands[0]->type == ir->type);
613      assert(ir->operands[0]->type->is_float());
614      assert(ir->operands[1]->type == glsl_type::int_type);
615      break;
616
617   case ir_triop_fma:
618      assert(ir->type->base_type == GLSL_TYPE_FLOAT ||
619             ir->type->base_type == GLSL_TYPE_DOUBLE);
620      assert(ir->type == ir->operands[0]->type);
621      assert(ir->type == ir->operands[1]->type);
622      assert(ir->type == ir->operands[2]->type);
623      break;
624
625   case ir_triop_lrp:
626      assert(ir->operands[0]->type->base_type == GLSL_TYPE_FLOAT ||
627             ir->operands[0]->type->base_type == GLSL_TYPE_DOUBLE);
628      assert(ir->operands[0]->type == ir->operands[1]->type);
629      assert(ir->operands[2]->type == ir->operands[0]->type ||
630             ir->operands[2]->type == glsl_type::float_type ||
631             ir->operands[2]->type == glsl_type::double_type);
632      break;
633
634   case ir_triop_csel:
635      assert(ir->operands[0]->type->base_type == GLSL_TYPE_BOOL);
636      assert(ir->type->vector_elements == ir->operands[0]->type->vector_elements);
637      assert(ir->type == ir->operands[1]->type);
638      assert(ir->type == ir->operands[2]->type);
639      break;
640
641   case ir_triop_bitfield_extract:
642      assert(ir->type->is_integer());
643      assert(ir->operands[0]->type == ir->type);
644      assert(ir->operands[1]->type == ir->type);
645      assert(ir->operands[2]->type == ir->type);
646      break;
647
648   case ir_triop_vector_insert:
649      assert(ir->operands[0]->type->is_vector());
650      assert(ir->operands[1]->type->is_scalar());
651      assert(ir->operands[0]->type->base_type == ir->operands[1]->type->base_type);
652      assert(ir->operands[2]->type->is_scalar()
653             && ir->operands[2]->type->is_integer());
654      assert(ir->type == ir->operands[0]->type);
655      break;
656
657   case ir_quadop_bitfield_insert:
658      assert(ir->type->is_integer());
659      assert(ir->operands[0]->type == ir->type);
660      assert(ir->operands[1]->type == ir->type);
661      assert(ir->operands[2]->type == ir->type);
662      assert(ir->operands[3]->type == ir->type);
663      break;
664
665   case ir_quadop_vector:
666      /* The vector operator collects some number of scalars and generates a
667       * vector from them.
668       *
669       *  - All of the operands must be scalar.
670       *  - Number of operands must matche the size of the resulting vector.
671       *  - Base type of the operands must match the base type of the result.
672       */
673      assert(ir->type->is_vector());
674      switch (ir->type->vector_elements) {
675      case 2:
676	 assert(ir->operands[0]->type->is_scalar());
677	 assert(ir->operands[0]->type->base_type == ir->type->base_type);
678	 assert(ir->operands[1]->type->is_scalar());
679	 assert(ir->operands[1]->type->base_type == ir->type->base_type);
680	 assert(ir->operands[2] == NULL);
681	 assert(ir->operands[3] == NULL);
682	 break;
683      case 3:
684	 assert(ir->operands[0]->type->is_scalar());
685	 assert(ir->operands[0]->type->base_type == ir->type->base_type);
686	 assert(ir->operands[1]->type->is_scalar());
687	 assert(ir->operands[1]->type->base_type == ir->type->base_type);
688	 assert(ir->operands[2]->type->is_scalar());
689	 assert(ir->operands[2]->type->base_type == ir->type->base_type);
690	 assert(ir->operands[3] == NULL);
691	 break;
692      case 4:
693	 assert(ir->operands[0]->type->is_scalar());
694	 assert(ir->operands[0]->type->base_type == ir->type->base_type);
695	 assert(ir->operands[1]->type->is_scalar());
696	 assert(ir->operands[1]->type->base_type == ir->type->base_type);
697	 assert(ir->operands[2]->type->is_scalar());
698	 assert(ir->operands[2]->type->base_type == ir->type->base_type);
699	 assert(ir->operands[3]->type->is_scalar());
700	 assert(ir->operands[3]->type->base_type == ir->type->base_type);
701	 break;
702      default:
703	 /* The is_vector assertion above should prevent execution from ever
704	  * getting here.
705	  */
706	 assert(!"Should not get here.");
707	 break;
708      }
709   }
710
711   return visit_continue;
712}
713
714ir_visitor_status
715ir_validate::visit_leave(ir_swizzle *ir)
716{
717   unsigned int chans[4] = {ir->mask.x, ir->mask.y, ir->mask.z, ir->mask.w};
718
719   for (unsigned int i = 0; i < ir->type->vector_elements; i++) {
720      if (chans[i] >= ir->val->type->vector_elements) {
721	 printf("ir_swizzle @ %p specifies a channel not present "
722		"in the value.\n", (void *) ir);
723	 ir->print();
724	 abort();
725      }
726   }
727
728   return visit_continue;
729}
730
731ir_visitor_status
732ir_validate::visit(ir_variable *ir)
733{
734   /* An ir_variable is the one thing that can (and will) appear multiple times
735    * in an IR tree.  It is added to the hashtable so that it can be used
736    * in the ir_dereference_variable handler to ensure that a variable is
737    * declared before it is dereferenced.
738    */
739   if (ir->name && ir->is_name_ralloced())
740      assert(ralloc_parent(ir->name) == ir);
741
742   _mesa_set_add(ir_set, ir);
743
744   /* If a variable is an array, verify that the maximum array index is in
745    * bounds.  There was once an error in AST-to-HIR conversion that set this
746    * to be out of bounds.
747    */
748   if (ir->type->array_size() > 0) {
749      if (ir->data.max_array_access >= (int)ir->type->length) {
750	 printf("ir_variable has maximum access out of bounds (%d vs %d)\n",
751		ir->data.max_array_access, ir->type->length - 1);
752	 ir->print();
753	 abort();
754      }
755   }
756
757   /* If a variable is an interface block (or an array of interface blocks),
758    * verify that the maximum array index for each interface member is in
759    * bounds.
760    */
761   if (ir->is_interface_instance()) {
762      const glsl_struct_field *fields =
763         ir->get_interface_type()->fields.structure;
764      for (unsigned i = 0; i < ir->get_interface_type()->length; i++) {
765         if (fields[i].type->array_size() > 0 &&
766             !fields[i].implicit_sized_array) {
767            const int *const max_ifc_array_access =
768               ir->get_max_ifc_array_access();
769
770            assert(max_ifc_array_access != NULL);
771
772            if (max_ifc_array_access[i] >= (int)fields[i].type->length) {
773               printf("ir_variable has maximum access out of bounds for "
774                      "field %s (%d vs %d)\n", fields[i].name,
775                      max_ifc_array_access[i], fields[i].type->length);
776               ir->print();
777               abort();
778            }
779         }
780      }
781   }
782
783   if (ir->constant_initializer != NULL && !ir->data.has_initializer) {
784      printf("ir_variable didn't have an initializer, but has a constant "
785	     "initializer value.\n");
786      ir->print();
787      abort();
788   }
789
790   if (ir->data.mode == ir_var_uniform
791       && is_gl_identifier(ir->name)
792       && ir->get_state_slots() == NULL) {
793      printf("built-in uniform has no state\n");
794      ir->print();
795      abort();
796   }
797
798   return visit_continue;
799}
800
801ir_visitor_status
802ir_validate::visit_enter(ir_assignment *ir)
803{
804   const ir_dereference *const lhs = ir->lhs;
805   if (lhs->type->is_scalar() || lhs->type->is_vector()) {
806      if (ir->write_mask == 0) {
807	 printf("Assignment LHS is %s, but write mask is 0:\n",
808		lhs->type->is_scalar() ? "scalar" : "vector");
809	 ir->print();
810	 abort();
811      }
812
813      int lhs_components = 0;
814      for (int i = 0; i < 4; i++) {
815	 if (ir->write_mask & (1 << i))
816	    lhs_components++;
817      }
818
819      if (lhs_components != ir->rhs->type->vector_elements) {
820	 printf("Assignment count of LHS write mask channels enabled not\n"
821		"matching RHS vector size (%d LHS, %d RHS).\n",
822		lhs_components, ir->rhs->type->vector_elements);
823	 ir->print();
824	 abort();
825      }
826   }
827
828   this->validate_ir(ir, this->data_enter);
829
830   return visit_continue;
831}
832
833ir_visitor_status
834ir_validate::visit_enter(ir_call *ir)
835{
836   ir_function_signature *const callee = ir->callee;
837
838   if (callee->ir_type != ir_type_function_signature) {
839      printf("IR called by ir_call is not ir_function_signature!\n");
840      abort();
841   }
842
843   if (ir->return_deref) {
844      if (ir->return_deref->type != callee->return_type) {
845	 printf("callee type %s does not match return storage type %s\n",
846	        callee->return_type->name, ir->return_deref->type->name);
847	 abort();
848      }
849   } else if (callee->return_type != glsl_type::void_type) {
850      printf("ir_call has non-void callee but no return storage\n");
851      abort();
852   }
853
854   const exec_node *formal_param_node = callee->parameters.get_head_raw();
855   const exec_node *actual_param_node = ir->actual_parameters.get_head_raw();
856   while (true) {
857      if (formal_param_node->is_tail_sentinel()
858          != actual_param_node->is_tail_sentinel()) {
859         printf("ir_call has the wrong number of parameters:\n");
860         goto dump_ir;
861      }
862      if (formal_param_node->is_tail_sentinel()) {
863         break;
864      }
865      const ir_variable *formal_param
866         = (const ir_variable *) formal_param_node;
867      const ir_rvalue *actual_param
868         = (const ir_rvalue *) actual_param_node;
869      if (formal_param->type != actual_param->type) {
870         printf("ir_call parameter type mismatch:\n");
871         goto dump_ir;
872      }
873      if (formal_param->data.mode == ir_var_function_out
874          || formal_param->data.mode == ir_var_function_inout) {
875         if (!actual_param->is_lvalue()) {
876            printf("ir_call out/inout parameters must be lvalues:\n");
877            goto dump_ir;
878         }
879      }
880      formal_param_node = formal_param_node->next;
881      actual_param_node = actual_param_node->next;
882   }
883
884   return visit_continue;
885
886dump_ir:
887   ir->print();
888   printf("callee:\n");
889   callee->print();
890   abort();
891   return visit_stop;
892}
893
894void
895ir_validate::validate_ir(ir_instruction *ir, void *data)
896{
897   struct set *ir_set = (struct set *) data;
898
899   if (_mesa_set_search(ir_set, ir)) {
900      printf("Instruction node present twice in ir tree:\n");
901      ir->print();
902      printf("\n");
903      abort();
904   }
905   _mesa_set_add(ir_set, ir);
906}
907
908void
909check_node_type(ir_instruction *ir, void *data)
910{
911   (void) data;
912
913   if (ir->ir_type >= ir_type_max) {
914      printf("Instruction node with unset type\n");
915      ir->print(); printf("\n");
916   }
917   ir_rvalue *value = ir->as_rvalue();
918   if (value != NULL)
919      assert(value->type != glsl_type::error_type);
920}
921
922void
923validate_ir_tree(exec_list *instructions)
924{
925   /* We shouldn't have any reason to validate IR in a release build,
926    * and it's half composed of assert()s anyway which wouldn't do
927    * anything.
928    */
929#ifdef DEBUG
930   ir_validate v;
931
932   v.run(instructions);
933
934   foreach_in_list(ir_instruction, ir, instructions) {
935      visit_tree(ir, check_node_type, NULL);
936   }
937#endif
938}
939