50#include "debug/Branch.hh"
93 for ([[maybe_unused]]
const auto& ph :
predHist)
99 bool uncond,
void * &bp_history)
101 panic(
"BPredUnit::branchPlaceholder() not implemented for this BP.\n");
110 bool taken =
predict(inst, seqNum,
pc, tid, bpu_history);
112 assert(bpu_history!=
nullptr);
115 predHist[tid].push_front(bpu_history);
118 "predHist.size(): %i\n", tid, seqNum,
predHist[tid].size());
130 assert(hist ==
nullptr);
141 stats.lookups[tid][brType]++;
158 ++
stats.condPredicted;
162 ++
stats.condPredictedTaken;
168 "[tid:%i, sn:%llu] Branch predictor predicted %i for PC:%#x %s\n",
198 tid, seqNum, hist->
pc, (hist->
btbHit) ?
"hit" :
"miss");
217 if (
ras && branch_detected) {
225 "a call, push return address %s onto the RAS\n",
226 tid, seqNum,
pc, *return_addr);
241 "return, RAS poped return addr: %s\n",
263 ++
stats.indirectLookups;
265 std::unique_ptr<const PCStateBase> itarget(
266 iPred->lookup(tid, seqNum,
pc.instAddr(),
271 ++
stats.indirectHits;
276 "[tid:%i, sn:%llu] Instruction %s predicted "
277 "indirect target is %s\n",
280 ++
stats.indirectMisses;
282 "[tid:%i, sn:%llu] PC:%#x no indirect target\n",
283 tid, seqNum,
pc.instAddr());
310 DPRINTF(
Branch,
"%s(tid:%i, sn:%i, PC:%#x, %s) -> taken:%i, target:%s "
311 "provider:%s\n", __func__, tid, seqNum, hist->
pc,
344 "[sn:%llu]\n", tid, done_sn);
347 predHist[tid].back()->seqNum <= done_sn) {
355 DPRINTF(
Branch,
"[tid:%i] [commit sn:%llu] pred_hist.size(): %i\n",
356 tid, done_sn,
predHist[tid].size());
369 stats.mispredictDueToBTBMiss[tid][hist->
type]++;
371 stats.mispredictDueToPredictor[tid][hist->
type]++;
377 "pred:%i, taken:%i, target:%#x\n",
380 hist->
target->instAddr());
387 hist->
target->instAddr());
409 predHist[tid].front()->seqNum > squashed_sn) {
417 DPRINTF(
Branch,
"[tid:%i] [squash sn:%llu] pred_hist.size(): %i\n",
418 tid, squashed_sn,
predHist[tid].size());
437 DPRINTF(
Branch,
"[tid:%i] [squash sn:%llu] Incorrect call/return "
438 "PC %#x. Fix RAS.\n", tid, history->
seqNum,
460 bool actually_taken,
ThreadID tid,
bool from_commit)
475 ++
stats.condIncorrect;
479 DPRINTF(
Branch,
"[tid:%i] Squash from %s start from sequence number %i, "
480 "setting target to %s\n", tid, from_commit ?
"commit" :
"decode",
481 squashed_sn, corr_target);
492 if (!pred_hist.empty()) {
496 DPRINTF(
Branch,
"[tid:%i] [squash sn:%llu] Mispredicted: %s, PC:%#x\n",
535 iPred->update(tid, squashed_sn, hist->
pc,
536 true, actually_taken, corr_target,
544 if (actually_taken && (hist->
rasHistory ==
nullptr)) {
547 if (hist->
type == BranchType::Return) {
549 "Incorrectly predicted return [sn:%llu] PC: %#x\n",
550 tid, squashed_sn, hist->
seqNum, hist->
pc);
560 corr_target, corr_target);
563 return_addr->set(hist->
pc + hist->
inst->
size());
567 "Incorrectly predicted call: [sn:%llu,PC:%#x] "
568 " Push return address %s onto RAS\n", tid,
569 squashed_sn, hist->
seqNum, hist->
pc,
575 }
else if (!actually_taken && (hist->
rasHistory !=
nullptr)) {
586 if (actually_taken &&
591 ++
stats.BTBMispredicted;
593 ++
stats.predTakenBTBMiss;
597 "PC %#x -> T: %#x\n", tid,
601 btb->update(tid, hist->
pc,
605 btb->incorrectTarget(hist->
pc, hist->
type);
610 "update\n", tid, squashed_sn);
621 auto hist = ph.begin();
623 cprintf(
"predHist[%i].size(): %i\n",
i++, ph.size());
625 while (hist != ph.end()) {
626 cprintf(
"sn:%llu], PC:%#x, tid:%i, predTaken:%i, "
627 "bpHistory:%#x, rasHistory:%#x\n",
628 (*hist)->seqNum, (*hist)->pc,
629 (*hist)->tid, (*hist)->predTaken,
630 (*hist)->bpHistory, (*hist)->rasHistory);
643 "Number of BP lookups"),
645 "Number of branches that got squashed (completely removed) as "
646 "an earlier branch was mispredicted."),
648 "Number of branches that got corrected but not yet commited. "
649 "Branches get corrected by decode or after execute. Also a "
650 "branch misprediction can be detected out-of-order. Therefore, "
651 "a corrected branch might not end up beeing committed in case "
652 "an even earlier branch was mispredicted"),
654 "Number of branches that got redirected after decode."),
656 "Number of branches finally committed "),
658 "Number of committed branches that were mispredicted."),
660 "Number of committed branches that were mispredicted by the "
663 "Number of committed branches that were mispredicted because of "
666 "The component providing the target for taken branches"),
668 "Number of branches where the target was incorrect or not "
669 "available at prediction time."),
671 "Number of conditional branches predicted"),
673 "Number of conditional branches predicted as taken"),
675 "Number of conditional branches incorrect"),
677 "Number of branches predicted taken but missed in BTB"),
679 "Number of BTB lookups"),
681 "Number of BTB updates"),
683 "Number of BTB hits"),
687 "Number BTB mispredictions. No target found or target wrong"),
689 "Number of indirect predictor lookups."),
691 "Number of indirect target hits."),
693 "Number of indirect misses."),
695 "Number of mispredicted indirect branches.")
704 lookups.ysubnames(enums::BranchTypeStrings);
709 squashes.ysubnames(enums::BranchTypeStrings);
714 corrected.ysubnames(enums::BranchTypeStrings);
724 committed.ysubnames(enums::BranchTypeStrings);
742 .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
bool isDirectCtrl() const
bool isUncondCtrl() const
virtual void advancePC(PCStateBase &pc_state) const =0
bool isIndirectCtrl() const
std::vector< History > 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...
virtual void branchPlaceholder(ThreadID tid, Addr pc, bool uncond, void *&bp_history)
Special function for the decoupled front-end.
virtual void updateHistories(ThreadID tid, Addr pc, bool uncond, bool taken, Addr target, const StaticInstPtr &inst, void *&bp_history)=0
Ones done with the prediction this function updates the path and global history.
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.
const unsigned numThreads
Number of the threads for which the branch history is maintained.
virtual bool lookup(ThreadID tid, Addr pc, void *&bp_history)=0
Looks up a given conditional branch PC of in the BP to see if it is taken or not taken.
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.
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.
std::deque< PredictorHistory * > History
#define ADD_STAT(n,...)
Convenience macro to add a stat to a statistics group.
#define panic(...)
This implements a cprintf based panic() function.
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...
bool predTaken
Whether or not it was predicted taken.
const InstSeqNum seqNum
The sequence number for the predictor history entry.
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.