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