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

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