e8ac9fbe456074560358d6ef0e9727c65b7fb133
[mesa.git] / src / glsl / ir_clone.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 #include <string.h>
25 #include "main/compiler.h"
26 #include "ir.h"
27 #include "glsl_types.h"
28 #include "program/hash_table.h"
29
30 /**
31 * Duplicate an IR variable
32 *
33 * \note
34 * This will probably be made \c virtual and moved to the base class
35 * eventually.
36 */
37 ir_variable *
38 ir_variable::clone(void *mem_ctx, struct hash_table *ht) const
39 {
40 ir_variable *var = new(mem_ctx) ir_variable(this->type, this->name,
41 (ir_variable_mode) this->mode);
42
43 var->max_array_access = this->max_array_access;
44 var->read_only = this->read_only;
45 var->centroid = this->centroid;
46 var->invariant = this->invariant;
47 var->interpolation = this->interpolation;
48 var->location = this->location;
49 var->warn_extension = this->warn_extension;
50 var->origin_upper_left = this->origin_upper_left;
51 var->pixel_center_integer = this->pixel_center_integer;
52 var->explicit_location = this->explicit_location;
53 var->has_initializer = this->has_initializer;
54
55 var->num_state_slots = this->num_state_slots;
56 if (this->state_slots) {
57 /* FINISHME: This really wants to use something like talloc_reference, but
58 * FINISHME: ralloc doesn't have any similar function.
59 */
60 var->state_slots = ralloc_array(var, ir_state_slot,
61 this->num_state_slots);
62 memcpy(var->state_slots, this->state_slots,
63 sizeof(this->state_slots[0]) * var->num_state_slots);
64 }
65
66 if (this->explicit_location)
67 var->location = this->location;
68
69 if (this->constant_value)
70 var->constant_value = this->constant_value->clone(mem_ctx, ht);
71
72 if (this->constant_initializer)
73 var->constant_initializer =
74 this->constant_initializer->clone(mem_ctx, ht);
75
76 if (ht) {
77 hash_table_insert(ht, var, (void *)const_cast<ir_variable *>(this));
78 }
79
80 return var;
81 }
82
83 ir_swizzle *
84 ir_swizzle::clone(void *mem_ctx, struct hash_table *ht) const
85 {
86 return new(mem_ctx) ir_swizzle(this->val->clone(mem_ctx, ht), this->mask);
87 }
88
89 ir_return *
90 ir_return::clone(void *mem_ctx, struct hash_table *ht) const
91 {
92 ir_rvalue *new_value = NULL;
93
94 if (this->value)
95 new_value = this->value->clone(mem_ctx, ht);
96
97 return new(mem_ctx) ir_return(new_value);
98 }
99
100 ir_discard *
101 ir_discard::clone(void *mem_ctx, struct hash_table *ht) const
102 {
103 ir_rvalue *new_condition = NULL;
104
105 if (this->condition != NULL)
106 new_condition = this->condition->clone(mem_ctx, ht);
107
108 return new(mem_ctx) ir_discard(new_condition);
109 }
110
111 ir_loop_jump *
112 ir_loop_jump::clone(void *mem_ctx, struct hash_table *ht) const
113 {
114 (void)ht;
115
116 return new(mem_ctx) ir_loop_jump(this->mode);
117 }
118
119 ir_if *
120 ir_if::clone(void *mem_ctx, struct hash_table *ht) const
121 {
122 ir_if *new_if = new(mem_ctx) ir_if(this->condition->clone(mem_ctx, ht));
123
124 foreach_iter(exec_list_iterator, iter, this->then_instructions) {
125 ir_instruction *ir = (ir_instruction *)iter.get();
126 new_if->then_instructions.push_tail(ir->clone(mem_ctx, ht));
127 }
128
129 foreach_iter(exec_list_iterator, iter, this->else_instructions) {
130 ir_instruction *ir = (ir_instruction *)iter.get();
131 new_if->else_instructions.push_tail(ir->clone(mem_ctx, ht));
132 }
133
134 return new_if;
135 }
136
137 ir_loop *
138 ir_loop::clone(void *mem_ctx, struct hash_table *ht) const
139 {
140 ir_loop *new_loop = new(mem_ctx) ir_loop();
141
142 if (this->from)
143 new_loop->from = this->from->clone(mem_ctx, ht);
144 if (this->to)
145 new_loop->to = this->to->clone(mem_ctx, ht);
146 if (this->increment)
147 new_loop->increment = this->increment->clone(mem_ctx, ht);
148 new_loop->counter = counter;
149
150 foreach_iter(exec_list_iterator, iter, this->body_instructions) {
151 ir_instruction *ir = (ir_instruction *)iter.get();
152 new_loop->body_instructions.push_tail(ir->clone(mem_ctx, ht));
153 }
154
155 new_loop->cmp = this->cmp;
156 return new_loop;
157 }
158
159 ir_call *
160 ir_call::clone(void *mem_ctx, struct hash_table *ht) const
161 {
162 if (this->type == glsl_type::error_type)
163 return ir_call::get_error_instruction(mem_ctx);
164
165 exec_list new_parameters;
166
167 foreach_iter(exec_list_iterator, iter, this->actual_parameters) {
168 ir_instruction *ir = (ir_instruction *)iter.get();
169 new_parameters.push_tail(ir->clone(mem_ctx, ht));
170 }
171
172 return new(mem_ctx) ir_call(this->callee, &new_parameters);
173 }
174
175 ir_expression *
176 ir_expression::clone(void *mem_ctx, struct hash_table *ht) const
177 {
178 ir_rvalue *op[Elements(this->operands)] = { NULL, };
179 unsigned int i;
180
181 for (i = 0; i < get_num_operands(); i++) {
182 op[i] = this->operands[i]->clone(mem_ctx, ht);
183 }
184
185 return new(mem_ctx) ir_expression(this->operation, this->type,
186 op[0], op[1], op[2], op[3]);
187 }
188
189 ir_dereference_variable *
190 ir_dereference_variable::clone(void *mem_ctx, struct hash_table *ht) const
191 {
192 ir_variable *new_var;
193
194 if (ht) {
195 new_var = (ir_variable *)hash_table_find(ht, this->var);
196 if (!new_var)
197 new_var = this->var;
198 } else {
199 new_var = this->var;
200 }
201
202 return new(mem_ctx) ir_dereference_variable(new_var);
203 }
204
205 ir_dereference_array *
206 ir_dereference_array::clone(void *mem_ctx, struct hash_table *ht) const
207 {
208 return new(mem_ctx) ir_dereference_array(this->array->clone(mem_ctx, ht),
209 this->array_index->clone(mem_ctx,
210 ht));
211 }
212
213 ir_dereference_record *
214 ir_dereference_record::clone(void *mem_ctx, struct hash_table *ht) const
215 {
216 return new(mem_ctx) ir_dereference_record(this->record->clone(mem_ctx, ht),
217 this->field);
218 }
219
220 ir_texture *
221 ir_texture::clone(void *mem_ctx, struct hash_table *ht) const
222 {
223 ir_texture *new_tex = new(mem_ctx) ir_texture(this->op);
224 new_tex->type = this->type;
225
226 new_tex->sampler = this->sampler->clone(mem_ctx, ht);
227 if (this->coordinate)
228 new_tex->coordinate = this->coordinate->clone(mem_ctx, ht);
229 if (this->projector)
230 new_tex->projector = this->projector->clone(mem_ctx, ht);
231 if (this->shadow_comparitor) {
232 new_tex->shadow_comparitor = this->shadow_comparitor->clone(mem_ctx, ht);
233 }
234
235 if (this->offset != NULL)
236 new_tex->offset = this->offset->clone(mem_ctx, ht);
237
238 switch (this->op) {
239 case ir_tex:
240 break;
241 case ir_txb:
242 new_tex->lod_info.bias = this->lod_info.bias->clone(mem_ctx, ht);
243 break;
244 case ir_txl:
245 case ir_txf:
246 case ir_txs:
247 new_tex->lod_info.lod = this->lod_info.lod->clone(mem_ctx, ht);
248 break;
249 case ir_txd:
250 new_tex->lod_info.grad.dPdx = this->lod_info.grad.dPdx->clone(mem_ctx, ht);
251 new_tex->lod_info.grad.dPdy = this->lod_info.grad.dPdy->clone(mem_ctx, ht);
252 break;
253 }
254
255 return new_tex;
256 }
257
258 ir_assignment *
259 ir_assignment::clone(void *mem_ctx, struct hash_table *ht) const
260 {
261 ir_rvalue *new_condition = NULL;
262
263 if (this->condition)
264 new_condition = this->condition->clone(mem_ctx, ht);
265
266 return new(mem_ctx) ir_assignment(this->lhs->clone(mem_ctx, ht),
267 this->rhs->clone(mem_ctx, ht),
268 new_condition,
269 this->write_mask);
270 }
271
272 ir_function *
273 ir_function::clone(void *mem_ctx, struct hash_table *ht) const
274 {
275 ir_function *copy = new(mem_ctx) ir_function(this->name);
276
277 foreach_list_const(node, &this->signatures) {
278 const ir_function_signature *const sig =
279 (const ir_function_signature *const) node;
280
281 ir_function_signature *sig_copy = sig->clone(mem_ctx, ht);
282 copy->add_signature(sig_copy);
283
284 if (ht != NULL)
285 hash_table_insert(ht, sig_copy,
286 (void *)const_cast<ir_function_signature *>(sig));
287 }
288
289 return copy;
290 }
291
292 ir_function_signature *
293 ir_function_signature::clone(void *mem_ctx, struct hash_table *ht) const
294 {
295 ir_function_signature *copy = this->clone_prototype(mem_ctx, ht);
296
297 copy->is_defined = this->is_defined;
298
299 /* Clone the instruction list.
300 */
301 foreach_list_const(node, &this->body) {
302 const ir_instruction *const inst = (const ir_instruction *) node;
303
304 ir_instruction *const inst_copy = inst->clone(mem_ctx, ht);
305 copy->body.push_tail(inst_copy);
306 }
307
308 return copy;
309 }
310
311 ir_function_signature *
312 ir_function_signature::clone_prototype(void *mem_ctx, struct hash_table *ht) const
313 {
314 ir_function_signature *copy =
315 new(mem_ctx) ir_function_signature(this->return_type);
316
317 copy->is_defined = false;
318 copy->is_builtin = this->is_builtin;
319
320 /* Clone the parameter list, but NOT the body.
321 */
322 foreach_list_const(node, &this->parameters) {
323 const ir_variable *const param = (const ir_variable *) node;
324
325 assert(const_cast<ir_variable *>(param)->as_variable() != NULL);
326
327 ir_variable *const param_copy = param->clone(mem_ctx, ht);
328 copy->parameters.push_tail(param_copy);
329 }
330
331 return copy;
332 }
333
334 ir_constant *
335 ir_constant::clone(void *mem_ctx, struct hash_table *ht) const
336 {
337 (void)ht;
338
339 switch (this->type->base_type) {
340 case GLSL_TYPE_UINT:
341 case GLSL_TYPE_INT:
342 case GLSL_TYPE_FLOAT:
343 case GLSL_TYPE_BOOL:
344 return new(mem_ctx) ir_constant(this->type, &this->value);
345
346 case GLSL_TYPE_STRUCT: {
347 ir_constant *c = new(mem_ctx) ir_constant;
348
349 c->type = this->type;
350 for (exec_node *node = this->components.head
351 ; !node->is_tail_sentinel()
352 ; node = node->next) {
353 ir_constant *const orig = (ir_constant *) node;
354
355 c->components.push_tail(orig->clone(mem_ctx, NULL));
356 }
357
358 return c;
359 }
360
361 case GLSL_TYPE_ARRAY: {
362 ir_constant *c = new(mem_ctx) ir_constant;
363
364 c->type = this->type;
365 c->array_elements = ralloc_array(c, ir_constant *, this->type->length);
366 for (unsigned i = 0; i < this->type->length; i++) {
367 c->array_elements[i] = this->array_elements[i]->clone(mem_ctx, NULL);
368 }
369 return c;
370 }
371
372 default:
373 assert(!"Should not get here.");
374 return NULL;
375 }
376 }
377
378
379 class fixup_ir_call_visitor : public ir_hierarchical_visitor {
380 public:
381 fixup_ir_call_visitor(struct hash_table *ht)
382 {
383 this->ht = ht;
384 }
385
386 virtual ir_visitor_status visit_enter(ir_call *ir)
387 {
388 /* Try to find the function signature referenced by the ir_call in the
389 * table. If it is found, replace it with the value from the table.
390 */
391 ir_function_signature *sig =
392 (ir_function_signature *) hash_table_find(this->ht, ir->get_callee());
393 if (sig != NULL)
394 ir->set_callee(sig);
395
396 /* Since this may be used before function call parameters are flattened,
397 * the children also need to be processed.
398 */
399 return visit_continue;
400 }
401
402 private:
403 struct hash_table *ht;
404 };
405
406
407 static void
408 fixup_function_calls(struct hash_table *ht, exec_list *instructions)
409 {
410 fixup_ir_call_visitor v(ht);
411 v.run(instructions);
412 }
413
414
415 void
416 clone_ir_list(void *mem_ctx, exec_list *out, const exec_list *in)
417 {
418 struct hash_table *ht =
419 hash_table_ctor(0, hash_table_pointer_hash, hash_table_pointer_compare);
420
421 foreach_list_const(node, in) {
422 const ir_instruction *const original = (ir_instruction *) node;
423 ir_instruction *copy = original->clone(mem_ctx, ht);
424
425 out->push_tail(copy);
426 }
427
428 /* Make a pass over the cloned tree to fix up ir_call nodes to point to the
429 * cloned ir_function_signature nodes. This cannot be done automatically
430 * during cloning because the ir_call might be a forward reference (i.e.,
431 * the function signature that it references may not have been cloned yet).
432 */
433 fixup_function_calls(ht, out);
434
435 hash_table_dtor(ht);
436 }