function.cpp 7.9 KB

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