xref: /XiangShan/src/main/scala/xiangshan/frontend/BPU.scala (revision 5b34357a510278f47619fd7e4a8ea484a0d5f6f6)
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 Stage1To2IO extends XSBundle {
25  val pc = Output(UInt(VAddrBits.W))
26  val btb = new Bundle {
27    val hits = Output(UInt(PredictWidth.W))
28    val targets = Output(Vec(PredictWidth, UInt(VAddrBits.W)))
29  }
30  val jbtac = new Bundle {
31    val hitIdx = Output(UInt(PredictWidth.W))
32    val target = Output(UInt(VAddrBits.W))
33  }
34  val tage = new Bundle {
35    val hits = Output(UInt(FetchWidth.W))
36    val takens = Output(Vec(FetchWidth, Bool()))
37  }
38  val hist = Output(Vec(PredictWidth, UInt(HistoryLength.W)))
39  val btbPred = ValidIO(new BranchPrediction)
40}
41
42class BPUStage1 extends XSModule {
43  val io = IO(new Bundle() {
44    val in = new Bundle { val pc = Flipped(Decoupled(UInt(VAddrBits.W))) }
45    // from backend
46    val redirectInfo = Input(new RedirectInfo)
47    // from Stage3
48    val flush = Input(Bool())
49    val s3RollBackHist = Input(UInt(HistoryLength.W))
50    val s3Taken = Input(Bool())
51    // to ifu, quick prediction result
52    val s1OutPred = ValidIO(new BranchPrediction)
53    // to Stage2
54    val out = Decoupled(new Stage1To2IO)
55  })
56
57  io.in.pc.ready := true.B
58
59  // flush Stage1 when io.flush
60  val flushS1 = BoolStopWatch(io.flush, io.in.pc.fire(), startHighPriority = true)
61  val s1OutPredLatch = RegEnable(io.s1OutPred.bits, RegNext(io.in.pc.fire()))
62  val outLatch = RegEnable(io.out.bits, RegNext(io.in.pc.fire()))
63
64  val s1Valid = RegInit(false.B)
65  when (io.flush) {
66    s1Valid := false.B
67  }.elsewhen (io.in.pc.fire()) {
68    s1Valid := true.B
69  }.elsewhen (io.out.fire()) {
70    s1Valid := false.B
71  }
72  io.out.valid := s1Valid
73
74
75  // global history register
76  val ghr = RegInit(0.U(HistoryLength.W))
77  // modify updateGhr and newGhr when updating ghr
78  val updateGhr = WireInit(false.B)
79  val newGhr = WireInit(0.U(HistoryLength.W))
80  when (updateGhr) { ghr := newGhr }
81  // use hist as global history!!!
82  val hist = Mux(updateGhr, newGhr, ghr)
83
84  // Tage predictor
85  val tage = if(EnableBPD) Module(new Tage) else Module(new FakeTAGE)
86  tage.io.req.valid := io.in.pc.fire()
87  tage.io.req.bits.pc := io.in.pc.bits
88  tage.io.req.bits.hist := hist
89  tage.io.redirectInfo <> io.redirectInfo
90  // io.s1OutPred.bits.tageMeta := tage.io.meta
91
92  // latch pc for 1 cycle latency when reading SRAM
93  val pcLatch = RegEnable(io.in.pc.bits, io.in.pc.fire())
94  // TODO: pass real mask in
95  // val maskLatch = RegEnable(btb.io.in.mask, io.in.pc.fire())
96  val maskLatch = Fill(PredictWidth, 1.U(1.W))
97
98  val r = io.redirectInfo.redirect
99  val updateFetchpc = r.pc - (r.fetchIdx << 1.U)
100  // BTB
101  val btb = Module(new BTB)
102  btb.io.in.pc <> io.in.pc
103  btb.io.in.pcLatch := pcLatch
104  // TODO: pass real mask in
105  btb.io.in.mask := Fill(PredictWidth, 1.U(1.W))
106  btb.io.redirectValid := io.redirectInfo.valid
107  btb.io.flush := io.flush
108
109  // btb.io.update.fetchPC := updateFetchpc
110  // btb.io.update.fetchIdx := r.fetchIdx
111  btb.io.update.pc := r.pc
112  btb.io.update.hit := r.btbHit
113  btb.io.update.misPred := io.redirectInfo.misPred
114  // btb.io.update.writeWay := r.btbVictimWay
115  btb.io.update.oldCtr := r.btbPredCtr
116  btb.io.update.taken := r.taken
117  btb.io.update.target := r.brTarget
118  btb.io.update.btbType := r.btbType
119  // TODO: add RVC logic
120  btb.io.update.isRVC := r.isRVC
121
122  // val btbHit = btb.io.out.hit
123  val btbTaken = btb.io.out.taken
124  val btbTakenIdx = btb.io.out.takenIdx
125  val btbTakenTarget = btb.io.out.target
126  // val btbWriteWay = btb.io.out.writeWay
127  val btbNotTakens = btb.io.out.notTakens
128  val btbCtrs = VecInit(btb.io.out.dEntries.map(_.pred))
129  val btbValids = btb.io.out.hits
130  val btbTargets = VecInit(btb.io.out.dEntries.map(_.target))
131  val btbTypes = VecInit(btb.io.out.dEntries.map(_.btbType))
132  val btbIsRVCs = VecInit(btb.io.out.dEntries.map(_.isRVC))
133
134
135  val jbtac = Module(new JBTAC)
136  jbtac.io.in.pc <> io.in.pc
137  jbtac.io.in.pcLatch := pcLatch
138  // TODO: pass real mask in
139  jbtac.io.in.mask := Fill(PredictWidth, 1.U(1.W))
140  jbtac.io.in.hist := hist
141  jbtac.io.redirectValid := io.redirectInfo.valid
142  jbtac.io.flush := io.flush
143
144  jbtac.io.update.fetchPC := updateFetchpc
145  jbtac.io.update.fetchIdx := r.fetchIdx
146  jbtac.io.update.misPred := io.redirectInfo.misPred
147  jbtac.io.update.btbType := r.btbType
148  jbtac.io.update.target := r.target
149  jbtac.io.update.hist := r.hist
150  jbtac.io.update.isRVC := r.isRVC
151
152  val jbtacHit = jbtac.io.out.hit
153  val jbtacTarget = jbtac.io.out.target
154  val jbtacHitIdx = jbtac.io.out.hitIdx
155
156  // calculate global history of each instr
157  val firstHist = RegNext(hist)
158  val histShift = Wire(Vec(PredictWidth, UInt(log2Up(PredictWidth).W)))
159  val shift = Wire(Vec(PredictWidth, Vec(PredictWidth, UInt(1.W))))
160  (0 until PredictWidth).map(i => shift(i) := Mux(!btbNotTakens(i), 0.U, ~LowerMask(UIntToOH(i.U), PredictWidth)).asTypeOf(Vec(PredictWidth, UInt(1.W))))
161  for (j <- 0 until PredictWidth) {
162    var tmp = 0.U
163    for (i <- 0 until PredictWidth) {
164      tmp = tmp + shift(i)(j)
165    }
166    histShift(j) := tmp
167  }
168  (0 until PredictWidth).map(i => io.s1OutPred.bits.hist(i) := firstHist << histShift(i))
169
170  // update ghr
171  updateGhr := io.flush || io.s1OutPred.bits.redirect || RegNext(io.in.pc.fire) && (btbNotTakens.asUInt & maskLatch).orR.asBool
172  val brJumpIdx = Mux(!btbTaken, 0.U, UIntToOH(btbTakenIdx))
173  val indirectIdx = Mux(!jbtacHit, 0.U, UIntToOH(jbtacHitIdx))
174  // if backend redirects, restore history from backend;
175  // if stage3 redirects, restore history from stage3;
176  // if stage1 redirects, speculatively update history;
177  // if none of above happens, check if stage1 has not-taken branches and shift zeroes accordingly
178  newGhr := Mux(io.redirectInfo.flush(),    (r.hist << 1.U) | !(r.btbType === BTBtype.B && !r.taken),
179            Mux(io.flush,                   Mux(io.s3Taken, (io.s3RollBackHist << 1.U) | 1.U, io.s3RollBackHist),
180            Mux(io.s1OutPred.bits.redirect, (PriorityMux(brJumpIdx | indirectIdx, io.s1OutPred.bits.hist) << 1.U | 1.U),
181                                            io.s1OutPred.bits.hist(0) << PopCount(btbNotTakens.asUInt & maskLatch))))
182
183  // redirect based on BTB and JBTAC
184  val takenIdx = LowestBit(brJumpIdx | indirectIdx, PredictWidth)
185
186  // io.out.valid := RegNext(io.in.pc.fire()) && !io.flush
187
188  // io.s1OutPred.valid := io.out.valid
189  io.s1OutPred.valid := io.out.fire()
190  when (RegNext(io.in.pc.fire())) {
191    io.s1OutPred.bits.redirect := btbTaken || jbtacHit
192    io.s1OutPred.bits.instrValid := Mux(!io.s1OutPred.bits.redirect || io.s1OutPred.bits.lateJump, maskLatch,
193                                    Mux(!btbIsRVCs(OHToUInt(takenIdx)), LowerMask(takenIdx << 1.U, PredictWidth),
194                                    LowerMask(takenIdx, PredictWidth))).asTypeOf(Vec(PredictWidth, Bool()))
195    io.s1OutPred.bits.target := Mux(takenIdx === 0.U, pcLatch + (PopCount(maskLatch) << 1.U), Mux(takenIdx === brJumpIdx, btbTakenTarget, jbtacTarget))
196    io.s1OutPred.bits.lateJump := btb.io.out.isRVILateJump || jbtac.io.out.isRVILateJump
197    (0 until PredictWidth).map(i => io.s1OutPred.bits.hist(i) := firstHist << histShift(i))
198    // io.s1OutPred.bits.btbVictimWay := btbWriteWay
199    io.s1OutPred.bits.predCtr := btbCtrs
200    io.s1OutPred.bits.btbHit := btbValids
201    io.s1OutPred.bits.tageMeta := DontCare // TODO: enableBPD
202    io.s1OutPred.bits.rasSp := DontCare
203    io.s1OutPred.bits.rasTopCtr := DontCare
204  }.otherwise {
205    io.s1OutPred.bits := s1OutPredLatch
206  }
207
208  when (RegNext(io.in.pc.fire())) {
209    io.out.bits.pc := pcLatch
210    io.out.bits.btb.hits := btbValids.asUInt
211    (0 until PredictWidth).map(i => io.out.bits.btb.targets(i) := btbTargets(i))
212    io.out.bits.jbtac.hitIdx := Mux(jbtacHit, UIntToOH(jbtacHitIdx), 0.U)
213    io.out.bits.jbtac.target := jbtacTarget
214    io.out.bits.tage <> tage.io.out
215    // TODO: we don't need this repeatedly!
216    io.out.bits.hist := io.s1OutPred.bits.hist
217    io.out.bits.btbPred := io.s1OutPred
218  }.otherwise {
219    io.out.bits := outLatch
220  }
221
222
223  // debug info
224  XSDebug("in:(%d %d)   pc=%x ghr=%b\n", io.in.pc.valid, io.in.pc.ready, io.in.pc.bits, hist)
225  XSDebug("outPred:(%d) pc=0x%x, redirect=%d instrValid=%b tgt=%x\n",
226    io.s1OutPred.valid, pcLatch, io.s1OutPred.bits.redirect, io.s1OutPred.bits.instrValid.asUInt, io.s1OutPred.bits.target)
227  XSDebug(io.flush && io.redirectInfo.flush(),
228    "flush from backend: pc=%x tgt=%x brTgt=%x btbType=%b taken=%d oldHist=%b fetchIdx=%d isExcpt=%d\n",
229    r.pc, r.target, r.brTarget, r.btbType, r.taken, r.hist, r.fetchIdx, r.isException)
230  XSDebug(io.flush && !io.redirectInfo.flush(),
231    "flush from Stage3:  s3Taken=%d s3RollBackHist=%b\n", io.s3Taken, io.s3RollBackHist)
232
233}
234
235class Stage2To3IO extends Stage1To2IO {
236}
237
238class BPUStage2 extends XSModule {
239  val io = IO(new Bundle() {
240    // flush from Stage3
241    val flush = Input(Bool())
242    val in = Flipped(Decoupled(new Stage1To2IO))
243    val out = Decoupled(new Stage2To3IO)
244  })
245
246  // flush Stage2 when Stage3 or banckend redirects
247  val flushS2 = BoolStopWatch(io.flush, io.in.fire(), startHighPriority = true)
248  val inLatch = RegInit(0.U.asTypeOf(io.in.bits))
249  when (io.in.fire()) { inLatch := io.in.bits }
250  val validLatch = RegInit(false.B)
251  when (io.flush) {
252    validLatch := false.B
253  }.elsewhen (io.in.fire()) {
254    validLatch := true.B
255  }.elsewhen (io.out.fire()) {
256    validLatch := false.B
257  }
258
259  io.out.valid := !io.flush && !flushS2 && validLatch
260  io.in.ready := !validLatch || io.out.fire()
261
262  // do nothing
263  io.out.bits := inLatch
264
265  // debug info
266  XSDebug("in:(%d %d) pc=%x out:(%d %d) pc=%x\n",
267    io.in.valid, io.in.ready, io.in.bits.pc, io.out.valid, io.out.ready, io.out.bits.pc)
268  XSDebug("validLatch=%d pc=%x\n", validLatch, inLatch.pc)
269  XSDebug(io.flush, "flush!!!\n")
270}
271
272class BPUStage3 extends XSModule {
273  val io = IO(new Bundle() {
274    val flush = Input(Bool())
275    val in = Flipped(Decoupled(new Stage2To3IO))
276    val out = Decoupled(new BranchPrediction)
277    // from icache
278    val predecode = Flipped(ValidIO(new Predecode))
279    // from backend
280    val redirectInfo = Input(new RedirectInfo)
281    // to Stage1 and Stage2
282    val flushBPU = Output(Bool())
283    // to Stage1, restore ghr in stage1 when flushBPU is valid
284    val s1RollBackHist = Output(UInt(HistoryLength.W))
285    val s3Taken = Output(Bool())
286  })
287
288  val flushS3 = BoolStopWatch(io.flush, io.in.fire(), startHighPriority = true)
289  val inLatch = RegInit(0.U.asTypeOf(io.in.bits))
290  val validLatch = RegInit(false.B)
291  val predecodeLatch = RegInit(0.U.asTypeOf(io.predecode.bits))
292  val predecodeValidLatch = RegInit(false.B)
293  when (io.in.fire()) { inLatch := io.in.bits }
294  when (io.flush) {
295    validLatch := false.B
296  }.elsewhen (io.in.fire()) {
297    validLatch := true.B
298  }.elsewhen (io.out.fire()) {
299    validLatch := false.B
300  }
301
302  when (io.predecode.valid) { predecodeLatch := io.predecode.bits }
303  when (io.flush || io.out.fire()) {
304    predecodeValidLatch := false.B
305  }.elsewhen (io.predecode.valid) {
306    predecodeValidLatch := true.B
307  }
308
309  val predecodeValid = io.predecode.valid || predecodeValidLatch
310  val predecode = Mux(io.predecode.valid, io.predecode.bits, predecodeLatch)
311  io.out.valid := validLatch && predecodeValid && !flushS3 && !io.flush
312  io.in.ready := !validLatch || io.out.fire()
313
314  // RAS
315  // TODO: split retAddr and ctr
316  def rasEntry() = new Bundle {
317    val retAddr = UInt(VAddrBits.W)
318    val ctr = UInt(8.W) // layer of nested call functions
319  }
320  val ras = RegInit(VecInit(Seq.fill(RasSize)(0.U.asTypeOf(rasEntry()))))
321  val sp = Counter(RasSize)
322  val rasTop = ras(sp.value)
323  val rasTopAddr = rasTop.retAddr
324
325  // get the first taken branch/jal/call/jalr/ret in a fetch line
326  // brTakenIdx/jalIdx/callIdx/jalrIdx/retIdx/jmpIdx is one-hot encoded.
327  // brNotTakenIdx indicates all the not-taken branches before the first jump instruction.
328  val brIdx = inLatch.btb.hits & Reverse(Cat(predecode.fuOpTypes.map { t => ALUOpType.isBranch(t) }).asUInt) & predecode.mask
329  val brTakenIdx = if(EnableBPD) {
330    LowestBit(brIdx & Reverse(Cat(inLatch.tage.takens.map {t => Fill(2, t.asUInt)}).asUInt), PredictWidth)
331  } else {
332    LowestBit(brIdx & Reverse(Cat(inLatch.btbPred.bits.predCtr.map {c => c(1)}).asUInt), PredictWidth)
333  }
334  // TODO: btb doesn't need to hit, jalIdx/callIdx can be calculated based on instructions read in Cache
335  val jalIdx = LowestBit(inLatch.btb.hits & Reverse(Cat(predecode.fuOpTypes.map { t => t === JumpOpType.jal }).asUInt) & predecode.mask, PredictWidth)
336  val callIdx = LowestBit(inLatch.btb.hits & predecode.mask & Reverse(Cat(predecode.fuOpTypes.map { t => t === JumpOpType.call }).asUInt), PredictWidth)
337  val jalrIdx = LowestBit(inLatch.jbtac.hitIdx & predecode.mask & Reverse(Cat(predecode.fuOpTypes.map { t => t === JumpOpType.jalr }).asUInt), PredictWidth)
338  val retIdx = LowestBit(predecode.mask & Reverse(Cat(predecode.fuOpTypes.map { t => t === JumpOpType.ret }).asUInt), PredictWidth)
339
340  val jmpIdx = LowestBit(brTakenIdx | jalIdx | callIdx | jalrIdx | retIdx, PredictWidth)
341  val brNotTakenIdx = brIdx & LowerMask(jmpIdx, PredictWidth) & (
342    if(EnableBPD) ~Reverse(Cat(inLatch.tage.takens.map {t => Fill(2, t.asUInt)}).asUInt)
343    else ~Reverse(Cat(inLatch.btbPred.bits.predCtr.map {c => c(1)}).asUInt))
344
345  val lateJump = jmpIdx === HighestBit(predecode.mask, PredictWidth) && !predecode.isRVC(OHToUInt(jmpIdx))
346
347  io.out.bits.target := Mux(jmpIdx === 0.U, inLatch.pc + (PopCount(predecode.mask) << 1.U),
348                        Mux(jmpIdx === retIdx, rasTopAddr,
349                        Mux(jmpIdx === jalrIdx, inLatch.jbtac.target,
350                        PriorityMux(jmpIdx, inLatch.btb.targets)))) // TODO: jal and call's target can be calculated here
351
352  io.out.bits.instrValid := Mux(!jmpIdx.orR || lateJump, predecode.mask,
353                            Mux(!predecode.isRVC(OHToUInt(jmpIdx)), LowerMask(jmpIdx << 1.U, PredictWidth),
354                            LowerMask(jmpIdx, PredictWidth))).asTypeOf(Vec(PredictWidth, Bool()))
355
356  // io.out.bits.btbVictimWay := inLatch.btbPred.bits.btbVictimWay
357  io.out.bits.lateJump := lateJump
358  io.out.bits.predCtr := inLatch.btbPred.bits.predCtr
359  io.out.bits.btbHit := inLatch.btbPred.bits.btbHit
360  io.out.bits.tageMeta := inLatch.btbPred.bits.tageMeta
361  //io.out.bits.btbType := Mux(jmpIdx === retIdx, BTBtype.R,
362  //  Mux(jmpIdx === jalrIdx, BTBtype.I,
363  //  Mux(jmpIdx === brTakenIdx, BTBtype.B, BTBtype.J)))
364  val firstHist = inLatch.btbPred.bits.hist(0)
365  // there may be several notTaken branches before the first jump instruction,
366  // so we need to calculate how many zeroes should each instruction shift in its global history.
367  // each history is exclusive of instruction's own jump direction.
368  val histShift = Wire(Vec(PredictWidth, UInt(log2Up(PredictWidth).W)))
369  val shift = Wire(Vec(PredictWidth, Vec(PredictWidth, UInt(1.W))))
370  (0 until PredictWidth).map(i => shift(i) := Mux(!brNotTakenIdx(i), 0.U, ~LowerMask(UIntToOH(i.U), PredictWidth)).asTypeOf(Vec(PredictWidth, UInt(1.W))))
371  for (j <- 0 until PredictWidth) {
372    var tmp = 0.U
373    for (i <- 0 until PredictWidth) {
374      tmp = tmp + shift(i)(j)
375    }
376    histShift(j) := tmp
377  }
378  (0 until PredictWidth).map(i => io.out.bits.hist(i) := firstHist << histShift(i))
379  // save ras checkpoint info
380  io.out.bits.rasSp := sp.value
381  io.out.bits.rasTopCtr := rasTop.ctr
382
383  // flush BPU and redirect when target differs from the target predicted in Stage1
384  // io.out.bits.redirect := (if(EnableBPD) (inLatch.btbPred.bits.redirect ^ jmpIdx.orR.asBool ||
385  //   inLatch.btbPred.bits.redirect && jmpIdx.orR.asBool && io.out.bits.target =/= inLatch.btbPred.bits.target)
386  //   else false.B)
387  io.out.bits.redirect := inLatch.btbPred.bits.redirect ^ jmpIdx.orR.asBool ||
388    inLatch.btbPred.bits.redirect && jmpIdx.orR.asBool && io.out.bits.target =/= inLatch.btbPred.bits.target
389  io.flushBPU := io.out.bits.redirect && io.out.fire()
390
391  // speculative update RAS
392  val rasWrite = WireInit(0.U.asTypeOf(rasEntry()))
393  rasWrite.retAddr := inLatch.pc + (OHToUInt(callIdx) << 1.U) + Mux(PriorityMux(callIdx, predecode.isRVC), 2.U, 4.U)
394  val allocNewEntry = rasWrite.retAddr =/= rasTopAddr
395  rasWrite.ctr := Mux(allocNewEntry, 1.U, rasTop.ctr + 1.U)
396  when (io.out.fire() && jmpIdx =/= 0.U) {
397    when (jmpIdx === callIdx) {
398      ras(Mux(allocNewEntry, sp.value + 1.U, sp.value)) := rasWrite
399      when (allocNewEntry) { sp.value := sp.value + 1.U }
400    }.elsewhen (jmpIdx === retIdx) {
401      when (rasTop.ctr === 1.U) {
402        sp.value := Mux(sp.value === 0.U, 0.U, sp.value - 1.U)
403      }.otherwise {
404        ras(sp.value) := Cat(rasTop.ctr - 1.U, rasTopAddr).asTypeOf(rasEntry())
405      }
406    }
407  }
408  // use checkpoint to recover RAS
409  val recoverSp = io.redirectInfo.redirect.rasSp
410  val recoverCtr = io.redirectInfo.redirect.rasTopCtr
411  when (io.redirectInfo.flush()) {
412    sp.value := recoverSp
413    ras(recoverSp) := Cat(recoverCtr, ras(recoverSp).retAddr).asTypeOf(rasEntry())
414  }
415
416  // roll back global history in S1 if S3 redirects
417  io.s1RollBackHist := Mux(io.s3Taken, PriorityMux(jmpIdx, io.out.bits.hist), io.out.bits.hist(0) << PopCount(brNotTakenIdx))
418  // whether Stage3 has a taken jump
419  io.s3Taken := jmpIdx.orR.asBool
420
421  // debug info
422  XSDebug(io.in.fire(), "in:(%d %d) pc=%x\n", io.in.valid, io.in.ready, io.in.bits.pc)
423  XSDebug(io.out.fire(), "out:(%d %d) pc=%x redirect=%d predcdMask=%b instrValid=%b tgt=%x\n",
424    io.out.valid, io.out.ready, inLatch.pc, io.out.bits.redirect, predecode.mask, io.out.bits.instrValid.asUInt, io.out.bits.target)
425  XSDebug("flushS3=%d\n", flushS3)
426  XSDebug("validLatch=%d predecodeValid=%d\n", validLatch, predecodeValid)
427  XSDebug("brIdx=%b brTakenIdx=%b brNTakenIdx=%b jalIdx=%b jalrIdx=%b callIdx=%b retIdx=%b\n",
428    brIdx, brTakenIdx, brNotTakenIdx, jalIdx, jalrIdx, callIdx, retIdx)
429}
430
431class BPU extends XSModule {
432  val io = IO(new Bundle() {
433    // from backend
434    // flush pipeline if misPred and update bpu based on redirect signals from brq
435    val redirectInfo = Input(new RedirectInfo)
436
437    val in = new Bundle { val pc = Flipped(Valid(UInt(VAddrBits.W))) }
438
439    val btbOut = ValidIO(new BranchPrediction)
440    val tageOut = Decoupled(new BranchPrediction)
441
442    // predecode info from icache
443    // TODO: simplify this after implement predecode unit
444    val predecode = Flipped(ValidIO(new Predecode))
445  })
446
447  val s1 = Module(new BPUStage1)
448  val s2 = Module(new BPUStage2)
449  val s3 = Module(new BPUStage3)
450
451  s1.io.redirectInfo <> io.redirectInfo
452  s1.io.flush := s3.io.flushBPU || io.redirectInfo.flush()
453  s1.io.in.pc.valid := io.in.pc.valid
454  s1.io.in.pc.bits <> io.in.pc.bits
455  io.btbOut <> s1.io.s1OutPred
456  s1.io.s3RollBackHist := s3.io.s1RollBackHist
457  s1.io.s3Taken := s3.io.s3Taken
458
459  s1.io.out <> s2.io.in
460  s2.io.flush := s3.io.flushBPU || io.redirectInfo.flush()
461
462  s2.io.out <> s3.io.in
463  s3.io.flush := io.redirectInfo.flush()
464  s3.io.predecode <> io.predecode
465  io.tageOut <> s3.io.out
466  s3.io.redirectInfo <> io.redirectInfo
467}