1 -- | Graph coloring register allocator.
4 -- Live range splitting:
5 -- At the moment regs that are spilled are spilled for all time, even though
6 -- we might be able to allocate them a hardreg in different parts of the code.
8 -- As we're aggressively coalescing before register allocation proper we're not currently
9 -- using the coalescence information present in the graph.
11 -- The function that choosing the potential spills could be a bit cleverer.
13 -- Colors in graphviz graphs could be nicer.
16 module RegAllocColor (
23 import qualified GraphColor as Color
42 -- | The maximum number of build/spill cycles we'll allow.
43 -- We should only need 3 or 4 cycles tops.
44 -- If we run for any longer than this we're probably in an infinite loop,
45 -- It's probably better just to bail out and report a bug at this stage.
50 -- | The top level of the graph coloring register allocator.
53 :: Bool -- ^ whether to generate RegAllocStats, or not.
54 -> UniqFM (UniqSet Reg) -- ^ the registers we can use for allocation
55 -> UniqSet Int -- ^ the set of available spill slots.
56 -> [LiveCmmTop] -- ^ code annotated with liveness information.
58 ( [NatCmmTop] -- ^ code with registers allocated.
59 , [RegAllocStats] ) -- ^ stats for each stage of allocation
61 regAlloc dump regsFree slotsFree code
63 (code_final, debug_codeGraphs, graph_final)
64 <- regAlloc_spin dump 0 trivColorable regsFree slotsFree [] code
67 , reverse debug_codeGraphs )
69 regAlloc_spin dump (spinCount :: Int) triv regsFree slotsFree debug_codeGraphs code
71 -- check that we're not running off down the garden path.
72 when (spinCount > maxSpinCount)
73 $ pprPanic "regAlloc_spin: max build/spill cycle count exceeded."
74 ( text "It looks like the register allocator is stuck in an infinite loop."
75 $$ text "max cycles = " <> int maxSpinCount
76 $$ text "regsFree = " <> (hcat $ punctuate space $ map (docToSDoc . pprUserReg)
77 $ uniqSetToList $ unionManyUniqSets $ eltsUFM regsFree)
78 $$ text "slotsFree = " <> ppr (sizeUniqSet slotsFree))
80 -- build a conflict graph from the code.
81 graph <- buildGraph code
83 -- build a map of how many instructions each reg lives for.
84 -- this is lazy, it won't be computed unless we need to spill
85 let fmLife = plusUFMs_C (\(r1, l1) (r2, l2) -> (r1, l1 + l2))
86 $ map lifetimeCount code
88 -- record startup state
91 then Just $ RegAllocStatsStart
94 , raLifetimes = fmLife }
98 -- the function to choose regs to leave uncolored
99 let spill = chooseSpill_maxLife fmLife
101 -- try and color the graph
102 let (graph_colored, rsSpill)
103 = Color.colorGraph regsFree triv spill graph
105 -- see if we've found a coloring
106 if isEmptyUniqSet rsSpill
108 -- patch the registers using the info in the graph
109 let code_patched = map (patchRegsFromGraph graph_colored) code
111 -- strip off liveness information
112 let code_nat = map stripLive code_patched
114 -- rewrite SPILL/REALOAD pseudos into real instructions
115 let spillNatTop = mapGenBlockTop spillNatBlock
116 let code_final = map spillNatTop code_nat
118 -- record what happened in this stage for debugging
121 { raGraph = graph_colored
122 , raPatchedCmm = code_patched
123 , raFinalCmm = code_final }
127 then [stat] ++ maybeToList stat1 ++ debug_codeGraphs
132 -- spill the uncolored regs
133 (code_spilled, slotsFree', spillStats)
134 <- regSpill code slotsFree rsSpill
136 -- recalculate liveness
137 let code_nat = map stripLive code_spilled
138 code_relive <- mapM regLiveness code_nat
140 -- record what happened in this stage for debugging
143 { raGraph = graph_colored
144 , raSpillStats = spillStats
145 , raLifetimes = fmLife
146 , raSpilled = code_spilled }
149 regAlloc_spin dump (spinCount + 1) triv regsFree slotsFree'
151 then [stat] ++ maybeToList stat1 ++ debug_codeGraphs
157 -- Simple maxconflicts isn't always good, because we
158 -- can naievely end up spilling vregs that only live for one or two instrs.
161 chooseSpill_maxConflicts
162 :: Color.Graph Reg RegClass Reg
165 chooseSpill_maxConflicts graph
166 = let node = maximumBy
168 (sizeUniqSet $ Color.nodeConflicts n1)
169 (sizeUniqSet $ Color.nodeConflicts n2))
170 $ eltsUFM $ Color.graphMap graph
178 -> Color.Graph Reg RegClass Reg
181 chooseSpill_maxLife life graph
182 = let node = maximumBy (\n1 n2 -> compare (getLife n1) (getLife n2))
183 $ eltsUFM $ Color.graphMap graph
185 -- Orphan vregs die in the same instruction they are born in.
186 -- They will be in the graph, but not in the liveness map.
187 -- Their liveness is 0.
189 = case lookupUFM life (Color.nodeId n) of
196 -- | Build a graph from the liveness and coalesce information in this code.
200 -> UniqSM (Color.Graph Reg RegClass Reg)
204 -- Add the reg-reg conflicts to the graph
205 let conflictSets = unionManyBags (map slurpConflicts code)
206 let graph_conflict = foldrBag graphAddConflictSet Color.initGraph conflictSets
209 -- Add the coalescences edges to the graph.
210 let coalesce = unionManyBags (map slurpJoinMovs code)
211 let graph_coalesce = foldrBag graphAddCoalesce graph_conflict coalesce
213 return $ graph_coalesce
216 -- | Add some conflict edges to the graph.
217 -- Conflicts between virtual and real regs are recorded as exclusions.
221 -> Color.Graph Reg RegClass Reg
222 -> Color.Graph Reg RegClass Reg
224 graphAddConflictSet set graph
225 = let reals = filterUFM isRealReg set
226 virtuals = filterUFM (not . isRealReg) set
228 graph1 = Color.addConflicts virtuals regClass graph
229 graph2 = foldr (\(r1, r2) -> Color.addExclusion r1 regClass r2)
232 | a <- uniqSetToList virtuals
233 , b <- uniqSetToList reals]
238 -- | Add some coalesence edges to the graph
239 -- Coalesences between virtual and real regs are recorded as preferences.
243 -> Color.Graph Reg RegClass Reg
244 -> Color.Graph Reg RegClass Reg
246 graphAddCoalesce (r1, r2) graph
247 | RealReg regno <- r1
248 = Color.addPreference (regWithClass r2) r1 graph
250 | RealReg regno <- r2
251 = Color.addPreference (regWithClass r1) r2 graph
254 = Color.addCoalesce (regWithClass r1) (regWithClass r2) graph
256 where regWithClass r = (r, regClass r)
259 -- | Patch registers in code using the reg -> reg mapping in this graph.
261 :: Color.Graph Reg RegClass Reg
262 -> LiveCmmTop -> LiveCmmTop
264 patchRegsFromGraph graph code
266 -- a function to lookup the hardreg for a virtual reg from the graph.
268 -- leave real regs alone.
272 -- this virtual has a regular node in the graph.
273 | Just node <- Color.lookupNode graph reg
274 = case Color.nodeColor node of
278 -- no node in the graph for this virtual, bad news.
280 = pprPanic "patchRegsFromGraph: register mapping failed."
281 ( text "There is no node in the graph for register " <> ppr reg
283 $$ Color.dotGraph (\x -> text "white") trivColorable graph)
285 in patchEraseLive patchF code
288 plusUFMs_C :: (elt -> elt -> elt) -> [UniqFM elt] -> UniqFM elt
290 = foldl (plusUFM_C f) emptyUFM maps