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_OUTPUT_STORES_H_ 17 #define SOURCE_OPT_ELIMINATE_DEAD_OUTPUT_STORES_H_ 18 19 #include <unordered_set> 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 EliminateDeadOutputStoresPass : public Pass { 30 public: EliminateDeadOutputStoresPass(std::unordered_set<uint32_t> * live_locs,std::unordered_set<uint32_t> * live_builtins)31 explicit EliminateDeadOutputStoresPass( 32 std::unordered_set<uint32_t>* live_locs, 33 std::unordered_set<uint32_t>* live_builtins) 34 : live_locs_(live_locs), live_builtins_(live_builtins) {} 35 name()36 const char* name() const override { return "eliminate-dead-output-stores"; } 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 // Initialize elimination 51 void InitializeElimination(); 52 53 // Do dead output store analysis 54 Status DoDeadOutputStoreElimination(); 55 56 // Kill all stores resulting from |ref|. 57 void KillAllStoresOfRef(Instruction* ref); 58 59 // Kill all dead stores resulting from |user| of loc-based |var|. 60 void KillAllDeadStoresOfLocRef(Instruction* user, Instruction* var); 61 62 // Kill all dead stores resulting from |user| of builtin |var|. 63 void KillAllDeadStoresOfBuiltinRef(Instruction* user, Instruction* var); 64 65 // Return true if any of |count| locations starting at location |start| are 66 // live. 67 bool AnyLocsAreLive(uint32_t start, uint32_t count); 68 69 // Return true if builtin |bi| is live. 70 bool IsLiveBuiltin(uint32_t bi); 71 72 std::unordered_set<uint32_t>* live_locs_; 73 std::unordered_set<uint32_t>* live_builtins_; 74 75 std::vector<Instruction*> kill_list_; 76 }; 77 78 } // namespace opt 79 } // namespace spvtools 80 81 #endif // SOURCE_OPT_ELIMINATE_DEAD_OUTPUT_STORES_H_ 82