Merge remote-tracking branch 'mesa-public/master' into vulkan
[mesa.git] / src / glsl / ir_equals.cpp
1 /*
2 * Copyright © 2013 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 #include "ir.h"
25
26 /**
27 * Helper for checking equality when one instruction might be NULL, since you
28 * can't access a's vtable in that case.
29 */
30 static bool
31 possibly_null_equals(const ir_instruction *a, const ir_instruction *b,
32 enum ir_node_type ignore)
33 {
34 if (!a || !b)
35 return !a && !b;
36
37 return a->equals(b, ignore);
38 }
39
40 /**
41 * The base equality function: Return not equal for anything we don't know
42 * about.
43 */
44 bool
45 ir_instruction::equals(const ir_instruction *, enum ir_node_type) const
46 {
47 return false;
48 }
49
50 bool
51 ir_constant::equals(const ir_instruction *ir, enum ir_node_type) const
52 {
53 const ir_constant *other = ir->as_constant();
54 if (!other)
55 return false;
56
57 if (type != other->type)
58 return false;
59
60 for (unsigned i = 0; i < type->components(); i++) {
61 if (value.u[i] != other->value.u[i])
62 return false;
63 }
64
65 return true;
66 }
67
68 bool
69 ir_dereference_variable::equals(const ir_instruction *ir,
70 enum ir_node_type) const
71 {
72 const ir_dereference_variable *other = ir->as_dereference_variable();
73 if (!other)
74 return false;
75
76 return var == other->var;
77 }
78
79 bool
80 ir_dereference_array::equals(const ir_instruction *ir,
81 enum ir_node_type ignore) const
82 {
83 const ir_dereference_array *other = ir->as_dereference_array();
84 if (!other)
85 return false;
86
87 if (type != other->type)
88 return false;
89
90 if (!array->equals(other->array, ignore))
91 return false;
92
93 if (!array_index->equals(other->array_index, ignore))
94 return false;
95
96 return true;
97 }
98
99 bool
100 ir_swizzle::equals(const ir_instruction *ir,
101 enum ir_node_type ignore) const
102 {
103 const ir_swizzle *other = ir->as_swizzle();
104 if (!other)
105 return false;
106
107 if (type != other->type)
108 return false;
109
110 if (ignore != ir_type_swizzle) {
111 if (mask.x != other->mask.x ||
112 mask.y != other->mask.y ||
113 mask.z != other->mask.z ||
114 mask.w != other->mask.w) {
115 return false;
116 }
117 }
118
119 return val->equals(other->val, ignore);
120 }
121
122 bool
123 ir_texture::equals(const ir_instruction *ir, enum ir_node_type ignore) const
124 {
125 const ir_texture *other = ir->as_texture();
126 if (!other)
127 return false;
128
129 if (type != other->type)
130 return false;
131
132 if (op != other->op)
133 return false;
134
135 if (!possibly_null_equals(coordinate, other->coordinate, ignore))
136 return false;
137
138 if (!possibly_null_equals(projector, other->projector, ignore))
139 return false;
140
141 if (!possibly_null_equals(shadow_comparitor, other->shadow_comparitor, ignore))
142 return false;
143
144 if (!possibly_null_equals(offset, other->offset, ignore))
145 return false;
146
147 if (!sampler->equals(other->sampler, ignore))
148 return false;
149
150 switch (op) {
151 case ir_tex:
152 case ir_lod:
153 case ir_query_levels:
154 break;
155 case ir_txb:
156 if (!lod_info.bias->equals(other->lod_info.bias, ignore))
157 return false;
158 break;
159 case ir_txl:
160 case ir_txf:
161 case ir_txs:
162 if (!lod_info.lod->equals(other->lod_info.lod, ignore))
163 return false;
164 break;
165 case ir_txd:
166 if (!lod_info.grad.dPdx->equals(other->lod_info.grad.dPdx, ignore) ||
167 !lod_info.grad.dPdy->equals(other->lod_info.grad.dPdy, ignore))
168 return false;
169 break;
170 case ir_txf_ms:
171 if (!lod_info.sample_index->equals(other->lod_info.sample_index, ignore))
172 return false;
173 break;
174 case ir_tg4:
175 if (!lod_info.component->equals(other->lod_info.component, ignore))
176 return false;
177 break;
178 default:
179 assert(!"Unrecognized texture op");
180 }
181
182 return true;
183 }
184
185 bool
186 ir_expression::equals(const ir_instruction *ir, enum ir_node_type ignore) const
187 {
188 const ir_expression *other = ir->as_expression();
189 if (!other)
190 return false;
191
192 if (type != other->type)
193 return false;
194
195 if (operation != other->operation)
196 return false;
197
198 for (unsigned i = 0; i < get_num_operands(); i++) {
199 if (!operands[i]->equals(other->operands[i], ignore))
200 return false;
201 }
202
203 return true;
204 }