2018-03-21 03:33:24 +00:00
|
|
|
// Copyright (c) 2018 Google LLC.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-08-03 12:05:33 +00:00
|
|
|
#ifndef SOURCE_OPT_COPY_PROP_ARRAYS_H_
|
|
|
|
#define SOURCE_OPT_COPY_PROP_ARRAYS_H_
|
2018-03-21 03:33:24 +00:00
|
|
|
|
2018-08-03 19:06:09 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "source/opt/mem_pass.h"
|
2018-03-21 03:33:24 +00:00
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
namespace opt {
|
|
|
|
|
|
|
|
// This pass implements a simple array copy propagation. It does not do a full
|
|
|
|
// array data flow. It looks for simple cases that meet the following
|
|
|
|
// conditions:
|
|
|
|
//
|
|
|
|
// 1) The source must never be stored to.
|
|
|
|
// 2) The target must be stored to exactly once.
|
|
|
|
// 3) The store to the target must be a store to the entire array, and be a
|
|
|
|
// copy of the entire source.
|
|
|
|
// 4) All loads of the target must be dominated by the store.
|
|
|
|
//
|
|
|
|
// The hard part is keeping all of the types correct. We do not want to
|
|
|
|
// have to do too large a search to update everything, which may not be
|
2022-02-11 21:13:14 +00:00
|
|
|
// possible, so we give up if we see any instruction that might be hard to
|
2018-03-21 03:33:24 +00:00
|
|
|
// update.
|
|
|
|
|
|
|
|
class CopyPropagateArrays : public MemPass {
|
|
|
|
public:
|
|
|
|
const char* name() const override { return "copy-propagate-arrays"; }
|
2018-07-12 13:08:45 +00:00
|
|
|
Status Process() override;
|
2018-07-09 15:32:29 +00:00
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
IRContext::Analysis GetPreservedAnalyses() override {
|
|
|
|
return IRContext::kAnalysisDefUse | IRContext::kAnalysisCFG |
|
|
|
|
IRContext::kAnalysisInstrToBlockMapping |
|
|
|
|
IRContext::kAnalysisLoopAnalysis | IRContext::kAnalysisDecorations |
|
2018-12-20 18:00:05 +00:00
|
|
|
IRContext::kAnalysisDominatorAnalysis | IRContext::kAnalysisNameMap |
|
|
|
|
IRContext::kAnalysisConstants | IRContext::kAnalysisTypes;
|
2018-03-27 18:09:53 +00:00
|
|
|
}
|
|
|
|
|
2018-03-21 03:33:24 +00:00
|
|
|
private:
|
2022-08-16 14:05:47 +00:00
|
|
|
// Represents one index in the OpAccessChain instruction. It can be either
|
|
|
|
// an instruction's result_id (OpConstant by ex), or a immediate value.
|
|
|
|
// Immediate values are used to prepare the final access chain without
|
|
|
|
// creating OpConstant instructions until done.
|
|
|
|
struct AccessChainEntry {
|
|
|
|
bool is_result_id;
|
|
|
|
union {
|
|
|
|
uint32_t result_id;
|
|
|
|
uint32_t immediate;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool operator!=(const AccessChainEntry& other) const {
|
|
|
|
return other.is_result_id != is_result_id || other.result_id != result_id;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-03-21 03:33:24 +00:00
|
|
|
// The class used to identify a particular memory object. This memory object
|
|
|
|
// will be owned by a particular variable, meaning that the memory is part of
|
|
|
|
// that variable. It could be the entire variable or a member of the
|
|
|
|
// variable.
|
|
|
|
class MemoryObject {
|
|
|
|
public:
|
|
|
|
// Construction a memory object that is owned by |var_inst|. The iterator
|
|
|
|
// |begin| and |end| traverse a container of integers that identify which
|
|
|
|
// member of |var_inst| this memory object will represent. These integers
|
2018-03-27 19:23:53 +00:00
|
|
|
// are interpreted the same way they would be in an |OpAccessChain|
|
2018-03-21 03:33:24 +00:00
|
|
|
// instruction.
|
|
|
|
template <class iterator>
|
2018-07-12 19:14:43 +00:00
|
|
|
MemoryObject(Instruction* var_inst, iterator begin, iterator end);
|
2018-03-21 03:33:24 +00:00
|
|
|
|
|
|
|
// Change |this| to now point to the member identified by |access_chain|
|
|
|
|
// (starting from the current member). The elements in |access_chain| are
|
2018-03-27 19:23:53 +00:00
|
|
|
// interpreted the same as the indices in the |OpAccessChain|
|
2018-03-21 03:33:24 +00:00
|
|
|
// instruction.
|
2022-08-16 14:05:47 +00:00
|
|
|
void PushIndirection(const std::vector<AccessChainEntry>& access_chain);
|
2018-03-21 03:33:24 +00:00
|
|
|
|
|
|
|
// Change |this| to now represent the first enclosing object to which it
|
|
|
|
// belongs. (Remove the last element off the access_chain). It is invalid
|
|
|
|
// to call this function if |this| does not represent a member of its owner.
|
2022-08-16 14:05:47 +00:00
|
|
|
void PopIndirection() {
|
2018-03-21 03:33:24 +00:00
|
|
|
assert(IsMember());
|
|
|
|
access_chain_.pop_back();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if |this| represents a member of its owner, and not the
|
|
|
|
// entire variable.
|
|
|
|
bool IsMember() const { return !access_chain_.empty(); }
|
|
|
|
|
|
|
|
// Returns the number of members in the object represented by |this|. If
|
|
|
|
// |this| does not represent a composite type, the return value will be 0.
|
|
|
|
uint32_t GetNumberOfMembers();
|
|
|
|
|
|
|
|
// Returns the owning variable that the memory object is contained in.
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* GetVariable() const { return variable_inst_; }
|
2018-03-21 03:33:24 +00:00
|
|
|
|
|
|
|
// Returns a vector of integers that can be used to access the specific
|
|
|
|
// member that |this| represents starting from the owning variable. These
|
2018-03-27 19:23:53 +00:00
|
|
|
// values are to be interpreted the same way the indices are in an
|
|
|
|
// |OpAccessChain| instruction.
|
2022-08-16 14:05:47 +00:00
|
|
|
const std::vector<AccessChainEntry>& AccessChain() const {
|
|
|
|
return access_chain_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Converts all immediate values in the AccessChain their OpConstant
|
|
|
|
// equivalent.
|
|
|
|
void BuildConstants();
|
2018-03-21 03:33:24 +00:00
|
|
|
|
|
|
|
// Returns the type id of the pointer type that can be used to point to this
|
|
|
|
// memory object.
|
2018-10-01 18:45:44 +00:00
|
|
|
uint32_t GetPointerTypeId(const CopyPropagateArrays* pass) const {
|
|
|
|
analysis::DefUseManager* def_use_mgr =
|
|
|
|
GetVariable()->context()->get_def_use_mgr();
|
2018-03-21 03:33:24 +00:00
|
|
|
analysis::TypeManager* type_mgr =
|
|
|
|
GetVariable()->context()->get_type_mgr();
|
2018-10-01 18:45:44 +00:00
|
|
|
|
|
|
|
Instruction* var_pointer_inst =
|
|
|
|
def_use_mgr->GetDef(GetVariable()->type_id());
|
|
|
|
|
|
|
|
uint32_t member_type_id = pass->GetMemberTypeId(
|
|
|
|
var_pointer_inst->GetSingleWordInOperand(1), GetAccessIds());
|
|
|
|
|
2018-03-21 03:33:24 +00:00
|
|
|
uint32_t member_pointer_type_id = type_mgr->FindPointerToType(
|
2022-11-04 21:27:10 +00:00
|
|
|
member_type_id, static_cast<spv::StorageClass>(
|
2018-10-01 18:45:44 +00:00
|
|
|
var_pointer_inst->GetSingleWordInOperand(0)));
|
2018-03-21 03:33:24 +00:00
|
|
|
return member_pointer_type_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the storage class of the memory object.
|
2022-11-04 21:27:10 +00:00
|
|
|
spv::StorageClass GetStorageClass() const {
|
2018-03-21 03:33:24 +00:00
|
|
|
analysis::TypeManager* type_mgr =
|
|
|
|
GetVariable()->context()->get_type_mgr();
|
|
|
|
const analysis::Pointer* pointer_type =
|
|
|
|
type_mgr->GetType(GetVariable()->type_id())->AsPointer();
|
|
|
|
return pointer_type->storage_class();
|
|
|
|
}
|
|
|
|
|
2018-03-28 16:27:19 +00:00
|
|
|
// Returns true if |other| represents memory that is contains inside of the
|
|
|
|
// memory represented by |this|.
|
|
|
|
bool Contains(MemoryObject* other);
|
|
|
|
|
2018-03-21 03:33:24 +00:00
|
|
|
private:
|
|
|
|
// The variable that owns this memory object.
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* variable_inst_;
|
2018-03-21 03:33:24 +00:00
|
|
|
|
|
|
|
// The access chain to reach the particular member the memory object
|
|
|
|
// represents. It should be interpreted the same way the indices in an
|
2018-03-27 19:23:53 +00:00
|
|
|
// |OpAccessChain| are interpreted.
|
2022-08-16 14:05:47 +00:00
|
|
|
std::vector<AccessChainEntry> access_chain_;
|
2018-03-27 19:23:53 +00:00
|
|
|
std::vector<uint32_t> GetAccessIds() const;
|
2018-03-21 03:33:24 +00:00
|
|
|
};
|
|
|
|
|
2018-03-27 19:23:53 +00:00
|
|
|
// Returns the memory object being stored to |var_inst| in the store
|
|
|
|
// instruction |store_inst|, if one exists, that can be used in place of
|
2018-03-21 03:33:24 +00:00
|
|
|
// |var_inst| in all of the loads of |var_inst|. This code is conservative
|
|
|
|
// and only identifies very simple cases. If no such memory object can be
|
|
|
|
// found, the return value is |nullptr|.
|
2018-03-27 19:23:53 +00:00
|
|
|
std::unique_ptr<CopyPropagateArrays::MemoryObject> FindSourceObjectIfPossible(
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* var_inst, Instruction* store_inst);
|
2018-03-21 03:33:24 +00:00
|
|
|
|
|
|
|
// Replaces all loads of |var_inst| with a load from |source| instead.
|
2018-03-27 19:23:53 +00:00
|
|
|
// |insertion_pos| is a position where it is possible to construct the
|
|
|
|
// address of |source| and also dominates all of the loads of |var_inst|.
|
2018-07-12 19:14:43 +00:00
|
|
|
void PropagateObject(Instruction* var_inst, MemoryObject* source,
|
|
|
|
Instruction* insertion_pos);
|
2018-03-21 03:33:24 +00:00
|
|
|
|
|
|
|
// Returns true if all of the references to |ptr_inst| can be rewritten and
|
|
|
|
// are dominated by |store_inst|.
|
2018-07-12 19:14:43 +00:00
|
|
|
bool HasValidReferencesOnly(Instruction* ptr_inst, Instruction* store_inst);
|
2018-03-21 03:33:24 +00:00
|
|
|
|
|
|
|
// Returns a memory object that at one time was equivalent to the value in
|
|
|
|
// |result|. If no such memory object exists, the return value is |nullptr|.
|
|
|
|
std::unique_ptr<MemoryObject> GetSourceObjectIfAny(uint32_t result);
|
|
|
|
|
|
|
|
// Returns the memory object that is loaded by |load_inst|. If a memory
|
|
|
|
// object cannot be identified, the return value is |nullptr|. The opcode of
|
|
|
|
// |load_inst| must be |OpLoad|.
|
|
|
|
std::unique_ptr<MemoryObject> BuildMemoryObjectFromLoad(
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* load_inst);
|
2018-03-21 03:33:24 +00:00
|
|
|
|
|
|
|
// Returns the memory object that at some point was equivalent to the result
|
|
|
|
// of |extract_inst|. If a memory object cannot be identified, the return
|
|
|
|
// value is |nullptr|. The opcode of |extract_inst| must be
|
|
|
|
// |OpCompositeExtract|.
|
|
|
|
std::unique_ptr<MemoryObject> BuildMemoryObjectFromExtract(
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* extract_inst);
|
2018-03-21 03:33:24 +00:00
|
|
|
|
|
|
|
// Returns the memory object that at some point was equivalent to the result
|
|
|
|
// of |construct_inst|. If a memory object cannot be identified, the return
|
2018-03-28 16:27:19 +00:00
|
|
|
// value is |nullptr|. The opcode of |constuct_inst| must be
|
2018-03-21 03:33:24 +00:00
|
|
|
// |OpCompositeConstruct|.
|
|
|
|
std::unique_ptr<MemoryObject> BuildMemoryObjectFromCompositeConstruct(
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* conststruct_inst);
|
2018-03-21 03:33:24 +00:00
|
|
|
|
2018-03-28 16:27:19 +00:00
|
|
|
// Returns the memory object that at some point was equivalent to the result
|
|
|
|
// of |insert_inst|. If a memory object cannot be identified, the return
|
|
|
|
// value is |nullptr\. The opcode of |insert_inst| must be
|
|
|
|
// |OpCompositeInsert|. This function looks for a series of
|
|
|
|
// |OpCompositeInsert| instructions that insert the elements one at a time in
|
|
|
|
// order from beginning to end.
|
|
|
|
std::unique_ptr<MemoryObject> BuildMemoryObjectFromInsert(
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* insert_inst);
|
2018-03-28 16:27:19 +00:00
|
|
|
|
2022-08-16 14:05:47 +00:00
|
|
|
// Return true if the given entry can represent the given value.
|
|
|
|
bool IsAccessChainIndexValidAndEqualTo(const AccessChainEntry& entry,
|
|
|
|
uint32_t value) const;
|
|
|
|
|
2018-03-21 03:33:24 +00:00
|
|
|
// Return true if |type_id| is a pointer type whose pointee type is an array.
|
|
|
|
bool IsPointerToArrayType(uint32_t type_id);
|
|
|
|
|
2022-08-11 16:59:37 +00:00
|
|
|
// Returns true if there are not stores using |ptr_inst| or something derived
|
2018-03-21 03:33:24 +00:00
|
|
|
// from it.
|
2018-07-12 19:14:43 +00:00
|
|
|
bool HasNoStores(Instruction* ptr_inst);
|
2018-03-21 03:33:24 +00:00
|
|
|
|
|
|
|
// Creates an |OpAccessChain| instruction whose result is a pointer the memory
|
|
|
|
// represented by |source|. The new instruction will be placed before
|
|
|
|
// |insertion_point|. |insertion_point| must be part of a function. Returns
|
|
|
|
// the new instruction.
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* BuildNewAccessChain(Instruction* insertion_point,
|
|
|
|
MemoryObject* source) const;
|
2018-03-21 03:33:24 +00:00
|
|
|
|
|
|
|
// Rewrites all uses of |original_ptr| to use |new_pointer_inst| updating
|
|
|
|
// types of other instructions as needed. This function should not be called
|
|
|
|
// if |CanUpdateUses(original_ptr_inst, new_pointer_inst->type_id())| returns
|
|
|
|
// false.
|
2018-07-12 19:14:43 +00:00
|
|
|
void UpdateUses(Instruction* original_ptr_inst,
|
|
|
|
Instruction* new_pointer_inst);
|
2018-03-21 03:33:24 +00:00
|
|
|
|
|
|
|
// Return true if |UpdateUses| is able to change all of the uses of
|
|
|
|
// |original_ptr_inst| to |type_id| and still have valid code.
|
2018-07-12 19:14:43 +00:00
|
|
|
bool CanUpdateUses(Instruction* original_ptr_inst, uint32_t type_id);
|
2018-03-26 18:43:24 +00:00
|
|
|
|
2018-03-27 19:23:53 +00:00
|
|
|
// Returns a store to |var_inst| that writes to the entire variable, and is
|
|
|
|
// the only store that does so. Note it does not look through OpAccessChain
|
|
|
|
// instruction, so partial stores are not considered.
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* FindStoreInstruction(const Instruction* var_inst) const;
|
2018-10-01 18:45:44 +00:00
|
|
|
|
|
|
|
// Return the type id of the member of the type |id| access using
|
|
|
|
// |access_chain|. The elements of |access_chain| are to be interpreted the
|
|
|
|
// same way the indexes are used in an |OpCompositeExtract| instruction.
|
|
|
|
uint32_t GetMemberTypeId(uint32_t id,
|
|
|
|
const std::vector<uint32_t>& access_chain) const;
|
2018-03-21 03:33:24 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace opt
|
|
|
|
} // namespace spvtools
|
|
|
|
|
2018-08-03 12:05:33 +00:00
|
|
|
#endif // SOURCE_OPT_COPY_PROP_ARRAYS_H_
|