50#include "debug/Branch.hh"
90 for ([[maybe_unused]]
const auto& ph :
predHist)
101 bool taken =
predict(inst, seqNum,
pc, tid, bpu_history);
103 assert(bpu_history!=
nullptr);
106 predHist[tid].push_front(bpu_history);
109 "predHist.size(): %i\n", tid, seqNum,
predHist[tid].size());
117 predHist[tid].push_front(bpu_history);
124 assert(hist ==
nullptr);
135 stats.lookups[tid][brType]++;
152 ++
stats.condPredicted;
156 ++
stats.condPredictedTaken;
162 "[tid:%i, sn:%llu] Branch predictor predicted %i for PC:%#x %s\n",
192 tid, seqNum, hist->
pc, (hist->
btbHit) ?
"hit" :
"miss");
211 if (
ras && branch_detected) {
219 "a call, push return address %s onto the RAS\n",
220 tid, seqNum,
pc, *return_addr);
235 "return, RAS poped return addr: %s\n",
257 ++
stats.indirectLookups;
259 std::unique_ptr<const PCStateBase> itarget(
260 iPred->lookup(tid, seqNum,
pc.instAddr(),
265 ++
stats.indirectHits;
270 "[tid:%i, sn:%llu] Instruction %s predicted "
271 "indirect target is %s\n",
274 ++
stats.indirectMisses;
276 "[tid:%i, sn:%llu] PC:%#x no indirect target\n",
277 tid, seqNum,
pc.instAddr());
304 DPRINTF(
Branch,
"%s(tid:%i, sn:%i, PC:%#x, %s) -> taken:%i, target:%s "
305 "provider:%s\n", __func__, tid, seqNum, hist->
pc,
339 "[sn:%llu]\n", tid, done_sn);
342 predHist[tid].back()->seqNum <= done_sn) {
350 DPRINTF(
Branch,
"[tid:%i] [commit sn:%llu] pred_hist.size(): %i\n",
351 tid, done_sn,
predHist[tid].size());
364 stats.mispredictDueToBTBMiss[tid][hist->
type]++;
366 stats.mispredictDueToPredictor[tid][hist->
type]++;
368 ++
stats.condIncorrect;
374 "pred:%i, taken:%i, target:%#x\n",
377 hist->
target->instAddr());
407 predHist[tid].front()->seqNum > squashed_sn) {
412 "[tid:%i, squash sn:%llu] Removing history for "
414 tid, squashed_sn, hist->seqNum, hist->pc);
420 DPRINTF(
Branch,
"[tid:%i] [squash sn:%llu] pred_hist.size(): %i\n",
421 tid, squashed_sn,
predHist[tid].size());
440 DPRINTF(
Branch,
"[tid:%i] [squash sn:%llu] Incorrect call/return "
441 "PC %#x. Fix RAS.\n", tid, history->
seqNum,
463 bool actually_taken,
ThreadID tid,
bool from_commit)
476 DPRINTF(
Branch,
"[tid:%i] Squash from %s start from sequence number %i, "
477 "setting target to %s\n", tid, from_commit ?
"commit" :
"decode",
478 squashed_sn, corr_target);
493 DPRINTF(
Branch,
"[tid:%i] [squash sn:%llu] Mispredicted: %s, PC:%#x\n",
532 iPred->update(tid, squashed_sn, hist->
pc,
533 true, actually_taken, corr_target,
541 if (actually_taken && (hist->
rasHistory ==
nullptr)) {
544 if (hist->
type == BranchType::Return) {
546 "Incorrectly predicted return [sn:%llu] PC: %#x\n",
547 tid, squashed_sn, hist->
seqNum, hist->
pc);
557 corr_target, corr_target);
560 return_addr->set(hist->
pc + hist->
inst->
size());
564 "Incorrectly predicted call: [sn:%llu,PC:%#x] "
565 " Push return address %s onto RAS\n", tid,
566 squashed_sn, hist->
seqNum, hist->
pc,
572 }
else if (!actually_taken && (hist->
rasHistory !=
nullptr)) {
585 "[tid:%i] [sn:%llu] predHist empty, can't "
604 DPRINTF(
Branch,
"[tid:%i] BTB Update for [sn:%llu] PC %#x -> T:%#x\n", tid,
608 ++
stats.BTBMispredicted;
614 btb->incorrectTarget(hist->
pc, hist->
type);
619 bool uncond,
void * &bp_history)
622 cPred->branchPlaceholder(tid,
pc, uncond, bp_history);
631 auto hist = ph.begin();
633 cprintf(
"predHist[%i].size(): %i\n",
i++, ph.size());
635 while (hist != ph.end()) {
636 cprintf(
"sn:%llu], PC:%#x, tid:%i, predTaken:%i, "
637 "bpHistory:%#x, rasHistory:%#x\n",
638 (*hist)->seqNum, (*hist)->pc,
639 (*hist)->tid, (*hist)->predTaken,
640 (*hist)->bpHistory, (*hist)->rasHistory);
653 "Number of BP lookups"),
655 "Number of branches that got squashed (completely removed) as "
656 "an earlier branch was mispredicted."),
658 "Number of branches that got corrected but not yet commited. "
659 "Branches get corrected by decode or after execute. Also a "
660 "branch misprediction can be detected out-of-order. Therefore, "
661 "a corrected branch might not end up beeing committed in case "
662 "an even earlier branch was mispredicted"),
664 "Number of branches that got redirected after decode."),
666 "Number of branches finally committed "),
668 "Number of committed branches that were mispredicted."),
670 "Number of committed branches that were mispredicted by the "
673 "Number of committed branches that were mispredicted because of "
676 "The component providing the target for taken branches"),
678 "Number of branches where the target was incorrect or not "
679 "available at prediction time."),
681 "Number of conditional branches predicted"),
683 "Number of conditional branches predicted as taken"),
685 "Number of conditional branches incorrect"),
687 "Number of branches predicted taken but missed in BTB"),
689 "Number of BTB lookups"),
691 "Number of BTB updates"),
693 "Number of BTB hits"),
697 "Number BTB mispredictions. No target found or target wrong"),
699 "Number of indirect predictor lookups."),
701 "Number of indirect target hits."),
703 "Number of indirect misses."),
705 "Number of mispredicted indirect branches.")
714 lookups.ysubnames(enums::BranchTypeStrings);
719 squashes.ysubnames(enums::BranchTypeStrings);
724 corrected.ysubnames(enums::BranchTypeStrings);
734 committed.ysubnames(enums::BranchTypeStrings);
752 .init(bp->
numThreads, enums::Num_TargetProvider)
virtual std::string name() const
Addr instAddr() const
Returns the memory address of the instruction this PC points to.
Base class for branch operations.
virtual std::unique_ptr< PCStateBase > buildRetPC(const PCStateBase &cur_pc, const PCStateBase &call_pc) const
virtual void advancePC(PCStateBase &pc_state) const =0
bool isIndirectCtrl() const
std::vector< std::deque< PredictorHistory * > > predHist
The per-thread predictor history.
probing::PMUUPtr pmuProbePoint(const char *name)
Helper method to instantiate probe points belonging to this object.
void update(const InstSeqNum &done_sn, ThreadID tid)
Tells the branch predictor to commit any updates until the given sequence number.
BPredUnit(const Params &p)
Branch Predictor Unit (BPU) interface functions.
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 take...
ConditionalPredictor * cPred
The conditional branch predictor.
void branchPlaceholder(ThreadID tid, Addr pc, bool uncond, void *&bp_history)
Special function for the decoupled front-end.
probing::PMUUPtr ppMisses
Miss-predicted branches.
const bool requiresBTBHit
Requires the BTB to hit for returns and indirect branches.
void drainSanityCheck() const
Perform sanity checks after a drain.
IndirectPredictor * iPred
The indirect target predictor.
void commitBranch(ThreadID tid, PredictorHistory *&bpu_history)
Commit a particular branch.
probing::PMUUPtr ppBranches
Branches seen by the branch predictor.
void regProbePoints() override
Register probe points for this object.
void insertPredictorHistory(ThreadID tid, PredictorHistory *&bpu_history)
Pushes a PredictorHistory object into the branch predictor history queue.
const unsigned numThreads
Number of the threads for which the branch history is maintained.
const unsigned instShiftAmt
Number of bits to shift instructions by for predictor addresses.
BranchPredictorParams Params
void squashHistory(ThreadID tid, PredictorHistory *&bpu_history)
Squashes a particular branch instance.
const bool updateBTBAtSquash
Update the BTB at squash time instead of commit.
ReturnAddrStack * ras
The return address stack.
void squash(const InstSeqNum &squashed_sn, ThreadID tid)
Squashes all outstanding updates until a given sequence number.
gem5::branch_prediction::BPredUnit::BPredUnitStats stats
BranchTargetBuffer * btb
The BTB.
void updateBTB(ThreadID tid, PredictorHistory *&bpu_history)
Update the BTB with the correct target of a branch.
#define ADD_STAT(n,...)
Convenience macro to add a stat to a statistics group.
const Params & params() const
SimObject(const Params &p)
ProbeManager * getProbeManager()
Get the probe manager for this object.
std::string toString(BranchType type)
BranchType getBranchType(StaticInstPtr inst)
enums::BranchType BranchType
std::unique_ptr< PMU > PMUUPtr
ProbePointArg< uint64_t > PMU
PMU probe point.
const FlagsType pdf
Print the percent of the total that this entry represents.
const FlagsType total
Print the total.
Copyright (c) 2024 Arm Limited All rights reserved.
int16_t ThreadID
Thread index/ID type.
void cprintf(const char *format, const Args &...args)
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
RefCountingPtr< StaticInst > StaticInstPtr
statistics::Vector2d corrected
statistics::Scalar BTBUpdates
statistics::Scalar indirectMispredicted
statistics::Scalar BTBLookups
BTB stats.
statistics::Scalar condIncorrect
statistics::Vector2d squashes
statistics::Vector2d committed
statistics::Scalar BTBMispredicted
statistics::Scalar indirectLookups
Indirect stats.
statistics::Vector2d lookups
Stats per branch type.
statistics::Scalar indirectHits
statistics::Vector2d targetProvider
Target prediction per branch type.
statistics::Scalar condPredictedTaken
statistics::Scalar condPredicted
Additional scalar stats for conditional branches.
statistics::Vector2d mispredictDueToBTBMiss
statistics::Vector2d mispredicted
statistics::Vector2d earlyResteers
BPredUnitStats(BPredUnit *bp)
statistics::Formula BTBHitRatio
statistics::Scalar predTakenBTBMiss
statistics::Scalar indirectMisses
statistics::Vector2d targetWrong
statistics::Scalar BTBHits
statistics::Vector2d mispredictDueToPredictor
Branch Predictor Unit (BPU) history object PredictorHistory This class holds all information needed t...
InstSeqNum seqNum
The sequence number for the predictor history entry.
bool predTaken
Whether or not it was predicted taken.
bool condPred
The prediction of the conditional predictor.
void * bpHistory
Pointer to the history objects passed back from the branch predictor subcomponents.
const Addr pc
The PC associated with the sequence number.
const bool call
Whether or not the instruction was a call.
const StaticInstPtr inst
The branch instrction.
bool actuallyTaken
To record the actual outcome of the branch.
bool mispredict
The branch was corrected hence was mispredicted.
const bool uncond
Was unconditional control.
const BranchType type
The type of the branch.
bool btbHit
Was BTB hit at prediction time.
std::unique_ptr< PCStateBase > target
The predicted target.
TargetProvider targetProvider
Which component provided the target.