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, 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,
33 strictnessInfo, setStrictnessInfo,
36 WorkerInfo(..), workerExists, wrapperArity, workerId,
37 workerInfo, setWorkerInfo, ppWorkerInfo,
40 unfoldingInfo, setUnfoldingInfo,
43 demandInfo, setDemandInfo,
47 inlinePragInfo, setInlinePragInfo, pprInlinePragInfo,
48 isNeverInlinePrag, neverInlinePrag,
51 OccInfo(..), isFragileOcc, isDeadOcc, isLoopBreaker,
52 InsideLam, OneBranch, insideLam, notInsideLam, oneBranch, notOneBranch,
56 specInfo, setSpecInfo,
59 UpdateInfo, UpdateSpec,
60 mkUpdateInfo, updateInfo, updateInfoMaybe, ppUpdateInfo, setUpdateInfo,
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 PrimOp ( PrimOp )
78 import BasicTypes ( OccInfo(..), isFragileOcc, isDeadOcc, seqOccInfo, isLoopBreaker,
79 InsideLam, insideLam, notInsideLam,
80 OneBranch, oneBranch, notOneBranch,
83 import DataCon ( DataCon )
84 import FieldLabel ( FieldLabel )
85 import Demand -- Lots of stuff
87 import Maybe ( isJust )
89 infixl 1 `setUpdateInfo`,
100 -- infixl so you can say (id `set` a `set` b)
103 An @IdInfo@ gives {\em optional} information about an @Id@. If
104 present it never lies, but it may not be present, in which case there
105 is always a conservative assumption which can be made.
107 There is one exception: the 'flavour' is *not* optional.
108 You must not discard it.
109 It used to be in Var.lhs, but that seems unclean.
111 Two @Id@s may have different info even though they have the same
112 @Unique@ (and are hence the same @Id@); for example, one might lack
113 the properties attached to the other.
115 The @IdInfo@ gives information about the value, or definition, of the
116 @Id@. It does {\em not} contain information about the @Id@'s usage
117 (except for @DemandInfo@? ToDo). (@lbvarInfo@ is also a marginal
123 flavourInfo :: IdFlavour, -- NOT OPTIONAL
124 arityInfo :: ArityInfo, -- Its arity
125 demandInfo :: Demand, -- Whether or not it is definitely demanded
126 specInfo :: CoreRules, -- Specialisations of this function which exist
127 strictnessInfo :: StrictnessInfo, -- Strictness properties
128 workerInfo :: WorkerInfo, -- Pointer to Worker Function
129 unfoldingInfo :: Unfolding, -- Its unfolding
130 updateInfo :: UpdateInfo, -- Which args should be updated
132 cprInfo :: CprInfo, -- Function always constructs a product result
133 lbvarInfo :: LBVarInfo, -- Info about a lambda-bound variable
134 inlinePragInfo :: InlinePragInfo, -- Inline pragma
135 occInfo :: OccInfo -- How it occurs
138 seqIdInfo :: IdInfo -> ()
139 seqIdInfo (IdInfo {}) = ()
141 megaSeqIdInfo :: IdInfo -> ()
143 = seqFlavour (flavourInfo info) `seq`
144 seqArity (arityInfo info) `seq`
145 seqDemand (demandInfo info) `seq`
146 seqRules (specInfo info) `seq`
147 seqStrictnessInfo (strictnessInfo info) `seq`
148 seqWorker (workerInfo info) `seq`
150 -- seqUnfolding (unfoldingInfo info) `seq`
151 -- Omitting this improves runtimes a little, presumably because
152 -- some unfoldings are not calculated at all
154 seqCaf (cafInfo info) `seq`
155 seqCpr (cprInfo info) `seq`
156 seqLBVar (lbvarInfo info) `seq`
157 seqOccInfo (occInfo info)
163 setWorkerInfo info wk = wk `seq` info { workerInfo = wk }
164 setSpecInfo info sp = PSEQ sp (info { specInfo = sp })
165 setInlinePragInfo info pr = pr `seq` info { inlinePragInfo = pr }
166 setOccInfo info oc = oc `seq` info { occInfo = oc }
167 setStrictnessInfo info st = st `seq` info { strictnessInfo = st }
168 -- Try to avoid spack leaks by seq'ing
170 setUnfoldingInfo info uf
171 | isEvaldUnfolding uf && isStrict (demandInfo info)
172 -- If the unfolding is a value, the demand info may
173 -- go pear-shaped, so we nuke it. Example:
175 -- case x of (p,q) -> h p q x
176 -- Here x is certainly demanded. But after we've nuked
177 -- the case, we'll get just
178 -- let x = (a,b) in h a b x
179 -- and now x is not demanded (I'm assuming h is lazy)
180 -- This really happens. The solution here is a bit ad hoc...
181 = info { unfoldingInfo = uf, demandInfo = wwLazy }
184 -- We do *not* seq on the unfolding info, For some reason, doing so
185 -- actually increases residency significantly.
186 = info { unfoldingInfo = uf }
188 setUpdateInfo info ud = info { updateInfo = ud }
189 setDemandInfo info dd = info { demandInfo = dd }
190 setArityInfo info ar = info { arityInfo = ar }
191 setCafInfo info cf = info { cafInfo = cf }
192 setCprInfo info cp = info { cprInfo = cp }
193 setLBVarInfo info lb = info { lbvarInfo = lb }
195 setNoDiscardInfo info = case flavourInfo info of
196 VanillaId -> info { flavourInfo = NoDiscardId }
198 zapSpecPragInfo info = case flavourInfo info of
199 SpecPragmaId -> info { flavourInfo = VanillaId }
205 vanillaIdInfo :: IdInfo
206 vanillaIdInfo = mkIdInfo VanillaId
208 mkIdInfo :: IdFlavour -> IdInfo
209 mkIdInfo flv = IdInfo {
211 arityInfo = UnknownArity,
213 specInfo = emptyCoreRules,
214 workerInfo = NoWorker,
215 strictnessInfo = NoStrictnessInfo,
216 unfoldingInfo = noUnfolding,
217 updateInfo = NoUpdateInfo,
218 cafInfo = MayHaveCafRefs,
220 lbvarInfo = NoLBVarInfo,
221 inlinePragInfo = NoInlinePragInfo,
227 %************************************************************************
231 %************************************************************************
235 = VanillaId -- Most Ids are like this
236 | DataConId DataCon -- The Id for a data constructor *worker*
237 | DataConWrapId DataCon -- The Id for a data constructor *wrapper*
238 -- [the only reasons we need to know is so that
239 -- a) we can suppress printing a definition in the interface file
240 -- b) when typechecking a pattern we can get from the
241 -- Id back to the data con]
242 | PrimOpId PrimOp -- The Id for a primitive operator
243 | RecordSelId FieldLabel -- The Id for a record selector
244 | SpecPragmaId -- Don't discard these
245 | NoDiscardId -- Don't discard these either
247 ppFlavourInfo :: IdFlavour -> SDoc
248 ppFlavourInfo VanillaId = empty
249 ppFlavourInfo (DataConId _) = ptext SLIT("[DataCon]")
250 ppFlavourInfo (DataConWrapId _) = ptext SLIT("[DataConWrapper]")
251 ppFlavourInfo (PrimOpId _) = ptext SLIT("[PrimOp]")
252 ppFlavourInfo (RecordSelId _) = ptext SLIT("[RecSel]")
253 ppFlavourInfo SpecPragmaId = ptext SLIT("[SpecPrag]")
254 ppFlavourInfo NoDiscardId = ptext SLIT("[NoDiscard]")
256 seqFlavour :: IdFlavour -> ()
257 seqFlavour f = f `seq` ()
260 The @SpecPragmaId@ exists only to make Ids that are
261 on the *LHS* of bindings created by SPECIALISE pragmas;
263 The SpecPragmaId is never itself mentioned; it
264 exists solely so that the specialiser will find
265 the call to f, and make specialised version of it.
266 The SpecPragmaId binding is discarded by the specialiser
267 when it gathers up overloaded calls.
268 Meanwhile, it is not discarded as dead code.
271 %************************************************************************
273 \subsection[arity-IdInfo]{Arity info about an @Id@}
275 %************************************************************************
277 For locally-defined Ids, the code generator maintains its own notion
278 of their arities; so it should not be asking... (but other things
279 besides the code-generator need arity info!)
283 = UnknownArity -- No idea
285 | ArityExactly Arity -- Arity is exactly this. We use this when importing a
286 -- function; it's already been compiled and we know its
289 | ArityAtLeast Arity -- A partial application of this Id to up to n-1 value arguments
290 -- does essentially no work. That is not necessarily the
291 -- same as saying that it has n leading lambdas, because coerces
292 -- may get in the way.
294 -- functions in the module being compiled. Their arity
295 -- might increase later in the compilation process, if
296 -- an extra lambda floats up to the binding site.
299 seqArity :: ArityInfo -> ()
300 seqArity a = arityLowerBound a `seq` ()
302 exactArity = ArityExactly
303 atLeastArity = ArityAtLeast
304 unknownArity = UnknownArity
306 arityLowerBound :: ArityInfo -> Arity
307 arityLowerBound UnknownArity = 0
308 arityLowerBound (ArityAtLeast n) = n
309 arityLowerBound (ArityExactly n) = n
311 hasArity :: ArityInfo -> Bool
312 hasArity UnknownArity = False
313 hasArity other = True
315 ppArityInfo UnknownArity = empty
316 ppArityInfo (ArityExactly arity) = hsep [ptext SLIT("__A"), int arity]
317 ppArityInfo (ArityAtLeast arity) = hsep [ptext SLIT("__AL"), int arity]
320 %************************************************************************
322 \subsection{Inline-pragma information}
324 %************************************************************************
329 | IMustNotBeINLINEd Bool -- True <=> came from an INLINE prag, False <=> came from a NOINLINE prag
330 (Maybe Int) -- Phase number from pragma, if any
332 -- The True, Nothing case doesn't need to be recorded
334 -- SEE COMMENTS WITH CoreUnfold.blackListed on the
335 -- exact significance of the IMustNotBeINLINEd pragma
337 isNeverInlinePrag :: InlinePragInfo -> Bool
338 isNeverInlinePrag (IMustNotBeINLINEd _ Nothing) = True
339 isNeverInlinePrag other = False
341 neverInlinePrag :: InlinePragInfo
342 neverInlinePrag = IMustNotBeINLINEd True{-should be False? --SDM -} Nothing
344 instance Outputable InlinePragInfo where
345 -- This is now parsed in interface files
346 ppr NoInlinePragInfo = empty
347 ppr other_prag = ptext SLIT("__U") <> pprInlinePragInfo other_prag
349 pprInlinePragInfo NoInlinePragInfo = empty
350 pprInlinePragInfo (IMustNotBeINLINEd True Nothing) = empty
351 pprInlinePragInfo (IMustNotBeINLINEd True (Just n)) = brackets (int n)
352 pprInlinePragInfo (IMustNotBeINLINEd False Nothing) = brackets (char '!')
353 pprInlinePragInfo (IMustNotBeINLINEd False (Just n)) = brackets (char '!' <> int n)
355 instance Show InlinePragInfo where
356 showsPrec p prag = showsPrecSDoc p (ppr prag)
360 %************************************************************************
362 \subsection[worker-IdInfo]{Worker info about an @Id@}
364 %************************************************************************
366 If this Id has a worker then we store a reference to it. Worker
367 functions are generated by the worker/wrapper pass. This uses
368 information from the strictness and CPR analyses.
370 There might not be a worker, even for a strict function, because:
371 (a) the function might be small enough to inline, so no need
373 (b) the strictness info might be "SSS" or something, so no w/w split.
377 data WorkerInfo = NoWorker
379 -- The Arity is the arity of the *wrapper* at the moment of the
380 -- w/w split. See comments in MkIface.ifaceId, with the 'Worker' code.
382 seqWorker :: WorkerInfo -> ()
383 seqWorker (HasWorker id _) = id `seq` ()
384 seqWorker NoWorker = ()
386 ppWorkerInfo NoWorker = empty
387 ppWorkerInfo (HasWorker wk_id _) = ptext SLIT("__P") <+> ppr wk_id
389 noWorkerInfo = NoWorker
391 workerExists :: WorkerInfo -> Bool
392 workerExists NoWorker = False
393 workerExists (HasWorker _ _) = True
395 workerId :: WorkerInfo -> Id
396 workerId (HasWorker id _) = id
398 wrapperArity :: WorkerInfo -> Arity
399 wrapperArity (HasWorker _ a) = a
403 %************************************************************************
405 \subsection[update-IdInfo]{Update-analysis info about an @Id@}
407 %************************************************************************
412 | SomeUpdateInfo UpdateSpec
414 -- we need Eq/Ord to cross-chk update infos in interfaces
416 -- the form in which we pass update-analysis info between modules:
417 type UpdateSpec = [Int]
421 mkUpdateInfo = SomeUpdateInfo
423 updateInfoMaybe NoUpdateInfo = Nothing
424 updateInfoMaybe (SomeUpdateInfo []) = Nothing
425 updateInfoMaybe (SomeUpdateInfo u) = Just u
428 Text instance so that the update annotations can be read in.
431 ppUpdateInfo NoUpdateInfo = empty
432 ppUpdateInfo (SomeUpdateInfo []) = empty
433 ppUpdateInfo (SomeUpdateInfo spec) = (<>) (ptext SLIT("__UA ")) (hcat (map int spec))
434 -- was "__U "; changed to avoid conflict with unfoldings. KSW 1999-07.
437 %************************************************************************
439 \subsection[CAF-IdInfo]{CAF-related information}
441 %************************************************************************
443 This information is used to build Static Reference Tables (see
444 simplStg/ComputeSRT.lhs).
448 = MayHaveCafRefs -- either:
449 -- (1) A function or static constructor
450 -- that refers to one or more CAFs,
451 -- (2) A real live CAF
453 | NoCafRefs -- A function or static constructor
454 -- that refers to no CAFs.
456 -- LATER: not sure how easy this is...
460 seqCaf c = c `seq` ()
462 ppCafInfo NoCafRefs = ptext SLIT("__C")
463 ppCafInfo MayHaveCafRefs = empty
467 %************************************************************************
469 \subsection[cpr-IdInfo]{Constructed Product Result info about an @Id@}
471 %************************************************************************
473 If the @Id@ is a function then it may have CPR info. A CPR analysis
474 phase detects whether:
478 The function's return value has a product type, i.e. an algebraic type
479 with a single constructor. Examples of such types are tuples and boxed
482 The function always 'constructs' the value that it is returning. It
483 must do this on every path through, and it's OK if it calls another
484 function which constructs the result.
487 If this is the case then we store a template which tells us the
488 function has the CPR property and which components of the result are
494 | ReturnsCPR -- Yes, this function returns a constructed product
495 -- Implicitly, this means "after the function has been applied
496 -- to all its arguments", so the worker/wrapper builder in
497 -- WwLib.mkWWcpr checks that that it is indeed saturated before
498 -- making use of the CPR info
500 -- We used to keep nested info about sub-components, but
501 -- we never used it so I threw it away
505 seqCpr :: CprInfo -> ()
506 seqCpr ReturnsCPR = ()
507 seqCpr NoCPRInfo = ()
509 noCprInfo = NoCPRInfo
511 ppCprInfo NoCPRInfo = empty
512 ppCprInfo ReturnsCPR = ptext SLIT("__M")
514 instance Outputable CprInfo where
517 instance Show CprInfo where
518 showsPrec p c = showsPrecSDoc p (ppr c)
522 %************************************************************************
524 \subsection[lbvar-IdInfo]{Lambda-bound var info about an @Id@}
526 %************************************************************************
528 If the @Id@ is a lambda-bound variable then it may have lambda-bound
529 var info. The usage analysis (UsageSP) detects whether the lambda
530 binding this var is a ``one-shot'' lambda; that is, whether it is
531 applied at most once.
533 This information may be useful in optimisation, as computations may
534 safely be floated inside such a lambda without risk of duplicating
541 | IsOneShotLambda -- The lambda that binds this Id is applied
543 -- HACK ALERT! placing this info here is a short-term hack,
544 -- but it minimises changes to the rest of the compiler.
545 -- Hack agreed by SLPJ/KSW 1999-04.
547 seqLBVar l = l `seq` ()
551 noLBVarInfo = NoLBVarInfo
553 -- not safe to print or parse LBVarInfo because it is not really a
554 -- property of the definition, but a property of the context.
555 pprLBVarInfo NoLBVarInfo = empty
556 pprLBVarInfo IsOneShotLambda = getPprStyle $ \ sty ->
557 if ifaceStyle sty then empty
558 else ptext SLIT("OneShot")
560 instance Outputable LBVarInfo where
563 instance Show LBVarInfo where
564 showsPrec p c = showsPrecSDoc p (ppr c)
568 %************************************************************************
570 \subsection{Bulk operations on IdInfo}
572 %************************************************************************
574 zapFragileInfo is used when cloning binders, mainly in the
575 simplifier. We must forget about used-once information because that
576 isn't necessarily correct in the transformed program.
577 Also forget specialisations and unfoldings because they would need
578 substitution to be correct. (They get pinned back on separately.)
581 zapFragileInfo :: IdInfo -> Maybe IdInfo
582 zapFragileInfo info@(IdInfo {occInfo = occ,
585 unfoldingInfo = unfolding})
586 | not (isFragileOcc occ)
587 -- We must forget about whether it was marked safe-to-inline,
588 -- because that isn't necessarily true in the simplified expression.
589 -- This is important because expressions may be re-simplified
590 -- We don't zap deadness or loop-breaker-ness.
591 -- The latter is important because it tells MkIface not to
592 -- spit out an inlining for the thing. The former doesn't
593 -- seem so important, but there's no harm.
595 && isEmptyCoreRules rules
596 -- Specialisations would need substituting. They get pinned
597 -- back on separately.
599 && not (workerExists wrkr)
601 && not (hasUnfolding unfolding)
602 -- This is very important; occasionally a let-bound binder is used
603 -- as a binder in some lambda, in which case its unfolding is utterly
604 -- bogus. Also the unfolding uses old binders so if we left it we'd
605 -- have to substitute it. Much better simply to give the Id a new
606 -- unfolding each time, which is what the simplifier does.
610 = Just (info {occInfo = robust_occ_info,
611 workerInfo = noWorkerInfo,
612 specInfo = emptyCoreRules,
613 unfoldingInfo = noUnfolding})
615 -- It's important to keep the loop-breaker info,
616 -- because the substitution doesn't remember it.
617 robust_occ_info = case occ of
618 OneOcc _ _ -> NoOccInfo
622 @zapLamInfo@ is used for lambda binders that turn out to to be
623 part of an unsaturated lambda
626 zapLamInfo :: IdInfo -> Maybe IdInfo
627 zapLamInfo info@(IdInfo {occInfo = occ, demandInfo = demand})
628 | is_safe_occ && not (isStrict demand)
631 = Just (info {occInfo = safe_occ,
632 demandInfo = wwLazy})
634 -- The "unsafe" occ info is the ones that say I'm not in a lambda
635 -- because that might not be true for an unsaturated lambda
636 is_safe_occ = case occ of
637 OneOcc in_lam once -> in_lam
640 safe_occ = case occ of
641 OneOcc _ once -> OneOcc insideLam once
646 copyIdInfo is used when shorting out a top-level binding
649 where f is exported. We are going to swizzle it around to
652 but we must be careful to combine their IdInfos right.
653 The fact that things can go wrong here is a bad sign, but I can't see
654 how to make it 'patently right', so copyIdInfo is derived (pretty much) by trial and error
656 Here 'from' is f_local, 'to' is f, and the result is attached to f
659 copyIdInfo :: IdInfo -- From
661 -> IdInfo -- To, updated with stuff from From; except flavour unchanged
662 copyIdInfo from to = from { flavourInfo = flavourInfo to,
663 specInfo = specInfo to,
664 inlinePragInfo = inlinePragInfo to
666 -- It's important to preserve the inline pragma on 'f'; e.g. consider
667 -- {-# NOINLINE f #-}
670 -- similarly, transformation rules may be attached to f
671 -- and we want to preserve them.
673 -- On the other hand, we want the strictness info from f_local.