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 :: UniqFM (UniqSet Reg) -- ^ the registers we can use for allocation
54 -> UniqSet Int -- ^ the set of available spill slots.
55 -> [LiveCmmTop] -- ^ code annotated with liveness information.
57 ( [NatCmmTop] -- ^ code with registers allocated.
58 , [RegAllocStats] ) -- ^ stats for each stage of allocation
60 regAlloc regsFree slotsFree code
62 (code_final, debug_codeGraphs, graph_final)
63 <- regAlloc_spin 0 trivColorable regsFree slotsFree [] code
66 , reverse debug_codeGraphs )
68 regAlloc_spin (spinCount :: Int) triv regsFree slotsFree debug_codeGraphs code
70 -- check that we're not running off down the garden path.
71 when (spinCount > maxSpinCount)
72 $ pprPanic "regAlloc_spin: max build/spill cycle count exceeded."
73 ( text "It looks like the register allocator is stuck in an infinite loop."
74 $$ text "max cycles = " <> int maxSpinCount
75 $$ text "regsFree = " <> (hcat $ punctuate space $ map (docToSDoc . pprUserReg)
76 $ uniqSetToList $ unionManyUniqSets $ eltsUFM regsFree)
77 $$ text "slotsFree = " <> ppr (sizeUniqSet slotsFree))
79 -- build a conflict graph from the code.
80 graph <- buildGraph code
82 -- build a map of how many instructions each reg lives for.
83 -- this is lazy, it won't be computed unless we need to spill
84 let fmLife = plusUFMs_C (\(r1, l1) (r2, l2) -> (r1, l1 + l2))
85 $ map lifetimeCount code
87 -- record startup state
90 then Just $ RegAllocStatsStart
93 , raLifetimes = fmLife }
97 -- the function to choose regs to leave uncolored
98 let spill = chooseSpill_maxLife fmLife
100 -- try and color the graph
101 let (graph_colored, rsSpill)
102 = Color.colorGraph regsFree triv spill graph
104 -- see if we've found a coloring
105 if isEmptyUniqSet rsSpill
107 -- patch the registers using the info in the graph
108 let code_patched = map (patchRegsFromGraph graph_colored) code
109 let code_nat = map stripLive code_patched
111 -- record what happened in this stage for debugging
114 { raGraph = graph_colored
115 , raPatchedCmm = code_patched }
118 , maybeToList stat1 ++ [stat] ++ debug_codeGraphs
122 -- spill the uncolored regs
123 (code_spilled, slotsFree', spillStats)
124 <- regSpill code slotsFree rsSpill
126 -- recalculate liveness
127 let code_nat = map stripLive code_spilled
128 code_relive <- mapM regLiveness code_nat
130 -- record what happened in this stage for debugging
133 { raGraph = graph_colored
134 , raSpillStats = spillStats
135 , raLifetimes = fmLife
136 , raSpilled = code_spilled }
139 regAlloc_spin (spinCount + 1) triv regsFree slotsFree'
140 (maybeToList stat1 ++ [stat] ++ debug_codeGraphs)
145 -- Simple maxconflicts isn't always good, because we
146 -- can naievely end up spilling vregs that only live for one or two instrs.
149 chooseSpill_maxConflicts
150 :: Color.Graph Reg RegClass Reg
153 chooseSpill_maxConflicts graph
154 = let node = maximumBy
156 (sizeUniqSet $ Color.nodeConflicts n1)
157 (sizeUniqSet $ Color.nodeConflicts n2))
158 $ eltsUFM $ Color.graphMap graph
166 -> Color.Graph Reg RegClass Reg
169 chooseSpill_maxLife life graph
170 = let node = maximumBy (\n1 n2 -> compare (getLife n1) (getLife n2))
171 $ eltsUFM $ Color.graphMap graph
173 -- Orphan vregs die in the same instruction they are born in.
174 -- They will be in the graph, but not in the liveness map.
175 -- Their liveness is 0.
177 = case lookupUFM life (Color.nodeId n) of
184 -- | Build a graph from the liveness and coalesce information in this code.
188 -> UniqSM (Color.Graph Reg RegClass Reg)
192 -- Add the reg-reg conflicts to the graph
193 let conflictSets = unionManyBags (map slurpConflicts code)
194 let graph_conflict = foldrBag graphAddConflictSet Color.initGraph conflictSets
197 -- Add the coalescences edges to the graph.
198 let coalesce = unionManyBags (map slurpJoinMovs code)
199 let graph_coalesce = foldrBag graphAddCoalesce graph_conflict coalesce
201 return $ graph_coalesce
204 -- | Add some conflict edges to the graph.
205 -- Conflicts between virtual and real regs are recorded as exclusions.
209 -> Color.Graph Reg RegClass Reg
210 -> Color.Graph Reg RegClass Reg
212 graphAddConflictSet set graph
213 = let reals = filterUFM isRealReg set
214 virtuals = filterUFM (not . isRealReg) set
216 graph1 = Color.addConflicts virtuals regClass graph
217 graph2 = foldr (\(r1, r2) -> Color.addExclusion r1 regClass r2)
220 | a <- uniqSetToList virtuals
221 , b <- uniqSetToList reals]
226 -- | Add some coalesence edges to the graph
227 -- Coalesences between virtual and real regs are recorded as preferences.
231 -> Color.Graph Reg RegClass Reg
232 -> Color.Graph Reg RegClass Reg
234 graphAddCoalesce (r1, r2) graph
235 | RealReg regno <- r1
236 = Color.addPreference (regWithClass r2) r1 graph
238 | RealReg regno <- r2
239 = Color.addPreference (regWithClass r1) r2 graph
242 = Color.addCoalesce (regWithClass r1) (regWithClass r2) graph
244 where regWithClass r = (r, regClass r)
247 -- | Patch registers in code using the reg -> reg mapping in this graph.
249 :: Color.Graph Reg RegClass Reg
250 -> LiveCmmTop -> LiveCmmTop
252 patchRegsFromGraph graph code
254 -- a function to lookup the hardreg for a virtual reg from the graph.
256 -- leave real regs alone.
260 -- this virtual has a regular node in the graph.
261 | Just node <- Color.lookupNode graph reg
262 = case Color.nodeColor node of
266 -- no node in the graph for this virtual, bad news.
268 = pprPanic "patchRegsFromGraph: register mapping failed."
269 ( text "There is no node in the graph for register " <> ppr reg
271 $$ Color.dotGraph (\x -> text "white") trivColorable graph)
273 in patchEraseLive patchF code
276 plusUFMs_C :: (elt -> elt -> elt) -> [UniqFM elt] -> UniqFM elt
278 = foldl (plusUFM_C f) emptyUFM maps