inline_exhaustive_pass.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. return (modified ? Status::SuccessWithChange : Status::SuccessWithoutChange);
  53. }
  54. Pass::Status InlineExhaustivePass::ProcessImpl() {
  55. Status status = Status::SuccessWithoutChange;
  56. // Attempt exhaustive inlining on each entry point function in module
  57. ProcessFunction pfn = [&status, this](Function* fp) {
  58. status = CombineStatus(status, InlineExhaustive(fp));
  59. return false;
  60. };
  61. context()->ProcessReachableCallTree(pfn);
  62. return status;
  63. }
  64. InlineExhaustivePass::InlineExhaustivePass() = default;
  65. Pass::Status InlineExhaustivePass::Process() {
  66. InitializeInline();
  67. return ProcessImpl();
  68. }
  69. } // namespace opt
  70. } // namespace spvtools