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