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_folding.cpp 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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_folding.cpp
  25. * Replace constant-valued expressions with references to constant values.
  26. */
  27. #define NULL 0
  28. #include "ir.h"
  29. #include "ir_visitor.h"
  30. #include "ir_constant_folding.h"
  31. #include "glsl_types.h"
  32. /**
  33. * Visitor class for replacing expressions with ir_constant values.
  34. */
  35. void
  36. ir_constant_folding_visitor::visit(ir_variable *ir)
  37. {
  38. (void) ir;
  39. }
  40. void
  41. ir_constant_folding_visitor::visit(ir_label *ir)
  42. {
  43. ir->signature->accept(this);
  44. }
  45. void
  46. ir_constant_folding_visitor::visit(ir_function_signature *ir)
  47. {
  48. visit_exec_list(&ir->body, this);
  49. }
  50. void
  51. ir_constant_folding_visitor::visit(ir_function *ir)
  52. {
  53. (void) ir;
  54. }
  55. void
  56. ir_constant_folding_visitor::visit(ir_expression *ir)
  57. {
  58. ir_constant *op[2];
  59. unsigned int operand;
  60. for (operand = 0; operand < ir->get_num_operands(); operand++) {
  61. op[operand] = ir->operands[operand]->constant_expression_value();
  62. if (op[operand]) {
  63. ir->operands[operand] = op[operand];
  64. } else {
  65. ir->operands[operand]->accept(this);
  66. }
  67. }
  68. }
  69. void
  70. ir_constant_folding_visitor::visit(ir_swizzle *ir)
  71. {
  72. ir->val->accept(this);
  73. }
  74. void
  75. ir_constant_folding_visitor::visit(ir_dereference *ir)
  76. {
  77. if (ir->mode == ir_dereference::ir_reference_array) {
  78. ir_constant *const_val = ir->selector.array_index->constant_expression_value();
  79. if (const_val)
  80. ir->selector.array_index = const_val;
  81. else
  82. ir->selector.array_index->accept(this);
  83. }
  84. ir->var->accept(this);
  85. }
  86. void
  87. ir_constant_folding_visitor::visit(ir_assignment *ir)
  88. {
  89. ir_constant *const_val = ir->rhs->constant_expression_value();
  90. if (const_val)
  91. ir->rhs = const_val;
  92. else
  93. ir->rhs->accept(this);
  94. }
  95. void
  96. ir_constant_folding_visitor::visit(ir_constant *ir)
  97. {
  98. (void) ir;
  99. }
  100. void
  101. ir_constant_folding_visitor::visit(ir_call *ir)
  102. {
  103. (void) ir;
  104. }
  105. void
  106. ir_constant_folding_visitor::visit(ir_return *ir)
  107. {
  108. (void) ir;
  109. }
  110. void
  111. ir_constant_folding_visitor::visit(ir_if *ir)
  112. {
  113. ir_constant *const_val = ir->condition->constant_expression_value();
  114. if (const_val)
  115. ir->condition = const_val;
  116. else
  117. ir->condition->accept(this);
  118. visit_exec_list(&ir->then_instructions, this);
  119. visit_exec_list(&ir->else_instructions, this);
  120. }
  121. void
  122. ir_constant_folding_visitor::visit(ir_loop *ir)
  123. {
  124. (void) ir;
  125. }
  126. void
  127. ir_constant_folding_visitor::visit(ir_loop_jump *ir)
  128. {
  129. (void) ir;
  130. }