1 // Copyright (c) 2022 The Khronos Group Inc. 2 // Copyright (c) 2022 LunarG Inc. 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 #ifndef SOURCE_OPT_ELIMINATE_DEAD_INPUT_COMPONENTS_H_ 17 #define SOURCE_OPT_ELIMINATE_DEAD_INPUT_COMPONENTS_H_ 18 19 #include <unordered_map> 20 21 #include "source/opt/ir_context.h" 22 #include "source/opt/module.h" 23 #include "source/opt/pass.h" 24 25 namespace spvtools { 26 namespace opt { 27 28 // See optimizer.hpp for documentation. 29 class EliminateDeadInputComponentsPass : public Pass { 30 public: EliminateDeadInputComponentsPass()31 explicit EliminateDeadInputComponentsPass() {} 32 name()33 const char* name() const override { 34 return "eliminate-dead-input-components"; 35 } 36 37 Status Process() override; 38 39 // Return the mask of preserved Analyses. GetPreservedAnalyses()40 IRContext::Analysis GetPreservedAnalyses() override { 41 return IRContext::kAnalysisDefUse | 42 IRContext::kAnalysisInstrToBlockMapping | 43 IRContext::kAnalysisCombinators | IRContext::kAnalysisCFG | 44 IRContext::kAnalysisDominatorAnalysis | 45 IRContext::kAnalysisLoopAnalysis | IRContext::kAnalysisNameMap | 46 IRContext::kAnalysisConstants | IRContext::kAnalysisTypes; 47 } 48 49 private: 50 // Find the max constant used to index the variable declared by |var| 51 // through OpAccessChain or OpInBoundsAccessChain. If any non-constant 52 // indices or non-Op*AccessChain use of |var|, return |original_max|. 53 unsigned FindMaxIndex(Instruction& var, unsigned original_max); 54 55 // Change the length of the array |inst| to |length| 56 void ChangeArrayLength(Instruction& inst, unsigned length); 57 58 // Change the length of the struct |struct_var| to |length| 59 void ChangeStructLength(Instruction& struct_var, unsigned length); 60 }; 61 62 } // namespace opt 63 } // namespace spvtools 64 65 #endif // SOURCE_OPT_ELIMINATE_DEAD_INPUT_COMPONENTS_H_ 66