function.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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. clone->non_semantic_.reserve(non_semantic_.size());
  43. for (auto& non_semantic : non_semantic_) {
  44. clone->AddNonSemanticInstruction(
  45. std::unique_ptr<Instruction>(non_semantic->Clone(ctx)));
  46. }
  47. return clone;
  48. }
  49. void Function::ForEachInst(const std::function<void(Instruction*)>& f,
  50. bool run_on_debug_line_insts,
  51. bool run_on_non_semantic_insts) {
  52. WhileEachInst(
  53. [&f](Instruction* inst) {
  54. f(inst);
  55. return true;
  56. },
  57. run_on_debug_line_insts, run_on_non_semantic_insts);
  58. }
  59. void Function::ForEachInst(const std::function<void(const Instruction*)>& f,
  60. bool run_on_debug_line_insts,
  61. bool run_on_non_semantic_insts) const {
  62. WhileEachInst(
  63. [&f](const Instruction* inst) {
  64. f(inst);
  65. return true;
  66. },
  67. run_on_debug_line_insts, run_on_non_semantic_insts);
  68. }
  69. bool Function::WhileEachInst(const std::function<bool(Instruction*)>& f,
  70. bool run_on_debug_line_insts,
  71. bool run_on_non_semantic_insts) {
  72. if (def_inst_) {
  73. if (!def_inst_->WhileEachInst(f, run_on_debug_line_insts)) {
  74. return false;
  75. }
  76. }
  77. for (auto& param : params_) {
  78. if (!param->WhileEachInst(f, run_on_debug_line_insts)) {
  79. return false;
  80. }
  81. }
  82. if (!debug_insts_in_header_.empty()) {
  83. Instruction* di = &debug_insts_in_header_.front();
  84. while (di != nullptr) {
  85. Instruction* next_instruction = di->NextNode();
  86. if (!di->WhileEachInst(f, run_on_debug_line_insts)) return false;
  87. di = next_instruction;
  88. }
  89. }
  90. for (auto& bb : blocks_) {
  91. if (!bb->WhileEachInst(f, run_on_debug_line_insts)) {
  92. return false;
  93. }
  94. }
  95. if (end_inst_) {
  96. if (!end_inst_->WhileEachInst(f, run_on_debug_line_insts)) {
  97. return false;
  98. }
  99. }
  100. if (run_on_non_semantic_insts) {
  101. for (auto& non_semantic : non_semantic_) {
  102. if (!non_semantic->WhileEachInst(f, run_on_debug_line_insts)) {
  103. return false;
  104. }
  105. }
  106. }
  107. return true;
  108. }
  109. bool Function::WhileEachInst(const std::function<bool(const Instruction*)>& f,
  110. bool run_on_debug_line_insts,
  111. bool run_on_non_semantic_insts) const {
  112. if (def_inst_) {
  113. if (!static_cast<const Instruction*>(def_inst_.get())
  114. ->WhileEachInst(f, run_on_debug_line_insts)) {
  115. return false;
  116. }
  117. }
  118. for (const auto& param : params_) {
  119. if (!static_cast<const Instruction*>(param.get())
  120. ->WhileEachInst(f, run_on_debug_line_insts)) {
  121. return false;
  122. }
  123. }
  124. for (const auto& di : debug_insts_in_header_) {
  125. if (!static_cast<const Instruction*>(&di)->WhileEachInst(
  126. f, run_on_debug_line_insts))
  127. return false;
  128. }
  129. for (const auto& bb : blocks_) {
  130. if (!static_cast<const BasicBlock*>(bb.get())->WhileEachInst(
  131. f, run_on_debug_line_insts)) {
  132. return false;
  133. }
  134. }
  135. if (end_inst_) {
  136. if (!static_cast<const Instruction*>(end_inst_.get())
  137. ->WhileEachInst(f, run_on_debug_line_insts)) {
  138. return false;
  139. }
  140. }
  141. if (run_on_non_semantic_insts) {
  142. for (auto& non_semantic : non_semantic_) {
  143. if (!static_cast<const Instruction*>(non_semantic.get())
  144. ->WhileEachInst(f, run_on_debug_line_insts)) {
  145. return false;
  146. }
  147. }
  148. }
  149. return true;
  150. }
  151. void Function::ForEachParam(const std::function<void(Instruction*)>& f,
  152. bool run_on_debug_line_insts) {
  153. for (auto& param : params_)
  154. static_cast<Instruction*>(param.get())
  155. ->ForEachInst(f, run_on_debug_line_insts);
  156. }
  157. void Function::ForEachParam(const std::function<void(const Instruction*)>& f,
  158. bool run_on_debug_line_insts) const {
  159. for (const auto& param : params_)
  160. static_cast<const Instruction*>(param.get())
  161. ->ForEachInst(f, run_on_debug_line_insts);
  162. }
  163. void Function::ForEachDebugInstructionsInHeader(
  164. const std::function<void(Instruction*)>& f) {
  165. if (debug_insts_in_header_.empty()) return;
  166. Instruction* di = &debug_insts_in_header_.front();
  167. while (di != nullptr) {
  168. Instruction* next_instruction = di->NextNode();
  169. di->ForEachInst(f);
  170. di = next_instruction;
  171. }
  172. }
  173. BasicBlock* Function::InsertBasicBlockAfter(
  174. std::unique_ptr<BasicBlock>&& new_block, BasicBlock* position) {
  175. for (auto bb_iter = begin(); bb_iter != end(); ++bb_iter) {
  176. if (&*bb_iter == position) {
  177. new_block->SetParent(this);
  178. ++bb_iter;
  179. bb_iter = bb_iter.InsertBefore(std::move(new_block));
  180. return &*bb_iter;
  181. }
  182. }
  183. assert(false && "Could not find insertion point.");
  184. return nullptr;
  185. }
  186. BasicBlock* Function::InsertBasicBlockBefore(
  187. std::unique_ptr<BasicBlock>&& new_block, BasicBlock* position) {
  188. for (auto bb_iter = begin(); bb_iter != end(); ++bb_iter) {
  189. if (&*bb_iter == position) {
  190. new_block->SetParent(this);
  191. bb_iter = bb_iter.InsertBefore(std::move(new_block));
  192. return &*bb_iter;
  193. }
  194. }
  195. assert(false && "Could not find insertion point.");
  196. return nullptr;
  197. }
  198. bool Function::IsRecursive() const {
  199. IRContext* ctx = blocks_.front()->GetLabel()->context();
  200. IRContext::ProcessFunction mark_visited = [this](Function* fp) {
  201. return fp == this;
  202. };
  203. // Process the call tree from all of the function called by |this|. If it get
  204. // back to |this|, then we have a recursive function.
  205. std::queue<uint32_t> roots;
  206. ctx->AddCalls(this, &roots);
  207. return ctx->ProcessCallTreeFromRoots(mark_visited, &roots);
  208. }
  209. std::ostream& operator<<(std::ostream& str, const Function& func) {
  210. str << func.PrettyPrint();
  211. return str;
  212. }
  213. void Function::Dump() const {
  214. std::cerr << "Function #" << result_id() << "\n" << *this << "\n";
  215. }
  216. std::string Function::PrettyPrint(uint32_t options) const {
  217. std::ostringstream str;
  218. ForEachInst([&str, options](const Instruction* inst) {
  219. str << inst->PrettyPrint(options);
  220. if (inst->opcode() != SpvOpFunctionEnd) {
  221. str << std::endl;
  222. }
  223. });
  224. return str.str();
  225. }
  226. } // namespace opt
  227. } // namespace spvtools