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 15 #ifndef SOURCE_OPT_CONST_FOLDING_RULES_H_ 16 #define SOURCE_OPT_CONST_FOLDING_RULES_H_ 17 18 #include <unordered_map> 19 #include <vector> 20 21 #include "source/opt/constants.h" 22 23 namespace spvtools { 24 namespace opt { 25 26 // Constant Folding Rules: 27 // 28 // The folding mechanism is built around the concept of a |ConstantFoldingRule|. 29 // A constant folding rule is a function that implements a method of simplifying 30 // an instruction to a constant. 31 // 32 // The inputs to a folding rule are: 33 // |inst| - the instruction to be simplified. 34 // |constants| - if an in-operands is an id of a constant, then the 35 // corresponding value in |constants| contains that 36 // constant value. Otherwise, the corresponding entry in 37 // |constants| is |nullptr|. 38 // 39 // A constant folding rule returns a pointer to an Constant if |inst| can be 40 // simplified using this rule. Otherwise, it returns |nullptr|. 41 // 42 // See const_folding_rules.cpp for examples on how to write a constant folding 43 // rule. 44 // 45 // Be sure to add new constant folding rules to the table of constant folding 46 // rules in the constructor for ConstantFoldingRules. The new rule should be 47 // added to the list for every opcode that it applies to. Note that earlier 48 // rules in the list are given priority. That is, if an earlier rule is able to 49 // fold an instruction, the later rules will not be attempted. 50 51 using ConstantFoldingRule = std::function<const analysis::Constant*( 52 IRContext* ctx, Instruction* inst, 53 const std::vector<const analysis::Constant*>& constants)>; 54 55 class ConstantFoldingRules { 56 public: 57 ConstantFoldingRules(); 58 59 // Returns true if there is at least 1 folding rule for |opcode|. HasFoldingRule(SpvOp opcode)60 bool HasFoldingRule(SpvOp opcode) const { return rules_.count(opcode); } 61 62 // Returns an vector of constant folding rules for |opcode|. GetRulesForOpcode(SpvOp opcode)63 const std::vector<ConstantFoldingRule>& GetRulesForOpcode( 64 SpvOp opcode) const { 65 auto it = rules_.find(opcode); 66 if (it != rules_.end()) { 67 return it->second; 68 } 69 return empty_vector_; 70 } 71 72 private: 73 std::unordered_map<uint32_t, std::vector<ConstantFoldingRule>> rules_; 74 std::vector<ConstantFoldingRule> empty_vector_; 75 }; 76 77 } // namespace opt 78 } // namespace spvtools 79 80 #endif // SOURCE_OPT_CONST_FOLDING_RULES_H_ 81