/compiler/GHC/Data/Graph/Base.hs

https://github.com/bgamari/ghc · Haskell · 107 lines · 47 code · 26 blank · 34 comment · 0 complexity · 10bdd995207cb6bce862dd05a7c82573 MD5 · raw file

  1. -- | Types for the general graph colorer.
  2. module GHC.Data.Graph.Base (
  3. Triv,
  4. Graph (..),
  5. initGraph,
  6. graphMapModify,
  7. Node (..), newNode,
  8. )
  9. where
  10. import GHC.Prelude
  11. import GHC.Types.Unique.Set
  12. import GHC.Types.Unique.FM
  13. -- | A fn to check if a node is trivially colorable
  14. -- For graphs who's color classes are disjoint then a node is 'trivially colorable'
  15. -- when it has less neighbors and exclusions than available colors for that node.
  16. --
  17. -- For graph's who's color classes overlap, ie some colors alias other colors, then
  18. -- this can be a bit more tricky. There is a general way to calculate this, but
  19. -- it's likely be too slow for use in the code. The coloring algorithm takes
  20. -- a canned function which can be optimised by the user to be specific to the
  21. -- specific graph being colored.
  22. --
  23. -- for details, see "A Generalised Algorithm for Graph-Coloring Register Allocation"
  24. -- Smith, Ramsey, Holloway - PLDI 2004.
  25. --
  26. type Triv k cls color
  27. = cls -- the class of the node we're trying to color.
  28. -> UniqSet k -- the node's neighbors.
  29. -> UniqSet color -- the node's exclusions.
  30. -> Bool
  31. -- | The Interference graph.
  32. -- There used to be more fields, but they were turfed out in a previous revision.
  33. -- maybe we'll want more later..
  34. --
  35. newtype Graph k cls color
  36. = Graph {
  37. -- | All active nodes in the graph.
  38. graphMap :: UniqFM k (Node k cls color) }
  39. -- | An empty graph.
  40. initGraph :: Graph k cls color
  41. initGraph
  42. = Graph
  43. { graphMap = emptyUFM }
  44. -- | Modify the finite map holding the nodes in the graph.
  45. graphMapModify
  46. :: (UniqFM k (Node k cls color) -> UniqFM k (Node k cls color))
  47. -> Graph k cls color -> Graph k cls color
  48. graphMapModify f graph
  49. = graph { graphMap = f (graphMap graph) }
  50. -- | Graph nodes.
  51. -- Represents a thing that can conflict with another thing.
  52. -- For the register allocater the nodes represent registers.
  53. --
  54. data Node k cls color
  55. = Node {
  56. -- | A unique identifier for this node.
  57. nodeId :: k
  58. -- | The class of this node,
  59. -- determines the set of colors that can be used.
  60. , nodeClass :: cls
  61. -- | The color of this node, if any.
  62. , nodeColor :: Maybe color
  63. -- | Neighbors which must be colored differently to this node.
  64. , nodeConflicts :: UniqSet k
  65. -- | Colors that cannot be used by this node.
  66. , nodeExclusions :: UniqSet color
  67. -- | Colors that this node would prefer to be, in descending order.
  68. , nodePreference :: [color]
  69. -- | Neighbors that this node would like to be colored the same as.
  70. , nodeCoalesce :: UniqSet k }
  71. -- | An empty node.
  72. newNode :: k -> cls -> Node k cls color
  73. newNode k cls
  74. = Node
  75. { nodeId = k
  76. , nodeClass = cls
  77. , nodeColor = Nothing
  78. , nodeConflicts = emptyUniqSet
  79. , nodeExclusions = emptyUniqSet
  80. , nodePreference = []
  81. , nodeCoalesce = emptyUniqSet }