ir_clone.cpp revision 4b1721eaf35ccb60d90850ab34a99d6ab1f89a05
14b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt/*
24b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt * Copyright © 2010 Intel Corporation
34b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt *
44b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt * Permission is hereby granted, free of charge, to any person obtaining a
54b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt * copy of this software and associated documentation files (the "Software"),
64b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt * to deal in the Software without restriction, including without limitation
74b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt * the rights to use, copy, modify, merge, publish, distribute, sublicense,
84b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt * and/or sell copies of the Software, and to permit persons to whom the
94b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt * Software is furnished to do so, subject to the following conditions:
104b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt *
114b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt * The above copyright notice and this permission notice (including the next
124b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt * paragraph) shall be included in all copies or substantial portions of the
134b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt * Software.
144b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt *
154b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
164b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
174b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
184b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
194b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
204b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
214b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt * DEALINGS IN THE SOFTWARE.
224b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt */
234b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt
244b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt#include <string.h>
254b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt#include "ir.h"
264b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt#include "glsl_types.h"
273d6012303c3ce24c75d209267e6914f706d025c5Eric Anholtextern "C" {
2831747155ea3a24190277b125bd188ac8689af719Aras Pranckevicius#include "program/hash_table.h"
293d6012303c3ce24c75d209267e6914f706d025c5Eric Anholt}
304b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt
314b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt/**
324b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt * Duplicate an IR variable
334b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt *
344b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt * \note
354b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt * This will probably be made \c virtual and moved to the base class
364b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt * eventually.
374b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt */
38ca088cc277ce9f986693c857f3961dc0e1a4d91cIan Romanickir_variable *
398273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholtir_variable::clone(void *mem_ctx, struct hash_table *ht) const
404b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt{
418273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt   ir_variable *var = new(mem_ctx) ir_variable(this->type, this->name,
428273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt					       (ir_variable_mode) this->mode);
434b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt
444b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   var->max_array_access = this->max_array_access;
454b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   var->read_only = this->read_only;
464b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   var->centroid = this->centroid;
474b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   var->invariant = this->invariant;
484b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   var->interpolation = this->interpolation;
4910d222b70266a1b6e8dde90652156c6e18bcd3c8Ian Romanick   var->array_lvalue = this->array_lvalue;
5010d222b70266a1b6e8dde90652156c6e18bcd3c8Ian Romanick   var->location = this->location;
5110d222b70266a1b6e8dde90652156c6e18bcd3c8Ian Romanick   var->warn_extension = this->warn_extension;
524a962170d7cf4243d6ae156fca20a6167388925dEric Anholt   var->origin_upper_left = this->origin_upper_left;
534a962170d7cf4243d6ae156fca20a6167388925dEric Anholt   var->pixel_center_integer = this->pixel_center_integer;
5410d222b70266a1b6e8dde90652156c6e18bcd3c8Ian Romanick
5510d222b70266a1b6e8dde90652156c6e18bcd3c8Ian Romanick   if (this->constant_value)
568273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt      var->constant_value = this->constant_value->clone(mem_ctx, ht);
574b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt
584b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   if (ht) {
591b2bcf791365f7bab282e38808efadba19291261Eric Anholt      hash_table_insert(ht, var, (void *)const_cast<ir_variable *>(this));
604b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   }
614b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt
624b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   return var;
634b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt}
644b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt
65ca088cc277ce9f986693c857f3961dc0e1a4d91cIan Romanickir_swizzle *
668273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholtir_swizzle::clone(void *mem_ctx, struct hash_table *ht) const
674b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt{
688273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt   return new(mem_ctx) ir_swizzle(this->val->clone(mem_ctx, ht), this->mask);
694b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt}
704b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt
71ca088cc277ce9f986693c857f3961dc0e1a4d91cIan Romanickir_return *
728273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholtir_return::clone(void *mem_ctx, struct hash_table *ht) const
734b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt{
744b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   ir_rvalue *new_value = NULL;
754b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt
764b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   if (this->value)
778273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt      new_value = this->value->clone(mem_ctx, ht);
784b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt
798273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt   return new(mem_ctx) ir_return(new_value);
804b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt}
814b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt
82ca088cc277ce9f986693c857f3961dc0e1a4d91cIan Romanickir_discard *
838273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholtir_discard::clone(void *mem_ctx, struct hash_table *ht) const
8416efab1c4dee6e6a827ba5f1c482378159545ae5Kenneth Graunke{
8516efab1c4dee6e6a827ba5f1c482378159545ae5Kenneth Graunke   ir_rvalue *new_condition = NULL;
8616efab1c4dee6e6a827ba5f1c482378159545ae5Kenneth Graunke
8716efab1c4dee6e6a827ba5f1c482378159545ae5Kenneth Graunke   if (this->condition != NULL)
888273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt      new_condition = this->condition->clone(mem_ctx, ht);
8916efab1c4dee6e6a827ba5f1c482378159545ae5Kenneth Graunke
908273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt   return new(mem_ctx) ir_discard(new_condition);
9116efab1c4dee6e6a827ba5f1c482378159545ae5Kenneth Graunke}
9216efab1c4dee6e6a827ba5f1c482378159545ae5Kenneth Graunke
93ca088cc277ce9f986693c857f3961dc0e1a4d91cIan Romanickir_loop_jump *
948273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholtir_loop_jump::clone(void *mem_ctx, struct hash_table *ht) const
954b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt{
964b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   (void)ht;
974b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt
988273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt   return new(mem_ctx) ir_loop_jump(this->mode);
994b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt}
1004b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt
101ca088cc277ce9f986693c857f3961dc0e1a4d91cIan Romanickir_if *
1028273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholtir_if::clone(void *mem_ctx, struct hash_table *ht) const
1034b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt{
1048273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt   ir_if *new_if = new(mem_ctx) ir_if(this->condition->clone(mem_ctx, ht));
1054b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt
1064b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   foreach_iter(exec_list_iterator, iter, this->then_instructions) {
1074b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt      ir_instruction *ir = (ir_instruction *)iter.get();
1088273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt      new_if->then_instructions.push_tail(ir->clone(mem_ctx, ht));
1094b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   }
1104b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt
1114b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   foreach_iter(exec_list_iterator, iter, this->else_instructions) {
1124b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt      ir_instruction *ir = (ir_instruction *)iter.get();
1138273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt      new_if->else_instructions.push_tail(ir->clone(mem_ctx, ht));
1144b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   }
1154b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt
1164b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   return new_if;
1174b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt}
1184b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt
119ca088cc277ce9f986693c857f3961dc0e1a4d91cIan Romanickir_loop *
1208273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholtir_loop::clone(void *mem_ctx, struct hash_table *ht) const
1214b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt{
1228273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt   ir_loop *new_loop = new(mem_ctx) ir_loop();
1234b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt
1244b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   if (this->from)
1258273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt      new_loop->from = this->from->clone(mem_ctx, ht);
1264b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   if (this->to)
1278273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt      new_loop->to = this->to->clone(mem_ctx, ht);
1284b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   if (this->increment)
1298273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt      new_loop->increment = this->increment->clone(mem_ctx, ht);
1304b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   new_loop->counter = counter;
1314b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt
1324b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   foreach_iter(exec_list_iterator, iter, this->body_instructions) {
1334b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt      ir_instruction *ir = (ir_instruction *)iter.get();
1348273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt      new_loop->body_instructions.push_tail(ir->clone(mem_ctx, ht));
1354b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   }
1364b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt
1374b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   return new_loop;
1384b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt}
1394b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt
140ca088cc277ce9f986693c857f3961dc0e1a4d91cIan Romanickir_call *
1418273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholtir_call::clone(void *mem_ctx, struct hash_table *ht) const
1424b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt{
1434b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   exec_list new_parameters;
1444b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt
1454b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   foreach_iter(exec_list_iterator, iter, this->actual_parameters) {
1464b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt      ir_instruction *ir = (ir_instruction *)iter.get();
1478273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt      new_parameters.push_tail(ir->clone(mem_ctx, ht));
1484b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   }
1494b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt
1508273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt   return new(mem_ctx) ir_call(this->callee, &new_parameters);
1514b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt}
1524b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt
153ca088cc277ce9f986693c857f3961dc0e1a4d91cIan Romanickir_expression *
1548273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholtir_expression::clone(void *mem_ctx, struct hash_table *ht) const
1554b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt{
1564b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   ir_rvalue *op[2] = {NULL, NULL};
1574b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   unsigned int i;
1584b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt
1594b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   for (i = 0; i < get_num_operands(); i++) {
1608273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt      op[i] = this->operands[i]->clone(mem_ctx, ht);
1614b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   }
1624b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt
1638273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt   return new(mem_ctx) ir_expression(this->operation, this->type, op[0], op[1]);
1644b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt}
1654b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt
166ca088cc277ce9f986693c857f3961dc0e1a4d91cIan Romanickir_dereference_variable *
1678273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholtir_dereference_variable::clone(void *mem_ctx, struct hash_table *ht) const
1684b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt{
1694b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   ir_variable *new_var;
1704b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt
1714b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   if (ht) {
1724b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt      new_var = (ir_variable *)hash_table_find(ht, this->var);
1734b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt      if (!new_var)
1744b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt	 new_var = this->var;
1754b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   } else {
1764b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt      new_var = this->var;
1774b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   }
1784b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt
1798273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt   return new(mem_ctx) ir_dereference_variable(new_var);
1804b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt}
1814b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt
182ca088cc277ce9f986693c857f3961dc0e1a4d91cIan Romanickir_dereference_array *
1838273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholtir_dereference_array::clone(void *mem_ctx, struct hash_table *ht) const
1844b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt{
1858273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt   return new(mem_ctx) ir_dereference_array(this->array->clone(mem_ctx, ht),
1868273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt					    this->array_index->clone(mem_ctx,
1878273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt								     ht));
1884b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt}
1894b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt
190ca088cc277ce9f986693c857f3961dc0e1a4d91cIan Romanickir_dereference_record *
1918273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholtir_dereference_record::clone(void *mem_ctx, struct hash_table *ht) const
1924b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt{
1938273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt   return new(mem_ctx) ir_dereference_record(this->record->clone(mem_ctx, ht),
1948273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt					     this->field);
1954b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt}
1964b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt
197ca088cc277ce9f986693c857f3961dc0e1a4d91cIan Romanickir_texture *
1988273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholtir_texture::clone(void *mem_ctx, struct hash_table *ht) const
1994b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt{
2008273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt   ir_texture *new_tex = new(mem_ctx) ir_texture(this->op);
201c3081e627302429cdf2ee23a40fb20fa5cbf5770Eric Anholt   new_tex->type = this->type;
2024b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt
2038273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt   new_tex->sampler = this->sampler->clone(mem_ctx, ht);
2048273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt   new_tex->coordinate = this->coordinate->clone(mem_ctx, ht);
2054b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   if (this->projector)
2068273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt      new_tex->projector = this->projector->clone(mem_ctx, ht);
2074b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   if (this->shadow_comparitor) {
2088273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt      new_tex->shadow_comparitor = this->shadow_comparitor->clone(mem_ctx, ht);
2094b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   }
2104b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt
2114b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   for (int i = 0; i < 3; i++)
2124b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt      new_tex->offsets[i] = this->offsets[i];
2134b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt
2144b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   switch (this->op) {
2154b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   case ir_tex:
2164b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt      break;
2174b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   case ir_txb:
2188273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt      new_tex->lod_info.bias = this->lod_info.bias->clone(mem_ctx, ht);
2194b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt      break;
2204b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   case ir_txl:
2214b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   case ir_txf:
2228273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt      new_tex->lod_info.lod = this->lod_info.lod->clone(mem_ctx, ht);
2234b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt      break;
2244b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   case ir_txd:
2258273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt      new_tex->lod_info.grad.dPdx = this->lod_info.grad.dPdx->clone(mem_ctx, ht);
2268273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt      new_tex->lod_info.grad.dPdy = this->lod_info.grad.dPdy->clone(mem_ctx, ht);
2274b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt      break;
2284b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   }
2294b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt
2304b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   return new_tex;
2314b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt}
2324b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt
233ca088cc277ce9f986693c857f3961dc0e1a4d91cIan Romanickir_assignment *
2348273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholtir_assignment::clone(void *mem_ctx, struct hash_table *ht) const
2354b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt{
2364b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   ir_rvalue *new_condition = NULL;
2374b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt
2384b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt   if (this->condition)
2398273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt      new_condition = this->condition->clone(mem_ctx, ht);
2404b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt
2418273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt   return new(mem_ctx) ir_assignment(this->lhs->clone(mem_ctx, ht),
2428273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt				     this->rhs->clone(mem_ctx, ht),
2435a7758efbe14dee026245a4f4f4fb3ccf7b2c23bIan Romanick				     new_condition,
2445a7758efbe14dee026245a4f4f4fb3ccf7b2c23bIan Romanick				     this->write_mask);
2454b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt}
2464b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt
247ca088cc277ce9f986693c857f3961dc0e1a4d91cIan Romanickir_function *
2488273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholtir_function::clone(void *mem_ctx, struct hash_table *ht) const
2494b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt{
250b50098122696c00e7f9e57089197e25e5fe0e0cfIan Romanick   ir_function *copy = new(mem_ctx) ir_function(this->name);
251b50098122696c00e7f9e57089197e25e5fe0e0cfIan Romanick
252b50098122696c00e7f9e57089197e25e5fe0e0cfIan Romanick   foreach_list_const(node, &this->signatures) {
253b50098122696c00e7f9e57089197e25e5fe0e0cfIan Romanick      const ir_function_signature *const sig =
254b50098122696c00e7f9e57089197e25e5fe0e0cfIan Romanick	 (const ir_function_signature *const) node;
255b50098122696c00e7f9e57089197e25e5fe0e0cfIan Romanick
2568273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt      ir_function_signature *sig_copy = sig->clone(mem_ctx, ht);
257b50098122696c00e7f9e57089197e25e5fe0e0cfIan Romanick      copy->add_signature(sig_copy);
258b50098122696c00e7f9e57089197e25e5fe0e0cfIan Romanick
259b50098122696c00e7f9e57089197e25e5fe0e0cfIan Romanick      if (ht != NULL)
260b50098122696c00e7f9e57089197e25e5fe0e0cfIan Romanick	 hash_table_insert(ht, sig_copy,
261b50098122696c00e7f9e57089197e25e5fe0e0cfIan Romanick			   (void *)const_cast<ir_function_signature *>(sig));
262b50098122696c00e7f9e57089197e25e5fe0e0cfIan Romanick   }
263b50098122696c00e7f9e57089197e25e5fe0e0cfIan Romanick
264b50098122696c00e7f9e57089197e25e5fe0e0cfIan Romanick   return copy;
2654b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt}
2664b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt
267ca088cc277ce9f986693c857f3961dc0e1a4d91cIan Romanickir_function_signature *
2688273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholtir_function_signature::clone(void *mem_ctx, struct hash_table *ht) const
2694b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt{
270b50098122696c00e7f9e57089197e25e5fe0e0cfIan Romanick   ir_function_signature *copy =
271b50098122696c00e7f9e57089197e25e5fe0e0cfIan Romanick      new(mem_ctx) ir_function_signature(this->return_type);
272b50098122696c00e7f9e57089197e25e5fe0e0cfIan Romanick
273b50098122696c00e7f9e57089197e25e5fe0e0cfIan Romanick   copy->is_defined = this->is_defined;
27413f782c4ae4e38e64ec4fe87a1c24597a5e894c3Ian Romanick   copy->is_built_in = this->is_built_in;
275b50098122696c00e7f9e57089197e25e5fe0e0cfIan Romanick
276b50098122696c00e7f9e57089197e25e5fe0e0cfIan Romanick   /* Clone the parameter list.
277b50098122696c00e7f9e57089197e25e5fe0e0cfIan Romanick    */
278b50098122696c00e7f9e57089197e25e5fe0e0cfIan Romanick   foreach_list_const(node, &this->parameters) {
279b50098122696c00e7f9e57089197e25e5fe0e0cfIan Romanick      const ir_variable *const param = (const ir_variable *) node;
280b50098122696c00e7f9e57089197e25e5fe0e0cfIan Romanick
281b50098122696c00e7f9e57089197e25e5fe0e0cfIan Romanick      assert(const_cast<ir_variable *>(param)->as_variable() != NULL);
282b50098122696c00e7f9e57089197e25e5fe0e0cfIan Romanick
2838273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt      ir_variable *const param_copy = param->clone(mem_ctx, ht);
284b50098122696c00e7f9e57089197e25e5fe0e0cfIan Romanick      copy->parameters.push_tail(param_copy);
285b50098122696c00e7f9e57089197e25e5fe0e0cfIan Romanick   }
286b50098122696c00e7f9e57089197e25e5fe0e0cfIan Romanick
287b50098122696c00e7f9e57089197e25e5fe0e0cfIan Romanick   /* Clone the instruction list.
288b50098122696c00e7f9e57089197e25e5fe0e0cfIan Romanick    */
289b50098122696c00e7f9e57089197e25e5fe0e0cfIan Romanick   foreach_list_const(node, &this->body) {
290b50098122696c00e7f9e57089197e25e5fe0e0cfIan Romanick      const ir_instruction *const inst = (const ir_instruction *) node;
291b50098122696c00e7f9e57089197e25e5fe0e0cfIan Romanick
2928273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt      ir_instruction *const inst_copy = inst->clone(mem_ctx, ht);
293b50098122696c00e7f9e57089197e25e5fe0e0cfIan Romanick      copy->body.push_tail(inst_copy);
294b50098122696c00e7f9e57089197e25e5fe0e0cfIan Romanick   }
295b50098122696c00e7f9e57089197e25e5fe0e0cfIan Romanick
296b50098122696c00e7f9e57089197e25e5fe0e0cfIan Romanick   return copy;
2974b6fd39c89f308a379882426c1ed3616d60c4628Eric Anholt}
2985f384088336c23c4fe332d2735450bf455c88200Eric Anholt
299ca088cc277ce9f986693c857f3961dc0e1a4d91cIan Romanickir_constant *
3008273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholtir_constant::clone(void *mem_ctx, struct hash_table *ht) const
3015f384088336c23c4fe332d2735450bf455c88200Eric Anholt{
3029ca0a022e6fcd46a6ce06b8d08dd4c046ec215c4Eric Anholt   (void)ht;
3039ca0a022e6fcd46a6ce06b8d08dd4c046ec215c4Eric Anholt
3045f384088336c23c4fe332d2735450bf455c88200Eric Anholt   switch (this->type->base_type) {
3055f384088336c23c4fe332d2735450bf455c88200Eric Anholt   case GLSL_TYPE_UINT:
3065f384088336c23c4fe332d2735450bf455c88200Eric Anholt   case GLSL_TYPE_INT:
3075f384088336c23c4fe332d2735450bf455c88200Eric Anholt   case GLSL_TYPE_FLOAT:
3085f384088336c23c4fe332d2735450bf455c88200Eric Anholt   case GLSL_TYPE_BOOL:
3098273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt      return new(mem_ctx) ir_constant(this->type, &this->value);
3105f384088336c23c4fe332d2735450bf455c88200Eric Anholt
3115f384088336c23c4fe332d2735450bf455c88200Eric Anholt   case GLSL_TYPE_STRUCT: {
3128273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt      ir_constant *c = new(mem_ctx) ir_constant;
3135f384088336c23c4fe332d2735450bf455c88200Eric Anholt
3145f384088336c23c4fe332d2735450bf455c88200Eric Anholt      c->type = this->type;
3155f384088336c23c4fe332d2735450bf455c88200Eric Anholt      for (exec_node *node = this->components.head
31662c4763b707e2227409f81b09dd5cf6e4410ea6aEric Anholt	      ; !node->is_tail_sentinel()
3175f384088336c23c4fe332d2735450bf455c88200Eric Anholt	      ; node = node->next) {
3185f384088336c23c4fe332d2735450bf455c88200Eric Anholt	 ir_constant *const orig = (ir_constant *) node;
3195f384088336c23c4fe332d2735450bf455c88200Eric Anholt
3208273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt	 c->components.push_tail(orig->clone(mem_ctx, NULL));
3215f384088336c23c4fe332d2735450bf455c88200Eric Anholt      }
3225f384088336c23c4fe332d2735450bf455c88200Eric Anholt
3235f384088336c23c4fe332d2735450bf455c88200Eric Anholt      return c;
3245f384088336c23c4fe332d2735450bf455c88200Eric Anholt   }
3255f384088336c23c4fe332d2735450bf455c88200Eric Anholt
32674e1802f5dd8921750851abc6128e4073602d405Kenneth Graunke   case GLSL_TYPE_ARRAY: {
3278273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt      ir_constant *c = new(mem_ctx) ir_constant;
32874e1802f5dd8921750851abc6128e4073602d405Kenneth Graunke
32974e1802f5dd8921750851abc6128e4073602d405Kenneth Graunke      c->type = this->type;
33074e1802f5dd8921750851abc6128e4073602d405Kenneth Graunke      c->array_elements = talloc_array(c, ir_constant *, this->type->length);
33174e1802f5dd8921750851abc6128e4073602d405Kenneth Graunke      for (unsigned i = 0; i < this->type->length; i++) {
3328273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt	 c->array_elements[i] = this->array_elements[i]->clone(mem_ctx, NULL);
33374e1802f5dd8921750851abc6128e4073602d405Kenneth Graunke      }
33474e1802f5dd8921750851abc6128e4073602d405Kenneth Graunke      return c;
33574e1802f5dd8921750851abc6128e4073602d405Kenneth Graunke   }
33674e1802f5dd8921750851abc6128e4073602d405Kenneth Graunke
3375f384088336c23c4fe332d2735450bf455c88200Eric Anholt   default:
3384b1721eaf35ccb60d90850ab34a99d6ab1f89a05José Fonseca      assert(!"Should not get here.");
3395f384088336c23c4fe332d2735450bf455c88200Eric Anholt      return NULL;
3405f384088336c23c4fe332d2735450bf455c88200Eric Anholt   }
3415f384088336c23c4fe332d2735450bf455c88200Eric Anholt}
342f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick
343f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick
344f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanickclass fixup_ir_call_visitor : public ir_hierarchical_visitor {
345f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanickpublic:
346f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick   fixup_ir_call_visitor(struct hash_table *ht)
347f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick   {
348f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick      this->ht = ht;
349f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick   }
350f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick
351f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick   virtual ir_visitor_status visit_enter(ir_call *ir)
352f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick   {
353f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick      /* Try to find the function signature referenced by the ir_call in the
354f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick       * table.  If it is found, replace it with the value from the table.
355f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick       */
3561f47245bdda2c85bf0f0174e6c24a50486b413aaEric Anholt      ir_function_signature *sig =
357f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick	 (ir_function_signature *) hash_table_find(this->ht, ir->get_callee());
358f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick      if (sig != NULL)
359f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick	 ir->set_callee(sig);
360f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick
361f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick      /* Since this may be used before function call parameters are flattened,
362f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick       * the children also need to be processed.
363f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick       */
364f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick      return visit_continue;
365f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick   }
366f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick
367f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanickprivate:
368f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick   struct hash_table *ht;
369f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick};
370f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick
371f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick
372f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanickstatic void
373f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanickfixup_function_calls(struct hash_table *ht, exec_list *instructions)
374f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick{
375f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick   fixup_ir_call_visitor v(ht);
376f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick   v.run(instructions);
377f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick}
378f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick
379f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick
380f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanickvoid
3818273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholtclone_ir_list(void *mem_ctx, exec_list *out, const exec_list *in)
382f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick{
383f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick   struct hash_table *ht =
384f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick      hash_table_ctor(0, hash_table_pointer_hash, hash_table_pointer_compare);
385f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick
386f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick   foreach_list_const(node, in) {
387f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick      const ir_instruction *const original = (ir_instruction *) node;
3888273bd46877e2ea2b8a02b87a11c68102d07e1f2Eric Anholt      ir_instruction *copy = original->clone(mem_ctx, ht);
389f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick
390f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick      out->push_tail(copy);
391f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick   }
392f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick
393f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick   /* Make a pass over the cloned tree to fix up ir_call nodes to point to the
394f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick    * cloned ir_function_signature nodes.  This cannot be done automatically
395f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick    * during cloning because the ir_call might be a forward reference (i.e.,
396f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick    * the function signature that it references may not have been cloned yet).
397f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick    */
398f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick   fixup_function_calls(ht, out);
399f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick
400f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick   hash_table_dtor(ht);
401f3235eb37f264244f4ea432700be7dd6b2930d6cIan Romanick}
402