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

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