-- | Types for the general graph colorer.
module GraphBase (
        Triv,
        Graph (..),
        initGraph,
        graphMapModify,

        Node  (..),     newNode,
)


where

import GhcPrelude

import UniqSet
import UniqFM


-- | A fn to check if a node is trivially colorable
--      For graphs who's color classes are disjoint then a node is 'trivially colorable'
--      when it has less neighbors and exclusions than available colors for that node.
--
--      For graph's who's color classes overlap, ie some colors alias other colors, then
--      this can be a bit more tricky. There is a general way to calculate this, but
--      it's likely be too slow for use in the code. The coloring algorithm takes
--      a canned function which can be optimised by the user to be specific to the
--      specific graph being colored.
--
--      for details, see  "A Generalised Algorithm for Graph-Coloring Register Allocation"
--                              Smith, Ramsey, Holloway - PLDI 2004.
--
type Triv k cls color
        =  cls                  -- the class of the node we're trying to color.
        -> UniqSet k            -- the node's neighbors.
        -> UniqSet color        -- the node's exclusions.
        -> Bool


-- | The Interference graph.
--      There used to be more fields, but they were turfed out in a previous revision.
--      maybe we'll want more later..
--
data Graph k cls color
        = Graph {
        -- | All active nodes in the graph.
          Graph k cls color -> UniqFM (Node k cls color)
graphMap              :: UniqFM (Node k cls color)  }


-- | An empty graph.
initGraph :: Graph k cls color
initGraph :: Graph k cls color
initGraph
        = Graph :: forall k cls color. UniqFM (Node k cls color) -> Graph k cls color
Graph
        { graphMap :: UniqFM (Node k cls color)
graphMap              = UniqFM (Node k cls color)
forall elt. UniqFM elt
emptyUFM }


-- | Modify the finite map holding the nodes in the graph.
graphMapModify
        :: (UniqFM (Node k cls color) -> UniqFM (Node k cls color))
        -> Graph k cls color -> Graph k cls color

graphMapModify :: (UniqFM (Node k cls color) -> UniqFM (Node k cls color))
-> Graph k cls color -> Graph k cls color
graphMapModify UniqFM (Node k cls color) -> UniqFM (Node k cls color)
f Graph k cls color
graph
        = Graph k cls color
graph { graphMap :: UniqFM (Node k cls color)
graphMap      = UniqFM (Node k cls color) -> UniqFM (Node k cls color)
f (Graph k cls color -> UniqFM (Node k cls color)
forall k cls color. Graph k cls color -> UniqFM (Node k cls color)
graphMap Graph k cls color
graph) }



-- | Graph nodes.
--      Represents a thing that can conflict with another thing.
--      For the register allocater the nodes represent registers.
--
data Node k cls color
        = Node {
        -- | A unique identifier for this node.
          Node k cls color -> k
nodeId                :: k

        -- | The class of this node,
        --      determines the set of colors that can be used.
        , Node k cls color -> cls
nodeClass             :: cls

        -- | The color of this node, if any.
        , Node k cls color -> Maybe color
nodeColor             :: Maybe color

        -- | Neighbors which must be colored differently to this node.
        , Node k cls color -> UniqSet k
nodeConflicts         :: UniqSet k

        -- | Colors that cannot be used by this node.
        , Node k cls color -> UniqSet color
nodeExclusions        :: UniqSet color

        -- | Colors that this node would prefer to be, in decending order.
        , Node k cls color -> [color]
nodePreference        :: [color]

        -- | Neighbors that this node would like to be colored the same as.
        , Node k cls color -> UniqSet k
nodeCoalesce          :: UniqSet k }


-- | An empty node.
newNode :: k -> cls -> Node k cls color
newNode :: k -> cls -> Node k cls color
newNode k
k cls
cls
        = Node :: forall k cls color.
k
-> cls
-> Maybe color
-> UniqSet k
-> UniqSet color
-> [color]
-> UniqSet k
-> Node k cls color
Node
        { nodeId :: k
nodeId                = k
k
        , nodeClass :: cls
nodeClass             = cls
cls
        , nodeColor :: Maybe color
nodeColor             = Maybe color
forall a. Maybe a
Nothing
        , nodeConflicts :: UniqSet k
nodeConflicts         = UniqSet k
forall a. UniqSet a
emptyUniqSet
        , nodeExclusions :: UniqSet color
nodeExclusions        = UniqSet color
forall a. UniqSet a
emptyUniqSet
        , nodePreference :: [color]
nodePreference        = []
        , nodeCoalesce :: UniqSet k
nodeCoalesce          = UniqSet k
forall a. UniqSet a
emptyUniqSet }