fold.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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_FOLD_H_
  15. #define SOURCE_OPT_FOLD_H_
  16. #include <cstdint>
  17. #include <vector>
  18. #include "source/opt/const_folding_rules.h"
  19. #include "source/opt/constants.h"
  20. #include "source/opt/def_use_manager.h"
  21. #include "source/opt/folding_rules.h"
  22. namespace spvtools {
  23. namespace opt {
  24. class InstructionFolder {
  25. public:
  26. explicit InstructionFolder(IRContext* context)
  27. : context_(context),
  28. const_folding_rules_(new ConstantFoldingRules(context)),
  29. folding_rules_(new FoldingRules(context)) {
  30. folding_rules_->AddFoldingRules();
  31. const_folding_rules_->AddFoldingRules();
  32. }
  33. explicit InstructionFolder(
  34. IRContext* context, std::unique_ptr<FoldingRules>&& folding_rules,
  35. std::unique_ptr<ConstantFoldingRules>&& constant_folding_rules)
  36. : context_(context),
  37. const_folding_rules_(std::move(constant_folding_rules)),
  38. folding_rules_(std::move(folding_rules)) {
  39. folding_rules_->AddFoldingRules();
  40. const_folding_rules_->AddFoldingRules();
  41. }
  42. // Returns the result of folding a scalar instruction with the given |opcode|
  43. // and |operands|. Each entry in |operands| is a pointer to an
  44. // analysis::Constant instance, which should've been created with the constant
  45. // manager (See IRContext::get_constant_mgr).
  46. //
  47. // It is an error to call this function with an opcode that does not pass the
  48. // IsFoldableOpcode test. If any error occurs during folding, the folder will
  49. // fail with a call to assert.
  50. uint32_t FoldScalars(
  51. SpvOp opcode,
  52. const std::vector<const analysis::Constant*>& operands) const;
  53. // Returns the result of performing an operation with the given |opcode| over
  54. // constant vectors with |num_dims| dimensions. Each entry in |operands| is a
  55. // pointer to an analysis::Constant instance, which should've been created
  56. // with the constant manager (See IRContext::get_constant_mgr).
  57. //
  58. // This function iterates through the given vector type constant operands and
  59. // calculates the result for each element of the result vector to return.
  60. // Vectors with longer than 32-bit scalar components are not accepted in this
  61. // function.
  62. //
  63. // It is an error to call this function with an opcode that does not pass the
  64. // IsFoldableOpcode test. If any error occurs during folding, the folder will
  65. // fail with a call to assert.
  66. std::vector<uint32_t> FoldVectors(
  67. SpvOp opcode, uint32_t num_dims,
  68. const std::vector<const analysis::Constant*>& operands) const;
  69. // Returns true if |opcode| represents an operation handled by FoldScalars or
  70. // FoldVectors.
  71. bool IsFoldableOpcode(SpvOp opcode) const;
  72. // Returns true if |cst| is supported by FoldScalars and FoldVectors.
  73. bool IsFoldableConstant(const analysis::Constant* cst) const;
  74. // Returns true if |FoldInstructionToConstant| could fold an instruction whose
  75. // result type is |type_inst|.
  76. bool IsFoldableType(Instruction* type_inst) const;
  77. // Tries to fold |inst| to a single constant, when the input ids to |inst|
  78. // have been substituted using |id_map|. Returns a pointer to the OpConstant*
  79. // instruction if successful. If necessary, a new constant instruction is
  80. // created and placed in the global values section.
  81. //
  82. // |id_map| is a function that takes one result id and returns another. It
  83. // can be used for things like CCP where it is known that some ids contain a
  84. // constant, but the instruction itself has not been updated yet. This can
  85. // map those ids to the appropriate constants.
  86. Instruction* FoldInstructionToConstant(
  87. Instruction* inst, std::function<uint32_t(uint32_t)> id_map) const;
  88. // Returns true if |inst| can be folded into a simpler instruction.
  89. // If |inst| can be simplified, |inst| is overwritten with the simplified
  90. // instruction reusing the same result id.
  91. //
  92. // If |inst| is simplified, it is possible that the resulting code in invalid
  93. // because the instruction is in a bad location. Callers of this function
  94. // have to handle the following cases:
  95. //
  96. // 1) An OpPhi becomes and OpCopyObject - If there are OpPhi instruction after
  97. // |inst| in a basic block then this is invalid. The caller must fix this
  98. // up.
  99. bool FoldInstruction(Instruction* inst) const;
  100. // Return true if this opcode has a const folding rule associtated with it.
  101. bool HasConstFoldingRule(const Instruction* inst) const {
  102. return GetConstantFoldingRules().HasFoldingRule(inst);
  103. }
  104. private:
  105. // Returns a reference to the ConstnatFoldingRules instance.
  106. const ConstantFoldingRules& GetConstantFoldingRules() const {
  107. return *const_folding_rules_;
  108. }
  109. // Returns a reference to the FoldingRules instance.
  110. const FoldingRules& GetFoldingRules() const { return *folding_rules_; }
  111. // Returns the single-word result from performing the given unary operation on
  112. // the operand value which is passed in as a 32-bit word.
  113. uint32_t UnaryOperate(SpvOp opcode, uint32_t operand) const;
  114. // Returns the single-word result from performing the given binary operation
  115. // on the operand values which are passed in as two 32-bit word.
  116. uint32_t BinaryOperate(SpvOp opcode, uint32_t a, uint32_t b) const;
  117. // Returns the single-word result from performing the given ternary operation
  118. // on the operand values which are passed in as three 32-bit word.
  119. uint32_t TernaryOperate(SpvOp opcode, uint32_t a, uint32_t b,
  120. uint32_t c) const;
  121. // Returns the single-word result from performing the given operation on the
  122. // operand words. This only works with 32-bit operations and uses boolean
  123. // convention that 0u is false, and anything else is boolean true.
  124. // TODO(qining): Support operands other than 32-bit wide.
  125. uint32_t OperateWords(SpvOp opcode,
  126. const std::vector<uint32_t>& operand_words) const;
  127. bool FoldInstructionInternal(Instruction* inst) const;
  128. // Returns true if |inst| is a binary operation that takes two integers as
  129. // parameters and folds to a constant that can be represented as an unsigned
  130. // 32-bit value when the ids have been replaced by |id_map|. If |inst| can be
  131. // folded, the resulting value is returned in |*result|. Valid result types
  132. // for the instruction are any integer (signed or unsigned) with 32-bits or
  133. // less, or a boolean value.
  134. bool FoldBinaryIntegerOpToConstant(
  135. Instruction* inst, const std::function<uint32_t(uint32_t)>& id_map,
  136. uint32_t* result) const;
  137. // Returns true if |inst| is a binary operation on two boolean values, and
  138. // folds
  139. // to a constant boolean value when the ids have been replaced using |id_map|.
  140. // If |inst| can be folded, the result value is returned in |*result|.
  141. bool FoldBinaryBooleanOpToConstant(
  142. Instruction* inst, const std::function<uint32_t(uint32_t)>& id_map,
  143. uint32_t* result) const;
  144. // Returns true if |inst| can be folded to an constant when the ids have been
  145. // substituted using id_map. If it can, the value is returned in |result|. If
  146. // not, |result| is unchanged. It is assumed that not all operands are
  147. // constant. Those cases are handled by |FoldScalar|.
  148. bool FoldIntegerOpToConstant(Instruction* inst,
  149. const std::function<uint32_t(uint32_t)>& id_map,
  150. uint32_t* result) const;
  151. IRContext* context_;
  152. // Folding rules used by |FoldInstructionToConstant| and |FoldInstruction|.
  153. std::unique_ptr<ConstantFoldingRules> const_folding_rules_;
  154. // Folding rules used by |FoldInstruction|.
  155. std::unique_ptr<FoldingRules> folding_rules_;
  156. };
  157. } // namespace opt
  158. } // namespace spvtools
  159. #endif // SOURCE_OPT_FOLD_H_