P4C
The P4 Compiler
Loading...
Searching...
No Matches
frontends/p4/def_use.h
1/*
2 * Copyright 2016 VMware, Inc.
3 * SPDX-FileCopyrightText: 2016 VMware, Inc.
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8#ifndef FRONTENDS_P4_DEF_USE_H_
9#define FRONTENDS_P4_DEF_USE_H_
10
11#include "absl/container/flat_hash_set.h"
12#include "absl/container/inlined_vector.h"
13#include "absl/container/node_hash_set.h"
14#include "frontends/common/resolveReferences/referenceMap.h"
15#include "ir/ir.h"
16#include "lib/alloc_trace.h"
17#include "lib/flat_map.h"
18#include "lib/hash.h"
19#include "lib/hvec_map.h"
20#include "lib/ordered_set.h"
21#include "typeMap.h"
22
23namespace P4 {
24
25class ComputeWriteSet;
26class StorageFactory;
27class LocationSet;
28
29// A location in the program. Includes the context from the visitor, which needs to
30// be copied out of the Visitor::Context objects, as they are allocated on the stack and
31// will become invalid as the IR traversal continues
32struct loc_t {
33 const IR::Node *node;
34 const loc_t *parent;
35 mutable std::size_t computedHash = 0;
36 bool operator==(const loc_t &a) const {
37 if (node != a.node) return false;
38 if (parent == a.parent) return true;
39 if (!parent || !a.parent) return false;
40 return *parent == *a.parent;
41 }
42 std::size_t hash() const;
43};
44
45// #define DEBUG_LOCATION_IDS
46
48class StorageLocation : public IHasDbPrint, public ICastable {
49#ifdef DEBUG_LOCATION_IDS
50 static unsigned crtid;
51 unsigned id;
52#endif
53
54 public:
55 virtual ~StorageLocation() {}
56 const IR::Type *type;
57 const cstring name;
58 StorageLocation(const IR::Type *type, cstring name)
59 :
60#ifdef DEBUG_LOCATION_IDS
61 id(crtid++),
62#endif
63 type(type),
64 name(name) {
65 CHECK_NULL(type);
66 }
67 void dbprint(std::ostream &out) const override {
68#ifdef DEBUG_LOCATION_IDS
69 out << id << " " << name;
70#else
71 out << dbp(type) << " " << name;
72#endif
73 }
74 cstring toString() const { return name; }
75 virtual const char *locClass() const = 0;
76
79 virtual void addValidBits(LocationSet *result) const = 0;
82 virtual void removeHeaders(LocationSet *result) const = 0;
85 virtual void addLastIndexField(LocationSet *result) const = 0;
86
87 DECLARE_TYPEINFO(StorageLocation);
88};
89
92class BaseLocation : public StorageLocation {
93 public:
94 BaseLocation(const IR::Type *type, cstring name) : StorageLocation(type, name) {
95 if (auto tt = type->to<IR::Type_Tuple>())
96 BUG_CHECK(tt->getSize() == 0, "%1%: tuples with fields are not base locations", tt);
97 else if (auto ts = type->to<IR::Type_StructLike>())
98 BUG_CHECK(ts->fields.size() == 0, "%1%: structs with fields are not base locations",
99 tt);
100 else
101 BUG_CHECK(type->is<IR::Type_Bits>() || type->is<IR::Type_Enum>() ||
102 type->is<IR::Type_Boolean>() || type->is<IR::Type_Var>() ||
103 type->is<IR::Type_Error>() || type->is<IR::Type_Varbits>() ||
104 type->is<IR::Type_Newtype>() || type->is<IR::Type_SerEnum>() ||
105 type->is<IR::Type_List>(),
106 "%1%: unexpected type", type);
107 }
108 void addValidBits(LocationSet *) const override {}
109 void addLastIndexField(LocationSet *) const override {}
110 void removeHeaders(LocationSet *result) const override;
111 const char *locClass() const override { return "Base"; }
112
113 DECLARE_TYPEINFO(BaseLocation, StorageLocation);
114};
115
117class WithFieldsLocation : public StorageLocation {
119 absl::InlinedVector<std::pair<cstring, const StorageLocation *>, 4>>
120 fieldLocations;
121
122 void createField(cstring name, StorageLocation *field) {
123 fieldLocations.emplace(name, field);
124 CHECK_NULL(field);
125 }
126 void replaceField(cstring field, StorageLocation *replacement) {
127 fieldLocations[field] = replacement;
128 }
129
130 friend class StorageFactory;
131
132 protected:
133 WithFieldsLocation(const IR::Type *type, cstring name) : StorageLocation(type, name) {}
134
135 public:
136 void addField(cstring field, LocationSet *addTo) const;
137 void removeHeaders(LocationSet *result) const override;
138 auto fields() const { return Values(fieldLocations); }
139 const char *locClass() const override { return "WithFields"; }
140 void dbprint(std::ostream &out) const override {
141 out << "<" << locClass() << ">{";
142 const char *sep = "";
143 for (auto f : fieldLocations) {
144 out << sep << *f.second;
145 sep = ", ";
146 }
147 out << "}";
148 }
149
150 DECLARE_TYPEINFO(WithFieldsLocation, StorageLocation);
151};
152
154class StructLocation : public WithFieldsLocation {
155 protected:
156 StructLocation(const IR::Type *type, cstring name) : WithFieldsLocation(type, name) {
157 BUG_CHECK(type->is<IR::Type_StructLike>(), "%1%: unexpected type", type);
158 }
159 friend class StorageFactory;
160
161 public:
162 void addValidBits(LocationSet *result) const override;
163 void addLastIndexField(LocationSet *result) const override;
164 bool isHeader() const { return type->is<IR::Type_Header>(); }
165 bool isHeaderUnion() const { return type->is<IR::Type_HeaderUnion>(); }
166 bool isStruct() const { return type->is<IR::Type_Struct>(); }
167 const char *locClass() const override { return "Struct"; }
168
169 DECLARE_TYPEINFO(StructLocation, WithFieldsLocation);
170};
171
173class IndexedLocation : public StorageLocation {
174 absl::InlinedVector<const StorageLocation *, 8> elements;
175
176 protected:
177 void createElement(unsigned index, StorageLocation *element) {
178 elements[index] = element;
179 CHECK_NULL(element);
180 }
181
182 IndexedLocation(const IR::Type *type, cstring name) : StorageLocation(type, name) {
183 CHECK_NULL(type);
184 const auto *it = type->to<IR::Type_Indexed>();
185 BUG_CHECK(it != nullptr, "%1%: unexpected type", type);
186 elements.resize(it->getSize());
187 }
188
189 friend class StorageFactory;
190
191 public:
192 void addElement(unsigned index, LocationSet *result) const;
193 auto begin() const { return elements.cbegin(); }
194 auto end() const { return elements.cend(); }
195 const char *locClass() const override { return "Indexed"; }
196 void dbprint(std::ostream &out) const override {
197 out << "<" << locClass() << ">[";
198 const char *sep = "";
199 for (unsigned i = 0; i < elements.size(); i++) {
200 out << sep << *elements.at(i);
201 sep = ", ";
202 }
203 out << "]";
204 }
205 size_t getSize() const { return elements.size(); }
206 void removeHeaders(LocationSet *result) const override;
207
208 DECLARE_TYPEINFO(IndexedLocation, StorageLocation);
209};
210
212class TupleLocation : public IndexedLocation {
213 protected:
214 TupleLocation(const IR::Type *type, cstring name) : IndexedLocation(type, name) {}
215 friend class StorageFactory;
216
217 public:
218 void addValidBits(LocationSet *) const override {}
219 void addLastIndexField(LocationSet *) const override {}
220 const char *locClass() const override { return "Tuple"; }
221
222 DECLARE_TYPEINFO(TupleLocation, IndexedLocation);
223};
224
225class ArrayLocation : public IndexedLocation {
226 const StorageLocation *lastIndexField = nullptr; // accessed by lastIndex
227
228 void setLastIndexField(const StorageLocation *location) { lastIndexField = location; }
229
230 protected:
231 ArrayLocation(const IR::Type *type, cstring name) : IndexedLocation(type, name) {}
232 friend class StorageFactory;
233
234 public:
235 const StorageLocation *getLastIndexField() const { return lastIndexField; }
236
237 void addValidBits(LocationSet *result) const override;
238 void removeHeaders(LocationSet *) const override {} // no results added
239 void addLastIndexField(LocationSet *result) const override;
240 const char *locClass() const override { return "Array"; }
241
242 DECLARE_TYPEINFO(ArrayLocation, IndexedLocation);
243};
244
246 // FIXME: Allocate StorageLocations from an arena, not global allocator
247 mutable std::vector<std::unique_ptr<StorageLocation>> storageLocations;
248
249 template <class T>
250 T *construct(const IR::Type *type, cstring name) const;
251
252 static constexpr std::string_view indexFieldName = "$last_index";
253
254 public:
255 StorageLocation *create(const IR::Type *type, cstring name) const;
256
257 static const cstring validFieldName;
258};
259
262class LocationSet : public IHasDbPrint {
263 using LocationsStorage = ordered_set<const StorageLocation *>;
264 LocationsStorage locations;
265
266 class canonical_iterator {
267 absl::InlinedVector<const StorageLocation *, 8> workList;
268
269 void unwrapTop() {
270 if (workList.empty()) return;
271
272 const auto *location = workList.back();
273 if (location->is<BaseLocation>()) return;
274
275 if (const auto *wfl = location->to<WithFieldsLocation>()) {
276 workList.pop_back();
277 for (const auto *f : Util::iterator_range(wfl->fields()).reverse())
278 workList.push_back(f);
279 unwrapTop();
280 return;
281 }
282
283 if (const auto *a = location->to<IndexedLocation>()) {
284 workList.pop_back();
285 for (const auto *f : Util::iterator_range(*a).reverse()) workList.push_back(f);
286 unwrapTop();
287 return;
288 }
289
290 BUG("unexpected location");
291 }
292
293 public:
294 using iterator_category = std::forward_iterator_tag;
295 using value_type = const StorageLocation *;
296 using difference_type = ptrdiff_t;
297 using pointer = value_type;
298 using reference = value_type;
299
300 canonical_iterator() = default;
301
302 explicit canonical_iterator(const LocationsStorage &locations) {
303 for (const auto *loc : Util::iterator_range(locations).reverse())
304 workList.push_back(loc);
305 unwrapTop();
306 }
307 canonical_iterator &operator++() {
308 workList.pop_back();
309 unwrapTop();
310 return *this;
311 }
312 canonical_iterator operator++(int) {
313 auto copy = *this;
314 ++*this;
315 return copy;
316 }
317 bool operator==(const canonical_iterator &i) const { return workList == i.workList; }
318 bool operator!=(const canonical_iterator &i) const { return workList != i.workList; }
319 reference operator*() const { return workList.back(); }
320 pointer operator->() const { return workList.back(); }
321 };
322
323 public:
324 LocationSet() = default;
325 explicit LocationSet(const ordered_set<const StorageLocation *> &other) : locations(other) {}
326 explicit LocationSet(const StorageLocation *location) {
327 CHECK_NULL(location);
328 locations.emplace(location);
329 }
330 static const LocationSet *empty;
331
332 const LocationSet *getField(cstring field) const;
333 const LocationSet *getValidField() const;
334 const LocationSet *getIndex(unsigned index) const;
335 const LocationSet *allElements() const;
336 const LocationSet *getArrayLastIndex() const;
337
338 void add(const StorageLocation *location) {
339 CHECK_NULL(location);
340 locations.emplace(location);
341 }
342 const LocationSet *join(const LocationSet *other) const;
345 const LocationSet *canonicalize() const;
346 void addCanonical(const StorageLocation *location);
347 auto begin() const { return locations.cbegin(); }
348 auto end() const { return locations.cend(); }
349 auto canon_begin() const { return canonical_iterator(locations); }
350 auto canon_end() const { return canonical_iterator(); }
351 auto canonical() const { return Util::iterator_range(canon_begin(), canon_end()); }
352
353 void dbprint(std::ostream &out) const override {
354 if (locations.empty()) out << "LocationSet::empty";
355 for (auto l : locations) {
356 l->dbprint(out);
357 out << "; ";
358 }
359 }
360 // only defined for canonical representations
361 bool overlaps(const LocationSet *other) const;
362 bool operator==(const LocationSet &other) const;
363 bool isEmpty() const { return locations.empty(); }
364};
365
367class StorageMap : public IHasDbPrint {
370 StorageFactory factory;
371
372 public:
373 ReferenceMap *refMap;
374 TypeMap *typeMap;
375
376 StorageMap(ReferenceMap *refMap, TypeMap *typeMap) : refMap(refMap), typeMap(typeMap) {
377 CHECK_NULL(refMap);
378 CHECK_NULL(typeMap);
379 }
380 const StorageLocation *add(const IR::IDeclaration *decl) {
381 CHECK_NULL(decl);
382 auto type = typeMap->getType(decl->getNode(), true);
383 auto loc = factory.create(type, decl->getName() + "/" + decl->externalName());
384 if (loc != nullptr) storage.emplace(decl, loc);
385 return loc;
386 }
387 const StorageLocation *getOrAdd(const IR::IDeclaration *decl) {
388 const auto *s = getStorage(decl);
389 if (s != nullptr) return s;
390 return add(decl);
391 }
392 const StorageLocation *getStorage(const IR::IDeclaration *decl) const {
393 CHECK_NULL(decl);
394 auto result = ::P4::get(storage, decl);
395 return result;
396 }
397 void dbprint(std::ostream &out) const override {
398 for (auto &it : storage) out << it.first << ": " << it.second << Log::endl;
399 }
400};
401
403class ProgramPoint : public IHasDbPrint {
411 absl::InlinedVector<const IR::Node *, 8> stack; // Has inline space for 8 nodes
412
413 public:
414 ProgramPoint() = default;
415 ProgramPoint(const ProgramPoint &other) : stack(other.stack) {}
416 explicit ProgramPoint(const IR::Node *node) {
417 CHECK_NULL(node);
418 assign(node);
419 }
420 ProgramPoint(const ProgramPoint &context, const IR::Node *node);
422 static ProgramPoint beforeStart;
424 ProgramPoint after() { return ProgramPoint(*this, nullptr); }
425 bool operator==(const ProgramPoint &other) const;
426 std::size_t hash() const;
427 void dbprint(std::ostream &out) const override {
428 if (isBeforeStart()) {
429 out << "<BeforeStart>";
430 } else {
431 bool first = true;
432 for (auto n : stack) {
433 if (!first) out << "//";
434 if (!n)
435 out << "After end";
436 else
437 out << dbp(n);
438 first = false;
439 }
440 auto l = stack.back();
441 if (l != nullptr &&
442 (l->is<IR::BaseAssignmentStatement>() || l->is<IR::MethodCallStatement>()))
443 out << "[[" << l << "]]";
444 }
445 }
446 void assign(const ProgramPoint &context, const IR::Node *node);
447 void assign(const IR::Node *node) { stack.assign({node}); }
448 void clear() { stack.clear(); }
449 const IR::Node *last() const { return stack.empty() ? nullptr : stack.back(); }
450 bool isBeforeStart() const { return stack.empty(); }
451 auto begin() const { return stack.begin(); }
452 auto end() const { return stack.end(); }
453 ProgramPoint &operator=(const ProgramPoint &) = default;
454 ProgramPoint &operator=(ProgramPoint &&) = default;
455};
456} // namespace P4
457
458// inject hash into std namespace so it is picked up by std::unordered_set
459namespace std {
460template <>
461struct hash<P4::ProgramPoint> {
462 std::size_t operator()(const P4::ProgramPoint &s) const { return s.hash(); }
463};
464
465template <>
466struct hash<P4::loc_t> {
467 std::size_t operator()(const P4::loc_t &loc) const { return loc.hash(); }
468};
469
470} // namespace std
471
472namespace P4::Util {
473template <>
475 size_t operator()(const P4::ProgramPoint &p) const { return p.hash(); }
476};
477
478template <>
479struct Hasher<P4::loc_t> {
480 size_t operator()(const P4::loc_t &loc) const { return loc.hash(); }
481};
482} // namespace P4::Util
483
484namespace P4 {
485class ProgramPoints : public IHasDbPrint {
486 typedef absl::flat_hash_set<ProgramPoint, Util::Hash> Points;
487 Points points;
488 explicit ProgramPoints(const Points &points) : points(points) {}
489
490 public:
491 ProgramPoints() = default;
492 explicit ProgramPoints(ProgramPoint point) { points.emplace(point); }
493 void add(const ProgramPoints *from);
494 const ProgramPoints *merge(const ProgramPoints *with) const;
495 bool operator==(const ProgramPoints &other) const;
496 void dbprint(std::ostream &out) const override {
497 out << "{";
498 for (auto p : points) out << p << " ";
499 out << "}";
500 }
501 size_t size() const { return points.size(); }
502 bool containsBeforeStart() const {
503 return points.find(ProgramPoint::beforeStart) != points.end();
504 }
505 Points::const_iterator begin() const { return points.cbegin(); }
506 Points::const_iterator end() const { return points.cend(); }
507};
508
510class Definitions : public IHasDbPrint {
515 bool unreachable = false;
516
517 public:
518 Definitions() = default;
519 Definitions(const Definitions &other)
520 : definitions(other.definitions), unreachable(other.unreachable) {}
521 Definitions *joinDefinitions(const Definitions *other) const;
523 Definitions *writes(ProgramPoint point, const LocationSet &locations) const;
524 void setDefintion(const BaseLocation *loc, const ProgramPoints *point) {
525 CHECK_NULL(loc);
526 CHECK_NULL(point);
527 definitions[loc] = point;
528 }
529 void setDefinition(const StorageLocation *loc, const ProgramPoints *point);
530 void setDefinition(const LocationSet &loc, const ProgramPoints *point);
531 Definitions *setUnreachable() {
532 unreachable = true;
533 return this;
534 }
535 bool isUnreachable() const { return unreachable; }
536 bool hasLocation(const BaseLocation *location) const {
537 return definitions.find(location) != definitions.end();
538 }
539 const ProgramPoints *getPoints(const BaseLocation *location) const {
540 auto r = ::P4::get(definitions, location);
541 BUG_CHECK(r != nullptr, "no definitions found for %1%", location);
542 return r;
543 }
544 const ProgramPoints *getPoints(const LocationSet &locations) const;
545 bool operator==(const Definitions &other) const;
546 void dbprint(std::ostream &out) const override {
547 if (unreachable) {
548 out << " Unreachable" << Log::endl;
549 }
550 if (definitions.empty()) out << " Empty definitions";
551 bool first = true;
552 for (auto d : definitions) {
553 if (!first) out << Log::endl;
554 out << " " << *d.first << "=>" << *d.second;
555 first = false;
556 }
557 }
558 Definitions *cloneDefinitions() const { return new Definitions(*this); }
559 void removeLocation(const StorageLocation *loc);
560 bool empty() const { return definitions.empty(); }
561 size_t size() const { return definitions.size(); }
562};
563
564class AllDefinitions : public IHasDbPrint {
570 StorageMap storageMap;
571
572 public:
573 AllDefinitions(ReferenceMap *refMap, TypeMap *typeMap) : storageMap(refMap, typeMap) {}
574
575 Definitions *getDefinitions(ProgramPoint point, bool emptyIfNotFound = false) {
576 auto it = atPoint.find(point);
577 if (it == atPoint.end()) {
578 if (emptyIfNotFound) {
579 auto defs = new Definitions();
580 setDefinitionsAt(point, defs, false);
581 return defs;
582 }
583 BUG("Unknown point %1% for definitions", &point);
584 }
585 return it->second;
586 }
587 void setDefinitionsAt(ProgramPoint point, Definitions *defs, bool overwrite) {
588 if (!overwrite) {
589 auto it = atPoint.find(point);
590 if (it != atPoint.end()) {
591 LOG2("Overwriting definitions at " << point << ": " << it->second << " with "
592 << defs);
593 BUG_CHECK(false, "Overwriting definitions at %1%", point);
594 }
595 }
596 atPoint[point] = defs;
597 }
598
599 const StorageLocation *getStorage(const IR::IDeclaration *decl) const {
600 return storageMap.getStorage(decl);
601 }
602
603 const StorageLocation *getOrAddStorage(const IR::IDeclaration *decl) {
604 return storageMap.getOrAdd(decl);
605 }
606
607 void dbprint(std::ostream &out) const override {
608 for (auto e : atPoint) out << e.first << " => " << e.second << Log::endl;
609 }
610};
611
622
623class ComputeWriteSet : public Inspector, public IHasDbPrint {
624 // We are using node_hash_set here as we need pointers to entries (loc_t) to be stable
625 using CachedLocs = absl::node_hash_set<loc_t, Util::Hash>;
626
627 public:
628 explicit ComputeWriteSet(AllDefinitions *allDefinitions, ReferenceMap *refMap, TypeMap *typeMap)
629 : refMap(refMap),
630 typeMap(typeMap),
631 allDefinitions(allDefinitions),
632 currentDefinitions(nullptr),
633 returnedDefinitions(nullptr),
635 lhs(false),
636 virtualMethod(false),
637 cached_locs(new CachedLocs) {
638 CHECK_NULL(allDefinitions);
639 CHECK_NULL(refMap);
640 CHECK_NULL(typeMap);
641 visitDagOnce = false;
642 }
643
644 // expressions
645 bool preorder(const IR::Literal *expression) override;
646 bool preorder(const IR::AbstractSlice *expression) override;
647 bool preorder(const IR::TypeNameExpression *expression) override;
648 bool preorder(const IR::PathExpression *expression) override;
649 bool preorder(const IR::Member *expression) override;
650 bool preorder(const IR::ArrayIndex *expression) override;
651 bool preorder(const IR::Operation_Binary *expression) override;
652 bool preorder(const IR::Mux *expression) override;
653 bool preorder(const IR::SelectExpression *expression) override;
654 bool preorder(const IR::ListExpression *expression) override;
655 bool preorder(const IR::Operation_Unary *expression) override;
656 bool preorder(const IR::MethodCallExpression *expression) override;
657 bool preorder(const IR::DefaultExpression *expression) override;
658 bool preorder(const IR::Expression *expression) override;
659 bool preorder(const IR::InvalidHeader *expression) override;
660 bool preorder(const IR::InvalidHeaderUnion *expression) override;
661 bool preorder(const IR::P4ListExpression *expression) override;
662 bool preorder(const IR::ArrayExpression *expression) override;
663 bool preorder(const IR::StructExpression *expression) override;
664 // statements
665 bool preorder(const IR::P4Parser *parser) override;
666 bool preorder(const IR::P4Control *control) override;
667 bool preorder(const IR::P4Action *action) override;
668 bool preorder(const IR::P4Table *table) override;
669 bool preorder(const IR::Function *function) override;
670 bool preorder(const IR::BaseAssignmentStatement *statement) override;
671 bool preorder(const IR::ReturnStatement *statement) override;
672 bool preorder(const IR::ExitStatement *statement) override;
673 bool preorder(const IR::BreakStatement *statement) override;
674 bool handleJump(const char *tok, Definitions *&defs);
675 bool preorder(const IR::ContinueStatement *statement) override;
676 bool preorder(const IR::IfStatement *statement) override;
677 bool preorder(const IR::ForStatement *statement) override;
678 bool preorder(const IR::ForInStatement *statement) override;
679 bool preorder(const IR::BlockStatement *statement) override;
680 bool preorder(const IR::SwitchStatement *statement) override;
681 bool preorder(const IR::EmptyStatement *statement) override;
682 bool preorder(const IR::MethodCallStatement *statement) override;
683
684 const LocationSet *writtenLocations(const IR::Expression *expression) {
685 expression->apply(*this);
686 return getWrites(expression);
687 }
688
689 protected:
690 ReferenceMap *refMap;
691 TypeMap *typeMap;
692 AllDefinitions *allDefinitions;
700 bool lhs;
703 bool virtualMethod;
705 alloc_trace_cb_t nested_trace;
706 static int nest_count;
707
710 ComputeWriteSet(const ComputeWriteSet *source, ProgramPoint context, Definitions *definitions,
711 std::shared_ptr<CachedLocs> cached_locs)
712 : refMap(source->refMap),
713 typeMap(source->typeMap),
714
715 allDefinitions(source->allDefinitions),
716 currentDefinitions(definitions),
717 returnedDefinitions(nullptr),
721 callingContext(context),
722 lhs(false),
723 virtualMethod(false),
724 cached_locs(cached_locs) {
725 visitDagOnce = false;
726 }
728 const loc_t *getLoc(const IR::Node *n, const loc_t *parentLoc);
729 const loc_t *getLoc(const Visitor::Context *ctxt);
730 const loc_t *getLoc(const IR::Node *n, const Visitor::Context *ctxt);
731 void enterScope(const IR::ParameterList *parameters,
732 const IR::IndexedVector<IR::Declaration> *locals, ProgramPoint startPoint,
733 bool clear = true);
734 void exitScope(const IR::ParameterList *parameters,
735 const IR::IndexedVector<IR::Declaration> *locals, ProgramPoint endPoint);
736 Definitions *getDefinitionsAfter(const IR::ParserState *state);
737 bool setDefinitions(Definitions *defs, const IR::Node *who = nullptr, bool overwrite = false);
738 ProgramPoint getProgramPoint(const IR::Node *node = nullptr) const;
739 // Get writes of a node that is a direct child of the currently being visited node.
740 const LocationSet *getWrites(const IR::Expression *expression) {
741 const loc_t &exprLoc = *getLoc(expression, getChildContext());
742 auto result = ::P4::get(writes, exprLoc);
743 BUG_CHECK(result != nullptr, "No location set known for %1%", expression);
744 return result;
745 }
746 // Get writes of a node that is not a direct child of the currently being visited node.
747 // In this case, parentLoc is the loc of expression's direct parent node.
748 const LocationSet *getWrites(const IR::Expression *expression, const loc_t *parentLoc) {
749 const loc_t &exprLoc = *getLoc(expression, parentLoc);
750 auto result = ::P4::get(writes, exprLoc);
751 BUG_CHECK(result != nullptr, "No location set known for %1%", expression);
752 return result;
753 }
754 // Register writes of expression, which is expected to be the currently visited node.
755 void expressionWrites(const IR::Expression *expression, const LocationSet *loc) {
756 CHECK_NULL(expression);
757 CHECK_NULL(loc);
758 LOG3(expression << dbp(expression) << " writes " << loc);
759 const Context *ctx = getChildContext();
760 BUG_CHECK(ctx->node == expression, "Expected ctx->node == expression.");
761 const loc_t &exprLoc = *getLoc(ctx);
762 if (auto it = writes.find(exprLoc); it != writes.end()) {
763 BUG_CHECK(*it->second == *loc || expression->is<IR::Literal>(),
764 "Expression %1% write set already set", expression);
765 } else {
766 writes.emplace(exprLoc, loc);
767 }
768 }
769 void dbprint(std::ostream &out) const override {
770 if (writes.empty()) out << "No writes";
771 for (auto &it : writes) out << it.first.node << " writes " << it.second << Log::endl;
772 }
773 profile_t init_apply(const IR::Node *root) override {
774 auto rv = Inspector::init_apply(root);
775 LOG1("starting ComputWriteSet" << Log::indent);
776 if (nest_count++ == 0 && LOGGING(2)) {
777 memuse.clear();
778 nested_trace = memuse.start();
779 }
780 return rv;
781 }
782 void end_apply() override {
783 LOG1("finished CWS" << Log::unindent);
784 if (--nest_count == 0 && LOGGING(2)) {
785 memuse.stop(nested_trace);
786 LOG2(memuse);
787 }
788 }
789
790 private:
791 std::shared_ptr<CachedLocs> cached_locs;
792};
793
794} // namespace P4
795
796#endif /* FRONTENDS_P4_DEF_USE_H_ */
Definition frontends/p4/def_use.h:564
Definition alloc_trace.h:20
Definition frontends/p4/def_use.h:92
Definition frontends/p4/def_use.h:623
bool lhs
if true we are processing an expression on the lhs of an assignment
Definition frontends/p4/def_use.h:700
AllocTrace memuse
True if we are analyzing a virtual method.
Definition frontends/p4/def_use.h:704
Definitions * breakDefinitions
Definitions after exit statements.
Definition frontends/p4/def_use.h:696
Definitions * returnedDefinitions
Before statement currently processed.
Definition frontends/p4/def_use.h:694
ComputeWriteSet(const ComputeWriteSet *source, ProgramPoint context, Definitions *definitions, std::shared_ptr< CachedLocs > cached_locs)
Definition frontends/p4/def_use.h:710
void visitVirtualMethods(const IR::IndexedVector< IR::Declaration > &locals)
Statements and other control structures.
Definition frontends/p4/def_use.cpp:760
Definitions * exitDefinitions
Definitions after return statements.
Definition frontends/p4/def_use.h:695
Definitions * currentDefinitions
Result computed by this pass.
Definition frontends/p4/def_use.h:693
Definitions * continueDefinitions
Definitions at break statements.
Definition frontends/p4/def_use.h:697
ProgramPoint callingContext
Definitions at continue statements.
Definition frontends/p4/def_use.h:698
hvec_map< loc_t, const LocationSet * > writes
For each program location the location set it writes.
Definition frontends/p4/def_use.h:702
List of definers for each base storage (at a specific program point).
Definition frontends/p4/def_use.h:510
Definitions * writes(ProgramPoint point, const LocationSet &locations) const
Point writes the specified LocationSet.
Definition frontends/p4/def_use.cpp:369
Definition castable.h:27
Definition stringify.h:24
The Declaration interface, representing objects with names.
Definition declaration.h:17
virtual ID getName() const =0
Definition indexed_vector.h:31
Definition node.h:44
Interface for locations that support an index operation.
Definition frontends/p4/def_use.h:173
Definition visitor.h:409
Definition frontends/p4/def_use.h:262
const LocationSet * canonicalize() const
Definition frontends/p4/def_use.cpp:232
Indicates a statement in the program.
Definition frontends/p4/def_use.h:403
static ProgramPoint beforeStart
A point logically before the function/control/action start.
Definition frontends/p4/def_use.h:422
ProgramPoint after()
We use a nullptr to indicate a point after the previous context.
Definition frontends/p4/def_use.h:424
Definition frontends/p4/def_use.h:485
Class used to encode maps from paths to declarations.
Definition referenceMap.h:58
Definition frontends/p4/def_use.h:245
Abstraction for something that is has a left value (variable, parameter)
Definition frontends/p4/def_use.h:48
LocationSet getValidBits() const
Definition frontends/p4/def_use.cpp:154
LocationSet removeHeaders() const
Definition frontends/p4/def_use.cpp:106
LocationSet getLastIndexField() const
Definition frontends/p4/def_use.cpp:160
Maps a declaration to its associated storage.
Definition frontends/p4/def_use.h:367
Definition typeMap.h:32
Definition iterator_range.h:44
Definition visitor.h:69
Base class for location sets that contain fields.
Definition frontends/p4/def_use.h:117
Definition cstring.h:76
Definition hvec_map.h:22
Definition ordered_set.h:23
TODO: this is not really specific to BMV2, it should reside somewhere else.
Definition applyOptionsPragmas.cpp:13
STL namespace.
Definition hash.h:123
Definition flat_map.h:29
Definition frontends/p4/def_use.h:32
Definition gc.h:18