inline_exhaustive_pass.cpp 2.9 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. // We need to kill the name and decorations for the call, which
  37. // will be deleted. Other instructions in the block will be moved to
  38. // newBlocks. We don't need to do anything with those.
  39. context()->KillNamesAndDecorates(&*ii);
  40. bi = bi.Erase();
  41. for (auto& bb : newBlocks) {
  42. bb->SetParent(func);
  43. }
  44. bi = bi.InsertBefore(&newBlocks);
  45. // Insert new function variables.
  46. if (newVars.size() > 0)
  47. func->begin()->begin().InsertBefore(std::move(newVars));
  48. // Restart inlining at beginning of calling block.
  49. ii = bi->begin();
  50. modified = true;
  51. } else {
  52. ++ii;
  53. }
  54. }
  55. }
  56. return (modified ? Status::SuccessWithChange : Status::SuccessWithoutChange);
  57. }
  58. Pass::Status InlineExhaustivePass::ProcessImpl() {
  59. Status status = Status::SuccessWithoutChange;
  60. // Attempt exhaustive inlining on each entry point function in module
  61. ProcessFunction pfn = [&status, this](Function* fp) {
  62. status = CombineStatus(status, InlineExhaustive(fp));
  63. return false;
  64. };
  65. context()->ProcessEntryPointCallTree(pfn);
  66. return status;
  67. }
  68. InlineExhaustivePass::InlineExhaustivePass() = default;
  69. Pass::Status InlineExhaustivePass::Process() {
  70. InitializeInline();
  71. return ProcessImpl();
  72. }
  73. } // namespace opt
  74. } // namespace spvtools