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
170 | isEvaldUnfolding uf && isStrict (demandInfo info)
171 -- If the unfolding is a value, the demand info may
172 -- go pear-shaped, so we nuke it. Example:
174 -- case x of (p,q) -> h p q x
175 -- Here x is certainly demanded. But after we've nuked
176 -- the case, we'll get just
177 -- let x = (a,b) in h a b x
178 -- and now x is not demanded (I'm assuming h is lazy)
179 -- This really happens. The solution here is a bit ad hoc...
180 = info { unfoldingInfo = uf, demandInfo = wwLazy }
183 -- We do *not* seq on the unfolding info, For some reason, doing so
184 -- actually increases residency significantly.
185 = info { unfoldingInfo = uf }
187 setUpdateInfo info ud = info { updateInfo = ud }
188 setDemandInfo info dd = info { demandInfo = dd }
189 setArityInfo info ar = info { arityInfo = ar }
190 setCafInfo info cf = info { cafInfo = cf }
191 setCprInfo info cp = info { cprInfo = cp }
192 setLBVarInfo info lb = info { lbvarInfo = lb }
194 setNoDiscardInfo info = case flavourInfo info of
195 VanillaId -> info { flavourInfo = NoDiscardId }
197 zapSpecPragInfo info = case flavourInfo info of
198 SpecPragmaId -> info { flavourInfo = VanillaId }
204 vanillaIdInfo :: IdInfo
205 vanillaIdInfo = mkIdInfo VanillaId
207 mkIdInfo :: IdFlavour -> IdInfo
208 mkIdInfo flv = IdInfo {
210 arityInfo = UnknownArity,
212 specInfo = emptyCoreRules,
213 workerInfo = NoWorker,
214 strictnessInfo = NoStrictnessInfo,
215 unfoldingInfo = noUnfolding,
216 updateInfo = NoUpdateInfo,
217 cafInfo = MayHaveCafRefs,
219 lbvarInfo = NoLBVarInfo,
220 inlinePragInfo = NoInlinePragInfo,
226 %************************************************************************
230 %************************************************************************
234 = VanillaId -- Most Ids are like this
235 | DataConId DataCon -- The Id for a data constructor *worker*
236 | DataConWrapId DataCon -- The Id for a data constructor *wrapper*
237 -- [the only reasons we need to know is so that
238 -- a) we can suppress printing a definition in the interface file
239 -- b) when typechecking a pattern we can get from the
240 -- Id back to the data con]
241 | PrimOpId PrimOp -- The Id for a primitive operator
242 | RecordSelId FieldLabel -- The Id for a record selector
243 | SpecPragmaId -- Don't discard these
244 | NoDiscardId -- Don't discard these either
246 ppFlavourInfo :: IdFlavour -> SDoc
247 ppFlavourInfo VanillaId = empty
248 ppFlavourInfo (DataConId _) = ptext SLIT("[DataCon]")
249 ppFlavourInfo (DataConWrapId _) = ptext SLIT("[DataConWrapper]")
250 ppFlavourInfo (PrimOpId _) = ptext SLIT("[PrimOp]")
251 ppFlavourInfo (RecordSelId _) = ptext SLIT("[RecSel]")
252 ppFlavourInfo SpecPragmaId = ptext SLIT("[SpecPrag]")
253 ppFlavourInfo NoDiscardId = ptext SLIT("[NoDiscard]")
255 seqFlavour :: IdFlavour -> ()
256 seqFlavour f = f `seq` ()
259 The @SpecPragmaId@ exists only to make Ids that are
260 on the *LHS* of bindings created by SPECIALISE pragmas;
262 The SpecPragmaId is never itself mentioned; it
263 exists solely so that the specialiser will find
264 the call to f, and make specialised version of it.
265 The SpecPragmaId binding is discarded by the specialiser
266 when it gathers up overloaded calls.
267 Meanwhile, it is not discarded as dead code.
270 %************************************************************************
272 \subsection[arity-IdInfo]{Arity info about an @Id@}
274 %************************************************************************
276 For locally-defined Ids, the code generator maintains its own notion
277 of their arities; so it should not be asking... (but other things
278 besides the code-generator need arity info!)
282 = UnknownArity -- No idea
284 | ArityExactly Arity -- Arity is exactly this. We use this when importing a
285 -- function; it's already been compiled and we know its
288 | ArityAtLeast Arity -- Arity is this or greater. We attach this arity to
289 -- functions in the module being compiled. Their arity
290 -- might increase later in the compilation process, if
291 -- an extra lambda floats up to the binding site.
293 seqArity :: ArityInfo -> ()
294 seqArity a = arityLowerBound a `seq` ()
296 exactArity = ArityExactly
297 atLeastArity = ArityAtLeast
298 unknownArity = UnknownArity
300 arityLowerBound :: ArityInfo -> Arity
301 arityLowerBound UnknownArity = 0
302 arityLowerBound (ArityAtLeast n) = n
303 arityLowerBound (ArityExactly n) = n
305 hasArity :: ArityInfo -> Bool
306 hasArity UnknownArity = False
307 hasArity other = True
309 ppArityInfo UnknownArity = empty
310 ppArityInfo (ArityExactly arity) = hsep [ptext SLIT("__A"), int arity]
311 ppArityInfo (ArityAtLeast arity) = hsep [ptext SLIT("__AL"), int arity]
314 %************************************************************************
316 \subsection{Inline-pragma information}
318 %************************************************************************
323 | IMustNotBeINLINEd Bool -- True <=> came from an INLINE prag, False <=> came from a NOINLINE prag
324 (Maybe Int) -- Phase number from pragma, if any
325 -- The True, Nothing case doesn't need to be recorded
327 instance Outputable InlinePragInfo where
328 -- This is now parsed in interface files
329 ppr NoInlinePragInfo = empty
330 ppr other_prag = ptext SLIT("__U") <> pprInlinePragInfo other_prag
332 pprInlinePragInfo NoInlinePragInfo = empty
333 pprInlinePragInfo (IMustNotBeINLINEd True Nothing) = empty
334 pprInlinePragInfo (IMustNotBeINLINEd True (Just n)) = brackets (int n)
335 pprInlinePragInfo (IMustNotBeINLINEd False Nothing) = brackets (char '!')
336 pprInlinePragInfo (IMustNotBeINLINEd False (Just n)) = brackets (char '!' <> int n)
338 instance Show InlinePragInfo where
339 showsPrec p prag = showsPrecSDoc p (ppr prag)
343 %************************************************************************
345 \subsection[worker-IdInfo]{Worker info about an @Id@}
347 %************************************************************************
349 If this Id has a worker then we store a reference to it. Worker
350 functions are generated by the worker/wrapper pass. This uses
351 information from the strictness and CPR analyses.
353 There might not be a worker, even for a strict function, because:
354 (a) the function might be small enough to inline, so no need
356 (b) the strictness info might be "SSS" or something, so no w/w split.
360 data WorkerInfo = NoWorker
362 -- The Arity is the arity of the *wrapper* at the moment of the
363 -- w/w split. It had better be the same as the arity of the wrapper
364 -- at the moment it is spat into the interface file.
365 -- This Arity just lets us make a (hopefully redundant) sanity check
367 seqWorker :: WorkerInfo -> ()
368 seqWorker (HasWorker id _) = id `seq` ()
369 seqWorker NoWorker = ()
371 ppWorkerInfo NoWorker = empty
372 ppWorkerInfo (HasWorker wk_id _) = ptext SLIT("__P") <+> ppr wk_id
374 noWorkerInfo = NoWorker
376 workerExists :: WorkerInfo -> Bool
377 workerExists NoWorker = False
378 workerExists (HasWorker _ _) = True
380 workerId :: WorkerInfo -> Id
381 workerId (HasWorker id _) = id
383 wrapperArity :: WorkerInfo -> Arity
384 wrapperArity (HasWorker _ a) = a
388 %************************************************************************
390 \subsection[update-IdInfo]{Update-analysis info about an @Id@}
392 %************************************************************************
397 | SomeUpdateInfo UpdateSpec
399 -- we need Eq/Ord to cross-chk update infos in interfaces
401 -- the form in which we pass update-analysis info between modules:
402 type UpdateSpec = [Int]
406 mkUpdateInfo = SomeUpdateInfo
408 updateInfoMaybe NoUpdateInfo = Nothing
409 updateInfoMaybe (SomeUpdateInfo []) = Nothing
410 updateInfoMaybe (SomeUpdateInfo u) = Just u
413 Text instance so that the update annotations can be read in.
416 ppUpdateInfo NoUpdateInfo = empty
417 ppUpdateInfo (SomeUpdateInfo []) = empty
418 ppUpdateInfo (SomeUpdateInfo spec) = (<>) (ptext SLIT("__UA ")) (hcat (map int spec))
419 -- was "__U "; changed to avoid conflict with unfoldings. KSW 1999-07.
422 %************************************************************************
424 \subsection[CAF-IdInfo]{CAF-related information}
426 %************************************************************************
428 This information is used to build Static Reference Tables (see
429 simplStg/ComputeSRT.lhs).
433 = MayHaveCafRefs -- either:
434 -- (1) A function or static constructor
435 -- that refers to one or more CAFs,
436 -- (2) A real live CAF
438 | NoCafRefs -- A function or static constructor
439 -- that refers to no CAFs.
441 -- LATER: not sure how easy this is...
445 seqCaf c = c `seq` ()
447 ppCafInfo NoCafRefs = ptext SLIT("__C")
448 ppCafInfo MayHaveCafRefs = empty
452 %************************************************************************
454 \subsection[cpr-IdInfo]{Constructed Product Result info about an @Id@}
456 %************************************************************************
458 If the @Id@ is a function then it may have CPR info. A CPR analysis
459 phase detects whether:
463 The function's return value has a product type, i.e. an algebraic type
464 with a single constructor. Examples of such types are tuples and boxed
467 The function always 'constructs' the value that it is returning. It
468 must do this on every path through, and it's OK if it calls another
469 function which constructs the result.
472 If this is the case then we store a template which tells us the
473 function has the CPR property and which components of the result are
479 | ReturnsCPR -- Yes, this function returns a constructed product
480 -- Implicitly, this means "after the function has been applied
481 -- to all its arguments", so the worker/wrapper builder in
482 -- WwLib.mkWWcpr checks that that it is indeed saturated before
483 -- making use of the CPR info
485 -- We used to keep nested info about sub-components, but
486 -- we never used it so I threw it away
490 seqCpr :: CprInfo -> ()
491 seqCpr ReturnsCPR = ()
492 seqCpr NoCPRInfo = ()
494 noCprInfo = NoCPRInfo
496 ppCprInfo NoCPRInfo = empty
497 ppCprInfo ReturnsCPR = ptext SLIT("__M")
499 instance Outputable CprInfo where
502 instance Show CprInfo where
503 showsPrec p c = showsPrecSDoc p (ppr c)
507 %************************************************************************
509 \subsection[lbvar-IdInfo]{Lambda-bound var info about an @Id@}
511 %************************************************************************
513 If the @Id@ is a lambda-bound variable then it may have lambda-bound
514 var info. The usage analysis (UsageSP) detects whether the lambda
515 binding this var is a ``one-shot'' lambda; that is, whether it is
516 applied at most once.
518 This information may be useful in optimisation, as computations may
519 safely be floated inside such a lambda without risk of duplicating
526 | IsOneShotLambda -- The lambda that binds this Id is applied
528 -- HACK ALERT! placing this info here is a short-term hack,
529 -- but it minimises changes to the rest of the compiler.
530 -- Hack agreed by SLPJ/KSW 1999-04.
532 seqLBVar l = l `seq` ()
536 noLBVarInfo = NoLBVarInfo
538 -- not safe to print or parse LBVarInfo because it is not really a
539 -- property of the definition, but a property of the context.
540 pprLBVarInfo NoLBVarInfo = empty
541 pprLBVarInfo IsOneShotLambda = getPprStyle $ \ sty ->
542 if ifaceStyle sty then empty
543 else ptext SLIT("OneShot")
545 instance Outputable LBVarInfo where
548 instance Show LBVarInfo where
549 showsPrec p c = showsPrecSDoc p (ppr c)
553 %************************************************************************
555 \subsection{Bulk operations on IdInfo}
557 %************************************************************************
559 zapFragileInfo is used when cloning binders, mainly in the
560 simplifier. We must forget about used-once information because that
561 isn't necessarily correct in the transformed program.
562 Also forget specialisations and unfoldings because they would need
563 substitution to be correct. (They get pinned back on separately.)
566 zapFragileInfo :: IdInfo -> Maybe IdInfo
567 zapFragileInfo info@(IdInfo {occInfo = occ,
570 unfoldingInfo = unfolding})
571 | not (isFragileOccInfo occ)
572 -- We must forget about whether it was marked safe-to-inline,
573 -- because that isn't necessarily true in the simplified expression.
574 -- This is important because expressions may be re-simplified
575 -- We don't zap deadness or loop-breaker-ness.
576 -- The latter is important because it tells MkIface not to
577 -- spit out an inlining for the thing. The former doesn't
578 -- seem so important, but there's no harm.
580 && isEmptyCoreRules rules
581 -- Specialisations would need substituting. They get pinned
582 -- back on separately.
584 && not (workerExists wrkr)
586 && not (hasUnfolding unfolding)
587 -- This is very important; occasionally a let-bound binder is used
588 -- as a binder in some lambda, in which case its unfolding is utterly
589 -- bogus. Also the unfolding uses old binders so if we left it we'd
590 -- have to substitute it. Much better simply to give the Id a new
591 -- unfolding each time, which is what the simplifier does.
595 = Just (info {occInfo = robust_occ_info,
596 workerInfo = noWorkerInfo,
597 specInfo = emptyCoreRules,
598 unfoldingInfo = noUnfolding})
600 -- It's important to keep the loop-breaker info,
601 -- because the substitution doesn't remember it.
602 robust_occ_info = case occ of
603 OneOcc _ _ -> NoOccInfo
607 @zapLamInfo@ is used for lambda binders that turn out to to be
608 part of an unsaturated lambda
611 zapLamInfo :: IdInfo -> Maybe IdInfo
612 zapLamInfo info@(IdInfo {occInfo = occ, demandInfo = demand})
613 | is_safe_occ && not (isStrict demand)
616 = Just (info {occInfo = safe_occ,
617 demandInfo = wwLazy})
619 -- The "unsafe" occ info is the ones that say I'm not in a lambda
620 -- because that might not be true for an unsaturated lambda
621 is_safe_occ = case occ of
622 OneOcc in_lam once -> in_lam
625 safe_occ = case occ of
626 OneOcc _ once -> OneOcc insideLam once
631 copyIdInfo is used when shorting out a top-level binding
634 where f is exported. We are going to swizzle it around to
637 but we must be careful to combine their IdInfos right.
638 The fact that things can go wrong here is a bad sign, but I can't see
639 how to make it 'patently right', so copyIdInfo is derived (pretty much) by trial and error
641 Here 'from' is f_local, 'to' is f, and the result is attached to f
644 copyIdInfo :: IdInfo -- From
646 -> IdInfo -- To, updated with stuff from From; except flavour unchanged
647 copyIdInfo from to = from { flavourInfo = flavourInfo to,
648 specInfo = specInfo to,
649 inlinePragInfo = inlinePragInfo to
651 -- It's important to preserve the inline pragma on 'f'; e.g. consider
652 -- {-# NOINLINE f #-}
655 -- similarly, transformation rules may be attached to f
656 -- and we want to preserve them.
658 -- On the other hand, we want the strictness info from f_local.