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
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
//
|
2023-01-01 16:18:39 +01:00
|
|
|
// Copyright 2003-2023 by Wilson Snyder. This program is free software; you
|
2020-03-21 16:24:24 +01:00
|
|
|
// 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 "V3SplitAs.h"
|
2022-08-05 11:56:57 +02:00
|
|
|
|
2007-01-18 01:51:26 +01:00
|
|
|
#include "V3Ast.h"
|
2022-08-05 11:56:57 +02:00
|
|
|
#include "V3Global.h"
|
|
|
|
|
#include "V3Stats.h"
|
2007-01-18 01:51:26 +01:00
|
|
|
|
2018-10-14 19:43:24 +02:00
|
|
|
#include <map>
|
|
|
|
|
|
2022-09-18 21:53:42 +02:00
|
|
|
VL_DEFINE_DEBUG_FUNCTIONS;
|
|
|
|
|
|
2007-01-18 01:51:26 +01:00
|
|
|
//######################################################################
|
|
|
|
|
// Find all split variables in a block
|
|
|
|
|
|
2023-05-27 15:45:49 +02:00
|
|
|
class SplitAsFindVisitor final : public VNVisitor {
|
2007-01-18 01:51:26 +01:00
|
|
|
private:
|
2023-05-27 15:45:49 +02:00
|
|
|
// STATE - across all visitors
|
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
|
2023-05-27 15:45:49 +02:00
|
|
|
void visit(AstVarRef* nodep) {
|
2020-11-07 16:37:55 +01:00
|
|
|
if (nodep->access().isWriteOrRW() && !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
|
|
|
}
|
2022-09-16 12:22:11 +02:00
|
|
|
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); }
|
2022-09-16 12:22:11 +02:00
|
|
|
~SplitAsFindVisitor() override = default;
|
2007-01-18 01:51:26 +01:00
|
|
|
// METHODS
|
|
|
|
|
AstVarScope* splitVscp() const { return m_splitVscp; }
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
// Remove nodes not containing proper references
|
|
|
|
|
|
2023-05-27 15:45:49 +02:00
|
|
|
class SplitAsCleanVisitor final : public VNVisitor {
|
2007-01-18 01:51:26 +01:00
|
|
|
private:
|
2023-05-27 15:45:49 +02:00
|
|
|
// STATE - across all visitors
|
|
|
|
|
const AstVarScope* const m_splitVscp; // Variable we want to split
|
2021-11-26 23:55:36 +01:00
|
|
|
const bool m_modeMatch; // Remove matching Vscp, else non-matching
|
2023-05-27 15:45:49 +02:00
|
|
|
// STATE - for current visit position (use VL_RESTORER)
|
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
|
2022-09-16 12:22:11 +02:00
|
|
|
void visit(AstVarRef* nodep) override {
|
2020-11-07 16:37:55 +01:00
|
|
|
if (nodep->access().isWriteOrRW()) {
|
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
|
|
|
}
|
2022-09-16 12:22:11 +02:00
|
|
|
void visit(AstNodeStmt* nodep) override {
|
2020-04-15 13:58:34 +02:00
|
|
|
UINFO(6, " CL STMT " << nodep << endl);
|
2021-06-21 00:32:57 +02:00
|
|
|
const bool oldKeep = m_keepStmt;
|
2019-05-19 22:13:13 +02:00
|
|
|
{
|
2023-05-27 15:45:49 +02:00
|
|
|
VL_RESTORER(m_matches);
|
2019-05-19 22:13:13 +02:00
|
|
|
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
|
|
|
}
|
2022-09-16 12:22:11 +02:00
|
|
|
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
|
|
|
}
|
2022-09-16 12:22:11 +02:00
|
|
|
~SplitAsCleanVisitor() override = default;
|
2007-01-18 01:51:26 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
// SplitAs class functions
|
|
|
|
|
|
2023-05-27 15:45:49 +02:00
|
|
|
class SplitAsVisitor final : public VNVisitor {
|
2007-01-18 01:51:26 +01:00
|
|
|
private:
|
|
|
|
|
// NODE STATE
|
2019-05-19 22:13:13 +02:00
|
|
|
// AstAlways::user() -> bool. True if already processed
|
2022-01-02 19:56:40 +01:00
|
|
|
const VNUser1InUse m_inuser1;
|
2007-01-18 01:51:26 +01:00
|
|
|
|
2023-05-27 15:45:49 +02:00
|
|
|
// STATE - across all visitors
|
2019-10-05 13:54:14 +02:00
|
|
|
VDouble0 m_statSplits; // Statistic tracking
|
2023-05-27 15:45:49 +02:00
|
|
|
// STATE - for current visit position (use VL_RESTORER)
|
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);
|
2022-11-27 14:31:22 +01:00
|
|
|
if (debug() >= 9) nodep->dumpTree("- in: ");
|
2019-05-19 22:13:13 +02:00
|
|
|
// Duplicate it and link in
|
2021-11-13 19:50:44 +01:00
|
|
|
AstAlways* const newp = nodep->cloneTree(false);
|
2019-05-19 22:13:13 +02:00
|
|
|
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
|
2021-11-26 23:55:36 +01:00
|
|
|
const SplitAsCleanVisitor visitor{nodep, m_splitVscp, false};
|
2022-11-27 14:31:22 +01:00
|
|
|
if (debug() >= 9) nodep->dumpTree("- 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
|
2021-11-26 23:55:36 +01:00
|
|
|
const SplitAsCleanVisitor visitor{newp, m_splitVscp, true};
|
2022-11-27 14:31:22 +01:00
|
|
|
if (debug() >= 9) newp->dumpTree("- out1: ");
|
2019-05-19 22:13:13 +02:00
|
|
|
}
|
2007-01-18 01:51:26 +01:00
|
|
|
}
|
|
|
|
|
|
2022-09-16 12:22:11 +02:00
|
|
|
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.
|
2023-05-27 15:45:49 +02:00
|
|
|
VL_RESTORER(m_splitVscp);
|
2021-11-13 19:50:44 +01:00
|
|
|
const AstVarScope* lastSplitVscp = nullptr;
|
2019-05-19 22:13:13 +02:00
|
|
|
while (!nodep->user1()) {
|
|
|
|
|
// Find any splittable variables
|
2021-11-26 23:55:36 +01:00
|
|
|
const 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
|
|
|
}
|
|
|
|
|
|
2022-10-12 11:19:21 +02:00
|
|
|
void visit(AstNodeExpr*) override {} // Accelerate
|
2022-09-16 12:22:11 +02:00
|
|
|
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
|
2020-10-08 01:39:59 +02:00
|
|
|
explicit SplitAsVisitor(AstNetlist* nodep) { iterate(nodep); }
|
2022-09-16 12:22:11 +02:00
|
|
|
~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);
|
2021-11-26 16:52:36 +01:00
|
|
|
{ SplitAsVisitor{nodep}; } // Destruct before checking
|
2023-05-04 00:04:10 +02:00
|
|
|
V3Global::dumpCheckGlobalTree("splitas", 0, dumpTreeLevel() >= 3);
|
2007-01-18 01:51:26 +01:00
|
|
|
}
|