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 IdFlavour(..), flavourInfo,
17 setNoDiscardInfo, zapSpecPragInfo, copyIdInfo,
22 exactArity, atLeastArity, unknownArity, hasArity,
23 arityInfo, setArityInfo, ppArityInfo, arityLowerBound,
26 StrictnessInfo(..), -- Non-abstract
28 noStrictnessInfo, strictnessInfo,
29 ppStrictnessInfo, setStrictnessInfo,
30 isBottomingStrictness, appIsBottom,
33 WorkerInfo, workerExists,
34 workerInfo, setWorkerInfo, ppWorkerInfo,
37 unfoldingInfo, setUnfoldingInfo,
40 demandInfo, setDemandInfo,
43 InlinePragInfo(..), OccInfo(..),
44 inlinePragInfo, setInlinePragInfo, notInsideLambda,
47 specInfo, setSpecInfo,
50 UpdateInfo, UpdateSpec,
51 mkUpdateInfo, updateInfo, updateInfoMaybe, ppUpdateInfo, setUpdateInfo,
54 CafInfo(..), cafInfo, setCafInfo, ppCafInfo,
56 -- Constructed Product Result Info
57 CprInfo(..), cprInfo, setCprInfo, ppCprInfo, noCprInfo,
60 zapLamIdInfo, zapFragileIdInfo, zapIdInfoForStg,
62 -- Lambda-bound variable info
63 LBVarInfo(..), lbvarInfo, setLBVarInfo, noLBVarInfo
66 #include "HsVersions.h"
69 import {-# SOURCE #-} CoreUnfold ( Unfolding, noUnfolding, hasUnfolding, seqUnfolding )
70 import {-# SOURCE #-} CoreSyn ( CoreExpr, CoreRules, emptyCoreRules, isEmptyCoreRules, seqRules )
71 import {-# SOURCE #-} Const ( Con )
74 import FieldLabel ( FieldLabel )
75 import Demand ( Demand, isStrict, isLazy, wwLazy, pprDemands, seqDemand, seqDemands )
76 import Type ( UsageAnn )
78 import Maybe ( isJust )
80 infixl 1 `setUpdateInfo`,
90 -- infixl so you can say (id `set` a `set` b)
93 An @IdInfo@ gives {\em optional} information about an @Id@. If
94 present it never lies, but it may not be present, in which case there
95 is always a conservative assumption which can be made.
97 There is one exception: the 'flavour' is *not* optional.
98 You must not discard it.
99 It used to be in Var.lhs, but that seems unclean.
101 Two @Id@s may have different info even though they have the same
102 @Unique@ (and are hence the same @Id@); for example, one might lack
103 the properties attached to the other.
105 The @IdInfo@ gives information about the value, or definition, of the
106 @Id@. It does {\em not} contain information about the @Id@'s usage
107 (except for @DemandInfo@? ToDo). (@lbvarInfo@ is also a marginal
113 flavourInfo :: IdFlavour, -- NOT OPTIONAL
114 arityInfo :: ArityInfo, -- Its arity
115 demandInfo :: Demand, -- Whether or not it is definitely demanded
116 specInfo :: CoreRules, -- Specialisations of this function which exist
117 strictnessInfo :: StrictnessInfo, -- Strictness properties
118 workerInfo :: WorkerInfo, -- Pointer to Worker Function
119 unfoldingInfo :: Unfolding, -- Its unfolding
120 updateInfo :: UpdateInfo, -- Which args should be updated
122 cprInfo :: CprInfo, -- Function always constructs a product result
123 lbvarInfo :: LBVarInfo, -- Info about a lambda-bound variable
124 inlinePragInfo :: InlinePragInfo -- Inline pragmas
127 seqIdInfo :: IdInfo -> ()
128 seqIdInfo (IdInfo {}) = ()
130 megaSeqIdInfo :: IdInfo -> ()
132 = seqFlavour (flavourInfo info) `seq`
133 seqArity (arityInfo info) `seq`
134 seqDemand (demandInfo info) `seq`
135 seqRules (specInfo info) `seq`
136 seqStrictness (strictnessInfo info) `seq`
137 seqWorker (workerInfo info) `seq`
139 -- seqUnfolding (unfoldingInfo info) `seq`
140 -- Omitting this improves runtimes a little, presumably because
141 -- some unfoldings are not calculated at all
143 seqCaf (cafInfo info) `seq`
144 seqCpr (cprInfo info) `seq`
145 seqLBVar (lbvarInfo info) `seq`
146 seqInlinePrag (inlinePragInfo info)
152 setWorkerInfo info wk = wk `seq` info { workerInfo = wk }
153 setSpecInfo info sp = PSEQ sp (info { specInfo = sp })
154 setInlinePragInfo info pr = pr `seq` info { inlinePragInfo = pr }
155 setStrictnessInfo info st = st `seq` info { strictnessInfo = st }
156 -- Try to avoid spack leaks by seq'ing
158 setUnfoldingInfo info uf = info { unfoldingInfo = uf }
159 -- We do *not* seq on the unfolding info, For some reason, doing so
160 -- actually increases residency significantly.
162 setUpdateInfo info ud = info { updateInfo = ud }
163 setDemandInfo info dd = info { demandInfo = dd }
164 setArityInfo info ar = info { arityInfo = ar }
165 setCafInfo info cf = info { cafInfo = cf }
166 setCprInfo info cp = info { cprInfo = cp }
167 setLBVarInfo info lb = info { lbvarInfo = lb }
169 setNoDiscardInfo info = case flavourInfo info of
170 VanillaId -> info { flavourInfo = NoDiscardId }
172 zapSpecPragInfo info = case flavourInfo info of
173 SpecPragmaId -> info { flavourInfo = VanillaId }
176 copyIdInfo :: IdInfo -- From
178 -> IdInfo -- To, updated with stuff from From; except flavour unchanged
179 -- copyIdInfo is used when shorting out a top-level binding
182 -- where f is exported. We are going to swizzle it around to
185 -- but we must be careful to combine their IdInfos right.
186 -- The fact that things can go wrong here is a bad sign, but I can't see
187 -- how to make it 'patently right', so copyIdInfo is derived (pretty much) by trial and error
189 -- Here 'from' is f_local, 'to' is f, and the result is attached to f
191 copyIdInfo from to = from { flavourInfo = flavourInfo to,
192 specInfo = specInfo to,
193 inlinePragInfo = inlinePragInfo to
195 -- It's important to preserve the inline pragma on 'f'; e.g. consider
196 -- {-# NOINLINE f #-}
199 -- similarly, transformation rules may be attached to f
200 -- and we want to preserve them.
202 -- On the other hand, we want the strictness info from f_local.
207 vanillaIdInfo :: IdInfo
208 vanillaIdInfo = mkIdInfo VanillaId
210 mkIdInfo :: IdFlavour -> IdInfo
211 mkIdInfo flv = IdInfo {
213 arityInfo = UnknownArity,
215 specInfo = emptyCoreRules,
216 workerInfo = Nothing,
217 strictnessInfo = NoStrictnessInfo,
218 unfoldingInfo = noUnfolding,
219 updateInfo = NoUpdateInfo,
220 cafInfo = MayHaveCafRefs,
222 lbvarInfo = NoLBVarInfo,
223 inlinePragInfo = NoInlinePragInfo
228 %************************************************************************
232 %************************************************************************
236 = VanillaId -- Most Ids are like this
237 | ConstantId Con -- The Id for a constant (data constructor or primop)
238 | RecordSelId FieldLabel -- The Id for a record selector
239 | SpecPragmaId -- Don't discard these
240 | NoDiscardId -- Don't discard these either
242 ppFlavourInfo :: IdFlavour -> SDoc
243 ppFlavourInfo VanillaId = empty
244 ppFlavourInfo (ConstantId _) = ptext SLIT("[Constr]")
245 ppFlavourInfo (RecordSelId _) = ptext SLIT("[RecSel]")
246 ppFlavourInfo SpecPragmaId = ptext SLIT("[SpecPrag]")
247 ppFlavourInfo NoDiscardId = ptext SLIT("[NoDiscard]")
249 seqFlavour :: IdFlavour -> ()
250 seqFlavour f = f `seq` ()
253 The @SpecPragmaId@ exists only to make Ids that are
254 on the *LHS* of bindings created by SPECIALISE pragmas;
256 The SpecPragmaId is never itself mentioned; it
257 exists solely so that the specialiser will find
258 the call to f, and make specialised version of it.
259 The SpecPragmaId binding is discarded by the specialiser
260 when it gathers up overloaded calls.
261 Meanwhile, it is not discarded as dead code.
264 %************************************************************************
266 \subsection[arity-IdInfo]{Arity info about an @Id@}
268 %************************************************************************
270 For locally-defined Ids, the code generator maintains its own notion
271 of their arities; so it should not be asking... (but other things
272 besides the code-generator need arity info!)
276 = UnknownArity -- No idea
278 | ArityExactly Int -- Arity is exactly this. We use this when importing a
279 -- function; it's already been compiled and we know its
282 | ArityAtLeast Int -- Arity is this or greater. We attach this arity to
283 -- functions in the module being compiled. Their arity
284 -- might increase later in the compilation process, if
285 -- an extra lambda floats up to the binding site.
287 seqArity :: ArityInfo -> ()
288 seqArity a = arityLowerBound a `seq` ()
290 exactArity = ArityExactly
291 atLeastArity = ArityAtLeast
292 unknownArity = UnknownArity
294 arityLowerBound :: ArityInfo -> Int
295 arityLowerBound UnknownArity = 0
296 arityLowerBound (ArityAtLeast n) = n
297 arityLowerBound (ArityExactly n) = n
299 hasArity :: ArityInfo -> Bool
300 hasArity UnknownArity = False
301 hasArity other = True
303 ppArityInfo UnknownArity = empty
304 ppArityInfo (ArityExactly arity) = hsep [ptext SLIT("__A"), int arity]
305 ppArityInfo (ArityAtLeast arity) = hsep [ptext SLIT("__AL"), int arity]
308 %************************************************************************
310 \subsection{Inline-pragma information}
312 %************************************************************************
318 | IMustNotBeINLINEd -- User NOINLINE pragma
320 | IAmALoopBreaker -- Used by the occurrence analyser to mark loop-breakers
321 -- in a group of recursive definitions
323 | ICanSafelyBeINLINEd -- Used by the occurrence analyser to mark things
324 -- that manifesly occur once, not inside SCCs,
325 -- not in constructor arguments
327 OccInfo -- Says whether the occurrence is inside a lambda
328 -- If so, must only substitute WHNFs
330 Bool -- False <=> occurs in more than one case branch
331 -- If so, there's a code-duplication issue
333 | IAmDead -- Marks unused variables. Sometimes useful for
334 -- lambda and case-bound variables.
336 | IMustBeINLINEd -- Absolutely must inline; used for PrimOps and
337 -- constructors only.
339 seqInlinePrag :: InlinePragInfo -> ()
340 seqInlinePrag (ICanSafelyBeINLINEd occ alts)
341 = occ `seq` alts `seq` ()
345 instance Outputable InlinePragInfo where
346 -- only used for debugging; never parsed. KSW 1999-07
347 ppr NoInlinePragInfo = empty
348 ppr IMustBeINLINEd = ptext SLIT("__UU")
349 ppr IMustNotBeINLINEd = ptext SLIT("__Unot")
350 ppr IAmALoopBreaker = ptext SLIT("__Ux")
351 ppr IAmDead = ptext SLIT("__Ud")
352 ppr (ICanSafelyBeINLINEd InsideLam _) = ptext SLIT("__Ul")
353 ppr (ICanSafelyBeINLINEd _ _) = ptext SLIT("__Us")
355 instance Show InlinePragInfo where
356 showsPrec p prag = showsPrecSDoc p (ppr prag)
363 | InsideLam -- Inside a non-linear lambda (that is, a lambda which
364 -- is sure to be instantiated only once).
365 -- Substituting a redex for this occurrence is
366 -- dangerous because it might duplicate work.
368 instance Outputable OccInfo where
369 ppr NotInsideLam = empty
370 ppr InsideLam = text "l"
373 notInsideLambda :: OccInfo -> Bool
374 notInsideLambda NotInsideLam = True
375 notInsideLambda InsideLam = False
378 %************************************************************************
380 \subsection[strictness-IdInfo]{Strictness info about an @Id@}
382 %************************************************************************
384 We specify the strictness of a function by giving information about
385 each of the ``wrapper's'' arguments (see the description about
386 worker/wrapper-style transformations in the PJ/Launchbury paper on
389 The list of @Demands@ specifies: (a)~the strictness properties of a
390 function's arguments; and (b)~the type signature of that worker (if it
391 exists); i.e. its calling convention.
393 Note that the existence of a worker function is now denoted by the Id's
400 | StrictnessInfo [Demand]
401 Bool -- True <=> the function diverges regardless of its arguments
402 -- Useful for "error" and other disguised variants thereof.
403 -- BUT NB: f = \x y. error "urk"
404 -- will have info SI [SS] True
405 -- but still (f) and (f 2) are not bot; only (f 3 2) is bot
407 seqStrictness :: StrictnessInfo -> ()
408 seqStrictness (StrictnessInfo ds b) = b `seq` seqDemands ds
409 seqStrictness other = ()
413 mkStrictnessInfo :: ([Demand], Bool) -> StrictnessInfo
415 mkStrictnessInfo (xs, is_bot)
416 | all isLazy xs && not is_bot = NoStrictnessInfo -- Uninteresting
417 | otherwise = StrictnessInfo xs is_bot
419 noStrictnessInfo = NoStrictnessInfo
421 isBottomingStrictness (StrictnessInfo _ bot) = bot
422 isBottomingStrictness NoStrictnessInfo = False
424 -- appIsBottom returns true if an application to n args would diverge
425 appIsBottom (StrictnessInfo ds bot) n = bot && (n >= length ds)
426 appIsBottom NoStrictnessInfo n = False
428 ppStrictnessInfo NoStrictnessInfo = empty
429 ppStrictnessInfo (StrictnessInfo wrapper_args bot)
430 = hsep [ptext SLIT("__S"), pprDemands wrapper_args bot]
433 %************************************************************************
435 \subsection[worker-IdInfo]{Worker info about an @Id@}
437 %************************************************************************
439 If this Id has a worker then we store a reference to it. Worker
440 functions are generated by the worker/wrapper pass. This uses
441 information from the strictness and CPR analyses.
443 There might not be a worker, even for a strict function, because:
444 (a) the function might be small enough to inline, so no need
446 (b) the strictness info might be "SSS" or something, so no w/w split.
450 type WorkerInfo = Maybe Id
453 mkWorkerInfo :: Id -> WorkerInfo
454 mkWorkerInfo wk_id = Just wk_id
457 seqWorker :: WorkerInfo -> ()
458 seqWorker (Just id) = id `seq` ()
459 seqWorker Nothing = ()
461 ppWorkerInfo Nothing = empty
462 ppWorkerInfo (Just wk_id) = ptext SLIT("__P") <+> ppr wk_id
464 noWorkerInfo = Nothing
466 workerExists :: Maybe Id -> Bool
467 workerExists = isJust
471 %************************************************************************
473 \subsection[update-IdInfo]{Update-analysis info about an @Id@}
475 %************************************************************************
480 | SomeUpdateInfo UpdateSpec
482 -- we need Eq/Ord to cross-chk update infos in interfaces
484 -- the form in which we pass update-analysis info between modules:
485 type UpdateSpec = [Int]
489 mkUpdateInfo = SomeUpdateInfo
491 updateInfoMaybe NoUpdateInfo = Nothing
492 updateInfoMaybe (SomeUpdateInfo []) = Nothing
493 updateInfoMaybe (SomeUpdateInfo u) = Just u
496 Text instance so that the update annotations can be read in.
499 ppUpdateInfo NoUpdateInfo = empty
500 ppUpdateInfo (SomeUpdateInfo []) = empty
501 ppUpdateInfo (SomeUpdateInfo spec) = (<>) (ptext SLIT("__UA ")) (hcat (map int spec))
502 -- was "__U "; changed to avoid conflict with unfoldings. KSW 1999-07.
505 %************************************************************************
507 \subsection[CAF-IdInfo]{CAF-related information}
509 %************************************************************************
511 This information is used to build Static Reference Tables (see
512 simplStg/ComputeSRT.lhs).
516 = MayHaveCafRefs -- either:
517 -- (1) A function or static constructor
518 -- that refers to one or more CAFs,
519 -- (2) A real live CAF
521 | NoCafRefs -- A function or static constructor
522 -- that refers to no CAFs.
524 -- LATER: not sure how easy this is...
528 seqCaf c = c `seq` ()
530 ppCafInfo NoCafRefs = ptext SLIT("__C")
531 ppCafInfo MayHaveCafRefs = empty
535 %************************************************************************
537 \subsection[CAF-IdInfo]{CAF-related information}
539 %************************************************************************
541 zapFragileIdInfo is used when cloning binders, mainly in the
542 simplifier. We must forget about used-once information because that
543 isn't necessarily correct in the transformed program.
544 Also forget specialisations and unfoldings because they would need
545 substitution to be correct. (They get pinned back on separately.)
548 zapFragileIdInfo :: IdInfo -> Maybe IdInfo
549 zapFragileIdInfo info@(IdInfo {inlinePragInfo = inline_prag,
552 unfoldingInfo = unfolding})
553 | not is_fragile_inline_prag
554 -- We must forget about whether it was marked safe-to-inline,
555 -- because that isn't necessarily true in the simplified expression.
556 -- This is important because expressions may be re-simplified
558 && isEmptyCoreRules rules
559 -- Specialisations would need substituting. They get pinned
560 -- back on separately.
562 && not (workerExists wrkr)
564 && not (hasUnfolding unfolding)
565 -- This is very important; occasionally a let-bound binder is used
566 -- as a binder in some lambda, in which case its unfolding is utterly
567 -- bogus. Also the unfolding uses old binders so if we left it we'd
568 -- have to substitute it. Much better simply to give the Id a new
569 -- unfolding each time, which is what the simplifier does.
573 = Just (info {inlinePragInfo = safe_inline_prag,
574 workerInfo = noWorkerInfo,
575 specInfo = emptyCoreRules,
576 unfoldingInfo = noUnfolding})
579 is_fragile_inline_prag = case inline_prag of
580 ICanSafelyBeINLINEd _ _ -> True
582 -- We used to say the dead-ness was fragile, but I don't
583 -- see why it is. Furthermore, deadness is a pain to lose;
584 -- see Simplify.mkDupableCont (Select ...)
589 -- Be careful not to destroy real 'pragma' info
590 safe_inline_prag | is_fragile_inline_prag = NoInlinePragInfo
591 | otherwise = inline_prag
595 @zapLamIdInfo@ is used for lambda binders that turn out to to be
596 part of an unsaturated lambda
599 zapLamIdInfo :: IdInfo -> Maybe IdInfo
600 zapLamIdInfo info@(IdInfo {inlinePragInfo = inline_prag, demandInfo = demand})
601 | is_safe_inline_prag && not (isStrict demand)
604 = Just (info {inlinePragInfo = safe_inline_prag,
605 demandInfo = wwLazy})
607 -- The "unsafe" prags are the ones that say I'm not in a lambda
608 -- because that might not be true for an unsaturated lambda
609 is_safe_inline_prag = case inline_prag of
610 ICanSafelyBeINLINEd NotInsideLam nalts -> False
613 safe_inline_prag = case inline_prag of
614 ICanSafelyBeINLINEd _ nalts
615 -> ICanSafelyBeINLINEd InsideLam nalts
620 zapIdInfoForStg :: IdInfo -> IdInfo
621 -- Return only the info needed for STG stuff
622 -- Namely, nothing, I think
623 zapIdInfoForStg info = vanillaIdInfo
627 %************************************************************************
629 \subsection[cpr-IdInfo]{Constructed Product Result info about an @Id@}
631 %************************************************************************
633 If the @Id@ is a function then it may have CPR info. A CPR analysis
634 phase detects whether:
638 The function's return value has a product type, i.e. an algebraic type
639 with a single constructor. Examples of such types are tuples and boxed
642 The function always 'constructs' the value that it is returning. It
643 must do this on every path through, and it's OK if it calls another
644 function which constructs the result.
647 If this is the case then we store a template which tells us the
648 function has the CPR property and which components of the result are
657 -- e.g. const 5 == CPRInfo [NoCPRInfo]
664 -- CPRInfo [CPRInfo [NoCPRInfo],
665 -- CPRInfo [NoCprInfo,
666 -- CPRInfo [NoCPRInfo],
673 seqCpr :: CprInfo -> ()
674 seqCpr (CPRInfo cs) = seqCprs cs
675 seqCpr NoCPRInfo = ()
678 seqCprs (c:cs) = seqCpr c `seq` seqCprs cs
681 noCprInfo = NoCPRInfo
683 ppCprInfo NoCPRInfo = empty
684 ppCprInfo c@(CPRInfo _)
685 = hsep [ptext SLIT("__M"), ppCprInfo' c]
687 ppCprInfo' NoCPRInfo = char '-'
688 ppCprInfo' (CPRInfo args) = parens (hcat (map ppCprInfo' args))
690 instance Outputable CprInfo where
693 instance Show CprInfo where
694 showsPrec p c = showsPrecSDoc p (ppr c)
698 %************************************************************************
700 \subsection[lbvar-IdInfo]{Lambda-bound var info about an @Id@}
702 %************************************************************************
704 If the @Id@ is a lambda-bound variable then it may have lambda-bound
705 var info. The usage analysis (UsageSP) detects whether the lambda
706 binding this var is a ``one-shot'' lambda; that is, whether it is
707 applied at most once.
709 This information may be useful in optimisation, as computations may
710 safely be floated inside such a lambda without risk of duplicating
717 | IsOneShotLambda -- The lambda that binds this Id is applied
719 -- HACK ALERT! placing this info here is a short-term hack,
720 -- but it minimises changes to the rest of the compiler.
721 -- Hack agreed by SLPJ/KSW 1999-04.
723 seqLBVar l = l `seq` ()
727 noLBVarInfo = NoLBVarInfo
729 -- not safe to print or parse LBVarInfo because it is not really a
730 -- property of the definition, but a property of the context.
731 pprLBVarInfo NoLBVarInfo = empty
732 pprLBVarInfo IsOneShotLambda = getPprStyle $ \ sty ->
733 if ifaceStyle sty then empty
734 else ptext SLIT("OneShot")
736 instance Outputable LBVarInfo where
739 instance Show LBVarInfo where
740 showsPrec p c = showsPrecSDoc p (ppr c)