PageRenderTime 62ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/ghc-7.0.4/compiler/simplCore/CoreMonad.lhs

http://picorec.googlecode.com/
Haskell | 1118 lines | 779 code | 202 blank | 137 comment | 13 complexity | 6d04987d0852200392116806deb86802 MD5 | raw file
Possible License(s): BSD-3-Clause, BSD-2-Clause
  1. %
  2. % (c) The AQUA Project, Glasgow University, 1993-1998
  3. %
  4. \section[CoreMonad]{The core pipeline monad}
  5. \begin{code}
  6. {-# LANGUAGE UndecidableInstances #-}
  7. module CoreMonad (
  8. -- * Configuration of the core-to-core passes
  9. CoreToDo(..),
  10. SimplifierMode(..),
  11. FloatOutSwitches(..),
  12. getCoreToDo, dumpSimplPhase,
  13. -- * Counting
  14. SimplCount, doSimplTick, doFreeSimplTick, simplCountN,
  15. pprSimplCount, plusSimplCount, zeroSimplCount, isZeroSimplCount, Tick(..),
  16. -- * The monad
  17. CoreM, runCoreM,
  18. -- ** Reading from the monad
  19. getHscEnv, getRuleBase, getModule,
  20. getDynFlags, getOrigNameCache,
  21. -- ** Writing to the monad
  22. addSimplCount,
  23. -- ** Lifting into the monad
  24. liftIO, liftIOWithCount,
  25. liftIO1, liftIO2, liftIO3, liftIO4,
  26. -- ** Dealing with annotations
  27. getAnnotations, getFirstAnnotations,
  28. -- ** Debug output
  29. showPass, endPass, endIteration, dumpIfSet,
  30. -- ** Screen output
  31. putMsg, putMsgS, errorMsg, errorMsgS,
  32. fatalErrorMsg, fatalErrorMsgS,
  33. debugTraceMsg, debugTraceMsgS,
  34. dumpIfSet_dyn,
  35. #ifdef GHCI
  36. -- * Getting 'Name's
  37. thNameToGhcName
  38. #endif
  39. ) where
  40. #ifdef GHCI
  41. import Name( Name )
  42. #endif
  43. import CoreSyn
  44. import PprCore
  45. import CoreUtils
  46. import CoreLint ( lintCoreBindings )
  47. import PrelNames ( iNTERACTIVE )
  48. import HscTypes
  49. import Module ( PackageId, Module )
  50. import DynFlags
  51. import StaticFlags
  52. import Rules ( RuleBase )
  53. import BasicTypes ( CompilerPhase(..) )
  54. import Annotations
  55. import Id ( Id )
  56. import IOEnv hiding ( liftIO, failM, failWithM )
  57. import qualified IOEnv ( liftIO )
  58. import TcEnv ( tcLookupGlobal )
  59. import TcRnMonad ( TcM, initTc )
  60. import Outputable
  61. import FastString
  62. import qualified ErrUtils as Err
  63. import Bag
  64. import Maybes
  65. import UniqSupply
  66. import UniqFM ( UniqFM, mapUFM, filterUFM )
  67. import Util ( split )
  68. import Data.List ( intersperse )
  69. import Data.Dynamic
  70. import Data.IORef
  71. import Data.Map (Map)
  72. import qualified Data.Map as Map
  73. import Data.Word
  74. import Control.Monad
  75. import Prelude hiding ( read )
  76. #ifdef GHCI
  77. import {-# SOURCE #-} TcSplice ( lookupThName_maybe )
  78. import qualified Language.Haskell.TH as TH
  79. #endif
  80. \end{code}
  81. %************************************************************************
  82. %* *
  83. Debug output
  84. %* *
  85. %************************************************************************
  86. These functions are not CoreM monad stuff, but they probably ought to
  87. be, and it makes a conveneint place. place for them. They print out
  88. stuff before and after core passes, and do Core Lint when necessary.
  89. \begin{code}
  90. showPass :: DynFlags -> CoreToDo -> IO ()
  91. showPass dflags pass = Err.showPass dflags (showSDoc (ppr pass))
  92. endPass :: DynFlags -> CoreToDo -> [CoreBind] -> [CoreRule] -> IO ()
  93. endPass dflags pass = dumpAndLint dflags True pass empty (coreDumpFlag pass)
  94. -- Same as endPass but doesn't dump Core even with -dverbose-core2core
  95. endIteration :: DynFlags -> CoreToDo -> Int -> [CoreBind] -> [CoreRule] -> IO ()
  96. endIteration dflags pass n
  97. = dumpAndLint dflags False pass (ptext (sLit "iteration=") <> int n)
  98. (Just Opt_D_dump_simpl_iterations)
  99. dumpIfSet :: Bool -> CoreToDo -> SDoc -> SDoc -> IO ()
  100. dumpIfSet dump_me pass extra_info doc
  101. = Err.dumpIfSet dump_me (showSDoc (ppr pass <+> extra_info)) doc
  102. dumpAndLint :: DynFlags -> Bool -> CoreToDo -> SDoc -> Maybe DynFlag
  103. -> [CoreBind] -> [CoreRule] -> IO ()
  104. -- The "show_all" parameter says to print dump if -dverbose-core2core is on
  105. dumpAndLint dflags show_all pass extra_info mb_dump_flag binds rules
  106. = do { -- Report result size if required
  107. -- This has the side effect of forcing the intermediate to be evaluated
  108. ; Err.debugTraceMsg dflags 2 $
  109. (text " Result size =" <+> int (coreBindsSize binds))
  110. -- Report verbosely, if required
  111. ; let pass_name = showSDoc (ppr pass <+> extra_info)
  112. dump_doc = pprCoreBindings binds
  113. $$ ppUnless (null rules) pp_rules
  114. ; case mb_dump_flag of
  115. Nothing -> return ()
  116. Just dump_flag -> Err.dumpIfSet_dyn_or dflags dump_flags pass_name dump_doc
  117. where
  118. dump_flags | show_all = [dump_flag, Opt_D_verbose_core2core]
  119. | otherwise = [dump_flag]
  120. -- Type check
  121. ; when (dopt Opt_DoCoreLinting dflags) $
  122. do { let (warns, errs) = lintCoreBindings binds
  123. ; Err.showPass dflags ("Core Linted result of " ++ pass_name)
  124. ; displayLintResults dflags pass warns errs binds } }
  125. where
  126. pp_rules = vcat [ blankLine
  127. , ptext (sLit "------ Local rules for imported ids --------")
  128. , pprRules rules ]
  129. displayLintResults :: DynFlags -> CoreToDo
  130. -> Bag Err.Message -> Bag Err.Message -> [CoreBind]
  131. -> IO ()
  132. displayLintResults dflags pass warns errs binds
  133. | not (isEmptyBag errs)
  134. = do { printDump (vcat [ banner "errors", Err.pprMessageBag errs
  135. , ptext (sLit "*** Offending Program ***")
  136. , pprCoreBindings binds
  137. , ptext (sLit "*** End of Offense ***") ])
  138. ; Err.ghcExit dflags 1 }
  139. | not (isEmptyBag warns)
  140. , not (case pass of { CoreDesugar -> True; _ -> False })
  141. -- Suppress warnings after desugaring pass because some
  142. -- are legitimate. Notably, the desugarer generates instance
  143. -- methods with INLINE pragmas that form a mutually recursive
  144. -- group. Only afer a round of simplification are they unravelled.
  145. , not opt_NoDebugOutput
  146. , showLintWarnings pass
  147. = printDump (banner "warnings" $$ Err.pprMessageBag warns)
  148. | otherwise = return ()
  149. where
  150. banner string = ptext (sLit "*** Core Lint") <+> text string
  151. <+> ptext (sLit ": in result of") <+> ppr pass
  152. <+> ptext (sLit "***")
  153. showLintWarnings :: CoreToDo -> Bool
  154. -- Disable Lint warnings on the first simplifier pass, because
  155. -- there may be some INLINE knots still tied, which is tiresomely noisy
  156. showLintWarnings (CoreDoSimplify _ (SimplMode { sm_phase = InitialPhase })) = False
  157. showLintWarnings _ = True
  158. \end{code}
  159. %************************************************************************
  160. %* *
  161. The CoreToDo type and related types
  162. Abstraction of core-to-core passes to run.
  163. %* *
  164. %************************************************************************
  165. \begin{code}
  166. data CoreToDo -- These are diff core-to-core passes,
  167. -- which may be invoked in any order,
  168. -- as many times as you like.
  169. = CoreDoSimplify -- The core-to-core simplifier.
  170. Int -- Max iterations
  171. SimplifierMode
  172. | CoreDoFloatInwards
  173. | CoreDoFloatOutwards FloatOutSwitches
  174. | CoreLiberateCase
  175. | CoreDoPrintCore
  176. | CoreDoStaticArgs
  177. | CoreDoStrictness
  178. | CoreDoWorkerWrapper
  179. | CoreDoSpecialising
  180. | CoreDoSpecConstr
  181. | CoreDoGlomBinds
  182. | CoreCSE
  183. | CoreDoRuleCheck CompilerPhase String -- Check for non-application of rules
  184. -- matching this string
  185. | CoreDoVectorisation PackageId
  186. | CoreDoNothing -- Useful when building up
  187. | CoreDoPasses [CoreToDo] -- lists of these things
  188. | CoreDesugar -- Not strictly a core-to-core pass, but produces
  189. -- Core output, and hence useful to pass to endPass
  190. | CoreTidy
  191. | CorePrep
  192. coreDumpFlag :: CoreToDo -> Maybe DynFlag
  193. coreDumpFlag (CoreDoSimplify {}) = Just Opt_D_dump_simpl_phases
  194. coreDumpFlag CoreDoFloatInwards = Just Opt_D_verbose_core2core
  195. coreDumpFlag (CoreDoFloatOutwards {}) = Just Opt_D_verbose_core2core
  196. coreDumpFlag CoreLiberateCase = Just Opt_D_verbose_core2core
  197. coreDumpFlag CoreDoStaticArgs = Just Opt_D_verbose_core2core
  198. coreDumpFlag CoreDoStrictness = Just Opt_D_dump_stranal
  199. coreDumpFlag CoreDoWorkerWrapper = Just Opt_D_dump_worker_wrapper
  200. coreDumpFlag CoreDoSpecialising = Just Opt_D_dump_spec
  201. coreDumpFlag CoreDoSpecConstr = Just Opt_D_dump_spec
  202. coreDumpFlag CoreCSE = Just Opt_D_dump_cse
  203. coreDumpFlag (CoreDoVectorisation {}) = Just Opt_D_dump_vect
  204. coreDumpFlag CoreDesugar = Just Opt_D_dump_ds
  205. coreDumpFlag CoreTidy = Just Opt_D_dump_simpl
  206. coreDumpFlag CorePrep = Just Opt_D_dump_prep
  207. coreDumpFlag CoreDoPrintCore = Nothing
  208. coreDumpFlag (CoreDoRuleCheck {}) = Nothing
  209. coreDumpFlag CoreDoNothing = Nothing
  210. coreDumpFlag CoreDoGlomBinds = Nothing
  211. coreDumpFlag (CoreDoPasses {}) = Nothing
  212. instance Outputable CoreToDo where
  213. ppr (CoreDoSimplify n md) = ptext (sLit "Simplifier")
  214. <+> ppr md
  215. <+> ptext (sLit "max-iterations=") <> int n
  216. ppr CoreDoFloatInwards = ptext (sLit "Float inwards")
  217. ppr (CoreDoFloatOutwards f) = ptext (sLit "Float out") <> parens (ppr f)
  218. ppr CoreLiberateCase = ptext (sLit "Liberate case")
  219. ppr CoreDoStaticArgs = ptext (sLit "Static argument")
  220. ppr CoreDoStrictness = ptext (sLit "Demand analysis")
  221. ppr CoreDoWorkerWrapper = ptext (sLit "Worker Wrapper binds")
  222. ppr CoreDoSpecialising = ptext (sLit "Specialise")
  223. ppr CoreDoSpecConstr = ptext (sLit "SpecConstr")
  224. ppr CoreCSE = ptext (sLit "Common sub-expression")
  225. ppr (CoreDoVectorisation {}) = ptext (sLit "Vectorisation")
  226. ppr CoreDesugar = ptext (sLit "Desugar")
  227. ppr CoreTidy = ptext (sLit "Tidy Core")
  228. ppr CorePrep = ptext (sLit "CorePrep")
  229. ppr CoreDoPrintCore = ptext (sLit "Print core")
  230. ppr (CoreDoRuleCheck {}) = ptext (sLit "Rule check")
  231. ppr CoreDoGlomBinds = ptext (sLit "Glom binds")
  232. ppr CoreDoNothing = ptext (sLit "CoreDoNothing")
  233. ppr (CoreDoPasses {}) = ptext (sLit "CoreDoPasses")
  234. \end{code}
  235. \begin{code}
  236. data SimplifierMode -- See comments in SimplMonad
  237. = SimplMode
  238. { sm_names :: [String] -- Name(s) of the phase
  239. , sm_phase :: CompilerPhase
  240. , sm_rules :: Bool -- Whether RULES are enabled
  241. , sm_inline :: Bool -- Whether inlining is enabled
  242. , sm_case_case :: Bool -- Whether case-of-case is enabled
  243. , sm_eta_expand :: Bool -- Whether eta-expansion is enabled
  244. }
  245. instance Outputable SimplifierMode where
  246. ppr (SimplMode { sm_phase = p, sm_names = ss
  247. , sm_rules = r, sm_inline = i
  248. , sm_eta_expand = eta, sm_case_case = cc })
  249. = ptext (sLit "SimplMode") <+> braces (
  250. sep [ ptext (sLit "Phase =") <+> ppr p <+>
  251. brackets (text (concat $ intersperse "," ss)) <> comma
  252. , pp_flag i (sLit "inline") <> comma
  253. , pp_flag r (sLit "rules") <> comma
  254. , pp_flag eta (sLit "eta-expand") <> comma
  255. , pp_flag cc (sLit "case-of-case") ])
  256. where
  257. pp_flag f s = ppUnless f (ptext (sLit "no")) <+> ptext s
  258. \end{code}
  259. \begin{code}
  260. data FloatOutSwitches = FloatOutSwitches {
  261. floatOutLambdas :: Maybe Int, -- ^ Just n <=> float lambdas to top level, if
  262. -- doing so will abstract over n or fewer
  263. -- value variables
  264. -- Nothing <=> float all lambdas to top level,
  265. -- regardless of how many free variables
  266. -- Just 0 is the vanilla case: float a lambda
  267. -- iff it has no free vars
  268. floatOutConstants :: Bool, -- ^ True <=> float constants to top level,
  269. -- even if they do not escape a lambda
  270. floatOutPartialApplications :: Bool -- ^ True <=> float out partial applications
  271. -- based on arity information.
  272. }
  273. instance Outputable FloatOutSwitches where
  274. ppr = pprFloatOutSwitches
  275. pprFloatOutSwitches :: FloatOutSwitches -> SDoc
  276. pprFloatOutSwitches sw
  277. = ptext (sLit "FOS") <+> (braces $
  278. sep $ punctuate comma $
  279. [ ptext (sLit "Lam =") <+> ppr (floatOutLambdas sw)
  280. , ptext (sLit "Consts =") <+> ppr (floatOutConstants sw)
  281. , ptext (sLit "PAPs =") <+> ppr (floatOutPartialApplications sw) ])
  282. \end{code}
  283. %************************************************************************
  284. %* *
  285. Generating the main optimisation pipeline
  286. %* *
  287. %************************************************************************
  288. \begin{code}
  289. getCoreToDo :: DynFlags -> [CoreToDo]
  290. getCoreToDo dflags
  291. = core_todo
  292. where
  293. opt_level = optLevel dflags
  294. phases = simplPhases dflags
  295. max_iter = maxSimplIterations dflags
  296. rule_check = ruleCheck dflags
  297. strictness = dopt Opt_Strictness dflags
  298. full_laziness = dopt Opt_FullLaziness dflags
  299. do_specialise = dopt Opt_Specialise dflags
  300. do_float_in = dopt Opt_FloatIn dflags
  301. cse = dopt Opt_CSE dflags
  302. spec_constr = dopt Opt_SpecConstr dflags
  303. liberate_case = dopt Opt_LiberateCase dflags
  304. static_args = dopt Opt_StaticArgumentTransformation dflags
  305. rules_on = dopt Opt_EnableRewriteRules dflags
  306. eta_expand_on = dopt Opt_DoLambdaEtaExpansion dflags
  307. maybe_rule_check phase = runMaybe rule_check (CoreDoRuleCheck phase)
  308. maybe_strictness_before phase
  309. = runWhen (phase `elem` strictnessBefore dflags) CoreDoStrictness
  310. base_mode = SimplMode { sm_phase = panic "base_mode"
  311. , sm_names = []
  312. , sm_rules = rules_on
  313. , sm_eta_expand = eta_expand_on
  314. , sm_inline = True
  315. , sm_case_case = True }
  316. simpl_phase phase names iter
  317. = CoreDoPasses
  318. [ maybe_strictness_before phase
  319. , CoreDoSimplify iter
  320. (base_mode { sm_phase = Phase phase
  321. , sm_names = names })
  322. , maybe_rule_check (Phase phase)
  323. ]
  324. vectorisation
  325. = runWhen (dopt Opt_Vectorise dflags)
  326. $ CoreDoPasses [ simpl_gently, CoreDoVectorisation (dphPackage dflags) ]
  327. -- By default, we have 2 phases before phase 0.
  328. -- Want to run with inline phase 2 after the specialiser to give
  329. -- maximum chance for fusion to work before we inline build/augment
  330. -- in phase 1. This made a difference in 'ansi' where an
  331. -- overloaded function wasn't inlined till too late.
  332. -- Need phase 1 so that build/augment get
  333. -- inlined. I found that spectral/hartel/genfft lost some useful
  334. -- strictness in the function sumcode' if augment is not inlined
  335. -- before strictness analysis runs
  336. simpl_phases = CoreDoPasses [ simpl_phase phase ["main"] max_iter
  337. | phase <- [phases, phases-1 .. 1] ]
  338. -- initial simplify: mk specialiser happy: minimum effort please
  339. simpl_gently = CoreDoSimplify max_iter
  340. (base_mode { sm_phase = InitialPhase
  341. , sm_names = ["Gentle"]
  342. , sm_rules = True -- Note [RULEs enabled in SimplGently]
  343. , sm_inline = False
  344. , sm_case_case = False })
  345. -- Don't do case-of-case transformations.
  346. -- This makes full laziness work better
  347. core_todo =
  348. if opt_level == 0 then
  349. [vectorisation,
  350. simpl_phase 0 ["final"] max_iter]
  351. else {- opt_level >= 1 -} [
  352. -- We want to do the static argument transform before full laziness as it
  353. -- may expose extra opportunities to float things outwards. However, to fix
  354. -- up the output of the transformation we need at do at least one simplify
  355. -- after this before anything else
  356. runWhen static_args (CoreDoPasses [ simpl_gently, CoreDoStaticArgs ]),
  357. -- We run vectorisation here for now, but we might also try to run
  358. -- it later
  359. vectorisation,
  360. -- initial simplify: mk specialiser happy: minimum effort please
  361. simpl_gently,
  362. -- Specialisation is best done before full laziness
  363. -- so that overloaded functions have all their dictionary lambdas manifest
  364. runWhen do_specialise CoreDoSpecialising,
  365. runWhen full_laziness $
  366. CoreDoFloatOutwards FloatOutSwitches {
  367. floatOutLambdas = Just 0,
  368. floatOutConstants = True,
  369. floatOutPartialApplications = False },
  370. -- Was: gentleFloatOutSwitches
  371. --
  372. -- I have no idea why, but not floating constants to
  373. -- top level is very bad in some cases.
  374. --
  375. -- Notably: p_ident in spectral/rewrite
  376. -- Changing from "gentle" to "constantsOnly"
  377. -- improved rewrite's allocation by 19%, and
  378. -- made 0.0% difference to any other nofib
  379. -- benchmark
  380. --
  381. -- Not doing floatOutPartialApplications yet, we'll do
  382. -- that later on when we've had a chance to get more
  383. -- accurate arity information. In fact it makes no
  384. -- difference at all to performance if we do it here,
  385. -- but maybe we save some unnecessary to-and-fro in
  386. -- the simplifier.
  387. runWhen do_float_in CoreDoFloatInwards,
  388. simpl_phases,
  389. -- Phase 0: allow all Ids to be inlined now
  390. -- This gets foldr inlined before strictness analysis
  391. -- At least 3 iterations because otherwise we land up with
  392. -- huge dead expressions because of an infelicity in the
  393. -- simpifier.
  394. -- let k = BIG in foldr k z xs
  395. -- ==> let k = BIG in letrec go = \xs -> ...(k x).... in go xs
  396. -- ==> let k = BIG in letrec go = \xs -> ...(BIG x).... in go xs
  397. -- Don't stop now!
  398. simpl_phase 0 ["main"] (max max_iter 3),
  399. runWhen strictness (CoreDoPasses [
  400. CoreDoStrictness,
  401. CoreDoWorkerWrapper,
  402. CoreDoGlomBinds,
  403. simpl_phase 0 ["post-worker-wrapper"] max_iter
  404. ]),
  405. runWhen full_laziness $
  406. CoreDoFloatOutwards FloatOutSwitches {
  407. floatOutLambdas = floatLamArgs dflags,
  408. floatOutConstants = True,
  409. floatOutPartialApplications = True },
  410. -- nofib/spectral/hartel/wang doubles in speed if you
  411. -- do full laziness late in the day. It only happens
  412. -- after fusion and other stuff, so the early pass doesn't
  413. -- catch it. For the record, the redex is
  414. -- f_el22 (f_el21 r_midblock)
  415. runWhen cse CoreCSE,
  416. -- We want CSE to follow the final full-laziness pass, because it may
  417. -- succeed in commoning up things floated out by full laziness.
  418. -- CSE used to rely on the no-shadowing invariant, but it doesn't any more
  419. runWhen do_float_in CoreDoFloatInwards,
  420. maybe_rule_check (Phase 0),
  421. -- Case-liberation for -O2. This should be after
  422. -- strictness analysis and the simplification which follows it.
  423. runWhen liberate_case (CoreDoPasses [
  424. CoreLiberateCase,
  425. simpl_phase 0 ["post-liberate-case"] max_iter
  426. ]), -- Run the simplifier after LiberateCase to vastly
  427. -- reduce the possiblility of shadowing
  428. -- Reason: see Note [Shadowing] in SpecConstr.lhs
  429. runWhen spec_constr CoreDoSpecConstr,
  430. maybe_rule_check (Phase 0),
  431. -- Final clean-up simplification:
  432. simpl_phase 0 ["final"] max_iter
  433. ]
  434. -- The core-to-core pass ordering is derived from the DynFlags:
  435. runWhen :: Bool -> CoreToDo -> CoreToDo
  436. runWhen True do_this = do_this
  437. runWhen False _ = CoreDoNothing
  438. runMaybe :: Maybe a -> (a -> CoreToDo) -> CoreToDo
  439. runMaybe (Just x) f = f x
  440. runMaybe Nothing _ = CoreDoNothing
  441. dumpSimplPhase :: DynFlags -> SimplifierMode -> Bool
  442. dumpSimplPhase dflags mode
  443. | Just spec_string <- shouldDumpSimplPhase dflags
  444. = match_spec spec_string
  445. | otherwise
  446. = dopt Opt_D_verbose_core2core dflags
  447. where
  448. match_spec :: String -> Bool
  449. match_spec spec_string
  450. = or $ map (and . map match . split ':')
  451. $ split ',' spec_string
  452. match :: String -> Bool
  453. match "" = True
  454. match s = case reads s of
  455. [(n,"")] -> phase_num n
  456. _ -> phase_name s
  457. phase_num :: Int -> Bool
  458. phase_num n = case sm_phase mode of
  459. Phase k -> n == k
  460. _ -> False
  461. phase_name :: String -> Bool
  462. phase_name s = s `elem` sm_names mode
  463. \end{code}
  464. Note [RULEs enabled in SimplGently]
  465. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  466. RULES are enabled when doing "gentle" simplification. Two reasons:
  467. * We really want the class-op cancellation to happen:
  468. op (df d1 d2) --> $cop3 d1 d2
  469. because this breaks the mutual recursion between 'op' and 'df'
  470. * I wanted the RULE
  471. lift String ===> ...
  472. to work in Template Haskell when simplifying
  473. splices, so we get simpler code for literal strings
  474. But watch out: list fusion can prevent floating. So use phase control
  475. to switch off those rules until after floating.
  476. Currently (Oct10) I think that sm_rules is always True, so we
  477. could remove it.
  478. %************************************************************************
  479. %* *
  480. Counting and logging
  481. %* *
  482. %************************************************************************
  483. \begin{code}
  484. verboseSimplStats :: Bool
  485. verboseSimplStats = opt_PprStyle_Debug -- For now, anyway
  486. zeroSimplCount :: DynFlags -> SimplCount
  487. isZeroSimplCount :: SimplCount -> Bool
  488. pprSimplCount :: SimplCount -> SDoc
  489. doSimplTick, doFreeSimplTick :: Tick -> SimplCount -> SimplCount
  490. plusSimplCount :: SimplCount -> SimplCount -> SimplCount
  491. \end{code}
  492. \begin{code}
  493. data SimplCount
  494. = VerySimplCount !Int -- Used when don't want detailed stats
  495. | SimplCount {
  496. ticks :: !Int, -- Total ticks
  497. details :: !TickCounts, -- How many of each type
  498. n_log :: !Int, -- N
  499. log1 :: [Tick], -- Last N events; <= opt_HistorySize,
  500. -- most recent first
  501. log2 :: [Tick] -- Last opt_HistorySize events before that
  502. -- Having log1, log2 lets us accumulate the
  503. -- recent history reasonably efficiently
  504. }
  505. type TickCounts = Map Tick Int
  506. simplCountN :: SimplCount -> Int
  507. simplCountN (VerySimplCount n) = n
  508. simplCountN (SimplCount { ticks = n }) = n
  509. zeroSimplCount dflags
  510. -- This is where we decide whether to do
  511. -- the VerySimpl version or the full-stats version
  512. | dopt Opt_D_dump_simpl_stats dflags
  513. = SimplCount {ticks = 0, details = Map.empty,
  514. n_log = 0, log1 = [], log2 = []}
  515. | otherwise
  516. = VerySimplCount 0
  517. isZeroSimplCount (VerySimplCount n) = n==0
  518. isZeroSimplCount (SimplCount { ticks = n }) = n==0
  519. doFreeSimplTick tick sc@SimplCount { details = dts }
  520. = sc { details = dts `addTick` tick }
  521. doFreeSimplTick _ sc = sc
  522. doSimplTick tick sc@SimplCount { ticks = tks, details = dts, n_log = nl, log1 = l1 }
  523. | nl >= opt_HistorySize = sc1 { n_log = 1, log1 = [tick], log2 = l1 }
  524. | otherwise = sc1 { n_log = nl+1, log1 = tick : l1 }
  525. where
  526. sc1 = sc { ticks = tks+1, details = dts `addTick` tick }
  527. doSimplTick _ (VerySimplCount n) = VerySimplCount (n+1)
  528. -- Don't use Map.unionWith because that's lazy, and we want to
  529. -- be pretty strict here!
  530. addTick :: TickCounts -> Tick -> TickCounts
  531. addTick fm tick = case Map.lookup tick fm of
  532. Nothing -> Map.insert tick 1 fm
  533. Just n -> n1 `seq` Map.insert tick n1 fm
  534. where
  535. n1 = n+1
  536. plusSimplCount sc1@(SimplCount { ticks = tks1, details = dts1 })
  537. sc2@(SimplCount { ticks = tks2, details = dts2 })
  538. = log_base { ticks = tks1 + tks2, details = Map.unionWith (+) dts1 dts2 }
  539. where
  540. -- A hackish way of getting recent log info
  541. log_base | null (log1 sc2) = sc1 -- Nothing at all in sc2
  542. | null (log2 sc2) = sc2 { log2 = log1 sc1 }
  543. | otherwise = sc2
  544. plusSimplCount (VerySimplCount n) (VerySimplCount m) = VerySimplCount (n+m)
  545. plusSimplCount _ _ = panic "plusSimplCount"
  546. -- We use one or the other consistently
  547. pprSimplCount (VerySimplCount n) = ptext (sLit "Total ticks:") <+> int n
  548. pprSimplCount (SimplCount { ticks = tks, details = dts, log1 = l1, log2 = l2 })
  549. = vcat [ptext (sLit "Total ticks: ") <+> int tks,
  550. blankLine,
  551. pprTickCounts (Map.toList dts),
  552. if verboseSimplStats then
  553. vcat [blankLine,
  554. ptext (sLit "Log (most recent first)"),
  555. nest 4 (vcat (map ppr l1) $$ vcat (map ppr l2))]
  556. else empty
  557. ]
  558. pprTickCounts :: [(Tick,Int)] -> SDoc
  559. pprTickCounts [] = empty
  560. pprTickCounts ((tick1,n1):ticks)
  561. = vcat [int tot_n <+> text (tickString tick1),
  562. pprTCDetails real_these,
  563. pprTickCounts others
  564. ]
  565. where
  566. tick1_tag = tickToTag tick1
  567. (these, others) = span same_tick ticks
  568. real_these = (tick1,n1):these
  569. same_tick (tick2,_) = tickToTag tick2 == tick1_tag
  570. tot_n = sum [n | (_,n) <- real_these]
  571. pprTCDetails :: [(Tick, Int)] -> SDoc
  572. pprTCDetails ticks
  573. = nest 4 (vcat [int n <+> pprTickCts tick | (tick,n) <- ticks])
  574. \end{code}
  575. \begin{code}
  576. data Tick
  577. = PreInlineUnconditionally Id
  578. | PostInlineUnconditionally Id
  579. | UnfoldingDone Id
  580. | RuleFired FastString -- Rule name
  581. | LetFloatFromLet
  582. | EtaExpansion Id -- LHS binder
  583. | EtaReduction Id -- Binder on outer lambda
  584. | BetaReduction Id -- Lambda binder
  585. | CaseOfCase Id -- Bndr on *inner* case
  586. | KnownBranch Id -- Case binder
  587. | CaseMerge Id -- Binder on outer case
  588. | AltMerge Id -- Case binder
  589. | CaseElim Id -- Case binder
  590. | CaseIdentity Id -- Case binder
  591. | FillInCaseDefault Id -- Case binder
  592. | BottomFound
  593. | SimplifierDone -- Ticked at each iteration of the simplifier
  594. instance Outputable Tick where
  595. ppr tick = text (tickString tick) <+> pprTickCts tick
  596. instance Eq Tick where
  597. a == b = case a `cmpTick` b of
  598. EQ -> True
  599. _ -> False
  600. instance Ord Tick where
  601. compare = cmpTick
  602. tickToTag :: Tick -> Int
  603. tickToTag (PreInlineUnconditionally _) = 0
  604. tickToTag (PostInlineUnconditionally _) = 1
  605. tickToTag (UnfoldingDone _) = 2
  606. tickToTag (RuleFired _) = 3
  607. tickToTag LetFloatFromLet = 4
  608. tickToTag (EtaExpansion _) = 5
  609. tickToTag (EtaReduction _) = 6
  610. tickToTag (BetaReduction _) = 7
  611. tickToTag (CaseOfCase _) = 8
  612. tickToTag (KnownBranch _) = 9
  613. tickToTag (CaseMerge _) = 10
  614. tickToTag (CaseElim _) = 11
  615. tickToTag (CaseIdentity _) = 12
  616. tickToTag (FillInCaseDefault _) = 13
  617. tickToTag BottomFound = 14
  618. tickToTag SimplifierDone = 16
  619. tickToTag (AltMerge _) = 17
  620. tickString :: Tick -> String
  621. tickString (PreInlineUnconditionally _) = "PreInlineUnconditionally"
  622. tickString (PostInlineUnconditionally _)= "PostInlineUnconditionally"
  623. tickString (UnfoldingDone _) = "UnfoldingDone"
  624. tickString (RuleFired _) = "RuleFired"
  625. tickString LetFloatFromLet = "LetFloatFromLet"
  626. tickString (EtaExpansion _) = "EtaExpansion"
  627. tickString (EtaReduction _) = "EtaReduction"
  628. tickString (BetaReduction _) = "BetaReduction"
  629. tickString (CaseOfCase _) = "CaseOfCase"
  630. tickString (KnownBranch _) = "KnownBranch"
  631. tickString (CaseMerge _) = "CaseMerge"
  632. tickString (AltMerge _) = "AltMerge"
  633. tickString (CaseElim _) = "CaseElim"
  634. tickString (CaseIdentity _) = "CaseIdentity"
  635. tickString (FillInCaseDefault _) = "FillInCaseDefault"
  636. tickString BottomFound = "BottomFound"
  637. tickString SimplifierDone = "SimplifierDone"
  638. pprTickCts :: Tick -> SDoc
  639. pprTickCts (PreInlineUnconditionally v) = ppr v
  640. pprTickCts (PostInlineUnconditionally v)= ppr v
  641. pprTickCts (UnfoldingDone v) = ppr v
  642. pprTickCts (RuleFired v) = ppr v
  643. pprTickCts LetFloatFromLet = empty
  644. pprTickCts (EtaExpansion v) = ppr v
  645. pprTickCts (EtaReduction v) = ppr v
  646. pprTickCts (BetaReduction v) = ppr v
  647. pprTickCts (CaseOfCase v) = ppr v
  648. pprTickCts (KnownBranch v) = ppr v
  649. pprTickCts (CaseMerge v) = ppr v
  650. pprTickCts (AltMerge v) = ppr v
  651. pprTickCts (CaseElim v) = ppr v
  652. pprTickCts (CaseIdentity v) = ppr v
  653. pprTickCts (FillInCaseDefault v) = ppr v
  654. pprTickCts _ = empty
  655. cmpTick :: Tick -> Tick -> Ordering
  656. cmpTick a b = case (tickToTag a `compare` tickToTag b) of
  657. GT -> GT
  658. EQ -> cmpEqTick a b
  659. LT -> LT
  660. cmpEqTick :: Tick -> Tick -> Ordering
  661. cmpEqTick (PreInlineUnconditionally a) (PreInlineUnconditionally b) = a `compare` b
  662. cmpEqTick (PostInlineUnconditionally a) (PostInlineUnconditionally b) = a `compare` b
  663. cmpEqTick (UnfoldingDone a) (UnfoldingDone b) = a `compare` b
  664. cmpEqTick (RuleFired a) (RuleFired b) = a `compare` b
  665. cmpEqTick (EtaExpansion a) (EtaExpansion b) = a `compare` b
  666. cmpEqTick (EtaReduction a) (EtaReduction b) = a `compare` b
  667. cmpEqTick (BetaReduction a) (BetaReduction b) = a `compare` b
  668. cmpEqTick (CaseOfCase a) (CaseOfCase b) = a `compare` b
  669. cmpEqTick (KnownBranch a) (KnownBranch b) = a `compare` b
  670. cmpEqTick (CaseMerge a) (CaseMerge b) = a `compare` b
  671. cmpEqTick (AltMerge a) (AltMerge b) = a `compare` b
  672. cmpEqTick (CaseElim a) (CaseElim b) = a `compare` b
  673. cmpEqTick (CaseIdentity a) (CaseIdentity b) = a `compare` b
  674. cmpEqTick (FillInCaseDefault a) (FillInCaseDefault b) = a `compare` b
  675. cmpEqTick _ _ = EQ
  676. \end{code}
  677. %************************************************************************
  678. %* *
  679. Monad and carried data structure definitions
  680. %* *
  681. %************************************************************************
  682. \begin{code}
  683. newtype CoreState = CoreState {
  684. cs_uniq_supply :: UniqSupply
  685. }
  686. data CoreReader = CoreReader {
  687. cr_hsc_env :: HscEnv,
  688. cr_rule_base :: RuleBase,
  689. cr_module :: Module
  690. }
  691. data CoreWriter = CoreWriter {
  692. cw_simpl_count :: SimplCount
  693. }
  694. emptyWriter :: DynFlags -> CoreWriter
  695. emptyWriter dflags = CoreWriter {
  696. cw_simpl_count = zeroSimplCount dflags
  697. }
  698. plusWriter :: CoreWriter -> CoreWriter -> CoreWriter
  699. plusWriter w1 w2 = CoreWriter {
  700. cw_simpl_count = (cw_simpl_count w1) `plusSimplCount` (cw_simpl_count w2)
  701. }
  702. type CoreIOEnv = IOEnv CoreReader
  703. -- | The monad used by Core-to-Core passes to access common state, register simplification
  704. -- statistics and so on
  705. newtype CoreM a = CoreM { unCoreM :: CoreState -> CoreIOEnv (a, CoreState, CoreWriter) }
  706. instance Functor CoreM where
  707. fmap f ma = do
  708. a <- ma
  709. return (f a)
  710. instance Monad CoreM where
  711. return x = CoreM (\s -> nop s x)
  712. mx >>= f = CoreM $ \s -> do
  713. (x, s', w1) <- unCoreM mx s
  714. (y, s'', w2) <- unCoreM (f x) s'
  715. return (y, s'', w1 `plusWriter` w2)
  716. instance Applicative CoreM where
  717. pure = return
  718. (<*>) = ap
  719. -- For use if the user has imported Control.Monad.Error from MTL
  720. -- Requires UndecidableInstances
  721. instance MonadPlus IO => MonadPlus CoreM where
  722. mzero = CoreM (const mzero)
  723. m `mplus` n = CoreM (\rs -> unCoreM m rs `mplus` unCoreM n rs)
  724. instance MonadUnique CoreM where
  725. getUniqueSupplyM = do
  726. us <- getS cs_uniq_supply
  727. let (us1, us2) = splitUniqSupply us
  728. modifyS (\s -> s { cs_uniq_supply = us2 })
  729. return us1
  730. runCoreM :: HscEnv
  731. -> RuleBase
  732. -> UniqSupply
  733. -> Module
  734. -> CoreM a
  735. -> IO (a, SimplCount)
  736. runCoreM hsc_env rule_base us mod m =
  737. liftM extract $ runIOEnv reader $ unCoreM m state
  738. where
  739. reader = CoreReader {
  740. cr_hsc_env = hsc_env,
  741. cr_rule_base = rule_base,
  742. cr_module = mod
  743. }
  744. state = CoreState {
  745. cs_uniq_supply = us
  746. }
  747. extract :: (a, CoreState, CoreWriter) -> (a, SimplCount)
  748. extract (value, _, writer) = (value, cw_simpl_count writer)
  749. \end{code}
  750. %************************************************************************
  751. %* *
  752. Core combinators, not exported
  753. %* *
  754. %************************************************************************
  755. \begin{code}
  756. nop :: CoreState -> a -> CoreIOEnv (a, CoreState, CoreWriter)
  757. nop s x = do
  758. r <- getEnv
  759. return (x, s, emptyWriter $ (hsc_dflags . cr_hsc_env) r)
  760. read :: (CoreReader -> a) -> CoreM a
  761. read f = CoreM (\s -> getEnv >>= (\r -> nop s (f r)))
  762. getS :: (CoreState -> a) -> CoreM a
  763. getS f = CoreM (\s -> nop s (f s))
  764. modifyS :: (CoreState -> CoreState) -> CoreM ()
  765. modifyS f = CoreM (\s -> nop (f s) ())
  766. write :: CoreWriter -> CoreM ()
  767. write w = CoreM (\s -> return ((), s, w))
  768. \end{code}
  769. \subsection{Lifting IO into the monad}
  770. \begin{code}
  771. -- | Lift an 'IOEnv' operation into 'CoreM'
  772. liftIOEnv :: CoreIOEnv a -> CoreM a
  773. liftIOEnv mx = CoreM (\s -> mx >>= (\x -> nop s x))
  774. instance MonadIO CoreM where
  775. liftIO = liftIOEnv . IOEnv.liftIO
  776. -- | Lift an 'IO' operation into 'CoreM' while consuming its 'SimplCount'
  777. liftIOWithCount :: IO (SimplCount, a) -> CoreM a
  778. liftIOWithCount what = liftIO what >>= (\(count, x) -> addSimplCount count >> return x)
  779. \end{code}
  780. %************************************************************************
  781. %* *
  782. Reader, writer and state accessors
  783. %* *
  784. %************************************************************************
  785. \begin{code}
  786. getHscEnv :: CoreM HscEnv
  787. getHscEnv = read cr_hsc_env
  788. getRuleBase :: CoreM RuleBase
  789. getRuleBase = read cr_rule_base
  790. getModule :: CoreM Module
  791. getModule = read cr_module
  792. addSimplCount :: SimplCount -> CoreM ()
  793. addSimplCount count = write (CoreWriter { cw_simpl_count = count })
  794. -- Convenience accessors for useful fields of HscEnv
  795. getDynFlags :: CoreM DynFlags
  796. getDynFlags = fmap hsc_dflags getHscEnv
  797. -- | The original name cache is the current mapping from 'Module' and
  798. -- 'OccName' to a compiler-wide unique 'Name'
  799. getOrigNameCache :: CoreM OrigNameCache
  800. getOrigNameCache = do
  801. nameCacheRef <- fmap hsc_NC getHscEnv
  802. liftIO $ fmap nsNames $ readIORef nameCacheRef
  803. \end{code}
  804. %************************************************************************
  805. %* *
  806. Dealing with annotations
  807. %* *
  808. %************************************************************************
  809. \begin{code}
  810. -- | Get all annotations of a given type. This happens lazily, that is
  811. -- no deserialization will take place until the [a] is actually demanded and
  812. -- the [a] can also be empty (the UniqFM is not filtered).
  813. --
  814. -- This should be done once at the start of a Core-to-Core pass that uses
  815. -- annotations.
  816. --
  817. -- See Note [Annotations]
  818. getAnnotations :: Typeable a => ([Word8] -> a) -> ModGuts -> CoreM (UniqFM [a])
  819. getAnnotations deserialize guts = do
  820. hsc_env <- getHscEnv
  821. ann_env <- liftIO $ prepareAnnotations hsc_env (Just guts)
  822. return (deserializeAnns deserialize ann_env)
  823. -- | Get at most one annotation of a given type per Unique.
  824. getFirstAnnotations :: Typeable a => ([Word8] -> a) -> ModGuts -> CoreM (UniqFM a)
  825. getFirstAnnotations deserialize guts
  826. = liftM (mapUFM head . filterUFM (not . null))
  827. $ getAnnotations deserialize guts
  828. \end{code}
  829. Note [Annotations]
  830. ~~~~~~~~~~~~~~~~~~
  831. A Core-to-Core pass that wants to make use of annotations calls
  832. getAnnotations or getFirstAnnotations at the beginning to obtain a UniqFM with
  833. annotations of a specific type. This produces all annotations from interface
  834. files read so far. However, annotations from interface files read during the
  835. pass will not be visible until getAnnotations is called again. This is similar
  836. to how rules work and probably isn't too bad.
  837. The current implementation could be optimised a bit: when looking up
  838. annotations for a thing from the HomePackageTable, we could search directly in
  839. the module where the thing is defined rather than building one UniqFM which
  840. contains all annotations we know of. This would work because annotations can
  841. only be given to things defined in the same module. However, since we would
  842. only want to deserialise every annotation once, we would have to build a cache
  843. for every module in the HTP. In the end, it's probably not worth it as long as
  844. we aren't using annotations heavily.
  845. %************************************************************************
  846. %* *
  847. Direct screen output
  848. %* *
  849. %************************************************************************
  850. \begin{code}
  851. msg :: (DynFlags -> SDoc -> IO ()) -> SDoc -> CoreM ()
  852. msg how doc = do
  853. dflags <- getDynFlags
  854. liftIO $ how dflags doc
  855. -- | Output a String message to the screen
  856. putMsgS :: String -> CoreM ()
  857. putMsgS = putMsg . text
  858. -- | Output a message to the screen
  859. putMsg :: SDoc -> CoreM ()
  860. putMsg = msg Err.putMsg
  861. -- | Output a string error to the screen
  862. errorMsgS :: String -> CoreM ()
  863. errorMsgS = errorMsg . text
  864. -- | Output an error to the screen
  865. errorMsg :: SDoc -> CoreM ()
  866. errorMsg = msg Err.errorMsg
  867. -- | Output a fatal string error to the screen. Note this does not by itself cause the compiler to die
  868. fatalErrorMsgS :: String -> CoreM ()
  869. fatalErrorMsgS = fatalErrorMsg . text
  870. -- | Output a fatal error to the screen. Note this does not by itself cause the compiler to die
  871. fatalErrorMsg :: SDoc -> CoreM ()
  872. fatalErrorMsg = msg Err.fatalErrorMsg
  873. -- | Output a string debugging message at verbosity level of @-v@ or higher
  874. debugTraceMsgS :: String -> CoreM ()
  875. debugTraceMsgS = debugTraceMsg . text
  876. -- | Outputs a debugging message at verbosity level of @-v@ or higher
  877. debugTraceMsg :: SDoc -> CoreM ()
  878. debugTraceMsg = msg (flip Err.debugTraceMsg 3)
  879. -- | Show some labelled 'SDoc' if a particular flag is set or at a verbosity level of @-v -ddump-most@ or higher
  880. dumpIfSet_dyn :: DynFlag -> String -> SDoc -> CoreM ()
  881. dumpIfSet_dyn flag str = msg (\dflags -> Err.dumpIfSet_dyn dflags flag str)
  882. \end{code}
  883. \begin{code}
  884. initTcForLookup :: HscEnv -> TcM a -> IO a
  885. initTcForLookup hsc_env = liftM (expectJust "initTcInteractive" . snd) . initTc hsc_env HsSrcFile False iNTERACTIVE
  886. \end{code}
  887. %************************************************************************
  888. %* *
  889. Finding TyThings
  890. %* *
  891. %************************************************************************
  892. \begin{code}
  893. instance MonadThings CoreM where
  894. lookupThing name = do
  895. hsc_env <- getHscEnv
  896. liftIO $ initTcForLookup hsc_env (tcLookupGlobal name)
  897. \end{code}
  898. %************************************************************************
  899. %* *
  900. Template Haskell interoperability
  901. %* *
  902. %************************************************************************
  903. \begin{code}
  904. #ifdef GHCI
  905. -- | Attempt to convert a Template Haskell name to one that GHC can
  906. -- understand. Original TH names such as those you get when you use
  907. -- the @'foo@ syntax will be translated to their equivalent GHC name
  908. -- exactly. Qualified or unqualifed TH names will be dynamically bound
  909. -- to names in the module being compiled, if possible. Exact TH names
  910. -- will be bound to the name they represent, exactly.
  911. thNameToGhcName :: TH.Name -> CoreM (Maybe Name)
  912. thNameToGhcName th_name = do
  913. hsc_env <- getHscEnv
  914. liftIO $ initTcForLookup hsc_env (lookupThName_maybe th_name)
  915. #endif
  916. \end{code}