2012-04-13 03:08:20 +02:00
|
|
|
|
// -*- mode: C++; c-file-style: "cc-mode" -*-
|
2006-08-26 13:35:28 +02:00
|
|
|
|
//*************************************************************************
|
|
|
|
|
|
// DESCRIPTION: Verilator: AssignPost Variable assignment elimination
|
|
|
|
|
|
//
|
2008-04-25 14:14:27 +02:00
|
|
|
|
// Code available from: http://www.veripool.org/verilator
|
2006-08-26 13:35:28 +02:00
|
|
|
|
//
|
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
|
//
|
2018-01-03 00:05:06 +01:00
|
|
|
|
// Copyright 2003-2018 by Wilson Snyder. This program is free software; you can
|
2006-08-26 13:35:28 +02:00
|
|
|
|
// 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.
|
2006-08-26 13:35:28 +02:00
|
|
|
|
//
|
|
|
|
|
|
// 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.
|
|
|
|
|
|
//
|
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
|
// LIFE TRANSFORMATIONS:
|
|
|
|
|
|
// Build control-flow graph with assignments and var usages
|
|
|
|
|
|
// All modules:
|
|
|
|
|
|
// Delete these
|
|
|
|
|
|
// ASSIGN(Vdly, a)
|
|
|
|
|
|
// ... {no reads or writes of a after the first write to Vdly}
|
|
|
|
|
|
// ... {no reads of a after the first write to Vdly}
|
|
|
|
|
|
// ASSIGNPOST(Vdly,tmp)
|
2008-06-10 03:25:10 +02:00
|
|
|
|
//
|
2006-08-26 13:35:28 +02:00
|
|
|
|
//*************************************************************************
|
|
|
|
|
|
|
2006-12-18 20:20:45 +01:00
|
|
|
|
#include "config_build.h"
|
|
|
|
|
|
#include "verilatedos.h"
|
2008-06-30 19:11:25 +02:00
|
|
|
|
#include <cstdio>
|
|
|
|
|
|
#include <cstdarg>
|
2006-08-26 13:35:28 +02:00
|
|
|
|
#include <unistd.h>
|
|
|
|
|
|
|
|
|
|
|
|
#include "V3Global.h"
|
|
|
|
|
|
#include "V3LifePost.h"
|
|
|
|
|
|
#include "V3Stats.h"
|
|
|
|
|
|
#include "V3Ast.h"
|
|
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
|
// LifePost state, as a visitor of each AstNode
|
|
|
|
|
|
|
|
|
|
|
|
class LifePostBaseVisitor : public AstNVisitor {
|
|
|
|
|
|
protected:
|
2018-05-14 12:50:47 +02:00
|
|
|
|
VL_DEBUG_FUNC; // Declare debug()
|
2006-08-26 13:35:28 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
|
// LifePost class functions
|
|
|
|
|
|
|
|
|
|
|
|
class LifePostElimVisitor : public LifePostBaseVisitor {
|
|
|
|
|
|
private:
|
2017-11-29 00:38:19 +01:00
|
|
|
|
bool m_tracingCall; // Iterating into a CCall to a CFunc
|
|
|
|
|
|
|
2006-08-26 13:35:28 +02:00
|
|
|
|
// NODE STATE
|
|
|
|
|
|
// INPUT:
|
|
|
|
|
|
// AstVarScope::user4p() -> AstVarScope*, If set, replace this varscope with specified new one
|
|
|
|
|
|
// STATE
|
|
|
|
|
|
// VISITORS
|
2016-11-27 14:11:38 +01:00
|
|
|
|
virtual void visit(AstVarRef* nodep) {
|
2006-08-26 13:35:28 +02:00
|
|
|
|
AstVarScope* vscp = nodep->varScopep();
|
|
|
|
|
|
if (!vscp) nodep->v3fatalSrc("Scope not assigned");
|
|
|
|
|
|
if (AstVarScope* newvscp = (AstVarScope*)vscp->user4p()) {
|
|
|
|
|
|
UINFO(9, " Replace "<<nodep<<" to "<<newvscp<<endl);
|
|
|
|
|
|
AstVarRef* newrefp = new AstVarRef(nodep->fileline(), newvscp, nodep->lvalue());
|
|
|
|
|
|
nodep->replaceWith(newrefp);
|
2015-10-04 19:16:35 +02:00
|
|
|
|
nodep->deleteTree(); VL_DANGLING(nodep);
|
2006-08-26 13:35:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-11-27 14:11:38 +01:00
|
|
|
|
virtual void visit(AstNodeModule* nodep) {
|
2006-08-26 13:35:28 +02:00
|
|
|
|
// Only track the top scopes, not lower level functions
|
2018-05-11 02:55:37 +02:00
|
|
|
|
if (nodep->isTop()) iterateChildren(nodep);
|
2006-08-26 13:35:28 +02:00
|
|
|
|
}
|
2016-11-27 14:11:38 +01:00
|
|
|
|
virtual void visit(AstCCall* nodep) {
|
2018-05-11 02:55:37 +02:00
|
|
|
|
iterateChildren(nodep);
|
2017-11-29 00:38:19 +01:00
|
|
|
|
if (!nodep->funcp()->entryPoint()) {
|
|
|
|
|
|
// Enter the function and trace it
|
|
|
|
|
|
m_tracingCall = true;
|
2018-05-11 02:55:37 +02:00
|
|
|
|
iterate(nodep->funcp());
|
2017-11-29 00:38:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
virtual void visit(AstCFunc* nodep) {
|
|
|
|
|
|
if (!m_tracingCall && !nodep->entryPoint()) return;
|
|
|
|
|
|
m_tracingCall = false;
|
2018-05-11 02:55:37 +02:00
|
|
|
|
iterateChildren(nodep);
|
2006-08-26 13:35:28 +02:00
|
|
|
|
}
|
2016-11-27 14:11:38 +01:00
|
|
|
|
virtual void visit(AstVar*) {} // Don't want varrefs under it
|
|
|
|
|
|
virtual void visit(AstNode* nodep) {
|
2018-05-11 02:55:37 +02:00
|
|
|
|
iterateChildren(nodep);
|
2006-08-26 13:35:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
public:
|
|
|
|
|
|
// CONSTRUCTORS
|
2017-11-29 00:38:19 +01:00
|
|
|
|
explicit LifePostElimVisitor(AstTopScope* nodep)
|
|
|
|
|
|
: m_tracingCall(false) {
|
2018-05-11 02:55:37 +02:00
|
|
|
|
iterate(nodep);
|
2006-08-26 13:35:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
virtual ~LifePostElimVisitor() {}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
2017-11-28 01:23:55 +01:00
|
|
|
|
// LifePost delay elimination
|
2006-08-26 13:35:28 +02:00
|
|
|
|
|
|
|
|
|
|
class LifePostDlyVisitor : public LifePostBaseVisitor {
|
|
|
|
|
|
private:
|
|
|
|
|
|
// NODE STATE
|
|
|
|
|
|
// Cleared on entire tree
|
2018-04-11 04:05:55 +02:00
|
|
|
|
// AstVarScope::user() -> Sequence # of first vertex setting this var.
|
2006-08-26 13:35:28 +02:00
|
|
|
|
// AstVarScope::user2() -> Sequence # of last consumption of this var
|
|
|
|
|
|
// AstVarScope::user4() -> AstVarScope*: Passed to LifePostElim to substitute this var
|
2008-11-25 15:03:49 +01:00
|
|
|
|
AstUser1InUse m_inuser1;
|
|
|
|
|
|
AstUser2InUse m_inuser2;
|
|
|
|
|
|
AstUser4InUse m_inuser4;
|
2006-08-26 13:35:28 +02:00
|
|
|
|
|
|
|
|
|
|
// STATE
|
|
|
|
|
|
uint32_t m_sequence; // Sequence number of assignments/varrefs
|
|
|
|
|
|
V3Double0 m_statAssnDel; // Statistic tracking
|
2017-11-29 00:38:19 +01:00
|
|
|
|
bool m_tracingCall; // Tracing a CCall to a CFunc
|
2006-08-26 13:35:28 +02:00
|
|
|
|
|
|
|
|
|
|
// VISITORS
|
2016-11-27 14:11:38 +01:00
|
|
|
|
virtual void visit(AstTopScope* nodep) {
|
2008-11-25 15:03:49 +01:00
|
|
|
|
AstNode::user1ClearTree(); // user1p() used on entire tree
|
|
|
|
|
|
AstNode::user2ClearTree(); // user2p() used on entire tree
|
|
|
|
|
|
AstNode::user4ClearTree(); // user4p() used on entire tree
|
2006-08-26 13:35:28 +02:00
|
|
|
|
m_sequence = 0;
|
2018-05-11 02:55:37 +02:00
|
|
|
|
iterateChildren(nodep);
|
2006-08-26 13:35:28 +02:00
|
|
|
|
|
|
|
|
|
|
// Replace any node4p varscopes with the new scope
|
|
|
|
|
|
LifePostElimVisitor visitor (nodep);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-11-27 14:11:38 +01:00
|
|
|
|
virtual void visit(AstVarRef* nodep) {
|
2008-06-10 03:25:10 +02:00
|
|
|
|
// Consumption/generation of a variable,
|
2006-08-26 13:35:28 +02:00
|
|
|
|
AstVarScope* vscp = nodep->varScopep();
|
|
|
|
|
|
if (!vscp) nodep->v3fatalSrc("Scope not assigned");
|
|
|
|
|
|
m_sequence++;
|
|
|
|
|
|
if (nodep->lvalue()) {
|
|
|
|
|
|
// First generator
|
2008-11-25 15:03:49 +01:00
|
|
|
|
if (!vscp->user1()) vscp->user1(m_sequence);
|
2006-08-26 13:35:28 +02:00
|
|
|
|
} else {
|
|
|
|
|
|
// Last consumer
|
|
|
|
|
|
vscp->user2(m_sequence);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2018-02-28 12:57:08 +01:00
|
|
|
|
|
|
|
|
|
|
virtual void visit(AstAssignPre* nodep) {
|
|
|
|
|
|
// Do not record varrefs within assign pre.
|
|
|
|
|
|
//
|
|
|
|
|
|
// The pre-assignment into the dly var should not count as its
|
|
|
|
|
|
// first write; we only want to consider reads and writes that
|
|
|
|
|
|
// would still happen if the dly var were eliminated.
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-11-27 14:11:38 +01:00
|
|
|
|
virtual void visit(AstAssignPost* nodep) {
|
2018-02-02 03:32:58 +01:00
|
|
|
|
if (AstVarRef* lhsp = VN_CAST(nodep->lhsp(), VarRef)) {
|
|
|
|
|
|
if (AstVarRef* rhsp = VN_CAST(nodep->rhsp(), VarRef)) {
|
2018-02-28 12:57:08 +01:00
|
|
|
|
// Scrunch these:
|
|
|
|
|
|
// X1: __Vdly__q = __PVT__clk_clocks;
|
|
|
|
|
|
// ... {no reads of __PVT__q after the first write to __Vdly__q}
|
|
|
|
|
|
// ... {no reads of __Vdly__q after the first write to __Vdly__q}
|
|
|
|
|
|
// X2: __PVT__q = __Vdly__q;
|
|
|
|
|
|
//
|
|
|
|
|
|
// Into just this:
|
|
|
|
|
|
// X1: __PVT__q = __PVT__clk_clocks;
|
|
|
|
|
|
// X2: (nothing)
|
2006-08-26 13:35:28 +02:00
|
|
|
|
UINFO(9," POST "<<nodep<<endl);
|
|
|
|
|
|
UINFO(9," lhs "<<lhsp<<endl);
|
|
|
|
|
|
UINFO(9," rhs "<<rhsp<<endl);
|
2018-02-28 12:57:08 +01:00
|
|
|
|
uint32_t firstDlyVarWrite = rhsp->varScopep()->user1();
|
|
|
|
|
|
uint32_t lastDlyVarRead = rhsp->varScopep()->user2();
|
|
|
|
|
|
uint32_t lastOrigVarRead = lhsp->varScopep()->user2();
|
|
|
|
|
|
UINFO(9," first_dly_w "<<firstDlyVarWrite
|
|
|
|
|
|
<<" last_dly_r "<<lastDlyVarRead
|
|
|
|
|
|
<<" last_orig_r "<<lastOrigVarRead<<endl);
|
|
|
|
|
|
if (lastDlyVarRead < firstDlyVarWrite
|
|
|
|
|
|
&& lastOrigVarRead < firstDlyVarWrite) {
|
2006-08-26 13:35:28 +02:00
|
|
|
|
UINFO(4," DELETE "<<nodep<<endl);
|
|
|
|
|
|
// Mark so LifePostElimVisitor will get it
|
|
|
|
|
|
rhsp->varScopep()->user4p(lhsp->varScopep());
|
2015-10-04 19:16:35 +02:00
|
|
|
|
nodep->unlinkFrBack()->deleteTree(); VL_DANGLING(nodep);
|
2011-08-05 03:15:24 +02:00
|
|
|
|
++m_statAssnDel;
|
2006-08-26 13:35:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2016-11-27 14:11:38 +01:00
|
|
|
|
virtual void visit(AstNodeModule* nodep) {
|
2006-08-26 13:35:28 +02:00
|
|
|
|
// Only track the top scopes, not lower level functions
|
2018-05-11 02:55:37 +02:00
|
|
|
|
if (nodep->isTop()) iterateChildren(nodep);
|
2006-08-26 13:35:28 +02:00
|
|
|
|
}
|
2016-11-27 14:11:38 +01:00
|
|
|
|
virtual void visit(AstCCall* nodep) {
|
2018-05-11 02:55:37 +02:00
|
|
|
|
iterateChildren(nodep);
|
2017-11-29 00:38:19 +01:00
|
|
|
|
if (!nodep->funcp()->entryPoint()) {
|
|
|
|
|
|
// Enter the function and trace it
|
|
|
|
|
|
m_tracingCall = true;
|
2018-05-11 02:55:37 +02:00
|
|
|
|
iterate(nodep->funcp());
|
2017-11-29 00:38:19 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
virtual void visit(AstCFunc* nodep) {
|
|
|
|
|
|
if (!m_tracingCall && !nodep->entryPoint()) return;
|
|
|
|
|
|
m_tracingCall = false;
|
2018-05-11 02:55:37 +02:00
|
|
|
|
iterateChildren(nodep);
|
2006-08-26 13:35:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//-----
|
2016-11-27 14:11:38 +01:00
|
|
|
|
virtual void visit(AstVar*) {} // Don't want varrefs under it
|
|
|
|
|
|
virtual void visit(AstNode* nodep) {
|
2018-05-11 02:55:37 +02:00
|
|
|
|
iterateChildren(nodep);
|
2006-08-26 13:35:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
public:
|
|
|
|
|
|
// CONSTRUCTORS
|
2017-11-29 00:38:19 +01:00
|
|
|
|
explicit LifePostDlyVisitor(AstNetlist* nodep)
|
2018-06-15 01:04:52 +02:00
|
|
|
|
: m_sequence(0)
|
|
|
|
|
|
, m_tracingCall(false) {
|
2018-05-11 02:55:37 +02:00
|
|
|
|
iterate(nodep);
|
2006-08-26 13:35:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
virtual ~LifePostDlyVisitor() {
|
|
|
|
|
|
V3Stats::addStat("Optimizations, Lifetime postassign deletions", m_statAssnDel);
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
|
// LifePost class functions
|
|
|
|
|
|
|
|
|
|
|
|
void V3LifePost::lifepostAll(AstNetlist* nodep) {
|
|
|
|
|
|
UINFO(2,__FUNCTION__<<": "<<endl);
|
|
|
|
|
|
// Mark redundant AssignPost
|
2018-03-10 18:57:50 +01:00
|
|
|
|
{
|
|
|
|
|
|
LifePostDlyVisitor visitor (nodep);
|
|
|
|
|
|
} // Destruct before checking
|
2017-09-18 04:52:57 +02:00
|
|
|
|
V3Global::dumpCheckGlobalTree("life_post", 0, v3Global.opt.dumpTreeLevel(__FILE__) >= 3);
|
2006-08-26 13:35:28 +02:00
|
|
|
|
}
|