| 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_COMBINE_ACCESS_CHAINS_H_ |
| 16 |
#define SOURCE_OPT_COMBINE_ACCESS_CHAINS_H_ |
| 17 |
|
| 18 |
#include <vector> |
| 19 |
|
| 20 |
#include "source/opt/pass.h" |
| 21 |
|
| 22 |
namespace spvtools { |
| 23 |
namespace opt { |
| 24 |
|
| 25 |
// See optimizer.hpp for documentation. |
| 26 |
class CombineAccessChains : public Pass { |
| 27 |
public: |
| 28 |
const char* name() const override { return "combine-access-chains"; } |
| 29 |
Status Process() override; |
| 30 |
|
| 31 |
IRContext::Analysis GetPreservedAnalyses() override { |
| 32 |
return IRContext::kAnalysisDefUse | |
| 33 |
IRContext::kAnalysisInstrToBlockMapping | |
| 34 |
IRContext::kAnalysisDecorations | IRContext::kAnalysisCombinators | |
| 35 |
IRContext::kAnalysisCFG | IRContext::kAnalysisDominatorAnalysis | |
| 36 |
IRContext::kAnalysisNameMap | IRContext::kAnalysisConstants | |
| 37 |
IRContext::kAnalysisTypes; |
| 38 |
} |
| 39 |
|
| 40 |
private: |
| 41 |
// Combine access chains in |function|. Blocks are processed in reverse |
| 42 |
// post-order. Returns true if the function is modified. |
| 43 |
bool ProcessFunction(Function& function); |
| 44 |
|
| 45 |
// Combines an access chain (normal, in bounds or pointer) |inst| if its base |
| 46 |
// pointer is another access chain. Returns true if the access chain was |
| 47 |
// modified. |
| 48 |
bool CombineAccessChain(Instruction* inst); |
| 49 |
|
| 50 |
// Returns the value of |constant_inst| as a uint32_t. |
| 51 |
uint32_t GetConstantValue(const analysis::Constant* constant_inst); |
| 52 |
|
| 53 |
// Returns the array stride of |inst|'s type. |
| 54 |
uint32_t GetArrayStride(const Instruction* inst); |
| 55 |
|
| 56 |
// Returns the type by resolving the index operands |inst|. |inst| must be an |
| 57 |
// access chain instruction. |
| 58 |
const analysis::Type* GetIndexedType(Instruction* inst); |
| 59 |
|
| 60 |
// Populates |new_operands| with the operands for the combined access chain. |
| 61 |
// Returns false if the access chains cannot be combined. |
| 62 |
bool CreateNewInputOperands(Instruction* ptr_input, Instruction* inst, |
| 63 |
std::vector<Operand>* new_operands); |
| 64 |
|
| 65 |
// Combines the last index of |ptr_input| with the element operand of |inst|. |
| 66 |
// Adds the combined operand to |new_operands|. |
| 67 |
bool CombineIndices(Instruction* ptr_input, Instruction* inst, |
| 68 |
std::vector<Operand>* new_operands); |
| 69 |
|
| 70 |
// Returns the opcode to use for the combined access chain. |
| 71 |
SpvOp UpdateOpcode(SpvOp base_opcode, SpvOp input_opcode); |
| 72 |
|
| 73 |
// Returns true if |opcode| is a pointer access chain. |
| 74 |
bool IsPtrAccessChain(SpvOp opcode); |
| 75 |
|
| 76 |
// Returns true if |inst| (an access chain) has 64-bit indices. |
| 77 |
bool Has64BitIndices(Instruction* inst); |
| 78 |
}; |
| 79 |
|
| 80 |
} // namespace opt |
| 81 |
} // namespace spvtools |
| 82 |
|
| 83 |
#endif // SOURCE_OPT_COMBINE_ACCESS_CHAINS_H_ |