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
|
|
|
//
|
|
|
|
|
//*************************************************************************
|
|
|
|
|
//
|
2020-03-21 16:24:24 +01: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 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-12-18 20:20:45 +01:00
|
|
|
#include "config_build.h"
|
|
|
|
|
#include "verilatedos.h"
|
2006-08-26 13:35:28 +02:00
|
|
|
|
|
|
|
|
#include "V3Global.h"
|
|
|
|
|
#include "V3Branch.h"
|
|
|
|
|
#include "V3Ast.h"
|
|
|
|
|
|
2018-10-14 19:43:24 +02:00
|
|
|
#include <map>
|
|
|
|
|
|
2006-08-26 13:35:28 +02:00
|
|
|
//######################################################################
|
|
|
|
|
// Branch state, as a visitor of each AstNode
|
|
|
|
|
|
|
|
|
|
class BranchVisitor : public AstNVisitor {
|
|
|
|
|
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
|
2020-04-14 04:51:35 +02:00
|
|
|
AstUser1InUse m_inuser1;
|
2014-11-27 16:52:38 +01:00
|
|
|
|
|
|
|
|
// TYPES
|
2018-02-02 03:24:41 +01:00
|
|
|
typedef std::vector<AstCFunc*> CFuncVec;
|
2014-11-27 16:52:38 +01:00
|
|
|
|
2006-08-26 13:35:28 +02:00
|
|
|
// STATE
|
2020-04-14 04:51:35 +02:00
|
|
|
int m_likely; // Excuses for branch likely taken
|
|
|
|
|
int m_unlikely; // Excuses for branch likely not taken
|
|
|
|
|
CFuncVec m_cfuncsp; // List of all tasks
|
2006-08-26 13:35:28 +02:00
|
|
|
|
|
|
|
|
// METHODS
|
2018-05-14 12:50:47 +02:00
|
|
|
VL_DEBUG_FUNC; // Declare debug()
|
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
|
2020-01-21 23:35:56 +01:00
|
|
|
virtual void visit(AstNodeIf* nodep) VL_OVERRIDE {
|
2020-04-14 04:51:35 +02:00
|
|
|
UINFO(4, " IF: " << nodep << endl);
|
2019-05-19 22:13:13 +02:00
|
|
|
int lastLikely = m_likely;
|
|
|
|
|
int lastUnlikely = m_unlikely;
|
|
|
|
|
{
|
|
|
|
|
// Do if
|
|
|
|
|
reset();
|
2018-05-11 02:55:37 +02:00
|
|
|
iterateAndNextNull(nodep->ifsp());
|
2019-05-19 22:13:13 +02:00
|
|
|
int ifLikely = m_likely;
|
|
|
|
|
int ifUnlikely = m_unlikely;
|
|
|
|
|
// Do else
|
|
|
|
|
reset();
|
2018-05-11 02:55:37 +02:00
|
|
|
iterateAndNextNull(nodep->elsesp());
|
2019-05-19 22:13:13 +02:00
|
|
|
int elseLikely = m_likely;
|
|
|
|
|
int elseUnlikely = m_unlikely;
|
|
|
|
|
// Compute
|
|
|
|
|
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
|
|
|
|
|
}
|
|
|
|
|
m_likely = lastLikely;
|
|
|
|
|
m_unlikely = lastUnlikely;
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
2020-03-07 18:52:11 +01:00
|
|
|
virtual void visit(AstNodeCCall* nodep) VL_OVERRIDE {
|
2019-05-19 22:13:13 +02:00
|
|
|
checkUnlikely(nodep);
|
|
|
|
|
nodep->funcp()->user1Inc();
|
2018-05-11 02:55:37 +02:00
|
|
|
iterateChildren(nodep);
|
2014-11-27 16:52:38 +01:00
|
|
|
}
|
2020-01-21 23:35:56 +01:00
|
|
|
virtual void visit(AstCFunc* nodep) VL_OVERRIDE {
|
2019-05-19 22:13:13 +02:00
|
|
|
checkUnlikely(nodep);
|
|
|
|
|
m_cfuncsp.push_back(nodep);
|
2018-05-11 02:55:37 +02:00
|
|
|
iterateChildren(nodep);
|
2014-11-27 16:52:38 +01:00
|
|
|
}
|
2020-01-21 23:35:56 +01:00
|
|
|
virtual void visit(AstNode* nodep) VL_OVERRIDE {
|
2019-05-19 22:13:13 +02:00
|
|
|
checkUnlikely(nodep);
|
2018-05-11 02:55:37 +02:00
|
|
|
iterateChildren(nodep);
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
|
|
|
|
|
2014-11-27 16:52:38 +01:00
|
|
|
// METHODS
|
|
|
|
|
void calc_tasks() {
|
2020-04-14 04:51:35 +02:00
|
|
|
for (CFuncVec::iterator it = m_cfuncsp.begin(); it != m_cfuncsp.end(); ++it) {
|
2019-05-19 22:13:13 +02:00
|
|
|
AstCFunc* nodep = *it;
|
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();
|
2018-05-11 02:55:37 +02:00
|
|
|
iterateChildren(nodep);
|
2019-05-19 22:13:13 +02:00
|
|
|
calc_tasks();
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|
|
|
|
|
virtual ~BranchVisitor() {}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
|
// 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);
|
|
|
|
|
BranchVisitor visitor(nodep);
|
2006-08-26 13:35:28 +02:00
|
|
|
}
|