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