2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 \section[DsBinds]{Pattern-matching bindings (HsBinds and MonoBinds)}
6 Handles @HsBinds@; those at the top level require different handling,
7 in that the @Rec@/@NonRec@/etc structure is thrown away (whereas at
8 lower levels it is preserved with @let@/@letrec@s).
11 module DsBinds ( dsMonoBinds, AutoScc(..) ) where
13 #include "HsVersions.h"
16 import {-# SOURCE #-} DsExpr( dsExpr )
18 import HsSyn -- lots of things
19 import CoreSyn -- lots of things
20 import CoreUtils ( exprType, mkInlineMe, mkSCC )
21 import TcHsSyn ( TypecheckedMonoBinds )
23 import DsGRHSs ( dsGuarded )
25 import Match ( matchWrapper )
27 import CmdLineOpts ( opt_AutoSccsOnAllToplevs, opt_AutoSccsOnExportedToplevs )
28 import CostCentre ( mkAutoCC, IsCafCC(..) )
29 import Id ( idType, idName, isExportedId, isSpecPragmaId, Id )
32 import TcType ( mkTyVarTy )
33 import Subst ( substTyWith )
34 import TysWiredIn ( voidTy )
36 import Maybe ( isJust )
39 %************************************************************************
41 \subsection[dsMonoBinds]{Desugaring a @MonoBinds@}
43 %************************************************************************
46 dsMonoBinds :: AutoScc -- scc annotation policy (see below)
47 -> TypecheckedMonoBinds
48 -> [(Id,CoreExpr)] -- Put this on the end (avoid quadratic append)
49 -> DsM [(Id,CoreExpr)] -- Result
51 dsMonoBinds _ EmptyMonoBinds rest = returnDs rest
53 dsMonoBinds auto_scc (AndMonoBinds binds_1 binds_2) rest
54 = dsMonoBinds auto_scc binds_2 rest `thenDs` \ rest' ->
55 dsMonoBinds auto_scc binds_1 rest'
57 dsMonoBinds _ (CoreMonoBind var core_expr) rest
58 = returnDs ((var, core_expr) : rest)
60 dsMonoBinds _ (VarMonoBind var expr) rest
61 = dsExpr expr `thenDs` \ core_expr ->
63 -- Dictionary bindings are always VarMonoBinds, so
64 -- we only need do this here
65 addDictScc var core_expr `thenDs` \ core_expr' ->
68 -- Gross hack to prevent inlining into SpecPragmaId rhss
69 -- Consider fromIntegral = fromInteger . toInteger
70 -- spec1 = fromIntegral Int Float
71 -- Even though fromIntegral is small we don't want to inline
72 -- it inside spec1, so that we collect the specialised call
73 -- Solution: make spec1 an INLINE thing.
74 core_expr'' = mkInline (isSpecPragmaId var) core_expr'
77 returnDs ((var, core_expr'') : rest)
79 dsMonoBinds auto_scc (FunMonoBind fun _ matches locn) rest
81 matchWrapper (FunRhs fun) matches `thenDs` \ (args, body) ->
82 addAutoScc auto_scc (fun, mkLams args body) `thenDs` \ pair ->
83 returnDs (pair : rest)
85 dsMonoBinds auto_scc (PatMonoBind pat grhss locn) rest
87 dsGuarded grhss `thenDs` \ body_expr ->
88 mkSelectorBinds pat body_expr `thenDs` \ sel_binds ->
89 mapDs (addAutoScc auto_scc) sel_binds `thenDs` \ sel_binds ->
90 returnDs (sel_binds ++ rest)
92 -- Common special case: no type or dictionary abstraction
93 -- For the (rare) case when there are some mixed-up
94 -- dictionary bindings (for which a Rec is convenient)
95 -- we reply on the enclosing dsBind to wrap a Rec around.
96 dsMonoBinds auto_scc (AbsBinds [] [] exports inlines binds) rest
97 = dsMonoBinds (addSccs auto_scc exports) binds []`thenDs` \ core_prs ->
99 core_prs' = addLocalInlines exports inlines core_prs
100 exports' = [(global, Var local) | (_, global, local) <- exports]
102 returnDs (core_prs' ++ exports' ++ rest)
104 -- Another common case: one exported variable
105 -- Non-recursive bindings come through this way
107 (AbsBinds all_tyvars dicts exps@[(tyvars, global, local)] inlines binds) rest
108 = ASSERT( all (`elem` tyvars) all_tyvars )
109 dsMonoBinds (addSccs auto_scc exps) binds [] `thenDs` \ core_prs ->
111 -- Always treat the binds as recursive, because the typechecker
112 -- makes rather mixed-up dictionary bindings
113 core_bind = Rec core_prs
115 -- The mkInline does directly what the
116 -- addLocalInlines do in the other cases
117 export' = (global, mkInline (idName global `elemNameSet` inlines) $
118 mkLams tyvars $ mkLams dicts $
119 Let core_bind (Var local))
121 returnDs (export' : rest)
123 dsMonoBinds auto_scc (AbsBinds all_tyvars dicts exports inlines binds) rest
124 = dsMonoBinds (addSccs auto_scc exports) binds []`thenDs` \ core_prs ->
126 -- Rec because of mixed-up dictionary bindings
127 core_bind = Rec (addLocalInlines exports inlines core_prs)
129 tup_expr = mkTupleExpr locals
130 tup_ty = exprType tup_expr
131 poly_tup_expr = mkLams all_tyvars $ mkLams dicts $
132 Let core_bind tup_expr
133 locals = [local | (_, _, local) <- exports]
134 local_tys = map idType locals
136 newSysLocalDs (exprType poly_tup_expr) `thenDs` \ poly_tup_id ->
138 dict_args = map Var dicts
140 mk_bind (tyvars, global, local) n -- locals !! n == local
141 = -- Need to make fresh locals to bind in the selector, because
142 -- some of the tyvars will be bound to voidTy
143 newSysLocalsDs (map substitute local_tys) `thenDs` \ locals' ->
144 newSysLocalDs (substitute tup_ty) `thenDs` \ tup_id ->
145 returnDs (global, mkLams tyvars $ mkLams dicts $
146 mkTupleSelector locals' (locals' !! n) tup_id $
147 mkApps (mkTyApps (Var poly_tup_id) ty_args) dict_args)
149 mk_ty_arg all_tyvar | all_tyvar `elem` tyvars = mkTyVarTy all_tyvar
151 ty_args = map mk_ty_arg all_tyvars
152 substitute = substTyWith all_tyvars ty_args
154 zipWithDs mk_bind exports [0..] `thenDs` \ export_binds ->
155 -- don't scc (auto-)annotate the tuple itself.
156 returnDs ((poly_tup_id, poly_tup_expr) : (export_binds ++ rest))
160 %************************************************************************
162 \subsection{Adding inline pragmas}
164 %************************************************************************
167 mkInline :: Bool -> CoreExpr -> CoreExpr
168 mkInline True body = mkInlineMe body
169 mkInline False body = body
171 addLocalInlines :: [(a, Id, Id)] -> NameSet -> [(Id,CoreExpr)] -> [(Id,CoreExpr)]
172 addLocalInlines exports inlines pairs
173 = [(bndr, mkInline (bndr `elemVarSet` local_inlines) rhs) | (bndr,rhs) <- pairs]
175 local_inlines = mkVarSet [l | (_,g,l) <- exports, idName g `elemNameSet` inlines]
179 %************************************************************************
181 \subsection[addAutoScc]{Adding automatic sccs}
183 %************************************************************************
188 | TopLevelAddSccs (Id -> Maybe Id)
191 addSccs :: AutoScc -> [(a,Id,Id)] -> AutoScc
192 addSccs auto_scc@(TopLevelAddSccs _) exports = auto_scc
193 addSccs NoSccs exports = NoSccs
194 addSccs TopLevel exports
195 = TopLevelAddSccs (\id -> case [ exp | (_,exp,loc) <- exports, loc == id ] of
196 (exp:_) | opt_AutoSccsOnAllToplevs ||
198 opt_AutoSccsOnExportedToplevs)
202 addAutoScc :: AutoScc -- if needs be, decorate toplevs?
204 -> DsM (Id, CoreExpr)
206 addAutoScc (TopLevelAddSccs auto_scc_fn) pair@(bndr, core_expr)
208 = getModuleDs `thenDs` \ mod ->
209 returnDs (bndr, mkSCC (mkAutoCC top_bndr mod NotCafCC) core_expr)
210 where do_auto_scc = isJust maybe_auto_scc
211 maybe_auto_scc = auto_scc_fn bndr
212 (Just top_bndr) = maybe_auto_scc
218 If profiling and dealing with a dict binding,
219 wrap the dict in @_scc_ DICT <dict>@:
222 addDictScc var rhs = returnDs rhs
224 {- DISABLED for now (need to somehow make up a name for the scc) -- SDM
225 | not ( opt_SccProfilingOn && opt_AutoSccsOnDicts)
226 || not (isDictTy (idType var))
227 = returnDs rhs -- That's easy: do nothing
230 = getModuleAndGroupDs `thenDs` \ (mod, grp) ->
231 -- ToDo: do -dicts-all flag (mark dict things with individual CCs)
232 returnDs (Note (SCC (mkAllDictsCC mod grp False)) rhs)