1 -- | Graph coloring register allocator.
4 -- The function that choosing the potential spills could be a bit cleverer.
5 -- Colors in graphviz graphs could be nicer.
7 {-# OPTIONS -fno-warn-missing-signatures #-}
16 import qualified GraphColor as Color
35 -- | The maximum number of build/spill cycles we'll allow.
36 -- We should only need 3 or 4 cycles tops.
37 -- If we run for any longer than this we're probably in an infinite loop,
38 -- It's probably better just to bail out and report a bug at this stage.
43 -- | The top level of the graph coloring register allocator.
46 :: Bool -- ^ whether to generate RegAllocStats, or not.
47 -> UniqFM (UniqSet Reg) -- ^ the registers we can use for allocation
48 -> UniqSet Int -- ^ the set of available spill slots.
49 -> [LiveCmmTop] -- ^ code annotated with liveness information.
51 ( [NatCmmTop] -- ^ code with registers allocated.
52 , [RegAllocStats] ) -- ^ stats for each stage of allocation
54 regAlloc dump regsFree slotsFree code
56 (code_final, debug_codeGraphs, _)
57 <- regAlloc_spin dump 0 trivColorable regsFree slotsFree [] code
60 , reverse debug_codeGraphs )
62 regAlloc_spin dump (spinCount :: Int) triv regsFree slotsFree debug_codeGraphs code
64 -- check that we're not running off down the garden path.
65 when (spinCount > maxSpinCount)
66 $ pprPanic "regAlloc_spin: max build/spill cycle count exceeded."
67 ( text "It looks like the register allocator is stuck in an infinite loop."
68 $$ text "max cycles = " <> int maxSpinCount
69 $$ text "regsFree = " <> (hcat $ punctuate space $ map (docToSDoc . pprUserReg)
70 $ uniqSetToList $ unionManyUniqSets $ eltsUFM regsFree)
71 $$ text "slotsFree = " <> ppr (sizeUniqSet slotsFree))
73 -- build a conflict graph from the code.
74 graph <- {-# SCC "BuildGraph" #-} buildGraph code
77 -- We really do want the graph to be fully evaluated _before_ we start coloring.
78 -- If we don't do this now then when the call to Color.colorGraph forces bits of it,
79 -- the heap will be filled with half evaluated pieces of graph and zillions of apply thunks.
81 seqGraph graph `seq` return ()
84 -- build a map of how many instructions each reg lives for.
85 -- this is lazy, it won't be computed unless we need to spill
87 let fmLife = {-# SCC "LifetimeCount" #-} plusUFMs_C (\(r1, l1) (_, l2) -> (r1, l1 + l2))
88 $ map lifetimeCount code
90 -- record startup state
93 then Just $ RegAllocStatsStart
96 , raLifetimes = fmLife }
100 -- the function to choose regs to leave uncolored
101 let spill = chooseSpill_maxLife fmLife
103 -- try and color the graph
104 let (graph_colored, rsSpill, rmCoalesce)
105 = {-# SCC "ColorGraph" #-} Color.colorGraph regsFree triv spill graph
107 -- rewrite regs in the code that have been coalesced
108 let patchF reg = case lookupUFM rmCoalesce reg of
112 = map (patchEraseLive patchF) code
115 -- see if we've found a coloring
116 if isEmptyUniqSet rsSpill
118 -- patch the registers using the info in the graph
119 let code_patched = map (patchRegsFromGraph graph_colored) code_coalesced
121 -- clean out unneeded SPILL/RELOADs
122 let code_spillclean = map cleanSpills code_patched
124 -- strip off liveness information
125 let code_nat = map stripLive code_patched
127 -- rewrite SPILL/RELOAD pseudos into real instructions
128 let spillNatTop = mapGenBlockTop spillNatBlock
129 let code_final = map spillNatTop code_nat
131 -- record what happened in this stage for debugging
134 { raGraph = graph_colored
135 , raCoalesced = rmCoalesce
136 , raPatched = code_patched
137 , raSpillClean = code_spillclean
138 , raFinal = code_final
139 , raSRMs = foldl' addSRM (0, 0, 0) $ map countSRMs code_spillclean }
143 if dump then [stat] ++ maybeToList stat1 ++ debug_codeGraphs
146 -- space leak avoidance
147 seqList statList `seq` return ()
154 -- spill the uncolored regs
155 (code_spilled, slotsFree', spillStats)
156 <- regSpill code_coalesced slotsFree rsSpill
158 -- recalculate liveness
159 let code_nat = map stripLive code_spilled
160 code_relive <- mapM regLiveness code_nat
162 -- record what happened in this stage for debugging
165 { raGraph = graph_colored
166 , raCoalesced = rmCoalesce
167 , raSpillStats = spillStats
168 , raLifetimes = fmLife
169 , raSpilled = code_spilled }
173 then [stat] ++ maybeToList stat1 ++ debug_codeGraphs
176 -- space leak avoidance
177 seqList statList `seq` return ()
179 regAlloc_spin dump (spinCount + 1) triv regsFree slotsFree'
185 -- Simple maxconflicts isn't always good, because we
186 -- can naievely end up spilling vregs that only live for one or two instrs.
189 chooseSpill_maxConflicts
190 :: Color.Graph Reg RegClass Reg
193 chooseSpill_maxConflicts graph
194 = let node = maximumBy
196 (sizeUniqSet $ Color.nodeConflicts n1)
197 (sizeUniqSet $ Color.nodeConflicts n2))
198 $ eltsUFM $ Color.graphMap graph
206 -> Color.Graph Reg RegClass Reg
209 chooseSpill_maxLife life graph
210 = let node = maximumBy (\n1 n2 -> compare (getLife n1) (getLife n2))
211 $ eltsUFM $ Color.graphMap graph
213 -- Orphan vregs die in the same instruction they are born in.
214 -- They will be in the graph, but not in the liveness map.
215 -- Their liveness is 0.
217 = case lookupUFM life (Color.nodeId n) of
224 -- | Build a graph from the liveness and coalesce information in this code.
228 -> UniqSM (Color.Graph Reg RegClass Reg)
232 -- Slurp out the conflicts and reg->reg moves from this code
233 let (conflictList, moveList) =
234 unzip $ map slurpConflicts code
236 let conflictBag = unionManyBags conflictList
237 let moveBag = unionManyBags moveList
239 -- Add the reg-reg conflicts to the graph
240 let graph_conflict = foldrBag graphAddConflictSet Color.initGraph conflictBag
242 -- Add the coalescences edges to the graph.
243 let graph_coalesce = foldrBag graphAddCoalesce graph_conflict moveBag
245 return graph_coalesce
248 -- | Add some conflict edges to the graph.
249 -- Conflicts between virtual and real regs are recorded as exclusions.
253 -> Color.Graph Reg RegClass Reg
254 -> Color.Graph Reg RegClass Reg
256 graphAddConflictSet set graph
257 = let reals = filterUFM isRealReg set
258 virtuals = filterUFM (not . isRealReg) set
260 graph1 = Color.addConflicts virtuals regClass graph
261 graph2 = foldr (\(r1, r2) -> Color.addExclusion r1 regClass r2)
264 | a <- uniqSetToList virtuals
265 , b <- uniqSetToList reals]
270 -- | Add some coalesence edges to the graph
271 -- Coalesences between virtual and real regs are recorded as preferences.
275 -> Color.Graph Reg RegClass Reg
276 -> Color.Graph Reg RegClass Reg
278 graphAddCoalesce (r1, r2) graph
280 = Color.addPreference (regWithClass r2) r1 graph
283 = Color.addPreference (regWithClass r1) r2 graph
286 = Color.addCoalesce (regWithClass r1) (regWithClass r2) graph
288 where regWithClass r = (r, regClass r)
291 -- | Patch registers in code using the reg -> reg mapping in this graph.
293 :: Color.Graph Reg RegClass Reg
294 -> LiveCmmTop -> LiveCmmTop
296 patchRegsFromGraph graph code
298 -- a function to lookup the hardreg for a virtual reg from the graph.
300 -- leave real regs alone.
304 -- this virtual has a regular node in the graph.
305 | Just node <- Color.lookupNode graph reg
306 = case Color.nodeColor node of
310 -- no node in the graph for this virtual, bad news.
312 = pprPanic "patchRegsFromGraph: register mapping failed."
313 ( text "There is no node in the graph for register " <> ppr reg
315 $$ Color.dotGraph (\_ -> text "white") trivColorable graph)
317 in patchEraseLive patchF code
320 plusUFMs_C :: (elt -> elt -> elt) -> [UniqFM elt] -> UniqFM elt
322 = foldl' (plusUFM_C f) emptyUFM maps
326 -- for when laziness just isn't what you wanted...
328 seqGraph :: Color.Graph Reg RegClass Reg -> ()
329 seqGraph graph = seqNodes (eltsUFM (Color.graphMap graph))
331 seqNodes :: [Color.Node Reg RegClass Reg] -> ()
335 (n : ns) -> seqNode n `seq` seqNodes ns
337 seqNode :: Color.Node Reg RegClass Reg -> ()
339 = seqReg (Color.nodeId node)
340 `seq` seqRegClass (Color.nodeClass node)
341 `seq` seqMaybeReg (Color.nodeColor node)
342 `seq` (seqRegList (uniqSetToList (Color.nodeConflicts node)))
343 `seq` (seqRegList (uniqSetToList (Color.nodeExclusions node)))
344 `seq` (seqRegList (Color.nodePreference node))
345 `seq` (seqRegList (uniqSetToList (Color.nodeCoalesce node)))
356 seqRegClass :: RegClass -> ()
363 seqMaybeReg :: Maybe Reg -> ()
369 seqRegList :: [Reg] -> ()
373 (r : rs) -> seqReg r `seq` seqRegList rs
379 (r : rs) -> r `seq` seqList rs