xref: /XiangShan/src/main/scala/xiangshan/frontend/BPU.scala (revision 0466583513e4c1ddbbb566b866b8963635acb20f)
1/***************************************************************************************
2* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
3* Copyright (c) 2020-2021 Peng Cheng Laboratory
4*
5* XiangShan is licensed under Mulan PSL v2.
6* You can use this software according to the terms and conditions of the Mulan PSL v2.
7* You may obtain a copy of Mulan PSL v2 at:
8*          http://license.coscl.org.cn/MulanPSL2
9*
10* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13*
14* See the Mulan PSL v2 for more details.
15***************************************************************************************/
16
17package xiangshan.frontend
18
19import chipsalliance.rocketchip.config.Parameters
20import chisel3._
21import chisel3.experimental.chiselName
22import chisel3.util._
23import xiangshan._
24import utils._
25import utility._
26
27import scala.math.min
28import xiangshan.backend.decode.ImmUnion
29
30trait HasBPUConst extends HasXSParameter {
31  val MaxMetaLength = if (!env.FPGAPlatform) 512 else 256 // TODO: Reduce meta length
32  val MaxBasicBlockSize = 32
33  val LHistoryLength = 32
34  // val numBr = 2
35  val useBPD = true
36  val useLHist = true
37  val numBrSlot = numBr-1
38  val totalSlot = numBrSlot + 1
39
40  def BP_STAGES = (0 until 3).map(_.U(2.W))
41  def BP_S1 = BP_STAGES(0)
42  def BP_S2 = BP_STAGES(1)
43  def BP_S3 = BP_STAGES(2)
44  val numBpStages = BP_STAGES.length
45
46  val debug = true
47  // TODO: Replace log2Up by log2Ceil
48}
49
50trait HasBPUParameter extends HasXSParameter with HasBPUConst {
51  val BPUDebug = true && !env.FPGAPlatform && env.EnablePerfDebug
52  val EnableCFICommitLog = true
53  val EnbaleCFIPredLog = true
54  val EnableBPUTimeRecord = (EnableCFICommitLog || EnbaleCFIPredLog) && !env.FPGAPlatform
55  val EnableCommit = false
56}
57
58class BPUCtrl(implicit p: Parameters) extends XSBundle {
59  val ubtb_enable = Bool()
60  val btb_enable  = Bool()
61  val bim_enable  = Bool()
62  val tage_enable = Bool()
63  val sc_enable   = Bool()
64  val ras_enable  = Bool()
65  val loop_enable = Bool()
66}
67
68trait BPUUtils extends HasXSParameter {
69  // circular shifting
70  def circularShiftLeft(source: UInt, len: Int, shamt: UInt): UInt = {
71    val res = Wire(UInt(len.W))
72    val higher = source << shamt
73    val lower = source >> (len.U - shamt)
74    res := higher | lower
75    res
76  }
77
78  def circularShiftRight(source: UInt, len: Int, shamt: UInt): UInt = {
79    val res = Wire(UInt(len.W))
80    val higher = source << (len.U - shamt)
81    val lower = source >> shamt
82    res := higher | lower
83    res
84  }
85
86  // To be verified
87  def satUpdate(old: UInt, len: Int, taken: Bool): UInt = {
88    val oldSatTaken = old === ((1 << len)-1).U
89    val oldSatNotTaken = old === 0.U
90    Mux(oldSatTaken && taken, ((1 << len)-1).U,
91      Mux(oldSatNotTaken && !taken, 0.U,
92        Mux(taken, old + 1.U, old - 1.U)))
93  }
94
95  def signedSatUpdate(old: SInt, len: Int, taken: Bool): SInt = {
96    val oldSatTaken = old === ((1 << (len-1))-1).S
97    val oldSatNotTaken = old === (-(1 << (len-1))).S
98    Mux(oldSatTaken && taken, ((1 << (len-1))-1).S,
99      Mux(oldSatNotTaken && !taken, (-(1 << (len-1))).S,
100        Mux(taken, old + 1.S, old - 1.S)))
101  }
102
103  def getFallThroughAddr(start: UInt, carry: Bool, pft: UInt) = {
104    val higher = start.head(VAddrBits-log2Ceil(PredictWidth)-instOffsetBits)
105    Cat(Mux(carry, higher+1.U, higher), pft, 0.U(instOffsetBits.W))
106  }
107
108  def foldTag(tag: UInt, l: Int): UInt = {
109    val nChunks = (tag.getWidth + l - 1) / l
110    val chunks = (0 until nChunks).map { i =>
111      tag(min((i+1)*l, tag.getWidth)-1, i*l)
112    }
113    ParallelXOR(chunks)
114  }
115}
116
117// class BranchPredictionUpdate(implicit p: Parameters) extends XSBundle with HasBPUConst {
118//   val pc = UInt(VAddrBits.W)
119//   val br_offset = Vec(num_br, UInt(log2Up(MaxBasicBlockSize).W))
120//   val br_mask = Vec(MaxBasicBlockSize, Bool())
121//
122//   val jmp_valid = Bool()
123//   val jmp_type = UInt(3.W)
124//
125//   val is_NextMask = Vec(FetchWidth*2, Bool())
126//
127//   val cfi_idx = Valid(UInt(log2Ceil(MaxBasicBlockSize).W))
128//   val cfi_mispredict = Bool()
129//   val cfi_is_br = Bool()
130//   val cfi_is_jal = Bool()
131//   val cfi_is_jalr = Bool()
132//
133//   val ghist = new ShiftingGlobalHistory()
134//
135//   val target = UInt(VAddrBits.W)
136//
137//   val meta = UInt(MaxMetaLength.W)
138//   val spec_meta = UInt(MaxMetaLength.W)
139//
140//   def taken = cfi_idx.valid
141// }
142
143
144class BasePredictorInput (implicit p: Parameters) extends XSBundle with HasBPUConst {
145  def nInputs = 1
146
147  val s0_pc = UInt(VAddrBits.W)
148
149  val folded_hist = new AllFoldedHistories(foldedGHistInfos)
150  val ghist = UInt(HistoryLength.W)
151
152  val resp_in = Vec(nInputs, new BranchPredictionResp)
153
154  // val final_preds = Vec(numBpStages, new)
155  // val toFtq_fire = Bool()
156
157  // val s0_all_ready = Bool()
158}
159
160class BasePredictorOutput (implicit p: Parameters) extends BranchPredictionResp {}
161
162class BasePredictorIO (implicit p: Parameters) extends XSBundle with HasBPUConst {
163  val reset_vector = Input(UInt(PAddrBits.W))
164  val in  = Flipped(DecoupledIO(new BasePredictorInput)) // TODO: Remove DecoupledIO
165  // val out = DecoupledIO(new BasePredictorOutput)
166  val out = Output(new BasePredictorOutput)
167  // val flush_out = Valid(UInt(VAddrBits.W))
168
169  val ctrl = Input(new BPUCtrl)
170
171  val s0_fire = Input(Bool())
172  val s1_fire = Input(Bool())
173  val s2_fire = Input(Bool())
174  val s3_fire = Input(Bool())
175
176  val s2_redirect = Input(Bool())
177  val s3_redirect = Input(Bool())
178
179  val s1_ready = Output(Bool())
180  val s2_ready = Output(Bool())
181  val s3_ready = Output(Bool())
182
183  val update = Flipped(Valid(new BranchPredictionUpdate))
184  val redirect = Flipped(Valid(new BranchPredictionRedirect))
185}
186
187abstract class BasePredictor(implicit p: Parameters) extends XSModule
188  with HasBPUConst with BPUUtils with HasPerfEvents {
189  val meta_size = 0
190  val spec_meta_size = 0
191  val is_fast_pred = false
192  val io = IO(new BasePredictorIO())
193
194  io.out := io.in.bits.resp_in(0)
195
196  io.out.last_stage_meta := 0.U
197
198  io.in.ready := !io.redirect.valid
199
200  io.s1_ready := true.B
201  io.s2_ready := true.B
202  io.s3_ready := true.B
203
204  val reset_vector = DelayN(io.reset_vector, 5)
205  val s0_pc       = WireInit(io.in.bits.s0_pc) // fetchIdx(io.f0_pc)
206  val s1_pc       = RegEnable(s0_pc, io.s0_fire)
207  val s2_pc       = RegEnable(s1_pc, io.s1_fire)
208  val s3_pc       = RegEnable(s2_pc, io.s2_fire)
209
210  when (RegNext(RegNext(reset.asBool) && !reset.asBool)) {
211    s1_pc := reset_vector
212  }
213
214  io.out.s1.pc := s1_pc
215  io.out.s2.pc := s2_pc
216  io.out.s3.pc := s3_pc
217
218  val perfEvents: Seq[(String, UInt)] = Seq()
219
220
221  def getFoldedHistoryInfo: Option[Set[FoldedHistoryInfo]] = None
222}
223
224class FakePredictor(implicit p: Parameters) extends BasePredictor {
225  io.in.ready                 := true.B
226  io.out.last_stage_meta      := 0.U
227  io.out := io.in.bits.resp_in(0)
228}
229
230class BpuToFtqIO(implicit p: Parameters) extends XSBundle {
231  val resp = DecoupledIO(new BpuToFtqBundle())
232}
233
234class PredictorIO(implicit p: Parameters) extends XSBundle {
235  val bpu_to_ftq = new BpuToFtqIO()
236  val ftq_to_bpu = Flipped(new FtqToBpuIO())
237  val ctrl = Input(new BPUCtrl)
238  val reset_vector = Input(UInt(PAddrBits.W))
239}
240
241@chiselName
242class Predictor(implicit p: Parameters) extends XSModule with HasBPUConst with HasPerfEvents with HasCircularQueuePtrHelper {
243  val io = IO(new PredictorIO)
244
245  val ctrl = DelayN(io.ctrl, 1)
246  val predictors = Module(if (useBPD) new Composer else new FakePredictor)
247
248  def numOfStage = 3
249  require(numOfStage > 1, "BPU numOfStage must be greater than 1")
250  val topdown_stages = RegInit(VecInit(Seq.fill(numOfStage)(0.U.asTypeOf(new FrontendTopDownBundle))))
251  dontTouch(topdown_stages)
252
253  // following can only happen on s1
254  val controlRedirectBubble = Wire(Bool())
255  val ControlBTBMissBubble = Wire(Bool())
256  val TAGEMissBubble = Wire(Bool())
257  val SCMissBubble = Wire(Bool())
258  val ITTAGEMissBubble = Wire(Bool())
259  val RASMissBubble = Wire(Bool())
260
261  val memVioRedirectBubble = Wire(Bool())
262  val otherRedirectBubble = Wire(Bool())
263  val btbMissBubble = Wire(Bool())
264  otherRedirectBubble := false.B
265  memVioRedirectBubble := false.B
266
267  // override can happen between s1-s2 and s2-s3
268  val overrideBubble = Wire(Vec(numOfStage - 1, Bool()))
269  def overrideStage = 1
270  // ftq update block can happen on s1, s2 and s3
271  val ftqUpdateBubble = Wire(Vec(numOfStage, Bool()))
272  def ftqUpdateStage = 0
273  // ftq full stall only happens on s3 (last stage)
274  val ftqFullStall = Wire(Bool())
275
276  // by default, no bubble event
277  topdown_stages(0) := 0.U.asTypeOf(new FrontendTopDownBundle)
278  // event movement driven by clock only
279  for (i <- 0 until numOfStage - 1) {
280    topdown_stages(i + 1) := topdown_stages(i)
281  }
282
283
284
285  // ctrl signal
286  predictors.io.ctrl := ctrl
287  predictors.io.reset_vector := io.reset_vector
288
289  val s0_fire, s1_fire, s2_fire, s3_fire = Wire(Bool())
290  val s1_valid, s2_valid, s3_valid = RegInit(false.B)
291  val s1_ready, s2_ready, s3_ready = Wire(Bool())
292  val s1_components_ready, s2_components_ready, s3_components_ready = Wire(Bool())
293
294  val reset_vector = DelayN(io.reset_vector, 5)
295  val s0_pc = Wire(UInt(VAddrBits.W))
296  val s0_pc_reg = RegNext(s0_pc)
297  when (RegNext(RegNext(reset.asBool) && !reset.asBool)) {
298    s0_pc_reg := reset_vector
299  }
300  val s1_pc = RegEnable(s0_pc, s0_fire)
301  val s2_pc = RegEnable(s1_pc, s1_fire)
302  val s3_pc = RegEnable(s2_pc, s2_fire)
303
304  val s0_folded_gh = Wire(new AllFoldedHistories(foldedGHistInfos))
305  val s0_folded_gh_reg = RegNext(s0_folded_gh, 0.U.asTypeOf(s0_folded_gh))
306  val s1_folded_gh = RegEnable(s0_folded_gh, 0.U.asTypeOf(s0_folded_gh), s0_fire)
307  val s2_folded_gh = RegEnable(s1_folded_gh, 0.U.asTypeOf(s0_folded_gh), s1_fire)
308  val s3_folded_gh = RegEnable(s2_folded_gh, 0.U.asTypeOf(s0_folded_gh), s2_fire)
309
310  val s0_last_br_num_oh = Wire(UInt((numBr+1).W))
311  val s0_last_br_num_oh_reg = RegNext(s0_last_br_num_oh, 0.U)
312  val s1_last_br_num_oh = RegEnable(s0_last_br_num_oh, 0.U, s0_fire)
313  val s2_last_br_num_oh = RegEnable(s1_last_br_num_oh, 0.U, s1_fire)
314  val s3_last_br_num_oh = RegEnable(s2_last_br_num_oh, 0.U, s2_fire)
315
316  val s0_ahead_fh_oldest_bits = Wire(new AllAheadFoldedHistoryOldestBits(foldedGHistInfos))
317  val s0_ahead_fh_oldest_bits_reg = RegNext(s0_ahead_fh_oldest_bits, 0.U.asTypeOf(s0_ahead_fh_oldest_bits))
318  val s1_ahead_fh_oldest_bits = RegEnable(s0_ahead_fh_oldest_bits, 0.U.asTypeOf(s0_ahead_fh_oldest_bits), s0_fire)
319  val s2_ahead_fh_oldest_bits = RegEnable(s1_ahead_fh_oldest_bits, 0.U.asTypeOf(s0_ahead_fh_oldest_bits), s1_fire)
320  val s3_ahead_fh_oldest_bits = RegEnable(s2_ahead_fh_oldest_bits, 0.U.asTypeOf(s0_ahead_fh_oldest_bits), s2_fire)
321
322  val npcGen   = new PhyPriorityMuxGenerator[UInt]
323  val foldedGhGen = new PhyPriorityMuxGenerator[AllFoldedHistories]
324  val ghistPtrGen = new PhyPriorityMuxGenerator[CGHPtr]
325  val lastBrNumOHGen = new PhyPriorityMuxGenerator[UInt]
326  val aheadFhObGen = new PhyPriorityMuxGenerator[AllAheadFoldedHistoryOldestBits]
327
328  val ghvBitWriteGens = Seq.tabulate(HistoryLength)(n => new PhyPriorityMuxGenerator[Bool])
329  // val ghistGen = new PhyPriorityMuxGenerator[UInt]
330
331  val ghv = RegInit(0.U.asTypeOf(Vec(HistoryLength, Bool())))
332  val ghv_wire = WireInit(ghv)
333
334  val s0_ghist = WireInit(0.U.asTypeOf(UInt(HistoryLength.W)))
335
336
337  println(f"history buffer length ${HistoryLength}")
338  val ghv_write_datas = Wire(Vec(HistoryLength, Bool()))
339  val ghv_wens = Wire(Vec(HistoryLength, Bool()))
340
341  val s0_ghist_ptr = Wire(new CGHPtr)
342  val s0_ghist_ptr_reg = RegNext(s0_ghist_ptr, 0.U.asTypeOf(new CGHPtr))
343  val s1_ghist_ptr = RegEnable(s0_ghist_ptr, 0.U.asTypeOf(new CGHPtr), s0_fire)
344  val s2_ghist_ptr = RegEnable(s1_ghist_ptr, 0.U.asTypeOf(new CGHPtr), s1_fire)
345  val s3_ghist_ptr = RegEnable(s2_ghist_ptr, 0.U.asTypeOf(new CGHPtr), s2_fire)
346
347  def getHist(ptr: CGHPtr): UInt = (Cat(ghv_wire.asUInt, ghv_wire.asUInt) >> (ptr.value+1.U))(HistoryLength-1, 0)
348  s0_ghist := getHist(s0_ghist_ptr)
349
350  val resp = predictors.io.out
351
352
353  val toFtq_fire = io.bpu_to_ftq.resp.valid && io.bpu_to_ftq.resp.ready
354
355  val s1_flush, s2_flush, s3_flush = Wire(Bool())
356  val s2_redirect, s3_redirect = Wire(Bool())
357
358  // predictors.io := DontCare
359  predictors.io.in.valid := s0_fire
360  predictors.io.in.bits.s0_pc := s0_pc
361  predictors.io.in.bits.ghist := s0_ghist
362  predictors.io.in.bits.folded_hist := s0_folded_gh
363  predictors.io.in.bits.resp_in(0) := (0.U).asTypeOf(new BranchPredictionResp)
364  // predictors.io.in.bits.resp_in(0).s1.pc := s0_pc
365  // predictors.io.in.bits.toFtq_fire := toFtq_fire
366
367  // predictors.io.out.ready := io.bpu_to_ftq.resp.ready
368
369  val redirect_req = io.ftq_to_bpu.redirect
370  val do_redirect = RegNext(redirect_req, 0.U.asTypeOf(io.ftq_to_bpu.redirect))
371
372  // Pipeline logic
373  s2_redirect := false.B
374  s3_redirect := false.B
375
376  s3_flush := redirect_req.valid // flush when redirect comes
377  s2_flush := s3_flush || s3_redirect
378  s1_flush := s2_flush || s2_redirect
379
380  s1_components_ready := predictors.io.s1_ready
381  s1_ready := s1_fire || !s1_valid
382  s0_fire := s1_components_ready && s1_ready
383  predictors.io.s0_fire := s0_fire
384
385  s2_components_ready := predictors.io.s2_ready
386  s2_ready := s2_fire || !s2_valid
387  s1_fire := s1_valid && s2_components_ready && s2_ready && io.bpu_to_ftq.resp.ready
388
389  s3_components_ready := predictors.io.s3_ready
390  s3_ready := s3_fire || !s3_valid
391  s2_fire := s2_valid && s3_components_ready && s3_ready
392
393  when (redirect_req.valid) { s1_valid := false.B }
394    .elsewhen(s0_fire)      { s1_valid := true.B  }
395    .elsewhen(s1_flush)     { s1_valid := false.B }
396    .elsewhen(s1_fire)      { s1_valid := false.B }
397
398  predictors.io.s1_fire := s1_fire
399
400  s2_fire := s2_valid
401
402  when(s2_flush)       { s2_valid := false.B }
403    .elsewhen(s1_fire) { s2_valid := !s1_flush }
404    .elsewhen(s2_fire) { s2_valid := false.B }
405
406  predictors.io.s2_fire := s2_fire
407  predictors.io.s2_redirect := s2_redirect
408
409  s3_fire := s3_valid
410
411  when(s3_flush)       { s3_valid := false.B }
412    .elsewhen(s2_fire) { s3_valid := !s2_flush }
413    .elsewhen(s3_fire) { s3_valid := false.B }
414
415  predictors.io.s3_fire := s3_fire
416  predictors.io.s3_redirect := s3_redirect
417
418
419  io.bpu_to_ftq.resp.valid :=
420    s1_valid && s2_components_ready && s2_ready ||
421    s2_fire && s2_redirect ||
422    s3_fire && s3_redirect
423  io.bpu_to_ftq.resp.bits  := predictors.io.out
424  io.bpu_to_ftq.resp.bits.last_stage_spec_info.folded_hist := s3_folded_gh
425  io.bpu_to_ftq.resp.bits.last_stage_spec_info.histPtr     := s3_ghist_ptr
426  io.bpu_to_ftq.resp.bits.last_stage_spec_info.lastBrNumOH := s3_last_br_num_oh
427  io.bpu_to_ftq.resp.bits.last_stage_spec_info.afhob       := s3_ahead_fh_oldest_bits
428
429  npcGen.register(true.B, s0_pc_reg, Some("stallPC"), 0)
430  foldedGhGen.register(true.B, s0_folded_gh_reg, Some("stallFGH"), 0)
431  ghistPtrGen.register(true.B, s0_ghist_ptr_reg, Some("stallGHPtr"), 0)
432  lastBrNumOHGen.register(true.B, s0_last_br_num_oh_reg, Some("stallBrNumOH"), 0)
433  aheadFhObGen.register(true.B, s0_ahead_fh_oldest_bits_reg, Some("stallAFHOB"), 0)
434
435  // History manage
436  // s1
437  val s1_possible_predicted_ghist_ptrs = (0 to numBr).map(s1_ghist_ptr - _.U)
438  val s1_predicted_ghist_ptr = Mux1H(resp.s1.lastBrPosOH, s1_possible_predicted_ghist_ptrs)
439
440  val s1_possible_predicted_fhs = (0 to numBr).map(i =>
441    s1_folded_gh.update(s1_ahead_fh_oldest_bits, s1_last_br_num_oh, i, resp.s1.brTaken && resp.s1.lastBrPosOH(i)))
442  val s1_predicted_fh = Mux1H(resp.s1.lastBrPosOH, s1_possible_predicted_fhs)
443
444  val s1_ahead_fh_ob_src = Wire(new AllAheadFoldedHistoryOldestBits(foldedGHistInfos))
445  s1_ahead_fh_ob_src.read(ghv, s1_ghist_ptr)
446
447  if (EnableGHistDiff) {
448    val s1_predicted_ghist = WireInit(getHist(s1_predicted_ghist_ptr).asTypeOf(Vec(HistoryLength, Bool())))
449    for (i <- 0 until numBr) {
450      when (resp.s1.shouldShiftVec(i)) {
451        s1_predicted_ghist(i) := resp.s1.brTaken && (i==0).B
452      }
453    }
454    when (s1_valid) {
455      s0_ghist := s1_predicted_ghist.asUInt
456    }
457  }
458
459  val s1_ghv_wens = (0 until HistoryLength).map(n =>
460    (0 until numBr).map(b => (s1_ghist_ptr).value === (CGHPtr(false.B, n.U) + b.U).value && resp.s1.shouldShiftVec(b) && s1_valid))
461  val s1_ghv_wdatas = (0 until HistoryLength).map(n =>
462    Mux1H(
463      (0 until numBr).map(b => (
464        (s1_ghist_ptr).value === (CGHPtr(false.B, n.U) + b.U).value && resp.s1.shouldShiftVec(b),
465        resp.s1.brTaken && resp.s1.lastBrPosOH(b+1)
466      ))
467    )
468  )
469
470  npcGen.register(s1_valid, resp.s1.getTarget, Some("s1_target"), 4)
471  foldedGhGen.register(s1_valid, s1_predicted_fh, Some("s1_FGH"), 4)
472  ghistPtrGen.register(s1_valid, s1_predicted_ghist_ptr, Some("s1_GHPtr"), 4)
473  lastBrNumOHGen.register(s1_valid, resp.s1.lastBrPosOH.asUInt, Some("s1_BrNumOH"), 4)
474  aheadFhObGen.register(s1_valid, s1_ahead_fh_ob_src, Some("s1_AFHOB"), 4)
475  ghvBitWriteGens.zip(s1_ghv_wens).zipWithIndex.map{case ((b, w), i) =>
476    b.register(w.reduce(_||_), s1_ghv_wdatas(i), Some(s"s1_new_bit_$i"), 4)
477  }
478
479  class PreviousPredInfo extends Bundle {
480    val target = UInt(VAddrBits.W)
481    val lastBrPosOH = UInt((numBr+1).W)
482    val taken = Bool()
483    val cfiIndex = UInt(log2Ceil(PredictWidth).W)
484  }
485
486  def preds_needs_redirect_vec(x: PreviousPredInfo, y: BranchPredictionBundle) = {
487    VecInit(
488      x.target =/= y.getTarget,
489      x.lastBrPosOH =/= y.lastBrPosOH.asUInt,
490      x.taken =/= y.taken,
491      (x.taken && y.taken) && x.cfiIndex =/= y.cfiIndex.bits,
492      // x.shouldShiftVec.asUInt =/= y.shouldShiftVec.asUInt,
493      // x.brTaken =/= y.brTaken
494    )
495  }
496
497  // s2
498  val s2_possible_predicted_ghist_ptrs = (0 to numBr).map(s2_ghist_ptr - _.U)
499  val s2_predicted_ghist_ptr = Mux1H(resp.s2.lastBrPosOH, s2_possible_predicted_ghist_ptrs)
500
501  val s2_possible_predicted_fhs = (0 to numBr).map(i =>
502    s2_folded_gh.update(s2_ahead_fh_oldest_bits, s2_last_br_num_oh, i, if (i > 0) resp.s2.full_pred.br_taken_mask(i-1) else false.B))
503  val s2_predicted_fh = Mux1H(resp.s2.lastBrPosOH, s2_possible_predicted_fhs)
504
505  val s2_ahead_fh_ob_src = Wire(new AllAheadFoldedHistoryOldestBits(foldedGHistInfos))
506  s2_ahead_fh_ob_src.read(ghv, s2_ghist_ptr)
507
508  if (EnableGHistDiff) {
509    val s2_predicted_ghist = WireInit(getHist(s2_predicted_ghist_ptr).asTypeOf(Vec(HistoryLength, Bool())))
510    for (i <- 0 until numBr) {
511      when (resp.s2.shouldShiftVec(i)) {
512        s2_predicted_ghist(i) := resp.s2.brTaken && (i==0).B
513      }
514    }
515    when(s2_redirect) {
516      s0_ghist := s2_predicted_ghist.asUInt
517    }
518  }
519
520  val s2_ghv_wens = (0 until HistoryLength).map(n =>
521    (0 until numBr).map(b => (s2_ghist_ptr).value === (CGHPtr(false.B, n.U) + b.U).value && resp.s2.shouldShiftVec(b) && s2_redirect))
522  val s2_ghv_wdatas = (0 until HistoryLength).map(n =>
523    Mux1H(
524      (0 until numBr).map(b => (
525        (s2_ghist_ptr).value === (CGHPtr(false.B, n.U) + b.U).value && resp.s2.shouldShiftVec(b),
526        resp.s2.full_pred.real_br_taken_mask()(b)
527      ))
528    )
529  )
530
531  val s1_pred_info = Wire(new PreviousPredInfo)
532  s1_pred_info.target := resp.s1.getTarget
533  s1_pred_info.lastBrPosOH := resp.s1.lastBrPosOH.asUInt
534  s1_pred_info.taken := resp.s1.taken
535  s1_pred_info.cfiIndex := resp.s1.cfiIndex.bits
536
537  val previous_s1_pred_info = RegEnable(s1_pred_info, init=0.U.asTypeOf(s1_pred_info), s1_fire)
538
539  val s2_redirect_s1_last_pred_vec = preds_needs_redirect_vec(previous_s1_pred_info, resp.s2)
540
541  s2_redirect := s2_fire && s2_redirect_s1_last_pred_vec.reduce(_||_)
542
543  npcGen.register(s2_redirect, resp.s2.getTarget, Some("s2_target"), 5)
544  foldedGhGen.register(s2_redirect, s2_predicted_fh, Some("s2_FGH"), 5)
545  ghistPtrGen.register(s2_redirect, s2_predicted_ghist_ptr, Some("s2_GHPtr"), 5)
546  lastBrNumOHGen.register(s2_redirect, resp.s2.lastBrPosOH.asUInt, Some("s2_BrNumOH"), 5)
547  aheadFhObGen.register(s2_redirect, s2_ahead_fh_ob_src, Some("s2_AFHOB"), 5)
548  ghvBitWriteGens.zip(s2_ghv_wens).zipWithIndex.map{case ((b, w), i) =>
549    b.register(w.reduce(_||_), s2_ghv_wdatas(i), Some(s"s2_new_bit_$i"), 5)
550  }
551
552  XSPerfAccumulate("s2_redirect_because_target_diff", s2_fire && s2_redirect_s1_last_pred_vec(0))
553  XSPerfAccumulate("s2_redirect_because_branch_num_diff", s2_fire && s2_redirect_s1_last_pred_vec(1))
554  XSPerfAccumulate("s2_redirect_because_direction_diff", s2_fire && s2_redirect_s1_last_pred_vec(2))
555  XSPerfAccumulate("s2_redirect_because_cfi_idx_diff", s2_fire && s2_redirect_s1_last_pred_vec(3))
556  // XSPerfAccumulate("s2_redirect_because_shouldShiftVec_diff", s2_fire && s2_redirect_s1_last_pred_vec(4))
557  // XSPerfAccumulate("s2_redirect_because_brTaken_diff", s2_fire && s2_redirect_s1_last_pred_vec(5))
558  XSPerfAccumulate("s2_redirect_because_fallThroughError", s2_fire && resp.s2.fallThruError)
559
560  XSPerfAccumulate("s2_redirect_when_taken", s2_redirect && resp.s2.taken && resp.s2.full_pred.hit)
561  XSPerfAccumulate("s2_redirect_when_not_taken", s2_redirect && !resp.s2.taken && resp.s2.full_pred.hit)
562  XSPerfAccumulate("s2_redirect_when_not_hit", s2_redirect && !resp.s2.full_pred.hit)
563
564
565  // s3
566  val s3_possible_predicted_ghist_ptrs = (0 to numBr).map(s3_ghist_ptr - _.U)
567  val s3_predicted_ghist_ptr = Mux1H(resp.s3.lastBrPosOH, s3_possible_predicted_ghist_ptrs)
568
569  val s3_possible_predicted_fhs = (0 to numBr).map(i =>
570    s3_folded_gh.update(s3_ahead_fh_oldest_bits, s3_last_br_num_oh, i, if (i > 0) resp.s3.full_pred.br_taken_mask(i-1) else false.B))
571  val s3_predicted_fh = Mux1H(resp.s3.lastBrPosOH, s3_possible_predicted_fhs)
572
573  val s3_ahead_fh_ob_src = Wire(new AllAheadFoldedHistoryOldestBits(foldedGHistInfos))
574  s3_ahead_fh_ob_src.read(ghv, s3_ghist_ptr)
575
576  if (EnableGHistDiff) {
577    val s3_predicted_ghist = WireInit(getHist(s3_predicted_ghist_ptr).asTypeOf(Vec(HistoryLength, Bool())))
578    for (i <- 0 until numBr) {
579      when (resp.s3.shouldShiftVec(i)) {
580        s3_predicted_ghist(i) := resp.s3.brTaken && (i==0).B
581      }
582    }
583    when(s3_redirect) {
584      s0_ghist := s3_predicted_ghist.asUInt
585    }
586  }
587
588  val s3_ghv_wens = (0 until HistoryLength).map(n =>
589    (0 until numBr).map(b => (s3_ghist_ptr).value === (CGHPtr(false.B, n.U) + b.U).value && resp.s3.shouldShiftVec(b) && s3_redirect))
590  val s3_ghv_wdatas = (0 until HistoryLength).map(n =>
591    Mux1H(
592      (0 until numBr).map(b => (
593        (s3_ghist_ptr).value === (CGHPtr(false.B, n.U) + b.U).value && resp.s3.shouldShiftVec(b),
594        resp.s3.full_pred.real_br_taken_mask()(b)
595      ))
596    )
597  )
598
599  val previous_s2_pred = RegEnable(resp.s2, 0.U.asTypeOf(resp.s2), s2_fire)
600
601  val s3_redirect_on_br_taken = resp.s3.full_pred.real_br_taken_mask().asUInt =/= previous_s2_pred.full_pred.real_br_taken_mask().asUInt
602  val s3_redirect_on_target = resp.s3.getTarget =/= previous_s2_pred.getTarget
603  val s3_redirect_on_jalr_target = resp.s3.full_pred.hit_taken_on_jalr && resp.s3.full_pred.jalr_target =/= previous_s2_pred.full_pred.jalr_target
604  val s3_redirect_on_fall_thru_error = resp.s3.fallThruError
605
606  s3_redirect := s3_fire && (
607    s3_redirect_on_br_taken || s3_redirect_on_target || s3_redirect_on_fall_thru_error
608  )
609
610  XSPerfAccumulate(f"s3_redirect_on_br_taken", s3_fire && s3_redirect_on_br_taken)
611  XSPerfAccumulate(f"s3_redirect_on_jalr_target", s3_fire && s3_redirect_on_jalr_target)
612  XSPerfAccumulate(f"s3_redirect_on_others", s3_redirect && !(s3_redirect_on_br_taken || s3_redirect_on_jalr_target))
613
614  npcGen.register(s3_redirect, resp.s3.getTarget, Some("s3_target"), 3)
615  foldedGhGen.register(s3_redirect, s3_predicted_fh, Some("s3_FGH"), 3)
616  ghistPtrGen.register(s3_redirect, s3_predicted_ghist_ptr, Some("s3_GHPtr"), 3)
617  lastBrNumOHGen.register(s3_redirect, resp.s3.lastBrPosOH.asUInt, Some("s3_BrNumOH"), 3)
618  aheadFhObGen.register(s3_redirect, s3_ahead_fh_ob_src, Some("s3_AFHOB"), 3)
619  ghvBitWriteGens.zip(s3_ghv_wens).zipWithIndex.map{case ((b, w), i) =>
620    b.register(w.reduce(_||_), s3_ghv_wdatas(i), Some(s"s3_new_bit_$i"), 3)
621  }
622
623  // Send signal tell Ftq override
624  val s2_ftq_idx = RegEnable(io.ftq_to_bpu.enq_ptr, s1_fire)
625  val s3_ftq_idx = RegEnable(s2_ftq_idx, s2_fire)
626
627  io.bpu_to_ftq.resp.bits.s1.valid := s1_fire && !s1_flush
628  io.bpu_to_ftq.resp.bits.s1.hasRedirect := false.B
629  io.bpu_to_ftq.resp.bits.s1.ftq_idx := DontCare
630  io.bpu_to_ftq.resp.bits.s2.valid := s2_fire && !s2_flush
631  io.bpu_to_ftq.resp.bits.s2.hasRedirect := s2_redirect
632  io.bpu_to_ftq.resp.bits.s2.ftq_idx := s2_ftq_idx
633  io.bpu_to_ftq.resp.bits.s3.valid := s3_fire && !s3_flush
634  io.bpu_to_ftq.resp.bits.s3.hasRedirect := s3_redirect
635  io.bpu_to_ftq.resp.bits.s3.ftq_idx := s3_ftq_idx
636
637  val redirect = do_redirect.bits
638
639  predictors.io.update := RegNext(io.ftq_to_bpu.update)
640  predictors.io.update.bits.ghist := RegNext(getHist(io.ftq_to_bpu.update.bits.spec_info.histPtr))
641  predictors.io.redirect := do_redirect
642
643  // Redirect logic
644  val shift = redirect.cfiUpdate.shift
645  val addIntoHist = redirect.cfiUpdate.addIntoHist
646  // TODO: remove these below
647  val shouldShiftVec = Mux(shift === 0.U, VecInit(0.U((1 << (log2Ceil(numBr) + 1)).W).asBools), VecInit((LowerMask(1.U << (shift-1.U))).asBools()))
648  // TODO end
649  val afhob = redirect.cfiUpdate.afhob
650  val lastBrNumOH = redirect.cfiUpdate.lastBrNumOH
651
652
653  val isBr = redirect.cfiUpdate.pd.isBr
654  val taken = redirect.cfiUpdate.taken
655  val real_br_taken_mask = (0 until numBr).map(i => shift === (i+1).U && taken && addIntoHist )
656
657  val oldPtr = redirect.cfiUpdate.histPtr
658  val oldFh = redirect.cfiUpdate.folded_hist
659  val updated_ptr = oldPtr - shift
660  val updated_fh = VecInit((0 to numBr).map(i => oldFh.update(afhob, lastBrNumOH, i, taken && addIntoHist)))(shift)
661  val thisBrNumOH = UIntToOH(shift, numBr+1)
662  val thisAheadFhOb = Wire(new AllAheadFoldedHistoryOldestBits(foldedGHistInfos))
663  thisAheadFhOb.read(ghv, oldPtr)
664  val redirect_ghv_wens = (0 until HistoryLength).map(n =>
665    (0 until numBr).map(b => oldPtr.value === (CGHPtr(false.B, n.U) + b.U).value && shouldShiftVec(b) && do_redirect.valid))
666  val redirect_ghv_wdatas = (0 until HistoryLength).map(n =>
667    Mux1H(
668      (0 until numBr).map(b => oldPtr.value === (CGHPtr(false.B, n.U) + b.U).value && shouldShiftVec(b)),
669      real_br_taken_mask
670    )
671  )
672
673  if (EnableGHistDiff) {
674    val updated_ghist = WireInit(getHist(updated_ptr).asTypeOf(Vec(HistoryLength, Bool())))
675    for (i <- 0 until numBr) {
676      when (shift >= (i+1).U) {
677        updated_ghist(i) := taken && addIntoHist && (i==0).B
678      }
679    }
680    when(do_redirect.valid) {
681      s0_ghist := updated_ghist.asUInt
682    }
683  }
684
685  // Commit time history checker
686  if (EnableCommitGHistDiff) {
687    val commitGHist = RegInit(0.U.asTypeOf(Vec(HistoryLength, Bool())))
688    val commitGHistPtr = RegInit(0.U.asTypeOf(new CGHPtr))
689    def getCommitHist(ptr: CGHPtr): UInt =
690      (Cat(commitGHist.asUInt, commitGHist.asUInt) >> (ptr.value+1.U))(HistoryLength-1, 0)
691
692    val updateValid        : Bool      = io.ftq_to_bpu.update.valid
693    val branchValidMask    : UInt      = io.ftq_to_bpu.update.bits.ftb_entry.brValids.asUInt
694    val branchCommittedMask: Vec[Bool] = io.ftq_to_bpu.update.bits.br_committed
695    val misPredictMask     : UInt      = io.ftq_to_bpu.update.bits.mispred_mask.asUInt
696    val takenMask          : UInt      =
697      io.ftq_to_bpu.update.bits.br_taken_mask.asUInt |
698        io.ftq_to_bpu.update.bits.ftb_entry.always_taken.asUInt // Always taken branch is recorded in history
699    val takenIdx       : UInt = (PriorityEncoder(takenMask) + 1.U((log2Ceil(numBr)+1).W)).asUInt
700    val misPredictIdx  : UInt = (PriorityEncoder(misPredictMask) + 1.U((log2Ceil(numBr)+1).W)).asUInt
701    val shouldShiftMask: UInt = Mux(takenMask.orR,
702        LowerMask(takenIdx).asUInt,
703        ((1 << numBr) - 1).asUInt) &
704      Mux(misPredictMask.orR,
705        LowerMask(misPredictIdx).asUInt,
706        ((1 << numBr) - 1).asUInt) &
707      branchCommittedMask.asUInt
708    val updateShift    : UInt   =
709      Mux(updateValid && branchValidMask.orR, PopCount(branchValidMask & shouldShiftMask), 0.U)
710    dontTouch(updateShift)
711    dontTouch(commitGHist)
712    dontTouch(commitGHistPtr)
713    dontTouch(takenMask)
714    dontTouch(branchValidMask)
715    dontTouch(branchCommittedMask)
716
717    // Maintain the commitGHist
718    for (i <- 0 until numBr) {
719      when(updateShift >= (i + 1).U) {
720        val ptr: CGHPtr = commitGHistPtr - i.asUInt
721        commitGHist(ptr.value) := takenMask(i)
722      }
723    }
724    when(updateValid) {
725      commitGHistPtr := commitGHistPtr - updateShift
726    }
727
728    // Calculate true history using Parallel XOR
729    def computeFoldedHist(hist: UInt, compLen: Int)(histLen: Int): UInt = {
730      if (histLen > 0) {
731        val nChunks     = (histLen + compLen - 1) / compLen
732        val hist_chunks = (0 until nChunks) map { i =>
733          hist(min((i + 1) * compLen, histLen) - 1, i * compLen)
734        }
735        ParallelXOR(hist_chunks)
736      }
737      else 0.U
738    }
739    // Do differential
740    val predictFHistAll: AllFoldedHistories = io.ftq_to_bpu.update.bits.spec_info.folded_hist
741    TageTableInfos.map {
742      case (nRows, histLen, _) => {
743        val nRowsPerBr = nRows / numBr
744        val commitTrueHist: UInt = computeFoldedHist(getCommitHist(commitGHistPtr), log2Ceil(nRowsPerBr))(histLen)
745        val predictFHist         : UInt = predictFHistAll.
746          getHistWithInfo((histLen, min(histLen, log2Ceil(nRowsPerBr)))).folded_hist
747        XSWarn(updateValid && predictFHist =/= commitTrueHist,
748          p"predict time ghist: ${predictFHist} is different from commit time: ${commitTrueHist}\n")
749      }
750    }
751  }
752
753
754  // val updatedGh = oldGh.update(shift, taken && addIntoHist)
755
756  npcGen.register(do_redirect.valid, do_redirect.bits.cfiUpdate.target, Some("redirect_target"), 2)
757  foldedGhGen.register(do_redirect.valid, updated_fh, Some("redirect_FGHT"), 2)
758  ghistPtrGen.register(do_redirect.valid, updated_ptr, Some("redirect_GHPtr"), 2)
759  lastBrNumOHGen.register(do_redirect.valid, thisBrNumOH, Some("redirect_BrNumOH"), 2)
760  aheadFhObGen.register(do_redirect.valid, thisAheadFhOb, Some("redirect_AFHOB"), 2)
761  ghvBitWriteGens.zip(redirect_ghv_wens).zipWithIndex.map{case ((b, w), i) =>
762    b.register(w.reduce(_||_), redirect_ghv_wdatas(i), Some(s"redirect_new_bit_$i"), 2)
763  }
764  // no need to assign s0_last_pred
765
766  // val need_reset = RegNext(reset.asBool) && !reset.asBool
767
768  // Reset
769  // npcGen.register(need_reset, resetVector.U, Some("reset_pc"), 1)
770  // foldedGhGen.register(need_reset, 0.U.asTypeOf(s0_folded_gh), Some("reset_FGH"), 1)
771  // ghistPtrGen.register(need_reset, 0.U.asTypeOf(new CGHPtr), Some("reset_GHPtr"), 1)
772
773  s0_pc         := npcGen()
774  s0_folded_gh  := foldedGhGen()
775  s0_ghist_ptr  := ghistPtrGen()
776  s0_ahead_fh_oldest_bits := aheadFhObGen()
777  s0_last_br_num_oh := lastBrNumOHGen()
778  (ghv_write_datas zip ghvBitWriteGens).map{case (wd, d) => wd := d()}
779  for (i <- 0 until HistoryLength) {
780    ghv_wens(i) := Seq(s1_ghv_wens, s2_ghv_wens, s3_ghv_wens, redirect_ghv_wens).map(_(i).reduce(_||_)).reduce(_||_)
781    when (ghv_wens(i)) {
782      ghv(i) := ghv_write_datas(i)
783    }
784  }
785
786  // TODO: signals for memVio and other Redirects
787  controlRedirectBubble := do_redirect.valid && do_redirect.bits.ControlRedirectBubble
788  ControlBTBMissBubble := do_redirect.bits.ControlBTBMissBubble
789  TAGEMissBubble := do_redirect.bits.TAGEMissBubble
790  SCMissBubble := do_redirect.bits.SCMissBubble
791  ITTAGEMissBubble := do_redirect.bits.ITTAGEMissBubble
792  RASMissBubble := do_redirect.bits.RASMissBubble
793
794  memVioRedirectBubble := do_redirect.valid && do_redirect.bits.MemVioRedirectBubble
795  otherRedirectBubble := do_redirect.valid && do_redirect.bits.OtherRedirectBubble
796  btbMissBubble := do_redirect.valid && do_redirect.bits.BTBMissBubble
797  overrideBubble(0) := s2_redirect
798  overrideBubble(1) := s3_redirect
799  ftqUpdateBubble(0) := !s1_components_ready
800  ftqUpdateBubble(1) := !s2_components_ready
801  ftqUpdateBubble(2) := !s3_components_ready
802  ftqFullStall := !io.bpu_to_ftq.resp.ready
803  io.bpu_to_ftq.resp.bits.topdown_info := topdown_stages(numOfStage - 1)
804
805  // topdown handling logic here
806  when (controlRedirectBubble) {
807    /*
808    for (i <- 0 until numOfStage)
809      topdown_stages(i).reasons(TopDownCounters.ControlRedirectBubble.id) := true.B
810    io.bpu_to_ftq.resp.bits.topdown_info.reasons(TopDownCounters.ControlRedirectBubble.id) := true.B
811    */
812    when (ControlBTBMissBubble) {
813      for (i <- 0 until numOfStage)
814        topdown_stages(i).reasons(TopDownCounters.BTBMissBubble.id) := true.B
815      io.bpu_to_ftq.resp.bits.topdown_info.reasons(TopDownCounters.BTBMissBubble.id) := true.B
816    } .elsewhen (TAGEMissBubble) {
817      for (i <- 0 until numOfStage)
818        topdown_stages(i).reasons(TopDownCounters.TAGEMissBubble.id) := true.B
819      io.bpu_to_ftq.resp.bits.topdown_info.reasons(TopDownCounters.TAGEMissBubble.id) := true.B
820    } .elsewhen (SCMissBubble) {
821      for (i <- 0 until numOfStage)
822        topdown_stages(i).reasons(TopDownCounters.SCMissBubble.id) := true.B
823      io.bpu_to_ftq.resp.bits.topdown_info.reasons(TopDownCounters.SCMissBubble.id) := true.B
824    } .elsewhen (ITTAGEMissBubble) {
825      for (i <- 0 until numOfStage)
826        topdown_stages(i).reasons(TopDownCounters.ITTAGEMissBubble.id) := true.B
827      io.bpu_to_ftq.resp.bits.topdown_info.reasons(TopDownCounters.ITTAGEMissBubble.id) := true.B
828    } .elsewhen (RASMissBubble) {
829      for (i <- 0 until numOfStage)
830        topdown_stages(i).reasons(TopDownCounters.RASMissBubble.id) := true.B
831      io.bpu_to_ftq.resp.bits.topdown_info.reasons(TopDownCounters.RASMissBubble.id) := true.B
832    }
833  }
834  when (memVioRedirectBubble) {
835    for (i <- 0 until numOfStage)
836      topdown_stages(i).reasons(TopDownCounters.MemVioRedirectBubble.id) := true.B
837    io.bpu_to_ftq.resp.bits.topdown_info.reasons(TopDownCounters.MemVioRedirectBubble.id) := true.B
838  }
839  when (otherRedirectBubble) {
840    for (i <- 0 until numOfStage)
841      topdown_stages(i).reasons(TopDownCounters.OtherRedirectBubble.id) := true.B
842    io.bpu_to_ftq.resp.bits.topdown_info.reasons(TopDownCounters.OtherRedirectBubble.id) := true.B
843  }
844  when (btbMissBubble) {
845    for (i <- 0 until numOfStage)
846      topdown_stages(i).reasons(TopDownCounters.BTBMissBubble.id) := true.B
847    io.bpu_to_ftq.resp.bits.topdown_info.reasons(TopDownCounters.BTBMissBubble.id) := true.B
848  }
849
850  for (i <- 0 until numOfStage) {
851    if (i < numOfStage - overrideStage) {
852      when (overrideBubble(i)) {
853        for (j <- 0 to i)
854          topdown_stages(j).reasons(TopDownCounters.OverrideBubble.id) := true.B
855      }
856    }
857    if (i < numOfStage - ftqUpdateStage) {
858      when (ftqUpdateBubble(i)) {
859        topdown_stages(i).reasons(TopDownCounters.FtqUpdateBubble.id) := true.B
860      }
861    }
862  }
863  when (ftqFullStall) {
864    topdown_stages(0).reasons(TopDownCounters.FtqFullStall.id) := true.B
865  }
866
867  XSError(isBefore(redirect.cfiUpdate.histPtr, s3_ghist_ptr) && do_redirect.valid, p"s3_ghist_ptr ${s3_ghist_ptr} exceeds redirect histPtr ${redirect.cfiUpdate.histPtr}\n")
868  XSError(isBefore(redirect.cfiUpdate.histPtr, s2_ghist_ptr) && do_redirect.valid, p"s2_ghist_ptr ${s2_ghist_ptr} exceeds redirect histPtr ${redirect.cfiUpdate.histPtr}\n")
869  XSError(isBefore(redirect.cfiUpdate.histPtr, s1_ghist_ptr) && do_redirect.valid, p"s1_ghist_ptr ${s1_ghist_ptr} exceeds redirect histPtr ${redirect.cfiUpdate.histPtr}\n")
870
871  XSDebug(RegNext(reset.asBool) && !reset.asBool, "Reseting...\n")
872  XSDebug(io.ftq_to_bpu.update.valid, p"Update from ftq\n")
873  XSDebug(io.ftq_to_bpu.redirect.valid, p"Redirect from ftq\n")
874
875  XSDebug("[BP0]                 fire=%d                      pc=%x\n", s0_fire, s0_pc)
876  XSDebug("[BP1] v=%d r=%d cr=%d fire=%d             flush=%d pc=%x\n",
877    s1_valid, s1_ready, s1_components_ready, s1_fire, s1_flush, s1_pc)
878  XSDebug("[BP2] v=%d r=%d cr=%d fire=%d redirect=%d flush=%d pc=%x\n",
879  s2_valid, s2_ready, s2_components_ready, s2_fire, s2_redirect, s2_flush, s2_pc)
880  XSDebug("[BP3] v=%d r=%d cr=%d fire=%d redirect=%d flush=%d pc=%x\n",
881  s3_valid, s3_ready, s3_components_ready, s3_fire, s3_redirect, s3_flush, s3_pc)
882  XSDebug("[FTQ] ready=%d\n", io.bpu_to_ftq.resp.ready)
883  XSDebug("resp.s1.target=%x\n", resp.s1.getTarget)
884  XSDebug("resp.s2.target=%x\n", resp.s2.getTarget)
885  // XSDebug("s0_ghist: %b\n", s0_ghist.predHist)
886  // XSDebug("s1_ghist: %b\n", s1_ghist.predHist)
887  // XSDebug("s2_ghist: %b\n", s2_ghist.predHist)
888  // XSDebug("s2_predicted_ghist: %b\n", s2_predicted_ghist.predHist)
889  XSDebug(p"s0_ghist_ptr: $s0_ghist_ptr\n")
890  XSDebug(p"s1_ghist_ptr: $s1_ghist_ptr\n")
891  XSDebug(p"s2_ghist_ptr: $s2_ghist_ptr\n")
892  XSDebug(p"s3_ghist_ptr: $s3_ghist_ptr\n")
893
894  io.ftq_to_bpu.update.bits.display(io.ftq_to_bpu.update.valid)
895  io.ftq_to_bpu.redirect.bits.display(io.ftq_to_bpu.redirect.valid)
896
897
898  XSPerfAccumulate("s2_redirect", s2_redirect)
899  XSPerfAccumulate("s3_redirect", s3_redirect)
900  XSPerfAccumulate("s1_not_valid", !s1_valid)
901
902  val perfEvents = predictors.asInstanceOf[Composer].getPerfEvents
903  generatePerfEvent()
904}
905