merge_blocks_reduction_opportunity.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright (c) 2019 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "source/reduce/merge_blocks_reduction_opportunity.h"
  15. #include "source/opt/block_merge_util.h"
  16. #include "source/opt/ir_context.h"
  17. namespace spvtools {
  18. namespace reduce {
  19. MergeBlocksReductionOpportunity::MergeBlocksReductionOpportunity(
  20. opt::IRContext* context, opt::Function* function, opt::BasicBlock* block) {
  21. // Precondition: the terminator has to be OpBranch.
  22. assert(block->terminator()->opcode() == spv::Op::OpBranch);
  23. context_ = context;
  24. function_ = function;
  25. // Get the successor block associated with the OpBranch.
  26. successor_block_ =
  27. context->cfg()->block(block->terminator()->GetSingleWordInOperand(0));
  28. }
  29. bool MergeBlocksReductionOpportunity::PreconditionHolds() {
  30. // Merge block opportunities can disable each other.
  31. // Example: Given blocks: A->B->C.
  32. // A is a loop header; B and C are blocks in the loop; C ends with OpReturn.
  33. // There are two opportunities: B and C can be merged with their predecessors.
  34. // Merge C. B now ends with OpReturn. We now just have: A->B.
  35. // Merge B is now disabled, as this would lead to A, a loop header, ending
  36. // with an OpReturn, which is invalid.
  37. const auto predecessors = context_->cfg()->preds(successor_block_->id());
  38. assert(1 == predecessors.size() &&
  39. "For a successor to be merged into its predecessor, exactly one "
  40. "predecessor must be present.");
  41. const uint32_t predecessor_id = predecessors[0];
  42. opt::BasicBlock* predecessor_block =
  43. context_->get_instr_block(predecessor_id);
  44. return opt::blockmergeutil::CanMergeWithSuccessor(context_,
  45. predecessor_block);
  46. }
  47. void MergeBlocksReductionOpportunity::Apply() {
  48. // While the original block that targeted the successor may not exist anymore
  49. // (it might have been merged with another block), some block must exist that
  50. // targets the successor. Find it.
  51. const auto predecessors = context_->cfg()->preds(successor_block_->id());
  52. assert(1 == predecessors.size() &&
  53. "For a successor to be merged into its predecessor, exactly one "
  54. "predecessor must be present.");
  55. const uint32_t predecessor_id = predecessors[0];
  56. // We need an iterator pointing to the predecessor, hence the loop.
  57. for (auto bi = function_->begin(); bi != function_->end(); ++bi) {
  58. if (bi->id() == predecessor_id) {
  59. opt::blockmergeutil::MergeWithSuccessor(context_, function_, bi);
  60. // Block merging changes the control flow graph, so invalidate it.
  61. context_->InvalidateAnalysesExceptFor(
  62. opt::IRContext::Analysis::kAnalysisNone);
  63. return;
  64. }
  65. }
  66. assert(false &&
  67. "Unreachable: we should have found a block with the desired id.");
  68. }
  69. } // namespace reduce
  70. } // namespace spvtools