1 // Copyright (c) 2017 The Khronos Group Inc. 2 // Copyright (c) 2017 Valve Corporation 3 // Copyright (c) 2017 LunarG Inc. 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 #ifndef SOURCE_OPT_BLOCK_MERGE_PASS_H_ 18 #define SOURCE_OPT_BLOCK_MERGE_PASS_H_ 19 20 #include <algorithm> 21 #include <map> 22 #include <queue> 23 #include <unordered_map> 24 #include <unordered_set> 25 #include <utility> 26 27 #include "source/opt/basic_block.h" 28 #include "source/opt/def_use_manager.h" 29 #include "source/opt/ir_context.h" 30 #include "source/opt/module.h" 31 #include "source/opt/pass.h" 32 33 namespace spvtools { 34 namespace opt { 35 36 // See optimizer.hpp for documentation. 37 class BlockMergePass : public Pass { 38 public: 39 BlockMergePass(); name()40 const char* name() const override { return "merge-blocks"; } 41 Status Process() override; 42 GetPreservedAnalyses()43 IRContext::Analysis GetPreservedAnalyses() override { 44 return IRContext::kAnalysisDefUse | 45 IRContext::kAnalysisInstrToBlockMapping | 46 IRContext::kAnalysisDecorations | IRContext::kAnalysisCombinators | 47 IRContext::kAnalysisNameMap; 48 } 49 50 private: 51 52 // Search |func| for blocks which have a single Branch to a block 53 // with no other predecessors. Merge these blocks into a single block. 54 bool MergeBlocks(Function* func); 55 56 // Returns true if |block| (or |id|) contains a merge instruction. 57 bool IsHeader(BasicBlock* block); 58 bool IsHeader(uint32_t id); 59 60 // Returns true if |block| (or |id|) is the merge target of a merge 61 // instruction. 62 bool IsMerge(BasicBlock* block); 63 bool IsMerge(uint32_t id); 64 }; 65 66 } // namespace opt 67 } // namespace spvtools 68 69 #endif // SOURCE_OPT_BLOCK_MERGE_PASS_H_ 70