2
0

function.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. for (auto& di : debug_insts_in_header_) {
  75. if (!di.WhileEachInst(f, run_on_debug_line_insts)) {
  76. return false;
  77. }
  78. }
  79. for (auto& bb : blocks_) {
  80. if (!bb->WhileEachInst(f, run_on_debug_line_insts)) {
  81. return false;
  82. }
  83. }
  84. if (end_inst_) return end_inst_->WhileEachInst(f, run_on_debug_line_insts);
  85. return true;
  86. }
  87. bool Function::WhileEachInst(const std::function<bool(const Instruction*)>& f,
  88. bool run_on_debug_line_insts) const {
  89. if (def_inst_) {
  90. if (!static_cast<const Instruction*>(def_inst_.get())
  91. ->WhileEachInst(f, run_on_debug_line_insts)) {
  92. return false;
  93. }
  94. }
  95. for (const auto& param : params_) {
  96. if (!static_cast<const Instruction*>(param.get())
  97. ->WhileEachInst(f, run_on_debug_line_insts)) {
  98. return false;
  99. }
  100. }
  101. for (const auto& di : debug_insts_in_header_) {
  102. if (!di.WhileEachInst(f, run_on_debug_line_insts)) {
  103. return false;
  104. }
  105. }
  106. for (const auto& bb : blocks_) {
  107. if (!static_cast<const BasicBlock*>(bb.get())->WhileEachInst(
  108. f, run_on_debug_line_insts)) {
  109. return false;
  110. }
  111. }
  112. if (end_inst_)
  113. return static_cast<const Instruction*>(end_inst_.get())
  114. ->WhileEachInst(f, run_on_debug_line_insts);
  115. return true;
  116. }
  117. void Function::ForEachParam(const std::function<void(Instruction*)>& f,
  118. bool run_on_debug_line_insts) {
  119. for (auto& param : params_)
  120. static_cast<Instruction*>(param.get())
  121. ->ForEachInst(f, run_on_debug_line_insts);
  122. }
  123. void Function::ForEachParam(const std::function<void(const Instruction*)>& f,
  124. bool run_on_debug_line_insts) const {
  125. for (const auto& param : params_)
  126. static_cast<const Instruction*>(param.get())
  127. ->ForEachInst(f, run_on_debug_line_insts);
  128. }
  129. BasicBlock* Function::InsertBasicBlockAfter(
  130. std::unique_ptr<BasicBlock>&& new_block, BasicBlock* position) {
  131. for (auto bb_iter = begin(); bb_iter != end(); ++bb_iter) {
  132. if (&*bb_iter == position) {
  133. new_block->SetParent(this);
  134. ++bb_iter;
  135. bb_iter = bb_iter.InsertBefore(std::move(new_block));
  136. return &*bb_iter;
  137. }
  138. }
  139. assert(false && "Could not find insertion point.");
  140. return nullptr;
  141. }
  142. BasicBlock* Function::InsertBasicBlockBefore(
  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 = bb_iter.InsertBefore(std::move(new_block));
  148. return &*bb_iter;
  149. }
  150. }
  151. assert(false && "Could not find insertion point.");
  152. return nullptr;
  153. }
  154. bool Function::IsRecursive() const {
  155. IRContext* ctx = blocks_.front()->GetLabel()->context();
  156. IRContext::ProcessFunction mark_visited = [this](Function* fp) {
  157. return fp == this;
  158. };
  159. // Process the call tree from all of the function called by |this|. If it get
  160. // back to |this|, then we have a recursive function.
  161. std::queue<uint32_t> roots;
  162. ctx->AddCalls(this, &roots);
  163. return ctx->ProcessCallTreeFromRoots(mark_visited, &roots);
  164. }
  165. std::ostream& operator<<(std::ostream& str, const Function& func) {
  166. str << func.PrettyPrint();
  167. return str;
  168. }
  169. void Function::Dump() const {
  170. std::cerr << "Function #" << result_id() << "\n" << *this << "\n";
  171. }
  172. std::string Function::PrettyPrint(uint32_t options) const {
  173. std::ostringstream str;
  174. ForEachInst([&str, options](const Instruction* inst) {
  175. str << inst->PrettyPrint(options);
  176. if (inst->opcode() != SpvOpFunctionEnd) {
  177. str << std::endl;
  178. }
  179. });
  180. return str.str();
  181. }
  182. } // namespace opt
  183. } // namespace spvtools