bpred_unit.hh revision 5529
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 "base/statistics.hh" 35#include "cpu/inst_seq.hh" 36 37#include "cpu/o3/2bit_local_pred.hh" 38#include "cpu/o3/btb.hh" 39#include "cpu/o3/ras.hh" 40#include "cpu/o3/tournament_pred.hh" 41 42#include "sim/host.hh" 43 44#include <list> 45 46class DerivO3CPUParams; 47 48/** 49 * Basically a wrapper class to hold both the branch predictor 50 * and the BTB. 51 */ 52template<class Impl> 53class BPredUnit 54{ 55 private: 56 typedef typename Impl::DynInstPtr DynInstPtr; 57 58 enum PredType { 59 Local, 60 Tournament 61 }; 62 63 PredType predictor; 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 /** 73 * Registers statistics. 74 */ 75 void regStats(); 76 77 void switchOut(); 78 79 void takeOverFrom(); 80 81 /** 82 * Predicts whether or not the instruction is a taken branch, and the 83 * target of the branch if it is taken. 84 * @param inst The branch instruction. 85 * @param PC The predicted PC is passed back through this parameter. 86 * @param tid The thread id. 87 * @return Returns if the branch is taken or not. 88 */ 89 bool predict(DynInstPtr &inst, Addr &PC, unsigned tid); 90 91 // @todo: Rename this function. 92 void BPUncond(void * &bp_history); 93 94 /** 95 * Tells the branch predictor to commit any updates until the given 96 * sequence number. 97 * @param done_sn The sequence number to commit any older updates up until. 98 * @param tid The thread id. 99 */ 100 void update(const InstSeqNum &done_sn, unsigned tid); 101 102 /** 103 * Squashes all outstanding updates until a given sequence number. 104 * @param squashed_sn The sequence number to squash any younger updates up 105 * until. 106 * @param tid The thread id. 107 */ 108 void squash(const InstSeqNum &squashed_sn, unsigned tid); 109 110 /** 111 * Squashes all outstanding updates until a given sequence number, and 112 * corrects that sn's update with the proper address and taken/not taken. 113 * @param squashed_sn The sequence number to squash any younger updates up 114 * until. 115 * @param corr_target The correct branch target. 116 * @param actually_taken The correct branch direction. 117 * @param tid The thread id. 118 */ 119 void squash(const InstSeqNum &squashed_sn, const Addr &corr_target, 120 bool actually_taken, unsigned tid); 121 122 /** 123 * @param bp_history Pointer to the history object. The predictor 124 * will need to update any state and delete the object. 125 */ 126 void BPSquash(void *bp_history); 127 128 /** 129 * Looks up a given PC in the BP to see if it is taken or not taken. 130 * @param inst_PC The PC to look up. 131 * @param bp_history Pointer that will be set to an object that 132 * has the branch predictor state associated with the lookup. 133 * @return Whether the branch is taken or not taken. 134 */ 135 bool BPLookup(Addr &inst_PC, void * &bp_history); 136 137 /** 138 * Looks up a given PC in the BTB to see if a matching entry exists. 139 * @param inst_PC The PC to look up. 140 * @return Whether the BTB contains the given PC. 141 */ 142 bool BTBValid(Addr &inst_PC) 143 { return BTB.valid(inst_PC, 0); } 144 145 /** 146 * Looks up a given PC in the BTB to get the predicted target. 147 * @param inst_PC The PC to look up. 148 * @return The address of the target of the branch. 149 */ 150 Addr BTBLookup(Addr &inst_PC) 151 { return BTB.lookup(inst_PC, 0); } 152 153 /** 154 * Updates the BP with taken/not taken information. 155 * @param inst_PC The branch's PC that will be updated. 156 * @param taken Whether the branch was taken or not taken. 157 * @param bp_history Pointer to the branch predictor state that is 158 * associated with the branch lookup that is being updated. 159 * @todo Make this update flexible enough to handle a global predictor. 160 */ 161 void BPUpdate(Addr &inst_PC, bool taken, void *bp_history); 162 163 /** 164 * Updates the BTB with the target of a branch. 165 * @param inst_PC The branch's PC that will be updated. 166 * @param target_PC The branch's target that will be added to the BTB. 167 */ 168 void BTBUpdate(Addr &inst_PC, Addr &target_PC) 169 { BTB.update(inst_PC, target_PC,0); } 170 171 void dump(); 172 173 private: 174 struct PredictorHistory { 175 /** 176 * Makes a predictor history struct that contains any 177 * information needed to update the predictor, BTB, and RAS. 178 */ 179 PredictorHistory(const InstSeqNum &seq_num, const Addr &inst_PC, 180 const bool pred_taken, void *bp_history, 181 const unsigned _tid) 182 : seqNum(seq_num), PC(inst_PC), RASTarget(0), 183 RASIndex(0), tid(_tid), predTaken(pred_taken), usedRAS(0), 184 wasCall(0), bpHistory(bp_history) 185 { } 186 187 /** The sequence number for the predictor history entry. */ 188 InstSeqNum seqNum; 189 190 /** The PC associated with the sequence number. */ 191 Addr PC; 192 193 /** The RAS target (only valid if a return). */ 194 Addr RASTarget; 195 196 /** The RAS index of the instruction (only valid if a call). */ 197 unsigned RASIndex; 198 199 /** The thread id. */ 200 unsigned tid; 201 202 /** Whether or not it was predicted taken. */ 203 bool predTaken; 204 205 /** Whether or not the RAS was used. */ 206 bool usedRAS; 207 208 /** Whether or not the instruction was a call. */ 209 bool wasCall; 210 211 /** Pointer to the history object passed back from the branch 212 * predictor. It is used to update or restore state of the 213 * branch predictor. 214 */ 215 void *bpHistory; 216 }; 217 218 typedef std::list<PredictorHistory> History; 219 220 /** 221 * The per-thread predictor history. This is used to update the predictor 222 * as instructions are committed, or restore it to the proper state after 223 * a squash. 224 */ 225 History predHist[Impl::MaxThreads]; 226 227 /** The local branch predictor. */ 228 LocalBP *localBP; 229 230 /** The tournament branch predictor. */ 231 TournamentBP *tournamentBP; 232 233 /** The BTB. */ 234 DefaultBTB BTB; 235 236 /** The per-thread return address stack. */ 237 ReturnAddrStack RAS[Impl::MaxThreads]; 238 239 /** Stat for number of BP lookups. */ 240 Stats::Scalar<> lookups; 241 /** Stat for number of conditional branches predicted. */ 242 Stats::Scalar<> condPredicted; 243 /** Stat for number of conditional branches predicted incorrectly. */ 244 Stats::Scalar<> condIncorrect; 245 /** Stat for number of BTB lookups. */ 246 Stats::Scalar<> BTBLookups; 247 /** Stat for number of BTB hits. */ 248 Stats::Scalar<> BTBHits; 249 /** Stat for number of times the BTB is correct. */ 250 Stats::Scalar<> BTBCorrect; 251 /** Stat for number of times the RAS is used to get a target. */ 252 Stats::Scalar<> usedRAS; 253 /** Stat for number of times the RAS is incorrect. */ 254 Stats::Scalar<> RASIncorrect; 255}; 256 257#endif // __CPU_O3_BPRED_UNIT_HH__ 258