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_dead_code_local.cpp 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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_dead_code_local.cpp
  25. *
  26. * Eliminates local dead assignments from the code.
  27. *
  28. * This operates on basic blocks, tracking assignments and finding if
  29. * they're used before the variable is completely reassigned.
  30. *
  31. * Compare this to ir_dead_code.cpp, which operates globally looking
  32. * for assignments to variables that are never read.
  33. */
  34. #include "ir.h"
  35. #include "ir_basic_block.h"
  36. #include "ir_optimization.h"
  37. #include "glsl_types.h"
  38. static bool debug = false;
  39. class assignment_entry : public exec_node
  40. {
  41. public:
  42. assignment_entry(ir_variable *lhs, ir_instruction *ir)
  43. {
  44. assert(lhs);
  45. assert(ir);
  46. this->lhs = lhs;
  47. this->ir = ir;
  48. }
  49. ir_variable *lhs;
  50. ir_instruction *ir;
  51. };
  52. class kill_for_derefs_visitor : public ir_hierarchical_visitor {
  53. public:
  54. kill_for_derefs_visitor(exec_list *assignments)
  55. {
  56. this->assignments = assignments;
  57. }
  58. virtual ir_visitor_status visit(ir_dereference_variable *ir)
  59. {
  60. ir_variable *const var = ir->variable_referenced();
  61. foreach_iter(exec_list_iterator, iter, *this->assignments) {
  62. assignment_entry *entry = (assignment_entry *)iter.get();
  63. if (entry->lhs == var) {
  64. if (debug)
  65. printf("kill %s\n", entry->lhs->name);
  66. entry->remove();
  67. }
  68. }
  69. return visit_continue;
  70. }
  71. private:
  72. exec_list *assignments;
  73. };
  74. class array_index_visit : public ir_hierarchical_visitor {
  75. public:
  76. array_index_visit(ir_hierarchical_visitor *v)
  77. {
  78. this->visitor = v;
  79. }
  80. virtual ir_visitor_status visit_enter(class ir_dereference_array *ir)
  81. {
  82. ir->array_index->accept(visitor);
  83. return visit_continue;
  84. }
  85. static void run(ir_instruction *ir, ir_hierarchical_visitor *v)
  86. {
  87. array_index_visit top_visit(v);
  88. ir->accept(& top_visit);
  89. }
  90. ir_hierarchical_visitor *visitor;
  91. };
  92. /**
  93. * Adds an entry to the available copy list if it's a plain assignment
  94. * of a variable to a variable.
  95. */
  96. static bool
  97. process_assignment(ir_assignment *ir, exec_list *assignments)
  98. {
  99. ir_variable *var = NULL;
  100. bool progress = false;
  101. kill_for_derefs_visitor v(assignments);
  102. /* Kill assignment entries for things used to produce this assignment. */
  103. ir->rhs->accept(&v);
  104. if (ir->condition) {
  105. ir->condition->accept(&v);
  106. }
  107. /* Kill assignment enties used as array indices.
  108. */
  109. array_index_visit::run(ir->lhs, &v);
  110. var = ir->lhs->variable_referenced();
  111. assert(var);
  112. bool always_assign = true;
  113. if (ir->condition) {
  114. ir_constant *condition = ir->condition->as_constant();
  115. if (!condition || !condition->value.b[0])
  116. always_assign = false;
  117. }
  118. /* Now, check if we did a whole-variable assignment. */
  119. if (always_assign && (ir->lhs->whole_variable_referenced() != NULL)) {
  120. /* We did a whole-variable assignment. So, any instruction in
  121. * the assignment list with the same LHS is dead.
  122. */
  123. if (debug)
  124. printf("looking for %s to remove\n", var->name);
  125. foreach_iter(exec_list_iterator, iter, *assignments) {
  126. assignment_entry *entry = (assignment_entry *)iter.get();
  127. if (entry->lhs == var) {
  128. if (debug)
  129. printf("removing %s\n", var->name);
  130. entry->ir->remove();
  131. entry->remove();
  132. progress = true;
  133. }
  134. }
  135. }
  136. /* Add this instruction to the assignment list. */
  137. assignment_entry *entry = new assignment_entry(var, ir);
  138. assignments->push_tail(entry);
  139. if (debug) {
  140. printf("add %s\n", var->name);
  141. printf("current entries\n");
  142. foreach_iter(exec_list_iterator, iter, *assignments) {
  143. assignment_entry *entry = (assignment_entry *)iter.get();
  144. printf(" %s\n", entry->lhs->name);
  145. }
  146. }
  147. return progress;
  148. }
  149. static void
  150. dead_code_local_basic_block(ir_instruction *first,
  151. ir_instruction *last,
  152. void *data)
  153. {
  154. ir_instruction *ir, *ir_next;
  155. /* List of avaialble_copy */
  156. exec_list assignments;
  157. bool *out_progress = (bool *)data;
  158. bool progress = false;
  159. /* Safe looping, since process_assignment */
  160. for (ir = first, ir_next = (ir_instruction *)first->next;;
  161. ir = ir_next, ir_next = (ir_instruction *)ir->next) {
  162. ir_assignment *ir_assign = ir->as_assignment();
  163. if (debug) {
  164. ir->print();
  165. printf("\n");
  166. }
  167. if (ir_assign) {
  168. progress = process_assignment(ir_assign, &assignments) || progress;
  169. } else {
  170. kill_for_derefs_visitor kill(&assignments);
  171. ir->accept(&kill);
  172. }
  173. if (ir == last)
  174. break;
  175. }
  176. *out_progress = progress;
  177. }
  178. /**
  179. * Does a copy propagation pass on the code present in the instruction stream.
  180. */
  181. bool
  182. do_dead_code_local(exec_list *instructions)
  183. {
  184. bool progress = false;
  185. call_for_basic_blocks(instructions, dead_code_local_basic_block, &progress);
  186. return progress;
  187. }