ccp_pass.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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_CCP_PASS_H_
  15. #define SOURCE_OPT_CCP_PASS_H_
  16. #include <memory>
  17. #include <unordered_map>
  18. #include "source/opt/constants.h"
  19. #include "source/opt/function.h"
  20. #include "source/opt/ir_context.h"
  21. #include "source/opt/mem_pass.h"
  22. #include "source/opt/module.h"
  23. #include "source/opt/propagator.h"
  24. namespace spvtools {
  25. namespace opt {
  26. class CCPPass : public MemPass {
  27. public:
  28. CCPPass() = default;
  29. const char* name() const override { return "ccp"; }
  30. Status Process() override;
  31. IRContext::Analysis GetPreservedAnalyses() override {
  32. return IRContext::kAnalysisDefUse |
  33. IRContext::kAnalysisInstrToBlockMapping |
  34. IRContext::kAnalysisDecorations | IRContext::kAnalysisCombinators |
  35. IRContext::kAnalysisCFG | IRContext::kAnalysisDominatorAnalysis |
  36. IRContext::kAnalysisNameMap | IRContext::kAnalysisConstants |
  37. IRContext::kAnalysisTypes;
  38. }
  39. private:
  40. // Initializes the pass.
  41. void Initialize();
  42. // Runs constant propagation on the given function |fp|. Returns true if any
  43. // constants were propagated and the IR modified.
  44. bool PropagateConstants(Function* fp);
  45. // Visits a single instruction |instr|. If the instruction is a conditional
  46. // branch that always jumps to the same basic block, it sets the destination
  47. // block in |dest_bb|.
  48. SSAPropagator::PropStatus VisitInstruction(Instruction* instr,
  49. BasicBlock** dest_bb);
  50. // Visits an OpPhi instruction |phi|. This applies the meet operator for the
  51. // CCP lattice. Essentially, if all the operands in |phi| have the same
  52. // constant value C, the result for |phi| gets assigned the value C.
  53. SSAPropagator::PropStatus VisitPhi(Instruction* phi);
  54. // Visits an SSA assignment instruction |instr|. If the RHS of |instr| folds
  55. // into a constant value C, then the LHS of |instr| is assigned the value C in
  56. // |values_|.
  57. SSAPropagator::PropStatus VisitAssignment(Instruction* instr);
  58. // Visits a branch instruction |instr|. If the branch is conditional
  59. // (OpBranchConditional or OpSwitch), and the value of its selector is known,
  60. // |dest_bb| will be set to the corresponding destination block. Unconditional
  61. // branches always set |dest_bb| to the single destination block.
  62. SSAPropagator::PropStatus VisitBranch(Instruction* instr,
  63. BasicBlock** dest_bb) const;
  64. // Replaces all operands used in |fp| with the corresponding constant values
  65. // in |values_|. Returns true if any operands were replaced, and false
  66. // otherwise.
  67. bool ReplaceValues();
  68. // Marks |instr| as varying by registering a varying value for its result
  69. // into the |values_| table. Returns SSAPropagator::kVarying.
  70. SSAPropagator::PropStatus MarkInstructionVarying(Instruction* instr);
  71. // Returns true if |id| is the special SSA id that corresponds to a varying
  72. // value.
  73. bool IsVaryingValue(uint32_t id) const;
  74. // Constant manager for the parent IR context. Used to record new constants
  75. // generated during propagation.
  76. analysis::ConstantManager* const_mgr_;
  77. // Returns a new value for |instr| by computing the meet operation between
  78. // its existing value and |val2|.
  79. //
  80. // Given two values val1 and val2, the meet operation in the constant
  81. // lattice uses the following rules:
  82. //
  83. // meet(val1, UNDEFINED) = val1
  84. // meet(val1, VARYING) = VARYING
  85. // meet(val1, val2) = val1 if val1 == val2
  86. // meet(val1, val2) = VARYING if val1 != val2
  87. //
  88. // When two different values meet, the result is always varying because CCP
  89. // does not allow lateral transitions in the lattice. This prevents
  90. // infinite cycles during propagation.
  91. uint32_t ComputeLatticeMeet(Instruction* instr, uint32_t val2);
  92. // Constant value table. Each entry <id, const_decl_id> in this map
  93. // represents the compile-time constant value for |id| as declared by
  94. // |const_decl_id|. Each |const_decl_id| in this table is an OpConstant
  95. // declaration for the current module.
  96. //
  97. // Additionally, this table keeps track of SSA IDs with varying values. If an
  98. // SSA ID is found to have a varying value, it will have an entry in this
  99. // table that maps to the special SSA id kVaryingSSAId. These values are
  100. // never replaced in the IR, they are used by CCP during propagation.
  101. std::unordered_map<uint32_t, uint32_t> values_;
  102. // Propagator engine used.
  103. std::unique_ptr<SSAPropagator> propagator_;
  104. // Value for the module's ID bound before running CCP. Used to detect whether
  105. // propagation created new instructions.
  106. uint32_t original_id_bound_;
  107. };
  108. } // namespace opt
  109. } // namespace spvtools
  110. #endif // SOURCE_OPT_CCP_PASS_H_