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 49 val ubtb = new UbtbResp 50 val btb = new BtbResp 51 val bim = new BimResp 52 val tage = new TageResp 53} 54 55abstract class BasePredictor extends XSModule { 56 val metaLen = 0 57 58 // An implementation MUST extend the IO bundle with a response 59 // and the special input from other predictors, as well as 60 // the metas to store in BRQ 61 abstract class Resp extends XSBundle {} 62 abstract class FromOthers extends XSBundle {} 63 abstract class Meta extends XSBundle {} 64 65 class DefaultBasePredictorIO extends XSBundle { 66 val flush = Input(Bool()) 67 val pc = Flipped(ValidIO(UInt(VAddrBits.W))) 68 val hist = Input(UInt(HistoryLength.W)) 69 val inMask = Input(UInt(PredictWidth.W)) 70 val update = Flipped(ValidIO(new BranchUpdateInfoWithHist)) 71 } 72 73 val io = new DefaultBasePredictorIO 74 75 // circular shifting 76 def circularShiftLeft(source: UInt, len: Int, shamt: UInt): UInt = { 77 val res = Wire(UInt(len.W)) 78 val higher = source << shamt 79 val lower = source >> (len.U - shamt) 80 res := higher | lower 81 res 82 } 83 84 def circularShiftRight(source: UInt, len: Int, shamt: UInt): UInt = { 85 val res = Wire(UInt(len.W)) 86 val higher = source << (len.U - shamt) 87 val lower = source >> shamt 88 res := higher | lower 89 res 90 } 91} 92 93class BPUStageIO extends XSBundle { 94 val pc = UInt(VAddrBits.W) 95 val mask = UInt(PredictWidth.W) 96 val resp = new PredictorResponse 97 val target = UInt(VAddrBits.W) 98 val brInfo = Vec(PredictWidth, new BranchInfo) 99} 100 101 102class BPUStage extends XSModule { 103 class DefaultIO extends XSBundle { 104 val flush = Input(Bool()) 105 val in = Flipped(Decoupled(new BPUStageIO)) 106 val pred = Decoupled(new BranchPrediction) 107 val out = Decoupled(new BPUStageIO) 108 val predecode = Flipped(ValidIO(new Predecode)) 109 } 110 val io = IO(new DefaultIO) 111 112 val predValid = RegInit(false.B) 113 114 io.in.ready := !predValid || io.out.fire() && io.pred.fire() 115 116 def npc(pc: UInt, instCount: UInt) = pc + (instCount << 1.U) 117 118 val inFire = io.in.fire() 119 val inLatch = RegEnable(io.in.bits, inFire) 120 121 val outFire = io.out.fire() 122 123 // Each stage has its own logic to decide 124 // takens, notTakens and target 125 126 val takens = VecInit((0 until PredictWidth).map(_ => false.B)) 127 val notTakens = VecInit((0 until PredictWidth).map(_ => false.B)) 128 val jmpIdx = PriorityEncoder(takens) 129 val hasNTBr = (0 until PredictWidth).map(i => i.U <= jmpIdx && notTakens(i)).reduce(_||_) 130 val taken = takens.reduce(_||_) 131 // get the last valid inst 132 // val lastValidPos = MuxCase(0.U, (PredictWidth-1 to 0).map(i => (inLatch.mask(i), i.U))) 133 val lastValidPos = PriorityMux(Reverse(inLatch.mask), (PredictWidth-1 to 0 by -1).map(i => i.U)) 134 // val lastValidPos = WireInit(0.U(log2Up(PredictWidth).W)) 135 // for (i <- 0 until PredictWidth) { 136 // when (inLatch.mask(i)) { lastValidPos := i.U } 137 // } 138 val target = WireInit(0.U(VAddrBits.W)) 139 140 io.pred.bits <> DontCare 141 io.pred.bits.taken := taken 142 io.pred.bits.jmpIdx := jmpIdx 143 io.pred.bits.hasNotTakenBrs := hasNTBr 144 io.pred.bits.target := target 145 146 io.out.bits <> DontCare 147 io.out.bits.pc := inLatch.pc 148 io.out.bits.mask := inLatch.mask 149 io.out.bits.target := target 150 io.out.bits.resp <> inLatch.resp 151 io.out.bits.brInfo := inLatch.brInfo 152 153 // Default logic 154 // pred.ready not taken into consideration 155 // could be broken 156 when (io.flush) { predValid := false.B } 157 .elsewhen (inFire) { predValid := true.B } 158 .elsewhen (outFire) { predValid := false.B } 159 .otherwise { predValid := predValid } 160 161 io.out.valid := predValid && !io.flush 162 io.pred.valid := predValid && !io.flush 163 164 XSDebug(io.in.fire(), "in:(%d %d) pc=%x, mask=%b, target=%x\n", 165 io.in.valid, io.in.ready, io.in.bits.pc, io.in.bits.mask, io.in.bits.target) 166 XSDebug(io.out.fire(), "out:(%d %d) pc=%x, mask=%b, target=%x\n", 167 io.out.valid, io.out.ready, io.out.bits.pc, io.out.bits.mask, io.out.bits.target) 168 XSDebug("flush=%d\n", io.flush) 169 XSDebug("taken=%d, takens=%b, notTakens=%b, jmpIdx=%d, hasNTBr=%d, lastValidPos=%d, target=%x\n", 170 taken, takens.asUInt, notTakens.asUInt, jmpIdx, hasNTBr, lastValidPos, target) 171 val p = io.pred.bits 172 XSDebug(io.pred.fire(), "outPred: redirect=%d, taken=%d, jmpIdx=%d, hasNTBrs=%d, target=%x, saveHalfRVI=%d\n", 173 p.redirect, p.taken, p.jmpIdx, p.hasNotTakenBrs, p.target, p.saveHalfRVI) 174} 175 176class BPUStage1 extends BPUStage { 177 178 // 'overrides' default logic 179 // when flush, the prediction should also starts 180 when (io.flush || inFire) { predValid := true.B } 181 .elsewhen(outFire) { predValid := false.B } 182 .otherwise { predValid := predValid } 183 io.in.ready := !predValid || io.out.fire() && io.pred.fire() || io.flush 184 io.out.valid := predValid 185 186 // ubtb is accessed with inLatch pc in s1, 187 // so we use io.in instead of inLatch 188 val ubtbResp = io.in.bits.resp.ubtb 189 // the read operation is already masked, so we do not need to mask here 190 takens := VecInit((0 until PredictWidth).map(i => ubtbResp.hits(i) && ubtbResp.takens(i))) 191 notTakens := VecInit((0 until PredictWidth).map(i => ubtbResp.hits(i) && ubtbResp.notTakens(i))) 192 target := Mux(taken, ubtbResp.targets(jmpIdx), npc(inLatch.pc, PopCount(inLatch.mask))) 193 194 io.pred.bits.redirect := taken 195 io.pred.bits.saveHalfRVI := ((lastValidPos === jmpIdx && taken) || !taken ) && !ubtbResp.is_RVC(lastValidPos) && ubtbResp.hits(lastValidPos) 196 197 // resp and brInfo are from the components, 198 // so it does not need to be latched 199 io.out.bits.resp <> io.in.bits.resp 200 io.out.bits.brInfo := io.in.bits.brInfo 201} 202 203class BPUStage2 extends BPUStage { 204 205 // Use latched response from s1 206 val btbResp = inLatch.resp.btb 207 val bimResp = inLatch.resp.bim 208 takens := VecInit((0 until PredictWidth).map(i => btbResp.hits(i) && (btbResp.types(i) === BrType.branch && bimResp.ctrs(i)(1) || btbResp.types(i) === BrType.jal))) 209 notTakens := VecInit((0 until PredictWidth).map(i => btbResp.hits(i) && btbResp.types(i) === BrType.branch && !bimResp.ctrs(i)(1))) 210 target := Mux(taken, btbResp.targets(jmpIdx), npc(inLatch.pc, PopCount(inLatch.mask))) 211 212 io.pred.bits.redirect := target =/= inLatch.target 213 io.pred.bits.saveHalfRVI := ((lastValidPos === jmpIdx && taken) || !taken ) && !btbResp.isRVC(lastValidPos) && btbResp.hits(lastValidPos) 214} 215 216class BPUStage3 extends BPUStage { 217 218 io.out.valid := predValid && io.predecode.valid && !io.flush 219 220 // TAGE has its own pipelines and the 221 // response comes directly from s3, 222 // so we do not use those from inLatch 223 val tageResp = io.in.bits.resp.tage 224 val tageValidTakens = VecInit((0 until PredictWidth).map( i => tageResp.takens(i) && tageResp.hits(i))) 225 226 val pdMask = io.predecode.bits.mask 227 val pds = io.predecode.bits.pd 228 229 val btbHits = inLatch.resp.btb.hits.asUInt 230 val bimTakens = VecInit(inLatch.resp.bim.ctrs.map(_(1))) 231 232 val brs = pdMask & Reverse(Cat(pds.map(_.isBr))) 233 val jals = pdMask & Reverse(Cat(pds.map(_.isJal))) 234 val jalrs = pdMask & Reverse(Cat(pds.map(_.isJalr))) 235 val calls = pdMask & Reverse(Cat(pds.map(_.isCall))) 236 val rets = pdMask & Reverse(Cat(pds.map(_.isRet))) 237 238 val callIdx = PriorityEncoder(calls) 239 val retIdx = PriorityEncoder(rets) 240 241 val brTakens = 242 if (EnableBPD) { 243 brs & Reverse(Cat((0 until PredictWidth).map(i => tageValidTakens(i)))) 244 } else { 245 brs & Reverse(Cat((0 until PredictWidth).map(i => bimTakens(i)))) 246 } 247 248 // predict taken only if btb has a target 249 takens := VecInit((0 until PredictWidth).map(i => (brTakens(i) || jals(i) || jalrs(i)) && btbHits(i))) 250 // Whether should we count in branches that are not recorded in btb? 251 // PS: Currently counted in. Whenever tage does not provide a valid 252 // taken prediction, the branch is counted as a not taken branch 253 notTakens := VecInit((0 until PredictWidth).map(i => brs(i) && !tageValidTakens(i))) 254 target := Mux(taken, inLatch.resp.btb.targets(jmpIdx), npc(inLatch.pc, PopCount(inLatch.mask))) 255 256 io.pred.bits.redirect := target =/= inLatch.target 257 io.pred.bits.saveHalfRVI := ((lastValidPos === jmpIdx && taken) || !taken ) && !pds(lastValidPos).isRVC && pdMask(lastValidPos) 258 259 // Wrap tage resp and tage meta in 260 // This is ugly 261 io.out.bits.resp.tage <> io.in.bits.resp.tage 262 for (i <- 0 until PredictWidth) { 263 io.out.bits.brInfo(i).tageMeta := io.in.bits.brInfo(i).tageMeta 264 } 265 266 XSDebug(io.predecode.valid, "predecode: mask:%b\n", io.predecode.bits.mask) 267 for (i <- 0 until PredictWidth) { 268 val p = io.predecode.bits.pd(i) 269 XSDebug(io.predecode.valid, "predecode(%d): brType:%d, br:%d, jal:%d, jalr:%d, call:%d, ret:%d, RVC:%d, excType:%d\n", 270 i.U, p.brType, p.isBr, p.isJal, p.isJalr, p.isCall, p.isRet, p.isRVC, p.excType) 271 } 272} 273 274trait BranchPredictorComponents extends HasXSParameter { 275 val ubtb = Module(new MicroBTB) 276 val btb = Module(new BTB) 277 val bim = Module(new BIM) 278 val tage = Module(new Tage) 279 val preds = Seq(ubtb, btb, bim, tage) 280 preds.map(_.io := DontCare) 281} 282 283class BPUReq extends XSBundle { 284 val pc = UInt(VAddrBits.W) 285 val hist = UInt(HistoryLength.W) 286 val inMask = UInt(PredictWidth.W) 287} 288 289class BranchUpdateInfoWithHist extends XSBundle { 290 val ui = new BranchUpdateInfo 291 val hist = UInt(HistoryLength.W) 292} 293 294object BranchUpdateInfoWithHist { 295 def apply (brInfo: BranchUpdateInfo, hist: UInt) = { 296 val b = Wire(new BranchUpdateInfoWithHist) 297 b.ui <> brInfo 298 b.hist := hist 299 b 300 } 301} 302 303abstract class BaseBPU extends XSModule with BranchPredictorComponents{ 304 val io = IO(new Bundle() { 305 // from backend 306 val inOrderBrInfo = Flipped(ValidIO(new BranchUpdateInfoWithHist)) 307 // from ifu, frontend redirect 308 val flush = Input(Vec(3, Bool())) 309 // from if1 310 val in = Flipped(ValidIO(new BPUReq)) 311 // to if2/if3/if4 312 val out = Vec(3, Decoupled(new BranchPrediction)) 313 // from if4 314 val predecode = Flipped(ValidIO(new Predecode)) 315 // to if4, some bpu info used for updating 316 val branchInfo = Decoupled(Vec(PredictWidth, new BranchInfo)) 317 }) 318 319 preds.map(_.io.update <> io.inOrderBrInfo) 320 321 val s1 = Module(new BPUStage1) 322 val s2 = Module(new BPUStage2) 323 val s3 = Module(new BPUStage3) 324 325 s1.io.flush := io.flush(0) 326 s2.io.flush := io.flush(1) 327 s3.io.flush := io.flush(2) 328 329 s1.io.in <> DontCare 330 s2.io.in <> s1.io.out 331 s3.io.in <> s2.io.out 332 333 io.out(0) <> s1.io.pred 334 io.out(1) <> s2.io.pred 335 io.out(2) <> s3.io.pred 336 337 s1.io.predecode <> DontCare 338 s2.io.predecode <> DontCare 339 s3.io.predecode <> io.predecode 340 341 io.branchInfo.valid := s3.io.out.valid 342 io.branchInfo.bits := s3.io.out.bits.brInfo 343 s3.io.out.ready := io.branchInfo.ready 344 345 XSDebug(io.branchInfo.fire(), "branchInfo sent!\n") 346 for (i <- 0 until PredictWidth) { 347 val b = io.branchInfo.bits(i) 348 XSDebug(io.branchInfo.fire(), "brInfo(%d): ubtbWrWay:%d, ubtbHit:%d, btbWrWay:%d, bimCtr:%d\n", 349 i.U, b.ubtbWriteWay, b.ubtbHits, b.btbWriteWay, b.bimCtr) 350 val t = b.tageMeta 351 XSDebug(io.branchInfo.fire(), " tageMeta: pvder(%d):%d, altDiffers:%d, pvderU:%d, pvderCtr:%d, allocate(%d):%d\n", 352 t.provider.valid, t.provider.bits, t.altDiffers, t.providerU, t.providerCtr, t.allocate.valid, t.allocate.bits) 353 } 354} 355 356 357class FakeBPU extends BaseBPU { 358 io.out.foreach(i => { 359 // Provide not takens 360 i.valid := true.B 361 i.bits <> DontCare 362 i.bits.redirect := false.B 363 }) 364 io.branchInfo <> DontCare 365} 366 367class BPU extends BaseBPU { 368 369 //**********************Stage 1****************************// 370 val s1_fire = s1.io.in.fire() 371 val s1_resp_in = Wire(new PredictorResponse) 372 val s1_brInfo_in = Wire(Vec(PredictWidth, new BranchInfo)) 373 374 s1_resp_in := DontCare 375 s1_brInfo_in := DontCare 376 377 val s1_inLatch = RegEnable(io.in, s1_fire) 378 ubtb.io.flush := io.flush(0) // TODO: fix this 379 ubtb.io.pc.valid := s1_inLatch.valid 380 ubtb.io.pc.bits := s1_inLatch.bits.pc 381 ubtb.io.inMask := s1_inLatch.bits.inMask 382 383 // Wrap ubtb response into resp_in and brInfo_in 384 s1_resp_in.ubtb <> ubtb.io.out 385 for (i <- 0 until PredictWidth) { 386 s1_brInfo_in(i).ubtbWriteWay := ubtb.io.uBTBBranchInfo.writeWay(i) 387 s1_brInfo_in(i).ubtbHits := ubtb.io.uBTBBranchInfo.hits(i) 388 } 389 390 btb.io.flush := io.flush(0) // TODO: fix this 391 btb.io.pc.valid := io.in.valid 392 btb.io.pc.bits := io.in.bits.pc 393 btb.io.inMask := io.in.bits.inMask 394 395 // Wrap btb response into resp_in and brInfo_in 396 s1_resp_in.btb <> btb.io.resp 397 for (i <- 0 until PredictWidth) { 398 s1_brInfo_in(i).btbWriteWay := btb.io.meta.writeWay(i) 399 } 400 401 bim.io.flush := io.flush(0) // TODO: fix this 402 bim.io.pc.valid := io.in.valid 403 bim.io.pc.bits := io.in.bits.pc 404 bim.io.inMask := io.in.bits.inMask 405 406 // Wrap bim response into resp_in and brInfo_in 407 s1_resp_in.bim <> bim.io.resp 408 for (i <- 0 until PredictWidth) { 409 s1_brInfo_in(i).bimCtr := bim.io.meta.ctrs(i) 410 } 411 412 413 s1.io.in.valid := io.in.valid 414 s1.io.in.bits.pc := io.in.bits.pc 415 s1.io.in.bits.mask := io.in.bits.inMask 416 s1.io.in.bits.target := DontCare 417 s1.io.in.bits.resp := s1_resp_in 418 s1.io.in.bits.brInfo <> s1_brInfo_in 419 420 //**********************Stage 2****************************// 421 tage.io.flush := io.flush(1) // TODO: fix this 422 tage.io.pc.valid := s1.io.out.fire() 423 tage.io.pc.bits := s1.io.out.bits.pc // PC from s1 424 tage.io.hist := io.in.bits.hist // The inst is from s1 425 tage.io.inMask := s1.io.out.bits.mask 426 tage.io.s3Fire := s3.io.in.fire() // Tell tage to march 1 stage 427 tage.io.bim <> s1.io.out.bits.resp.bim // Use bim results from s1 428 429 //**********************Stage 3****************************// 430 // Wrap tage response and meta into s3.io.in.bits 431 // This is ugly 432 433 s3.io.in.bits.resp.tage <> tage.io.resp 434 for (i <- 0 until PredictWidth) { 435 s3.io.in.bits.brInfo(i).tageMeta := tage.io.meta(i) 436 } 437 438} 439