dead_variable_elimination.h 1.8 KB

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