Skip to content

Commit 9469e8f

Browse files
author
Cao Jiaming
committed
cpu-o3: enhance branch index calculation in MicroTAGE to handle edge cases
1 parent 5cfbb4f commit 9469e8f

1 file changed

Lines changed: 20 additions & 4 deletions

File tree

src/cpu/pred/btb/microtage.cc

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -903,10 +903,26 @@ MicroTAGE::getBaseTableIndex(Addr pc) {
903903
unsigned
904904
MicroTAGE::getBranchIndexInBlock(Addr branchPC, Addr startPC) {
905905
// Calculate branch position within the fetch block (0 .. maxBranchPositions-1)
906-
Addr alignedPC = startPC & ~(blockSize - 1);
907-
Addr offset = (branchPC - alignedPC) >> instShiftAmt;
908-
assert(offset < maxBranchPositions);
909-
return offset;
906+
const Addr alignedPC = startPC & ~(blockSize - 1);
907+
908+
unsigned position = 0;
909+
if (branchPC >= alignedPC) {
910+
const Addr byteOffset = branchPC - alignedPC;
911+
position = byteOffset >> instShiftAmt;
912+
} else {
913+
warn_once("MicroTAGE: branch %#lx precedes block start %#lx; treating as offset 0",
914+
branchPC, startPC);
915+
}
916+
917+
if (position >= maxBranchPositions) {
918+
warn_once("MicroTAGE: branch %#lx exceeds block [%#lx, %#lx) (blockSize=%lu, instShift=%u, maxPositions=%u); clamping index",
919+
branchPC, alignedPC,
920+
alignedPC + blockSize,
921+
static_cast<unsigned long>(blockSize), instShiftAmt, maxBranchPositions);
922+
position %= maxBranchPositions;
923+
}
924+
925+
return position;
910926
}
911927

912928
unsigned

0 commit comments

Comments
 (0)