PageRenderTime 60ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/compiler/utils/GraphBase.hs

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