2 * Copyright (c) 2014 The University of Wisconsin
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)
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are
12 * met: redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer;
14 * redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution;
17 * neither the name of the copyright holders nor the names of its
18 * contributors may be used to endorse or promote products derived from
19 * this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 * Authors: Vignyan Reddy, Dibakar Gope and Arthur Perais,
34 * from André Seznec's code.
37 #ifndef __CPU_PRED_LOOP_PREDICTOR_HH__
38 #define __CPU_PRED_LOOP_PREDICTOR_HH__
40 #include "base/statistics.hh"
41 #include "base/types.hh"
42 #include "sim/sim_object.hh"
44 struct LoopPredictorParams;
46 class LoopPredictor : public SimObject
49 const unsigned logSizeLoopPred;
50 const unsigned loopTableAgeBits;
51 const unsigned loopTableConfidenceBits;
52 const unsigned loopTableTagBits;
53 const unsigned loopTableIterBits;
54 const unsigned logLoopTableAssoc;
55 const uint8_t confidenceThreshold;
56 const uint16_t loopTagMask;
57 const uint16_t loopNumIterMask;
58 const int loopSetMask;
60 // Prediction Structures
61 // Loop Predictor Entry
66 uint16_t currentIterSpec; // only for useSpeculation
70 bool dir; // only for useDirectionBit
72 LoopEntry() : numIter(0), currentIter(0), currentIterSpec(0),
73 confidence(0), tag(0), age(0), dir(0) { }
78 int8_t loopUseCounter;
79 unsigned withLoopBits;
81 const bool useDirectionBit;
82 const bool useSpeculation;
83 const bool useHashing;
84 const bool restrictAllocation;
85 const unsigned initialLoopIter;
86 const unsigned initialLoopAge;
87 const bool optionalAgeReset;
90 Stats::Scalar loopPredictorCorrect;
91 Stats::Scalar loopPredictorWrong;
94 * Updates an unsigned counter based on up/down parameter
95 * @param ctr Reference to counter to update.
96 * @param up Boolean indicating if the counter is incremented/decremented
97 * If true it is incremented, if false it is decremented
98 * @param nbits Counter width.
100 static inline void unsignedCtrUpdate(uint8_t &ctr, bool up, unsigned nbits)
102 assert(nbits <= sizeof(uint8_t) << 3);
104 if (ctr < ((1 << nbits) - 1))
111 static inline void signedCtrUpdate(int8_t &ctr, bool up, unsigned nbits)
114 if (ctr < ((1 << (nbits - 1)) - 1))
117 if (ctr > -(1 << (nbits - 1)))
122 // Primary branch history entry
126 uint16_t currentIter;
132 int loopIndexB; // only for useHashing
137 : loopTag(0), currentIter(0),
139 loopPredValid(false), loopIndex(0), loopIndexB(0), loopHit(0),
145 * Computes the index used to access the
147 * @param pc_in The unshifted branch PC.
148 * @param instShiftAmt Shift the pc by as many bits
150 int lindex(Addr pc_in, unsigned instShiftAmt) const;
153 * Computes the index used to access the
155 * It may take hashing into account
156 * @param index Result of lindex function
157 * @param lowPcBits PC bits masked with set size
158 * @param way Way to be used
160 int finallindex(int lindex, int lowPcBits, int way) const;
163 * Get a branch prediction from the loop
165 * @param pc The unshifted branch PC.
166 * @param bi Pointer to information on the
168 * @param speculative Use speculative number of iterations
169 * @param instShiftAmt Shift the pc by as many bits (if hashing is not
171 * @result the result of the prediction, if it could be predicted
173 bool getLoop(Addr pc, BranchInfo* bi, bool speculative,
174 unsigned instShiftAmt) const;
177 * Updates the loop predictor.
178 * @param pc The unshifted branch PC.
179 * @param taken The actual branch outcome.
180 * @param bi Pointer to information on the
181 * prediction recorded at prediction time.
182 * @param tage_pred tage prediction of the branch
184 void loopUpdate(Addr pc, bool Taken, BranchInfo* bi, bool tage_pred);
187 * Speculatively updates the loop predictor
188 * iteration count (only for useSpeculation).
189 * @param taken The predicted branch outcome.
190 * @param bi Pointer to information on the prediction
191 * recorded at prediction time.
193 void specLoopUpdate(bool taken, BranchInfo* bi);
196 * Update LTAGE for conditional branches.
197 * @param branch_pc The unshifted branch PC.
198 * @param taken Actual branch outcome.
199 * @param tage_pred Prediction from TAGE
200 * @param bi Pointer to information on the prediction
201 * recorded at prediction time.
202 * @param instShiftAmt Number of bits to shift instructions
204 void condBranchUpdate(ThreadID tid, Addr branch_pc, bool taken,
205 bool tage_pred, BranchInfo* bi, unsigned instShiftAmt);
208 * Get the loop prediction
209 * @param tid The thread ID to select the global
211 * @param branch_pc The unshifted branch PC.
212 * @param cond_branch True if the branch is conditional.
213 * @param bi Reference to wrapping pointer to allow storing
214 * derived class prediction information in the base class.
215 * @param prev_pred_taken Result of the TAGE prediction
216 * @param instShiftAmt Shift the pc by as many bits
217 * @param instShiftAmt Shift the pc by as many bits
218 * @result the prediction, true if taken
221 ThreadID tid, Addr branch_pc, bool cond_branch,
222 BranchInfo* bi, bool prev_pred_taken, unsigned instShiftAmt);
226 * @param taken Actual branch outcome
227 * @param bi Pointer to information on the prediction
228 * recorded at prediction time.
230 void updateStats(bool taken, BranchInfo* bi);
232 void squashLoop(BranchInfo * bi);
234 void squash(ThreadID tid, BranchInfo *bi);
236 virtual bool calcConf(int index) const;
238 virtual bool optionalAgeInc() const;
240 virtual BranchInfo *makeBranchInfo();
243 * Gets the value of the loop use counter
244 * @return the loop use counter value
246 int8_t getLoopUseCounter() const
248 return loopUseCounter;
252 * Initialize the loop predictor
254 void init() override;
257 * Register stats for this object
259 void regStats() override;
261 LoopPredictor(LoopPredictorParams *p);
263 size_t getSizeInBits() const;
265 #endif//__CPU_PRED_LOOP_PREDICTOR_HH__