Skip to content
3 changes: 2 additions & 1 deletion src/cpu/pred/btb/btb_tage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,8 @@ BTBTAGE::generateSinglePrediction(const BTBEntry &btb_entry,
// Use base table instead of btb_entry.ctr
Addr base_idx = getBaseTableIndex(startPC);
unsigned branch_idx = getBranchIndexInBlock(btb_entry.pc, startPC);
bool base_taken = getDelay() != 0 ? baseTable[base_idx][branch_idx] >= 0 : btb_entry.ctr >= 0;
//bool base_taken = getDelay() != 0 ? baseTable[base_idx][branch_idx] >= 0 : btb_entry.ctr >= 0;
bool base_taken = btb_entry.ctr >= 0;
bool alt_pred = alt_provided ? alt_taken : base_taken; // if alt provided, use alt prediction, otherwise use base

// use_alt_on_na gating: when provider weak, consult per-PC counter
Expand Down
43 changes: 38 additions & 5 deletions src/cpu/pred/btb/mbtb.cc
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,8 @@ MBTB::lookupSingleBlock(Addr block_pc)
for (auto &way : btb_set) {
if (way.valid && way.tag == current_tag) {
res.push_back(way);
way.tick = curTick(); // Update timestamp for MRU
bool this_branch_taken = way.isIndirect||way.isDirect||way.alwaysTaken||(way.ctr >= 0);
way.tick = this_branch_taken ? curTick() : way.tick; // Update timestamp for MRU
std::make_heap(target_mru[btb_idx].begin(), target_mru[btb_idx].end(), older());
}
}
Expand Down Expand Up @@ -539,7 +540,10 @@ MBTB::updateBTBEntry(const BTBEntry& entry, const FetchStream &stream)
}

auto entry_to_write = buildUpdatedEntry(entry, existing_ptr, stream, btb_tag);
auto ticked_entry = TickedBTBEntry(entry_to_write, curTick());
auto ticked_entry = TickedBTBEntry(entry_to_write, it->tick);
if (stream.exeBranchInfo.pc == ticked_entry.pc && stream.exeTaken) {
ticked_entry.tick = curTick();
}

if (found) {
// Update in-place in SRAM set
Expand Down Expand Up @@ -691,10 +695,39 @@ MBTB::update(const FetchStream &stream)
std::static_pointer_cast<BTBMeta>(stream.predMetas[getComponentIdx()]).get());

// only update btb entry for control squash T-> NT or NT -> T
if (stream.squashType == SQUASH_CTRL) {
warn_if(stream.exeBranchInfo.pc > stream.updateEndInstPC, "exeBranchInfo.pc > updateEndInstPC");
updateBTBEntry(stream.exeBranchInfo, stream);
// if (stream.squashType == SQUASH_CTRL) {
// warn_if(stream.exeBranchInfo.pc > stream.updateEndInstPC, "exeBranchInfo.pc > updateEndInstPC");
// updateBTBEntry(stream.exeBranchInfo, stream);
// }
auto entries_need_update = prepareUpdateEntries(stream);
for (auto &entry : entries_need_update) {
updateBTBEntry(entry, stream);
}
}


std::vector<BTBEntry>
MBTB::prepareUpdateEntries(const FetchStream &stream) {
auto all_entries = stream.updateBTBEntries;

// Add potential new BTB entry if it's a btb miss during prediction
if (!stream.updateIsOldEntry) {
BTBEntry potential_new_entry = stream.updateNewBTBEntry;
bool new_entry_taken = stream.exeTaken && stream.getControlPC() == potential_new_entry.pc;
if (!new_entry_taken) {
potential_new_entry.alwaysTaken = false;
}
all_entries.push_back(potential_new_entry);
}

// Filter: only keep conditional branches that are not always taken
if (getResolvedUpdate()) {
auto remove_it = std::remove_if(all_entries.begin(), all_entries.end(),
[](const BTBEntry &e) { return !( e.resolved); });
all_entries.erase(remove_it, all_entries.end());
}

return all_entries;
}

/**
Expand Down
2 changes: 2 additions & 0 deletions src/cpu/pred/btb/mbtb.hh
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ class MBTB : public TimedBaseBTBPredictor
*/
void update(const FetchStream &stream) override;

std::vector<BTBEntry> prepareUpdateEntries(const FetchStream &stream);

void printBTBEntry(const BTBEntry &e, uint64_t tick = 0) {
DPRINTF(BTB, "BTB entry: valid %d, pc:%#lx, tag: %#lx, size:%d, target:%#lx, \
cond:%d, indirect:%d, call:%d, return:%d, always_taken:%d, tick:%lu\n",
Expand Down
2 changes: 2 additions & 0 deletions src/cpu/pred/btb/stream_struct.hh
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ struct BranchInfo
bool resolved;
bool isCond;
bool isIndirect;
bool isDirect;
bool isCall;
bool isReturn;
uint8_t size;
Expand All @@ -103,6 +104,7 @@ struct BranchInfo
resolved(false),
isCond(static_inst->isCondCtrl()),
isIndirect(static_inst->isIndirectCtrl()),
isDirect(static_inst->isDirectCtrl()),
isCall(static_inst->isCall()),
isReturn(static_inst->isReturn() && !static_inst->isNonSpeculative() && !static_inst->isDirectCtrl()),
size(size)
Expand Down