Initial implementation of merge return pass.
[platform/upstream/SPIRV-Tools.git] / source / opt / merge_return_pass.h
1 // Copyright (c) 2017 Google Inc.
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 #ifndef LIBSPIRV_OPT_MERGE_RETURN_PASS_H_
16 #define LIBSPIRV_OPT_MERGE_RETURN_PASS_H_
17
18 #include "basic_block.h"
19 #include "function.h"
20 #include "pass.h"
21
22 #include <vector>
23
24 namespace spvtools {
25 namespace opt {
26
27 // Documented in optimizer.hpp
28 class MergeReturnPass : public Pass {
29  public:
30   MergeReturnPass() = default;
31   const char* name() const override { return "merge-return-pass"; }
32   Status Process(ir::IRContext*) override;
33
34   ir::IRContext::Analysis GetPreservedAnalyses() override {
35     return ir::IRContext::kAnalysisDefUse;
36   }
37
38  private:
39   // Returns all BasicBlocks terminated by OpReturn or OpReturnValue in
40   // |function|.
41   std::vector<ir::BasicBlock*> CollectReturnBlocks(ir::Function* function);
42
43   // Returns |true| if returns were merged, |false| otherwise.
44   //
45   // Creates a new basic block with a single return. If |function| returns a
46   // value, a phi node is created to select the correct value to return.
47   // Replaces old returns with an unconditional branch to the new block.
48   bool MergeReturnBlocks(ir::Function* function,
49                          const std::vector<ir::BasicBlock*>& returnBlocks);
50 };
51
52 }  // namespace opt
53 }  // namespace spvtools
54
55 #endif  // LIBSPIRV_OPT_MERGE_RETURN_PASS_H_