1 // Copyright (c) 2018 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 15 #ifndef SOURCE_REDUCE_OPERAND_TO_DOMINATING_ID_REDUCTION_PASS_H_ 16 #define SOURCE_REDUCE_OPERAND_TO_DOMINATING_ID_REDUCTION_PASS_H_ 17 18 #include "reduction_pass.h" 19 20 namespace spvtools { 21 namespace reduce { 22 23 // A reduction pass that aims to bring to SPIR-V (and generalize) the idea from 24 // human-readable languages of e.g. replacing an expression with one of its 25 // arguments, (x + y) -> x, or with a reference to an identifier that was 26 // assigned to higher up in the program. The generalization of this is to 27 // replace an id with a different id of the same type defined in some 28 // dominating instruction. 29 // 30 // If id x is defined and then used several times, changing each use of x to 31 // some dominating definition may eventually allow the statement defining x 32 // to be eliminated by another pass. 33 class OperandToDominatingIdReductionPass : public ReductionPass { 34 public: 35 // Creates the reduction pass in the context of the given target environment 36 // |target_env| OperandToDominatingIdReductionPass(const spv_target_env target_env)37 explicit OperandToDominatingIdReductionPass(const spv_target_env target_env) 38 : ReductionPass(target_env) {} 39 40 ~OperandToDominatingIdReductionPass() override = default; 41 42 // The name of this pass. 43 std::string GetName() const final; 44 45 protected: 46 // Finds all opportunities for replacing an operand with a dominating 47 // instruction in a given module. 48 std::vector<std::unique_ptr<ReductionOpportunity>> GetAvailableOpportunities( 49 opt::IRContext* context) const final; 50 51 private: 52 void GetOpportunitiesForDominatingInst( 53 std::vector<std::unique_ptr<ReductionOpportunity>>* opportunities, 54 opt::Instruction* dominating_instruction, 55 opt::Function::iterator candidate_dominator_block, 56 opt::Function* function, opt::IRContext* context) const; 57 }; 58 59 } // namespace reduce 60 } // namespace spvtools 61 62 #endif // SOURCE_REDUCE_OPERAND_TO_DOMINATING_ID_REDUCTION_PASS_H_ 63