xref: /XiangShan/src/main/scala/xiangshan/backend/rename/Rename.scala (revision 211d620b07edb797ba35b635d24fef4e7294bae2)
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.backend.rename
18
19import org.chipsalliance.cde.config.Parameters
20import chisel3._
21import chisel3.util._
22import utility._
23import utils._
24import xiangshan._
25import xiangshan.backend.Bundles.{DecodedInst, DynInst}
26import xiangshan.backend.decode.{FusionDecodeInfo, ImmUnion, Imm_I, Imm_LUI_LOAD, Imm_U}
27import xiangshan.backend.fu.FuType
28import xiangshan.backend.rename.freelist._
29import xiangshan.backend.rob.{RobEnqIO, RobPtr}
30import xiangshan.mem.mdp._
31import xiangshan.ExceptionNO._
32import xiangshan.backend.fu.FuType._
33import xiangshan.mem.{EewLog2, GenUSWholeEmul}
34import xiangshan.mem.GenRealFlowNum
35import xiangshan.backend.trace._
36import xiangshan.backend.decode.isa.bitfield.{OPCODE5Bit, XSInstBitFields}
37import xiangshan.backend.fu.util.CSRConst
38
39class Rename(implicit p: Parameters) extends XSModule with HasCircularQueuePtrHelper with HasPerfEvents {
40
41  // params alias
42  private val numRegSrc = backendParams.numRegSrc
43  private val numVecRegSrc = backendParams.numVecRegSrc
44  private val numVecRatPorts = numVecRegSrc
45
46  println(s"[Rename] numRegSrc: $numRegSrc")
47
48  val io = IO(new Bundle() {
49    val redirect = Flipped(ValidIO(new Redirect))
50    val rabCommits = Input(new RabCommitIO)
51    // from csr
52    val singleStep = Input(Bool())
53    // from decode
54    val in = Vec(RenameWidth, Flipped(DecoupledIO(new DecodedInst)))
55    val fusionInfo = Vec(DecodeWidth - 1, Flipped(new FusionDecodeInfo))
56    // ssit read result
57    val ssit = Flipped(Vec(RenameWidth, Output(new SSITEntry)))
58    // waittable read result
59    val waittable = Flipped(Vec(RenameWidth, Output(Bool())))
60    // to rename table
61    val intReadPorts = Vec(RenameWidth, Vec(2, Input(UInt(PhyRegIdxWidth.W))))
62    val fpReadPorts = Vec(RenameWidth, Vec(3, Input(UInt(PhyRegIdxWidth.W))))
63    val vecReadPorts = Vec(RenameWidth, Vec(numVecRatPorts, Input(UInt(PhyRegIdxWidth.W))))
64    val v0ReadPorts = Vec(RenameWidth, Vec(1, Input(UInt(PhyRegIdxWidth.W))))
65    val vlReadPorts = Vec(RenameWidth, Vec(1, Input(UInt(PhyRegIdxWidth.W))))
66    val intRenamePorts = Vec(RenameWidth, Output(new RatWritePort(log2Ceil(IntLogicRegs))))
67    val fpRenamePorts = Vec(RenameWidth, Output(new RatWritePort(log2Ceil(FpLogicRegs))))
68    val vecRenamePorts = Vec(RenameWidth, Output(new RatWritePort(log2Ceil(VecLogicRegs))))
69    val v0RenamePorts = Vec(RenameWidth, Output(new RatWritePort(log2Ceil(V0LogicRegs))))
70    val vlRenamePorts = Vec(RenameWidth, Output(new RatWritePort(log2Ceil(VlLogicRegs))))
71    // from rename table
72    val int_old_pdest = Vec(RabCommitWidth, Input(UInt(PhyRegIdxWidth.W)))
73    val fp_old_pdest = Vec(RabCommitWidth, Input(UInt(PhyRegIdxWidth.W)))
74    val vec_old_pdest = Vec(RabCommitWidth, Input(UInt(PhyRegIdxWidth.W)))
75    val v0_old_pdest = Vec(RabCommitWidth, Input(UInt(PhyRegIdxWidth.W)))
76    val vl_old_pdest = Vec(RabCommitWidth, Input(UInt(PhyRegIdxWidth.W)))
77    val int_need_free = Vec(RabCommitWidth, Input(Bool()))
78    // to dispatch1
79    val out = Vec(RenameWidth, DecoupledIO(new DynInst))
80    // for snapshots
81    val snpt = Input(new SnapshotPort)
82    val snptLastEnq = Flipped(ValidIO(new RobPtr))
83    val snptIsFull= Input(Bool())
84    // debug arch ports
85    val debug_int_rat = if (backendParams.debugEn) Some(Vec(32, Input(UInt(PhyRegIdxWidth.W)))) else None
86    val debug_fp_rat  = if (backendParams.debugEn) Some(Vec(32, Input(UInt(PhyRegIdxWidth.W)))) else None
87    val debug_vec_rat = if (backendParams.debugEn) Some(Vec(31, Input(UInt(PhyRegIdxWidth.W)))) else None
88    val debug_v0_rat  = if (backendParams.debugEn) Some(Vec(1, Input(UInt(PhyRegIdxWidth.W)))) else None
89    val debug_vl_rat  = if (backendParams.debugEn) Some(Vec(1, Input(UInt(PhyRegIdxWidth.W)))) else None
90    // perf only
91    val stallReason = new Bundle {
92      val in = Flipped(new StallReasonIO(RenameWidth))
93      val out = new StallReasonIO(RenameWidth)
94    }
95  })
96
97  // io alias
98  private val dispatchCanAcc = io.out.head.ready
99
100  val compressUnit = Module(new CompressUnit())
101  // create free list and rat
102  val intFreeList = Module(new MEFreeList(IntPhyRegs))
103  val fpFreeList = Module(new StdFreeList(FpPhyRegs - FpLogicRegs, FpLogicRegs, Reg_F))
104  val vecFreeList = Module(new StdFreeList(VfPhyRegs - VecLogicRegs, VecLogicRegs, Reg_V, 31))
105  val v0FreeList = Module(new StdFreeList(V0PhyRegs - V0LogicRegs, V0LogicRegs, Reg_V0, 1))
106  val vlFreeList = Module(new StdFreeList(VlPhyRegs - VlLogicRegs, VlLogicRegs, Reg_Vl, 1))
107
108
109  intFreeList.io.commit    <> io.rabCommits
110  intFreeList.io.debug_rat.foreach(_ <> io.debug_int_rat.get)
111  fpFreeList.io.commit     <> io.rabCommits
112  fpFreeList.io.debug_rat.foreach(_ <> io.debug_fp_rat.get)
113  vecFreeList.io.commit    <> io.rabCommits
114  vecFreeList.io.debug_rat.foreach(_ <> io.debug_vec_rat.get)
115  v0FreeList.io.commit <> io.rabCommits
116  v0FreeList.io.debug_rat.foreach(_ <> io.debug_v0_rat.get)
117  vlFreeList.io.commit <> io.rabCommits
118  vlFreeList.io.debug_rat.foreach(_ <> io.debug_vl_rat.get)
119
120  // decide if given instruction needs allocating a new physical register (CfCtrl: from decode; RobCommitInfo: from rob)
121  def needDestReg[T <: DecodedInst](reg_t: RegType, x: T): Bool = reg_t match {
122    case Reg_I => x.rfWen
123    case Reg_F => x.fpWen
124    case Reg_V => x.vecWen
125    case Reg_V0 => x.v0Wen
126    case Reg_Vl => x.vlWen
127  }
128  def needDestRegCommit[T <: RabCommitInfo](reg_t: RegType, x: T): Bool = {
129    reg_t match {
130      case Reg_I => x.rfWen
131      case Reg_F => x.fpWen
132      case Reg_V => x.vecWen
133      case Reg_V0 => x.v0Wen
134      case Reg_Vl => x.vlWen
135    }
136  }
137  def needDestRegWalk[T <: RabCommitInfo](reg_t: RegType, x: T): Bool = {
138    reg_t match {
139      case Reg_I => x.rfWen
140      case Reg_F => x.fpWen
141      case Reg_V => x.vecWen
142      case Reg_V0 => x.v0Wen
143      case Reg_Vl => x.vlWen
144    }
145  }
146
147  // connect [redirect + walk] ports for fp & vec & int free list
148  Seq(fpFreeList, vecFreeList, intFreeList, v0FreeList, vlFreeList).foreach { case fl =>
149    fl.io.redirect := io.redirect.valid
150    fl.io.walk := io.rabCommits.isWalk
151  }
152  // only when all free list and dispatch1 has enough space can we do allocation
153  // when isWalk, freelist can definitely allocate
154  intFreeList.io.doAllocate := fpFreeList.io.canAllocate && vecFreeList.io.canAllocate && v0FreeList.io.canAllocate && vlFreeList.io.canAllocate && dispatchCanAcc || io.rabCommits.isWalk
155  fpFreeList.io.doAllocate := intFreeList.io.canAllocate && vecFreeList.io.canAllocate && v0FreeList.io.canAllocate && vlFreeList.io.canAllocate && dispatchCanAcc || io.rabCommits.isWalk
156  vecFreeList.io.doAllocate := intFreeList.io.canAllocate && fpFreeList.io.canAllocate && v0FreeList.io.canAllocate && vlFreeList.io.canAllocate && dispatchCanAcc || io.rabCommits.isWalk
157  v0FreeList.io.doAllocate := intFreeList.io.canAllocate && fpFreeList.io.canAllocate && vecFreeList.io.canAllocate && vlFreeList.io.canAllocate && dispatchCanAcc || io.rabCommits.isWalk
158  vlFreeList.io.doAllocate := intFreeList.io.canAllocate && fpFreeList.io.canAllocate && vecFreeList.io.canAllocate && v0FreeList.io.canAllocate && dispatchCanAcc || io.rabCommits.isWalk
159
160  //           dispatch1 ready ++ float point free list ready ++ int free list ready ++ vec free list ready     ++ not walk
161  val canOut = dispatchCanAcc && fpFreeList.io.canAllocate && intFreeList.io.canAllocate && vecFreeList.io.canAllocate && v0FreeList.io.canAllocate && vlFreeList.io.canAllocate && !io.rabCommits.isWalk
162
163  compressUnit.io.in.zip(io.in).foreach{ case(sink, source) =>
164    sink.valid := source.valid && !io.singleStep
165    sink.bits := source.bits
166  }
167  val needRobFlags = compressUnit.io.out.needRobFlags
168  val instrSizesVec = compressUnit.io.out.instrSizes
169  val compressMasksVec = compressUnit.io.out.masks
170
171  // speculatively assign the instruction with an robIdx
172  val validCount = PopCount(io.in.zip(needRobFlags).map{ case(in, needRobFlag) => in.valid && in.bits.lastUop && needRobFlag}) // number of instructions waiting to enter rob (from decode)
173  val robIdxHead = RegInit(0.U.asTypeOf(new RobPtr))
174  val lastCycleMisprediction = GatedValidRegNext(io.redirect.valid && !io.redirect.bits.flushItself())
175  val robIdxHeadNext = Mux(io.redirect.valid, io.redirect.bits.robIdx, // redirect: move ptr to given rob index
176         Mux(lastCycleMisprediction, robIdxHead + 1.U, // mis-predict: not flush robIdx itself
177           Mux(canOut, robIdxHead + validCount, // instructions successfully entered next stage: increase robIdx
178                      /* default */  robIdxHead))) // no instructions passed by this cycle: stick to old value
179  robIdxHead := robIdxHeadNext
180
181  /**
182    * Rename: allocate free physical register and update rename table
183    */
184  val uops = Wire(Vec(RenameWidth, new DynInst))
185  uops.foreach( uop => {
186    uop.srcState      := DontCare
187    uop.debugInfo     := DontCare
188    uop.lqIdx         := DontCare
189    uop.sqIdx         := DontCare
190    uop.waitForRobIdx := DontCare
191    uop.singleStep    := DontCare
192    uop.snapshot      := DontCare
193    uop.srcLoadDependency := DontCare
194    uop.numLsElem       :=  DontCare
195    uop.hasException  :=  DontCare
196    uop.useRegCache   := DontCare
197    uop.regCacheIdx   := DontCare
198    uop.traceBlockInPipe := DontCare
199  })
200  private val inst         = Wire(Vec(RenameWidth, new XSInstBitFields))
201  private val isCsr        = Wire(Vec(RenameWidth, Bool()))
202  private val isCsrr       = Wire(Vec(RenameWidth, Bool()))
203  private val isRoCsrr     = Wire(Vec(RenameWidth, Bool()))
204  private val fuType       = uops.map(_.fuType)
205  private val fuOpType     = uops.map(_.fuOpType)
206  private val vtype        = uops.map(_.vpu.vtype)
207  private val sew          = vtype.map(_.vsew)
208  private val lmul         = vtype.map(_.vlmul)
209  private val eew          = uops.map(_.vpu.veew)
210  private val mop          = fuOpType.map(fuOpTypeItem => LSUOpType.getVecLSMop(fuOpTypeItem))
211  private val isVlsType    = fuType.map(fuTypeItem => isVls(fuTypeItem))
212  private val isSegment    = fuType.map(fuTypeItem => isVsegls(fuTypeItem))
213  private val isUnitStride = fuOpType.map(fuOpTypeItem => LSUOpType.isAllUS(fuOpTypeItem))
214  private val nf           = fuOpType.zip(uops.map(_.vpu.nf)).map { case (fuOpTypeItem, nfItem) => Mux(LSUOpType.isWhole(fuOpTypeItem), 0.U, nfItem) }
215  private val mulBits      = 3 // dirty code
216  private val emul         = fuOpType.zipWithIndex.map { case (fuOpTypeItem, index) =>
217    Mux(
218      LSUOpType.isWhole(fuOpTypeItem),
219      GenUSWholeEmul(nf(index)),
220      Mux(
221        LSUOpType.isMasked(fuOpTypeItem),
222        0.U(mulBits.W),
223        EewLog2(eew(index)) - sew(index) + lmul(index)
224      )
225    )
226  }
227  private val isVecUnitType = isVlsType.zip(isUnitStride).map { case (isVlsTypeItme, isUnitStrideItem) =>
228    isVlsTypeItme && isUnitStrideItem
229  }
230  private val isfofFixVlUop   = uops.map{x => x.vpu.isVleff && x.lastUop}
231  private val instType = isSegment.zip(mop).map { case (isSegementItem, mopItem) => Cat(isSegementItem, mopItem) }
232  // There is no way to calculate the 'flow' for 'unit-stride' exactly:
233  //  Whether 'unit-stride' needs to be split can only be known after obtaining the address.
234  // For scalar instructions, this is not handled here, and different assignments are done later according to the situation.
235  private val numLsElem = instType.zipWithIndex.map { case (instTypeItem, index) =>
236    Mux(
237      isVecUnitType(index),
238      VecMemUnitStrideMaxFlowNum.U,
239      GenRealFlowNum(instTypeItem, emul(index), lmul(index), eew(index), sew(index))
240    )
241  }
242  uops.zipWithIndex.map { case(u, i) =>
243    u.numLsElem := Mux(io.in(i).valid & isVlsType(i) && !isfofFixVlUop(i), numLsElem(i), 0.U)
244  }
245
246  val needVecDest    = Wire(Vec(RenameWidth, Bool()))
247  val needFpDest     = Wire(Vec(RenameWidth, Bool()))
248  val needIntDest    = Wire(Vec(RenameWidth, Bool()))
249  val needV0Dest     = Wire(Vec(RenameWidth, Bool()))
250  val needVlDest     = Wire(Vec(RenameWidth, Bool()))
251  private val inHeadValid = io.in.head.valid
252
253  val isMove = Wire(Vec(RenameWidth, Bool()))
254  isMove zip io.in.map(_.bits) foreach {
255    case (move, in) => move := Mux(in.exceptionVec.asUInt.orR, false.B, in.isMove)
256  }
257
258  val walkNeedIntDest = WireDefault(VecInit(Seq.fill(RenameWidth)(false.B)))
259  val walkNeedFpDest = WireDefault(VecInit(Seq.fill(RenameWidth)(false.B)))
260  val walkNeedVecDest = WireDefault(VecInit(Seq.fill(RenameWidth)(false.B)))
261  val walkNeedV0Dest = WireDefault(VecInit(Seq.fill(RenameWidth)(false.B)))
262  val walkNeedVlDest = WireDefault(VecInit(Seq.fill(RenameWidth)(false.B)))
263  val walkIsMove = WireDefault(VecInit(Seq.fill(RenameWidth)(false.B)))
264
265  val intSpecWen = Wire(Vec(RenameWidth, Bool()))
266  val fpSpecWen  = Wire(Vec(RenameWidth, Bool()))
267  val vecSpecWen = Wire(Vec(RenameWidth, Bool()))
268  val v0SpecWen = Wire(Vec(RenameWidth, Bool()))
269  val vlSpecWen = Wire(Vec(RenameWidth, Bool()))
270
271  val walkIntSpecWen = WireDefault(VecInit(Seq.fill(RenameWidth)(false.B)))
272
273  val walkPdest = Wire(Vec(RenameWidth, UInt(PhyRegIdxWidth.W)))
274
275  // uop calculation
276  for (i <- 0 until RenameWidth) {
277    (uops(i): Data).waiveAll :<= (io.in(i).bits: Data).waiveAll
278
279    // read only CSRR instruction support: remove blockBackward and waitForward
280    inst(i) := uops(i).instr.asTypeOf(new XSInstBitFields)
281    isCsr(i) := inst(i).OPCODE5Bit === OPCODE5Bit.SYSTEM && inst(i).FUNCT3(1, 0) =/= 0.U
282    isCsrr(i) := isCsr(i) && inst(i).FUNCT3 === BitPat("b?1?") && inst(i).RS1 === 0.U
283    isRoCsrr(i) := isCsrr(i) && LookupTreeDefault(
284      inst(i).CSRIDX, false.B, CSRConst.roCsrrAddr.map(_.U -> true.B))
285
286    /*
287     * For read-only CSRs, CSRR instructions do not need to wait forward instructions to finish.
288     * For all CSRs, CSRR instructions do not need to block backward instructions for issuing.
289     * Signal "isCsrr" contains not only alias instruction CSRR, but also other csr instructions which
290     *   do not require write to any CSR.
291     */
292    uops(i).waitForward := io.in(i).bits.waitForward && !isRoCsrr(i)
293    uops(i).blockBackward := io.in(i).bits.blockBackward && !isCsrr(i)
294
295    // update cf according to ssit result
296    uops(i).storeSetHit := io.ssit(i).valid
297    uops(i).loadWaitStrict := io.ssit(i).strict && io.ssit(i).valid
298    uops(i).ssid := io.ssit(i).ssid
299
300    // update cf according to waittable result
301    uops(i).loadWaitBit := io.waittable(i)
302
303    uops(i).replayInst := false.B // set by IQ or MemQ
304    // alloc a new phy reg
305    needV0Dest(i) := io.in(i).valid && needDestReg(Reg_V0, io.in(i).bits)
306    needVlDest(i) := io.in(i).valid && needDestReg(Reg_Vl, io.in(i).bits)
307    needVecDest(i) := io.in(i).valid && needDestReg(Reg_V, io.in(i).bits)
308    needFpDest(i) := io.in(i).valid && needDestReg(Reg_F, io.in(i).bits)
309    needIntDest(i) := io.in(i).valid && needDestReg(Reg_I, io.in(i).bits)
310    if (i < RabCommitWidth) {
311      walkNeedIntDest(i) := io.rabCommits.walkValid(i) && needDestRegWalk(Reg_I, io.rabCommits.info(i))
312      walkNeedFpDest(i) := io.rabCommits.walkValid(i) && needDestRegWalk(Reg_F, io.rabCommits.info(i))
313      walkNeedVecDest(i) := io.rabCommits.walkValid(i) && needDestRegWalk(Reg_V, io.rabCommits.info(i))
314      walkNeedV0Dest(i) := io.rabCommits.walkValid(i) && needDestRegWalk(Reg_V0, io.rabCommits.info(i))
315      walkNeedVlDest(i) := io.rabCommits.walkValid(i) && needDestRegWalk(Reg_Vl, io.rabCommits.info(i))
316      walkIsMove(i) := io.rabCommits.info(i).isMove
317    }
318    fpFreeList.io.allocateReq(i) := needFpDest(i)
319    fpFreeList.io.walkReq(i) := walkNeedFpDest(i)
320    vecFreeList.io.allocateReq(i) := needVecDest(i)
321    vecFreeList.io.walkReq(i) := walkNeedVecDest(i)
322    v0FreeList.io.allocateReq(i) := needV0Dest(i)
323    v0FreeList.io.walkReq(i) := walkNeedV0Dest(i)
324    vlFreeList.io.allocateReq(i) := needVlDest(i)
325    vlFreeList.io.walkReq(i) := walkNeedVlDest(i)
326    intFreeList.io.allocateReq(i) := needIntDest(i) && !isMove(i)
327    intFreeList.io.walkReq(i) := walkNeedIntDest(i) && !walkIsMove(i)
328
329    // no valid instruction from decode stage || all resources (dispatch1 + both free lists) ready
330    io.in(i).ready := !io.in(0).valid || canOut
331
332    uops(i).robIdx := robIdxHead + PopCount(io.in.zip(needRobFlags).take(i).map{ case(in, needRobFlag) => in.valid && in.bits.lastUop && needRobFlag})
333    uops(i).instrSize := instrSizesVec(i)
334    val hasExceptionExceptFlushPipe = Cat(selectFrontend(uops(i).exceptionVec) :+ uops(i).exceptionVec(illegalInstr) :+ uops(i).exceptionVec(virtualInstr)).orR || TriggerAction.isDmode(uops(i).trigger)
335    when(isMove(i) || hasExceptionExceptFlushPipe) {
336      uops(i).numUops := 0.U
337      uops(i).numWB := 0.U
338    }
339    if (i > 0) {
340      when(!needRobFlags(i - 1)) {
341        uops(i).firstUop := false.B
342        uops(i).ftqPtr := uops(i - 1).ftqPtr
343        uops(i).ftqOffset := uops(i - 1).ftqOffset
344        uops(i).numUops := instrSizesVec(i) - PopCount(compressMasksVec(i) & Cat(isMove.reverse))
345        uops(i).numWB := instrSizesVec(i) - PopCount(compressMasksVec(i) & Cat(isMove.reverse))
346      }
347    }
348    when(!needRobFlags(i)) {
349      uops(i).lastUop := false.B
350      uops(i).numUops := instrSizesVec(i) - PopCount(compressMasksVec(i) & Cat(isMove.reverse))
351      uops(i).numWB := instrSizesVec(i) - PopCount(compressMasksVec(i) & Cat(isMove.reverse))
352    }
353    uops(i).wfflags := (compressMasksVec(i) & Cat(io.in.map(_.bits.wfflags).reverse)).orR
354    uops(i).dirtyFs := (compressMasksVec(i) & Cat(io.in.map(_.bits.fpWen).reverse)).orR
355    // vector instructions' uopSplitType cannot be UopSplitType.SCA_SIM
356    uops(i).dirtyVs := (compressMasksVec(i) & Cat(io.in.map(_.bits.uopSplitType =/= UopSplitType.SCA_SIM).reverse)).orR
357    // psrc0,psrc1,psrc2 don't require v0ReadPorts because their srcType can distinguish whether they are V0 or not
358    uops(i).psrc(0) := Mux1H(uops(i).srcType(0)(2, 0), Seq(io.intReadPorts(i)(0), io.fpReadPorts(i)(0), io.vecReadPorts(i)(0)))
359    uops(i).psrc(1) := Mux1H(uops(i).srcType(1)(2, 0), Seq(io.intReadPorts(i)(1), io.fpReadPorts(i)(1), io.vecReadPorts(i)(1)))
360    uops(i).psrc(2) := Mux1H(uops(i).srcType(2)(2, 1), Seq(io.fpReadPorts(i)(2), io.vecReadPorts(i)(2)))
361    uops(i).psrc(3) := io.v0ReadPorts(i)(0)
362    uops(i).psrc(4) := io.vlReadPorts(i)(0)
363
364    // int psrc2 should be bypassed from next instruction if it is fused
365    if (i < RenameWidth - 1) {
366      when (io.fusionInfo(i).rs2FromRs2 || io.fusionInfo(i).rs2FromRs1) {
367        uops(i).psrc(1) := Mux(io.fusionInfo(i).rs2FromRs2, io.intReadPorts(i + 1)(1), io.intReadPorts(i + 1)(0))
368      }.elsewhen(io.fusionInfo(i).rs2FromZero) {
369        uops(i).psrc(1) := 0.U
370      }
371    }
372    uops(i).eliminatedMove := isMove(i)
373
374    // update pdest
375    uops(i).pdest := MuxCase(0.U, Seq(
376      needIntDest(i)    ->  intFreeList.io.allocatePhyReg(i),
377      needFpDest(i)     ->  fpFreeList.io.allocatePhyReg(i),
378      needVecDest(i)    ->  vecFreeList.io.allocatePhyReg(i),
379      needV0Dest(i)    ->  v0FreeList.io.allocatePhyReg(i),
380      needVlDest(i)    ->  vlFreeList.io.allocatePhyReg(i),
381    ))
382
383    // Assign performance counters
384    uops(i).debugInfo.renameTime := GTimer()
385
386    io.out(i).valid := io.in(i).valid && intFreeList.io.canAllocate && fpFreeList.io.canAllocate && vecFreeList.io.canAllocate && v0FreeList.io.canAllocate && vlFreeList.io.canAllocate && !io.rabCommits.isWalk
387    io.out(i).bits := uops(i)
388    // Todo: move these shit in decode stage
389    // dirty code for fence. The lsrc is passed by imm.
390    when (io.out(i).bits.fuType === FuType.fence.U) {
391      io.out(i).bits.imm := Cat(io.in(i).bits.lsrc(1), io.in(i).bits.lsrc(0))
392    }
393
394    // dirty code for SoftPrefetch (prefetch.r/prefetch.w)
395//    when (io.in(i).bits.isSoftPrefetch) {
396//      io.out(i).bits.fuType := FuType.ldu.U
397//      io.out(i).bits.fuOpType := Mux(io.in(i).bits.lsrc(1) === 1.U, LSUOpType.prefetch_r, LSUOpType.prefetch_w)
398//      io.out(i).bits.selImm := SelImm.IMM_S
399//      io.out(i).bits.imm := Cat(io.in(i).bits.imm(io.in(i).bits.imm.getWidth - 1, 5), 0.U(5.W))
400//    }
401
402    // dirty code for lui+addi(w) fusion
403    if (i < RenameWidth - 1) {
404      val fused_lui32 = io.in(i).bits.selImm === SelImm.IMM_LUI32 && io.in(i).bits.fuType === FuType.alu.U
405      when (fused_lui32) {
406        val lui_imm = io.in(i).bits.imm(19, 0)
407        val add_imm = io.in(i + 1).bits.imm(11, 0)
408        require(io.out(i).bits.imm.getWidth >= lui_imm.getWidth + add_imm.getWidth)
409        io.out(i).bits.imm := Cat(lui_imm, add_imm)
410      }
411    }
412
413    // write speculative rename table
414    // we update rat later inside commit code
415    intSpecWen(i) := needIntDest(i) && intFreeList.io.canAllocate && intFreeList.io.doAllocate && !io.rabCommits.isWalk && !io.redirect.valid
416    fpSpecWen(i)  := needFpDest(i)  && fpFreeList.io.canAllocate  && fpFreeList.io.doAllocate  && !io.rabCommits.isWalk && !io.redirect.valid
417    vecSpecWen(i) := needVecDest(i) && vecFreeList.io.canAllocate && vecFreeList.io.doAllocate && !io.rabCommits.isWalk && !io.redirect.valid
418    v0SpecWen(i) := needV0Dest(i) && v0FreeList.io.canAllocate && v0FreeList.io.doAllocate && !io.rabCommits.isWalk && !io.redirect.valid
419    vlSpecWen(i) := needVlDest(i) && vlFreeList.io.canAllocate && vlFreeList.io.doAllocate && !io.rabCommits.isWalk && !io.redirect.valid
420
421
422    if (i < RabCommitWidth) {
423      walkIntSpecWen(i) := walkNeedIntDest(i) && !io.redirect.valid
424      walkPdest(i) := io.rabCommits.info(i).pdest
425    } else {
426      walkPdest(i) := io.out(i).bits.pdest
427    }
428  }
429
430  /**
431   * trace begin
432   */
433  val inVec = io.in.map(_.bits)
434  val canRobCompressVec = inVec.map(_.canRobCompress)
435  val isRVCVec = inVec.map(_.preDecodeInfo.isRVC)
436  val halfWordNumVec = (0 until RenameWidth).map{
437    i => compressMasksVec(i).asBools.zip(isRVCVec).map{
438      case (mask, isRVC) => Mux(mask, Mux(isRVC, 1.U, 2.U), 0.U)
439    }
440  }
441
442  for (i <- 0 until RenameWidth) {
443    // iretire
444    uops(i).traceBlockInPipe.iretire := Mux(canRobCompressVec(i),
445      halfWordNumVec(i).reduce(_ +& _),
446      Mux(isRVCVec(i), 1.U, 2.U)
447    )
448
449    // ilastsize
450    val j = i
451    val lastIsRVC = WireInit(false.B)
452    (j until RenameWidth).map { j =>
453      when(compressMasksVec(i)(j)) {
454        lastIsRVC := io.in(j).bits.preDecodeInfo.isRVC
455      }
456    }
457
458    uops(i).traceBlockInPipe.ilastsize := Mux(canRobCompressVec(i),
459      Mux(lastIsRVC, Ilastsize.HalfWord, Ilastsize.Word),
460      Mux(isRVCVec(i), Ilastsize.HalfWord, Ilastsize.Word)
461    )
462
463    // itype
464    uops(i).traceBlockInPipe.itype := Itype.jumpTypeGen(inVec(i).preDecodeInfo.brType, inVec(i).ldest.asTypeOf(new OpRegType), inVec(i).lsrc(0).asTypeOf((new OpRegType)))
465  }
466  /**
467   * trace end
468   */
469
470  /**
471    * How to set psrc:
472    * - bypass the pdest to psrc if previous instructions write to the same ldest as lsrc
473    * - default: psrc from RAT
474    * How to set pdest:
475    * - Mux(isMove, psrc, pdest_from_freelist).
476    *
477    * The critical path of rename lies here:
478    * When move elimination is enabled, we need to update the rat with psrc.
479    * However, psrc maybe comes from previous instructions' pdest, which comes from freelist.
480    *
481    * If we expand these logic for pdest(N):
482    * pdest(N) = Mux(isMove(N), psrc(N), freelist_out(N))
483    *          = Mux(isMove(N), Mux(bypass(N, N - 1), pdest(N - 1),
484    *                           Mux(bypass(N, N - 2), pdest(N - 2),
485    *                           ...
486    *                           Mux(bypass(N, 0),     pdest(0),
487    *                                                 rat_out(N))...)),
488    *                           freelist_out(N))
489    */
490  // a simple functional model for now
491  io.out(0).bits.pdest := Mux(isMove(0), uops(0).psrc.head, uops(0).pdest)
492
493  // psrc(n) + pdest(1)
494  val bypassCond: Vec[MixedVec[UInt]] = Wire(Vec(numRegSrc, MixedVec(List.tabulate(RenameWidth-1)(i => UInt((i+1).W)))))
495  require(io.in(0).bits.srcType.size == io.in(0).bits.numSrc)
496  private val pdestLoc = io.in.head.bits.srcType.size // 2 vector src: v0, vl&vtype
497  println(s"[Rename] idx of pdest in bypassCond $pdestLoc")
498  for (i <- 1 until RenameWidth) {
499    val v0Cond = io.in(i).bits.srcType.zipWithIndex.map{ case (s, i) =>
500      if (i == 3) (s === SrcType.vp) || (s === SrcType.v0)
501      else false.B
502    }
503    val vlCond = io.in(i).bits.srcType.zipWithIndex.map{ case (s, i) =>
504      if (i == 4) s === SrcType.vp
505      else false.B
506    }
507    val vecCond = io.in(i).bits.srcType.map(_ === SrcType.vp)
508    val fpCond  = io.in(i).bits.srcType.map(_ === SrcType.fp)
509    val intCond = io.in(i).bits.srcType.map(_ === SrcType.xp)
510    val target = io.in(i).bits.lsrc
511    for ((((((cond1, (condV0, condVl)), cond2), cond3), t), j) <- vecCond.zip(v0Cond.zip(vlCond)).zip(fpCond).zip(intCond).zip(target).zipWithIndex) {
512      val destToSrc = io.in.take(i).zipWithIndex.map { case (in, j) =>
513        val indexMatch = in.bits.ldest === t
514        val writeMatch =  cond3 && needIntDest(j) || cond2 && needFpDest(j) || cond1 && needVecDest(j)
515        val v0vlMatch = condV0 && needV0Dest(j) || condVl && needVlDest(j)
516        indexMatch && writeMatch || v0vlMatch
517      }
518      bypassCond(j)(i - 1) := VecInit(destToSrc).asUInt
519    }
520    io.out(i).bits.psrc(0) := io.out.take(i).map(_.bits.pdest).zip(bypassCond(0)(i-1).asBools).foldLeft(uops(i).psrc(0)) {
521      (z, next) => Mux(next._2, next._1, z)
522    }
523    io.out(i).bits.psrc(1) := io.out.take(i).map(_.bits.pdest).zip(bypassCond(1)(i-1).asBools).foldLeft(uops(i).psrc(1)) {
524      (z, next) => Mux(next._2, next._1, z)
525    }
526    io.out(i).bits.psrc(2) := io.out.take(i).map(_.bits.pdest).zip(bypassCond(2)(i-1).asBools).foldLeft(uops(i).psrc(2)) {
527      (z, next) => Mux(next._2, next._1, z)
528    }
529    io.out(i).bits.psrc(3) := io.out.take(i).map(_.bits.pdest).zip(bypassCond(3)(i-1).asBools).foldLeft(uops(i).psrc(3)) {
530      (z, next) => Mux(next._2, next._1, z)
531    }
532    io.out(i).bits.psrc(4) := io.out.take(i).map(_.bits.pdest).zip(bypassCond(4)(i-1).asBools).foldLeft(uops(i).psrc(4)) {
533      (z, next) => Mux(next._2, next._1, z)
534    }
535    io.out(i).bits.pdest := Mux(isMove(i), io.out(i).bits.psrc(0), uops(i).pdest)
536
537    // Todo: better implementation for fields reuse
538    // For fused-lui-load, load.src(0) is replaced by the imm.
539    val last_is_lui = io.in(i - 1).bits.selImm === SelImm.IMM_U && io.in(i - 1).bits.srcType(0) =/= SrcType.pc
540    val this_is_load = io.in(i).bits.fuType === FuType.ldu.U
541    val lui_to_load = io.in(i - 1).valid && io.in(i - 1).bits.ldest === io.in(i).bits.lsrc(0)
542    val fused_lui_load = last_is_lui && this_is_load && lui_to_load
543    when (fused_lui_load) {
544      // The first LOAD operand (base address) is replaced by LUI-imm and stored in imm
545      val lui_imm = io.in(i - 1).bits.imm(ImmUnion.U.len - 1, 0)
546      val ld_imm = io.in(i).bits.imm(ImmUnion.I.len - 1, 0)
547      require(io.out(i).bits.imm.getWidth >= lui_imm.getWidth + ld_imm.getWidth)
548      io.out(i).bits.srcType(0) := SrcType.imm
549      io.out(i).bits.imm := Cat(lui_imm, ld_imm)
550    }
551
552  }
553
554  val genSnapshot = Cat(io.out.map(out => out.fire && out.bits.snapshot)).orR
555  val lastCycleCreateSnpt = RegInit(false.B)
556  lastCycleCreateSnpt := genSnapshot && !io.snptIsFull
557  val sameSnptDistance = (RobCommitWidth * 4).U
558  // notInSameSnpt: 1.robidxHead - snapLastEnq >= sameSnptDistance 2.no snap
559  val notInSameSnpt = GatedValidRegNext(distanceBetween(robIdxHeadNext, io.snptLastEnq.bits) >= sameSnptDistance || !io.snptLastEnq.valid)
560  val allowSnpt = if (EnableRenameSnapshot) notInSameSnpt && !lastCycleCreateSnpt && io.in.head.bits.firstUop else false.B
561  io.out.zip(io.in).foreach{ case (out, in) => out.bits.snapshot := allowSnpt && (!in.bits.preDecodeInfo.notCFI || FuType.isJump(in.bits.fuType)) && in.fire }
562  io.out.map{ x =>
563    x.bits.hasException := Cat(selectFrontend(x.bits.exceptionVec) :+ x.bits.exceptionVec(illegalInstr) :+ x.bits.exceptionVec(virtualInstr)).orR || TriggerAction.isDmode(x.bits.trigger)
564  }
565  if(backendParams.debugEn){
566    dontTouch(robIdxHeadNext)
567    dontTouch(notInSameSnpt)
568    dontTouch(genSnapshot)
569  }
570  intFreeList.io.snpt := io.snpt
571  fpFreeList.io.snpt := io.snpt
572  vecFreeList.io.snpt := io.snpt
573  v0FreeList.io.snpt := io.snpt
574  vlFreeList.io.snpt := io.snpt
575  intFreeList.io.snpt.snptEnq := genSnapshot
576  fpFreeList.io.snpt.snptEnq := genSnapshot
577  vecFreeList.io.snpt.snptEnq := genSnapshot
578  v0FreeList.io.snpt.snptEnq := genSnapshot
579  vlFreeList.io.snpt.snptEnq := genSnapshot
580
581  /**
582    * Instructions commit: update freelist and rename table
583    */
584  for (i <- 0 until RabCommitWidth) {
585    val commitValid = io.rabCommits.isCommit && io.rabCommits.commitValid(i)
586    val walkValid = io.rabCommits.isWalk && io.rabCommits.walkValid(i)
587
588    // I. RAT Update
589    // When redirect happens (mis-prediction), don't update the rename table
590    io.intRenamePorts(i).wen  := intSpecWen(i)
591    io.intRenamePorts(i).addr := uops(i).ldest(log2Ceil(IntLogicRegs) - 1, 0)
592    io.intRenamePorts(i).data := io.out(i).bits.pdest
593
594    io.fpRenamePorts(i).wen  := fpSpecWen(i)
595    io.fpRenamePorts(i).addr := uops(i).ldest(log2Ceil(FpLogicRegs) - 1, 0)
596    io.fpRenamePorts(i).data := fpFreeList.io.allocatePhyReg(i)
597
598    io.vecRenamePorts(i).wen := vecSpecWen(i)
599    io.vecRenamePorts(i).addr := uops(i).ldest(log2Ceil(VecLogicRegs) - 1, 0)
600    io.vecRenamePorts(i).data := vecFreeList.io.allocatePhyReg(i)
601
602    io.v0RenamePorts(i).wen := v0SpecWen(i)
603    io.v0RenamePorts(i).addr := uops(i).ldest(log2Ceil(V0LogicRegs) - 1, 0)
604    io.v0RenamePorts(i).data := v0FreeList.io.allocatePhyReg(i)
605
606    io.vlRenamePorts(i).wen := vlSpecWen(i)
607    io.vlRenamePorts(i).addr := uops(i).ldest(log2Ceil(VlLogicRegs) - 1, 0)
608    io.vlRenamePorts(i).data := vlFreeList.io.allocatePhyReg(i)
609
610    // II. Free List Update
611    intFreeList.io.freeReq(i) := io.int_need_free(i)
612    intFreeList.io.freePhyReg(i) := RegNext(io.int_old_pdest(i))
613    fpFreeList.io.freeReq(i)  := GatedValidRegNext(commitValid && needDestRegCommit(Reg_F, io.rabCommits.info(i)))
614    fpFreeList.io.freePhyReg(i) := io.fp_old_pdest(i)
615    vecFreeList.io.freeReq(i)  := GatedValidRegNext(commitValid && needDestRegCommit(Reg_V, io.rabCommits.info(i)))
616    vecFreeList.io.freePhyReg(i) := io.vec_old_pdest(i)
617    v0FreeList.io.freeReq(i) := GatedValidRegNext(commitValid && needDestRegCommit(Reg_V0, io.rabCommits.info(i)))
618    v0FreeList.io.freePhyReg(i) := io.v0_old_pdest(i)
619    vlFreeList.io.freeReq(i) := GatedValidRegNext(commitValid && needDestRegCommit(Reg_Vl, io.rabCommits.info(i)))
620    vlFreeList.io.freePhyReg(i) := io.vl_old_pdest(i)
621  }
622
623  /*
624  Debug and performance counters
625   */
626  def printRenameInfo(in: DecoupledIO[DecodedInst], out: DecoupledIO[DynInst]) = {
627    XSInfo(out.fire, p"pc:${Hexadecimal(in.bits.pc)} in(${in.valid},${in.ready}) " +
628      p"lsrc(0):${in.bits.lsrc(0)} -> psrc(0):${out.bits.psrc(0)} " +
629      p"lsrc(1):${in.bits.lsrc(1)} -> psrc(1):${out.bits.psrc(1)} " +
630      p"lsrc(2):${in.bits.lsrc(2)} -> psrc(2):${out.bits.psrc(2)} " +
631      p"ldest:${in.bits.ldest} -> pdest:${out.bits.pdest}\n"
632    )
633  }
634
635  for ((x,y) <- io.in.zip(io.out)) {
636    printRenameInfo(x, y)
637  }
638
639  io.out.map { case x =>
640    when(x.valid && x.bits.rfWen){
641      assert(x.bits.ldest =/= 0.U, "rfWen cannot be 1 when Int regfile ldest is 0")
642    }
643  }
644  val debugRedirect = RegEnable(io.redirect.bits, io.redirect.valid)
645  // bad speculation
646  val recStall = io.redirect.valid || io.rabCommits.isWalk
647  val ctrlRecStall = Mux(io.redirect.valid, io.redirect.bits.debugIsCtrl, io.rabCommits.isWalk && debugRedirect.debugIsCtrl)
648  val mvioRecStall = Mux(io.redirect.valid, io.redirect.bits.debugIsMemVio, io.rabCommits.isWalk && debugRedirect.debugIsMemVio)
649  val otherRecStall = recStall && !(ctrlRecStall || mvioRecStall)
650  XSPerfAccumulate("recovery_stall", recStall)
651  XSPerfAccumulate("control_recovery_stall", ctrlRecStall)
652  XSPerfAccumulate("mem_violation_recovery_stall", mvioRecStall)
653  XSPerfAccumulate("other_recovery_stall", otherRecStall)
654  // freelist stall
655  val notRecStall = !io.out.head.valid && !recStall
656  val intFlStall = notRecStall && inHeadValid && fpFreeList.io.canAllocate && vecFreeList.io.canAllocate && v0FreeList.io.canAllocate && vlFreeList.io.canAllocate && !intFreeList.io.canAllocate
657  val fpFlStall = notRecStall && inHeadValid && intFreeList.io.canAllocate && vecFreeList.io.canAllocate && v0FreeList.io.canAllocate && vlFreeList.io.canAllocate && !fpFreeList.io.canAllocate
658  val vecFlStall = notRecStall && inHeadValid && intFreeList.io.canAllocate && fpFreeList.io.canAllocate && v0FreeList.io.canAllocate && vlFreeList.io.canAllocate && !vecFreeList.io.canAllocate
659  val v0FlStall = notRecStall && inHeadValid && intFreeList.io.canAllocate && fpFreeList.io.canAllocate && vecFreeList.io.canAllocate && vlFreeList.io.canAllocate && !v0FreeList.io.canAllocate
660  val vlFlStall = notRecStall && inHeadValid && intFreeList.io.canAllocate && fpFreeList.io.canAllocate && vecFreeList.io.canAllocate && v0FreeList.io.canAllocate && !vlFreeList.io.canAllocate
661  val multiFlStall = notRecStall && inHeadValid && (PopCount(Cat(
662    !intFreeList.io.canAllocate,
663    !fpFreeList.io.canAllocate,
664    !vecFreeList.io.canAllocate,
665    !v0FreeList.io.canAllocate,
666    !vlFreeList.io.canAllocate,
667  )) > 1.U)
668  // other stall
669  val otherStall = notRecStall && !intFlStall && !fpFlStall && !vecFlStall && !v0FlStall && !vlFlStall && !multiFlStall
670
671  io.stallReason.in.backReason.valid := io.stallReason.out.backReason.valid || !io.in.head.ready
672  io.stallReason.in.backReason.bits := Mux(io.stallReason.out.backReason.valid, io.stallReason.out.backReason.bits,
673    MuxCase(TopDownCounters.OtherCoreStall.id.U, Seq(
674      ctrlRecStall  -> TopDownCounters.ControlRecoveryStall.id.U,
675      mvioRecStall  -> TopDownCounters.MemVioRecoveryStall.id.U,
676      otherRecStall -> TopDownCounters.OtherRecoveryStall.id.U,
677      intFlStall    -> TopDownCounters.IntFlStall.id.U,
678      fpFlStall     -> TopDownCounters.FpFlStall.id.U,
679      vecFlStall    -> TopDownCounters.VecFlStall.id.U,
680      v0FlStall     -> TopDownCounters.V0FlStall.id.U,
681      vlFlStall     -> TopDownCounters.VlFlStall.id.U,
682      multiFlStall  -> TopDownCounters.MultiFlStall.id.U,
683    )
684  ))
685  io.stallReason.out.reason.zip(io.stallReason.in.reason).zip(io.in.map(_.valid)).foreach { case ((out, in), valid) =>
686    out := Mux(io.stallReason.in.backReason.valid, io.stallReason.in.backReason.bits, in)
687  }
688
689  XSDebug(io.rabCommits.isWalk, p"Walk Recovery Enabled\n")
690  XSDebug(io.rabCommits.isWalk, p"validVec:${Binary(io.rabCommits.walkValid.asUInt)}\n")
691  for (i <- 0 until RabCommitWidth) {
692    val info = io.rabCommits.info(i)
693    XSDebug(io.rabCommits.isWalk && io.rabCommits.walkValid(i), p"[#$i walk info] " +
694      p"ldest:${info.ldest} rfWen:${info.rfWen} fpWen:${info.fpWen} vecWen:${info.vecWen} v0Wen:${info.v0Wen} vlWen:${info.vlWen}")
695  }
696
697  XSDebug(p"inValidVec: ${Binary(Cat(io.in.map(_.valid)))}\n")
698
699  XSPerfAccumulate("in_valid_count", PopCount(io.in.map(_.valid)))
700  XSPerfAccumulate("in_fire_count", PopCount(io.in.map(_.fire)))
701  XSPerfAccumulate("in_valid_not_ready_count", PopCount(io.in.map(x => x.valid && !x.ready)))
702  XSPerfAccumulate("wait_cycle", !io.in.head.valid && dispatchCanAcc)
703
704  // These stall reasons could overlap each other, but we configure the priority as fellows.
705  // walk stall > dispatch stall > int freelist stall > fp freelist stall
706  private val inHeadStall = io.in.head match { case x => x.valid && !x.ready }
707  private val stallForWalk      = inHeadValid &&  io.rabCommits.isWalk
708  private val stallForDispatch  = inHeadValid && !io.rabCommits.isWalk && !dispatchCanAcc
709  private val stallForIntFL     = inHeadValid && !io.rabCommits.isWalk && dispatchCanAcc && fpFreeList.io.canAllocate && vecFreeList.io.canAllocate && v0FreeList.io.canAllocate && vlFreeList.io.canAllocate && !intFreeList.io.canAllocate
710  private val stallForFpFL      = inHeadValid && !io.rabCommits.isWalk && dispatchCanAcc && intFreeList.io.canAllocate && vecFreeList.io.canAllocate && v0FreeList.io.canAllocate && vlFreeList.io.canAllocate && !fpFreeList.io.canAllocate
711  private val stallForVecFL     = inHeadValid && !io.rabCommits.isWalk && dispatchCanAcc && intFreeList.io.canAllocate && fpFreeList.io.canAllocate && v0FreeList.io.canAllocate && vlFreeList.io.canAllocate && !vecFreeList.io.canAllocate
712  private val stallForV0FL      = inHeadValid && !io.rabCommits.isWalk && dispatchCanAcc && intFreeList.io.canAllocate && fpFreeList.io.canAllocate && vecFreeList.io.canAllocate && vlFreeList.io.canAllocate && !v0FreeList.io.canAllocate
713  private val stallForVlFL      = inHeadValid && !io.rabCommits.isWalk && dispatchCanAcc && intFreeList.io.canAllocate && fpFreeList.io.canAllocate && vecFreeList.io.canAllocate && v0FreeList.io.canAllocate && !vlFreeList.io.canAllocate
714  XSPerfAccumulate("stall_cycle",          inHeadStall)
715  XSPerfAccumulate("stall_cycle_walk",     stallForWalk)
716  XSPerfAccumulate("stall_cycle_dispatch", stallForDispatch)
717  XSPerfAccumulate("stall_cycle_int",      stallForIntFL)
718  XSPerfAccumulate("stall_cycle_fp",       stallForFpFL)
719  XSPerfAccumulate("stall_cycle_vec",      stallForVecFL)
720  XSPerfAccumulate("stall_cycle_vec",      stallForV0FL)
721  XSPerfAccumulate("stall_cycle_vec",      stallForVlFL)
722
723  XSPerfHistogram("in_valid_range",  PopCount(io.in.map(_.valid)),  true.B, 0, DecodeWidth + 1, 1)
724  XSPerfHistogram("in_fire_range",   PopCount(io.in.map(_.fire)),   true.B, 0, DecodeWidth + 1, 1)
725  XSPerfHistogram("out_valid_range", PopCount(io.out.map(_.valid)), true.B, 0, DecodeWidth + 1, 1)
726  XSPerfHistogram("out_fire_range",  PopCount(io.out.map(_.fire)),  true.B, 0, DecodeWidth + 1, 1)
727
728  XSPerfAccumulate("move_instr_count", PopCount(io.out.map(out => out.fire && out.bits.isMove)))
729  val is_fused_lui_load = io.out.map(o => o.fire && o.bits.fuType === FuType.ldu.U && o.bits.srcType(0) === SrcType.imm)
730  XSPerfAccumulate("fused_lui_load_instr_count", PopCount(is_fused_lui_load))
731
732  val renamePerf = Seq(
733    ("rename_in                  ", PopCount(io.in.map(_.valid & io.in(0).ready ))                                                               ),
734    ("rename_waitinstr           ", PopCount((0 until RenameWidth).map(i => io.in(i).valid && !io.in(i).ready))                                  ),
735    ("rename_stall               ", inHeadStall),
736    ("rename_stall_cycle_walk    ", inHeadValid &&  io.rabCommits.isWalk),
737    ("rename_stall_cycle_dispatch", inHeadValid && !io.rabCommits.isWalk && !dispatchCanAcc),
738    ("rename_stall_cycle_int     ", inHeadValid && !io.rabCommits.isWalk && dispatchCanAcc && fpFreeList.io.canAllocate && vecFreeList.io.canAllocate && v0FreeList.io.canAllocate && vlFreeList.io.canAllocate && !intFreeList.io.canAllocate),
739    ("rename_stall_cycle_fp      ", inHeadValid && !io.rabCommits.isWalk && dispatchCanAcc && intFreeList.io.canAllocate && vecFreeList.io.canAllocate && v0FreeList.io.canAllocate && vlFreeList.io.canAllocate && !fpFreeList.io.canAllocate),
740    ("rename_stall_cycle_vec     ", inHeadValid && !io.rabCommits.isWalk && dispatchCanAcc && intFreeList.io.canAllocate && fpFreeList.io.canAllocate && v0FreeList.io.canAllocate && vlFreeList.io.canAllocate && !vecFreeList.io.canAllocate),
741    ("rename_stall_cycle_v0      ", inHeadValid && !io.rabCommits.isWalk && dispatchCanAcc && intFreeList.io.canAllocate && fpFreeList.io.canAllocate && vecFreeList.io.canAllocate && vlFreeList.io.canAllocate && !v0FreeList.io.canAllocate),
742    ("rename_stall_cycle_vl      ", inHeadValid && !io.rabCommits.isWalk && dispatchCanAcc && intFreeList.io.canAllocate && fpFreeList.io.canAllocate && vecFreeList.io.canAllocate && v0FreeList.io.canAllocate && !vlFreeList.io.canAllocate),
743  )
744  val intFlPerf = intFreeList.getPerfEvents
745  val fpFlPerf = fpFreeList.getPerfEvents
746  val vecFlPerf = vecFreeList.getPerfEvents
747  val v0FlPerf = v0FreeList.getPerfEvents
748  val vlFlPerf = vlFreeList.getPerfEvents
749  val perfEvents = renamePerf ++ intFlPerf ++ fpFlPerf ++ vecFlPerf ++ v0FlPerf ++ vlFlPerf
750  generatePerfEvent()
751}
752