Clone of mesa.
Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

ir_copy_propagation.cpp 6.4KB

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