2012-04-13 03:08:20 +02:00
|
|
|
// -*- mode: C++; c-file-style: "cc-mode" -*-
|
2007-01-18 01:51:26 +01:00
|
|
|
//*************************************************************************
|
|
|
|
|
// DESCRIPTION: Verilator: Break always into separate statements to reduce temps
|
|
|
|
|
//
|
2019-11-08 04:33:59 +01:00
|
|
|
// Code available from: https://verilator.org
|
2007-01-18 01:51:26 +01:00
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
//
|
2020-03-21 16:24:24 +01:00
|
|
|
// Copyright 2003-2020 by Wilson Snyder. This program is free software; you
|
|
|
|
|
// can redistribute it and/or modify it under the terms of either the GNU
|
2009-05-04 23:07:57 +02:00
|
|
|
// Lesser General Public License Version 3 or the Perl Artistic License
|
|
|
|
|
// Version 2.0.
|
2020-03-21 16:24:24 +01:00
|
|
|
// SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0
|
2007-01-18 01:51:26 +01:00
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
// V3SplitAs's Transformations:
|
|
|
|
|
//
|
2019-05-19 22:13:13 +02:00
|
|
|
// Search each ALWAYS for a VARREF lvalue with a /*isolate_assignments*/ attribute
|
|
|
|
|
// If found, color statements with both, assignment to that varref, or other assignments.
|
|
|
|
|
// Replicate the Always, and remove mis-colored duplicate code.
|
2007-01-18 01:51:26 +01:00
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
2019-10-05 02:17:11 +02:00
|
|
|
|
2007-01-18 01:51:26 +01:00
|
|
|
#include "config_build.h"
|
|
|
|
|
#include "verilatedos.h"
|
|
|
|
|
|
|
|
|
|
#include "V3Global.h"
|
|
|
|
|
#include "V3SplitAs.h"
|
|
|
|
|
#include "V3Stats.h"
|
|
|
|
|
#include "V3Ast.h"
|
|
|
|
|
|
2018-10-14 19:43:24 +02:00
|
|
|
#include <map>
|
|
|
|
|
|
2007-01-18 01:51:26 +01:00
|
|
|
//######################################################################
|
|
|
|
|
|
|
|
|
|
class SplitAsBaseVisitor : public AstNVisitor {
|
|
|
|
|
public:
|
|
|
|
|
// METHODS
|
2018-05-14 12:50:47 +02:00
|
|
|
VL_DEBUG_FUNC; // Declare debug()
|
2007-01-18 01:51:26 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
// Find all split variables in a block
|
|
|
|
|
|
|
|
|
|
class SplitAsFindVisitor : public SplitAsBaseVisitor {
|
|
|
|
|
private:
|
|
|
|
|
// STATE
|
2020-08-16 15:55:36 +02:00
|
|
|
AstVarScope* m_splitVscp = nullptr; // Variable we want to split
|
2007-01-18 01:51:26 +01:00
|
|
|
|
|
|
|
|
// METHODS
|
2020-08-15 16:03:34 +02:00
|
|
|
virtual void visit(AstVarRef* nodep) override {
|
2020-04-15 13:58:34 +02:00
|
|
|
if (nodep->lvalue() && !m_splitVscp && nodep->varp()->attrIsolateAssign()) {
|
2019-05-19 22:13:13 +02:00
|
|
|
m_splitVscp = nodep->varScopep();
|
|
|
|
|
}
|
2007-01-18 01:51:26 +01:00
|
|
|
}
|
2020-08-15 16:03:34 +02:00
|
|
|
virtual void visit(AstNode* nodep) override { iterateChildren(nodep); }
|
2020-04-04 14:31:14 +02:00
|
|
|
|
2007-01-18 01:51:26 +01:00
|
|
|
public:
|
2019-09-12 13:22:22 +02:00
|
|
|
// CONSTRUCTORS
|
2020-08-16 15:55:36 +02:00
|
|
|
explicit SplitAsFindVisitor(AstAlways* nodep) { iterate(nodep); }
|
2020-08-15 17:44:10 +02:00
|
|
|
virtual ~SplitAsFindVisitor() override {}
|
2007-01-18 01:51:26 +01:00
|
|
|
// METHODS
|
|
|
|
|
AstVarScope* splitVscp() const { return m_splitVscp; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
// Remove nodes not containing proper references
|
|
|
|
|
|
|
|
|
|
class SplitAsCleanVisitor : public SplitAsBaseVisitor {
|
|
|
|
|
private:
|
|
|
|
|
// STATE
|
2020-04-15 13:58:34 +02:00
|
|
|
AstVarScope* m_splitVscp; // Variable we want to split
|
|
|
|
|
bool m_modeMatch; // Remove matching Vscp, else non-matching
|
2020-08-15 19:11:27 +02:00
|
|
|
bool m_keepStmt = false; // Current Statement must be preserved
|
|
|
|
|
bool m_matches = false; // Statement below has matching lvalue reference
|
2007-01-18 01:51:26 +01:00
|
|
|
|
|
|
|
|
// METHODS
|
2020-08-15 16:03:34 +02:00
|
|
|
virtual void visit(AstVarRef* nodep) override {
|
2019-05-19 22:13:13 +02:00
|
|
|
if (nodep->lvalue()) {
|
2020-04-15 13:58:34 +02:00
|
|
|
if (nodep->varScopep() == m_splitVscp) {
|
|
|
|
|
UINFO(6, " CL VAR " << nodep << endl);
|
2019-05-19 22:13:13 +02:00
|
|
|
m_matches = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-01-18 01:51:26 +01:00
|
|
|
}
|
2020-08-15 16:03:34 +02:00
|
|
|
virtual void visit(AstNodeStmt* nodep) override {
|
2019-01-06 23:38:27 +01:00
|
|
|
if (!nodep->isStatement()) {
|
|
|
|
|
iterateChildren(nodep);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-04-15 13:58:34 +02:00
|
|
|
UINFO(6, " CL STMT " << nodep << endl);
|
2019-05-19 22:13:13 +02:00
|
|
|
bool oldKeep = m_keepStmt;
|
|
|
|
|
{
|
|
|
|
|
m_matches = false;
|
|
|
|
|
m_keepStmt = false;
|
2007-01-18 01:51:26 +01:00
|
|
|
|
2018-05-11 02:55:37 +02:00
|
|
|
iterateChildren(nodep);
|
2007-01-18 01:51:26 +01:00
|
|
|
|
2020-04-15 13:58:34 +02:00
|
|
|
if (m_keepStmt || (m_modeMatch ? m_matches : !m_matches)) {
|
|
|
|
|
UINFO(6, " Keep STMT " << nodep << endl);
|
2019-05-19 22:13:13 +02:00
|
|
|
m_keepStmt = true;
|
|
|
|
|
} else {
|
2020-04-15 13:58:34 +02:00
|
|
|
UINFO(6, " Delete STMT " << nodep << endl);
|
|
|
|
|
nodep->unlinkFrBack();
|
|
|
|
|
pushDeletep(nodep);
|
2019-05-19 22:13:13 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// If something below matches, the upper statement remains too.
|
|
|
|
|
m_keepStmt = oldKeep || m_keepStmt;
|
2020-04-15 13:58:34 +02:00
|
|
|
UINFO(9, " upKeep=" << m_keepStmt << " STMT " << nodep << endl);
|
2007-01-18 01:51:26 +01:00
|
|
|
}
|
2020-08-15 16:03:34 +02:00
|
|
|
virtual void visit(AstNode* nodep) override { iterateChildren(nodep); }
|
2020-04-04 14:31:14 +02:00
|
|
|
|
2007-01-18 01:51:26 +01:00
|
|
|
public:
|
2019-09-12 13:22:22 +02:00
|
|
|
// CONSTRUCTORS
|
2020-08-15 19:11:27 +02:00
|
|
|
SplitAsCleanVisitor(AstAlways* nodep, AstVarScope* vscp, bool modeMatch)
|
2020-08-16 15:55:36 +02:00
|
|
|
: m_splitVscp{vscp}
|
|
|
|
|
, m_modeMatch{modeMatch} {
|
2018-05-11 02:55:37 +02:00
|
|
|
iterate(nodep);
|
2007-01-18 01:51:26 +01:00
|
|
|
}
|
2020-08-15 17:44:10 +02:00
|
|
|
virtual ~SplitAsCleanVisitor() override {}
|
2007-01-18 01:51:26 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
// SplitAs class functions
|
|
|
|
|
|
|
|
|
|
class SplitAsVisitor : public SplitAsBaseVisitor {
|
|
|
|
|
private:
|
|
|
|
|
// NODE STATE
|
2019-05-19 22:13:13 +02:00
|
|
|
// AstAlways::user() -> bool. True if already processed
|
2020-04-15 13:58:34 +02:00
|
|
|
AstUser1InUse m_inuser1;
|
2007-01-18 01:51:26 +01:00
|
|
|
|
|
|
|
|
// STATE
|
2019-10-05 13:54:14 +02:00
|
|
|
VDouble0 m_statSplits; // Statistic tracking
|
2020-08-16 15:55:36 +02:00
|
|
|
AstVarScope* m_splitVscp = nullptr; // Variable we want to split
|
2007-01-18 01:51:26 +01:00
|
|
|
|
|
|
|
|
// METHODS
|
|
|
|
|
void splitAlways(AstAlways* nodep) {
|
2020-04-15 13:58:34 +02:00
|
|
|
UINFO(3, "Split " << nodep << endl);
|
|
|
|
|
UINFO(3, " For " << m_splitVscp << endl);
|
|
|
|
|
if (debug() >= 9) nodep->dumpTree(cout, "-in : ");
|
2019-05-19 22:13:13 +02:00
|
|
|
// Duplicate it and link in
|
|
|
|
|
AstAlways* newp = nodep->cloneTree(false);
|
|
|
|
|
newp->user1(true); // So we don't clone it again
|
|
|
|
|
nodep->addNextHere(newp);
|
2020-04-15 13:58:34 +02:00
|
|
|
{ // Delete stuff we don't want in old
|
|
|
|
|
SplitAsCleanVisitor visitor(nodep, m_splitVscp, false);
|
|
|
|
|
if (debug() >= 9) nodep->dumpTree(cout, "-out0: ");
|
2019-05-19 22:13:13 +02:00
|
|
|
}
|
2020-04-15 13:58:34 +02:00
|
|
|
{ // Delete stuff we don't want in new
|
|
|
|
|
SplitAsCleanVisitor visitor(newp, m_splitVscp, true);
|
|
|
|
|
if (debug() >= 9) newp->dumpTree(cout, "-out1: ");
|
2019-05-19 22:13:13 +02:00
|
|
|
}
|
2007-01-18 01:51:26 +01:00
|
|
|
}
|
|
|
|
|
|
2020-08-15 16:03:34 +02:00
|
|
|
virtual void visit(AstAlways* nodep) override {
|
2019-05-19 22:13:13 +02:00
|
|
|
// Are there any lvalue references below this?
|
|
|
|
|
// There could be more than one. So, we process the first one found first.
|
2020-08-15 16:12:55 +02:00
|
|
|
AstVarScope* lastSplitVscp = nullptr;
|
2019-05-19 22:13:13 +02:00
|
|
|
while (!nodep->user1()) {
|
|
|
|
|
// Find any splittable variables
|
2020-04-15 13:58:34 +02:00
|
|
|
SplitAsFindVisitor visitor(nodep);
|
2019-05-19 22:13:13 +02:00
|
|
|
m_splitVscp = visitor.splitVscp();
|
|
|
|
|
if (m_splitVscp && m_splitVscp == lastSplitVscp) {
|
|
|
|
|
// We did this last time! Something's stuck!
|
|
|
|
|
nodep->v3fatalSrc("Infinite loop in isolate_assignments removal for: "
|
2020-04-15 13:58:34 +02:00
|
|
|
<< m_splitVscp->prettyNameQ());
|
2019-05-19 22:13:13 +02:00
|
|
|
}
|
|
|
|
|
lastSplitVscp = m_splitVscp;
|
|
|
|
|
// Now isolate the always
|
|
|
|
|
if (m_splitVscp) {
|
|
|
|
|
splitAlways(nodep);
|
|
|
|
|
++m_statSplits;
|
|
|
|
|
} else {
|
|
|
|
|
nodep->user1(true);
|
|
|
|
|
}
|
|
|
|
|
}
|
2007-01-18 01:51:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Speedup; no always under math
|
2020-08-15 16:03:34 +02:00
|
|
|
virtual void visit(AstNodeMath*) override {}
|
|
|
|
|
virtual void visit(AstNode* nodep) override { iterateChildren(nodep); }
|
2007-01-18 01:51:26 +01:00
|
|
|
|
|
|
|
|
public:
|
2019-09-12 13:22:22 +02:00
|
|
|
// CONSTRUCTORS
|
2015-10-04 04:33:06 +02:00
|
|
|
explicit SplitAsVisitor(AstNetlist* nodep) {
|
2019-05-19 22:13:13 +02:00
|
|
|
AstNode::user1ClearTree(); // user1p() used on entire tree
|
2018-05-11 02:55:37 +02:00
|
|
|
iterate(nodep);
|
2007-01-18 01:51:26 +01:00
|
|
|
}
|
2020-08-15 17:44:10 +02:00
|
|
|
virtual ~SplitAsVisitor() override {
|
2019-05-19 22:13:13 +02:00
|
|
|
V3Stats::addStat("Optimizations, isolate_assignments blocks", m_statSplits);
|
2007-01-18 01:51:26 +01:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
// SplitAs class functions
|
|
|
|
|
|
|
|
|
|
void V3SplitAs::splitAsAll(AstNetlist* nodep) {
|
2020-04-15 13:58:34 +02:00
|
|
|
UINFO(2, __FUNCTION__ << ": " << endl);
|
|
|
|
|
{ SplitAsVisitor visitor(nodep); } // Destruct before checking
|
2017-09-18 04:52:57 +02:00
|
|
|
V3Global::dumpCheckGlobalTree("splitas", 0, v3Global.opt.dumpTreeLevel(__FILE__) >= 3);
|
2007-01-18 01:51:26 +01:00
|
|
|
}
|