2007-01-18 00:51:26 +00:00
|
|
|
|
// $Id$
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
// DESCRIPTION: Verilator: Break always into separate statements to reduce temps
|
|
|
|
|
//
|
|
|
|
|
// Code available from: http://www.veripool.com/verilator
|
|
|
|
|
//
|
|
|
|
|
// AUTHORS: Wilson Snyder with Paul Wasson, Duane Gabli
|
|
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
//
|
2008-01-15 14:29:08 +00:00
|
|
|
|
// Copyright 2003-2008 by Wilson Snyder. This program is free software; you can
|
2007-01-18 00:51:26 +00:00
|
|
|
|
// redistribute it and/or modify it under the terms of either the GNU
|
|
|
|
|
// General Public License or the Perl Artistic License.
|
|
|
|
|
//
|
|
|
|
|
// Verilator is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
// V3SplitAs's Transformations:
|
|
|
|
|
//
|
2007-01-23 18:11:26 +00:00
|
|
|
|
// Search each ALWAYS for a VARREF lvalue with a /*isolate_assignments*/ attribute
|
2007-01-18 00:51:26 +00:00
|
|
|
|
// If found, color statements with both, assignment to that varref, or other assignments.
|
|
|
|
|
// Replicate the Always, and remove mis-colored duplicate code.
|
|
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
|
|
|
|
|
#include "config_build.h"
|
|
|
|
|
#include "verilatedos.h"
|
|
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdarg.h>
|
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <vector>
|
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
|
|
#include "V3Global.h"
|
|
|
|
|
#include "V3SplitAs.h"
|
|
|
|
|
#include "V3Stats.h"
|
|
|
|
|
#include "V3Ast.h"
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
|
|
|
|
|
class SplitAsBaseVisitor : public AstNVisitor {
|
|
|
|
|
public:
|
|
|
|
|
// METHODS
|
|
|
|
|
//int debug() { return 9; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
// Find all split variables in a block
|
|
|
|
|
|
|
|
|
|
class SplitAsFindVisitor : public SplitAsBaseVisitor {
|
|
|
|
|
private:
|
|
|
|
|
// STATE
|
|
|
|
|
AstVarScope* m_splitVscp; // Variable we want to split
|
|
|
|
|
|
|
|
|
|
// METHODS
|
|
|
|
|
virtual void visit(AstVarRef* nodep, AstNUser*) {
|
|
|
|
|
if (nodep->lvalue() && !m_splitVscp
|
|
|
|
|
&& nodep->varp()->attrIsolateAssign()) {
|
|
|
|
|
m_splitVscp = nodep->varScopep();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstNode* nodep, AstNUser*) {
|
|
|
|
|
nodep->iterateChildren(*this);
|
|
|
|
|
}
|
|
|
|
|
public:
|
|
|
|
|
// CONSTUCTORS
|
|
|
|
|
SplitAsFindVisitor(AstAlways* nodep) {
|
|
|
|
|
m_splitVscp = NULL;
|
|
|
|
|
nodep->accept(*this);
|
|
|
|
|
}
|
|
|
|
|
virtual ~SplitAsFindVisitor() {}
|
|
|
|
|
// METHODS
|
|
|
|
|
AstVarScope* splitVscp() const { return m_splitVscp; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
// Remove nodes not containing proper references
|
|
|
|
|
|
|
|
|
|
class SplitAsCleanVisitor : public SplitAsBaseVisitor {
|
|
|
|
|
private:
|
|
|
|
|
// STATE
|
|
|
|
|
AstVarScope* m_splitVscp; // Variable we want to split
|
|
|
|
|
bool m_modeMatch; // Remove matching Vscp, else non-matching
|
|
|
|
|
bool m_keepStmt; // Current Statement must be preserved
|
|
|
|
|
bool m_matches; // Statement below has matching lvalue reference
|
|
|
|
|
|
|
|
|
|
// METHODS
|
|
|
|
|
virtual void visit(AstVarRef* nodep, AstNUser*) {
|
|
|
|
|
if (nodep->lvalue()) {
|
|
|
|
|
if (nodep->varScopep()==m_splitVscp) {
|
|
|
|
|
UINFO(6," CL VAR "<<nodep<<endl);
|
|
|
|
|
m_matches = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstNodeStmt* nodep, AstNUser*) {
|
|
|
|
|
UINFO(6," CL STMT "<<nodep<<endl);
|
|
|
|
|
bool oldKeep = m_keepStmt;
|
|
|
|
|
{
|
|
|
|
|
m_matches = false;
|
|
|
|
|
m_keepStmt = false;
|
|
|
|
|
|
|
|
|
|
nodep->iterateChildren(*this);
|
|
|
|
|
|
|
|
|
|
if (m_keepStmt
|
|
|
|
|
|| (m_modeMatch ? m_matches : !m_matches)) {
|
|
|
|
|
UINFO(6," Keep STMT "<<nodep<<endl);
|
|
|
|
|
m_keepStmt = true;
|
|
|
|
|
} else {
|
|
|
|
|
UINFO(6," Delete STMT "<<nodep<<endl);
|
|
|
|
|
nodep->unlinkFrBack(); pushDeletep(nodep);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// If something below matches, the upper statement remains too.
|
|
|
|
|
m_keepStmt = oldKeep || m_keepStmt;
|
|
|
|
|
UINFO(9," upKeep="<<m_keepStmt<<" STMT "<<nodep<<endl);
|
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstNode* nodep, AstNUser*) {
|
|
|
|
|
nodep->iterateChildren(*this);
|
|
|
|
|
}
|
|
|
|
|
public:
|
|
|
|
|
// CONSTUCTORS
|
|
|
|
|
SplitAsCleanVisitor(AstAlways* nodep, AstVarScope* vscp, bool modeMatch) {
|
|
|
|
|
m_splitVscp = vscp;
|
|
|
|
|
m_modeMatch = modeMatch;
|
|
|
|
|
nodep->accept(*this);
|
|
|
|
|
}
|
|
|
|
|
virtual ~SplitAsCleanVisitor() {}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
// SplitAs class functions
|
|
|
|
|
|
|
|
|
|
class SplitAsVisitor : public SplitAsBaseVisitor {
|
|
|
|
|
private:
|
|
|
|
|
// NODE STATE
|
|
|
|
|
// AstAlways::user() -> bool. True if already processed
|
|
|
|
|
|
|
|
|
|
// STATE
|
|
|
|
|
V3Double0 m_statSplits; // Statistic tracking
|
|
|
|
|
AstVarScope* m_splitVscp; // Variable we want to split
|
|
|
|
|
|
|
|
|
|
// METHODS
|
|
|
|
|
void splitAlways(AstAlways* nodep) {
|
|
|
|
|
UINFO(3,"Split "<<nodep<<endl);
|
|
|
|
|
UINFO(3," For "<<m_splitVscp<<endl);
|
|
|
|
|
if (debug()>=9) nodep->dumpTree(cout,"-in : ");
|
|
|
|
|
// Duplicate it and link in
|
|
|
|
|
AstAlways* newp = nodep->cloneTree(false)->castAlways();
|
|
|
|
|
newp->user(true); // So we don't clone it again
|
|
|
|
|
nodep->addNextHere(newp);
|
|
|
|
|
{ // Delete stuff we don't want in old
|
|
|
|
|
SplitAsCleanVisitor visitor (nodep, m_splitVscp, false);
|
|
|
|
|
if (debug()>=9) nodep->dumpTree(cout,"-out0: ");
|
|
|
|
|
}
|
|
|
|
|
{ // Delete stuff we don't want in new
|
|
|
|
|
SplitAsCleanVisitor visitor (newp, m_splitVscp, true);
|
|
|
|
|
if (debug()>=9) newp->dumpTree(cout,"-out1: ");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void visit(AstAlways* nodep, AstNUser*) {
|
|
|
|
|
// Are there any lvalue references below this?
|
2007-11-30 22:12:53 +00:00
|
|
|
|
// There could be more than one. So, we process the first one found first.
|
2007-01-23 18:11:26 +00:00
|
|
|
|
AstVarScope* lastSplitVscp = NULL;
|
|
|
|
|
while (!nodep->user()) {
|
|
|
|
|
// Find any splittable variables
|
2007-01-18 00:51:26 +00:00
|
|
|
|
SplitAsFindVisitor visitor (nodep);
|
|
|
|
|
m_splitVscp = visitor.splitVscp();
|
2007-01-23 18:11:26 +00:00
|
|
|
|
if (m_splitVscp && m_splitVscp == lastSplitVscp) {
|
|
|
|
|
// We did this last time! Something's stuck!
|
|
|
|
|
nodep->v3fatalSrc("Infinite loop in isolate_assignments removal for: "<<m_splitVscp->prettyName())
|
|
|
|
|
m_splitVscp = NULL;
|
|
|
|
|
}
|
|
|
|
|
lastSplitVscp = m_splitVscp;
|
|
|
|
|
// Now isolate the always
|
2007-01-18 00:51:26 +00:00
|
|
|
|
if (m_splitVscp) {
|
|
|
|
|
splitAlways(nodep);
|
|
|
|
|
m_statSplits++;
|
2007-01-23 18:11:26 +00:00
|
|
|
|
} else {
|
|
|
|
|
nodep->user(true);
|
2007-01-18 00:51:26 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Speedup; no always under math
|
|
|
|
|
virtual void visit(AstNodeMath* nodep, AstNUser*) {}
|
|
|
|
|
|
|
|
|
|
virtual void visit(AstNode* nodep, AstNUser*) {
|
|
|
|
|
nodep->iterateChildren(*this);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
// CONSTUCTORS
|
|
|
|
|
SplitAsVisitor(AstNetlist* nodep) {
|
|
|
|
|
m_splitVscp = NULL;
|
|
|
|
|
AstNode::userClearTree(); // userp() used on entire tree
|
|
|
|
|
nodep->accept(*this);
|
|
|
|
|
}
|
|
|
|
|
virtual ~SplitAsVisitor() {
|
|
|
|
|
V3Stats::addStat("Optimizations, isolate_assignments blocks", m_statSplits);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
// SplitAs class functions
|
|
|
|
|
|
|
|
|
|
void V3SplitAs::splitAsAll(AstNetlist* nodep) {
|
|
|
|
|
UINFO(2,__FUNCTION__<<": "<<endl);
|
|
|
|
|
SplitAsVisitor visitor (nodep);
|
|
|
|
|
}
|