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._ 25import utility._ 26 27import scala.math.min 28import xiangshan.backend.decode.ImmUnion 29 30trait HasBPUConst extends HasXSParameter { 31 val MaxMetaLength = if (!env.FPGAPlatform) 512 else 256 // TODO: Reduce meta length 32 val MaxBasicBlockSize = 32 33 val LHistoryLength = 32 34 // val numBr = 2 35 val useBPD = true 36 val useLHist = true 37 val numBrSlot = numBr-1 38 val totalSlot = numBrSlot + 1 39 40 val numDup = 4 41 42 def BP_STAGES = (0 until 3).map(_.U(2.W)) 43 def BP_S1 = BP_STAGES(0) 44 def BP_S2 = BP_STAGES(1) 45 def BP_S3 = BP_STAGES(2) 46 47 def dup_seq[T](src: T, num: Int = numDup) = Seq.tabulate(num)(n => src) 48 def dup[T <: Data](src: T, num: Int = numDup) = VecInit(Seq.tabulate(num)(n => src)) 49 def dup_wire[T <: Data](src: T, num: Int = numDup) = Wire(Vec(num, src.cloneType)) 50 def dup_idx = Seq.tabulate(numDup)(n => n.toString()) 51 val numBpStages = BP_STAGES.length 52 53 val debug = true 54 // TODO: Replace log2Up by log2Ceil 55} 56 57trait HasBPUParameter extends HasXSParameter with HasBPUConst { 58 val BPUDebug = true && !env.FPGAPlatform && env.EnablePerfDebug 59 val EnableCFICommitLog = true 60 val EnbaleCFIPredLog = true 61 val EnableBPUTimeRecord = (EnableCFICommitLog || EnbaleCFIPredLog) && !env.FPGAPlatform 62 val EnableCommit = false 63} 64 65class BPUCtrl(implicit p: Parameters) extends XSBundle { 66 val ubtb_enable = Bool() 67 val btb_enable = Bool() 68 val bim_enable = Bool() 69 val tage_enable = Bool() 70 val sc_enable = Bool() 71 val ras_enable = Bool() 72 val loop_enable = Bool() 73} 74 75trait BPUUtils extends HasXSParameter { 76 // circular shifting 77 def circularShiftLeft(source: UInt, len: Int, shamt: UInt): UInt = { 78 val res = Wire(UInt(len.W)) 79 val higher = source << shamt 80 val lower = source >> (len.U - shamt) 81 res := higher | lower 82 res 83 } 84 85 def circularShiftRight(source: UInt, len: Int, shamt: UInt): UInt = { 86 val res = Wire(UInt(len.W)) 87 val higher = source << (len.U - shamt) 88 val lower = source >> shamt 89 res := higher | lower 90 res 91 } 92 93 // To be verified 94 def satUpdate(old: UInt, len: Int, taken: Bool): UInt = { 95 val oldSatTaken = old === ((1 << len)-1).U 96 val oldSatNotTaken = old === 0.U 97 Mux(oldSatTaken && taken, ((1 << len)-1).U, 98 Mux(oldSatNotTaken && !taken, 0.U, 99 Mux(taken, old + 1.U, old - 1.U))) 100 } 101 102 def signedSatUpdate(old: SInt, len: Int, taken: Bool): SInt = { 103 val oldSatTaken = old === ((1 << (len-1))-1).S 104 val oldSatNotTaken = old === (-(1 << (len-1))).S 105 Mux(oldSatTaken && taken, ((1 << (len-1))-1).S, 106 Mux(oldSatNotTaken && !taken, (-(1 << (len-1))).S, 107 Mux(taken, old + 1.S, old - 1.S))) 108 } 109 110 def getFallThroughAddr(start: UInt, carry: Bool, pft: UInt) = { 111 val higher = start.head(VAddrBits-log2Ceil(PredictWidth)-instOffsetBits) 112 Cat(Mux(carry, higher+1.U, higher), pft, 0.U(instOffsetBits.W)) 113 } 114 115 def foldTag(tag: UInt, l: Int): UInt = { 116 val nChunks = (tag.getWidth + l - 1) / l 117 val chunks = (0 until nChunks).map { i => 118 tag(min((i+1)*l, tag.getWidth)-1, i*l) 119 } 120 ParallelXOR(chunks) 121 } 122} 123 124class BasePredictorInput (implicit p: Parameters) extends XSBundle with HasBPUConst { 125 def nInputs = 1 126 127 val s0_pc = Vec(numDup, UInt(VAddrBits.W)) 128 129 val folded_hist = Vec(numDup, new AllFoldedHistories(foldedGHistInfos)) 130 val ghist = UInt(HistoryLength.W) 131 132 val resp_in = Vec(nInputs, new BranchPredictionResp) 133 134 // val final_preds = Vec(numBpStages, new) 135 // val toFtq_fire = Bool() 136 137 // val s0_all_ready = Bool() 138} 139 140class BasePredictorOutput (implicit p: Parameters) extends BranchPredictionResp {} 141 142class BasePredictorIO (implicit p: Parameters) extends XSBundle with HasBPUConst { 143 val reset_vector = Input(UInt(PAddrBits.W)) 144 val in = Flipped(DecoupledIO(new BasePredictorInput)) // TODO: Remove DecoupledIO 145 // val out = DecoupledIO(new BasePredictorOutput) 146 val out = Output(new BasePredictorOutput) 147 // val flush_out = Valid(UInt(VAddrBits.W)) 148 149 val ctrl = Input(new BPUCtrl) 150 151 val s0_fire = Input(Vec(numDup, Bool())) 152 val s1_fire = Input(Vec(numDup, Bool())) 153 val s2_fire = Input(Vec(numDup, Bool())) 154 val s3_fire = Input(Vec(numDup, Bool())) 155 156 val s2_redirect = Input(Vec(numDup, Bool())) 157 val s3_redirect = Input(Vec(numDup, Bool())) 158 159 val s1_ready = Output(Bool()) 160 val s2_ready = Output(Bool()) 161 val s3_ready = Output(Bool()) 162 163 val update = Flipped(Valid(new BranchPredictionUpdate)) 164 val redirect = Flipped(Valid(new BranchPredictionRedirect)) 165} 166 167abstract class BasePredictor(implicit p: Parameters) extends XSModule 168 with HasBPUConst with BPUUtils with HasPerfEvents { 169 val meta_size = 0 170 val spec_meta_size = 0 171 val is_fast_pred = false 172 val io = IO(new BasePredictorIO()) 173 174 io.out := io.in.bits.resp_in(0) 175 176 io.out.last_stage_meta := 0.U 177 178 io.in.ready := !io.redirect.valid 179 180 io.s1_ready := true.B 181 io.s2_ready := true.B 182 io.s3_ready := true.B 183 184 val reset_vector = DelayN(io.reset_vector, 5) 185 186 val s0_pc_dup = WireInit(io.in.bits.s0_pc) // fetchIdx(io.f0_pc) 187 val s1_pc_dup = s0_pc_dup.zip(io.s0_fire).map {case (s0_pc, s0_fire) => RegEnable(s0_pc, s0_fire)} 188 val s2_pc_dup = s1_pc_dup.zip(io.s1_fire).map {case (s1_pc, s1_fire) => RegEnable(s1_pc, s1_fire)} 189 val s3_pc_dup = s2_pc_dup.zip(io.s2_fire).map {case (s2_pc, s2_fire) => RegEnable(s2_pc, s2_fire)} 190 191 when (RegNext(RegNext(reset.asBool) && !reset.asBool)) { 192 s1_pc_dup.map{case s1_pc => s1_pc := reset_vector} 193 } 194 195 io.out.s1.pc := s1_pc_dup 196 io.out.s2.pc := s2_pc_dup 197 io.out.s3.pc := s3_pc_dup 198 199 val perfEvents: Seq[(String, UInt)] = Seq() 200 201 202 def getFoldedHistoryInfo: Option[Set[FoldedHistoryInfo]] = None 203} 204 205class FakePredictor(implicit p: Parameters) extends BasePredictor { 206 io.in.ready := true.B 207 io.out.last_stage_meta := 0.U 208 io.out := io.in.bits.resp_in(0) 209} 210 211class BpuToFtqIO(implicit p: Parameters) extends XSBundle { 212 val resp = DecoupledIO(new BpuToFtqBundle()) 213} 214 215class PredictorIO(implicit p: Parameters) extends XSBundle { 216 val bpu_to_ftq = new BpuToFtqIO() 217 val ftq_to_bpu = Flipped(new FtqToBpuIO()) 218 val ctrl = Input(new BPUCtrl) 219 val reset_vector = Input(UInt(PAddrBits.W)) 220} 221 222@chiselName 223class Predictor(implicit p: Parameters) extends XSModule with HasBPUConst with HasPerfEvents with HasCircularQueuePtrHelper { 224 val io = IO(new PredictorIO) 225 226 val ctrl = DelayN(io.ctrl, 1) 227 val predictors = Module(if (useBPD) new Composer else new FakePredictor) 228 229 def numOfStage = 3 230 require(numOfStage > 1, "BPU numOfStage must be greater than 1") 231 val topdown_stages = RegInit(VecInit(Seq.fill(numOfStage)(0.U.asTypeOf(new FrontendTopDownBundle)))) 232 dontTouch(topdown_stages) 233 234 // following can only happen on s1 235 val controlRedirectBubble = Wire(Bool()) 236 val ControlBTBMissBubble = Wire(Bool()) 237 val TAGEMissBubble = Wire(Bool()) 238 val SCMissBubble = Wire(Bool()) 239 val ITTAGEMissBubble = Wire(Bool()) 240 val RASMissBubble = Wire(Bool()) 241 242 val memVioRedirectBubble = Wire(Bool()) 243 val otherRedirectBubble = Wire(Bool()) 244 val btbMissBubble = Wire(Bool()) 245 otherRedirectBubble := false.B 246 memVioRedirectBubble := false.B 247 248 // override can happen between s1-s2 and s2-s3 249 val overrideBubble = Wire(Vec(numOfStage - 1, Bool())) 250 def overrideStage = 1 251 // ftq update block can happen on s1, s2 and s3 252 val ftqUpdateBubble = Wire(Vec(numOfStage, Bool())) 253 def ftqUpdateStage = 0 254 // ftq full stall only happens on s3 (last stage) 255 val ftqFullStall = Wire(Bool()) 256 257 // by default, no bubble event 258 topdown_stages(0) := 0.U.asTypeOf(new FrontendTopDownBundle) 259 // event movement driven by clock only 260 for (i <- 0 until numOfStage - 1) { 261 topdown_stages(i + 1) := topdown_stages(i) 262 } 263 264 265 266 // ctrl signal 267 predictors.io.ctrl := ctrl 268 predictors.io.reset_vector := io.reset_vector 269 270 271 val reset_vector = DelayN(io.reset_vector, 5) 272 273 val s0_fire_dup, s1_fire_dup, s2_fire_dup, s3_fire_dup = dup_wire(Bool()) 274 val s1_valid_dup, s2_valid_dup, s3_valid_dup = dup_seq(RegInit(false.B)) 275 val s1_ready_dup, s2_ready_dup, s3_ready_dup = dup_wire(Bool()) 276 val s1_components_ready_dup, s2_components_ready_dup, s3_components_ready_dup = dup_wire(Bool()) 277 278 val s0_pc_dup = dup(WireInit(0.U.asTypeOf(UInt(VAddrBits.W)))) 279 val s0_pc_reg_dup = s0_pc_dup.map(x => RegNext(x)) 280 when (RegNext(RegNext(reset.asBool) && !reset.asBool)) { 281 s0_pc_reg_dup.map{case s0_pc => s0_pc := reset_vector} 282 } 283 val s1_pc = RegEnable(s0_pc_dup(0), s0_fire_dup(0)) 284 val s2_pc = RegEnable(s1_pc, s1_fire_dup(0)) 285 val s3_pc = RegEnable(s2_pc, s2_fire_dup(0)) 286 287 val s0_folded_gh_dup = dup_wire(new AllFoldedHistories(foldedGHistInfos)) 288 val s0_folded_gh_reg_dup = s0_folded_gh_dup.map(x => RegNext(x, init=0.U.asTypeOf(s0_folded_gh_dup(0)))) 289 val s1_folded_gh_dup = RegEnable(s0_folded_gh_dup, 0.U.asTypeOf(s0_folded_gh_dup), s0_fire_dup(1)) 290 val s2_folded_gh_dup = RegEnable(s1_folded_gh_dup, 0.U.asTypeOf(s0_folded_gh_dup), s1_fire_dup(1)) 291 val s3_folded_gh_dup = RegEnable(s2_folded_gh_dup, 0.U.asTypeOf(s0_folded_gh_dup), s2_fire_dup(1)) 292 293 val s0_last_br_num_oh_dup = dup_wire(UInt((numBr+1).W)) 294 val s0_last_br_num_oh_reg_dup = s0_last_br_num_oh_dup.map(x => RegNext(x, init=0.U)) 295 val s1_last_br_num_oh_dup = RegEnable(s0_last_br_num_oh_dup, 0.U.asTypeOf(s0_last_br_num_oh_dup), s0_fire_dup(1)) 296 val s2_last_br_num_oh_dup = RegEnable(s1_last_br_num_oh_dup, 0.U.asTypeOf(s0_last_br_num_oh_dup), s1_fire_dup(1)) 297 val s3_last_br_num_oh_dup = RegEnable(s2_last_br_num_oh_dup, 0.U.asTypeOf(s0_last_br_num_oh_dup), s2_fire_dup(1)) 298 299 val s0_ahead_fh_oldest_bits_dup = dup_wire(new AllAheadFoldedHistoryOldestBits(foldedGHistInfos)) 300 val s0_ahead_fh_oldest_bits_reg_dup = s0_ahead_fh_oldest_bits_dup.map(x => RegNext(x, init=0.U.asTypeOf(s0_ahead_fh_oldest_bits_dup(0)))) 301 val s1_ahead_fh_oldest_bits_dup = RegEnable(s0_ahead_fh_oldest_bits_dup, 0.U.asTypeOf(s0_ahead_fh_oldest_bits_dup), s0_fire_dup(1)) 302 val s2_ahead_fh_oldest_bits_dup = RegEnable(s1_ahead_fh_oldest_bits_dup, 0.U.asTypeOf(s0_ahead_fh_oldest_bits_dup), s1_fire_dup(1)) 303 val s3_ahead_fh_oldest_bits_dup = RegEnable(s2_ahead_fh_oldest_bits_dup, 0.U.asTypeOf(s0_ahead_fh_oldest_bits_dup), s2_fire_dup(1)) 304 305 val npcGen_dup = Seq.tabulate(numDup)(n => new PhyPriorityMuxGenerator[UInt]) 306 val foldedGhGen_dup = Seq.tabulate(numDup)(n => new PhyPriorityMuxGenerator[AllFoldedHistories]) 307 val ghistPtrGen_dup = Seq.tabulate(numDup)(n => new PhyPriorityMuxGenerator[CGHPtr]) 308 val lastBrNumOHGen_dup = Seq.tabulate(numDup)(n => new PhyPriorityMuxGenerator[UInt]) 309 val aheadFhObGen_dup = Seq.tabulate(numDup)(n => new PhyPriorityMuxGenerator[AllAheadFoldedHistoryOldestBits]) 310 311 val ghvBitWriteGens = Seq.tabulate(HistoryLength)(n => new PhyPriorityMuxGenerator[Bool]) 312 // val ghistGen = new PhyPriorityMuxGenerator[UInt] 313 314 val ghv = RegInit(0.U.asTypeOf(Vec(HistoryLength, Bool()))) 315 val ghv_wire = WireInit(ghv) 316 317 val s0_ghist = WireInit(0.U.asTypeOf(UInt(HistoryLength.W))) 318 319 320 println(f"history buffer length ${HistoryLength}") 321 val ghv_write_datas = Wire(Vec(HistoryLength, Bool())) 322 val ghv_wens = Wire(Vec(HistoryLength, Bool())) 323 324 val s0_ghist_ptr_dup = dup_wire(new CGHPtr) 325 val s0_ghist_ptr_reg_dup = s0_ghist_ptr_dup.map(x => RegNext(x, init=0.U.asTypeOf(new CGHPtr))) 326 val s1_ghist_ptr_dup = RegEnable(s0_ghist_ptr_dup, 0.U.asTypeOf(s0_ghist_ptr_dup), s0_fire_dup(1)) 327 val s2_ghist_ptr_dup = RegEnable(s1_ghist_ptr_dup, 0.U.asTypeOf(s0_ghist_ptr_dup), s1_fire_dup(1)) 328 val s3_ghist_ptr_dup = RegEnable(s2_ghist_ptr_dup, 0.U.asTypeOf(s0_ghist_ptr_dup), s2_fire_dup(1)) 329 330 def getHist(ptr: CGHPtr): UInt = (Cat(ghv_wire.asUInt, ghv_wire.asUInt) >> (ptr.value+1.U))(HistoryLength-1, 0) 331 s0_ghist := getHist(s0_ghist_ptr_dup(0)) 332 333 val resp = predictors.io.out 334 335 336 val toFtq_fire = io.bpu_to_ftq.resp.valid && io.bpu_to_ftq.resp.ready 337 338 val s1_flush_dup, s2_flush_dup, s3_flush_dup = dup_wire(Bool()) 339 val s2_redirect_dup, s3_redirect_dup = dup_wire(Bool()) 340 341 // predictors.io := DontCare 342 predictors.io.in.valid := s0_fire_dup(0) 343 predictors.io.in.bits.s0_pc := s0_pc_dup 344 predictors.io.in.bits.ghist := s0_ghist 345 predictors.io.in.bits.folded_hist := s0_folded_gh_dup 346 predictors.io.in.bits.resp_in(0) := (0.U).asTypeOf(new BranchPredictionResp) 347 // predictors.io.in.bits.resp_in(0).s1.pc := s0_pc 348 // predictors.io.in.bits.toFtq_fire := toFtq_fire 349 350 // predictors.io.out.ready := io.bpu_to_ftq.resp.ready 351 352 val redirect_req = io.ftq_to_bpu.redirect 353 val do_redirect_dup = dup_seq(RegNext(redirect_req, init=0.U.asTypeOf(io.ftq_to_bpu.redirect))) 354 355 // Pipeline logic 356 s2_redirect_dup.map(_ := false.B) 357 s3_redirect_dup.map(_ := false.B) 358 359 s3_flush_dup.map(_ := redirect_req.valid) // flush when redirect comes 360 for (((s2_flush, s3_flush), s3_redirect) <- s2_flush_dup zip s3_flush_dup zip s3_redirect_dup) 361 s2_flush := s3_flush || s3_redirect 362 for (((s1_flush, s2_flush), s2_redirect) <- s1_flush_dup zip s2_flush_dup zip s2_redirect_dup) 363 s1_flush := s2_flush || s2_redirect 364 365 366 s1_components_ready_dup.map(_ := predictors.io.s1_ready) 367 for (((s1_ready, s1_fire), s1_valid) <- s1_ready_dup zip s1_fire_dup zip s1_valid_dup) 368 s1_ready := s1_fire || !s1_valid 369 for (((s0_fire, s1_components_ready), s1_ready) <- s0_fire_dup zip s1_components_ready_dup zip s1_ready_dup) 370 s0_fire := s1_components_ready && s1_ready 371 predictors.io.s0_fire := s0_fire_dup 372 373 s2_components_ready_dup.map(_ := predictors.io.s2_ready) 374 for (((s2_ready, s2_fire), s2_valid) <- s2_ready_dup zip s2_fire_dup zip s2_valid_dup) 375 s2_ready := s2_fire || !s2_valid 376 for ((((s1_fire, s2_components_ready), s2_ready), s1_valid) <- s1_fire_dup zip s2_components_ready_dup zip s2_ready_dup zip s1_valid_dup) 377 s1_fire := s1_valid && s2_components_ready && s2_ready && io.bpu_to_ftq.resp.ready 378 379 s3_components_ready_dup.map(_ := predictors.io.s3_ready) 380 for (((s3_ready, s3_fire), s3_valid) <- s3_ready_dup zip s3_fire_dup zip s3_valid_dup) 381 s3_ready := s3_fire || !s3_valid 382 for ((((s2_fire, s3_components_ready), s3_ready), s2_valid) <- s2_fire_dup zip s3_components_ready_dup zip s3_ready_dup zip s2_valid_dup) 383 s2_fire := s2_valid && s3_components_ready && s3_ready 384 385 for ((((s0_fire, s1_flush), s1_fire), s1_valid) <- s0_fire_dup zip s1_flush_dup zip s1_fire_dup zip s1_valid_dup) { 386 when (redirect_req.valid) { s1_valid := false.B } 387 .elsewhen(s0_fire) { s1_valid := true.B } 388 .elsewhen(s1_flush) { s1_valid := false.B } 389 .elsewhen(s1_fire) { s1_valid := false.B } 390 } 391 predictors.io.s1_fire := s1_fire_dup 392 393 s2_fire_dup := s2_valid_dup 394 395 for (((((s1_fire, s2_flush), s2_fire), s2_valid), s1_flush) <- 396 s1_fire_dup zip s2_flush_dup zip s2_fire_dup zip s2_valid_dup zip s1_flush_dup) { 397 398 when (s2_flush) { s2_valid := false.B } 399 .elsewhen(s1_fire) { s2_valid := !s1_flush } 400 .elsewhen(s2_fire) { s2_valid := false.B } 401 } 402 403 predictors.io.s2_fire := s2_fire_dup 404 predictors.io.s2_redirect := s2_redirect_dup 405 406 s3_fire_dup := s3_valid_dup 407 408 for (((((s2_fire, s3_flush), s3_fire), s3_valid), s2_flush) <- 409 s2_fire_dup zip s3_flush_dup zip s3_fire_dup zip s3_valid_dup zip s2_flush_dup) { 410 411 when (s3_flush) { s3_valid := false.B } 412 .elsewhen(s2_fire) { s3_valid := !s2_flush } 413 .elsewhen(s3_fire) { s3_valid := false.B } 414 } 415 416 predictors.io.s3_fire := s3_fire_dup 417 predictors.io.s3_redirect := s3_redirect_dup 418 419 420 io.bpu_to_ftq.resp.valid := 421 s1_valid_dup(2) && s2_components_ready_dup(2) && s2_ready_dup(2) || 422 s2_fire_dup(2) && s2_redirect_dup(2) || 423 s3_fire_dup(2) && s3_redirect_dup(2) 424 io.bpu_to_ftq.resp.bits := predictors.io.out 425 io.bpu_to_ftq.resp.bits.last_stage_spec_info.folded_hist := s3_folded_gh_dup(2) 426 io.bpu_to_ftq.resp.bits.last_stage_spec_info.histPtr := s3_ghist_ptr_dup(2) 427 io.bpu_to_ftq.resp.bits.last_stage_spec_info.lastBrNumOH := s3_last_br_num_oh_dup(2) 428 io.bpu_to_ftq.resp.bits.last_stage_spec_info.afhob := s3_ahead_fh_oldest_bits_dup(2) 429 430 npcGen_dup.zip(s0_pc_reg_dup).map{ case (gen, reg) => 431 gen.register(true.B, reg, Some("stallPC"), 0)} 432 foldedGhGen_dup.zip(s0_folded_gh_reg_dup).map{ case (gen, reg) => 433 gen.register(true.B, reg, Some("stallFGH"), 0)} 434 ghistPtrGen_dup.zip(s0_ghist_ptr_reg_dup).map{ case (gen, reg) => 435 gen.register(true.B, reg, Some("stallGHPtr"), 0)} 436 lastBrNumOHGen_dup.zip(s0_last_br_num_oh_reg_dup).map{ case (gen, reg) => 437 gen.register(true.B, reg, Some("stallBrNumOH"), 0)} 438 aheadFhObGen_dup.zip(s0_ahead_fh_oldest_bits_reg_dup).map{ case (gen, reg) => 439 gen.register(true.B, reg, Some("stallAFHOB"), 0)} 440 441 // History manage 442 // s1 443 val s1_possible_predicted_ghist_ptrs_dup = s1_ghist_ptr_dup.map(ptr => (0 to numBr).map(ptr - _.U)) 444 val s1_predicted_ghist_ptr_dup = s1_possible_predicted_ghist_ptrs_dup.zip(resp.s1.lastBrPosOH).map{ case (ptr, oh) => Mux1H(oh, ptr)} 445 val s1_possible_predicted_fhs_dup = 446 for (((((fgh, afh), br_num_oh), t), br_pos_oh) <- 447 s1_folded_gh_dup zip s1_ahead_fh_oldest_bits_dup zip s1_last_br_num_oh_dup zip resp.s1.brTaken zip resp.s1.lastBrPosOH) 448 yield (0 to numBr).map(i => 449 fgh.update(afh, br_num_oh, i, t & br_pos_oh(i)) 450 ) 451 val s1_predicted_fh_dup = resp.s1.lastBrPosOH.zip(s1_possible_predicted_fhs_dup).map{ case (oh, fh) => Mux1H(oh, fh)} 452 453 val s1_ahead_fh_ob_src_dup = dup_wire(new AllAheadFoldedHistoryOldestBits(foldedGHistInfos)) 454 s1_ahead_fh_ob_src_dup.zip(s1_ghist_ptr_dup).map{ case (src, ptr) => src.read(ghv, ptr)} 455 456 if (EnableGHistDiff) { 457 val s1_predicted_ghist = WireInit(getHist(s1_predicted_ghist_ptr_dup(0)).asTypeOf(Vec(HistoryLength, Bool()))) 458 for (i <- 0 until numBr) { 459 when (resp.s1.shouldShiftVec(0)(i)) { 460 s1_predicted_ghist(i) := resp.s1.brTaken(0) && (i==0).B 461 } 462 } 463 when (s1_valid_dup(0)) { 464 s0_ghist := s1_predicted_ghist.asUInt 465 } 466 } 467 468 val s1_ghv_wens = (0 until HistoryLength).map(n => 469 (0 until numBr).map(b => (s1_ghist_ptr_dup(0)).value === (CGHPtr(false.B, n.U) + b.U).value && resp.s1.shouldShiftVec(0)(b) && s1_valid_dup(0))) 470 val s1_ghv_wdatas = (0 until HistoryLength).map(n => 471 Mux1H( 472 (0 until numBr).map(b => ( 473 (s1_ghist_ptr_dup(0)).value === (CGHPtr(false.B, n.U) + b.U).value && resp.s1.shouldShiftVec(0)(b), 474 resp.s1.brTaken(0) && resp.s1.lastBrPosOH(0)(b+1) 475 )) 476 ) 477 ) 478 479 480 for (((npcGen, s1_valid), s1_target) <- npcGen_dup zip s1_valid_dup zip resp.s1.getTarget) 481 npcGen.register(s1_valid, s1_target, Some("s1_target"), 4) 482 for (((foldedGhGen, s1_valid), s1_predicted_fh) <- foldedGhGen_dup zip s1_valid_dup zip s1_predicted_fh_dup) 483 foldedGhGen.register(s1_valid, s1_predicted_fh, Some("s1_FGH"), 4) 484 for (((ghistPtrGen, s1_valid), s1_predicted_ghist_ptr) <- ghistPtrGen_dup zip s1_valid_dup zip s1_predicted_ghist_ptr_dup) 485 ghistPtrGen.register(s1_valid, s1_predicted_ghist_ptr, Some("s1_GHPtr"), 4) 486 for (((lastBrNumOHGen, s1_valid), s1_brPosOH) <- lastBrNumOHGen_dup zip s1_valid_dup zip resp.s1.lastBrPosOH.map(_.asUInt)) 487 lastBrNumOHGen.register(s1_valid, s1_brPosOH, Some("s1_BrNumOH"), 4) 488 for (((aheadFhObGen, s1_valid), s1_ahead_fh_ob_src) <- aheadFhObGen_dup zip s1_valid_dup zip s1_ahead_fh_ob_src_dup) 489 aheadFhObGen.register(s1_valid, s1_ahead_fh_ob_src, Some("s1_AFHOB"), 4) 490 ghvBitWriteGens.zip(s1_ghv_wens).zipWithIndex.map{case ((b, w), i) => 491 b.register(w.reduce(_||_), s1_ghv_wdatas(i), Some(s"s1_new_bit_$i"), 4) 492 } 493 494 class PreviousPredInfo extends Bundle { 495 val target = Vec(numDup, UInt(VAddrBits.W)) 496 val lastBrPosOH = Vec(numDup, Vec(numBr+1, Bool())) 497 val taken = Vec(numDup, Bool()) 498 val cfiIndex = Vec(numDup, UInt(log2Ceil(PredictWidth).W)) 499 } 500 501 def preds_needs_redirect_vec_dup(x: PreviousPredInfo, y: BranchPredictionBundle) = { 502 val target_diff = x.target.zip(y.getTarget).map {case (t1, t2) => t1 =/= t2 } 503 val lastBrPosOH_diff = x.lastBrPosOH.zip(y.lastBrPosOH).map {case (oh1, oh2) => oh1.asUInt =/= oh2.asUInt} 504 val taken_diff = x.taken.zip(y.taken).map {case (t1, t2) => t1 =/= t2} 505 val takenOffset_diff = x.cfiIndex.zip(y.cfiIndex).zip(x.taken).zip(y.taken).map {case (((i1, i2), xt), yt) => xt && yt && i1 =/= i2.bits} 506 VecInit( 507 for ((((tgtd, lbpohd), tkd), tod) <- 508 target_diff zip lastBrPosOH_diff zip taken_diff zip takenOffset_diff) 509 yield VecInit(tgtd, lbpohd, tkd, tod) 510 // x.shouldShiftVec.asUInt =/= y.shouldShiftVec.asUInt, 511 // x.brTaken =/= y.brTaken 512 ) 513 } 514 515 // s2 516 val s2_possible_predicted_ghist_ptrs_dup = s2_ghist_ptr_dup.map(ptr => (0 to numBr).map(ptr - _.U)) 517 val s2_predicted_ghist_ptr_dup = s2_possible_predicted_ghist_ptrs_dup.zip(resp.s2.lastBrPosOH).map{ case (ptr, oh) => Mux1H(oh, ptr)} 518 519 val s2_possible_predicted_fhs_dup = 520 for ((((fgh, afh), br_num_oh), full_pred) <- 521 s2_folded_gh_dup zip s2_ahead_fh_oldest_bits_dup zip s2_last_br_num_oh_dup zip resp.s2.full_pred) 522 yield (0 to numBr).map(i => 523 fgh.update(afh, br_num_oh, i, if (i > 0) full_pred.br_taken_mask(i-1) else false.B) 524 ) 525 val s2_predicted_fh_dup = resp.s2.lastBrPosOH.zip(s2_possible_predicted_fhs_dup).map{ case (oh, fh) => Mux1H(oh, fh)} 526 527 val s2_ahead_fh_ob_src_dup = dup_wire(new AllAheadFoldedHistoryOldestBits(foldedGHistInfos)) 528 s2_ahead_fh_ob_src_dup.zip(s2_ghist_ptr_dup).map{ case (src, ptr) => src.read(ghv, ptr)} 529 530 if (EnableGHistDiff) { 531 val s2_predicted_ghist = WireInit(getHist(s2_predicted_ghist_ptr_dup(0)).asTypeOf(Vec(HistoryLength, Bool()))) 532 for (i <- 0 until numBr) { 533 when (resp.s2.shouldShiftVec(0)(i)) { 534 s2_predicted_ghist(i) := resp.s2.brTaken(0) && (i==0).B 535 } 536 } 537 when(s2_redirect_dup(0)) { 538 s0_ghist := s2_predicted_ghist.asUInt 539 } 540 } 541 542 val s2_ghv_wens = (0 until HistoryLength).map(n => 543 (0 until numBr).map(b => (s2_ghist_ptr_dup(0)).value === (CGHPtr(false.B, n.U) + b.U).value && resp.s2.shouldShiftVec(0)(b) && s2_redirect_dup(0))) 544 val s2_ghv_wdatas = (0 until HistoryLength).map(n => 545 Mux1H( 546 (0 until numBr).map(b => ( 547 (s2_ghist_ptr_dup(0)).value === (CGHPtr(false.B, n.U) + b.U).value && resp.s2.shouldShiftVec(0)(b), 548 resp.s2.full_pred(0).real_br_taken_mask()(b) 549 )) 550 ) 551 ) 552 553 val s1_pred_info = Wire(new PreviousPredInfo) 554 s1_pred_info.target := resp.s1.getTarget 555 s1_pred_info.lastBrPosOH := resp.s1.lastBrPosOH 556 s1_pred_info.taken := resp.s1.taken 557 s1_pred_info.cfiIndex := resp.s1.cfiIndex.map{case x => x.bits} 558 559 val previous_s1_pred_info = RegEnable(s1_pred_info, init=0.U.asTypeOf(new PreviousPredInfo), s1_fire_dup(0)) 560 561 val s2_redirect_s1_last_pred_vec_dup = preds_needs_redirect_vec_dup(previous_s1_pred_info, resp.s2) 562 563 for (((s2_redirect, s2_fire), s2_redirect_s1_last_pred_vec) <- s2_redirect_dup zip s2_fire_dup zip s2_redirect_s1_last_pred_vec_dup) 564 s2_redirect := s2_fire && s2_redirect_s1_last_pred_vec.reduce(_||_) 565 566 567 for (((npcGen, s2_redirect), s2_target) <- npcGen_dup zip s2_redirect_dup zip resp.s2.getTarget) 568 npcGen.register(s2_redirect, s2_target, Some("s2_target"), 5) 569 for (((foldedGhGen, s2_redirect), s2_predicted_fh) <- foldedGhGen_dup zip s2_redirect_dup zip s2_predicted_fh_dup) 570 foldedGhGen.register(s2_redirect, s2_predicted_fh, Some("s2_FGH"), 5) 571 for (((ghistPtrGen, s2_redirect), s2_predicted_ghist_ptr) <- ghistPtrGen_dup zip s2_redirect_dup zip s2_predicted_ghist_ptr_dup) 572 ghistPtrGen.register(s2_redirect, s2_predicted_ghist_ptr, Some("s2_GHPtr"), 5) 573 for (((lastBrNumOHGen, s2_redirect), s2_brPosOH) <- lastBrNumOHGen_dup zip s2_redirect_dup zip resp.s2.lastBrPosOH.map(_.asUInt)) 574 lastBrNumOHGen.register(s2_redirect, s2_brPosOH, Some("s2_BrNumOH"), 5) 575 for (((aheadFhObGen, s2_redirect), s2_ahead_fh_ob_src) <- aheadFhObGen_dup zip s2_redirect_dup zip s2_ahead_fh_ob_src_dup) 576 aheadFhObGen.register(s2_redirect, s2_ahead_fh_ob_src, Some("s2_AFHOB"), 5) 577 ghvBitWriteGens.zip(s2_ghv_wens).zipWithIndex.map{case ((b, w), i) => 578 b.register(w.reduce(_||_), s2_ghv_wdatas(i), Some(s"s2_new_bit_$i"), 5) 579 } 580 581 XSPerfAccumulate("s2_redirect_because_target_diff", s2_fire_dup(0) && s2_redirect_s1_last_pred_vec_dup(0)(0)) 582 XSPerfAccumulate("s2_redirect_because_branch_num_diff", s2_fire_dup(0) && s2_redirect_s1_last_pred_vec_dup(0)(1)) 583 XSPerfAccumulate("s2_redirect_because_direction_diff", s2_fire_dup(0) && s2_redirect_s1_last_pred_vec_dup(0)(2)) 584 XSPerfAccumulate("s2_redirect_because_cfi_idx_diff", s2_fire_dup(0) && s2_redirect_s1_last_pred_vec_dup(0)(3)) 585 // XSPerfAccumulate("s2_redirect_because_shouldShiftVec_diff", s2_fire && s2_redirect_s1_last_pred_vec(4)) 586 // XSPerfAccumulate("s2_redirect_because_brTaken_diff", s2_fire && s2_redirect_s1_last_pred_vec(5)) 587 XSPerfAccumulate("s2_redirect_because_fallThroughError", s2_fire_dup(0) && resp.s2.fallThruError(0)) 588 589 XSPerfAccumulate("s2_redirect_when_taken", s2_redirect_dup(0) && resp.s2.taken(0) && resp.s2.full_pred(0).hit) 590 XSPerfAccumulate("s2_redirect_when_not_taken", s2_redirect_dup(0) && !resp.s2.taken(0) && resp.s2.full_pred(0).hit) 591 XSPerfAccumulate("s2_redirect_when_not_hit", s2_redirect_dup(0) && !resp.s2.full_pred(0).hit) 592 593 594 // s3 595 val s3_possible_predicted_ghist_ptrs_dup = s3_ghist_ptr_dup.map(ptr => (0 to numBr).map(ptr - _.U)) 596 val s3_predicted_ghist_ptr_dup = s3_possible_predicted_ghist_ptrs_dup.zip(resp.s3.lastBrPosOH).map{ case (ptr, oh) => Mux1H(oh, ptr)} 597 598 val s3_possible_predicted_fhs_dup = 599 for ((((fgh, afh), br_num_oh), full_pred) <- 600 s3_folded_gh_dup zip s3_ahead_fh_oldest_bits_dup zip s3_last_br_num_oh_dup zip resp.s3.full_pred) 601 yield (0 to numBr).map(i => 602 fgh.update(afh, br_num_oh, i, if (i > 0) full_pred.br_taken_mask(i-1) else false.B) 603 ) 604 val s3_predicted_fh_dup = resp.s3.lastBrPosOH.zip(s3_possible_predicted_fhs_dup).map{ case (oh, fh) => Mux1H(oh, fh)} 605 606 val s3_ahead_fh_ob_src_dup = dup_wire(new AllAheadFoldedHistoryOldestBits(foldedGHistInfos)) 607 s3_ahead_fh_ob_src_dup.zip(s3_ghist_ptr_dup).map{ case (src, ptr) => src.read(ghv, ptr)} 608 609 if (EnableGHistDiff) { 610 val s3_predicted_ghist = WireInit(getHist(s3_predicted_ghist_ptr_dup(0)).asTypeOf(Vec(HistoryLength, Bool()))) 611 for (i <- 0 until numBr) { 612 when (resp.s3.shouldShiftVec(0)(i)) { 613 s3_predicted_ghist(i) := resp.s3.brTaken(0) && (i==0).B 614 } 615 } 616 when(s3_redirect_dup(0)) { 617 s0_ghist := s3_predicted_ghist.asUInt 618 } 619 } 620 621 val s3_ghv_wens = (0 until HistoryLength).map(n => 622 (0 until numBr).map(b => (s3_ghist_ptr_dup(0)).value === (CGHPtr(false.B, n.U) + b.U).value && resp.s3.shouldShiftVec(0)(b) && s3_redirect_dup(0))) 623 val s3_ghv_wdatas = (0 until HistoryLength).map(n => 624 Mux1H( 625 (0 until numBr).map(b => ( 626 (s3_ghist_ptr_dup(0)).value === (CGHPtr(false.B, n.U) + b.U).value && resp.s3.shouldShiftVec(0)(b), 627 resp.s3.full_pred(0).real_br_taken_mask()(b) 628 )) 629 ) 630 ) 631 632 val previous_s2_pred = RegEnable(resp.s2, init=0.U.asTypeOf(resp.s2), s2_fire_dup(0)) 633 634 val s3_redirect_on_br_taken_dup = resp.s3.full_pred.zip(previous_s2_pred.full_pred).map {case (fp1, fp2) => fp1.real_br_taken_mask().asUInt =/= fp2.real_br_taken_mask().asUInt} 635 val s3_redirect_on_target_dup = resp.s3.getTarget.zip(previous_s2_pred.getTarget).map {case (t1, t2) => t1 =/= t2} 636 val s3_redirect_on_jalr_target_dup = resp.s3.full_pred.zip(previous_s2_pred.full_pred).map {case (fp1, fp2) => fp1.hit_taken_on_jalr && fp1.jalr_target =/= fp2.jalr_target} 637 val s3_redirect_on_fall_thru_error_dup = resp.s3.fallThruError 638 639 for (((((s3_redirect, s3_fire), s3_redirect_on_br_taken), s3_redirect_on_target), s3_redirect_on_fall_thru_error) <- 640 s3_redirect_dup zip s3_fire_dup zip s3_redirect_on_br_taken_dup zip s3_redirect_on_target_dup zip s3_redirect_on_fall_thru_error_dup) { 641 642 s3_redirect := s3_fire && ( 643 s3_redirect_on_br_taken || s3_redirect_on_target || s3_redirect_on_fall_thru_error 644 ) 645 } 646 647 XSPerfAccumulate(f"s3_redirect_on_br_taken", s3_fire_dup(0) && s3_redirect_on_br_taken_dup(0)) 648 XSPerfAccumulate(f"s3_redirect_on_jalr_target", s3_fire_dup(0) && s3_redirect_on_jalr_target_dup(0)) 649 XSPerfAccumulate(f"s3_redirect_on_others", s3_redirect_dup(0) && !(s3_redirect_on_br_taken_dup(0) || s3_redirect_on_jalr_target_dup(0))) 650 651 for (((npcGen, s3_redirect), s3_target) <- npcGen_dup zip s3_redirect_dup zip resp.s3.getTarget) 652 npcGen.register(s3_redirect, s3_target, Some("s3_target"), 3) 653 for (((foldedGhGen, s3_redirect), s3_predicted_fh) <- foldedGhGen_dup zip s3_redirect_dup zip s3_predicted_fh_dup) 654 foldedGhGen.register(s3_redirect, s3_predicted_fh, Some("s3_FGH"), 3) 655 for (((ghistPtrGen, s3_redirect), s3_predicted_ghist_ptr) <- ghistPtrGen_dup zip s3_redirect_dup zip s3_predicted_ghist_ptr_dup) 656 ghistPtrGen.register(s3_redirect, s3_predicted_ghist_ptr, Some("s3_GHPtr"), 3) 657 for (((lastBrNumOHGen, s3_redirect), s3_brPosOH) <- lastBrNumOHGen_dup zip s3_redirect_dup zip resp.s3.lastBrPosOH.map(_.asUInt)) 658 lastBrNumOHGen.register(s3_redirect, s3_brPosOH, Some("s3_BrNumOH"), 3) 659 for (((aheadFhObGen, s3_redirect), s3_ahead_fh_ob_src) <- aheadFhObGen_dup zip s3_redirect_dup zip s3_ahead_fh_ob_src_dup) 660 aheadFhObGen.register(s3_redirect, s3_ahead_fh_ob_src, Some("s3_AFHOB"), 3) 661 ghvBitWriteGens.zip(s3_ghv_wens).zipWithIndex.map{case ((b, w), i) => 662 b.register(w.reduce(_||_), s3_ghv_wdatas(i), Some(s"s3_new_bit_$i"), 3) 663 } 664 665 // Send signal tell Ftq override 666 val s2_ftq_idx = RegEnable(io.ftq_to_bpu.enq_ptr, s1_fire_dup(0)) 667 val s3_ftq_idx = RegEnable(s2_ftq_idx, s2_fire_dup(0)) 668 669 for (((to_ftq_s1_valid, s1_fire), s1_flush) <- io.bpu_to_ftq.resp.bits.s1.valid zip s1_fire_dup zip s1_flush_dup) { 670 to_ftq_s1_valid := s1_fire && !s1_flush 671 } 672 io.bpu_to_ftq.resp.bits.s1.hasRedirect.map(_ := false.B) 673 io.bpu_to_ftq.resp.bits.s1.ftq_idx := DontCare 674 for (((to_ftq_s2_valid, s2_fire), s2_flush) <- io.bpu_to_ftq.resp.bits.s2.valid zip s2_fire_dup zip s2_flush_dup) { 675 to_ftq_s2_valid := s2_fire && !s2_flush 676 } 677 io.bpu_to_ftq.resp.bits.s2.hasRedirect.zip(s2_redirect_dup).map {case (hr, r) => hr := r} 678 io.bpu_to_ftq.resp.bits.s2.ftq_idx := s2_ftq_idx 679 for (((to_ftq_s3_valid, s3_fire), s3_flush) <- io.bpu_to_ftq.resp.bits.s3.valid zip s3_fire_dup zip s3_flush_dup) { 680 to_ftq_s3_valid := s3_fire && !s3_flush 681 } 682 io.bpu_to_ftq.resp.bits.s3.hasRedirect.zip(s3_redirect_dup).map {case (hr, r) => hr := r} 683 io.bpu_to_ftq.resp.bits.s3.ftq_idx := s3_ftq_idx 684 685 predictors.io.update := RegNext(io.ftq_to_bpu.update) 686 predictors.io.update.bits.ghist := RegNext(getHist(io.ftq_to_bpu.update.bits.spec_info.histPtr)) 687 688 val redirect_dup = do_redirect_dup.map(_.bits) 689 predictors.io.redirect := do_redirect_dup(0) 690 691 // Redirect logic 692 val shift_dup = redirect_dup.map(_.cfiUpdate.shift) 693 val addIntoHist_dup = redirect_dup.map(_.cfiUpdate.addIntoHist) 694 // TODO: remove these below 695 val shouldShiftVec_dup = shift_dup.map(shift => Mux(shift === 0.U, VecInit(0.U((1 << (log2Ceil(numBr) + 1)).W).asBools), VecInit((LowerMask(1.U << (shift-1.U))).asBools()))) 696 // TODO end 697 val afhob_dup = redirect_dup.map(_.cfiUpdate.afhob) 698 val lastBrNumOH_dup = redirect_dup.map(_.cfiUpdate.lastBrNumOH) 699 700 701 val isBr_dup = redirect_dup.map(_.cfiUpdate.pd.isBr) 702 val taken_dup = redirect_dup.map(_.cfiUpdate.taken) 703 val real_br_taken_mask_dup = 704 for (((shift, taken), addIntoHist) <- shift_dup zip taken_dup zip addIntoHist_dup) 705 yield (0 until numBr).map(i => shift === (i+1).U && taken && addIntoHist ) 706 707 val oldPtr_dup = redirect_dup.map(_.cfiUpdate.histPtr) 708 val oldFh_dup = redirect_dup.map(_.cfiUpdate.folded_hist) 709 val updated_ptr_dup = oldPtr_dup.zip(shift_dup).map {case (oldPtr, shift) => oldPtr - shift} 710 val updated_fh_dup = 711 for ((((((oldFh, afhob), lastBrNumOH), taken), addIntoHist), shift) <- 712 oldFh_dup zip afhob_dup zip lastBrNumOH_dup zip taken_dup zip addIntoHist_dup zip shift_dup) 713 yield VecInit((0 to numBr).map(i => oldFh.update(afhob, lastBrNumOH, i, taken && addIntoHist)))(shift) 714 val thisBrNumOH_dup = shift_dup.map(shift => UIntToOH(shift, numBr+1)) 715 val thisAheadFhOb_dup = dup_wire(new AllAheadFoldedHistoryOldestBits(foldedGHistInfos)) 716 thisAheadFhOb_dup.zip(oldPtr_dup).map {case (afhob, oldPtr) => afhob.read(ghv, oldPtr)} 717 val redirect_ghv_wens = (0 until HistoryLength).map(n => 718 (0 until numBr).map(b => oldPtr_dup(0).value === (CGHPtr(false.B, n.U) + b.U).value && shouldShiftVec_dup(0)(b) && do_redirect_dup(0).valid)) 719 val redirect_ghv_wdatas = (0 until HistoryLength).map(n => 720 Mux1H( 721 (0 until numBr).map(b => oldPtr_dup(0).value === (CGHPtr(false.B, n.U) + b.U).value && shouldShiftVec_dup(0)(b)), 722 real_br_taken_mask_dup(0) 723 ) 724 ) 725 726 if (EnableGHistDiff) { 727 val updated_ghist = WireInit(getHist(updated_ptr_dup(0)).asTypeOf(Vec(HistoryLength, Bool()))) 728 for (i <- 0 until numBr) { 729 when (shift_dup(0) >= (i+1).U) { 730 updated_ghist(i) := taken_dup(0) && addIntoHist_dup(0) && (i==0).B 731 } 732 } 733 when(do_redirect_dup(0).valid) { 734 s0_ghist := updated_ghist.asUInt 735 } 736 } 737 738 // Commit time history checker 739 if (EnableCommitGHistDiff) { 740 val commitGHist = RegInit(0.U.asTypeOf(Vec(HistoryLength, Bool()))) 741 val commitGHistPtr = RegInit(0.U.asTypeOf(new CGHPtr)) 742 def getCommitHist(ptr: CGHPtr): UInt = 743 (Cat(commitGHist.asUInt, commitGHist.asUInt) >> (ptr.value+1.U))(HistoryLength-1, 0) 744 745 val updateValid : Bool = io.ftq_to_bpu.update.valid 746 val branchValidMask : UInt = io.ftq_to_bpu.update.bits.ftb_entry.brValids.asUInt 747 val branchCommittedMask: Vec[Bool] = io.ftq_to_bpu.update.bits.br_committed 748 val misPredictMask : UInt = io.ftq_to_bpu.update.bits.mispred_mask.asUInt 749 val takenMask : UInt = 750 io.ftq_to_bpu.update.bits.br_taken_mask.asUInt | 751 io.ftq_to_bpu.update.bits.ftb_entry.always_taken.asUInt // Always taken branch is recorded in history 752 val takenIdx : UInt = (PriorityEncoder(takenMask) + 1.U((log2Ceil(numBr)+1).W)).asUInt 753 val misPredictIdx : UInt = (PriorityEncoder(misPredictMask) + 1.U((log2Ceil(numBr)+1).W)).asUInt 754 val shouldShiftMask: UInt = Mux(takenMask.orR, 755 LowerMask(takenIdx).asUInt, 756 ((1 << numBr) - 1).asUInt) & 757 Mux(misPredictMask.orR, 758 LowerMask(misPredictIdx).asUInt, 759 ((1 << numBr) - 1).asUInt) & 760 branchCommittedMask.asUInt 761 val updateShift : UInt = 762 Mux(updateValid && branchValidMask.orR, PopCount(branchValidMask & shouldShiftMask), 0.U) 763 dontTouch(updateShift) 764 dontTouch(commitGHist) 765 dontTouch(commitGHistPtr) 766 dontTouch(takenMask) 767 dontTouch(branchValidMask) 768 dontTouch(branchCommittedMask) 769 770 // Maintain the commitGHist 771 for (i <- 0 until numBr) { 772 when(updateShift >= (i + 1).U) { 773 val ptr: CGHPtr = commitGHistPtr - i.asUInt 774 commitGHist(ptr.value) := takenMask(i) 775 } 776 } 777 when(updateValid) { 778 commitGHistPtr := commitGHistPtr - updateShift 779 } 780 781 // Calculate true history using Parallel XOR 782 def computeFoldedHist(hist: UInt, compLen: Int)(histLen: Int): UInt = { 783 if (histLen > 0) { 784 val nChunks = (histLen + compLen - 1) / compLen 785 val hist_chunks = (0 until nChunks) map { i => 786 hist(min((i + 1) * compLen, histLen) - 1, i * compLen) 787 } 788 ParallelXOR(hist_chunks) 789 } 790 else 0.U 791 } 792 // Do differential 793 val predictFHistAll: AllFoldedHistories = io.ftq_to_bpu.update.bits.spec_info.folded_hist 794 TageTableInfos.map { 795 case (nRows, histLen, _) => { 796 val nRowsPerBr = nRows / numBr 797 val commitTrueHist: UInt = computeFoldedHist(getCommitHist(commitGHistPtr), log2Ceil(nRowsPerBr))(histLen) 798 val predictFHist : UInt = predictFHistAll. 799 getHistWithInfo((histLen, min(histLen, log2Ceil(nRowsPerBr)))).folded_hist 800 XSWarn(updateValid && predictFHist =/= commitTrueHist, 801 p"predict time ghist: ${predictFHist} is different from commit time: ${commitTrueHist}\n") 802 } 803 } 804 } 805 806 807 // val updatedGh = oldGh.update(shift, taken && addIntoHist) 808 for ((npcGen, do_redirect) <- npcGen_dup zip do_redirect_dup) 809 npcGen.register(do_redirect.valid, do_redirect.bits.cfiUpdate.target, Some("redirect_target"), 2) 810 for (((foldedGhGen, do_redirect), updated_fh) <- foldedGhGen_dup zip do_redirect_dup zip updated_fh_dup) 811 foldedGhGen.register(do_redirect.valid, updated_fh, Some("redirect_FGHT"), 2) 812 for (((ghistPtrGen, do_redirect), updated_ptr) <- ghistPtrGen_dup zip do_redirect_dup zip updated_ptr_dup) 813 ghistPtrGen.register(do_redirect.valid, updated_ptr, Some("redirect_GHPtr"), 2) 814 for (((lastBrNumOHGen, do_redirect), thisBrNumOH) <- lastBrNumOHGen_dup zip do_redirect_dup zip thisBrNumOH_dup) 815 lastBrNumOHGen.register(do_redirect.valid, thisBrNumOH, Some("redirect_BrNumOH"), 2) 816 for (((aheadFhObGen, do_redirect), thisAheadFhOb) <- aheadFhObGen_dup zip do_redirect_dup zip thisAheadFhOb_dup) 817 aheadFhObGen.register(do_redirect.valid, thisAheadFhOb, Some("redirect_AFHOB"), 2) 818 ghvBitWriteGens.zip(redirect_ghv_wens).zipWithIndex.map{case ((b, w), i) => 819 b.register(w.reduce(_||_), redirect_ghv_wdatas(i), Some(s"redirect_new_bit_$i"), 2) 820 } 821 // no need to assign s0_last_pred 822 823 // val need_reset = RegNext(reset.asBool) && !reset.asBool 824 825 // Reset 826 // npcGen.register(need_reset, resetVector.U, Some("reset_pc"), 1) 827 // foldedGhGen.register(need_reset, 0.U.asTypeOf(s0_folded_gh), Some("reset_FGH"), 1) 828 // ghistPtrGen.register(need_reset, 0.U.asTypeOf(new CGHPtr), Some("reset_GHPtr"), 1) 829 830 s0_pc_dup.zip(npcGen_dup).map {case (s0_pc, npcGen) => s0_pc := npcGen()} 831 s0_folded_gh_dup.zip(foldedGhGen_dup).map {case (s0_folded_gh, foldedGhGen) => s0_folded_gh := foldedGhGen()} 832 s0_ghist_ptr_dup.zip(ghistPtrGen_dup).map {case (s0_ghist_ptr, ghistPtrGen) => s0_ghist_ptr := ghistPtrGen()} 833 s0_ahead_fh_oldest_bits_dup.zip(aheadFhObGen_dup).map {case (s0_ahead_fh_oldest_bits, aheadFhObGen) => 834 s0_ahead_fh_oldest_bits := aheadFhObGen()} 835 s0_last_br_num_oh_dup.zip(lastBrNumOHGen_dup).map {case (s0_last_br_num_oh, lastBrNumOHGen) => 836 s0_last_br_num_oh := lastBrNumOHGen()} 837 (ghv_write_datas zip ghvBitWriteGens).map{case (wd, d) => wd := d()} 838 for (i <- 0 until HistoryLength) { 839 ghv_wens(i) := Seq(s1_ghv_wens, s2_ghv_wens, s3_ghv_wens, redirect_ghv_wens).map(_(i).reduce(_||_)).reduce(_||_) 840 when (ghv_wens(i)) { 841 ghv(i) := ghv_write_datas(i) 842 } 843 } 844 845 // TODO: signals for memVio and other Redirects 846 controlRedirectBubble := do_redirect_dup(0).valid && do_redirect_dup(0).bits.ControlRedirectBubble 847 ControlBTBMissBubble := do_redirect_dup(0).bits.ControlBTBMissBubble 848 TAGEMissBubble := do_redirect_dup(0).bits.TAGEMissBubble 849 SCMissBubble := do_redirect_dup(0).bits.SCMissBubble 850 ITTAGEMissBubble := do_redirect_dup(0).bits.ITTAGEMissBubble 851 RASMissBubble := do_redirect_dup(0).bits.RASMissBubble 852 853 memVioRedirectBubble := do_redirect_dup(0).valid && do_redirect_dup(0).bits.MemVioRedirectBubble 854 otherRedirectBubble := do_redirect_dup(0).valid && do_redirect_dup(0).bits.OtherRedirectBubble 855 btbMissBubble := do_redirect_dup(0).valid && do_redirect_dup(0).bits.BTBMissBubble 856 overrideBubble(0) := s2_redirect_dup(0) 857 overrideBubble(1) := s3_redirect_dup(0) 858 ftqUpdateBubble(0) := !s1_components_ready_dup(0) 859 ftqUpdateBubble(1) := !s2_components_ready_dup(0) 860 ftqUpdateBubble(2) := !s3_components_ready_dup(0) 861 ftqFullStall := !io.bpu_to_ftq.resp.ready 862 io.bpu_to_ftq.resp.bits.topdown_info := topdown_stages(numOfStage - 1) 863 864 // topdown handling logic here 865 when (controlRedirectBubble) { 866 /* 867 for (i <- 0 until numOfStage) 868 topdown_stages(i).reasons(TopDownCounters.ControlRedirectBubble.id) := true.B 869 io.bpu_to_ftq.resp.bits.topdown_info.reasons(TopDownCounters.ControlRedirectBubble.id) := true.B 870 */ 871 when (ControlBTBMissBubble) { 872 for (i <- 0 until numOfStage) 873 topdown_stages(i).reasons(TopDownCounters.BTBMissBubble.id) := true.B 874 io.bpu_to_ftq.resp.bits.topdown_info.reasons(TopDownCounters.BTBMissBubble.id) := true.B 875 } .elsewhen (TAGEMissBubble) { 876 for (i <- 0 until numOfStage) 877 topdown_stages(i).reasons(TopDownCounters.TAGEMissBubble.id) := true.B 878 io.bpu_to_ftq.resp.bits.topdown_info.reasons(TopDownCounters.TAGEMissBubble.id) := true.B 879 } .elsewhen (SCMissBubble) { 880 for (i <- 0 until numOfStage) 881 topdown_stages(i).reasons(TopDownCounters.SCMissBubble.id) := true.B 882 io.bpu_to_ftq.resp.bits.topdown_info.reasons(TopDownCounters.SCMissBubble.id) := true.B 883 } .elsewhen (ITTAGEMissBubble) { 884 for (i <- 0 until numOfStage) 885 topdown_stages(i).reasons(TopDownCounters.ITTAGEMissBubble.id) := true.B 886 io.bpu_to_ftq.resp.bits.topdown_info.reasons(TopDownCounters.ITTAGEMissBubble.id) := true.B 887 } .elsewhen (RASMissBubble) { 888 for (i <- 0 until numOfStage) 889 topdown_stages(i).reasons(TopDownCounters.RASMissBubble.id) := true.B 890 io.bpu_to_ftq.resp.bits.topdown_info.reasons(TopDownCounters.RASMissBubble.id) := true.B 891 } 892 } 893 when (memVioRedirectBubble) { 894 for (i <- 0 until numOfStage) 895 topdown_stages(i).reasons(TopDownCounters.MemVioRedirectBubble.id) := true.B 896 io.bpu_to_ftq.resp.bits.topdown_info.reasons(TopDownCounters.MemVioRedirectBubble.id) := true.B 897 } 898 when (otherRedirectBubble) { 899 for (i <- 0 until numOfStage) 900 topdown_stages(i).reasons(TopDownCounters.OtherRedirectBubble.id) := true.B 901 io.bpu_to_ftq.resp.bits.topdown_info.reasons(TopDownCounters.OtherRedirectBubble.id) := true.B 902 } 903 when (btbMissBubble) { 904 for (i <- 0 until numOfStage) 905 topdown_stages(i).reasons(TopDownCounters.BTBMissBubble.id) := true.B 906 io.bpu_to_ftq.resp.bits.topdown_info.reasons(TopDownCounters.BTBMissBubble.id) := true.B 907 } 908 909 for (i <- 0 until numOfStage) { 910 if (i < numOfStage - overrideStage) { 911 when (overrideBubble(i)) { 912 for (j <- 0 to i) 913 topdown_stages(j).reasons(TopDownCounters.OverrideBubble.id) := true.B 914 } 915 } 916 if (i < numOfStage - ftqUpdateStage) { 917 when (ftqUpdateBubble(i)) { 918 topdown_stages(i).reasons(TopDownCounters.FtqUpdateBubble.id) := true.B 919 } 920 } 921 } 922 when (ftqFullStall) { 923 topdown_stages(0).reasons(TopDownCounters.FtqFullStall.id) := true.B 924 } 925 926 XSError(isBefore(redirect_dup(0).cfiUpdate.histPtr, s3_ghist_ptr_dup(0)) && do_redirect_dup(0).valid, 927 p"s3_ghist_ptr ${s3_ghist_ptr_dup(0)} exceeds redirect histPtr ${redirect_dup(0).cfiUpdate.histPtr}\n") 928 XSError(isBefore(redirect_dup(0).cfiUpdate.histPtr, s2_ghist_ptr_dup(0)) && do_redirect_dup(0).valid, 929 p"s2_ghist_ptr ${s2_ghist_ptr_dup(0)} exceeds redirect histPtr ${redirect_dup(0).cfiUpdate.histPtr}\n") 930 XSError(isBefore(redirect_dup(0).cfiUpdate.histPtr, s1_ghist_ptr_dup(0)) && do_redirect_dup(0).valid, 931 p"s1_ghist_ptr ${s1_ghist_ptr_dup(0)} exceeds redirect histPtr ${redirect_dup(0).cfiUpdate.histPtr}\n") 932 933 XSDebug(RegNext(reset.asBool) && !reset.asBool, "Reseting...\n") 934 XSDebug(io.ftq_to_bpu.update.valid, p"Update from ftq\n") 935 XSDebug(io.ftq_to_bpu.redirect.valid, p"Redirect from ftq\n") 936 937 XSDebug("[BP0] fire=%d pc=%x\n", s0_fire_dup(0), s0_pc_dup(0)) 938 XSDebug("[BP1] v=%d r=%d cr=%d fire=%d flush=%d pc=%x\n", 939 s1_valid_dup(0), s1_ready_dup(0), s1_components_ready_dup(0), s1_fire_dup(0), s1_flush_dup(0), s1_pc) 940 XSDebug("[BP2] v=%d r=%d cr=%d fire=%d redirect=%d flush=%d pc=%x\n", 941 s2_valid_dup(0), s2_ready_dup(0), s2_components_ready_dup(0), s2_fire_dup(0), s2_redirect_dup(0), s2_flush_dup(0), s2_pc) 942 XSDebug("[BP3] v=%d r=%d cr=%d fire=%d redirect=%d flush=%d pc=%x\n", 943 s3_valid_dup(0), s3_ready_dup(0), s3_components_ready_dup(0), s3_fire_dup(0), s3_redirect_dup(0), s3_flush_dup(0), s3_pc) 944 XSDebug("[FTQ] ready=%d\n", io.bpu_to_ftq.resp.ready) 945 XSDebug("resp.s1.target=%x\n", resp.s1.getTarget(0)) 946 XSDebug("resp.s2.target=%x\n", resp.s2.getTarget(0)) 947 // XSDebug("s0_ghist: %b\n", s0_ghist.predHist) 948 // XSDebug("s1_ghist: %b\n", s1_ghist.predHist) 949 // XSDebug("s2_ghist: %b\n", s2_ghist.predHist) 950 // XSDebug("s2_predicted_ghist: %b\n", s2_predicted_ghist.predHist) 951 XSDebug(p"s0_ghist_ptr: ${s0_ghist_ptr_dup(0)}\n") 952 XSDebug(p"s1_ghist_ptr: ${s1_ghist_ptr_dup(0)}\n") 953 XSDebug(p"s2_ghist_ptr: ${s2_ghist_ptr_dup(0)}\n") 954 XSDebug(p"s3_ghist_ptr: ${s3_ghist_ptr_dup(0)}\n") 955 956 io.ftq_to_bpu.update.bits.display(io.ftq_to_bpu.update.valid) 957 io.ftq_to_bpu.redirect.bits.display(io.ftq_to_bpu.redirect.valid) 958 959 960 XSPerfAccumulate("s2_redirect", s2_redirect_dup(0)) 961 XSPerfAccumulate("s3_redirect", s3_redirect_dup(0)) 962 XSPerfAccumulate("s1_not_valid", !s1_valid_dup(0)) 963 964 val perfEvents = predictors.asInstanceOf[Composer].getPerfEvents 965 generatePerfEvent() 966} 967