function.cpp 8.1 KB

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