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