local_redundancy_elimination.h 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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_LOCAL_REDUNDANCY_ELIMINATION_H_
  15. #define SOURCE_OPT_LOCAL_REDUNDANCY_ELIMINATION_H_
  16. #include <map>
  17. #include "source/opt/ir_context.h"
  18. #include "source/opt/pass.h"
  19. #include "source/opt/value_number_table.h"
  20. namespace spvtools {
  21. namespace opt {
  22. // This pass implements local redundancy elimination. Its goal is to reduce the
  23. // number of times the same value is computed. It works on each basic block
  24. // independently, ie local. For each instruction in a basic block, it gets the
  25. // value number for the result id, |id|, of the instruction. If that value
  26. // number has already been computed in the basic block, it tries to replace the
  27. // uses of |id| by the id that already contains the same value. Then the
  28. // current instruction is deleted.
  29. class LocalRedundancyEliminationPass : public Pass {
  30. public:
  31. const char* name() const override { return "local-redundancy-elimination"; }
  32. Status Process() override;
  33. IRContext::Analysis GetPreservedAnalyses() override {
  34. return IRContext::kAnalysisDefUse |
  35. IRContext::kAnalysisInstrToBlockMapping |
  36. IRContext::kAnalysisDecorations | IRContext::kAnalysisCombinators |
  37. IRContext::kAnalysisCFG | IRContext::kAnalysisDominatorAnalysis |
  38. IRContext::kAnalysisNameMap | IRContext::kAnalysisConstants |
  39. IRContext::kAnalysisTypes;
  40. }
  41. protected:
  42. // Deletes instructions in |block| whose value is in |value_to_ids| or is
  43. // computed earlier in |block|.
  44. //
  45. // |vnTable| must have computed a value number for every result id defined
  46. // in |bb|.
  47. //
  48. // |value_to_ids| is a map from value number to ids. If {vn, id} is in
  49. // |value_to_ids| then vn is the value number of id, and the definition of id
  50. // dominates |bb|.
  51. //
  52. // Returns true if the module is changed.
  53. bool EliminateRedundanciesInBB(BasicBlock* block,
  54. const ValueNumberTable& vnTable,
  55. std::map<uint32_t, uint32_t>* value_to_ids);
  56. };
  57. } // namespace opt
  58. } // namespace spvtools
  59. #endif // SOURCE_OPT_LOCAL_REDUNDANCY_ELIMINATION_H_