never executed always true always false
1 module GHC.CmmToAsm.Reg.Linear.Stats (
2 binSpillReasons,
3 countRegRegMovesNat,
4 pprStats
5 )
6
7 where
8
9 import GHC.Prelude
10
11 import GHC.CmmToAsm.Reg.Linear.Base
12 import GHC.CmmToAsm.Reg.Liveness
13 import GHC.CmmToAsm.Instr
14 import GHC.Types.Unique (Unique)
15 import GHC.CmmToAsm.Types
16
17 import GHC.Types.Unique.FM
18
19 import GHC.Utils.Outputable
20 import GHC.Utils.Monad.State.Strict
21
22 -- | Build a map of how many times each reg was alloced, clobbered, loaded etc.
23 binSpillReasons
24 :: [SpillReason] -> UniqFM Unique [Int]
25 -- See Note [UniqFM and the register allocator]
26 binSpillReasons reasons
27 = addListToUFM_C
28 (zipWith (+))
29 emptyUFM
30 (map (\reason -> case reason of
31 SpillAlloc r -> (r, [1, 0, 0, 0, 0])
32 SpillClobber r -> (r, [0, 1, 0, 0, 0])
33 SpillLoad r -> (r, [0, 0, 1, 0, 0])
34 SpillJoinRR r -> (r, [0, 0, 0, 1, 0])
35 SpillJoinRM r -> (r, [0, 0, 0, 0, 1])) reasons)
36
37
38 -- | Count reg-reg moves remaining in this code.
39 countRegRegMovesNat
40 :: Instruction instr
41 => NatCmmDecl statics instr -> Int
42
43 countRegRegMovesNat cmm
44 = execState (mapGenBlockTopM countBlock cmm) 0
45 where
46 countBlock b@(BasicBlock _ instrs)
47 = do mapM_ countInstr instrs
48 return b
49
50 countInstr instr
51 | Just _ <- takeRegRegMoveInstr instr
52 = do modify (+ 1)
53 return instr
54
55 | otherwise
56 = return instr
57
58
59 -- | Pretty print some RegAllocStats
60 pprStats
61 :: Instruction instr
62 => [NatCmmDecl statics instr] -> [RegAllocStats] -> SDoc
63
64 pprStats code statss
65 = let -- sum up all the instrs inserted by the spiller
66 -- See Note [UniqFM and the register allocator]
67 spills :: UniqFM Unique [Int]
68 spills = foldl' (plusUFM_C (zipWith (+)))
69 emptyUFM
70 $ map ra_spillInstrs statss
71
72 spillTotals = foldl' (zipWith (+))
73 [0, 0, 0, 0, 0]
74 $ nonDetEltsUFM spills
75 -- See Note [Unique Determinism and code generation]
76
77 -- count how many reg-reg-moves remain in the code
78 moves = sum $ map countRegRegMovesNat code
79
80 pprSpill (reg, spills)
81 = parens $ (hcat $ punctuate (text ", ") (doubleQuotes (ppr reg) : map ppr spills))
82
83 in ( text "-- spills-added-total"
84 $$ text "-- (allocs, clobbers, loads, joinRR, joinRM, reg_reg_moves_remaining)"
85 $$ (parens $ (hcat $ punctuate (text ", ") (map ppr spillTotals ++ [ppr moves])))
86 $$ text ""
87 $$ text "-- spills-added"
88 $$ text "-- (reg_name, allocs, clobbers, loads, joinRR, joinRM)"
89 $$ (pprUFMWithKeys spills (vcat . map pprSpill))
90 $$ text "")
91