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
110 -- strip off liveness information
111 let code_nat = map stripLive code_patched
113 -- rewrite SPILL/REALOAD pseudos into real instructions
114 let spillNatTop = mapGenBlockTop spillNatBlock
115 let code_final = map spillNatTop code_nat
117 -- record what happened in this stage for debugging
120 { raGraph = graph_colored
121 , raPatchedCmm = code_patched
122 , raFinalCmm = code_final }
125 , [stat] ++ maybeToList stat1 ++ debug_codeGraphs
129 -- spill the uncolored regs
130 (code_spilled, slotsFree', spillStats)
131 <- regSpill code slotsFree rsSpill
133 -- recalculate liveness
134 let code_nat = map stripLive code_spilled
135 code_relive <- mapM regLiveness code_nat
137 -- record what happened in this stage for debugging
140 { raGraph = graph_colored
141 , raSpillStats = spillStats
142 , raLifetimes = fmLife
143 , raSpilled = code_spilled }
146 regAlloc_spin (spinCount + 1) triv regsFree slotsFree'
147 ([stat] ++ maybeToList stat1 ++ debug_codeGraphs)
152 -- Simple maxconflicts isn't always good, because we
153 -- can naievely end up spilling vregs that only live for one or two instrs.
156 chooseSpill_maxConflicts
157 :: Color.Graph Reg RegClass Reg
160 chooseSpill_maxConflicts graph
161 = let node = maximumBy
163 (sizeUniqSet $ Color.nodeConflicts n1)
164 (sizeUniqSet $ Color.nodeConflicts n2))
165 $ eltsUFM $ Color.graphMap graph
173 -> Color.Graph Reg RegClass Reg
176 chooseSpill_maxLife life graph
177 = let node = maximumBy (\n1 n2 -> compare (getLife n1) (getLife n2))
178 $ eltsUFM $ Color.graphMap graph
180 -- Orphan vregs die in the same instruction they are born in.
181 -- They will be in the graph, but not in the liveness map.
182 -- Their liveness is 0.
184 = case lookupUFM life (Color.nodeId n) of
191 -- | Build a graph from the liveness and coalesce information in this code.
195 -> UniqSM (Color.Graph Reg RegClass Reg)
199 -- Add the reg-reg conflicts to the graph
200 let conflictSets = unionManyBags (map slurpConflicts code)
201 let graph_conflict = foldrBag graphAddConflictSet Color.initGraph conflictSets
204 -- Add the coalescences edges to the graph.
205 let coalesce = unionManyBags (map slurpJoinMovs code)
206 let graph_coalesce = foldrBag graphAddCoalesce graph_conflict coalesce
208 return $ graph_coalesce
211 -- | Add some conflict edges to the graph.
212 -- Conflicts between virtual and real regs are recorded as exclusions.
216 -> Color.Graph Reg RegClass Reg
217 -> Color.Graph Reg RegClass Reg
219 graphAddConflictSet set graph
220 = let reals = filterUFM isRealReg set
221 virtuals = filterUFM (not . isRealReg) set
223 graph1 = Color.addConflicts virtuals regClass graph
224 graph2 = foldr (\(r1, r2) -> Color.addExclusion r1 regClass r2)
227 | a <- uniqSetToList virtuals
228 , b <- uniqSetToList reals]
233 -- | Add some coalesence edges to the graph
234 -- Coalesences between virtual and real regs are recorded as preferences.
238 -> Color.Graph Reg RegClass Reg
239 -> Color.Graph Reg RegClass Reg
241 graphAddCoalesce (r1, r2) graph
242 | RealReg regno <- r1
243 = Color.addPreference (regWithClass r2) r1 graph
245 | RealReg regno <- r2
246 = Color.addPreference (regWithClass r1) r2 graph
249 = Color.addCoalesce (regWithClass r1) (regWithClass r2) graph
251 where regWithClass r = (r, regClass r)
254 -- | Patch registers in code using the reg -> reg mapping in this graph.
256 :: Color.Graph Reg RegClass Reg
257 -> LiveCmmTop -> LiveCmmTop
259 patchRegsFromGraph graph code
261 -- a function to lookup the hardreg for a virtual reg from the graph.
263 -- leave real regs alone.
267 -- this virtual has a regular node in the graph.
268 | Just node <- Color.lookupNode graph reg
269 = case Color.nodeColor node of
273 -- no node in the graph for this virtual, bad news.
275 = pprPanic "patchRegsFromGraph: register mapping failed."
276 ( text "There is no node in the graph for register " <> ppr reg
278 $$ Color.dotGraph (\x -> text "white") trivColorable graph)
280 in patchEraseLive patchF code
283 plusUFMs_C :: (elt -> elt -> elt) -> [UniqFM elt] -> UniqFM elt
285 = foldl (plusUFM_C f) emptyUFM maps