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-1) 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 142class AllFoldedHistories(val gen: Seq[Tuple2[Int, Int]])(implicit p: Parameters) extends XSBundle with HasBPUConst { 143 val hist = MixedVec(gen.map{case (l, cl) => new FoldedHistory(l, cl, numBr)}) 144 // println(gen.mkString) 145 require(gen.toSet.toList.equals(gen)) 146 def getHistWithInfo(info: Tuple2[Int, Int]) = { 147 val selected = hist.filter(_.info.equals(info)) 148 require(selected.length == 1) 149 selected(0) 150 } 151 def autoConnectFrom(that: AllFoldedHistories) = { 152 require(this.hist.length <= that.hist.length) 153 for (h <- this.hist) { 154 h := that.getHistWithInfo(h.info) 155 } 156 } 157 def update(ghv: Vec[Bool], ptr: CGHPtr, shift: Int, taken: Bool): AllFoldedHistories = { 158 val res = WireInit(this) 159 for (i <- 0 until this.hist.length) { 160 res.hist(i) := this.hist(i).update(ghv, ptr, shift, taken) 161 } 162 res 163 } 164 165 def display(cond: Bool) = { 166 for (h <- hist) { 167 XSDebug(cond, p"hist len ${h.len}, folded len ${h.compLen}, value ${Binary(h.folded_hist)}\n") 168 } 169 } 170} 171 172class BasePredictorInput (implicit p: Parameters) extends XSBundle with HasBPUConst { 173 def nInputs = 1 174 175 val s0_pc = UInt(VAddrBits.W) 176 177 val folded_hist = new AllFoldedHistories(foldedGHistInfos) 178 val ghist = UInt(HistoryLength.W) 179 180 val resp_in = Vec(nInputs, new BranchPredictionResp) 181 182 // val final_preds = Vec(numBpStages, new) 183 // val toFtq_fire = Bool() 184 185 // val s0_all_ready = Bool() 186} 187 188class BasePredictorOutput (implicit p: Parameters) extends XSBundle with HasBPUConst { 189 val last_stage_meta = UInt(MaxMetaLength.W) // This is use by composer 190 val resp = new BranchPredictionResp 191 192 // These store in meta, extract in composer 193 // val rasSp = UInt(log2Ceil(RasSize).W) 194 // val rasTop = new RASEntry 195 // val specCnt = Vec(PredictWidth, UInt(10.W)) 196} 197 198class BasePredictorIO (implicit p: Parameters) extends XSBundle with HasBPUConst { 199 val in = Flipped(DecoupledIO(new BasePredictorInput)) // TODO: Remove DecoupledIO 200 // val out = DecoupledIO(new BasePredictorOutput) 201 val out = Output(new BasePredictorOutput) 202 // val flush_out = Valid(UInt(VAddrBits.W)) 203 204 // val ctrl = Input(new BPUCtrl()) 205 206 val s0_fire = Input(Bool()) 207 val s1_fire = Input(Bool()) 208 val s2_fire = Input(Bool()) 209 val s3_fire = Input(Bool()) 210 211 val s1_ready = Output(Bool()) 212 val s2_ready = Output(Bool()) 213 val s3_ready = Output(Bool()) 214 215 val update = Flipped(Valid(new BranchPredictionUpdate)) 216 val redirect = Flipped(Valid(new BranchPredictionRedirect)) 217} 218 219abstract class BasePredictor(implicit p: Parameters) extends XSModule with HasBPUConst with BPUUtils { 220 val meta_size = 0 221 val spec_meta_size = 0 222 val io = IO(new BasePredictorIO()) 223 224 io.out.resp := io.in.bits.resp_in(0) 225 226 io.out.last_stage_meta := 0.U 227 228 io.in.ready := !io.redirect.valid 229 230 io.s1_ready := true.B 231 io.s2_ready := true.B 232 io.s3_ready := true.B 233 234 val s0_pc = WireInit(io.in.bits.s0_pc) // fetchIdx(io.f0_pc) 235 val s1_pc = RegEnable(s0_pc, resetVector.U, io.s0_fire) 236 val s2_pc = RegEnable(s1_pc, io.s1_fire) 237 val s3_pc = RegEnable(s2_pc, io.s2_fire) 238 239 io.out.resp.s1.pc := s1_pc 240 io.out.resp.s2.pc := s2_pc 241 io.out.resp.s3.pc := s3_pc 242 243 244 def getFoldedHistoryInfo: Option[Set[FoldedHistoryInfo]] = None 245} 246 247class FakePredictor(implicit p: Parameters) extends BasePredictor { 248 io.in.ready := true.B 249 io.out.last_stage_meta := 0.U 250 io.out.resp := io.in.bits.resp_in(0) 251} 252 253class BpuToFtqIO(implicit p: Parameters) extends XSBundle { 254 val resp = DecoupledIO(new BpuToFtqBundle()) 255} 256 257class PredictorIO(implicit p: Parameters) extends XSBundle { 258 val bpu_to_ftq = new BpuToFtqIO() 259 val ftq_to_bpu = Flipped(new FtqToBpuIO()) 260} 261 262@chiselName 263class Predictor(implicit p: Parameters) extends XSModule with HasBPUConst with HasPerfEvents { 264 val io = IO(new PredictorIO) 265 266 val predictors = Module(if (useBPD) new Composer else new FakePredictor) 267 268 val folded_hist_infos = predictors.getFoldedHistoryInfo.getOrElse(Set()).toList 269 for ((len, compLen) <- folded_hist_infos) { 270 println(f"folded hist info: len $len, compLen $compLen") 271 } 272 273 val s0_fire, s1_fire, s2_fire, s3_fire = Wire(Bool()) 274 val s1_valid, s2_valid, s3_valid = RegInit(false.B) 275 val s1_ready, s2_ready, s3_ready = Wire(Bool()) 276 val s1_components_ready, s2_components_ready, s3_components_ready = Wire(Bool()) 277 278 val s0_pc = WireInit(resetVector.U) 279 val s0_pc_reg = RegNext(s0_pc, init=resetVector.U) 280 val s1_pc = RegEnable(s0_pc, s0_fire) 281 val s2_pc = RegEnable(s1_pc, s1_fire) 282 val s3_pc = RegEnable(s2_pc, s2_fire) 283 284 val s0_folded_gh = Wire(new AllFoldedHistories(foldedGHistInfos)) 285 val s0_folded_gh_reg = RegNext(s0_folded_gh, init=0.U.asTypeOf(s0_folded_gh)) 286 val s1_folded_gh = RegEnable(s0_folded_gh, 0.U.asTypeOf(s0_folded_gh), s0_fire) 287 val s2_folded_gh = RegEnable(s1_folded_gh, 0.U.asTypeOf(s0_folded_gh), s1_fire) 288 val s3_folded_gh = RegEnable(s2_folded_gh, 0.U.asTypeOf(s0_folded_gh), s2_fire) 289 290 val npcGen = new PhyPriorityMuxGenerator[UInt] 291 val foldedGhGen = new PhyPriorityMuxGenerator[AllFoldedHistories] 292 val ghistPtrGen = new PhyPriorityMuxGenerator[CGHPtr] 293 val ghvBitWriteGens = Seq.tabulate(HistoryLength)(n => new PhyPriorityMuxGenerator[Bool]) 294 // val ghistGen = new PhyPriorityMuxGenerator[UInt] 295 296 val ghv = RegInit(0.U.asTypeOf(Vec(HistoryLength, Bool()))) 297 val ghv_wire = WireInit(ghv) 298 299 val s0_ghist = WireInit(0.U.asTypeOf(UInt(HistoryLength.W))) 300 301 302 val ghv_write_datas = Wire(Vec(HistoryLength, Bool())) 303 val ghv_wens = Wire(Vec(HistoryLength, Bool())) 304 305 val s0_ghist_ptr = Wire(new CGHPtr) 306 val s0_ghist_ptr_reg = RegNext(s0_ghist_ptr, init=0.U.asTypeOf(new CGHPtr)) 307 val s1_ghist_ptr = RegEnable(s0_ghist_ptr, 0.U.asTypeOf(new CGHPtr), s0_fire) 308 val s2_ghist_ptr = RegEnable(s1_ghist_ptr, 0.U.asTypeOf(new CGHPtr), s1_fire) 309 val s3_ghist_ptr = RegEnable(s2_ghist_ptr, 0.U.asTypeOf(new CGHPtr), s2_fire) 310 311 def getHist(ptr: CGHPtr): UInt = (Cat(ghv_wire.asUInt, ghv_wire.asUInt) >> (ptr.value+1.U))(HistoryLength-1, 0) 312 s0_ghist := getHist(s0_ghist_ptr) 313 314 val resp = predictors.io.out.resp 315 316 317 val toFtq_fire = io.bpu_to_ftq.resp.valid && io.bpu_to_ftq.resp.ready 318 319 val s1_flush, s2_flush, s3_flush = Wire(Bool()) 320 val s2_redirect, s3_redirect = Wire(Bool()) 321 322 // predictors.io := DontCare 323 predictors.io.in.valid := s0_fire 324 predictors.io.in.bits.s0_pc := s0_pc 325 predictors.io.in.bits.ghist := s0_ghist 326 predictors.io.in.bits.folded_hist := s0_folded_gh 327 predictors.io.in.bits.resp_in(0) := (0.U).asTypeOf(new BranchPredictionResp) 328 // predictors.io.in.bits.resp_in(0).s1.pc := s0_pc 329 // predictors.io.in.bits.toFtq_fire := toFtq_fire 330 331 // predictors.io.out.ready := io.bpu_to_ftq.resp.ready 332 333 // Pipeline logic 334 s2_redirect := false.B 335 s3_redirect := false.B 336 337 s3_flush := io.ftq_to_bpu.redirect.valid 338 s2_flush := s3_flush || s3_redirect 339 s1_flush := s2_flush || s2_redirect 340 341 s1_components_ready := predictors.io.s1_ready 342 s1_ready := s1_fire || !s1_valid 343 s0_fire := !reset.asBool && s1_components_ready && s1_ready 344 predictors.io.s0_fire := s0_fire 345 346 s2_components_ready := predictors.io.s2_ready 347 s2_ready := s2_fire || !s2_valid 348 s1_fire := s1_valid && s2_components_ready && s2_ready && io.bpu_to_ftq.resp.ready 349 350 s3_components_ready := predictors.io.s3_ready 351 s3_ready := s3_fire || !s3_valid 352 s2_fire := s2_valid && s3_components_ready && s3_ready 353 354 when(s0_fire) { s1_valid := true.B } 355 .elsewhen(s1_flush) { s1_valid := false.B } 356 .elsewhen(s1_fire) { s1_valid := false.B } 357 358 predictors.io.s1_fire := s1_fire 359 360 s2_fire := s2_valid 361 362 when(s2_flush) { s2_valid := false.B } 363 .elsewhen(s1_fire) { s2_valid := !s1_flush } 364 .elsewhen(s2_fire) { s2_valid := false.B } 365 366 predictors.io.s2_fire := s2_fire 367 368 s3_fire := s3_valid 369 370 when(s3_flush) { s3_valid := false.B } 371 .elsewhen(s2_fire) { s3_valid := !s2_flush } 372 .elsewhen(s3_fire) { s3_valid := false.B } 373 374 predictors.io.s3_fire := s3_fire 375 376 377 io.bpu_to_ftq.resp.valid := 378 s1_valid && s2_components_ready && s2_ready || 379 s2_fire && s2_redirect || 380 s3_fire && s3_redirect 381 io.bpu_to_ftq.resp.bits := BpuToFtqBundle(predictors.io.out.resp) 382 io.bpu_to_ftq.resp.bits.meta := predictors.io.out.last_stage_meta // TODO: change to lastStageMeta 383 io.bpu_to_ftq.resp.bits.s3.folded_hist := s3_folded_gh 384 io.bpu_to_ftq.resp.bits.s3.histPtr := s3_ghist_ptr 385 386 npcGen.register(true.B, s0_pc_reg, Some("stallPC"), 0) 387 foldedGhGen.register(true.B, s0_folded_gh_reg, Some("stallFGH"), 0) 388 ghistPtrGen.register(true.B, s0_ghist_ptr_reg, Some("stallGHPtr"), 0) 389 390 // History manage 391 // s1 392 val s1_possible_predicted_ghist_ptrs = (0 to numBr).map(s1_ghist_ptr - _.U) 393 val s1_predicted_ghist_ptr = Mux1H(resp.s1.lastBrPosOH, s1_possible_predicted_ghist_ptrs) 394 395 val s1_possible_predicted_fhs = (0 to numBr).map(i => 396 s1_folded_gh.update(ghv, s1_ghist_ptr, i, resp.s1.brTaken && resp.s1.lastBrPosOH(i))) 397 val s1_predicted_fh = Mux1H(resp.s1.lastBrPosOH, s1_possible_predicted_fhs) 398 399 if (EnableGHistDiff) { 400 val s1_predicted_ghist = WireInit(getHist(s1_predicted_ghist_ptr).asTypeOf(Vec(HistoryLength, Bool()))) 401 for (i <- 0 until numBr) { 402 when (resp.s1.shouldShiftVec(i)) { 403 s1_predicted_ghist(i) := resp.s1.brTaken && (i==0).B 404 } 405 } 406 when (s1_valid) { 407 s0_ghist := s1_predicted_ghist.asUInt 408 } 409 } 410 411 require(isPow2(HistoryLength)) 412 val s1_ghv_wens = (0 until HistoryLength).map(n => 413 (0 until numBr).map(b => (s1_ghist_ptr).value === n.U(log2Ceil(HistoryLength).W) + b.U && 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 === n.U(log2Ceil(HistoryLength).W) + b.U && 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 ghvBitWriteGens.zip(s1_ghv_wens).zipWithIndex.map{case ((b, w), i) => 429 b.register(w.reduce(_||_), s1_ghv_wdatas(i), Some(s"s1_new_bit_$i"), 4) 430 } 431 432 def preds_needs_redirect_vec(x: BranchPredictionBundle, y: BranchPredictionBundle) = { 433 VecInit( 434 x.getTarget =/= y.getTarget, 435 x.lastBrPosOH.asUInt =/= y.lastBrPosOH.asUInt, 436 x.taken =/= y.taken, 437 (x.taken && y.taken) && x.cfiIndex.bits =/= y.cfiIndex.bits, 438 (!x.taken && !y.taken) && x.oversize =/= y.oversize 439 // x.shouldShiftVec.asUInt =/= y.shouldShiftVec.asUInt, 440 // x.brTaken =/= y.brTaken 441 ) 442 } 443 444 // s2 445 val s2_possible_predicted_ghist_ptrs = (0 to numBr).map(s2_ghist_ptr - _.U) 446 val s2_predicted_ghist_ptr = Mux1H(resp.s2.lastBrPosOH, s2_possible_predicted_ghist_ptrs) 447 448 val s2_possible_predicted_fhs = (0 to numBr).map(i => 449 s2_folded_gh.update(ghv, s2_ghist_ptr, i, if (i > 0) resp.s2.full_pred.br_taken_mask(i-1) else false.B)) 450 val s2_predicted_fh = Mux1H(resp.s2.lastBrPosOH, s2_possible_predicted_fhs) 451 452 if (EnableGHistDiff) { 453 val s2_predicted_ghist = WireInit(getHist(s2_predicted_ghist_ptr).asTypeOf(Vec(HistoryLength, Bool()))) 454 for (i <- 0 until numBr) { 455 when (resp.s2.shouldShiftVec(i)) { 456 s2_predicted_ghist(i) := resp.s2.brTaken && (i==0).B 457 } 458 } 459 when(s2_redirect) { 460 s0_ghist := s2_predicted_ghist.asUInt 461 } 462 } 463 464 val s2_ghv_wens = (0 until HistoryLength).map(n => 465 (0 until numBr).map(b => (s2_ghist_ptr).value === n.U(log2Ceil(HistoryLength).W) + b.U && resp.s2.shouldShiftVec(b) && s2_redirect)) 466 val s2_ghv_wdatas = (0 until HistoryLength).map(n => 467 Mux1H( 468 (0 until numBr).map(b => ( 469 (s2_ghist_ptr).value === n.U(log2Ceil(HistoryLength).W) + b.U && resp.s2.shouldShiftVec(b), 470 resp.s2.full_pred.real_br_taken_mask()(b) 471 )) 472 ) 473 ) 474 475 val previous_s1_pred = RegEnable(resp.s1, init=0.U.asTypeOf(resp.s1), s1_fire) 476 477 val s2_redirect_s1_last_pred_vec = preds_needs_redirect_vec(previous_s1_pred, resp.s2) 478 479 s2_redirect := s2_fire && (s2_redirect_s1_last_pred_vec.reduce(_||_) || resp.s2.fallThruError) 480 481 XSError(resp.s2.is_minimal, "s2 should not be minimal!\n") 482 483 npcGen.register(s2_redirect, resp.s2.getTarget, Some("s2_target"), 5) 484 foldedGhGen.register(s2_redirect, s2_predicted_fh, Some("s2_FGH"), 5) 485 ghistPtrGen.register(s2_redirect, s2_predicted_ghist_ptr, Some("s2_GHPtr"), 5) 486 ghvBitWriteGens.zip(s2_ghv_wens).zipWithIndex.map{case ((b, w), i) => 487 b.register(w.reduce(_||_), s2_ghv_wdatas(i), Some(s"s2_new_bit_$i"), 5) 488 } 489 490 XSPerfAccumulate("s2_redirect_because_target_diff", s2_fire && s2_redirect_s1_last_pred_vec(0)) 491 XSPerfAccumulate("s2_redirect_because_branch_num_diff", s2_fire && s2_redirect_s1_last_pred_vec(1)) 492 XSPerfAccumulate("s2_redirect_because_direction_diff", s2_fire && s2_redirect_s1_last_pred_vec(2)) 493 XSPerfAccumulate("s2_redirect_because_cfi_idx_diff", s2_fire && s2_redirect_s1_last_pred_vec(3)) 494 // XSPerfAccumulate("s2_redirect_because_shouldShiftVec_diff", s2_fire && s2_redirect_s1_last_pred_vec(4)) 495 // XSPerfAccumulate("s2_redirect_because_brTaken_diff", s2_fire && s2_redirect_s1_last_pred_vec(5)) 496 XSPerfAccumulate("s2_redirect_because_fallThroughError", s2_fire && resp.s2.fallThruError) 497 498 XSPerfAccumulate("s2_redirect_when_taken", s2_redirect && resp.s2.taken && resp.s2.full_pred.hit) 499 XSPerfAccumulate("s2_redirect_when_not_taken", s2_redirect && !resp.s2.taken && resp.s2.full_pred.hit) 500 XSPerfAccumulate("s2_redirect_when_not_hit", s2_redirect && !resp.s2.full_pred.hit) 501 502 503 // s3 504 val s3_possible_predicted_ghist_ptrs = (0 to numBr).map(s3_ghist_ptr - _.U) 505 val s3_predicted_ghist_ptr = Mux1H(resp.s3.lastBrPosOH, s3_possible_predicted_ghist_ptrs) 506 507 val s3_possible_predicted_fhs = (0 to numBr).map(i => 508 s3_folded_gh.update(ghv, s3_ghist_ptr, i, if (i > 0) resp.s3.full_pred.br_taken_mask(i-1) else false.B)) 509 val s3_predicted_fh = Mux1H(resp.s3.lastBrPosOH, s3_possible_predicted_fhs) 510 511 if (EnableGHistDiff) { 512 val s3_predicted_ghist = WireInit(getHist(s3_predicted_ghist_ptr).asTypeOf(Vec(HistoryLength, Bool()))) 513 for (i <- 0 until numBr) { 514 when (resp.s3.shouldShiftVec(i)) { 515 s3_predicted_ghist(i) := resp.s3.brTaken && (i==0).B 516 } 517 } 518 when(s3_redirect) { 519 s0_ghist := s3_predicted_ghist.asUInt 520 } 521 } 522 523 val s3_ghv_wens = (0 until HistoryLength).map(n => 524 (0 until numBr).map(b => (s3_ghist_ptr).value === n.U(log2Ceil(HistoryLength).W) + b.U && resp.s3.shouldShiftVec(b) && s3_redirect)) 525 val s3_ghv_wdatas = (0 until HistoryLength).map(n => 526 Mux1H( 527 (0 until numBr).map(b => ( 528 (s3_ghist_ptr).value === n.U(log2Ceil(HistoryLength).W) + b.U && resp.s3.shouldShiftVec(b), 529 resp.s3.full_pred.real_br_taken_mask()(b) 530 )) 531 ) 532 ) 533 534 val previous_s2_pred = RegEnable(resp.s2, init=0.U.asTypeOf(resp.s2), s2_fire) 535 536 val s3_redirect_s2_last_pred_vec = preds_needs_redirect_vec(previous_s1_pred, resp.s2) 537 // TODO: 538 539 s3_redirect := s3_fire && !previous_s2_pred.fallThruError && ( 540 resp.s3.full_pred.real_br_taken_mask().asUInt =/= previous_s2_pred.full_pred.real_br_taken_mask().asUInt 541 ) 542 543 npcGen.register(s3_redirect, resp.s3.getTarget, Some("s3_target"), 3) 544 foldedGhGen.register(s3_redirect, s3_predicted_fh, Some("s3_FGH"), 3) 545 ghistPtrGen.register(s3_redirect, s3_predicted_ghist_ptr, Some("s3_GHPtr"), 3) 546 ghvBitWriteGens.zip(s3_ghv_wens).zipWithIndex.map{case ((b, w), i) => 547 b.register(w.reduce(_||_), s3_ghv_wdatas(i), Some(s"s3_new_bit_$i"), 3) 548 } 549 550 // Send signal tell Ftq override 551 val s2_ftq_idx = RegEnable(io.ftq_to_bpu.enq_ptr, s1_fire) 552 val s3_ftq_idx = RegEnable(s2_ftq_idx, s2_fire) 553 554 io.bpu_to_ftq.resp.bits.s1.valid := s1_fire && !s1_flush 555 io.bpu_to_ftq.resp.bits.s1.hasRedirect := false.B 556 io.bpu_to_ftq.resp.bits.s1.ftq_idx := DontCare 557 io.bpu_to_ftq.resp.bits.s2.valid := s2_fire && !s2_flush 558 io.bpu_to_ftq.resp.bits.s2.hasRedirect := s2_redirect 559 io.bpu_to_ftq.resp.bits.s2.ftq_idx := s2_ftq_idx 560 io.bpu_to_ftq.resp.bits.s3.valid := s3_fire && !s3_flush 561 io.bpu_to_ftq.resp.bits.s3.hasRedirect := s3_redirect 562 io.bpu_to_ftq.resp.bits.s3.ftq_idx := s3_ftq_idx 563 564 val redirect = io.ftq_to_bpu.redirect.bits 565 566 predictors.io.update := io.ftq_to_bpu.update 567 predictors.io.update.bits.ghist := getHist(io.ftq_to_bpu.update.bits.histPtr) 568 predictors.io.redirect := io.ftq_to_bpu.redirect 569 570 // Redirect logic 571 val shift = redirect.cfiUpdate.shift 572 val addIntoHist = redirect.cfiUpdate.addIntoHist 573 // TODO: remove these below 574 val shouldShiftVec = Mux(shift === 0.U, VecInit(0.U((1 << (log2Ceil(numBr) + 1)).W).asBools), VecInit((LowerMask(1.U << (shift-1.U))).asBools())) 575 // TODO end 576 577 val isBr = redirect.cfiUpdate.pd.isBr 578 val taken = redirect.cfiUpdate.taken 579 val real_br_taken_mask = (0 until numBr).map(i => shift === (i+1).U && taken && addIntoHist ) 580 581 val oldPtr = redirect.cfiUpdate.histPtr 582 val oldFh = redirect.cfiUpdate.folded_hist 583 val updated_ptr = oldPtr - shift 584 val updated_fh = VecInit((0 to numBr).map(i => oldFh.update(ghv, oldPtr, i, taken && addIntoHist)))(shift) 585 val redirect_ghv_wens = (0 until HistoryLength).map(n => 586 (0 until numBr).map(b => oldPtr.value === (n.U(log2Ceil(HistoryLength).W) + b.U) && shouldShiftVec(b) && io.ftq_to_bpu.redirect.valid)) 587 val redirect_ghv_wdatas = (0 until HistoryLength).map(n => 588 Mux1H( 589 (0 until numBr).map(b => oldPtr.value === (n.U(log2Ceil(HistoryLength).W) + b.U) && shouldShiftVec(b)), 590 real_br_taken_mask 591 ) 592 ) 593 594 if (EnableGHistDiff) { 595 val updated_ghist = WireInit(getHist(updated_ptr).asTypeOf(Vec(HistoryLength, Bool()))) 596 for (i <- 0 until numBr) { 597 when (shift >= (i+1).U) { 598 updated_ghist(i) := taken && addIntoHist && (i==0).B 599 } 600 } 601 when(io.ftq_to_bpu.redirect.valid) { 602 s0_ghist := updated_ghist.asUInt 603 } 604 } 605 606 607 // val updatedGh = oldGh.update(shift, taken && addIntoHist) 608 609 npcGen.register(io.ftq_to_bpu.redirect.valid, redirect.cfiUpdate.target, Some("redirect_target"), 2) 610 foldedGhGen.register(io.ftq_to_bpu.redirect.valid, updated_fh, Some("redirect_FGHT"), 2) 611 ghistPtrGen.register(io.ftq_to_bpu.redirect.valid, updated_ptr, Some("redirect_GHPtr"), 2) 612 ghvBitWriteGens.zip(redirect_ghv_wens).zipWithIndex.map{case ((b, w), i) => 613 b.register(w.reduce(_||_), redirect_ghv_wdatas(i), Some(s"redirect_new_bit_$i"), 2) 614 } 615 // no need to assign s0_last_pred 616 617 val need_reset = RegNext(reset.asBool) && !reset.asBool 618 619 // Reset 620 npcGen.register(need_reset, resetVector.U, Some("reset_pc"), 1) 621 foldedGhGen.register(need_reset, 0.U.asTypeOf(s0_folded_gh), Some("reset_FGH"), 1) 622 ghistPtrGen.register(need_reset, 0.U.asTypeOf(new CGHPtr), Some("reset_GHPtr"), 1) 623 624 s0_pc := npcGen() 625 s0_pc_reg := s0_pc 626 s0_folded_gh := foldedGhGen() 627 s0_ghist_ptr := ghistPtrGen() 628 (ghv_write_datas zip ghvBitWriteGens).map{case (wd, d) => wd := d()} 629 for (i <- 0 until HistoryLength) { 630 ghv_wens(i) := Seq(s1_ghv_wens, s2_ghv_wens, s3_ghv_wens, redirect_ghv_wens).map(_(i).reduce(_||_)).reduce(_||_) 631 when (ghv_wens(i)) { 632 ghv(i) := ghv_write_datas(i) 633 } 634 } 635 636 XSDebug(RegNext(reset.asBool) && !reset.asBool, "Reseting...\n") 637 XSDebug(io.ftq_to_bpu.update.valid, p"Update from ftq\n") 638 XSDebug(io.ftq_to_bpu.redirect.valid, p"Redirect from ftq\n") 639 640 XSDebug("[BP0] fire=%d pc=%x\n", s0_fire, s0_pc) 641 XSDebug("[BP1] v=%d r=%d cr=%d fire=%d flush=%d pc=%x\n", 642 s1_valid, s1_ready, s1_components_ready, s1_fire, s1_flush, s1_pc) 643 XSDebug("[BP2] v=%d r=%d cr=%d fire=%d redirect=%d flush=%d pc=%x\n", 644 s2_valid, s2_ready, s2_components_ready, s2_fire, s2_redirect, s2_flush, s2_pc) 645 XSDebug("[BP3] v=%d r=%d cr=%d fire=%d redirect=%d flush=%d pc=%x\n", 646 s3_valid, s3_ready, s3_components_ready, s3_fire, s3_redirect, s3_flush, s3_pc) 647 XSDebug("[FTQ] ready=%d\n", io.bpu_to_ftq.resp.ready) 648 XSDebug("resp.s1.target=%x\n", resp.s1.getTarget) 649 XSDebug("resp.s2.target=%x\n", resp.s2.getTarget) 650 // XSDebug("s0_ghist: %b\n", s0_ghist.predHist) 651 // XSDebug("s1_ghist: %b\n", s1_ghist.predHist) 652 // XSDebug("s2_ghist: %b\n", s2_ghist.predHist) 653 // XSDebug("s2_predicted_ghist: %b\n", s2_predicted_ghist.predHist) 654 XSDebug(p"s0_ghist_ptr: $s0_ghist_ptr\n") 655 XSDebug(p"s1_ghist_ptr: $s1_ghist_ptr\n") 656 XSDebug(p"s2_ghist_ptr: $s2_ghist_ptr\n") 657 XSDebug(p"s3_ghist_ptr: $s3_ghist_ptr\n") 658 659 io.ftq_to_bpu.update.bits.display(io.ftq_to_bpu.update.valid) 660 io.ftq_to_bpu.redirect.bits.display(io.ftq_to_bpu.redirect.valid) 661 662 663 XSPerfAccumulate("s2_redirect", s2_redirect) 664 XSPerfAccumulate("s3_redirect", s3_redirect) 665 666 val perfEvents = predictors.asInstanceOf[Composer].getPerfEvents 667 generatePerfEvent() 668} 669