1fd4e5da5Sopenharmony_ci// Copyright (c) 2019 Google LLC
2fd4e5da5Sopenharmony_ci//
3fd4e5da5Sopenharmony_ci// Licensed under the Apache License, Version 2.0 (the "License");
4fd4e5da5Sopenharmony_ci// you may not use this file except in compliance with the License.
5fd4e5da5Sopenharmony_ci// You may obtain a copy of the License at
6fd4e5da5Sopenharmony_ci//
7fd4e5da5Sopenharmony_ci//     http://www.apache.org/licenses/LICENSE-2.0
8fd4e5da5Sopenharmony_ci//
9fd4e5da5Sopenharmony_ci// Unless required by applicable law or agreed to in writing, software
10fd4e5da5Sopenharmony_ci// distributed under the License is distributed on an "AS IS" BASIS,
11fd4e5da5Sopenharmony_ci// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12fd4e5da5Sopenharmony_ci// See the License for the specific language governing permissions and
13fd4e5da5Sopenharmony_ci// limitations under the License.
14fd4e5da5Sopenharmony_ci
15fd4e5da5Sopenharmony_ci#ifndef SOURCE_REDUCE_MERGE_BLOCKS_REDUCTION_OPPORTUNITY_H_
16fd4e5da5Sopenharmony_ci#define SOURCE_REDUCE_MERGE_BLOCKS_REDUCTION_OPPORTUNITY_H_
17fd4e5da5Sopenharmony_ci
18fd4e5da5Sopenharmony_ci#include "source/opt/basic_block.h"
19fd4e5da5Sopenharmony_ci#include "source/opt/function.h"
20fd4e5da5Sopenharmony_ci#include "source/reduce/reduction_opportunity.h"
21fd4e5da5Sopenharmony_ci
22fd4e5da5Sopenharmony_cinamespace spvtools {
23fd4e5da5Sopenharmony_cinamespace reduce {
24fd4e5da5Sopenharmony_ci
25fd4e5da5Sopenharmony_ci// An opportunity to merge two blocks into one.
26fd4e5da5Sopenharmony_ciclass MergeBlocksReductionOpportunity : public ReductionOpportunity {
27fd4e5da5Sopenharmony_ci public:
28fd4e5da5Sopenharmony_ci  // Creates the opportunity to merge |block| with its successor, where |block|
29fd4e5da5Sopenharmony_ci  // is inside |function|, and |context| is the enclosing IR context.
30fd4e5da5Sopenharmony_ci  MergeBlocksReductionOpportunity(opt::IRContext* context,
31fd4e5da5Sopenharmony_ci                                  opt::Function* function,
32fd4e5da5Sopenharmony_ci                                  opt::BasicBlock* block);
33fd4e5da5Sopenharmony_ci
34fd4e5da5Sopenharmony_ci  bool PreconditionHolds() override;
35fd4e5da5Sopenharmony_ci
36fd4e5da5Sopenharmony_ci protected:
37fd4e5da5Sopenharmony_ci  void Apply() override;
38fd4e5da5Sopenharmony_ci
39fd4e5da5Sopenharmony_ci private:
40fd4e5da5Sopenharmony_ci  opt::IRContext* context_;
41fd4e5da5Sopenharmony_ci  opt::Function* function_;
42fd4e5da5Sopenharmony_ci
43fd4e5da5Sopenharmony_ci  // Rather than holding on to the block that can be merged with its successor,
44fd4e5da5Sopenharmony_ci  // we hold on to its successor. This is because the predecessor block might
45fd4e5da5Sopenharmony_ci  // get merged with *its* predecessor, and so will no longer exist, while the
46fd4e5da5Sopenharmony_ci  // successor will continue to exist until this opportunity gets applied.
47fd4e5da5Sopenharmony_ci  opt::BasicBlock* successor_block_;
48fd4e5da5Sopenharmony_ci};
49fd4e5da5Sopenharmony_ci
50fd4e5da5Sopenharmony_ci}  // namespace reduce
51fd4e5da5Sopenharmony_ci}  // namespace spvtools
52fd4e5da5Sopenharmony_ci
53fd4e5da5Sopenharmony_ci#endif  // SOURCE_REDUCE_MERGE_BLOCKS_REDUCTION_OPPORTUNITY_H_
54