2 -- Carries interesting info for debugging / profiling of the
3 -- graph coloring register allocator.
17 #include "nativeGen/NCG.h"
19 import qualified GraphColor as Color
34 { raLiveCmm :: [LiveCmmTop] -- ^ code we tried to allocate regs for
35 , raGraph :: Color.Graph Reg RegClass Reg -- ^ the partially colored graph
36 , raSpillStats :: SpillStats -- ^ spiller stats
37 , raLifetimes :: UniqFM (Reg, Int) } -- ^ number of instrs each reg lives for
39 -- a successful coloring
40 | RegAllocStatsColored
41 { raLiveCmm :: [LiveCmmTop] -- ^ the code we allocated regs for
42 , raGraph :: Color.Graph Reg RegClass Reg -- ^ the colored graph
43 , raPatchedCmm :: [LiveCmmTop] -- ^ code with register allocation
44 , raLifetimes :: UniqFM (Reg, Int) } -- ^ number of instrs each reg lives for
47 instance Outputable RegAllocStats where
49 ppr (s@RegAllocStatsSpill{})
52 $$ text "-- Native code with liveness information."
56 $$ text "-- Register conflict graph."
57 $$ Color.dotGraph regDotColor trivColorable (raGraph s)
59 $$ text "-- Spill statistics."
60 $$ ppr (raSpillStats s)
63 ppr (s@RegAllocStatsColored{})
66 $$ text "-- Native code with liveness information."
70 $$ text "-- Register conflict graph."
71 $$ Color.dotGraph regDotColor trivColorable (raGraph s)
73 $$ text "-- Native code after register allocation."
74 $$ ppr (raPatchedCmm s)
77 -- | Dump a table of how many spill loads / stores were inserted for each vreg.
79 :: [RegAllocStats] -> SDoc
82 = let -- slurp out the stats from all the spiller stages
83 spillStats = [ s | s@RegAllocStatsSpill{} <- stats]
85 -- build a map of how many spill load/stores were inserted for each vreg
86 spillLS = foldl' (plusUFM_C accSpillLS) emptyUFM
87 $ map (spillLoadStore . raSpillStats) spillStats
89 -- print the count of load/spills as a tuple so we can read back from the file easilly
90 pprSpillLS (r, loads, stores)
91 = (parens $ (hcat $ punctuate (text ", ") [doubleQuotes (ppr r), int loads, int stores]))
94 in ( text "-- spills-added"
95 $$ text "-- (reg_name, spill_loads_added, spill_stores_added)."
96 $$ (vcat $ map pprSpillLS $ eltsUFM spillLS)
101 -- | Dump a table of how long vregs tend to live for.
103 :: [RegAllocStats] -> SDoc
105 pprStatsLifetimes stats
106 = let lifeMap = foldl' plusUFM emptyUFM $ map raLifetimes stats
107 lifeBins = binLifetimeCount lifeMap
109 in ( text "-- vreg-population-lifetimes"
110 $$ text "-- (instruction_count, number_of_vregs_that_lived_that_long)"
111 $$ (vcat $ map ppr $ eltsUFM lifeBins)
114 binLifetimeCount :: UniqFM (Reg, Int) -> UniqFM (Int, Int)
116 = let lifes = map (\l -> (l, (l, 1)))
121 (\(l1, c1) (l2, c2) -> (l1, c1 + c2))
126 -- | Dump a table of how many conflicts vregs tend to have.
128 :: [RegAllocStats] -> SDoc
130 pprStatsConflict stats
131 = let confMap = foldl' (plusUFM_C (\(c1, n1) (c2, n2) -> (c1, n1 + n2)))
133 $ map Color.slurpNodeConflictCount
136 in ( text "-- vreg-conflicts"
137 $$ text "-- (conflict_count, number_of_vregs_that_had_that_many_conflicts)"
138 $$ (vcat $ map ppr $ eltsUFM confMap)
142 -- | For every vreg, dump it's how many conflicts it has and its lifetime
143 -- good for making a scatter plot.
145 :: [RegAllocStats] -> Color.Graph Reg RegClass Reg -> SDoc
147 pprStatsLifeConflict stats graph
148 = let lifeMap = foldl' plusUFM emptyUFM $ map raLifetimes stats
149 scatter = map (\r -> let Just (_, lifetime) = lookupUFM lifeMap r
150 Just node = Color.lookupNode graph r
151 in parens $ hcat $ punctuate (text ", ")
152 [ doubleQuotes $ ppr $ Color.nodeId node
153 , ppr $ sizeUniqSet (Color.nodeConflicts node)
157 $ Color.graphMap graph
159 in ( text "-- vreg-conflict-lifetime"
160 $$ text "-- (vreg, vreg_conflicts, vreg_lifetime)"
166 -- Register colors for drawing conflict graphs
167 -- Keep this out of MachRegs.hs because it's specific to the graph coloring allocator.
170 -- reg colors for x86
172 regDotColor :: Reg -> SDoc
174 = let Just str = lookupUFM regColors reg
189 , (fake5, "#5500ff") ]
193 -- reg colors for x86_64
194 #if x86_64_TARGET_ARCH
195 regDotColor :: Reg -> SDoc
197 = let Just str = lookupUFM regColors reg
202 $ [ (rax, "#00ff00"), (eax, "#00ff00")
203 , (rbx, "#0000ff"), (ebx, "#0000ff")
204 , (rcx, "#00ffff"), (ecx, "#00ffff")
205 , (rdx, "#0080ff"), (edx, "#00ffff")
215 ++ zip (map RealReg [16..31]) (repeat "red")
219 -- reg colors for ppc
220 #if powerpc_TARGET_ARCH
221 regDotColor :: Reg -> SDoc
223 = case regClass reg of
224 RcInteger -> text "blue"
225 RcFloat -> text "red"
231 = let rs = padL 2 '0' (showHex r "")
232 gs = padL 2 '0' (showHex r "")
233 bs = padL 2 '0' (showHex r "")
236 = replicate (n - length s) c ++ s
237 in "#" ++ rs ++ gs ++ bs