• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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:
name()28   const char* name() const override { return "combine-access-chains"; }
29   Status Process() override;
30 
GetPreservedAnalyses()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;
37   }
38 
39  private:
40   // Combine access chains in |function|. Blocks are processed in reverse
41   // post-order. Returns true if the function is modified.
42   bool ProcessFunction(Function& function);
43 
44   // Combines an access chain (normal, in bounds or pointer) |inst| if its base
45   // pointer is another access chain. Returns true if the access chain was
46   // modified.
47   bool CombineAccessChain(Instruction* inst);
48 
49   // Returns the value of |constant_inst| as a uint32_t.
50   uint32_t GetConstantValue(const analysis::Constant* constant_inst);
51 
52   // Returns the array stride of |inst|'s type.
53   uint32_t GetArrayStride(const Instruction* inst);
54 
55   // Returns the type by resolving the index operands |inst|. |inst| must be an
56   // access chain instruction.
57   const analysis::Type* GetIndexedType(Instruction* inst);
58 
59   // Populates |new_operands| with the operands for the combined access chain.
60   // Returns false if the access chains cannot be combined.
61   bool CreateNewInputOperands(Instruction* ptr_input, Instruction* inst,
62                               std::vector<Operand>* new_operands);
63 
64   // Combines the last index of |ptr_input| with the element operand of |inst|.
65   // Adds the combined operand to |new_operands|.
66   bool CombineIndices(Instruction* ptr_input, Instruction* inst,
67                       std::vector<Operand>* new_operands);
68 
69   // Returns the opcode to use for the combined access chain.
70   SpvOp UpdateOpcode(SpvOp base_opcode, SpvOp input_opcode);
71 
72   // Returns true if |opcode| is a pointer access chain.
73   bool IsPtrAccessChain(SpvOp opcode);
74 
75   // Returns true if |inst| (an access chain) has 64-bit indices.
76   bool Has64BitIndices(Instruction* inst);
77 };
78 
79 }  // namespace opt
80 }  // namespace spvtools
81 
82 #endif  // SOURCE_OPT_COMBINE_ACCESS_CHAINS_H_
83