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
|
|
|
|
//
|
2019-11-08 03:33:59 +00:00
|
|
|
// Code available from: https://verilator.org
|
2006-08-26 11:35:28 +00:00
|
|
|
//
|
|
|
|
//*************************************************************************
|
|
|
|
//
|
2020-03-21 15:24:24 +00: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 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
|
|
|
//
|
|
|
|
//*************************************************************************
|
|
|
|
// V3Unroll's Transformations:
|
2019-05-19 20:13:13 +00:00
|
|
|
// 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:
|
2019-05-19 20:13:13 +00:00
|
|
|
// 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-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 "V3Unroll.h"
|
|
|
|
#include "V3Stats.h"
|
|
|
|
#include "V3Const.h"
|
|
|
|
#include "V3Ast.h"
|
2016-01-22 00:00:19 +00:00
|
|
|
#include "V3Simulate.h"
|
2006-08-26 11:35:28 +00:00
|
|
|
|
2018-10-14 17:43:24 +00:00
|
|
|
#include <algorithm>
|
|
|
|
#include <cstdarg>
|
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
//######################################################################
|
|
|
|
// Unroll state, as a visitor of each AstNode
|
|
|
|
|
|
|
|
class UnrollVisitor : public AstNVisitor {
|
|
|
|
private:
|
|
|
|
// STATE
|
2020-04-15 11:58:34 +00:00
|
|
|
AstVar* m_forVarp; // Iterator variable
|
|
|
|
AstVarScope* m_forVscp; // Iterator variable scope (NULL for generate pass)
|
|
|
|
AstConst* m_varValuep; // Current value of loop
|
|
|
|
AstNode* m_ignoreIncp; // Increment node to ignore
|
|
|
|
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
|
|
|
|
string m_beginName; // What name to give begin iterations
|
|
|
|
VDouble0 m_statLoops; // Statistic tracking
|
|
|
|
VDouble0 m_statIters; // Statistic tracking
|
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
|
|
|
|
|
|
|
// VISITORS
|
2006-09-05 20:06:23 +00:00
|
|
|
bool cantUnroll(AstNode* nodep, const char* reason) {
|
2020-04-15 11:58:34 +00:00
|
|
|
if (m_generate) nodep->v3error("Unsupported: Can't unroll generate for; " << reason);
|
|
|
|
UINFO(3, " Can't Unroll: " << reason << " :" << nodep << endl);
|
|
|
|
// if (debug() >= 9) nodep->dumpTree(cout, "-cant-");
|
|
|
|
V3Stats::addStatSum(string("Unrolling gave up, ") + reason, 1);
|
2019-05-19 20:13:13 +00:00
|
|
|
return false;
|
2006-08-26 11:35:28 +00:00
|
|
|
}
|
|
|
|
|
2008-10-06 13:59:22 +00:00
|
|
|
int unrollCount() {
|
2020-04-15 11:58:34 +00:00
|
|
|
return m_generate ? v3Global.opt.unrollCount() * 16 : v3Global.opt.unrollCount();
|
2008-10-06 13:59:22 +00:00
|
|
|
}
|
|
|
|
|
2010-04-17 12:01:22 +00:00
|
|
|
bool bodySizeOverRecurse(AstNode* nodep, int& bodySize, int bodyLimit) {
|
2019-05-19 20:13:13 +00:00
|
|
|
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);
|
2010-04-17 12:01:22 +00:00
|
|
|
}
|
|
|
|
|
2020-04-15 11:58:34 +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) {
|
2019-05-19 20:13:13 +00:00
|
|
|
// To keep the IF levels low, we return as each test fails.
|
2020-04-15 11:58:34 +00:00
|
|
|
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);
|
2019-05-19 20:13:13 +00:00
|
|
|
|
|
|
|
// Initial value check
|
2018-02-02 02:32:58 +00:00
|
|
|
AstAssign* initAssp = VN_CAST(initp, Assign);
|
2019-05-19 20:13:13 +00:00
|
|
|
if (!initAssp) return cantUnroll(nodep, "no initial assignment");
|
2020-04-15 11:58:34 +00:00
|
|
|
UASSERT_OBJ(!(initp->nextp() && initp->nextp() != nodep), nodep,
|
2019-07-06 16:57:50 +00:00
|
|
|
"initial assignment shouldn't be a list");
|
2020-04-15 11:58:34 +00:00
|
|
|
if (!VN_IS(initAssp->lhsp(), VarRef)) {
|
|
|
|
return cantUnroll(nodep, "no initial assignment to simple variable");
|
|
|
|
}
|
2019-05-19 20:13:13 +00:00
|
|
|
//
|
|
|
|
// Condition check
|
2019-07-06 16:57:50 +00:00
|
|
|
UASSERT_OBJ(!condp->nextp(), nodep, "conditional shouldn't be a list");
|
2019-05-19 20:13:13 +00:00
|
|
|
//
|
|
|
|
// Assignment of next value check
|
2018-02-02 02:32:58 +00:00
|
|
|
AstAssign* incAssp = VN_CAST(incp, Assign);
|
2019-05-19 20:13:13 +00:00
|
|
|
if (!incAssp) return cantUnroll(nodep, "no increment assignment");
|
2019-07-06 16:57:50 +00:00
|
|
|
UASSERT_OBJ(!incAssp->nextp(), nodep, "increment shouldn't be a list");
|
2016-01-22 00:00:19 +00:00
|
|
|
|
2018-02-02 02:32:58 +00:00
|
|
|
m_forVarp = VN_CAST(initAssp->lhsp(), VarRef)->varp();
|
|
|
|
m_forVscp = VN_CAST(initAssp->lhsp(), VarRef)->varScopep();
|
|
|
|
if (VN_IS(nodep, GenFor) && !m_forVarp->isGenVar()) {
|
2020-04-15 11:58:34 +00:00
|
|
|
nodep->v3error("Non-genvar used in generate for: " //
|
|
|
|
<< m_forVarp->prettyNameQ() << endl);
|
|
|
|
} else if (!VN_IS(nodep, GenFor) && m_forVarp->isGenVar()) {
|
2020-01-31 01:23:57 +00:00
|
|
|
nodep->v3error("Genvar not legal in non-generate for (IEEE 1800-2017 27.4): "
|
2020-01-30 02:16:44 +00:00
|
|
|
<< m_forVarp->prettyNameQ() << endl
|
|
|
|
<< nodep->warnMore()
|
|
|
|
<< "... Suggest move for loop upwards to generate-level scope.");
|
|
|
|
}
|
2019-05-19 20:13:13 +00:00
|
|
|
if (m_generate) V3Const::constifyParamsEdit(initAssp->rhsp()); // rhsp may change
|
|
|
|
|
|
|
|
// This check shouldn't be needed when using V3Simulate
|
|
|
|
// however, for repeat loops, the loop variable is auto-generated
|
|
|
|
// and the initp statements will reference a variable outside of the initp scope
|
|
|
|
// alas, failing to simulate.
|
2018-02-02 02:32:58 +00:00
|
|
|
AstConst* constInitp = VN_CAST(initAssp->rhsp(), Const);
|
2019-05-19 20:13:13 +00:00
|
|
|
if (!constInitp) return cantUnroll(nodep, "non-constant initializer");
|
2016-01-22 00:00:19 +00:00
|
|
|
|
2019-05-19 20:13:13 +00:00
|
|
|
//
|
|
|
|
// Now, make sure there's no assignment to this variable in the loop
|
|
|
|
m_varModeCheck = true;
|
|
|
|
m_varAssignHit = false;
|
|
|
|
m_ignoreIncp = incp;
|
2018-05-11 00:55:37 +00:00
|
|
|
iterateAndNextNull(precondsp);
|
|
|
|
iterateAndNextNull(bodysp);
|
|
|
|
iterateAndNextNull(incp);
|
2019-05-19 20:13:13 +00:00
|
|
|
m_varModeCheck = false;
|
|
|
|
m_ignoreIncp = NULL;
|
|
|
|
if (m_varAssignHit) return cantUnroll(nodep, "genvar assigned *inside* loop");
|
2016-01-22 00:00:19 +00:00
|
|
|
|
2019-05-19 20:13:13 +00:00
|
|
|
//
|
2020-04-15 11:58:34 +00:00
|
|
|
if (m_forVscp) {
|
|
|
|
UINFO(8, " Loop Variable: " << m_forVscp << endl);
|
|
|
|
} else {
|
|
|
|
UINFO(8, " Loop Variable: " << m_forVarp << endl);
|
|
|
|
}
|
|
|
|
if (debug() >= 9) nodep->dumpTree(cout, "- for: ");
|
2014-10-16 01:29:37 +00:00
|
|
|
|
2019-05-19 20:13:13 +00:00
|
|
|
if (!m_generate) {
|
2020-04-15 11:58:34 +00:00
|
|
|
AstAssign* incpAssign = VN_CAST(incp, Assign);
|
|
|
|
if (!canSimulate(incpAssign->rhsp())) {
|
|
|
|
return cantUnroll(incp, "Unable to simulate increment");
|
|
|
|
}
|
2019-05-19 20:13:13 +00:00
|
|
|
if (!canSimulate(condp)) return cantUnroll(condp, "Unable to simulate condition");
|
|
|
|
|
|
|
|
// Check whether to we actually want to try and unroll.
|
|
|
|
int loops;
|
2020-04-15 11:58:34 +00:00
|
|
|
if (!countLoops(initAssp, condp, incp, unrollCount(), loops)) {
|
2019-05-19 20:13:13 +00:00
|
|
|
return cantUnroll(nodep, "Unable to simulate loop");
|
2020-04-15 11:58:34 +00:00
|
|
|
}
|
2019-05-19 20:13:13 +00:00
|
|
|
|
|
|
|
// Less than 10 statements in the body?
|
|
|
|
int bodySize = 0;
|
|
|
|
int bodyLimit = v3Global.opt.unrollStmts();
|
2020-04-15 11:58:34 +00:00
|
|
|
if (loops > 0) bodyLimit = v3Global.opt.unrollStmts() / loops;
|
|
|
|
if (bodySizeOverRecurse(precondsp, bodySize /*ref*/, bodyLimit)
|
|
|
|
|| bodySizeOverRecurse(bodysp, bodySize /*ref*/, bodyLimit)
|
|
|
|
|| bodySizeOverRecurse(incp, bodySize /*ref*/, bodyLimit)) {
|
2019-05-19 20:13:13 +00:00
|
|
|
return cantUnroll(nodep, "too many statements");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Finally, we can do it
|
|
|
|
if (!forUnroller(nodep, initAssp, condp, precondsp, incp, bodysp)) {
|
|
|
|
return cantUnroll(nodep, "Unable to unroll loop");
|
|
|
|
}
|
|
|
|
VL_DANGLING(nodep);
|
|
|
|
// Cleanup
|
|
|
|
return true;
|
2006-08-26 11:35:28 +00:00
|
|
|
}
|
|
|
|
|
2020-04-15 11:58:34 +00:00
|
|
|
bool canSimulate(AstNode* nodep) {
|
2018-03-10 19:10:41 +00:00
|
|
|
SimulateVisitor simvis;
|
|
|
|
AstNode* clonep = nodep->cloneTree(true);
|
|
|
|
simvis.mainCheckTree(clonep);
|
2020-01-18 15:29:49 +00:00
|
|
|
VL_DO_CLEAR(pushDeletep(clonep), clonep = NULL);
|
2018-03-10 19:10:41 +00:00
|
|
|
return simvis.optimizable();
|
2016-01-22 00:00:19 +00:00
|
|
|
}
|
|
|
|
|
2020-04-15 11:58:34 +00:00
|
|
|
bool simulateTree(AstNode* nodep, const V3Number* loopValue, AstNode* dtypep,
|
|
|
|
V3Number& outNum) {
|
2019-05-30 02:43:26 +00:00
|
|
|
AstNode* clonep = nodep->cloneTree(true);
|
2019-07-06 16:57:50 +00:00
|
|
|
UASSERT_OBJ(clonep, nodep, "Failed to clone tree");
|
2019-05-19 20:13:13 +00:00
|
|
|
if (loopValue) {
|
|
|
|
m_varValuep = new AstConst(nodep->fileline(), *loopValue);
|
|
|
|
// Iteration requires a back, so put under temporary node
|
2019-05-30 02:43:26 +00:00
|
|
|
AstBegin* tempp = new AstBegin(nodep->fileline(), "[EditWrapper]", clonep);
|
2019-05-19 20:13:13 +00:00
|
|
|
m_varModeReplace = true;
|
2018-05-11 00:55:37 +00:00
|
|
|
iterateAndNextNull(tempp->stmtsp());
|
2019-05-19 20:13:13 +00:00
|
|
|
m_varModeReplace = false;
|
2019-05-30 02:43:26 +00:00
|
|
|
clonep = tempp->stmtsp()->unlinkFrBackWithNext();
|
2019-05-19 20:13:13 +00:00
|
|
|
tempp->deleteTree();
|
|
|
|
tempp = NULL;
|
2020-01-18 15:29:49 +00:00
|
|
|
VL_DO_CLEAR(pushDeletep(m_varValuep), m_varValuep = NULL);
|
2019-05-19 20:13:13 +00:00
|
|
|
}
|
|
|
|
SimulateVisitor simvis;
|
2019-05-30 02:43:26 +00:00
|
|
|
simvis.mainParamEmulate(clonep);
|
2019-05-19 20:13:13 +00:00
|
|
|
if (!simvis.optimizable()) {
|
|
|
|
UINFO(3, "Unable to simulate" << endl);
|
2020-04-15 11:58:34 +00:00
|
|
|
if (debug() >= 9) nodep->dumpTree(cout, "- _simtree: ");
|
2020-01-17 01:17:11 +00:00
|
|
|
VL_DO_DANGLING(clonep->deleteTree(), clonep);
|
2019-05-19 20:13:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Fetch the result
|
2019-05-30 02:43:26 +00:00
|
|
|
V3Number* res = simvis.fetchNumberNull(clonep);
|
2019-05-19 20:13:13 +00:00
|
|
|
if (!res) {
|
|
|
|
UINFO(3, "No number returned from simulation" << endl);
|
2020-01-17 01:17:11 +00:00
|
|
|
VL_DO_DANGLING(clonep->deleteTree(), clonep);
|
2019-05-19 20:13:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Patch up datatype
|
|
|
|
if (dtypep) {
|
2020-04-15 11:58:34 +00:00
|
|
|
AstConst new_con(clonep->fileline(), *res);
|
2019-05-19 20:13:13 +00:00
|
|
|
new_con.dtypeFrom(dtypep);
|
|
|
|
outNum = new_con.num();
|
2020-01-17 01:17:11 +00:00
|
|
|
VL_DO_DANGLING(clonep->deleteTree(), clonep);
|
2019-05-19 20:13:13 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
outNum = *res;
|
2020-01-17 01:17:11 +00:00
|
|
|
VL_DO_DANGLING(clonep->deleteTree(), clonep);
|
2019-05-19 20:13:13 +00:00
|
|
|
return true;
|
2016-01-22 00:00:19 +00:00
|
|
|
}
|
|
|
|
|
2020-04-15 11:58:34 +00:00
|
|
|
bool countLoops(AstAssign* initp, AstNode* condp, AstNode* incp, int max, int& outLoopsr) {
|
2019-05-19 20:13:13 +00:00
|
|
|
outLoopsr = 0;
|
2019-05-10 00:03:19 +00:00
|
|
|
V3Number loopValue = V3Number(initp);
|
2020-04-15 11:58:34 +00:00
|
|
|
if (!simulateTree(initp->rhsp(), NULL, initp, loopValue)) { //
|
2019-05-10 00:03:19 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-04-04 02:31:54 +00:00
|
|
|
while (true) {
|
2019-05-10 00:03:19 +00:00
|
|
|
V3Number res = V3Number(initp);
|
2020-04-15 11:58:34 +00:00
|
|
|
if (!simulateTree(condp, &loopValue, NULL, res)) { //
|
2019-05-19 20:13:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
2020-04-15 11:58:34 +00:00
|
|
|
if (!res.isEqOne()) break;
|
2016-01-22 00:00:19 +00:00
|
|
|
|
2019-05-19 20:13:13 +00:00
|
|
|
outLoopsr++;
|
2016-01-22 00:00:19 +00:00
|
|
|
|
2019-05-19 20:13:13 +00:00
|
|
|
// Run inc
|
2018-02-02 02:32:58 +00:00
|
|
|
AstAssign* incpass = VN_CAST(incp, Assign);
|
2019-05-10 00:03:19 +00:00
|
|
|
V3Number newLoopValue = V3Number(initp);
|
|
|
|
if (!simulateTree(incpass->rhsp(), &loopValue, incpass, newLoopValue)) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-19 20:13:13 +00:00
|
|
|
loopValue.opAssign(newLoopValue);
|
2020-04-15 11:58:34 +00:00
|
|
|
if (outLoopsr > max) return false;
|
2019-05-19 20:13:13 +00:00
|
|
|
}
|
|
|
|
return true;
|
2016-01-22 00:00:19 +00:00
|
|
|
}
|
|
|
|
|
2020-04-15 11:58:34 +00:00
|
|
|
bool forUnroller(AstNode* nodep, AstAssign* initp, AstNode* condp, AstNode* precondsp,
|
2019-05-19 20:13:13 +00:00
|
|
|
AstNode* incp, AstNode* bodysp) {
|
2020-04-15 11:58:34 +00:00
|
|
|
UINFO(9, "forUnroller " << nodep << endl);
|
2019-05-10 00:03:19 +00:00
|
|
|
V3Number loopValue = V3Number(nodep);
|
2020-04-15 11:58:34 +00:00
|
|
|
if (!simulateTree(initp->rhsp(), NULL, initp, loopValue)) { //
|
2019-05-19 20:13:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
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
|
2019-11-15 23:24:55 +00:00
|
|
|
// needed if we have > 1 loop, as we're constant propagating it
|
2019-05-19 20:13:13 +00:00
|
|
|
}
|
|
|
|
if (precondsp) {
|
|
|
|
precondsp->unlinkFrBackWithNext();
|
|
|
|
stmtsp = AstNode::addNextNull(stmtsp, precondsp);
|
|
|
|
}
|
|
|
|
if (bodysp) {
|
|
|
|
bodysp->unlinkFrBackWithNext();
|
|
|
|
stmtsp = AstNode::addNextNull(stmtsp, bodysp); // Maybe null if no body
|
|
|
|
}
|
2018-02-02 02:32:58 +00:00
|
|
|
if (incp && !VN_IS(nodep, GenFor)) { // Generates don't need to increment loop index
|
2019-05-19 20:13:13 +00:00
|
|
|
incp->unlinkFrBackWithNext();
|
|
|
|
stmtsp = AstNode::addNextNull(stmtsp, incp); // Maybe null if no body
|
|
|
|
}
|
|
|
|
// Mark variable to disable some later warnings
|
|
|
|
m_forVarp->usedLoopIdx(true);
|
|
|
|
|
|
|
|
AstNode* newbodysp = NULL;
|
|
|
|
++m_statLoops;
|
|
|
|
if (stmtsp) {
|
2019-11-16 08:14:55 +00:00
|
|
|
int times = 0;
|
2020-04-04 02:31:54 +00:00
|
|
|
while (true) {
|
2020-04-15 11:58:34 +00:00
|
|
|
UINFO(8, " Looping " << loopValue << endl);
|
2019-05-10 00:03:19 +00:00
|
|
|
V3Number res = V3Number(nodep);
|
|
|
|
if (!simulateTree(condp, &loopValue, NULL, res)) {
|
2019-05-19 20:13:13 +00:00
|
|
|
nodep->v3error("Loop unrolling failed.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!res.isEqOne()) {
|
|
|
|
break; // Done with the loop
|
2020-04-15 11:58:34 +00:00
|
|
|
} else {
|
2019-05-19 20:13:13 +00:00
|
|
|
// Replace iterator values with constant.
|
|
|
|
AstNode* oneloopp = stmtsp->cloneTree(true);
|
|
|
|
|
|
|
|
m_varValuep = new AstConst(nodep->fileline(), loopValue);
|
|
|
|
|
|
|
|
// Iteration requires a back, so put under temporary node
|
|
|
|
if (oneloopp) {
|
2020-04-15 11:58:34 +00:00
|
|
|
AstBegin* tempp
|
|
|
|
= new AstBegin(oneloopp->fileline(), "[EditWrapper]", oneloopp);
|
2019-05-19 20:13:13 +00:00
|
|
|
m_varModeReplace = true;
|
2018-05-11 00:55:37 +00:00
|
|
|
iterateAndNextNull(tempp->stmtsp());
|
2019-05-19 20:13:13 +00:00
|
|
|
m_varModeReplace = false;
|
|
|
|
oneloopp = tempp->stmtsp()->unlinkFrBackWithNext();
|
2020-01-17 01:17:11 +00:00
|
|
|
VL_DO_DANGLING(tempp->deleteTree(), tempp);
|
2019-05-19 20:13:13 +00:00
|
|
|
}
|
|
|
|
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);
|
|
|
|
}
|
2020-01-18 15:29:49 +00:00
|
|
|
VL_DO_CLEAR(pushDeletep(m_varValuep), m_varValuep = NULL);
|
2020-04-15 11:58:34 +00:00
|
|
|
if (newbodysp) {
|
|
|
|
newbodysp->addNext(oneloopp);
|
|
|
|
} else {
|
|
|
|
newbodysp = oneloopp;
|
|
|
|
}
|
2019-05-19 20:13:13 +00:00
|
|
|
|
|
|
|
++m_statIters;
|
2020-04-15 11:58:34 +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());
|
2019-05-19 20:13:13 +00:00
|
|
|
break;
|
|
|
|
}
|
2006-08-26 11:35:28 +00:00
|
|
|
|
2019-05-19 20:13:13 +00:00
|
|
|
// loopValue += valInc
|
2020-04-15 11:58:34 +00:00
|
|
|
AstAssign* incpass = VN_CAST(incp, Assign);
|
2019-05-10 00:03:19 +00:00
|
|
|
V3Number newLoopValue = V3Number(nodep);
|
|
|
|
if (!simulateTree(incpass->rhsp(), &loopValue, incpass, newLoopValue)) {
|
|
|
|
nodep->v3error("Loop unrolling failed");
|
2019-05-19 20:13:13 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
loopValue.opAssign(newLoopValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-15 23:24:55 +00:00
|
|
|
if (!newbodysp) { // initp might have effects after the loop
|
|
|
|
newbodysp = initp; // Maybe NULL
|
|
|
|
initp = NULL;
|
|
|
|
}
|
2019-05-19 20:13:13 +00:00
|
|
|
// Replace the FOR()
|
2020-04-15 11:58:34 +00:00
|
|
|
if (newbodysp) {
|
|
|
|
nodep->replaceWith(newbodysp);
|
|
|
|
} else {
|
|
|
|
nodep->unlinkFrBack();
|
|
|
|
}
|
2020-01-17 01:17:11 +00:00
|
|
|
if (bodysp) { VL_DO_DANGLING(pushDeletep(bodysp), bodysp); }
|
|
|
|
if (precondsp) { VL_DO_DANGLING(pushDeletep(precondsp), precondsp); }
|
|
|
|
if (initp) { VL_DO_DANGLING(pushDeletep(initp), initp); }
|
|
|
|
if (incp && !incp->backp()) { VL_DO_DANGLING(pushDeletep(incp), incp); }
|
2020-04-15 11:58:34 +00:00
|
|
|
if (debug() >= 9 && newbodysp) newbodysp->dumpTree(cout, "- _new: ");
|
2019-05-19 20:13:13 +00:00
|
|
|
return true;
|
2006-08-26 11:35:28 +00:00
|
|
|
}
|
|
|
|
|
2020-01-21 22:35:56 +00:00
|
|
|
virtual void visit(AstWhile* nodep) VL_OVERRIDE {
|
2018-05-11 00:55:37 +00:00
|
|
|
iterateChildren(nodep);
|
2019-05-19 20:13:13 +00:00
|
|
|
if (m_varModeCheck || m_varModeReplace) {
|
|
|
|
} else {
|
|
|
|
// Constify before unroll call, as it may change what is underneath.
|
2020-04-15 11:58:34 +00:00
|
|
|
if (nodep->precondsp()) {
|
|
|
|
V3Const::constifyEdit(nodep->precondsp()); // precondsp may change
|
|
|
|
}
|
2019-05-19 20:13:13 +00:00
|
|
|
if (nodep->condp()) V3Const::constifyEdit(nodep->condp()); // condp may change
|
|
|
|
// Grab initial value
|
|
|
|
AstNode* initp = NULL; // Should be statement before the while.
|
|
|
|
if (nodep->backp()->nextp() == nodep) initp = nodep->backp();
|
2020-01-17 01:17:11 +00:00
|
|
|
if (initp) { VL_DO_DANGLING(V3Const::constifyEdit(initp), initp); }
|
2019-05-19 20:13:13 +00:00
|
|
|
if (nodep->backp()->nextp() == nodep) initp = nodep->backp();
|
|
|
|
// Grab assignment
|
|
|
|
AstNode* incp = NULL; // Should be last statement
|
2020-05-10 19:26:41 +00:00
|
|
|
AstNode* bodysp = nodep->bodysp();
|
2019-05-19 20:13:13 +00:00
|
|
|
if (nodep->incsp()) V3Const::constifyEdit(nodep->incsp());
|
2020-02-04 04:21:56 +00:00
|
|
|
// cppcheck-suppress duplicateCondition
|
2020-04-15 11:58:34 +00:00
|
|
|
if (nodep->incsp()) {
|
|
|
|
incp = nodep->incsp();
|
|
|
|
} else {
|
|
|
|
for (incp = nodep->bodysp(); incp && incp->nextp(); incp = incp->nextp()) {}
|
|
|
|
if (incp) VL_DO_DANGLING(V3Const::constifyEdit(incp), incp);
|
|
|
|
// Again, as may have changed
|
2020-05-10 19:26:41 +00:00
|
|
|
bodysp = nodep->bodysp();
|
2019-05-19 20:13:13 +00:00
|
|
|
for (incp = nodep->bodysp(); incp && incp->nextp(); incp = incp->nextp()) {}
|
2020-05-10 19:26:41 +00:00
|
|
|
if (incp == bodysp) bodysp = NULL;
|
2019-05-19 20:13:13 +00:00
|
|
|
}
|
|
|
|
// And check it
|
2020-05-10 19:26:41 +00:00
|
|
|
if (forUnrollCheck(nodep, initp, nodep->precondsp(), nodep->condp(), incp, bodysp)) {
|
2020-01-17 01:17:11 +00:00
|
|
|
VL_DO_DANGLING(pushDeletep(nodep), nodep); // Did replacement
|
2019-05-19 20:13:13 +00:00
|
|
|
}
|
|
|
|
}
|
2006-09-05 20:06:23 +00:00
|
|
|
}
|
2020-01-21 22:35:56 +00:00
|
|
|
virtual void visit(AstGenFor* nodep) VL_OVERRIDE {
|
2019-05-19 20:13:13 +00:00
|
|
|
if (!m_generate || m_varModeReplace) {
|
2018-05-11 00:55:37 +00:00
|
|
|
iterateChildren(nodep);
|
2019-05-19 20:13:13 +00:00
|
|
|
} // else V3Param will recursively call each for loop to be unrolled for us
|
|
|
|
if (m_varModeCheck || m_varModeReplace) {
|
|
|
|
} else {
|
|
|
|
// Constify before unroll call, as it may change what is underneath.
|
|
|
|
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
|
|
|
|
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.
|
2020-01-17 01:17:11 +00:00
|
|
|
VL_DO_DANGLING(pushDeletep(nodep->unlinkFrBack()), nodep);
|
2020-04-15 11:58:34 +00:00
|
|
|
} else if (forUnrollCheck(nodep, nodep->initsp(), NULL, nodep->condp(), nodep->incsp(),
|
|
|
|
nodep->bodysp())) {
|
2020-01-17 01:17:11 +00:00
|
|
|
VL_DO_DANGLING(pushDeletep(nodep), nodep); // Did replacement
|
2019-05-19 20:13:13 +00:00
|
|
|
} else {
|
|
|
|
nodep->v3error("For loop doesn't have genvar index, or is malformed");
|
|
|
|
}
|
|
|
|
}
|
2006-08-26 11:35:28 +00:00
|
|
|
}
|
2020-01-21 22:35:56 +00:00
|
|
|
virtual void visit(AstNodeFor* nodep) VL_OVERRIDE {
|
2019-05-19 20:13:13 +00:00
|
|
|
if (m_generate) { // Ignore for's when expanding genfor's
|
2018-05-11 00:55:37 +00:00
|
|
|
iterateChildren(nodep);
|
2019-05-19 20:13:13 +00:00
|
|
|
} else {
|
|
|
|
nodep->v3error("V3Begin should have removed standard FORs");
|
|
|
|
}
|
2006-09-05 20:06:23 +00:00
|
|
|
}
|
2006-08-26 11:35:28 +00:00
|
|
|
|
2020-01-21 22:35:56 +00:00
|
|
|
virtual void visit(AstVarRef* nodep) VL_OVERRIDE {
|
2020-04-15 11:58:34 +00:00
|
|
|
if (m_varModeCheck && nodep->varp() == m_forVarp && nodep->varScopep() == m_forVscp
|
2019-05-19 20:13:13 +00:00
|
|
|
&& nodep->lvalue()) {
|
2020-04-15 11:58:34 +00:00
|
|
|
UINFO(8, " Itervar assigned to: " << nodep << endl);
|
2019-05-19 20:13:13 +00:00
|
|
|
m_varAssignHit = true;
|
|
|
|
}
|
|
|
|
|
2020-04-15 11:58:34 +00:00
|
|
|
if (m_varModeReplace && nodep->varp() == m_forVarp && nodep->varScopep() == m_forVscp
|
2019-05-19 20:13:13 +00:00
|
|
|
&& !nodep->lvalue()) {
|
|
|
|
AstNode* newconstp = m_varValuep->cloneTree(false);
|
|
|
|
nodep->replaceWith(newconstp);
|
|
|
|
pushDeletep(nodep);
|
|
|
|
}
|
2006-08-26 11:35:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------
|
|
|
|
// Default: Just iterate
|
2020-01-21 22:35:56 +00:00
|
|
|
virtual void visit(AstNode* nodep) VL_OVERRIDE {
|
2019-05-19 20:13:13 +00:00
|
|
|
if (m_varModeCheck && nodep == m_ignoreIncp) {
|
|
|
|
// Ignore subtree that is the increment
|
|
|
|
} else {
|
2018-05-11 00:55:37 +00:00
|
|
|
iterateChildren(nodep);
|
2019-05-19 20:13:13 +00:00
|
|
|
}
|
2006-08-26 11:35:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2019-09-12 11:22:22 +00:00
|
|
|
// CONSTRUCTORS
|
2019-01-06 21:56:56 +00:00
|
|
|
UnrollVisitor() { init(false, ""); }
|
|
|
|
virtual ~UnrollVisitor() {
|
|
|
|
V3Stats::addStatSum("Optimizations, Unrolled Loops", m_statLoops);
|
|
|
|
V3Stats::addStatSum("Optimizations, Unrolled Iterations", m_statIters);
|
|
|
|
}
|
2019-09-12 11:22:22 +00:00
|
|
|
// METHODS
|
2019-01-06 21:56:56 +00:00
|
|
|
void init(bool generate, const string& beginName) {
|
2019-05-19 20:13:13 +00:00
|
|
|
m_forVarp = NULL;
|
|
|
|
m_forVscp = NULL;
|
2018-06-14 22:59:24 +00:00
|
|
|
m_varValuep = NULL;
|
2019-05-19 20:13:13 +00:00
|
|
|
m_ignoreIncp = NULL;
|
|
|
|
m_varModeCheck = false;
|
|
|
|
m_varModeReplace = false;
|
2018-06-14 22:59:24 +00:00
|
|
|
m_varAssignHit = false;
|
2019-05-19 20:13:13 +00:00
|
|
|
m_generate = generate;
|
|
|
|
m_beginName = beginName;
|
2006-08-26 11:35:28 +00:00
|
|
|
}
|
2019-01-06 21:56:56 +00:00
|
|
|
void process(AstNode* nodep, bool generate, const string& beginName) {
|
|
|
|
init(generate, beginName);
|
|
|
|
iterate(nodep);
|
2006-08-26 11:35:28 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
// Unroll class functions
|
|
|
|
|
2020-04-15 11:58:34 +00:00
|
|
|
UnrollStateful::UnrollStateful()
|
|
|
|
: m_unrollerp(new UnrollVisitor) {}
|
2019-01-06 21:56:56 +00:00
|
|
|
UnrollStateful::~UnrollStateful() { delete m_unrollerp; }
|
|
|
|
|
|
|
|
void UnrollStateful::unrollGen(AstNodeFor* nodep, const string& beginName) {
|
2020-04-15 11:58:34 +00:00
|
|
|
UINFO(5, __FUNCTION__ << ": " << endl);
|
2019-01-06 21:56:56 +00:00
|
|
|
m_unrollerp->process(nodep, true, beginName);
|
|
|
|
}
|
|
|
|
|
2020-04-15 11:58:34 +00:00
|
|
|
void UnrollStateful::unrollAll(AstNetlist* nodep) { m_unrollerp->process(nodep, false, ""); }
|
2019-01-06 21:56:56 +00:00
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
void V3Unroll::unrollAll(AstNetlist* nodep) {
|
2020-04-15 11:58:34 +00:00
|
|
|
UINFO(2, __FUNCTION__ << ": " << endl);
|
2018-03-10 17:57:50 +00:00
|
|
|
{
|
2019-01-06 21:56:56 +00:00
|
|
|
UnrollStateful unroller;
|
|
|
|
unroller.unrollAll(nodep);
|
2018-03-10 17:57:50 +00:00
|
|
|
} // Destruct before checking
|
2017-09-18 02:52:57 +00:00
|
|
|
V3Global::dumpCheckGlobalTree("unroll", 0, v3Global.opt.dumpTreeLevel(__FILE__) >= 3);
|
2006-08-26 11:35:28 +00:00
|
|
|
}
|