P4C
The P4 Compiler
Loading...
Searching...
No Matches
unusedDeclarations.h
1/*
2Copyright 2013-present Barefoot Networks, Inc.
3
4Licensed under the Apache License, Version 2.0 (the "License");
5you may not use this file except in compliance with the License.
6You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10Unless required by applicable law or agreed to in writing, software
11distributed under the License is distributed on an "AS IS" BASIS,
12WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13See the License for the specific language governing permissions and
14limitations under the License.
15*/
16
17#ifndef P4_UNUSEDDECLARATIONS_H_
18#define P4_UNUSEDDECLARATIONS_H_
19
20#include "../common/resolveReferences/resolveReferences.h"
21#include "ir/ir.h"
22#include "ir/pass_manager.h"
23#include "lib/stringify.h"
24
25namespace P4 {
26
27class RemoveUnusedDeclarations;
28
29class UsedDeclSet : public IHasDbPrint {
31 absl::flat_hash_set<const IR::IDeclaration *, Util::Hash> usedDecls;
32
33 public:
34 bool setUsed(const IR::IDeclaration *decl) { return usedDecls.emplace(decl).second; }
35
36 [[nodiscard]] auto begin() const { return usedDecls.begin(); }
37
38 [[nodiscard]] auto end() const { return usedDecls.end(); }
39
40 void clear() { usedDecls.clear(); }
41
42 void dbprint(std::ostream &cout) const override;
43
45 bool isUsed(const IR::IDeclaration *decl) const { return usedDecls.contains(decl); }
46};
47
49 public:
53 bool warn = false) const;
54};
55
58 UsedDeclSet &used;
59
60 public:
61 explicit CollectUsedDeclarations(UsedDeclSet &used) : used(used) {}
62
63 // We might be invoked in PassRepeated scenario, so the used set should be
64 // force cleared.
65 Visitor::profile_t init_apply(const IR::Node *node) override {
66 auto rv = Inspector::init_apply(node);
67 used.clear();
68
69 return rv;
70 }
71
72 bool preorder(const IR::KeyElement *ke) override;
73 bool preorder(const IR::PathExpression *path) override;
74 bool preorder(const IR::Type_Name *type) override;
75};
76
100 protected:
101 const UsedDeclSet &used;
102
111 std::set<const IR::Node *> *warned;
112
120 bool giveWarning(const IR::Node *node);
121 const IR::Node *process(const IR::IDeclaration *decl);
122 const IR::Node *warnIfUnused(const IR::Node *node);
123
124 // Prevent direct instantiations of this class.
125 friend class RemoveUnusedPolicy;
126 RemoveUnusedDeclarations(const UsedDeclSet &used, bool warn)
127 : used(used), warned(warn ? new std::set<const IR::Node *> : nullptr) {
128 setName("RemoveUnusedDeclarations");
129 }
130
131 public:
132 using Transform::init_apply;
133 using Transform::postorder;
134 using Transform::preorder;
135
136 Visitor::profile_t init_apply(const IR::Node *root) override;
137
138 const IR::Node *preorder(IR::P4Control *cont) override;
139 const IR::Node *preorder(IR::P4Parser *cont) override;
140 const IR::Node *preorder(IR::P4Table *cont) override;
141 const IR::Node *preorder(IR::ParserState *state) override;
142 const IR::Node *preorder(IR::Type_Enum *type) override;
143 const IR::Node *preorder(IR::Type_SerEnum *type) override;
144
145 const IR::Node *preorder(IR::Declaration_Instance *decl) override;
146 const IR::Node *preorder(IR::Method *decl) override;
147
148 // The following kinds of nodes are not deleted even if they are unreferenced
149 const IR::Node *preorder(IR::Type_Error *type) override {
150 prune();
151 return type;
152 }
153 const IR::Node *preorder(IR::Declaration_MatchKind *decl) override {
154 prune();
155 return decl;
156 }
157 const IR::Node *preorder(IR::Type_StructLike *type) override {
158 visit(type->typeParameters);
159 prune();
160 return type;
161 }
162 const IR::Node *preorder(IR::Type_Extern *type) override {
163 visit(type->typeParameters);
164 prune();
165 return type;
166 }
167 const IR::Node *preorder(IR::Type_Method *type) override {
168 visit(type->typeParameters);
169 prune();
170 return type;
171 }
172 const IR::Node *preorder(IR::Parameter *param) override;
173 const IR::Node *preorder(IR::NamedExpression *ne) override { return ne; } // never dead
174 const IR::Node *preorder(IR::Type_Var *p) override {
175 prune();
176 return warnIfUnused(p);
177 }
178
179 const IR::Node *preorder(IR::Declaration_Variable *decl) override;
180 const IR::Node *preorder(IR::Declaration *decl) override { return process(decl); }
181 const IR::Node *preorder(IR::Type_Declaration *decl) override { return process(decl); }
182};
183
190 UsedDeclSet used;
191
192 public:
193 explicit RemoveAllUnusedDeclarations(const RemoveUnusedPolicy &policy, bool warn = false)
195 policy.getRemoveUnusedDeclarationsPass(used, warn)}) {
196 setName("RemoveAllUnusedDeclarations");
197 setStopOnError(true);
198 }
199};
200
201} // namespace P4
202
203#endif /* P4_UNUSEDDECLARATIONS_H_ */
Collects all used declarations into @used set.
Definition unusedDeclarations.h:57
Definition stringify.h:33
The Declaration interface, representing objects with names.
Definition declaration.h:26
Definition node.h:94
Definition visitor.h:400
Definition ir/pass_manager.h:40
Definition ir/pass_manager.h:145
Iterates RemoveUnusedDeclarations until convergence.
Definition unusedDeclarations.h:189
Removes unused declarations.
Definition unusedDeclarations.h:99
std::set< const IR::Node * > * warned
Definition unusedDeclarations.h:111
bool giveWarning(const IR::Node *node)
Definition unusedDeclarations.cpp:63
Definition unusedDeclarations.h:48
virtual RemoveUnusedDeclarations * getRemoveUnusedDeclarationsPass(const UsedDeclSet &used, bool warn=false) const
Definition unusedDeclarations.cpp:23
Visitor mixin for looking up names in enclosing scopes from the Visitor::Context.
Definition resolveReferences.h:35
Definition visitor.h:424
Definition unusedDeclarations.h:29
bool isUsed(const IR::IDeclaration *decl) const
Definition unusedDeclarations.h:45
Definition visitor.h:78
The namespace encapsulating IR node classes.
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:24
STL namespace.