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: Branch prediction
|
|
|
|
|
//
|
2019-11-08 04:33:59 +01:00
|
|
|
// Code available from: https://verilator.org
|
2006-08-26 13:35:28 +02: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
|
2006-08-26 13:35:28 +02:00
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
// BRANCH TRANSFORMATIONS:
|
2019-05-19 22:13:13 +02:00
|
|
|
// At each IF/(IF else).
|
|
|
|
|
// Count underneath $display/$stop statements.
|
|
|
|
|
// If more on if than else, this branch is unlikely, or vice-versa.
|
|
|
|
|
// At each FTASKREF,
|
|
|
|
|
// Count calls into the function
|
|
|
|
|
// Then, if FTASK is called only once, add inline attribute
|
2006-08-26 13:35:28 +02:00
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
2019-10-05 02:17:11 +02:00
|
|
|
|
2006-08-26 13:35:28 +02:00
|
|
|
#include "V3Branch.h"
|
2022-08-05 11:56:57 +02:00
|
|
|
|
2023-10-18 04:50:27 +02:00
|
|
|
#include "V3PchAstNoMT.h" // VL_MT_DISABLED_CODE_UNIT
|
|
|
|
|
|
2022-09-18 21:53:42 +02:00
|
|
|
VL_DEFINE_DEBUG_FUNCTIONS;
|
|
|
|
|
|
2006-08-26 13:35:28 +02:00
|
|
|
//######################################################################
|
|
|
|
|
// Branch state, as a visitor of each AstNode
|
|
|
|
|
|
2023-03-18 17:17:25 +01:00
|
|
|
class BranchVisitor final : public VNVisitorConst {
|
2006-08-26 13:35:28 +02:00
|
|
|
private:
|
2014-11-27 16:52:38 +01:00
|
|
|
// NODE STATE
|
|
|
|
|
// Entire netlist:
|
2019-05-19 22:13:13 +02:00
|
|
|
// AstFTask::user1() -> int. Number of references
|
2022-01-02 19:56:40 +01:00
|
|
|
const VNUser1InUse m_inuser1;
|
2014-11-27 16:52:38 +01:00
|
|
|
|
2023-05-27 15:43:23 +02:00
|
|
|
// STATE - across all visitors
|
|
|
|
|
std::vector<AstCFunc*> m_cfuncsp; // List of all tasks
|
|
|
|
|
|
|
|
|
|
// STATE - for current visit position (use VL_RESTORER)
|
2021-07-25 19:38:27 +02:00
|
|
|
int m_likely = false; // Excuses for branch likely taken
|
|
|
|
|
int m_unlikely = false; // Excuses for branch likely not taken
|
2006-08-26 13:35:28 +02:00
|
|
|
|
|
|
|
|
// METHODS
|
2009-01-21 22:56:50 +01:00
|
|
|
|
2006-08-26 13:35:28 +02:00
|
|
|
void reset() {
|
2019-05-19 22:13:13 +02:00
|
|
|
m_likely = false;
|
|
|
|
|
m_unlikely = false;
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
2014-11-27 16:52:38 +01:00
|
|
|
void checkUnlikely(AstNode* nodep) {
|
2019-05-19 22:13:13 +02:00
|
|
|
if (nodep->isUnlikely()) {
|
2020-04-14 04:51:35 +02:00
|
|
|
UINFO(4, " UNLIKELY: " << nodep << endl);
|
2019-05-19 22:13:13 +02:00
|
|
|
m_unlikely++;
|
|
|
|
|
}
|
2014-11-27 16:52:38 +01:00
|
|
|
}
|
2006-08-26 13:35:28 +02:00
|
|
|
|
|
|
|
|
// VISITORS
|
2022-09-16 12:22:11 +02:00
|
|
|
void visit(AstNodeIf* nodep) override {
|
2020-04-14 04:51:35 +02:00
|
|
|
UINFO(4, " IF: " << nodep << endl);
|
2020-08-25 03:10:43 +02:00
|
|
|
VL_RESTORER(m_likely);
|
|
|
|
|
VL_RESTORER(m_unlikely);
|
2019-05-19 22:13:13 +02:00
|
|
|
{
|
|
|
|
|
// Do if
|
|
|
|
|
reset();
|
2023-03-18 17:17:25 +01:00
|
|
|
iterateAndNextConstNull(nodep->thensp());
|
2021-06-21 00:32:57 +02:00
|
|
|
const int ifLikely = m_likely;
|
|
|
|
|
const int ifUnlikely = m_unlikely;
|
2019-05-19 22:13:13 +02:00
|
|
|
// Do else
|
|
|
|
|
reset();
|
2023-03-18 17:17:25 +01:00
|
|
|
iterateAndNextConstNull(nodep->elsesp());
|
2021-06-21 00:32:57 +02:00
|
|
|
const int elseLikely = m_likely;
|
|
|
|
|
const int elseUnlikely = m_unlikely;
|
2019-05-19 22:13:13 +02:00
|
|
|
// Compute
|
2021-06-21 00:32:57 +02:00
|
|
|
const int likeness = ifLikely - ifUnlikely - (elseLikely - elseUnlikely);
|
2019-10-05 13:54:14 +02:00
|
|
|
if (likeness > 0) {
|
|
|
|
|
nodep->branchPred(VBranchPred::BP_LIKELY);
|
|
|
|
|
} else if (likeness < 0) {
|
|
|
|
|
nodep->branchPred(VBranchPred::BP_UNLIKELY);
|
2019-05-19 22:13:13 +02:00
|
|
|
} // else leave unknown
|
|
|
|
|
}
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
2022-09-16 12:22:11 +02:00
|
|
|
void visit(AstNodeCCall* nodep) override {
|
2019-05-19 22:13:13 +02:00
|
|
|
checkUnlikely(nodep);
|
|
|
|
|
nodep->funcp()->user1Inc();
|
2023-03-18 17:17:25 +01:00
|
|
|
iterateChildrenConst(nodep);
|
2014-11-27 16:52:38 +01:00
|
|
|
}
|
2022-09-16 12:22:11 +02:00
|
|
|
void visit(AstCFunc* nodep) override {
|
2019-05-19 22:13:13 +02:00
|
|
|
checkUnlikely(nodep);
|
|
|
|
|
m_cfuncsp.push_back(nodep);
|
2023-03-18 17:17:25 +01:00
|
|
|
iterateChildrenConst(nodep);
|
2014-11-27 16:52:38 +01:00
|
|
|
}
|
2022-09-16 12:22:11 +02:00
|
|
|
void visit(AstNode* nodep) override {
|
2019-05-19 22:13:13 +02:00
|
|
|
checkUnlikely(nodep);
|
2023-03-18 17:17:25 +01:00
|
|
|
iterateChildrenConst(nodep);
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
|
|
|
|
|
2014-11-27 16:52:38 +01:00
|
|
|
// METHODS
|
|
|
|
|
void calc_tasks() {
|
2020-08-16 17:43:49 +02:00
|
|
|
for (AstCFunc* nodep : m_cfuncsp) {
|
2020-04-14 04:51:35 +02:00
|
|
|
if (!nodep->dontInline()) nodep->isInline(true);
|
2019-05-19 22:13:13 +02:00
|
|
|
}
|
2014-11-27 16:52:38 +01:00
|
|
|
}
|
|
|
|
|
|
2006-08-26 13:35:28 +02:00
|
|
|
public:
|
2019-09-12 13:22:22 +02:00
|
|
|
// CONSTRUCTORS
|
2015-10-04 04:33:06 +02:00
|
|
|
explicit BranchVisitor(AstNetlist* nodep) {
|
2019-05-19 22:13:13 +02:00
|
|
|
reset();
|
2023-03-18 17:17:25 +01:00
|
|
|
iterateChildrenConst(nodep);
|
2019-05-19 22:13:13 +02:00
|
|
|
calc_tasks();
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
2022-09-16 12:22:11 +02:00
|
|
|
~BranchVisitor() override = default;
|
2006-08-26 13:35:28 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
// Branch class functions
|
|
|
|
|
|
2018-10-15 00:39:33 +02:00
|
|
|
void V3Branch::branchAll(AstNetlist* nodep) {
|
2020-04-14 04:51:35 +02:00
|
|
|
UINFO(2, __FUNCTION__ << ": " << endl);
|
2021-11-26 16:52:36 +01:00
|
|
|
{ BranchVisitor{nodep}; }
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|