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 BasicTypes ( RecFlag(..) )
28 import CmdLineOpts ( opt_SccProfilingOn, opt_AutoSccsOnAllToplevs,
29 opt_AutoSccsOnExportedToplevs, opt_AutoSccsOnDicts
31 import CostCentre ( CostCentre, mkAutoCC, IsCafCC(..) )
32 import Id ( idType, idName, isUserExportedId, isSpecPragmaId, Id )
36 import Type ( mkTyVarTy, isDictTy )
37 import Subst ( mkTyVarSubst, substTy )
38 import TysWiredIn ( voidTy )
40 import Maybe ( isJust )
43 %************************************************************************
45 \subsection[dsMonoBinds]{Desugaring a @MonoBinds@}
47 %************************************************************************
50 dsMonoBinds :: AutoScc -- scc annotation policy (see below)
51 -> TypecheckedMonoBinds
52 -> [(Id,CoreExpr)] -- Put this on the end (avoid quadratic append)
53 -> DsM [(Id,CoreExpr)] -- Result
55 dsMonoBinds _ EmptyMonoBinds rest = returnDs rest
57 dsMonoBinds auto_scc (AndMonoBinds binds_1 binds_2) rest
58 = dsMonoBinds auto_scc binds_2 rest `thenDs` \ rest' ->
59 dsMonoBinds auto_scc binds_1 rest'
61 dsMonoBinds _ (CoreMonoBind var core_expr) rest
62 = returnDs ((var, core_expr) : rest)
64 dsMonoBinds _ (VarMonoBind var expr) rest
65 = dsExpr expr `thenDs` \ core_expr ->
67 -- Dictionary bindings are always VarMonoBinds, so
68 -- we only need do this here
69 addDictScc var core_expr `thenDs` \ core_expr' ->
72 -- Gross hack to prevent inlining into SpecPragmaId rhss
73 -- Consider fromIntegral = fromInteger . toInteger
74 -- spec1 = fromIntegral Int Float
75 -- Even though fromIntegral is small we don't want to inline
76 -- it inside spec1, so that we collect the specialised call
77 -- Solution: make spec1 an INLINE thing.
78 core_expr'' = mkInline (isSpecPragmaId var) core_expr'
81 returnDs ((var, core_expr'') : rest)
83 dsMonoBinds auto_scc (FunMonoBind fun _ matches locn) rest
85 matchWrapper (FunMatch fun) matches error_string `thenDs` \ (args, body) ->
86 addAutoScc auto_scc (fun, mkLams args body) `thenDs` \ pair ->
87 returnDs (pair : rest)
89 error_string = "function " ++ showSDoc (ppr fun)
91 dsMonoBinds auto_scc (PatMonoBind pat grhss locn) rest
93 dsGuarded grhss `thenDs` \ body_expr ->
94 mkSelectorBinds pat body_expr `thenDs` \ sel_binds ->
95 mapDs (addAutoScc auto_scc) sel_binds `thenDs` \ sel_binds ->
96 returnDs (sel_binds ++ rest)
98 -- Common special case: no type or dictionary abstraction
99 dsMonoBinds auto_scc (AbsBinds [] [] exports inlines binds) rest
100 = dsMonoBinds (addSccs auto_scc exports) binds []`thenDs` \ core_prs ->
102 exports' = [(global, Var local) | (_, global, local) <- exports]
104 returnDs (addLocalInlines exports inlines core_prs ++ exports' ++ rest)
106 -- Another common case: one exported variable
107 -- Non-recursive bindings come through this way
109 (AbsBinds all_tyvars dicts exps@[(tyvars, global, local)] inlines binds) rest
110 = ASSERT( all (`elem` tyvars) all_tyvars )
111 dsMonoBinds (addSccs auto_scc exps) binds [] `thenDs` \ core_prs ->
113 -- Always treat the binds as recursive, because the typechecker
114 -- makes rather mixed-up dictionary bindings
115 core_binds = [Rec core_prs]
116 global' = (global, mkInline (idName global `elemNameSet` inlines) $
117 mkLams tyvars $ mkLams dicts $
118 mkDsLets core_binds (Var local))
120 returnDs (global' : rest)
122 dsMonoBinds auto_scc (AbsBinds all_tyvars dicts exports inlines binds) rest
123 = dsMonoBinds (addSccs auto_scc exports) binds []`thenDs` \ core_prs ->
125 core_binds = [Rec (addLocalInlines exports inlines core_prs)]
127 tup_expr = mkTupleExpr locals
128 tup_ty = exprType tup_expr
129 poly_tup_expr = mkLams all_tyvars $ mkLams dicts $
130 mkDsLets core_binds tup_expr
131 locals = [local | (_, _, local) <- exports]
132 local_tys = map idType locals
134 newSysLocalDs (exprType poly_tup_expr) `thenDs` \ poly_tup_id ->
136 dict_args = map Var dicts
138 mk_bind (tyvars, global, local) n -- locals !! n == local
139 = -- Need to make fresh locals to bind in the selector, because
140 -- some of the tyvars will be bound to voidTy
141 newSysLocalsDs (map (substTy env) local_tys) `thenDs` \ locals' ->
142 newSysLocalDs (substTy env tup_ty) `thenDs` \ tup_id ->
143 returnDs (global, mkLams tyvars $ mkLams dicts $
144 mkTupleSelector locals' (locals' !! n) tup_id $
145 mkApps (mkTyApps (Var poly_tup_id) ty_args) dict_args)
147 mk_ty_arg all_tyvar | all_tyvar `elem` tyvars = mkTyVarTy all_tyvar
149 ty_args = map mk_ty_arg all_tyvars
150 env = mkTyVarSubst all_tyvars ty_args
152 zipWithDs mk_bind exports [0..] `thenDs` \ export_binds ->
153 -- don't scc (auto-)annotate the tuple itself.
154 returnDs ((poly_tup_id, poly_tup_expr) : (export_binds ++ rest))
158 %************************************************************************
160 \subsection{Adding inline pragmas}
162 %************************************************************************
165 mkInline :: Bool -> CoreExpr -> CoreExpr
166 mkInline True body = mkInlineMe body
167 mkInline False body = body
169 addLocalInlines :: [(a, Id, Id)] -> NameSet -> [(Id,CoreExpr)] -> [(Id,CoreExpr)]
170 addLocalInlines exports inlines pairs
171 = [(bndr, mkInline (bndr `elemVarSet` local_inlines) rhs) | (bndr,rhs) <- pairs]
173 local_inlines = mkVarSet [l | (_,g,l) <- exports, idName g `elemNameSet` inlines]
177 %************************************************************************
179 \subsection[addAutoScc]{Adding automatic sccs}
181 %************************************************************************
186 | TopLevelAddSccs (Id -> Maybe Id)
189 addSccs :: AutoScc -> [(a,Id,Id)] -> AutoScc
190 addSccs auto_scc@(TopLevelAddSccs _) exports = auto_scc
191 addSccs NoSccs exports = NoSccs
192 addSccs TopLevel exports
193 = TopLevelAddSccs (\id -> case [ exp | (_,exp,loc) <- exports, loc == id ] of
194 (exp:_) | opt_AutoSccsOnAllToplevs ||
195 (isUserExportedId exp &&
196 opt_AutoSccsOnExportedToplevs)
200 addAutoScc :: AutoScc -- if needs be, decorate toplevs?
202 -> DsM (Id, CoreExpr)
204 addAutoScc (TopLevelAddSccs auto_scc_fn) pair@(bndr, core_expr)
205 | do_auto_scc && worthSCC core_expr
206 = getModuleDs `thenDs` \ mod ->
207 returnDs (bndr, mkSCC (mkAutoCC top_bndr mod NotCafCC) core_expr)
208 where do_auto_scc = isJust maybe_auto_scc
209 maybe_auto_scc = auto_scc_fn bndr
210 (Just top_bndr) = maybe_auto_scc
215 noUserSCC (Note (SCC _) _) = False
216 worthSCC core_expr = True
219 If profiling and dealing with a dict binding,
220 wrap the dict in @_scc_ DICT <dict>@:
223 addDictScc var rhs = returnDs rhs
225 {- DISABLED for now (need to somehow make up a name for the scc) -- SDM
226 | not ( opt_SccProfilingOn && opt_AutoSccsOnDicts)
227 || not (isDictTy (idType var))
228 = returnDs rhs -- That's easy: do nothing
231 = getModuleAndGroupDs `thenDs` \ (mod, grp) ->
232 -- ToDo: do -dicts-all flag (mark dict things with individual CCs)
233 returnDs (Note (SCC (mkAllDictsCC mod grp False)) rhs)