block_merge_pass.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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/block_merge_pass.h"
  17. #include <vector>
  18. #include "source/opt/block_merge_util.h"
  19. #include "source/opt/ir_context.h"
  20. #include "source/opt/iterator.h"
  21. namespace spvtools {
  22. namespace opt {
  23. bool BlockMergePass::MergeBlocks(Function* func) {
  24. bool modified = false;
  25. for (auto bi = func->begin(); bi != func->end();) {
  26. // Don't bother trying to merge unreachable blocks.
  27. if (context()->IsReachable(*bi) &&
  28. blockmergeutil::CanMergeWithSuccessor(context(), &*bi)) {
  29. blockmergeutil::MergeWithSuccessor(context(), func, bi);
  30. // Reprocess block.
  31. modified = true;
  32. } else {
  33. ++bi;
  34. }
  35. }
  36. return modified;
  37. }
  38. Pass::Status BlockMergePass::Process() {
  39. // Process all entry point functions.
  40. ProcessFunction pfn = [this](Function* fp) { return MergeBlocks(fp); };
  41. bool modified = context()->ProcessReachableCallTree(pfn);
  42. return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
  43. }
  44. BlockMergePass::BlockMergePass() = default;
  45. } // namespace opt
  46. } // namespace spvtools