inline_exhaustive_pass.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // Copyright (c) 2017 The Khronos Group Inc.
  2. // Copyright (c) 2017 Valve Corporation
  3. // Copyright (c) 2017 LunarG Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. #include "source/opt/inline_exhaustive_pass.h"
  17. #include <utility>
  18. namespace spvtools {
  19. namespace opt {
  20. Pass::Status InlineExhaustivePass::InlineExhaustive(Function* func) {
  21. bool modified = false;
  22. // Using block iterators here because of block erasures and insertions.
  23. for (auto bi = func->begin(); bi != func->end(); ++bi) {
  24. for (auto ii = bi->begin(); ii != bi->end();) {
  25. if (IsInlinableFunctionCall(&*ii)) {
  26. // Inline call.
  27. std::vector<std::unique_ptr<BasicBlock>> newBlocks;
  28. std::vector<std::unique_ptr<Instruction>> newVars;
  29. if (!GenInlineCode(&newBlocks, &newVars, ii, bi)) {
  30. return Status::Failure;
  31. }
  32. // If call block is replaced with more than one block, point
  33. // succeeding phis at new last block.
  34. if (newBlocks.size() > 1) UpdateSucceedingPhis(newBlocks);
  35. // Replace old calling block with new block(s).
  36. bi = bi.Erase();
  37. for (auto& bb : newBlocks) {
  38. bb->SetParent(func);
  39. }
  40. bi = bi.InsertBefore(&newBlocks);
  41. // Insert new function variables.
  42. if (newVars.size() > 0)
  43. func->begin()->begin().InsertBefore(std::move(newVars));
  44. // Restart inlining at beginning of calling block.
  45. ii = bi->begin();
  46. modified = true;
  47. } else {
  48. ++ii;
  49. }
  50. }
  51. }
  52. if (modified) {
  53. FixDebugDeclares(func);
  54. }
  55. return (modified ? Status::SuccessWithChange : Status::SuccessWithoutChange);
  56. }
  57. Pass::Status InlineExhaustivePass::ProcessImpl() {
  58. Status status = Status::SuccessWithoutChange;
  59. // Attempt exhaustive inlining on each entry point function in module
  60. ProcessFunction pfn = [&status, this](Function* fp) {
  61. status = CombineStatus(status, InlineExhaustive(fp));
  62. return false;
  63. };
  64. context()->ProcessReachableCallTree(pfn);
  65. return status;
  66. }
  67. InlineExhaustivePass::InlineExhaustivePass() = default;
  68. Pass::Status InlineExhaustivePass::Process() {
  69. InitializeInline();
  70. return ProcessImpl();
  71. }
  72. } // namespace opt
  73. } // namespace spvtools