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