{-# LANGUAGE GADTs #-}
{-# LANGUAGE ScopedTypeVariables #-}

module GHC.Cmm.LRegSet (
    LRegSet,
    LRegKey,

    emptyLRegSet,
    nullLRegSet,
    insertLRegSet,
    elemLRegSet,

    deleteFromLRegSet,
    sizeLRegSet,

    plusLRegSet,
    elemsLRegSet
  ) where

import GHC.Prelude
import GHC.Types.Unique
import GHC.Cmm.Expr
import GHC.Word

import GHC.Data.Word64Set as Word64Set

-- Compact sets for membership tests of local variables.

type LRegSet = Word64Set.Word64Set
type LRegKey = Word64

emptyLRegSet :: LRegSet
emptyLRegSet :: LRegSet
emptyLRegSet = LRegSet
Word64Set.empty

nullLRegSet :: LRegSet -> Bool
nullLRegSet :: LRegSet -> Bool
nullLRegSet = LRegSet -> Bool
Word64Set.null

insertLRegSet :: LocalReg -> LRegSet -> LRegSet
insertLRegSet :: LocalReg -> LRegSet -> LRegSet
insertLRegSet LocalReg
l = Key -> LRegSet -> LRegSet
Word64Set.insert (Unique -> Key
getKey (LocalReg -> Unique
forall a. Uniquable a => a -> Unique
getUnique LocalReg
l))

elemLRegSet :: LocalReg -> LRegSet -> Bool
elemLRegSet :: LocalReg -> LRegSet -> Bool
elemLRegSet LocalReg
l = Key -> LRegSet -> Bool
Word64Set.member (Unique -> Key
getKey (LocalReg -> Unique
forall a. Uniquable a => a -> Unique
getUnique LocalReg
l))

deleteFromLRegSet :: LRegSet -> LocalReg -> LRegSet
deleteFromLRegSet :: LRegSet -> LocalReg -> LRegSet
deleteFromLRegSet LRegSet
set LocalReg
reg = Key -> LRegSet -> LRegSet
Word64Set.delete (Unique -> Key
getKey (Unique -> Key) -> (LocalReg -> Unique) -> LocalReg -> Key
forall b c a. (b -> c) -> (a -> b) -> a -> c
. LocalReg -> Unique
forall a. Uniquable a => a -> Unique
getUnique (LocalReg -> Key) -> LocalReg -> Key
forall a b. (a -> b) -> a -> b
$ LocalReg
reg) LRegSet
set

sizeLRegSet :: Word64Set -> Int
sizeLRegSet :: LRegSet -> Int
sizeLRegSet = LRegSet -> Int
Word64Set.size

plusLRegSet :: Word64Set -> Word64Set -> Word64Set
plusLRegSet :: LRegSet -> LRegSet -> LRegSet
plusLRegSet = LRegSet -> LRegSet -> LRegSet
Word64Set.union

elemsLRegSet :: Word64Set -> [Word64]
elemsLRegSet :: LRegSet -> [Key]
elemsLRegSet = LRegSet -> [Key]
Word64Set.toList