1 // Copyright (c) 2017 The Khronos Group Inc. 2 // Copyright (c) 2017 Valve Corporation 3 // Copyright (c) 2017 LunarG Inc. 4 // 5 // Licensed under the Apache License, Version 2.0 (the "License"); 6 // you may not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, software 12 // distributed under the License is distributed on an "AS IS" BASIS, 13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 // See the License for the specific language governing permissions and 15 // limitations under the License. 16 17 #ifndef SOURCE_OPT_LOCAL_SINGLE_STORE_ELIM_PASS_H_ 18 #define SOURCE_OPT_LOCAL_SINGLE_STORE_ELIM_PASS_H_ 19 20 #include <algorithm> 21 #include <map> 22 #include <queue> 23 #include <string> 24 #include <unordered_map> 25 #include <unordered_set> 26 #include <utility> 27 #include <vector> 28 29 #include "source/opt/basic_block.h" 30 #include "source/opt/def_use_manager.h" 31 #include "source/opt/mem_pass.h" 32 #include "source/opt/module.h" 33 34 namespace spvtools { 35 namespace opt { 36 37 // See optimizer.hpp for documentation. 38 class LocalSingleStoreElimPass : public Pass { 39 using cbb_ptr = const BasicBlock*; 40 41 public: 42 LocalSingleStoreElimPass(); 43 44 const char* name() const override { return "eliminate-local-single-store"; } 45 Status Process() override; 46 47 IRContext::Analysis GetPreservedAnalyses() override { 48 return IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping; 49 } 50 51 private: 52 // Do "single-store" optimization of function variables defined only 53 // with a single non-access-chain store in |func|. Replace all their 54 // non-access-chain loads with the value that is stored and eliminate 55 // any resulting dead code. 56 bool LocalSingleStoreElim(Function* func); 57 58 // Initialize extensions whitelist 59 void InitExtensionWhiteList(); 60 61 // Return true if all extensions in this module are allowed by this pass. 62 bool AllExtensionsSupported() const; 63 64 Pass::Status ProcessImpl(); 65 66 // If there is a single store to |var_inst|, and it covers the entire 67 // variable, then replace all of the loads of the entire variable that are 68 // dominated by the store by the value that was stored. Returns true if the 69 // module was changed. 70 bool ProcessVariable(Instruction* var_inst); 71 72 // Collects all of the uses of |var_inst| into |uses|. This looks through 73 // OpObjectCopy's that copy the address of the variable, and collects those 74 // uses as well. 75 void FindUses(const Instruction* var_inst, 76 std::vector<Instruction*>* uses) const; 77 78 // Returns a store to |var_inst| if 79 // - it is a store to the entire variable, 80 // - and there are no other instructions that may modify |var_inst|. 81 Instruction* FindSingleStoreAndCheckUses( 82 Instruction* var_inst, const std::vector<Instruction*>& users) const; 83 84 // Returns true if the address that results from |inst| may be used as a base 85 // address in a store instruction or may be used to compute the base address 86 // of a store instruction. 87 bool FeedsAStore(Instruction* inst) const; 88 89 // Replaces all of the loads in |uses| by the value stored in |store_inst|. 90 // The load instructions are then killed. 91 bool RewriteLoads(Instruction* store_inst, 92 const std::vector<Instruction*>& uses); 93 94 // Extensions supported by this pass. 95 std::unordered_set<std::string> extensions_whitelist_; 96 }; 97 98 } // namespace opt 99 } // namespace spvtools 100 101 #endif // SOURCE_OPT_LOCAL_SINGLE_STORE_ELIM_PASS_H_ 102