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_constant_folding.cpp 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. * \file ir_constant_folding.cpp
  25. * Replace constant-valued expressions with references to constant values.
  26. */
  27. #include "ir.h"
  28. #include "ir_visitor.h"
  29. #include "ir_optimization.h"
  30. #include "glsl_types.h"
  31. /**
  32. * Visitor class for replacing expressions with ir_constant values.
  33. */
  34. class ir_constant_folding_visitor : public ir_visitor {
  35. public:
  36. ir_constant_folding_visitor()
  37. {
  38. /* empty */
  39. }
  40. virtual ~ir_constant_folding_visitor()
  41. {
  42. /* empty */
  43. }
  44. /**
  45. * \name Visit methods
  46. *
  47. * As typical for the visitor pattern, there must be one \c visit method for
  48. * each concrete subclass of \c ir_instruction. Virtual base classes within
  49. * the hierarchy should not have \c visit methods.
  50. */
  51. /*@{*/
  52. virtual void visit(ir_variable *);
  53. virtual void visit(ir_function_signature *);
  54. virtual void visit(ir_function *);
  55. virtual void visit(ir_expression *);
  56. virtual void visit(ir_texture *);
  57. virtual void visit(ir_swizzle *);
  58. virtual void visit(ir_dereference_variable *);
  59. virtual void visit(ir_dereference_array *);
  60. virtual void visit(ir_dereference_record *);
  61. virtual void visit(ir_assignment *);
  62. virtual void visit(ir_constant *);
  63. virtual void visit(ir_call *);
  64. virtual void visit(ir_return *);
  65. virtual void visit(ir_if *);
  66. virtual void visit(ir_loop *);
  67. virtual void visit(ir_loop_jump *);
  68. /*@}*/
  69. };
  70. void
  71. ir_constant_folding_visitor::visit(ir_variable *ir)
  72. {
  73. (void) ir;
  74. }
  75. void
  76. ir_constant_folding_visitor::visit(ir_function_signature *ir)
  77. {
  78. visit_exec_list(&ir->body, this);
  79. }
  80. void
  81. ir_constant_folding_visitor::visit(ir_function *ir)
  82. {
  83. foreach_iter(exec_list_iterator, iter, *ir) {
  84. ir_function_signature *const sig = (ir_function_signature *) iter.get();
  85. sig->accept(this);
  86. }
  87. }
  88. void
  89. ir_constant_folding_visitor::visit(ir_expression *ir)
  90. {
  91. ir_constant *op[2];
  92. unsigned int operand;
  93. for (operand = 0; operand < ir->get_num_operands(); operand++) {
  94. op[operand] = ir->operands[operand]->constant_expression_value();
  95. if (op[operand]) {
  96. ir->operands[operand] = op[operand];
  97. } else {
  98. ir->operands[operand]->accept(this);
  99. }
  100. }
  101. }
  102. void
  103. ir_constant_folding_visitor::visit(ir_texture *ir)
  104. {
  105. // FINISHME: Do stuff with texture lookups
  106. (void) ir;
  107. }
  108. void
  109. ir_constant_folding_visitor::visit(ir_swizzle *ir)
  110. {
  111. ir->val->accept(this);
  112. }
  113. void
  114. ir_constant_folding_visitor::visit(ir_dereference_variable *ir)
  115. {
  116. (void) ir;
  117. }
  118. void
  119. ir_constant_folding_visitor::visit(ir_dereference_array *ir)
  120. {
  121. ir_constant *const_val =
  122. ir->array_index->constant_expression_value();
  123. if (const_val)
  124. ir->array_index = const_val;
  125. else
  126. ir->array_index->accept(this);
  127. ir->array->accept(this);
  128. }
  129. void
  130. ir_constant_folding_visitor::visit(ir_dereference_record *ir)
  131. {
  132. ir->record->accept(this);
  133. }
  134. void
  135. ir_constant_folding_visitor::visit(ir_assignment *ir)
  136. {
  137. ir_constant *const_val = ir->rhs->constant_expression_value();
  138. if (const_val)
  139. ir->rhs = const_val;
  140. else
  141. ir->rhs->accept(this);
  142. }
  143. void
  144. ir_constant_folding_visitor::visit(ir_constant *ir)
  145. {
  146. (void) ir;
  147. }
  148. void
  149. ir_constant_folding_visitor::visit(ir_call *ir)
  150. {
  151. (void) ir;
  152. }
  153. void
  154. ir_constant_folding_visitor::visit(ir_return *ir)
  155. {
  156. (void) ir;
  157. }
  158. void
  159. ir_constant_folding_visitor::visit(ir_if *ir)
  160. {
  161. ir_constant *const_val = ir->condition->constant_expression_value();
  162. if (const_val)
  163. ir->condition = const_val;
  164. else
  165. ir->condition->accept(this);
  166. visit_exec_list(&ir->then_instructions, this);
  167. visit_exec_list(&ir->else_instructions, this);
  168. }
  169. void
  170. ir_constant_folding_visitor::visit(ir_loop *ir)
  171. {
  172. (void) ir;
  173. }
  174. void
  175. ir_constant_folding_visitor::visit(ir_loop_jump *ir)
  176. {
  177. (void) ir;
  178. }
  179. bool
  180. do_constant_folding(exec_list *instructions)
  181. {
  182. ir_constant_folding_visitor constant_folding;
  183. visit_exec_list(instructions, &constant_folding);
  184. /* FINISHME: Return real progress. */
  185. return false;
  186. }