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