1212#include " debug/DecoupleBPVerbose.hh"
1313#include " debug/DecoupleBPUseful.hh"
1414#include " debug/ITTAGE.hh"
15+ #include " debug/ITTAGEHistory.hh"
1516
1617namespace gem5 {
1718
@@ -30,6 +31,7 @@ maxHistLen(p.maxHistLen),
3031numTablesToAlloc (p.numTablesToAlloc),
3132ittageStats (this , p.numPredictors)
3233{
34+ this ->needMoreHistories = p.needMoreHistories ;
3335 DPRINTF (ITTAGE, " BTBITTAGE constructor numBr=%d\n " , numBr);
3436 tageTable.resize (numPredictors);
3537 tableIndexBits.resize (numPredictors);
@@ -51,9 +53,9 @@ ittageStats(this, p.numPredictors)
5153
5254 assert (tablePcShifts.size () >= numPredictors);
5355
54- tagFoldedHist.push_back (GlobalFoldedHist ((int )histLengths[i], (int )tableTagBits[i], (int )16 ));
55- altTagFoldedHist.push_back (GlobalFoldedHist ((int )histLengths[i], (int )tableTagBits[i]-1 , (int )16 ));
56- indexFoldedHist.push_back (GlobalFoldedHist ((int )histLengths[i], (int )tableIndexBits[i], (int )16 ));
56+ tagFoldedHist.push_back (PathFoldedHist ((int )histLengths[i], (int )tableTagBits[i], (int )16 ));
57+ altTagFoldedHist.push_back (PathFoldedHist ((int )histLengths[i], (int )tableTagBits[i]-1 , (int )16 ));
58+ indexFoldedHist.push_back (PathFoldedHist ((int )histLengths[i], (int )tableIndexBits[i], (int )16 ));
5759 }
5860 // useAlt.resize(128);
5961 // for (unsigned i = 0; i < useAlt.size(); ++i) {
@@ -451,50 +453,84 @@ BTBITTAGE::satDecrement(int min, short &counter)
451453 return counter == min;
452454}
453455
456+ /* *
457+ * @brief Updates branch history for speculative execution
458+ *
459+ * This function updates three types of folded histories:
460+ * - Tag folded history: Used for tag computation
461+ * - Alternative tag folded history: Used for alternative tag computation
462+ * - Index folded history: Used for table index computation
463+ *
464+ * @param history The current branch history
465+ * @param taken Whether the branch was taken
466+ * @param pc The program counter of the branch
467+ * @param target The target address of the branch
468+ */
454469void
455- BTBITTAGE::doUpdateHist (const boost::dynamic_bitset<> &history, int shamt, bool taken )
470+ BTBITTAGE::doUpdateHist (const boost::dynamic_bitset<> &history, bool taken, Addr pc, Addr target )
456471{
457- if (debugFlag ) {
472+ if (debug::ITTAGEHistory ) { // if debug flag is off, do not use to_string since it's too slow
458473 std::string buf;
459474 boost::to_string (history, buf);
460- DPRINTF (ITTAGE , " in doUpdateHist, shamt %d, taken %d , history %s\n " , shamt, taken , buf);
475+ DPRINTF (ITTAGEHistory , " in doUpdateHist, taken %d, pc %#lx , history %s\n " , taken, pc , buf. c_str () );
461476 }
462- if (shamt == 0 ) {
463- DPRINTF (ITTAGE , " shamt is 0, returning \n " );
477+ if (!taken ) {
478+ DPRINTF (ITTAGEHistory , " not updating folded history, since FB not taken \n " );
464479 return ;
465480 }
466481
467482 for (int t = 0 ; t < numPredictors; t++) {
468483 for (int type = 0 ; type < 3 ; type++) {
469- DPRINTF (ITTAGE, " t: %d, type: %d\n " , t, type);
470-
471484 auto &foldedHist = type == 0 ? indexFoldedHist[t] : type == 1 ? tagFoldedHist[t] : altTagFoldedHist[t];
472- foldedHist.update (history, shamt, taken);
485+ // since we have folded path history, we can put arbitrary shamt here, and it wouldn't make a difference
486+ foldedHist.update (history, 2 , taken, pc, target);
487+ DPRINTF (ITTAGEHistory, " t: %d, type: %d, foldedHist _folded 0x%lx\n " , t, type, foldedHist.get ());
473488 }
474489 }
475490}
476491
492+ /* *
493+ * @brief Updates branch history for speculative execution
494+ *
495+ * This function updates the branch history for speculative execution
496+ * based on the provided history and prediction information.
497+ *
498+ * It first retrieves the history information from the prediction metadata
499+ * and then calls the doUpdateHist function to update the folded histories.
500+ *
501+ * @param history The current branch history
502+ * @param pred The prediction metadata containing history information
503+ */
477504void
478- BTBITTAGE::specUpdateHist (const boost::dynamic_bitset<> &history, FullBTBPrediction &pred)
505+ BTBITTAGE::specUpdatePHist (const boost::dynamic_bitset<> &history, FullBTBPrediction &pred)
479506{
480- int shamt;
481- bool cond_taken;
482- std::tie (shamt, cond_taken) = pred.getHistInfo ();
483- doUpdateHist (history, shamt, cond_taken);
507+ auto [pc, target, taken] = pred.getPHistInfo ();
508+ doUpdateHist (history, taken, pc, target);
484509}
485510
511+ /* *
512+ * @brief Recovers branch history state after a misprediction
513+ *
514+ * This function:
515+ * 1. Restores the folded histories from the saved metadata
516+ * 2. Updates the histories with the correct branch outcome
517+ * 3. Ensures predictor state is consistent after recovery
518+ *
519+ * @param history The branch history to recover to
520+ * @param entry The fetch stream entry containing recovery information
521+ * @param shamt Number of bits to shift in history update
522+ * @param cond_taken The actual branch outcome
523+ */
486524void
487- BTBITTAGE::recoverHist (const boost::dynamic_bitset<> &history,
488- const FetchStream &entry, int shamt, bool cond_taken)
525+ BTBITTAGE::recoverPHist (const boost::dynamic_bitset<> &history, const FetchStream &entry, int shamt, bool cond_taken)
489526{
490- // TODO: need to get idx
491527 std::shared_ptr<TageMeta> predMeta = std::static_pointer_cast<TageMeta>(entry.predMetas [getComponentIdx ()]);
492528 for (int i = 0 ; i < numPredictors; i++) {
493529 tagFoldedHist[i].recover (predMeta->tagFoldedHist [i]);
494530 altTagFoldedHist[i].recover (predMeta->altTagFoldedHist [i]);
495531 indexFoldedHist[i].recover (predMeta->indexFoldedHist [i]);
496532 }
497- doUpdateHist (history, shamt, cond_taken );
533+ doUpdateHist (history, cond_taken, entry. getControlPC (), entry. getTakenTarget () );
498534}
499535
500536void
0 commit comments