module SCCfinal ( stgMassageForProfiling ) where
#include "HsVersions.h"
import GhcPrelude
import StgSyn
import CostCentre
import Id
import Name
import Module
import UniqSupply ( UniqSupply )
import ListSetOps ( removeDups )
import Outputable
import DynFlags
import CoreSyn ( Tickish(..) )
import FastString
import SrcLoc
import Util
import Control.Monad (liftM, ap)
stgMassageForProfiling
:: DynFlags
-> Module
-> UniqSupply
-> [StgTopBinding]
-> (CollectedCCs, [StgTopBinding])
stgMassageForProfiling dflags mod_name _us stg_binds
= let
((local_ccs, extern_ccs, cc_stacks),
stg_binds2)
= initMM mod_name (do_top_bindings stg_binds)
(fixed_ccs, fixed_cc_stacks)
= if gopt Opt_AutoSccsOnIndividualCafs dflags
then ([],[])
else ([all_cafs_cc], [all_cafs_ccs])
local_ccs_no_dups = fst (removeDups cmpCostCentre local_ccs)
extern_ccs_no_dups = fst (removeDups cmpCostCentre extern_ccs)
in
((fixed_ccs ++ local_ccs_no_dups,
extern_ccs_no_dups,
fixed_cc_stacks ++ cc_stacks), stg_binds2)
where
span = mkGeneralSrcSpan (mkFastString "<entire-module>")
all_cafs_cc = mkAllCafsCC mod_name span
all_cafs_ccs = mkSingletonCCS all_cafs_cc
do_top_bindings :: [StgTopBinding] -> MassageM [StgTopBinding]
do_top_bindings [] = return []
do_top_bindings (StgTopLifted (StgNonRec b rhs) : bs) = do
rhs' <- do_top_rhs b rhs
bs' <- do_top_bindings bs
return (StgTopLifted (StgNonRec b rhs') : bs')
do_top_bindings (StgTopLifted (StgRec pairs) : bs) = do
pairs2 <- mapM do_pair pairs
bs' <- do_top_bindings bs
return (StgTopLifted (StgRec pairs2) : bs')
where
do_pair (b, rhs) = do
rhs2 <- do_top_rhs b rhs
return (b, rhs2)
do_top_bindings (b@StgTopStringLit{} : bs) = do
bs' <- do_top_bindings bs
return (b : bs')
do_top_rhs :: Id -> StgRhs -> MassageM StgRhs
do_top_rhs _ (StgRhsClosure _ _ _ _ []
(StgTick (ProfNote _cc False _push)
(StgConApp con args _)))
| not (isDllConApp dflags mod_name con args)
= return (StgRhsCon dontCareCCS con args)
do_top_rhs binder (StgRhsClosure _ bi fv u [] body)
= do
caf_ccs <- if gopt Opt_AutoSccsOnIndividualCafs dflags
then let cc = mkAutoCC binder modl CafCC
ccs = mkSingletonCCS cc
modl | Just m <- nameModule_maybe (idName binder) = m
| otherwise = mod_name
in do
collectNewCC cc
collectCCS ccs
return ccs
else
return all_cafs_ccs
body' <- do_expr body
return (StgRhsClosure caf_ccs bi fv u [] body')
do_top_rhs _ (StgRhsClosure _no_ccs bi fv u args body)
= do body' <- do_expr body
return (StgRhsClosure dontCareCCS bi fv u args body')
do_top_rhs _ (StgRhsCon _ con args)
= return (StgRhsCon dontCareCCS con args)
do_expr :: StgExpr -> MassageM StgExpr
do_expr (StgLit l) = return (StgLit l)
do_expr (StgApp fn args)
= return (StgApp fn args)
do_expr (StgConApp con args ty_args)
= return (StgConApp con args ty_args)
do_expr (StgOpApp con args res_ty)
= return (StgOpApp con args res_ty)
do_expr (StgTick note@(ProfNote cc _ _) expr) = do
collectCC cc
expr' <- do_expr expr
return (StgTick note expr')
do_expr (StgTick ti expr) = do
expr' <- do_expr expr
return (StgTick ti expr')
do_expr (StgCase expr bndr alt_type alts) = do
expr' <- do_expr expr
alts' <- mapM do_alt alts
return (StgCase expr' bndr alt_type alts')
where
do_alt (id, bs, e) = do
e' <- do_expr e
return (id, bs, e')
do_expr (StgLet b e) = do
(b,e) <- do_let b e
return (StgLet b e)
do_expr (StgLetNoEscape b e) = do
(b,e) <- do_let b e
return (StgLetNoEscape b e)
do_expr other = pprPanic "SCCfinal.do_expr" (ppr other)
do_let (StgNonRec b rhs) e = do
rhs' <- do_rhs rhs
e' <- do_expr e
return (StgNonRec b rhs',e')
do_let (StgRec pairs) e = do
pairs' <- mapM do_pair pairs
e' <- do_expr e
return (StgRec pairs', e')
where
do_pair (b, rhs) = do
rhs2 <- do_rhs rhs
return (b, rhs2)
do_rhs :: StgRhs -> MassageM StgRhs
do_rhs (StgRhsClosure _closure_cc _bi _fv _u []
(StgTick (ProfNote cc False _push)
(StgConApp con args _)))
= do collectCC cc
return (StgRhsCon currentCCS con args)
do_rhs (StgRhsClosure _ bi fv u args expr) = do
expr' <- do_expr expr
return (StgRhsClosure currentCCS bi fv u args expr')
do_rhs (StgRhsCon _ con args)
= return (StgRhsCon currentCCS con args)
newtype MassageM result
= MassageM {
unMassageM :: Module
-> CollectedCCs
-> (CollectedCCs, result)
}
instance Functor MassageM where
fmap = liftM
instance Applicative MassageM where
pure x = MassageM (\_ ccs -> (ccs, x))
(<*>) = ap
(*>) = thenMM_
instance Monad MassageM where
(>>=) = thenMM
(>>) = (*>)
initMM :: Module
-> MassageM a
-> (CollectedCCs, a)
initMM mod_name (MassageM m) = m mod_name ([],[],[])
thenMM :: MassageM a -> (a -> MassageM b) -> MassageM b
thenMM_ :: MassageM a -> (MassageM b) -> MassageM b
thenMM expr cont = MassageM $ \mod ccs ->
case unMassageM expr mod ccs of { (ccs2, result) ->
unMassageM (cont result) mod ccs2 }
thenMM_ expr cont = MassageM $ \mod ccs ->
case unMassageM expr mod ccs of { (ccs2, _) ->
unMassageM cont mod ccs2 }
collectCC :: CostCentre -> MassageM ()
collectCC cc
= MassageM $ \mod_name (local_ccs, extern_ccs, ccss)
-> if (cc `ccFromThisModule` mod_name) then
((cc : local_ccs, extern_ccs, ccss), ())
else
((local_ccs, cc : extern_ccs, ccss), ())
collectNewCC :: CostCentre -> MassageM ()
collectNewCC cc
= MassageM $ \_mod_name (local_ccs, extern_ccs, ccss)
-> ((cc : local_ccs, extern_ccs, ccss), ())
collectCCS :: CostCentreStack -> MassageM ()
collectCCS ccs
= MassageM $ \_mod_name (local_ccs, extern_ccs, ccss)
-> ASSERT(not (noCCSAttached ccs))
((local_ccs, extern_ccs, ccs : ccss), ())