Comments about -Wredundant-constraints
[ghc.git] / compiler / basicTypes / MkId.hs
1 {-
2 (c) The University of Glasgow 2006
3 (c) The AQUA Project, Glasgow University, 1998
4
5
6 This module contains definitions for the IdInfo for things that
7 have a standard form, namely:
8
9 - data constructors
10 - record selectors
11 - method and superclass selectors
12 - primitive operations
13 -}
14
15 {-# LANGUAGE CPP #-}
16
17 module MkId (
18 mkDictFunId, mkDictFunTy, mkDictSelId, mkDictSelRhs,
19
20 mkPrimOpId, mkFCallId,
21
22 wrapNewTypeBody, unwrapNewTypeBody,
23 wrapFamInstBody, unwrapFamInstScrut,
24 wrapTypeUnbranchedFamInstBody, unwrapTypeUnbranchedFamInstScrut,
25
26 DataConBoxer(..), mkDataConRep, mkDataConWorkId,
27
28 -- And some particular Ids; see below for why they are wired in
29 wiredInIds, ghcPrimIds,
30 unsafeCoerceName, unsafeCoerceId, realWorldPrimId,
31 voidPrimId, voidArgId,
32 nullAddrId, seqId, lazyId, lazyIdKey, runRWId,
33 coercionTokenId, magicDictId, coerceId,
34 proxyHashId, noinlineIdName,
35
36 -- Re-export error Ids
37 module PrelRules
38 ) where
39
40 #include "HsVersions.h"
41
42 import Rules
43 import TysPrim
44 import TysWiredIn
45 import PrelRules
46 import Type
47 import FamInstEnv
48 import Coercion
49 import TcType
50 import MkCore
51 import CoreUtils ( exprType, mkCast )
52 import CoreUnfold
53 import Literal
54 import TyCon
55 import CoAxiom
56 import Class
57 import NameSet
58 import VarSet
59 import Name
60 import PrimOp
61 import ForeignCall
62 import DataCon
63 import Id
64 import IdInfo
65 import Demand
66 import CoreSyn
67 import Unique
68 import UniqSupply
69 import PrelNames
70 import BasicTypes hiding ( SuccessFlag(..) )
71 import Util
72 import Pair
73 import DynFlags
74 import Outputable
75 import FastString
76 import ListSetOps
77 import qualified GHC.LanguageExtensions as LangExt
78
79 import Data.Maybe ( maybeToList )
80
81 {-
82 ************************************************************************
83 * *
84 \subsection{Wired in Ids}
85 * *
86 ************************************************************************
87
88 Note [Wired-in Ids]
89 ~~~~~~~~~~~~~~~~~~~
90 There are several reasons why an Id might appear in the wiredInIds:
91
92 (1) The ghcPrimIds are wired in because they can't be defined in
93 Haskell at all, although the can be defined in Core. They have
94 compulsory unfoldings, so they are always inlined and they have
95 no definition site. Their home module is GHC.Prim, so they
96 also have a description in primops.txt.pp, where they are called
97 'pseudoops'.
98
99 (2) The 'error' function, eRROR_ID, is wired in because we don't yet have
100 a way to express in an interface file that the result type variable
101 is 'open'; that is can be unified with an unboxed type
102
103 [The interface file format now carry such information, but there's
104 no way yet of expressing at the definition site for these
105 error-reporting functions that they have an 'open'
106 result type. -- sof 1/99]
107
108 (3) Other error functions (rUNTIME_ERROR_ID) are wired in (a) because
109 the desugarer generates code that mentions them directly, and
110 (b) for the same reason as eRROR_ID
111
112 (4) lazyId is wired in because the wired-in version overrides the
113 strictness of the version defined in GHC.Base
114
115 (5) noinlineId is wired in because when we serialize to interfaces
116 we may insert noinline statements.
117
118 In cases (2-4), the function has a definition in a library module, and
119 can be called; but the wired-in version means that the details are
120 never read from that module's interface file; instead, the full definition
121 is right here.
122 -}
123
124 wiredInIds :: [Id]
125 wiredInIds
126 = [lazyId, dollarId, oneShotId, runRWId, noinlineId]
127 ++ errorIds -- Defined in MkCore
128 ++ ghcPrimIds
129
130 -- These Ids are exported from GHC.Prim
131 ghcPrimIds :: [Id]
132 ghcPrimIds
133 = [ -- These can't be defined in Haskell, but they have
134 -- perfectly reasonable unfoldings in Core
135 realWorldPrimId,
136 voidPrimId,
137 unsafeCoerceId,
138 nullAddrId,
139 seqId,
140 magicDictId,
141 coerceId,
142 proxyHashId
143 ]
144
145 {-
146 ************************************************************************
147 * *
148 \subsection{Data constructors}
149 * *
150 ************************************************************************
151
152 The wrapper for a constructor is an ordinary top-level binding that evaluates
153 any strict args, unboxes any args that are going to be flattened, and calls
154 the worker.
155
156 We're going to build a constructor that looks like:
157
158 data (Data a, C b) => T a b = T1 !a !Int b
159
160 T1 = /\ a b ->
161 \d1::Data a, d2::C b ->
162 \p q r -> case p of { p ->
163 case q of { q ->
164 Con T1 [a,b] [p,q,r]}}
165
166 Notice that
167
168 * d2 is thrown away --- a context in a data decl is used to make sure
169 one *could* construct dictionaries at the site the constructor
170 is used, but the dictionary isn't actually used.
171
172 * We have to check that we can construct Data dictionaries for
173 the types a and Int. Once we've done that we can throw d1 away too.
174
175 * We use (case p of q -> ...) to evaluate p, rather than "seq" because
176 all that matters is that the arguments are evaluated. "seq" is
177 very careful to preserve evaluation order, which we don't need
178 to be here.
179
180 You might think that we could simply give constructors some strictness
181 info, like PrimOps, and let CoreToStg do the let-to-case transformation.
182 But we don't do that because in the case of primops and functions strictness
183 is a *property* not a *requirement*. In the case of constructors we need to
184 do something active to evaluate the argument.
185
186 Making an explicit case expression allows the simplifier to eliminate
187 it in the (common) case where the constructor arg is already evaluated.
188
189 Note [Wrappers for data instance tycons]
190 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
191 In the case of data instances, the wrapper also applies the coercion turning
192 the representation type into the family instance type to cast the result of
193 the wrapper. For example, consider the declarations
194
195 data family Map k :: * -> *
196 data instance Map (a, b) v = MapPair (Map a (Pair b v))
197
198 The tycon to which the datacon MapPair belongs gets a unique internal
199 name of the form :R123Map, and we call it the representation tycon.
200 In contrast, Map is the family tycon (accessible via
201 tyConFamInst_maybe). A coercion allows you to move between
202 representation and family type. It is accessible from :R123Map via
203 tyConFamilyCoercion_maybe and has kind
204
205 Co123Map a b v :: {Map (a, b) v ~ :R123Map a b v}
206
207 The wrapper and worker of MapPair get the types
208
209 -- Wrapper
210 $WMapPair :: forall a b v. Map a (Map a b v) -> Map (a, b) v
211 $WMapPair a b v = MapPair a b v `cast` sym (Co123Map a b v)
212
213 -- Worker
214 MapPair :: forall a b v. Map a (Map a b v) -> :R123Map a b v
215
216 This coercion is conditionally applied by wrapFamInstBody.
217
218 It's a bit more complicated if the data instance is a GADT as well!
219
220 data instance T [a] where
221 T1 :: forall b. b -> T [Maybe b]
222
223 Hence we translate to
224
225 -- Wrapper
226 $WT1 :: forall b. b -> T [Maybe b]
227 $WT1 b v = T1 (Maybe b) b (Maybe b) v
228 `cast` sym (Co7T (Maybe b))
229
230 -- Worker
231 T1 :: forall c b. (c ~ Maybe b) => b -> :R7T c
232
233 -- Coercion from family type to representation type
234 Co7T a :: T [a] ~ :R7T a
235
236 Note [Newtype datacons]
237 ~~~~~~~~~~~~~~~~~~~~~~~
238 The "data constructor" for a newtype should always be vanilla. At one
239 point this wasn't true, because the newtype arising from
240 class C a => D a
241 looked like
242 newtype T:D a = D:D (C a)
243 so the data constructor for T:C had a single argument, namely the
244 predicate (C a). But now we treat that as an ordinary argument, not
245 part of the theta-type, so all is well.
246
247
248 ************************************************************************
249 * *
250 \subsection{Dictionary selectors}
251 * *
252 ************************************************************************
253
254 Selecting a field for a dictionary. If there is just one field, then
255 there's nothing to do.
256
257 Dictionary selectors may get nested forall-types. Thus:
258
259 class Foo a where
260 op :: forall b. Ord b => a -> b -> b
261
262 Then the top-level type for op is
263
264 op :: forall a. Foo a =>
265 forall b. Ord b =>
266 a -> b -> b
267
268 -}
269
270 mkDictSelId :: Name -- Name of one of the *value* selectors
271 -- (dictionary superclass or method)
272 -> Class -> Id
273 mkDictSelId name clas
274 = mkGlobalId (ClassOpId clas) name sel_ty info
275 where
276 tycon = classTyCon clas
277 sel_names = map idName (classAllSelIds clas)
278 new_tycon = isNewTyCon tycon
279 [data_con] = tyConDataCons tycon
280 tyvars = dataConUnivTyVarBinders data_con
281 n_ty_args = length tyvars
282 arg_tys = dataConRepArgTys data_con -- Includes the dictionary superclasses
283 val_index = assoc "MkId.mkDictSelId" (sel_names `zip` [0..]) name
284
285 sel_ty = mkForAllTys tyvars $
286 mkFunTy (mkClassPred clas (mkTyVarTys (binderVars tyvars))) $
287 getNth arg_tys val_index
288
289 base_info = noCafIdInfo
290 `setArityInfo` 1
291 `setStrictnessInfo` strict_sig
292
293 info | new_tycon
294 = base_info `setInlinePragInfo` alwaysInlinePragma
295 `setUnfoldingInfo` mkInlineUnfolding (Just 1) (mkDictSelRhs clas val_index)
296 -- See Note [Single-method classes] in TcInstDcls
297 -- for why alwaysInlinePragma
298
299 | otherwise
300 = base_info `setRuleInfo` mkRuleInfo [rule]
301 -- Add a magic BuiltinRule, but no unfolding
302 -- so that the rule is always available to fire.
303 -- See Note [ClassOp/DFun selection] in TcInstDcls
304
305 -- This is the built-in rule that goes
306 -- op (dfT d1 d2) ---> opT d1 d2
307 rule = BuiltinRule { ru_name = fsLit "Class op " `appendFS`
308 occNameFS (getOccName name)
309 , ru_fn = name
310 , ru_nargs = n_ty_args + 1
311 , ru_try = dictSelRule val_index n_ty_args }
312
313 -- The strictness signature is of the form U(AAAVAAAA) -> T
314 -- where the V depends on which item we are selecting
315 -- It's worth giving one, so that absence info etc is generated
316 -- even if the selector isn't inlined
317
318 strict_sig = mkClosedStrictSig [arg_dmd] topRes
319 arg_dmd | new_tycon = evalDmd
320 | otherwise = mkManyUsedDmd $
321 mkProdDmd [ if name == sel_name then evalDmd else absDmd
322 | sel_name <- sel_names ]
323
324 mkDictSelRhs :: Class
325 -> Int -- 0-indexed selector among (superclasses ++ methods)
326 -> CoreExpr
327 mkDictSelRhs clas val_index
328 = mkLams tyvars (Lam dict_id rhs_body)
329 where
330 tycon = classTyCon clas
331 new_tycon = isNewTyCon tycon
332 [data_con] = tyConDataCons tycon
333 tyvars = dataConUnivTyVars data_con
334 arg_tys = dataConRepArgTys data_con -- Includes the dictionary superclasses
335
336 the_arg_id = getNth arg_ids val_index
337 pred = mkClassPred clas (mkTyVarTys tyvars)
338 dict_id = mkTemplateLocal 1 pred
339 arg_ids = mkTemplateLocalsNum 2 arg_tys
340
341 rhs_body | new_tycon = unwrapNewTypeBody tycon (mkTyVarTys tyvars) (Var dict_id)
342 | otherwise = Case (Var dict_id) dict_id (idType the_arg_id)
343 [(DataAlt data_con, arg_ids, varToCoreExpr the_arg_id)]
344 -- varToCoreExpr needed for equality superclass selectors
345 -- sel a b d = case x of { MkC _ (g:a~b) _ -> CO g }
346
347 dictSelRule :: Int -> Arity -> RuleFun
348 -- Tries to persuade the argument to look like a constructor
349 -- application, using exprIsConApp_maybe, and then selects
350 -- from it
351 -- sel_i t1..tk (D t1..tk op1 ... opm) = opi
352 --
353 dictSelRule val_index n_ty_args _ id_unf _ args
354 | (dict_arg : _) <- drop n_ty_args args
355 , Just (_, _, con_args) <- exprIsConApp_maybe id_unf dict_arg
356 = Just (getNth con_args val_index)
357 | otherwise
358 = Nothing
359
360 {-
361 ************************************************************************
362 * *
363 Data constructors
364 * *
365 ************************************************************************
366 -}
367
368 mkDataConWorkId :: Name -> DataCon -> Id
369 mkDataConWorkId wkr_name data_con
370 | isNewTyCon tycon
371 = mkGlobalId (DataConWrapId data_con) wkr_name nt_wrap_ty nt_work_info
372 | otherwise
373 = mkGlobalId (DataConWorkId data_con) wkr_name alg_wkr_ty wkr_info
374
375 where
376 tycon = dataConTyCon data_con
377
378 ----------- Workers for data types --------------
379 alg_wkr_ty = dataConRepType data_con
380 wkr_arity = dataConRepArity data_con
381 wkr_info = noCafIdInfo
382 `setArityInfo` wkr_arity
383 `setStrictnessInfo` wkr_sig
384 `setUnfoldingInfo` evaldUnfolding -- Record that it's evaluated,
385 -- even if arity = 0
386
387 wkr_sig = mkClosedStrictSig (replicate wkr_arity topDmd) (dataConCPR data_con)
388 -- Note [Data-con worker strictness]
389 -- Notice that we do *not* say the worker is strict
390 -- even if the data constructor is declared strict
391 -- e.g. data T = MkT !(Int,Int)
392 -- Why? Because the *wrapper* is strict (and its unfolding has case
393 -- expressions that do the evals) but the *worker* itself is not.
394 -- If we pretend it is strict then when we see
395 -- case x of y -> $wMkT y
396 -- the simplifier thinks that y is "sure to be evaluated" (because
397 -- $wMkT is strict) and drops the case. No, $wMkT is not strict.
398 --
399 -- When the simplifier sees a pattern
400 -- case e of MkT x -> ...
401 -- it uses the dataConRepStrictness of MkT to mark x as evaluated;
402 -- but that's fine... dataConRepStrictness comes from the data con
403 -- not from the worker Id.
404
405 ----------- Workers for newtypes --------------
406 (nt_tvs, _, nt_arg_tys, _) = dataConSig data_con
407 res_ty_args = mkTyVarTys nt_tvs
408 nt_wrap_ty = dataConUserType data_con
409 nt_work_info = noCafIdInfo -- The NoCaf-ness is set by noCafIdInfo
410 `setArityInfo` 1 -- Arity 1
411 `setInlinePragInfo` alwaysInlinePragma
412 `setUnfoldingInfo` newtype_unf
413 id_arg1 = mkTemplateLocal 1 (head nt_arg_tys)
414 newtype_unf = ASSERT2( isVanillaDataCon data_con &&
415 isSingleton nt_arg_tys, ppr data_con )
416 -- Note [Newtype datacons]
417 mkCompulsoryUnfolding $
418 mkLams nt_tvs $ Lam id_arg1 $
419 wrapNewTypeBody tycon res_ty_args (Var id_arg1)
420
421 dataConCPR :: DataCon -> DmdResult
422 dataConCPR con
423 | isDataTyCon tycon -- Real data types only; that is,
424 -- not unboxed tuples or newtypes
425 , null (dataConExTyVars con) -- No existentials
426 , wkr_arity > 0
427 , wkr_arity <= mAX_CPR_SIZE
428 = if is_prod then vanillaCprProdRes (dataConRepArity con)
429 else cprSumRes (dataConTag con)
430 | otherwise
431 = topRes
432 where
433 is_prod = isProductTyCon tycon
434 tycon = dataConTyCon con
435 wkr_arity = dataConRepArity con
436
437 mAX_CPR_SIZE :: Arity
438 mAX_CPR_SIZE = 10
439 -- We do not treat very big tuples as CPR-ish:
440 -- a) for a start we get into trouble because there aren't
441 -- "enough" unboxed tuple types (a tiresome restriction,
442 -- but hard to fix),
443 -- b) more importantly, big unboxed tuples get returned mainly
444 -- on the stack, and are often then allocated in the heap
445 -- by the caller. So doing CPR for them may in fact make
446 -- things worse.
447
448 {-
449 -------------------------------------------------
450 -- Data constructor representation
451 --
452 -- This is where we decide how to wrap/unwrap the
453 -- constructor fields
454 --
455 --------------------------------------------------
456 -}
457
458 type Unboxer = Var -> UniqSM ([Var], CoreExpr -> CoreExpr)
459 -- Unbox: bind rep vars by decomposing src var
460
461 data Boxer = UnitBox | Boxer (TCvSubst -> UniqSM ([Var], CoreExpr))
462 -- Box: build src arg using these rep vars
463
464 -- | Data Constructor Boxer
465 newtype DataConBoxer = DCB ([Type] -> [Var] -> UniqSM ([Var], [CoreBind]))
466 -- Bind these src-level vars, returning the
467 -- rep-level vars to bind in the pattern
468
469 mkDataConRep :: DynFlags
470 -> FamInstEnvs
471 -> Name
472 -> Maybe [HsImplBang]
473 -- See Note [Bangs on imported data constructors]
474 -> DataCon
475 -> UniqSM DataConRep
476 mkDataConRep dflags fam_envs wrap_name mb_bangs data_con
477 | not wrapper_reqd
478 = return NoDataConRep
479
480 | otherwise
481 = do { wrap_args <- mapM newLocal wrap_arg_tys
482 ; wrap_body <- mk_rep_app (wrap_args `zip` dropList eq_spec unboxers)
483 initial_wrap_app
484
485 ; let wrap_id = mkGlobalId (DataConWrapId data_con) wrap_name wrap_ty wrap_info
486 wrap_info = noCafIdInfo
487 `setArityInfo` wrap_arity
488 -- It's important to specify the arity, so that partial
489 -- applications are treated as values
490 `setInlinePragInfo` alwaysInlinePragma
491 `setUnfoldingInfo` wrap_unf
492 `setStrictnessInfo` wrap_sig
493 -- We need to get the CAF info right here because TidyPgm
494 -- does not tidy the IdInfo of implicit bindings (like the wrapper)
495 -- so it not make sure that the CAF info is sane
496
497 wrap_sig = mkClosedStrictSig wrap_arg_dmds (dataConCPR data_con)
498 wrap_arg_dmds = map mk_dmd arg_ibangs
499 mk_dmd str | isBanged str = evalDmd
500 | otherwise = topDmd
501 -- The Cpr info can be important inside INLINE rhss, where the
502 -- wrapper constructor isn't inlined.
503 -- And the argument strictness can be important too; we
504 -- may not inline a constructor when it is partially applied.
505 -- For example:
506 -- data W = C !Int !Int !Int
507 -- ...(let w = C x in ...(w p q)...)...
508 -- we want to see that w is strict in its two arguments
509
510 wrap_unf = mkInlineUnfolding (Just wrap_arity) wrap_rhs
511 wrap_tvs = (univ_tvs `minusList` map eqSpecTyVar eq_spec) ++ ex_tvs
512 wrap_rhs = mkLams wrap_tvs $
513 mkLams wrap_args $
514 wrapFamInstBody tycon res_ty_args $
515 wrap_body
516
517 ; return (DCR { dcr_wrap_id = wrap_id
518 , dcr_boxer = mk_boxer boxers
519 , dcr_arg_tys = rep_tys
520 , dcr_stricts = rep_strs
521 , dcr_bangs = arg_ibangs }) }
522
523 where
524 (univ_tvs, ex_tvs, eq_spec, theta, orig_arg_tys, _orig_res_ty)
525 = dataConFullSig data_con
526 res_ty_args = substTyVars (mkTvSubstPrs (map eqSpecPair eq_spec)) univ_tvs
527
528 tycon = dataConTyCon data_con -- The representation TyCon (not family)
529 wrap_ty = dataConUserType data_con
530 ev_tys = eqSpecPreds eq_spec ++ theta
531 all_arg_tys = ev_tys ++ orig_arg_tys
532 ev_ibangs = map (const HsLazy) ev_tys
533 orig_bangs = dataConSrcBangs data_con
534
535 wrap_arg_tys = theta ++ orig_arg_tys
536 wrap_arity = length wrap_arg_tys
537 -- The wrap_args are the arguments *other than* the eq_spec
538 -- Because we are going to apply the eq_spec args manually in the
539 -- wrapper
540
541 arg_ibangs =
542 case mb_bangs of
543 Nothing -> zipWith (dataConSrcToImplBang dflags fam_envs)
544 orig_arg_tys orig_bangs
545 Just bangs -> bangs
546
547 (rep_tys_w_strs, wrappers)
548 = unzip (zipWith dataConArgRep all_arg_tys (ev_ibangs ++ arg_ibangs))
549
550 (unboxers, boxers) = unzip wrappers
551 (rep_tys, rep_strs) = unzip (concat rep_tys_w_strs)
552
553 wrapper_reqd = not (isNewTyCon tycon) -- Newtypes have only a worker
554 && (any isBanged (ev_ibangs ++ arg_ibangs)
555 -- Some forcing/unboxing (includes eq_spec)
556 || isFamInstTyCon tycon -- Cast result
557 || (not $ null eq_spec)) -- GADT
558
559 initial_wrap_app = Var (dataConWorkId data_con)
560 `mkTyApps` res_ty_args
561 `mkVarApps` ex_tvs
562 `mkCoApps` map (mkReflCo Nominal . eqSpecType) eq_spec
563
564 mk_boxer :: [Boxer] -> DataConBoxer
565 mk_boxer boxers = DCB (\ ty_args src_vars ->
566 do { let (ex_vars, term_vars) = splitAtList ex_tvs src_vars
567 subst1 = zipTvSubst univ_tvs ty_args
568 subst2 = extendTvSubstList subst1 ex_tvs
569 (mkTyVarTys ex_vars)
570 ; (rep_ids, binds) <- go subst2 boxers term_vars
571 ; return (ex_vars ++ rep_ids, binds) } )
572
573 go _ [] src_vars = ASSERT2( null src_vars, ppr data_con ) return ([], [])
574 go subst (UnitBox : boxers) (src_var : src_vars)
575 = do { (rep_ids2, binds) <- go subst boxers src_vars
576 ; return (src_var : rep_ids2, binds) }
577 go subst (Boxer boxer : boxers) (src_var : src_vars)
578 = do { (rep_ids1, arg) <- boxer subst
579 ; (rep_ids2, binds) <- go subst boxers src_vars
580 ; return (rep_ids1 ++ rep_ids2, NonRec src_var arg : binds) }
581 go _ (_:_) [] = pprPanic "mk_boxer" (ppr data_con)
582
583 mk_rep_app :: [(Id,Unboxer)] -> CoreExpr -> UniqSM CoreExpr
584 mk_rep_app [] con_app
585 = return con_app
586 mk_rep_app ((wrap_arg, unboxer) : prs) con_app
587 = do { (rep_ids, unbox_fn) <- unboxer wrap_arg
588 ; expr <- mk_rep_app prs (mkVarApps con_app rep_ids)
589 ; return (unbox_fn expr) }
590
591 {-
592 Note [Bangs on imported data constructors]
593 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
594
595 We pass Maybe [HsImplBang] to mkDataConRep to make use of HsImplBangs
596 from imported modules.
597
598 - Nothing <=> use HsSrcBangs
599 - Just bangs <=> use HsImplBangs
600
601 For imported types we can't work it all out from the HsSrcBangs,
602 because we want to be very sure to follow what the original module
603 (where the data type was declared) decided, and that depends on what
604 flags were enabled when it was compiled. So we record the decisions in
605 the interface file.
606
607 The HsImplBangs passed are in 1-1 correspondence with the
608 dataConOrigArgTys of the DataCon.
609
610 -}
611
612 -------------------------
613 newLocal :: Type -> UniqSM Var
614 newLocal ty = do { uniq <- getUniqueM
615 ; return (mkSysLocalOrCoVar (fsLit "dt") uniq ty) }
616
617 -- | Unpack/Strictness decisions from source module
618 dataConSrcToImplBang
619 :: DynFlags
620 -> FamInstEnvs
621 -> Type
622 -> HsSrcBang
623 -> HsImplBang
624
625 dataConSrcToImplBang dflags fam_envs arg_ty
626 (HsSrcBang ann unpk NoSrcStrict)
627 | xopt LangExt.StrictData dflags -- StrictData => strict field
628 = dataConSrcToImplBang dflags fam_envs arg_ty
629 (HsSrcBang ann unpk SrcStrict)
630 | otherwise -- no StrictData => lazy field
631 = HsLazy
632
633 dataConSrcToImplBang _ _ _ (HsSrcBang _ _ SrcLazy)
634 = HsLazy
635
636 dataConSrcToImplBang dflags fam_envs arg_ty
637 (HsSrcBang _ unpk_prag SrcStrict)
638 | not (gopt Opt_OmitInterfacePragmas dflags) -- Don't unpack if -fomit-iface-pragmas
639 -- Don't unpack if we aren't optimising; rather arbitrarily,
640 -- we use -fomit-iface-pragmas as the indication
641 , let mb_co = topNormaliseType_maybe fam_envs arg_ty
642 -- Unwrap type families and newtypes
643 arg_ty' = case mb_co of { Just (_,ty) -> ty; Nothing -> arg_ty }
644 , isUnpackableType dflags fam_envs arg_ty'
645 , (rep_tys, _) <- dataConArgUnpack arg_ty'
646 , case unpk_prag of
647 NoSrcUnpack ->
648 gopt Opt_UnboxStrictFields dflags
649 || (gopt Opt_UnboxSmallStrictFields dflags
650 && length rep_tys <= 1) -- See Note [Unpack one-wide fields]
651 srcUnpack -> isSrcUnpacked srcUnpack
652 = case mb_co of
653 Nothing -> HsUnpack Nothing
654 Just (co,_) -> HsUnpack (Just co)
655
656 | otherwise -- Record the strict-but-no-unpack decision
657 = HsStrict
658
659
660 -- | Wrappers/Workers and representation following Unpack/Strictness
661 -- decisions
662 dataConArgRep
663 :: Type
664 -> HsImplBang
665 -> ([(Type,StrictnessMark)] -- Rep types
666 ,(Unboxer,Boxer))
667
668 dataConArgRep arg_ty HsLazy
669 = ([(arg_ty, NotMarkedStrict)], (unitUnboxer, unitBoxer))
670
671 dataConArgRep arg_ty HsStrict
672 = ([(arg_ty, MarkedStrict)], (seqUnboxer, unitBoxer))
673
674 dataConArgRep arg_ty (HsUnpack Nothing)
675 | (rep_tys, wrappers) <- dataConArgUnpack arg_ty
676 = (rep_tys, wrappers)
677
678 dataConArgRep _ (HsUnpack (Just co))
679 | let co_rep_ty = pSnd (coercionKind co)
680 , (rep_tys, wrappers) <- dataConArgUnpack co_rep_ty
681 = (rep_tys, wrapCo co co_rep_ty wrappers)
682
683
684 -------------------------
685 wrapCo :: Coercion -> Type -> (Unboxer, Boxer) -> (Unboxer, Boxer)
686 wrapCo co rep_ty (unbox_rep, box_rep) -- co :: arg_ty ~ rep_ty
687 = (unboxer, boxer)
688 where
689 unboxer arg_id = do { rep_id <- newLocal rep_ty
690 ; (rep_ids, rep_fn) <- unbox_rep rep_id
691 ; let co_bind = NonRec rep_id (Var arg_id `Cast` co)
692 ; return (rep_ids, Let co_bind . rep_fn) }
693 boxer = Boxer $ \ subst ->
694 do { (rep_ids, rep_expr)
695 <- case box_rep of
696 UnitBox -> do { rep_id <- newLocal (TcType.substTy subst rep_ty)
697 ; return ([rep_id], Var rep_id) }
698 Boxer boxer -> boxer subst
699 ; let sco = substCoUnchecked subst co
700 ; return (rep_ids, rep_expr `Cast` mkSymCo sco) }
701
702 ------------------------
703 seqUnboxer :: Unboxer
704 seqUnboxer v = return ([v], \e -> Case (Var v) v (exprType e) [(DEFAULT, [], e)])
705
706 unitUnboxer :: Unboxer
707 unitUnboxer v = return ([v], \e -> e)
708
709 unitBoxer :: Boxer
710 unitBoxer = UnitBox
711
712 -------------------------
713 dataConArgUnpack
714 :: Type
715 -> ( [(Type, StrictnessMark)] -- Rep types
716 , (Unboxer, Boxer) )
717
718 dataConArgUnpack arg_ty
719 | Just (tc, tc_args) <- splitTyConApp_maybe arg_ty
720 , Just con <- tyConSingleAlgDataCon_maybe tc
721 -- NB: check for an *algebraic* data type
722 -- A recursive newtype might mean that
723 -- 'arg_ty' is a newtype
724 , let rep_tys = dataConInstArgTys con tc_args
725 = ASSERT( isVanillaDataCon con )
726 ( rep_tys `zip` dataConRepStrictness con
727 ,( \ arg_id ->
728 do { rep_ids <- mapM newLocal rep_tys
729 ; let unbox_fn body
730 = Case (Var arg_id) arg_id (exprType body)
731 [(DataAlt con, rep_ids, body)]
732 ; return (rep_ids, unbox_fn) }
733 , Boxer $ \ subst ->
734 do { rep_ids <- mapM (newLocal . TcType.substTyUnchecked subst) rep_tys
735 ; return (rep_ids, Var (dataConWorkId con)
736 `mkTyApps` (substTysUnchecked subst tc_args)
737 `mkVarApps` rep_ids ) } ) )
738 | otherwise
739 = pprPanic "dataConArgUnpack" (ppr arg_ty)
740 -- An interface file specified Unpacked, but we couldn't unpack it
741
742 isUnpackableType :: DynFlags -> FamInstEnvs -> Type -> Bool
743 -- True if we can unpack the UNPACK the argument type
744 -- See Note [Recursive unboxing]
745 -- We look "deeply" inside rather than relying on the DataCons
746 -- we encounter on the way, because otherwise we might well
747 -- end up relying on ourselves!
748 isUnpackableType dflags fam_envs ty
749 | Just (tc, _) <- splitTyConApp_maybe ty
750 , Just con <- tyConSingleAlgDataCon_maybe tc
751 , isVanillaDataCon con
752 = ok_con_args (unitNameSet (getName tc)) con
753 | otherwise
754 = False
755 where
756 ok_arg tcs (ty, bang) = not (attempt_unpack bang) || ok_ty tcs norm_ty
757 where
758 norm_ty = topNormaliseType fam_envs ty
759 ok_ty tcs ty
760 | Just (tc, _) <- splitTyConApp_maybe ty
761 , let tc_name = getName tc
762 = not (tc_name `elemNameSet` tcs)
763 && case tyConSingleAlgDataCon_maybe tc of
764 Just con | isVanillaDataCon con
765 -> ok_con_args (tcs `extendNameSet` getName tc) con
766 _ -> True
767 | otherwise
768 = True
769
770 ok_con_args tcs con
771 = all (ok_arg tcs) (dataConOrigArgTys con `zip` dataConSrcBangs con)
772 -- NB: dataConSrcBangs gives the *user* request;
773 -- We'd get a black hole if we used dataConImplBangs
774
775 attempt_unpack (HsSrcBang _ SrcUnpack NoSrcStrict)
776 = xopt LangExt.StrictData dflags
777 attempt_unpack (HsSrcBang _ SrcUnpack SrcStrict)
778 = True
779 attempt_unpack (HsSrcBang _ NoSrcUnpack SrcStrict)
780 = True -- Be conservative
781 attempt_unpack (HsSrcBang _ NoSrcUnpack NoSrcStrict)
782 = xopt LangExt.StrictData dflags -- Be conservative
783 attempt_unpack _ = False
784
785 {-
786 Note [Unpack one-wide fields]
787 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
788 The flag UnboxSmallStrictFields ensures that any field that can
789 (safely) be unboxed to a word-sized unboxed field, should be so unboxed.
790 For example:
791
792 data A = A Int#
793 newtype B = B A
794 data C = C !B
795 data D = D !C
796 data E = E !()
797 data F = F !D
798 data G = G !F !F
799
800 All of these should have an Int# as their representation, except
801 G which should have two Int#s.
802
803 However
804
805 data T = T !(S Int)
806 data S = S !a
807
808 Here we can represent T with an Int#.
809
810 Note [Recursive unboxing]
811 ~~~~~~~~~~~~~~~~~~~~~~~~~
812 Consider
813 data R = MkR {-# UNPACK #-} !S Int
814 data S = MkS {-# UNPACK #-} !Int
815 The representation arguments of MkR are the *representation* arguments
816 of S (plus Int); the rep args of MkS are Int#. This is all fine.
817
818 But be careful not to try to unbox this!
819 data T = MkT {-# UNPACK #-} !T Int
820 Because then we'd get an infinite number of arguments.
821
822 Here is a more complicated case:
823 data S = MkS {-# UNPACK #-} !T Int
824 data T = MkT {-# UNPACK #-} !S Int
825 Each of S and T must decide independently whether to unpack
826 and they had better not both say yes. So they must both say no.
827
828 Also behave conservatively when there is no UNPACK pragma
829 data T = MkS !T Int
830 with -funbox-strict-fields or -funbox-small-strict-fields
831 we need to behave as if there was an UNPACK pragma there.
832
833 But it's the *argument* type that matters. This is fine:
834 data S = MkS S !Int
835 because Int is non-recursive.
836
837 ************************************************************************
838 * *
839 Wrapping and unwrapping newtypes and type families
840 * *
841 ************************************************************************
842 -}
843
844 wrapNewTypeBody :: TyCon -> [Type] -> CoreExpr -> CoreExpr
845 -- The wrapper for the data constructor for a newtype looks like this:
846 -- newtype T a = MkT (a,Int)
847 -- MkT :: forall a. (a,Int) -> T a
848 -- MkT = /\a. \(x:(a,Int)). x `cast` sym (CoT a)
849 -- where CoT is the coercion TyCon associated with the newtype
850 --
851 -- The call (wrapNewTypeBody T [a] e) returns the
852 -- body of the wrapper, namely
853 -- e `cast` (CoT [a])
854 --
855 -- If a coercion constructor is provided in the newtype, then we use
856 -- it, otherwise the wrap/unwrap are both no-ops
857 --
858 -- If the we are dealing with a newtype *instance*, we have a second coercion
859 -- identifying the family instance with the constructor of the newtype
860 -- instance. This coercion is applied in any case (ie, composed with the
861 -- coercion constructor of the newtype or applied by itself).
862
863 wrapNewTypeBody tycon args result_expr
864 = ASSERT( isNewTyCon tycon )
865 wrapFamInstBody tycon args $
866 mkCast result_expr (mkSymCo co)
867 where
868 co = mkUnbranchedAxInstCo Representational (newTyConCo tycon) args []
869
870 -- When unwrapping, we do *not* apply any family coercion, because this will
871 -- be done via a CoPat by the type checker. We have to do it this way as
872 -- computing the right type arguments for the coercion requires more than just
873 -- a spliting operation (cf, TcPat.tcConPat).
874
875 unwrapNewTypeBody :: TyCon -> [Type] -> CoreExpr -> CoreExpr
876 unwrapNewTypeBody tycon args result_expr
877 = ASSERT( isNewTyCon tycon )
878 mkCast result_expr (mkUnbranchedAxInstCo Representational (newTyConCo tycon) args [])
879
880 -- If the type constructor is a representation type of a data instance, wrap
881 -- the expression into a cast adjusting the expression type, which is an
882 -- instance of the representation type, to the corresponding instance of the
883 -- family instance type.
884 -- See Note [Wrappers for data instance tycons]
885 wrapFamInstBody :: TyCon -> [Type] -> CoreExpr -> CoreExpr
886 wrapFamInstBody tycon args body
887 | Just co_con <- tyConFamilyCoercion_maybe tycon
888 = mkCast body (mkSymCo (mkUnbranchedAxInstCo Representational co_con args []))
889 | otherwise
890 = body
891
892 -- Same as `wrapFamInstBody`, but for type family instances, which are
893 -- represented by a `CoAxiom`, and not a `TyCon`
894 wrapTypeFamInstBody :: CoAxiom br -> Int -> [Type] -> [Coercion]
895 -> CoreExpr -> CoreExpr
896 wrapTypeFamInstBody axiom ind args cos body
897 = mkCast body (mkSymCo (mkAxInstCo Representational axiom ind args cos))
898
899 wrapTypeUnbranchedFamInstBody :: CoAxiom Unbranched -> [Type] -> [Coercion]
900 -> CoreExpr -> CoreExpr
901 wrapTypeUnbranchedFamInstBody axiom
902 = wrapTypeFamInstBody axiom 0
903
904 unwrapFamInstScrut :: TyCon -> [Type] -> CoreExpr -> CoreExpr
905 unwrapFamInstScrut tycon args scrut
906 | Just co_con <- tyConFamilyCoercion_maybe tycon
907 = mkCast scrut (mkUnbranchedAxInstCo Representational co_con args []) -- data instances only
908 | otherwise
909 = scrut
910
911 unwrapTypeFamInstScrut :: CoAxiom br -> Int -> [Type] -> [Coercion]
912 -> CoreExpr -> CoreExpr
913 unwrapTypeFamInstScrut axiom ind args cos scrut
914 = mkCast scrut (mkAxInstCo Representational axiom ind args cos)
915
916 unwrapTypeUnbranchedFamInstScrut :: CoAxiom Unbranched -> [Type] -> [Coercion]
917 -> CoreExpr -> CoreExpr
918 unwrapTypeUnbranchedFamInstScrut axiom
919 = unwrapTypeFamInstScrut axiom 0
920
921 {-
922 ************************************************************************
923 * *
924 \subsection{Primitive operations}
925 * *
926 ************************************************************************
927 -}
928
929 mkPrimOpId :: PrimOp -> Id
930 mkPrimOpId prim_op
931 = id
932 where
933 (tyvars,arg_tys,res_ty, arity, strict_sig) = primOpSig prim_op
934 ty = mkSpecForAllTys tyvars (mkFunTys arg_tys res_ty)
935 name = mkWiredInName gHC_PRIM (primOpOcc prim_op)
936 (mkPrimOpIdUnique (primOpTag prim_op))
937 (AnId id) UserSyntax
938 id = mkGlobalId (PrimOpId prim_op) name ty info
939
940 info = noCafIdInfo
941 `setRuleInfo` mkRuleInfo (maybeToList $ primOpRules name prim_op)
942 `setArityInfo` arity
943 `setStrictnessInfo` strict_sig
944 `setInlinePragInfo` neverInlinePragma
945 -- We give PrimOps a NOINLINE pragma so that we don't
946 -- get silly warnings from Desugar.dsRule (the inline_shadows_rule
947 -- test) about a RULE conflicting with a possible inlining
948 -- cf Trac #7287
949
950 -- For each ccall we manufacture a separate CCallOpId, giving it
951 -- a fresh unique, a type that is correct for this particular ccall,
952 -- and a CCall structure that gives the correct details about calling
953 -- convention etc.
954 --
955 -- The *name* of this Id is a local name whose OccName gives the full
956 -- details of the ccall, type and all. This means that the interface
957 -- file reader can reconstruct a suitable Id
958
959 mkFCallId :: DynFlags -> Unique -> ForeignCall -> Type -> Id
960 mkFCallId dflags uniq fcall ty
961 = ASSERT( isEmptyVarSet (tyCoVarsOfType ty) )
962 -- A CCallOpId should have no free type variables;
963 -- when doing substitutions won't substitute over it
964 mkGlobalId (FCallId fcall) name ty info
965 where
966 occ_str = showSDoc dflags (braces (ppr fcall <+> ppr ty))
967 -- The "occurrence name" of a ccall is the full info about the
968 -- ccall; it is encoded, but may have embedded spaces etc!
969
970 name = mkFCallName uniq occ_str
971
972 info = noCafIdInfo
973 `setArityInfo` arity
974 `setStrictnessInfo` strict_sig
975
976 (bndrs, _) = tcSplitPiTys ty
977 arity = count isAnonTyBinder bndrs
978 strict_sig = mkClosedStrictSig (replicate arity topDmd) topRes
979 -- the call does not claim to be strict in its arguments, since they
980 -- may be lifted (foreign import prim) and the called code doesn't
981 -- necessarily force them. See Trac #11076.
982 {-
983 ************************************************************************
984 * *
985 \subsection{DictFuns and default methods}
986 * *
987 ************************************************************************
988
989 Note [Dict funs and default methods]
990 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
991 Dict funs and default methods are *not* ImplicitIds. Their definition
992 involves user-written code, so we can't figure out their strictness etc
993 based on fixed info, as we can for constructors and record selectors (say).
994
995 NB: See also Note [Exported LocalIds] in Id
996 -}
997
998 mkDictFunId :: Name -- Name to use for the dict fun;
999 -> [TyVar]
1000 -> ThetaType
1001 -> Class
1002 -> [Type]
1003 -> Id
1004 -- Implements the DFun Superclass Invariant (see TcInstDcls)
1005 -- See Note [Dict funs and default methods]
1006
1007 mkDictFunId dfun_name tvs theta clas tys
1008 = mkExportedLocalId (DFunId is_nt)
1009 dfun_name
1010 dfun_ty
1011 where
1012 is_nt = isNewTyCon (classTyCon clas)
1013 dfun_ty = mkDictFunTy tvs theta clas tys
1014
1015 mkDictFunTy :: [TyVar] -> ThetaType -> Class -> [Type] -> Type
1016 mkDictFunTy tvs theta clas tys
1017 = mkSpecSigmaTy tvs theta (mkClassPred clas tys)
1018
1019 {-
1020 ************************************************************************
1021 * *
1022 \subsection{Un-definable}
1023 * *
1024 ************************************************************************
1025
1026 These Ids can't be defined in Haskell. They could be defined in
1027 unfoldings in the wired-in GHC.Prim interface file, but we'd have to
1028 ensure that they were definitely, definitely inlined, because there is
1029 no curried identifier for them. That's what mkCompulsoryUnfolding
1030 does. If we had a way to get a compulsory unfolding from an interface
1031 file, we could do that, but we don't right now.
1032
1033 unsafeCoerce# isn't so much a PrimOp as a phantom identifier, that
1034 just gets expanded into a type coercion wherever it occurs. Hence we
1035 add it as a built-in Id with an unfolding here.
1036
1037 The type variables we use here are "open" type variables: this means
1038 they can unify with both unlifted and lifted types. Hence we provide
1039 another gun with which to shoot yourself in the foot.
1040 -}
1041
1042 lazyIdName, unsafeCoerceName, nullAddrName, seqName,
1043 realWorldName, voidPrimIdName, coercionTokenName,
1044 magicDictName, coerceName, proxyName, dollarName, oneShotName,
1045 runRWName, noinlineIdName :: Name
1046 unsafeCoerceName = mkWiredInIdName gHC_PRIM (fsLit "unsafeCoerce#") unsafeCoerceIdKey unsafeCoerceId
1047 nullAddrName = mkWiredInIdName gHC_PRIM (fsLit "nullAddr#") nullAddrIdKey nullAddrId
1048 seqName = mkWiredInIdName gHC_PRIM (fsLit "seq") seqIdKey seqId
1049 realWorldName = mkWiredInIdName gHC_PRIM (fsLit "realWorld#") realWorldPrimIdKey realWorldPrimId
1050 voidPrimIdName = mkWiredInIdName gHC_PRIM (fsLit "void#") voidPrimIdKey voidPrimId
1051 lazyIdName = mkWiredInIdName gHC_MAGIC (fsLit "lazy") lazyIdKey lazyId
1052 coercionTokenName = mkWiredInIdName gHC_PRIM (fsLit "coercionToken#") coercionTokenIdKey coercionTokenId
1053 magicDictName = mkWiredInIdName gHC_PRIM (fsLit "magicDict") magicDictKey magicDictId
1054 coerceName = mkWiredInIdName gHC_PRIM (fsLit "coerce") coerceKey coerceId
1055 proxyName = mkWiredInIdName gHC_PRIM (fsLit "proxy#") proxyHashKey proxyHashId
1056 dollarName = mkWiredInIdName gHC_BASE (fsLit "$") dollarIdKey dollarId
1057 oneShotName = mkWiredInIdName gHC_MAGIC (fsLit "oneShot") oneShotKey oneShotId
1058 runRWName = mkWiredInIdName gHC_MAGIC (fsLit "runRW#") runRWKey runRWId
1059 noinlineIdName = mkWiredInIdName gHC_MAGIC (fsLit "noinline") noinlineIdKey noinlineId
1060
1061 dollarId :: Id -- Note [dollarId magic]
1062 dollarId = pcMiscPrelId dollarName ty
1063 (noCafIdInfo `setUnfoldingInfo` unf)
1064 where
1065 fun_ty = mkFunTy alphaTy openBetaTy
1066 ty = mkSpecForAllTys [runtimeRep2TyVar, alphaTyVar, openBetaTyVar] $
1067 mkFunTy fun_ty fun_ty
1068 unf = mkInlineUnfolding (Just 2) rhs
1069 [f,x] = mkTemplateLocals [fun_ty, alphaTy]
1070 rhs = mkLams [runtimeRep2TyVar, alphaTyVar, openBetaTyVar, f, x] $
1071 App (Var f) (Var x)
1072
1073 ------------------------------------------------
1074 proxyHashId :: Id
1075 proxyHashId
1076 = pcMiscPrelId proxyName ty
1077 (noCafIdInfo `setUnfoldingInfo` evaldUnfolding) -- Note [evaldUnfoldings]
1078 where
1079 -- proxy# :: forall k (a:k). Proxy# k a
1080 bndrs = mkTemplateKiTyVars [liftedTypeKind] (\ks -> ks)
1081 [k,t] = mkTyVarTys bndrs
1082 ty = mkSpecForAllTys bndrs (mkProxyPrimTy k t)
1083
1084 ------------------------------------------------
1085 unsafeCoerceId :: Id
1086 unsafeCoerceId
1087 = pcMiscPrelId unsafeCoerceName ty info
1088 where
1089 info = noCafIdInfo `setInlinePragInfo` alwaysInlinePragma
1090 `setUnfoldingInfo` mkCompulsoryUnfolding rhs
1091
1092 -- unsafeCoerce# :: forall (r1 :: RuntimeRep) (r2 :: RuntimeRep)
1093 -- (a :: TYPE r1) (b :: TYPE r2).
1094 -- a -> b
1095 bndrs = mkTemplateKiTyVars [runtimeRepTy, runtimeRepTy]
1096 (\ks -> map tYPE ks)
1097
1098 [_, _, a, b] = mkTyVarTys bndrs
1099
1100 ty = mkSpecForAllTys bndrs (mkFunTy a b)
1101
1102 [x] = mkTemplateLocals [a]
1103 rhs = mkLams (bndrs ++ [x]) $
1104 Cast (Var x) (mkUnsafeCo Representational a b)
1105
1106 ------------------------------------------------
1107 nullAddrId :: Id
1108 -- nullAddr# :: Addr#
1109 -- The reason is is here is because we don't provide
1110 -- a way to write this literal in Haskell.
1111 nullAddrId = pcMiscPrelId nullAddrName addrPrimTy info
1112 where
1113 info = noCafIdInfo `setInlinePragInfo` alwaysInlinePragma
1114 `setUnfoldingInfo` mkCompulsoryUnfolding (Lit nullAddrLit)
1115
1116 ------------------------------------------------
1117 seqId :: Id -- See Note [seqId magic]
1118 seqId = pcMiscPrelId seqName ty info
1119 where
1120 info = noCafIdInfo `setInlinePragInfo` inline_prag
1121 `setUnfoldingInfo` mkCompulsoryUnfolding rhs
1122 `setRuleInfo` mkRuleInfo [seq_cast_rule]
1123
1124 inline_prag
1125 = alwaysInlinePragma `setInlinePragmaActivation` ActiveAfter "0" 0
1126 -- Make 'seq' not inline-always, so that simpleOptExpr
1127 -- (see CoreSubst.simple_app) won't inline 'seq' on the
1128 -- LHS of rules. That way we can have rules for 'seq';
1129 -- see Note [seqId magic]
1130
1131 ty = mkSpecForAllTys [alphaTyVar,betaTyVar]
1132 (mkFunTy alphaTy (mkFunTy betaTy betaTy))
1133
1134 [x,y] = mkTemplateLocals [alphaTy, betaTy]
1135 rhs = mkLams [alphaTyVar,betaTyVar,x,y] (Case (Var x) x betaTy [(DEFAULT, [], Var y)])
1136
1137 -- See Note [Built-in RULES for seq]
1138 -- NB: ru_nargs = 3, not 4, to match the code in
1139 -- Simplify.rebuildCase which tries to apply this rule
1140 seq_cast_rule = BuiltinRule { ru_name = fsLit "seq of cast"
1141 , ru_fn = seqName
1142 , ru_nargs = 3
1143 , ru_try = match_seq_of_cast }
1144
1145 match_seq_of_cast :: RuleFun
1146 -- See Note [Built-in RULES for seq]
1147 match_seq_of_cast _ _ _ [Type _, Type res_ty, Cast scrut co]
1148 = Just (fun `App` scrut)
1149 where
1150 fun = Lam x $ Lam y $
1151 Case (Var x) x res_ty [(DEFAULT,[],Var y)]
1152 -- Generate a Case directly, not a call to seq, which
1153 -- might be ill-kinded if res_ty is unboxed
1154 [x,y] = mkTemplateLocals [scrut_ty, res_ty]
1155 scrut_ty = pFst (coercionKind co)
1156
1157 match_seq_of_cast _ _ _ _ = Nothing
1158
1159 ------------------------------------------------
1160 lazyId :: Id -- See Note [lazyId magic]
1161 lazyId = pcMiscPrelId lazyIdName ty info
1162 where
1163 info = noCafIdInfo
1164 ty = mkSpecForAllTys [alphaTyVar] (mkFunTy alphaTy alphaTy)
1165
1166 noinlineId :: Id -- See Note [noinlineId magic]
1167 noinlineId = pcMiscPrelId noinlineIdName ty info
1168 where
1169 info = noCafIdInfo
1170 ty = mkSpecForAllTys [alphaTyVar] (mkFunTy alphaTy alphaTy)
1171
1172 oneShotId :: Id -- See Note [The oneShot function]
1173 oneShotId = pcMiscPrelId oneShotName ty info
1174 where
1175 info = noCafIdInfo `setInlinePragInfo` alwaysInlinePragma
1176 `setUnfoldingInfo` mkCompulsoryUnfolding rhs
1177 ty = mkSpecForAllTys [ runtimeRep1TyVar, runtimeRep2TyVar
1178 , openAlphaTyVar, openBetaTyVar ]
1179 (mkFunTy fun_ty fun_ty)
1180 fun_ty = mkFunTy openAlphaTy openBetaTy
1181 [body, x] = mkTemplateLocals [fun_ty, openAlphaTy]
1182 x' = setOneShotLambda x
1183 rhs = mkLams [ runtimeRep1TyVar, runtimeRep2TyVar
1184 , openAlphaTyVar, openBetaTyVar
1185 , body, x'] $
1186 Var body `App` Var x
1187
1188 runRWId :: Id -- See Note [runRW magic] in this module
1189 runRWId = pcMiscPrelId runRWName ty info
1190 where
1191 info = noCafIdInfo `setInlinePragInfo` neverInlinePragma
1192 `setStrictnessInfo` strict_sig
1193 `setArityInfo` 1
1194 strict_sig = mkClosedStrictSig [strictApply1Dmd] topRes
1195 -- Important to express its strictness,
1196 -- since it is not inlined until CorePrep
1197 -- Also see Note [runRW arg] in CorePrep
1198
1199 -- State# RealWorld
1200 stateRW = mkTyConApp statePrimTyCon [realWorldTy]
1201 -- (# State# RealWorld, o #)
1202 ret_ty = mkTupleTy Unboxed [stateRW, openAlphaTy]
1203 -- State# RealWorld -> (# State# RealWorld, o #)
1204 arg_ty = stateRW `mkFunTy` ret_ty
1205 -- (State# RealWorld -> (# State# RealWorld, o #))
1206 -- -> (# State# RealWorld, o #)
1207 ty = mkSpecForAllTys [runtimeRep1TyVar, openAlphaTyVar] $
1208 arg_ty `mkFunTy` ret_ty
1209
1210 --------------------------------------------------------------------------------
1211 magicDictId :: Id -- See Note [magicDictId magic]
1212 magicDictId = pcMiscPrelId magicDictName ty info
1213 where
1214 info = noCafIdInfo `setInlinePragInfo` neverInlinePragma
1215 ty = mkSpecForAllTys [alphaTyVar] alphaTy
1216
1217 --------------------------------------------------------------------------------
1218
1219 coerceId :: Id
1220 coerceId = pcMiscPrelId coerceName ty info
1221 where
1222 info = noCafIdInfo `setInlinePragInfo` alwaysInlinePragma
1223 `setUnfoldingInfo` mkCompulsoryUnfolding rhs
1224 eqRTy = mkTyConApp coercibleTyCon [ liftedTypeKind
1225 , alphaTy, betaTy ]
1226 eqRPrimTy = mkTyConApp eqReprPrimTyCon [ liftedTypeKind
1227 , liftedTypeKind
1228 , alphaTy, betaTy ]
1229 ty = mkSpecForAllTys [alphaTyVar, betaTyVar] $
1230 mkFunTys [eqRTy, alphaTy] betaTy
1231
1232 [eqR,x,eq] = mkTemplateLocals [eqRTy, alphaTy, eqRPrimTy]
1233 rhs = mkLams [alphaTyVar, betaTyVar, eqR, x] $
1234 mkWildCase (Var eqR) eqRTy betaTy $
1235 [(DataAlt coercibleDataCon, [eq], Cast (Var x) (mkCoVarCo eq))]
1236
1237 {-
1238 Note [dollarId magic]
1239 ~~~~~~~~~~~~~~~~~~~~~
1240 The only reason that ($) is wired in is so that its type can be
1241 forall (a:*, b:Open). (a->b) -> a -> b
1242 That is, the return type can be unboxed. E.g. this is OK
1243 foo $ True where foo :: Bool -> Int#
1244 because ($) doesn't inspect or move the result of the call to foo.
1245 See Trac #8739.
1246
1247 There is a special typing rule for ($) in TcExpr, so the type of ($)
1248 isn't looked at there, BUT Lint subsequently (and rightly) complains
1249 if sees ($) applied to Int# (say), unless we give it a wired-in type
1250 as we do here.
1251
1252 Note [Unsafe coerce magic]
1253 ~~~~~~~~~~~~~~~~~~~~~~~~~~
1254 We define a *primitive*
1255 GHC.Prim.unsafeCoerce#
1256 and then in the base library we define the ordinary function
1257 Unsafe.Coerce.unsafeCoerce :: forall (a:*) (b:*). a -> b
1258 unsafeCoerce x = unsafeCoerce# x
1259
1260 Notice that unsafeCoerce has a civilized (albeit still dangerous)
1261 polymorphic type, whose type args have kind *. So you can't use it on
1262 unboxed values (unsafeCoerce 3#).
1263
1264 In contrast unsafeCoerce# is even more dangerous because you *can* use
1265 it on unboxed things, (unsafeCoerce# 3#) :: Int. Its type is
1266 forall (a:OpenKind) (b:OpenKind). a -> b
1267
1268 Note [seqId magic]
1269 ~~~~~~~~~~~~~~~~~~
1270 'GHC.Prim.seq' is special in several ways.
1271
1272 a) In source Haskell its second arg can have an unboxed type
1273 x `seq` (v +# w)
1274 But see Note [Typing rule for seq] in TcExpr, which
1275 explains why we give seq itself an ordinary type
1276 seq :: forall a b. a -> b -> b
1277 and treat it as a language construct from a typing point of view.
1278
1279 b) Its fixity is set in LoadIface.ghcPrimIface
1280
1281 c) It has quite a bit of desugaring magic.
1282 See DsUtils.hs Note [Desugaring seq (1)] and (2) and (3)
1283
1284 d) There is some special rule handing: Note [User-defined RULES for seq]
1285
1286 Note [User-defined RULES for seq]
1287 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1288 Roman found situations where he had
1289 case (f n) of _ -> e
1290 where he knew that f (which was strict in n) would terminate if n did.
1291 Notice that the result of (f n) is discarded. So it makes sense to
1292 transform to
1293 case n of _ -> e
1294
1295 Rather than attempt some general analysis to support this, I've added
1296 enough support that you can do this using a rewrite rule:
1297
1298 RULE "f/seq" forall n. seq (f n) = seq n
1299
1300 You write that rule. When GHC sees a case expression that discards
1301 its result, it mentally transforms it to a call to 'seq' and looks for
1302 a RULE. (This is done in Simplify.rebuildCase.) As usual, the
1303 correctness of the rule is up to you.
1304
1305 VERY IMPORTANT: to make this work, we give the RULE an arity of 1, not 2.
1306 If we wrote
1307 RULE "f/seq" forall n e. seq (f n) e = seq n e
1308 with rule arity 2, then two bad things would happen:
1309
1310 - The magical desugaring done in Note [seqId magic] item (c)
1311 for saturated application of 'seq' would turn the LHS into
1312 a case expression!
1313
1314 - The code in Simplify.rebuildCase would need to actually supply
1315 the value argument, which turns out to be awkward.
1316
1317 Note [Built-in RULES for seq]
1318 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1319 We also have the following built-in rule for seq
1320
1321 seq (x `cast` co) y = seq x y
1322
1323 This eliminates unnecessary casts and also allows other seq rules to
1324 match more often. Notably,
1325
1326 seq (f x `cast` co) y --> seq (f x) y
1327
1328 and now a user-defined rule for seq (see Note [User-defined RULES for seq])
1329 may fire.
1330
1331
1332 Note [lazyId magic]
1333 ~~~~~~~~~~~~~~~~~~~
1334 lazy :: forall a?. a? -> a? (i.e. works for unboxed types too)
1335
1336 'lazy' is used to make sure that a sub-expression, and its free variables,
1337 are truly used call-by-need, with no code motion. Key examples:
1338
1339 * pseq: pseq a b = a `seq` lazy b
1340 We want to make sure that the free vars of 'b' are not evaluated
1341 before 'a', even though the expression is plainly strict in 'b'.
1342
1343 * catch: catch a b = catch# (lazy a) b
1344 Again, it's clear that 'a' will be evaluated strictly (and indeed
1345 applied to a state token) but we want to make sure that any exceptions
1346 arising from the evaluation of 'a' are caught by the catch (see
1347 Trac #11555).
1348
1349 Implementing 'lazy' is a bit tricky:
1350
1351 * It must not have a strictness signature: by being a built-in Id,
1352 all the info about lazyId comes from here, not from GHC.Base.hi.
1353 This is important, because the strictness analyser will spot it as
1354 strict!
1355
1356 * It must not have an unfolding: it gets "inlined" by a HACK in
1357 CorePrep. It's very important to do this inlining *after* unfoldings
1358 are exposed in the interface file. Otherwise, the unfolding for
1359 (say) pseq in the interface file will not mention 'lazy', so if we
1360 inline 'pseq' we'll totally miss the very thing that 'lazy' was
1361 there for in the first place. See Trac #3259 for a real world
1362 example.
1363
1364 * Suppose CorePrep sees (catch# (lazy e) b). At all costs we must
1365 avoid using call by value here:
1366 case e of r -> catch# r b
1367 Avoiding that is the whole point of 'lazy'. So in CorePrep (which
1368 generate the 'case' expression for a call-by-value call) we must
1369 spot the 'lazy' on the arg (in CorePrep.cpeApp), and build a 'let'
1370 instead.
1371
1372 * lazyId is defined in GHC.Base, so we don't *have* to inline it. If it
1373 appears un-applied, we'll end up just calling it.
1374
1375 Note [noinlineId magic]
1376 ~~~~~~~~~~~~~~~~~~~~~~~
1377 noinline :: forall a. a -> a
1378
1379 'noinline' is used to make sure that a function f is never inlined,
1380 e.g., as in 'noinline f x'. Ordinarily, the identity function with NOINLINE
1381 could be used to achieve this effect; however, this has the unfortunate
1382 result of leaving a (useless) call to noinline at runtime. So we have
1383 a little bit of magic to optimize away 'noinline' after we are done
1384 running the simplifier.
1385
1386 'noinline' needs to be wired-in because it gets inserted automatically
1387 when we serialize an expression to the interface format, and we DON'T
1388 want use its fingerprints.
1389
1390
1391 Note [runRW magic]
1392 ~~~~~~~~~~~~~~~~~~
1393 Some definitions, for instance @runST@, must have careful control over float out
1394 of the bindings in their body. Consider this use of @runST@,
1395
1396 f x = runST ( \ s -> let (a, s') = newArray# 100 [] s
1397 (_, s'') = fill_in_array_or_something a x s'
1398 in freezeArray# a s'' )
1399
1400 If we inline @runST@, we'll get:
1401
1402 f x = let (a, s') = newArray# 100 [] realWorld#{-NB-}
1403 (_, s'') = fill_in_array_or_something a x s'
1404 in freezeArray# a s''
1405
1406 And now if we allow the @newArray#@ binding to float out to become a CAF,
1407 we end up with a result that is totally and utterly wrong:
1408
1409 f = let (a, s') = newArray# 100 [] realWorld#{-NB-} -- YIKES!!!
1410 in \ x ->
1411 let (_, s'') = fill_in_array_or_something a x s'
1412 in freezeArray# a s''
1413
1414 All calls to @f@ will share a {\em single} array! Clearly this is nonsense and
1415 must be prevented.
1416
1417 This is what @runRW#@ gives us: by being inlined extremely late in the
1418 optimization (right before lowering to STG, in CorePrep), we can ensure that
1419 no further floating will occur. This allows us to safely inline things like
1420 @runST@, which are otherwise needlessly expensive (see #10678 and #5916).
1421
1422 While the definition of @GHC.Magic.runRW#@, we override its type in @MkId@
1423 to be open-kinded,
1424
1425 runRW# :: forall (r1 :: RuntimeRep). (o :: TYPE r)
1426 => (State# RealWorld -> (# State# RealWorld, o #))
1427 -> (# State# RealWorld, o #)
1428
1429
1430 Note [The oneShot function]
1431 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
1432 In the context of making left-folds fuse somewhat okish (see ticket #7994
1433 and Note [Left folds via right fold]) it was determined that it would be useful
1434 if library authors could explicitly tell the compiler that a certain lambda is
1435 called at most once. The oneShot function allows that.
1436
1437 'oneShot' is open kinded, i.e. the type variables can refer to unlifted
1438 types as well (Trac #10744); e.g.
1439 oneShot (\x:Int# -> x +# 1#)
1440
1441 Like most magic functions it has a compulsary unfolding, so there is no need
1442 for a real definition somewhere. We have one in GHC.Magic for the convenience
1443 of putting the documentation there.
1444
1445 It uses `setOneShotLambda` on the lambda's binder. That is the whole magic:
1446
1447 A typical call looks like
1448 oneShot (\y. e)
1449 after unfolding the definition `oneShot = \f \x[oneshot]. f x` we get
1450 (\f \x[oneshot]. f x) (\y. e)
1451 --> \x[oneshot]. ((\y.e) x)
1452 --> \x[oneshot] e[x/y]
1453 which is what we want.
1454
1455 It is only effective if the one-shot info survives as long as possible; in
1456 particular it must make it into the interface in unfoldings. See Note [Preserve
1457 OneShotInfo] in CoreTidy.
1458
1459 Also see https://ghc.haskell.org/trac/ghc/wiki/OneShot.
1460
1461
1462 Note [magicDictId magic]
1463 ~~~~~~~~~~~~~~~~~~~~~~~~~
1464 The identifier `magicDict` is just a place-holder, which is used to
1465 implement a primitve that we cannot define in Haskell but we can write
1466 in Core. It is declared with a place-holder type:
1467
1468 magicDict :: forall a. a
1469
1470 The intention is that the identifier will be used in a very specific way,
1471 to create dictionaries for classes with a single method. Consider a class
1472 like this:
1473
1474 class C a where
1475 f :: T a
1476
1477 We are going to use `magicDict`, in conjunction with a built-in Prelude
1478 rule, to cast values of type `T a` into dictionaries for `C a`. To do
1479 this, we define a function like this in the library:
1480
1481 data WrapC a b = WrapC (C a => Proxy a -> b)
1482
1483 withT :: (C a => Proxy a -> b)
1484 -> T a -> Proxy a -> b
1485 withT f x y = magicDict (WrapC f) x y
1486
1487 The purpose of `WrapC` is to avoid having `f` instantiated.
1488 Also, it avoids impredicativity, because `magicDict`'s type
1489 cannot be instantiated with a forall. The field of `WrapC` contains
1490 a `Proxy` parameter which is used to link the type of the constraint,
1491 `C a`, with the type of the `Wrap` value being made.
1492
1493 Next, we add a built-in Prelude rule (see prelude/PrelRules.hs),
1494 which will replace the RHS of this definition with the appropriate
1495 definition in Core. The rewrite rule works as follows:
1496
1497 magicDict @t (wrap @a @b f) x y
1498 ---->
1499 f (x `cast` co a) y
1500
1501 The `co` coercion is the newtype-coercion extracted from the type-class.
1502 The type class is obtain by looking at the type of wrap.
1503
1504
1505 -------------------------------------------------------------
1506 @realWorld#@ used to be a magic literal, \tr{void#}. If things get
1507 nasty as-is, change it back to a literal (@Literal@).
1508
1509 voidArgId is a Local Id used simply as an argument in functions
1510 where we just want an arg to avoid having a thunk of unlifted type.
1511 E.g.
1512 x = \ void :: Void# -> (# p, q #)
1513
1514 This comes up in strictness analysis
1515
1516 Note [evaldUnfoldings]
1517 ~~~~~~~~~~~~~~~~~~~~~~
1518 The evaldUnfolding makes it look that some primitive value is
1519 evaluated, which in turn makes Simplify.interestingArg return True,
1520 which in turn makes INLINE things applied to said value likely to be
1521 inlined.
1522 -}
1523
1524 realWorldPrimId :: Id -- :: State# RealWorld
1525 realWorldPrimId = pcMiscPrelId realWorldName realWorldStatePrimTy
1526 (noCafIdInfo `setUnfoldingInfo` evaldUnfolding -- Note [evaldUnfoldings]
1527 `setOneShotInfo` stateHackOneShot)
1528
1529 voidPrimId :: Id -- Global constant :: Void#
1530 voidPrimId = pcMiscPrelId voidPrimIdName voidPrimTy
1531 (noCafIdInfo `setUnfoldingInfo` evaldUnfolding) -- Note [evaldUnfoldings]
1532
1533 voidArgId :: Id -- Local lambda-bound :: Void#
1534 voidArgId = mkSysLocal (fsLit "void") voidArgIdKey voidPrimTy
1535
1536 coercionTokenId :: Id -- :: () ~ ()
1537 coercionTokenId -- Used to replace Coercion terms when we go to STG
1538 = pcMiscPrelId coercionTokenName
1539 (mkTyConApp eqPrimTyCon [liftedTypeKind, liftedTypeKind, unitTy, unitTy])
1540 noCafIdInfo
1541
1542 pcMiscPrelId :: Name -> Type -> IdInfo -> Id
1543 pcMiscPrelId name ty info
1544 = mkVanillaGlobalWithInfo name ty info
1545 -- We lie and say the thing is imported; otherwise, we get into
1546 -- a mess with dependency analysis; e.g., core2stg may heave in
1547 -- random calls to GHCbase.unpackPS__. If GHCbase is the module
1548 -- being compiled, then it's just a matter of luck if the definition
1549 -- will be in "the right place" to be in scope.