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,
50 OccInfo(..), isFragileOccInfo,
51 InsideLam, OneBranch, insideLam, notInsideLam, oneBranch, notOneBranch,
55 specInfo, setSpecInfo,
58 UpdateInfo, UpdateSpec,
59 mkUpdateInfo, updateInfo, updateInfoMaybe, ppUpdateInfo, setUpdateInfo,
62 CafInfo(..), cafInfo, setCafInfo, ppCafInfo,
64 -- Constructed Product Result Info
65 CprInfo(..), cprInfo, setCprInfo, ppCprInfo, noCprInfo,
67 -- Lambda-bound variable info
68 LBVarInfo(..), lbvarInfo, setLBVarInfo, noLBVarInfo
71 #include "HsVersions.h"
75 import PrimOp ( PrimOp )
77 import BasicTypes ( OccInfo(..), isFragileOccInfo, seqOccInfo,
78 InsideLam, insideLam, notInsideLam,
79 OneBranch, oneBranch, notOneBranch,
82 import DataCon ( DataCon )
83 import FieldLabel ( FieldLabel )
84 import Demand -- Lots of stuff
86 import Maybe ( isJust )
88 infixl 1 `setUpdateInfo`,
99 -- infixl so you can say (id `set` a `set` b)
102 An @IdInfo@ gives {\em optional} information about an @Id@. If
103 present it never lies, but it may not be present, in which case there
104 is always a conservative assumption which can be made.
106 There is one exception: the 'flavour' is *not* optional.
107 You must not discard it.
108 It used to be in Var.lhs, but that seems unclean.
110 Two @Id@s may have different info even though they have the same
111 @Unique@ (and are hence the same @Id@); for example, one might lack
112 the properties attached to the other.
114 The @IdInfo@ gives information about the value, or definition, of the
115 @Id@. It does {\em not} contain information about the @Id@'s usage
116 (except for @DemandInfo@? ToDo). (@lbvarInfo@ is also a marginal
122 flavourInfo :: IdFlavour, -- NOT OPTIONAL
123 arityInfo :: ArityInfo, -- Its arity
124 demandInfo :: Demand, -- Whether or not it is definitely demanded
125 specInfo :: CoreRules, -- Specialisations of this function which exist
126 strictnessInfo :: StrictnessInfo, -- Strictness properties
127 workerInfo :: WorkerInfo, -- Pointer to Worker Function
128 unfoldingInfo :: Unfolding, -- Its unfolding
129 updateInfo :: UpdateInfo, -- Which args should be updated
131 cprInfo :: CprInfo, -- Function always constructs a product result
132 lbvarInfo :: LBVarInfo, -- Info about a lambda-bound variable
133 inlinePragInfo :: InlinePragInfo, -- Inline pragma
134 occInfo :: OccInfo -- How it occurs
137 seqIdInfo :: IdInfo -> ()
138 seqIdInfo (IdInfo {}) = ()
140 megaSeqIdInfo :: IdInfo -> ()
142 = seqFlavour (flavourInfo info) `seq`
143 seqArity (arityInfo info) `seq`
144 seqDemand (demandInfo info) `seq`
145 seqRules (specInfo info) `seq`
146 seqStrictnessInfo (strictnessInfo info) `seq`
147 seqWorker (workerInfo info) `seq`
149 -- seqUnfolding (unfoldingInfo info) `seq`
150 -- Omitting this improves runtimes a little, presumably because
151 -- some unfoldings are not calculated at all
153 seqCaf (cafInfo info) `seq`
154 seqCpr (cprInfo info) `seq`
155 seqLBVar (lbvarInfo info) `seq`
156 seqOccInfo (occInfo info)
162 setWorkerInfo info wk = wk `seq` info { workerInfo = wk }
163 setSpecInfo info sp = PSEQ sp (info { specInfo = sp })
164 setInlinePragInfo info pr = pr `seq` info { inlinePragInfo = pr }
165 setOccInfo info oc = oc `seq` info { occInfo = oc }
166 setStrictnessInfo info st = st `seq` info { strictnessInfo = st }
167 -- Try to avoid spack leaks by seq'ing
169 setUnfoldingInfo info uf = info { unfoldingInfo = uf }
170 -- We do *not* seq on the unfolding info, For some reason, doing so
171 -- actually increases residency significantly.
173 setUpdateInfo info ud = info { updateInfo = ud }
174 setDemandInfo info dd = info { demandInfo = dd }
175 setArityInfo info ar = info { arityInfo = ar }
176 setCafInfo info cf = info { cafInfo = cf }
177 setCprInfo info cp = info { cprInfo = cp }
178 setLBVarInfo info lb = info { lbvarInfo = lb }
180 setNoDiscardInfo info = case flavourInfo info of
181 VanillaId -> info { flavourInfo = NoDiscardId }
183 zapSpecPragInfo info = case flavourInfo info of
184 SpecPragmaId -> info { flavourInfo = VanillaId }
190 vanillaIdInfo :: IdInfo
191 vanillaIdInfo = mkIdInfo VanillaId
193 mkIdInfo :: IdFlavour -> IdInfo
194 mkIdInfo flv = IdInfo {
196 arityInfo = UnknownArity,
198 specInfo = emptyCoreRules,
199 workerInfo = NoWorker,
200 strictnessInfo = NoStrictnessInfo,
201 unfoldingInfo = noUnfolding,
202 updateInfo = NoUpdateInfo,
203 cafInfo = MayHaveCafRefs,
205 lbvarInfo = NoLBVarInfo,
206 inlinePragInfo = NoInlinePragInfo,
212 %************************************************************************
216 %************************************************************************
220 = VanillaId -- Most Ids are like this
221 | DataConId DataCon -- The Id for a data constructor *worker*
222 | DataConWrapId DataCon -- The Id for a data constructor *wrapper*
223 -- [the only reasons we need to know is so that
224 -- a) we can suppress printing a definition in the interface file
225 -- b) when typechecking a pattern we can get from the
226 -- Id back to the data con]
227 | PrimOpId PrimOp -- The Id for a primitive operator
228 | RecordSelId FieldLabel -- The Id for a record selector
229 | SpecPragmaId -- Don't discard these
230 | NoDiscardId -- Don't discard these either
232 ppFlavourInfo :: IdFlavour -> SDoc
233 ppFlavourInfo VanillaId = empty
234 ppFlavourInfo (DataConId _) = ptext SLIT("[DataCon]")
235 ppFlavourInfo (DataConWrapId _) = ptext SLIT("[DataConWrapper]")
236 ppFlavourInfo (PrimOpId _) = ptext SLIT("[PrimOp]")
237 ppFlavourInfo (RecordSelId _) = ptext SLIT("[RecSel]")
238 ppFlavourInfo SpecPragmaId = ptext SLIT("[SpecPrag]")
239 ppFlavourInfo NoDiscardId = ptext SLIT("[NoDiscard]")
241 seqFlavour :: IdFlavour -> ()
242 seqFlavour f = f `seq` ()
245 The @SpecPragmaId@ exists only to make Ids that are
246 on the *LHS* of bindings created by SPECIALISE pragmas;
248 The SpecPragmaId is never itself mentioned; it
249 exists solely so that the specialiser will find
250 the call to f, and make specialised version of it.
251 The SpecPragmaId binding is discarded by the specialiser
252 when it gathers up overloaded calls.
253 Meanwhile, it is not discarded as dead code.
256 %************************************************************************
258 \subsection[arity-IdInfo]{Arity info about an @Id@}
260 %************************************************************************
262 For locally-defined Ids, the code generator maintains its own notion
263 of their arities; so it should not be asking... (but other things
264 besides the code-generator need arity info!)
268 = UnknownArity -- No idea
270 | ArityExactly Arity -- Arity is exactly this. We use this when importing a
271 -- function; it's already been compiled and we know its
274 | ArityAtLeast Arity -- Arity is this or greater. We attach this arity to
275 -- functions in the module being compiled. Their arity
276 -- might increase later in the compilation process, if
277 -- an extra lambda floats up to the binding site.
279 seqArity :: ArityInfo -> ()
280 seqArity a = arityLowerBound a `seq` ()
282 exactArity = ArityExactly
283 atLeastArity = ArityAtLeast
284 unknownArity = UnknownArity
286 arityLowerBound :: ArityInfo -> Arity
287 arityLowerBound UnknownArity = 0
288 arityLowerBound (ArityAtLeast n) = n
289 arityLowerBound (ArityExactly n) = n
291 hasArity :: ArityInfo -> Bool
292 hasArity UnknownArity = False
293 hasArity other = True
295 ppArityInfo UnknownArity = empty
296 ppArityInfo (ArityExactly arity) = hsep [ptext SLIT("__A"), int arity]
297 ppArityInfo (ArityAtLeast arity) = hsep [ptext SLIT("__AL"), int arity]
300 %************************************************************************
302 \subsection{Inline-pragma information}
304 %************************************************************************
309 | IMustNotBeINLINEd Bool -- True <=> came from an INLINE prag, False <=> came from a NOINLINE prag
310 (Maybe Int) -- Phase number from pragma, if any
311 -- The True, Nothing case doesn't need to be recorded
313 instance Outputable InlinePragInfo where
314 -- This is now parsed in interface files
315 ppr NoInlinePragInfo = empty
316 ppr other_prag = ptext SLIT("__U") <> pprInlinePragInfo other_prag
318 pprInlinePragInfo NoInlinePragInfo = empty
319 pprInlinePragInfo (IMustNotBeINLINEd True Nothing) = empty
320 pprInlinePragInfo (IMustNotBeINLINEd True (Just n)) = brackets (int n)
321 pprInlinePragInfo (IMustNotBeINLINEd False Nothing) = brackets (char '!')
322 pprInlinePragInfo (IMustNotBeINLINEd False (Just n)) = brackets (char '!' <> int n)
324 instance Show InlinePragInfo where
325 showsPrec p prag = showsPrecSDoc p (ppr prag)
329 %************************************************************************
331 \subsection[worker-IdInfo]{Worker info about an @Id@}
333 %************************************************************************
335 If this Id has a worker then we store a reference to it. Worker
336 functions are generated by the worker/wrapper pass. This uses
337 information from the strictness and CPR analyses.
339 There might not be a worker, even for a strict function, because:
340 (a) the function might be small enough to inline, so no need
342 (b) the strictness info might be "SSS" or something, so no w/w split.
346 data WorkerInfo = NoWorker
348 -- The Arity is the arity of the *wrapper* at the moment of the
349 -- w/w split. It had better be the same as the arity of the wrapper
350 -- at the moment it is spat into the interface file.
351 -- This Arity just lets us make a (hopefully redundant) sanity check
353 seqWorker :: WorkerInfo -> ()
354 seqWorker (HasWorker id _) = id `seq` ()
355 seqWorker NoWorker = ()
357 ppWorkerInfo NoWorker = empty
358 ppWorkerInfo (HasWorker wk_id _) = ptext SLIT("__P") <+> ppr wk_id
360 noWorkerInfo = NoWorker
362 workerExists :: WorkerInfo -> Bool
363 workerExists NoWorker = False
364 workerExists (HasWorker _ _) = True
366 workerId :: WorkerInfo -> Id
367 workerId (HasWorker id _) = id
369 wrapperArity :: WorkerInfo -> Arity
370 wrapperArity (HasWorker _ a) = a
374 %************************************************************************
376 \subsection[update-IdInfo]{Update-analysis info about an @Id@}
378 %************************************************************************
383 | SomeUpdateInfo UpdateSpec
385 -- we need Eq/Ord to cross-chk update infos in interfaces
387 -- the form in which we pass update-analysis info between modules:
388 type UpdateSpec = [Int]
392 mkUpdateInfo = SomeUpdateInfo
394 updateInfoMaybe NoUpdateInfo = Nothing
395 updateInfoMaybe (SomeUpdateInfo []) = Nothing
396 updateInfoMaybe (SomeUpdateInfo u) = Just u
399 Text instance so that the update annotations can be read in.
402 ppUpdateInfo NoUpdateInfo = empty
403 ppUpdateInfo (SomeUpdateInfo []) = empty
404 ppUpdateInfo (SomeUpdateInfo spec) = (<>) (ptext SLIT("__UA ")) (hcat (map int spec))
405 -- was "__U "; changed to avoid conflict with unfoldings. KSW 1999-07.
408 %************************************************************************
410 \subsection[CAF-IdInfo]{CAF-related information}
412 %************************************************************************
414 This information is used to build Static Reference Tables (see
415 simplStg/ComputeSRT.lhs).
419 = MayHaveCafRefs -- either:
420 -- (1) A function or static constructor
421 -- that refers to one or more CAFs,
422 -- (2) A real live CAF
424 | NoCafRefs -- A function or static constructor
425 -- that refers to no CAFs.
427 -- LATER: not sure how easy this is...
431 seqCaf c = c `seq` ()
433 ppCafInfo NoCafRefs = ptext SLIT("__C")
434 ppCafInfo MayHaveCafRefs = empty
438 %************************************************************************
440 \subsection[cpr-IdInfo]{Constructed Product Result info about an @Id@}
442 %************************************************************************
444 If the @Id@ is a function then it may have CPR info. A CPR analysis
445 phase detects whether:
449 The function's return value has a product type, i.e. an algebraic type
450 with a single constructor. Examples of such types are tuples and boxed
453 The function always 'constructs' the value that it is returning. It
454 must do this on every path through, and it's OK if it calls another
455 function which constructs the result.
458 If this is the case then we store a template which tells us the
459 function has the CPR property and which components of the result are
465 | ReturnsCPR -- Yes, this function returns a constructed product
466 -- Implicitly, this means "after the function has been applied
467 -- to all its arguments", so the worker/wrapper builder in
468 -- WwLib.mkWWcpr checks that that it is indeed saturated before
469 -- making use of the CPR info
471 -- We used to keep nested info about sub-components, but
472 -- we never used it so I threw it away
476 seqCpr :: CprInfo -> ()
477 seqCpr ReturnsCPR = ()
478 seqCpr NoCPRInfo = ()
480 noCprInfo = NoCPRInfo
482 ppCprInfo NoCPRInfo = empty
483 ppCprInfo ReturnsCPR = ptext SLIT("__M")
485 instance Outputable CprInfo where
488 instance Show CprInfo where
489 showsPrec p c = showsPrecSDoc p (ppr c)
493 %************************************************************************
495 \subsection[lbvar-IdInfo]{Lambda-bound var info about an @Id@}
497 %************************************************************************
499 If the @Id@ is a lambda-bound variable then it may have lambda-bound
500 var info. The usage analysis (UsageSP) detects whether the lambda
501 binding this var is a ``one-shot'' lambda; that is, whether it is
502 applied at most once.
504 This information may be useful in optimisation, as computations may
505 safely be floated inside such a lambda without risk of duplicating
512 | IsOneShotLambda -- The lambda that binds this Id is applied
514 -- HACK ALERT! placing this info here is a short-term hack,
515 -- but it minimises changes to the rest of the compiler.
516 -- Hack agreed by SLPJ/KSW 1999-04.
518 seqLBVar l = l `seq` ()
522 noLBVarInfo = NoLBVarInfo
524 -- not safe to print or parse LBVarInfo because it is not really a
525 -- property of the definition, but a property of the context.
526 pprLBVarInfo NoLBVarInfo = empty
527 pprLBVarInfo IsOneShotLambda = getPprStyle $ \ sty ->
528 if ifaceStyle sty then empty
529 else ptext SLIT("OneShot")
531 instance Outputable LBVarInfo where
534 instance Show LBVarInfo where
535 showsPrec p c = showsPrecSDoc p (ppr c)
539 %************************************************************************
541 \subsection{Bulk operations on IdInfo}
543 %************************************************************************
545 zapFragileInfo is used when cloning binders, mainly in the
546 simplifier. We must forget about used-once information because that
547 isn't necessarily correct in the transformed program.
548 Also forget specialisations and unfoldings because they would need
549 substitution to be correct. (They get pinned back on separately.)
552 zapFragileInfo :: IdInfo -> Maybe IdInfo
553 zapFragileInfo info@(IdInfo {occInfo = occ,
556 unfoldingInfo = unfolding})
557 | not (isFragileOccInfo occ)
558 -- We must forget about whether it was marked safe-to-inline,
559 -- because that isn't necessarily true in the simplified expression.
560 -- This is important because expressions may be re-simplified
561 -- We don't zap deadness or loop-breaker-ness.
562 -- The latter is important because it tells MkIface not to
563 -- spit out an inlining for the thing. The former doesn't
564 -- seem so important, but there's no harm.
566 && isEmptyCoreRules rules
567 -- Specialisations would need substituting. They get pinned
568 -- back on separately.
570 && not (workerExists wrkr)
572 && not (hasUnfolding unfolding)
573 -- This is very important; occasionally a let-bound binder is used
574 -- as a binder in some lambda, in which case its unfolding is utterly
575 -- bogus. Also the unfolding uses old binders so if we left it we'd
576 -- have to substitute it. Much better simply to give the Id a new
577 -- unfolding each time, which is what the simplifier does.
581 = Just (info {occInfo = robust_occ_info,
582 workerInfo = noWorkerInfo,
583 specInfo = emptyCoreRules,
584 unfoldingInfo = noUnfolding})
586 -- It's important to keep the loop-breaker info,
587 -- because the substitution doesn't remember it.
588 robust_occ_info = case occ of
589 OneOcc _ _ -> NoOccInfo
593 @zapLamInfo@ is used for lambda binders that turn out to to be
594 part of an unsaturated lambda
597 zapLamInfo :: IdInfo -> Maybe IdInfo
598 zapLamInfo info@(IdInfo {occInfo = occ, demandInfo = demand})
599 | is_safe_occ && not (isStrict demand)
602 = Just (info {occInfo = safe_occ,
603 demandInfo = wwLazy})
605 -- The "unsafe" occ info is the ones that say I'm not in a lambda
606 -- because that might not be true for an unsaturated lambda
607 is_safe_occ = case occ of
608 OneOcc in_lam once -> in_lam
611 safe_occ = case occ of
612 OneOcc _ once -> OneOcc insideLam once
617 copyIdInfo is used when shorting out a top-level binding
620 where f is exported. We are going to swizzle it around to
623 but we must be careful to combine their IdInfos right.
624 The fact that things can go wrong here is a bad sign, but I can't see
625 how to make it 'patently right', so copyIdInfo is derived (pretty much) by trial and error
627 Here 'from' is f_local, 'to' is f, and the result is attached to f
630 copyIdInfo :: IdInfo -- From
632 -> IdInfo -- To, updated with stuff from From; except flavour unchanged
633 copyIdInfo from to = from { flavourInfo = flavourInfo to,
634 specInfo = specInfo to,
635 inlinePragInfo = inlinePragInfo to
637 -- It's important to preserve the inline pragma on 'f'; e.g. consider
638 -- {-# NOINLINE f #-}
641 -- similarly, transformation rules may be attached to f
642 -- and we want to preserve them.
644 -- On the other hand, we want the strictness info from f_local.