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 org.chipsalliance.cde.config.Parameters 20import chisel3._ 21import chisel3.util._ 22import xiangshan._ 23import utils._ 24import utility._ 25 26import scala.math.min 27import xiangshan.backend.decode.ImmUnion 28 29trait HasBPUConst extends HasXSParameter { 30 val MaxMetaBaseLength = if (!env.FPGAPlatform) 512 else 247 // TODO: Reduce meta length 31 val MaxMetaLength = if (HasHExtension) MaxMetaBaseLength + 4 else MaxMetaBaseLength 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 222class Predictor(implicit p: Parameters) extends XSModule with HasBPUConst with HasPerfEvents with HasCircularQueuePtrHelper { 223 val io = IO(new PredictorIO) 224 225 val ctrl = DelayN(io.ctrl, 1) 226 val predictors = Module(if (useBPD) new Composer else new FakePredictor) 227 228 def numOfStage = 3 229 require(numOfStage > 1, "BPU numOfStage must be greater than 1") 230 val topdown_stages = RegInit(VecInit(Seq.fill(numOfStage)(0.U.asTypeOf(new FrontendTopDownBundle)))) 231 232 // following can only happen on s1 233 val controlRedirectBubble = Wire(Bool()) 234 val ControlBTBMissBubble = Wire(Bool()) 235 val TAGEMissBubble = Wire(Bool()) 236 val SCMissBubble = Wire(Bool()) 237 val ITTAGEMissBubble = Wire(Bool()) 238 val RASMissBubble = Wire(Bool()) 239 240 val memVioRedirectBubble = Wire(Bool()) 241 val otherRedirectBubble = Wire(Bool()) 242 val btbMissBubble = Wire(Bool()) 243 otherRedirectBubble := false.B 244 memVioRedirectBubble := false.B 245 246 // override can happen between s1-s2 and s2-s3 247 val overrideBubble = Wire(Vec(numOfStage - 1, Bool())) 248 def overrideStage = 1 249 // ftq update block can happen on s1, s2 and s3 250 val ftqUpdateBubble = Wire(Vec(numOfStage, Bool())) 251 def ftqUpdateStage = 0 252 // ftq full stall only happens on s3 (last stage) 253 val ftqFullStall = Wire(Bool()) 254 255 // by default, no bubble event 256 topdown_stages(0) := 0.U.asTypeOf(new FrontendTopDownBundle) 257 // event movement driven by clock only 258 for (i <- 0 until numOfStage - 1) { 259 topdown_stages(i + 1) := topdown_stages(i) 260 } 261 262 263 264 // ctrl signal 265 predictors.io.ctrl := ctrl 266 predictors.io.reset_vector := io.reset_vector 267 268 269 val reset_vector = DelayN(io.reset_vector, 5) 270 271 val s0_fire_dup, s1_fire_dup, s2_fire_dup, s3_fire_dup = dup_wire(Bool()) 272 val s1_valid_dup, s2_valid_dup, s3_valid_dup = dup_seq(RegInit(false.B)) 273 val s1_ready_dup, s2_ready_dup, s3_ready_dup = dup_wire(Bool()) 274 val s1_components_ready_dup, s2_components_ready_dup, s3_components_ready_dup = dup_wire(Bool()) 275 276 val s0_pc_dup = dup(WireInit(0.U.asTypeOf(UInt(VAddrBits.W)))) 277 val s0_pc_reg_dup = s0_pc_dup.map(x => RegNext(x)) 278 when (RegNext(RegNext(reset.asBool) && !reset.asBool)) { 279 s0_pc_reg_dup.map{case s0_pc => s0_pc := reset_vector} 280 } 281 val s1_pc = RegEnable(s0_pc_dup(0), s0_fire_dup(0)) 282 val s2_pc = RegEnable(s1_pc, s1_fire_dup(0)) 283 val s3_pc = RegEnable(s2_pc, s2_fire_dup(0)) 284 285 val s0_folded_gh_dup = dup_wire(new AllFoldedHistories(foldedGHistInfos)) 286 val s0_folded_gh_reg_dup = s0_folded_gh_dup.map(x => RegNext(x, init=0.U.asTypeOf(s0_folded_gh_dup(0)))) 287 val s1_folded_gh_dup = RegEnable(s0_folded_gh_dup, 0.U.asTypeOf(s0_folded_gh_dup), s0_fire_dup(1)) 288 val s2_folded_gh_dup = RegEnable(s1_folded_gh_dup, 0.U.asTypeOf(s0_folded_gh_dup), s1_fire_dup(1)) 289 val s3_folded_gh_dup = RegEnable(s2_folded_gh_dup, 0.U.asTypeOf(s0_folded_gh_dup), s2_fire_dup(1)) 290 291 val s0_last_br_num_oh_dup = dup_wire(UInt((numBr+1).W)) 292 val s0_last_br_num_oh_reg_dup = s0_last_br_num_oh_dup.map(x => RegNext(x, init=0.U)) 293 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)) 294 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)) 295 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)) 296 297 val s0_ahead_fh_oldest_bits_dup = dup_wire(new AllAheadFoldedHistoryOldestBits(foldedGHistInfos)) 298 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)))) 299 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)) 300 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)) 301 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)) 302 303 val npcGen_dup = Seq.tabulate(numDup)(n => new PhyPriorityMuxGenerator[UInt]) 304 val foldedGhGen_dup = Seq.tabulate(numDup)(n => new PhyPriorityMuxGenerator[AllFoldedHistories]) 305 val ghistPtrGen_dup = Seq.tabulate(numDup)(n => new PhyPriorityMuxGenerator[CGHPtr]) 306 val lastBrNumOHGen_dup = Seq.tabulate(numDup)(n => new PhyPriorityMuxGenerator[UInt]) 307 val aheadFhObGen_dup = Seq.tabulate(numDup)(n => new PhyPriorityMuxGenerator[AllAheadFoldedHistoryOldestBits]) 308 309 val ghvBitWriteGens = Seq.tabulate(HistoryLength)(n => new PhyPriorityMuxGenerator[Bool]) 310 // val ghistGen = new PhyPriorityMuxGenerator[UInt] 311 312 val ghv = RegInit(0.U.asTypeOf(Vec(HistoryLength, Bool()))) 313 val ghv_wire = WireInit(ghv) 314 315 val s0_ghist = WireInit(0.U.asTypeOf(UInt(HistoryLength.W))) 316 317 318 println(f"history buffer length ${HistoryLength}") 319 val ghv_write_datas = Wire(Vec(HistoryLength, Bool())) 320 val ghv_wens = Wire(Vec(HistoryLength, Bool())) 321 322 val s0_ghist_ptr_dup = dup_wire(new CGHPtr) 323 val s0_ghist_ptr_reg_dup = s0_ghist_ptr_dup.map(x => RegNext(x, init=0.U.asTypeOf(new CGHPtr))) 324 val s1_ghist_ptr_dup = RegEnable(s0_ghist_ptr_dup, 0.U.asTypeOf(s0_ghist_ptr_dup), s0_fire_dup(1)) 325 val s2_ghist_ptr_dup = RegEnable(s1_ghist_ptr_dup, 0.U.asTypeOf(s0_ghist_ptr_dup), s1_fire_dup(1)) 326 val s3_ghist_ptr_dup = RegEnable(s2_ghist_ptr_dup, 0.U.asTypeOf(s0_ghist_ptr_dup), s2_fire_dup(1)) 327 328 def getHist(ptr: CGHPtr): UInt = (Cat(ghv_wire.asUInt, ghv_wire.asUInt) >> (ptr.value+1.U))(HistoryLength-1, 0) 329 s0_ghist := getHist(s0_ghist_ptr_dup(0)) 330 331 val resp = predictors.io.out 332 333 334 val toFtq_fire = io.bpu_to_ftq.resp.valid && io.bpu_to_ftq.resp.ready 335 336 val s1_flush_dup, s2_flush_dup, s3_flush_dup = dup_wire(Bool()) 337 val s2_redirect_dup, s3_redirect_dup = dup_wire(Bool()) 338 339 // predictors.io := DontCare 340 predictors.io.in.valid := s0_fire_dup(0) 341 predictors.io.in.bits.s0_pc := s0_pc_dup 342 predictors.io.in.bits.ghist := s0_ghist 343 predictors.io.in.bits.folded_hist := s0_folded_gh_dup 344 predictors.io.in.bits.resp_in(0) := (0.U).asTypeOf(new BranchPredictionResp) 345 // predictors.io.in.bits.resp_in(0).s1.pc := s0_pc 346 // predictors.io.in.bits.toFtq_fire := toFtq_fire 347 348 // predictors.io.out.ready := io.bpu_to_ftq.resp.ready 349 350 val redirect_req = io.ftq_to_bpu.redirect 351 val do_redirect_dup = dup_seq(RegNextWithEnable(redirect_req)) 352 353 // Pipeline logic 354 s2_redirect_dup.map(_ := false.B) 355 s3_redirect_dup.map(_ := false.B) 356 357 s3_flush_dup.map(_ := redirect_req.valid) // flush when redirect comes 358 for (((s2_flush, s3_flush), s3_redirect) <- s2_flush_dup zip s3_flush_dup zip s3_redirect_dup) 359 s2_flush := s3_flush || s3_redirect 360 for (((s1_flush, s2_flush), s2_redirect) <- s1_flush_dup zip s2_flush_dup zip s2_redirect_dup) 361 s1_flush := s2_flush || s2_redirect 362 363 364 s1_components_ready_dup.map(_ := predictors.io.s1_ready) 365 for (((s1_ready, s1_fire), s1_valid) <- s1_ready_dup zip s1_fire_dup zip s1_valid_dup) 366 s1_ready := s1_fire || !s1_valid 367 for (((s0_fire, s1_components_ready), s1_ready) <- s0_fire_dup zip s1_components_ready_dup zip s1_ready_dup) 368 s0_fire := s1_components_ready && s1_ready 369 predictors.io.s0_fire := s0_fire_dup 370 371 s2_components_ready_dup.map(_ := predictors.io.s2_ready) 372 for (((s2_ready, s2_fire), s2_valid) <- s2_ready_dup zip s2_fire_dup zip s2_valid_dup) 373 s2_ready := s2_fire || !s2_valid 374 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) 375 s1_fire := s1_valid && s2_components_ready && s2_ready && io.bpu_to_ftq.resp.ready 376 377 s3_components_ready_dup.map(_ := predictors.io.s3_ready) 378 for (((s3_ready, s3_fire), s3_valid) <- s3_ready_dup zip s3_fire_dup zip s3_valid_dup) 379 s3_ready := s3_fire || !s3_valid 380 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) 381 s2_fire := s2_valid && s3_components_ready && s3_ready 382 383 for ((((s0_fire, s1_flush), s1_fire), s1_valid) <- s0_fire_dup zip s1_flush_dup zip s1_fire_dup zip s1_valid_dup) { 384 when (redirect_req.valid) { s1_valid := false.B } 385 .elsewhen(s0_fire) { s1_valid := true.B } 386 .elsewhen(s1_flush) { s1_valid := false.B } 387 .elsewhen(s1_fire) { s1_valid := false.B } 388 } 389 predictors.io.s1_fire := s1_fire_dup 390 391 s2_fire_dup := s2_valid_dup 392 393 for (((((s1_fire, s2_flush), s2_fire), s2_valid), s1_flush) <- 394 s1_fire_dup zip s2_flush_dup zip s2_fire_dup zip s2_valid_dup zip s1_flush_dup) { 395 396 when (s2_flush) { s2_valid := false.B } 397 .elsewhen(s1_fire) { s2_valid := !s1_flush } 398 .elsewhen(s2_fire) { s2_valid := false.B } 399 } 400 401 predictors.io.s2_fire := s2_fire_dup 402 predictors.io.s2_redirect := s2_redirect_dup 403 404 s3_fire_dup := s3_valid_dup 405 406 for (((((s2_fire, s3_flush), s3_fire), s3_valid), s2_flush) <- 407 s2_fire_dup zip s3_flush_dup zip s3_fire_dup zip s3_valid_dup zip s2_flush_dup) { 408 409 when (s3_flush) { s3_valid := false.B } 410 .elsewhen(s2_fire) { s3_valid := !s2_flush } 411 .elsewhen(s3_fire) { s3_valid := false.B } 412 } 413 414 predictors.io.s3_fire := s3_fire_dup 415 predictors.io.s3_redirect := s3_redirect_dup 416 417 418 io.bpu_to_ftq.resp.valid := 419 s1_valid_dup(2) && s2_components_ready_dup(2) && s2_ready_dup(2) || 420 s2_fire_dup(2) && s2_redirect_dup(2) || 421 s3_fire_dup(2) && s3_redirect_dup(2) 422 io.bpu_to_ftq.resp.bits := predictors.io.out 423 io.bpu_to_ftq.resp.bits.last_stage_spec_info.histPtr := s3_ghist_ptr_dup(2) 424 425 val full_pred_diff = WireInit(false.B) 426 val full_pred_diff_stage = WireInit(0.U) 427 val full_pred_diff_offset = WireInit(0.U) 428 for (i <- 0 until numDup - 1) { 429 when (io.bpu_to_ftq.resp.valid && 430 ((io.bpu_to_ftq.resp.bits.s1.full_pred(i).asTypeOf(UInt()) =/= io.bpu_to_ftq.resp.bits.s1.full_pred(i+1).asTypeOf(UInt()) && io.bpu_to_ftq.resp.bits.s1.full_pred(i).hit) || 431 (io.bpu_to_ftq.resp.bits.s2.full_pred(i).asTypeOf(UInt()) =/= io.bpu_to_ftq.resp.bits.s2.full_pred(i+1).asTypeOf(UInt()) && io.bpu_to_ftq.resp.bits.s2.full_pred(i).hit) || 432 (io.bpu_to_ftq.resp.bits.s3.full_pred(i).asTypeOf(UInt()) =/= io.bpu_to_ftq.resp.bits.s3.full_pred(i+1).asTypeOf(UInt()) && io.bpu_to_ftq.resp.bits.s3.full_pred(i).hit))) { 433 full_pred_diff := true.B 434 full_pred_diff_offset := i.U 435 when (io.bpu_to_ftq.resp.bits.s1.full_pred(i).asTypeOf(UInt()) =/= io.bpu_to_ftq.resp.bits.s1.full_pred(i+1).asTypeOf(UInt())) { 436 full_pred_diff_stage := 1.U 437 } .elsewhen (io.bpu_to_ftq.resp.bits.s2.full_pred(i).asTypeOf(UInt()) =/= io.bpu_to_ftq.resp.bits.s2.full_pred(i+1).asTypeOf(UInt())) { 438 full_pred_diff_stage := 2.U 439 } .otherwise { 440 full_pred_diff_stage := 3.U 441 } 442 } 443 } 444 XSError(full_pred_diff, "Full prediction difference detected!") 445 446 npcGen_dup.zip(s0_pc_reg_dup).map{ case (gen, reg) => 447 gen.register(true.B, reg, Some("stallPC"), 0)} 448 foldedGhGen_dup.zip(s0_folded_gh_reg_dup).map{ case (gen, reg) => 449 gen.register(true.B, reg, Some("stallFGH"), 0)} 450 ghistPtrGen_dup.zip(s0_ghist_ptr_reg_dup).map{ case (gen, reg) => 451 gen.register(true.B, reg, Some("stallGHPtr"), 0)} 452 lastBrNumOHGen_dup.zip(s0_last_br_num_oh_reg_dup).map{ case (gen, reg) => 453 gen.register(true.B, reg, Some("stallBrNumOH"), 0)} 454 aheadFhObGen_dup.zip(s0_ahead_fh_oldest_bits_reg_dup).map{ case (gen, reg) => 455 gen.register(true.B, reg, Some("stallAFHOB"), 0)} 456 457 // assign pred cycle for profiling 458 io.bpu_to_ftq.resp.bits.s1.full_pred.map(_.predCycle.map(_ := GTimer())) 459 io.bpu_to_ftq.resp.bits.s2.full_pred.map(_.predCycle.map(_ := GTimer())) 460 io.bpu_to_ftq.resp.bits.s3.full_pred.map(_.predCycle.map(_ := GTimer())) 461 462 463 464 // History manage 465 // s1 466 val s1_possible_predicted_ghist_ptrs_dup = s1_ghist_ptr_dup.map(ptr => (0 to numBr).map(ptr - _.U)) 467 val s1_predicted_ghist_ptr_dup = s1_possible_predicted_ghist_ptrs_dup.zip(resp.s1.lastBrPosOH).map{ case (ptr, oh) => Mux1H(oh, ptr)} 468 val s1_possible_predicted_fhs_dup = 469 for (((((fgh, afh), br_num_oh), t), br_pos_oh) <- 470 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) 471 yield (0 to numBr).map(i => 472 fgh.update(afh, br_num_oh, i, t & br_pos_oh(i)) 473 ) 474 val s1_predicted_fh_dup = resp.s1.lastBrPosOH.zip(s1_possible_predicted_fhs_dup).map{ case (oh, fh) => Mux1H(oh, fh)} 475 476 val s1_ahead_fh_ob_src_dup = dup_wire(new AllAheadFoldedHistoryOldestBits(foldedGHistInfos)) 477 s1_ahead_fh_ob_src_dup.zip(s1_ghist_ptr_dup).map{ case (src, ptr) => src.read(ghv, ptr)} 478 479 if (EnableGHistDiff) { 480 val s1_predicted_ghist = WireInit(getHist(s1_predicted_ghist_ptr_dup(0)).asTypeOf(Vec(HistoryLength, Bool()))) 481 for (i <- 0 until numBr) { 482 when (resp.s1.shouldShiftVec(0)(i)) { 483 s1_predicted_ghist(i) := resp.s1.brTaken(0) && (i==0).B 484 } 485 } 486 when (s1_valid_dup(0)) { 487 s0_ghist := s1_predicted_ghist.asUInt 488 } 489 } 490 491 val s1_ghv_wens = (0 until HistoryLength).map(n => 492 (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))) 493 val s1_ghv_wdatas = (0 until HistoryLength).map(n => 494 Mux1H( 495 (0 until numBr).map(b => ( 496 (s1_ghist_ptr_dup(0)).value === (CGHPtr(false.B, n.U) + b.U).value && resp.s1.shouldShiftVec(0)(b), 497 resp.s1.brTaken(0) && resp.s1.lastBrPosOH(0)(b+1) 498 )) 499 ) 500 ) 501 502 503 for (((npcGen, s1_valid), s1_target) <- npcGen_dup zip s1_valid_dup zip resp.s1.getTarget) 504 npcGen.register(s1_valid, s1_target, Some("s1_target"), 4) 505 for (((foldedGhGen, s1_valid), s1_predicted_fh) <- foldedGhGen_dup zip s1_valid_dup zip s1_predicted_fh_dup) 506 foldedGhGen.register(s1_valid, s1_predicted_fh, Some("s1_FGH"), 4) 507 for (((ghistPtrGen, s1_valid), s1_predicted_ghist_ptr) <- ghistPtrGen_dup zip s1_valid_dup zip s1_predicted_ghist_ptr_dup) 508 ghistPtrGen.register(s1_valid, s1_predicted_ghist_ptr, Some("s1_GHPtr"), 4) 509 for (((lastBrNumOHGen, s1_valid), s1_brPosOH) <- lastBrNumOHGen_dup zip s1_valid_dup zip resp.s1.lastBrPosOH.map(_.asUInt)) 510 lastBrNumOHGen.register(s1_valid, s1_brPosOH, Some("s1_BrNumOH"), 4) 511 for (((aheadFhObGen, s1_valid), s1_ahead_fh_ob_src) <- aheadFhObGen_dup zip s1_valid_dup zip s1_ahead_fh_ob_src_dup) 512 aheadFhObGen.register(s1_valid, s1_ahead_fh_ob_src, Some("s1_AFHOB"), 4) 513 ghvBitWriteGens.zip(s1_ghv_wens).zipWithIndex.map{case ((b, w), i) => 514 b.register(w.reduce(_||_), s1_ghv_wdatas(i), Some(s"s1_new_bit_$i"), 4) 515 } 516 517 class PreviousPredInfo extends Bundle { 518 val hit = Vec(numDup, Bool()) 519 val target = Vec(numDup, UInt(VAddrBits.W)) 520 val lastBrPosOH = Vec(numDup, Vec(numBr+1, Bool())) 521 val taken = Vec(numDup, Bool()) 522 val takenMask = Vec(numDup, Vec(numBr, Bool())) 523 val cfiIndex = Vec(numDup, UInt(log2Ceil(PredictWidth).W)) 524 } 525 526 def preds_needs_redirect_vec_dup(x: PreviousPredInfo, y: BranchPredictionBundle) = { 527 // Timing optimization 528 // We first compare all target with previous stage target, 529 // then select the difference by taken & hit 530 // Usually target is generated quicker than taken, so do target compare before select can help timing 531 val targetDiffVec: IndexedSeq[Vec[Bool]] = 532 x.target.zip(y.getAllTargets).map { 533 case (xTarget, yAllTarget) => VecInit(yAllTarget.map(_ =/= xTarget)) 534 } // [numDup][all Target comparison] 535 val targetDiff : IndexedSeq[Bool] = 536 targetDiffVec.zip(x.hit).zip(x.takenMask).map { 537 case ((diff, hit), takenMask) => selectByTaken(takenMask, hit, diff) 538 } // [numDup] 539 540 val lastBrPosOHDiff: IndexedSeq[Bool] = x.lastBrPosOH.zip(y.lastBrPosOH).map { case (oh1, oh2) => oh1.asUInt =/= oh2.asUInt } 541 val takenDiff : IndexedSeq[Bool] = x.taken.zip(y.taken).map { case (t1, t2) => t1 =/= t2 } 542 val takenOffsetDiff: IndexedSeq[Bool] = x.cfiIndex.zip(y.cfiIndex).zip(x.taken).zip(y.taken).map { case (((i1, i2), xt), yt) => xt && yt && i1 =/= i2.bits } 543 VecInit( 544 for ((((tgtd, lbpohd), tkd), tod) <- 545 targetDiff zip lastBrPosOHDiff zip takenDiff zip takenOffsetDiff) 546 yield VecInit(tgtd, lbpohd, tkd, tod) 547 // x.shouldShiftVec.asUInt =/= y.shouldShiftVec.asUInt, 548 // x.brTaken =/= y.brTaken 549 ) 550 } 551 552 // s2 553 val s2_possible_predicted_ghist_ptrs_dup = s2_ghist_ptr_dup.map(ptr => (0 to numBr).map(ptr - _.U)) 554 val s2_predicted_ghist_ptr_dup = s2_possible_predicted_ghist_ptrs_dup.zip(resp.s2.lastBrPosOH).map{ case (ptr, oh) => Mux1H(oh, ptr)} 555 556 val s2_possible_predicted_fhs_dup = 557 for ((((fgh, afh), br_num_oh), full_pred) <- 558 s2_folded_gh_dup zip s2_ahead_fh_oldest_bits_dup zip s2_last_br_num_oh_dup zip resp.s2.full_pred) 559 yield (0 to numBr).map(i => 560 fgh.update(afh, br_num_oh, i, if (i > 0) full_pred.br_taken_mask(i-1) else false.B) 561 ) 562 val s2_predicted_fh_dup = resp.s2.lastBrPosOH.zip(s2_possible_predicted_fhs_dup).map{ case (oh, fh) => Mux1H(oh, fh)} 563 564 val s2_ahead_fh_ob_src_dup = dup_wire(new AllAheadFoldedHistoryOldestBits(foldedGHistInfos)) 565 s2_ahead_fh_ob_src_dup.zip(s2_ghist_ptr_dup).map{ case (src, ptr) => src.read(ghv, ptr)} 566 567 if (EnableGHistDiff) { 568 val s2_predicted_ghist = WireInit(getHist(s2_predicted_ghist_ptr_dup(0)).asTypeOf(Vec(HistoryLength, Bool()))) 569 for (i <- 0 until numBr) { 570 when (resp.s2.shouldShiftVec(0)(i)) { 571 s2_predicted_ghist(i) := resp.s2.brTaken(0) && (i==0).B 572 } 573 } 574 when(s2_redirect_dup(0)) { 575 s0_ghist := s2_predicted_ghist.asUInt 576 } 577 } 578 579 val s2_ghv_wens = (0 until HistoryLength).map(n => 580 (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))) 581 val s2_ghv_wdatas = (0 until HistoryLength).map(n => 582 Mux1H( 583 (0 until numBr).map(b => ( 584 (s2_ghist_ptr_dup(0)).value === (CGHPtr(false.B, n.U) + b.U).value && resp.s2.shouldShiftVec(0)(b), 585 resp.s2.full_pred(0).real_br_taken_mask()(b) 586 )) 587 ) 588 ) 589 590 val s1_pred_info = Wire(new PreviousPredInfo) 591 s1_pred_info.hit := resp.s1.full_pred.map(_.hit) 592 s1_pred_info.target := resp.s1.getTarget 593 s1_pred_info.lastBrPosOH := resp.s1.lastBrPosOH 594 s1_pred_info.taken := resp.s1.taken 595 s1_pred_info.takenMask := resp.s1.full_pred.map(_.taken_mask_on_slot) 596 s1_pred_info.cfiIndex := resp.s1.cfiIndex.map { case x => x.bits } 597 598 val previous_s1_pred_info = RegEnable(s1_pred_info, 0.U.asTypeOf(new PreviousPredInfo), s1_fire_dup(0)) 599 600 val s2_redirect_s1_last_pred_vec_dup = preds_needs_redirect_vec_dup(previous_s1_pred_info, resp.s2) 601 602 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) 603 s2_redirect := s2_fire && s2_redirect_s1_last_pred_vec.reduce(_||_) 604 605 606 for (((npcGen, s2_redirect), s2_target) <- npcGen_dup zip s2_redirect_dup zip resp.s2.getTarget) 607 npcGen.register(s2_redirect, s2_target, Some("s2_target"), 5) 608 for (((foldedGhGen, s2_redirect), s2_predicted_fh) <- foldedGhGen_dup zip s2_redirect_dup zip s2_predicted_fh_dup) 609 foldedGhGen.register(s2_redirect, s2_predicted_fh, Some("s2_FGH"), 5) 610 for (((ghistPtrGen, s2_redirect), s2_predicted_ghist_ptr) <- ghistPtrGen_dup zip s2_redirect_dup zip s2_predicted_ghist_ptr_dup) 611 ghistPtrGen.register(s2_redirect, s2_predicted_ghist_ptr, Some("s2_GHPtr"), 5) 612 for (((lastBrNumOHGen, s2_redirect), s2_brPosOH) <- lastBrNumOHGen_dup zip s2_redirect_dup zip resp.s2.lastBrPosOH.map(_.asUInt)) 613 lastBrNumOHGen.register(s2_redirect, s2_brPosOH, Some("s2_BrNumOH"), 5) 614 for (((aheadFhObGen, s2_redirect), s2_ahead_fh_ob_src) <- aheadFhObGen_dup zip s2_redirect_dup zip s2_ahead_fh_ob_src_dup) 615 aheadFhObGen.register(s2_redirect, s2_ahead_fh_ob_src, Some("s2_AFHOB"), 5) 616 ghvBitWriteGens.zip(s2_ghv_wens).zipWithIndex.map{case ((b, w), i) => 617 b.register(w.reduce(_||_), s2_ghv_wdatas(i), Some(s"s2_new_bit_$i"), 5) 618 } 619 620 XSPerfAccumulate("s2_redirect_because_target_diff", s2_fire_dup(0) && s2_redirect_s1_last_pred_vec_dup(0)(0)) 621 XSPerfAccumulate("s2_redirect_because_branch_num_diff", s2_fire_dup(0) && s2_redirect_s1_last_pred_vec_dup(0)(1)) 622 XSPerfAccumulate("s2_redirect_because_direction_diff", s2_fire_dup(0) && s2_redirect_s1_last_pred_vec_dup(0)(2)) 623 XSPerfAccumulate("s2_redirect_because_cfi_idx_diff", s2_fire_dup(0) && s2_redirect_s1_last_pred_vec_dup(0)(3)) 624 // XSPerfAccumulate("s2_redirect_because_shouldShiftVec_diff", s2_fire && s2_redirect_s1_last_pred_vec(4)) 625 // XSPerfAccumulate("s2_redirect_because_brTaken_diff", s2_fire && s2_redirect_s1_last_pred_vec(5)) 626 XSPerfAccumulate("s2_redirect_because_fallThroughError", s2_fire_dup(0) && resp.s2.fallThruError(0)) 627 628 XSPerfAccumulate("s2_redirect_when_taken", s2_redirect_dup(0) && resp.s2.taken(0) && resp.s2.full_pred(0).hit) 629 XSPerfAccumulate("s2_redirect_when_not_taken", s2_redirect_dup(0) && !resp.s2.taken(0) && resp.s2.full_pred(0).hit) 630 XSPerfAccumulate("s2_redirect_when_not_hit", s2_redirect_dup(0) && !resp.s2.full_pred(0).hit) 631 632 633 // s3 634 val s3_possible_predicted_ghist_ptrs_dup = s3_ghist_ptr_dup.map(ptr => (0 to numBr).map(ptr - _.U)) 635 val s3_predicted_ghist_ptr_dup = s3_possible_predicted_ghist_ptrs_dup.zip(resp.s3.lastBrPosOH).map{ case (ptr, oh) => Mux1H(oh, ptr)} 636 637 val s3_possible_predicted_fhs_dup = 638 for ((((fgh, afh), br_num_oh), full_pred) <- 639 s3_folded_gh_dup zip s3_ahead_fh_oldest_bits_dup zip s3_last_br_num_oh_dup zip resp.s3.full_pred) 640 yield (0 to numBr).map(i => 641 fgh.update(afh, br_num_oh, i, if (i > 0) full_pred.br_taken_mask(i-1) else false.B) 642 ) 643 val s3_predicted_fh_dup = resp.s3.lastBrPosOH.zip(s3_possible_predicted_fhs_dup).map{ case (oh, fh) => Mux1H(oh, fh)} 644 645 val s3_ahead_fh_ob_src_dup = dup_wire(new AllAheadFoldedHistoryOldestBits(foldedGHistInfos)) 646 s3_ahead_fh_ob_src_dup.zip(s3_ghist_ptr_dup).map{ case (src, ptr) => src.read(ghv, ptr)} 647 648 if (EnableGHistDiff) { 649 val s3_predicted_ghist = WireInit(getHist(s3_predicted_ghist_ptr_dup(0)).asTypeOf(Vec(HistoryLength, Bool()))) 650 for (i <- 0 until numBr) { 651 when (resp.s3.shouldShiftVec(0)(i)) { 652 s3_predicted_ghist(i) := resp.s3.brTaken(0) && (i==0).B 653 } 654 } 655 when(s3_redirect_dup(0)) { 656 s0_ghist := s3_predicted_ghist.asUInt 657 } 658 } 659 660 val s3_ghv_wens = (0 until HistoryLength).map(n => 661 (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))) 662 val s3_ghv_wdatas = (0 until HistoryLength).map(n => 663 Mux1H( 664 (0 until numBr).map(b => ( 665 (s3_ghist_ptr_dup(0)).value === (CGHPtr(false.B, n.U) + b.U).value && resp.s3.shouldShiftVec(0)(b), 666 resp.s3.full_pred(0).real_br_taken_mask()(b) 667 )) 668 ) 669 ) 670 671 val previous_s2_pred = RegEnable(resp.s2, 0.U.asTypeOf(resp.s2), s2_fire_dup(0)) 672 673 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} 674 val s3_both_first_taken_dup = resp.s3.full_pred.zip(previous_s2_pred.full_pred).map {case (fp1, fp2) => fp1.real_br_taken_mask()(0) && fp2.real_br_taken_mask()(0)} 675 val s3_redirect_on_target_dup = resp.s3.getTarget.zip(previous_s2_pred.getTarget).map {case (t1, t2) => t1 =/= t2} 676 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} 677 val s3_redirect_on_fall_thru_error_dup = resp.s3.fallThruError 678 679 for ((((((s3_redirect, s3_fire), s3_redirect_on_br_taken), s3_redirect_on_target), s3_redirect_on_fall_thru_error), s3_both_first_taken) <- 680 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 zip s3_both_first_taken_dup) { 681 682 s3_redirect := s3_fire && ( 683 (s3_redirect_on_br_taken && !s3_both_first_taken) || s3_redirect_on_target || s3_redirect_on_fall_thru_error 684 ) 685 } 686 687 XSPerfAccumulate(f"s3_redirect_on_br_taken", s3_fire_dup(0) && s3_redirect_on_br_taken_dup(0)) 688 XSPerfAccumulate(f"s3_redirect_on_jalr_target", s3_fire_dup(0) && s3_redirect_on_jalr_target_dup(0)) 689 XSPerfAccumulate(f"s3_redirect_on_others", s3_redirect_dup(0) && !(s3_redirect_on_br_taken_dup(0) || s3_redirect_on_jalr_target_dup(0))) 690 691 for (((npcGen, s3_redirect), s3_target) <- npcGen_dup zip s3_redirect_dup zip resp.s3.getTarget) 692 npcGen.register(s3_redirect, s3_target, Some("s3_target"), 3) 693 for (((foldedGhGen, s3_redirect), s3_predicted_fh) <- foldedGhGen_dup zip s3_redirect_dup zip s3_predicted_fh_dup) 694 foldedGhGen.register(s3_redirect, s3_predicted_fh, Some("s3_FGH"), 3) 695 for (((ghistPtrGen, s3_redirect), s3_predicted_ghist_ptr) <- ghistPtrGen_dup zip s3_redirect_dup zip s3_predicted_ghist_ptr_dup) 696 ghistPtrGen.register(s3_redirect, s3_predicted_ghist_ptr, Some("s3_GHPtr"), 3) 697 for (((lastBrNumOHGen, s3_redirect), s3_brPosOH) <- lastBrNumOHGen_dup zip s3_redirect_dup zip resp.s3.lastBrPosOH.map(_.asUInt)) 698 lastBrNumOHGen.register(s3_redirect, s3_brPosOH, Some("s3_BrNumOH"), 3) 699 for (((aheadFhObGen, s3_redirect), s3_ahead_fh_ob_src) <- aheadFhObGen_dup zip s3_redirect_dup zip s3_ahead_fh_ob_src_dup) 700 aheadFhObGen.register(s3_redirect, s3_ahead_fh_ob_src, Some("s3_AFHOB"), 3) 701 ghvBitWriteGens.zip(s3_ghv_wens).zipWithIndex.map{case ((b, w), i) => 702 b.register(w.reduce(_||_), s3_ghv_wdatas(i), Some(s"s3_new_bit_$i"), 3) 703 } 704 705 // Send signal tell Ftq override 706 val s2_ftq_idx = RegEnable(io.ftq_to_bpu.enq_ptr, s1_fire_dup(0)) 707 val s3_ftq_idx = RegEnable(s2_ftq_idx, s2_fire_dup(0)) 708 709 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) { 710 to_ftq_s1_valid := s1_fire && !s1_flush 711 } 712 io.bpu_to_ftq.resp.bits.s1.hasRedirect.map(_ := false.B) 713 io.bpu_to_ftq.resp.bits.s1.ftq_idx := DontCare 714 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) { 715 to_ftq_s2_valid := s2_fire && !s2_flush 716 } 717 io.bpu_to_ftq.resp.bits.s2.hasRedirect.zip(s2_redirect_dup).map {case (hr, r) => hr := r} 718 io.bpu_to_ftq.resp.bits.s2.ftq_idx := s2_ftq_idx 719 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) { 720 to_ftq_s3_valid := s3_fire && !s3_flush 721 } 722 io.bpu_to_ftq.resp.bits.s3.hasRedirect.zip(s3_redirect_dup).map {case (hr, r) => hr := r} 723 io.bpu_to_ftq.resp.bits.s3.ftq_idx := s3_ftq_idx 724 725 predictors.io.update.valid := RegNext(io.ftq_to_bpu.update.valid, init = false.B) 726 predictors.io.update.bits := RegEnable(io.ftq_to_bpu.update.bits, io.ftq_to_bpu.update.valid) 727 predictors.io.update.bits.ghist := RegEnable( 728 getHist(io.ftq_to_bpu.update.bits.spec_info.histPtr), io.ftq_to_bpu.update.valid) 729 730 val redirect_dup = do_redirect_dup.map(_.bits) 731 predictors.io.redirect := do_redirect_dup(0) 732 733 // Redirect logic 734 val shift_dup = redirect_dup.map(_.cfiUpdate.shift) 735 val addIntoHist_dup = redirect_dup.map(_.cfiUpdate.addIntoHist) 736 // TODO: remove these below 737 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))) 738 // TODO end 739 val afhob_dup = redirect_dup.map(_.cfiUpdate.afhob) 740 val lastBrNumOH_dup = redirect_dup.map(_.cfiUpdate.lastBrNumOH) 741 742 743 val isBr_dup = redirect_dup.map(_.cfiUpdate.pd.isBr) 744 val taken_dup = redirect_dup.map(_.cfiUpdate.taken) 745 val real_br_taken_mask_dup = 746 for (((shift, taken), addIntoHist) <- shift_dup zip taken_dup zip addIntoHist_dup) 747 yield (0 until numBr).map(i => shift === (i+1).U && taken && addIntoHist ) 748 749 val oldPtr_dup = redirect_dup.map(_.cfiUpdate.histPtr) 750 val updated_ptr_dup = oldPtr_dup.zip(shift_dup).map {case (oldPtr, shift) => oldPtr - shift} 751 def computeFoldedHist(hist: UInt, compLen: Int)(histLen: Int): UInt = { 752 if (histLen > 0) { 753 val nChunks = (histLen + compLen - 1) / compLen 754 val hist_chunks = (0 until nChunks) map { i => 755 hist(min((i + 1) * compLen, histLen) - 1, i * compLen) 756 } 757 ParallelXOR(hist_chunks) 758 } 759 else 0.U 760 } 761 762 val oldFh_dup = dup_seq(WireInit(0.U.asTypeOf(new AllFoldedHistories(foldedGHistInfos)))) 763 oldFh_dup.zip(oldPtr_dup).map { case (oldFh, oldPtr) => 764 foldedGHistInfos.foreach { case (histLen, compLen) => 765 oldFh.getHistWithInfo((histLen, compLen)).folded_hist := computeFoldedHist(getHist(oldPtr), compLen)(histLen) 766 } 767 } 768 769 val updated_fh_dup = 770 for (((((oldFh, oldPtr), taken), addIntoHist), shift) <- 771 oldFh_dup zip oldPtr_dup zip taken_dup zip addIntoHist_dup zip shift_dup) 772 yield VecInit((0 to numBr).map(i => oldFh.update(ghv, oldPtr, i, taken && addIntoHist)))(shift) 773 val thisBrNumOH_dup = shift_dup.map(shift => UIntToOH(shift, numBr+1)) 774 val thisAheadFhOb_dup = dup_wire(new AllAheadFoldedHistoryOldestBits(foldedGHistInfos)) 775 thisAheadFhOb_dup.zip(oldPtr_dup).map {case (afhob, oldPtr) => afhob.read(ghv, oldPtr)} 776 val redirect_ghv_wens = (0 until HistoryLength).map(n => 777 (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)) 778 val redirect_ghv_wdatas = (0 until HistoryLength).map(n => 779 Mux1H( 780 (0 until numBr).map(b => oldPtr_dup(0).value === (CGHPtr(false.B, n.U) + b.U).value && shouldShiftVec_dup(0)(b)), 781 real_br_taken_mask_dup(0) 782 ) 783 ) 784 785 if (EnableGHistDiff) { 786 val updated_ghist = WireInit(getHist(updated_ptr_dup(0)).asTypeOf(Vec(HistoryLength, Bool()))) 787 for (i <- 0 until numBr) { 788 when (shift_dup(0) >= (i+1).U) { 789 updated_ghist(i) := taken_dup(0) && addIntoHist_dup(0) && (i==0).B 790 } 791 } 792 when(do_redirect_dup(0).valid) { 793 s0_ghist := updated_ghist.asUInt 794 } 795 } 796 797 // Commit time history checker 798 if (EnableCommitGHistDiff) { 799 val commitGHist = RegInit(0.U.asTypeOf(Vec(HistoryLength, Bool()))) 800 val commitGHistPtr = RegInit(0.U.asTypeOf(new CGHPtr)) 801 def getCommitHist(ptr: CGHPtr): UInt = 802 (Cat(commitGHist.asUInt, commitGHist.asUInt) >> (ptr.value+1.U))(HistoryLength-1, 0) 803 804 val updateValid : Bool = io.ftq_to_bpu.update.valid 805 val branchValidMask : UInt = io.ftq_to_bpu.update.bits.ftb_entry.brValids.asUInt 806 val branchCommittedMask: Vec[Bool] = io.ftq_to_bpu.update.bits.br_committed 807 val misPredictMask : UInt = io.ftq_to_bpu.update.bits.mispred_mask.asUInt 808 val takenMask : UInt = 809 io.ftq_to_bpu.update.bits.br_taken_mask.asUInt | 810 io.ftq_to_bpu.update.bits.ftb_entry.always_taken.asUInt // Always taken branch is recorded in history 811 val takenIdx : UInt = (PriorityEncoder(takenMask) + 1.U((log2Ceil(numBr)+1).W)).asUInt 812 val misPredictIdx : UInt = (PriorityEncoder(misPredictMask) + 1.U((log2Ceil(numBr)+1).W)).asUInt 813 val shouldShiftMask: UInt = Mux(takenMask.orR, 814 LowerMask(takenIdx).asUInt, 815 ((1 << numBr) - 1).asUInt) & 816 Mux(misPredictMask.orR, 817 LowerMask(misPredictIdx).asUInt, 818 ((1 << numBr) - 1).asUInt) & 819 branchCommittedMask.asUInt 820 val updateShift : UInt = 821 Mux(updateValid && branchValidMask.orR, PopCount(branchValidMask & shouldShiftMask), 0.U) 822 823 // Maintain the commitGHist 824 for (i <- 0 until numBr) { 825 when(updateShift >= (i + 1).U) { 826 val ptr: CGHPtr = commitGHistPtr - i.asUInt 827 commitGHist(ptr.value) := takenMask(i) 828 } 829 } 830 when(updateValid) { 831 commitGHistPtr := commitGHistPtr - updateShift 832 } 833 834 // Calculate true history using Parallel XOR 835 // Do differential 836 TageTableInfos.map { 837 case (nRows, histLen, _) => { 838 val nRowsPerBr = nRows / numBr 839 val predictGHistPtr = io.ftq_to_bpu.update.bits.spec_info.histPtr 840 val commitTrueHist: UInt = computeFoldedHist(getCommitHist(commitGHistPtr), log2Ceil(nRowsPerBr))(histLen) 841 val predictFHist : UInt = computeFoldedHist(getHist(predictGHistPtr), log2Ceil(nRowsPerBr))(histLen) 842 XSWarn(updateValid && predictFHist =/= commitTrueHist, 843 p"predict time ghist: ${predictFHist} is different from commit time: ${commitTrueHist}\n") 844 } 845 } 846 } 847 848 849 // val updatedGh = oldGh.update(shift, taken && addIntoHist) 850 for ((npcGen, do_redirect) <- npcGen_dup zip do_redirect_dup) 851 npcGen.register(do_redirect.valid, do_redirect.bits.cfiUpdate.target, Some("redirect_target"), 2) 852 for (((foldedGhGen, do_redirect), updated_fh) <- foldedGhGen_dup zip do_redirect_dup zip updated_fh_dup) 853 foldedGhGen.register(do_redirect.valid, updated_fh, Some("redirect_FGHT"), 2) 854 for (((ghistPtrGen, do_redirect), updated_ptr) <- ghistPtrGen_dup zip do_redirect_dup zip updated_ptr_dup) 855 ghistPtrGen.register(do_redirect.valid, updated_ptr, Some("redirect_GHPtr"), 2) 856 for (((lastBrNumOHGen, do_redirect), thisBrNumOH) <- lastBrNumOHGen_dup zip do_redirect_dup zip thisBrNumOH_dup) 857 lastBrNumOHGen.register(do_redirect.valid, thisBrNumOH, Some("redirect_BrNumOH"), 2) 858 for (((aheadFhObGen, do_redirect), thisAheadFhOb) <- aheadFhObGen_dup zip do_redirect_dup zip thisAheadFhOb_dup) 859 aheadFhObGen.register(do_redirect.valid, thisAheadFhOb, Some("redirect_AFHOB"), 2) 860 ghvBitWriteGens.zip(redirect_ghv_wens).zipWithIndex.map{case ((b, w), i) => 861 b.register(w.reduce(_||_), redirect_ghv_wdatas(i), Some(s"redirect_new_bit_$i"), 2) 862 } 863 // no need to assign s0_last_pred 864 865 // val need_reset = RegNext(reset.asBool) && !reset.asBool 866 867 // Reset 868 // npcGen.register(need_reset, resetVector.U, Some("reset_pc"), 1) 869 // foldedGhGen.register(need_reset, 0.U.asTypeOf(s0_folded_gh), Some("reset_FGH"), 1) 870 // ghistPtrGen.register(need_reset, 0.U.asTypeOf(new CGHPtr), Some("reset_GHPtr"), 1) 871 872 s0_pc_dup.zip(npcGen_dup).map {case (s0_pc, npcGen) => s0_pc := npcGen()} 873 s0_folded_gh_dup.zip(foldedGhGen_dup).map {case (s0_folded_gh, foldedGhGen) => s0_folded_gh := foldedGhGen()} 874 s0_ghist_ptr_dup.zip(ghistPtrGen_dup).map {case (s0_ghist_ptr, ghistPtrGen) => s0_ghist_ptr := ghistPtrGen()} 875 s0_ahead_fh_oldest_bits_dup.zip(aheadFhObGen_dup).map {case (s0_ahead_fh_oldest_bits, aheadFhObGen) => 876 s0_ahead_fh_oldest_bits := aheadFhObGen()} 877 s0_last_br_num_oh_dup.zip(lastBrNumOHGen_dup).map {case (s0_last_br_num_oh, lastBrNumOHGen) => 878 s0_last_br_num_oh := lastBrNumOHGen()} 879 (ghv_write_datas zip ghvBitWriteGens).map{case (wd, d) => wd := d()} 880 for (i <- 0 until HistoryLength) { 881 ghv_wens(i) := Seq(s1_ghv_wens, s2_ghv_wens, s3_ghv_wens, redirect_ghv_wens).map(_(i).reduce(_||_)).reduce(_||_) 882 when (ghv_wens(i)) { 883 ghv(i) := ghv_write_datas(i) 884 } 885 } 886 887 // TODO: signals for memVio and other Redirects 888 controlRedirectBubble := do_redirect_dup(0).valid && do_redirect_dup(0).bits.ControlRedirectBubble 889 ControlBTBMissBubble := do_redirect_dup(0).bits.ControlBTBMissBubble 890 TAGEMissBubble := do_redirect_dup(0).bits.TAGEMissBubble 891 SCMissBubble := do_redirect_dup(0).bits.SCMissBubble 892 ITTAGEMissBubble := do_redirect_dup(0).bits.ITTAGEMissBubble 893 RASMissBubble := do_redirect_dup(0).bits.RASMissBubble 894 895 memVioRedirectBubble := do_redirect_dup(0).valid && do_redirect_dup(0).bits.MemVioRedirectBubble 896 otherRedirectBubble := do_redirect_dup(0).valid && do_redirect_dup(0).bits.OtherRedirectBubble 897 btbMissBubble := do_redirect_dup(0).valid && do_redirect_dup(0).bits.BTBMissBubble 898 overrideBubble(0) := s2_redirect_dup(0) 899 overrideBubble(1) := s3_redirect_dup(0) 900 ftqUpdateBubble(0) := !s1_components_ready_dup(0) 901 ftqUpdateBubble(1) := !s2_components_ready_dup(0) 902 ftqUpdateBubble(2) := !s3_components_ready_dup(0) 903 ftqFullStall := !io.bpu_to_ftq.resp.ready 904 io.bpu_to_ftq.resp.bits.topdown_info := topdown_stages(numOfStage - 1) 905 906 // topdown handling logic here 907 when (controlRedirectBubble) { 908 /* 909 for (i <- 0 until numOfStage) 910 topdown_stages(i).reasons(TopDownCounters.ControlRedirectBubble.id) := true.B 911 io.bpu_to_ftq.resp.bits.topdown_info.reasons(TopDownCounters.ControlRedirectBubble.id) := true.B 912 */ 913 when (ControlBTBMissBubble) { 914 for (i <- 0 until numOfStage) 915 topdown_stages(i).reasons(TopDownCounters.BTBMissBubble.id) := true.B 916 io.bpu_to_ftq.resp.bits.topdown_info.reasons(TopDownCounters.BTBMissBubble.id) := true.B 917 } .elsewhen (TAGEMissBubble) { 918 for (i <- 0 until numOfStage) 919 topdown_stages(i).reasons(TopDownCounters.TAGEMissBubble.id) := true.B 920 io.bpu_to_ftq.resp.bits.topdown_info.reasons(TopDownCounters.TAGEMissBubble.id) := true.B 921 } .elsewhen (SCMissBubble) { 922 for (i <- 0 until numOfStage) 923 topdown_stages(i).reasons(TopDownCounters.SCMissBubble.id) := true.B 924 io.bpu_to_ftq.resp.bits.topdown_info.reasons(TopDownCounters.SCMissBubble.id) := true.B 925 } .elsewhen (ITTAGEMissBubble) { 926 for (i <- 0 until numOfStage) 927 topdown_stages(i).reasons(TopDownCounters.ITTAGEMissBubble.id) := true.B 928 io.bpu_to_ftq.resp.bits.topdown_info.reasons(TopDownCounters.ITTAGEMissBubble.id) := true.B 929 } .elsewhen (RASMissBubble) { 930 for (i <- 0 until numOfStage) 931 topdown_stages(i).reasons(TopDownCounters.RASMissBubble.id) := true.B 932 io.bpu_to_ftq.resp.bits.topdown_info.reasons(TopDownCounters.RASMissBubble.id) := true.B 933 } 934 } 935 when (memVioRedirectBubble) { 936 for (i <- 0 until numOfStage) 937 topdown_stages(i).reasons(TopDownCounters.MemVioRedirectBubble.id) := true.B 938 io.bpu_to_ftq.resp.bits.topdown_info.reasons(TopDownCounters.MemVioRedirectBubble.id) := true.B 939 } 940 when (otherRedirectBubble) { 941 for (i <- 0 until numOfStage) 942 topdown_stages(i).reasons(TopDownCounters.OtherRedirectBubble.id) := true.B 943 io.bpu_to_ftq.resp.bits.topdown_info.reasons(TopDownCounters.OtherRedirectBubble.id) := true.B 944 } 945 when (btbMissBubble) { 946 for (i <- 0 until numOfStage) 947 topdown_stages(i).reasons(TopDownCounters.BTBMissBubble.id) := true.B 948 io.bpu_to_ftq.resp.bits.topdown_info.reasons(TopDownCounters.BTBMissBubble.id) := true.B 949 } 950 951 for (i <- 0 until numOfStage) { 952 if (i < numOfStage - overrideStage) { 953 when (overrideBubble(i)) { 954 for (j <- 0 to i) 955 topdown_stages(j).reasons(TopDownCounters.OverrideBubble.id) := true.B 956 } 957 } 958 if (i < numOfStage - ftqUpdateStage) { 959 when (ftqUpdateBubble(i)) { 960 topdown_stages(i).reasons(TopDownCounters.FtqUpdateBubble.id) := true.B 961 } 962 } 963 } 964 when (ftqFullStall) { 965 topdown_stages(0).reasons(TopDownCounters.FtqFullStall.id) := true.B 966 } 967 968 XSError(isBefore(redirect_dup(0).cfiUpdate.histPtr, s3_ghist_ptr_dup(0)) && do_redirect_dup(0).valid, 969 p"s3_ghist_ptr ${s3_ghist_ptr_dup(0)} exceeds redirect histPtr ${redirect_dup(0).cfiUpdate.histPtr}\n") 970 XSError(isBefore(redirect_dup(0).cfiUpdate.histPtr, s2_ghist_ptr_dup(0)) && do_redirect_dup(0).valid, 971 p"s2_ghist_ptr ${s2_ghist_ptr_dup(0)} exceeds redirect histPtr ${redirect_dup(0).cfiUpdate.histPtr}\n") 972 XSError(isBefore(redirect_dup(0).cfiUpdate.histPtr, s1_ghist_ptr_dup(0)) && do_redirect_dup(0).valid, 973 p"s1_ghist_ptr ${s1_ghist_ptr_dup(0)} exceeds redirect histPtr ${redirect_dup(0).cfiUpdate.histPtr}\n") 974 975 XSDebug(RegNext(reset.asBool) && !reset.asBool, "Reseting...\n") 976 XSDebug(io.ftq_to_bpu.update.valid, p"Update from ftq\n") 977 XSDebug(io.ftq_to_bpu.redirect.valid, p"Redirect from ftq\n") 978 979 XSDebug("[BP0] fire=%d pc=%x\n", s0_fire_dup(0), s0_pc_dup(0)) 980 XSDebug("[BP1] v=%d r=%d cr=%d fire=%d flush=%d pc=%x\n", 981 s1_valid_dup(0), s1_ready_dup(0), s1_components_ready_dup(0), s1_fire_dup(0), s1_flush_dup(0), s1_pc) 982 XSDebug("[BP2] v=%d r=%d cr=%d fire=%d redirect=%d flush=%d pc=%x\n", 983 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) 984 XSDebug("[BP3] v=%d r=%d cr=%d fire=%d redirect=%d flush=%d pc=%x\n", 985 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) 986 XSDebug("[FTQ] ready=%d\n", io.bpu_to_ftq.resp.ready) 987 XSDebug("resp.s1.target=%x\n", resp.s1.getTarget(0)) 988 XSDebug("resp.s2.target=%x\n", resp.s2.getTarget(0)) 989 // XSDebug("s0_ghist: %b\n", s0_ghist.predHist) 990 // XSDebug("s1_ghist: %b\n", s1_ghist.predHist) 991 // XSDebug("s2_ghist: %b\n", s2_ghist.predHist) 992 // XSDebug("s2_predicted_ghist: %b\n", s2_predicted_ghist.predHist) 993 XSDebug(p"s0_ghist_ptr: ${s0_ghist_ptr_dup(0)}\n") 994 XSDebug(p"s1_ghist_ptr: ${s1_ghist_ptr_dup(0)}\n") 995 XSDebug(p"s2_ghist_ptr: ${s2_ghist_ptr_dup(0)}\n") 996 XSDebug(p"s3_ghist_ptr: ${s3_ghist_ptr_dup(0)}\n") 997 998 io.ftq_to_bpu.update.bits.display(io.ftq_to_bpu.update.valid) 999 io.ftq_to_bpu.redirect.bits.display(io.ftq_to_bpu.redirect.valid) 1000 1001 1002 XSPerfAccumulate("s2_redirect", s2_redirect_dup(0)) 1003 XSPerfAccumulate("s3_redirect", s3_redirect_dup(0)) 1004 XSPerfAccumulate("s1_not_valid", !s1_valid_dup(0)) 1005 1006 val perfEvents = predictors.asInstanceOf[Composer].getPerfEvents 1007 generatePerfEvent() 1008} 1009