bpred_unit.hh revision 8229
1/* 2 * Copyright (c) 2004-2005 The Regents of The University of Michigan 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer; 9 * redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution; 12 * neither the name of the copyright holders nor the names of its 13 * contributors may be used to endorse or promote products derived from 14 * this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 * 28 * Authors: Kevin Lim 29 */ 30 31#ifndef __CPU_O3_BPRED_UNIT_HH__ 32#define __CPU_O3_BPRED_UNIT_HH__ 33 34#include <list> 35 36#include "base/statistics.hh" 37#include "base/types.hh" 38#include "cpu/pred/2bit_local.hh" 39#include "cpu/pred/btb.hh" 40#include "cpu/pred/ras.hh" 41#include "cpu/pred/tournament.hh" 42#include "cpu/inst_seq.hh" 43 44class DerivO3CPUParams; 45 46/** 47 * Basically a wrapper class to hold both the branch predictor 48 * and the BTB. 49 */ 50template<class Impl> 51class BPredUnit 52{ 53 private: 54 typedef typename Impl::DynInstPtr DynInstPtr; 55 56 enum PredType { 57 Local, 58 Tournament 59 }; 60 61 PredType predictor; 62 63 const std::string _name; 64 65 public: 66 67 /** 68 * @param params The params object, that has the size of the BP and BTB. 69 */ 70 BPredUnit(DerivO3CPUParams *params); 71 72 const std::string &name() const { return _name; } 73 74 /** 75 * Registers statistics. 76 */ 77 void regStats(); 78 79 void switchOut(); 80 81 void takeOverFrom(); 82 83 /** 84 * Predicts whether or not the instruction is a taken branch, and the 85 * target of the branch if it is taken. 86 * @param inst The branch instruction. 87 * @param PC The predicted PC is passed back through this parameter. 88 * @param tid The thread id. 89 * @return Returns if the branch is taken or not. 90 */ 91 bool predict(DynInstPtr &inst, TheISA::PCState &pc, ThreadID tid); 92 93 // @todo: Rename this function. 94 void BPUncond(void * &bp_history); 95 96 /** 97 * Tells the branch predictor to commit any updates until the given 98 * sequence number. 99 * @param done_sn The sequence number to commit any older updates up until. 100 * @param tid The thread id. 101 */ 102 void update(const InstSeqNum &done_sn, ThreadID tid); 103 104 /** 105 * Squashes all outstanding updates until a given sequence number. 106 * @param squashed_sn The sequence number to squash any younger updates up 107 * until. 108 * @param tid The thread id. 109 */ 110 void squash(const InstSeqNum &squashed_sn, ThreadID tid); 111 112 /** 113 * Squashes all outstanding updates until a given sequence number, and 114 * corrects that sn's update with the proper address and taken/not taken. 115 * @param squashed_sn The sequence number to squash any younger updates up 116 * until. 117 * @param corr_target The correct branch target. 118 * @param actually_taken The correct branch direction. 119 * @param tid The thread id. 120 */ 121 void squash(const InstSeqNum &squashed_sn, 122 const TheISA::PCState &corr_target, 123 bool actually_taken, ThreadID tid); 124 125 /** 126 * @param bp_history Pointer to the history object. The predictor 127 * will need to update any state and delete the object. 128 */ 129 void BPSquash(void *bp_history); 130 131 /** 132 * Looks up a given PC in the BP to see if it is taken or not taken. 133 * @param inst_PC The PC to look up. 134 * @param bp_history Pointer that will be set to an object that 135 * has the branch predictor state associated with the lookup. 136 * @return Whether the branch is taken or not taken. 137 */ 138 bool BPLookup(Addr instPC, void * &bp_history); 139 140 /** 141 * Looks up a given PC in the BTB to see if a matching entry exists. 142 * @param inst_PC The PC to look up. 143 * @return Whether the BTB contains the given PC. 144 */ 145 bool BTBValid(Addr instPC) 146 { return BTB.valid(instPC, 0); } 147 148 /** 149 * Looks up a given PC in the BTB to get the predicted target. 150 * @param inst_PC The PC to look up. 151 * @return The address of the target of the branch. 152 */ 153 TheISA::PCState BTBLookup(Addr instPC) 154 { return BTB.lookup(instPC, 0); } 155 156 /** 157 * Updates the BP with taken/not taken information. 158 * @param inst_PC The branch's PC that will be updated. 159 * @param taken Whether the branch was taken or not taken. 160 * @param bp_history Pointer to the branch predictor state that is 161 * associated with the branch lookup that is being updated. 162 * @todo Make this update flexible enough to handle a global predictor. 163 */ 164 void BPUpdate(Addr instPC, bool taken, void *bp_history); 165 166 /** 167 * Updates the BTB with the target of a branch. 168 * @param inst_PC The branch's PC that will be updated. 169 * @param target_PC The branch's target that will be added to the BTB. 170 */ 171 void BTBUpdate(Addr instPC, const TheISA::PCState &target) 172 { BTB.update(instPC, target, 0); } 173 174 void dump(); 175 176 private: 177 struct PredictorHistory { 178 /** 179 * Makes a predictor history struct that contains any 180 * information needed to update the predictor, BTB, and RAS. 181 */ 182 PredictorHistory(const InstSeqNum &seq_num, Addr instPC, 183 bool pred_taken, void *bp_history, 184 ThreadID _tid) 185 : seqNum(seq_num), pc(instPC), RASTarget(0), RASIndex(0), 186 tid(_tid), predTaken(pred_taken), usedRAS(0), 187 wasCall(0), bpHistory(bp_history) 188 {} 189 190 bool operator==(const PredictorHistory &entry) const { 191 return this->seqNum == entry.seqNum; 192 } 193 194 /** The sequence number for the predictor history entry. */ 195 InstSeqNum seqNum; 196 197 /** The PC associated with the sequence number. */ 198 Addr pc; 199 200 /** The RAS target (only valid if a return). */ 201 TheISA::PCState RASTarget; 202 203 /** The RAS index of the instruction (only valid if a call). */ 204 unsigned RASIndex; 205 206 /** The thread id. */ 207 ThreadID tid; 208 209 /** Whether or not it was predicted taken. */ 210 bool predTaken; 211 212 /** Whether or not the RAS was used. */ 213 bool usedRAS; 214 215 /** Whether or not the instruction was a call. */ 216 bool wasCall; 217 218 /** Pointer to the history object passed back from the branch 219 * predictor. It is used to update or restore state of the 220 * branch predictor. 221 */ 222 void *bpHistory; 223 }; 224 225 typedef std::list<PredictorHistory> History; 226 typedef typename History::iterator HistoryIt; 227 228 /** 229 * The per-thread predictor history. This is used to update the predictor 230 * as instructions are committed, or restore it to the proper state after 231 * a squash. 232 */ 233 History predHist[Impl::MaxThreads]; 234 235 /** The local branch predictor. */ 236 LocalBP *localBP; 237 238 /** The tournament branch predictor. */ 239 TournamentBP *tournamentBP; 240 241 /** The BTB. */ 242 DefaultBTB BTB; 243 244 /** The per-thread return address stack. */ 245 ReturnAddrStack RAS[Impl::MaxThreads]; 246 247 /** Stat for number of BP lookups. */ 248 Stats::Scalar lookups; 249 /** Stat for number of conditional branches predicted. */ 250 Stats::Scalar condPredicted; 251 /** Stat for number of conditional branches predicted incorrectly. */ 252 Stats::Scalar condIncorrect; 253 /** Stat for number of BTB lookups. */ 254 Stats::Scalar BTBLookups; 255 /** Stat for number of BTB hits. */ 256 Stats::Scalar BTBHits; 257 /** Stat for number of times the BTB is correct. */ 258 Stats::Scalar BTBCorrect; 259 /** Stat for number of times the RAS is used to get a target. */ 260 Stats::Scalar usedRAS; 261 /** Stat for number of times the RAS is incorrect. */ 262 Stats::Scalar RASIncorrect; 263}; 264 265#endif // __CPU_O3_BPRED_UNIT_HH__ 266