Clone of mesa.
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

ir_copy_propagation.cpp 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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_copy_propagation.cpp
  25. *
  26. * Moves usage of recently-copied variables to the previous copy of
  27. * the variable within basic blocks.
  28. *
  29. * This should reduce the number of MOV instructions in the generated
  30. * programs unless copy propagation is also done on the LIR, and may
  31. * help anyway by triggering other optimizations that live in the HIR.
  32. */
  33. #include "ir.h"
  34. #include "ir_visitor.h"
  35. #include "ir_basic_block.h"
  36. #include "ir_optimization.h"
  37. #include "glsl_types.h"
  38. class acp_entry : public exec_node
  39. {
  40. public:
  41. acp_entry(ir_variable *lhs, ir_variable *rhs)
  42. {
  43. assert(lhs);
  44. assert(rhs);
  45. this->lhs = lhs;
  46. this->rhs = rhs;
  47. }
  48. ir_variable *lhs;
  49. ir_variable *rhs;
  50. };
  51. class ir_copy_propagation_visitor : public ir_hierarchical_visitor {
  52. public:
  53. ir_copy_propagation_visitor(exec_list *acp)
  54. {
  55. progress = false;
  56. in_lhs = false;
  57. this->acp = acp;
  58. }
  59. virtual ir_visitor_status visit(class ir_dereference_variable *);
  60. virtual ir_visitor_status visit_enter(class ir_loop *);
  61. virtual ir_visitor_status visit_enter(class ir_function_signature *);
  62. virtual ir_visitor_status visit_enter(class ir_function *);
  63. virtual ir_visitor_status visit_enter(class ir_assignment *);
  64. virtual ir_visitor_status visit_enter(class ir_call *);
  65. virtual ir_visitor_status visit_enter(class ir_if *);
  66. /** List of acp_entry */
  67. exec_list *acp;
  68. bool progress;
  69. /** Currently in the LHS of an assignment? */
  70. bool in_lhs;
  71. };
  72. ir_visitor_status
  73. ir_copy_propagation_visitor::visit_enter(ir_loop *ir)
  74. {
  75. (void)ir;
  76. return visit_continue_with_parent;
  77. }
  78. ir_visitor_status
  79. ir_copy_propagation_visitor::visit_enter(ir_function_signature *ir)
  80. {
  81. (void)ir;
  82. return visit_continue_with_parent;
  83. }
  84. ir_visitor_status
  85. ir_copy_propagation_visitor::visit_enter(ir_assignment *ir)
  86. {
  87. (void) ir;
  88. this->in_lhs = true;
  89. return visit_continue;
  90. }
  91. ir_visitor_status
  92. ir_copy_propagation_visitor::visit_enter(ir_function *ir)
  93. {
  94. (void) ir;
  95. return visit_continue_with_parent;
  96. }
  97. /**
  98. * Replaces dereferences of ACP RHS variables with ACP LHS variables.
  99. *
  100. * This is where the actual copy propagation occurs. Note that the
  101. * rewriting of ir_dereference means that the ir_dereference instance
  102. * must not be shared by multiple IR operations!
  103. */
  104. ir_visitor_status
  105. ir_copy_propagation_visitor::visit(ir_dereference_variable *ir)
  106. {
  107. /* Ignores the LHS. Don't want to rewrite the LHS to point at some
  108. * other storage!
  109. */
  110. if (this->in_lhs) {
  111. this->in_lhs = false;
  112. return visit_continue;
  113. }
  114. ir_variable *var = ir->variable_referenced();
  115. foreach_iter(exec_list_iterator, iter, *this->acp) {
  116. acp_entry *entry = (acp_entry *)iter.get();
  117. if (var == entry->lhs) {
  118. ir->var = entry->rhs;
  119. this->progress = true;
  120. break;
  121. }
  122. }
  123. return visit_continue;
  124. }
  125. ir_visitor_status
  126. ir_copy_propagation_visitor::visit_enter(ir_call *ir)
  127. {
  128. (void)ir;
  129. /* Note, if we were to do copy propagation to parameters of calls, we'd
  130. * have to be careful about out params.
  131. */
  132. return visit_continue_with_parent;
  133. }
  134. ir_visitor_status
  135. ir_copy_propagation_visitor::visit_enter(ir_if *ir)
  136. {
  137. ir->condition->accept(this);
  138. /* Do not traverse into the body of the if-statement since that is a
  139. * different basic block.
  140. */
  141. return visit_continue_with_parent;
  142. }
  143. static bool
  144. propagate_copies(ir_instruction *ir, exec_list *acp)
  145. {
  146. ir_copy_propagation_visitor v(acp);
  147. ir->accept(&v);
  148. return v.progress;
  149. }
  150. static void
  151. kill_invalidated_copies(ir_assignment *ir, exec_list *acp)
  152. {
  153. ir_variable *var = ir->lhs->variable_referenced();
  154. assert(var != NULL);
  155. foreach_iter(exec_list_iterator, iter, *acp) {
  156. acp_entry *entry = (acp_entry *)iter.get();
  157. if (entry->lhs == var || entry->rhs == var) {
  158. entry->remove();
  159. }
  160. }
  161. }
  162. /**
  163. * Adds an entry to the available copy list if it's a plain assignment
  164. * of a variable to a variable.
  165. */
  166. static void
  167. add_copy(ir_assignment *ir, exec_list *acp)
  168. {
  169. acp_entry *entry;
  170. if (ir->condition) {
  171. ir_constant *condition = ir->condition->as_constant();
  172. if (!condition || !condition->value.b[0])
  173. return;
  174. }
  175. ir_variable *lhs_var = ir->lhs->whole_variable_referenced();
  176. ir_variable *rhs_var = ir->rhs->whole_variable_referenced();
  177. if ((lhs_var != NULL) && (rhs_var != NULL)) {
  178. entry = new acp_entry(lhs_var, rhs_var);
  179. acp->push_tail(entry);
  180. }
  181. }
  182. static void
  183. copy_propagation_basic_block(ir_instruction *first,
  184. ir_instruction *last,
  185. void *data)
  186. {
  187. ir_instruction *ir;
  188. /* List of avaialble_copy */
  189. exec_list acp;
  190. bool *out_progress = (bool *)data;
  191. bool progress = false;
  192. for (ir = first;; ir = (ir_instruction *)ir->next) {
  193. ir_assignment *ir_assign = ir->as_assignment();
  194. progress = propagate_copies(ir, &acp) || progress;
  195. if (ir_assign) {
  196. kill_invalidated_copies(ir_assign, &acp);
  197. add_copy(ir_assign, &acp);
  198. }
  199. if (ir == last)
  200. break;
  201. }
  202. *out_progress = progress;
  203. }
  204. /**
  205. * Does a copy propagation pass on the code present in the instruction stream.
  206. */
  207. bool
  208. do_copy_propagation(exec_list *instructions)
  209. {
  210. bool progress = false;
  211. call_for_basic_blocks(instructions, copy_propagation_basic_block, &progress);
  212. return progress;
  213. }