PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/compiler/nativeGen/RegAlloc/Linear/Stats.hs

https://github.com/jberthold/ghc
Haskell | 88 lines | 63 code | 20 blank | 5 comment | 0 complexity | 751dad3bd7e53d7b42632d5e0f4d27a0 MD5 | raw file
  1. module RegAlloc.Linear.Stats (
  2. binSpillReasons,
  3. countRegRegMovesNat,
  4. pprStats
  5. )
  6. where
  7. import GhcPrelude
  8. import RegAlloc.Linear.Base
  9. import RegAlloc.Liveness
  10. import Instruction
  11. import UniqFM
  12. import Outputable
  13. import Data.List
  14. import State
  15. -- | Build a map of how many times each reg was alloced, clobbered, loaded etc.
  16. binSpillReasons
  17. :: [SpillReason] -> UniqFM [Int]
  18. binSpillReasons reasons
  19. = addListToUFM_C
  20. (zipWith (+))
  21. emptyUFM
  22. (map (\reason -> case reason of
  23. SpillAlloc r -> (r, [1, 0, 0, 0, 0])
  24. SpillClobber r -> (r, [0, 1, 0, 0, 0])
  25. SpillLoad r -> (r, [0, 0, 1, 0, 0])
  26. SpillJoinRR r -> (r, [0, 0, 0, 1, 0])
  27. SpillJoinRM r -> (r, [0, 0, 0, 0, 1])) reasons)
  28. -- | Count reg-reg moves remaining in this code.
  29. countRegRegMovesNat
  30. :: Instruction instr
  31. => NatCmmDecl statics instr -> Int
  32. countRegRegMovesNat cmm
  33. = execState (mapGenBlockTopM countBlock cmm) 0
  34. where
  35. countBlock b@(BasicBlock _ instrs)
  36. = do mapM_ countInstr instrs
  37. return b
  38. countInstr instr
  39. | Just _ <- takeRegRegMoveInstr instr
  40. = do modify (+ 1)
  41. return instr
  42. | otherwise
  43. = return instr
  44. -- | Pretty print some RegAllocStats
  45. pprStats
  46. :: Instruction instr
  47. => [NatCmmDecl statics instr] -> [RegAllocStats] -> SDoc
  48. pprStats code statss
  49. = let -- sum up all the instrs inserted by the spiller
  50. spills = foldl' (plusUFM_C (zipWith (+)))
  51. emptyUFM
  52. $ map ra_spillInstrs statss
  53. spillTotals = foldl' (zipWith (+))
  54. [0, 0, 0, 0, 0]
  55. $ nonDetEltsUFM spills
  56. -- See Note [Unique Determinism and code generation]
  57. -- count how many reg-reg-moves remain in the code
  58. moves = sum $ map countRegRegMovesNat code
  59. pprSpill (reg, spills)
  60. = parens $ (hcat $ punctuate (text ", ") (doubleQuotes (ppr reg) : map ppr spills))
  61. in ( text "-- spills-added-total"
  62. $$ text "-- (allocs, clobbers, loads, joinRR, joinRM, reg_reg_moves_remaining)"
  63. $$ (parens $ (hcat $ punctuate (text ", ") (map ppr spillTotals ++ [ppr moves])))
  64. $$ text ""
  65. $$ text "-- spills-added"
  66. $$ text "-- (reg_name, allocs, clobbers, loads, joinRR, joinRM)"
  67. $$ (pprUFMWithKeys spills (vcat . map pprSpill))
  68. $$ text "")