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_if_simplification.cpp 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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_function_inlining.cpp
  25. *
  26. * Moves constant branches of if statements out to the surrounding
  27. * instruction stream.
  28. */
  29. #include "ir.h"
  30. class ir_if_simplification_visitor : public ir_hierarchical_visitor {
  31. public:
  32. ir_if_simplification_visitor()
  33. {
  34. this->made_progress = false;
  35. }
  36. ir_visitor_status visit_leave(ir_if *);
  37. bool made_progress;
  38. };
  39. bool
  40. do_if_simplification(exec_list *instructions)
  41. {
  42. ir_if_simplification_visitor v;
  43. v.run(instructions);
  44. return v.made_progress;
  45. }
  46. ir_visitor_status
  47. ir_if_simplification_visitor::visit_leave(ir_if *ir)
  48. {
  49. /* FINISHME: Ideally there would be a way to note that the condition results
  50. * FINISHME: in a constant before processing both of the other subtrees.
  51. * FINISHME: This can probably be done with some flags, but it would take
  52. * FINISHME: some work to get right.
  53. */
  54. ir_constant *condition_constant = ir->condition->constant_expression_value();
  55. if (condition_constant) {
  56. /* Move the contents of the one branch of the conditional
  57. * that matters out.
  58. */
  59. if (condition_constant->value.b[0]) {
  60. foreach_iter(exec_list_iterator, then_iter, ir->then_instructions) {
  61. ir_instruction *then_ir = (ir_instruction *)then_iter.get();
  62. ir->insert_before(then_ir);
  63. }
  64. } else {
  65. foreach_iter(exec_list_iterator, else_iter, ir->else_instructions) {
  66. ir_instruction *else_ir = (ir_instruction *)else_iter.get();
  67. ir->insert_before(else_ir);
  68. }
  69. }
  70. ir->remove();
  71. this->made_progress = true;
  72. }
  73. return visit_continue;
  74. }