2012-04-13 03:08:20 +02:00
|
|
|
// -*- mode: C++; c-file-style: "cc-mode" -*-
|
2006-08-26 13:35:28 +02:00
|
|
|
//*************************************************************************
|
2025-10-27 11:41:30 +01:00
|
|
|
// DESCRIPTION: Verilator: Post scheduling transformations
|
2006-08-26 13:35:28 +02:00
|
|
|
//
|
2019-11-08 04:33:59 +01:00
|
|
|
// Code available from: https://verilator.org
|
2006-08-26 13:35:28 +02:00
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
//
|
2025-01-01 14:30:25 +01:00
|
|
|
// Copyright 2003-2025 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
|
2006-08-26 13:35:28 +02:00
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
// V3Clock's Transformations:
|
2008-06-10 03:25:10 +02:00
|
|
|
//
|
2025-10-27 11:41:30 +01:00
|
|
|
// This pass is historic and does some arbitray post scheduling rewrites
|
2006-08-26 13:35:28 +02:00
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
2019-10-05 02:17:11 +02:00
|
|
|
|
2023-10-18 12:37:46 +02:00
|
|
|
#include "V3PchAstNoMT.h" // VL_MT_DISABLED_CODE_UNIT
|
|
|
|
|
|
2006-08-26 13:35:28 +02:00
|
|
|
#include "V3Clock.h"
|
2022-08-05 11:56:57 +02:00
|
|
|
|
2024-09-25 11:35:50 +02:00
|
|
|
#include "V3Const.h"
|
2025-11-06 14:31:40 +01:00
|
|
|
#include "V3Sched.h"
|
2006-08-26 13:35:28 +02:00
|
|
|
|
2022-09-18 21:53:42 +02:00
|
|
|
VL_DEFINE_DEBUG_FUNCTIONS;
|
|
|
|
|
|
2021-06-17 19:49:45 +02:00
|
|
|
//######################################################################
|
|
|
|
|
// Convert every WRITE AstVarRef to a READ ref
|
|
|
|
|
|
2022-01-02 19:56:40 +01:00
|
|
|
class ConvertWriteRefsToRead final : public VNVisitor {
|
2021-06-17 19:49:45 +02:00
|
|
|
// MEMBERS
|
2022-11-13 21:33:11 +01:00
|
|
|
AstNodeExpr* m_result = nullptr;
|
2021-06-17 19:49:45 +02:00
|
|
|
|
|
|
|
|
// CONSTRUCTORS
|
2022-11-13 21:33:11 +01:00
|
|
|
explicit ConvertWriteRefsToRead(AstNodeExpr* nodep) {
|
|
|
|
|
m_result = VN_AS(iterateSubtreeReturnEdits(nodep), NodeExpr);
|
2021-06-17 19:49:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// VISITORS
|
|
|
|
|
void visit(AstVarRef* nodep) override {
|
|
|
|
|
UASSERT_OBJ(!nodep->access().isRW(), nodep, "Cannot handle a READWRITE reference");
|
|
|
|
|
if (nodep->access().isWriteOnly()) {
|
|
|
|
|
nodep->replaceWith(
|
2022-11-19 20:45:33 +01:00
|
|
|
new AstVarRef{nodep->fileline(), nodep->varScopep(), VAccess::READ});
|
2024-03-23 23:12:43 +01:00
|
|
|
VL_DO_DANGLING(pushDeletep(nodep), nodep);
|
2021-06-17 19:49:45 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void visit(AstNode* nodep) override { iterateChildren(nodep); }
|
|
|
|
|
|
|
|
|
|
public:
|
2022-11-20 21:06:49 +01:00
|
|
|
static AstNodeExpr* main(AstNodeExpr* nodep) { return ConvertWriteRefsToRead{nodep}.m_result; }
|
2021-06-17 19:49:45 +02:00
|
|
|
};
|
|
|
|
|
|
2006-08-26 13:35:28 +02:00
|
|
|
//######################################################################
|
|
|
|
|
// Clock state, as a visitor of each AstNode
|
|
|
|
|
|
2022-01-02 19:56:40 +01:00
|
|
|
class ClockVisitor final : public VNVisitor {
|
2024-03-07 21:26:58 +01:00
|
|
|
// NODE STATE
|
2025-04-27 20:17:24 +02:00
|
|
|
|
2006-08-26 13:35:28 +02:00
|
|
|
// STATE
|
2025-11-06 14:31:40 +01:00
|
|
|
AstCFunc* m_sampleCFuncp = nullptr; // The CFunc to populate with sampled value assignments
|
2025-10-27 11:41:30 +01:00
|
|
|
|
2006-08-26 13:35:28 +02:00
|
|
|
// VISITORS
|
2022-09-16 12:22:11 +02:00
|
|
|
void visit(AstCoverToggle* nodep) override {
|
2025-08-02 19:44:40 +02:00
|
|
|
// UINFOTREE(1, nodep, "", "ct");
|
2020-04-15 13:58:34 +02:00
|
|
|
// COVERTOGGLE(INC, ORIG, CHANGE) ->
|
2019-05-19 22:13:13 +02:00
|
|
|
// IF(ORIG ^ CHANGE) { INC; CHANGE = ORIG; }
|
2025-08-04 14:29:56 +02:00
|
|
|
AstCoverInc* const incp = nodep->incp()->unlinkFrBack();
|
2022-11-13 21:33:11 +01:00
|
|
|
AstNodeExpr* const origp = nodep->origp()->unlinkFrBack();
|
|
|
|
|
AstNodeExpr* const changeWrp = nodep->changep()->unlinkFrBack();
|
|
|
|
|
AstNodeExpr* const changeRdp = ConvertWriteRefsToRead::main(changeWrp->cloneTree(false));
|
2024-02-08 02:11:27 +01:00
|
|
|
AstNodeExpr* comparedp = nullptr;
|
2025-08-04 14:29:56 +02:00
|
|
|
incp->toggleExprp(origp->cloneTree(false));
|
|
|
|
|
incp->toggleCovExprp(changeRdp->cloneTree(false));
|
2024-02-08 02:11:27 +01:00
|
|
|
// Xor will optimize better than Eq, when CoverToggle has bit selects,
|
|
|
|
|
// but can only use Xor with non-opaque types
|
|
|
|
|
if (const AstBasicDType* const bdtypep
|
|
|
|
|
= VN_CAST(origp->dtypep()->skipRefp(), BasicDType)) {
|
|
|
|
|
if (!bdtypep->isOpaque()) comparedp = new AstXor{nodep->fileline(), origp, changeRdp};
|
|
|
|
|
}
|
2025-08-01 13:24:18 +02:00
|
|
|
if (!comparedp) comparedp = AstNeq::newTyped(nodep->fileline(), origp, changeRdp);
|
2024-02-08 02:11:27 +01:00
|
|
|
AstIf* const newp = new AstIf{nodep->fileline(), comparedp, incp};
|
2019-05-19 22:13:13 +02:00
|
|
|
// We could add another IF to detect posedges, and only increment if so.
|
2019-09-09 13:50:21 +02:00
|
|
|
// It's another whole branch though versus a potential memory miss.
|
2019-05-19 22:13:13 +02:00
|
|
|
// We'll go with the miss.
|
2022-11-19 20:45:33 +01:00
|
|
|
newp->addThensp(new AstAssign{nodep->fileline(), changeWrp, origp->cloneTree(false)});
|
2020-04-15 13:58:34 +02:00
|
|
|
nodep->replaceWith(newp);
|
|
|
|
|
VL_DO_DANGLING(nodep->deleteTree(), nodep);
|
2008-12-12 21:34:02 +01:00
|
|
|
}
|
2022-09-16 12:22:11 +02:00
|
|
|
void visit(AstSenTree* nodep) override {
|
2025-10-27 11:41:30 +01:00
|
|
|
pushDeletep(nodep->unlinkFrBack()); // No longer needed
|
2018-07-23 02:54:28 +02:00
|
|
|
}
|
2006-08-26 13:35:28 +02:00
|
|
|
|
2024-07-10 00:31:58 +02:00
|
|
|
//========== Move sampled assignments
|
|
|
|
|
void visit(AstVarScope* nodep) override {
|
2025-10-21 17:37:32 +02:00
|
|
|
AstVar* const varp = nodep->varp();
|
2025-11-06 14:31:40 +01:00
|
|
|
if (!varp->valuep()) return;
|
|
|
|
|
if (!VString::startsWith(varp->name(), "__Vsampled")) return;
|
|
|
|
|
|
|
|
|
|
// Create the containing function on first encounter
|
|
|
|
|
if (!m_sampleCFuncp) {
|
|
|
|
|
m_sampleCFuncp = V3Sched::util::makeSubFunction(v3Global.rootp(), "_sample", false);
|
2022-08-29 14:39:41 +02:00
|
|
|
}
|
2025-11-06 14:31:40 +01:00
|
|
|
|
|
|
|
|
FileLine* const flp = nodep->fileline();
|
|
|
|
|
AstNodeExpr* const rhsp = VN_AS(varp->valuep()->unlinkFrBack(), NodeExpr);
|
|
|
|
|
AstVarRef* const lhsp = new AstVarRef{flp, nodep, VAccess::WRITE};
|
|
|
|
|
m_sampleCFuncp->addStmtsp(new AstAssign{flp, lhsp, rhsp});
|
|
|
|
|
varp->direction(VDirection::NONE); // Restore defaults
|
|
|
|
|
varp->primaryIO(false);
|
2022-08-29 14:39:41 +02:00
|
|
|
}
|
|
|
|
|
|
2006-08-26 13:35:28 +02:00
|
|
|
//--------------------
|
2022-09-16 12:22:11 +02:00
|
|
|
void visit(AstNode* nodep) override { iterateChildren(nodep); }
|
2006-08-26 13:35:28 +02:00
|
|
|
|
|
|
|
|
public:
|
2019-09-12 13:22:22 +02:00
|
|
|
// CONSTRUCTORS
|
2025-11-06 14:31:40 +01:00
|
|
|
explicit ClockVisitor(AstNetlist* netlistp) {
|
2022-08-29 14:39:41 +02:00
|
|
|
iterate(netlistp);
|
2025-11-06 14:31:40 +01:00
|
|
|
// If we need a sample function, call it at the begining of eval
|
|
|
|
|
if (m_sampleCFuncp) {
|
|
|
|
|
V3Sched::util::splitCheck(m_sampleCFuncp);
|
|
|
|
|
AstCCall* const callp = new AstCCall{m_sampleCFuncp->fileline(), m_sampleCFuncp};
|
|
|
|
|
callp->dtypeSetVoid();
|
|
|
|
|
netlistp->evalp()->stmtsp()->addHereThisAsNext(callp->makeStmt());
|
|
|
|
|
}
|
2022-08-29 14:39:41 +02:00
|
|
|
}
|
2022-09-16 12:22:11 +02:00
|
|
|
~ClockVisitor() override = default;
|
2006-08-26 13:35:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
// Clock class functions
|
|
|
|
|
|
|
|
|
|
void V3Clock::clockAll(AstNetlist* nodep) {
|
2025-05-23 02:29:32 +02:00
|
|
|
UINFO(2, __FUNCTION__ << ":");
|
2021-11-26 16:52:36 +01:00
|
|
|
{ ClockVisitor{nodep}; } // Destruct before checking
|
2024-01-09 16:35:13 +01:00
|
|
|
V3Global::dumpCheckGlobalTree("clock", 0, dumpTreeEitherLevel() >= 3);
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|