Debian Codegraph @

third_party/spirv-tools/source/opt/local_single_store_elim_pass.h

raw
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 |
49 IRContext::kAnalysisInstrToBlockMapping |
50 IRContext::kAnalysisConstants | IRContext::kAnalysisTypes;
51 }
52
53 private:
54 // Do "single-store" optimization of function variables defined only
55 // with a single non-access-chain store in |func|. Replace all their
56 // non-access-chain loads with the value that is stored and eliminate
57 // any resulting dead code.
58 bool LocalSingleStoreElim(Function* func);
59
60 // Initialize extensions allowlist
61 void InitExtensionAllowList();
62
63 // Return true if all extensions in this module are allowed by this pass.
64 bool AllExtensionsSupported() const;
65
66 Pass::Status ProcessImpl();
67
68 // If there is a single store to |var_inst|, and it covers the entire
69 // variable, then replace all of the loads of the entire variable that are
70 // dominated by the store by the value that was stored. Returns true if the
71 // module was changed.
72 bool ProcessVariable(Instruction* var_inst);
73
74 // Collects all of the uses of |var_inst| into |uses|. This looks through
75 // OpObjectCopy's that copy the address of the variable, and collects those
76 // uses as well.
77 void FindUses(const Instruction* var_inst,
78 std::vector<Instruction*>* uses) const;
79
80 // Returns a store to |var_inst| if
81 // - it is a store to the entire variable,
82 // - and there are no other instructions that may modify |var_inst|.
83 Instruction* FindSingleStoreAndCheckUses(
84 Instruction* var_inst, const std::vector<Instruction*>& users) const;
85
86 // Returns true if the address that results from |inst| may be used as a base
87 // address in a store instruction or may be used to compute the base address
88 // of a store instruction.
89 bool FeedsAStore(Instruction* inst) const;
90
91 // Replaces all of the loads in |uses| by the value stored in |store_inst|.
92 // The load instructions are then killed. |all_rewritten| is true iff all
93 // uses have been rewritten.
94 bool RewriteLoads(Instruction* store_inst,
95 const std::vector<Instruction*>& uses, bool* all_rewritten);
96
97 // Replaces DebugDeclares of |var_id| with DebugValues using the value
98 // assignment of |store_inst|.
99 bool RewriteDebugDeclares(Instruction* store_inst, uint32_t var_id);
100
101 // Extensions supported by this pass.
102 std::unordered_set<std::string> extensions_allowlist_;
103 };
104
105 } // namespace opt
106 } // namespace spvtools
107
108 #endif // SOURCE_OPT_LOCAL_SINGLE_STORE_ELIM_PASS_H_