1package xiangshan.frontend 2 3import chisel3._ 4import chisel3.util._ 5import utils._ 6import xiangshan._ 7import xiangshan.backend.ALUOpType 8import xiangshan.backend.JumpOpType 9 10trait HasBPUParameter extends HasXSParameter { 11 val BPUDebug = false 12} 13 14class TableAddr(val idxBits: Int, val banks: Int) extends XSBundle { 15 def tagBits = VAddrBits - idxBits - 1 16 17 val tag = UInt(tagBits.W) 18 val idx = UInt(idxBits.W) 19 val offset = UInt(1.W) 20 21 def fromUInt(x: UInt) = x.asTypeOf(UInt(VAddrBits.W)).asTypeOf(this) 22 def getTag(x: UInt) = fromUInt(x).tag 23 def getIdx(x: UInt) = fromUInt(x).idx 24 def getBank(x: UInt) = getIdx(x)(log2Up(banks) - 1, 0) 25 def getBankIdx(x: UInt) = getIdx(x)(idxBits - 1, log2Up(banks)) 26} 27 28class PredictorResponse extends XSBundle { 29 class UbtbResp extends XSBundle { 30 // the valid bits indicates whether a target is hit 31 val targets = Vec(PredictWidth, UInt(VAddrBits.W)) 32 val hits = Vec(PredictWidth, Bool()) 33 val takens = Vec(PredictWidth, Bool()) 34 val notTakens = Vec(PredictWidth, Bool()) 35 val is_RVC = Vec(PredictWidth, Bool()) 36 } 37 class BtbResp extends XSBundle { 38 // the valid bits indicates whether a target is hit 39 val targets = Vec(PredictWidth, UInt(VAddrBits.W)) 40 val hits = Vec(PredictWidth, Bool()) 41 val types = Vec(PredictWidth, UInt(2.W)) 42 val isRVC = Vec(PredictWidth, Bool()) 43 } 44 class BimResp extends XSBundle { 45 val ctrs = Vec(PredictWidth, UInt(2.W)) 46 } 47 class TageResp extends XSBundle { 48 // the valid bits indicates whether a prediction is hit 49 val takens = Vec(PredictWidth, Bool()) 50 val hits = Vec(PredictWidth, Bool()) 51 } 52 53 val ubtb = new UbtbResp 54 val btb = new BtbResp 55 val bim = new BimResp 56 val tage = new TageResp 57} 58 59abstract class BasePredictor extends XSModule with HasBPUParameter{ 60 val metaLen = 0 61 62 // An implementation MUST extend the IO bundle with a response 63 // and the special input from other predictors, as well as 64 // the metas to store in BRQ 65 abstract class Resp extends XSBundle {} 66 abstract class FromOthers extends XSBundle {} 67 abstract class Meta extends XSBundle {} 68 69 class DefaultBasePredictorIO extends XSBundle { 70 val flush = Input(Bool()) 71 val pc = Flipped(ValidIO(UInt(VAddrBits.W))) 72 val hist = Input(UInt(HistoryLength.W)) 73 val inMask = Input(UInt(PredictWidth.W)) 74 val update = Flipped(ValidIO(new BranchUpdateInfoWithHist)) 75 } 76 77 val io = new DefaultBasePredictorIO 78 79 val debug = false 80 81 // circular shifting 82 def circularShiftLeft(source: UInt, len: Int, shamt: UInt): UInt = { 83 val res = Wire(UInt(len.W)) 84 val higher = source << shamt 85 val lower = source >> (len.U - shamt) 86 res := higher | lower 87 res 88 } 89 90 def circularShiftRight(source: UInt, len: Int, shamt: UInt): UInt = { 91 val res = Wire(UInt(len.W)) 92 val higher = source << (len.U - shamt) 93 val lower = source >> shamt 94 res := higher | lower 95 res 96 } 97} 98 99class BPUStageIO extends XSBundle { 100 val pc = UInt(VAddrBits.W) 101 val mask = UInt(PredictWidth.W) 102 val resp = new PredictorResponse 103 val target = UInt(VAddrBits.W) 104 val brInfo = Vec(PredictWidth, new BranchInfo) 105} 106 107 108abstract class BPUStage extends XSModule with HasBPUParameter{ 109 class DefaultIO extends XSBundle { 110 val flush = Input(Bool()) 111 val in = Flipped(Decoupled(new BPUStageIO)) 112 val pred = Decoupled(new BranchPrediction) 113 val out = Decoupled(new BPUStageIO) 114 val predecode = Flipped(ValidIO(new Predecode)) 115 } 116 val io = IO(new DefaultIO) 117 118 val predValid = RegInit(false.B) 119 120 io.in.ready := !predValid || io.out.fire() && io.pred.fire() || io.flush 121 122 def npc(pc: UInt, instCount: UInt) = pc + (instCount << 1.U) 123 124 val inFire = io.in.fire() 125 val inLatch = RegEnable(io.in.bits, inFire) 126 127 val outFire = io.out.fire() 128 129 // Each stage has its own logic to decide 130 // takens, notTakens and target 131 132 val takens = Wire(Vec(PredictWidth, Bool())) 133 val notTakens = Wire(Vec(PredictWidth, Bool())) 134 val jmpIdx = PriorityEncoder(takens) 135 val hasNTBr = (0 until PredictWidth).map(i => i.U <= jmpIdx && notTakens(i)).reduce(_||_) 136 val taken = takens.reduce(_||_) 137 // get the last valid inst 138 // val lastValidPos = MuxCase(0.U, (PredictWidth-1 to 0).map(i => (inLatch.mask(i), i.U))) 139 val lastValidPos = PriorityMux(Reverse(inLatch.mask), (PredictWidth-1 to 0 by -1).map(i => i.U)) 140 val lastHit = Wire(Bool()) 141 val lastIsRVC = Wire(Bool()) 142 // val lastValidPos = WireInit(0.U(log2Up(PredictWidth).W)) 143 // for (i <- 0 until PredictWidth) { 144 // when (inLatch.mask(i)) { lastValidPos := i.U } 145 // } 146 val targetSrc = Wire(Vec(PredictWidth, UInt(VAddrBits.W))) 147 val target = Mux(taken, targetSrc(jmpIdx), npc(inLatch.pc, PopCount(inLatch.mask))) 148 149 io.pred.bits <> DontCare 150 io.pred.bits.redirect := target =/= inLatch.target 151 io.pred.bits.taken := taken 152 io.pred.bits.jmpIdx := jmpIdx 153 io.pred.bits.hasNotTakenBrs := hasNTBr 154 io.pred.bits.target := target 155 io.pred.bits.saveHalfRVI := ((lastValidPos === jmpIdx && taken) || !taken ) && !lastIsRVC && lastHit 156 157 io.out.bits <> DontCare 158 io.out.bits.pc := inLatch.pc 159 io.out.bits.mask := inLatch.mask 160 io.out.bits.target := target 161 io.out.bits.resp <> inLatch.resp 162 io.out.bits.brInfo := inLatch.brInfo 163 164 // Default logic 165 // pred.ready not taken into consideration 166 // could be broken 167 when (io.flush) { predValid := false.B } 168 .elsewhen (inFire) { predValid := true.B } 169 .elsewhen (outFire) { predValid := false.B } 170 .otherwise { predValid := predValid } 171 172 io.out.valid := predValid && !io.flush 173 io.pred.valid := predValid && !io.flush 174 175 if (BPUDebug) { 176 XSDebug(io.in.fire(), "in:(%d %d) pc=%x, mask=%b, target=%x\n", 177 io.in.valid, io.in.ready, io.in.bits.pc, io.in.bits.mask, io.in.bits.target) 178 XSDebug(io.out.fire(), "out:(%d %d) pc=%x, mask=%b, target=%x\n", 179 io.out.valid, io.out.ready, io.out.bits.pc, io.out.bits.mask, io.out.bits.target) 180 XSDebug("flush=%d\n", io.flush) 181 XSDebug("taken=%d, takens=%b, notTakens=%b, jmpIdx=%d, hasNTBr=%d, lastValidPos=%d, target=%x\n", 182 taken, takens.asUInt, notTakens.asUInt, jmpIdx, hasNTBr, lastValidPos, target) 183 val p = io.pred.bits 184 XSDebug(io.pred.fire(), "outPred: redirect=%d, taken=%d, jmpIdx=%d, hasNTBrs=%d, target=%x, saveHalfRVI=%d\n", 185 p.redirect, p.taken, p.jmpIdx, p.hasNotTakenBrs, p.target, p.saveHalfRVI) 186 XSDebug(io.pred.fire() && p.taken, "outPredTaken: fetchPC:%x, jmpPC:%x\n", 187 inLatch.pc, inLatch.pc + (jmpIdx << 1.U)) 188 XSDebug(io.pred.fire() && p.redirect, "outPred: previous target:%x redirected to %x \n", 189 inLatch.target, p.target) 190 XSDebug(io.pred.fire(), "outPred targetSrc: ") 191 for (i <- 0 until PredictWidth) { 192 XSDebug(false, io.pred.fire(), "(%d):%x ", i.U, targetSrc(i)) 193 } 194 XSDebug(false, io.pred.fire(), "\n") 195 } 196} 197 198class BPUStage1 extends BPUStage { 199 200 // 'overrides' default logic 201 // when flush, the prediction should also starts 202 when (inFire) { predValid := true.B } 203 .elsewhen (io.flush) { predValid := false.B } 204 .elsewhen (outFire) { predValid := false.B } 205 .otherwise { predValid := predValid } 206 // io.out.valid := predValid 207 208 // ubtb is accessed with inLatch pc in s1, 209 // so we use io.in instead of inLatch 210 val ubtbResp = io.in.bits.resp.ubtb 211 // the read operation is already masked, so we do not need to mask here 212 takens := VecInit((0 until PredictWidth).map(i => ubtbResp.hits(i) && ubtbResp.takens(i))) 213 notTakens := VecInit((0 until PredictWidth).map(i => ubtbResp.hits(i) && ubtbResp.notTakens(i))) 214 targetSrc := ubtbResp.targets 215 216 lastIsRVC := ubtbResp.is_RVC(lastValidPos) 217 lastHit := ubtbResp.hits(lastValidPos) 218 219 // resp and brInfo are from the components, 220 // so it does not need to be latched 221 io.out.bits.resp <> io.in.bits.resp 222 io.out.bits.brInfo := io.in.bits.brInfo 223 224 if (BPUDebug) { 225 io.out.bits.brInfo.map(_.debug_ubtb_cycle := GTimer()) 226 XSDebug(io.pred.fire(), "outPred using ubtb resp: hits:%b, takens:%b, notTakens:%b, isRVC:%b\n", 227 ubtbResp.hits.asUInt, ubtbResp.takens.asUInt, ubtbResp.notTakens.asUInt, ubtbResp.is_RVC.asUInt) 228 } 229} 230 231class BPUStage2 extends BPUStage { 232 233 // Use latched response from s1 234 val btbResp = inLatch.resp.btb 235 val bimResp = inLatch.resp.bim 236 takens := VecInit((0 until PredictWidth).map(i => btbResp.hits(i) && (btbResp.types(i) === BTBtype.B && bimResp.ctrs(i)(1) || btbResp.types(i) =/= BTBtype.B))) 237 notTakens := VecInit((0 until PredictWidth).map(i => btbResp.hits(i) && btbResp.types(i) === BTBtype.B && !bimResp.ctrs(i)(1))) 238 targetSrc := btbResp.targets 239 240 lastIsRVC := btbResp.isRVC(lastValidPos) 241 lastHit := btbResp.hits(lastValidPos) 242 243 244 if (BPUDebug) { 245 io.out.bits.brInfo.map(_.debug_btb_cycle := GTimer()) 246 XSDebug(io.pred.fire(), "outPred using btb&bim resp: hits:%b, ctrTakens:%b\n", 247 btbResp.hits.asUInt, VecInit(bimResp.ctrs.map(_(1))).asUInt) 248 } 249} 250 251class BPUStage3 extends BPUStage { 252 253 io.out.valid := predValid && io.predecode.valid && !io.flush 254 255 // TAGE has its own pipelines and the 256 // response comes directly from s3, 257 // so we do not use those from inLatch 258 val tageResp = io.in.bits.resp.tage 259 val tageTakens = tageResp.takens 260 261 val pdMask = io.predecode.bits.mask 262 val pds = io.predecode.bits.pd 263 264 val btbHits = inLatch.resp.btb.hits.asUInt 265 val bimTakens = VecInit(inLatch.resp.bim.ctrs.map(_(1))) 266 267 val brs = pdMask & Reverse(Cat(pds.map(_.isBr))) 268 val jals = pdMask & Reverse(Cat(pds.map(_.isJal))) 269 val jalrs = pdMask & Reverse(Cat(pds.map(_.isJalr))) 270 // val calls = pdMask & Reverse(Cat(pds.map(_.isCall))) 271 // val rets = pdMask & Reverse(Cat(pds.map(_.isRet))) 272 273 // val callIdx = PriorityEncoder(calls) 274 // val retIdx = PriorityEncoder(rets) 275 276 val brTakens = 277 if (EnableBPD) { 278 brs & Reverse(Cat((0 until PredictWidth).map(i => tageTakens(i)))) 279 } else { 280 brs & Reverse(Cat((0 until PredictWidth).map(i => bimTakens(i)))) 281 } 282 283 // predict taken only if btb has a target, jal targets will be provided by IFU 284 takens := VecInit((0 until PredictWidth).map(i => (brTakens(i) || jalrs(i)) && btbHits(i) || jals(i))) 285 // Whether should we count in branches that are not recorded in btb? 286 // PS: Currently counted in. Whenever tage does not provide a valid 287 // taken prediction, the branch is counted as a not taken branch 288 notTakens := (if (EnableBPD) { VecInit((0 until PredictWidth).map(i => brs(i) && !tageTakens(i)))} 289 else { VecInit((0 until PredictWidth).map(i => brs(i) && !bimTakens(i)))}) 290 targetSrc := inLatch.resp.btb.targets 291 292 lastIsRVC := pds(lastValidPos).isRVC 293 when (lastValidPos === 1.U) { 294 lastHit := pdMask(1) | 295 !pdMask(0) & !pdMask(1) | 296 pdMask(0) & !pdMask(1) & (pds(0).isRVC | !io.predecode.bits.isFetchpcEqualFirstpc) 297 }.elsewhen (lastValidPos > 0.U) { 298 lastHit := pdMask(lastValidPos) | 299 !pdMask(lastValidPos - 1.U) & !pdMask(lastValidPos) | 300 pdMask(lastValidPos - 1.U) & !pdMask(lastValidPos) & pds(lastValidPos - 1.U).isRVC 301 }.otherwise { 302 lastHit := pdMask(0) | !pdMask(0) & !pds(0).isRVC 303 } 304 305 306 // Wrap tage resp and tage meta in 307 // This is ugly 308 io.out.bits.resp.tage <> io.in.bits.resp.tage 309 for (i <- 0 until PredictWidth) { 310 io.out.bits.brInfo(i).tageMeta := io.in.bits.brInfo(i).tageMeta 311 } 312 313 if (BPUDebug) { 314 io.out.bits.brInfo.map(_.debug_tage_cycle := GTimer()) 315 XSDebug(io.predecode.valid, "predecode: pc:%x, mask:%b\n", inLatch.pc, io.predecode.bits.mask) 316 for (i <- 0 until PredictWidth) { 317 val p = io.predecode.bits.pd(i) 318 XSDebug(io.predecode.valid && io.predecode.bits.mask(i), "predecode(%d): brType:%d, br:%d, jal:%d, jalr:%d, call:%d, ret:%d, RVC:%d, excType:%d\n", 319 i.U, p.brType, p.isBr, p.isJal, p.isJalr, p.isCall, p.isRet, p.isRVC, p.excType) 320 } 321 } 322} 323 324trait BranchPredictorComponents extends HasXSParameter { 325 val ubtb = Module(new MicroBTB) 326 val btb = Module(new BTB) 327 val bim = Module(new BIM) 328 val tage = (if(EnableBPD) { Module(new Tage) } 329 else { Module(new FakeTage) }) 330 val preds = Seq(ubtb, btb, bim, tage) 331 preds.map(_.io := DontCare) 332} 333 334class BPUReq extends XSBundle { 335 val pc = UInt(VAddrBits.W) 336 val hist = UInt(HistoryLength.W) 337 val inMask = UInt(PredictWidth.W) 338} 339 340class BranchUpdateInfoWithHist extends XSBundle { 341 val ui = new BranchUpdateInfo 342 val hist = UInt(HistoryLength.W) 343} 344 345object BranchUpdateInfoWithHist { 346 def apply (brInfo: BranchUpdateInfo, hist: UInt) = { 347 val b = Wire(new BranchUpdateInfoWithHist) 348 b.ui <> brInfo 349 b.hist := hist 350 b 351 } 352} 353 354abstract class BaseBPU extends XSModule with BranchPredictorComponents with HasBPUParameter{ 355 val io = IO(new Bundle() { 356 // from backend 357 val inOrderBrInfo = Flipped(ValidIO(new BranchUpdateInfoWithHist)) 358 val outOfOrderBrInfo = Flipped(ValidIO(new BranchUpdateInfoWithHist)) 359 // from ifu, frontend redirect 360 val flush = Input(Vec(3, Bool())) 361 // from if1 362 val in = Flipped(ValidIO(new BPUReq)) 363 // to if2/if3/if4 364 val out = Vec(3, Decoupled(new BranchPrediction)) 365 // from if4 366 val predecode = Flipped(ValidIO(new Predecode)) 367 // to if4, some bpu info used for updating 368 val branchInfo = Decoupled(Vec(PredictWidth, new BranchInfo)) 369 }) 370 371 def npc(pc: UInt, instCount: UInt) = pc + (instCount << 1.U) 372 373 preds.map(_.io.update <> io.outOfOrderBrInfo) 374 tage.io.update <> io.inOrderBrInfo 375 376 val s1 = Module(new BPUStage1) 377 val s2 = Module(new BPUStage2) 378 val s3 = Module(new BPUStage3) 379 380 s1.io.flush := io.flush(0) 381 s2.io.flush := io.flush(1) 382 s3.io.flush := io.flush(2) 383 384 s1.io.in <> DontCare 385 s2.io.in <> s1.io.out 386 s3.io.in <> s2.io.out 387 388 io.out(0) <> s1.io.pred 389 io.out(1) <> s2.io.pred 390 io.out(2) <> s3.io.pred 391 392 s1.io.predecode <> DontCare 393 s2.io.predecode <> DontCare 394 s3.io.predecode <> io.predecode 395 396 io.branchInfo.valid := s3.io.out.valid 397 io.branchInfo.bits := s3.io.out.bits.brInfo 398 s3.io.out.ready := io.branchInfo.ready 399 400 if (BPUDebug) { 401 XSDebug(io.branchInfo.fire(), "branchInfo sent!\n") 402 for (i <- 0 until PredictWidth) { 403 val b = io.branchInfo.bits(i) 404 XSDebug(io.branchInfo.fire(), "brInfo(%d): ubtbWrWay:%d, ubtbHit:%d, btbWrWay:%d, btbHitJal:%d, bimCtr:%d, fetchIdx:%d\n", 405 i.U, b.ubtbWriteWay, b.ubtbHits, b.btbWriteWay, b.btbHitJal, b.bimCtr, b.fetchIdx) 406 val t = b.tageMeta 407 XSDebug(io.branchInfo.fire(), " tageMeta: pvder(%d):%d, altDiffers:%d, pvderU:%d, pvderCtr:%d, allocate(%d):%d\n", 408 t.provider.valid, t.provider.bits, t.altDiffers, t.providerU, t.providerCtr, t.allocate.valid, t.allocate.bits) 409 } 410 } 411 val debug_verbose = false 412} 413 414 415class FakeBPU extends BaseBPU { 416 io.out.foreach(i => { 417 // Provide not takens 418 i.valid := true.B 419 i.bits <> DontCare 420 i.bits.redirect := false.B 421 }) 422 io.branchInfo <> DontCare 423} 424 425class BPU extends BaseBPU { 426 427 //**********************Stage 1****************************// 428 val s1_fire = s1.io.in.fire() 429 val s1_resp_in = Wire(new PredictorResponse) 430 val s1_brInfo_in = Wire(Vec(PredictWidth, new BranchInfo)) 431 432 s1_resp_in.tage := DontCare 433 s1_brInfo_in := DontCare 434 (0 until PredictWidth).foreach(i => s1_brInfo_in(i).fetchIdx := i.U) 435 436 val s1_inLatch = RegEnable(io.in, s1_fire) 437 ubtb.io.flush := io.flush(0) // TODO: fix this 438 ubtb.io.pc.valid := s1_inLatch.valid 439 ubtb.io.pc.bits := s1_inLatch.bits.pc 440 ubtb.io.inMask := s1_inLatch.bits.inMask 441 442 443 444 // Wrap ubtb response into resp_in and brInfo_in 445 s1_resp_in.ubtb <> ubtb.io.out 446 for (i <- 0 until PredictWidth) { 447 s1_brInfo_in(i).ubtbWriteWay := ubtb.io.uBTBBranchInfo.writeWay(i) 448 s1_brInfo_in(i).ubtbHits := ubtb.io.uBTBBranchInfo.hits(i) 449 } 450 451 btb.io.flush := io.flush(0) // TODO: fix this 452 btb.io.pc.valid := io.in.valid 453 btb.io.pc.bits := io.in.bits.pc 454 btb.io.inMask := io.in.bits.inMask 455 456 457 458 // Wrap btb response into resp_in and brInfo_in 459 s1_resp_in.btb <> btb.io.resp 460 for (i <- 0 until PredictWidth) { 461 s1_brInfo_in(i).btbWriteWay := btb.io.meta.writeWay(i) 462 s1_brInfo_in(i).btbHitJal := btb.io.meta.hitJal(i) 463 } 464 465 bim.io.flush := io.flush(0) // TODO: fix this 466 bim.io.pc.valid := io.in.valid 467 bim.io.pc.bits := io.in.bits.pc 468 bim.io.inMask := io.in.bits.inMask 469 470 471 // Wrap bim response into resp_in and brInfo_in 472 s1_resp_in.bim <> bim.io.resp 473 for (i <- 0 until PredictWidth) { 474 s1_brInfo_in(i).bimCtr := bim.io.meta.ctrs(i) 475 } 476 477 478 s1.io.in.valid := io.in.valid 479 s1.io.in.bits.pc := io.in.bits.pc 480 s1.io.in.bits.mask := io.in.bits.inMask 481 s1.io.in.bits.target := npc(io.in.bits.pc, PopCount(io.in.bits.inMask)) // Deault target npc 482 s1.io.in.bits.resp <> s1_resp_in 483 s1.io.in.bits.brInfo <> s1_brInfo_in 484 485 val s1_hist = RegEnable(io.in.bits.hist, enable=io.in.valid) 486 487 //**********************Stage 2****************************// 488 tage.io.flush := io.flush(1) // TODO: fix this 489 tage.io.pc.valid := s1.io.out.fire() 490 tage.io.pc.bits := s1.io.out.bits.pc // PC from s1 491 tage.io.hist := s1_hist // The inst is from s1 492 tage.io.inMask := s1.io.out.bits.mask 493 tage.io.s3Fire := s3.io.in.fire() // Tell tage to march 1 stage 494 tage.io.bim <> s1.io.out.bits.resp.bim // Use bim results from s1 495 496 //**********************Stage 3****************************// 497 // Wrap tage response and meta into s3.io.in.bits 498 // This is ugly 499 500 s3.io.in.bits.resp.tage <> tage.io.resp 501 for (i <- 0 until PredictWidth) { 502 s3.io.in.bits.brInfo(i).tageMeta := tage.io.meta(i) 503 } 504 505 if (BPUDebug) { 506 if (debug_verbose) { 507 val uo = ubtb.io.out 508 XSDebug("debug: ubtb hits:%b, takens:%b, notTakens:%b\n", uo.hits.asUInt, uo.takens.asUInt, uo.notTakens.asUInt) 509 val bio = bim.io.resp 510 XSDebug("debug: bim takens:%b\n", VecInit(bio.ctrs.map(_(1))).asUInt) 511 val bo = btb.io.resp 512 XSDebug("debug: btb hits:%b\n", bo.hits.asUInt) 513 } 514 } 515 516} 517