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,
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 CafInfo(..), cafInfo, setCafInfo, ppCafInfo,
61 -- Constructed Product Result Info
62 CprInfo(..), cprInfo, setCprInfo, ppCprInfo, noCprInfo,
64 -- Lambda-bound variable info
65 LBVarInfo(..), lbvarInfo, setLBVarInfo, noLBVarInfo
68 #include "HsVersions.h"
72 import PrimOp ( PrimOp )
74 import BasicTypes ( OccInfo(..), isFragileOcc, isDeadOcc, seqOccInfo, isLoopBreaker,
75 InsideLam, insideLam, notInsideLam,
76 OneBranch, oneBranch, notOneBranch,
79 import DataCon ( DataCon )
80 import FieldLabel ( FieldLabel )
81 import Demand -- Lots of stuff
84 infixl 1 `setDemandInfo`,
94 -- infixl so you can say (id `set` a `set` b)
97 An @IdInfo@ gives {\em optional} information about an @Id@. If
98 present it never lies, but it may not be present, in which case there
99 is always a conservative assumption which can be made.
101 There is one exception: the 'flavour' is *not* optional.
102 You must not discard it.
103 It used to be in Var.lhs, but that seems unclean.
105 Two @Id@s may have different info even though they have the same
106 @Unique@ (and are hence the same @Id@); for example, one might lack
107 the properties attached to the other.
109 The @IdInfo@ gives information about the value, or definition, of the
110 @Id@. It does {\em not} contain information about the @Id@'s usage
111 (except for @DemandInfo@? ToDo). (@lbvarInfo@ is also a marginal
117 flavourInfo :: IdFlavour, -- NOT OPTIONAL
118 arityInfo :: ArityInfo, -- Its arity
119 demandInfo :: Demand, -- Whether or not it is definitely demanded
120 specInfo :: CoreRules, -- Specialisations of this function which exist
121 strictnessInfo :: StrictnessInfo, -- Strictness properties
122 workerInfo :: WorkerInfo, -- Pointer to Worker Function
123 unfoldingInfo :: Unfolding, -- Its unfolding
125 cprInfo :: CprInfo, -- Function always constructs a product result
126 lbvarInfo :: LBVarInfo, -- Info about a lambda-bound variable
127 inlinePragInfo :: InlinePragInfo, -- Inline pragma
128 occInfo :: OccInfo -- How it occurs
131 seqIdInfo :: IdInfo -> ()
132 seqIdInfo (IdInfo {}) = ()
134 megaSeqIdInfo :: IdInfo -> ()
136 = seqFlavour (flavourInfo info) `seq`
137 seqArity (arityInfo info) `seq`
138 seqDemand (demandInfo info) `seq`
139 seqRules (specInfo info) `seq`
140 seqStrictnessInfo (strictnessInfo info) `seq`
141 seqWorker (workerInfo info) `seq`
143 -- seqUnfolding (unfoldingInfo info) `seq`
144 -- Omitting this improves runtimes a little, presumably because
145 -- some unfoldings are not calculated at all
147 seqCaf (cafInfo info) `seq`
148 seqCpr (cprInfo info) `seq`
149 seqLBVar (lbvarInfo info) `seq`
150 seqOccInfo (occInfo info)
156 setWorkerInfo info wk = wk `seq` info { workerInfo = wk }
157 setSpecInfo info sp = PSEQ sp (info { specInfo = sp })
158 setInlinePragInfo info pr = pr `seq` info { inlinePragInfo = pr }
159 setOccInfo info oc = oc `seq` info { occInfo = oc }
160 setStrictnessInfo info st = st `seq` info { strictnessInfo = st }
161 -- Try to avoid spack leaks by seq'ing
163 setUnfoldingInfo info uf
164 | isEvaldUnfolding uf && isStrict (demandInfo info)
165 -- If the unfolding is a value, the demand info may
166 -- go pear-shaped, so we nuke it. Example:
168 -- case x of (p,q) -> h p q x
169 -- Here x is certainly demanded. But after we've nuked
170 -- the case, we'll get just
171 -- let x = (a,b) in h a b x
172 -- and now x is not demanded (I'm assuming h is lazy)
173 -- This really happens. The solution here is a bit ad hoc...
174 = info { unfoldingInfo = uf, demandInfo = wwLazy }
177 -- We do *not* seq on the unfolding info, For some reason, doing so
178 -- actually increases residency significantly.
179 = info { unfoldingInfo = uf }
181 setDemandInfo info dd = info { demandInfo = dd }
182 setArityInfo info ar = info { arityInfo = ar }
183 setCafInfo info cf = info { cafInfo = cf }
184 setCprInfo info cp = info { cprInfo = cp }
185 setLBVarInfo info lb = info { lbvarInfo = lb }
187 setNoDiscardInfo info = case flavourInfo info of
188 VanillaId -> info { flavourInfo = NoDiscardId }
190 zapSpecPragInfo info = case flavourInfo info of
191 SpecPragmaId -> info { flavourInfo = VanillaId }
197 vanillaIdInfo :: IdInfo
198 vanillaIdInfo = mkIdInfo VanillaId
200 mkIdInfo :: IdFlavour -> IdInfo
201 mkIdInfo flv = IdInfo {
203 arityInfo = UnknownArity,
205 specInfo = emptyCoreRules,
206 workerInfo = NoWorker,
207 strictnessInfo = NoStrictnessInfo,
208 unfoldingInfo = noUnfolding,
209 cafInfo = MayHaveCafRefs,
211 lbvarInfo = NoLBVarInfo,
212 inlinePragInfo = NoInlinePragInfo,
218 %************************************************************************
222 %************************************************************************
226 = VanillaId -- Most Ids are like this
227 | DataConId DataCon -- The Id for a data constructor *worker*
228 | DataConWrapId DataCon -- The Id for a data constructor *wrapper*
229 -- [the only reasons we need to know is so that
230 -- a) we can suppress printing a definition in the interface file
231 -- b) when typechecking a pattern we can get from the
232 -- Id back to the data con]
233 | PrimOpId PrimOp -- The Id for a primitive operator
234 | RecordSelId FieldLabel -- The Id for a record selector
235 | SpecPragmaId -- Don't discard these
236 | NoDiscardId -- Don't discard these either
238 ppFlavourInfo :: IdFlavour -> SDoc
239 ppFlavourInfo VanillaId = empty
240 ppFlavourInfo (DataConId _) = ptext SLIT("[DataCon]")
241 ppFlavourInfo (DataConWrapId _) = ptext SLIT("[DataConWrapper]")
242 ppFlavourInfo (PrimOpId _) = ptext SLIT("[PrimOp]")
243 ppFlavourInfo (RecordSelId _) = ptext SLIT("[RecSel]")
244 ppFlavourInfo SpecPragmaId = ptext SLIT("[SpecPrag]")
245 ppFlavourInfo NoDiscardId = ptext SLIT("[NoDiscard]")
247 seqFlavour :: IdFlavour -> ()
248 seqFlavour f = f `seq` ()
251 The @SpecPragmaId@ exists only to make Ids that are
252 on the *LHS* of bindings created by SPECIALISE pragmas;
254 The SpecPragmaId is never itself mentioned; it
255 exists solely so that the specialiser will find
256 the call to f, and make specialised version of it.
257 The SpecPragmaId binding is discarded by the specialiser
258 when it gathers up overloaded calls.
259 Meanwhile, it is not discarded as dead code.
262 %************************************************************************
264 \subsection[arity-IdInfo]{Arity info about an @Id@}
266 %************************************************************************
268 For locally-defined Ids, the code generator maintains its own notion
269 of their arities; so it should not be asking... (but other things
270 besides the code-generator need arity info!)
274 = UnknownArity -- No idea
276 | ArityExactly Arity -- Arity is exactly this. We use this when importing a
277 -- function; it's already been compiled and we know its
280 | ArityAtLeast Arity -- A partial application of this Id to up to n-1 value arguments
281 -- does essentially no work. That is not necessarily the
282 -- same as saying that it has n leading lambdas, because coerces
283 -- may get in the way.
285 -- functions in the module being compiled. Their arity
286 -- might increase later in the compilation process, if
287 -- an extra lambda floats up to the binding site.
290 seqArity :: ArityInfo -> ()
291 seqArity a = arityLowerBound a `seq` ()
293 exactArity = ArityExactly
294 atLeastArity = ArityAtLeast
295 unknownArity = UnknownArity
297 arityLowerBound :: ArityInfo -> Arity
298 arityLowerBound UnknownArity = 0
299 arityLowerBound (ArityAtLeast n) = n
300 arityLowerBound (ArityExactly n) = n
302 hasArity :: ArityInfo -> Bool
303 hasArity UnknownArity = False
304 hasArity other = True
306 ppArityInfo UnknownArity = empty
307 ppArityInfo (ArityExactly arity) = hsep [ptext SLIT("__A"), int arity]
308 ppArityInfo (ArityAtLeast arity) = hsep [ptext SLIT("__AL"), int arity]
311 %************************************************************************
313 \subsection{Inline-pragma information}
315 %************************************************************************
320 | IMustNotBeINLINEd Bool -- True <=> came from an INLINE prag, False <=> came from a NOINLINE prag
321 (Maybe Int) -- Phase number from pragma, if any
323 -- The True, Nothing case doesn't need to be recorded
325 -- SEE COMMENTS WITH CoreUnfold.blackListed on the
326 -- exact significance of the IMustNotBeINLINEd pragma
328 isNeverInlinePrag :: InlinePragInfo -> Bool
329 isNeverInlinePrag (IMustNotBeINLINEd _ Nothing) = True
330 isNeverInlinePrag other = False
332 neverInlinePrag :: InlinePragInfo
333 neverInlinePrag = IMustNotBeINLINEd True{-should be False? --SDM -} Nothing
335 instance Outputable InlinePragInfo where
336 -- This is now parsed in interface files
337 ppr NoInlinePragInfo = empty
338 ppr other_prag = ptext SLIT("__U") <> pprInlinePragInfo other_prag
340 pprInlinePragInfo NoInlinePragInfo = empty
341 pprInlinePragInfo (IMustNotBeINLINEd True Nothing) = empty
342 pprInlinePragInfo (IMustNotBeINLINEd True (Just n)) = brackets (int n)
343 pprInlinePragInfo (IMustNotBeINLINEd False Nothing) = brackets (char '!')
344 pprInlinePragInfo (IMustNotBeINLINEd False (Just n)) = brackets (char '!' <> int n)
346 instance Show InlinePragInfo where
347 showsPrec p prag = showsPrecSDoc p (ppr prag)
351 %************************************************************************
353 \subsection[worker-IdInfo]{Worker info about an @Id@}
355 %************************************************************************
357 If this Id has a worker then we store a reference to it. Worker
358 functions are generated by the worker/wrapper pass. This uses
359 information from the strictness and CPR analyses.
361 There might not be a worker, even for a strict function, because:
362 (a) the function might be small enough to inline, so no need
364 (b) the strictness info might be "SSS" or something, so no w/w split.
368 data WorkerInfo = NoWorker
370 -- The Arity is the arity of the *wrapper* at the moment of the
371 -- w/w split. See comments in MkIface.ifaceId, with the 'Worker' code.
373 seqWorker :: WorkerInfo -> ()
374 seqWorker (HasWorker id _) = id `seq` ()
375 seqWorker NoWorker = ()
377 ppWorkerInfo NoWorker = empty
378 ppWorkerInfo (HasWorker wk_id _) = ptext SLIT("__P") <+> ppr wk_id
380 noWorkerInfo = NoWorker
382 workerExists :: WorkerInfo -> Bool
383 workerExists NoWorker = False
384 workerExists (HasWorker _ _) = True
386 workerId :: WorkerInfo -> Id
387 workerId (HasWorker id _) = id
389 wrapperArity :: WorkerInfo -> Arity
390 wrapperArity (HasWorker _ a) = a
394 %************************************************************************
396 \subsection[CAF-IdInfo]{CAF-related information}
398 %************************************************************************
400 This information is used to build Static Reference Tables (see
401 simplStg/ComputeSRT.lhs).
405 = MayHaveCafRefs -- either:
406 -- (1) A function or static constructor
407 -- that refers to one or more CAFs,
408 -- (2) A real live CAF
410 | NoCafRefs -- A function or static constructor
411 -- that refers to no CAFs.
413 -- LATER: not sure how easy this is...
417 seqCaf c = c `seq` ()
419 ppCafInfo NoCafRefs = ptext SLIT("__C")
420 ppCafInfo MayHaveCafRefs = empty
424 %************************************************************************
426 \subsection[cpr-IdInfo]{Constructed Product Result info about an @Id@}
428 %************************************************************************
430 If the @Id@ is a function then it may have CPR info. A CPR analysis
431 phase detects whether:
435 The function's return value has a product type, i.e. an algebraic type
436 with a single constructor. Examples of such types are tuples and boxed
439 The function always 'constructs' the value that it is returning. It
440 must do this on every path through, and it's OK if it calls another
441 function which constructs the result.
444 If this is the case then we store a template which tells us the
445 function has the CPR property and which components of the result are
451 | ReturnsCPR -- Yes, this function returns a constructed product
452 -- Implicitly, this means "after the function has been applied
453 -- to all its arguments", so the worker/wrapper builder in
454 -- WwLib.mkWWcpr checks that that it is indeed saturated before
455 -- making use of the CPR info
457 -- We used to keep nested info about sub-components, but
458 -- we never used it so I threw it away
462 seqCpr :: CprInfo -> ()
463 seqCpr ReturnsCPR = ()
464 seqCpr NoCPRInfo = ()
466 noCprInfo = NoCPRInfo
468 ppCprInfo NoCPRInfo = empty
469 ppCprInfo ReturnsCPR = ptext SLIT("__M")
471 instance Outputable CprInfo where
474 instance Show CprInfo where
475 showsPrec p c = showsPrecSDoc p (ppr c)
479 %************************************************************************
481 \subsection[lbvar-IdInfo]{Lambda-bound var info about an @Id@}
483 %************************************************************************
485 If the @Id@ is a lambda-bound variable then it may have lambda-bound
486 var info. The usage analysis (UsageSP) detects whether the lambda
487 binding this var is a ``one-shot'' lambda; that is, whether it is
488 applied at most once.
490 This information may be useful in optimisation, as computations may
491 safely be floated inside such a lambda without risk of duplicating
498 | IsOneShotLambda -- The lambda that binds this Id is applied
500 -- HACK ALERT! placing this info here is a short-term hack,
501 -- but it minimises changes to the rest of the compiler.
502 -- Hack agreed by SLPJ/KSW 1999-04.
504 seqLBVar l = l `seq` ()
508 noLBVarInfo = NoLBVarInfo
510 -- not safe to print or parse LBVarInfo because it is not really a
511 -- property of the definition, but a property of the context.
512 pprLBVarInfo NoLBVarInfo = empty
513 pprLBVarInfo IsOneShotLambda = getPprStyle $ \ sty ->
514 if ifaceStyle sty then empty
515 else ptext SLIT("OneShot")
517 instance Outputable LBVarInfo where
520 instance Show LBVarInfo where
521 showsPrec p c = showsPrecSDoc p (ppr c)
525 %************************************************************************
527 \subsection{Bulk operations on IdInfo}
529 %************************************************************************
531 zapFragileInfo is used when cloning binders, mainly in the
532 simplifier. We must forget about used-once information because that
533 isn't necessarily correct in the transformed program.
534 Also forget specialisations and unfoldings because they would need
535 substitution to be correct. (They get pinned back on separately.)
538 zapFragileInfo :: IdInfo -> Maybe IdInfo
539 zapFragileInfo info@(IdInfo {occInfo = occ,
542 unfoldingInfo = unfolding})
543 | not (isFragileOcc occ)
544 -- We must forget about whether it was marked safe-to-inline,
545 -- because that isn't necessarily true in the simplified expression.
546 -- This is important because expressions may be re-simplified
547 -- We don't zap deadness or loop-breaker-ness.
548 -- The latter is important because it tells MkIface not to
549 -- spit out an inlining for the thing. The former doesn't
550 -- seem so important, but there's no harm.
552 && isEmptyCoreRules rules
553 -- Specialisations would need substituting. They get pinned
554 -- back on separately.
556 && not (workerExists wrkr)
558 && not (hasUnfolding unfolding)
559 -- This is very important; occasionally a let-bound binder is used
560 -- as a binder in some lambda, in which case its unfolding is utterly
561 -- bogus. Also the unfolding uses old binders so if we left it we'd
562 -- have to substitute it. Much better simply to give the Id a new
563 -- unfolding each time, which is what the simplifier does.
567 = Just (info {occInfo = robust_occ_info,
568 workerInfo = noWorkerInfo,
569 specInfo = emptyCoreRules,
570 unfoldingInfo = noUnfolding})
572 -- It's important to keep the loop-breaker info,
573 -- because the substitution doesn't remember it.
574 robust_occ_info = case occ of
575 OneOcc _ _ -> NoOccInfo
579 @zapLamInfo@ is used for lambda binders that turn out to to be
580 part of an unsaturated lambda
583 zapLamInfo :: IdInfo -> Maybe IdInfo
584 zapLamInfo info@(IdInfo {occInfo = occ, demandInfo = demand})
585 | is_safe_occ && not (isStrict demand)
588 = Just (info {occInfo = safe_occ,
589 demandInfo = wwLazy})
591 -- The "unsafe" occ info is the ones that say I'm not in a lambda
592 -- because that might not be true for an unsaturated lambda
593 is_safe_occ = case occ of
594 OneOcc in_lam once -> in_lam
597 safe_occ = case occ of
598 OneOcc _ once -> OneOcc insideLam once
603 copyIdInfo is used when shorting out a top-level binding
606 where f is exported. We are going to swizzle it around to
610 BUT (a) we must be careful about messing up rules
611 (b) we must ensure f's IdInfo ends up right
613 (a) Messing up the rules
615 The example that went bad on me was this one:
617 iterate :: (a -> a) -> a -> [a]
618 iterate = iterateList
620 iterateFB c f x = x `c` iterateFB c f (f x)
621 iterateList f x = x : iterateList f (f x)
624 "iterate" forall f x. iterate f x = build (\c _n -> iterateFB c f x)
625 "iterateFB" iterateFB (:) = iterateList
628 This got shorted out to:
630 iterateList :: (a -> a) -> a -> [a]
631 iterateList = iterate
633 iterateFB c f x = x `c` iterateFB c f (f x)
634 iterate f x = x : iterate f (f x)
637 "iterate" forall f x. iterate f x = build (\c _n -> iterateFB c f x)
638 "iterateFB" iterateFB (:) = iterate
641 And now we get an infinite loop in the rule system
642 iterate f x -> build (\cn -> iterateFB c f x
646 Tiresome solution: don't do shorting out if f has rewrite rules.
647 Hence shortableIdInfo.
649 (b) Keeping the IdInfo right
650 ~~~~~~~~~~~~~~~~~~~~~~~~
651 We want to move strictness/worker info from f_local to f, but keep the rest.
655 shortableIdInfo :: IdInfo -> Bool
656 shortableIdInfo info = isEmptyCoreRules (specInfo info)
658 copyIdInfo :: IdInfo -- f_local
659 -> IdInfo -- f (the exported one)
660 -> IdInfo -- New info for f
661 copyIdInfo f_local f = f { strictnessInfo = strictnessInfo f_local,
662 workerInfo = workerInfo f_local,
663 cprInfo = cprInfo f_local