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_basic_block.cpp 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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_basic_block.cpp
  25. *
  26. * Basic block analysis of instruction streams.
  27. */
  28. #include "ir.h"
  29. #include "ir_visitor.h"
  30. #include "ir_basic_block.h"
  31. #include "glsl_types.h"
  32. class ir_has_call_visitor : public ir_hierarchical_visitor {
  33. public:
  34. ir_has_call_visitor()
  35. {
  36. has_call = false;
  37. }
  38. virtual ir_visitor_status visit_enter(ir_call *ir)
  39. {
  40. (void) ir;
  41. has_call = true;
  42. return visit_stop;
  43. }
  44. bool has_call;
  45. };
  46. /**
  47. * Calls a user function for every basic block in the instruction stream.
  48. *
  49. * Basic block analysis is pretty easy in our IR thanks to the lack of
  50. * unstructured control flow. We've got:
  51. *
  52. * ir_loop (for () {}, while () {}, do {} while ())
  53. * ir_loop_jump (
  54. * ir_if () {}
  55. * ir_return
  56. * ir_call()
  57. *
  58. * Note that the basic blocks returned by this don't encompass all
  59. * operations performed by the program -- for example, if conditions
  60. * don't get returned, nor do the assignments that will be generated
  61. * for ir_call parameters.
  62. */
  63. void call_for_basic_blocks(exec_list *instructions,
  64. void (*callback)(ir_instruction *first,
  65. ir_instruction *last,
  66. void *data),
  67. void *data)
  68. {
  69. ir_instruction *leader = NULL;
  70. ir_instruction *last = NULL;
  71. foreach_iter(exec_list_iterator, iter, *instructions) {
  72. ir_instruction *ir = (ir_instruction *)iter.get();
  73. ir_if *ir_if;
  74. ir_loop *ir_loop;
  75. ir_function *ir_function;
  76. if (!leader)
  77. leader = ir;
  78. if ((ir_if = ir->as_if())) {
  79. callback(leader, ir, data);
  80. leader = NULL;
  81. call_for_basic_blocks(&ir_if->then_instructions, callback, data);
  82. call_for_basic_blocks(&ir_if->else_instructions, callback, data);
  83. } else if ((ir_loop = ir->as_loop())) {
  84. callback(leader, ir, data);
  85. leader = NULL;
  86. call_for_basic_blocks(&ir_loop->body_instructions, callback, data);
  87. } else if (ir->as_return() || ir->as_call()) {
  88. callback(leader, ir, data);
  89. leader = NULL;
  90. } else if ((ir_function = ir->as_function())) {
  91. /* A function definition doesn't interrupt our basic block
  92. * since execution doesn't go into it. We should process the
  93. * bodies of its signatures for BBs, though.
  94. *
  95. * Note that we miss an opportunity for producing more
  96. * maximal BBs between the instructions that precede main()
  97. * and the body of main(). Perhaps those instructions ought
  98. * to live inside of main().
  99. */
  100. foreach_iter(exec_list_iterator, fun_iter, *ir_function) {
  101. ir_function_signature *ir_sig;
  102. ir_sig = (ir_function_signature *)fun_iter.get();
  103. call_for_basic_blocks(&ir_sig->body, callback, data);
  104. }
  105. } else if (ir->as_assignment()) {
  106. ir_has_call_visitor v;
  107. /* If there's a call in the expression tree being assigned,
  108. * then that ends the BB too.
  109. *
  110. * The assumption is that any consumer of the basic block
  111. * walker is fine with the fact that the call is somewhere in
  112. * the tree even if portions of the tree may be evaluated
  113. * after the call.
  114. *
  115. * A consumer that has an issue with this could not process
  116. * the last instruction of the basic block. If doing so,
  117. * expression flattener may be useful before using the basic
  118. * block finder to get more maximal basic blocks out.
  119. */
  120. ir->accept(&v);
  121. if (v.has_call) {
  122. callback(leader, ir, data);
  123. leader = NULL;
  124. }
  125. }
  126. last = ir;
  127. }
  128. if (leader) {
  129. callback(leader, last, data);
  130. }
  131. }