Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ir_clone.cpp 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. #include <string.h>
  24. #include "main/compiler.h"
  25. #include "ir.h"
  26. #include "glsl_types.h"
  27. extern "C" {
  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. var->max_array_access = this->max_array_access;
  43. var->read_only = this->read_only;
  44. var->centroid = this->centroid;
  45. var->invariant = this->invariant;
  46. var->interpolation = this->interpolation;
  47. var->array_lvalue = this->array_lvalue;
  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. if (this->explicit_location)
  54. var->location = this->location;
  55. if (this->constant_value)
  56. var->constant_value = this->constant_value->clone(mem_ctx, ht);
  57. if (ht) {
  58. hash_table_insert(ht, var, (void *)const_cast<ir_variable *>(this));
  59. }
  60. return var;
  61. }
  62. ir_swizzle *
  63. ir_swizzle::clone(void *mem_ctx, struct hash_table *ht) const
  64. {
  65. return new(mem_ctx) ir_swizzle(this->val->clone(mem_ctx, ht), this->mask);
  66. }
  67. ir_return *
  68. ir_return::clone(void *mem_ctx, struct hash_table *ht) const
  69. {
  70. ir_rvalue *new_value = NULL;
  71. if (this->value)
  72. new_value = this->value->clone(mem_ctx, ht);
  73. return new(mem_ctx) ir_return(new_value);
  74. }
  75. ir_discard *
  76. ir_discard::clone(void *mem_ctx, struct hash_table *ht) const
  77. {
  78. ir_rvalue *new_condition = NULL;
  79. if (this->condition != NULL)
  80. new_condition = this->condition->clone(mem_ctx, ht);
  81. return new(mem_ctx) ir_discard(new_condition);
  82. }
  83. ir_loop_jump *
  84. ir_loop_jump::clone(void *mem_ctx, struct hash_table *ht) const
  85. {
  86. (void)ht;
  87. return new(mem_ctx) ir_loop_jump(this->mode);
  88. }
  89. ir_if *
  90. ir_if::clone(void *mem_ctx, struct hash_table *ht) const
  91. {
  92. ir_if *new_if = new(mem_ctx) ir_if(this->condition->clone(mem_ctx, ht));
  93. foreach_iter(exec_list_iterator, iter, this->then_instructions) {
  94. ir_instruction *ir = (ir_instruction *)iter.get();
  95. new_if->then_instructions.push_tail(ir->clone(mem_ctx, ht));
  96. }
  97. foreach_iter(exec_list_iterator, iter, this->else_instructions) {
  98. ir_instruction *ir = (ir_instruction *)iter.get();
  99. new_if->else_instructions.push_tail(ir->clone(mem_ctx, ht));
  100. }
  101. return new_if;
  102. }
  103. ir_loop *
  104. ir_loop::clone(void *mem_ctx, struct hash_table *ht) const
  105. {
  106. ir_loop *new_loop = new(mem_ctx) ir_loop();
  107. if (this->from)
  108. new_loop->from = this->from->clone(mem_ctx, ht);
  109. if (this->to)
  110. new_loop->to = this->to->clone(mem_ctx, ht);
  111. if (this->increment)
  112. new_loop->increment = this->increment->clone(mem_ctx, ht);
  113. new_loop->counter = counter;
  114. foreach_iter(exec_list_iterator, iter, this->body_instructions) {
  115. ir_instruction *ir = (ir_instruction *)iter.get();
  116. new_loop->body_instructions.push_tail(ir->clone(mem_ctx, ht));
  117. }
  118. new_loop->cmp = this->cmp;
  119. return new_loop;
  120. }
  121. ir_call *
  122. ir_call::clone(void *mem_ctx, struct hash_table *ht) const
  123. {
  124. if (this->type == glsl_type::error_type)
  125. return ir_call::get_error_instruction(mem_ctx);
  126. exec_list new_parameters;
  127. foreach_iter(exec_list_iterator, iter, this->actual_parameters) {
  128. ir_instruction *ir = (ir_instruction *)iter.get();
  129. new_parameters.push_tail(ir->clone(mem_ctx, ht));
  130. }
  131. return new(mem_ctx) ir_call(this->callee, &new_parameters);
  132. }
  133. ir_expression *
  134. ir_expression::clone(void *mem_ctx, struct hash_table *ht) const
  135. {
  136. ir_rvalue *op[Elements(this->operands)] = { NULL, };
  137. unsigned int i;
  138. for (i = 0; i < get_num_operands(); i++) {
  139. op[i] = this->operands[i]->clone(mem_ctx, ht);
  140. }
  141. return new(mem_ctx) ir_expression(this->operation, this->type,
  142. op[0], op[1], op[2], op[3]);
  143. }
  144. ir_dereference_variable *
  145. ir_dereference_variable::clone(void *mem_ctx, struct hash_table *ht) const
  146. {
  147. ir_variable *new_var;
  148. if (ht) {
  149. new_var = (ir_variable *)hash_table_find(ht, this->var);
  150. if (!new_var)
  151. new_var = this->var;
  152. } else {
  153. new_var = this->var;
  154. }
  155. return new(mem_ctx) ir_dereference_variable(new_var);
  156. }
  157. ir_dereference_array *
  158. ir_dereference_array::clone(void *mem_ctx, struct hash_table *ht) const
  159. {
  160. return new(mem_ctx) ir_dereference_array(this->array->clone(mem_ctx, ht),
  161. this->array_index->clone(mem_ctx,
  162. ht));
  163. }
  164. ir_dereference_record *
  165. ir_dereference_record::clone(void *mem_ctx, struct hash_table *ht) const
  166. {
  167. return new(mem_ctx) ir_dereference_record(this->record->clone(mem_ctx, ht),
  168. this->field);
  169. }
  170. ir_texture *
  171. ir_texture::clone(void *mem_ctx, struct hash_table *ht) const
  172. {
  173. ir_texture *new_tex = new(mem_ctx) ir_texture(this->op);
  174. new_tex->type = this->type;
  175. new_tex->sampler = this->sampler->clone(mem_ctx, ht);
  176. new_tex->coordinate = this->coordinate->clone(mem_ctx, ht);
  177. if (this->projector)
  178. new_tex->projector = this->projector->clone(mem_ctx, ht);
  179. if (this->shadow_comparitor) {
  180. new_tex->shadow_comparitor = this->shadow_comparitor->clone(mem_ctx, ht);
  181. }
  182. if (this->offset != NULL)
  183. new_tex->offset = this->offset->clone(mem_ctx, ht);
  184. switch (this->op) {
  185. case ir_tex:
  186. break;
  187. case ir_txb:
  188. new_tex->lod_info.bias = this->lod_info.bias->clone(mem_ctx, ht);
  189. break;
  190. case ir_txl:
  191. case ir_txf:
  192. new_tex->lod_info.lod = this->lod_info.lod->clone(mem_ctx, ht);
  193. break;
  194. case ir_txd:
  195. new_tex->lod_info.grad.dPdx = this->lod_info.grad.dPdx->clone(mem_ctx, ht);
  196. new_tex->lod_info.grad.dPdy = this->lod_info.grad.dPdy->clone(mem_ctx, ht);
  197. break;
  198. }
  199. return new_tex;
  200. }
  201. ir_assignment *
  202. ir_assignment::clone(void *mem_ctx, struct hash_table *ht) const
  203. {
  204. ir_rvalue *new_condition = NULL;
  205. if (this->condition)
  206. new_condition = this->condition->clone(mem_ctx, ht);
  207. return new(mem_ctx) ir_assignment(this->lhs->clone(mem_ctx, ht),
  208. this->rhs->clone(mem_ctx, ht),
  209. new_condition,
  210. this->write_mask);
  211. }
  212. ir_function *
  213. ir_function::clone(void *mem_ctx, struct hash_table *ht) const
  214. {
  215. ir_function *copy = new(mem_ctx) ir_function(this->name);
  216. foreach_list_const(node, &this->signatures) {
  217. const ir_function_signature *const sig =
  218. (const ir_function_signature *const) node;
  219. ir_function_signature *sig_copy = sig->clone(mem_ctx, ht);
  220. copy->add_signature(sig_copy);
  221. if (ht != NULL)
  222. hash_table_insert(ht, sig_copy,
  223. (void *)const_cast<ir_function_signature *>(sig));
  224. }
  225. return copy;
  226. }
  227. ir_function_signature *
  228. ir_function_signature::clone(void *mem_ctx, struct hash_table *ht) const
  229. {
  230. ir_function_signature *copy = this->clone_prototype(mem_ctx, ht);
  231. copy->is_defined = this->is_defined;
  232. /* Clone the instruction list.
  233. */
  234. foreach_list_const(node, &this->body) {
  235. const ir_instruction *const inst = (const ir_instruction *) node;
  236. ir_instruction *const inst_copy = inst->clone(mem_ctx, ht);
  237. copy->body.push_tail(inst_copy);
  238. }
  239. return copy;
  240. }
  241. ir_function_signature *
  242. ir_function_signature::clone_prototype(void *mem_ctx, struct hash_table *ht) const
  243. {
  244. ir_function_signature *copy =
  245. new(mem_ctx) ir_function_signature(this->return_type);
  246. copy->is_defined = false;
  247. copy->is_builtin = this->is_builtin;
  248. /* Clone the parameter list, but NOT the body.
  249. */
  250. foreach_list_const(node, &this->parameters) {
  251. const ir_variable *const param = (const ir_variable *) node;
  252. assert(const_cast<ir_variable *>(param)->as_variable() != NULL);
  253. ir_variable *const param_copy = param->clone(mem_ctx, ht);
  254. copy->parameters.push_tail(param_copy);
  255. }
  256. return copy;
  257. }
  258. ir_constant *
  259. ir_constant::clone(void *mem_ctx, struct hash_table *ht) const
  260. {
  261. (void)ht;
  262. switch (this->type->base_type) {
  263. case GLSL_TYPE_UINT:
  264. case GLSL_TYPE_INT:
  265. case GLSL_TYPE_FLOAT:
  266. case GLSL_TYPE_BOOL:
  267. return new(mem_ctx) ir_constant(this->type, &this->value);
  268. case GLSL_TYPE_STRUCT: {
  269. ir_constant *c = new(mem_ctx) ir_constant;
  270. c->type = this->type;
  271. for (exec_node *node = this->components.head
  272. ; !node->is_tail_sentinel()
  273. ; node = node->next) {
  274. ir_constant *const orig = (ir_constant *) node;
  275. c->components.push_tail(orig->clone(mem_ctx, NULL));
  276. }
  277. return c;
  278. }
  279. case GLSL_TYPE_ARRAY: {
  280. ir_constant *c = new(mem_ctx) ir_constant;
  281. c->type = this->type;
  282. c->array_elements = ralloc_array(c, ir_constant *, this->type->length);
  283. for (unsigned i = 0; i < this->type->length; i++) {
  284. c->array_elements[i] = this->array_elements[i]->clone(mem_ctx, NULL);
  285. }
  286. return c;
  287. }
  288. default:
  289. assert(!"Should not get here.");
  290. return NULL;
  291. }
  292. }
  293. class fixup_ir_call_visitor : public ir_hierarchical_visitor {
  294. public:
  295. fixup_ir_call_visitor(struct hash_table *ht)
  296. {
  297. this->ht = ht;
  298. }
  299. virtual ir_visitor_status visit_enter(ir_call *ir)
  300. {
  301. /* Try to find the function signature referenced by the ir_call in the
  302. * table. If it is found, replace it with the value from the table.
  303. */
  304. ir_function_signature *sig =
  305. (ir_function_signature *) hash_table_find(this->ht, ir->get_callee());
  306. if (sig != NULL)
  307. ir->set_callee(sig);
  308. /* Since this may be used before function call parameters are flattened,
  309. * the children also need to be processed.
  310. */
  311. return visit_continue;
  312. }
  313. private:
  314. struct hash_table *ht;
  315. };
  316. static void
  317. fixup_function_calls(struct hash_table *ht, exec_list *instructions)
  318. {
  319. fixup_ir_call_visitor v(ht);
  320. v.run(instructions);
  321. }
  322. void
  323. clone_ir_list(void *mem_ctx, exec_list *out, const exec_list *in)
  324. {
  325. struct hash_table *ht =
  326. hash_table_ctor(0, hash_table_pointer_hash, hash_table_pointer_compare);
  327. foreach_list_const(node, in) {
  328. const ir_instruction *const original = (ir_instruction *) node;
  329. ir_instruction *copy = original->clone(mem_ctx, ht);
  330. out->push_tail(copy);
  331. }
  332. /* Make a pass over the cloned tree to fix up ir_call nodes to point to the
  333. * cloned ir_function_signature nodes. This cannot be done automatically
  334. * during cloning because the ir_call might be a forward reference (i.e.,
  335. * the function signature that it references may not have been cloned yet).
  336. */
  337. fixup_function_calls(ht, out);
  338. hash_table_dtor(ht);
  339. }