function.cpp 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. BasicBlock* Function::InsertBasicBlockAfter(
  133. std::unique_ptr<BasicBlock>&& new_block, BasicBlock* position) {
  134. for (auto bb_iter = begin(); bb_iter != end(); ++bb_iter) {
  135. if (&*bb_iter == position) {
  136. new_block->SetParent(this);
  137. ++bb_iter;
  138. bb_iter = bb_iter.InsertBefore(std::move(new_block));
  139. return &*bb_iter;
  140. }
  141. }
  142. assert(false && "Could not find insertion point.");
  143. return nullptr;
  144. }
  145. BasicBlock* Function::InsertBasicBlockBefore(
  146. std::unique_ptr<BasicBlock>&& new_block, BasicBlock* position) {
  147. for (auto bb_iter = begin(); bb_iter != end(); ++bb_iter) {
  148. if (&*bb_iter == position) {
  149. new_block->SetParent(this);
  150. bb_iter = bb_iter.InsertBefore(std::move(new_block));
  151. return &*bb_iter;
  152. }
  153. }
  154. assert(false && "Could not find insertion point.");
  155. return nullptr;
  156. }
  157. bool Function::IsRecursive() const {
  158. IRContext* ctx = blocks_.front()->GetLabel()->context();
  159. IRContext::ProcessFunction mark_visited = [this](Function* fp) {
  160. return fp == this;
  161. };
  162. // Process the call tree from all of the function called by |this|. If it get
  163. // back to |this|, then we have a recursive function.
  164. std::queue<uint32_t> roots;
  165. ctx->AddCalls(this, &roots);
  166. return ctx->ProcessCallTreeFromRoots(mark_visited, &roots);
  167. }
  168. std::ostream& operator<<(std::ostream& str, const Function& func) {
  169. str << func.PrettyPrint();
  170. return str;
  171. }
  172. void Function::Dump() const {
  173. std::cerr << "Function #" << result_id() << "\n" << *this << "\n";
  174. }
  175. std::string Function::PrettyPrint(uint32_t options) const {
  176. std::ostringstream str;
  177. ForEachInst([&str, options](const Instruction* inst) {
  178. str << inst->PrettyPrint(options);
  179. if (inst->opcode() != SpvOpFunctionEnd) {
  180. str << std::endl;
  181. }
  182. });
  183. return str.str();
  184. }
  185. } // namespace opt
  186. } // namespace spvtools