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: Add temporaries, such as for unroll nodes
|
|
|
|
|
//
|
2008-04-25 12:14:27 +00:00
|
|
|
|
// Code available from: http://www.veripool.org/verilator
|
2006-08-26 11:35:28 +00:00
|
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
//
|
2015-01-07 23:25:53 +00:00
|
|
|
|
// Copyright 2003-2015 by Wilson Snyder. This program is free software; you can
|
2006-08-26 11:35:28 +00:00
|
|
|
|
// 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.
|
2006-08-26 11:35:28 +00: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.
|
|
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
// V3Unroll's Transformations:
|
|
|
|
|
// Note is called twice. Once on modules for GenFor unrolling,
|
|
|
|
|
// Again after V3Scope for normal for loop unrolling.
|
2008-06-10 01:25:10 +00:00
|
|
|
|
//
|
2006-08-26 11:35:28 +00:00
|
|
|
|
// Each module:
|
|
|
|
|
// Look for "FOR" loops and unroll them if <= 32 loops.
|
|
|
|
|
// (Eventually, a better way would be to simulate the entire loop; ala V3Table.)
|
|
|
|
|
// Convert remaining FORs to WHILEs
|
|
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
|
2006-12-18 19:20:45 +00:00
|
|
|
|
#include "config_build.h"
|
|
|
|
|
#include "verilatedos.h"
|
2008-06-30 17:11:25 +00:00
|
|
|
|
#include <cstdio>
|
|
|
|
|
#include <cstdarg>
|
2006-08-26 11:35:28 +00:00
|
|
|
|
#include <unistd.h>
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
|
|
|
|
|
#include "V3Global.h"
|
|
|
|
|
#include "V3Unroll.h"
|
|
|
|
|
#include "V3Stats.h"
|
|
|
|
|
#include "V3Const.h"
|
|
|
|
|
#include "V3Ast.h"
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
// Unroll state, as a visitor of each AstNode
|
|
|
|
|
|
|
|
|
|
class UnrollVisitor : public AstNVisitor {
|
|
|
|
|
private:
|
|
|
|
|
// STATE
|
|
|
|
|
AstVar* m_forVarp; // Iterator variable
|
|
|
|
|
AstVarScope* m_forVscp; // Iterator variable scope (NULL for generate pass)
|
|
|
|
|
AstConst* m_varValuep; // Current value of loop
|
2006-09-05 20:06:23 +00:00
|
|
|
|
AstNode* m_ignoreIncp; // Increment node to ignore
|
2012-07-28 14:52:29 +00:00
|
|
|
|
AstAttrOf* m_attrp; // Current attribute
|
2006-08-26 11:35:28 +00:00
|
|
|
|
bool m_varModeCheck; // Just checking RHS assignments
|
|
|
|
|
bool m_varModeReplace; // Replacing varrefs
|
|
|
|
|
bool m_varAssignHit; // Assign var hit
|
|
|
|
|
bool m_generate; // Expand single generate For loop
|
2012-07-21 21:12:42 +00:00
|
|
|
|
string m_beginName; // What name to give begin iterations
|
2006-08-26 11:35:28 +00:00
|
|
|
|
V3Double0 m_statLoops; // Statistic tracking
|
|
|
|
|
V3Double0 m_statIters; // Statistic tracking
|
|
|
|
|
|
2009-01-21 21:56:50 +00:00
|
|
|
|
// METHODS
|
|
|
|
|
static int debug() {
|
|
|
|
|
static int level = -1;
|
|
|
|
|
if (VL_UNLIKELY(level < 0)) level = v3Global.opt.debugSrcLevel(__FILE__);
|
|
|
|
|
return level;
|
|
|
|
|
}
|
2006-08-26 11:35:28 +00:00
|
|
|
|
|
|
|
|
|
// VISITORS
|
2006-09-05 20:06:23 +00:00
|
|
|
|
bool cantUnroll(AstNode* nodep, const char* reason) {
|
2006-08-26 11:35:28 +00:00
|
|
|
|
if (m_generate) {
|
|
|
|
|
nodep->v3error("Unsupported: Can't unroll generate for; "<<reason);
|
|
|
|
|
}
|
|
|
|
|
UINFO(3," Can't Unroll: "<<reason<<" :"<<nodep<<endl);
|
2014-03-09 21:12:52 +00:00
|
|
|
|
//if (debug()>=9) nodep->dumpTree(cout,"-cant-");
|
2006-08-26 11:35:28 +00:00
|
|
|
|
V3Stats::addStatSum(string("Unrolling gave up, ")+reason, 1);
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2008-10-06 13:59:22 +00:00
|
|
|
|
int unrollCount() {
|
|
|
|
|
return m_generate ? v3Global.opt.unrollCount()*16
|
|
|
|
|
: v3Global.opt.unrollCount();
|
|
|
|
|
}
|
|
|
|
|
|
2010-04-17 12:01:22 +00:00
|
|
|
|
bool bodySizeOverRecurse(AstNode* nodep, int& bodySize, int bodyLimit) {
|
|
|
|
|
if (!nodep) return false;
|
|
|
|
|
bodySize++;
|
|
|
|
|
// Exit once exceeds limits, rather than always total
|
|
|
|
|
// so don't go O(n^2) when can't unroll
|
|
|
|
|
if (bodySize > bodyLimit) return true;
|
|
|
|
|
if (bodySizeOverRecurse(nodep->op1p(), bodySize, bodyLimit)) return true;
|
|
|
|
|
if (bodySizeOverRecurse(nodep->op2p(), bodySize, bodyLimit)) return true;
|
|
|
|
|
if (bodySizeOverRecurse(nodep->op3p(), bodySize, bodyLimit)) return true;
|
|
|
|
|
if (bodySizeOverRecurse(nodep->op4p(), bodySize, bodyLimit)) return true;
|
|
|
|
|
// Tail recurse.
|
|
|
|
|
return bodySizeOverRecurse(nodep->nextp(), bodySize, bodyLimit);
|
|
|
|
|
}
|
|
|
|
|
|
2006-09-05 20:06:23 +00:00
|
|
|
|
bool forUnrollCheck(AstNode* nodep,
|
|
|
|
|
AstNode* initp, // Maybe under nodep (no nextp), or standalone (ignore nextp)
|
|
|
|
|
AstNode* precondsp, AstNode* condp,
|
|
|
|
|
AstNode* incp, // Maybe under nodep or in bodysp
|
|
|
|
|
AstNode* bodysp) {
|
|
|
|
|
// To keep the IF levels low, we return as each test fails.
|
|
|
|
|
UINFO(4, " FOR Check "<<nodep<<endl);
|
|
|
|
|
if (initp) UINFO(6, " Init "<<initp<<endl);
|
|
|
|
|
if (precondsp) UINFO(6, " Pcon "<<precondsp<<endl);
|
|
|
|
|
if (condp) UINFO(6, " Cond "<<condp<<endl);
|
|
|
|
|
if (incp) UINFO(6, " Inc "<<incp<<endl);
|
|
|
|
|
// Initial value check
|
|
|
|
|
AstAssign* initAssp = initp->castAssign();
|
|
|
|
|
if (!initAssp) return cantUnroll(nodep, "no initial assignment");
|
|
|
|
|
if (initp->nextp() && initp->nextp()!=nodep) nodep->v3fatalSrc("initial assignment shouldn't be a list");
|
2009-06-08 19:59:33 +00:00
|
|
|
|
if (!initAssp->lhsp()->castVarRef()) return cantUnroll(nodep, "no initial assignment to simple variable");
|
2006-09-05 20:06:23 +00:00
|
|
|
|
m_forVarp = initAssp->lhsp()->castVarRef()->varp();
|
|
|
|
|
m_forVscp = initAssp->lhsp()->castVarRef()->varScopep();
|
2006-08-26 11:35:28 +00:00
|
|
|
|
if (nodep->castGenFor() && !m_forVarp->isGenVar()) {
|
2013-03-12 11:27:17 +00:00
|
|
|
|
nodep->v3error("Non-genvar used in generate for: "<<m_forVarp->prettyName()<<endl);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|
2009-10-15 00:13:04 +00:00
|
|
|
|
if (m_generate) V3Const::constifyParamsEdit(initAssp->rhsp()); // rhsp may change
|
2006-09-05 20:06:23 +00:00
|
|
|
|
AstConst* constInitp = initAssp->rhsp()->castConst();
|
|
|
|
|
if (!constInitp) return cantUnroll(nodep, "non-constant initializer");
|
|
|
|
|
//
|
|
|
|
|
// Condition check
|
|
|
|
|
if (condp->nextp()) nodep->v3fatalSrc("conditional shouldn't be a list");
|
|
|
|
|
//
|
|
|
|
|
// Assignment of next value check
|
|
|
|
|
AstAssign* incAssp = incp->castAssign();
|
|
|
|
|
if (!incAssp) return cantUnroll(nodep, "no increment assignment");
|
|
|
|
|
if (incAssp->nextp()) nodep->v3fatalSrc("increment shouldn't be a list");
|
|
|
|
|
AstNodeBiop* incInstrp = incAssp->rhsp()->castNodeBiop();
|
|
|
|
|
//
|
2006-08-26 11:35:28 +00:00
|
|
|
|
if (m_forVscp) { UINFO(8, " Loop Variable: "<<m_forVscp<<endl); }
|
|
|
|
|
else { UINFO(8, " Loop Variable: "<<m_forVarp<<endl); }
|
2006-09-05 20:06:23 +00:00
|
|
|
|
if (debug()>=9) nodep->dumpTree(cout,"- for: ");
|
2006-08-26 11:35:28 +00:00
|
|
|
|
//
|
|
|
|
|
// Extract the constant loop bounds
|
|
|
|
|
bool subtract = incInstrp->castSub();
|
|
|
|
|
{
|
|
|
|
|
if (!subtract && !incInstrp->castAdd()) return cantUnroll(nodep, "missing add/sub for incrementer");
|
|
|
|
|
AstVarRef* incVarrp = (subtract ? incInstrp->lhsp()->castVarRef()
|
|
|
|
|
: incInstrp->rhsp()->castVarRef());
|
|
|
|
|
if (!incVarrp) return cantUnroll(nodep, "missing variable in incrementer");
|
|
|
|
|
if (incVarrp->varp() != m_forVarp
|
|
|
|
|
|| incVarrp->varScopep() != m_forVscp) {
|
|
|
|
|
return cantUnroll(nodep, "different variables in incrementer");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//
|
|
|
|
|
// Adds have the # on the lhsp because V3Const pushes rhs consts over to the lhs
|
|
|
|
|
// Subtracts have it on the rhs, because you write i=i-1; i=1-i is non-sensible.
|
|
|
|
|
AstConst* preconstIncp = (subtract ? incInstrp->rhsp()->castConst()
|
|
|
|
|
: incInstrp->lhsp()->castConst());
|
2009-10-15 00:13:04 +00:00
|
|
|
|
if (m_generate) preconstIncp = V3Const::constifyParamsEdit(preconstIncp)->castConst();
|
2006-08-26 11:35:28 +00:00
|
|
|
|
AstConst* constIncp = (subtract ? incInstrp->rhsp()->castConst()
|
|
|
|
|
: incInstrp->lhsp()->castConst());
|
|
|
|
|
UINFO(8, " Inc expr ok: "<<constIncp<<endl);
|
|
|
|
|
if (!constIncp) return cantUnroll(nodep, "non-constant increment");
|
|
|
|
|
if (constIncp->isZero()) return cantUnroll(nodep, "zero increment"); // Or we could loop forever below...
|
|
|
|
|
|
2014-03-09 21:12:52 +00:00
|
|
|
|
bool lt = condp->castLt() || condp->castLtS();
|
2006-08-26 11:35:28 +00:00
|
|
|
|
bool lte = condp->castLte() || condp->castLteS();
|
2014-03-09 21:12:52 +00:00
|
|
|
|
bool gt = condp->castGt() || condp->castGtS();
|
2006-08-26 11:35:28 +00:00
|
|
|
|
bool gte = condp->castGte() || condp->castGteS();
|
|
|
|
|
if (!lt && !lte && !gt && !gte)
|
2014-10-16 01:29:37 +00:00
|
|
|
|
return cantUnroll(nodep, "condition not <=, <, >= or >");
|
|
|
|
|
AstNodeBiop* cmpInstrp = condp->castNodeBiop();
|
|
|
|
|
bool cmpVarLhs;
|
|
|
|
|
if (cmpInstrp->lhsp()->castVarRef()
|
|
|
|
|
&& cmpInstrp->lhsp()->castVarRef()->varp() == m_forVarp
|
|
|
|
|
&& cmpInstrp->lhsp()->castVarRef()->varScopep() == m_forVscp) {
|
|
|
|
|
cmpVarLhs = true;
|
|
|
|
|
} else if (cmpInstrp->rhsp()->castVarRef()
|
|
|
|
|
&& cmpInstrp->rhsp()->castVarRef()->varp() == m_forVarp
|
|
|
|
|
&& cmpInstrp->rhsp()->castVarRef()->varScopep() == m_forVscp) {
|
|
|
|
|
cmpVarLhs = false;
|
|
|
|
|
} else if (!cmpInstrp->rhsp()->castVarRef()) {
|
2014-03-09 21:12:52 +00:00
|
|
|
|
return cantUnroll(nodep, "no variable on rhs of condition");
|
2014-10-16 01:29:37 +00:00
|
|
|
|
} else {
|
2006-08-26 11:35:28 +00:00
|
|
|
|
return cantUnroll(nodep, "different variable in condition");
|
2014-10-16 01:29:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (m_generate) V3Const::constifyParamsEdit(cmpVarLhs ? cmpInstrp->rhsp()
|
|
|
|
|
: cmpInstrp->lhsp()); // rhsp/lhsp may change
|
|
|
|
|
AstConst* constStopp = (cmpVarLhs ? cmpInstrp->rhsp()->castConst()
|
|
|
|
|
: cmpInstrp->lhsp()->castConst());
|
2006-08-26 11:35:28 +00:00
|
|
|
|
if (!constStopp) return cantUnroll(nodep, "non-constant final value");
|
|
|
|
|
UINFO(8, " Stop expr ok: "<<constStopp<<endl);
|
|
|
|
|
//
|
|
|
|
|
if (constInitp->width()>32 || constInitp->num().isFourState()
|
|
|
|
|
|| constStopp->width()>32 || constStopp->num().isFourState()
|
|
|
|
|
|| constIncp->width()>32 || constIncp->num().isFourState())
|
|
|
|
|
return cantUnroll(nodep, "init/final/increment too large or four state");
|
2008-10-06 13:59:22 +00:00
|
|
|
|
vlsint32_t valInit = constInitp->num().toSInt();
|
|
|
|
|
vlsint32_t valStop = constStopp->num().toSInt();
|
2014-03-09 21:12:52 +00:00
|
|
|
|
if (gte) valStop++; if (lte) valStop--; // 23 >= a, handle as if 24 > a
|
2008-09-03 21:40:01 +00:00
|
|
|
|
vlsint32_t valInc = constIncp->num().toSInt();
|
2006-08-26 11:35:28 +00:00
|
|
|
|
if (subtract) valInc = -valInc;
|
|
|
|
|
UINFO(8," In Numbers: for (v="<<valInit<<"; v<"<<valStop<<"; v=v+"<<valInc<<")\n");
|
|
|
|
|
//
|
|
|
|
|
if (!m_generate) {
|
2010-04-17 12:01:22 +00:00
|
|
|
|
int loops = ((valStop - valInit)/valInc);
|
2011-03-12 12:45:04 +00:00
|
|
|
|
if (loops < 0) { loops += (1ULL<<constStopp->width()); } // Will roll around
|
|
|
|
|
UINFO(8, " ~Iters: "<<loops<<" c="<<unrollCount()<<endl);
|
2010-04-17 12:01:22 +00:00
|
|
|
|
if (loops > unrollCount())
|
2006-08-26 11:35:28 +00:00
|
|
|
|
return cantUnroll(nodep, "too many iterations");
|
|
|
|
|
|
2007-11-30 22:12:53 +00:00
|
|
|
|
// Less than 10 statements in the body?
|
2006-08-26 11:35:28 +00:00
|
|
|
|
int bodySize = 0;
|
2010-04-17 12:01:22 +00:00
|
|
|
|
int bodyLimit = v3Global.opt.unrollStmts();
|
|
|
|
|
if (loops>0) bodyLimit = v3Global.opt.unrollStmts() / loops;
|
|
|
|
|
if (bodySizeOverRecurse(precondsp, bodySize/*ref*/, bodyLimit)
|
|
|
|
|
|| bodySizeOverRecurse(bodysp, bodySize/*ref*/, bodyLimit)
|
|
|
|
|
|| bodySizeOverRecurse(incp, bodySize/*ref*/, bodyLimit)) {
|
2006-08-26 11:35:28 +00:00
|
|
|
|
return cantUnroll(nodep, "too many statements");
|
2010-04-17 12:01:22 +00:00
|
|
|
|
}
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|
|
|
|
|
//
|
|
|
|
|
// Now, make sure there's no assignment to this variable in the loop
|
|
|
|
|
m_varModeCheck = true;
|
|
|
|
|
m_varAssignHit = false;
|
2006-09-05 20:06:23 +00:00
|
|
|
|
m_ignoreIncp = incp;
|
|
|
|
|
precondsp->iterateAndNext(*this);
|
|
|
|
|
bodysp->iterateAndNext(*this);
|
2010-02-14 15:01:21 +00:00
|
|
|
|
incp->iterateAndNext(*this);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
m_varModeCheck = false;
|
2006-09-05 20:06:23 +00:00
|
|
|
|
m_ignoreIncp = NULL;
|
2006-08-26 11:35:28 +00:00
|
|
|
|
if (m_varAssignHit) return cantUnroll(nodep, "genvar assigned *inside* loop");
|
|
|
|
|
//
|
|
|
|
|
// Finally, we can do it
|
2014-10-16 01:29:37 +00:00
|
|
|
|
forUnroller(nodep, initp, precondsp, incp, bodysp,
|
2006-09-05 20:06:23 +00:00
|
|
|
|
constInitp->num(),
|
2014-10-16 01:29:37 +00:00
|
|
|
|
cmpInstrp, constStopp->num(), cmpVarLhs,
|
2006-08-26 11:35:28 +00:00
|
|
|
|
incInstrp, constIncp->num()); nodep = NULL;
|
2007-11-02 11:23:03 +00:00
|
|
|
|
// Cleanup
|
2006-08-26 11:35:28 +00:00
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
2006-09-05 20:06:23 +00:00
|
|
|
|
void forUnroller(AstNode* nodep,
|
|
|
|
|
AstNode* initp,
|
2014-10-16 01:29:37 +00:00
|
|
|
|
AstNode* precondsp,
|
2006-09-05 20:06:23 +00:00
|
|
|
|
AstNode* incp, AstNode* bodysp,
|
|
|
|
|
const V3Number& numInit,
|
2014-10-16 01:29:37 +00:00
|
|
|
|
AstNodeBiop* cmpInstrp, const V3Number& numStop, bool cmpVarLhs,
|
2006-08-26 11:35:28 +00:00
|
|
|
|
AstNodeBiop* incInstrp, const V3Number& numInc) {
|
|
|
|
|
UINFO(4, " Unroll for var="<<numInit<<"; var<"<<numStop<<"; var+="<<numInc<<endl);
|
2006-09-05 20:06:23 +00:00
|
|
|
|
UINFO(6, " cmpI "<<cmpInstrp<<endl);
|
|
|
|
|
UINFO(6, " IncI "<<incInstrp<<endl);
|
|
|
|
|
AstNode* stmtsp = NULL;
|
|
|
|
|
if (initp) {
|
|
|
|
|
initp->unlinkFrBack(); // Always a single statement; nextp() may be nodep
|
|
|
|
|
// Don't add to list, we do it once, and setting loop index isn't needed as we're constant propagating it
|
|
|
|
|
}
|
|
|
|
|
if (precondsp) {
|
|
|
|
|
precondsp->unlinkFrBackWithNext();
|
2011-08-05 01:58:45 +00:00
|
|
|
|
// cppcheck-suppress nullPointer // addNextNull deals with it
|
2006-09-05 20:06:23 +00:00
|
|
|
|
stmtsp = stmtsp->addNextNull(precondsp);
|
|
|
|
|
}
|
|
|
|
|
if (bodysp) {
|
|
|
|
|
bodysp->unlinkFrBackWithNext();
|
2011-08-05 01:58:45 +00:00
|
|
|
|
// cppcheck-suppress nullPointer // addNextNull deals with it
|
2006-09-05 20:06:23 +00:00
|
|
|
|
stmtsp = stmtsp->addNextNull(bodysp); // Maybe null if no body
|
|
|
|
|
}
|
2010-02-14 15:01:21 +00:00
|
|
|
|
if (incp && !nodep->castGenFor()) { // Generates don't need to increment loop index
|
|
|
|
|
incp->unlinkFrBackWithNext();
|
2011-08-05 01:58:45 +00:00
|
|
|
|
// cppcheck-suppress nullPointer // addNextNull deals with it
|
2010-02-14 15:01:21 +00:00
|
|
|
|
stmtsp = stmtsp->addNextNull(incp); // Maybe null if no body
|
|
|
|
|
}
|
2011-01-01 01:18:21 +00:00
|
|
|
|
// Mark variable to disable some later warnings
|
|
|
|
|
m_forVarp->usedLoopIdx(true);
|
|
|
|
|
|
2006-09-05 20:06:23 +00:00
|
|
|
|
// If it's a While, then incp is already part of bodysp.
|
2006-08-26 11:35:28 +00:00
|
|
|
|
V3Number loopValue(nodep->fileline(), m_forVarp->width()); // May differ in size from numInitp
|
|
|
|
|
loopValue.opAssign(numInit);
|
|
|
|
|
|
2006-09-05 20:06:23 +00:00
|
|
|
|
AstNode* newbodysp = NULL;
|
2011-08-05 01:15:24 +00:00
|
|
|
|
++m_statLoops;
|
2006-09-05 20:06:23 +00:00
|
|
|
|
if (stmtsp) {
|
2006-08-26 11:35:28 +00:00
|
|
|
|
int times = 0;
|
|
|
|
|
while (1) {
|
|
|
|
|
UINFO(8," Looping "<<loopValue<<endl);
|
|
|
|
|
// if loopValue<valStop
|
|
|
|
|
V3Number contin (nodep->fileline(), 1);
|
2014-10-16 01:29:37 +00:00
|
|
|
|
if (cmpVarLhs) {
|
|
|
|
|
cmpInstrp->numberOperate(contin, loopValue, numStop);
|
|
|
|
|
} else {
|
|
|
|
|
cmpInstrp->numberOperate(contin, numStop, loopValue);
|
|
|
|
|
}
|
2006-08-26 11:35:28 +00:00
|
|
|
|
if (contin.isEqZero()) {
|
|
|
|
|
break; // Done with the loop
|
|
|
|
|
} else {
|
2006-09-05 20:06:23 +00:00
|
|
|
|
// Replace iterator values with constant.
|
|
|
|
|
AstNode* oneloopp = stmtsp->cloneTree(true);
|
|
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
|
m_varValuep = new AstConst(nodep->fileline(), loopValue);
|
2007-11-02 11:23:03 +00:00
|
|
|
|
|
2009-10-04 21:01:35 +00:00
|
|
|
|
// Iteration requires a back, so put under temporary node
|
2012-03-20 20:01:53 +00:00
|
|
|
|
if (oneloopp) {
|
2009-10-04 21:01:35 +00:00
|
|
|
|
AstBegin* tempp = new AstBegin(oneloopp->fileline(),"[EditWrapper]",oneloopp);
|
|
|
|
|
m_varModeReplace = true;
|
|
|
|
|
tempp->stmtsp()->iterateAndNext(*this);
|
|
|
|
|
m_varModeReplace = false;
|
2012-07-21 21:12:42 +00:00
|
|
|
|
oneloopp = tempp->stmtsp()->unlinkFrBackWithNext(); tempp->deleteTree(); tempp=NULL;
|
|
|
|
|
}
|
|
|
|
|
if (m_generate) {
|
|
|
|
|
string index = AstNode::encodeNumber(m_varValuep->toSInt());
|
|
|
|
|
string nname = m_beginName + "__BRA__" + index + "__KET__";
|
|
|
|
|
oneloopp = new AstBegin(oneloopp->fileline(),nname,oneloopp,true);
|
2009-10-04 21:01:35 +00:00
|
|
|
|
}
|
2006-08-26 11:35:28 +00:00
|
|
|
|
|
|
|
|
|
if (newbodysp) newbodysp->addNext(oneloopp);
|
|
|
|
|
else newbodysp = oneloopp;
|
|
|
|
|
|
2011-08-05 01:15:24 +00:00
|
|
|
|
++m_statIters;
|
2008-10-06 13:59:22 +00:00
|
|
|
|
if (++times > unrollCount()*3) {
|
|
|
|
|
nodep->v3error("Loop unrolling took too long; probably this is an infinite loop, or set --unroll-count above "<<unrollCount());
|
2006-08-26 11:35:28 +00:00
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//loopValue += valInc
|
|
|
|
|
V3Number newnum(nodep->fileline(), m_forVarp->width()); // Can't increment in-place
|
|
|
|
|
incInstrp->numberOperate(newnum, loopValue, numInc);
|
|
|
|
|
loopValue.opAssign(newnum);
|
2007-11-02 11:23:03 +00:00
|
|
|
|
|
|
|
|
|
pushDeletep(m_varValuep); m_varValuep=NULL;
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Replace the FOR()
|
|
|
|
|
if (newbodysp) nodep->replaceWith(newbodysp);
|
|
|
|
|
else nodep->unlinkFrBack();
|
2007-11-02 11:23:03 +00:00
|
|
|
|
if (bodysp) { pushDeletep(bodysp); bodysp=NULL; }
|
|
|
|
|
if (precondsp) { pushDeletep(precondsp); precondsp=NULL; }
|
|
|
|
|
if (initp) { pushDeletep(initp); initp=NULL; }
|
2012-05-31 03:17:55 +00:00
|
|
|
|
if (incp && !incp->backp()) { pushDeletep(incp); incp=NULL; }
|
2006-09-05 20:06:23 +00:00
|
|
|
|
if (debug()>=9) newbodysp->dumpTree(cout,"- _new: ");
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2006-09-05 20:06:23 +00:00
|
|
|
|
virtual void visit(AstWhile* nodep, AstNUser*) {
|
|
|
|
|
nodep->iterateChildren(*this);
|
|
|
|
|
if (m_varModeCheck || m_varModeReplace) {
|
|
|
|
|
} else {
|
|
|
|
|
// Constify before unroll call, as it may change what is underneath.
|
2009-10-15 00:13:04 +00:00
|
|
|
|
if (nodep->precondsp()) V3Const::constifyEdit(nodep->precondsp()); // precondsp may change
|
|
|
|
|
if (nodep->condp()) V3Const::constifyEdit(nodep->condp()); //condp may change
|
2006-09-05 20:06:23 +00:00
|
|
|
|
// Grab initial value
|
|
|
|
|
AstNode* initp = NULL; // Should be statement before the while.
|
|
|
|
|
if (nodep->backp()->nextp() == nodep) initp=nodep->backp();
|
2009-10-15 00:13:04 +00:00
|
|
|
|
if (initp) { V3Const::constifyEdit(initp); initp=NULL; }
|
|
|
|
|
if (nodep->backp()->nextp() == nodep) initp=nodep->backp();
|
2006-09-05 20:06:23 +00:00
|
|
|
|
// Grab assignment
|
|
|
|
|
AstNode* incp = NULL; // Should be last statement
|
2010-02-14 15:01:21 +00:00
|
|
|
|
if (nodep->incsp()) V3Const::constifyEdit(nodep->incsp());
|
|
|
|
|
if (nodep->incsp()) incp = nodep->incsp();
|
|
|
|
|
else {
|
|
|
|
|
for (incp = nodep->bodysp(); incp && incp->nextp(); incp = incp->nextp()) {}
|
|
|
|
|
if (incp) { V3Const::constifyEdit(incp); incp=NULL; }
|
|
|
|
|
for (incp = nodep->bodysp(); incp && incp->nextp(); incp = incp->nextp()) {} // Again, as may have changed
|
|
|
|
|
}
|
2006-09-05 20:06:23 +00:00
|
|
|
|
// And check it
|
|
|
|
|
if (forUnrollCheck(nodep, initp,
|
|
|
|
|
nodep->precondsp(), nodep->condp(),
|
|
|
|
|
incp, nodep->bodysp())) {
|
2007-11-02 11:23:03 +00:00
|
|
|
|
pushDeletep(nodep); nodep=NULL; // Did replacement
|
2006-09-05 20:06:23 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
virtual void visit(AstGenFor* nodep, AstNUser*) {
|
2006-08-26 11:35:28 +00:00
|
|
|
|
if (!m_generate || m_varModeReplace) {
|
|
|
|
|
nodep->iterateChildren(*this);
|
2006-09-05 20:06:23 +00:00
|
|
|
|
} // else V3Param will recursively call each for loop to be unrolled for us
|
2006-08-26 11:35:28 +00:00
|
|
|
|
if (m_varModeCheck || m_varModeReplace) {
|
|
|
|
|
} else {
|
2006-09-05 20:06:23 +00:00
|
|
|
|
// Constify before unroll call, as it may change what is underneath.
|
2009-10-15 00:13:04 +00:00
|
|
|
|
if (nodep->initsp()) V3Const::constifyEdit(nodep->initsp()); // initsp may change
|
|
|
|
|
if (nodep->condp()) V3Const::constifyEdit(nodep->condp()); // condp may change
|
|
|
|
|
if (nodep->incsp()) V3Const::constifyEdit(nodep->incsp()); // incsp may change
|
2007-11-05 14:09:22 +00:00
|
|
|
|
if (nodep->condp()->isZero()) {
|
|
|
|
|
// We don't need to do any loops. Remove the GenFor,
|
|
|
|
|
// Genvar's don't care about any initial assignments.
|
|
|
|
|
//
|
|
|
|
|
// Note normal For's can't do exactly this deletion, as
|
|
|
|
|
// we'd need to initialize the variable to the initial
|
|
|
|
|
// condition, but they'll become while's which can be
|
|
|
|
|
// deleted by V3Const.
|
|
|
|
|
nodep->unlinkFrBack()->deleteTree(); nodep=NULL;
|
|
|
|
|
} else if (forUnrollCheck(nodep, nodep->initsp(),
|
|
|
|
|
NULL, nodep->condp(),
|
|
|
|
|
nodep->incsp(), nodep->bodysp())) {
|
2007-11-02 11:23:03 +00:00
|
|
|
|
pushDeletep(nodep); nodep=NULL; // Did replacement
|
2006-08-26 11:35:28 +00:00
|
|
|
|
} else {
|
2010-04-09 23:45:46 +00:00
|
|
|
|
nodep->v3error("For loop doesn't have genvar index, or is malformed");
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-09-05 20:06:23 +00:00
|
|
|
|
virtual void visit(AstNodeFor* nodep, AstNUser*) {
|
2008-10-29 01:38:01 +00:00
|
|
|
|
if (m_generate) { // Ignore for's when expanding genfor's
|
|
|
|
|
nodep->iterateChildren(*this);
|
|
|
|
|
} else {
|
2009-02-26 03:06:59 +00:00
|
|
|
|
nodep->v3error("V3Begin should have removed standard FORs");
|
2008-10-29 01:38:01 +00:00
|
|
|
|
}
|
2006-09-05 20:06:23 +00:00
|
|
|
|
}
|
2012-07-28 14:52:29 +00:00
|
|
|
|
virtual void visit(AstAttrOf* nodep, AstNUser*) {
|
|
|
|
|
AstAttrOf* oldAttr = m_attrp;
|
|
|
|
|
m_attrp = nodep;
|
|
|
|
|
nodep->iterateChildren(*this);
|
|
|
|
|
m_attrp = oldAttr;
|
|
|
|
|
}
|
2006-08-26 11:35:28 +00:00
|
|
|
|
|
|
|
|
|
virtual void visit(AstVarRef* nodep, AstNUser*) {
|
|
|
|
|
if (m_varModeCheck
|
|
|
|
|
&& nodep->varp() == m_forVarp
|
|
|
|
|
&& nodep->varScopep() == m_forVscp
|
|
|
|
|
&& nodep->lvalue()) {
|
2008-06-10 01:25:10 +00:00
|
|
|
|
UINFO(8," Itervar assigned to: "<<nodep<<endl);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
m_varAssignHit = true;
|
|
|
|
|
}
|
|
|
|
|
if (m_varModeReplace
|
|
|
|
|
&& nodep->varp() == m_forVarp
|
2006-09-05 20:06:23 +00:00
|
|
|
|
&& nodep->varScopep() == m_forVscp
|
2009-10-25 20:53:55 +00:00
|
|
|
|
&& !nodep->lvalue()
|
2012-07-28 14:52:29 +00:00
|
|
|
|
&& !m_attrp) { // Most likely under a select
|
2006-08-26 11:35:28 +00:00
|
|
|
|
AstNode* newconstp = m_varValuep->cloneTree(false);
|
|
|
|
|
nodep->replaceWith(newconstp);
|
2007-11-02 11:23:03 +00:00
|
|
|
|
pushDeletep(nodep);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//--------------------
|
|
|
|
|
// Default: Just iterate
|
|
|
|
|
virtual void visit(AstNode* nodep, AstNUser*) {
|
2006-09-05 20:06:23 +00:00
|
|
|
|
if (m_varModeCheck && nodep == m_ignoreIncp) {
|
|
|
|
|
// Ignore subtree that is the increment
|
|
|
|
|
} else {
|
|
|
|
|
nodep->iterateChildren(*this);
|
|
|
|
|
}
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
// CONSTUCTORS
|
2012-07-21 21:12:42 +00:00
|
|
|
|
UnrollVisitor(AstNode* nodep, bool generate, string beginName) {
|
2006-08-26 11:35:28 +00:00
|
|
|
|
m_forVarp = NULL;
|
|
|
|
|
m_forVscp = NULL;
|
2006-09-05 20:06:23 +00:00
|
|
|
|
m_ignoreIncp = NULL;
|
2006-08-26 11:35:28 +00:00
|
|
|
|
m_varModeCheck = false;
|
|
|
|
|
m_varModeReplace = false;
|
|
|
|
|
m_generate = generate;
|
2012-07-21 21:12:42 +00:00
|
|
|
|
m_beginName = beginName;
|
2012-07-28 14:52:29 +00:00
|
|
|
|
m_attrp = NULL;
|
2006-08-26 11:35:28 +00:00
|
|
|
|
//
|
|
|
|
|
nodep->accept(*this);
|
|
|
|
|
}
|
|
|
|
|
virtual ~UnrollVisitor() {
|
|
|
|
|
V3Stats::addStat("Optimizations, Unrolled Loops", m_statLoops);
|
|
|
|
|
V3Stats::addStat("Optimizations, Unrolled Iterations", m_statIters);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
// Unroll class functions
|
|
|
|
|
|
|
|
|
|
void V3Unroll::unrollAll(AstNetlist* nodep) {
|
|
|
|
|
UINFO(2,__FUNCTION__<<": "<<endl);
|
2012-07-21 21:12:42 +00:00
|
|
|
|
UnrollVisitor visitor (nodep, false, "");
|
2015-03-12 23:47:54 +00:00
|
|
|
|
V3Global::dumpCheckGlobalTree("unroll.tree", 0, v3Global.opt.dumpTreeLevel(__FILE__) >= 3);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|
|
|
|
|
|
2012-07-21 21:12:42 +00:00
|
|
|
|
void V3Unroll::unrollGen(AstNodeFor* nodep, string beginName) {
|
2006-08-26 11:35:28 +00:00
|
|
|
|
UINFO(2,__FUNCTION__<<": "<<endl);
|
2012-07-21 21:12:42 +00:00
|
|
|
|
UnrollVisitor visitor (nodep, true, beginName);
|
2006-08-26 11:35:28 +00:00
|
|
|
|
}
|