verilator/src/V3DupFinder.h
Geza Lore 8814111724
Rework Ast hashing to be stable (#2974)
Rework Ast hashing to be stable

Eliminated reliance on pointer values in AstNode hashes in order to make
them stable. This requires moving the sameHash functions into a visitor,
as nodes pointed to via members (and not child nodes) need to be hashed
themselves.

The hashes are now stable (as far as I could test them), and the impact
on verilation time is small enough not to be reliably measurable.
2021-05-21 14:34:27 +01:00

79 lines
2.4 KiB
C++

// -*- mode: C++; c-file-style: "cc-mode" -*-
//*************************************************************************
// DESCRIPTION: Verilator: Hash AST trees to find duplicates
//
// Code available from: https://verilator.org
//
//*************************************************************************
//
// Copyright 2005-2021 by Wilson Snyder. This program is free software; you
// can redistribute it and/or modify it under the terms of either the GNU
// Lesser General Public License Version 3 or the Perl Artistic License
// Version 2.0.
// SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0
//
//*************************************************************************
//
// Datastructure for finding duplicate AstNode trees via hashing
//
//*************************************************************************
#ifndef VERILATOR_V3DUPFINDER_H_
#define VERILATOR_V3DUPFINDER_H_
#include "config_build.h"
#include "verilatedos.h"
#include "V3Error.h"
#include "V3Ast.h"
#include "V3Hasher.h"
#include <map>
//============================================================================
struct V3DupFinderUserSame {
// Functor for V3DupFinder::findDuplicate
virtual bool isSame(AstNode*, AstNode*) = 0;
V3DupFinderUserSame() = default;
virtual ~V3DupFinderUserSame() = default;
};
// This really is just a multimap from 'node hash' to 'node pointer', with some minor extensions.
class V3DupFinder final : private std::multimap<V3Hash, AstNode*> {
using Super = std::multimap<V3Hash, AstNode*>;
// MEMBERS
const V3Hasher m_hasher;
public:
// CONSTRUCTORS
V3DupFinder(){};
~V3DupFinder() = default;
// METHODS
VL_DEBUG_FUNC; // Declare debug()
// Expose minimal set of superclass interface
using Super::begin;
using Super::cbegin;
using Super::cend;
using Super::clear;
using Super::const_iterator;
using Super::empty;
using Super::end;
using Super::erase;
using Super::iterator;
// Insert node into data structure
iterator insert(AstNode* nodep) { return emplace(m_hasher(nodep), nodep); }
// Return duplicate, if one was inserted, with optional user check for sameness
iterator findDuplicate(AstNode* nodep, V3DupFinderUserSame* checkp = nullptr);
// Dump for debug
void dumpFile(const string& filename, bool tree);
void dumpFilePrefixed(const string& nameComment, bool tree = false);
};
#endif // Guard