blob: 8c86748d26a1d41cf0ff91d284015b26d4b3f254 [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"
Ian Romanick865cf2d2010-06-22 18:41:50 -070038#include "ir_hierarchical_visitor.h"
Eric Anholt53cdb7e2010-06-22 12:07:21 -070039#include "hash_table.h"
Eric Anholt53cdb7e2010-06-22 12:07:21 -070040
Ian Romanick865cf2d2010-06-22 18:41:50 -070041
42class ir_validate : public ir_hierarchical_visitor {
43public:
44 ir_validate()
45 {
Ian Romanickd1a1ee52010-07-06 14:49:14 -070046 this->ht = hash_table_ctor(0, hash_table_pointer_hash,
47 hash_table_pointer_compare);
Ian Romanick865cf2d2010-06-22 18:41:50 -070048
Ian Romanickc67016d2010-07-02 13:30:23 -070049 this->current_function = NULL;
50
Ian Romanick865cf2d2010-06-22 18:41:50 -070051 this->callback = ir_validate::validate_ir;
52 this->data = ht;
53 }
54
55 ~ir_validate()
56 {
57 hash_table_dtor(this->ht);
58 }
59
60 virtual ir_visitor_status visit(ir_variable *v);
61
Ian Romanickc67016d2010-07-02 13:30:23 -070062 virtual ir_visitor_status visit_enter(ir_function *ir);
63 virtual ir_visitor_status visit_leave(ir_function *ir);
64 virtual ir_visitor_status visit_enter(ir_function_signature *ir);
65
Ian Romanick865cf2d2010-06-22 18:41:50 -070066 static void validate_ir(ir_instruction *ir, void *data);
67
Ian Romanickc67016d2010-07-02 13:30:23 -070068 ir_function *current_function;
69
Ian Romanick865cf2d2010-06-22 18:41:50 -070070 struct hash_table *ht;
71};
72
73ir_visitor_status
Ian Romanickc67016d2010-07-02 13:30:23 -070074ir_validate::visit_enter(ir_function *ir)
75{
76 /* Function definitions cannot be nested.
77 */
78 if (this->current_function != NULL) {
79 printf("Function definition nested inside another function "
80 "definition:\n");
81 printf("%s %p inside %s %p\n",
82 ir->name, ir,
83 this->current_function->name, this->current_function);
84 abort();
85 }
86
87 /* Store the current function hierarchy being traversed. This is used
88 * by the function signature visitor to ensure that the signatures are
89 * linked with the correct functions.
90 */
91 this->current_function = ir;
92
93 this->validate_ir(ir, this->data);
94
95 return visit_continue;
96}
97
98ir_visitor_status
99ir_validate::visit_leave(ir_function *ir)
100{
101 (void) ir;
102
103 this->current_function = NULL;
104 return visit_continue;
105}
106
107ir_visitor_status
108ir_validate::visit_enter(ir_function_signature *ir)
109{
110 if (this->current_function != ir->function()) {
111 printf("Function signature nested inside wrong function "
112 "definition:\n");
113 printf("%p inside %s %p instead of %s %p\n",
114 ir,
115 this->current_function->name, this->current_function,
116 ir->function_name(), ir->function());
117 abort();
118 }
119
120 this->validate_ir(ir, this->data);
121
122 return visit_continue;
123}
124
125ir_visitor_status
Ian Romanick865cf2d2010-06-22 18:41:50 -0700126ir_validate::visit(ir_variable *ir)
Eric Anholt53cdb7e2010-06-22 12:07:21 -0700127{
Ian Romanick865cf2d2010-06-22 18:41:50 -0700128 /* An ir_variable is the one thing that can (and will) appear multiple times
129 * in an IR tree.
130 */
131 (void) ir;
132 return visit_continue;
133}
134
135void
136ir_validate::validate_ir(ir_instruction *ir, void *data)
137{
138 struct hash_table *ht = (struct hash_table *) data;
139
140 if (hash_table_find(ht, ir)) {
Eric Anholt53cdb7e2010-06-22 12:07:21 -0700141 printf("Instruction node present twice in ir tree:\n");
142 ir->print();
143 printf("\n");
144 abort();
145 }
Ian Romanick865cf2d2010-06-22 18:41:50 -0700146 hash_table_insert(ht, ir, ir);
Eric Anholt53cdb7e2010-06-22 12:07:21 -0700147}
148
149void
150validate_ir_tree(exec_list *instructions)
151{
152 ir_validate v;
153
Eric Anholt53cdb7e2010-06-22 12:07:21 -0700154 v.run(instructions);
Eric Anholt53cdb7e2010-06-22 12:07:21 -0700155}