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_REDUCE_LOAD_SIZE_H_ |
16 | #define SOURCE_OPT_REDUCE_LOAD_SIZE_H_ |
17 | |
18 | #include <unordered_map> |
19 | |
20 | #include "source/opt/ir_context.h" |
21 | #include "source/opt/module.h" |
22 | #include "source/opt/pass.h" |
23 | |
24 | namespace spvtools { |
25 | namespace opt { |
26 | |
27 | // See optimizer.hpp for documentation. |
28 | class ReduceLoadSize : public Pass { |
29 | public: |
30 | const char* name() const override { return "reduce-load-size" ; } |
31 | Status Process() override; |
32 | |
33 | // Return the mask of preserved Analyses. |
34 | IRContext::Analysis GetPreservedAnalyses() override { |
35 | return IRContext::kAnalysisDefUse | |
36 | IRContext::kAnalysisInstrToBlockMapping | |
37 | IRContext::kAnalysisCombinators | IRContext::kAnalysisCFG | |
38 | IRContext::kAnalysisDominatorAnalysis | |
39 | IRContext::kAnalysisLoopAnalysis | IRContext::kAnalysisNameMap | |
40 | IRContext::kAnalysisConstants | IRContext::kAnalysisTypes; |
41 | } |
42 | |
43 | private: |
44 | // Replaces |inst|, which must be an OpCompositeExtract instruction, with |
45 | // an OpAccessChain and a load if possible. This happens only if it is a load |
46 | // feeding |inst|. Returns true if the substitution happened. The position |
47 | // of the new instructions will be in the same place as the load feeding the |
48 | // extract. |
49 | bool (Instruction* inst); |
50 | |
51 | // Returns true if the OpCompositeExtract instruction |inst| should be replace |
52 | // or not. This is determined by looking at the load that feeds |inst| if |
53 | // it is a load. |should_replace_cache_| is used to cache the results based |
54 | // on the load feeding |inst|. |
55 | bool (Instruction* inst); |
56 | |
57 | // Maps the result id of an OpLoad instruction to the result of whether or |
58 | // not the OpCompositeExtract that use the id should be replaced. |
59 | std::unordered_map<uint32_t, bool> should_replace_cache_; |
60 | }; |
61 | |
62 | } // namespace opt |
63 | } // namespace spvtools |
64 | |
65 | #endif // SOURCE_OPT_REDUCE_LOAD_SIZE_H_ |
66 | |