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 SOURCE_OPT_DEAD_VARIABLE_ELIMINATION_H_ 16 #define SOURCE_OPT_DEAD_VARIABLE_ELIMINATION_H_ 17 18 #include <climits> 19 #include <unordered_map> 20 21 #include "source/opt/decoration_manager.h" 22 #include "source/opt/mem_pass.h" 23 24 namespace spvtools { 25 namespace opt { 26 27 class DeadVariableElimination : public MemPass { 28 public: 29 const char* name() const override { return "eliminate-dead-variables"; } 30 Status Process() override; 31 32 IRContext::Analysis GetPreservedAnalyses() override { 33 return IRContext::kAnalysisDefUse | IRContext::kAnalysisConstants | 34 IRContext::kAnalysisTypes; 35 } 36 37 private: 38 // Deletes the OpVariable instruction who result id is |result_id|. 39 void DeleteVariable(uint32_t result_id); 40 41 // Keeps track of the number of references of an id. Once that value is 0, it 42 // is safe to remove the corresponding instruction. 43 // 44 // Note that the special value kMustKeep is used to indicate that the 45 // instruction cannot be deleted for reasons other that is being explicitly 46 // referenced. 47 std::unordered_map<uint32_t, size_t> reference_count_; 48 49 // Special value used to indicate that an id cannot be safely deleted. 50 enum { kMustKeep = INT_MAX }; 51 }; 52 53 } // namespace opt 54 } // namespace spvtools 55 56 #endif // SOURCE_OPT_DEAD_VARIABLE_ELIMINATION_H_ 57