| 1 |
// Copyright (c) 2020 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_REMOVE_STRUCT_MEMBER_REDUCTION_OPPORTUNITY_H_ |
| 16 |
#define SOURCE_REDUCE_REMOVE_STRUCT_MEMBER_REDUCTION_OPPORTUNITY_H_ |
| 17 |
|
| 18 |
#include "source/reduce/reduction_opportunity.h" |
| 19 |
|
| 20 |
#include "source/opt/instruction.h" |
| 21 |
|
| 22 |
namespace spvtools { |
| 23 |
namespace reduce { |
| 24 |
|
| 25 |
// An opportunity for removing a member from a struct type, adjusting all uses |
| 26 |
// of the struct accordingly. |
| 27 |
class RemoveStructMemberReductionOpportunity : public ReductionOpportunity { |
| 28 |
public: |
| 29 |
// Constructs a reduction opportunity from the struct type |struct_type|, for |
| 30 |
// removal of member |member_index|. |
| 31 |
RemoveStructMemberReductionOpportunity(opt::Instruction* struct_type, |
| 32 |
uint32_t member_index) |
| 33 |
: struct_type_(struct_type), |
| 34 |
member_index_(member_index), |
| 35 |
original_number_of_members_(struct_type->NumInOperands()) {} |
| 36 |
|
| 37 |
// Opportunities to remove fields from a common struct type mutually |
| 38 |
// invalidate each other. We guard against this by requiring that the struct |
| 39 |
// still has the number of members it had when the opportunity was created. |
| 40 |
bool PreconditionHolds() override; |
| 41 |
|
| 42 |
protected: |
| 43 |
void Apply() override; |
| 44 |
|
| 45 |
private: |
| 46 |
// |composite_access_instruction| is an instruction that accesses a composite |
| 47 |
// id using either a series of literal indices (e.g. in the case of |
| 48 |
// OpCompositeInsert) or a series of index ids (e.g. in the case of |
| 49 |
// OpAccessChain). |
| 50 |
// |
| 51 |
// This function adjusts the indices that are used by |
| 52 |
// |composite_access_instruction| to that whenever an index is accessing a |
| 53 |
// member of |struct_type_|, it is decremented if the member is beyond |
| 54 |
// |member_index_|, to account for the removal of the |member_index_|-th |
| 55 |
// member. |
| 56 |
// |
| 57 |
// |composite_type_id| is the id of the composite type that the series of |
| 58 |
// indices is to be applied to. |
| 59 |
// |
| 60 |
// |first_index_input_operand| specifies the first input operand that is an |
| 61 |
// index. |
| 62 |
// |
| 63 |
// |literal_indices| specifies whether indices are given as literals (true), |
| 64 |
// or as ids (false). |
| 65 |
// |
| 66 |
// If id-based indexing is used, this function will add a constant for |
| 67 |
// |member_index_| - 1 to the module if needed. |
| 68 |
void AdjustAccessedIndices( |
| 69 |
uint32_t composite_type_id, uint32_t first_index_input_operand, |
| 70 |
bool literal_indices, opt::IRContext* context, |
| 71 |
opt::Instruction* composite_access_instruction) const; |
| 72 |
|
| 73 |
// The struct type from which a member is to be removed. |
| 74 |
opt::Instruction* struct_type_; |
| 75 |
|
| 76 |
uint32_t member_index_; |
| 77 |
|
| 78 |
uint32_t original_number_of_members_; |
| 79 |
}; |
| 80 |
|
| 81 |
} // namespace reduce |
| 82 |
} // namespace spvtools |
| 83 |
|
| 84 |
#endif // SOURCE_REDUCE_REMOVE_STRUCT_MEMBER_REDUCTION_OPPORTUNITY_H_ |