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_finder.h"
16 #include "source/opt/block_merge_util.h"
17 #include "source/reduce/merge_blocks_reduction_opportunity.h"
18
19 namespace spvtools {
20 namespace reduce {
21
GetName() const22 std::string MergeBlocksReductionOpportunityFinder::GetName() const {
23 return "MergeBlocksReductionOpportunityFinder";
24 }
25
26 std::vector<std::unique_ptr<ReductionOpportunity>>
GetAvailableOpportunities(opt::IRContext * context,uint32_t target_function) const27 MergeBlocksReductionOpportunityFinder::GetAvailableOpportunities(
28 opt::IRContext* context, uint32_t target_function) const {
29 std::vector<std::unique_ptr<ReductionOpportunity>> result;
30
31 // Consider every block in every function.
32 for (auto* function : GetTargetFunctions(context, target_function)) {
33 for (auto& block : *function) {
34 // See whether it is possible to merge this block with its successor.
35 if (opt::blockmergeutil::CanMergeWithSuccessor(context, &block)) {
36 // It is, so record an opportunity to do this.
37 result.push_back(spvtools::MakeUnique<MergeBlocksReductionOpportunity>(
38 context, function, &block));
39 }
40 }
41 }
42 return result;
43 }
44
45 } // namespace reduce
46 } // namespace spvtools
47