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