xref: /XiangShan/src/main/scala/xiangshan/backend/rename/Rename.scala (revision 51ad03b0f625dd72200fc44bfef6a42a0563df21)
1c6d43980SLemover/***************************************************************************************
2c6d43980SLemover* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
3f320e0f0SYinan Xu* Copyright (c) 2020-2021 Peng Cheng Laboratory
4c6d43980SLemover*
5c6d43980SLemover* XiangShan is licensed under Mulan PSL v2.
6c6d43980SLemover* You can use this software according to the terms and conditions of the Mulan PSL v2.
7c6d43980SLemover* You may obtain a copy of Mulan PSL v2 at:
8c6d43980SLemover*          http://license.coscl.org.cn/MulanPSL2
9c6d43980SLemover*
10c6d43980SLemover* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
11c6d43980SLemover* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
12c6d43980SLemover* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
13c6d43980SLemover*
14c6d43980SLemover* See the Mulan PSL v2 for more details.
15c6d43980SLemover***************************************************************************************/
16c6d43980SLemover
175844fcf0SLinJiaweipackage xiangshan.backend.rename
185844fcf0SLinJiawei
198891a219SYinan Xuimport org.chipsalliance.cde.config.Parameters
205844fcf0SLinJiaweiimport chisel3._
215844fcf0SLinJiaweiimport chisel3.util._
223c02ee8fSwakafaimport utility._
233b739f49SXuan Huimport utils._
243b739f49SXuan Huimport xiangshan._
2589cc69c1STang Haojinimport xiangshan.backend.Bundles.{DecodedInst, DynInst}
26765e58c6Ssinsanctionimport xiangshan.backend.decode.{FusionDecodeInfo, ImmUnion, Imm_I, Imm_LUI_LOAD, Imm_U}
27730cfbc0SXuan Huimport xiangshan.backend.fu.FuType
2870224bf6SYinan Xuimport xiangshan.backend.rename.freelist._
29c3f16425Sxiaofeibao-xjtuimport xiangshan.backend.rob.{RobEnqIO, RobPtr}
30980c1bc3SWilliam Wangimport xiangshan.mem.mdp._
318daac0bfSxiaofeibao-xjtuimport xiangshan.ExceptionNO._
32d77cf63cSxiaofeibao-xjtuimport xiangshan.backend.fu.FuType._
33d77cf63cSxiaofeibao-xjtuimport xiangshan.mem.{EewLog2, GenUSWholeEmul}
34d77cf63cSxiaofeibao-xjtuimport xiangshan.mem.GenRealFlowNum
3549162c9aSGuanghui Chengimport xiangshan.backend.trace._
36a9becb0dSJunxiong Jiimport xiangshan.backend.decode.isa.bitfield.{OPCODE5Bit, XSInstBitFields}
37075d4937Sjunxiong-jiimport xiangshan.backend.fu.NewCSR.CSROoORead
38547d96acSTang Haojinimport yunsuan.{VfaluType, VipuType}
3999b8dc2cSYinan Xu
40ccfddc82SHaojin Tangclass Rename(implicit p: Parameters) extends XSModule with HasCircularQueuePtrHelper with HasPerfEvents {
41d6f9198fSXuan Hu
42d6f9198fSXuan Hu  // params alias
4398639abbSXuan Hu  private val numRegSrc = backendParams.numRegSrc
44d6f9198fSXuan Hu  private val numVecRegSrc = backendParams.numVecRegSrc
455718c384SHaojin Tang  private val numVecRatPorts = numVecRegSrc
4698639abbSXuan Hu
4798639abbSXuan Hu  println(s"[Rename] numRegSrc: $numRegSrc")
4898639abbSXuan Hu
495844fcf0SLinJiawei  val io = IO(new Bundle() {
505844fcf0SLinJiawei    val redirect = Flipped(ValidIO(new Redirect))
516b102a39SHaojin Tang    val rabCommits = Input(new RabCommitIO)
52a3fe955fSGuanghui Cheng    // from csr
53a3fe955fSGuanghui Cheng    val singleStep = Input(Bool())
547fa2c198SYinan Xu    // from decode
553b739f49SXuan Hu    val in = Vec(RenameWidth, Flipped(DecoupledIO(new DecodedInst)))
56a0db5a4bSYinan Xu    val fusionInfo = Vec(DecodeWidth - 1, Flipped(new FusionDecodeInfo))
57980c1bc3SWilliam Wang    // ssit read result
58980c1bc3SWilliam Wang    val ssit = Flipped(Vec(RenameWidth, Output(new SSITEntry)))
59980c1bc3SWilliam Wang    // waittable read result
60980c1bc3SWilliam Wang    val waittable = Flipped(Vec(RenameWidth, Output(Bool())))
617fa2c198SYinan Xu    // to rename table
625718c384SHaojin Tang    val intReadPorts = Vec(RenameWidth, Vec(2, Input(UInt(PhyRegIdxWidth.W))))
635718c384SHaojin Tang    val fpReadPorts = Vec(RenameWidth, Vec(3, Input(UInt(PhyRegIdxWidth.W))))
64d6f9198fSXuan Hu    val vecReadPorts = Vec(RenameWidth, Vec(numVecRatPorts, Input(UInt(PhyRegIdxWidth.W))))
65368cbcecSxiaofeibao    val v0ReadPorts = Vec(RenameWidth, Vec(1, Input(UInt(PhyRegIdxWidth.W))))
66368cbcecSxiaofeibao    val vlReadPorts = Vec(RenameWidth, Vec(1, Input(UInt(PhyRegIdxWidth.W))))
67ad5c9e6eSJunxiong Ji    val intRenamePorts = Vec(RenameWidth, Output(new RatWritePort(log2Ceil(IntLogicRegs))))
68ad5c9e6eSJunxiong Ji    val fpRenamePorts = Vec(RenameWidth, Output(new RatWritePort(log2Ceil(FpLogicRegs))))
69ad5c9e6eSJunxiong Ji    val vecRenamePorts = Vec(RenameWidth, Output(new RatWritePort(log2Ceil(VecLogicRegs))))
70ad5c9e6eSJunxiong Ji    val v0RenamePorts = Vec(RenameWidth, Output(new RatWritePort(log2Ceil(V0LogicRegs))))
71ad5c9e6eSJunxiong Ji    val vlRenamePorts = Vec(RenameWidth, Output(new RatWritePort(log2Ceil(VlLogicRegs))))
72dcf3a679STang Haojin    // from rename table
73780712aaSxiaofeibao-xjtu    val int_old_pdest = Vec(RabCommitWidth, Input(UInt(PhyRegIdxWidth.W)))
74780712aaSxiaofeibao-xjtu    val fp_old_pdest = Vec(RabCommitWidth, Input(UInt(PhyRegIdxWidth.W)))
75780712aaSxiaofeibao-xjtu    val vec_old_pdest = Vec(RabCommitWidth, Input(UInt(PhyRegIdxWidth.W)))
76368cbcecSxiaofeibao    val v0_old_pdest = Vec(RabCommitWidth, Input(UInt(PhyRegIdxWidth.W)))
77368cbcecSxiaofeibao    val vl_old_pdest = Vec(RabCommitWidth, Input(UInt(PhyRegIdxWidth.W)))
78780712aaSxiaofeibao-xjtu    val int_need_free = Vec(RabCommitWidth, Input(Bool()))
7957c4f8d6SLinJiawei    // to dispatch1
803b739f49SXuan Hu    val out = Vec(RenameWidth, DecoupledIO(new DynInst))
81fa7f2c26STang Haojin    // for snapshots
82fa7f2c26STang Haojin    val snpt = Input(new SnapshotPort)
83c4b56310SHaojin Tang    val snptLastEnq = Flipped(ValidIO(new RobPtr))
84bb7e6e3aSxiaofeibao-xjtu    val snptIsFull= Input(Bool())
85ccfddc82SHaojin Tang    // debug arch ports
86b7d9e8d5Sxiaofeibao-xjtu    val debug_int_rat = if (backendParams.debugEn) Some(Vec(32, Input(UInt(PhyRegIdxWidth.W)))) else None
87b7d9e8d5Sxiaofeibao-xjtu    val debug_fp_rat  = if (backendParams.debugEn) Some(Vec(32, Input(UInt(PhyRegIdxWidth.W)))) else None
88368cbcecSxiaofeibao    val debug_vec_rat = if (backendParams.debugEn) Some(Vec(31, Input(UInt(PhyRegIdxWidth.W)))) else None
89d1e473c9Sxiaofeibao    val debug_v0_rat  = if (backendParams.debugEn) Some(Vec(1, Input(UInt(PhyRegIdxWidth.W)))) else None
90d1e473c9Sxiaofeibao    val debug_vl_rat  = if (backendParams.debugEn) Some(Vec(1, Input(UInt(PhyRegIdxWidth.W)))) else None
91d2b20d1aSTang Haojin    // perf only
92d2b20d1aSTang Haojin    val stallReason = new Bundle {
93d2b20d1aSTang Haojin      val in = Flipped(new StallReasonIO(RenameWidth))
94d2b20d1aSTang Haojin      val out = new StallReasonIO(RenameWidth)
95d2b20d1aSTang Haojin    }
965844fcf0SLinJiawei  })
97b034d3b9SLinJiawei
981592abd1SYan Xu  io.in.zipWithIndex.map { case (o, i) =>
991592abd1SYan Xu    PerfCCT.updateInstPos(o.bits.debug_seqNum, PerfCCT.InstPos.AtRename.id.U, o.valid, clock, reset)
1001592abd1SYan Xu  }
1011592abd1SYan Xu
1026374b1d6SXuan Hu  // io alias
1036374b1d6SXuan Hu  private val dispatchCanAcc = io.out.head.ready
1046374b1d6SXuan Hu
10589cc69c1STang Haojin  val compressUnit = Module(new CompressUnit())
1068b8e745dSYikeZhou  // create free list and rat
10739c59369SXuan Hu  val intFreeList = Module(new MEFreeList(IntPhyRegs))
1084eebf274Ssinsanction  val fpFreeList = Module(new StdFreeList(FpPhyRegs - FpLogicRegs, FpLogicRegs, Reg_F))
109d1e473c9Sxiaofeibao  val vecFreeList = Module(new StdFreeList(VfPhyRegs - VecLogicRegs, VecLogicRegs, Reg_V, 31))
110d1e473c9Sxiaofeibao  val v0FreeList = Module(new StdFreeList(V0PhyRegs - V0LogicRegs, V0LogicRegs, Reg_V0, 1))
111d1e473c9Sxiaofeibao  val vlFreeList = Module(new StdFreeList(VlPhyRegs - VlLogicRegs, VlLogicRegs, Reg_Vl, 1))
112368cbcecSxiaofeibao
1138b8e745dSYikeZhou
1146b102a39SHaojin Tang  intFreeList.io.commit    <> io.rabCommits
115b7d9e8d5Sxiaofeibao-xjtu  intFreeList.io.debug_rat.foreach(_ <> io.debug_int_rat.get)
1166b102a39SHaojin Tang  fpFreeList.io.commit     <> io.rabCommits
117b7d9e8d5Sxiaofeibao-xjtu  fpFreeList.io.debug_rat.foreach(_ <> io.debug_fp_rat.get)
1184eebf274Ssinsanction  vecFreeList.io.commit    <> io.rabCommits
1194eebf274Ssinsanction  vecFreeList.io.debug_rat.foreach(_ <> io.debug_vec_rat.get)
120368cbcecSxiaofeibao  v0FreeList.io.commit <> io.rabCommits
121368cbcecSxiaofeibao  v0FreeList.io.debug_rat.foreach(_ <> io.debug_v0_rat.get)
122368cbcecSxiaofeibao  vlFreeList.io.commit <> io.rabCommits
123368cbcecSxiaofeibao  vlFreeList.io.debug_rat.foreach(_ <> io.debug_vl_rat.get)
124ccfddc82SHaojin Tang
1259aca92b9SYinan Xu  // decide if given instruction needs allocating a new physical register (CfCtrl: from decode; RobCommitInfo: from rob)
1263b739f49SXuan Hu  def needDestReg[T <: DecodedInst](reg_t: RegType, x: T): Bool = reg_t match {
1276112d994Sxiaofeibao    case Reg_I => x.rfWen
1283b739f49SXuan Hu    case Reg_F => x.fpWen
1293b739f49SXuan Hu    case Reg_V => x.vecWen
130368cbcecSxiaofeibao    case Reg_V0 => x.v0Wen
131368cbcecSxiaofeibao    case Reg_Vl => x.vlWen
132b034d3b9SLinJiawei  }
1336b102a39SHaojin Tang  def needDestRegCommit[T <: RabCommitInfo](reg_t: RegType, x: T): Bool = {
1343b739f49SXuan Hu    reg_t match {
1353b739f49SXuan Hu      case Reg_I => x.rfWen
1363b739f49SXuan Hu      case Reg_F => x.fpWen
1373b739f49SXuan Hu      case Reg_V => x.vecWen
138368cbcecSxiaofeibao      case Reg_V0 => x.v0Wen
139368cbcecSxiaofeibao      case Reg_Vl => x.vlWen
140fe6452fcSYinan Xu    }
141deb6421eSHaojin Tang  }
1426b102a39SHaojin Tang  def needDestRegWalk[T <: RabCommitInfo](reg_t: RegType, x: T): Bool = {
1433b739f49SXuan Hu    reg_t match {
1446112d994Sxiaofeibao      case Reg_I => x.rfWen
1453b739f49SXuan Hu      case Reg_F => x.fpWen
1463b739f49SXuan Hu      case Reg_V => x.vecWen
147368cbcecSxiaofeibao      case Reg_V0 => x.v0Wen
148368cbcecSxiaofeibao      case Reg_Vl => x.vlWen
1493b739f49SXuan Hu    }
150ccfddc82SHaojin Tang  }
1518b8e745dSYikeZhou
1524eebf274Ssinsanction  // connect [redirect + walk] ports for fp & vec & int free list
153368cbcecSxiaofeibao  Seq(fpFreeList, vecFreeList, intFreeList, v0FreeList, vlFreeList).foreach { case fl =>
15470224bf6SYinan Xu    fl.io.redirect := io.redirect.valid
1556b102a39SHaojin Tang    fl.io.walk := io.rabCommits.isWalk
1564efb89cbSYikeZhou  }
1574eebf274Ssinsanction  // only when all free list and dispatch1 has enough space can we do allocation
158ccfddc82SHaojin Tang  // when isWalk, freelist can definitely allocate
159368cbcecSxiaofeibao  intFreeList.io.doAllocate := fpFreeList.io.canAllocate && vecFreeList.io.canAllocate && v0FreeList.io.canAllocate && vlFreeList.io.canAllocate && dispatchCanAcc || io.rabCommits.isWalk
160368cbcecSxiaofeibao  fpFreeList.io.doAllocate := intFreeList.io.canAllocate && vecFreeList.io.canAllocate && v0FreeList.io.canAllocate && vlFreeList.io.canAllocate && dispatchCanAcc || io.rabCommits.isWalk
161368cbcecSxiaofeibao  vecFreeList.io.doAllocate := intFreeList.io.canAllocate && fpFreeList.io.canAllocate && v0FreeList.io.canAllocate && vlFreeList.io.canAllocate && dispatchCanAcc || io.rabCommits.isWalk
162368cbcecSxiaofeibao  v0FreeList.io.doAllocate := intFreeList.io.canAllocate && fpFreeList.io.canAllocate && vecFreeList.io.canAllocate && vlFreeList.io.canAllocate && dispatchCanAcc || io.rabCommits.isWalk
163368cbcecSxiaofeibao  vlFreeList.io.doAllocate := intFreeList.io.canAllocate && fpFreeList.io.canAllocate && vecFreeList.io.canAllocate && v0FreeList.io.canAllocate && dispatchCanAcc || io.rabCommits.isWalk
1645eb4af5bSYikeZhou
1654eebf274Ssinsanction  //           dispatch1 ready ++ float point free list ready ++ int free list ready ++ vec free list ready     ++ not walk
166368cbcecSxiaofeibao  val canOut = dispatchCanAcc && fpFreeList.io.canAllocate && intFreeList.io.canAllocate && vecFreeList.io.canAllocate && v0FreeList.io.canAllocate && vlFreeList.io.canAllocate && !io.rabCommits.isWalk
1675eb4af5bSYikeZhou
16889cc69c1STang Haojin  compressUnit.io.in.zip(io.in).foreach{ case(sink, source) =>
169a3fe955fSGuanghui Cheng    sink.valid := source.valid && !io.singleStep
17089cc69c1STang Haojin    sink.bits := source.bits
17189cc69c1STang Haojin  }
17289cc69c1STang Haojin  val needRobFlags = compressUnit.io.out.needRobFlags
17389cc69c1STang Haojin  val instrSizesVec = compressUnit.io.out.instrSizes
17489cc69c1STang Haojin  val compressMasksVec = compressUnit.io.out.masks
175b034d3b9SLinJiawei
1769aca92b9SYinan Xu  // speculatively assign the instruction with an robIdx
17789cc69c1STang Haojin  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)
1789aca92b9SYinan Xu  val robIdxHead = RegInit(0.U.asTypeOf(new RobPtr))
1795f8b6c9eSsinceforYy  val lastCycleMisprediction = GatedValidRegNext(io.redirect.valid && !io.redirect.bits.flushItself())
180f4b2089aSYinan Xu  val robIdxHeadNext = Mux(io.redirect.valid, io.redirect.bits.robIdx, // redirect: move ptr to given rob index
1819aca92b9SYinan Xu         Mux(lastCycleMisprediction, robIdxHead + 1.U, // mis-predict: not flush robIdx itself
182ac78003fSzhanglyGit           Mux(canOut, robIdxHead + validCount, // instructions successfully entered next stage: increase robIdx
183f4b2089aSYinan Xu                      /* default */  robIdxHead))) // no instructions passed by this cycle: stick to old value
1849aca92b9SYinan Xu  robIdxHead := robIdxHeadNext
185588ceab5SYinan Xu
18600ad41d0SYinan Xu  /**
18700ad41d0SYinan Xu    * Rename: allocate free physical register and update rename table
18800ad41d0SYinan Xu    */
1893b739f49SXuan Hu  val uops = Wire(Vec(RenameWidth, new DynInst))
190b034d3b9SLinJiawei  uops.foreach( uop => {
191a7a8a6ccSHaojin Tang    uop.srcState      := DontCare
1927cef916fSYinan Xu    uop.debugInfo     := DontCare
193bc86598fSWilliam Wang    uop.lqIdx         := DontCare
194bc86598fSWilliam Wang    uop.sqIdx         := DontCare
1953b739f49SXuan Hu    uop.waitForRobIdx := DontCare
1963b739f49SXuan Hu    uop.singleStep    := DontCare
197fa7f2c26STang Haojin    uop.snapshot      := DontCare
19813551487SzhanglyGit    uop.srcLoadDependency := DontCare
199f3a9fb05SAnzo    uop.numLsElem       :=  DontCare
2008daac0bfSxiaofeibao-xjtu    uop.hasException  :=  DontCare
201955b4beaSsinsanction    uop.useRegCache   := DontCare
202955b4beaSsinsanction    uop.regCacheIdx   := DontCare
20349162c9aSGuanghui Cheng    uop.traceBlockInPipe := DontCare
20441eedc8dSlinzhida    uop.isDropAmocasSta := DontCare
205b034d3b9SLinJiawei  })
206a9becb0dSJunxiong Ji  private val inst         = Wire(Vec(RenameWidth, new XSInstBitFields))
207a9becb0dSJunxiong Ji  private val isCsr        = Wire(Vec(RenameWidth, Bool()))
208a9becb0dSJunxiong Ji  private val isCsrr       = Wire(Vec(RenameWidth, Bool()))
209*51ad03b0SZhaoyang You  private val isNotWaitForwardCsrr = Wire(Vec(RenameWidth, Bool()))
210*51ad03b0SZhaoyang You  private val isNotBlockBackwardCsrr = Wire(Vec(RenameWidth, Bool()))
211d77cf63cSxiaofeibao-xjtu  private val fuType       = uops.map(_.fuType)
212d77cf63cSxiaofeibao-xjtu  private val fuOpType     = uops.map(_.fuOpType)
213d77cf63cSxiaofeibao-xjtu  private val vtype        = uops.map(_.vpu.vtype)
214d77cf63cSxiaofeibao-xjtu  private val sew          = vtype.map(_.vsew)
215d77cf63cSxiaofeibao-xjtu  private val lmul         = vtype.map(_.vlmul)
216d77cf63cSxiaofeibao-xjtu  private val eew          = uops.map(_.vpu.veew)
217d77cf63cSxiaofeibao-xjtu  private val mop          = fuOpType.map(fuOpTypeItem => LSUOpType.getVecLSMop(fuOpTypeItem))
218d77cf63cSxiaofeibao-xjtu  private val isVlsType    = fuType.map(fuTypeItem => isVls(fuTypeItem))
219d77cf63cSxiaofeibao-xjtu  private val isSegment    = fuType.map(fuTypeItem => isVsegls(fuTypeItem))
220d77cf63cSxiaofeibao-xjtu  private val isUnitStride = fuOpType.map(fuOpTypeItem => LSUOpType.isAllUS(fuOpTypeItem))
221d77cf63cSxiaofeibao-xjtu  private val nf           = fuOpType.zip(uops.map(_.vpu.nf)).map { case (fuOpTypeItem, nfItem) => Mux(LSUOpType.isWhole(fuOpTypeItem), 0.U, nfItem) }
222d77cf63cSxiaofeibao-xjtu  private val mulBits      = 3 // dirty code
223d77cf63cSxiaofeibao-xjtu  private val emul         = fuOpType.zipWithIndex.map { case (fuOpTypeItem, index) =>
224d77cf63cSxiaofeibao-xjtu    Mux(
225d77cf63cSxiaofeibao-xjtu      LSUOpType.isWhole(fuOpTypeItem),
226d77cf63cSxiaofeibao-xjtu      GenUSWholeEmul(nf(index)),
227d77cf63cSxiaofeibao-xjtu      Mux(
228d77cf63cSxiaofeibao-xjtu        LSUOpType.isMasked(fuOpTypeItem),
229d77cf63cSxiaofeibao-xjtu        0.U(mulBits.W),
230d77cf63cSxiaofeibao-xjtu        EewLog2(eew(index)) - sew(index) + lmul(index)
231d77cf63cSxiaofeibao-xjtu      )
232d77cf63cSxiaofeibao-xjtu    )
233d77cf63cSxiaofeibao-xjtu  }
234d77cf63cSxiaofeibao-xjtu  private val isVecUnitType = isVlsType.zip(isUnitStride).map { case (isVlsTypeItme, isUnitStrideItem) =>
235d77cf63cSxiaofeibao-xjtu    isVlsTypeItme && isUnitStrideItem
236d77cf63cSxiaofeibao-xjtu  }
237df3b4b92SAnzooooo  private val isfofFixVlUop   = uops.map{x => x.vpu.isVleff && x.lastUop}
238d77cf63cSxiaofeibao-xjtu  private val instType = isSegment.zip(mop).map { case (isSegementItem, mopItem) => Cat(isSegementItem, mopItem) }
239d77cf63cSxiaofeibao-xjtu  // There is no way to calculate the 'flow' for 'unit-stride' exactly:
240d77cf63cSxiaofeibao-xjtu  //  Whether 'unit-stride' needs to be split can only be known after obtaining the address.
241d77cf63cSxiaofeibao-xjtu  // For scalar instructions, this is not handled here, and different assignments are done later according to the situation.
242d77cf63cSxiaofeibao-xjtu  private val numLsElem = instType.zipWithIndex.map { case (instTypeItem, index) =>
243d77cf63cSxiaofeibao-xjtu    Mux(
244d77cf63cSxiaofeibao-xjtu      isVecUnitType(index),
245d77cf63cSxiaofeibao-xjtu      VecMemUnitStrideMaxFlowNum.U,
246d77cf63cSxiaofeibao-xjtu      GenRealFlowNum(instTypeItem, emul(index), lmul(index), eew(index), sew(index))
247d77cf63cSxiaofeibao-xjtu    )
248d77cf63cSxiaofeibao-xjtu  }
249d77cf63cSxiaofeibao-xjtu  uops.zipWithIndex.map { case(u, i) =>
250df3b4b92SAnzooooo    u.numLsElem := Mux(io.in(i).valid & isVlsType(i) && !isfofFixVlUop(i), numLsElem(i), 0.U)
251d77cf63cSxiaofeibao-xjtu  }
252b034d3b9SLinJiawei
253deb6421eSHaojin Tang  val needVecDest    = Wire(Vec(RenameWidth, Bool()))
25499b8dc2cSYinan Xu  val needFpDest     = Wire(Vec(RenameWidth, Bool()))
25599b8dc2cSYinan Xu  val needIntDest    = Wire(Vec(RenameWidth, Bool()))
256368cbcecSxiaofeibao  val needV0Dest     = Wire(Vec(RenameWidth, Bool()))
257368cbcecSxiaofeibao  val needVlDest     = Wire(Vec(RenameWidth, Bool()))
258a63155a6SXuan Hu  private val inHeadValid = io.in.head.valid
2598b8e745dSYikeZhou
260c58c2872STang Haojin  val isMove = Wire(Vec(RenameWidth, Bool()))
261c58c2872STang Haojin  isMove zip io.in.map(_.bits) foreach {
262c58c2872STang Haojin    case (move, in) => move := Mux(in.exceptionVec.asUInt.orR, false.B, in.isMove)
263c58c2872STang Haojin  }
2648b8e745dSYikeZhou
265ccfddc82SHaojin Tang  val walkNeedIntDest = WireDefault(VecInit(Seq.fill(RenameWidth)(false.B)))
2663b739f49SXuan Hu  val walkNeedFpDest = WireDefault(VecInit(Seq.fill(RenameWidth)(false.B)))
2673b739f49SXuan Hu  val walkNeedVecDest = WireDefault(VecInit(Seq.fill(RenameWidth)(false.B)))
268368cbcecSxiaofeibao  val walkNeedV0Dest = WireDefault(VecInit(Seq.fill(RenameWidth)(false.B)))
269368cbcecSxiaofeibao  val walkNeedVlDest = WireDefault(VecInit(Seq.fill(RenameWidth)(false.B)))
270ccfddc82SHaojin Tang  val walkIsMove = WireDefault(VecInit(Seq.fill(RenameWidth)(false.B)))
271ccfddc82SHaojin Tang
2728b8e745dSYikeZhou  val intSpecWen = Wire(Vec(RenameWidth, Bool()))
2738b8e745dSYikeZhou  val fpSpecWen  = Wire(Vec(RenameWidth, Bool()))
274deb6421eSHaojin Tang  val vecSpecWen = Wire(Vec(RenameWidth, Bool()))
275368cbcecSxiaofeibao  val v0SpecWen = Wire(Vec(RenameWidth, Bool()))
276368cbcecSxiaofeibao  val vlSpecWen = Wire(Vec(RenameWidth, Bool()))
2778b8e745dSYikeZhou
278ccfddc82SHaojin Tang  val walkIntSpecWen = WireDefault(VecInit(Seq.fill(RenameWidth)(false.B)))
279ccfddc82SHaojin Tang
280ccfddc82SHaojin Tang  val walkPdest = Wire(Vec(RenameWidth, UInt(PhyRegIdxWidth.W)))
281ccfddc82SHaojin Tang
2821592abd1SYan Xu  io.out.zipWithIndex.foreach{ case (o, i) =>
2831592abd1SYan Xu    o.bits.debug_seqNum := io.in(i).bits.debug_seqNum
2841592abd1SYan Xu  }
2851592abd1SYan Xu
2868b8e745dSYikeZhou  // uop calculation
287b034d3b9SLinJiawei  for (i <- 0 until RenameWidth) {
2880c01a27aSHaojin Tang    (uops(i): Data).waiveAll :<= (io.in(i).bits: Data).waiveAll
289b034d3b9SLinJiawei
290a9becb0dSJunxiong Ji    // read only CSRR instruction support: remove blockBackward and waitForward
291a9becb0dSJunxiong Ji    inst(i) := uops(i).instr.asTypeOf(new XSInstBitFields)
292a9becb0dSJunxiong Ji    isCsr(i) := inst(i).OPCODE5Bit === OPCODE5Bit.SYSTEM && inst(i).FUNCT3(1, 0) =/= 0.U
293a9becb0dSJunxiong Ji    isCsrr(i) := isCsr(i) && inst(i).FUNCT3 === BitPat("b?1?") && inst(i).RS1 === 0.U
294*51ad03b0SZhaoyang You    isNotWaitForwardCsrr(i) := isCsrr(i) && LookupTreeDefault(
2958893eb2cSZhaoyang You      inst(i).CSRIDX, true.B, CSROoORead.waitForwardInOrderCsrReadList.map(_.U -> false.B))
296*51ad03b0SZhaoyang You    isNotBlockBackwardCsrr(i) := isCsrr(i) && LookupTreeDefault(
2978893eb2cSZhaoyang You      inst(i).CSRIDX, true.B, CSROoORead.blockBackwardInOrderCsrReadList.map(_.U -> false.B))
298a9becb0dSJunxiong Ji
299253db1a4SJunxiong Ji    /*
300075d4937Sjunxiong-ji     * For most CSRs, CSRR instructions do not need to wait forward instructions.
301075d4937Sjunxiong-ji     *
3028893eb2cSZhaoyang You     * For most CSRs, CSRR instructions do not need to block backward instructions.
303075d4937Sjunxiong-ji     *
304075d4937Sjunxiong-ji     * Signal "isCsrr" contains not only "CSRR", but also other CSR instructions that do not require writing to CSR.
305253db1a4SJunxiong Ji     */
306*51ad03b0SZhaoyang You    uops(i).waitForward := io.in(i).bits.waitForward && !isNotWaitForwardCsrr(i)
307*51ad03b0SZhaoyang You    uops(i).blockBackward := io.in(i).bits.blockBackward && !isNotBlockBackwardCsrr(i)
308a9becb0dSJunxiong Ji
309980c1bc3SWilliam Wang    // update cf according to ssit result
3103b739f49SXuan Hu    uops(i).storeSetHit := io.ssit(i).valid
3113b739f49SXuan Hu    uops(i).loadWaitStrict := io.ssit(i).strict && io.ssit(i).valid
3123b739f49SXuan Hu    uops(i).ssid := io.ssit(i).ssid
313980c1bc3SWilliam Wang
314980c1bc3SWilliam Wang    // update cf according to waittable result
3153b739f49SXuan Hu    uops(i).loadWaitBit := io.waittable(i)
316980c1bc3SWilliam Wang
3173b739f49SXuan Hu    uops(i).replayInst := false.B // set by IQ or MemQ
3184eebf274Ssinsanction    // alloc a new phy reg
319368cbcecSxiaofeibao    needV0Dest(i) := io.in(i).valid && needDestReg(Reg_V0, io.in(i).bits)
320368cbcecSxiaofeibao    needVlDest(i) := io.in(i).valid && needDestReg(Reg_Vl, io.in(i).bits)
321ac78003fSzhanglyGit    needVecDest(i) := io.in(i).valid && needDestReg(Reg_V, io.in(i).bits)
322ac78003fSzhanglyGit    needFpDest(i) := io.in(i).valid && needDestReg(Reg_F, io.in(i).bits)
323ac78003fSzhanglyGit    needIntDest(i) := io.in(i).valid && needDestReg(Reg_I, io.in(i).bits)
324780712aaSxiaofeibao-xjtu    if (i < RabCommitWidth) {
3256b102a39SHaojin Tang      walkNeedIntDest(i) := io.rabCommits.walkValid(i) && needDestRegWalk(Reg_I, io.rabCommits.info(i))
3266b102a39SHaojin Tang      walkNeedFpDest(i) := io.rabCommits.walkValid(i) && needDestRegWalk(Reg_F, io.rabCommits.info(i))
3276b102a39SHaojin Tang      walkNeedVecDest(i) := io.rabCommits.walkValid(i) && needDestRegWalk(Reg_V, io.rabCommits.info(i))
328368cbcecSxiaofeibao      walkNeedV0Dest(i) := io.rabCommits.walkValid(i) && needDestRegWalk(Reg_V0, io.rabCommits.info(i))
329368cbcecSxiaofeibao      walkNeedVlDest(i) := io.rabCommits.walkValid(i) && needDestRegWalk(Reg_Vl, io.rabCommits.info(i))
3306b102a39SHaojin Tang      walkIsMove(i) := io.rabCommits.info(i).isMove
331ccfddc82SHaojin Tang    }
3324eebf274Ssinsanction    fpFreeList.io.allocateReq(i) := needFpDest(i)
3334eebf274Ssinsanction    fpFreeList.io.walkReq(i) := walkNeedFpDest(i)
3344eebf274Ssinsanction    vecFreeList.io.allocateReq(i) := needVecDest(i)
3354eebf274Ssinsanction    vecFreeList.io.walkReq(i) := walkNeedVecDest(i)
336368cbcecSxiaofeibao    v0FreeList.io.allocateReq(i) := needV0Dest(i)
337368cbcecSxiaofeibao    v0FreeList.io.walkReq(i) := walkNeedV0Dest(i)
338368cbcecSxiaofeibao    vlFreeList.io.allocateReq(i) := needVlDest(i)
339368cbcecSxiaofeibao    vlFreeList.io.walkReq(i) := walkNeedVlDest(i)
340dcf3a679STang Haojin    intFreeList.io.allocateReq(i) := needIntDest(i) && !isMove(i)
341dcf3a679STang Haojin    intFreeList.io.walkReq(i) := walkNeedIntDest(i) && !walkIsMove(i)
3422438f9ebSYinan Xu
3438b8e745dSYikeZhou    // no valid instruction from decode stage || all resources (dispatch1 + both free lists) ready
3441c6572a6Sxiaofeibao    io.in(i).ready := !io.in(0).valid || canOut
34558e06390SLinJiawei
34689cc69c1STang Haojin    uops(i).robIdx := robIdxHead + PopCount(io.in.zip(needRobFlags).take(i).map{ case(in, needRobFlag) => in.valid && in.bits.lastUop && needRobFlag})
34789cc69c1STang Haojin    uops(i).instrSize := instrSizesVec(i)
3487e0f64b0SGuanghui Cheng    val hasExceptionExceptFlushPipe = Cat(selectFrontend(uops(i).exceptionVec) :+ uops(i).exceptionVec(illegalInstr) :+ uops(i).exceptionVec(virtualInstr)).orR || TriggerAction.isDmode(uops(i).trigger)
349571677c9Sxiaofeibao-xjtu    when(isMove(i) || hasExceptionExceptFlushPipe) {
35089cc69c1STang Haojin      uops(i).numUops := 0.U
3513235a9d8SZiyue-Zhang      uops(i).numWB := 0.U
35289cc69c1STang Haojin    }
35389cc69c1STang Haojin    if (i > 0) {
35489cc69c1STang Haojin      when(!needRobFlags(i - 1)) {
35589cc69c1STang Haojin        uops(i).firstUop := false.B
35689cc69c1STang Haojin        uops(i).ftqPtr := uops(i - 1).ftqPtr
35789cc69c1STang Haojin        uops(i).ftqOffset := uops(i - 1).ftqOffset
35889cc69c1STang Haojin        uops(i).numUops := instrSizesVec(i) - PopCount(compressMasksVec(i) & Cat(isMove.reverse))
3593235a9d8SZiyue-Zhang        uops(i).numWB := instrSizesVec(i) - PopCount(compressMasksVec(i) & Cat(isMove.reverse))
36089cc69c1STang Haojin      }
36189cc69c1STang Haojin    }
36289cc69c1STang Haojin    when(!needRobFlags(i)) {
36389cc69c1STang Haojin      uops(i).lastUop := false.B
36489cc69c1STang Haojin      uops(i).numUops := instrSizesVec(i) - PopCount(compressMasksVec(i) & Cat(isMove.reverse))
3653235a9d8SZiyue-Zhang      uops(i).numWB := instrSizesVec(i) - PopCount(compressMasksVec(i) & Cat(isMove.reverse))
36689cc69c1STang Haojin    }
367f1ba628bSHaojin Tang    uops(i).wfflags := (compressMasksVec(i) & Cat(io.in.map(_.bits.wfflags).reverse)).orR
368f1ba628bSHaojin Tang    uops(i).dirtyFs := (compressMasksVec(i) & Cat(io.in.map(_.bits.fpWen).reverse)).orR
369547d96acSTang Haojin    uops(i).dirtyVs := (
370547d96acSTang Haojin      compressMasksVec(i) & Cat(io.in.map(in =>
3713af3539fSZiyue Zhang        // vector instructions' uopSplitType cannot be UopSplitType.SCA_SIM
372547d96acSTang Haojin        in.bits.uopSplitType =/= UopSplitType.SCA_SIM &&
37338c29594Szhanglinjuan        !UopSplitType.isAMOCAS(in.bits.uopSplitType) &&
374547d96acSTang Haojin        // vfmv.f.s, vcpop.m, vfirst.m and vmv.x.s don't change vector state
375547d96acSTang Haojin        !Seq(
376547d96acSTang Haojin          (FuType.vfalu, VfaluType.vfmv_f_s), // vfmv.f.s
377547d96acSTang Haojin          (FuType.vipu, VipuType.vcpop_m),    // vcpop.m
378547d96acSTang Haojin          (FuType.vipu, VipuType.vfirst_m),   // vfirst.m
379547d96acSTang Haojin          (FuType.vipu, VipuType.vmv_x_s)     // vmv.x.s
380547d96acSTang Haojin        ).map(x => FuTypeOrR(in.bits.fuType, x._1) && in.bits.fuOpType === x._2).reduce(_ || _)
381547d96acSTang Haojin      ).reverse)
382547d96acSTang Haojin    ).orR
383368cbcecSxiaofeibao    // psrc0,psrc1,psrc2 don't require v0ReadPorts because their srcType can distinguish whether they are V0 or not
384368cbcecSxiaofeibao    uops(i).psrc(0) := Mux1H(uops(i).srcType(0)(2, 0), Seq(io.intReadPorts(i)(0), io.fpReadPorts(i)(0), io.vecReadPorts(i)(0)))
385368cbcecSxiaofeibao    uops(i).psrc(1) := Mux1H(uops(i).srcType(1)(2, 0), Seq(io.intReadPorts(i)(1), io.fpReadPorts(i)(1), io.vecReadPorts(i)(1)))
3863b739f49SXuan Hu    uops(i).psrc(2) := Mux1H(uops(i).srcType(2)(2, 1), Seq(io.fpReadPorts(i)(2), io.vecReadPorts(i)(2)))
387368cbcecSxiaofeibao    uops(i).psrc(3) := io.v0ReadPorts(i)(0)
388368cbcecSxiaofeibao    uops(i).psrc(4) := io.vlReadPorts(i)(0)
389f5710817SXuan Hu
390a0db5a4bSYinan Xu    // int psrc2 should be bypassed from next instruction if it is fused
391a0db5a4bSYinan Xu    if (i < RenameWidth - 1) {
392a0db5a4bSYinan Xu      when (io.fusionInfo(i).rs2FromRs2 || io.fusionInfo(i).rs2FromRs1) {
393a0db5a4bSYinan Xu        uops(i).psrc(1) := Mux(io.fusionInfo(i).rs2FromRs2, io.intReadPorts(i + 1)(1), io.intReadPorts(i + 1)(0))
394a0db5a4bSYinan Xu      }.elsewhen(io.fusionInfo(i).rs2FromZero) {
395a0db5a4bSYinan Xu        uops(i).psrc(1) := 0.U
396a0db5a4bSYinan Xu      }
397a0db5a4bSYinan Xu    }
39870224bf6SYinan Xu    uops(i).eliminatedMove := isMove(i)
3998b8e745dSYikeZhou
4008b8e745dSYikeZhou    // update pdest
401ac78003fSzhanglyGit    uops(i).pdest := MuxCase(0.U, Seq(
402ac78003fSzhanglyGit      needIntDest(i)    ->  intFreeList.io.allocatePhyReg(i),
4034eebf274Ssinsanction      needFpDest(i)     ->  fpFreeList.io.allocatePhyReg(i),
4044eebf274Ssinsanction      needVecDest(i)    ->  vecFreeList.io.allocatePhyReg(i),
405368cbcecSxiaofeibao      needV0Dest(i)    ->  v0FreeList.io.allocatePhyReg(i),
406368cbcecSxiaofeibao      needVlDest(i)    ->  vlFreeList.io.allocatePhyReg(i),
4073b739f49SXuan Hu    ))
4088b8e745dSYikeZhou
409ebb8ebf8SYinan Xu    // Assign performance counters
410ebb8ebf8SYinan Xu    uops(i).debugInfo.renameTime := GTimer()
411ebb8ebf8SYinan Xu
412368cbcecSxiaofeibao    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
413ebb8ebf8SYinan Xu    io.out(i).bits := uops(i)
4140a7d1d5cSxiaofeibao    // dirty code
4150a7d1d5cSxiaofeibao    if (i == 0) {
4160a7d1d5cSxiaofeibao      io.out(i).bits.psrc(0) := Mux(io.out(i).bits.isLUI, 0.U, uops(i).psrc(0))
4170a7d1d5cSxiaofeibao    }
4183b739f49SXuan Hu    // Todo: move these shit in decode stage
419f025d715SYinan Xu    // dirty code for fence. The lsrc is passed by imm.
4203b739f49SXuan Hu    when (io.out(i).bits.fuType === FuType.fence.U) {
4213b739f49SXuan Hu      io.out(i).bits.imm := Cat(io.in(i).bits.lsrc(1), io.in(i).bits.lsrc(0))
422a020ce37SYinan Xu    }
423d91483a6Sfdy
424f025d715SYinan Xu    // dirty code for SoftPrefetch (prefetch.r/prefetch.w)
425621007d9SXuan Hu//    when (io.in(i).bits.isSoftPrefetch) {
426621007d9SXuan Hu//      io.out(i).bits.fuType := FuType.ldu.U
427621007d9SXuan Hu//      io.out(i).bits.fuOpType := Mux(io.in(i).bits.lsrc(1) === 1.U, LSUOpType.prefetch_r, LSUOpType.prefetch_w)
428621007d9SXuan Hu//      io.out(i).bits.selImm := SelImm.IMM_S
429621007d9SXuan Hu//      io.out(i).bits.imm := Cat(io.in(i).bits.imm(io.in(i).bits.imm.getWidth - 1, 5), 0.U(5.W))
430621007d9SXuan Hu//    }
431ebb8ebf8SYinan Xu
432765e58c6Ssinsanction    // dirty code for lui+addi(w) fusion
433765e58c6Ssinsanction    if (i < RenameWidth - 1) {
434765e58c6Ssinsanction      val fused_lui32 = io.in(i).bits.selImm === SelImm.IMM_LUI32 && io.in(i).bits.fuType === FuType.alu.U
435765e58c6Ssinsanction      when (fused_lui32) {
436765e58c6Ssinsanction        val lui_imm = io.in(i).bits.imm(19, 0)
437765e58c6Ssinsanction        val add_imm = io.in(i + 1).bits.imm(11, 0)
43849f433deSXuan Hu        require(io.out(i).bits.imm.getWidth >= lui_imm.getWidth + add_imm.getWidth)
43949f433deSXuan Hu        io.out(i).bits.imm := Cat(lui_imm, add_imm)
440765e58c6Ssinsanction      }
441765e58c6Ssinsanction    }
442765e58c6Ssinsanction
4438b8e745dSYikeZhou    // write speculative rename table
44439d3280eSYikeZhou    // we update rat later inside commit code
4456b102a39SHaojin Tang    intSpecWen(i) := needIntDest(i) && intFreeList.io.canAllocate && intFreeList.io.doAllocate && !io.rabCommits.isWalk && !io.redirect.valid
4466b102a39SHaojin Tang    fpSpecWen(i)  := needFpDest(i)  && fpFreeList.io.canAllocate  && fpFreeList.io.doAllocate  && !io.rabCommits.isWalk && !io.redirect.valid
4474eebf274Ssinsanction    vecSpecWen(i) := needVecDest(i) && vecFreeList.io.canAllocate && vecFreeList.io.doAllocate && !io.rabCommits.isWalk && !io.redirect.valid
448368cbcecSxiaofeibao    v0SpecWen(i) := needV0Dest(i) && v0FreeList.io.canAllocate && v0FreeList.io.doAllocate && !io.rabCommits.isWalk && !io.redirect.valid
449368cbcecSxiaofeibao    vlSpecWen(i) := needVlDest(i) && vlFreeList.io.canAllocate && vlFreeList.io.doAllocate && !io.rabCommits.isWalk && !io.redirect.valid
450ac78003fSzhanglyGit
45170224bf6SYinan Xu
452780712aaSxiaofeibao-xjtu    if (i < RabCommitWidth) {
453ccfddc82SHaojin Tang      walkIntSpecWen(i) := walkNeedIntDest(i) && !io.redirect.valid
4546b102a39SHaojin Tang      walkPdest(i) := io.rabCommits.info(i).pdest
455ccfddc82SHaojin Tang    } else {
456ccfddc82SHaojin Tang      walkPdest(i) := io.out(i).bits.pdest
457ccfddc82SHaojin Tang    }
458b034d3b9SLinJiawei  }
459b034d3b9SLinJiawei
46070224bf6SYinan Xu  /**
46149162c9aSGuanghui Cheng   * trace begin
46249162c9aSGuanghui Cheng   */
463b720b0cdSchengguanghui  // note: fusionInst can't robcompress
46449162c9aSGuanghui Cheng  val inVec = io.in.map(_.bits)
46549162c9aSGuanghui Cheng  val isRVCVec = inVec.map(_.preDecodeInfo.isRVC)
466b720b0cdSchengguanghui  val isFusionVec = inVec.map(_.commitType).map(ctype => CommitType.isFused(ctype))
467b720b0cdSchengguanghui
468b720b0cdSchengguanghui  val canRobCompressVec = compressUnit.io.out.canCompressVec
469b720b0cdSchengguanghui  val iLastSizeVec = isRVCVec.map(isRVC => Mux(isRVC, Ilastsize.HalfWord, Ilastsize.Word))
470b720b0cdSchengguanghui  val halfWordNumVec = isRVCVec.map(isRVC => Mux(isRVC, 1.U, 2.U))
471b720b0cdSchengguanghui  val halfWordNumMatrix = (0 until RenameWidth).map(
472b720b0cdSchengguanghui    i => compressMasksVec(i).asBools.zipWithIndex.map{ case(mask, j) =>
473b720b0cdSchengguanghui      Mux(mask, halfWordNumVec(j), 0.U)
47449162c9aSGuanghui Cheng    }
475b720b0cdSchengguanghui  )
47649162c9aSGuanghui Cheng
47749162c9aSGuanghui Cheng  for (i <- 0 until RenameWidth) {
47849162c9aSGuanghui Cheng    // iretire
47949162c9aSGuanghui Cheng    uops(i).traceBlockInPipe.iretire := Mux(canRobCompressVec(i),
480b720b0cdSchengguanghui      halfWordNumMatrix(i).reduce(_ +& _),
481b720b0cdSchengguanghui      (if(i < RenameWidth -1) Mux(isFusionVec(i), halfWordNumVec(i+1), 0.U) else 0.U) +& halfWordNumVec(i)
48249162c9aSGuanghui Cheng    )
48349162c9aSGuanghui Cheng
48449162c9aSGuanghui Cheng    // ilastsize
485b720b0cdSchengguanghui    val tmp = i
48649162c9aSGuanghui Cheng    val lastIsRVC = WireInit(false.B)
487b720b0cdSchengguanghui    (tmp until RenameWidth).map { j =>
48849162c9aSGuanghui Cheng      when(compressMasksVec(i)(j)) {
48949162c9aSGuanghui Cheng        lastIsRVC := io.in(j).bits.preDecodeInfo.isRVC
49049162c9aSGuanghui Cheng      }
49149162c9aSGuanghui Cheng    }
49249162c9aSGuanghui Cheng    uops(i).traceBlockInPipe.ilastsize := Mux(canRobCompressVec(i),
49349162c9aSGuanghui Cheng      Mux(lastIsRVC, Ilastsize.HalfWord, Ilastsize.Word),
494b720b0cdSchengguanghui      (if(i < RenameWidth -1) Mux(isFusionVec(i), iLastSizeVec(i+1), iLastSizeVec(i)) else iLastSizeVec(i))
49549162c9aSGuanghui Cheng    )
49649162c9aSGuanghui Cheng
49749162c9aSGuanghui Cheng    // itype
49849162c9aSGuanghui Cheng    uops(i).traceBlockInPipe.itype := Itype.jumpTypeGen(inVec(i).preDecodeInfo.brType, inVec(i).ldest.asTypeOf(new OpRegType), inVec(i).lsrc(0).asTypeOf((new OpRegType)))
49949162c9aSGuanghui Cheng  }
50049162c9aSGuanghui Cheng  /**
50149162c9aSGuanghui Cheng   * trace end
50249162c9aSGuanghui Cheng   */
50349162c9aSGuanghui Cheng
50449162c9aSGuanghui Cheng  /**
50570224bf6SYinan Xu    * How to set psrc:
50670224bf6SYinan Xu    * - bypass the pdest to psrc if previous instructions write to the same ldest as lsrc
50770224bf6SYinan Xu    * - default: psrc from RAT
50870224bf6SYinan Xu    * How to set pdest:
50970224bf6SYinan Xu    * - Mux(isMove, psrc, pdest_from_freelist).
51070224bf6SYinan Xu    *
51170224bf6SYinan Xu    * The critical path of rename lies here:
51270224bf6SYinan Xu    * When move elimination is enabled, we need to update the rat with psrc.
51370224bf6SYinan Xu    * However, psrc maybe comes from previous instructions' pdest, which comes from freelist.
51470224bf6SYinan Xu    *
51570224bf6SYinan Xu    * If we expand these logic for pdest(N):
51670224bf6SYinan Xu    * pdest(N) = Mux(isMove(N), psrc(N), freelist_out(N))
51770224bf6SYinan Xu    *          = Mux(isMove(N), Mux(bypass(N, N - 1), pdest(N - 1),
51870224bf6SYinan Xu    *                           Mux(bypass(N, N - 2), pdest(N - 2),
51970224bf6SYinan Xu    *                           ...
52070224bf6SYinan Xu    *                           Mux(bypass(N, 0),     pdest(0),
52170224bf6SYinan Xu    *                                                 rat_out(N))...)),
52270224bf6SYinan Xu    *                           freelist_out(N))
52370224bf6SYinan Xu    */
52470224bf6SYinan Xu  // a simple functional model for now
52570224bf6SYinan Xu  io.out(0).bits.pdest := Mux(isMove(0), uops(0).psrc.head, uops(0).pdest)
5263b739f49SXuan Hu
5273b739f49SXuan Hu  // psrc(n) + pdest(1)
528b9dc808dSJinHong Zeng  val bypassCond: Vec[MixedVec[UInt]] = Wire(Vec(numRegSrc, MixedVec(List.tabulate(RenameWidth-1)(i => UInt((i+1).W)))))
52998639abbSXuan Hu  require(io.in(0).bits.srcType.size == io.in(0).bits.numSrc)
53098639abbSXuan Hu  private val pdestLoc = io.in.head.bits.srcType.size // 2 vector src: v0, vl&vtype
5313b739f49SXuan Hu  println(s"[Rename] idx of pdest in bypassCond $pdestLoc")
53299b8dc2cSYinan Xu  for (i <- 1 until RenameWidth) {
533368cbcecSxiaofeibao    val v0Cond = io.in(i).bits.srcType.zipWithIndex.map{ case (s, i) =>
534368cbcecSxiaofeibao      if (i == 3) (s === SrcType.vp) || (s === SrcType.v0)
535368cbcecSxiaofeibao      else false.B
536b9dc808dSJinHong Zeng    }
537368cbcecSxiaofeibao    val vlCond = io.in(i).bits.srcType.zipWithIndex.map{ case (s, i) =>
538368cbcecSxiaofeibao      if (i == 4) s === SrcType.vp
539368cbcecSxiaofeibao      else false.B
540b9dc808dSJinHong Zeng    }
541b9dc808dSJinHong Zeng    val vecCond = io.in(i).bits.srcType.map(_ === SrcType.vp)
542b9dc808dSJinHong Zeng    val fpCond  = io.in(i).bits.srcType.map(_ === SrcType.fp)
543b9dc808dSJinHong Zeng    val intCond = io.in(i).bits.srcType.map(_ === SrcType.xp)
544b9dc808dSJinHong Zeng    val target = io.in(i).bits.lsrc
545368cbcecSxiaofeibao    for ((((((cond1, (condV0, condVl)), cond2), cond3), t), j) <- vecCond.zip(v0Cond.zip(vlCond)).zip(fpCond).zip(intCond).zip(target).zipWithIndex) {
54670224bf6SYinan Xu      val destToSrc = io.in.take(i).zipWithIndex.map { case (in, j) =>
5473b739f49SXuan Hu        val indexMatch = in.bits.ldest === t
548deb6421eSHaojin Tang        val writeMatch =  cond3 && needIntDest(j) || cond2 && needFpDest(j) || cond1 && needVecDest(j)
549368cbcecSxiaofeibao        val v0vlMatch = condV0 && needV0Dest(j) || condVl && needVlDest(j)
550368cbcecSxiaofeibao        indexMatch && writeMatch || v0vlMatch
55170224bf6SYinan Xu      }
55270224bf6SYinan Xu      bypassCond(j)(i - 1) := VecInit(destToSrc).asUInt
55370224bf6SYinan Xu    }
5540a7d1d5cSxiaofeibao    // For the LUI instruction: psrc(0) is from register file and should always be zero.
5550a7d1d5cSxiaofeibao    io.out(i).bits.psrc(0) := Mux(io.out(i).bits.isLUI, 0.U, io.out.take(i).map(_.bits.pdest).zip(bypassCond(0)(i-1).asBools).foldLeft(uops(i).psrc(0)) {
55670224bf6SYinan Xu      (z, next) => Mux(next._2, next._1, z)
5570a7d1d5cSxiaofeibao    })
55870224bf6SYinan Xu    io.out(i).bits.psrc(1) := io.out.take(i).map(_.bits.pdest).zip(bypassCond(1)(i-1).asBools).foldLeft(uops(i).psrc(1)) {
55970224bf6SYinan Xu      (z, next) => Mux(next._2, next._1, z)
56070224bf6SYinan Xu    }
56170224bf6SYinan Xu    io.out(i).bits.psrc(2) := io.out.take(i).map(_.bits.pdest).zip(bypassCond(2)(i-1).asBools).foldLeft(uops(i).psrc(2)) {
56270224bf6SYinan Xu      (z, next) => Mux(next._2, next._1, z)
56370224bf6SYinan Xu    }
564a7a8a6ccSHaojin Tang    io.out(i).bits.psrc(3) := io.out.take(i).map(_.bits.pdest).zip(bypassCond(3)(i-1).asBools).foldLeft(uops(i).psrc(3)) {
565a7a8a6ccSHaojin Tang      (z, next) => Mux(next._2, next._1, z)
566a7a8a6ccSHaojin Tang    }
567996aacc9SXuan Hu    io.out(i).bits.psrc(4) := io.out.take(i).map(_.bits.pdest).zip(bypassCond(4)(i-1).asBools).foldLeft(uops(i).psrc(4)) {
5683b739f49SXuan Hu      (z, next) => Mux(next._2, next._1, z)
5693b739f49SXuan Hu    }
57070224bf6SYinan Xu    io.out(i).bits.pdest := Mux(isMove(i), io.out(i).bits.psrc(0), uops(i).pdest)
571fd7603d9SYinan Xu
5723b739f49SXuan Hu    // Todo: better implementation for fields reuse
573fd7603d9SYinan Xu    // For fused-lui-load, load.src(0) is replaced by the imm.
5743b739f49SXuan Hu    val last_is_lui = io.in(i - 1).bits.selImm === SelImm.IMM_U && io.in(i - 1).bits.srcType(0) =/= SrcType.pc
5753b739f49SXuan Hu    val this_is_load = io.in(i).bits.fuType === FuType.ldu.U
576398aeef6STang Haojin    val lui_to_load = io.in(i - 1).valid && io.in(i - 1).bits.rfWen && io.in(i - 1).bits.ldest === io.in(i).bits.lsrc(0)
577f4dcd9fcSsinsanction    val fused_lui_load = last_is_lui && this_is_load && lui_to_load
578fd7603d9SYinan Xu    when (fused_lui_load) {
57949f433deSXuan Hu      // The first LOAD operand (base address) is replaced by LUI-imm and stored in imm
58049f433deSXuan Hu      val lui_imm = io.in(i - 1).bits.imm(ImmUnion.U.len - 1, 0)
58149f433deSXuan Hu      val ld_imm = io.in(i).bits.imm(ImmUnion.I.len - 1, 0)
58249f433deSXuan Hu      require(io.out(i).bits.imm.getWidth >= lui_imm.getWidth + ld_imm.getWidth)
5833b739f49SXuan Hu      io.out(i).bits.srcType(0) := SrcType.imm
58449f433deSXuan Hu      io.out(i).bits.imm := Cat(lui_imm, ld_imm)
585fd7603d9SYinan Xu    }
586fd7603d9SYinan Xu
587b034d3b9SLinJiawei  }
58800ad41d0SYinan Xu
589c4b56310SHaojin Tang  val genSnapshot = Cat(io.out.map(out => out.fire && out.bits.snapshot)).orR
590bb7e6e3aSxiaofeibao-xjtu  val lastCycleCreateSnpt = RegInit(false.B)
591bb7e6e3aSxiaofeibao-xjtu  lastCycleCreateSnpt := genSnapshot && !io.snptIsFull
592bb7e6e3aSxiaofeibao-xjtu  val sameSnptDistance = (RobCommitWidth * 4).U
593bb7e6e3aSxiaofeibao-xjtu  // notInSameSnpt: 1.robidxHead - snapLastEnq >= sameSnptDistance 2.no snap
594bb7e6e3aSxiaofeibao-xjtu  val notInSameSnpt = GatedValidRegNext(distanceBetween(robIdxHeadNext, io.snptLastEnq.bits) >= sameSnptDistance || !io.snptLastEnq.valid)
595bb7e6e3aSxiaofeibao-xjtu  val allowSnpt = if (EnableRenameSnapshot) notInSameSnpt && !lastCycleCreateSnpt && io.in.head.bits.firstUop else false.B
596c4b56310SHaojin Tang  io.out.zip(io.in).foreach{ case (out, in) => out.bits.snapshot := allowSnpt && (!in.bits.preDecodeInfo.notCFI || FuType.isJump(in.bits.fuType)) && in.fire }
5978daac0bfSxiaofeibao-xjtu  io.out.map{ x =>
5987e0f64b0SGuanghui Cheng    x.bits.hasException := Cat(selectFrontend(x.bits.exceptionVec) :+ x.bits.exceptionVec(illegalInstr) :+ x.bits.exceptionVec(virtualInstr)).orR || TriggerAction.isDmode(x.bits.trigger)
5998daac0bfSxiaofeibao-xjtu  }
600780712aaSxiaofeibao-xjtu  if(backendParams.debugEn){
601780712aaSxiaofeibao-xjtu    dontTouch(robIdxHeadNext)
602780712aaSxiaofeibao-xjtu    dontTouch(notInSameSnpt)
603780712aaSxiaofeibao-xjtu    dontTouch(genSnapshot)
604fa7f2c26STang Haojin  }
605fa7f2c26STang Haojin  intFreeList.io.snpt := io.snpt
606fa7f2c26STang Haojin  fpFreeList.io.snpt := io.snpt
6074eebf274Ssinsanction  vecFreeList.io.snpt := io.snpt
608368cbcecSxiaofeibao  v0FreeList.io.snpt := io.snpt
609368cbcecSxiaofeibao  vlFreeList.io.snpt := io.snpt
610c4b56310SHaojin Tang  intFreeList.io.snpt.snptEnq := genSnapshot
611c4b56310SHaojin Tang  fpFreeList.io.snpt.snptEnq := genSnapshot
6124eebf274Ssinsanction  vecFreeList.io.snpt.snptEnq := genSnapshot
613368cbcecSxiaofeibao  v0FreeList.io.snpt.snptEnq := genSnapshot
614368cbcecSxiaofeibao  vlFreeList.io.snpt.snptEnq := genSnapshot
615fa7f2c26STang Haojin
61600ad41d0SYinan Xu  /**
61700ad41d0SYinan Xu    * Instructions commit: update freelist and rename table
61800ad41d0SYinan Xu    */
619780712aaSxiaofeibao-xjtu  for (i <- 0 until RabCommitWidth) {
6206b102a39SHaojin Tang    val commitValid = io.rabCommits.isCommit && io.rabCommits.commitValid(i)
6216b102a39SHaojin Tang    val walkValid = io.rabCommits.isWalk && io.rabCommits.walkValid(i)
62200ad41d0SYinan Xu
623deb6421eSHaojin Tang    // I. RAT Update
6247fa2c198SYinan Xu    // When redirect happens (mis-prediction), don't update the rename table
625deb6421eSHaojin Tang    io.intRenamePorts(i).wen  := intSpecWen(i)
626ad5c9e6eSJunxiong Ji    io.intRenamePorts(i).addr := uops(i).ldest(log2Ceil(IntLogicRegs) - 1, 0)
627deb6421eSHaojin Tang    io.intRenamePorts(i).data := io.out(i).bits.pdest
6288b8e745dSYikeZhou
629deb6421eSHaojin Tang    io.fpRenamePorts(i).wen  := fpSpecWen(i)
630ad5c9e6eSJunxiong Ji    io.fpRenamePorts(i).addr := uops(i).ldest(log2Ceil(FpLogicRegs) - 1, 0)
631deb6421eSHaojin Tang    io.fpRenamePorts(i).data := fpFreeList.io.allocatePhyReg(i)
632deb6421eSHaojin Tang
633deb6421eSHaojin Tang    io.vecRenamePorts(i).wen := vecSpecWen(i)
634ad5c9e6eSJunxiong Ji    io.vecRenamePorts(i).addr := uops(i).ldest(log2Ceil(VecLogicRegs) - 1, 0)
6354eebf274Ssinsanction    io.vecRenamePorts(i).data := vecFreeList.io.allocatePhyReg(i)
636deb6421eSHaojin Tang
637368cbcecSxiaofeibao    io.v0RenamePorts(i).wen := v0SpecWen(i)
638ad5c9e6eSJunxiong Ji    io.v0RenamePorts(i).addr := uops(i).ldest(log2Ceil(V0LogicRegs) - 1, 0)
639368cbcecSxiaofeibao    io.v0RenamePorts(i).data := v0FreeList.io.allocatePhyReg(i)
640368cbcecSxiaofeibao
641368cbcecSxiaofeibao    io.vlRenamePorts(i).wen := vlSpecWen(i)
642ad5c9e6eSJunxiong Ji    io.vlRenamePorts(i).addr := uops(i).ldest(log2Ceil(VlLogicRegs) - 1, 0)
643368cbcecSxiaofeibao    io.vlRenamePorts(i).data := vlFreeList.io.allocatePhyReg(i)
644368cbcecSxiaofeibao
645deb6421eSHaojin Tang    // II. Free List Update
646dcf3a679STang Haojin    intFreeList.io.freeReq(i) := io.int_need_free(i)
647dcf3a679STang Haojin    intFreeList.io.freePhyReg(i) := RegNext(io.int_old_pdest(i))
6484eebf274Ssinsanction    fpFreeList.io.freeReq(i)  := GatedValidRegNext(commitValid && needDestRegCommit(Reg_F, io.rabCommits.info(i)))
6497042bac3Ssinsanction    fpFreeList.io.freePhyReg(i) := io.fp_old_pdest(i)
6504eebf274Ssinsanction    vecFreeList.io.freeReq(i)  := GatedValidRegNext(commitValid && needDestRegCommit(Reg_V, io.rabCommits.info(i)))
6517042bac3Ssinsanction    vecFreeList.io.freePhyReg(i) := io.vec_old_pdest(i)
652368cbcecSxiaofeibao    v0FreeList.io.freeReq(i) := GatedValidRegNext(commitValid && needDestRegCommit(Reg_V0, io.rabCommits.info(i)))
653f6e3bebeSxiaofeibao    v0FreeList.io.freePhyReg(i) := io.v0_old_pdest(i)
654368cbcecSxiaofeibao    vlFreeList.io.freeReq(i) := GatedValidRegNext(commitValid && needDestRegCommit(Reg_Vl, io.rabCommits.info(i)))
655f6e3bebeSxiaofeibao    vlFreeList.io.freePhyReg(i) := io.vl_old_pdest(i)
6568b8e745dSYikeZhou  }
6578b8e745dSYikeZhou
6588b8e745dSYikeZhou  /*
65970224bf6SYinan Xu  Debug and performance counters
6608b8e745dSYikeZhou   */
6613b739f49SXuan Hu  def printRenameInfo(in: DecoupledIO[DecodedInst], out: DecoupledIO[DynInst]) = {
6623b739f49SXuan Hu    XSInfo(out.fire, p"pc:${Hexadecimal(in.bits.pc)} in(${in.valid},${in.ready}) " +
6633b739f49SXuan Hu      p"lsrc(0):${in.bits.lsrc(0)} -> psrc(0):${out.bits.psrc(0)} " +
6643b739f49SXuan Hu      p"lsrc(1):${in.bits.lsrc(1)} -> psrc(1):${out.bits.psrc(1)} " +
6653b739f49SXuan Hu      p"lsrc(2):${in.bits.lsrc(2)} -> psrc(2):${out.bits.psrc(2)} " +
666c61abc0cSXuan Hu      p"ldest:${in.bits.ldest} -> pdest:${out.bits.pdest}\n"
6678b8e745dSYikeZhou    )
6688b8e745dSYikeZhou  }
6698b8e745dSYikeZhou
6708b8e745dSYikeZhou  for ((x,y) <- io.in.zip(io.out)) {
6718b8e745dSYikeZhou    printRenameInfo(x, y)
6728b8e745dSYikeZhou  }
6738b8e745dSYikeZhou
67442bcc716Sxiaofeibao-xjtu  io.out.map { case x =>
67542bcc716Sxiaofeibao-xjtu    when(x.valid && x.bits.rfWen){
67642bcc716Sxiaofeibao-xjtu      assert(x.bits.ldest =/= 0.U, "rfWen cannot be 1 when Int regfile ldest is 0")
67742bcc716Sxiaofeibao-xjtu    }
67842bcc716Sxiaofeibao-xjtu  }
679d2b20d1aSTang Haojin  val debugRedirect = RegEnable(io.redirect.bits, io.redirect.valid)
680d2b20d1aSTang Haojin  // bad speculation
6816b102a39SHaojin Tang  val recStall = io.redirect.valid || io.rabCommits.isWalk
6826b102a39SHaojin Tang  val ctrlRecStall = Mux(io.redirect.valid, io.redirect.bits.debugIsCtrl, io.rabCommits.isWalk && debugRedirect.debugIsCtrl)
6836b102a39SHaojin Tang  val mvioRecStall = Mux(io.redirect.valid, io.redirect.bits.debugIsMemVio, io.rabCommits.isWalk && debugRedirect.debugIsMemVio)
684d2b20d1aSTang Haojin  val otherRecStall = recStall && !(ctrlRecStall || mvioRecStall)
685d2b20d1aSTang Haojin  XSPerfAccumulate("recovery_stall", recStall)
686d2b20d1aSTang Haojin  XSPerfAccumulate("control_recovery_stall", ctrlRecStall)
687d2b20d1aSTang Haojin  XSPerfAccumulate("mem_violation_recovery_stall", mvioRecStall)
688d2b20d1aSTang Haojin  XSPerfAccumulate("other_recovery_stall", otherRecStall)
689d2b20d1aSTang Haojin  // freelist stall
690d2b20d1aSTang Haojin  val notRecStall = !io.out.head.valid && !recStall
691368cbcecSxiaofeibao  val intFlStall = notRecStall && inHeadValid && fpFreeList.io.canAllocate && vecFreeList.io.canAllocate && v0FreeList.io.canAllocate && vlFreeList.io.canAllocate && !intFreeList.io.canAllocate
692368cbcecSxiaofeibao  val fpFlStall = notRecStall && inHeadValid && intFreeList.io.canAllocate && vecFreeList.io.canAllocate && v0FreeList.io.canAllocate && vlFreeList.io.canAllocate && !fpFreeList.io.canAllocate
693368cbcecSxiaofeibao  val vecFlStall = notRecStall && inHeadValid && intFreeList.io.canAllocate && fpFreeList.io.canAllocate && v0FreeList.io.canAllocate && vlFreeList.io.canAllocate && !vecFreeList.io.canAllocate
694368cbcecSxiaofeibao  val v0FlStall = notRecStall && inHeadValid && intFreeList.io.canAllocate && fpFreeList.io.canAllocate && vecFreeList.io.canAllocate && vlFreeList.io.canAllocate && !v0FreeList.io.canAllocate
695368cbcecSxiaofeibao  val vlFlStall = notRecStall && inHeadValid && intFreeList.io.canAllocate && fpFreeList.io.canAllocate && vecFreeList.io.canAllocate && v0FreeList.io.canAllocate && !vlFreeList.io.canAllocate
696368cbcecSxiaofeibao  val multiFlStall = notRecStall && inHeadValid && (PopCount(Cat(
697368cbcecSxiaofeibao    !intFreeList.io.canAllocate,
698368cbcecSxiaofeibao    !fpFreeList.io.canAllocate,
699368cbcecSxiaofeibao    !vecFreeList.io.canAllocate,
700368cbcecSxiaofeibao    !v0FreeList.io.canAllocate,
701368cbcecSxiaofeibao    !vlFreeList.io.canAllocate,
702368cbcecSxiaofeibao  )) > 1.U)
703d2b20d1aSTang Haojin  // other stall
704368cbcecSxiaofeibao  val otherStall = notRecStall && !intFlStall && !fpFlStall && !vecFlStall && !v0FlStall && !vlFlStall && !multiFlStall
705d2b20d1aSTang Haojin
706d2b20d1aSTang Haojin  io.stallReason.in.backReason.valid := io.stallReason.out.backReason.valid || !io.in.head.ready
707d2b20d1aSTang Haojin  io.stallReason.in.backReason.bits := Mux(io.stallReason.out.backReason.valid, io.stallReason.out.backReason.bits,
708d2b20d1aSTang Haojin    MuxCase(TopDownCounters.OtherCoreStall.id.U, Seq(
709d2b20d1aSTang Haojin      ctrlRecStall  -> TopDownCounters.ControlRecoveryStall.id.U,
710d2b20d1aSTang Haojin      mvioRecStall  -> TopDownCounters.MemVioRecoveryStall.id.U,
711d2b20d1aSTang Haojin      otherRecStall -> TopDownCounters.OtherRecoveryStall.id.U,
712d2b20d1aSTang Haojin      intFlStall    -> TopDownCounters.IntFlStall.id.U,
7134eebf274Ssinsanction      fpFlStall     -> TopDownCounters.FpFlStall.id.U,
7144eebf274Ssinsanction      vecFlStall    -> TopDownCounters.VecFlStall.id.U,
715368cbcecSxiaofeibao      v0FlStall     -> TopDownCounters.V0FlStall.id.U,
716368cbcecSxiaofeibao      vlFlStall     -> TopDownCounters.VlFlStall.id.U,
717368cbcecSxiaofeibao      multiFlStall  -> TopDownCounters.MultiFlStall.id.U,
718d2b20d1aSTang Haojin    )
719d2b20d1aSTang Haojin  ))
720d2b20d1aSTang Haojin  io.stallReason.out.reason.zip(io.stallReason.in.reason).zip(io.in.map(_.valid)).foreach { case ((out, in), valid) =>
7210adf86dcSHaojin Tang    out := Mux(io.stallReason.in.backReason.valid, io.stallReason.in.backReason.bits, in)
722d2b20d1aSTang Haojin  }
723d2b20d1aSTang Haojin
7246b102a39SHaojin Tang  XSDebug(io.rabCommits.isWalk, p"Walk Recovery Enabled\n")
7256b102a39SHaojin Tang  XSDebug(io.rabCommits.isWalk, p"validVec:${Binary(io.rabCommits.walkValid.asUInt)}\n")
726780712aaSxiaofeibao-xjtu  for (i <- 0 until RabCommitWidth) {
7276b102a39SHaojin Tang    val info = io.rabCommits.info(i)
7286b102a39SHaojin Tang    XSDebug(io.rabCommits.isWalk && io.rabCommits.walkValid(i), p"[#$i walk info] " +
729368cbcecSxiaofeibao      p"ldest:${info.ldest} rfWen:${info.rfWen} fpWen:${info.fpWen} vecWen:${info.vecWen} v0Wen:${info.v0Wen} vlWen:${info.vlWen}")
7308b8e745dSYikeZhou  }
7318b8e745dSYikeZhou
7328b8e745dSYikeZhou  XSDebug(p"inValidVec: ${Binary(Cat(io.in.map(_.valid)))}\n")
7338b8e745dSYikeZhou
734a63155a6SXuan Hu  XSPerfAccumulate("in_valid_count", PopCount(io.in.map(_.valid)))
735a63155a6SXuan Hu  XSPerfAccumulate("in_fire_count", PopCount(io.in.map(_.fire)))
736a63155a6SXuan Hu  XSPerfAccumulate("in_valid_not_ready_count", PopCount(io.in.map(x => x.valid && !x.ready)))
7376374b1d6SXuan Hu  XSPerfAccumulate("wait_cycle", !io.in.head.valid && dispatchCanAcc)
7385eb4af5bSYikeZhou
739a63155a6SXuan Hu  // These stall reasons could overlap each other, but we configure the priority as fellows.
740a63155a6SXuan Hu  // walk stall > dispatch stall > int freelist stall > fp freelist stall
741a63155a6SXuan Hu  private val inHeadStall = io.in.head match { case x => x.valid && !x.ready }
7426b102a39SHaojin Tang  private val stallForWalk      = inHeadValid &&  io.rabCommits.isWalk
7436374b1d6SXuan Hu  private val stallForDispatch  = inHeadValid && !io.rabCommits.isWalk && !dispatchCanAcc
744368cbcecSxiaofeibao  private val stallForIntFL     = inHeadValid && !io.rabCommits.isWalk && dispatchCanAcc && fpFreeList.io.canAllocate && vecFreeList.io.canAllocate && v0FreeList.io.canAllocate && vlFreeList.io.canAllocate && !intFreeList.io.canAllocate
745368cbcecSxiaofeibao  private val stallForFpFL      = inHeadValid && !io.rabCommits.isWalk && dispatchCanAcc && intFreeList.io.canAllocate && vecFreeList.io.canAllocate && v0FreeList.io.canAllocate && vlFreeList.io.canAllocate && !fpFreeList.io.canAllocate
746368cbcecSxiaofeibao  private val stallForVecFL     = inHeadValid && !io.rabCommits.isWalk && dispatchCanAcc && intFreeList.io.canAllocate && fpFreeList.io.canAllocate && v0FreeList.io.canAllocate && vlFreeList.io.canAllocate && !vecFreeList.io.canAllocate
747368cbcecSxiaofeibao  private val stallForV0FL      = inHeadValid && !io.rabCommits.isWalk && dispatchCanAcc && intFreeList.io.canAllocate && fpFreeList.io.canAllocate && vecFreeList.io.canAllocate && vlFreeList.io.canAllocate && !v0FreeList.io.canAllocate
748368cbcecSxiaofeibao  private val stallForVlFL      = inHeadValid && !io.rabCommits.isWalk && dispatchCanAcc && intFreeList.io.canAllocate && fpFreeList.io.canAllocate && vecFreeList.io.canAllocate && v0FreeList.io.canAllocate && !vlFreeList.io.canAllocate
749a63155a6SXuan Hu  XSPerfAccumulate("stall_cycle",          inHeadStall)
750a63155a6SXuan Hu  XSPerfAccumulate("stall_cycle_walk",     stallForWalk)
751a63155a6SXuan Hu  XSPerfAccumulate("stall_cycle_dispatch", stallForDispatch)
752a63155a6SXuan Hu  XSPerfAccumulate("stall_cycle_int",      stallForIntFL)
753a63155a6SXuan Hu  XSPerfAccumulate("stall_cycle_fp",       stallForFpFL)
7544eebf274Ssinsanction  XSPerfAccumulate("stall_cycle_vec",      stallForVecFL)
755368cbcecSxiaofeibao  XSPerfAccumulate("stall_cycle_vec",      stallForV0FL)
756368cbcecSxiaofeibao  XSPerfAccumulate("stall_cycle_vec",      stallForVlFL)
757a63155a6SXuan Hu
758a63155a6SXuan Hu  XSPerfHistogram("in_valid_range",  PopCount(io.in.map(_.valid)),  true.B, 0, DecodeWidth + 1, 1)
759a63155a6SXuan Hu  XSPerfHistogram("in_fire_range",   PopCount(io.in.map(_.fire)),   true.B, 0, DecodeWidth + 1, 1)
760a63155a6SXuan Hu  XSPerfHistogram("out_valid_range", PopCount(io.out.map(_.valid)), true.B, 0, DecodeWidth + 1, 1)
761a63155a6SXuan Hu  XSPerfHistogram("out_fire_range",  PopCount(io.out.map(_.fire)),  true.B, 0, DecodeWidth + 1, 1)
762d8aa3d57SbugGenerator
7633b739f49SXuan Hu  XSPerfAccumulate("move_instr_count", PopCount(io.out.map(out => out.fire && out.bits.isMove)))
7643b739f49SXuan Hu  val is_fused_lui_load = io.out.map(o => o.fire && o.bits.fuType === FuType.ldu.U && o.bits.srcType(0) === SrcType.imm)
765fd7603d9SYinan Xu  XSPerfAccumulate("fused_lui_load_instr_count", PopCount(is_fused_lui_load))
766cd365d4cSrvcoresjw
7671ca0e4f3SYinan Xu  val renamePerf = Seq(
768cd365d4cSrvcoresjw    ("rename_in                  ", PopCount(io.in.map(_.valid & io.in(0).ready ))),
769cd365d4cSrvcoresjw    ("rename_waitinstr           ", PopCount((0 until RenameWidth).map(i => io.in(i).valid && !io.in(i).ready))),
770a63155a6SXuan Hu    ("rename_stall               ", inHeadStall),
7716b102a39SHaojin Tang    ("rename_stall_cycle_walk    ", inHeadValid &&  io.rabCommits.isWalk),
7726374b1d6SXuan Hu    ("rename_stall_cycle_dispatch", inHeadValid && !io.rabCommits.isWalk && !dispatchCanAcc),
773368cbcecSxiaofeibao    ("rename_stall_cycle_int     ", inHeadValid && !io.rabCommits.isWalk && dispatchCanAcc && fpFreeList.io.canAllocate && vecFreeList.io.canAllocate && v0FreeList.io.canAllocate && vlFreeList.io.canAllocate && !intFreeList.io.canAllocate),
774368cbcecSxiaofeibao    ("rename_stall_cycle_fp      ", inHeadValid && !io.rabCommits.isWalk && dispatchCanAcc && intFreeList.io.canAllocate && vecFreeList.io.canAllocate && v0FreeList.io.canAllocate && vlFreeList.io.canAllocate && !fpFreeList.io.canAllocate),
775368cbcecSxiaofeibao    ("rename_stall_cycle_vec     ", inHeadValid && !io.rabCommits.isWalk && dispatchCanAcc && intFreeList.io.canAllocate && fpFreeList.io.canAllocate && v0FreeList.io.canAllocate && vlFreeList.io.canAllocate && !vecFreeList.io.canAllocate),
776368cbcecSxiaofeibao    ("rename_stall_cycle_v0      ", inHeadValid && !io.rabCommits.isWalk && dispatchCanAcc && intFreeList.io.canAllocate && fpFreeList.io.canAllocate && vecFreeList.io.canAllocate && vlFreeList.io.canAllocate && !v0FreeList.io.canAllocate),
777368cbcecSxiaofeibao    ("rename_stall_cycle_vl      ", inHeadValid && !io.rabCommits.isWalk && dispatchCanAcc && intFreeList.io.canAllocate && fpFreeList.io.canAllocate && vecFreeList.io.canAllocate && v0FreeList.io.canAllocate && !vlFreeList.io.canAllocate),
778cd365d4cSrvcoresjw  )
7791ca0e4f3SYinan Xu  val intFlPerf = intFreeList.getPerfEvents
7801ca0e4f3SYinan Xu  val fpFlPerf = fpFreeList.getPerfEvents
7814eebf274Ssinsanction  val vecFlPerf = vecFreeList.getPerfEvents
782368cbcecSxiaofeibao  val v0FlPerf = v0FreeList.getPerfEvents
783368cbcecSxiaofeibao  val vlFlPerf = vlFreeList.getPerfEvents
784368cbcecSxiaofeibao  val perfEvents = renamePerf ++ intFlPerf ++ fpFlPerf ++ vecFlPerf ++ v0FlPerf ++ vlFlPerf
7851ca0e4f3SYinan Xu  generatePerfEvent()
7865eb4af5bSYikeZhou}
787