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_constant_variable.cpp 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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_constant_variable.cpp
  25. *
  26. * Marks variables assigned a single constant value over the course
  27. * of the program as constant.
  28. *
  29. * The goal here is to trigger further constant folding and then dead
  30. * code elimination. This is common with vector/matrix constructors
  31. * and calls to builtin functions.
  32. */
  33. #include "ir.h"
  34. #include "ir_visitor.h"
  35. #include "ir_optimization.h"
  36. #include "glsl_types.h"
  37. struct assignment_entry {
  38. exec_node link;
  39. int assignment_count;
  40. ir_variable *var;
  41. ir_constant *constval;
  42. };
  43. class ir_constant_variable_visitor : public ir_hierarchical_visitor {
  44. public:
  45. virtual ir_visitor_status visit_enter(ir_assignment *);
  46. exec_list list;
  47. };
  48. static struct assignment_entry *
  49. get_assignment_entry(ir_variable *var, exec_list *list)
  50. {
  51. struct assignment_entry *entry;
  52. foreach_list_typed(struct assignment_entry, entry, link, list) {
  53. if (entry->var == var)
  54. return entry;
  55. }
  56. entry = (struct assignment_entry *)calloc(1, sizeof(*entry));
  57. entry->var = var;
  58. list->push_head(&entry->link);
  59. return entry;
  60. }
  61. ir_visitor_status
  62. ir_constant_variable_visitor::visit_enter(ir_assignment *ir)
  63. {
  64. ir_constant *constval;
  65. struct assignment_entry *entry;
  66. entry = get_assignment_entry(ir->lhs->variable_referenced(), &this->list);
  67. assert(entry);
  68. entry->assignment_count++;
  69. /* If it's already constant, don't do the work. */
  70. if (entry->var->constant_value)
  71. return visit_continue;
  72. /* OK, now find if we actually have all the right conditions for
  73. * this to be a constant value assigned to the var.
  74. */
  75. if (ir->condition) {
  76. constval = ir->condition->constant_expression_value();
  77. if (!constval || !constval->value.b[0])
  78. return visit_continue;
  79. }
  80. ir_variable *var = ir->lhs->whole_variable_referenced();
  81. if (!var)
  82. return visit_continue;
  83. constval = ir->rhs->constant_expression_value();
  84. if (!constval)
  85. return visit_continue;
  86. /* Mark this entry as having a constant assignment (if the
  87. * assignment count doesn't go >1). do_constant_variable will fix
  88. * up the variable with the constant value later.
  89. */
  90. entry->constval = constval;
  91. return visit_continue;
  92. }
  93. /**
  94. * Does a copy propagation pass on the code present in the instruction stream.
  95. */
  96. bool
  97. do_constant_variable(exec_list *instructions)
  98. {
  99. bool progress = false;
  100. ir_constant_variable_visitor v;
  101. v.run(instructions);
  102. while (!v.list.is_empty()) {
  103. struct assignment_entry *entry;
  104. entry = exec_node_data(struct assignment_entry, v.list.head, link);
  105. if (entry->assignment_count == 1 && entry->constval) {
  106. entry->var->constant_value = entry->constval;
  107. progress = true;
  108. }
  109. entry->link.remove();
  110. free(entry);
  111. }
  112. return progress;
  113. }
  114. bool
  115. do_constant_variable_unlinked(exec_list *instructions)
  116. {
  117. bool progress = false;
  118. foreach_iter(exec_list_iterator, iter, *instructions) {
  119. ir_instruction *ir = (ir_instruction *)iter.get();
  120. ir_function *f = ir->as_function();
  121. if (f) {
  122. foreach_iter(exec_list_iterator, sigiter, *f) {
  123. ir_function_signature *sig =
  124. (ir_function_signature *) sigiter.get();
  125. if (do_constant_variable(&sig->body))
  126. progress = true;
  127. }
  128. }
  129. }
  130. return progress;
  131. }