2012-04-12 21:08:20 -04:00
|
|
|
// -*- mode: C++; c-file-style: "cc-mode" -*-
|
2006-08-26 11:35:28 +00:00
|
|
|
//*************************************************************************
|
|
|
|
|
// DESCRIPTION: Verilator: Branch prediction
|
|
|
|
|
//
|
2019-11-07 22:33:59 -05:00
|
|
|
// Code available from: https://verilator.org
|
2006-08-26 11:35:28 +00:00
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
//
|
2023-01-01 10:18:39 -05:00
|
|
|
// Copyright 2003-2023 by Wilson Snyder. This program is free software; you
|
2020-03-21 11:24:24 -04:00
|
|
|
// can redistribute it and/or modify it under the terms of either the GNU
|
2009-05-04 17:07:57 -04:00
|
|
|
// Lesser General Public License Version 3 or the Perl Artistic License
|
|
|
|
|
// Version 2.0.
|
2020-03-21 11:24:24 -04:00
|
|
|
// SPDX-License-Identifier: LGPL-3.0-only OR Artistic-2.0
|
2006-08-26 11:35:28 +00:00
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
// BRANCH TRANSFORMATIONS:
|
2019-05-19 16:13:13 -04: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 11:35:28 +00:00
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
2019-10-04 20:17:11 -04: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 "V3Branch.h"
|
2022-08-05 10:56:57 +01:00
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
#include "V3Ast.h"
|
2022-08-05 10:56:57 +01:00
|
|
|
#include "V3Global.h"
|
2006-08-26 11:35:28 +00:00
|
|
|
|
2018-10-14 13:43:24 -04:00
|
|
|
#include <map>
|
|
|
|
|
|
2022-09-18 20:53:42 +01:00
|
|
|
VL_DEFINE_DEBUG_FUNCTIONS;
|
|
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
//######################################################################
|
|
|
|
|
// Branch state, as a visitor of each AstNode
|
|
|
|
|
|
2023-03-18 12:17:25 -04:00
|
|
|
class BranchVisitor final : public VNVisitorConst {
|
2006-08-26 11:35:28 +00:00
|
|
|
private:
|
2014-11-27 10:52:38 -05:00
|
|
|
// NODE STATE
|
|
|
|
|
// Entire netlist:
|
2019-05-19 16:13:13 -04:00
|
|
|
// AstFTask::user1() -> int. Number of references
|
2022-01-02 13:56:40 -05:00
|
|
|
const VNUser1InUse m_inuser1;
|
2014-11-27 10:52:38 -05:00
|
|
|
|
2023-05-27 09:43:23 -04: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 13:38:27 -04:00
|
|
|
int m_likely = false; // Excuses for branch likely taken
|
|
|
|
|
int m_unlikely = false; // Excuses for branch likely not taken
|
2006-08-26 11:35:28 +00:00
|
|
|
|
|
|
|
|
// METHODS
|
2009-01-21 16:56:50 -05:00
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
void reset() {
|
2019-05-19 16:13:13 -04:00
|
|
|
m_likely = false;
|
|
|
|
|
m_unlikely = false;
|
2006-08-26 11:35:28 +00:00
|
|
|
}
|
2014-11-27 10:52:38 -05:00
|
|
|
void checkUnlikely(AstNode* nodep) {
|
2019-05-19 16:13:13 -04:00
|
|
|
if (nodep->isUnlikely()) {
|
2020-04-13 22:51:35 -04:00
|
|
|
UINFO(4, " UNLIKELY: " << nodep << endl);
|
2019-05-19 16:13:13 -04:00
|
|
|
m_unlikely++;
|
|
|
|
|
}
|
2014-11-27 10:52:38 -05:00
|
|
|
}
|
2006-08-26 11:35:28 +00:00
|
|
|
|
|
|
|
|
// VISITORS
|
2022-09-16 11:22:11 +01:00
|
|
|
void visit(AstNodeIf* nodep) override {
|
2020-04-13 22:51:35 -04:00
|
|
|
UINFO(4, " IF: " << nodep << endl);
|
2020-08-24 21:10:43 -04:00
|
|
|
VL_RESTORER(m_likely);
|
|
|
|
|
VL_RESTORER(m_unlikely);
|
2019-05-19 16:13:13 -04:00
|
|
|
{
|
|
|
|
|
// Do if
|
|
|
|
|
reset();
|
2023-03-18 12:17:25 -04:00
|
|
|
iterateAndNextConstNull(nodep->thensp());
|
2021-06-20 18:32:57 -04:00
|
|
|
const int ifLikely = m_likely;
|
|
|
|
|
const int ifUnlikely = m_unlikely;
|
2019-05-19 16:13:13 -04:00
|
|
|
// Do else
|
|
|
|
|
reset();
|
2023-03-18 12:17:25 -04:00
|
|
|
iterateAndNextConstNull(nodep->elsesp());
|
2021-06-20 18:32:57 -04:00
|
|
|
const int elseLikely = m_likely;
|
|
|
|
|
const int elseUnlikely = m_unlikely;
|
2019-05-19 16:13:13 -04:00
|
|
|
// Compute
|
2021-06-20 18:32:57 -04:00
|
|
|
const int likeness = ifLikely - ifUnlikely - (elseLikely - elseUnlikely);
|
2019-10-05 07:54:14 -04:00
|
|
|
if (likeness > 0) {
|
|
|
|
|
nodep->branchPred(VBranchPred::BP_LIKELY);
|
|
|
|
|
} else if (likeness < 0) {
|
|
|
|
|
nodep->branchPred(VBranchPred::BP_UNLIKELY);
|
2019-05-19 16:13:13 -04:00
|
|
|
} // else leave unknown
|
|
|
|
|
}
|
2006-08-26 11:35:28 +00:00
|
|
|
}
|
2022-09-16 11:22:11 +01:00
|
|
|
void visit(AstNodeCCall* nodep) override {
|
2019-05-19 16:13:13 -04:00
|
|
|
checkUnlikely(nodep);
|
|
|
|
|
nodep->funcp()->user1Inc();
|
2023-03-18 12:17:25 -04:00
|
|
|
iterateChildrenConst(nodep);
|
2014-11-27 10:52:38 -05:00
|
|
|
}
|
2022-09-16 11:22:11 +01:00
|
|
|
void visit(AstCFunc* nodep) override {
|
2019-05-19 16:13:13 -04:00
|
|
|
checkUnlikely(nodep);
|
|
|
|
|
m_cfuncsp.push_back(nodep);
|
2023-03-18 12:17:25 -04:00
|
|
|
iterateChildrenConst(nodep);
|
2014-11-27 10:52:38 -05:00
|
|
|
}
|
2022-09-16 11:22:11 +01:00
|
|
|
void visit(AstNode* nodep) override {
|
2019-05-19 16:13:13 -04:00
|
|
|
checkUnlikely(nodep);
|
2023-03-18 12:17:25 -04:00
|
|
|
iterateChildrenConst(nodep);
|
2006-08-26 11:35:28 +00:00
|
|
|
}
|
|
|
|
|
|
2014-11-27 10:52:38 -05:00
|
|
|
// METHODS
|
|
|
|
|
void calc_tasks() {
|
2020-08-16 11:43:49 -04:00
|
|
|
for (AstCFunc* nodep : m_cfuncsp) {
|
2020-04-13 22:51:35 -04:00
|
|
|
if (!nodep->dontInline()) nodep->isInline(true);
|
2019-05-19 16:13:13 -04:00
|
|
|
}
|
2014-11-27 10:52:38 -05:00
|
|
|
}
|
|
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
public:
|
2019-09-12 07:22:22 -04:00
|
|
|
// CONSTRUCTORS
|
2015-10-03 22:33:06 -04:00
|
|
|
explicit BranchVisitor(AstNetlist* nodep) {
|
2019-05-19 16:13:13 -04:00
|
|
|
reset();
|
2023-03-18 12:17:25 -04:00
|
|
|
iterateChildrenConst(nodep);
|
2019-05-19 16:13:13 -04:00
|
|
|
calc_tasks();
|
2006-08-26 11:35:28 +00:00
|
|
|
}
|
2022-09-16 11:22:11 +01:00
|
|
|
~BranchVisitor() override = default;
|
2006-08-26 11:35:28 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
// Branch class functions
|
|
|
|
|
|
2018-10-14 18:39:33 -04:00
|
|
|
void V3Branch::branchAll(AstNetlist* nodep) {
|
2020-04-13 22:51:35 -04:00
|
|
|
UINFO(2, __FUNCTION__ << ": " << endl);
|
2021-11-26 10:52:36 -05:00
|
|
|
{ BranchVisitor{nodep}; }
|
2006-08-26 11:35:28 +00:00
|
|
|
}
|