2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
5 \section[IdInfo]{@IdInfos@: Non-essential information about @Ids@}
7 (And a pretty good illustration of quite a few things wrong with
12 GlobalIdDetails(..), notGlobalId, -- Not abstract
15 vanillaIdInfo, noCafIdInfo,
16 seqIdInfo, megaSeqIdInfo,
19 zapLamInfo, zapDemandInfo,
24 arityInfo, setArityInfo, ppArityInfo,
26 -- New demand and strictness info
27 newStrictnessInfo, setNewStrictnessInfo,
28 newDemandInfo, setNewDemandInfo, pprNewStrictness,
32 -- Strictness; imported from Demand
34 mkStrictnessInfo, noStrictnessInfo,
35 ppStrictnessInfo,isBottomingStrictness,
39 WorkerInfo(..), workerExists, wrapperArity, workerId,
40 workerInfo, setWorkerInfo, ppWorkerInfo,
43 unfoldingInfo, setUnfoldingInfo, setUnfoldingInfoLazily,
46 -- Old DemandInfo and StrictnessInfo
47 demandInfo, setDemandInfo,
48 strictnessInfo, setStrictnessInfo,
49 cprInfoFromNewStrictness,
50 oldStrictnessFromNew, newStrictnessFromOld,
53 -- Constructed Product Result Info
54 CprInfo(..), cprInfo, setCprInfo, ppCprInfo, noCprInfo,
59 inlinePragInfo, setInlinePragInfo,
62 OccInfo(..), isFragileOcc, isDeadOcc, isLoopBreaker,
63 InsideLam, OneBranch, insideLam, notInsideLam, oneBranch, notOneBranch,
67 SpecInfo(..), specInfo, setSpecInfo, isEmptySpecInfo,
68 specInfoFreeVars, specInfoRules, seqSpecInfo,
71 CafInfo(..), cafInfo, ppCafInfo, setCafInfo, mayHaveCafRefs,
73 -- Lambda-bound variable info
74 LBVarInfo(..), lbvarInfo, setLBVarInfo, noLBVarInfo, hasNoLBVarInfo
77 #include "HsVersions.h"
96 import qualified Demand
101 -- infixl so you can say (id `set` a `set` b)
102 infixl 1 `setSpecInfo`,
110 `setNewStrictnessInfo`,
111 `setAllStrictnessInfo`,
113 #ifdef OLD_STRICTNESS
116 , `setStrictnessInfo`
120 %************************************************************************
122 \subsection{New strictness info}
124 %************************************************************************
129 -- setAllStrictnessInfo :: IdInfo -> Maybe StrictSig -> IdInfo
130 -- Set old and new strictness info
131 setAllStrictnessInfo info Nothing
132 = info { newStrictnessInfo = Nothing
133 #ifdef OLD_STRICTNESS
134 , strictnessInfo = NoStrictnessInfo
135 , cprInfo = NoCPRInfo
139 setAllStrictnessInfo info (Just sig)
140 = info { newStrictnessInfo = Just sig
141 #ifdef OLD_STRICTNESS
142 , strictnessInfo = oldStrictnessFromNew sig
143 , cprInfo = cprInfoFromNewStrictness sig
147 seqNewStrictnessInfo Nothing = ()
148 seqNewStrictnessInfo (Just ty) = seqStrictSig ty
150 pprNewStrictness Nothing = empty
151 pprNewStrictness (Just sig) = ftext FSLIT("Str:") <+> ppr sig
153 #ifdef OLD_STRICTNESS
154 oldStrictnessFromNew :: StrictSig -> Demand.StrictnessInfo
155 oldStrictnessFromNew sig = mkStrictnessInfo (map oldDemand dmds, isBotRes res_info)
157 (dmds, res_info) = splitStrictSig sig
159 cprInfoFromNewStrictness :: StrictSig -> CprInfo
160 cprInfoFromNewStrictness sig = case strictSigResInfo sig of
164 newStrictnessFromOld :: Name -> Arity -> Demand.StrictnessInfo -> CprInfo -> StrictSig
165 newStrictnessFromOld name arity (Demand.StrictnessInfo ds res) cpr
166 | listLengthCmp ds arity /= GT -- length ds <= arity
167 -- Sometimes the old strictness analyser has more
168 -- demands than the arity justifies
169 = mk_strict_sig name arity $
170 mkTopDmdType (map newDemand ds) (newRes res cpr)
172 newStrictnessFromOld name arity other cpr
173 = -- Either no strictness info, or arity is too small
174 -- In either case we can't say anything useful
175 mk_strict_sig name arity $
176 mkTopDmdType (replicate arity lazyDmd) (newRes False cpr)
178 mk_strict_sig name arity dmd_ty
179 = WARN( arity /= dmdTypeDepth dmd_ty, ppr name <+> (ppr arity $$ ppr dmd_ty) )
182 newRes True _ = BotRes
183 newRes False ReturnsCPR = retCPR
184 newRes False NoCPRInfo = TopRes
186 newDemand :: Demand.Demand -> NewDemand.Demand
187 newDemand (WwLazy True) = Abs
188 newDemand (WwLazy False) = lazyDmd
189 newDemand WwStrict = evalDmd
190 newDemand (WwUnpack unpk ds) = Eval (Prod (map newDemand ds))
191 newDemand WwPrim = lazyDmd
192 newDemand WwEnum = evalDmd
194 oldDemand :: NewDemand.Demand -> Demand.Demand
195 oldDemand Abs = WwLazy True
196 oldDemand Top = WwLazy False
197 oldDemand Bot = WwStrict
198 oldDemand (Box Bot) = WwStrict
199 oldDemand (Box Abs) = WwLazy False
200 oldDemand (Box (Eval _)) = WwStrict -- Pass box only
201 oldDemand (Defer d) = WwLazy False
202 oldDemand (Eval (Prod ds)) = WwUnpack True (map oldDemand ds)
203 oldDemand (Eval (Poly _)) = WwStrict
204 oldDemand (Call _) = WwStrict
206 #endif /* OLD_STRICTNESS */
211 seqNewDemandInfo Nothing = ()
212 seqNewDemandInfo (Just dmd) = seqDemand dmd
216 %************************************************************************
218 \subsection{GlobalIdDetails
220 %************************************************************************
222 This type is here (rather than in Id.lhs) mainly because there's
223 an IdInfo.hi-boot, but no Id.hi-boot, and GlobalIdDetails is imported
224 (recursively) by Var.lhs.
228 = VanillaGlobal -- Imported from elsewhere, a default method Id.
230 | RecordSelId -- The Id for a record selector
232 , sel_label :: FieldLabel
233 , sel_naughty :: Bool -- True <=> naughty
234 } -- See Note [Naughty record selectors]
235 -- with MkId.mkRecordSelectorId
237 | DataConWorkId DataCon -- The Id for a data constructor *worker*
238 | DataConWrapId DataCon -- The Id for a data constructor *wrapper*
239 -- [the only reasons we need to know is so that
240 -- a) to support isImplicitId
241 -- b) when desugaring a RecordCon we can get
242 -- from the Id back to the data con]
244 | ClassOpId Class -- An operation of a class
246 | PrimOpId PrimOp -- The Id for a primitive operator
247 | FCallId ForeignCall -- The Id for a foreign call
249 | NotGlobalId -- Used as a convenient extra return value from globalIdDetails
251 notGlobalId = NotGlobalId
253 instance Outputable GlobalIdDetails where
254 ppr NotGlobalId = ptext SLIT("[***NotGlobalId***]")
255 ppr VanillaGlobal = ptext SLIT("[GlobalId]")
256 ppr (DataConWorkId _) = ptext SLIT("[DataCon]")
257 ppr (DataConWrapId _) = ptext SLIT("[DataConWrapper]")
258 ppr (ClassOpId _) = ptext SLIT("[ClassOp]")
259 ppr (PrimOpId _) = ptext SLIT("[PrimOp]")
260 ppr (FCallId _) = ptext SLIT("[ForeignCall]")
261 ppr (RecordSelId {}) = ptext SLIT("[RecSel]")
265 %************************************************************************
267 \subsection{The main IdInfo type}
269 %************************************************************************
271 An @IdInfo@ gives {\em optional} information about an @Id@. If
272 present it never lies, but it may not be present, in which case there
273 is always a conservative assumption which can be made.
275 Two @Id@s may have different info even though they have the same
276 @Unique@ (and are hence the same @Id@); for example, one might lack
277 the properties attached to the other.
279 The @IdInfo@ gives information about the value, or definition, of the
280 @Id@. It does {\em not} contain information about the @Id@'s usage
281 (except for @DemandInfo@? ToDo). (@lbvarInfo@ is also a marginal
287 arityInfo :: !ArityInfo, -- Its arity
288 specInfo :: SpecInfo, -- Specialisations of this function which exist
289 #ifdef OLD_STRICTNESS
290 cprInfo :: CprInfo, -- Function always constructs a product result
291 demandInfo :: Demand.Demand, -- Whether or not it is definitely demanded
292 strictnessInfo :: StrictnessInfo, -- Strictness properties
294 workerInfo :: WorkerInfo, -- Pointer to Worker Function
295 -- Within one module this is irrelevant; the
296 -- inlining of a worker is handled via the Unfolding
297 -- WorkerInfo is used *only* to indicate the form of
298 -- the RHS, so that interface files don't actually
299 -- need to contain the RHS; it can be derived from
300 -- the strictness info
302 unfoldingInfo :: Unfolding, -- Its unfolding
303 cafInfo :: CafInfo, -- CAF info
304 lbvarInfo :: LBVarInfo, -- Info about a lambda-bound variable
305 inlinePragInfo :: InlinePragInfo, -- Inline pragma
306 occInfo :: OccInfo, -- How it occurs
308 newStrictnessInfo :: Maybe StrictSig, -- Reason for Maybe: the DmdAnal phase needs to
309 -- know whether whether this is the first visit,
310 -- so it can assign botSig. Other customers want
311 -- topSig. So Nothing is good.
313 newDemandInfo :: Maybe Demand -- Similarly we want to know if there's no
314 -- known demand yet, for when we are looking for
318 seqIdInfo :: IdInfo -> ()
319 seqIdInfo (IdInfo {}) = ()
321 megaSeqIdInfo :: IdInfo -> ()
323 = seqSpecInfo (specInfo info) `seq`
324 seqWorker (workerInfo info) `seq`
326 -- Omitting this improves runtimes a little, presumably because
327 -- some unfoldings are not calculated at all
328 -- seqUnfolding (unfoldingInfo info) `seq`
330 seqNewDemandInfo (newDemandInfo info) `seq`
331 seqNewStrictnessInfo (newStrictnessInfo info) `seq`
333 #ifdef OLD_STRICTNESS
334 Demand.seqDemand (demandInfo info) `seq`
335 seqStrictnessInfo (strictnessInfo info) `seq`
336 seqCpr (cprInfo info) `seq`
339 seqCaf (cafInfo info) `seq`
340 seqLBVar (lbvarInfo info) `seq`
341 seqOccInfo (occInfo info)
347 setWorkerInfo info wk = wk `seq` info { workerInfo = wk }
348 setSpecInfo info sp = sp `seq` info { specInfo = sp }
349 setInlinePragInfo info pr = pr `seq` info { inlinePragInfo = pr }
350 setOccInfo info oc = oc `seq` info { occInfo = oc }
351 #ifdef OLD_STRICTNESS
352 setStrictnessInfo info st = st `seq` info { strictnessInfo = st }
354 -- Try to avoid spack leaks by seq'ing
356 setUnfoldingInfoLazily info uf -- Lazy variant to avoid looking at the
357 = -- unfolding of an imported Id unless necessary
358 info { unfoldingInfo = uf } -- (In this case the demand-zapping is redundant.)
360 setUnfoldingInfo info uf
361 -- We do *not* seq on the unfolding info, For some reason, doing so
362 -- actually increases residency significantly.
363 = info { unfoldingInfo = uf }
365 #ifdef OLD_STRICTNESS
366 setDemandInfo info dd = info { demandInfo = dd }
367 setCprInfo info cp = info { cprInfo = cp }
370 setArityInfo info ar = info { arityInfo = ar }
371 setCafInfo info caf = info { cafInfo = caf }
373 setLBVarInfo info lb = {-lb `seq`-} info { lbvarInfo = lb }
375 setNewDemandInfo info dd = dd `seq` info { newDemandInfo = dd }
376 setNewStrictnessInfo info dd = dd `seq` info { newStrictnessInfo = dd }
381 vanillaIdInfo :: IdInfo
384 cafInfo = vanillaCafInfo,
385 arityInfo = unknownArity,
386 #ifdef OLD_STRICTNESS
389 strictnessInfo = NoStrictnessInfo,
391 specInfo = emptySpecInfo,
392 workerInfo = NoWorker,
393 unfoldingInfo = noUnfolding,
394 lbvarInfo = NoLBVarInfo,
395 inlinePragInfo = AlwaysActive,
397 newDemandInfo = Nothing,
398 newStrictnessInfo = Nothing
401 noCafIdInfo = vanillaIdInfo `setCafInfo` NoCafRefs
402 -- Used for built-in type Ids in MkId.
406 %************************************************************************
408 \subsection[arity-IdInfo]{Arity info about an @Id@}
410 %************************************************************************
412 For locally-defined Ids, the code generator maintains its own notion
413 of their arities; so it should not be asking... (but other things
414 besides the code-generator need arity info!)
417 type ArityInfo = Arity
418 -- A partial application of this Id to up to n-1 value arguments
419 -- does essentially no work. That is not necessarily the
420 -- same as saying that it has n leading lambdas, because coerces
421 -- may get in the way.
423 -- The arity might increase later in the compilation process, if
424 -- an extra lambda floats up to the binding site.
426 unknownArity = 0 :: Arity
428 ppArityInfo 0 = empty
429 ppArityInfo n = hsep [ptext SLIT("Arity"), int n]
432 %************************************************************************
434 \subsection{Inline-pragma information}
436 %************************************************************************
439 type InlinePragInfo = Activation
440 -- Tells when the inlining is active
441 -- When it is active the thing may be inlined, depending on how
444 -- If there was an INLINE pragma, then as a separate matter, the
445 -- RHS will have been made to look small with a CoreSyn Inline Note
447 -- The default InlinePragInfo is AlwaysActive, so the info serves
448 -- entirely as a way to inhibit inlining until we want it
452 %************************************************************************
456 %************************************************************************
459 -- CoreRules is used only in an idSpecialisation (move to IdInfo?)
461 = SpecInfo [CoreRule] VarSet -- Locally-defined free vars of RHSs
463 emptySpecInfo :: SpecInfo
464 emptySpecInfo = SpecInfo [] emptyVarSet
466 isEmptySpecInfo :: SpecInfo -> Bool
467 isEmptySpecInfo (SpecInfo rs _) = null rs
469 specInfoFreeVars :: SpecInfo -> VarSet
470 specInfoFreeVars (SpecInfo _ fvs) = fvs
472 specInfoRules :: SpecInfo -> [CoreRule]
473 specInfoRules (SpecInfo rules _) = rules
475 seqSpecInfo (SpecInfo rules fvs) = seqRules rules `seq` seqVarSet fvs
479 %************************************************************************
481 \subsection[worker-IdInfo]{Worker info about an @Id@}
483 %************************************************************************
485 If this Id has a worker then we store a reference to it. Worker
486 functions are generated by the worker/wrapper pass. This uses
487 information from strictness analysis.
489 There might not be a worker, even for a strict function, because:
490 (a) the function might be small enough to inline, so no need
492 (b) the strictness info might be "SSS" or something, so no w/w split.
494 Sometimes the arity of a wrapper changes from the original arity from
495 which it was generated, so we always emit the "original" arity into
496 the interface file, as part of the worker info.
498 How can this happen? Sometimes we get
499 f = coerce t (\x y -> $wf x y)
500 at the moment of w/w split; but the eta reducer turns it into
502 which is perfectly fine except that the exposed arity so far as
503 the code generator is concerned (zero) differs from the arity
504 when we did the split (2).
506 All this arises because we use 'arity' to mean "exactly how many
507 top level lambdas are there" in interface files; but during the
508 compilation of this module it means "how many things can I apply
513 data WorkerInfo = NoWorker
515 -- The Arity is the arity of the *wrapper* at the moment of the
516 -- w/w split. See notes above.
518 seqWorker :: WorkerInfo -> ()
519 seqWorker (HasWorker id a) = id `seq` a `seq` ()
520 seqWorker NoWorker = ()
522 ppWorkerInfo NoWorker = empty
523 ppWorkerInfo (HasWorker wk_id _) = ptext SLIT("Worker") <+> ppr wk_id
525 workerExists :: WorkerInfo -> Bool
526 workerExists NoWorker = False
527 workerExists (HasWorker _ _) = True
529 workerId :: WorkerInfo -> Id
530 workerId (HasWorker id _) = id
532 wrapperArity :: WorkerInfo -> Arity
533 wrapperArity (HasWorker _ a) = a
537 %************************************************************************
539 \subsection[CG-IdInfo]{Code generator-related information}
541 %************************************************************************
544 -- CafInfo is used to build Static Reference Tables (see simplStg/SRT.lhs).
547 = MayHaveCafRefs -- either:
548 -- (1) A function or static constructor
549 -- that refers to one or more CAFs,
550 -- (2) A real live CAF
552 | NoCafRefs -- A function or static constructor
553 -- that refers to no CAFs.
555 vanillaCafInfo = MayHaveCafRefs -- Definitely safe
557 mayHaveCafRefs MayHaveCafRefs = True
558 mayHaveCafRefs _ = False
560 seqCaf c = c `seq` ()
562 ppCafInfo NoCafRefs = ptext SLIT("NoCafRefs")
563 ppCafInfo MayHaveCafRefs = empty
566 %************************************************************************
568 \subsection[cpr-IdInfo]{Constructed Product Result info about an @Id@}
570 %************************************************************************
572 If the @Id@ is a function then it may have CPR info. A CPR analysis
573 phase detects whether:
577 The function's return value has a product type, i.e. an algebraic type
578 with a single constructor. Examples of such types are tuples and boxed
581 The function always 'constructs' the value that it is returning. It
582 must do this on every path through, and it's OK if it calls another
583 function which constructs the result.
586 If this is the case then we store a template which tells us the
587 function has the CPR property and which components of the result are
591 #ifdef OLD_STRICTNESS
594 | ReturnsCPR -- Yes, this function returns a constructed product
595 -- Implicitly, this means "after the function has been applied
596 -- to all its arguments", so the worker/wrapper builder in
597 -- WwLib.mkWWcpr checks that that it is indeed saturated before
598 -- making use of the CPR info
600 -- We used to keep nested info about sub-components, but
601 -- we never used it so I threw it away
603 seqCpr :: CprInfo -> ()
604 seqCpr ReturnsCPR = ()
605 seqCpr NoCPRInfo = ()
607 noCprInfo = NoCPRInfo
609 ppCprInfo NoCPRInfo = empty
610 ppCprInfo ReturnsCPR = ptext SLIT("__M")
612 instance Outputable CprInfo where
615 instance Show CprInfo where
616 showsPrec p c = showsPrecSDoc p (ppr c)
621 %************************************************************************
623 \subsection[lbvar-IdInfo]{Lambda-bound var info about an @Id@}
625 %************************************************************************
627 If the @Id@ is a lambda-bound variable then it may have lambda-bound
628 var info. Sometimes we know whether the lambda binding this var is a
629 ``one-shot'' lambda; that is, whether it is applied at most once.
631 This information may be useful in optimisation, as computations may
632 safely be floated inside such a lambda without risk of duplicating
636 data LBVarInfo = NoLBVarInfo
637 | IsOneShotLambda -- The lambda is applied at most once).
639 seqLBVar l = l `seq` ()
643 hasNoLBVarInfo NoLBVarInfo = True
644 hasNoLBVarInfo IsOneShotLambda = False
646 noLBVarInfo = NoLBVarInfo
648 pprLBVarInfo NoLBVarInfo = empty
649 pprLBVarInfo IsOneShotLambda = ptext SLIT("OneShot")
651 instance Outputable LBVarInfo where
654 instance Show LBVarInfo where
655 showsPrec p c = showsPrecSDoc p (ppr c)
659 %************************************************************************
661 \subsection{Bulk operations on IdInfo}
663 %************************************************************************
665 @zapLamInfo@ is used for lambda binders that turn out to to be
666 part of an unsaturated lambda
669 zapLamInfo :: IdInfo -> Maybe IdInfo
670 zapLamInfo info@(IdInfo {occInfo = occ, newDemandInfo = demand})
671 | is_safe_occ occ && is_safe_dmd demand
674 = Just (info {occInfo = safe_occ, newDemandInfo = Nothing})
676 -- The "unsafe" occ info is the ones that say I'm not in a lambda
677 -- because that might not be true for an unsaturated lambda
678 is_safe_occ (OneOcc in_lam _ _) = in_lam
679 is_safe_occ other = True
681 safe_occ = case occ of
682 OneOcc _ once int_cxt -> OneOcc insideLam once int_cxt
685 is_safe_dmd Nothing = True
686 is_safe_dmd (Just dmd) = not (isStrictDmd dmd)
690 zapDemandInfo :: IdInfo -> Maybe IdInfo
691 zapDemandInfo info@(IdInfo {newDemandInfo = dmd})
692 | isJust dmd = Just (info {newDemandInfo = Nothing})
693 | otherwise = Nothing