Clone of mesa.
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

opt_dead_code_local.cpp 8.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  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 opt_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. namespace {
  40. class assignment_entry : public exec_node
  41. {
  42. public:
  43. assignment_entry(ir_variable *lhs, ir_assignment *ir)
  44. {
  45. assert(lhs);
  46. assert(ir);
  47. this->lhs = lhs;
  48. this->ir = ir;
  49. this->available = ir->write_mask;
  50. }
  51. ir_variable *lhs;
  52. ir_assignment *ir;
  53. /* bitmask of xyzw channels written that haven't been used so far. */
  54. int available;
  55. };
  56. class kill_for_derefs_visitor : public ir_hierarchical_visitor {
  57. public:
  58. kill_for_derefs_visitor(exec_list *assignments)
  59. {
  60. this->assignments = assignments;
  61. }
  62. void kill_channels(ir_variable *const var, int used)
  63. {
  64. foreach_iter(exec_list_iterator, iter, *this->assignments) {
  65. assignment_entry *entry = (assignment_entry *)iter.get();
  66. if (entry->lhs == var) {
  67. if (var->type->is_scalar() || var->type->is_vector()) {
  68. if (debug)
  69. printf("kill %s (0x%01x - 0x%01x)\n", entry->lhs->name,
  70. entry->available, used);
  71. entry->available &= ~used;
  72. if (!entry->available)
  73. entry->remove();
  74. } else {
  75. if (debug)
  76. printf("kill %s\n", entry->lhs->name);
  77. entry->remove();
  78. }
  79. }
  80. }
  81. }
  82. virtual ir_visitor_status visit(ir_dereference_variable *ir)
  83. {
  84. kill_channels(ir->var, ~0);
  85. return visit_continue;
  86. }
  87. virtual ir_visitor_status visit(ir_swizzle *ir)
  88. {
  89. ir_dereference_variable *deref = ir->val->as_dereference_variable();
  90. if (!deref)
  91. return visit_continue;
  92. int used = 0;
  93. used |= 1 << ir->mask.x;
  94. used |= 1 << ir->mask.y;
  95. used |= 1 << ir->mask.z;
  96. used |= 1 << ir->mask.w;
  97. kill_channels(deref->var, used);
  98. return visit_continue_with_parent;
  99. }
  100. virtual ir_visitor_status visit(ir_emit_vertex *ir)
  101. {
  102. /* For the purpose of dead code elimination, emitting a vertex counts as
  103. * "reading" all of the currently assigned output variables.
  104. */
  105. foreach_iter(exec_list_iterator, iter, *this->assignments) {
  106. assignment_entry *entry = (assignment_entry *)iter.get();
  107. if (entry->lhs->data.mode == ir_var_shader_out) {
  108. if (debug)
  109. printf("kill %s\n", entry->lhs->name);
  110. entry->remove();
  111. }
  112. }
  113. return visit_continue;
  114. }
  115. private:
  116. exec_list *assignments;
  117. };
  118. class array_index_visit : public ir_hierarchical_visitor {
  119. public:
  120. array_index_visit(ir_hierarchical_visitor *v)
  121. {
  122. this->visitor = v;
  123. }
  124. virtual ir_visitor_status visit_enter(class ir_dereference_array *ir)
  125. {
  126. ir->array_index->accept(visitor);
  127. return visit_continue;
  128. }
  129. static void run(ir_instruction *ir, ir_hierarchical_visitor *v)
  130. {
  131. array_index_visit top_visit(v);
  132. ir->accept(& top_visit);
  133. }
  134. ir_hierarchical_visitor *visitor;
  135. };
  136. } /* unnamed namespace */
  137. /**
  138. * Adds an entry to the available copy list if it's a plain assignment
  139. * of a variable to a variable.
  140. */
  141. static bool
  142. process_assignment(void *ctx, ir_assignment *ir, exec_list *assignments)
  143. {
  144. ir_variable *var = NULL;
  145. bool progress = false;
  146. kill_for_derefs_visitor v(assignments);
  147. /* Kill assignment entries for things used to produce this assignment. */
  148. ir->rhs->accept(&v);
  149. if (ir->condition) {
  150. ir->condition->accept(&v);
  151. }
  152. /* Kill assignment enties used as array indices.
  153. */
  154. array_index_visit::run(ir->lhs, &v);
  155. var = ir->lhs->variable_referenced();
  156. assert(var);
  157. /* Now, check if we did a whole-variable assignment. */
  158. if (!ir->condition) {
  159. ir_dereference_variable *deref_var = ir->lhs->as_dereference_variable();
  160. /* If it's a vector type, we can do per-channel elimination of
  161. * use of the RHS.
  162. */
  163. if (deref_var && (deref_var->var->type->is_scalar() ||
  164. deref_var->var->type->is_vector())) {
  165. if (debug)
  166. printf("looking for %s.0x%01x to remove\n", var->name,
  167. ir->write_mask);
  168. foreach_iter(exec_list_iterator, iter, *assignments) {
  169. assignment_entry *entry = (assignment_entry *)iter.get();
  170. if (entry->lhs != var)
  171. continue;
  172. int remove = entry->available & ir->write_mask;
  173. if (debug) {
  174. printf("%s 0x%01x - 0x%01x = 0x%01x\n",
  175. var->name,
  176. entry->ir->write_mask,
  177. remove, entry->ir->write_mask & ~remove);
  178. }
  179. if (remove) {
  180. progress = true;
  181. if (debug) {
  182. printf("rewriting:\n ");
  183. entry->ir->print();
  184. printf("\n");
  185. }
  186. entry->ir->write_mask &= ~remove;
  187. entry->available &= ~remove;
  188. if (entry->ir->write_mask == 0) {
  189. /* Delete the dead assignment. */
  190. entry->ir->remove();
  191. entry->remove();
  192. } else {
  193. void *mem_ctx = ralloc_parent(entry->ir);
  194. /* Reswizzle the RHS arguments according to the new
  195. * write_mask.
  196. */
  197. unsigned components[4];
  198. unsigned channels = 0;
  199. unsigned next = 0;
  200. for (int i = 0; i < 4; i++) {
  201. if ((entry->ir->write_mask | remove) & (1 << i)) {
  202. if (!(remove & (1 << i)))
  203. components[channels++] = next;
  204. next++;
  205. }
  206. }
  207. entry->ir->rhs = new(mem_ctx) ir_swizzle(entry->ir->rhs,
  208. components,
  209. channels);
  210. if (debug) {
  211. printf("to:\n ");
  212. entry->ir->print();
  213. printf("\n");
  214. }
  215. }
  216. }
  217. }
  218. } else if (ir->whole_variable_written() != NULL) {
  219. /* We did a whole-variable assignment. So, any instruction in
  220. * the assignment list with the same LHS is dead.
  221. */
  222. if (debug)
  223. printf("looking for %s to remove\n", var->name);
  224. foreach_iter(exec_list_iterator, iter, *assignments) {
  225. assignment_entry *entry = (assignment_entry *)iter.get();
  226. if (entry->lhs == var) {
  227. if (debug)
  228. printf("removing %s\n", var->name);
  229. entry->ir->remove();
  230. entry->remove();
  231. progress = true;
  232. }
  233. }
  234. }
  235. }
  236. /* Add this instruction to the assignment list available to be removed. */
  237. assignment_entry *entry = new(ctx) assignment_entry(var, ir);
  238. assignments->push_tail(entry);
  239. if (debug) {
  240. printf("add %s\n", var->name);
  241. printf("current entries\n");
  242. foreach_iter(exec_list_iterator, iter, *assignments) {
  243. assignment_entry *entry = (assignment_entry *)iter.get();
  244. printf(" %s (0x%01x)\n", entry->lhs->name, entry->available);
  245. }
  246. }
  247. return progress;
  248. }
  249. static void
  250. dead_code_local_basic_block(ir_instruction *first,
  251. ir_instruction *last,
  252. void *data)
  253. {
  254. ir_instruction *ir, *ir_next;
  255. /* List of avaialble_copy */
  256. exec_list assignments;
  257. bool *out_progress = (bool *)data;
  258. bool progress = false;
  259. void *ctx = ralloc_context(NULL);
  260. /* Safe looping, since process_assignment */
  261. for (ir = first, ir_next = (ir_instruction *)first->next;;
  262. ir = ir_next, ir_next = (ir_instruction *)ir->next) {
  263. ir_assignment *ir_assign = ir->as_assignment();
  264. if (debug) {
  265. ir->print();
  266. printf("\n");
  267. }
  268. if (ir_assign) {
  269. progress = process_assignment(ctx, ir_assign, &assignments) || progress;
  270. } else {
  271. kill_for_derefs_visitor kill(&assignments);
  272. ir->accept(&kill);
  273. }
  274. if (ir == last)
  275. break;
  276. }
  277. *out_progress = progress;
  278. ralloc_free(ctx);
  279. }
  280. /**
  281. * Does a copy propagation pass on the code present in the instruction stream.
  282. */
  283. bool
  284. do_dead_code_local(exec_list *instructions)
  285. {
  286. bool progress = false;
  287. call_for_basic_blocks(instructions, dead_code_local_basic_block, &progress);
  288. return progress;
  289. }