1/*************************************************************************************** 2* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences 3* Copyright (c) 2020-2021 Peng Cheng Laboratory 4* 5* XiangShan is licensed under Mulan PSL v2. 6* You can use this software according to the terms and conditions of the Mulan PSL v2. 7* You may obtain a copy of Mulan PSL v2 at: 8* http://license.coscl.org.cn/MulanPSL2 9* 10* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, 11* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, 12* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. 13* 14* See the Mulan PSL v2 for more details. 15***************************************************************************************/ 16 17package xiangshan.frontend 18 19import chipsalliance.rocketchip.config.Parameters 20import chisel3._ 21import chisel3.experimental.chiselName 22import chisel3.util._ 23import xiangshan._ 24import utils._ 25 26import scala.math.min 27 28trait HasBPUConst extends HasXSParameter { 29 val MaxMetaLength = 512 // TODO: Reduce meta length 30 val MaxBasicBlockSize = 32 31 val LHistoryLength = 32 32 // val numBr = 2 33 val useBPD = true 34 val useLHist = true 35 val numBrSlot = numBr-1 36 val totalSlot = numBrSlot + 1 37 38 def BP_STAGES = (0 until 3).map(_.U(2.W)) 39 def BP_S1 = BP_STAGES(0) 40 def BP_S2 = BP_STAGES(1) 41 def BP_S3 = BP_STAGES(2) 42 val numBpStages = BP_STAGES.length 43 44 val debug = true 45 val resetVector = 0x10000000L 46 // TODO: Replace log2Up by log2Ceil 47} 48 49trait HasBPUParameter extends HasXSParameter with HasBPUConst { 50 val BPUDebug = true && !env.FPGAPlatform && env.EnablePerfDebug 51 val EnableCFICommitLog = true 52 val EnbaleCFIPredLog = true 53 val EnableBPUTimeRecord = (EnableCFICommitLog || EnbaleCFIPredLog) && !env.FPGAPlatform 54 val EnableCommit = false 55} 56 57class BPUCtrl(implicit p: Parameters) extends XSBundle { 58 val ubtb_enable = Bool() 59 val btb_enable = Bool() 60 val bim_enable = Bool() 61 val tage_enable = Bool() 62 val sc_enable = Bool() 63 val ras_enable = Bool() 64 val loop_enable = Bool() 65} 66 67trait BPUUtils extends HasXSParameter { 68 // circular shifting 69 def circularShiftLeft(source: UInt, len: Int, shamt: UInt): UInt = { 70 val res = Wire(UInt(len.W)) 71 val higher = source << shamt 72 val lower = source >> (len.U - shamt) 73 res := higher | lower 74 res 75 } 76 77 def circularShiftRight(source: UInt, len: Int, shamt: UInt): UInt = { 78 val res = Wire(UInt(len.W)) 79 val higher = source << (len.U - shamt) 80 val lower = source >> shamt 81 res := higher | lower 82 res 83 } 84 85 // To be verified 86 def satUpdate(old: UInt, len: Int, taken: Bool): UInt = { 87 val oldSatTaken = old === ((1 << len)-1).U 88 val oldSatNotTaken = old === 0.U 89 Mux(oldSatTaken && taken, ((1 << len)-1).U, 90 Mux(oldSatNotTaken && !taken, 0.U, 91 Mux(taken, old + 1.U, old - 1.U))) 92 } 93 94 def signedSatUpdate(old: SInt, len: Int, taken: Bool): SInt = { 95 val oldSatTaken = old === ((1 << (len-1))-1).S 96 val oldSatNotTaken = old === (-(1 << (len-1))).S 97 Mux(oldSatTaken && taken, ((1 << (len-1))-1).S, 98 Mux(oldSatNotTaken && !taken, (-(1 << (len-1))).S, 99 Mux(taken, old + 1.S, old - 1.S))) 100 } 101 102 def getFallThroughAddr(start: UInt, carry: Bool, pft: UInt) = { 103 val higher = start.head(VAddrBits-log2Ceil(PredictWidth)-instOffsetBits) 104 Cat(Mux(carry, higher+1.U, higher), pft, 0.U(instOffsetBits.W)) 105 } 106 107 def foldTag(tag: UInt, l: Int): UInt = { 108 val nChunks = (tag.getWidth + l - 1) / l 109 val chunks = (0 until nChunks).map { i => 110 tag(min((i+1)*l, tag.getWidth)-1, i*l) 111 } 112 ParallelXOR(chunks) 113 } 114} 115 116// class BranchPredictionUpdate(implicit p: Parameters) extends XSBundle with HasBPUConst { 117// val pc = UInt(VAddrBits.W) 118// val br_offset = Vec(num_br, UInt(log2Up(MaxBasicBlockSize).W)) 119// val br_mask = Vec(MaxBasicBlockSize, Bool()) 120// 121// val jmp_valid = Bool() 122// val jmp_type = UInt(3.W) 123// 124// val is_NextMask = Vec(FetchWidth*2, Bool()) 125// 126// val cfi_idx = Valid(UInt(log2Ceil(MaxBasicBlockSize).W)) 127// val cfi_mispredict = Bool() 128// val cfi_is_br = Bool() 129// val cfi_is_jal = Bool() 130// val cfi_is_jalr = Bool() 131// 132// val ghist = new ShiftingGlobalHistory() 133// 134// val target = UInt(VAddrBits.W) 135// 136// val meta = UInt(MaxMetaLength.W) 137// val spec_meta = UInt(MaxMetaLength.W) 138// 139// def taken = cfi_idx.valid 140// } 141 142 143class BasePredictorInput (implicit p: Parameters) extends XSBundle with HasBPUConst { 144 def nInputs = 1 145 146 val s0_pc = UInt(VAddrBits.W) 147 148 val folded_hist = new AllFoldedHistories(foldedGHistInfos) 149 val ghist = UInt(HistoryLength.W) 150 151 val resp_in = Vec(nInputs, new BranchPredictionResp) 152 153 // val final_preds = Vec(numBpStages, new) 154 // val toFtq_fire = Bool() 155 156 // val s0_all_ready = Bool() 157} 158 159class BasePredictorOutput (implicit p: Parameters) extends XSBundle with HasBPUConst { 160 val last_stage_meta = UInt(MaxMetaLength.W) // This is use by composer 161 val resp = new BranchPredictionResp 162 163 // These store in meta, extract in composer 164 // val rasSp = UInt(log2Ceil(RasSize).W) 165 // val rasTop = new RASEntry 166 // val specCnt = Vec(PredictWidth, UInt(10.W)) 167} 168 169class BasePredictorIO (implicit p: Parameters) extends XSBundle with HasBPUConst { 170 val in = Flipped(DecoupledIO(new BasePredictorInput)) // TODO: Remove DecoupledIO 171 // val out = DecoupledIO(new BasePredictorOutput) 172 val out = Output(new BasePredictorOutput) 173 // val flush_out = Valid(UInt(VAddrBits.W)) 174 175 // val ctrl = Input(new BPUCtrl()) 176 177 val s0_fire = Input(Bool()) 178 val s1_fire = Input(Bool()) 179 val s2_fire = Input(Bool()) 180 val s3_fire = Input(Bool()) 181 182 val s2_redirect = Input(Bool()) 183 val s3_redirect = Input(Bool()) 184 185 val s1_ready = Output(Bool()) 186 val s2_ready = Output(Bool()) 187 val s3_ready = Output(Bool()) 188 189 val update = Flipped(Valid(new BranchPredictionUpdate)) 190 val redirect = Flipped(Valid(new BranchPredictionRedirect)) 191} 192 193abstract class BasePredictor(implicit p: Parameters) extends XSModule 194 with HasBPUConst with BPUUtils with HasPerfEvents { 195 val meta_size = 0 196 val spec_meta_size = 0 197 val io = IO(new BasePredictorIO()) 198 199 io.out.resp := io.in.bits.resp_in(0) 200 201 io.out.last_stage_meta := 0.U 202 203 io.in.ready := !io.redirect.valid 204 205 io.s1_ready := true.B 206 io.s2_ready := true.B 207 io.s3_ready := true.B 208 209 val s0_pc = WireInit(io.in.bits.s0_pc) // fetchIdx(io.f0_pc) 210 val s1_pc = RegEnable(s0_pc, resetVector.U, io.s0_fire) 211 val s2_pc = RegEnable(s1_pc, io.s1_fire) 212 val s3_pc = RegEnable(s2_pc, io.s2_fire) 213 214 io.out.resp.s1.pc := s1_pc 215 io.out.resp.s2.pc := s2_pc 216 io.out.resp.s3.pc := s3_pc 217 218 val perfEvents: Seq[(String, UInt)] = Seq() 219 220 221 def getFoldedHistoryInfo: Option[Set[FoldedHistoryInfo]] = None 222} 223 224class FakePredictor(implicit p: Parameters) extends BasePredictor { 225 io.in.ready := true.B 226 io.out.last_stage_meta := 0.U 227 io.out.resp := io.in.bits.resp_in(0) 228} 229 230class BpuToFtqIO(implicit p: Parameters) extends XSBundle { 231 val resp = DecoupledIO(new BpuToFtqBundle()) 232} 233 234class PredictorIO(implicit p: Parameters) extends XSBundle { 235 val bpu_to_ftq = new BpuToFtqIO() 236 val ftq_to_bpu = Flipped(new FtqToBpuIO()) 237} 238 239@chiselName 240class Predictor(implicit p: Parameters) extends XSModule with HasBPUConst with HasPerfEvents with HasCircularQueuePtrHelper { 241 val io = IO(new PredictorIO) 242 243 val predictors = Module(if (useBPD) new Composer else new FakePredictor) 244 245 val s0_fire, s1_fire, s2_fire, s3_fire = Wire(Bool()) 246 val s1_valid, s2_valid, s3_valid = RegInit(false.B) 247 val s1_ready, s2_ready, s3_ready = Wire(Bool()) 248 val s1_components_ready, s2_components_ready, s3_components_ready = Wire(Bool()) 249 250 val s0_pc = WireInit(resetVector.U) 251 val s0_pc_reg = RegNext(s0_pc, init=resetVector.U) 252 val s1_pc = RegEnable(s0_pc, s0_fire) 253 val s2_pc = RegEnable(s1_pc, s1_fire) 254 val s3_pc = RegEnable(s2_pc, s2_fire) 255 256 val s0_folded_gh = Wire(new AllFoldedHistories(foldedGHistInfos)) 257 val s0_folded_gh_reg = RegNext(s0_folded_gh, init=0.U.asTypeOf(s0_folded_gh)) 258 val s1_folded_gh = RegEnable(s0_folded_gh, 0.U.asTypeOf(s0_folded_gh), s0_fire) 259 val s2_folded_gh = RegEnable(s1_folded_gh, 0.U.asTypeOf(s0_folded_gh), s1_fire) 260 val s3_folded_gh = RegEnable(s2_folded_gh, 0.U.asTypeOf(s0_folded_gh), s2_fire) 261 262 val s0_last_br_num_oh = Wire(UInt((numBr+1).W)) 263 val s0_last_br_num_oh_reg = RegNext(s0_last_br_num_oh, init=0.U) 264 val s1_last_br_num_oh = RegEnable(s0_last_br_num_oh, 0.U, s0_fire) 265 val s2_last_br_num_oh = RegEnable(s1_last_br_num_oh, 0.U, s1_fire) 266 val s3_last_br_num_oh = RegEnable(s2_last_br_num_oh, 0.U, s2_fire) 267 268 val s0_ahead_fh_oldest_bits = Wire(new AllAheadFoldedHistoryOldestBits(foldedGHistInfos)) 269 val s0_ahead_fh_oldest_bits_reg = RegNext(s0_ahead_fh_oldest_bits, init=0.U.asTypeOf(s0_ahead_fh_oldest_bits)) 270 val s1_ahead_fh_oldest_bits = RegEnable(s0_ahead_fh_oldest_bits, 0.U.asTypeOf(s0_ahead_fh_oldest_bits), s0_fire) 271 val s2_ahead_fh_oldest_bits = RegEnable(s1_ahead_fh_oldest_bits, 0.U.asTypeOf(s0_ahead_fh_oldest_bits), s1_fire) 272 val s3_ahead_fh_oldest_bits = RegEnable(s2_ahead_fh_oldest_bits, 0.U.asTypeOf(s0_ahead_fh_oldest_bits), s2_fire) 273 274 val npcGen = new PhyPriorityMuxGenerator[UInt] 275 val foldedGhGen = new PhyPriorityMuxGenerator[AllFoldedHistories] 276 val ghistPtrGen = new PhyPriorityMuxGenerator[CGHPtr] 277 val lastBrNumOHGen = new PhyPriorityMuxGenerator[UInt] 278 val aheadFhObGen = new PhyPriorityMuxGenerator[AllAheadFoldedHistoryOldestBits] 279 280 val ghvBitWriteGens = Seq.tabulate(HistoryLength)(n => new PhyPriorityMuxGenerator[Bool]) 281 // val ghistGen = new PhyPriorityMuxGenerator[UInt] 282 283 val ghv = RegInit(0.U.asTypeOf(Vec(HistoryLength, Bool()))) 284 val ghv_wire = WireInit(ghv) 285 286 val s0_ghist = WireInit(0.U.asTypeOf(UInt(HistoryLength.W))) 287 288 289 println(f"history buffer length ${HistoryLength}") 290 val ghv_write_datas = Wire(Vec(HistoryLength, Bool())) 291 val ghv_wens = Wire(Vec(HistoryLength, Bool())) 292 293 val s0_ghist_ptr = Wire(new CGHPtr) 294 val s0_ghist_ptr_reg = RegNext(s0_ghist_ptr, init=0.U.asTypeOf(new CGHPtr)) 295 val s1_ghist_ptr = RegEnable(s0_ghist_ptr, 0.U.asTypeOf(new CGHPtr), s0_fire) 296 val s2_ghist_ptr = RegEnable(s1_ghist_ptr, 0.U.asTypeOf(new CGHPtr), s1_fire) 297 val s3_ghist_ptr = RegEnable(s2_ghist_ptr, 0.U.asTypeOf(new CGHPtr), s2_fire) 298 299 def getHist(ptr: CGHPtr): UInt = (Cat(ghv_wire.asUInt, ghv_wire.asUInt) >> (ptr.value+1.U))(HistoryLength-1, 0) 300 s0_ghist := getHist(s0_ghist_ptr) 301 302 val resp = predictors.io.out.resp 303 304 305 val toFtq_fire = io.bpu_to_ftq.resp.valid && io.bpu_to_ftq.resp.ready 306 307 val s1_flush, s2_flush, s3_flush = Wire(Bool()) 308 val s2_redirect, s3_redirect = Wire(Bool()) 309 310 // predictors.io := DontCare 311 predictors.io.in.valid := s0_fire 312 predictors.io.in.bits.s0_pc := s0_pc 313 predictors.io.in.bits.ghist := s0_ghist 314 predictors.io.in.bits.folded_hist := s0_folded_gh 315 predictors.io.in.bits.resp_in(0) := (0.U).asTypeOf(new BranchPredictionResp) 316 // predictors.io.in.bits.resp_in(0).s1.pc := s0_pc 317 // predictors.io.in.bits.toFtq_fire := toFtq_fire 318 319 // predictors.io.out.ready := io.bpu_to_ftq.resp.ready 320 321 val redirect_req = io.ftq_to_bpu.redirect 322 val do_redirect = RegNext(redirect_req, init=0.U.asTypeOf(io.ftq_to_bpu.redirect)) 323 324 // Pipeline logic 325 s2_redirect := false.B 326 s3_redirect := false.B 327 328 s3_flush := redirect_req.valid // flush when redirect comes 329 s2_flush := s3_flush || s3_redirect 330 s1_flush := s2_flush || s2_redirect 331 332 s1_components_ready := predictors.io.s1_ready 333 s1_ready := s1_fire || !s1_valid 334 s0_fire := !reset.asBool && s1_components_ready && s1_ready 335 predictors.io.s0_fire := s0_fire 336 337 s2_components_ready := predictors.io.s2_ready 338 s2_ready := s2_fire || !s2_valid 339 s1_fire := s1_valid && s2_components_ready && s2_ready && io.bpu_to_ftq.resp.ready 340 341 s3_components_ready := predictors.io.s3_ready 342 s3_ready := s3_fire || !s3_valid 343 s2_fire := s2_valid && s3_components_ready && s3_ready 344 345 when (redirect_req.valid) { s1_valid := false.B } 346 .elsewhen(s0_fire) { s1_valid := true.B } 347 .elsewhen(s1_flush) { s1_valid := false.B } 348 .elsewhen(s1_fire) { s1_valid := false.B } 349 350 predictors.io.s1_fire := s1_fire 351 352 s2_fire := s2_valid 353 354 when(s2_flush) { s2_valid := false.B } 355 .elsewhen(s1_fire) { s2_valid := !s1_flush } 356 .elsewhen(s2_fire) { s2_valid := false.B } 357 358 predictors.io.s2_fire := s2_fire 359 predictors.io.s2_redirect := s2_redirect 360 361 s3_fire := s3_valid 362 363 when(s3_flush) { s3_valid := false.B } 364 .elsewhen(s2_fire) { s3_valid := !s2_flush } 365 .elsewhen(s3_fire) { s3_valid := false.B } 366 367 predictors.io.s3_fire := s3_fire 368 predictors.io.s3_redirect := s3_redirect 369 370 371 io.bpu_to_ftq.resp.valid := 372 s1_valid && s2_components_ready && s2_ready || 373 s2_fire && s2_redirect || 374 s3_fire && s3_redirect 375 io.bpu_to_ftq.resp.bits := BpuToFtqBundle(predictors.io.out.resp) 376 io.bpu_to_ftq.resp.bits.meta := predictors.io.out.last_stage_meta // TODO: change to lastStageMeta 377 io.bpu_to_ftq.resp.bits.s3.folded_hist := s3_folded_gh 378 io.bpu_to_ftq.resp.bits.s3.histPtr := s3_ghist_ptr 379 io.bpu_to_ftq.resp.bits.s3.lastBrNumOH := s3_last_br_num_oh 380 io.bpu_to_ftq.resp.bits.s3.afhob := s3_ahead_fh_oldest_bits 381 382 npcGen.register(true.B, s0_pc_reg, Some("stallPC"), 0) 383 foldedGhGen.register(true.B, s0_folded_gh_reg, Some("stallFGH"), 0) 384 ghistPtrGen.register(true.B, s0_ghist_ptr_reg, Some("stallGHPtr"), 0) 385 lastBrNumOHGen.register(true.B, s0_last_br_num_oh_reg, Some("stallBrNumOH"), 0) 386 aheadFhObGen.register(true.B, s0_ahead_fh_oldest_bits_reg, Some("stallAFHOB"), 0) 387 388 // History manage 389 // s1 390 val s1_possible_predicted_ghist_ptrs = (0 to numBr).map(s1_ghist_ptr - _.U) 391 val s1_predicted_ghist_ptr = Mux1H(resp.s1.lastBrPosOH, s1_possible_predicted_ghist_ptrs) 392 393 val s1_possible_predicted_fhs = (0 to numBr).map(i => 394 s1_folded_gh.update(s1_ahead_fh_oldest_bits, s1_last_br_num_oh, i, resp.s1.brTaken && resp.s1.lastBrPosOH(i))) 395 val s1_predicted_fh = Mux1H(resp.s1.lastBrPosOH, s1_possible_predicted_fhs) 396 397 val s1_ahead_fh_ob_src = Wire(new AllAheadFoldedHistoryOldestBits(foldedGHistInfos)) 398 s1_ahead_fh_ob_src.read(ghv, s1_ghist_ptr) 399 400 if (EnableGHistDiff) { 401 val s1_predicted_ghist = WireInit(getHist(s1_predicted_ghist_ptr).asTypeOf(Vec(HistoryLength, Bool()))) 402 for (i <- 0 until numBr) { 403 when (resp.s1.shouldShiftVec(i)) { 404 s1_predicted_ghist(i) := resp.s1.brTaken && (i==0).B 405 } 406 } 407 when (s1_valid) { 408 s0_ghist := s1_predicted_ghist.asUInt 409 } 410 } 411 412 val s1_ghv_wens = (0 until HistoryLength).map(n => 413 (0 until numBr).map(b => (s1_ghist_ptr).value === (CGHPtr(false.B, n.U) + b.U).value && resp.s1.shouldShiftVec(b) && s1_valid)) 414 val s1_ghv_wdatas = (0 until HistoryLength).map(n => 415 Mux1H( 416 (0 until numBr).map(b => ( 417 (s1_ghist_ptr).value === (CGHPtr(false.B, n.U) + b.U).value && resp.s1.shouldShiftVec(b), 418 resp.s1.brTaken && resp.s1.lastBrPosOH(b+1) 419 )) 420 ) 421 ) 422 423 XSError(!resp.s1.is_minimal, "s1 should be minimal!\n") 424 425 npcGen.register(s1_valid, resp.s1.getTarget, Some("s1_target"), 4) 426 foldedGhGen.register(s1_valid, s1_predicted_fh, Some("s1_FGH"), 4) 427 ghistPtrGen.register(s1_valid, s1_predicted_ghist_ptr, Some("s1_GHPtr"), 4) 428 lastBrNumOHGen.register(s1_valid, resp.s1.lastBrPosOH.asUInt, Some("s1_BrNumOH"), 4) 429 aheadFhObGen.register(s1_valid, s1_ahead_fh_ob_src, Some("s1_AFHOB"), 4) 430 ghvBitWriteGens.zip(s1_ghv_wens).zipWithIndex.map{case ((b, w), i) => 431 b.register(w.reduce(_||_), s1_ghv_wdatas(i), Some(s"s1_new_bit_$i"), 4) 432 } 433 434 def preds_needs_redirect_vec(x: BranchPredictionBundle, y: BranchPredictionBundle) = { 435 VecInit( 436 x.getTarget =/= y.getTarget, 437 x.lastBrPosOH.asUInt =/= y.lastBrPosOH.asUInt, 438 x.taken =/= y.taken, 439 (x.taken && y.taken) && x.cfiIndex.bits =/= y.cfiIndex.bits, 440 // x.shouldShiftVec.asUInt =/= y.shouldShiftVec.asUInt, 441 // x.brTaken =/= y.brTaken 442 ) 443 } 444 445 // s2 446 val s2_possible_predicted_ghist_ptrs = (0 to numBr).map(s2_ghist_ptr - _.U) 447 val s2_predicted_ghist_ptr = Mux1H(resp.s2.lastBrPosOH, s2_possible_predicted_ghist_ptrs) 448 449 val s2_possible_predicted_fhs = (0 to numBr).map(i => 450 s2_folded_gh.update(s2_ahead_fh_oldest_bits, s2_last_br_num_oh, i, if (i > 0) resp.s2.full_pred.br_taken_mask(i-1) else false.B)) 451 val s2_predicted_fh = Mux1H(resp.s2.lastBrPosOH, s2_possible_predicted_fhs) 452 453 val s2_ahead_fh_ob_src = Wire(new AllAheadFoldedHistoryOldestBits(foldedGHistInfos)) 454 s2_ahead_fh_ob_src.read(ghv, s2_ghist_ptr) 455 456 if (EnableGHistDiff) { 457 val s2_predicted_ghist = WireInit(getHist(s2_predicted_ghist_ptr).asTypeOf(Vec(HistoryLength, Bool()))) 458 for (i <- 0 until numBr) { 459 when (resp.s2.shouldShiftVec(i)) { 460 s2_predicted_ghist(i) := resp.s2.brTaken && (i==0).B 461 } 462 } 463 when(s2_redirect) { 464 s0_ghist := s2_predicted_ghist.asUInt 465 } 466 } 467 468 val s2_ghv_wens = (0 until HistoryLength).map(n => 469 (0 until numBr).map(b => (s2_ghist_ptr).value === (CGHPtr(false.B, n.U) + b.U).value && resp.s2.shouldShiftVec(b) && s2_redirect)) 470 val s2_ghv_wdatas = (0 until HistoryLength).map(n => 471 Mux1H( 472 (0 until numBr).map(b => ( 473 (s2_ghist_ptr).value === (CGHPtr(false.B, n.U) + b.U).value && resp.s2.shouldShiftVec(b), 474 resp.s2.full_pred.real_br_taken_mask()(b) 475 )) 476 ) 477 ) 478 479 val previous_s1_pred = RegEnable(resp.s1, init=0.U.asTypeOf(resp.s1), s1_fire) 480 481 val s2_redirect_s1_last_pred_vec = preds_needs_redirect_vec(previous_s1_pred, resp.s2) 482 483 s2_redirect := s2_fire && s2_redirect_s1_last_pred_vec.reduce(_||_) 484 485 XSError(resp.s2.is_minimal, "s2 should not be minimal!\n") 486 487 npcGen.register(s2_redirect, resp.s2.getTarget, Some("s2_target"), 5) 488 foldedGhGen.register(s2_redirect, s2_predicted_fh, Some("s2_FGH"), 5) 489 ghistPtrGen.register(s2_redirect, s2_predicted_ghist_ptr, Some("s2_GHPtr"), 5) 490 lastBrNumOHGen.register(s2_redirect, resp.s2.lastBrPosOH.asUInt, Some("s2_BrNumOH"), 5) 491 aheadFhObGen.register(s2_redirect, s2_ahead_fh_ob_src, Some("s2_AFHOB"), 5) 492 ghvBitWriteGens.zip(s2_ghv_wens).zipWithIndex.map{case ((b, w), i) => 493 b.register(w.reduce(_||_), s2_ghv_wdatas(i), Some(s"s2_new_bit_$i"), 5) 494 } 495 496 XSPerfAccumulate("s2_redirect_because_target_diff", s2_fire && s2_redirect_s1_last_pred_vec(0)) 497 XSPerfAccumulate("s2_redirect_because_branch_num_diff", s2_fire && s2_redirect_s1_last_pred_vec(1)) 498 XSPerfAccumulate("s2_redirect_because_direction_diff", s2_fire && s2_redirect_s1_last_pred_vec(2)) 499 XSPerfAccumulate("s2_redirect_because_cfi_idx_diff", s2_fire && s2_redirect_s1_last_pred_vec(3)) 500 // XSPerfAccumulate("s2_redirect_because_shouldShiftVec_diff", s2_fire && s2_redirect_s1_last_pred_vec(4)) 501 // XSPerfAccumulate("s2_redirect_because_brTaken_diff", s2_fire && s2_redirect_s1_last_pred_vec(5)) 502 XSPerfAccumulate("s2_redirect_because_fallThroughError", s2_fire && resp.s2.fallThruError) 503 504 XSPerfAccumulate("s2_redirect_when_taken", s2_redirect && resp.s2.taken && resp.s2.full_pred.hit) 505 XSPerfAccumulate("s2_redirect_when_not_taken", s2_redirect && !resp.s2.taken && resp.s2.full_pred.hit) 506 XSPerfAccumulate("s2_redirect_when_not_hit", s2_redirect && !resp.s2.full_pred.hit) 507 508 509 // s3 510 val s3_possible_predicted_ghist_ptrs = (0 to numBr).map(s3_ghist_ptr - _.U) 511 val s3_predicted_ghist_ptr = Mux1H(resp.s3.lastBrPosOH, s3_possible_predicted_ghist_ptrs) 512 513 val s3_possible_predicted_fhs = (0 to numBr).map(i => 514 s3_folded_gh.update(s3_ahead_fh_oldest_bits, s3_last_br_num_oh, i, if (i > 0) resp.s3.full_pred.br_taken_mask(i-1) else false.B)) 515 val s3_predicted_fh = Mux1H(resp.s3.lastBrPosOH, s3_possible_predicted_fhs) 516 517 val s3_ahead_fh_ob_src = Wire(new AllAheadFoldedHistoryOldestBits(foldedGHistInfos)) 518 s3_ahead_fh_ob_src.read(ghv, s3_ghist_ptr) 519 520 if (EnableGHistDiff) { 521 val s3_predicted_ghist = WireInit(getHist(s3_predicted_ghist_ptr).asTypeOf(Vec(HistoryLength, Bool()))) 522 for (i <- 0 until numBr) { 523 when (resp.s3.shouldShiftVec(i)) { 524 s3_predicted_ghist(i) := resp.s3.brTaken && (i==0).B 525 } 526 } 527 when(s3_redirect) { 528 s0_ghist := s3_predicted_ghist.asUInt 529 } 530 } 531 532 val s3_ghv_wens = (0 until HistoryLength).map(n => 533 (0 until numBr).map(b => (s3_ghist_ptr).value === (CGHPtr(false.B, n.U) + b.U).value && resp.s3.shouldShiftVec(b) && s3_redirect)) 534 val s3_ghv_wdatas = (0 until HistoryLength).map(n => 535 Mux1H( 536 (0 until numBr).map(b => ( 537 (s3_ghist_ptr).value === (CGHPtr(false.B, n.U) + b.U).value && resp.s3.shouldShiftVec(b), 538 resp.s3.full_pred.real_br_taken_mask()(b) 539 )) 540 ) 541 ) 542 543 val previous_s2_pred = RegEnable(resp.s2, init=0.U.asTypeOf(resp.s2), s2_fire) 544 545 val s3_redirect_on_br_taken = resp.s3.full_pred.real_br_taken_mask().asUInt =/= previous_s2_pred.full_pred.real_br_taken_mask().asUInt 546 val s3_redirect_on_target = resp.s3.getTarget =/= previous_s2_pred.getTarget 547 val s3_redirect_on_jalr_target = resp.s3.full_pred.hit_taken_on_jalr && resp.s3.full_pred.jalr_target =/= previous_s2_pred.full_pred.jalr_target 548 val s3_redirect_on_fall_thru_error = resp.s3.fallThruError 549 550 s3_redirect := s3_fire && ( 551 s3_redirect_on_br_taken || s3_redirect_on_target || s3_redirect_on_fall_thru_error 552 ) 553 554 XSPerfAccumulate(f"s3_redirect_on_br_taken", s3_fire && s3_redirect_on_br_taken) 555 XSPerfAccumulate(f"s3_redirect_on_jalr_target", s3_fire && s3_redirect_on_jalr_target) 556 XSPerfAccumulate(f"s3_redirect_on_others", s3_redirect && !(s3_redirect_on_br_taken || s3_redirect_on_jalr_target)) 557 558 npcGen.register(s3_redirect, resp.s3.getTarget, Some("s3_target"), 3) 559 foldedGhGen.register(s3_redirect, s3_predicted_fh, Some("s3_FGH"), 3) 560 ghistPtrGen.register(s3_redirect, s3_predicted_ghist_ptr, Some("s3_GHPtr"), 3) 561 lastBrNumOHGen.register(s3_redirect, resp.s3.lastBrPosOH.asUInt, Some("s3_BrNumOH"), 3) 562 aheadFhObGen.register(s3_redirect, s3_ahead_fh_ob_src, Some("s3_AFHOB"), 3) 563 ghvBitWriteGens.zip(s3_ghv_wens).zipWithIndex.map{case ((b, w), i) => 564 b.register(w.reduce(_||_), s3_ghv_wdatas(i), Some(s"s3_new_bit_$i"), 3) 565 } 566 567 // Send signal tell Ftq override 568 val s2_ftq_idx = RegEnable(io.ftq_to_bpu.enq_ptr, s1_fire) 569 val s3_ftq_idx = RegEnable(s2_ftq_idx, s2_fire) 570 571 io.bpu_to_ftq.resp.bits.s1.valid := s1_fire && !s1_flush 572 io.bpu_to_ftq.resp.bits.s1.hasRedirect := false.B 573 io.bpu_to_ftq.resp.bits.s1.ftq_idx := DontCare 574 io.bpu_to_ftq.resp.bits.s2.valid := s2_fire && !s2_flush 575 io.bpu_to_ftq.resp.bits.s2.hasRedirect := s2_redirect 576 io.bpu_to_ftq.resp.bits.s2.ftq_idx := s2_ftq_idx 577 io.bpu_to_ftq.resp.bits.s3.valid := s3_fire && !s3_flush 578 io.bpu_to_ftq.resp.bits.s3.hasRedirect := s3_redirect 579 io.bpu_to_ftq.resp.bits.s3.ftq_idx := s3_ftq_idx 580 581 val redirect = do_redirect.bits 582 583 predictors.io.update := io.ftq_to_bpu.update 584 predictors.io.update.bits.ghist := getHist(io.ftq_to_bpu.update.bits.histPtr) 585 predictors.io.redirect := do_redirect 586 587 // Redirect logic 588 val shift = redirect.cfiUpdate.shift 589 val addIntoHist = redirect.cfiUpdate.addIntoHist 590 // TODO: remove these below 591 val shouldShiftVec = Mux(shift === 0.U, VecInit(0.U((1 << (log2Ceil(numBr) + 1)).W).asBools), VecInit((LowerMask(1.U << (shift-1.U))).asBools())) 592 // TODO end 593 val afhob = redirect.cfiUpdate.afhob 594 val lastBrNumOH = redirect.cfiUpdate.lastBrNumOH 595 596 597 val isBr = redirect.cfiUpdate.pd.isBr 598 val taken = redirect.cfiUpdate.taken 599 val real_br_taken_mask = (0 until numBr).map(i => shift === (i+1).U && taken && addIntoHist ) 600 601 val oldPtr = redirect.cfiUpdate.histPtr 602 val oldFh = redirect.cfiUpdate.folded_hist 603 val updated_ptr = oldPtr - shift 604 val updated_fh = VecInit((0 to numBr).map(i => oldFh.update(afhob, lastBrNumOH, i, taken && addIntoHist)))(shift) 605 val thisBrNumOH = UIntToOH(shift, numBr+1) 606 val thisAheadFhOb = Wire(new AllAheadFoldedHistoryOldestBits(foldedGHistInfos)) 607 thisAheadFhOb.read(ghv, oldPtr) 608 val redirect_ghv_wens = (0 until HistoryLength).map(n => 609 (0 until numBr).map(b => oldPtr.value === (CGHPtr(false.B, n.U) + b.U).value && shouldShiftVec(b) && do_redirect.valid)) 610 val redirect_ghv_wdatas = (0 until HistoryLength).map(n => 611 Mux1H( 612 (0 until numBr).map(b => oldPtr.value === (CGHPtr(false.B, n.U) + b.U).value && shouldShiftVec(b)), 613 real_br_taken_mask 614 ) 615 ) 616 617 if (EnableGHistDiff) { 618 val updated_ghist = WireInit(getHist(updated_ptr).asTypeOf(Vec(HistoryLength, Bool()))) 619 for (i <- 0 until numBr) { 620 when (shift >= (i+1).U) { 621 updated_ghist(i) := taken && addIntoHist && (i==0).B 622 } 623 } 624 when(do_redirect.valid) { 625 s0_ghist := updated_ghist.asUInt 626 } 627 } 628 629 630 // val updatedGh = oldGh.update(shift, taken && addIntoHist) 631 632 npcGen.register(do_redirect.valid, do_redirect.bits.cfiUpdate.target, Some("redirect_target"), 2) 633 foldedGhGen.register(do_redirect.valid, updated_fh, Some("redirect_FGHT"), 2) 634 ghistPtrGen.register(do_redirect.valid, updated_ptr, Some("redirect_GHPtr"), 2) 635 lastBrNumOHGen.register(do_redirect.valid, thisBrNumOH, Some("redirect_BrNumOH"), 2) 636 aheadFhObGen.register(do_redirect.valid, thisAheadFhOb, Some("redirect_AFHOB"), 2) 637 ghvBitWriteGens.zip(redirect_ghv_wens).zipWithIndex.map{case ((b, w), i) => 638 b.register(w.reduce(_||_), redirect_ghv_wdatas(i), Some(s"redirect_new_bit_$i"), 2) 639 } 640 // no need to assign s0_last_pred 641 642 // val need_reset = RegNext(reset.asBool) && !reset.asBool 643 644 // Reset 645 // npcGen.register(need_reset, resetVector.U, Some("reset_pc"), 1) 646 // foldedGhGen.register(need_reset, 0.U.asTypeOf(s0_folded_gh), Some("reset_FGH"), 1) 647 // ghistPtrGen.register(need_reset, 0.U.asTypeOf(new CGHPtr), Some("reset_GHPtr"), 1) 648 649 s0_pc := npcGen() 650 s0_pc_reg := s0_pc 651 s0_folded_gh := foldedGhGen() 652 s0_ghist_ptr := ghistPtrGen() 653 s0_ahead_fh_oldest_bits := aheadFhObGen() 654 s0_last_br_num_oh := lastBrNumOHGen() 655 (ghv_write_datas zip ghvBitWriteGens).map{case (wd, d) => wd := d()} 656 for (i <- 0 until HistoryLength) { 657 ghv_wens(i) := Seq(s1_ghv_wens, s2_ghv_wens, s3_ghv_wens, redirect_ghv_wens).map(_(i).reduce(_||_)).reduce(_||_) 658 when (ghv_wens(i)) { 659 ghv(i) := ghv_write_datas(i) 660 } 661 } 662 663 XSError(isBefore(redirect.cfiUpdate.histPtr, s3_ghist_ptr) && do_redirect.valid, p"s3_ghist_ptr ${s3_ghist_ptr} exceeds redirect histPtr ${redirect.cfiUpdate.histPtr}\n") 664 XSError(isBefore(redirect.cfiUpdate.histPtr, s2_ghist_ptr) && do_redirect.valid, p"s2_ghist_ptr ${s2_ghist_ptr} exceeds redirect histPtr ${redirect.cfiUpdate.histPtr}\n") 665 XSError(isBefore(redirect.cfiUpdate.histPtr, s1_ghist_ptr) && do_redirect.valid, p"s1_ghist_ptr ${s1_ghist_ptr} exceeds redirect histPtr ${redirect.cfiUpdate.histPtr}\n") 666 667 XSDebug(RegNext(reset.asBool) && !reset.asBool, "Reseting...\n") 668 XSDebug(io.ftq_to_bpu.update.valid, p"Update from ftq\n") 669 XSDebug(io.ftq_to_bpu.redirect.valid, p"Redirect from ftq\n") 670 671 XSDebug("[BP0] fire=%d pc=%x\n", s0_fire, s0_pc) 672 XSDebug("[BP1] v=%d r=%d cr=%d fire=%d flush=%d pc=%x\n", 673 s1_valid, s1_ready, s1_components_ready, s1_fire, s1_flush, s1_pc) 674 XSDebug("[BP2] v=%d r=%d cr=%d fire=%d redirect=%d flush=%d pc=%x\n", 675 s2_valid, s2_ready, s2_components_ready, s2_fire, s2_redirect, s2_flush, s2_pc) 676 XSDebug("[BP3] v=%d r=%d cr=%d fire=%d redirect=%d flush=%d pc=%x\n", 677 s3_valid, s3_ready, s3_components_ready, s3_fire, s3_redirect, s3_flush, s3_pc) 678 XSDebug("[FTQ] ready=%d\n", io.bpu_to_ftq.resp.ready) 679 XSDebug("resp.s1.target=%x\n", resp.s1.getTarget) 680 XSDebug("resp.s2.target=%x\n", resp.s2.getTarget) 681 // XSDebug("s0_ghist: %b\n", s0_ghist.predHist) 682 // XSDebug("s1_ghist: %b\n", s1_ghist.predHist) 683 // XSDebug("s2_ghist: %b\n", s2_ghist.predHist) 684 // XSDebug("s2_predicted_ghist: %b\n", s2_predicted_ghist.predHist) 685 XSDebug(p"s0_ghist_ptr: $s0_ghist_ptr\n") 686 XSDebug(p"s1_ghist_ptr: $s1_ghist_ptr\n") 687 XSDebug(p"s2_ghist_ptr: $s2_ghist_ptr\n") 688 XSDebug(p"s3_ghist_ptr: $s3_ghist_ptr\n") 689 690 io.ftq_to_bpu.update.bits.display(io.ftq_to_bpu.update.valid) 691 io.ftq_to_bpu.redirect.bits.display(io.ftq_to_bpu.redirect.valid) 692 693 694 XSPerfAccumulate("s2_redirect", s2_redirect) 695 XSPerfAccumulate("s3_redirect", s3_redirect) 696 XSPerfAccumulate("s1_not_valid", !s1_valid) 697 698 val perfEvents = predictors.asInstanceOf[Composer].getPerfEvents 699 generatePerfEvent() 700} 701