-- | Minimum viable implementation of jump short-cutting: No short-cutting.
--
-- The functions here simply implement the no-short-cutting case. Implementing
-- the real behaviour would be a great optimization in future.
module GHC.CmmToAsm.RV64.RegInfo
  ( getJumpDestBlockId,
    canShortcut,
    shortcutStatics,
    shortcutJump,
    JumpDest (..),
  )
where

import GHC.Cmm
import GHC.Cmm.BlockId
import GHC.CmmToAsm.RV64.Instr
import GHC.Prelude
import GHC.Utils.Outputable

newtype JumpDest = DestBlockId BlockId

instance Outputable JumpDest where
  ppr :: JumpDest -> SDoc
ppr (DestBlockId BlockId
bid) = String -> SDoc
forall doc. IsLine doc => String -> doc
text String
"jd<blk>:" SDoc -> SDoc -> SDoc
forall doc. IsLine doc => doc -> doc -> doc
<> BlockId -> SDoc
forall a. Outputable a => a -> SDoc
ppr BlockId
bid

-- | Extract BlockId
--
-- Never `Nothing` for Riscv64 NCG.
getJumpDestBlockId :: JumpDest -> Maybe BlockId
getJumpDestBlockId :: JumpDest -> Maybe BlockId
getJumpDestBlockId (DestBlockId BlockId
bid) = BlockId -> Maybe BlockId
forall a. a -> Maybe a
Just BlockId
bid

-- No `Instr`s can bet shortcut (for now)
canShortcut :: Instr -> Maybe JumpDest
canShortcut :: Instr -> Maybe JumpDest
canShortcut Instr
_ = Maybe JumpDest
forall a. Maybe a
Nothing

-- Identity of the provided `RawCmmStatics`
shortcutStatics :: (BlockId -> Maybe JumpDest) -> RawCmmStatics -> RawCmmStatics
shortcutStatics :: (BlockId -> Maybe JumpDest) -> RawCmmStatics -> RawCmmStatics
shortcutStatics BlockId -> Maybe JumpDest
_ RawCmmStatics
other_static = RawCmmStatics
other_static

-- Identity of the provided `Instr`
shortcutJump :: (BlockId -> Maybe JumpDest) -> Instr -> Instr
shortcutJump :: (BlockId -> Maybe JumpDest) -> Instr -> Instr
shortcutJump BlockId -> Maybe JumpDest
_ Instr
other = Instr
other