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.9KB

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