PageRenderTime 53ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/compiler/utils/GraphBase.hs

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