xref: /XiangShan/src/main/scala/xiangshan/frontend/BPU.scala (revision 87e3f53a5fb92aa3b5e28d1e48ce520bfb4dcb20)
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
249class BranchUpdateInfoWithHist extends BranchUpdateInfo {
250  val hist = UInt(HistoryLength.W)
251}
252
253abstract class BaseBPU extends XSModule with BranchPredictorComponents{
254  val io = IO(new Bundle() {
255    // from backend
256    val inOrderBrInfo = Flipped(ValidIO(new BranchUpdateInfoWithHist))
257    // from ifu, frontend redirect
258    val flush = Input(UInt(3.W))
259    // from if1
260    val in = Flipped(ValidIO(new BPUReq))
261    // to if2/if3/if4
262    val out = Vec(3, Decoupled(new BranchPrediction))
263    // from if4
264    val predecode = Flipped(ValidIO(new Predecode))
265    // to if4, some bpu info used for updating
266    val branchInfo = Decoupled(Vec(PredictWidth, new BranchInfo))
267  })
268
269  val s1 = Module(new BPUStage1)
270  val s2 = Module(new BPUStage2)
271  val s3 = Module(new BPUStage3)
272
273  // TODO: whether to update ubtb when btb successfully
274  // corrects the wrong prediction from ubtb
275  preds.map(_.io.update <> io.inOrderBrInfo)
276
277  s1.io.flush := io.flush(0)
278  s2.io.flush := io.flush(1)
279  s3.io.flush := io.flush(2)
280
281  s1.io.in <> DontCare
282  s2.io.in <> s1.io.out
283  s3.io.in <> s2.io.out
284
285  io.out(0) <> s1.io.pred
286  io.out(1) <> s2.io.pred
287  io.out(2) <> s3.io.pred
288
289  s3.io.predecode <> io.predecode
290
291  io.branchInfo.valid := s3.io.out.valid
292  io.branchInfo.bits := s3.io.out.bits.branchInfo.metas
293  s3.io.out.ready := io.branchInfo.ready
294
295}
296
297
298class FakeBPU extends BaseBPU {
299  io.out.foreach(i => {
300    i <> DontCare
301    i.redirect := false.B
302  })
303  io.branchInfo <> DontCare
304}
305
306class BPU extends BaseBPU {
307
308
309  //**********************Stage 1****************************//
310  val s1_fire = s1.io.inFire
311  val s1_resp_in = new PredictorResponse
312  val s1_brInfo_in = VecInit(0.U.asTypeOf(Vec(PredictWidth, new BranchInfo)))
313
314  s1_resp_in := DontCare
315  s1_brInfo_in := DontCare
316
317  val s1_inLatch = RegEnable(io.in, s1_fire)
318  ubtb.io.flush := io.flush(0) // TODO: fix this
319  ubtb.io.in.pc.valid := s1_inLatch.valid
320  ubtb.io.in.pc.bits := s1_inLatch.bits.pc
321  ubtb.io.inMask := s1_inLatch.bits.inMask
322
323  // Wrap ubtb response into resp_in and brInfo_in
324  s1_resp_in.ubtb <> ubtb.io.out
325  for (i <- 0 until PredictWidth) {
326    s1_brInfo_in(i).ubtbWriteWay := ubtb.io.meta.writeWay(i)
327    s1_brInfo_in(i).ubtbHits := ubtb.io.out.targets(i).valid
328  }
329
330  btb.io.flush := io.flush(0) // TODO: fix this
331  btb.io.in.pc.valid := io.in.valid
332  btb.io.in.pc.bits := io.in.bits.pc
333  btb.io.inMask := io.in.bits.inMask
334
335  // Wrap btb response into resp_in and brInfo_in
336  s1_resp_in.btb <> btb.io.out
337  for (i <- 0 until PredictWidth) {
338    s1_brInfo_in(i).btbWriteWay := btb.io.meta.writeWay(i)
339  }
340
341  bim.io.flush := io.flush(0) // TODO: fix this
342  bim.io.in.pc.valid := io.in.valid
343  bim.io.in.pc.bits := io.in.bits.pc
344  bim.io.inMask := io.in.bits.inMask
345
346  // Wrap bim response into resp_in and brInfo_in
347  s1_resp_in.bim <> bim.io.out
348  for (i <- 0 until PredictWidth) {
349    s1_brInfo_in(i).bimCtr := bim.io.out(i)
350  }
351
352
353  s1.io.in.valid := io.in.valid
354  s1.io.in.bits.pc := io.in.pc.bits
355  s1.io.in.bits.mask := io.in.mask
356  s1.io.in.bits.target := DontCare
357  s1.io.in.bits.resp := s1_resp_in
358  s1.io.in.bits.brInfo <> s1_brInfo_in
359
360
361  tage.io.flush := io.flush(0) // TODO: fix this
362  tage.io.in.pc.valid := s1.io.out.fire()
363  tage.io.in.pc.bits := s1.io.out.bits.pc // PC from s1
364  tage.io.in.hist := io.in.hist // The inst is from s1
365  tage.io.in.inMask := s1.io.out.bits.mask
366  tage.io.in.s3Fire := s3.io.inFire // Tell tage to march 1 stage
367  tage.io.fromOthers <> s1.io.out.resp.bim // Use bim results from s1
368
369
370  // Wrap tage response and meta into s3.io.in.bits
371  // This is ugly
372  s3.io.in.bits.resp.tage <> tage.io.out
373  for (i <- 0 until PredictWidth) {
374    s3.io.in.bits.brInfo(i).tageMeta := tage.io.meta(i)
375  }
376
377}
378