xref: /XiangShan/src/main/scala/xiangshan/backend/rename/Rename.scala (revision 73c4359ef65acf2a13c3464140d2475b49de1e5b)
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
192225d46eSJiawei Linimport chipsalliance.rocketchip.config.Parameters
205844fcf0SLinJiaweiimport chisel3._
215844fcf0SLinJiaweiimport chisel3.util._
225844fcf0SLinJiaweiimport xiangshan._
237cef916fSYinan Xuimport utils._
24588ceab5SYinan Xuimport xiangshan.backend.roq.RoqPtr
25049559e7SYinan Xuimport xiangshan.backend.dispatch.PreDispatchInfo
265844fcf0SLinJiawei
272225d46eSJiawei Linclass RenameBypassInfo(implicit p: Parameters) extends XSBundle {
2899b8dc2cSYinan Xu  val lsrc1_bypass = MixedVec(List.tabulate(RenameWidth-1)(i => UInt((i+1).W)))
2999b8dc2cSYinan Xu  val lsrc2_bypass = MixedVec(List.tabulate(RenameWidth-1)(i => UInt((i+1).W)))
3099b8dc2cSYinan Xu  val lsrc3_bypass = MixedVec(List.tabulate(RenameWidth-1)(i => UInt((i+1).W)))
3199b8dc2cSYinan Xu  val ldest_bypass = MixedVec(List.tabulate(RenameWidth-1)(i => UInt((i+1).W)))
3299b8dc2cSYinan Xu}
3399b8dc2cSYinan Xu
3439d3280eSYikeZhouclass Rename(implicit p: Parameters) extends XSModule {
355844fcf0SLinJiawei  val io = IO(new Bundle() {
365844fcf0SLinJiawei    val redirect = Flipped(ValidIO(new Redirect))
372d7c7105SYinan Xu    val flush = Input(Bool())
3821e7a6c5SYinan Xu    val roqCommits = Flipped(new RoqCommitIO)
3957c4f8d6SLinJiawei    // from decode buffer
409a2e6b8aSLinJiawei    val in = Vec(RenameWidth, Flipped(DecoupledIO(new CfCtrl)))
4157c4f8d6SLinJiawei    // to dispatch1
429a2e6b8aSLinJiawei    val out = Vec(RenameWidth, DecoupledIO(new MicroOp))
4399b8dc2cSYinan Xu    val renameBypass = Output(new RenameBypassInfo)
44049559e7SYinan Xu    val dispatchInfo = Output(new PreDispatchInfo)
458b8e745dSYikeZhou    // for debug printing
462225d46eSJiawei Lin    val debug_int_rat = Vec(32, Output(UInt(PhyRegIdxWidth.W)))
472225d46eSJiawei Lin    val debug_fp_rat = Vec(32, Output(UInt(PhyRegIdxWidth.W)))
485844fcf0SLinJiawei  })
49b034d3b9SLinJiawei
508b8e745dSYikeZhou  // create free list and rat
5139d3280eSYikeZhou  val intFreeList = Module(if (EnableIntMoveElim) new freelist.MEFreeList else new freelist.StdFreeList)
5239d3280eSYikeZhou  val fpFreeList = Module(new freelist.StdFreeList)
538b8e745dSYikeZhou
548b8e745dSYikeZhou  val intRat = Module(new RenameTable(float = false))
558b8e745dSYikeZhou  val fpRat = Module(new RenameTable(float = true))
568b8e745dSYikeZhou
578b8e745dSYikeZhou  // connect flush and redirect ports for rat
588b8e745dSYikeZhou  Seq(intRat, fpRat) foreach { case rat =>
598b8e745dSYikeZhou    rat.io.redirect := io.redirect.valid
608b8e745dSYikeZhou    rat.io.flush := io.flush
618b8e745dSYikeZhou    rat.io.walkWen := io.roqCommits.isWalk
622e9d39e0SLinJiawei  }
632e9d39e0SLinJiawei
648b8e745dSYikeZhou  // decide if given instruction needs allocating a new physical register (CfCtrl: from decode; RoqCommitInfo: from roq)
65b034d3b9SLinJiawei  def needDestReg[T <: CfCtrl](fp: Boolean, x: T): Bool = {
66b034d3b9SLinJiawei    {if(fp) x.ctrl.fpWen else x.ctrl.rfWen && (x.ctrl.ldest =/= 0.U)}
67b034d3b9SLinJiawei  }
68fe6452fcSYinan Xu  def needDestRegCommit[T <: RoqCommitInfo](fp: Boolean, x: T): Bool = {
69fe6452fcSYinan Xu    {if(fp) x.fpWen else x.rfWen && (x.ldest =/= 0.U)}
70fe6452fcSYinan Xu  }
718b8e745dSYikeZhou
725eb4af5bSYikeZhou  // connect [flush + redirect + walk] ports for __float point__ & __integer__ free list
735eb4af5bSYikeZhou  Seq((fpFreeList, true), (intFreeList, false)).foreach{ case (fl, isFp) =>
745eb4af5bSYikeZhou    fl.flush := io.flush
755eb4af5bSYikeZhou    fl.redirect := io.redirect.valid
765eb4af5bSYikeZhou    fl.walk := io.roqCommits.isWalk
775eb4af5bSYikeZhou    // when isWalk, use stepBack to restore head pointer of free list
785eb4af5bSYikeZhou    // (if ME enabled, stepBack of intFreeList should be useless thus optimized out)
795eb4af5bSYikeZhou    fl.stepBack := PopCount(io.roqCommits.valid.zip(io.roqCommits.info).map{case (v, i) => v && needDestRegCommit(isFp, i)})
805eb4af5bSYikeZhou    // walk has higher priority than allocation and thus we don't use isWalk here
815eb4af5bSYikeZhou    // only when both fp and int free list and dispatch1 has enough space can we do allocation
825eb4af5bSYikeZhou    fl.doAllocate := fl.canAllocate && io.out(0).ready
835eb4af5bSYikeZhou  }
845eb4af5bSYikeZhou
855eb4af5bSYikeZhou  //           dispatch1 ready ++ float point free list ready ++ int free list ready      ++ not walk
865eb4af5bSYikeZhou  val canOut = io.out(0).ready && fpFreeList.canAllocate && intFreeList.canAllocate && !io.roqCommits.isWalk
875eb4af5bSYikeZhou
88b034d3b9SLinJiawei
89588ceab5SYinan Xu  // speculatively assign the instruction with an roqIdx
908b8e745dSYikeZhou  val validCount = PopCount(io.in.map(_.valid)) // number of instructions waiting to enter roq (from decode)
91588ceab5SYinan Xu  val roqIdxHead = RegInit(0.U.asTypeOf(new RoqPtr))
928f77f081SYinan Xu  val lastCycleMisprediction = RegNext(io.redirect.valid && !io.redirect.bits.flushItself())
938b8e745dSYikeZhou  val roqIdxHeadNext = Mux(io.flush, 0.U.asTypeOf(new RoqPtr), // flush: clear roq
948b8e745dSYikeZhou              Mux(io.redirect.valid, io.redirect.bits.roqIdx, // redirect: move ptr to given roq index (flush itself)
958b8e745dSYikeZhou         Mux(lastCycleMisprediction, roqIdxHead + 1.U, // mis-predict: not flush roqIdx itself
968b8e745dSYikeZhou                         Mux(canOut, roqIdxHead + validCount, // instructions successfully entered next stage: increase roqIdx
978b8e745dSYikeZhou                      /* default */  roqIdxHead)))) // no instructions passed by this cycle: stick to old value
98588ceab5SYinan Xu  roqIdxHead := roqIdxHeadNext
99588ceab5SYinan Xu
1008b8e745dSYikeZhou
10100ad41d0SYinan Xu  /**
10200ad41d0SYinan Xu    * Rename: allocate free physical register and update rename table
10300ad41d0SYinan Xu    */
104b034d3b9SLinJiawei  val uops = Wire(Vec(RenameWidth, new MicroOp))
105b034d3b9SLinJiawei  uops.foreach( uop => {
10620e31bd1SYinan Xu    uop.srcState(0) := DontCare
10720e31bd1SYinan Xu    uop.srcState(1) := DontCare
10820e31bd1SYinan Xu    uop.srcState(2) := DontCare
109b034d3b9SLinJiawei    uop.roqIdx := DontCare
1106ae7ac7cSAllen    uop.diffTestDebugLrScValid := DontCare
1117cef916fSYinan Xu    uop.debugInfo := DontCare
112bc86598fSWilliam Wang    uop.lqIdx := DontCare
113bc86598fSWilliam Wang    uop.sqIdx := DontCare
114b034d3b9SLinJiawei  })
115b034d3b9SLinJiawei
11699b8dc2cSYinan Xu  val needFpDest = Wire(Vec(RenameWidth, Bool()))
11799b8dc2cSYinan Xu  val needIntDest = Wire(Vec(RenameWidth, Bool()))
118b424051cSYinan Xu  val hasValid = Cat(io.in.map(_.valid)).orR
1198b8e745dSYikeZhou
1208b8e745dSYikeZhou  val isMove = io.in.map(_.bits.ctrl.isMove)
12139d3280eSYikeZhou  val isMax = if (EnableIntMoveElim) Some(intFreeList.asInstanceOf[freelist.MEFreeList].maxVec) else None
1228b8e745dSYikeZhou  val meEnable = WireInit(VecInit(Seq.fill(RenameWidth)(false.B)))
1238b8e745dSYikeZhou  val psrc_cmp = Wire(MixedVec(List.tabulate(RenameWidth-1)(i => UInt((i+1).W))))
1248b8e745dSYikeZhou
1258b8e745dSYikeZhou  val intSpecWen = Wire(Vec(RenameWidth, Bool()))
1268b8e745dSYikeZhou  val fpSpecWen = Wire(Vec(RenameWidth, Bool()))
1278b8e745dSYikeZhou
1288b8e745dSYikeZhou  // uop calculation
129b034d3b9SLinJiawei  for (i <- 0 until RenameWidth) {
130b034d3b9SLinJiawei    uops(i).cf := io.in(i).bits.cf
131b034d3b9SLinJiawei    uops(i).ctrl := io.in(i).bits.ctrl
132b034d3b9SLinJiawei
133567096a6Slinjiawei    val inValid = io.in(i).valid
1342dcb2daaSLinJiawei
135b034d3b9SLinJiawei    // alloc a new phy reg
13699b8dc2cSYinan Xu    needFpDest(i) := inValid && needDestReg(fp = true, io.in(i).bits)
13799b8dc2cSYinan Xu    needIntDest(i) := inValid && needDestReg(fp = false, io.in(i).bits)
13839d3280eSYikeZhou    fpFreeList.allocateReq(i) := needFpDest(i)
13939d3280eSYikeZhou    intFreeList.allocateReq(i) := needIntDest(i)
1402438f9ebSYinan Xu
1418b8e745dSYikeZhou    // no valid instruction from decode stage || all resources (dispatch1 + both free lists) ready
142b424051cSYinan Xu    io.in(i).ready := !hasValid || canOut
14358e06390SLinJiawei
144c7054babSLinJiawei    // do checkpoints when a branch inst come
1454f787118SYinan Xu    // for(fl <- Seq(fpFreeList, intFreeList)){
1464f787118SYinan Xu    //   fl.cpReqs(i).valid := inValid
1474f787118SYinan Xu    //   fl.cpReqs(i).bits := io.in(i).bits.brTag
1484f787118SYinan Xu    // }
14958e06390SLinJiawei
150b034d3b9SLinJiawei
151588ceab5SYinan Xu    uops(i).roqIdx := roqIdxHead + i.U
152588ceab5SYinan Xu
15339d3280eSYikeZhou    io.out(i).valid := io.in(i).valid && intFreeList.canAllocate && fpFreeList.canAllocate && !io.roqCommits.isWalk
154b034d3b9SLinJiawei    io.out(i).bits := uops(i)
155b034d3b9SLinJiawei
156b034d3b9SLinJiawei
157b034d3b9SLinJiawei    // read rename table
158b034d3b9SLinJiawei    def readRat(lsrcList: List[UInt], ldest: UInt, fp: Boolean) = {
159b034d3b9SLinJiawei      val rat = if(fp) fpRat else intRat
160b034d3b9SLinJiawei      val srcCnt = lsrcList.size
161b034d3b9SLinJiawei      val psrcVec = Wire(Vec(srcCnt, UInt(PhyRegIdxWidth.W)))
162b034d3b9SLinJiawei      val old_pdest = Wire(UInt(PhyRegIdxWidth.W))
163b034d3b9SLinJiawei      for(k <- 0 until srcCnt+1){
164b034d3b9SLinJiawei        val rportIdx = i * (srcCnt+1) + k
165b034d3b9SLinJiawei        if(k != srcCnt){
1668b8e745dSYikeZhou          rat.io.readPorts(rportIdx).addr := lsrcList(k)
1678b8e745dSYikeZhou          psrcVec(k) := rat.io.readPorts(rportIdx).rdata
168b034d3b9SLinJiawei        } else {
1698b8e745dSYikeZhou          rat.io.readPorts(rportIdx).addr := ldest
1708b8e745dSYikeZhou          old_pdest := rat.io.readPorts(rportIdx).rdata
171b034d3b9SLinJiawei        }
172b034d3b9SLinJiawei      }
173b034d3b9SLinJiawei      (psrcVec, old_pdest)
174b034d3b9SLinJiawei    }
17520e31bd1SYinan Xu    val lsrcList = List(uops(i).ctrl.lsrc(0), uops(i).ctrl.lsrc(1), uops(i).ctrl.lsrc(2))
176b034d3b9SLinJiawei    val ldest = uops(i).ctrl.ldest
177b034d3b9SLinJiawei    val (intPhySrcVec, intOldPdest) = readRat(lsrcList.take(2), ldest, fp = false)
178b034d3b9SLinJiawei    val (fpPhySrcVec, fpOldPdest) = readRat(lsrcList, ldest, fp = true)
17920e31bd1SYinan Xu    uops(i).psrc(0) := Mux(uops(i).ctrl.srcType(0) === SrcType.reg, intPhySrcVec(0), fpPhySrcVec(0))
18020e31bd1SYinan Xu    uops(i).psrc(1) := Mux(uops(i).ctrl.srcType(1) === SrcType.reg, intPhySrcVec(1), fpPhySrcVec(1))
18120e31bd1SYinan Xu    uops(i).psrc(2) := fpPhySrcVec(2)
182b034d3b9SLinJiawei    uops(i).old_pdest := Mux(uops(i).ctrl.rfWen, intOldPdest, fpOldPdest)
1838b8e745dSYikeZhou
1845eb4af5bSYikeZhou    if (EnableIntMoveElim) {
1855eb4af5bSYikeZhou
1868b8e745dSYikeZhou      if (i == 0) {
1878b8e745dSYikeZhou        // calculate meEnable
18839d3280eSYikeZhou        meEnable(i) := isMove(i) && !isMax.get(uops(i).psrc(0))
1898b8e745dSYikeZhou      } else {
1908b8e745dSYikeZhou        // compare psrc0
1918b8e745dSYikeZhou        psrc_cmp(i-1) := Cat((0 until i).map(j => {
192d3975becSYikeZhou          uops(i).psrc(0) === uops(j).psrc(0) && io.in(i).bits.ctrl.isMove && io.in(j).bits.ctrl.isMove
1938b8e745dSYikeZhou        }) /* reverse is not necessary here */)
1948b8e745dSYikeZhou
1958b8e745dSYikeZhou        // calculate meEnable
19639d3280eSYikeZhou        meEnable(i) := isMove(i) && !(io.renameBypass.lsrc1_bypass(i-1).orR | psrc_cmp(i-1).orR | isMax.get(uops(i).psrc(0)))
1978b8e745dSYikeZhou      }
198*73c4359eSYikeZhou      uops(i).eliminatedMove := meEnable(i) || (uops(i).ctrl.isMove && uops(i).ctrl.ldest === 0.U)
1998b8e745dSYikeZhou
2008b8e745dSYikeZhou      // send psrc of eliminated move instructions to free list and label them as eliminated
2018b8e745dSYikeZhou      when (meEnable(i)) {
20239d3280eSYikeZhou        intFreeList.asInstanceOf[freelist.MEFreeList].psrcOfMove(i).valid := true.B
20339d3280eSYikeZhou        intFreeList.asInstanceOf[freelist.MEFreeList].psrcOfMove(i).bits := uops(i).psrc(0)
2048b8e745dSYikeZhou        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")
2058b8e745dSYikeZhou      } .otherwise {
20639d3280eSYikeZhou        intFreeList.asInstanceOf[freelist.MEFreeList].psrcOfMove(i).valid := false.B
20739d3280eSYikeZhou        intFreeList.asInstanceOf[freelist.MEFreeList].psrcOfMove(i).bits := DontCare
2088b8e745dSYikeZhou        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")
2098b8e745dSYikeZhou      }
2108b8e745dSYikeZhou
2118b8e745dSYikeZhou      // update pdest
2128b8e745dSYikeZhou      uops(i).pdest := Mux(meEnable(i), uops(i).psrc(0), // move eliminated
21339d3280eSYikeZhou                       Mux(needIntDest(i), intFreeList.allocatePhyReg(i), // normal int inst
2148b8e745dSYikeZhou                       Mux(uops(i).ctrl.ldest===0.U && uops(i).ctrl.rfWen, 0.U // int inst with dst=r0
21539d3280eSYikeZhou                       /* default */, fpFreeList.allocatePhyReg(i)))) // normal fp inst
2165eb4af5bSYikeZhou    } else {
2175eb4af5bSYikeZhou      uops(i).eliminatedMove := DontCare
2185eb4af5bSYikeZhou      psrc_cmp.foreach(_ := DontCare)
2195eb4af5bSYikeZhou      // update pdest
2205eb4af5bSYikeZhou      uops(i).pdest := Mux(needIntDest(i), intFreeList.allocatePhyReg(i), // normal int inst
2215eb4af5bSYikeZhou                       Mux(uops(i).ctrl.ldest===0.U && uops(i).ctrl.rfWen, 0.U // int inst with dst=r0
2225eb4af5bSYikeZhou                       /* default */, fpFreeList.allocatePhyReg(i))) // normal fp inst
2235eb4af5bSYikeZhou    }
2248b8e745dSYikeZhou
2258b8e745dSYikeZhou    // write speculative rename table
22639d3280eSYikeZhou    // we update rat later inside commit code
22739d3280eSYikeZhou    intSpecWen(i) := intFreeList.allocateReq(i) && intFreeList.canAllocate && intFreeList.doAllocate && !io.roqCommits.isWalk
22839d3280eSYikeZhou    fpSpecWen(i) := fpFreeList.allocateReq(i) && fpFreeList.canAllocate && fpFreeList.doAllocate && !io.roqCommits.isWalk
229b034d3b9SLinJiawei  }
230b034d3b9SLinJiawei
23199b8dc2cSYinan Xu  // We don't bypass the old_pdest from valid instructions with the same ldest currently in rename stage.
2328b8e745dSYikeZhou  // Instead, we determine whether there're some dependencies between the valid instructions.
23399b8dc2cSYinan Xu  for (i <- 1 until RenameWidth) {
23499b8dc2cSYinan Xu    io.renameBypass.lsrc1_bypass(i-1) := Cat((0 until i).map(j => {
23520e31bd1SYinan Xu      val fpMatch  = needFpDest(j) && io.in(i).bits.ctrl.srcType(0) === SrcType.fp
23620e31bd1SYinan Xu      val intMatch = needIntDest(j) && io.in(i).bits.ctrl.srcType(0) === SrcType.reg
23720e31bd1SYinan Xu      (fpMatch || intMatch) && io.in(j).bits.ctrl.ldest === io.in(i).bits.ctrl.lsrc(0)
23899b8dc2cSYinan Xu    }).reverse)
23999b8dc2cSYinan Xu    io.renameBypass.lsrc2_bypass(i-1) := Cat((0 until i).map(j => {
24020e31bd1SYinan Xu      val fpMatch  = needFpDest(j) && io.in(i).bits.ctrl.srcType(1) === SrcType.fp
24120e31bd1SYinan Xu      val intMatch = needIntDest(j) && io.in(i).bits.ctrl.srcType(1) === SrcType.reg
24220e31bd1SYinan Xu      (fpMatch || intMatch) && io.in(j).bits.ctrl.ldest === io.in(i).bits.ctrl.lsrc(1)
24399b8dc2cSYinan Xu    }).reverse)
24499b8dc2cSYinan Xu    io.renameBypass.lsrc3_bypass(i-1) := Cat((0 until i).map(j => {
24520e31bd1SYinan Xu      val fpMatch  = needFpDest(j) && io.in(i).bits.ctrl.srcType(2) === SrcType.fp
24620e31bd1SYinan Xu      val intMatch = needIntDest(j) && io.in(i).bits.ctrl.srcType(2) === SrcType.reg
24720e31bd1SYinan Xu      (fpMatch || intMatch) && io.in(j).bits.ctrl.ldest === io.in(i).bits.ctrl.lsrc(2)
24899b8dc2cSYinan Xu    }).reverse)
24999b8dc2cSYinan Xu    io.renameBypass.ldest_bypass(i-1) := Cat((0 until i).map(j => {
25099b8dc2cSYinan Xu      val fpMatch  = needFpDest(j) && needFpDest(i)
25199b8dc2cSYinan Xu      val intMatch = needIntDest(j) && needIntDest(i)
25299b8dc2cSYinan Xu      (fpMatch || intMatch) && io.in(j).bits.ctrl.ldest === io.in(i).bits.ctrl.ldest
25399b8dc2cSYinan Xu    }).reverse)
254b034d3b9SLinJiawei  }
25500ad41d0SYinan Xu
2568b8e745dSYikeZhou  // calculate lsq space requirement
257049559e7SYinan Xu  val isLs    = VecInit(uops.map(uop => FuType.isLoadStore(uop.ctrl.fuType)))
258049559e7SYinan Xu  val isStore = VecInit(uops.map(uop => FuType.isStoreExu(uop.ctrl.fuType)))
259049559e7SYinan Xu  val isAMO   = VecInit(uops.map(uop => FuType.isAMO(uop.ctrl.fuType)))
260049559e7SYinan Xu  io.dispatchInfo.lsqNeedAlloc := VecInit((0 until RenameWidth).map(i =>
261049559e7SYinan Xu    Mux(isLs(i), Mux(isStore(i) && !isAMO(i), 2.U, 1.U), 0.U)))
262049559e7SYinan Xu
26300ad41d0SYinan Xu  /**
26400ad41d0SYinan Xu    * Instructions commit: update freelist and rename table
26500ad41d0SYinan Xu    */
26600ad41d0SYinan Xu  for (i <- 0 until CommitWidth) {
2678b8e745dSYikeZhou    // when RenameWidth <= CommitWidth, there will be more write ports than read ports, which must be initialized
2688b8e745dSYikeZhou    // normally, they are initialized in 'normal write' section
26900ad41d0SYinan Xu    if (i >= RenameWidth) {
2708b8e745dSYikeZhou      Seq(intRat, fpRat) foreach { case rat =>
2718b8e745dSYikeZhou        rat.io.specWritePorts(i).wen   := false.B
2728b8e745dSYikeZhou        rat.io.specWritePorts(i).addr  := DontCare
2738b8e745dSYikeZhou        rat.io.specWritePorts(i).wdata := DontCare
27400ad41d0SYinan Xu      }
27500ad41d0SYinan Xu    }
27600ad41d0SYinan Xu
2778b8e745dSYikeZhou    Seq((intRat, false), (fpRat, true)) foreach { case (rat, fp) =>
2788b8e745dSYikeZhou      // is valid commit req and given instruction has destination register
27900ad41d0SYinan Xu      val commitDestValid = io.roqCommits.valid(i) && needDestRegCommit(fp, io.roqCommits.info(i))
2808b8e745dSYikeZhou      XSDebug(p"isFp[${fp}]index[$i]-commitDestValid:$commitDestValid,isWalk:${io.roqCommits.isWalk}\n")
2818b8e745dSYikeZhou
2828b8e745dSYikeZhou      /*
2838b8e745dSYikeZhou      I. RAT Update
2848b8e745dSYikeZhou       */
2858b8e745dSYikeZhou
2868b8e745dSYikeZhou      // walk back write - restore spec state : ldest => old_pdest
2878b8e745dSYikeZhou      if (fp && i < RenameWidth) {
2888b8e745dSYikeZhou        rat.io.specWritePorts(i).wen := (commitDestValid && io.roqCommits.isWalk) || fpSpecWen(i)
2898b8e745dSYikeZhou        rat.io.specWritePorts(i).addr := Mux(fpSpecWen(i), uops(i).ctrl.ldest, io.roqCommits.info(i).ldest)
29039d3280eSYikeZhou        rat.io.specWritePorts(i).wdata := Mux(fpSpecWen(i), fpFreeList.allocatePhyReg(i), io.roqCommits.info(i).old_pdest)
2918b8e745dSYikeZhou      } else if (!fp && i < RenameWidth) {
2928b8e745dSYikeZhou        rat.io.specWritePorts(i).wen := (commitDestValid && io.roqCommits.isWalk) || intSpecWen(i)
2938b8e745dSYikeZhou        rat.io.specWritePorts(i).addr := Mux(intSpecWen(i), uops(i).ctrl.ldest, io.roqCommits.info(i).ldest)
29439d3280eSYikeZhou        if (EnableIntMoveElim) {
29539d3280eSYikeZhou          rat.io.specWritePorts(i).wdata :=
29639d3280eSYikeZhou            Mux(intSpecWen(i), Mux(meEnable(i), uops(i).psrc(0), intFreeList.allocatePhyReg(i)), io.roqCommits.info(i).old_pdest)
29739d3280eSYikeZhou        } else {
29839d3280eSYikeZhou          rat.io.specWritePorts(i).wdata :=
29939d3280eSYikeZhou            Mux(intSpecWen(i), intFreeList.allocatePhyReg(i), io.roqCommits.info(i).old_pdest)
30039d3280eSYikeZhou        }
30139d3280eSYikeZhou      // when i >= RenameWidth, this write must happens during WALK process
30239d3280eSYikeZhou      } else if (i >= RenameWidth) {
3038b8e745dSYikeZhou        rat.io.specWritePorts(i).wen := commitDestValid && io.roqCommits.isWalk
3048b8e745dSYikeZhou        rat.io.specWritePorts(i).addr := io.roqCommits.info(i).ldest
3058b8e745dSYikeZhou        rat.io.specWritePorts(i).wdata := io.roqCommits.info(i).old_pdest
3068b8e745dSYikeZhou      }
30700ad41d0SYinan Xu
30800ad41d0SYinan Xu      when (commitDestValid && io.roqCommits.isWalk) {
3098b8e745dSYikeZhou        XSInfo({if(fp) p"[fp" else p"[int"} + p" walk] " +
3108b8e745dSYikeZhou          p"ldest:${rat.io.specWritePorts(i).addr} -> old_pdest:${rat.io.specWritePorts(i).wdata}\n")
31100ad41d0SYinan Xu      }
31200ad41d0SYinan Xu
3138b8e745dSYikeZhou      // normal write - update arch state (serve as initialization)
3148b8e745dSYikeZhou      rat.io.archWritePorts(i).wen := commitDestValid && !io.roqCommits.isWalk
3158b8e745dSYikeZhou      rat.io.archWritePorts(i).addr := io.roqCommits.info(i).ldest
3168b8e745dSYikeZhou      rat.io.archWritePorts(i).wdata := io.roqCommits.info(i).pdest
31700ad41d0SYinan Xu
3188b8e745dSYikeZhou      XSInfo(rat.io.archWritePorts(i).wen,
3198b8e745dSYikeZhou        {if(fp) p"[fp" else p"[int"} + p" arch rat update] ldest:${rat.io.archWritePorts(i).addr} ->" +
3208b8e745dSYikeZhou        p" pdest:${rat.io.archWritePorts(i).wdata}\n"
32100ad41d0SYinan Xu      )
32200ad41d0SYinan Xu
3238b8e745dSYikeZhou
3248b8e745dSYikeZhou      /*
3258b8e745dSYikeZhou      II. Free List Update
3268b8e745dSYikeZhou       */
3278b8e745dSYikeZhou
3288b8e745dSYikeZhou      if (fp) { // Float Point free list
32939d3280eSYikeZhou        fpFreeList.freeReq(i)  := commitDestValid && !io.roqCommits.isWalk
33039d3280eSYikeZhou        fpFreeList.freePhyReg(i) := io.roqCommits.info(i).old_pdest
33139d3280eSYikeZhou      } else if (EnableIntMoveElim) { // Integer free list
3328b8e745dSYikeZhou
3338b8e745dSYikeZhou        // during walk process:
3348b8e745dSYikeZhou        // 1. for normal inst, free pdest + revert rat from ldest->pdest to ldest->old_pdest
3358b8e745dSYikeZhou        // 2. for ME inst, free pdest(commit counter++) + revert rat
3368b8e745dSYikeZhou
3378b8e745dSYikeZhou        // conclusion:
3388b8e745dSYikeZhou        // a. rat recovery has nothing to do with ME or not
3398b8e745dSYikeZhou        // b. treat walk as normal commit except replace old_pdests with pdests and set io.walk to true
3408b8e745dSYikeZhou        // c. ignore pdests port when walking
3418b8e745dSYikeZhou
34239d3280eSYikeZhou        intFreeList.freeReq(i) := commitDestValid // walk or not walk
34339d3280eSYikeZhou        intFreeList.freePhyReg(i)  := Mux(io.roqCommits.isWalk, io.roqCommits.info(i).pdest, io.roqCommits.info(i).old_pdest)
34439d3280eSYikeZhou        intFreeList.asInstanceOf[freelist.MEFreeList].eliminatedMove(i) := io.roqCommits.info(i).eliminatedMove
34539d3280eSYikeZhou        intFreeList.asInstanceOf[freelist.MEFreeList].multiRefPhyReg(i) := io.roqCommits.info(i).pdest
34639d3280eSYikeZhou      } else {
34739d3280eSYikeZhou        intFreeList.freeReq(i) := commitDestValid && !io.roqCommits.isWalk
34839d3280eSYikeZhou        intFreeList.freePhyReg(i)  := io.roqCommits.info(i).old_pdest
34900ad41d0SYinan Xu      }
35000ad41d0SYinan Xu    }
3518b8e745dSYikeZhou  }
3528b8e745dSYikeZhou
3538b8e745dSYikeZhou
3548b8e745dSYikeZhou  /*
3558b8e745dSYikeZhou  Debug and performance counter
3568b8e745dSYikeZhou   */
3578b8e745dSYikeZhou
3588b8e745dSYikeZhou  def printRenameInfo(in: DecoupledIO[CfCtrl], out: DecoupledIO[MicroOp]) = {
3598b8e745dSYikeZhou    XSInfo(
3608b8e745dSYikeZhou      in.valid && in.ready,
3618b8e745dSYikeZhou      p"pc:${Hexadecimal(in.bits.cf.pc)} in v:${in.valid} in rdy:${in.ready} " +
3628b8e745dSYikeZhou        p"lsrc(0):${in.bits.ctrl.lsrc(0)} -> psrc(0):${out.bits.psrc(0)} " +
3638b8e745dSYikeZhou        p"lsrc(1):${in.bits.ctrl.lsrc(1)} -> psrc(1):${out.bits.psrc(1)} " +
3648b8e745dSYikeZhou        p"lsrc(2):${in.bits.ctrl.lsrc(2)} -> psrc(2):${out.bits.psrc(2)} " +
3658b8e745dSYikeZhou        p"ldest:${in.bits.ctrl.ldest} -> pdest:${out.bits.pdest} " +
3668b8e745dSYikeZhou        p"old_pdest:${out.bits.old_pdest} " +
3678b8e745dSYikeZhou        p"out v:${out.valid} r:${out.ready}\n"
3688b8e745dSYikeZhou    )
3698b8e745dSYikeZhou  }
3708b8e745dSYikeZhou
3718b8e745dSYikeZhou  for((x,y) <- io.in.zip(io.out)){
3728b8e745dSYikeZhou    printRenameInfo(x, y)
3738b8e745dSYikeZhou  }
3748b8e745dSYikeZhou
3758b8e745dSYikeZhou  XSDebug(io.roqCommits.isWalk, p"Walk Recovery Enabled\n")
3768b8e745dSYikeZhou  XSDebug(io.roqCommits.isWalk, p"validVec:${Binary(io.roqCommits.valid.asUInt)}\n")
3778b8e745dSYikeZhou  for (i <- 0 until CommitWidth) {
3788b8e745dSYikeZhou    val info = io.roqCommits.info(i)
3798b8e745dSYikeZhou    XSDebug(io.roqCommits.isWalk && io.roqCommits.valid(i), p"[#$i walk info] pc:${Hexadecimal(info.pc)} " +
3805eb4af5bSYikeZhou      p"ldest:${info.ldest} rfWen:${info.rfWen} fpWen:${info.fpWen} " + { if (EnableIntMoveElim) p"eliminatedMove:${info.eliminatedMove} " else p"" } +
3818b8e745dSYikeZhou      p"pdest:${info.pdest} old_pdest:${info.old_pdest}\n")
3828b8e745dSYikeZhou  }
3838b8e745dSYikeZhou
3848b8e745dSYikeZhou  XSDebug(p"inValidVec: ${Binary(Cat(io.in.map(_.valid)))}\n")
38539d3280eSYikeZhou  XSInfo(!canOut, p"stall at rename, hasValid:${hasValid}, fpCanAlloc:${fpFreeList.canAllocate}, intCanAlloc:${intFreeList.canAllocate} dispatch1ready:${io.out(0).ready}, isWalk:${io.roqCommits.isWalk}\n")
3868b8e745dSYikeZhou
3878b8e745dSYikeZhou  intRat.io.debug_rdata <> io.debug_int_rat
3888b8e745dSYikeZhou  fpRat.io.debug_rdata <> io.debug_fp_rat
3898b8e745dSYikeZhou
3908b8e745dSYikeZhou  XSDebug(p"Arch Int RAT:" + io.debug_int_rat.zipWithIndex.map{ case (r, i) => p"#$i:$r " }.reduceLeft(_ + _) + p"\n")
391d479a3a8SYinan Xu
392408a32b7SAllen  XSPerfAccumulate("in", Mux(RegNext(io.in(0).ready), PopCount(io.in.map(_.valid)), 0.U))
393408a32b7SAllen  XSPerfAccumulate("utilization", PopCount(io.in.map(_.valid)))
394408a32b7SAllen  XSPerfAccumulate("waitInstr", PopCount((0 until RenameWidth).map(i => io.in(i).valid && !io.in(i).ready)))
39539d3280eSYikeZhou  XSPerfAccumulate("stall_cycle_dispatch", hasValid && !io.out(0).ready && fpFreeList.canAllocate && intFreeList.canAllocate && !io.roqCommits.isWalk)
39639d3280eSYikeZhou  XSPerfAccumulate("stall_cycle_fp", hasValid && io.out(0).ready && !fpFreeList.canAllocate && intFreeList.canAllocate && !io.roqCommits.isWalk)
39739d3280eSYikeZhou  XSPerfAccumulate("stall_cycle_int", hasValid && io.out(0).ready && fpFreeList.canAllocate && !intFreeList.canAllocate && !io.roqCommits.isWalk)
39839d3280eSYikeZhou  XSPerfAccumulate("stall_cycle_walk", hasValid && io.out(0).ready && fpFreeList.canAllocate && intFreeList.canAllocate && io.roqCommits.isWalk)
3995eb4af5bSYikeZhou
4005eb4af5bSYikeZhou
4015eb4af5bSYikeZhou  if (EnableIntMoveElim) {
402d3975becSYikeZhou    XSPerfAccumulate("move_instr_count", PopCount(Seq.tabulate(RenameWidth)(i => io.out(i).fire() && io.in(i).bits.ctrl.isMove)))
403d3975becSYikeZhou    XSPerfAccumulate("move_elim_enabled", PopCount(Seq.tabulate(RenameWidth)(i => io.out(i).fire() && meEnable(i))))
404d3975becSYikeZhou    XSPerfAccumulate("move_elim_cancelled", PopCount(Seq.tabulate(RenameWidth)(i => io.out(i).fire() && io.in(i).bits.ctrl.isMove && !meEnable(i))))
405d3975becSYikeZhou    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 })))
40639d3280eSYikeZhou    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)))))
407d3975becSYikeZhou    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 })))
408d3975becSYikeZhou
409d3975becSYikeZhou    // to make sure meEnable functions as expected
410d3975becSYikeZhou    for (i <- 0 until RenameWidth) {
41139d3280eSYikeZhou      XSDebug(io.out(i).fire() && io.in(i).bits.ctrl.isMove && !meEnable(i) && isMax.get(io.out(i).bits.psrc(0)),
412d3975becSYikeZhou        p"ME_CANCELLED: ref counter hits max value (pc:0x${Hexadecimal(io.in(i).bits.cf.pc)})\n")
413d3975becSYikeZhou      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 },
414d3975becSYikeZhou        p"ME_CANCELLED: RAW dependency (pc:0x${Hexadecimal(io.in(i).bits.cf.pc)})\n")
415d3975becSYikeZhou      XSDebug(io.out(i).fire() && io.in(i).bits.ctrl.isMove && !meEnable(i) && { if (i == 0) false.B else psrc_cmp(i-1).orR },
416d3975becSYikeZhou        p"ME_CANCELLED: psrc duplicates with former instruction (pc:0x${Hexadecimal(io.in(i).bits.cf.pc)})\n")
417d3975becSYikeZhou    }
418d3975becSYikeZhou    XSDebug(VecInit(Seq.tabulate(RenameWidth)(i => io.out(i).fire() && io.in(i).bits.ctrl.isMove && !meEnable(i))).asUInt().orR,
419d3975becSYikeZhou      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")
4205eb4af5bSYikeZhou    XSInfo(meEnable.asUInt().orR(), p"meEnableVec:${Binary(meEnable.asUInt)}\n")
4215eb4af5bSYikeZhou  }
422b034d3b9SLinJiawei}
423