2012-04-13 01:08:20 +00:00
|
|
|
// -*- mode: C++; c-file-style: "cc-mode" -*-
|
2006-08-26 11:35:28 +00:00
|
|
|
//*************************************************************************
|
|
|
|
// DESCRIPTION: Verilator: List class with storage in existing classes
|
|
|
|
//
|
2019-11-08 03:33:59 +00:00
|
|
|
// Code available from: https://verilator.org
|
2006-08-26 11:35:28 +00:00
|
|
|
//
|
|
|
|
//*************************************************************************
|
|
|
|
//
|
2023-01-01 15:18:39 +00:00
|
|
|
// Copyright 2003-2023 by Wilson Snyder. This program is free software; you
|
2020-03-21 15:24:24 +00:00
|
|
|
// can redistribute it and/or modify it under the terms of either the GNU
|
2009-05-04 21:07:57 +00:00
|
|
|
// Lesser General Public License Version 3 or the Perl Artistic License
|
|
|
|
// Version 2.0.
|
2020-03-21 15:24:24 +00:00
|
|
|
// SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0
|
2006-08-26 11:35:28 +00:00
|
|
|
//
|
|
|
|
//*************************************************************************
|
2019-10-05 00:17:11 +00:00
|
|
|
|
2021-03-04 02:57:07 +00:00
|
|
|
#ifndef VERILATOR_V3LIST_H_
|
|
|
|
#define VERILATOR_V3LIST_H_
|
2018-10-14 17:43:24 +00:00
|
|
|
|
2006-12-18 19:20:45 +00:00
|
|
|
#include "config_build.h"
|
|
|
|
#include "verilatedos.h"
|
2018-10-14 17:43:24 +00:00
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
|
2022-08-05 09:56:57 +00:00
|
|
|
template <class T>
|
|
|
|
class V3List;
|
|
|
|
template <class T>
|
|
|
|
class V3ListEnt;
|
2006-08-26 11:35:28 +00:00
|
|
|
|
2022-08-05 09:56:57 +00:00
|
|
|
template <class T>
|
|
|
|
class V3List final {
|
2006-08-26 11:35:28 +00:00
|
|
|
// List container for linked list of elements of type *T (T is a pointer type)
|
|
|
|
private:
|
2019-05-19 20:13:13 +00:00
|
|
|
// MEMBERS
|
2020-08-15 17:11:27 +00:00
|
|
|
T m_headp = nullptr; // First element
|
|
|
|
T m_tailp = nullptr; // Last element
|
2006-08-26 11:35:28 +00:00
|
|
|
friend class V3ListEnt<T>;
|
2020-04-14 02:51:35 +00:00
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
public:
|
2020-11-17 00:56:16 +00:00
|
|
|
V3List() = default;
|
|
|
|
~V3List() = default;
|
2006-08-26 11:35:28 +00:00
|
|
|
// METHODS
|
|
|
|
T begin() const { return m_headp; }
|
2020-08-15 14:12:55 +00:00
|
|
|
T end() const { return nullptr; }
|
Introduce DFG based combinational logic optimizer (#3527)
Added a new data-flow graph (DFG) based combinational logic optimizer.
The capabilities of this covers a combination of V3Const and V3Gate, but
is also more capable of transforming combinational logic into simplified
forms and more.
This entail adding a new internal representation, `DfgGraph`, and
appropriate `astToDfg` and `dfgToAst` conversion functions. The graph
represents some of the combinational equations (~continuous assignments)
in a module, and for the duration of the DFG passes, it takes over the
role of AstModule. A bulk of the Dfg vertices represent expressions.
These vertex classes, and the corresponding conversions to/from AST are
mostly auto-generated by astgen, together with a DfgVVisitor that can be
used for dynamic dispatch based on vertex (operation) types.
The resulting combinational logic graph (a `DfgGraph`) is then optimized
in various ways. Currently we perform common sub-expression elimination,
variable inlining, and some specific peephole optimizations, but there
is scope for more optimizations in the future using the same
representation. The optimizer is run directly before and after inlining.
The pre inline pass can operate on smaller graphs and hence converges
faster, but still has a chance of substantially reducing the size of the
logic on some designs, making inlining both faster and less memory
intensive. The post inline pass can then optimize across the inlined
module boundaries. No optimization is performed across a module
boundary.
For debugging purposes, each peephole optimization can be disabled
individually via the -fno-dfg-peepnole-<OPT> option, where <OPT> is one
of the optimizations listed in V3DfgPeephole.h, for example
-fno-dfg-peephole-remove-not-not.
The peephole patterns currently implemented were mostly picked based on
the design that inspired this work, and on that design the optimizations
yields ~30% single threaded speedup, and ~50% speedup on 4 threads. As
you can imagine not having to haul around redundant combinational
networks in the rest of the compilation pipeline also helps with memory
consumption, and up to 30% peak memory usage of Verilator was observed
on the same design.
Gains on other arbitrary designs are smaller (and can be improved by
analyzing those designs). For example OpenTitan gains between 1-15%
speedup depending on build type.
2022-09-23 15:46:22 +00:00
|
|
|
T rbegin() const { return m_tailp; }
|
|
|
|
T rend() const { return nullptr; }
|
2020-08-15 14:12:55 +00:00
|
|
|
bool empty() const { return m_headp == nullptr; }
|
2020-04-14 02:51:35 +00:00
|
|
|
void reset() { // clear() without walking the list
|
2020-08-15 14:12:55 +00:00
|
|
|
m_headp = nullptr;
|
|
|
|
m_tailp = nullptr;
|
2020-04-14 02:51:35 +00:00
|
|
|
}
|
2006-08-26 11:35:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
|
2022-08-05 09:56:57 +00:00
|
|
|
template <class T>
|
|
|
|
class V3ListEnt final {
|
2006-08-26 11:35:28 +00:00
|
|
|
// List entry for linked list of elements of type *T (T is a pointer type)
|
|
|
|
private:
|
2019-05-19 20:13:13 +00:00
|
|
|
// MEMBERS
|
2020-08-15 17:11:27 +00:00
|
|
|
T m_nextp = nullptr; // Pointer to next element, nullptr=end
|
|
|
|
T m_prevp = nullptr; // Pointer to previous element, nullptr=beginning
|
2006-08-26 11:35:28 +00:00
|
|
|
friend class V3List<T>;
|
2011-08-05 01:58:45 +00:00
|
|
|
static V3ListEnt* baseToListEnt(void* newbasep, size_t offset) {
|
2022-12-10 02:06:27 +00:00
|
|
|
// "this" must be an element inside of *basep
|
2019-05-19 20:13:13 +00:00
|
|
|
// Use that to determine a structure offset, then apply to the new base
|
|
|
|
// to get our new pointer information
|
2022-03-27 19:27:40 +00:00
|
|
|
return (V3ListEnt*)(((uint8_t*)newbasep) + offset);
|
2006-08-26 11:35:28 +00:00
|
|
|
}
|
2020-04-14 02:51:35 +00:00
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
public:
|
2020-11-17 00:56:16 +00:00
|
|
|
V3ListEnt() = default;
|
2006-08-26 11:35:28 +00:00
|
|
|
~V3ListEnt() {
|
|
|
|
#ifdef VL_DEBUG
|
2019-05-19 20:13:13 +00:00
|
|
|
// Load bogus pointers so we can catch deletion bugs
|
2020-04-04 02:31:54 +00:00
|
|
|
m_nextp = reinterpret_cast<T>(1);
|
|
|
|
m_prevp = reinterpret_cast<T>(1);
|
2006-08-26 11:35:28 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
T nextp() const { return m_nextp; }
|
Introduce DFG based combinational logic optimizer (#3527)
Added a new data-flow graph (DFG) based combinational logic optimizer.
The capabilities of this covers a combination of V3Const and V3Gate, but
is also more capable of transforming combinational logic into simplified
forms and more.
This entail adding a new internal representation, `DfgGraph`, and
appropriate `astToDfg` and `dfgToAst` conversion functions. The graph
represents some of the combinational equations (~continuous assignments)
in a module, and for the duration of the DFG passes, it takes over the
role of AstModule. A bulk of the Dfg vertices represent expressions.
These vertex classes, and the corresponding conversions to/from AST are
mostly auto-generated by astgen, together with a DfgVVisitor that can be
used for dynamic dispatch based on vertex (operation) types.
The resulting combinational logic graph (a `DfgGraph`) is then optimized
in various ways. Currently we perform common sub-expression elimination,
variable inlining, and some specific peephole optimizations, but there
is scope for more optimizations in the future using the same
representation. The optimizer is run directly before and after inlining.
The pre inline pass can operate on smaller graphs and hence converges
faster, but still has a chance of substantially reducing the size of the
logic on some designs, making inlining both faster and less memory
intensive. The post inline pass can then optimize across the inlined
module boundaries. No optimization is performed across a module
boundary.
For debugging purposes, each peephole optimization can be disabled
individually via the -fno-dfg-peepnole-<OPT> option, where <OPT> is one
of the optimizations listed in V3DfgPeephole.h, for example
-fno-dfg-peephole-remove-not-not.
The peephole patterns currently implemented were mostly picked based on
the design that inspired this work, and on that design the optimizations
yields ~30% single threaded speedup, and ~50% speedup on 4 threads. As
you can imagine not having to haul around redundant combinational
networks in the rest of the compilation pipeline also helps with memory
consumption, and up to 30% peak memory usage of Verilator was observed
on the same design.
Gains on other arbitrary designs are smaller (and can be improved by
analyzing those designs). For example OpenTitan gains between 1-15%
speedup depending on build type.
2022-09-23 15:46:22 +00:00
|
|
|
T prevp() const { return m_prevp; }
|
2006-08-26 11:35:28 +00:00
|
|
|
// METHODS
|
2018-08-25 13:52:45 +00:00
|
|
|
void pushBack(V3List<T>& listr, T newp) {
|
2022-12-10 02:06:27 +00:00
|
|
|
// "this" must be an element inside of *newp
|
2019-05-19 20:13:13 +00:00
|
|
|
// cppcheck-suppress thisSubtraction
|
2022-03-27 19:27:40 +00:00
|
|
|
const size_t offset = (size_t)(uint8_t*)(this) - (size_t)(uint8_t*)(newp);
|
2020-08-15 14:12:55 +00:00
|
|
|
m_nextp = nullptr;
|
2019-05-19 20:13:13 +00:00
|
|
|
if (!listr.m_headp) listr.m_headp = newp;
|
|
|
|
m_prevp = listr.m_tailp;
|
|
|
|
if (m_prevp) baseToListEnt(m_prevp, offset)->m_nextp = newp;
|
|
|
|
listr.m_tailp = newp;
|
2006-08-26 11:35:28 +00:00
|
|
|
}
|
2018-08-25 13:52:45 +00:00
|
|
|
void pushFront(V3List<T>& listr, T newp) {
|
2022-12-10 02:06:27 +00:00
|
|
|
// "this" must be an element inside of *newp
|
2019-05-19 20:13:13 +00:00
|
|
|
// cppcheck-suppress thisSubtraction
|
2022-03-27 19:27:40 +00:00
|
|
|
const size_t offset = (size_t)(uint8_t*)(this) - (size_t)(uint8_t*)(newp);
|
2019-05-19 20:13:13 +00:00
|
|
|
m_nextp = listr.m_headp;
|
|
|
|
if (m_nextp) baseToListEnt(m_nextp, offset)->m_prevp = newp;
|
|
|
|
listr.m_headp = newp;
|
2020-08-15 14:12:55 +00:00
|
|
|
m_prevp = nullptr;
|
2019-05-19 20:13:13 +00:00
|
|
|
if (!listr.m_tailp) listr.m_tailp = newp;
|
2006-08-26 11:35:28 +00:00
|
|
|
}
|
|
|
|
// Unlink from side
|
2018-08-25 13:52:45 +00:00
|
|
|
void unlink(V3List<T>& listr, T oldp) {
|
2022-12-10 02:06:27 +00:00
|
|
|
// "this" must be an element inside of *oldp
|
2019-05-19 20:13:13 +00:00
|
|
|
// cppcheck-suppress thisSubtraction
|
2022-03-27 19:27:40 +00:00
|
|
|
const size_t offset = (size_t)(uint8_t*)(this) - (size_t)(uint8_t*)(oldp);
|
2020-04-14 02:51:35 +00:00
|
|
|
if (m_nextp) {
|
|
|
|
baseToListEnt(m_nextp, offset)->m_prevp = m_prevp;
|
|
|
|
} else {
|
|
|
|
listr.m_tailp = m_prevp;
|
|
|
|
}
|
|
|
|
if (m_prevp) {
|
|
|
|
baseToListEnt(m_prevp, offset)->m_nextp = m_nextp;
|
|
|
|
} else {
|
|
|
|
listr.m_headp = m_nextp;
|
|
|
|
}
|
2020-08-15 14:12:55 +00:00
|
|
|
m_prevp = m_nextp = nullptr;
|
2006-08-26 11:35:28 +00:00
|
|
|
}
|
2022-10-08 11:05:30 +00:00
|
|
|
// Remove all nodes from 'oldListr', append them to 'newListr'. 'this' must be a member of the
|
|
|
|
// object at 'selfp', and 'selfp' must be the head of the list in 'oldListr'.
|
|
|
|
void moveAppend(V3List<T>& oldListr, V3List<T>& newListr, T selfp) {
|
|
|
|
UASSERT(selfp == oldListr.m_headp, "Must be head of list to use 'moveAppend'");
|
|
|
|
const size_t offset = (size_t)(uint8_t*)(this) - (size_t)(uint8_t*)(selfp);
|
|
|
|
const T headp = selfp;
|
|
|
|
const T tailp = oldListr.m_tailp;
|
|
|
|
oldListr.reset();
|
|
|
|
if (newListr.empty()) {
|
|
|
|
newListr.m_headp = headp;
|
|
|
|
newListr.m_tailp = tailp;
|
|
|
|
} else {
|
|
|
|
baseToListEnt(newListr.m_tailp, offset)->m_nextp = headp;
|
|
|
|
m_prevp = newListr.m_tailp;
|
|
|
|
newListr.m_tailp = tailp;
|
|
|
|
}
|
|
|
|
}
|
2006-08-26 11:35:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//============================================================================
|
|
|
|
|
2019-05-19 20:13:13 +00:00
|
|
|
#endif // Guard
|