diff --git a/frontends/p4/def_use.cpp b/frontends/p4/def_use.cpp index 70b2b203a11..2abff7cebe3 100644 --- a/frontends/p4/def_use.cpp +++ b/frontends/p4/def_use.cpp @@ -764,9 +764,13 @@ void ComputeWriteSet::visitVirtualMethods(const IR::IndexedVectorid); - - return Util::Hash{}(node->id, parent->hash()); + if (!computedHash) { + if (!parent) + computedHash = Util::Hash{}(node->id); + else + computedHash = Util::Hash{}(node->id, parent->hash()); + } + return computedHash; } // Returns program location of n, given the program location of n's direct parent. diff --git a/frontends/p4/def_use.h b/frontends/p4/def_use.h index fcbf5de1107..aa46ddfb1d4 100644 --- a/frontends/p4/def_use.h +++ b/frontends/p4/def_use.h @@ -39,6 +39,7 @@ class LocationSet; struct loc_t { const IR::Node *node; const loc_t *parent; + mutable std::size_t computedHash = 0; bool operator==(const loc_t &a) const { if (node != a.node) return false; if (parent == a.parent) return true;