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, ValidUndirectioned(UInt(VaddrBits.W))) 28 val takens = Vec(PredictWidth, Bool()) 29 val notTakens = Vec(PredictWidth, Bool()) 30 val isRVC = Vec(PredictWidth, Bool()) 31 } 32 class BtbResp extends XSBundle { 33 // the valid bits indicates whether a target is hit 34 val targets = Vec(PredictWidth, ValidUndirectioned(UInt(VaddrBits.W))) 35 val types = Vec(PredictWidth, UInt(2.W)) 36 val isRVC = Vec(PredictWidth, Bool()) 37 } 38 class BimResp extends XSBundle { 39 val ctrs = Vec(PredictWidth, ValidUndirectioned(UInt(2.W))) 40 } 41 class TageResp extends XSBundle { 42 // the valid bits indicates whether a prediction is hit 43 val takens = Vec(PredictWidth, ValidUndirectioned(Bool())) 44 } 45 46 val ubtb = new UbtbResp 47 val btb = new BtbResp 48 val bim = new BimResp 49 val tage = new TageResp 50} 51 52abstract class BasePredictor extends XSModule { 53 val metaLen = 0 54 55 // An implementation MUST extend the IO bundle with a response 56 // and the special input from other predictors, as well as 57 // the metas to store in BRQ 58 abstract class Resp extends XSBundle with PredictorResponse {} 59 abstract class FromOthers extends XSBundle {} 60 abstract class Meta extends XSBundle {} 61 62 class DefaultBasePredictorIO extends XSBundle { 63 val flush = Input(Bool()) 64 val pc = Flipped(ValidIO(UInt(VAddrBits.W))) 65 val hist = Input(UInt(HistoryLength.W)) 66 val inMask = Input(UInt(PredictWidth.W)) 67 val update = Flipped(ValidIO(new BranchUpdateInfo)) 68 } 69} 70 71class BPUStageIO extends XSBundle { 72 val pc = UInt(VAddrBits.W) 73 val mask = UInt(PredictWidth.W) 74 val resp = new PredictorResponse 75 val target = UInt(VaddrBits.W) 76 val brInfo = Vec(PredictWidth, new BranchInfo) 77} 78 79 80abstract class BPUStage extends XSModule { 81 class DefaultIO extends XSBundle { 82 val flush = Input(Bool()) 83 val in = Flipped(Decoupled(new BPUStageIO)) 84 val pred = Decoupled(new BranchPrediction) 85 val out = Decoupled(new BPUStageIO) 86 87 val inFire = OutPut(Bool()) 88 } 89 def npc(pc: UInt, instCount: UInt) = pc + (instCount << 1.U) 90 91 io.in.ready = !outValid || io.out.fire() && io.pred.fire() 92 val inFire = io.in.fire() 93 val inLatch = RegEnable(io.in.bits, inFire) 94 io.inFire := inFire 95 96 val predValid = RegInit(false.B) 97 val outFire = io.out.fire() 98 99 // Each stage has its own logic to decide 100 // takens, notTakens and target 101 102 val takens = Vec(PredictWidth, Bool()) 103 val notTakens = Vec(PredictWidth, Bool()) 104 val hasNTBr = (0 until PredictWidth).map(i => i.U <= jmpIdx && notTakens(i)).reduce(_||_) 105 val taken = takens.reduce(_||_) 106 val jmpIdx = PriorityEncoder(takens) 107 // get the last valid inst 108 val lastValidPos = PriorityMux((PredictWidth-1 to 0).map(i => (inLatch.mask(i), i.U))) 109 val target = UInt(VaddrBits.W) 110 111 io.pred.bits <> DontCare 112 io.pred.bits.taken := taken 113 io.pred.bits.jmpIdx := jmpIdx 114 io.pred.bits.hasNotTakenBrs := hasNTBr 115 io.pred.bits.target := target 116 117 io.out.bits <> DontCare 118 io.out.bits.pc := inLatch.pc.bits 119 io.out.bits.mask := inLatch.inMask 120 io.out.bits.target := target 121 io.out.bits.resp <> inLatch.resp 122 io.out.bits.brInfo := inLatch.brInfo 123 124 // Default logic 125 // pred.ready not taken into consideration 126 // could be broken 127 when (io.flush) { 128 predValid := false.B 129 }.elsewhen (inFire) { 130 predValid := true.B 131 }.elsewhen (outFire) { 132 predValid := false.B 133 }.otherwise { 134 predValid := outValid 135 } 136 137 io.out.valid := predValid && !io.flush 138 io.pred.valid := predValid && !io.flush 139} 140 141class BPUStage1 extends BPUStage { 142 143 val io = new DefaultIO 144 145 // 'overrides' default logic 146 // when flush, the prediction should also starts 147 override predValid = BoolStopWatch(io.flush || inFire, outFire, true) 148 io.out.valid := predValid 149 150 // ubtb is accessed with inLatch pc in s1, 151 // so we use io.in instead of inLatch 152 val ubtbResp = io.in.bits.ubtb 153 // the read operation is already masked, so we do not need to mask here 154 takens := VecInit((0 until PredictWidth).map(i => ubtbResp.targets(i).valid && ubtbResp.takens(i))) 155 notTakens := VecInit((0 until PredictWidth).map(i => ubtbResp.targets(i).valid && ubtbResp.notTakens(i))) 156 target := Mux(taken, ubtbResp.targets(jmpIdx), npc(inLatch.pc, PopCount(inLatch.mask))) 157 158 io.pred.bits.redirect := taken 159 io.pred.bits.saveHalfRVI := ((lastValidPos === jmpIdx && taken) || !taken ) && !ubtbResp.isRVC(lastValidPos) 160 161 // resp and brInfo are from the components, 162 // so it does not need to be latched 163 io.out.bits.resp <> io.in.bits.resp 164 io.out.bits.brInfo := io.in.bits.brInfo 165} 166 167class BPUStage2 extends XSModule { 168 val io = new DefaultIO 169 170 // Use latched response from s1 171 val btbResp = inLatch.btb 172 val bimResp = inLatch.bim 173 takens := VecInit((0 until PredictWidth).map(i => btbResp.targets(i).valid && bimResp.ctrs(i)(1))) 174 notTakens := VecInit((0 until PredictWidth).map(i => btbResp.targets(i).valid && btbResp.types(i) === BrType.branch && !bimResp.ctrs(i)(1))) 175 target := Mux(taken, btbResp.targets(jmpIdx), npc(inLatch.pc, PopCount(inLatch.mask))) 176 177 io.pred.bits.redirect := target =/= inLatch.target 178 io.pred.bits.saveHalfRVI := ((lastValidPos === jmpIdx && taken) || !taken ) && !btbResp.isRVC(lastValidPos) 179} 180 181class BPUStage3 extends XSModule { 182 class S3IO extends DefaultIO { 183 val predecode = Flipped(ValidIO(new Predecode)) 184 } 185 val io = new S3IO 186 io.out.valid := outValid && io.predecode.valid && !io.flush 187 188 // TAGE has its own pipelines and the 189 // response comes directly from s3, 190 // so we do not use those from inLatch 191 val tageResp = io.in.bits.tage 192 val tageValidTakens = VecInit(tageResp.takens.map(t => t.valid && t.bits)) 193 194 val pdMask = io.predecode.bits.mask 195 val pds = io.predecode.bits.pd 196 197 val btbHits = VecInit(inLatch.btb.targets.map(_.valid)).asUInt 198 val bimTakens = VecInit(inLatch.bim.ctrs.map(_.bits(1))) 199 200 val brs = pdMask & Reverse(Cat(pds.map(_.isBr))) 201 val jals = pdMask & Reverse(Cat(pds.map(_.isJal))) 202 val jalrs = pdMask & Reverse(Cat(pds.map(_.isJalr))) 203 val calls = pdMask & Reverse(Cat(pds.map(_.isCall))) 204 val rets = pdMask & Reverse(Cat(pds.map(_.isRet))) 205 206 val callIdx = PriorityEncoder(calls) 207 val retIdx = PriorityEncoder(rets) 208 209 val brTakens = 210 if (EnableBPD) { 211 brs & Reverse(Cat((0 until PredictWidth).map(i => btbHits(i) && tageValidTakens(i)))) 212 } else { 213 brs & Reverse(Cat((0 until PredictWidth).map(i => btbHits(i) && bimTakens(i)))) 214 } 215 216 takens := VecInit((0 until PredictWidth).map(i => brTakens(i) || jals(i) || jalrs(i))) 217 // Whether should we count in branches that are not recorded in btb? 218 // PS: Currently counted in. Whenever tage does not provide a valid 219 // taken prediction, the branch is counted as a not taken branch 220 notTakens := VecInit((0 until PredictWidth).map(i => brs(i) && !tageValidTakens(i))) 221 target := Mux(taken, inLatch.btb.targets(jmpIdx), npc(inLatch.pc, PopCount(inLatch.mask))) 222 223 io.pred.bits.redirect := target =/= inLatch.target 224 io.pred.bits.saveHalfRVI := ((lastValidPos === jmpIdx && taken) || !taken ) && !pds(lastValidPos).isRVC 225 226 // Wrap tage resp and tage meta in 227 // This is ugly 228 io.out.bits.resp.tage <> io.in.bits.tage 229 for (i <- 0 until PredictWidth) { 230 io.out.bits.brInfo(i).tageMeta := io.in.bits.brInfo(i).tageMeta 231 } 232} 233 234trait BranchPredictorComponents extends HasXSParameter { 235 val ubtb = new Module(MicroBTB) 236 val btb = new Module(BTB) 237 val bim = new Module(BIM) 238 val tage = new Module(Tage) 239 val preds = Seq(ubtb, btb, bim, tage) 240 preds.map(_.io := DontCare) 241} 242 243class BPUReq extends XSBundle { 244 val pc = UInt(VAddrBits.W) 245 val hist = UInt(HistoryLength.W) 246 val inMask = UInt(PredictWidth.W) 247} 248 249abstract class BaseBPU extends XSModule with BranchPredictorComponents{ 250 val io = IO(new Bundle() { 251 // from backend 252 val inOrderBrInfo = Flipped(ValidIO(new BranchUpdateInfo)) 253 // from ifu, frontend redirect 254 val flush = Input(UInt(3.W)) 255 // from if1 256 val in = Flipped(ValidIO(new BPUReq)) 257 // to if2/if3/if4 258 val out = Vec(3, Decoupled(new BranchPrediction)) 259 // from if4 260 val predecode = Flipped(ValidIO(new Predecode)) 261 // to if4, some bpu info used for updating 262 val branchInfo = Decoupled(Vec(PredictWidth, new BranchInfo)) 263 }) 264 265 val s1 = Module(new BPUStage1) 266 val s2 = Module(new BPUStage2) 267 val s3 = Module(new BPUStage3) 268 269 // TODO: whether to update ubtb when btb successfully 270 // corrects the wrong prediction from ubtb 271 preds.map(_.io.update <> io.inOrderBrInfo) 272 273 s1.io.flush := io.flush(0) 274 s2.io.flush := io.flush(1) 275 s3.io.flush := io.flush(2) 276 277 s1.io.in <> DontCare 278 s2.io.in <> s1.io.out 279 s3.io.in <> s2.io.out 280 281 io.out(0) <> s1.io.pred 282 io.out(1) <> s2.io.pred 283 io.out(2) <> s3.io.pred 284 285 s3.io.predecode <> io.predecode 286 287 io.branchInfo.valid := s3.io.out.valid 288 io.branchInfo.bits := s3.io.out.bits.branchInfo.metas 289 s3.io.out.ready := io.branchInfo.ready 290 291} 292 293 294class FakeBPU extends BaseBPU { 295 io.out.foreach(i => { 296 i <> DontCare 297 i.redirect := false.B 298 }) 299 io.branchInfo <> DontCare 300} 301 302class BPU extends BaseBPU { 303 304 305 //**********************Stage 1****************************// 306 val s1_fire = s1.io.inFire 307 val s1_resp_in = new PredictorResponse 308 val s1_brInfo_in = VecInit(0.U.asTypeOf(Vec(PredictWidth, new BranchInfo))) 309 310 s1_resp_in := DontCare 311 s1_brInfo_in := DontCare 312 313 val s1_inLatch = RegEnable(io.in, s1_fire) 314 ubtb.io.flush := io.flush(0) // TODO: fix this 315 ubtb.io.in.pc.valid := s1_inLatch.valid 316 ubtb.io.in.pc.bits := s1_inLatch.bits.pc 317 ubtb.io.inMask := s1_inLatch.bits.inMask 318 319 // Wrap ubtb response into resp_in and brInfo_in 320 s1_resp_in.ubtb <> ubtb.io.out 321 for (i <- 0 until PredictWidth) { 322 s1_brInfo_in(i).ubtbWriteWay := ubtb.io.meta.writeWay(i) 323 s1_brInfo_in(i).ubtbHits := ubtb.io.out.targets(i).valid 324 } 325 326 btb.io.flush := io.flush(0) // TODO: fix this 327 btb.io.in.pc.valid := io.in.valid 328 btb.io.in.pc.bits := io.in.bits.pc 329 btb.io.inMask := io.in.bits.inMask 330 331 // Wrap btb response into resp_in and brInfo_in 332 s1_resp_in.btb <> btb.io.out 333 for (i <- 0 until PredictWidth) { 334 s1_brInfo_in(i).btbWriteWay := btb.io.meta.writeWay(i) 335 } 336 337 bim.io.flush := io.flush(0) // TODO: fix this 338 bim.io.in.pc.valid := io.in.valid 339 bim.io.in.pc.bits := io.in.bits.pc 340 bim.io.inMask := io.in.bits.inMask 341 342 // Wrap bim response into resp_in and brInfo_in 343 s1_resp_in.bim <> bim.io.out 344 for (i <- 0 until PredictWidth) { 345 s1_brInfo_in(i).bimCtr := bim.io.out(i) 346 } 347 348 349 s1.io.in.valid := io.in.valid 350 s1.io.in.bits.pc := io.in.pc.bits 351 s1.io.in.bits.mask := io.in.mask 352 s1.io.in.bits.target := DontCare 353 s1.io.in.bits.resp := s1_resp_in 354 s1.io.in.bits.brInfo <> s1_brInfo_in 355 356 357 tage.io.flush := io.flush(0) // TODO: fix this 358 tage.io.in.pc.valid := s1.io.out.fire() 359 tage.io.in.pc.bits := s1.io.out.bits.pc // PC from s1 360 tage.io.in.hist := io.in.hist // The inst is from s1 361 tage.io.in.inMask := s1.io.out.bits.mask 362 tage.io.in.s3Fire := s3.io.inFire // Tell tage to march 1 stage 363 tage.io.fromOthers <> s1.io.out.resp.bim // Use bim results from s1 364 365 366 // Wrap tage response and meta into s3.io.in.bits 367 // This is ugly 368 s3.io.in.bits.resp.tage <> tage.io.out 369 for (i <- 0 until PredictWidth) { 370 s3.io.in.bits.brInfo(i).tageMeta := tage.io.meta(i) 371 } 372 373} 374