module GHC.CmmToAsm.Utils
   ( topInfoTable
   , entryBlocks
   )
where

import GHC.Prelude

import GHC.Cmm.BlockId
import GHC.Cmm.Dataflow.Label
import GHC.Cmm hiding (topInfoTable)

-- | Returns the info table associated with the CmmDecl's entry point,
-- if any.
topInfoTable :: GenCmmDecl a (LabelMap i) (ListGraph b) -> Maybe i
topInfoTable :: forall a i b. GenCmmDecl a (LabelMap i) (ListGraph b) -> Maybe i
topInfoTable (CmmProc LabelMap i
infos CLabel
_ [GlobalReg]
_ (ListGraph (GenBasicBlock b
b:[GenBasicBlock b]
_)))
  = Label -> LabelMap i -> Maybe i
forall a. Label -> LabelMap a -> Maybe a
mapLookup (GenBasicBlock b -> Label
forall i. GenBasicBlock i -> Label
blockId GenBasicBlock b
b) LabelMap i
infos
topInfoTable GenCmmDecl a (LabelMap i) (ListGraph b)
_
  = Maybe i
forall a. Maybe a
Nothing

-- | Return the list of BlockIds in a CmmDecl that are entry points
-- for this proc (i.e. they may be jumped to from outside this proc).
entryBlocks :: GenCmmDecl a (LabelMap i) (ListGraph b) -> [BlockId]
entryBlocks :: forall a i b. GenCmmDecl a (LabelMap i) (ListGraph b) -> [Label]
entryBlocks (CmmProc LabelMap i
info CLabel
_ [GlobalReg]
_ (ListGraph [GenBasicBlock b]
code)) = [Label]
entries
  where
        infos :: [Label]
infos = LabelMap i -> [Label]
forall a. LabelMap a -> [Label]
mapKeys LabelMap i
info
        entries :: [Label]
entries = case [GenBasicBlock b]
code of
                    [] -> [Label]
infos
                    BasicBlock Label
entry [b]
_ : [GenBasicBlock b]
_ -- first block is the entry point
                       | Label
entry Label -> [Label] -> Bool
forall a. Eq a => a -> [a] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [Label]
infos -> [Label]
infos
                       | Bool
otherwise          -> Label
entry Label -> [Label] -> [Label]
forall a. a -> [a] -> [a]
: [Label]
infos
entryBlocks GenCmmDecl a (LabelMap i) (ListGraph b)
_ = []