• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
15 #include "source/reduce/merge_blocks_reduction_opportunity.h"
16 
17 #include "source/opt/block_merge_util.h"
18 #include "source/opt/ir_context.h"
19 
20 namespace spvtools {
21 namespace reduce {
22 
MergeBlocksReductionOpportunity(opt::IRContext * context,opt::Function * function,opt::BasicBlock * block)23 MergeBlocksReductionOpportunity::MergeBlocksReductionOpportunity(
24     opt::IRContext* context, opt::Function* function, opt::BasicBlock* block) {
25   // Precondition: the terminator has to be OpBranch.
26   assert(block->terminator()->opcode() == SpvOpBranch);
27   context_ = context;
28   function_ = function;
29   // Get the successor block associated with the OpBranch.
30   successor_block_ =
31       context->cfg()->block(block->terminator()->GetSingleWordInOperand(0));
32 }
33 
PreconditionHolds()34 bool MergeBlocksReductionOpportunity::PreconditionHolds() {
35   // Merge block opportunities can disable each other.
36   // Example: Given blocks: A->B->C.
37   // A is a loop header; B and C are blocks in the loop; C ends with OpReturn.
38   // There are two opportunities: B and C can be merged with their predecessors.
39   // Merge C. B now ends with OpReturn. We now just have: A->B.
40   // Merge B is now disabled, as this would lead to A, a loop header, ending
41   // with an OpReturn, which is invalid.
42 
43   const auto predecessors = context_->cfg()->preds(successor_block_->id());
44   assert(1 == predecessors.size() &&
45          "For a successor to be merged into its predecessor, exactly one "
46          "predecessor must be present.");
47   const uint32_t predecessor_id = predecessors[0];
48   opt::BasicBlock* predecessor_block =
49       context_->get_instr_block(predecessor_id);
50   return opt::blockmergeutil::CanMergeWithSuccessor(context_,
51                                                     predecessor_block);
52 }
53 
Apply()54 void MergeBlocksReductionOpportunity::Apply() {
55   // While the original block that targeted the successor may not exist anymore
56   // (it might have been merged with another block), some block must exist that
57   // targets the successor.  Find it.
58 
59   const auto predecessors = context_->cfg()->preds(successor_block_->id());
60   assert(1 == predecessors.size() &&
61          "For a successor to be merged into its predecessor, exactly one "
62          "predecessor must be present.");
63   const uint32_t predecessor_id = predecessors[0];
64 
65   // We need an iterator pointing to the predecessor, hence the loop.
66   for (auto bi = function_->begin(); bi != function_->end(); ++bi) {
67     if (bi->id() == predecessor_id) {
68       opt::blockmergeutil::MergeWithSuccessor(context_, function_, bi);
69       // Block merging changes the control flow graph, so invalidate it.
70       context_->InvalidateAnalysesExceptFor(
71           opt::IRContext::Analysis::kAnalysisNone);
72       return;
73     }
74   }
75 
76   assert(false &&
77          "Unreachable: we should have found a block with the desired id.");
78 }
79 
80 }  // namespace reduce
81 }  // namespace spvtools
82