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