1 // Copyright (c) 2018 The Khronos Group Inc.
2 // Copyright (c) 2018 Valve Corporation
3 // Copyright (c) 2018 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_DEAD_INSERT_ELIM_PASS_H_
18 #define SOURCE_OPT_DEAD_INSERT_ELIM_PASS_H_
19 
20 #include <algorithm>
21 #include <map>
22 #include <unordered_map>
23 #include <unordered_set>
24 #include <utility>
25 #include <vector>
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/mem_pass.h"
31 #include "source/opt/module.h"
32 
33 namespace spvtools {
34 namespace opt {
35 
36 // See optimizer.hpp for documentation.
37 class DeadInsertElimPass : public MemPass {
38  public:
39   DeadInsertElimPass() = default;
40 
name()41   const char* name() const override { return "eliminate-dead-inserts"; }
42   Status Process() override;
GetPreservedAnalyses()43   IRContext::Analysis GetPreservedAnalyses() override {
44     return IRContext::kAnalysisDefUse |
45            IRContext::kAnalysisInstrToBlockMapping |
46            IRContext::kAnalysisDecorations | IRContext::kAnalysisCombinators |
47            IRContext::kAnalysisCFG | IRContext::kAnalysisDominatorAnalysis |
48            IRContext::kAnalysisNameMap;
49   }
50 
51  private:
52   // Return the number of subcomponents in the composite type |typeId|.
53   // Return 0 if not a composite type or number of components is not a
54   // 32-bit constant.
55   uint32_t NumComponents(Instruction* typeInst);
56 
57   // Mark all inserts in instruction chain ending at |insertChain| with
58   // indices that intersect with extract indices |extIndices| starting with
59   // index at |extOffset|. Chains are composed solely of Inserts and Phis.
60   // Mark all inserts in chain if |extIndices| is nullptr.
61   void MarkInsertChain(Instruction* insertChain,
62                        std::vector<uint32_t>* extIndices, uint32_t extOffset,
63                        std::unordered_set<uint32_t>* visited_phis);
64 
65   // Perform EliminateDeadInsertsOnePass(|func|) until no modification is
66   // made. Return true if modified.
67   bool EliminateDeadInserts(Function* func);
68 
69   // DCE all dead struct, matrix and vector inserts in |func|. An insert is
70   // dead if the value it inserts is never used. Replace any reference to the
71   // insert with its original composite. Return true if modified. Dead inserts
72   // in dependence cycles are not currently eliminated. Dead inserts into
73   // arrays are not currently eliminated.
74   bool EliminateDeadInsertsOnePass(Function* func);
75 
76   // Return true if all extensions in this module are allowed by this pass.
77   bool AllExtensionsSupported() const;
78 
79   // Live inserts
80   std::unordered_set<uint32_t> liveInserts_;
81 
82   // Visited phis as insert chain is traversed; used to avoid infinite loop
83   std::unordered_map<uint32_t, bool> visitedPhis_;
84 };
85 
86 }  // namespace opt
87 }  // namespace spvtools
88 
89 #endif  // SOURCE_OPT_DEAD_INSERT_ELIM_PASS_H_
90