Deleted Added
sdiff udiff text old ( 13443:a111cb197897 ) new ( 13444:26f81be73cb7 )
full compact
1/*
2 * Copyright (c) 2014 The University of Wisconsin
3 *
4 * Copyright (c) 2006 INRIA (Institut National de Recherche en
5 * Informatique et en Automatique / French National Research Institute
6 * for Computer Science and Applied Mathematics)
7 *
8 * All rights reserved.

--- 35 unchanged lines hidden (view full) ---

44#include "base/logging.hh"
45#include "base/random.hh"
46#include "base/trace.hh"
47#include "debug/Fetch.hh"
48#include "debug/LTage.hh"
49
50LTAGE::LTAGE(const LTAGEParams *params)
51 : BPredUnit(params),
52 logRatioBiModalHystEntries(params->logRatioBiModalHystEntries),
53 logSizeLoopPred(params->logSizeLoopPred),
54 nHistoryTables(params->nHistoryTables),
55 tagTableCounterBits(params->tagTableCounterBits),
56 tagTableUBits(params->tagTableUBits),
57 histBufferSize(params->histBufferSize),
58 minHist(params->minHist),
59 maxHist(params->maxHist),
60 pathHistBits(params->pathHistBits),
61 loopTableAgeBits(params->loopTableAgeBits),
62 loopTableConfidenceBits(params->loopTableConfidenceBits),
63 loopTableTagBits(params->loopTableTagBits),
64 loopTableIterBits(params->loopTableIterBits),
65 logLoopTableAssoc(params->logLoopTableAssoc),
66 confidenceThreshold((1 << loopTableConfidenceBits) - 1),
67 loopTagMask((1 << loopTableTagBits) - 1),
68 loopNumIterMask((1 << loopTableIterBits) - 1),
69 tagTableTagWidths(params->tagTableTagWidths),
70 logTagTableSizes(params->logTagTableSizes),
71 threadHistory(params->numThreads),
72 logUResetPeriod(params->logUResetPeriod),
73 useAltOnNaBits(params->useAltOnNaBits),
74 withLoopBits(params->withLoopBits)
75{
76 // Current method for periodically resetting the u counter bits only
77 // works for 1 or 2 bits
78 // Also make sure that it is not 0
79 assert(tagTableUBits <= 2 && (tagTableUBits > 0));
80
81 // we use uint16_t type for these vales, so they cannot be more than
82 // 16 bits
83 assert(loopTableTagBits <= 16);
84 assert(loopTableIterBits <= 16);
85
86 assert(logSizeLoopPred >= logLoopTableAssoc);
87
88 // we use int type for the path history, so it cannot be more than
89 // its size
90 assert(pathHistBits <= (sizeof(int)*8));
91
92 // initialize the counter to half of the period
93 assert(logUResetPeriod != 0);
94 tCounter = ULL(1) << (logUResetPeriod - 1);
95
96 assert(params->histBufferSize > params->maxHist * 2);
97 useAltPredForNewlyAllocated = 0;
98
99 for (auto& history : threadHistory) {
100 history.pathHist = 0;
101 history.globalHistory = new uint8_t[histBufferSize];
102 history.gHist = history.globalHistory;
103 memset(history.gHist, 0, histBufferSize);
104 history.ptGhist = 0;
105 }

--- 4 unchanged lines hidden (view full) ---

110
111 for (int i = 2; i <= nHistoryTables; i++) {
112 histLengths[i] = (int) (((double) minHist *
113 pow ((double) (maxHist) / (double) minHist,
114 (double) (i - 1) / (double) ((nHistoryTables- 1))))
115 + 0.5);
116 }
117
118 assert(tagTableTagWidths.size() == (nHistoryTables+1));
119 assert(logTagTableSizes.size() == (nHistoryTables+1));
120
121 // First entry is for the Bimodal table and it is untagged in this
122 // implementation
123 assert(tagTableTagWidths[0] == 0);
124
125 for (auto& history : threadHistory) {
126 history.computeIndices = new FoldedHistory[nHistoryTables+1];
127 history.computeTags[0] = new FoldedHistory[nHistoryTables+1];
128 history.computeTags[1] = new FoldedHistory[nHistoryTables+1];
129
130 for (int i = 1; i <= nHistoryTables; i++) {
131 history.computeIndices[i].init(
132 histLengths[i], (logTagTableSizes[i]));
133 history.computeTags[0][i].init(
134 history.computeIndices[i].origLength, tagTableTagWidths[i]);
135 history.computeTags[1][i].init(
136 history.computeIndices[i].origLength, tagTableTagWidths[i]-1);
137 DPRINTF(LTage, "HistLength:%d, TTSize:%d, TTTWidth:%d\n",
138 histLengths[i], logTagTableSizes[i], tagTableTagWidths[i]);
139 }
140 }
141
142 const uint64_t bimodalTableSize = ULL(1) << logTagTableSizes[0];
143 btablePrediction.resize(bimodalTableSize, false);
144 btableHysteresis.resize(bimodalTableSize >> logRatioBiModalHystEntries,
145 true);
146
147 ltable = new LoopEntry[ULL(1) << logSizeLoopPred];
148 gtable = new TageEntry*[nHistoryTables + 1];
149 for (int i = 1; i <= nHistoryTables; i++) {
150 gtable[i] = new TageEntry[1<<(logTagTableSizes[i])];
151 }
152
153 tableIndices = new int [nHistoryTables+1];
154 tableTags = new int [nHistoryTables+1];
155
156 loopUseCounter = 0;
157}
158
159int
160LTAGE::bindex(Addr pc_in) const
161{
162 return ((pc_in >> instShiftAmt) & ((ULL(1) << (logTagTableSizes[0])) - 1));
163}
164
165int
166LTAGE::lindex(Addr pc_in) const
167{
168 // The loop table is implemented as a linear table
169 // If associativity is N (N being 1 << logLoopTableAssoc),
170 // the first N entries are for set 0, the next N entries are for set 1,
171 // and so on.
172 // Thus, this function calculates the set and then it gets left shifted
173 // by logLoopTableAssoc in order to return the index of the first of the
174 // N entries of the set
175 Addr mask = (ULL(1) << (logSizeLoopPred - logLoopTableAssoc)) - 1;
176 return (((pc_in >> instShiftAmt) & mask) << logLoopTableAssoc);
177}
178
179int
180LTAGE::F(int A, int size, int bank) const
181{
182 int A1, A2;
183
184 A = A & ((ULL(1) << size) - 1);
185 A1 = (A & ((ULL(1) << logTagTableSizes[bank]) - 1));
186 A2 = (A >> logTagTableSizes[bank]);
187 A2 = ((A2 << bank) & ((ULL(1) << logTagTableSizes[bank]) - 1))
188 + (A2 >> (logTagTableSizes[bank] - bank));
189 A = A1 ^ A2;
190 A = ((A << bank) & ((ULL(1) << logTagTableSizes[bank]) - 1))
191 + (A >> (logTagTableSizes[bank] - bank));
192 return (A);
193}
194
195
196// gindex computes a full hash of pc, ghist and pathHist
197int
198LTAGE::gindex(ThreadID tid, Addr pc, int bank) const
199{
200 int index;
201 int hlen = (histLengths[bank] > pathHistBits) ? pathHistBits :
202 histLengths[bank];
203 const Addr shiftedPc = pc >> instShiftAmt;
204 index =
205 shiftedPc ^
206 (shiftedPc >> ((int) abs(logTagTableSizes[bank] - bank) + 1)) ^
207 threadHistory[tid].computeIndices[bank].comp ^
208 F(threadHistory[tid].pathHist, hlen, bank);
209
210 return (index & ((ULL(1) << (logTagTableSizes[bank])) - 1));
211}
212
213
214// Tag computation
215uint16_t
216LTAGE::gtag(ThreadID tid, Addr pc, int bank) const
217{
218 int tag = (pc >> instShiftAmt) ^
219 threadHistory[tid].computeTags[0][bank].comp ^
220 (threadHistory[tid].computeTags[1][bank].comp << 1);
221
222 return (tag & ((ULL(1) << tagTableTagWidths[bank]) - 1));
223}
224
225
226// Up-down saturating counter
227void
228LTAGE::ctrUpdate(int8_t & ctr, bool taken, int nbits)
229{
230 assert(nbits <= sizeof(int8_t) << 3);

--- 51 unchanged lines hidden (view full) ---

282
283//loop prediction: only used if high confidence
284bool
285LTAGE::getLoop(Addr pc, BranchInfo* bi) const
286{
287 bi->loopHit = -1;
288 bi->loopPredValid = false;
289 bi->loopIndex = lindex(pc);
290 unsigned pcShift = instShiftAmt + logSizeLoopPred - logLoopTableAssoc;
291 bi->loopTag = ((pc) >> pcShift) & loopTagMask;
292
293 for (int i = 0; i < (1 << logLoopTableAssoc); i++) {
294 if (ltable[bi->loopIndex + i].tag == bi->loopTag) {
295 bi->loopHit = i;
296 bi->loopPredValid =
297 ltable[bi->loopIndex + i].confidence == confidenceThreshold;
298 bi->currentIter = ltable[bi->loopIndex + i].currentIterSpec;
299 if (ltable[bi->loopIndex + i].currentIterSpec + 1 ==
300 ltable[bi->loopIndex + i].numIter) {
301 return !(ltable[bi->loopIndex + i].dir);

--- 80 unchanged lines hidden (view full) ---

382 }
383 }
384 ltable[idx].currentIter = 0;
385 }
386
387 } else if (taken) {
388 //try to allocate an entry on taken branch
389 int nrand = random_mt.random<int>();
390 for (int i = 0; i < (1 << logLoopTableAssoc); i++) {
391 int loop_hit = (nrand + i) & ((1 << logLoopTableAssoc) - 1);
392 idx = bi->loopIndex + loop_hit;
393 if (ltable[idx].age == 0) {
394 DPRINTF(LTage, "Allocating loop pred entry for branch %lx\n",
395 pc);
396 ltable[idx].dir = !taken;
397 ltable[idx].tag = bi->loopTag;
398 ltable[idx].numIter = 0;
399 ltable[idx].age = (1 << loopTableAgeBits) - 1;

--- 155 unchanged lines hidden (view full) ---

555 if (bi->condBranch) {
556 DPRINTF(LTage, "Updating tables for branch:%lx; taken?:%d\n",
557 branch_pc, taken);
558 // first update the loop predictor
559 loopUpdate(pc, taken, bi);
560
561 if (bi->loopPredValid) {
562 if (bi->tagePred != bi->loopPred) {
563 ctrUpdate(loopUseCounter,
564 (bi->loopPred == taken),
565 withLoopBits);
566 }
567 }
568
569 // TAGE UPDATE
570 // try to allocate a new entries only if prediction was wrong
571 bool longest_match_pred = false;
572 bool alloc = (bi->tagePred != taken) && (bi->hitBank < nHistoryTables);
573 if (bi->hitBank > 0) {

--- 6 unchanged lines hidden (view full) ---

580 if (PseudoNewAlloc) {
581 if (longest_match_pred == taken) {
582 alloc = false;
583 }
584 // if it was delivering the correct prediction, no need to
585 // allocate new entry even if the overall prediction was false
586 if (longest_match_pred != bi->altTaken) {
587 ctrUpdate(useAltPredForNewlyAllocated,
588 bi->altTaken == taken, useAltOnNaBits);
589 }
590 }
591 }
592
593 if (alloc) {
594 // is there some "unuseful" entry to allocate
595 uint8_t min = 1;
596 for (int i = nHistoryTables; i > bi->hitBank; i--) {

--- 25 unchanged lines hidden (view full) ---

622 gtable[i][bi->tableIndices[i]].tag = bi->tableTags[i];
623 gtable[i][bi->tableIndices[i]].ctr = (taken) ? 0 : -1;
624 break;
625 }
626 }
627 }
628 //periodic reset of u: reset is not complete but bit by bit
629 tCounter++;
630 if ((tCounter & ((ULL(1) << logUResetPeriod) - 1)) == 0) {
631 // reset least significant bit
632 // most significant bit becomes least significant bit
633 for (int i = 1; i <= nHistoryTables; i++) {
634 for (int j = 0; j < (ULL(1) << logTagTableSizes[i]); j++) {
635 gtable[i][j].u = gtable[i][j].u >> 1;
636 }
637 }
638 }
639
640 if (bi->hitBank > 0) {
641 DPRINTF(LTage, "Updating tag table entry (%d,%d) for branch %lx\n",
642 bi->hitBank, bi->hitBankIndex, branch_pc);

--- 36 unchanged lines hidden (view full) ---

679 BranchInfo* bi = (BranchInfo*)(b);
680 ThreadHistory& tHist = threadHistory[tid];
681 // UPDATE HISTORIES
682 bool pathbit = ((branch_pc >> instShiftAmt) & 1);
683 //on a squash, return pointers to this and recompute indices.
684 //update user history
685 updateGHist(tHist.gHist, taken, tHist.globalHistory, tHist.ptGhist);
686 tHist.pathHist = (tHist.pathHist << 1) + pathbit;
687 tHist.pathHist = (tHist.pathHist & ((ULL(1) << pathHistBits) - 1));
688
689 bi->ptGhist = tHist.ptGhist;
690 bi->pathHist = tHist.pathHist;
691 //prepare next index and tag computations for user branchs
692 for (int i = 1; i <= nHistoryTables; i++)
693 {
694 bi->ci[i] = tHist.computeIndices[i].comp;
695 bi->ct0[i] = tHist.computeTags[0][i].comp;

--- 100 unchanged lines hidden ---