* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- *
- * Authors: Kevin Lim
*/
+#include "cpu/pred/tournament.hh"
+
#include "base/bitfield.hh"
#include "base/intmath.hh"
-#include "cpu/pred/tournament.hh"
TournamentBP::TournamentBP(const TournamentBPParams *params)
: BPredUnit(params),
localPredictorSize(params->localPredictorSize),
localCtrBits(params->localCtrBits),
+ localCtrs(localPredictorSize, SatCounter(localCtrBits)),
localHistoryTableSize(params->localHistoryTableSize),
localHistoryBits(ceilLog2(params->localPredictorSize)),
globalPredictorSize(params->globalPredictorSize),
globalCtrBits(params->globalCtrBits),
+ globalCtrs(globalPredictorSize, SatCounter(globalCtrBits)),
globalHistory(params->numThreads, 0),
globalHistoryBits(
ceilLog2(params->globalPredictorSize) >
ceilLog2(params->globalPredictorSize) :
ceilLog2(params->choicePredictorSize)),
choicePredictorSize(params->choicePredictorSize),
- choiceCtrBits(params->choiceCtrBits)
+ choiceCtrBits(params->choiceCtrBits),
+ choiceCtrs(choicePredictorSize, SatCounter(choiceCtrBits))
{
if (!isPowerOf2(localPredictorSize)) {
fatal("Invalid local predictor size!\n");
fatal("Invalid global predictor size!\n");
}
- //Set up the array of counters for the local predictor
- localCtrs.resize(localPredictorSize);
-
- for (int i = 0; i < localPredictorSize; ++i)
- localCtrs[i].setBits(localCtrBits);
-
localPredictorMask = mask(localHistoryBits);
if (!isPowerOf2(localHistoryTableSize)) {
for (int i = 0; i < localHistoryTableSize; ++i)
localHistoryTable[i] = 0;
- //Setup the array of counters for the global predictor
- globalCtrs.resize(globalPredictorSize);
-
- for (int i = 0; i < globalPredictorSize; ++i)
- globalCtrs[i].setBits(globalCtrBits);
-
// Set up the global history mask
// this is equivalent to mask(log2(globalPredictorSize)
globalHistoryMask = globalPredictorSize - 1;
// this is equivalent to mask(log2(choicePredictorSize)
choiceHistoryMask = choicePredictorSize - 1;
- //Setup the array of counters for the choice predictor
- choiceCtrs.resize(choicePredictorSize);
-
- for (int i = 0; i < choicePredictorSize; ++i)
- choiceCtrs[i].setBits(choiceCtrBits);
-
//Set up historyRegisterMask
historyRegisterMask = mask(globalHistoryBits);
local_history_idx = calcLocHistIdx(branch_addr);
local_predictor_idx = localHistoryTable[local_history_idx]
& localPredictorMask;
- local_prediction = localCtrs[local_predictor_idx].read() > localThreshold;
+ local_prediction = localCtrs[local_predictor_idx] > localThreshold;
//Lookup in the global predictor to get its branch prediction
global_prediction = globalThreshold <
- globalCtrs[globalHistory[tid] & globalHistoryMask].read();
+ globalCtrs[globalHistory[tid] & globalHistoryMask];
//Lookup in the choice predictor to see which one to use
choice_prediction = choiceThreshold <
- choiceCtrs[globalHistory[tid] & choiceHistoryMask].read();
+ choiceCtrs[globalHistory[tid] & choiceHistoryMask];
// Create BPHistory and pass it back to be recorded.
BPHistory *history = new BPHistory;
void
TournamentBP::update(ThreadID tid, Addr branch_addr, bool taken,
- void *bp_history, bool squashed)
+ void *bp_history, bool squashed,
+ const StaticInstPtr & inst, Addr corrTarget)
{
- unsigned local_history_idx;
- unsigned local_predictor_idx M5_VAR_USED;
- unsigned local_predictor_hist;
+ assert(bp_history);
- // Get the local predictor's current prediction
- local_history_idx = calcLocHistIdx(branch_addr);
- local_predictor_hist = localHistoryTable[local_history_idx];
- local_predictor_idx = local_predictor_hist & localPredictorMask;
-
- if (bp_history) {
- BPHistory *history = static_cast<BPHistory *>(bp_history);
- // Update may also be called if the Branch target is incorrect even if
- // the prediction is correct. In that case do not update the counters.
- bool historyPred = false;
- unsigned old_local_pred_index = history->localHistory &
- localPredictorMask;
-
- bool old_local_pred_valid = history->localHistory !=
- invalidPredictorIndex;
+ BPHistory *history = static_cast<BPHistory *>(bp_history);
- assert(old_local_pred_index < localPredictorSize);
+ unsigned local_history_idx = calcLocHistIdx(branch_addr);
- if (history->globalUsed) {
- historyPred = history->globalPredTaken;
- } else {
- historyPred = history->localPredTaken;
- }
- if (historyPred != taken || !squashed) {
- // Update the choice predictor to tell it which one was correct if
- // there was a prediction.
- if (history->localPredTaken != history->globalPredTaken) {
- // If the local prediction matches the actual outcome,
- // decerement the counter. Otherwise increment the
- // counter.
- unsigned choice_predictor_idx =
- history->globalHistory & choiceHistoryMask;
- if (history->localPredTaken == taken) {
- choiceCtrs[choice_predictor_idx].decrement();
- } else if (history->globalPredTaken == taken) {
- choiceCtrs[choice_predictor_idx].increment();
- }
-
- }
-
- // Update the counters and local history with the proper
- // resolution of the branch. Global history is updated
- // speculatively and restored upon squash() calls, so it does not
- // need to be updated.
- unsigned global_predictor_idx =
- history->globalHistory & globalHistoryMask;
- if (taken) {
- globalCtrs[global_predictor_idx].increment();
- if (old_local_pred_valid) {
- localCtrs[old_local_pred_index].increment();
- }
- } else {
- globalCtrs[global_predictor_idx].decrement();
- if (old_local_pred_valid) {
- localCtrs[old_local_pred_index].decrement();
- }
- }
- }
- if (squashed) {
- if (taken) {
- globalHistory[tid] = (history->globalHistory << 1) | 1;
- globalHistory[tid] = globalHistory[tid] & historyRegisterMask;
- if (old_local_pred_valid) {
- localHistoryTable[local_history_idx] =
- (history->localHistory << 1) | 1;
- }
- } else {
- globalHistory[tid] = (history->globalHistory << 1);
- globalHistory[tid] = globalHistory[tid] & historyRegisterMask;
- if (old_local_pred_valid) {
- localHistoryTable[local_history_idx] =
- history->localHistory << 1;
- }
- }
+ assert(local_history_idx < localHistoryTableSize);
- } else {
- // We're done with this history, now delete it.
- delete history;
+ // Unconditional branches do not use local history.
+ bool old_local_pred_valid = history->localHistory !=
+ invalidPredictorIndex;
+
+ // If this is a misprediction, restore the speculatively
+ // updated state (global history register and local history)
+ // and update again.
+ if (squashed) {
+ // Global history restore and update
+ globalHistory[tid] = (history->globalHistory << 1) | taken;
+ globalHistory[tid] &= historyRegisterMask;
+
+ // Local history restore and update.
+ if (old_local_pred_valid) {
+ localHistoryTable[local_history_idx] =
+ (history->localHistory << 1) | taken;
}
- }
- assert(local_history_idx < localHistoryTableSize);
+ return;
+ }
+ unsigned old_local_pred_index = history->localHistory &
+ localPredictorMask;
+
+ assert(old_local_pred_index < localPredictorSize);
+
+ // Update the choice predictor to tell it which one was correct if
+ // there was a prediction.
+ if (history->localPredTaken != history->globalPredTaken &&
+ old_local_pred_valid)
+ {
+ // If the local prediction matches the actual outcome,
+ // decrement the counter. Otherwise increment the
+ // counter.
+ unsigned choice_predictor_idx =
+ history->globalHistory & choiceHistoryMask;
+ if (history->localPredTaken == taken) {
+ choiceCtrs[choice_predictor_idx]--;
+ } else if (history->globalPredTaken == taken) {
+ choiceCtrs[choice_predictor_idx]++;
+ }
+ }
-}
+ // Update the counters with the proper
+ // resolution of the branch. Histories are updated
+ // speculatively, restored upon squash() calls, and
+ // recomputed upon update(squash = true) calls,
+ // so they do not need to be updated.
+ unsigned global_predictor_idx =
+ history->globalHistory & globalHistoryMask;
+ if (taken) {
+ globalCtrs[global_predictor_idx]++;
+ if (old_local_pred_valid) {
+ localCtrs[old_local_pred_index]++;
+ }
+ } else {
+ globalCtrs[global_predictor_idx]--;
+ if (old_local_pred_valid) {
+ localCtrs[old_local_pred_index]--;
+ }
+ }
-void
-TournamentBP::retireSquashed(ThreadID tid, void *bp_history)
-{
- BPHistory *history = static_cast<BPHistory *>(bp_history);
+ // We're done with this history, now delete it.
delete history;
}
return new TournamentBP(this);
}
-unsigned
-TournamentBP::getGHR(ThreadID tid, void *bp_history) const
-{
- return static_cast<BPHistory *>(bp_history)->globalHistory;
-}
-
#ifdef DEBUG
int
TournamentBP::BPHistory::newCount = 0;