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_BLOCK_ELIM_PASS_H_ 18 #define SOURCE_OPT_LOCAL_SINGLE_BLOCK_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 28 #include "source/opt/basic_block.h" 29 #include "source/opt/def_use_manager.h" 30 #include "source/opt/mem_pass.h" 31 #include "source/opt/module.h" 32 33 namespace spvtools { 34 namespace opt { 35 36 // See optimizer.hpp for documentation. 37 class LocalSingleBlockLoadStoreElimPass : public MemPass { 38 public: 39 LocalSingleBlockLoadStoreElimPass(); 40 name()41 const char* name() const override { return "eliminate-local-single-block"; } 42 Status Process() override; 43 GetPreservedAnalyses()44 IRContext::Analysis GetPreservedAnalyses() override { 45 return IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping; 46 } 47 48 private: 49 // Return true if all uses of |varId| are only through supported reference 50 // operations ie. loads and store. Also cache in supported_ref_ptrs_. 51 // TODO(dnovillo): This function is replicated in other passes and it's 52 // slightly different in every pass. Is it possible to make one common 53 // implementation? 54 bool HasOnlySupportedRefs(uint32_t varId); 55 56 // On all entry point functions, within each basic block, eliminate 57 // loads and stores to function variables where possible. For 58 // loads, if previous load or store to same variable, replace 59 // load id with previous id and delete load. Finally, check if 60 // remaining stores are useless, and delete store and variable 61 // where possible. Assumes logical addressing. 62 bool LocalSingleBlockLoadStoreElim(Function* func); 63 64 // Initialize extensions whitelist 65 void InitExtensions(); 66 67 // Return true if all extensions in this module are supported by this pass. 68 bool AllExtensionsSupported() const; 69 70 void Initialize(); 71 Pass::Status ProcessImpl(); 72 73 // Map from function scope variable to a store of that variable in the 74 // current block whose value is currently valid. This map is cleared 75 // at the start of each block and incrementally updated as the block 76 // is scanned. The stores are candidates for elimination. The map is 77 // conservatively cleared when a function call is encountered. 78 std::unordered_map<uint32_t, Instruction*> var2store_; 79 80 // Map from function scope variable to a load of that variable in the 81 // current block whose value is currently valid. This map is cleared 82 // at the start of each block and incrementally updated as the block 83 // is scanned. The stores are candidates for elimination. The map is 84 // conservatively cleared when a function call is encountered. 85 std::unordered_map<uint32_t, Instruction*> var2load_; 86 87 // Set of variables whose most recent store in the current block cannot be 88 // deleted, for example, if there is a load of the variable which is 89 // dependent on the store and is not replaced and deleted by this pass, 90 // for example, a load through an access chain. A variable is removed 91 // from this set each time a new store of that variable is encountered. 92 std::unordered_set<uint32_t> pinned_vars_; 93 94 // Extensions supported by this pass. 95 std::unordered_set<std::string> extensions_whitelist_; 96 97 // Variables that are only referenced by supported operations for this 98 // pass ie. loads and stores. 99 std::unordered_set<uint32_t> supported_ref_ptrs_; 100 }; 101 102 } // namespace opt 103 } // namespace spvtools 104 105 #endif // SOURCE_OPT_LOCAL_SINGLE_BLOCK_ELIM_PASS_H_ 106