2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
4 \section[IdInfo]{@IdInfos@: Non-essential information about @Ids@}
6 (And a pretty good illustration of quite a few things wrong with
13 vanillaIdInfo, mkIdInfo, seqIdInfo, megaSeqIdInfo,
16 zapFragileInfo, zapLamInfo, zapSpecPragInfo, shortableIdInfo, copyIdInfo,
19 IdFlavour(..), flavourInfo,
25 exactArity, atLeastArity, unknownArity, hasArity,
26 arityInfo, setArityInfo, ppArityInfo, arityLowerBound,
28 -- Strictness; imported from Demand
30 mkStrictnessInfo, noStrictnessInfo,
31 ppStrictnessInfo,isBottomingStrictness,
32 strictnessInfo, setStrictnessInfo,
34 -- Usage generalisation
36 tyGenInfo, setTyGenInfo,
37 noTyGenInfo, isNoTyGenInfo, ppTyGenInfo, tyGenInfoString,
40 WorkerInfo(..), workerExists, wrapperArity, workerId,
41 workerInfo, setWorkerInfo, ppWorkerInfo,
44 unfoldingInfo, setUnfoldingInfo,
47 demandInfo, setDemandInfo,
51 inlinePragInfo, setInlinePragInfo, pprInlinePragInfo,
52 isNeverInlinePrag, neverInlinePrag,
55 OccInfo(..), isFragileOcc, isDeadOcc, isLoopBreaker,
56 InsideLam, OneBranch, insideLam, notInsideLam, oneBranch, notOneBranch,
60 specInfo, setSpecInfo,
63 CafInfo(..), cafInfo, setCafInfo, ppCafInfo,
65 -- Constructed Product Result Info
66 CprInfo(..), cprInfo, setCprInfo, ppCprInfo, noCprInfo,
68 -- Lambda-bound variable info
69 LBVarInfo(..), lbvarInfo, setLBVarInfo, noLBVarInfo
72 #include "HsVersions.h"
76 import Type ( Type, usOnce )
77 import PrimOp ( PrimOp )
79 import BasicTypes ( OccInfo(..), isFragileOcc, isDeadOcc, seqOccInfo, isLoopBreaker,
80 InsideLam, insideLam, notInsideLam,
81 OneBranch, oneBranch, notOneBranch,
84 import DataCon ( DataCon )
85 import FieldLabel ( FieldLabel )
86 import Type ( usOnce, usMany )
87 import Demand -- Lots of stuff
89 import Util ( seqList )
91 infixl 1 `setDemandInfo`,
103 -- infixl so you can say (id `set` a `set` b)
106 An @IdInfo@ gives {\em optional} information about an @Id@. If
107 present it never lies, but it may not be present, in which case there
108 is always a conservative assumption which can be made.
110 There is one exception: the 'flavour' is *not* optional.
111 You must not discard it.
112 It used to be in Var.lhs, but that seems unclean.
114 Two @Id@s may have different info even though they have the same
115 @Unique@ (and are hence the same @Id@); for example, one might lack
116 the properties attached to the other.
118 The @IdInfo@ gives information about the value, or definition, of the
119 @Id@. It does {\em not} contain information about the @Id@'s usage
120 (except for @DemandInfo@? ToDo). (@lbvarInfo@ is also a marginal
126 flavourInfo :: IdFlavour, -- NOT OPTIONAL
127 arityInfo :: ArityInfo, -- Its arity
128 demandInfo :: Demand, -- Whether or not it is definitely demanded
129 specInfo :: CoreRules, -- Specialisations of this function which exist
130 tyGenInfo :: TyGenInfo, -- Restrictions on usage-generalisation of this Id
131 strictnessInfo :: StrictnessInfo, -- Strictness properties
132 workerInfo :: WorkerInfo, -- Pointer to Worker Function
133 unfoldingInfo :: Unfolding, -- Its unfolding
135 cprInfo :: CprInfo, -- Function always constructs a product result
136 lbvarInfo :: LBVarInfo, -- Info about a lambda-bound variable
137 inlinePragInfo :: InlinePragInfo, -- Inline pragma
138 occInfo :: OccInfo -- How it occurs
141 seqIdInfo :: IdInfo -> ()
142 seqIdInfo (IdInfo {}) = ()
144 megaSeqIdInfo :: IdInfo -> ()
146 = seqFlavour (flavourInfo info) `seq`
147 seqArity (arityInfo info) `seq`
148 seqDemand (demandInfo info) `seq`
149 seqRules (specInfo info) `seq`
150 seqTyGenInfo (tyGenInfo info) `seq`
151 seqStrictnessInfo (strictnessInfo info) `seq`
152 seqWorker (workerInfo info) `seq`
154 -- seqUnfolding (unfoldingInfo info) `seq`
155 -- Omitting this improves runtimes a little, presumably because
156 -- some unfoldings are not calculated at all
158 seqCaf (cafInfo info) `seq`
159 seqCpr (cprInfo info) `seq`
160 seqLBVar (lbvarInfo info) `seq`
161 seqOccInfo (occInfo info)
167 setWorkerInfo info wk = wk `seq` info { workerInfo = wk }
168 setSpecInfo info sp = PSEQ sp (info { specInfo = sp })
169 setTyGenInfo info tg = tg `seq` info { tyGenInfo = tg }
170 setInlinePragInfo info pr = pr `seq` info { inlinePragInfo = pr }
171 setOccInfo info oc = oc `seq` info { occInfo = oc }
172 setStrictnessInfo info st = st `seq` info { strictnessInfo = st }
173 -- Try to avoid spack leaks by seq'ing
175 setUnfoldingInfo info uf
176 | isEvaldUnfolding uf && isStrict (demandInfo info)
177 -- If the unfolding is a value, the demand info may
178 -- go pear-shaped, so we nuke it. Example:
180 -- case x of (p,q) -> h p q x
181 -- Here x is certainly demanded. But after we've nuked
182 -- the case, we'll get just
183 -- let x = (a,b) in h a b x
184 -- and now x is not demanded (I'm assuming h is lazy)
185 -- This really happens. The solution here is a bit ad hoc...
186 = info { unfoldingInfo = uf, demandInfo = wwLazy }
189 -- We do *not* seq on the unfolding info, For some reason, doing so
190 -- actually increases residency significantly.
191 = info { unfoldingInfo = uf }
193 setDemandInfo info dd = info { demandInfo = dd }
194 setArityInfo info ar = info { arityInfo = ar }
195 setCafInfo info cf = info { cafInfo = cf }
196 setCprInfo info cp = info { cprInfo = cp }
197 setLBVarInfo info lb = info { lbvarInfo = lb }
199 setNoDiscardInfo info = case flavourInfo info of
200 VanillaId -> info { flavourInfo = NoDiscardId }
202 zapSpecPragInfo info = case flavourInfo info of
203 SpecPragmaId -> info { flavourInfo = VanillaId }
209 vanillaIdInfo :: IdInfo
210 vanillaIdInfo = mkIdInfo VanillaId
212 mkIdInfo :: IdFlavour -> IdInfo
213 mkIdInfo flv = IdInfo {
215 arityInfo = UnknownArity,
217 specInfo = emptyCoreRules,
218 tyGenInfo = noTyGenInfo,
219 workerInfo = NoWorker,
220 strictnessInfo = NoStrictnessInfo,
221 unfoldingInfo = noUnfolding,
222 cafInfo = MayHaveCafRefs,
224 lbvarInfo = NoLBVarInfo,
225 inlinePragInfo = NoInlinePragInfo,
231 %************************************************************************
235 %************************************************************************
239 = VanillaId -- Most Ids are like this
240 | DataConId DataCon -- The Id for a data constructor *worker*
241 | DataConWrapId DataCon -- The Id for a data constructor *wrapper*
242 -- [the only reasons we need to know is so that
243 -- a) we can suppress printing a definition in the interface file
244 -- b) when typechecking a pattern we can get from the
245 -- Id back to the data con]
246 | PrimOpId PrimOp -- The Id for a primitive operator
247 | RecordSelId FieldLabel -- The Id for a record selector
248 | SpecPragmaId -- Don't discard these
249 | NoDiscardId -- Don't discard these either
251 ppFlavourInfo :: IdFlavour -> SDoc
252 ppFlavourInfo VanillaId = empty
253 ppFlavourInfo (DataConId _) = ptext SLIT("[DataCon]")
254 ppFlavourInfo (DataConWrapId _) = ptext SLIT("[DataConWrapper]")
255 ppFlavourInfo (PrimOpId _) = ptext SLIT("[PrimOp]")
256 ppFlavourInfo (RecordSelId _) = ptext SLIT("[RecSel]")
257 ppFlavourInfo SpecPragmaId = ptext SLIT("[SpecPrag]")
258 ppFlavourInfo NoDiscardId = ptext SLIT("[NoDiscard]")
260 seqFlavour :: IdFlavour -> ()
261 seqFlavour f = f `seq` ()
264 The @SpecPragmaId@ exists only to make Ids that are
265 on the *LHS* of bindings created by SPECIALISE pragmas;
267 The SpecPragmaId is never itself mentioned; it
268 exists solely so that the specialiser will find
269 the call to f, and make specialised version of it.
270 The SpecPragmaId binding is discarded by the specialiser
271 when it gathers up overloaded calls.
272 Meanwhile, it is not discarded as dead code.
275 %************************************************************************
277 \subsection[arity-IdInfo]{Arity info about an @Id@}
279 %************************************************************************
281 For locally-defined Ids, the code generator maintains its own notion
282 of their arities; so it should not be asking... (but other things
283 besides the code-generator need arity info!)
287 = UnknownArity -- No idea
289 | ArityExactly Arity -- Arity is exactly this. We use this when importing a
290 -- function; it's already been compiled and we know its
293 | ArityAtLeast Arity -- A partial application of this Id to up to n-1 value arguments
294 -- does essentially no work. That is not necessarily the
295 -- same as saying that it has n leading lambdas, because coerces
296 -- may get in the way.
298 -- functions in the module being compiled. Their arity
299 -- might increase later in the compilation process, if
300 -- an extra lambda floats up to the binding site.
303 seqArity :: ArityInfo -> ()
304 seqArity a = arityLowerBound a `seq` ()
306 exactArity = ArityExactly
307 atLeastArity = ArityAtLeast
308 unknownArity = UnknownArity
310 arityLowerBound :: ArityInfo -> Arity
311 arityLowerBound UnknownArity = 0
312 arityLowerBound (ArityAtLeast n) = n
313 arityLowerBound (ArityExactly n) = n
315 hasArity :: ArityInfo -> Bool
316 hasArity UnknownArity = False
317 hasArity other = True
319 ppArityInfo UnknownArity = empty
320 ppArityInfo (ArityExactly arity) = hsep [ptext SLIT("__A"), int arity]
321 ppArityInfo (ArityAtLeast arity) = hsep [ptext SLIT("__AL"), int arity]
324 %************************************************************************
326 \subsection{Inline-pragma information}
328 %************************************************************************
333 | IMustNotBeINLINEd Bool -- True <=> came from an INLINE prag, False <=> came from a NOINLINE prag
334 (Maybe Int) -- Phase number from pragma, if any
336 -- The True, Nothing case doesn't need to be recorded
338 -- SEE COMMENTS WITH CoreUnfold.blackListed on the
339 -- exact significance of the IMustNotBeINLINEd pragma
341 isNeverInlinePrag :: InlinePragInfo -> Bool
342 isNeverInlinePrag (IMustNotBeINLINEd _ Nothing) = True
343 isNeverInlinePrag other = False
345 neverInlinePrag :: InlinePragInfo
346 neverInlinePrag = IMustNotBeINLINEd True{-should be False? --SDM -} Nothing
348 instance Outputable InlinePragInfo where
349 -- This is now parsed in interface files
350 ppr NoInlinePragInfo = empty
351 ppr other_prag = ptext SLIT("__U") <> pprInlinePragInfo other_prag
353 pprInlinePragInfo NoInlinePragInfo = empty
354 pprInlinePragInfo (IMustNotBeINLINEd True Nothing) = empty
355 pprInlinePragInfo (IMustNotBeINLINEd True (Just n)) = brackets (int n)
356 pprInlinePragInfo (IMustNotBeINLINEd False Nothing) = brackets (char '!')
357 pprInlinePragInfo (IMustNotBeINLINEd False (Just n)) = brackets (char '!' <> int n)
359 instance Show InlinePragInfo where
360 showsPrec p prag = showsPrecSDoc p (ppr prag)
364 %************************************************************************
366 \subsection[TyGen-IdInfo]{Type generalisation info about an @Id@}
368 %************************************************************************
370 Certain passes (notably usage inference) may change the type of an
371 identifier, modifying all in-scope uses of that identifier
372 appropriately to maintain type safety.
374 However, some identifiers must not have their types changed in this
375 way, because their types are conjured up in the front end of the
376 compiler rather than being read from the interface file. Default
377 methods, dictionary functions, record selectors, and others are in
378 this category. (see comment at TcClassDcl.tcClassSig).
380 To indicate this property, such identifiers are marked TyGenNever.
382 Furthermore, if the usage inference generates a usage-specialised
383 variant of a function, we must NOT re-infer a fully-generalised type
384 at the next inference. This finer property is indicated by a
385 TyGenUInfo on the identifier.
389 = NoTyGenInfo -- no restriction on type generalisation
391 | TyGenUInfo [Maybe Type] -- restrict generalisation of this Id to
392 -- preserve specified usage annotations
394 | TyGenNever -- never generalise the type of this Id
399 For TyGenUInfo, the list has one entry for each usage annotation on
400 the type of the Id, in left-to-right pre-order (annotations come
401 before the type they annotate). Nothing means no restriction; Just
402 usOnce or Just usMany forces that annotation to that value. Other
403 usage annotations are illegal.
406 seqTyGenInfo :: TyGenInfo -> ()
407 seqTyGenInfo NoTyGenInfo = ()
408 seqTyGenInfo (TyGenUInfo us) = seqList us ()
409 seqTyGenInfo TyGenNever = ()
411 noTyGenInfo :: TyGenInfo
412 noTyGenInfo = NoTyGenInfo
414 isNoTyGenInfo :: TyGenInfo -> Bool
415 isNoTyGenInfo NoTyGenInfo = True
416 isNoTyGenInfo _ = False
418 -- NB: There's probably no need to write this information out to the interface file.
419 -- Why? Simply because imported identifiers never get their types re-inferred.
420 -- But it's definitely nice to see in dumps, it for debugging purposes.
422 ppTyGenInfo :: TyGenInfo -> SDoc
423 ppTyGenInfo NoTyGenInfo = empty
424 ppTyGenInfo (TyGenUInfo us) = ptext SLIT("__G") <+> text (tyGenInfoString us)
425 ppTyGenInfo TyGenNever = ptext SLIT("__G N")
427 tyGenInfoString us = map go us
428 where go Nothing = 'x' -- for legibility, choose
429 go (Just u) | u == usOnce = '1' -- chars with identity
430 | u == usMany = 'M' -- Z-encoding.
431 go other = pprPanic "IdInfo.tyGenInfoString: unexpected annotation" (ppr other)
433 instance Outputable TyGenInfo where
436 instance Show TyGenInfo where
437 showsPrec p c = showsPrecSDoc p (ppr c)
441 %************************************************************************
443 \subsection[worker-IdInfo]{Worker info about an @Id@}
445 %************************************************************************
447 If this Id has a worker then we store a reference to it. Worker
448 functions are generated by the worker/wrapper pass. This uses
449 information from the strictness and CPR analyses.
451 There might not be a worker, even for a strict function, because:
452 (a) the function might be small enough to inline, so no need
454 (b) the strictness info might be "SSS" or something, so no w/w split.
458 data WorkerInfo = NoWorker
460 -- The Arity is the arity of the *wrapper* at the moment of the
461 -- w/w split. See comments in MkIface.ifaceId, with the 'Worker' code.
463 seqWorker :: WorkerInfo -> ()
464 seqWorker (HasWorker id _) = id `seq` ()
465 seqWorker NoWorker = ()
467 ppWorkerInfo NoWorker = empty
468 ppWorkerInfo (HasWorker wk_id _) = ptext SLIT("__P") <+> ppr wk_id
470 noWorkerInfo = NoWorker
472 workerExists :: WorkerInfo -> Bool
473 workerExists NoWorker = False
474 workerExists (HasWorker _ _) = True
476 workerId :: WorkerInfo -> Id
477 workerId (HasWorker id _) = id
479 wrapperArity :: WorkerInfo -> Arity
480 wrapperArity (HasWorker _ a) = a
484 %************************************************************************
486 \subsection[CAF-IdInfo]{CAF-related information}
488 %************************************************************************
490 This information is used to build Static Reference Tables (see
491 simplStg/ComputeSRT.lhs).
495 = MayHaveCafRefs -- either:
496 -- (1) A function or static constructor
497 -- that refers to one or more CAFs,
498 -- (2) A real live CAF
500 | NoCafRefs -- A function or static constructor
501 -- that refers to no CAFs.
503 -- LATER: not sure how easy this is...
507 seqCaf c = c `seq` ()
509 ppCafInfo NoCafRefs = ptext SLIT("__C")
510 ppCafInfo MayHaveCafRefs = empty
514 %************************************************************************
516 \subsection[cpr-IdInfo]{Constructed Product Result info about an @Id@}
518 %************************************************************************
520 If the @Id@ is a function then it may have CPR info. A CPR analysis
521 phase detects whether:
525 The function's return value has a product type, i.e. an algebraic type
526 with a single constructor. Examples of such types are tuples and boxed
529 The function always 'constructs' the value that it is returning. It
530 must do this on every path through, and it's OK if it calls another
531 function which constructs the result.
534 If this is the case then we store a template which tells us the
535 function has the CPR property and which components of the result are
541 | ReturnsCPR -- Yes, this function returns a constructed product
542 -- Implicitly, this means "after the function has been applied
543 -- to all its arguments", so the worker/wrapper builder in
544 -- WwLib.mkWWcpr checks that that it is indeed saturated before
545 -- making use of the CPR info
547 -- We used to keep nested info about sub-components, but
548 -- we never used it so I threw it away
552 seqCpr :: CprInfo -> ()
553 seqCpr ReturnsCPR = ()
554 seqCpr NoCPRInfo = ()
556 noCprInfo = NoCPRInfo
558 ppCprInfo NoCPRInfo = empty
559 ppCprInfo ReturnsCPR = ptext SLIT("__M")
561 instance Outputable CprInfo where
564 instance Show CprInfo where
565 showsPrec p c = showsPrecSDoc p (ppr c)
569 %************************************************************************
571 \subsection[lbvar-IdInfo]{Lambda-bound var info about an @Id@}
573 %************************************************************************
575 If the @Id@ is a lambda-bound variable then it may have lambda-bound
576 var info. The usage analysis (UsageSP) detects whether the lambda
577 binding this var is a ``one-shot'' lambda; that is, whether it is
578 applied at most once.
580 This information may be useful in optimisation, as computations may
581 safely be floated inside such a lambda without risk of duplicating
588 | LBVarInfo Type -- The lambda that binds this Id has this usage
589 -- annotation (i.e., if ==usOnce, then the
590 -- lambda is applied at most once).
591 -- The annotation's kind must be `$'
592 -- HACK ALERT! placing this info here is a short-term hack,
593 -- but it minimises changes to the rest of the compiler.
594 -- Hack agreed by SLPJ/KSW 1999-04.
596 seqLBVar l = l `seq` ()
600 noLBVarInfo = NoLBVarInfo
602 -- not safe to print or parse LBVarInfo because it is not really a
603 -- property of the definition, but a property of the context.
604 pprLBVarInfo NoLBVarInfo = empty
605 pprLBVarInfo (LBVarInfo u) | u == usOnce
606 = getPprStyle $ \ sty ->
609 else ptext SLIT("OneShot")
613 instance Outputable LBVarInfo where
616 instance Show LBVarInfo where
617 showsPrec p c = showsPrecSDoc p (ppr c)
621 %************************************************************************
623 \subsection{Bulk operations on IdInfo}
625 %************************************************************************
627 zapFragileInfo is used when cloning binders, mainly in the
628 simplifier. We must forget about used-once information because that
629 isn't necessarily correct in the transformed program.
630 Also forget specialisations and unfoldings because they would need
631 substitution to be correct. (They get pinned back on separately.)
634 zapFragileInfo :: IdInfo -> Maybe IdInfo
635 zapFragileInfo info@(IdInfo {occInfo = occ,
638 unfoldingInfo = unfolding})
639 | not (isFragileOcc occ)
640 -- We must forget about whether it was marked safe-to-inline,
641 -- because that isn't necessarily true in the simplified expression.
642 -- This is important because expressions may be re-simplified
643 -- We don't zap deadness or loop-breaker-ness.
644 -- The latter is important because it tells MkIface not to
645 -- spit out an inlining for the thing. The former doesn't
646 -- seem so important, but there's no harm.
648 && isEmptyCoreRules rules
649 -- Specialisations would need substituting. They get pinned
650 -- back on separately.
652 && not (workerExists wrkr)
654 && not (hasUnfolding unfolding)
655 -- This is very important; occasionally a let-bound binder is used
656 -- as a binder in some lambda, in which case its unfolding is utterly
657 -- bogus. Also the unfolding uses old binders so if we left it we'd
658 -- have to substitute it. Much better simply to give the Id a new
659 -- unfolding each time, which is what the simplifier does.
663 = Just (info {occInfo = robust_occ_info,
664 workerInfo = noWorkerInfo,
665 specInfo = emptyCoreRules,
666 unfoldingInfo = noUnfolding})
668 -- It's important to keep the loop-breaker info,
669 -- because the substitution doesn't remember it.
670 robust_occ_info = case occ of
671 OneOcc _ _ -> NoOccInfo
675 @zapLamInfo@ is used for lambda binders that turn out to to be
676 part of an unsaturated lambda
679 zapLamInfo :: IdInfo -> Maybe IdInfo
680 zapLamInfo info@(IdInfo {occInfo = occ, demandInfo = demand})
681 | is_safe_occ && not (isStrict demand)
684 = Just (info {occInfo = safe_occ,
685 demandInfo = wwLazy})
687 -- The "unsafe" occ info is the ones that say I'm not in a lambda
688 -- because that might not be true for an unsaturated lambda
689 is_safe_occ = case occ of
690 OneOcc in_lam once -> in_lam
693 safe_occ = case occ of
694 OneOcc _ once -> OneOcc insideLam once
699 copyIdInfo is used when shorting out a top-level binding
702 where f is exported. We are going to swizzle it around to
706 BUT (a) we must be careful about messing up rules
707 (b) we must ensure f's IdInfo ends up right
709 (a) Messing up the rules
711 The example that went bad on me was this one:
713 iterate :: (a -> a) -> a -> [a]
714 iterate = iterateList
716 iterateFB c f x = x `c` iterateFB c f (f x)
717 iterateList f x = x : iterateList f (f x)
720 "iterate" forall f x. iterate f x = build (\c _n -> iterateFB c f x)
721 "iterateFB" iterateFB (:) = iterateList
724 This got shorted out to:
726 iterateList :: (a -> a) -> a -> [a]
727 iterateList = iterate
729 iterateFB c f x = x `c` iterateFB c f (f x)
730 iterate f x = x : iterate f (f x)
733 "iterate" forall f x. iterate f x = build (\c _n -> iterateFB c f x)
734 "iterateFB" iterateFB (:) = iterate
737 And now we get an infinite loop in the rule system
738 iterate f x -> build (\cn -> iterateFB c f x
742 Tiresome solution: don't do shorting out if f has rewrite rules.
743 Hence shortableIdInfo.
745 (b) Keeping the IdInfo right
746 ~~~~~~~~~~~~~~~~~~~~~~~~
747 We want to move strictness/worker info from f_local to f, but keep the rest.
751 shortableIdInfo :: IdInfo -> Bool
752 shortableIdInfo info = isEmptyCoreRules (specInfo info)
754 copyIdInfo :: IdInfo -- f_local
755 -> IdInfo -- f (the exported one)
756 -> IdInfo -- New info for f
757 copyIdInfo f_local f = f { strictnessInfo = strictnessInfo f_local,
758 workerInfo = workerInfo f_local,
759 cprInfo = cprInfo f_local