Rename.scala 21.0 KB
Newer Older
L
Lemover 已提交
1 2
/***************************************************************************************
* Copyright (c) 2020-2021 Institute of Computing Technology, Chinese Academy of Sciences
Y
Yinan Xu 已提交
3
* Copyright (c) 2020-2021 Peng Cheng Laboratory
L
Lemover 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16
*
* XiangShan is licensed under Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*          http://license.coscl.org.cn/MulanPSL2
*
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
*
* See the Mulan PSL v2 for more details.
***************************************************************************************/

17 18
package xiangshan.backend.rename

19
import chipsalliance.rocketchip.config.Parameters
20 21 22
import chisel3._
import chisel3.util._
import xiangshan._
Y
Yinan Xu 已提交
23
import utils._
24
import xiangshan.backend.roq.RoqPtr
25
import xiangshan.backend.dispatch.PreDispatchInfo
26

27
class RenameBypassInfo(implicit p: Parameters) extends XSBundle {
28 29 30 31 32 33
  val lsrc1_bypass = MixedVec(List.tabulate(RenameWidth-1)(i => UInt((i+1).W)))
  val lsrc2_bypass = MixedVec(List.tabulate(RenameWidth-1)(i => UInt((i+1).W)))
  val lsrc3_bypass = MixedVec(List.tabulate(RenameWidth-1)(i => UInt((i+1).W)))
  val ldest_bypass = MixedVec(List.tabulate(RenameWidth-1)(i => UInt((i+1).W)))
}

34
class Rename(implicit p: Parameters) extends XSModule {
35 36
  val io = IO(new Bundle() {
    val redirect = Flipped(ValidIO(new Redirect))
37
    val flush = Input(Bool())
Y
Yinan Xu 已提交
38
    val roqCommits = Flipped(new RoqCommitIO)
39
    // from decode buffer
40
    val in = Vec(RenameWidth, Flipped(DecoupledIO(new CfCtrl)))
41
    // to dispatch1
42
    val out = Vec(RenameWidth, DecoupledIO(new MicroOp))
43
    val renameBypass = Output(new RenameBypassInfo)
44
    val dispatchInfo = Output(new PreDispatchInfo)
45
    // for debug printing
46 47
    val debug_int_rat = Vec(32, Output(UInt(PhyRegIdxWidth.W)))
    val debug_fp_rat = Vec(32, Output(UInt(PhyRegIdxWidth.W)))
48
  })
49

50
  // create free list and rat
51 52
  val intFreeList = Module(if (EnableIntMoveElim) new freelist.MEFreeList else new freelist.StdFreeList)
  val fpFreeList = Module(new freelist.StdFreeList)
L
LinJiawei 已提交
53

54 55
  val intRat = Module(new RenameTable(float = false))
  val fpRat = Module(new RenameTable(float = true))
L
LinJiawei 已提交
56

57 58 59 60 61
  // connect flush and redirect ports for rat
  Seq(intRat, fpRat) foreach { case rat =>
    rat.io.redirect := io.redirect.valid
    rat.io.flush := io.flush
    rat.io.walkWen := io.roqCommits.isWalk
Y
Yinan Xu 已提交
62
  }
63

64 65 66 67 68 69 70 71
  // decide if given instruction needs allocating a new physical register (CfCtrl: from decode; RoqCommitInfo: from roq)
  def needDestReg[T <: CfCtrl](fp: Boolean, x: T): Bool = {
    {if(fp) x.ctrl.fpWen else x.ctrl.rfWen && (x.ctrl.ldest =/= 0.U)}
  }
  def needDestRegCommit[T <: RoqCommitInfo](fp: Boolean, x: T): Bool = {
    {if(fp) x.fpWen else x.rfWen && (x.ldest =/= 0.U)}
  }

72
  // connect [flush + redirect + walk] ports for __float point__ & __integer__ free list
73
  Seq((fpFreeList, true), (intFreeList, false)).foreach{ case (fl, isFp) =>
74 75 76 77 78
    fl.flush := io.flush
    fl.redirect := io.redirect.valid
    fl.walk := io.roqCommits.isWalk
    // when isWalk, use stepBack to restore head pointer of free list
    // (if ME enabled, stepBack of intFreeList should be useless thus optimized out)
79
    fl.stepBack := PopCount(io.roqCommits.valid.zip(io.roqCommits.info).map{case (v, i) => v && needDestRegCommit(isFp, i)})
80
  }
81 82 83 84
  // walk has higher priority than allocation and thus we don't use isWalk here
  // only when both fp and int free list and dispatch1 has enough space can we do allocation
  intFreeList.doAllocate := fpFreeList.canAllocate && io.out(0).ready
  fpFreeList.doAllocate := intFreeList.canAllocate && io.out(0).ready
85 86

  //           dispatch1 ready ++ float point free list ready ++ int free list ready      ++ not walk
87
  val canOut = io.out(0).ready && fpFreeList.canAllocate && intFreeList.canAllocate && !io.roqCommits.isWalk
88

89

90
  // speculatively assign the instruction with an roqIdx
91
  val validCount = PopCount(io.in.map(_.valid)) // number of instructions waiting to enter roq (from decode)
92
  val roqIdxHead = RegInit(0.U.asTypeOf(new RoqPtr))
93
  val lastCycleMisprediction = RegNext(io.redirect.valid && !io.redirect.bits.flushItself())
94 95 96 97 98
  val roqIdxHeadNext = Mux(io.flush, 0.U.asTypeOf(new RoqPtr), // flush: clear roq
              Mux(io.redirect.valid, io.redirect.bits.roqIdx, // redirect: move ptr to given roq index (flush itself)
         Mux(lastCycleMisprediction, roqIdxHead + 1.U, // mis-predict: not flush roqIdx itself
                         Mux(canOut, roqIdxHead + validCount, // instructions successfully entered next stage: increase roqIdx
                      /* default */  roqIdxHead)))) // no instructions passed by this cycle: stick to old value
99 100
  roqIdxHead := roqIdxHeadNext

101

Y
Yinan Xu 已提交
102 103 104
  /**
    * Rename: allocate free physical register and update rename table
    */
105 106
  val uops = Wire(Vec(RenameWidth, new MicroOp))
  uops.foreach( uop => {
107 108 109
    uop.srcState(0) := DontCare
    uop.srcState(1) := DontCare
    uop.srcState(2) := DontCare
110
    uop.roqIdx := DontCare
111
    uop.diffTestDebugLrScValid := DontCare
Y
Yinan Xu 已提交
112
    uop.debugInfo := DontCare
113 114
    uop.lqIdx := DontCare
    uop.sqIdx := DontCare
115 116
  })

117 118
  val needFpDest = Wire(Vec(RenameWidth, Bool()))
  val needIntDest = Wire(Vec(RenameWidth, Bool()))
119
  val hasValid = Cat(io.in.map(_.valid)).orR
120 121

  val isMove = io.in.map(_.bits.ctrl.isMove)
122
  val isMax = if (EnableIntMoveElim) Some(intFreeList.asInstanceOf[freelist.MEFreeList].maxVec) else None
123 124 125 126 127 128 129
  val meEnable = WireInit(VecInit(Seq.fill(RenameWidth)(false.B)))
  val psrc_cmp = Wire(MixedVec(List.tabulate(RenameWidth-1)(i => UInt((i+1).W))))

  val intSpecWen = Wire(Vec(RenameWidth, Bool()))
  val fpSpecWen = Wire(Vec(RenameWidth, Bool()))

  // uop calculation
Y
Yinan Xu 已提交
130
  for (i <- 0 until RenameWidth) {
131 132 133
    uops(i).cf := io.in(i).bits.cf
    uops(i).ctrl := io.in(i).bits.ctrl

134
    val inValid = io.in(i).valid
L
LinJiawei 已提交
135

136
    // alloc a new phy reg
137 138
    needFpDest(i) := inValid && needDestReg(fp = true, io.in(i).bits)
    needIntDest(i) := inValid && needDestReg(fp = false, io.in(i).bits)
139 140
    fpFreeList.allocateReq(i) := needFpDest(i)
    intFreeList.allocateReq(i) := needIntDest(i)
L
LinJiawei 已提交
141

142
    // no valid instruction from decode stage || all resources (dispatch1 + both free lists) ready
143
    io.in(i).ready := !hasValid || canOut
L
LinJiawei 已提交
144 145

    // do checkpoints when a branch inst come
Y
Yinan Xu 已提交
146 147 148 149
    // for(fl <- Seq(fpFreeList, intFreeList)){
    //   fl.cpReqs(i).valid := inValid
    //   fl.cpReqs(i).bits := io.in(i).bits.brTag
    // }
L
LinJiawei 已提交
150

151

152 153
    uops(i).roqIdx := roqIdxHead + i.U

154
    io.out(i).valid := io.in(i).valid && intFreeList.canAllocate && fpFreeList.canAllocate && !io.roqCommits.isWalk
155 156 157 158 159 160 161 162 163 164 165 166
    io.out(i).bits := uops(i)


    // read rename table
    def readRat(lsrcList: List[UInt], ldest: UInt, fp: Boolean) = {
      val rat = if(fp) fpRat else intRat
      val srcCnt = lsrcList.size
      val psrcVec = Wire(Vec(srcCnt, UInt(PhyRegIdxWidth.W)))
      val old_pdest = Wire(UInt(PhyRegIdxWidth.W))
      for(k <- 0 until srcCnt+1){
        val rportIdx = i * (srcCnt+1) + k
        if(k != srcCnt){
167 168
          rat.io.readPorts(rportIdx).addr := lsrcList(k)
          psrcVec(k) := rat.io.readPorts(rportIdx).rdata
169
        } else {
170 171
          rat.io.readPorts(rportIdx).addr := ldest
          old_pdest := rat.io.readPorts(rportIdx).rdata
172 173 174 175
        }
      }
      (psrcVec, old_pdest)
    }
176
    val lsrcList = List(uops(i).ctrl.lsrc(0), uops(i).ctrl.lsrc(1), uops(i).ctrl.lsrc(2))
177 178 179
    val ldest = uops(i).ctrl.ldest
    val (intPhySrcVec, intOldPdest) = readRat(lsrcList.take(2), ldest, fp = false)
    val (fpPhySrcVec, fpOldPdest) = readRat(lsrcList, ldest, fp = true)
180 181 182
    uops(i).psrc(0) := Mux(uops(i).ctrl.srcType(0) === SrcType.reg, intPhySrcVec(0), fpPhySrcVec(0))
    uops(i).psrc(1) := Mux(uops(i).ctrl.srcType(1) === SrcType.reg, intPhySrcVec(1), fpPhySrcVec(1))
    uops(i).psrc(2) := fpPhySrcVec(2)
183
    uops(i).old_pdest := Mux(uops(i).ctrl.rfWen, intOldPdest, fpOldPdest)
184

185
    if (EnableIntMoveElim) {
186

187 188
      if (i == 0) {
        // calculate meEnable
189
        meEnable(i) := isMove(i) && (!isMax.get(uops(i).psrc(0)) || uops(i).ctrl.lsrc(0) === 0.U)
190 191 192 193 194 195 196
      } else {
        // compare psrc0
        psrc_cmp(i-1) := Cat((0 until i).map(j => {
          uops(i).psrc(0) === uops(j).psrc(0) && io.in(i).bits.ctrl.isMove && io.in(j).bits.ctrl.isMove
        }) /* reverse is not necessary here */)
  
        // calculate meEnable
197
        meEnable(i) := isMove(i) && (!(io.renameBypass.lsrc1_bypass(i-1).orR | psrc_cmp(i-1).orR | isMax.get(uops(i).psrc(0))) || uops(i).ctrl.lsrc(0) === 0.U)
198
      }
199
      uops(i).eliminatedMove := meEnable(i) || (uops(i).ctrl.isMove && uops(i).ctrl.ldest === 0.U)
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
  
      // send psrc of eliminated move instructions to free list and label them as eliminated
      when (meEnable(i)) {
        intFreeList.asInstanceOf[freelist.MEFreeList].psrcOfMove(i).valid := true.B
        intFreeList.asInstanceOf[freelist.MEFreeList].psrcOfMove(i).bits := uops(i).psrc(0)
        XSInfo(io.in(i).valid && io.out(i).valid, p"Move instruction ${Hexadecimal(io.in(i).bits.cf.pc)} eliminated successfully! psrc:${uops(i).psrc(0)}\n")
      } .otherwise {
        intFreeList.asInstanceOf[freelist.MEFreeList].psrcOfMove(i).valid := false.B
        intFreeList.asInstanceOf[freelist.MEFreeList].psrcOfMove(i).bits := DontCare
        XSInfo(io.in(i).valid && io.out(i).valid && isMove(i), p"Move instruction ${Hexadecimal(io.in(i).bits.cf.pc)} failed to be eliminated! psrc:${uops(i).psrc(0)}\n")
      }
  
      // update pdest
      uops(i).pdest := Mux(meEnable(i), uops(i).psrc(0), // move eliminated
                       Mux(needIntDest(i), intFreeList.allocatePhyReg(i), // normal int inst
                       Mux(uops(i).ctrl.ldest===0.U && uops(i).ctrl.rfWen, 0.U // int inst with dst=r0
                       /* default */, fpFreeList.allocatePhyReg(i)))) // normal fp inst
    } else {
      uops(i).eliminatedMove := DontCare
      psrc_cmp.foreach(_ := DontCare)
      // update pdest
      uops(i).pdest := Mux(needIntDest(i), intFreeList.allocatePhyReg(i), // normal int inst
                       Mux(uops(i).ctrl.ldest===0.U && uops(i).ctrl.rfWen, 0.U // int inst with dst=r0
                       /* default */, fpFreeList.allocatePhyReg(i))) // normal fp inst
224 225 226
    }

    // write speculative rename table
227 228 229
    // we update rat later inside commit code
    intSpecWen(i) := intFreeList.allocateReq(i) && intFreeList.canAllocate && intFreeList.doAllocate && !io.roqCommits.isWalk
    fpSpecWen(i) := fpFreeList.allocateReq(i) && fpFreeList.canAllocate && fpFreeList.doAllocate && !io.roqCommits.isWalk
230 231
  }

232
  // We don't bypass the old_pdest from valid instructions with the same ldest currently in rename stage.
233
  // Instead, we determine whether there're some dependencies between the valid instructions.
234 235
  for (i <- 1 until RenameWidth) {
    io.renameBypass.lsrc1_bypass(i-1) := Cat((0 until i).map(j => {
236 237 238
      val fpMatch  = needFpDest(j) && io.in(i).bits.ctrl.srcType(0) === SrcType.fp
      val intMatch = needIntDest(j) && io.in(i).bits.ctrl.srcType(0) === SrcType.reg
      (fpMatch || intMatch) && io.in(j).bits.ctrl.ldest === io.in(i).bits.ctrl.lsrc(0)
239 240
    }).reverse)
    io.renameBypass.lsrc2_bypass(i-1) := Cat((0 until i).map(j => {
241 242 243
      val fpMatch  = needFpDest(j) && io.in(i).bits.ctrl.srcType(1) === SrcType.fp
      val intMatch = needIntDest(j) && io.in(i).bits.ctrl.srcType(1) === SrcType.reg
      (fpMatch || intMatch) && io.in(j).bits.ctrl.ldest === io.in(i).bits.ctrl.lsrc(1)
244 245
    }).reverse)
    io.renameBypass.lsrc3_bypass(i-1) := Cat((0 until i).map(j => {
246 247 248
      val fpMatch  = needFpDest(j) && io.in(i).bits.ctrl.srcType(2) === SrcType.fp
      val intMatch = needIntDest(j) && io.in(i).bits.ctrl.srcType(2) === SrcType.reg
      (fpMatch || intMatch) && io.in(j).bits.ctrl.ldest === io.in(i).bits.ctrl.lsrc(2)
249 250 251 252 253 254
    }).reverse)
    io.renameBypass.ldest_bypass(i-1) := Cat((0 until i).map(j => {
      val fpMatch  = needFpDest(j) && needFpDest(i)
      val intMatch = needIntDest(j) && needIntDest(i)
      (fpMatch || intMatch) && io.in(j).bits.ctrl.ldest === io.in(i).bits.ctrl.ldest
    }).reverse)
255
  }
Y
Yinan Xu 已提交
256

257
  // calculate lsq space requirement
258 259 260 261 262 263
  val isLs    = VecInit(uops.map(uop => FuType.isLoadStore(uop.ctrl.fuType)))
  val isStore = VecInit(uops.map(uop => FuType.isStoreExu(uop.ctrl.fuType)))
  val isAMO   = VecInit(uops.map(uop => FuType.isAMO(uop.ctrl.fuType)))
  io.dispatchInfo.lsqNeedAlloc := VecInit((0 until RenameWidth).map(i =>
    Mux(isLs(i), Mux(isStore(i) && !isAMO(i), 2.U, 1.U), 0.U)))

Y
Yinan Xu 已提交
264 265 266 267 268
  /**
    * Instructions commit: update freelist and rename table
    */
  for (i <- 0 until CommitWidth) {

269 270
    Seq((intRat, false), (fpRat, true)) foreach { case (rat, fp) => 
      // is valid commit req and given instruction has destination register
Y
Yinan Xu 已提交
271
      val commitDestValid = io.roqCommits.valid(i) && needDestRegCommit(fp, io.roqCommits.info(i))
272 273 274 275 276 277 278 279 280 281
      XSDebug(p"isFp[${fp}]index[$i]-commitDestValid:$commitDestValid,isWalk:${io.roqCommits.isWalk}\n")

      /*
      I. RAT Update
       */

      // walk back write - restore spec state : ldest => old_pdest
      if (fp && i < RenameWidth) {
        rat.io.specWritePorts(i).wen := (commitDestValid && io.roqCommits.isWalk) || fpSpecWen(i)
        rat.io.specWritePorts(i).addr := Mux(fpSpecWen(i), uops(i).ctrl.ldest, io.roqCommits.info(i).ldest)
282
        rat.io.specWritePorts(i).wdata := Mux(fpSpecWen(i), fpFreeList.allocatePhyReg(i), io.roqCommits.info(i).old_pdest)
283 284 285
      } else if (!fp && i < RenameWidth) {
        rat.io.specWritePorts(i).wen := (commitDestValid && io.roqCommits.isWalk) || intSpecWen(i)
        rat.io.specWritePorts(i).addr := Mux(intSpecWen(i), uops(i).ctrl.ldest, io.roqCommits.info(i).ldest)
286 287 288 289 290 291 292 293 294
        if (EnableIntMoveElim) {
          rat.io.specWritePorts(i).wdata := 
            Mux(intSpecWen(i), Mux(meEnable(i), uops(i).psrc(0), intFreeList.allocatePhyReg(i)), io.roqCommits.info(i).old_pdest)
        } else {
          rat.io.specWritePorts(i).wdata := 
            Mux(intSpecWen(i), intFreeList.allocatePhyReg(i), io.roqCommits.info(i).old_pdest)
        }
      // when i >= RenameWidth, this write must happens during WALK process
      } else if (i >= RenameWidth) {
295 296 297 298
        rat.io.specWritePorts(i).wen := commitDestValid && io.roqCommits.isWalk
        rat.io.specWritePorts(i).addr := io.roqCommits.info(i).ldest
        rat.io.specWritePorts(i).wdata := io.roqCommits.info(i).old_pdest
      }
Y
Yinan Xu 已提交
299 300

      when (commitDestValid && io.roqCommits.isWalk) {
301 302
        XSInfo({if(fp) p"[fp" else p"[int"} + p" walk] " +
          p"ldest:${rat.io.specWritePorts(i).addr} -> old_pdest:${rat.io.specWritePorts(i).wdata}\n")
Y
Yinan Xu 已提交
303 304
      }

305 306 307 308
      // normal write - update arch state (serve as initialization)
      rat.io.archWritePorts(i).wen := commitDestValid && !io.roqCommits.isWalk
      rat.io.archWritePorts(i).addr := io.roqCommits.info(i).ldest
      rat.io.archWritePorts(i).wdata := io.roqCommits.info(i).pdest
Y
Yinan Xu 已提交
309

310 311 312
      XSInfo(rat.io.archWritePorts(i).wen,
        {if(fp) p"[fp" else p"[int"} + p" arch rat update] ldest:${rat.io.archWritePorts(i).addr} ->" +
        p" pdest:${rat.io.archWritePorts(i).wdata}\n"
Y
Yinan Xu 已提交
313 314
      )

315 316 317 318 319 320

      /*
      II. Free List Update
       */

      if (fp) { // Float Point free list
321 322 323
        fpFreeList.freeReq(i)  := commitDestValid && !io.roqCommits.isWalk
        fpFreeList.freePhyReg(i) := io.roqCommits.info(i).old_pdest
      } else if (EnableIntMoveElim) { // Integer free list
324 325 326 327 328 329 330 331 332 333

        // during walk process:
        // 1. for normal inst, free pdest + revert rat from ldest->pdest to ldest->old_pdest
        // 2. for ME inst, free pdest(commit counter++) + revert rat

        // conclusion: 
        // a. rat recovery has nothing to do with ME or not
        // b. treat walk as normal commit except replace old_pdests with pdests and set io.walk to true
        // c. ignore pdests port when walking

334 335 336 337 338 339 340
        intFreeList.freeReq(i) := commitDestValid // walk or not walk
        intFreeList.freePhyReg(i)  := Mux(io.roqCommits.isWalk, io.roqCommits.info(i).pdest, io.roqCommits.info(i).old_pdest)
        intFreeList.asInstanceOf[freelist.MEFreeList].eliminatedMove(i) := io.roqCommits.info(i).eliminatedMove
        intFreeList.asInstanceOf[freelist.MEFreeList].multiRefPhyReg(i) := io.roqCommits.info(i).pdest
      } else {
        intFreeList.freeReq(i) := commitDestValid && !io.roqCommits.isWalk
        intFreeList.freePhyReg(i)  := io.roqCommits.info(i).old_pdest
341
      }
Y
Yinan Xu 已提交
342 343
    }
  }
Y
Yinan Xu 已提交
344

345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371

  /*
  Debug and performance counter
   */

  def printRenameInfo(in: DecoupledIO[CfCtrl], out: DecoupledIO[MicroOp]) = {
    XSInfo(
      in.valid && in.ready,
      p"pc:${Hexadecimal(in.bits.cf.pc)} in v:${in.valid} in rdy:${in.ready} " +
        p"lsrc(0):${in.bits.ctrl.lsrc(0)} -> psrc(0):${out.bits.psrc(0)} " +
        p"lsrc(1):${in.bits.ctrl.lsrc(1)} -> psrc(1):${out.bits.psrc(1)} " +
        p"lsrc(2):${in.bits.ctrl.lsrc(2)} -> psrc(2):${out.bits.psrc(2)} " +
        p"ldest:${in.bits.ctrl.ldest} -> pdest:${out.bits.pdest} " +
        p"old_pdest:${out.bits.old_pdest} " +
        p"out v:${out.valid} r:${out.ready}\n"
    )
  }

  for((x,y) <- io.in.zip(io.out)){
    printRenameInfo(x, y)
  }

  XSDebug(io.roqCommits.isWalk, p"Walk Recovery Enabled\n")
  XSDebug(io.roqCommits.isWalk, p"validVec:${Binary(io.roqCommits.valid.asUInt)}\n")
  for (i <- 0 until CommitWidth) {
    val info = io.roqCommits.info(i)
    XSDebug(io.roqCommits.isWalk && io.roqCommits.valid(i), p"[#$i walk info] pc:${Hexadecimal(info.pc)} " +
372
      p"ldest:${info.ldest} rfWen:${info.rfWen} fpWen:${info.fpWen} " + { if (EnableIntMoveElim) p"eliminatedMove:${info.eliminatedMove} " else p"" } +
373 374 375 376
      p"pdest:${info.pdest} old_pdest:${info.old_pdest}\n")
  }

  XSDebug(p"inValidVec: ${Binary(Cat(io.in.map(_.valid)))}\n")
377
  XSInfo(!canOut, p"stall at rename, hasValid:${hasValid}, fpCanAlloc:${fpFreeList.canAllocate}, intCanAlloc:${intFreeList.canAllocate} dispatch1ready:${io.out(0).ready}, isWalk:${io.roqCommits.isWalk}\n")
378 379 380 381 382 383

  intRat.io.debug_rdata <> io.debug_int_rat
  fpRat.io.debug_rdata <> io.debug_fp_rat

  XSDebug(p"Arch Int RAT:" + io.debug_int_rat.zipWithIndex.map{ case (r, i) => p"#$i:$r " }.reduceLeft(_ + _) + p"\n")

384 385 386
  XSPerfAccumulate("in", Mux(RegNext(io.in(0).ready), PopCount(io.in.map(_.valid)), 0.U))
  XSPerfAccumulate("utilization", PopCount(io.in.map(_.valid)))
  XSPerfAccumulate("waitInstr", PopCount((0 until RenameWidth).map(i => io.in(i).valid && !io.in(i).ready)))
387 388 389 390
  XSPerfAccumulate("stall_cycle_dispatch", hasValid && !io.out(0).ready && fpFreeList.canAllocate && intFreeList.canAllocate && !io.roqCommits.isWalk)
  XSPerfAccumulate("stall_cycle_fp", hasValid && io.out(0).ready && !fpFreeList.canAllocate && intFreeList.canAllocate && !io.roqCommits.isWalk)
  XSPerfAccumulate("stall_cycle_int", hasValid && io.out(0).ready && fpFreeList.canAllocate && !intFreeList.canAllocate && !io.roqCommits.isWalk)
  XSPerfAccumulate("stall_cycle_walk", hasValid && io.out(0).ready && fpFreeList.canAllocate && intFreeList.canAllocate && io.roqCommits.isWalk)
391 392 393
  if (!env.FPGAPlatform) {
    ExcitingUtils.addSource(io.roqCommits.isWalk, "TMA_backendiswalk")
  }
394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414

  if (EnableIntMoveElim) {
    XSPerfAccumulate("move_instr_count", PopCount(Seq.tabulate(RenameWidth)(i => io.out(i).fire() && io.in(i).bits.ctrl.isMove)))
    XSPerfAccumulate("move_elim_enabled", PopCount(Seq.tabulate(RenameWidth)(i => io.out(i).fire() && meEnable(i))))
    XSPerfAccumulate("move_elim_cancelled", PopCount(Seq.tabulate(RenameWidth)(i => io.out(i).fire() && io.in(i).bits.ctrl.isMove && !meEnable(i))))
    XSPerfAccumulate("move_elim_cancelled_psrc_bypass", PopCount(Seq.tabulate(RenameWidth)(i => io.out(i).fire() && io.in(i).bits.ctrl.isMove && !meEnable(i) && { if (i == 0) false.B else io.renameBypass.lsrc1_bypass(i-1).orR })))
    XSPerfAccumulate("move_elim_cancelled_cnt_limit", PopCount(Seq.tabulate(RenameWidth)(i => io.out(i).fire() && io.in(i).bits.ctrl.isMove && !meEnable(i) && isMax.get(io.out(i).bits.psrc(0)))))
    XSPerfAccumulate("move_elim_cancelled_inc_more_than_one", PopCount(Seq.tabulate(RenameWidth)(i => io.out(i).fire() && io.in(i).bits.ctrl.isMove && !meEnable(i) && { if (i == 0) false.B else psrc_cmp(i-1).orR })))

    // to make sure meEnable functions as expected
    for (i <- 0 until RenameWidth) {
      XSDebug(io.out(i).fire() && io.in(i).bits.ctrl.isMove && !meEnable(i) && isMax.get(io.out(i).bits.psrc(0)),
        p"ME_CANCELLED: ref counter hits max value (pc:0x${Hexadecimal(io.in(i).bits.cf.pc)})\n")
      XSDebug(io.out(i).fire() && io.in(i).bits.ctrl.isMove && !meEnable(i) && { if (i == 0) false.B else io.renameBypass.lsrc1_bypass(i-1).orR },
        p"ME_CANCELLED: RAW dependency (pc:0x${Hexadecimal(io.in(i).bits.cf.pc)})\n")
      XSDebug(io.out(i).fire() && io.in(i).bits.ctrl.isMove && !meEnable(i) && { if (i == 0) false.B else psrc_cmp(i-1).orR },
        p"ME_CANCELLED: psrc duplicates with former instruction (pc:0x${Hexadecimal(io.in(i).bits.cf.pc)})\n")
    }
    XSDebug(VecInit(Seq.tabulate(RenameWidth)(i => io.out(i).fire() && io.in(i).bits.ctrl.isMove && !meEnable(i))).asUInt().orR,
      p"ME_CANCELLED: pc group [ " + (0 until RenameWidth).map(i => p"fire:${io.out(i).fire()},pc:0x${Hexadecimal(io.in(i).bits.cf.pc)} ").reduceLeft(_ + _) + p"]\n")
    XSInfo(meEnable.asUInt().orR(), p"meEnableVec:${Binary(meEnable.asUInt)}\n")
415
  }
416
}