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