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.cpp 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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.cpp
  25. *
  26. * Eliminates dead assignments and variable declarations from the code.
  27. */
  28. #include "ir.h"
  29. #include "ir_visitor.h"
  30. #include "ir_expression_flattening.h"
  31. #include "glsl_types.h"
  32. class variable_entry : public exec_node
  33. {
  34. public:
  35. variable_entry(ir_variable *var)
  36. {
  37. this->var = var;
  38. assign = NULL;
  39. referenced_count = 0;
  40. assigned_count = 0;
  41. declaration = false;
  42. }
  43. ir_variable *var; /* The key: the variable's pointer. */
  44. ir_assignment *assign; /* An assignment to the variable, if any */
  45. /** Number of times the variable is referenced, including assignments. */
  46. unsigned referenced_count;
  47. /** Number of times the variable is assignmened. */
  48. unsigned assigned_count;
  49. bool declaration; /* If the variable had a decl in the instruction stream */
  50. };
  51. class ir_dead_code_visitor : public ir_hierarchical_visitor {
  52. public:
  53. virtual ir_visitor_status visit(ir_variable *);
  54. virtual ir_visitor_status visit(ir_dereference_variable *);
  55. virtual ir_visitor_status visit_enter(ir_function *);
  56. virtual ir_visitor_status visit_leave(ir_assignment *);
  57. variable_entry *get_variable_entry(ir_variable *var);
  58. bool (*predicate)(ir_instruction *ir);
  59. ir_instruction *base_ir;
  60. /* List of variable_entry */
  61. exec_list variable_list;
  62. };
  63. variable_entry *
  64. ir_dead_code_visitor::get_variable_entry(ir_variable *var)
  65. {
  66. assert(var);
  67. foreach_iter(exec_list_iterator, iter, this->variable_list) {
  68. variable_entry *entry = (variable_entry *)iter.get();
  69. if (entry->var == var)
  70. return entry;
  71. }
  72. variable_entry *entry = new variable_entry(var);
  73. this->variable_list.push_tail(entry);
  74. return entry;
  75. }
  76. ir_visitor_status
  77. ir_dead_code_visitor::visit(ir_variable *ir)
  78. {
  79. variable_entry *entry = this->get_variable_entry(ir);
  80. if (entry)
  81. entry->declaration = true;
  82. return visit_continue;
  83. }
  84. ir_visitor_status
  85. ir_dead_code_visitor::visit(ir_dereference_variable *ir)
  86. {
  87. ir_variable *const var = ir->variable_referenced();
  88. variable_entry *entry = this->get_variable_entry(var);
  89. if (entry)
  90. entry->referenced_count++;
  91. return visit_continue;
  92. }
  93. ir_visitor_status
  94. ir_dead_code_visitor::visit_enter(ir_function *ir)
  95. {
  96. (void) ir;
  97. return visit_continue_with_parent;
  98. }
  99. ir_visitor_status
  100. ir_dead_code_visitor::visit_leave(ir_assignment *ir)
  101. {
  102. variable_entry *entry;
  103. entry = this->get_variable_entry(ir->lhs->variable_referenced());
  104. if (entry) {
  105. entry->assigned_count++;
  106. if (entry->assign == NULL)
  107. entry->assign = ir;
  108. }
  109. return visit_continue;
  110. }
  111. /**
  112. * Do a dead code pass over instructions and everything that instructions
  113. * references.
  114. *
  115. * Note that this will remove assignments to globals, so it is not suitable
  116. * for usage on an unlinked instruction stream.
  117. */
  118. bool
  119. do_dead_code(exec_list *instructions)
  120. {
  121. ir_dead_code_visitor v;
  122. bool progress = false;
  123. v.run(instructions);
  124. foreach_iter(exec_list_iterator, iter, v.variable_list) {
  125. variable_entry *entry = (variable_entry *)iter.get();
  126. /* Since each assignment is a reference, the refereneced count must be
  127. * greater than or equal to the assignment count. If they are equal,
  128. * then all of the references are assignments, and the variable is
  129. * dead.
  130. *
  131. * Note that if the variable is neither assigned nor referenced, both
  132. * counts will be zero and will be caught by the equality test.
  133. */
  134. assert(entry->referenced_count >= entry->assigned_count);
  135. if ((entry->referenced_count > entry->assigned_count)
  136. || !entry->declaration)
  137. continue;
  138. if (entry->assign) {
  139. /* Remove a single dead assignment to the variable we found.
  140. * Don't do so if it's a shader output, though.
  141. */
  142. if (!entry->var->shader_out) {
  143. entry->assign->remove();
  144. progress = true;
  145. }
  146. } else {
  147. /* If there are no assignments or references to the variable left,
  148. * then we can remove its declaration.
  149. */
  150. entry->var->remove();
  151. progress = true;
  152. }
  153. }
  154. return progress;
  155. }
  156. /**
  157. * Does a dead code pass on the functions present in the instruction stream.
  158. *
  159. * This is suitable for use while the program is not linked, as it will
  160. * ignore variable declarations (and the assignments to them) for variables
  161. * with global scope.
  162. */
  163. bool
  164. do_dead_code_unlinked(exec_list *instructions)
  165. {
  166. bool progress = false;
  167. foreach_iter(exec_list_iterator, iter, *instructions) {
  168. ir_instruction *ir = (ir_instruction *)iter.get();
  169. ir_function *f = ir->as_function();
  170. if (f) {
  171. foreach_iter(exec_list_iterator, sigiter, *f) {
  172. ir_function_signature *sig =
  173. (ir_function_signature *) sigiter.get();
  174. if (do_dead_code(&sig->body))
  175. progress = true;
  176. }
  177. }
  178. }
  179. return progress;
  180. }