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: name()30 const char* name() const override { return "reduce-load-size"; } 31 Status Process() override; 32 33 // Return the mask of preserved Analyses. GetPreservedAnalyses()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 } 41 42 private: 43 // Replaces |inst|, which must be an OpCompositeExtract instruction, with 44 // an OpAccessChain and a load if possible. This happens only if it is a load 45 // feeding |inst|. Returns true if the substitution happened. The position 46 // of the new instructions will be in the same place as the load feeding the 47 // extract. 48 bool ReplaceExtract(Instruction* inst); 49 50 // Returns true if the OpCompositeExtract instruction |inst| should be replace 51 // or not. This is determined by looking at the load that feeds |inst| if 52 // it is a load. |should_replace_cache_| is used to cache the results based 53 // on the load feeding |inst|. 54 bool ShouldReplaceExtract(Instruction* inst); 55 56 // Maps the result id of an OpLoad instruction to the result of whether or 57 // not the OpCompositeExtract that use the id should be replaced. 58 std::unordered_map<uint32_t, bool> should_replace_cache_; 59 }; 60 61 } // namespace opt 62 } // namespace spvtools 63 64 #endif // SOURCE_OPT_REDUCE_LOAD_SIZE_H_ 65