inline_exhaustive_pass.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. bool 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. GenInlineCode(&newBlocks, &newVars, ii, bi);
  30. // If call block is replaced with more than one block, point
  31. // succeeding phis at new last block.
  32. if (newBlocks.size() > 1) UpdateSucceedingPhis(newBlocks);
  33. // Replace old calling block with new block(s).
  34. // We need to kill the name and decorations for the call, which
  35. // will be deleted. Other instructions in the block will be moved to
  36. // newBlocks. We don't need to do anything with those.
  37. context()->KillNamesAndDecorates(&*ii);
  38. bi = bi.Erase();
  39. for (auto& bb : newBlocks) {
  40. bb->SetParent(func);
  41. }
  42. bi = bi.InsertBefore(&newBlocks);
  43. // Insert new function variables.
  44. if (newVars.size() > 0)
  45. func->begin()->begin().InsertBefore(std::move(newVars));
  46. // Restart inlining at beginning of calling block.
  47. ii = bi->begin();
  48. modified = true;
  49. } else {
  50. ++ii;
  51. }
  52. }
  53. }
  54. return modified;
  55. }
  56. Pass::Status InlineExhaustivePass::ProcessImpl() {
  57. // Attempt exhaustive inlining on each entry point function in module
  58. ProcessFunction pfn = [this](Function* fp) { return InlineExhaustive(fp); };
  59. bool modified = ProcessEntryPointCallTree(pfn, get_module());
  60. return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
  61. }
  62. InlineExhaustivePass::InlineExhaustivePass() = default;
  63. Pass::Status InlineExhaustivePass::Process() {
  64. InitializeInline();
  65. return ProcessImpl();
  66. }
  67. } // namespace opt
  68. } // namespace spvtools