2
0

private_to_local_pass.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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_PRIVATE_TO_LOCAL_PASS_H_
  15. #define SOURCE_OPT_PRIVATE_TO_LOCAL_PASS_H_
  16. #include "source/opt/ir_context.h"
  17. #include "source/opt/pass.h"
  18. namespace spvtools {
  19. namespace opt {
  20. // This pass implements total redundancy elimination. This is the same as
  21. // local redundancy elimination except it looks across basic block boundaries.
  22. // An instruction, inst, is totally redundant if there is another instruction
  23. // that dominates inst, and also computes the same value.
  24. class PrivateToLocalPass : public Pass {
  25. public:
  26. const char* name() const override { return "private-to-local"; }
  27. Status Process() override;
  28. IRContext::Analysis GetPreservedAnalyses() override {
  29. return IRContext::kAnalysisDefUse |
  30. IRContext::kAnalysisInstrToBlockMapping |
  31. IRContext::kAnalysisDecorations | IRContext::kAnalysisCombinators |
  32. IRContext::kAnalysisCFG | IRContext::kAnalysisDominatorAnalysis |
  33. IRContext::kAnalysisNameMap | IRContext::kAnalysisConstants |
  34. IRContext::kAnalysisTypes;
  35. }
  36. private:
  37. // Moves |variable| from the private storage class to the function storage
  38. // class of |function|. Returns false if the variable could not be moved.
  39. bool MoveVariable(Instruction* variable, Function* function);
  40. // |inst| is an instruction declaring a variable. If that variable is
  41. // referenced in a single function and all of uses are valid as defined by
  42. // |IsValidUse|, then that function is returned. Otherwise, the return
  43. // value is |nullptr|.
  44. Function* FindLocalFunction(const Instruction& inst) const;
  45. // Returns true is |inst| is a valid use of a pointer. In this case, a
  46. // valid use is one where the transformation is able to rewrite the type to
  47. // match a change in storage class of the original variable.
  48. bool IsValidUse(const Instruction* inst) const;
  49. // Given the result id of a pointer type, |old_type_id|, this function
  50. // returns the id of a the same pointer type except the storage class has
  51. // been changed to function. If the type does not already exist, it will be
  52. // created. Returns 0 if the new type could not be found or generated.
  53. uint32_t GetNewType(uint32_t old_type_id);
  54. // Updates |inst|, and any instruction dependent on |inst|, to reflect the
  55. // change of the base pointer now pointing to the function storage class.
  56. bool UpdateUse(Instruction* inst, Instruction* user);
  57. bool UpdateUses(Instruction* inst);
  58. };
  59. } // namespace opt
  60. } // namespace spvtools
  61. #endif // SOURCE_OPT_PRIVATE_TO_LOCAL_PASS_H_