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