blob: c6c18df51a74f2f13bc2a4da9ce04d771176d3b1 [file] [log] [blame]
Eric Anholt53cdb7e2010-06-22 12:07:21 -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_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 <inttypes.h>
37#include "ir.h"
38#include "ir_visitor.h"
39#include "ir_optimization.h"
40#include "glsl_types.h"
41#include "hash_table.h"
42
43/**
44 * Visitor class for replacing expressions with ir_constant values.
45 */
46
47
48class ir_validate : public ir_hierarchical_visitor {
49public:
50 virtual ir_visitor_status visit_enter(class ir_constant *);
51 virtual ir_visitor_status visit_enter(class ir_loop *);
52 virtual ir_visitor_status visit_enter(class ir_function_signature *);
53 virtual ir_visitor_status visit_enter(class ir_function *);
54 virtual ir_visitor_status visit_enter(class ir_expression *);
55 virtual ir_visitor_status visit_enter(class ir_texture *);
56 virtual ir_visitor_status visit_enter(class ir_swizzle *);
57 virtual ir_visitor_status visit_enter(class ir_dereference_array *);
58 virtual ir_visitor_status visit_enter(class ir_dereference_record *);
59 virtual ir_visitor_status visit_enter(class ir_assignment *);
60 virtual ir_visitor_status visit_enter(class ir_call *);
61 virtual ir_visitor_status visit_enter(class ir_return *);
62 virtual ir_visitor_status visit_enter(class ir_if *);
63
64 void validate_ir(ir_instruction *ir);
65
66 struct hash_table *ht;
67};
68
69unsigned int hash_func(const void *key)
70{
71 return (unsigned int)(uintptr_t)key;
72}
73
74int hash_compare_func(const void *key1, const void *key2)
75{
76 return key1 == key2 ? 0 : 1;
77}
78
79void
80ir_validate::validate_ir(ir_instruction *ir)
81{
82 if (hash_table_find(this->ht, ir)) {
83 printf("Instruction node present twice in ir tree:\n");
84 ir->print();
85 printf("\n");
86 abort();
87 }
88 hash_table_insert(this->ht, ir, ir);
89}
90
91ir_visitor_status
92ir_validate::visit_enter(ir_constant *ir)
93{
94 validate_ir(ir);
95 return visit_continue;
96}
97
98ir_visitor_status
99ir_validate::visit_enter(ir_loop *ir)
100{
101 validate_ir(ir);
102 return visit_continue;
103}
104
105ir_visitor_status
106ir_validate::visit_enter(ir_function_signature *ir)
107{
108 validate_ir(ir);
109 return visit_continue;
110}
111
112ir_visitor_status
113ir_validate::visit_enter(ir_function *ir)
114{
115 validate_ir(ir);
116 return visit_continue;
117}
118
119ir_visitor_status
120ir_validate::visit_enter(ir_expression *ir)
121{
122 validate_ir(ir);
123 return visit_continue;
124}
125
126ir_visitor_status
127ir_validate::visit_enter(ir_texture *ir)
128{
129 validate_ir(ir);
130 return visit_continue;
131}
132
133ir_visitor_status
134ir_validate::visit_enter(ir_swizzle *ir)
135{
136 validate_ir(ir);
137 return visit_continue;
138}
139
140ir_visitor_status
141ir_validate::visit_enter(ir_dereference_array *ir)
142{
143 validate_ir(ir);
144 return visit_continue;
145}
146
147ir_visitor_status
148ir_validate::visit_enter(ir_dereference_record *ir)
149{
150 validate_ir(ir);
151 return visit_continue;
152}
153
154ir_visitor_status
155ir_validate::visit_enter(ir_assignment *ir)
156{
157 validate_ir(ir);
158 return visit_continue;
159}
160
161ir_visitor_status
162ir_validate::visit_enter(ir_call *ir)
163{
164 validate_ir(ir);
165 return visit_continue;
166}
167
168ir_visitor_status
169ir_validate::visit_enter(ir_return *ir)
170{
171 validate_ir(ir);
172 return visit_continue;
173}
174
175ir_visitor_status
176ir_validate::visit_enter(ir_if *ir)
177{
178 validate_ir(ir);
179 return visit_continue;
180}
181
182void
183validate_ir_tree(exec_list *instructions)
184{
185 ir_validate v;
186
187 v.ht = hash_table_ctor(0, hash_func, hash_compare_func);
188
189 v.run(instructions);
190
191 hash_table_dtor(v.ht);
192}