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