Clone of mesa.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

ir_expression_flattening.cpp 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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_expression_flattening.cpp
  25. *
  26. * Takes the leaves of expression trees and makes them dereferences of
  27. * assignments of the leaves to temporaries, according to a predicate.
  28. *
  29. * This is used for automatic function inlining, where we want to take
  30. * an expression containing a call and move the call out to its own
  31. * assignment so that we can inline it at the appropriate place in the
  32. * instruction stream.
  33. */
  34. #include "ir.h"
  35. #include "ir_visitor.h"
  36. #include "ir_expression_flattening.h"
  37. #include "glsl_types.h"
  38. class ir_expression_flattening_visitor : public ir_hierarchical_visitor {
  39. public:
  40. ir_expression_flattening_visitor(ir_instruction *base_ir,
  41. bool (*predicate)(ir_instruction *ir))
  42. {
  43. this->base_ir = base_ir;
  44. this->predicate = predicate;
  45. }
  46. virtual ~ir_expression_flattening_visitor()
  47. {
  48. /* empty */
  49. }
  50. virtual ir_visitor_status visit_enter(ir_call *);
  51. virtual ir_visitor_status visit_enter(ir_return *);
  52. virtual ir_visitor_status visit_enter(ir_function_signature *);
  53. virtual ir_visitor_status visit_enter(ir_if *);
  54. virtual ir_visitor_status visit_enter(ir_loop *);
  55. virtual ir_visitor_status visit_leave(ir_expression *);
  56. virtual ir_visitor_status visit_leave(ir_swizzle *);
  57. bool (*predicate)(ir_instruction *ir);
  58. ir_instruction *base_ir;
  59. };
  60. void
  61. do_expression_flattening(exec_list *instructions,
  62. bool (*predicate)(ir_instruction *ir))
  63. {
  64. foreach_iter(exec_list_iterator, iter, *instructions) {
  65. ir_instruction *ir = (ir_instruction *)iter.get();
  66. ir_expression_flattening_visitor v(ir, predicate);
  67. ir->accept(&v);
  68. }
  69. }
  70. static ir_rvalue *
  71. operand_to_temp(ir_instruction *base_ir, ir_rvalue *ir)
  72. {
  73. ir_variable *var;
  74. ir_assignment *assign;
  75. var = new ir_variable(ir->type, "flattening_tmp");
  76. base_ir->insert_before(var);
  77. assign = new ir_assignment(new ir_dereference_variable(var),
  78. ir,
  79. NULL);
  80. base_ir->insert_before(assign);
  81. return new ir_dereference_variable(var);
  82. }
  83. ir_visitor_status
  84. ir_expression_flattening_visitor::visit_enter(ir_function_signature *ir)
  85. {
  86. do_expression_flattening(&ir->body, this->predicate);
  87. return visit_continue_with_parent;
  88. }
  89. ir_visitor_status
  90. ir_expression_flattening_visitor::visit_enter(ir_loop *ir)
  91. {
  92. do_expression_flattening(&ir->body_instructions, this->predicate);
  93. return visit_continue_with_parent;
  94. }
  95. ir_visitor_status
  96. ir_expression_flattening_visitor::visit_enter(ir_if *ir)
  97. {
  98. ir->condition->accept(this);
  99. do_expression_flattening(&ir->then_instructions, this->predicate);
  100. do_expression_flattening(&ir->else_instructions, this->predicate);
  101. return visit_continue_with_parent;
  102. }
  103. ir_visitor_status
  104. ir_expression_flattening_visitor::visit_leave(ir_expression *ir)
  105. {
  106. unsigned int operand;
  107. for (operand = 0; operand < ir->get_num_operands(); operand++) {
  108. /* If the operand matches the predicate, then we'll assign its
  109. * value to a temporary and deref the temporary as the operand.
  110. */
  111. if (this->predicate(ir->operands[operand])) {
  112. ir->operands[operand] = operand_to_temp(base_ir,
  113. ir->operands[operand]);
  114. }
  115. }
  116. return visit_continue;
  117. }
  118. ir_visitor_status
  119. ir_expression_flattening_visitor::visit_leave(ir_swizzle *ir)
  120. {
  121. if (this->predicate(ir->val)) {
  122. ir->val = operand_to_temp(this->base_ir, ir->val);
  123. }
  124. return visit_continue;
  125. }
  126. ir_visitor_status
  127. ir_expression_flattening_visitor::visit_enter(ir_call *ir)
  128. {
  129. /* FINISHME: Why not process the call parameters? (Same behavior as original
  130. * FINISHME: code.)
  131. */
  132. (void) ir;
  133. return visit_continue_with_parent;
  134. }
  135. ir_visitor_status
  136. ir_expression_flattening_visitor::visit_enter(ir_return *ir)
  137. {
  138. /* FINISHME: Why not process the return value? (Same behavior as original
  139. * FINISHME: code.)
  140. */
  141. (void) ir;
  142. return visit_continue_with_parent;
  143. }