Home | History | Annotate | Download | only in reduce
      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_REDUCE_CHANGE_OPERAND_REDUCTION_OPPORTUNITY_H_
     16 #define SOURCE_REDUCE_CHANGE_OPERAND_REDUCTION_OPPORTUNITY_H_
     17 
     18 #include "reduction_opportunity.h"
     19 #include "source/opt/instruction.h"
     20 #include "spirv-tools/libspirv.h"
     21 
     22 namespace spvtools {
     23 namespace reduce {
     24 
     25 using namespace opt;
     26 
     27 // Captures the opportunity to change an id operand of an instruction to some
     28 // other id.
     29 class ChangeOperandReductionOpportunity : public ReductionOpportunity {
     30  public:
     31   // Constructs the opportunity to replace operand |operand_index| of |inst|
     32   // with |new_id|.
     33   ChangeOperandReductionOpportunity(Instruction* inst, uint32_t operand_index,
     34                                     uint32_t new_id)
     35       : inst_(inst),
     36         operand_index_(operand_index),
     37         original_id_(inst->GetOperand(operand_index).words[0]),
     38         original_type_(inst->GetOperand(operand_index).type),
     39         new_id_(new_id) {}
     40 
     41   // Determines whether the opportunity can be applied; it may have been viable
     42   // when discovered but later disabled by the application of some other
     43   // reduction opportunity.
     44   bool PreconditionHolds() override;
     45 
     46  protected:
     47   // Apply the change of operand.
     48   void Apply() override;
     49 
     50  private:
     51   Instruction* const inst_;
     52   const uint32_t operand_index_;
     53   const uint32_t original_id_;
     54   const spv_operand_type_t original_type_;
     55   const uint32_t new_id_;
     56 };
     57 
     58 }  // namespace reduce
     59 }  // namespace spvtools
     60 
     61 #endif  // SOURCE_REDUCE_CHANGE_OPERAND_REDUCTION_OPPORTUNITY_H_
     62