function.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. namespace spvtools {
  18. namespace opt {
  19. Function* Function::Clone(IRContext* ctx) const {
  20. Function* clone =
  21. new Function(std::unique_ptr<Instruction>(DefInst().Clone(ctx)));
  22. clone->params_.reserve(params_.size());
  23. ForEachParam(
  24. [clone, ctx](const Instruction* inst) {
  25. clone->AddParameter(std::unique_ptr<Instruction>(inst->Clone(ctx)));
  26. },
  27. true);
  28. clone->blocks_.reserve(blocks_.size());
  29. for (const auto& b : blocks_) {
  30. std::unique_ptr<BasicBlock> bb(b->Clone(ctx));
  31. bb->SetParent(clone);
  32. clone->AddBasicBlock(std::move(bb));
  33. }
  34. clone->SetFunctionEnd(std::unique_ptr<Instruction>(EndInst()->Clone(ctx)));
  35. return clone;
  36. }
  37. void Function::ForEachInst(const std::function<void(Instruction*)>& f,
  38. bool run_on_debug_line_insts) {
  39. if (def_inst_) def_inst_->ForEachInst(f, run_on_debug_line_insts);
  40. for (auto& param : params_) param->ForEachInst(f, run_on_debug_line_insts);
  41. for (auto& bb : blocks_) bb->ForEachInst(f, run_on_debug_line_insts);
  42. if (end_inst_) end_inst_->ForEachInst(f, run_on_debug_line_insts);
  43. }
  44. void Function::ForEachInst(const std::function<void(const Instruction*)>& f,
  45. bool run_on_debug_line_insts) const {
  46. if (def_inst_)
  47. static_cast<const Instruction*>(def_inst_.get())
  48. ->ForEachInst(f, run_on_debug_line_insts);
  49. for (const auto& param : params_)
  50. static_cast<const Instruction*>(param.get())
  51. ->ForEachInst(f, run_on_debug_line_insts);
  52. for (const auto& bb : blocks_)
  53. static_cast<const BasicBlock*>(bb.get())->ForEachInst(
  54. f, run_on_debug_line_insts);
  55. if (end_inst_)
  56. static_cast<const Instruction*>(end_inst_.get())
  57. ->ForEachInst(f, run_on_debug_line_insts);
  58. }
  59. void Function::ForEachParam(const std::function<void(const Instruction*)>& f,
  60. bool run_on_debug_line_insts) const {
  61. for (const auto& param : params_)
  62. static_cast<const Instruction*>(param.get())
  63. ->ForEachInst(f, run_on_debug_line_insts);
  64. }
  65. BasicBlock* Function::InsertBasicBlockAfter(
  66. std::unique_ptr<BasicBlock>&& new_block, BasicBlock* position) {
  67. for (auto bb_iter = begin(); bb_iter != end(); ++bb_iter) {
  68. if (&*bb_iter == position) {
  69. new_block->SetParent(this);
  70. ++bb_iter;
  71. bb_iter = bb_iter.InsertBefore(std::move(new_block));
  72. return &*bb_iter;
  73. }
  74. }
  75. assert(false && "Could not find insertion point.");
  76. return nullptr;
  77. }
  78. std::ostream& operator<<(std::ostream& str, const Function& func) {
  79. str << func.PrettyPrint();
  80. return str;
  81. }
  82. std::string Function::PrettyPrint(uint32_t options) const {
  83. std::ostringstream str;
  84. ForEachInst([&str, options](const Instruction* inst) {
  85. str << inst->PrettyPrint(options);
  86. if (inst->opcode() != SpvOpFunctionEnd) {
  87. str << std::endl;
  88. }
  89. });
  90. return str.str();
  91. }
  92. } // namespace opt
  93. } // namespace spvtools