1 {-# OPTIONS -fno-warn-missing-signatures #-}
2 -- | Graph coloring register allocator.
4 -- TODO: The colors in graphviz graphs for x86_64 and ppc could be nicer.
7 module RegAlloc.Graph.Main (
14 import qualified GraphColor as Color
15 import RegAlloc.Liveness
16 import RegAlloc.Graph.Spill
17 import RegAlloc.Graph.SpillClean
18 import RegAlloc.Graph.SpillCost
19 import RegAlloc.Graph.Stats
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.
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.
50 -> UniqSM ( [NatCmmTop], [RegAllocStats] )
51 -- ^ code with registers allocated and stats for each stage of
54 regAlloc dflags regsFree slotsFree code
56 (code_final, debug_codeGraphs, _)
57 <- regAlloc_spin dflags 0 trivColorable regsFree slotsFree [] code
60 , reverse debug_codeGraphs )
62 regAlloc_spin dflags spinCount triv regsFree slotsFree debug_codeGraphs code
64 -- if any of these dump flags are turned on we want to hang on to
65 -- intermediate structures in the allocator - otherwise tell the
66 -- allocator to ditch them early so we don't end up creating space leaks.
68 [ dopt Opt_D_dump_asm_regalloc_stages dflags
69 , dopt Opt_D_dump_asm_stats dflags
70 , dopt Opt_D_dump_asm_conflicts dflags ]
72 -- check that we're not running off down the garden path.
73 when (spinCount > maxSpinCount)
74 $ pprPanic "regAlloc_spin: max build/spill cycle count exceeded."
75 ( text "It looks like the register allocator is stuck in an infinite loop."
76 $$ text "max cycles = " <> int maxSpinCount
77 $$ text "regsFree = " <> (hcat $ punctuate space $ map (docToSDoc . pprUserReg)
78 $ uniqSetToList $ unionManyUniqSets $ eltsUFM regsFree)
79 $$ text "slotsFree = " <> ppr (sizeUniqSet slotsFree))
81 -- build a conflict graph from the code.
82 graph <- {-# SCC "BuildGraph" #-} buildGraph code
85 -- We really do want the graph to be fully evaluated _before_ we start coloring.
86 -- If we don't do this now then when the call to Color.colorGraph forces bits of it,
87 -- the heap will be filled with half evaluated pieces of graph and zillions of apply thunks.
89 seqGraph graph `seq` return ()
92 -- build a map of the cost of spilling each instruction
93 -- this will only actually be computed if we have to spill something.
94 let spillCosts = foldl' plusSpillCostInfo zeroSpillCostInfo
95 $ map slurpSpillCostInfo code
97 -- the function to choose regs to leave uncolored
98 let spill = chooseSpill spillCosts
100 -- record startup state
103 then Just $ RegAllocStatsStart
106 , raSpillCosts = spillCosts }
109 -- try and color the graph
110 let (graph_colored, rsSpill, rmCoalesce)
111 = {-# SCC "ColorGraph" #-}
113 (dopt Opt_RegsIterative dflags)
115 regsFree triv spill graph
117 -- rewrite regs in the code that have been coalesced
118 let patchF reg = case lookupUFM rmCoalesce reg of
119 Just reg' -> patchF reg'
122 = map (patchEraseLive patchF) code
125 -- see if we've found a coloring
126 if isEmptyUniqSet rsSpill
128 -- if -fasm-lint is turned on then validate the graph
129 let graph_colored_lint =
130 if dopt Opt_DoAsmLinting dflags
131 then Color.validateGraph (text "")
132 True -- require all nodes to be colored
136 -- patch the registers using the info in the graph
137 let code_patched = map (patchRegsFromGraph graph_colored_lint) code_coalesced
139 -- clean out unneeded SPILL/RELOADs
140 let code_spillclean = map cleanSpills code_patched
142 -- strip off liveness information
143 let code_nat = map stripLive code_spillclean
145 -- rewrite SPILL/RELOAD pseudos into real instructions
146 let spillNatTop = mapGenBlockTop spillNatBlock
147 let code_final = map spillNatTop code_nat
149 -- record what happened in this stage for debugging
153 , raGraphColored = graph_colored_lint
154 , raCoalesced = rmCoalesce
155 , raPatched = code_patched
156 , raSpillClean = code_spillclean
157 , raFinal = code_final
158 , raSRMs = foldl' addSRM (0, 0, 0) $ map countSRMs code_spillclean }
162 if dump then [stat] ++ maybeToList stat1 ++ debug_codeGraphs
165 -- space leak avoidance
166 seqList statList `seq` return ()
170 , graph_colored_lint)
172 -- we couldn't find a coloring, time to spill something
174 -- if -fasm-lint is turned on then validate the graph
175 let graph_colored_lint =
176 if dopt Opt_DoAsmLinting dflags
177 then Color.validateGraph (text "")
178 False -- don't require nodes to be colored
182 -- spill the uncolored regs
183 (code_spilled, slotsFree', spillStats)
184 <- regSpill code_coalesced slotsFree rsSpill
186 -- recalculate liveness
187 let code_nat = map stripLive code_spilled
188 code_relive <- mapM regLiveness code_nat
190 -- record what happened in this stage for debugging
193 { raGraph = graph_colored_lint
194 , raCoalesced = rmCoalesce
195 , raSpillStats = spillStats
196 , raSpillCosts = spillCosts
197 , raSpilled = code_spilled }
201 then [stat] ++ maybeToList stat1 ++ debug_codeGraphs
204 -- space leak avoidance
205 seqList statList `seq` return ()
207 regAlloc_spin dflags (spinCount + 1) triv regsFree slotsFree'
213 -- | Build a graph from the liveness and coalesce information in this code.
217 -> UniqSM (Color.Graph Reg RegClass Reg)
221 -- Slurp out the conflicts and reg->reg moves from this code
222 let (conflictList, moveList) =
223 unzip $ map slurpConflicts code
225 -- Slurp out the spill/reload coalesces
226 let moveList2 = map slurpReloadCoalesce code
228 -- Add the reg-reg conflicts to the graph
229 let conflictBag = unionManyBags conflictList
230 let graph_conflict = foldrBag graphAddConflictSet Color.initGraph conflictBag
232 -- Add the coalescences edges to the graph.
233 let moveBag = unionBags (unionManyBags moveList2) (unionManyBags moveList)
234 let graph_coalesce = foldrBag graphAddCoalesce graph_conflict moveBag
236 return graph_coalesce
239 -- | Add some conflict edges to the graph.
240 -- Conflicts between virtual and real regs are recorded as exclusions.
244 -> Color.Graph Reg RegClass Reg
245 -> Color.Graph Reg RegClass Reg
247 graphAddConflictSet set graph
248 = let reals = filterUFM isRealReg set
249 virtuals = filterUFM (not . isRealReg) set
251 graph1 = Color.addConflicts virtuals regClass graph
252 graph2 = foldr (\(r1, r2) -> Color.addExclusion r1 regClass r2)
255 | a <- uniqSetToList virtuals
256 , b <- uniqSetToList reals]
261 -- | Add some coalesence edges to the graph
262 -- Coalesences between virtual and real regs are recorded as preferences.
266 -> Color.Graph Reg RegClass Reg
267 -> Color.Graph Reg RegClass Reg
269 graphAddCoalesce (r1, r2) graph
271 = Color.addPreference (regWithClass r2) r1 graph
274 = Color.addPreference (regWithClass r1) r2 graph
277 = Color.addCoalesce (regWithClass r1) (regWithClass r2) graph
279 where regWithClass r = (r, regClass r)
282 -- | Patch registers in code using the reg -> reg mapping in this graph.
284 :: Color.Graph Reg RegClass Reg
285 -> LiveCmmTop -> LiveCmmTop
287 patchRegsFromGraph graph code
289 -- a function to lookup the hardreg for a virtual reg from the graph.
291 -- leave real regs alone.
295 -- this virtual has a regular node in the graph.
296 | Just node <- Color.lookupNode graph reg
297 = case Color.nodeColor node of
301 -- no node in the graph for this virtual, bad news.
303 = pprPanic "patchRegsFromGraph: register mapping failed."
304 ( text "There is no node in the graph for register " <> ppr reg
306 $$ Color.dotGraph (\_ -> text "white") trivColorable graph)
308 in patchEraseLive patchF code
312 -- for when laziness just isn't what you wanted...
314 seqGraph :: Color.Graph Reg RegClass Reg -> ()
315 seqGraph graph = seqNodes (eltsUFM (Color.graphMap graph))
317 seqNodes :: [Color.Node Reg RegClass Reg] -> ()
321 (n : ns) -> seqNode n `seq` seqNodes ns
323 seqNode :: Color.Node Reg RegClass Reg -> ()
325 = seqReg (Color.nodeId node)
326 `seq` seqRegClass (Color.nodeClass node)
327 `seq` seqMaybeReg (Color.nodeColor node)
328 `seq` (seqRegList (uniqSetToList (Color.nodeConflicts node)))
329 `seq` (seqRegList (uniqSetToList (Color.nodeExclusions node)))
330 `seq` (seqRegList (Color.nodePreference node))
331 `seq` (seqRegList (uniqSetToList (Color.nodeCoalesce node)))
342 seqRegClass :: RegClass -> ()
349 seqMaybeReg :: Maybe Reg -> ()
355 seqRegList :: [Reg] -> ()
359 (r : rs) -> seqReg r `seq` seqRegList rs
365 (r : rs) -> r `seq` seqList rs