2012-04-13 01:08:20 +00:00
|
|
|
// -*- mode: C++; c-file-style: "cc-mode" -*-
|
2006-08-26 11:35:28 +00:00
|
|
|
//*************************************************************************
|
|
|
|
// DESCRIPTION: Verilator: Branch prediction
|
|
|
|
//
|
2008-04-25 12:14:27 +00:00
|
|
|
// Code available from: http://www.veripool.org/verilator
|
2006-08-26 11:35:28 +00:00
|
|
|
//
|
|
|
|
//*************************************************************************
|
|
|
|
//
|
2019-01-04 00:17:22 +00:00
|
|
|
// Copyright 2003-2019 by Wilson Snyder. This program is free software; you can
|
2006-08-26 11:35:28 +00:00
|
|
|
// redistribute it and/or modify it under the terms of either the GNU
|
2009-05-04 21:07:57 +00:00
|
|
|
// Lesser General Public License Version 3 or the Perl Artistic License
|
|
|
|
// Version 2.0.
|
2006-08-26 11:35:28 +00:00
|
|
|
//
|
|
|
|
// Verilator is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
//*************************************************************************
|
|
|
|
// BRANCH TRANSFORMATIONS:
|
2019-05-19 20:13:13 +00: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-05 00:17:11 +00: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 "V3Global.h"
|
|
|
|
#include "V3Branch.h"
|
|
|
|
#include "V3Ast.h"
|
|
|
|
|
2018-10-14 17:43:24 +00:00
|
|
|
#include <cstdarg>
|
|
|
|
#include <map>
|
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
//######################################################################
|
|
|
|
// Branch state, as a visitor of each AstNode
|
|
|
|
|
|
|
|
class BranchVisitor : public AstNVisitor {
|
|
|
|
private:
|
2014-11-27 15:52:38 +00:00
|
|
|
// NODE STATE
|
|
|
|
// Entire netlist:
|
2019-05-19 20:13:13 +00:00
|
|
|
// AstFTask::user1() -> int. Number of references
|
|
|
|
AstUser1InUse m_inuser1;
|
2014-11-27 15:52:38 +00:00
|
|
|
|
|
|
|
// TYPES
|
2018-02-02 02:24:41 +00:00
|
|
|
typedef std::vector<AstCFunc*> CFuncVec;
|
2014-11-27 15:52:38 +00:00
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
// STATE
|
2019-05-19 20:13:13 +00: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 11:35:28 +00:00
|
|
|
|
|
|
|
// METHODS
|
2018-05-14 10:50:47 +00:00
|
|
|
VL_DEBUG_FUNC; // Declare debug()
|
2009-01-21 21:56:50 +00:00
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
void reset() {
|
2019-05-19 20:13:13 +00:00
|
|
|
m_likely = false;
|
|
|
|
m_unlikely = false;
|
2006-08-26 11:35:28 +00:00
|
|
|
}
|
2014-11-27 15:52:38 +00:00
|
|
|
void checkUnlikely(AstNode* nodep) {
|
2019-05-19 20:13:13 +00:00
|
|
|
if (nodep->isUnlikely()) {
|
|
|
|
UINFO(4," UNLIKELY: "<<nodep<<endl);
|
|
|
|
m_unlikely++;
|
|
|
|
}
|
2014-11-27 15:52:38 +00:00
|
|
|
}
|
2006-08-26 11:35:28 +00:00
|
|
|
|
|
|
|
// VISITORS
|
2016-11-27 13:11:38 +00:00
|
|
|
virtual void visit(AstNodeIf* nodep) {
|
2019-05-19 20:13:13 +00:00
|
|
|
UINFO(4," IF: "<<nodep<<endl);
|
|
|
|
int lastLikely = m_likely;
|
|
|
|
int lastUnlikely = m_unlikely;
|
|
|
|
{
|
|
|
|
// Do if
|
|
|
|
reset();
|
2018-05-11 00:55:37 +00:00
|
|
|
iterateAndNextNull(nodep->ifsp());
|
2019-05-19 20:13:13 +00:00
|
|
|
int ifLikely = m_likely;
|
|
|
|
int ifUnlikely = m_unlikely;
|
|
|
|
// Do else
|
|
|
|
reset();
|
2018-05-11 00:55:37 +00:00
|
|
|
iterateAndNextNull(nodep->elsesp());
|
2019-05-19 20:13:13 +00:00
|
|
|
int elseLikely = m_likely;
|
|
|
|
int elseUnlikely = m_unlikely;
|
|
|
|
// Compute
|
|
|
|
int likeness = ifLikely - ifUnlikely - (elseLikely - elseUnlikely);
|
2019-10-05 11:54:14 +00:00
|
|
|
if (likeness > 0) {
|
|
|
|
nodep->branchPred(VBranchPred::BP_LIKELY);
|
|
|
|
} else if (likeness < 0) {
|
|
|
|
nodep->branchPred(VBranchPred::BP_UNLIKELY);
|
2019-05-19 20:13:13 +00:00
|
|
|
} // else leave unknown
|
|
|
|
}
|
|
|
|
m_likely = lastLikely;
|
|
|
|
m_unlikely = lastUnlikely;
|
2006-08-26 11:35:28 +00:00
|
|
|
}
|
2016-11-27 13:11:38 +00:00
|
|
|
virtual void visit(AstCCall* nodep) {
|
2019-05-19 20:13:13 +00:00
|
|
|
checkUnlikely(nodep);
|
|
|
|
nodep->funcp()->user1Inc();
|
2018-05-11 00:55:37 +00:00
|
|
|
iterateChildren(nodep);
|
2014-11-27 15:52:38 +00:00
|
|
|
}
|
2016-11-27 13:11:38 +00:00
|
|
|
virtual void visit(AstCFunc* nodep) {
|
2019-05-19 20:13:13 +00:00
|
|
|
checkUnlikely(nodep);
|
|
|
|
m_cfuncsp.push_back(nodep);
|
2018-05-11 00:55:37 +00:00
|
|
|
iterateChildren(nodep);
|
2014-11-27 15:52:38 +00:00
|
|
|
}
|
2016-11-27 13:11:38 +00:00
|
|
|
virtual void visit(AstNode* nodep) {
|
2019-05-19 20:13:13 +00:00
|
|
|
checkUnlikely(nodep);
|
2018-05-11 00:55:37 +00:00
|
|
|
iterateChildren(nodep);
|
2006-08-26 11:35:28 +00:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:52:38 +00:00
|
|
|
// METHODS
|
|
|
|
void calc_tasks() {
|
2019-05-19 20:13:13 +00:00
|
|
|
for (CFuncVec::iterator it=m_cfuncsp.begin(); it!=m_cfuncsp.end(); ++it) {
|
|
|
|
AstCFunc* nodep = *it;
|
|
|
|
if (!nodep->dontInline()) {
|
|
|
|
nodep->isInline(true);
|
|
|
|
}
|
|
|
|
}
|
2014-11-27 15:52:38 +00:00
|
|
|
}
|
|
|
|
|
2006-08-26 11:35:28 +00:00
|
|
|
public:
|
2019-09-12 11:22:22 +00:00
|
|
|
// CONSTRUCTORS
|
2015-10-04 02:33:06 +00:00
|
|
|
explicit BranchVisitor(AstNetlist* nodep) {
|
2019-05-19 20:13:13 +00:00
|
|
|
reset();
|
2018-05-11 00:55:37 +00:00
|
|
|
iterateChildren(nodep);
|
2019-05-19 20:13:13 +00:00
|
|
|
calc_tasks();
|
2006-08-26 11:35:28 +00:00
|
|
|
}
|
|
|
|
virtual ~BranchVisitor() {}
|
|
|
|
};
|
|
|
|
|
|
|
|
//######################################################################
|
|
|
|
// Branch class functions
|
|
|
|
|
2018-10-14 22:39:33 +00:00
|
|
|
void V3Branch::branchAll(AstNetlist* nodep) {
|
2006-08-26 11:35:28 +00:00
|
|
|
UINFO(2,__FUNCTION__<<": "<<endl);
|
2018-10-14 22:39:33 +00:00
|
|
|
BranchVisitor visitor (nodep);
|
2006-08-26 11:35:28 +00:00
|
|
|
}
|