1 -- | Basic operations on graphs.
3 {-# OPTIONS -fno-warn-missing-signatures #-}
6 addNode, delNode, getNode, lookupNode, modNode,
9 addConflict, delConflict, addConflicts,
10 addCoalesce, delCoalesce,
17 slurpNodeConflictCount
28 import Data.List hiding (union)
32 -- | Lookup a node from the graph.
36 -> k -> Maybe (Node k cls color)
39 = lookupUFM (graphMap graph) k
42 -- | Get a node from the graph, throwing an error if it's not there
46 -> k -> Node k cls color
49 = case lookupUFM (graphMap graph) k of
51 Nothing -> panic "ColorOps.getNode: not found"
54 -- | Add a node to the graph, linking up its edges
55 addNode :: Uniquable k
56 => k -> Node k cls color
57 -> Graph k cls color -> Graph k cls color
61 -- add back conflict edges from other nodes to this one
64 (adjustUFM (\n -> n { nodeConflicts = addOneToUniqSet (nodeConflicts n) k}))
68 -- add back coalesce edges from other nodes to this one
71 (adjustUFM (\n -> n { nodeCoalesce = addOneToUniqSet (nodeCoalesce n) k}))
76 { graphMap = addToUFM map_coalesce k node}
80 -- | Delete a node and all its edges from the graph.
81 -- Throws an error if it's not there.
82 delNode :: Uniquable k
83 => k -> Graph k cls color -> Graph k cls color
86 = let Just node = lookupNode graph k
88 -- delete conflict edges from other nodes to this one.
89 graph1 = foldl' (\g k1 -> let Just g' = delConflict k1 k g in g') graph
90 $ uniqSetToList (nodeConflicts node)
92 -- delete coalesce edge from other nodes to this one.
93 graph2 = foldl' (\g k1 -> let Just g' = delCoalesce k1 k g in g') graph1
94 $ uniqSetToList (nodeCoalesce node)
97 graph3 = graphMapModify (\fm -> delFromUFM fm k) graph2
102 -- | Modify a node in the graph.
103 -- returns Nothing if the node isn't present.
105 modNode :: Uniquable k
106 => (Node k cls color -> Node k cls color)
107 -> k -> Graph k cls color -> Maybe (Graph k cls color)
110 = case lookupNode graph k of
114 (\fm -> let Just node = lookupUFM fm k
116 in addToUFM fm k node')
121 -- | Get the size of the graph, O(n)
123 => Graph k cls color -> Int
126 = sizeUFM $ graphMap graph
129 -- | Union two graphs together.
131 => Graph k cls color -> Graph k cls color -> Graph k cls color
135 { graphMap = plusUFM (graphMap graph1) (graphMap graph2) }
140 -- | Add a conflict between nodes to the graph, creating the nodes required.
141 -- Conflicts are virtual regs which need to be colored differently.
144 => (k, cls) -> (k, cls)
145 -> Graph k cls color -> Graph k cls color
147 addConflict (u1, c1) (u2, c2)
148 = let addNeighbor u c u'
149 = adjustWithDefaultUFM
150 (\node -> node { nodeConflicts = addOneToUniqSet (nodeConflicts node) u' })
151 (newNode u c) { nodeConflicts = unitUniqSet u' }
155 ( addNeighbor u1 c1 u2
156 . addNeighbor u2 c2 u1)
159 -- | Delete a conflict edge. k1 -> k2
160 -- returns Nothing if the node isn't in the graph
164 -> Graph k cls color -> Maybe (Graph k cls color)
168 (\node -> node { nodeConflicts = delOneFromUniqSet (nodeConflicts node) k2 })
172 -- | Add some conflicts to the graph, creating nodes if required.
173 -- All the nodes in the set are taken to conflict with each other.
176 => UniqSet k -> (k -> cls)
177 -> Graph k cls color -> Graph k cls color
179 addConflicts conflicts getClass
181 -- just a single node, but no conflicts, create the node anyway.
182 | (u : []) <- uniqSetToList conflicts
184 $ adjustWithDefaultUFM
186 (newNode u (getClass u))
191 $ (\fm -> foldr (\u -> addConflictSet1 u getClass conflicts) fm
192 $ uniqSetToList conflicts)
195 addConflictSet1 u getClass set
196 = let set' = delOneFromUniqSet set u
197 in adjustWithDefaultUFM
198 (\node -> node { nodeConflicts = unionUniqSets set' (nodeConflicts node) } )
199 (newNode u (getClass u)) { nodeConflicts = set' }
203 -- | Add an exclusion to the graph, creating nodes if required.
204 -- These are extra colors that the node cannot use.
206 :: (Uniquable k, Uniquable color)
207 => k -> (k -> cls) -> color
208 -> Graph k cls color -> Graph k cls color
210 addExclusion u getClass color
212 $ adjustWithDefaultUFM
213 (\node -> node { nodeExclusions = addOneToUniqSet (nodeExclusions node) color })
214 (newNode u (getClass u)) { nodeExclusions = unitUniqSet color }
218 -- | Add a coalescence edge to the graph, creating nodes if requried.
219 -- It is considered adventageous to assign the same color to nodes in a coalesence.
222 => (k, cls) -> (k, cls)
223 -> Graph k cls color -> Graph k cls color
225 addCoalesce (u1, c1) (u2, c2)
226 = let addCoalesce u c u'
227 = adjustWithDefaultUFM
228 (\node -> node { nodeCoalesce = addOneToUniqSet (nodeCoalesce node) u' })
229 (newNode u c) { nodeCoalesce = unitUniqSet u' }
233 ( addCoalesce u1 c1 u2
234 . addCoalesce u2 c2 u1)
237 -- | Delete a coalescence edge (k1 -> k2) from the graph.
241 -> Graph k cls color -> Maybe (Graph k cls color)
244 = modNode (\node -> node { nodeCoalesce = delOneFromUniqSet (nodeCoalesce node) k2 })
248 -- | Add a color preference to the graph, creating nodes if required.
249 -- The most recently added preference is the most prefered.
250 -- The algorithm tries to assign a node it's prefered color if possible.
255 -> Graph k cls color -> Graph k cls color
257 addPreference (u, c) color
259 $ adjustWithDefaultUFM
260 (\node -> node { nodePreference = color : (nodePreference node) })
261 (newNode u c) { nodePreference = [color] }
265 -- | Do agressive coalescing on this graph.
266 -- returns the new graph and the list of pairs of nodes that got coaleced together.
267 -- for each pair, the resulting node will have the least key and be second in the pair.
270 :: (Uniquable k, Ord k, Eq cls, Outputable k)
273 -> (Graph k cls color, [(k, k)])
275 coalesceGraph triv graph
277 -- find all the nodes that have coalescence edges
278 cNodes = filter (\node -> not $ isEmptyUniqSet (nodeCoalesce node))
279 $ eltsUFM $ graphMap graph
281 -- build a list of pairs of keys for node's we'll try and coalesce
282 -- every pair of nodes will appear twice in this list
283 -- ie [(k1, k2), (k2, k1) ... ]
284 -- This is ok, GrapOps.coalesceNodes handles this and it's convenient for
285 -- build a list of what nodes get coalesced together for later on.
287 cList = [ (nodeId node1, k2)
289 , k2 <- uniqSetToList $ nodeCoalesce node1 ]
291 -- do the coalescing, returning the new graph and a list of pairs of keys
292 -- that got coalesced together.
294 = mapAccumL (coalesceNodes False triv) graph cList
296 in (graph', catMaybes mPairs)
299 -- | Coalesce this pair of nodes unconditionally / agressively.
300 -- The resulting node is the one with the least key.
302 -- returns: Just the pair of keys if the nodes were coalesced
303 -- the second element of the pair being the least one
305 -- Nothing if either of the nodes weren't in the graph
308 :: (Uniquable k, Ord k, Eq cls, Outputable k)
309 => Bool -- ^ If True, coalesce nodes even if this might make the graph
310 -- less colorable (aggressive coalescing)
313 -> (k, k) -- ^ keys of the nodes to be coalesced
314 -> (Graph k cls color, Maybe (k, k))
316 coalesceNodes aggressive triv graph (k1, k2)
317 | (kMin, kMax) <- if k1 < k2
321 -- the nodes being coalesced must be in the graph
322 , Just nMin <- lookupNode graph kMin
323 , Just nMax <- lookupNode graph kMax
325 -- can't coalesce conflicting modes
326 , not $ elementOfUniqSet kMin (nodeConflicts nMax)
327 , not $ elementOfUniqSet kMax (nodeConflicts nMin)
329 = coalesceNodes_merge aggressive triv graph kMin kMax nMin nMax
331 -- don't do the coalescing after all
335 coalesceNodes_merge aggressive triv graph kMin kMax nMin nMax
338 | nodeClass nMin /= nodeClass nMax
339 = error "GraphOps.coalesceNodes: can't coalesce nodes of different classes."
341 | not (isNothing (nodeColor nMin) && isNothing (nodeColor nMax))
342 = error "GraphOps.coalesceNodes: can't coalesce colored nodes."
347 -- the new node gets all the edges from its two components
350 , nodeClass = nodeClass nMin
351 , nodeColor = Nothing
353 -- nodes don't conflict with themselves..
355 = (unionUniqSets (nodeConflicts nMin) (nodeConflicts nMax))
356 `delOneFromUniqSet` kMin
357 `delOneFromUniqSet` kMax
359 , nodeExclusions = unionUniqSets (nodeExclusions nMin) (nodeExclusions nMax)
360 , nodePreference = nodePreference nMin ++ nodePreference nMax
362 -- nodes don't coalesce with themselves..
364 = (unionUniqSets (nodeCoalesce nMin) (nodeCoalesce nMax))
365 `delOneFromUniqSet` kMin
366 `delOneFromUniqSet` kMax
369 in coalesceNodes_check aggressive triv graph kMin kMax node
371 coalesceNodes_check aggressive triv graph kMin kMax node
373 -- Unless we're coalescing aggressively, if the result node is not trivially
374 -- colorable then don't do the coalescing.
376 , not $ triv (nodeClass node) (nodeConflicts node) (nodeExclusions node)
380 = let -- delete the old nodes from the graph and add the new one
381 graph' = addNode kMin node
386 in (graph', Just (kMax, kMin))
389 -- | validate the internal structure of a graph
390 -- all its edges should point to valid nodes
391 -- if they don't then throw an error
394 :: (Uniquable k, Outputable k)
399 validateGraph doc graph
400 = let edges = unionUniqSets
402 (map nodeConflicts $ eltsUFM $ graphMap graph))
404 (map nodeCoalesce $ eltsUFM $ graphMap graph))
406 nodes = mkUniqSet $ map nodeId $ eltsUFM $ graphMap graph
408 badEdges = minusUniqSet edges nodes
410 in if isEmptyUniqSet badEdges
412 else pprPanic "GraphOps.validateGraph"
413 ( text "-- bad edges"
414 $$ vcat (map ppr $ uniqSetToList badEdges)
415 $$ text "----------------------------"
419 -- | Slurp out a map of how many nodes had a certain number of conflict neighbours
421 slurpNodeConflictCount
424 -> UniqFM (Int, Int) -- ^ (conflict neighbours, num nodes with that many conflicts)
426 slurpNodeConflictCount graph
428 (\(c1, n1) (_, n2) -> (c1, n1 + n2))
431 -> let count = sizeUniqSet $ nodeConflicts node
432 in (count, (count, 1)))
437 -- | Set the color of a certain node
441 -> Graph k cls color -> Graph k cls color
446 (\n -> n { nodeColor = Just color })
452 => (a -> a) -> a -> k
453 -> UniqFM a -> UniqFM a
455 adjustWithDefaultUFM f def k map
465 -> k -> UniqFM a -> UniqFM a
468 = case lookupUFM map k of
470 Just a -> addToUFM map k (f a)