function.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. // Copyright (c) 2016 Google Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "source/opt/function.h"
  15. #include <ostream>
  16. #include <sstream>
  17. #include "function.h"
  18. #include "ir_context.h"
  19. #include "source/util/bit_vector.h"
  20. namespace spvtools {
  21. namespace opt {
  22. Function* Function::Clone(IRContext* ctx) const {
  23. Function* clone =
  24. new Function(std::unique_ptr<Instruction>(DefInst().Clone(ctx)));
  25. clone->params_.reserve(params_.size());
  26. ForEachParam(
  27. [clone, ctx](const Instruction* inst) {
  28. clone->AddParameter(std::unique_ptr<Instruction>(inst->Clone(ctx)));
  29. },
  30. true);
  31. for (const auto& i : debug_insts_in_header_) {
  32. clone->AddDebugInstructionInHeader(
  33. std::unique_ptr<Instruction>(i.Clone(ctx)));
  34. }
  35. clone->blocks_.reserve(blocks_.size());
  36. for (const auto& b : blocks_) {
  37. std::unique_ptr<BasicBlock> bb(b->Clone(ctx));
  38. bb->SetParent(clone);
  39. clone->AddBasicBlock(std::move(bb));
  40. }
  41. clone->SetFunctionEnd(std::unique_ptr<Instruction>(EndInst()->Clone(ctx)));
  42. return clone;
  43. }
  44. void Function::ForEachInst(const std::function<void(Instruction*)>& f,
  45. bool run_on_debug_line_insts) {
  46. WhileEachInst(
  47. [&f](Instruction* inst) {
  48. f(inst);
  49. return true;
  50. },
  51. run_on_debug_line_insts);
  52. }
  53. void Function::ForEachInst(const std::function<void(const Instruction*)>& f,
  54. bool run_on_debug_line_insts) const {
  55. WhileEachInst(
  56. [&f](const Instruction* inst) {
  57. f(inst);
  58. return true;
  59. },
  60. run_on_debug_line_insts);
  61. }
  62. bool Function::WhileEachInst(const std::function<bool(Instruction*)>& f,
  63. bool run_on_debug_line_insts) {
  64. if (def_inst_) {
  65. if (!def_inst_->WhileEachInst(f, run_on_debug_line_insts)) {
  66. return false;
  67. }
  68. }
  69. for (auto& param : params_) {
  70. if (!param->WhileEachInst(f, run_on_debug_line_insts)) {
  71. return false;
  72. }
  73. }
  74. if (!debug_insts_in_header_.empty()) {
  75. Instruction* di = &debug_insts_in_header_.front();
  76. while (di != nullptr) {
  77. Instruction* next_instruction = di->NextNode();
  78. if (!di->WhileEachInst(f, run_on_debug_line_insts)) return false;
  79. di = next_instruction;
  80. }
  81. }
  82. for (auto& bb : blocks_) {
  83. if (!bb->WhileEachInst(f, run_on_debug_line_insts)) {
  84. return false;
  85. }
  86. }
  87. if (end_inst_) return end_inst_->WhileEachInst(f, run_on_debug_line_insts);
  88. return true;
  89. }
  90. bool Function::WhileEachInst(const std::function<bool(const Instruction*)>& f,
  91. bool run_on_debug_line_insts) const {
  92. if (def_inst_) {
  93. if (!static_cast<const Instruction*>(def_inst_.get())
  94. ->WhileEachInst(f, run_on_debug_line_insts)) {
  95. return false;
  96. }
  97. }
  98. for (const auto& param : params_) {
  99. if (!static_cast<const Instruction*>(param.get())
  100. ->WhileEachInst(f, run_on_debug_line_insts)) {
  101. return false;
  102. }
  103. }
  104. for (const auto& di : debug_insts_in_header_) {
  105. if (!static_cast<const Instruction*>(&di)->WhileEachInst(
  106. f, run_on_debug_line_insts))
  107. return false;
  108. }
  109. for (const auto& bb : blocks_) {
  110. if (!static_cast<const BasicBlock*>(bb.get())->WhileEachInst(
  111. f, run_on_debug_line_insts)) {
  112. return false;
  113. }
  114. }
  115. if (end_inst_)
  116. return static_cast<const Instruction*>(end_inst_.get())
  117. ->WhileEachInst(f, run_on_debug_line_insts);
  118. return true;
  119. }
  120. void Function::ForEachParam(const std::function<void(Instruction*)>& f,
  121. bool run_on_debug_line_insts) {
  122. for (auto& param : params_)
  123. static_cast<Instruction*>(param.get())
  124. ->ForEachInst(f, run_on_debug_line_insts);
  125. }
  126. void Function::ForEachParam(const std::function<void(const Instruction*)>& f,
  127. bool run_on_debug_line_insts) const {
  128. for (const auto& param : params_)
  129. static_cast<const Instruction*>(param.get())
  130. ->ForEachInst(f, run_on_debug_line_insts);
  131. }
  132. void Function::ForEachDebugInstructionsInHeader(
  133. const std::function<void(Instruction*)>& f) {
  134. if (debug_insts_in_header_.empty()) return;
  135. Instruction* di = &debug_insts_in_header_.front();
  136. while (di != nullptr) {
  137. Instruction* next_instruction = di->NextNode();
  138. di->ForEachInst(f);
  139. di = next_instruction;
  140. }
  141. }
  142. BasicBlock* Function::InsertBasicBlockAfter(
  143. std::unique_ptr<BasicBlock>&& new_block, BasicBlock* position) {
  144. for (auto bb_iter = begin(); bb_iter != end(); ++bb_iter) {
  145. if (&*bb_iter == position) {
  146. new_block->SetParent(this);
  147. ++bb_iter;
  148. bb_iter = bb_iter.InsertBefore(std::move(new_block));
  149. return &*bb_iter;
  150. }
  151. }
  152. assert(false && "Could not find insertion point.");
  153. return nullptr;
  154. }
  155. BasicBlock* Function::InsertBasicBlockBefore(
  156. std::unique_ptr<BasicBlock>&& new_block, BasicBlock* position) {
  157. for (auto bb_iter = begin(); bb_iter != end(); ++bb_iter) {
  158. if (&*bb_iter == position) {
  159. new_block->SetParent(this);
  160. bb_iter = bb_iter.InsertBefore(std::move(new_block));
  161. return &*bb_iter;
  162. }
  163. }
  164. assert(false && "Could not find insertion point.");
  165. return nullptr;
  166. }
  167. bool Function::IsRecursive() const {
  168. IRContext* ctx = blocks_.front()->GetLabel()->context();
  169. IRContext::ProcessFunction mark_visited = [this](Function* fp) {
  170. return fp == this;
  171. };
  172. // Process the call tree from all of the function called by |this|. If it get
  173. // back to |this|, then we have a recursive function.
  174. std::queue<uint32_t> roots;
  175. ctx->AddCalls(this, &roots);
  176. return ctx->ProcessCallTreeFromRoots(mark_visited, &roots);
  177. }
  178. std::ostream& operator<<(std::ostream& str, const Function& func) {
  179. str << func.PrettyPrint();
  180. return str;
  181. }
  182. void Function::Dump() const {
  183. std::cerr << "Function #" << result_id() << "\n" << *this << "\n";
  184. }
  185. std::string Function::PrettyPrint(uint32_t options) const {
  186. std::ostringstream str;
  187. ForEachInst([&str, options](const Instruction* inst) {
  188. str << inst->PrettyPrint(options);
  189. if (inst->opcode() != SpvOpFunctionEnd) {
  190. str << std::endl;
  191. }
  192. });
  193. return str.str();
  194. }
  195. } // namespace opt
  196. } // namespace spvtools