gem5 [DEVELOP-FOR-25.1]
Loading...
Searching...
No Matches
gem5::branch_prediction::BPredUnit Class Reference

Basically a wrapper class to hold both the branch predictor and the BTB. More...

#include <bpred_unit.hh>

Inheritance diagram for gem5::branch_prediction::BPredUnit:
gem5::SimObject gem5::EventManager gem5::Serializable gem5::Drainable gem5::statistics::Group gem5::Named gem5::branch_prediction::GshareBP

Classes

struct  BPredUnitStats
 Statistics. More...
struct  PredictorHistory
 Branch Predictor Unit (BPU) history object PredictorHistory This class holds all information needed to manage the speculative state of a in-flight branch prediction. More...

Public Member Functions

 BPredUnit (const Params &p)
 Branch Predictor Unit (BPU) interface functions.
void regProbePoints () override
 Register probe points for this object.
void drainSanityCheck () const
 Perform sanity checks after a drain.
bool predict (const StaticInstPtr &inst, const InstSeqNum &seqNum, PCStateBase &pc, ThreadID tid)
 Predicts whether or not the instruction is a taken branch, and the target of the branch if it is taken.
void update (const InstSeqNum &done_sn, ThreadID tid)
 Tells the branch predictor to commit any updates until the given sequence number.
void squash (const InstSeqNum &squashed_sn, ThreadID tid)
 Squashes all outstanding updates until a given sequence number.
void squash (const InstSeqNum &squashed_sn, const PCStateBase &corr_target, bool actually_taken, ThreadID tid, bool from_commit=true)
 Squashes all outstanding updates until a given sequence number, and corrects that sn's update with the proper address and taken/not taken.
bool BTBValid (ThreadID tid, Addr pc)
 Looks up a given PC in the BTB to see if a matching entry exists.
const PCStateBaseBTBLookup (ThreadID tid, PCStateBase &pc)
 Looks up a given PC in the BTB to get the predicted target.
const StaticInstPtr BTBGetInst (ThreadID tid, Addr pc)
 Looks up a given PC in the BTB to get current static instruction information.
void BTBUpdate (ThreadID tid, Addr pc, const PCStateBase &target)
 Updates the BTB with the target of a branch.
void branchPlaceholder (ThreadID tid, Addr pc, bool uncond, void *&bp_history)
 Special function for the decoupled front-end.
void dump ()
void insertPredictorHistory (ThreadID tid, PredictorHistory *&bpu_history)
 Pushes a PredictorHistory object into the branch predictor history queue.
bool predict (const StaticInstPtr &inst, const InstSeqNum &seqNum, PCStateBase &pc, ThreadID tid, PredictorHistory *&bpu_history)
 Internal prediction function.
void squashHistory (ThreadID tid, PredictorHistory *&bpu_history)
 Squashes a particular branch instance.
void commitBranch (ThreadID tid, PredictorHistory *&bpu_history)
 Commit a particular branch.
void updateBTB (ThreadID tid, PredictorHistory *&bpu_history)
 Update the BTB with the correct target of a branch.
Public Member Functions inherited from gem5::SimObject
const Paramsparams () const
 SimObject (const Params &p)
virtual ~SimObject ()
virtual void init ()
 init() is called after all C++ SimObjects have been created and all ports are connected.
virtual void loadState (CheckpointIn &cp)
 loadState() is called on each SimObject when restoring from a checkpoint.
virtual void initState ()
 initState() is called on each SimObject when not restoring from a checkpoint.
virtual void regProbeListeners ()
 Register probe listeners for this object.
ProbeManagergetProbeManager ()
 Get the probe manager for this object.
virtual PortgetPort (const std::string &if_name, PortID idx=InvalidPortID)
 Get a port with a given name and index.
virtual void startup ()
 startup() is the final initialization call before simulation.
DrainState drain () override
 Provide a default implementation of the drain interface for objects that don't need draining.
virtual void memWriteback ()
 Write back dirty buffers to memory using functional writes.
virtual void memInvalidate ()
 Invalidate the contents of memory buffers.
void serialize (CheckpointOut &cp) const override
 Serialize an object.
void unserialize (CheckpointIn &cp) override
 Unserialize an object.
Public Member Functions inherited from gem5::EventManager
EventQueueeventQueue () const
void schedule (Event &event, Tick when)
void deschedule (Event &event)
void reschedule (Event &event, Tick when, bool always=false)
void schedule (Event *event, Tick when)
void deschedule (Event *event)
void reschedule (Event *event, Tick when, bool always=false)
void wakeupEventQueue (Tick when=(Tick) -1)
 This function is not needed by the usual gem5 event loop but may be necessary in derived EventQueues which host gem5 on other schedulers.
void setCurTick (Tick newVal)
 EventManager (EventManager &em)
 Event manger manages events in the event queue.
 EventManager (EventManager *em)
 EventManager (EventQueue *eq)
Public Member Functions inherited from gem5::Serializable
 Serializable ()
virtual ~Serializable ()
void serializeSection (CheckpointOut &cp, const char *name) const
 Serialize an object into a new section.
void serializeSection (CheckpointOut &cp, const std::string &name) const
void unserializeSection (CheckpointIn &cp, const char *name)
 Unserialize an a child object.
void unserializeSection (CheckpointIn &cp, const std::string &name)
Public Member Functions inherited from gem5::Drainable
DrainState drainState () const
 Return the current drain state of an object.
virtual void notifyFork ()
 Notify a child process of a fork.
Public Member Functions inherited from gem5::statistics::Group
 Group (Group *parent, const char *name=nullptr)
 Construct a new statistics group.
virtual ~Group ()
virtual void regStats ()
 Callback to set stat parameters.
virtual void resetStats ()
 Callback to reset stats.
virtual void preDumpStats ()
 Callback before stats are dumped.
void addStat (statistics::Info *info)
 Register a stat with this group.
const std::map< std::string, Group * > & getStatGroups () const
 Get all child groups associated with this object.
const std::vector< Info * > & getStats () const
 Get all stats associated with this object.
void addStatGroup (const char *name, Group *block)
 Add a stat block as a child of this block.
const InforesolveStat (std::string name) const
 Resolve a stat by its name within this group.
void mergeStatGroup (Group *block)
 Merge the contents (stats & children) of a block to this block.
 Group ()=delete
 Group (const Group &)=delete
Groupoperator= (const Group &)=delete
Public Member Functions inherited from gem5::Named
 Named (std::string_view name_)
virtual ~Named ()=default
virtual std::string name () const

Protected Attributes

const unsigned numThreads
 Number of the threads for which the branch history is maintained.
const bool requiresBTBHit
 Requires the BTB to hit for returns and indirect branches.
const bool updateBTBAtSquash
 Update the BTB at squash time instead of commit.
const unsigned instShiftAmt
 Number of bits to shift instructions by for predictor addresses.
std::vector< std::deque< PredictorHistory * > > predHist
 The per-thread predictor history.
BranchTargetBufferbtb
 The BTB.
ReturnAddrStackras
 The return address stack.
ConditionalPredictorcPred
 The conditional branch predictor.
IndirectPredictoriPred
 The indirect target predictor.
gem5::branch_prediction::BPredUnit::BPredUnitStats stats
Protected Attributes inherited from gem5::SimObject
const SimObjectParams & _params
 Cached copy of the object parameters.
Protected Attributes inherited from gem5::EventManager
EventQueueeventq
 A pointer to this object's event queue.

Private Types

typedef BranchPredictorParams Params
typedef enums::TargetProvider TargetProvider

PMU Probe points.

probing::PMUUPtr ppBranches
 Branches seen by the branch predictor.
probing::PMUUPtr ppMisses
 Miss-predicted branches.
probing::PMUUPtr pmuProbePoint (const char *name)
 Helper method to instantiate probe points belonging to this object.

Additional Inherited Members

Public Types inherited from gem5::SimObject
typedef SimObjectParams Params
Static Public Member Functions inherited from gem5::SimObject
static void serializeAll (const std::string &cpt_dir)
 Create a checkpoint by serializing all SimObjects in the system.
static SimObjectfind (const char *name)
 Find the SimObject with the given name and return a pointer to it.
static void setSimObjectResolver (SimObjectResolver *resolver)
 There is a single object name resolver, and it is only set when simulation is restoring from checkpoints.
static SimObjectResolvergetSimObjectResolver ()
 There is a single object name resolver, and it is only set when simulation is restoring from checkpoints.
Static Public Member Functions inherited from gem5::Serializable
static const std::string & currentSection ()
 Gets the fully-qualified name of the active section.
static void generateCheckpointOut (const std::string &cpt_dir, std::ofstream &outstream)
 Generate a checkpoint file so that the serialization can be routed to it.
Protected Member Functions inherited from gem5::Drainable
 Drainable ()
virtual ~Drainable ()
virtual void drainResume ()
 Resume execution after a successful drain.
void signalDrainDone () const
 Signal that an object is drained.

Detailed Description

Basically a wrapper class to hold both the branch predictor and the BTB.

Definition at line 71 of file bpred_unit.hh.

Member Typedef Documentation

◆ Params

typedef BranchPredictorParams gem5::branch_prediction::BPredUnit::Params
private

Definition at line 73 of file bpred_unit.hh.

◆ TargetProvider

typedef enums::TargetProvider gem5::branch_prediction::BPredUnit::TargetProvider
private

Definition at line 74 of file bpred_unit.hh.

Constructor & Destructor Documentation

◆ BPredUnit()

gem5::branch_prediction::BPredUnit::BPredUnit ( const Params & p)

Branch Predictor Unit (BPU) interface functions.

Parameters
paramsThe params object, that has the size of the BP and BTB.

Definition at line 58 of file bpred_unit.cc.

References btb, cPred, instShiftAmt, iPred, numThreads, gem5::SimObject::params(), predHist, ras, requiresBTBHit, gem5::SimObject::SimObject(), stats, and updateBTBAtSquash.

Referenced by gem5::branch_prediction::BPredUnit::BPredUnitStats::BPredUnitStats(), and gem5::branch_prediction::GshareBP::GshareBP().

Member Function Documentation

◆ branchPlaceholder()

void gem5::branch_prediction::BPredUnit::branchPlaceholder ( ThreadID tid,
Addr pc,
bool uncond,
void *& bp_history )

Special function for the decoupled front-end.

In it there can be branches which are not detected by the BPU in the first place as it requires a BTB hit. This function will generate a placeholder for such a branch once it is pre-decoded in the fetch stage. It will only create the branch history object but not update any internal state of the BPU. If the branch turns to be wrong then decode or commit will be able to use the normal squash functionality to correct the branch. Note that not all branch predictors implement this functionality.

Parameters
tidThe thread id.
pcThe branch's PC.
uncondWhether or not this branch is an unconditional branch.
bp_historyPointer that will be set to an branch history object.

Definition at line 618 of file bpred_unit.cc.

References cPred, and gem5::MipsISA::pc.

◆ BTBGetInst()

const StaticInstPtr gem5::branch_prediction::BPredUnit::BTBGetInst ( ThreadID tid,
Addr pc )
inline

Looks up a given PC in the BTB to get current static instruction information.

This is necessary in a decoupled frontend as the information does not usually exist at that this point. Only for instructions (branches) that hit in the BTB this information is available as the BTB stores them together with the target.

Parameters
tidThe thread id.
pcThe PC to look up.
Returns
The static instruction info of the given PC if existant.

Definition at line 166 of file bpred_unit.hh.

References btb, and gem5::MipsISA::pc.

◆ BTBLookup()

const PCStateBase * gem5::branch_prediction::BPredUnit::BTBLookup ( ThreadID tid,
PCStateBase & pc )
inline

Looks up a given PC in the BTB to get the predicted target.

The PC may be changed or deleted in the future, so it needs to be used immediately, and/or copied for use later.

Parameters
tidThe thread id.
pcThe PC to look up.
Returns
The address of the target of the branch.

Definition at line 150 of file bpred_unit.hh.

References btb, and gem5::MipsISA::pc.

◆ BTBUpdate()

void gem5::branch_prediction::BPredUnit::BTBUpdate ( ThreadID tid,
Addr pc,
const PCStateBase & target )
inline

Updates the BTB with the target of a branch.

Parameters
tidThe thread id.
pcThe branch's PC that will be updated.
targetThe branch's target that will be added to the BTB.

Definition at line 178 of file bpred_unit.hh.

References btb, gem5::MipsISA::pc, and stats.

◆ BTBValid()

bool gem5::branch_prediction::BPredUnit::BTBValid ( ThreadID tid,
Addr pc )
inline

Looks up a given PC in the BTB to see if a matching entry exists.

Parameters
tidThe thread id.
pcThe PC to look up.
Returns
Whether the BTB contains the given PC.

Definition at line 136 of file bpred_unit.hh.

References btb, and gem5::MipsISA::pc.

◆ commitBranch()

◆ drainSanityCheck()

void gem5::branch_prediction::BPredUnit::drainSanityCheck ( ) const

Perform sanity checks after a drain.

Definition at line 86 of file bpred_unit.cc.

References predHist.

◆ dump()

void gem5::branch_prediction::BPredUnit::dump ( )

Definition at line 626 of file bpred_unit.cc.

References gem5::cprintf(), gem5::ArmISA::i, and predHist.

◆ insertPredictorHistory()

void gem5::branch_prediction::BPredUnit::insertPredictorHistory ( ThreadID tid,
PredictorHistory *& bpu_history )

Pushes a PredictorHistory object into the branch predictor history queue.

This is used by the decoupled front-end to move predictions histories from the fetch target back to the branch predictor.

Parameters
tidThe thread id.
bpu_historyThe history to be inserted.

Definition at line 115 of file bpred_unit.cc.

References predHist.

◆ pmuProbePoint()

probing::PMUUPtr gem5::branch_prediction::BPredUnit::pmuProbePoint ( const char * name)
protected

Helper method to instantiate probe points belonging to this object.

Parameters
nameName of the probe point.
Returns
A unique_ptr to the new probe point.

Definition at line 70 of file bpred_unit.cc.

References gem5::SimObject::getProbeManager(), and gem5::Named::name().

Referenced by regProbePoints().

◆ predict() [1/2]

bool gem5::branch_prediction::BPredUnit::predict ( const StaticInstPtr & inst,
const InstSeqNum & seqNum,
PCStateBase & pc,
ThreadID tid )

Predicts whether or not the instruction is a taken branch, and the target of the branch if it is taken.

Parameters
instThe branch instruction.
seqNumThe sequence number of the instruction.
PCThe predicted PC is passed back through this parameter.
tidThe thread id.
Returns
Returns if the branch is taken or not.

Perform the prediction.

Push the record into the history buffer

Definition at line 96 of file bpred_unit.cc.

References DPRINTF, gem5::MipsISA::pc, predHist, and predict().

Referenced by predict().

◆ predict() [2/2]

bool gem5::branch_prediction::BPredUnit::predict ( const StaticInstPtr & inst,
const InstSeqNum & seqNum,
PCStateBase & pc,
ThreadID tid,
PredictorHistory *& bpu_history )

Internal prediction function.


Fallthrough

All the target predictors did their job. If there is no target its either not taken or a BTB miss. In that case we just fallthrough.


Speculative history update

Now that the prediction is done the predictor may update its histories speculative. (local and global path). A later squash will revert the history update if needed. The actual prediction tables will updated once we know the correct direction.

Definition at line 121 of file bpred_unit.cc.

References gem5::branch_prediction::BPredUnit::PredictorHistory::actuallyTaken, gem5::StaticInst::advancePC(), gem5::branch_prediction::BPredUnit::PredictorHistory::bpHistory, btb, gem5::branch_prediction::BPredUnit::PredictorHistory::btbHit, gem5::StaticInst::buildRetPC(), gem5::branch_prediction::BPredUnit::PredictorHistory::condPred, cPred, DPRINTF, gem5::branch_prediction::getBranchType(), gem5::branch_prediction::BPredUnit::PredictorHistory::indirectHistory, gem5::branch_prediction::BPredUnit::PredictorHistory::inst, iPred, gem5::StaticInst::isCall(), gem5::StaticInst::isIndirectCtrl(), gem5::StaticInst::isReturn(), gem5::branch_prediction::BPredUnit::PredictorHistory::pc, gem5::MipsISA::pc, ppBranches, gem5::branch_prediction::BPredUnit::PredictorHistory::predTaken, ras, gem5::branch_prediction::BPredUnit::PredictorHistory::rasHistory, requiresBTBHit, gem5::ArmISA::set, stats, gem5::branch_prediction::BPredUnit::PredictorHistory::target, gem5::branch_prediction::BPredUnit::PredictorHistory::targetProvider, gem5::branch_prediction::toString(), and gem5::branch_prediction::BPredUnit::PredictorHistory::uncond.

◆ regProbePoints()

void gem5::branch_prediction::BPredUnit::regProbePoints ( )
overridevirtual

Register probe points for this object.

No probe points by default, so do nothing in base.

Reimplemented from gem5::SimObject.

Definition at line 79 of file bpred_unit.cc.

References pmuProbePoint(), ppBranches, and ppMisses.

◆ squash() [1/2]

void gem5::branch_prediction::BPredUnit::squash ( const InstSeqNum & squashed_sn,
const PCStateBase & corr_target,
bool actually_taken,
ThreadID tid,
bool from_commit = true )

◆ squash() [2/2]

void gem5::branch_prediction::BPredUnit::squash ( const InstSeqNum & squashed_sn,
ThreadID tid )

Squashes all outstanding updates until a given sequence number.

Parameters
squashed_snThe sequence number to squash any younger updates up until.
tidThe thread id.

Definition at line 403 of file bpred_unit.cc.

References DPRINTF, predHist, and squashHistory().

Referenced by squash().

◆ squashHistory()

void gem5::branch_prediction::BPredUnit::squashHistory ( ThreadID tid,
PredictorHistory *& bpu_history )

◆ update()

void gem5::branch_prediction::BPredUnit::update ( const InstSeqNum & done_sn,
ThreadID tid )

Tells the branch predictor to commit any updates until the given sequence number.

Parameters
done_snThe sequence number to commit any older updates up until.
tidThe thread id.

Definition at line 336 of file bpred_unit.cc.

References commitBranch(), DPRINTF, and predHist.

◆ updateBTB()

Member Data Documentation

◆ btb

BranchTargetBuffer* gem5::branch_prediction::BPredUnit::btb
protected

The BTB.

Definition at line 432 of file bpred_unit.hh.

Referenced by BPredUnit(), BTBGetInst(), BTBLookup(), BTBUpdate(), BTBValid(), predict(), and updateBTB().

◆ cPred

ConditionalPredictor* gem5::branch_prediction::BPredUnit::cPred
protected

The conditional branch predictor.

Definition at line 438 of file bpred_unit.hh.

Referenced by BPredUnit(), branchPlaceholder(), commitBranch(), predict(), squash(), and squashHistory().

◆ instShiftAmt

const unsigned gem5::branch_prediction::BPredUnit::instShiftAmt
protected

Number of bits to shift instructions by for predictor addresses.

Definition at line 422 of file bpred_unit.hh.

Referenced by BPredUnit(), gem5::branch_prediction::GshareBP::lookup(), and gem5::branch_prediction::GshareBP::update().

◆ iPred

IndirectPredictor* gem5::branch_prediction::BPredUnit::iPred
protected

The indirect target predictor.

Definition at line 441 of file bpred_unit.hh.

Referenced by BPredUnit(), commitBranch(), predict(), squash(), squashHistory(), and updateBTB().

◆ numThreads

const unsigned gem5::branch_prediction::BPredUnit::numThreads
protected

Number of the threads for which the branch history is maintained.

Definition at line 405 of file bpred_unit.hh.

Referenced by BPredUnit(), gem5::branch_prediction::BPredUnit::BPredUnitStats::BPredUnitStats(), and gem5::branch_prediction::GshareBP::GshareBP().

◆ ppBranches

probing::PMUUPtr gem5::branch_prediction::BPredUnit::ppBranches
protected

Branches seen by the branch predictor.

Note
This counter includes speculative branches.

Definition at line 505 of file bpred_unit.hh.

Referenced by predict(), and regProbePoints().

◆ ppMisses

probing::PMUUPtr gem5::branch_prediction::BPredUnit::ppMisses
protected

Miss-predicted branches.

Definition at line 508 of file bpred_unit.hh.

Referenced by commitBranch(), and regProbePoints().

◆ predHist

std::vector<std::deque<PredictorHistory *> > gem5::branch_prediction::BPredUnit::predHist
protected

The per-thread predictor history.

This is used to update the predictor as instructions are committed, or restore it to the proper state after a squash.

Definition at line 429 of file bpred_unit.hh.

Referenced by BPredUnit(), drainSanityCheck(), dump(), insertPredictorHistory(), predict(), squash(), squash(), and update().

◆ ras

ReturnAddrStack* gem5::branch_prediction::BPredUnit::ras
protected

The return address stack.

Definition at line 435 of file bpred_unit.hh.

Referenced by BPredUnit(), commitBranch(), predict(), squash(), and squashHistory().

◆ requiresBTBHit

const bool gem5::branch_prediction::BPredUnit::requiresBTBHit
protected

Requires the BTB to hit for returns and indirect branches.

For an advanced front-end there is no other way than a BTB hit to know that the branch exists in the first place. Furthermore, the BPU needs to know the branch type to make the correct RAS operations. This info is only available from the BTB. Low-end CPUs predecoding might be used to identify branches.

Definition at line 413 of file bpred_unit.hh.

Referenced by BPredUnit(), predict(), and updateBTB().

◆ stats

gem5::branch_prediction::BPredUnit::BPredUnitStats gem5::branch_prediction::BPredUnit::stats
protected

◆ updateBTBAtSquash

const bool gem5::branch_prediction::BPredUnit::updateBTBAtSquash
protected

Update the BTB at squash time instead of commit.

This can be useful to update the BTB earlier to avoid BTB misses on subsequent branches. However, it can also lead to BTB pollution if the branch is on the false path and will be squashed later.

Definition at line 419 of file bpred_unit.hh.

Referenced by BPredUnit(), commitBranch(), and squash().


The documentation for this class was generated from the following files:

Generated on Mon Oct 27 2025 04:13:18 for gem5 by doxygen 1.14.0