simplification_pass.h 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // Copyright (c) 2018 Google LLC
  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_SIMPLIFICATION_PASS_H_
  15. #define SOURCE_OPT_SIMPLIFICATION_PASS_H_
  16. #include "source/opt/function.h"
  17. #include "source/opt/ir_context.h"
  18. #include "source/opt/pass.h"
  19. namespace spvtools {
  20. namespace opt {
  21. // See optimizer.hpp for documentation.
  22. class SimplificationPass : public Pass {
  23. public:
  24. const char* name() const override { return "simplify-instructions"; }
  25. Status Process() override;
  26. IRContext::Analysis GetPreservedAnalyses() override {
  27. return IRContext::kAnalysisDefUse |
  28. IRContext::kAnalysisInstrToBlockMapping |
  29. IRContext::kAnalysisDecorations | IRContext::kAnalysisCombinators |
  30. IRContext::kAnalysisCFG | IRContext::kAnalysisDominatorAnalysis |
  31. IRContext::kAnalysisNameMap | IRContext::kAnalysisConstants |
  32. IRContext::kAnalysisTypes;
  33. }
  34. private:
  35. // Returns true if the module was changed. The simplifier is called on every
  36. // instruction in |function| until nothing else in the function can be
  37. // simplified.
  38. bool SimplifyFunction(Function* function);
  39. // FactorAddMul can create |folded_inst| Mul of new Add. If Mul, push any Add
  40. // operand not in |seen_inst| into |worklist|. This is heavily restricted to
  41. // improve compile time but can be expanded for future simplifications which
  42. // simiarly create new operations.
  43. void AddNewOperands(Instruction* folded_inst,
  44. std::unordered_set<Instruction*>* inst_seen,
  45. std::vector<Instruction*>* work_list);
  46. };
  47. } // namespace opt
  48. } // namespace spvtools
  49. #endif // SOURCE_OPT_SIMPLIFICATION_PASS_H_