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: Convert BLOCKTEMPs to local variables
|
|
|
|
//
|
2019-11-08 03:33:59 +00:00
|
|
|
// Code available from: https://verilator.org
|
2006-08-26 11:35:28 +00:00
|
|
|
//
|
|
|
|
//*************************************************************************
|
|
|
|
//
|
2021-01-01 15:29:54 +00:00
|
|
|
// Copyright 2003-2021 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
|
|
|
//
|
|
|
|
//*************************************************************************
|
|
|
|
// LOCALIZE TRANSFORMATIONS:
|
2019-05-19 20:13:13 +00:00
|
|
|
// All modules:
|
|
|
|
// VAR(BLOCKTEMP...
|
|
|
|
// if only referenced in a CFUNC, make it local to that CFUNC
|
|
|
|
// VAR(others
|
|
|
|
// if non-public, set before used, and in single CFUNC, make it local
|
2008-06-10 01:25:10 +00:00
|
|
|
//
|
2006-08-26 11:35:28 +00:00
|
|
|
//*************************************************************************
|
2019-10-05 00:17:11 +00:00
|
|
|
|
2006-12-18 19:20:45 +00:00
|
|
|
#include "config_build.h"
|
|
|
|
#include "verilatedos.h"
|
2006-08-26 11:35:28 +00:00
|
|
|
|
|
|
|
#include "V3Global.h"
|
|
|
|
#include "V3Localize.h"
|
|
|
|
#include "V3Stats.h"
|
|
|
|
#include "V3Ast.h"
|
|
|
|
|
2018-10-14 17:43:24 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
//######################################################################
|
|
|
|
// Localize base class
|
|
|
|
|
2020-11-19 02:32:16 +00:00
|
|
|
class LocalizeBaseVisitor VL_NOT_FINAL : public AstNVisitor {
|
2006-08-26 11:35:28 +00:00
|
|
|
protected:
|
|
|
|
// NODE STATE
|
|
|
|
// Cleared on entire tree
|
2019-05-19 20:13:13 +00:00
|
|
|
// AstVar::user1p() -> CFunc which references the variable
|
|
|
|
// AstVar::user2() -> VarFlags. Flag state
|
|
|
|
// AstVar::user4() -> AstVarRef*. First place signal set; must be first assignment
|
2006-08-26 11:35:28 +00:00
|
|
|
|
2009-01-21 21:56:50 +00:00
|
|
|
// METHODS
|
2018-05-14 10:50:47 +00:00
|
|
|
VL_DEBUG_FUNC; // Declare debug()
|
2006-08-26 11:35:28 +00:00
|
|
|
|
|
|
|
// TYPES
|
|
|
|
union VarFlags {
|
2019-05-19 20:13:13 +00:00
|
|
|
// Per-variable flags
|
|
|
|
// Used in user()'s so initializes to all zeros
|
|
|
|
struct {
|
2020-04-14 02:51:35 +00:00
|
|
|
int m_notOpt : 1; // NOT optimizable
|
|
|
|
int m_notStd : 1; // NOT optimizable if a non-blocktemp signal
|
|
|
|
int m_stdFuncAsn : 1; // Found simple assignment
|
|
|
|
int m_done : 1; // Removed
|
2019-05-19 20:13:13 +00:00
|
|
|
};
|
2018-02-08 00:31:21 +00:00
|
|
|
// cppcheck-suppress unusedStructMember
|
2019-05-19 20:13:13 +00:00
|
|
|
uint32_t m_flags;
|
2018-10-14 22:39:33 +00:00
|
|
|
explicit VarFlags(AstNode* nodep) { m_flags = nodep->user2(); }
|
2019-05-19 20:13:13 +00:00
|
|
|
void setNodeFlags(AstNode* nodep) { nodep->user2(m_flags); }
|
2006-08-26 11:35:28 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
// Localize class functions
|
|
|
|
|
2020-11-19 02:32:16 +00:00
|
|
|
class LocalizeDehierVisitor final : public LocalizeBaseVisitor {
|
2006-08-26 11:35:28 +00:00
|
|
|
private:
|
|
|
|
// NODE STATE/TYPES
|
|
|
|
// See above
|
|
|
|
|
|
|
|
// METHODS
|
2020-08-15 14:03:34 +00:00
|
|
|
virtual void visit(AstVarRef* nodep) override {
|
2020-04-05 22:30:46 +00:00
|
|
|
// cppcheck-suppress unreadVariable // cppcheck 1.90 bug
|
2020-04-14 02:51:35 +00:00
|
|
|
VarFlags flags(nodep->varp());
|
2019-05-19 20:13:13 +00:00
|
|
|
if (flags.m_done) {
|
2020-11-25 03:46:02 +00:00
|
|
|
nodep->hiernameToProt(""); // Remove this->
|
|
|
|
nodep->hiernameToUnprot(""); // Remove this->
|
2019-05-19 20:13:13 +00:00
|
|
|
nodep->hierThis(true);
|
|
|
|
}
|
2006-08-26 11:35:28 +00:00
|
|
|
}
|
2020-08-15 14:03:34 +00:00
|
|
|
virtual void visit(AstNode* nodep) override { iterateChildren(nodep); }
|
2020-04-04 12:31:14 +00:00
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
public:
|
|
|
|
// CONSTRUCTORS
|
2020-04-14 02:51:35 +00:00
|
|
|
explicit LocalizeDehierVisitor(AstNetlist* nodep) { iterate(nodep); }
|
2020-11-17 00:56:16 +00:00
|
|
|
virtual ~LocalizeDehierVisitor() override = default;
|
2006-08-26 11:35:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
// Localize class functions
|
|
|
|
|
2020-11-19 02:32:16 +00:00
|
|
|
class LocalizeVisitor final : public LocalizeBaseVisitor {
|
2006-08-26 11:35:28 +00:00
|
|
|
private:
|
|
|
|
// NODE STATE/TYPES
|
|
|
|
// See above
|
2020-04-14 02:51:35 +00:00
|
|
|
AstUser1InUse m_inuser1;
|
|
|
|
AstUser2InUse m_inuser2;
|
|
|
|
AstUser4InUse m_inuser4;
|
2006-08-26 11:35:28 +00:00
|
|
|
|
|
|
|
// STATE
|
2019-10-05 11:54:14 +00:00
|
|
|
VDouble0 m_statLocVars; // Statistic tracking
|
2020-08-16 13:55:36 +00:00
|
|
|
AstCFunc* m_cfuncp = nullptr; // Current active function
|
2019-05-19 20:13:13 +00:00
|
|
|
std::vector<AstVar*> m_varps; // List of variables to consider for deletion
|
2006-08-26 11:35:28 +00:00
|
|
|
|
|
|
|
// METHODS
|
|
|
|
void clearOptimizable(AstVar* nodep, const char* reason) {
|
2020-04-14 02:51:35 +00:00
|
|
|
UINFO(4, " NoOpt " << reason << " " << nodep << endl);
|
|
|
|
VarFlags flags(nodep);
|
2019-05-19 20:13:13 +00:00
|
|
|
flags.m_notOpt = true;
|
|
|
|
flags.setNodeFlags(nodep);
|
2006-08-26 11:35:28 +00:00
|
|
|
}
|
|
|
|
void clearStdOptimizable(AstVar* nodep, const char* reason) {
|
2020-04-14 02:51:35 +00:00
|
|
|
UINFO(4, " NoStd " << reason << " " << nodep << endl);
|
|
|
|
VarFlags flags(nodep);
|
2019-05-19 20:13:13 +00:00
|
|
|
flags.m_notStd = true;
|
|
|
|
flags.setNodeFlags(nodep);
|
2006-08-26 11:35:28 +00:00
|
|
|
}
|
|
|
|
void moveVars() {
|
2020-08-16 15:43:49 +00:00
|
|
|
for (AstVar* nodep : m_varps) {
|
2019-05-19 20:13:13 +00:00
|
|
|
if (nodep->valuep()) clearOptimizable(nodep, "HasInitValue");
|
|
|
|
if (!VarFlags(nodep).m_stdFuncAsn) clearStdOptimizable(nodep, "NoStdAssign");
|
2020-04-14 02:51:35 +00:00
|
|
|
VarFlags flags(nodep);
|
2020-04-12 22:57:12 +00:00
|
|
|
|
2019-05-19 20:13:13 +00:00
|
|
|
if ((nodep->isMovableToBlock() // Blocktemp
|
|
|
|
|| !flags.m_notStd) // Or used only in block
|
|
|
|
&& !flags.m_notOpt // Optimizable
|
2020-04-14 02:51:35 +00:00
|
|
|
&& !nodep->isClassMember() && nodep->user1p()) { // Single cfunc
|
2019-05-19 20:13:13 +00:00
|
|
|
// We don't need to test for tracing; it would be in the tracefunc if it was needed
|
2020-04-14 02:51:35 +00:00
|
|
|
UINFO(4, " ModVar->BlkVar " << nodep << endl);
|
2019-05-19 20:13:13 +00:00
|
|
|
++m_statLocVars;
|
2018-02-02 02:32:58 +00:00
|
|
|
AstCFunc* newfuncp = VN_CAST(nodep->user1p(), CFunc);
|
2019-05-19 20:13:13 +00:00
|
|
|
nodep->unlinkFrBack();
|
|
|
|
newfuncp->addInitsp(nodep);
|
|
|
|
// Done
|
|
|
|
flags.m_done = true;
|
|
|
|
flags.setNodeFlags(nodep);
|
|
|
|
} else {
|
|
|
|
clearOptimizable(nodep, "NotDone");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
m_varps.clear();
|
2006-08-26 11:35:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// VISITORS
|
2020-08-15 14:03:34 +00:00
|
|
|
virtual void visit(AstNetlist* nodep) override {
|
2018-05-11 00:55:37 +00:00
|
|
|
iterateChildren(nodep);
|
2019-05-19 20:13:13 +00:00
|
|
|
moveVars();
|
2006-08-26 11:35:28 +00:00
|
|
|
}
|
2020-08-15 14:03:34 +00:00
|
|
|
virtual void visit(AstCFunc* nodep) override {
|
2020-04-14 02:51:35 +00:00
|
|
|
UINFO(4, " CFUNC " << nodep << endl);
|
2020-10-31 12:59:35 +00:00
|
|
|
VL_RESTORER(m_cfuncp);
|
|
|
|
{
|
|
|
|
m_cfuncp = nodep;
|
|
|
|
searchFuncStmts(nodep->argsp());
|
|
|
|
searchFuncStmts(nodep->initsp());
|
|
|
|
searchFuncStmts(nodep->stmtsp());
|
|
|
|
searchFuncStmts(nodep->finalsp());
|
|
|
|
iterateChildren(nodep);
|
|
|
|
}
|
2006-08-26 11:35:28 +00:00
|
|
|
}
|
|
|
|
void searchFuncStmts(AstNode* nodep) {
|
2019-05-19 20:13:13 +00:00
|
|
|
// Search for basic assignments to allow moving non-blocktemps
|
|
|
|
// For now we only find simple assignments not under any other statement.
|
|
|
|
// This could be more complicated; allow always-set under both branches of a IF.
|
|
|
|
// If so, check for ArrayRef's and such, as they aren't acceptable.
|
2020-04-14 02:51:35 +00:00
|
|
|
for (; nodep; nodep = nodep->nextp()) {
|
2018-02-02 02:32:58 +00:00
|
|
|
if (VN_IS(nodep, NodeAssign)) {
|
|
|
|
if (AstVarRef* varrefp = VN_CAST(VN_CAST(nodep, NodeAssign)->lhsp(), VarRef)) {
|
2020-11-07 15:37:55 +00:00
|
|
|
UASSERT_OBJ(varrefp->access().isWriteOrRW(), varrefp,
|
|
|
|
"LHS assignment not lvalue");
|
2019-05-19 20:13:13 +00:00
|
|
|
if (!varrefp->varp()->user4p()) {
|
2020-04-14 02:51:35 +00:00
|
|
|
UINFO(4, " FuncAsn " << varrefp << endl);
|
2019-05-19 20:13:13 +00:00
|
|
|
varrefp->varp()->user4p(varrefp);
|
2020-04-14 02:51:35 +00:00
|
|
|
VarFlags flags(varrefp->varp());
|
2019-05-19 20:13:13 +00:00
|
|
|
flags.m_stdFuncAsn = true;
|
|
|
|
flags.setNodeFlags(varrefp->varp());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2006-08-26 11:35:28 +00:00
|
|
|
}
|
|
|
|
|
2020-08-15 14:03:34 +00:00
|
|
|
virtual void visit(AstVar* nodep) override {
|
2020-04-14 02:51:35 +00:00
|
|
|
if (!nodep->isSigPublic() && !nodep->isPrimaryIO()
|
2019-05-19 20:13:13 +00:00
|
|
|
&& !m_cfuncp) { // Not already inside a function
|
2020-04-14 02:51:35 +00:00
|
|
|
UINFO(4, " BLKVAR " << nodep << endl);
|
2019-05-19 20:13:13 +00:00
|
|
|
m_varps.push_back(nodep);
|
|
|
|
}
|
|
|
|
// No iterate; Don't want varrefs under it
|
2006-08-26 11:35:28 +00:00
|
|
|
}
|
2020-08-15 14:03:34 +00:00
|
|
|
virtual void visit(AstVarRef* nodep) override {
|
2019-05-19 20:13:13 +00:00
|
|
|
if (!VarFlags(nodep->varp()).m_notOpt) {
|
|
|
|
if (!m_cfuncp) { // Not in function, can't optimize
|
2020-05-29 23:35:54 +00:00
|
|
|
// Perhaps impossible, but better safe
|
|
|
|
clearOptimizable(nodep->varp(), "BVnofunc"); // LCOV_EXCL_LINE
|
2020-04-14 02:51:35 +00:00
|
|
|
} else {
|
2019-05-19 20:13:13 +00:00
|
|
|
// If we're scoping down to it, it isn't really in the same block
|
|
|
|
if (!nodep->hierThis()) clearOptimizable(nodep->varp(), "HierRef");
|
|
|
|
// Allow a variable to appear in only a single function
|
|
|
|
AstNode* oldfunc = nodep->varp()->user1p();
|
|
|
|
if (!oldfunc) {
|
2020-04-14 02:51:35 +00:00
|
|
|
UINFO(4, " BVnewref " << nodep << endl);
|
2019-05-19 20:13:13 +00:00
|
|
|
nodep->varp()->user1p(m_cfuncp); // Remember where it was used
|
|
|
|
} else if (m_cfuncp == oldfunc) {
|
|
|
|
// Same usage
|
|
|
|
} else {
|
|
|
|
// Used in multiple functions
|
|
|
|
clearOptimizable(nodep->varp(), "BVmultiF");
|
|
|
|
}
|
|
|
|
// First varref in function must be assignment found earlier
|
2018-10-14 22:39:33 +00:00
|
|
|
AstVarRef* firstasn = static_cast<AstVarRef*>(nodep->varp()->user4p());
|
2020-04-14 02:51:35 +00:00
|
|
|
if (firstasn && nodep != firstasn) {
|
2019-05-19 20:13:13 +00:00
|
|
|
clearStdOptimizable(nodep->varp(), "notFirstAsn");
|
2020-08-15 14:12:55 +00:00
|
|
|
nodep->varp()->user4p(nullptr);
|
2019-05-19 20:13:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// No iterate; Don't want varrefs under it
|
2006-08-26 11:35:28 +00:00
|
|
|
}
|
2020-08-15 14:03:34 +00:00
|
|
|
virtual void visit(AstNode* nodep) override { iterateChildren(nodep); }
|
2020-04-04 12:31:14 +00:00
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
public:
|
|
|
|
// CONSTRUCTORS
|
2020-08-16 13:55:36 +00:00
|
|
|
explicit LocalizeVisitor(AstNetlist* nodep) { iterate(nodep); }
|
2020-08-15 15:44:10 +00:00
|
|
|
virtual ~LocalizeVisitor() override {
|
2019-05-19 20:13:13 +00:00
|
|
|
V3Stats::addStat("Optimizations, Vars localized", m_statLocVars);
|
2006-08-26 11:35:28 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
// Localize class functions
|
|
|
|
|
|
|
|
void V3Localize::localizeAll(AstNetlist* nodep) {
|
2020-04-14 02:51:35 +00:00
|
|
|
UINFO(2, __FUNCTION__ << ": " << endl);
|
2018-03-10 17:57:50 +00:00
|
|
|
{
|
2020-04-14 02:51:35 +00:00
|
|
|
LocalizeVisitor visitor(nodep);
|
2018-03-10 17:57:50 +00:00
|
|
|
// Fix up hiernames
|
2020-04-14 02:51:35 +00:00
|
|
|
LocalizeDehierVisitor dvisitor(nodep);
|
2018-03-10 17:57:50 +00:00
|
|
|
} // Destruct before checking
|
2017-09-18 02:52:57 +00:00
|
|
|
V3Global::dumpCheckGlobalTree("localize", 0, v3Global.opt.dumpTreeLevel(__FILE__) >= 6);
|
2006-08-26 11:35:28 +00:00
|
|
|
}
|