xref: /XiangShan/src/main/scala/xiangshan/frontend/FTB.scala (revision 8891a219bbc84f568e1d134854d8d5ed86d6d560)
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 org.chipsalliance.cde.config.Parameters
20import chisel3._
21import chisel3.util._
22import xiangshan._
23import utils._
24import utility._
25
26import scala.math.min
27import scala.{Tuple2 => &}
28import os.copy
29
30
31trait FTBParams extends HasXSParameter with HasBPUConst {
32  val numEntries = FtbSize
33  val numWays    = FtbWays
34  val numSets    = numEntries/numWays // 512
35  val tagSize    = 20
36
37
38
39  val TAR_STAT_SZ = 2
40  def TAR_FIT = 0.U(TAR_STAT_SZ.W)
41  def TAR_OVF = 1.U(TAR_STAT_SZ.W)
42  def TAR_UDF = 2.U(TAR_STAT_SZ.W)
43
44  def BR_OFFSET_LEN = 12
45  def JMP_OFFSET_LEN = 20
46}
47
48class FtbSlot(val offsetLen: Int, val subOffsetLen: Option[Int] = None)(implicit p: Parameters) extends XSBundle with FTBParams {
49  if (subOffsetLen.isDefined) {
50    require(subOffsetLen.get <= offsetLen)
51  }
52  val offset  = UInt(log2Ceil(PredictWidth).W)
53  val lower   = UInt(offsetLen.W)
54  val tarStat = UInt(TAR_STAT_SZ.W)
55  val sharing = Bool()
56  val valid   = Bool()
57
58  val sc      = Bool() // set by sc in s3, perf use only
59
60  def setLowerStatByTarget(pc: UInt, target: UInt, isShare: Boolean) = {
61    def getTargetStatByHigher(pc_higher: UInt, target_higher: UInt) =
62      Mux(target_higher > pc_higher, TAR_OVF,
63        Mux(target_higher < pc_higher, TAR_UDF, TAR_FIT))
64    def getLowerByTarget(target: UInt, offsetLen: Int) = target(offsetLen, 1)
65    val offLen = if (isShare) this.subOffsetLen.get else this.offsetLen
66    val pc_higher = pc(VAddrBits-1, offLen+1)
67    val target_higher = target(VAddrBits-1, offLen+1)
68    val stat = getTargetStatByHigher(pc_higher, target_higher)
69    val lower = ZeroExt(getLowerByTarget(target, offLen), this.offsetLen)
70    this.lower := lower
71    this.tarStat := stat
72    this.sharing := isShare.B
73  }
74
75  def getTarget(pc: UInt, last_stage: Option[Tuple2[UInt, Bool]] = None) = {
76    def getTarget(offLen: Int)(pc: UInt, lower: UInt, stat: UInt,
77      last_stage: Option[Tuple2[UInt, Bool]] = None) = {
78      val h                = pc(VAddrBits - 1, offLen + 1)
79      val higher           = Wire(UInt((VAddrBits - offLen - 1).W))
80      val higher_plus_one  = Wire(UInt((VAddrBits - offLen - 1).W))
81      val higher_minus_one = Wire(UInt((VAddrBits-offLen-1).W))
82
83      // Switch between previous stage pc and current stage pc
84      // Give flexibility for timing
85      if (last_stage.isDefined) {
86        val last_stage_pc = last_stage.get._1
87        val last_stage_pc_h = last_stage_pc(VAddrBits-1, offLen+1)
88        val stage_en = last_stage.get._2
89        higher := RegEnable(last_stage_pc_h, stage_en)
90        higher_plus_one := RegEnable(last_stage_pc_h+1.U, stage_en)
91        higher_minus_one := RegEnable(last_stage_pc_h-1.U, stage_en)
92      } else {
93        higher := h
94        higher_plus_one := h + 1.U
95        higher_minus_one := h - 1.U
96      }
97      val target =
98        Cat(
99          Mux1H(Seq(
100            (stat === TAR_OVF, higher_plus_one),
101            (stat === TAR_UDF, higher_minus_one),
102            (stat === TAR_FIT, higher),
103          )),
104          lower(offLen-1, 0), 0.U(1.W)
105        )
106      require(target.getWidth == VAddrBits)
107      require(offLen != 0)
108      target
109    }
110    if (subOffsetLen.isDefined)
111      Mux(sharing,
112        getTarget(subOffsetLen.get)(pc, lower, tarStat, last_stage),
113        getTarget(offsetLen)(pc, lower, tarStat, last_stage)
114      )
115    else
116      getTarget(offsetLen)(pc, lower, tarStat, last_stage)
117  }
118  def fromAnotherSlot(that: FtbSlot) = {
119    require(
120      this.offsetLen > that.offsetLen && this.subOffsetLen.map(_ == that.offsetLen).getOrElse(true) ||
121      this.offsetLen == that.offsetLen
122    )
123    this.offset := that.offset
124    this.tarStat := that.tarStat
125    this.sharing := (this.offsetLen > that.offsetLen && that.offsetLen == this.subOffsetLen.get).B
126    this.valid := that.valid
127    this.lower := ZeroExt(that.lower, this.offsetLen)
128  }
129
130}
131
132class FTBEntry(implicit p: Parameters) extends XSBundle with FTBParams with BPUUtils {
133
134
135  val valid       = Bool()
136
137  val brSlots = Vec(numBrSlot, new FtbSlot(BR_OFFSET_LEN))
138
139  val tailSlot = new FtbSlot(JMP_OFFSET_LEN, Some(BR_OFFSET_LEN))
140
141  // Partial Fall-Through Address
142  val pftAddr     = UInt(log2Up(PredictWidth).W)
143  val carry       = Bool()
144
145  val isCall      = Bool()
146  val isRet       = Bool()
147  val isJalr      = Bool()
148
149  val last_may_be_rvi_call = Bool()
150
151  val always_taken = Vec(numBr, Bool())
152
153  def getSlotForBr(idx: Int): FtbSlot = {
154    require(idx <= numBr-1)
155    (idx, numBr) match {
156      case (i, n) if i == n-1 => this.tailSlot
157      case _ => this.brSlots(idx)
158    }
159  }
160  def allSlotsForBr = {
161    (0 until numBr).map(getSlotForBr(_))
162  }
163  def setByBrTarget(brIdx: Int, pc: UInt, target: UInt) = {
164    val slot = getSlotForBr(brIdx)
165    slot.setLowerStatByTarget(pc, target, brIdx == numBr-1)
166  }
167  def setByJmpTarget(pc: UInt, target: UInt) = {
168    this.tailSlot.setLowerStatByTarget(pc, target, false)
169  }
170
171  def getTargetVec(pc: UInt, last_stage: Option[Tuple2[UInt, Bool]] = None) = {
172    VecInit((brSlots :+ tailSlot).map(_.getTarget(pc, last_stage)))
173  }
174
175  def getOffsetVec = VecInit(brSlots.map(_.offset) :+ tailSlot.offset)
176  def isJal = !isJalr
177  def getFallThrough(pc: UInt, last_stage_entry: Option[Tuple2[FTBEntry, Bool]] = None) = {
178    if (last_stage_entry.isDefined) {
179      var stashed_carry = RegEnable(last_stage_entry.get._1.carry, last_stage_entry.get._2)
180      getFallThroughAddr(pc, stashed_carry, pftAddr)
181    } else {
182      getFallThroughAddr(pc, carry, pftAddr)
183    }
184  }
185
186  def hasBr(offset: UInt) =
187    brSlots.map{ s => s.valid && s.offset <= offset}.reduce(_||_) ||
188    (tailSlot.valid && tailSlot.offset <= offset && tailSlot.sharing)
189
190  def getBrMaskByOffset(offset: UInt) =
191    brSlots.map{ s => s.valid && s.offset <= offset } :+
192    (tailSlot.valid && tailSlot.offset <= offset && tailSlot.sharing)
193
194  def getBrRecordedVec(offset: UInt) = {
195    VecInit(
196      brSlots.map(s => s.valid && s.offset === offset) :+
197      (tailSlot.valid && tailSlot.offset === offset && tailSlot.sharing)
198    )
199  }
200
201  def brIsSaved(offset: UInt) = getBrRecordedVec(offset).reduce(_||_)
202
203  def brValids = {
204    VecInit(
205      brSlots.map(_.valid) :+ (tailSlot.valid && tailSlot.sharing)
206    )
207  }
208
209  def noEmptySlotForNewBr = {
210    VecInit(brSlots.map(_.valid) :+ tailSlot.valid).reduce(_&&_)
211  }
212
213  def newBrCanNotInsert(offset: UInt) = {
214    val lastSlotForBr = tailSlot
215    lastSlotForBr.valid && lastSlotForBr.offset < offset
216  }
217
218  def jmpValid = {
219    tailSlot.valid && !tailSlot.sharing
220  }
221
222  def brOffset = {
223    VecInit(brSlots.map(_.offset) :+ tailSlot.offset)
224  }
225
226  def display(cond: Bool): Unit = {
227    XSDebug(cond, p"-----------FTB entry----------- \n")
228    XSDebug(cond, p"v=${valid}\n")
229    for(i <- 0 until numBr) {
230      XSDebug(cond, p"[br$i]: v=${allSlotsForBr(i).valid}, offset=${allSlotsForBr(i).offset}," +
231        p"lower=${Hexadecimal(allSlotsForBr(i).lower)}\n")
232    }
233    XSDebug(cond, p"[tailSlot]: v=${tailSlot.valid}, offset=${tailSlot.offset}," +
234      p"lower=${Hexadecimal(tailSlot.lower)}, sharing=${tailSlot.sharing}}\n")
235    XSDebug(cond, p"pftAddr=${Hexadecimal(pftAddr)}, carry=$carry\n")
236    XSDebug(cond, p"isCall=$isCall, isRet=$isRet, isjalr=$isJalr\n")
237    XSDebug(cond, p"last_may_be_rvi_call=$last_may_be_rvi_call\n")
238    XSDebug(cond, p"------------------------------- \n")
239  }
240
241}
242
243class FTBEntryWithTag(implicit p: Parameters) extends XSBundle with FTBParams with BPUUtils {
244  val entry = new FTBEntry
245  val tag = UInt(tagSize.W)
246  def display(cond: Bool): Unit = {
247    entry.display(cond)
248    XSDebug(cond, p"tag is ${Hexadecimal(tag)}\n------------------------------- \n")
249  }
250}
251
252class FTBMeta(implicit p: Parameters) extends XSBundle with FTBParams {
253  val writeWay = UInt(log2Ceil(numWays).W)
254  val hit = Bool()
255  val pred_cycle = if (!env.FPGAPlatform) Some(UInt(64.W)) else None
256}
257
258object FTBMeta {
259  def apply(writeWay: UInt, hit: Bool, pred_cycle: UInt)(implicit p: Parameters): FTBMeta = {
260    val e = Wire(new FTBMeta)
261    e.writeWay := writeWay
262    e.hit := hit
263    e.pred_cycle.map(_ := pred_cycle)
264    e
265  }
266}
267
268// class UpdateQueueEntry(implicit p: Parameters) extends XSBundle with FTBParams {
269//   val pc = UInt(VAddrBits.W)
270//   val ftb_entry = new FTBEntry
271//   val hit = Bool()
272//   val hit_way = UInt(log2Ceil(numWays).W)
273// }
274//
275// object UpdateQueueEntry {
276//   def apply(pc: UInt, fe: FTBEntry, hit: Bool, hit_way: UInt)(implicit p: Parameters): UpdateQueueEntry = {
277//     val e = Wire(new UpdateQueueEntry)
278//     e.pc := pc
279//     e.ftb_entry := fe
280//     e.hit := hit
281//     e.hit_way := hit_way
282//     e
283//   }
284// }
285
286class FTB(implicit p: Parameters) extends BasePredictor with FTBParams with BPUUtils
287  with HasCircularQueuePtrHelper with HasPerfEvents {
288  override val meta_size = WireInit(0.U.asTypeOf(new FTBMeta)).getWidth
289
290  val ftbAddr = new TableAddr(log2Up(numSets), 1)
291
292  class FTBBank(val numSets: Int, val nWays: Int) extends XSModule with BPUUtils {
293    val io = IO(new Bundle {
294      val s1_fire = Input(Bool())
295
296      // when ftb hit, read_hits.valid is true, and read_hits.bits is OH of hit way
297      // when ftb not hit, read_hits.valid is false, and read_hits is OH of allocWay
298      // val read_hits = Valid(Vec(numWays, Bool()))
299      val req_pc = Flipped(DecoupledIO(UInt(VAddrBits.W)))
300      val read_resp = Output(new FTBEntry)
301      val read_hits = Valid(UInt(log2Ceil(numWays).W))
302
303      val u_req_pc = Flipped(DecoupledIO(UInt(VAddrBits.W)))
304      val update_hits = Valid(UInt(log2Ceil(numWays).W))
305      val update_access = Input(Bool())
306
307      val update_pc = Input(UInt(VAddrBits.W))
308      val update_write_data = Flipped(Valid(new FTBEntryWithTag))
309      val update_write_way = Input(UInt(log2Ceil(numWays).W))
310      val update_write_alloc = Input(Bool())
311    })
312
313    // Extract holdRead logic to fix bug that update read override predict read result
314    val ftb = Module(new SRAMTemplate(new FTBEntryWithTag, set = numSets, way = numWays, shouldReset = true, holdRead = false, singlePort = true))
315    val ftb_r_entries = ftb.io.r.resp.data.map(_.entry)
316
317    val pred_rdata   = HoldUnless(ftb.io.r.resp.data, RegNext(io.req_pc.valid && !io.update_access))
318    ftb.io.r.req.valid := io.req_pc.valid || io.u_req_pc.valid // io.s0_fire
319    ftb.io.r.req.bits.setIdx := Mux(io.u_req_pc.valid, ftbAddr.getIdx(io.u_req_pc.bits), ftbAddr.getIdx(io.req_pc.bits)) // s0_idx
320
321    assert(!(io.req_pc.valid && io.u_req_pc.valid))
322
323    io.req_pc.ready := ftb.io.r.req.ready
324    io.u_req_pc.ready := ftb.io.r.req.ready
325
326    val req_tag = RegEnable(ftbAddr.getTag(io.req_pc.bits)(tagSize-1, 0), io.req_pc.valid)
327    val req_idx = RegEnable(ftbAddr.getIdx(io.req_pc.bits), io.req_pc.valid)
328
329    val u_req_tag = RegEnable(ftbAddr.getTag(io.u_req_pc.bits)(tagSize-1, 0), io.u_req_pc.valid)
330
331    val read_entries = pred_rdata.map(_.entry)
332    val read_tags    = pred_rdata.map(_.tag)
333
334    val total_hits = VecInit((0 until numWays).map(b => read_tags(b) === req_tag && read_entries(b).valid && io.s1_fire))
335    val hit = total_hits.reduce(_||_)
336    // val hit_way_1h = VecInit(PriorityEncoderOH(total_hits))
337    val hit_way = OHToUInt(total_hits)
338
339    val u_total_hits = VecInit((0 until numWays).map(b =>
340        ftb.io.r.resp.data(b).tag === u_req_tag && ftb.io.r.resp.data(b).entry.valid && RegNext(io.update_access)))
341    val u_hit = u_total_hits.reduce(_||_)
342    // val hit_way_1h = VecInit(PriorityEncoderOH(total_hits))
343    val u_hit_way = OHToUInt(u_total_hits)
344
345    // assert(PopCount(total_hits) === 1.U || PopCount(total_hits) === 0.U)
346    // assert(PopCount(u_total_hits) === 1.U || PopCount(u_total_hits) === 0.U)
347    for (n <- 1 to numWays) {
348      XSPerfAccumulate(f"ftb_pred_${n}_way_hit", PopCount(total_hits) === n.U)
349      XSPerfAccumulate(f"ftb_update_${n}_way_hit", PopCount(u_total_hits) === n.U)
350    }
351
352    val replacer = ReplacementPolicy.fromString(Some("setplru"), numWays, numSets)
353    // val allocWriteWay = replacer.way(req_idx)
354
355    val touch_set = Seq.fill(1)(Wire(UInt(log2Ceil(numSets).W)))
356    val touch_way = Seq.fill(1)(Wire(Valid(UInt(log2Ceil(numWays).W))))
357
358    val write_set = Wire(UInt(log2Ceil(numSets).W))
359    val write_way = Wire(Valid(UInt(log2Ceil(numWays).W)))
360
361    val read_set = Wire(UInt(log2Ceil(numSets).W))
362    val read_way = Wire(Valid(UInt(log2Ceil(numWays).W)))
363
364    read_set := req_idx
365    read_way.valid := hit
366    read_way.bits  := hit_way
367
368    // Read replacer access is postponed for 1 cycle
369    // this helps timing
370    touch_set(0) := Mux(write_way.valid, write_set, RegNext(read_set))
371    touch_way(0).valid := write_way.valid || RegNext(read_way.valid)
372    touch_way(0).bits := Mux(write_way.valid, write_way.bits, RegNext(read_way.bits))
373
374    replacer.access(touch_set, touch_way)
375
376    // Select the update allocate way
377    // Selection logic:
378    //    1. if any entries within the same index is not valid, select it
379    //    2. if all entries is valid, use replacer
380    def allocWay(valids: UInt, idx: UInt): UInt = {
381      if (numWays > 1) {
382        val w = Wire(UInt(log2Up(numWays).W))
383        val valid = WireInit(valids.andR)
384        w := Mux(valid, replacer.way(idx), PriorityEncoder(~valids))
385        w
386      } else {
387        val w = WireInit(0.U(log2Up(numWays).W))
388        w
389      }
390    }
391
392    io.read_resp := Mux1H(total_hits, read_entries) // Mux1H
393    io.read_hits.valid := hit
394    io.read_hits.bits := hit_way
395
396    io.update_hits.valid := u_hit
397    io.update_hits.bits := u_hit_way
398
399    // Update logic
400    val u_valid = io.update_write_data.valid
401    val u_data = io.update_write_data.bits
402    val u_idx = ftbAddr.getIdx(io.update_pc)
403    val allocWriteWay = allocWay(RegNext(VecInit(ftb_r_entries.map(_.valid))).asUInt, u_idx)
404    val u_way = Mux(io.update_write_alloc, allocWriteWay, io.update_write_way)
405    val u_mask = UIntToOH(u_way)
406
407    for (i <- 0 until numWays) {
408      XSPerfAccumulate(f"ftb_replace_way$i", u_valid && io.update_write_alloc && u_way === i.U)
409      XSPerfAccumulate(f"ftb_replace_way${i}_has_empty", u_valid && io.update_write_alloc && !ftb_r_entries.map(_.valid).reduce(_&&_) && u_way === i.U)
410      XSPerfAccumulate(f"ftb_hit_way$i", hit && !io.update_access && hit_way === i.U)
411    }
412
413    ftb.io.w.apply(u_valid, u_data, u_idx, u_mask)
414
415    // for replacer
416    write_set := u_idx
417    write_way.valid := u_valid
418    write_way.bits := Mux(io.update_write_alloc, allocWriteWay, io.update_write_way)
419
420    // print hit entry info
421    Mux1H(total_hits, ftb.io.r.resp.data).display(true.B)
422  } // FTBBank
423
424  val ftbBank = Module(new FTBBank(numSets, numWays))
425
426  ftbBank.io.req_pc.valid := io.s0_fire(0)
427  ftbBank.io.req_pc.bits := s0_pc_dup(0)
428
429  val btb_enable_dup = dup(RegNext(io.ctrl.btb_enable))
430  val s2_ftb_entry_dup = io.s1_fire.map(f => RegEnable(ftbBank.io.read_resp, f))
431  val s3_ftb_entry_dup = io.s2_fire.zip(s2_ftb_entry_dup).map {case (f, e) => RegEnable(e, f)}
432
433  val s1_hit = ftbBank.io.read_hits.valid && io.ctrl.btb_enable
434  val s2_hit_dup = io.s1_fire.map(f => RegEnable(s1_hit, 0.B, f))
435  val s3_hit_dup = io.s2_fire.zip(s2_hit_dup).map {case (f, h) => RegEnable(h, 0.B, f)}
436  val writeWay = ftbBank.io.read_hits.bits
437
438  // io.out.bits.resp := RegEnable(io.in.bits.resp_in(0), 0.U.asTypeOf(new BranchPredictionResp), io.s1_fire)
439  io.out := io.in.bits.resp_in(0)
440
441  io.out.s2.full_pred.zip(s2_hit_dup).map {case (fp, h) => fp.hit := h}
442  io.out.s2.pc                  := s2_pc_dup
443  for (full_pred & s2_ftb_entry & s2_pc & s1_pc & s1_fire <-
444    io.out.s2.full_pred zip s2_ftb_entry_dup zip s2_pc_dup zip s1_pc_dup zip io.s1_fire) {
445      full_pred.fromFtbEntry(s2_ftb_entry,
446        s2_pc,
447        // Previous stage meta for better timing
448        Some(s1_pc, s1_fire),
449        Some(ftbBank.io.read_resp, s1_fire)
450      )
451  }
452
453  io.out.s3.full_pred.zip(s3_hit_dup).map {case (fp, h) => fp.hit := h}
454  io.out.s3.pc                  := s3_pc_dup
455  for (full_pred & s3_ftb_entry & s3_pc & s2_pc & s2_fire <-
456    io.out.s3.full_pred zip s3_ftb_entry_dup zip s3_pc_dup zip s2_pc_dup zip io.s2_fire)
457      full_pred.fromFtbEntry(s3_ftb_entry, s3_pc, Some((s2_pc, s2_fire)))
458
459  io.out.last_stage_ftb_entry := s3_ftb_entry_dup(0)
460  io.out.last_stage_meta := RegEnable(RegEnable(FTBMeta(writeWay.asUInt, s1_hit, GTimer()).asUInt, io.s1_fire(0)), io.s2_fire(0))
461
462  // always taken logic
463  for (i <- 0 until numBr) {
464    for (out_fp & in_fp & s2_hit & s2_ftb_entry <-
465      io.out.s2.full_pred zip io.in.bits.resp_in(0).s2.full_pred zip s2_hit_dup zip s2_ftb_entry_dup)
466      out_fp.br_taken_mask(i) := in_fp.br_taken_mask(i) || s2_hit && s2_ftb_entry.always_taken(i)
467    for (out_fp & in_fp & s3_hit & s3_ftb_entry <-
468      io.out.s3.full_pred zip io.in.bits.resp_in(0).s3.full_pred zip s3_hit_dup zip s3_ftb_entry_dup)
469      out_fp.br_taken_mask(i) := in_fp.br_taken_mask(i) || s3_hit && s3_ftb_entry.always_taken(i)
470  }
471
472  // Update logic
473  val update = io.update.bits
474
475  val u_meta = update.meta.asTypeOf(new FTBMeta)
476  val u_valid = io.update.valid && !io.update.bits.old_entry
477
478  val delay2_pc = DelayN(update.pc, 2)
479  val delay2_entry = DelayN(update.ftb_entry, 2)
480
481
482  val update_now = u_valid && u_meta.hit
483  val update_need_read = u_valid && !u_meta.hit
484  // stall one more cycle because we use a whole cycle to do update read tag hit
485  io.s1_ready := ftbBank.io.req_pc.ready && !(update_need_read) && !RegNext(update_need_read)
486
487  ftbBank.io.u_req_pc.valid := update_need_read
488  ftbBank.io.u_req_pc.bits := update.pc
489
490
491
492  val ftb_write = Wire(new FTBEntryWithTag)
493  ftb_write.entry := Mux(update_now, update.ftb_entry, delay2_entry)
494  ftb_write.tag   := ftbAddr.getTag(Mux(update_now, update.pc, delay2_pc))(tagSize-1, 0)
495
496  val write_valid = update_now || DelayN(u_valid && !u_meta.hit, 2)
497
498  ftbBank.io.update_write_data.valid := write_valid
499  ftbBank.io.update_write_data.bits := ftb_write
500  ftbBank.io.update_pc          := Mux(update_now, update.pc,       delay2_pc)
501  ftbBank.io.update_write_way   := Mux(update_now, u_meta.writeWay, RegNext(ftbBank.io.update_hits.bits)) // use it one cycle later
502  ftbBank.io.update_write_alloc := Mux(update_now, false.B,         RegNext(!ftbBank.io.update_hits.valid)) // use it one cycle later
503  ftbBank.io.update_access := u_valid && !u_meta.hit
504  ftbBank.io.s1_fire := io.s1_fire(0)
505
506  XSDebug("req_v=%b, req_pc=%x, ready=%b (resp at next cycle)\n", io.s0_fire(0), s0_pc_dup(0), ftbBank.io.req_pc.ready)
507  XSDebug("s2_hit=%b, hit_way=%b\n", s2_hit_dup(0), writeWay.asUInt)
508  XSDebug("s2_br_taken_mask=%b, s2_real_taken_mask=%b\n",
509    io.in.bits.resp_in(0).s2.full_pred(0).br_taken_mask.asUInt, io.out.s2.full_pred(0).real_slot_taken_mask().asUInt)
510  XSDebug("s2_target=%x\n", io.out.s2.getTarget(0))
511
512  s2_ftb_entry_dup(0).display(true.B)
513
514  XSPerfAccumulate("ftb_read_hits", RegNext(io.s0_fire(0)) && s1_hit)
515  XSPerfAccumulate("ftb_read_misses", RegNext(io.s0_fire(0)) && !s1_hit)
516
517  XSPerfAccumulate("ftb_commit_hits", io.update.valid && u_meta.hit)
518  XSPerfAccumulate("ftb_commit_misses", io.update.valid && !u_meta.hit)
519
520  XSPerfAccumulate("ftb_update_req", io.update.valid)
521  XSPerfAccumulate("ftb_update_ignored", io.update.valid && io.update.bits.old_entry)
522  XSPerfAccumulate("ftb_updated", u_valid)
523
524  override val perfEvents = Seq(
525    ("ftb_commit_hits            ", io.update.valid  &&  u_meta.hit),
526    ("ftb_commit_misses          ", io.update.valid  && !u_meta.hit),
527  )
528  generatePerfEvent()
529}
530