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, zapFragileInfo,
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, setSpecInfoHead,
71 CafInfo(..), cafInfo, ppCafInfo, setCafInfo, mayHaveCafRefs,
73 -- Lambda-bound variable info
74 LBVarInfo(..), lbvarInfo, setLBVarInfo, noLBVarInfo, hasNoLBVarInfo,
77 TickBoxOp(..), TickBoxId,
80 #include "HsVersions.h"
100 import qualified Demand
105 -- infixl so you can say (id `set` a `set` b)
106 infixl 1 `setSpecInfo`,
114 `setNewStrictnessInfo`,
115 `setAllStrictnessInfo`,
117 #ifdef OLD_STRICTNESS
120 , `setStrictnessInfo`
124 %************************************************************************
126 \subsection{New strictness info}
128 %************************************************************************
133 -- Set old and new strictness info
134 setAllStrictnessInfo :: IdInfo -> Maybe StrictSig -> IdInfo
135 setAllStrictnessInfo info Nothing
136 = info { newStrictnessInfo = Nothing
137 #ifdef OLD_STRICTNESS
138 , strictnessInfo = NoStrictnessInfo
139 , cprInfo = NoCPRInfo
143 setAllStrictnessInfo info (Just sig)
144 = info { newStrictnessInfo = Just sig
145 #ifdef OLD_STRICTNESS
146 , strictnessInfo = oldStrictnessFromNew sig
147 , cprInfo = cprInfoFromNewStrictness sig
151 seqNewStrictnessInfo :: Maybe StrictSig -> ()
152 seqNewStrictnessInfo Nothing = ()
153 seqNewStrictnessInfo (Just ty) = seqStrictSig ty
155 pprNewStrictness :: Maybe StrictSig -> SDoc
156 pprNewStrictness Nothing = empty
157 pprNewStrictness (Just sig) = ftext FSLIT("Str:") <+> ppr sig
159 #ifdef OLD_STRICTNESS
160 oldStrictnessFromNew :: StrictSig -> Demand.StrictnessInfo
161 oldStrictnessFromNew sig = mkStrictnessInfo (map oldDemand dmds, isBotRes res_info)
163 (dmds, res_info) = splitStrictSig sig
165 cprInfoFromNewStrictness :: StrictSig -> CprInfo
166 cprInfoFromNewStrictness sig = case strictSigResInfo sig of
170 newStrictnessFromOld :: Name -> Arity -> Demand.StrictnessInfo -> CprInfo -> StrictSig
171 newStrictnessFromOld name arity (Demand.StrictnessInfo ds res) cpr
172 | listLengthCmp ds arity /= GT -- length ds <= arity
173 -- Sometimes the old strictness analyser has more
174 -- demands than the arity justifies
175 = mk_strict_sig name arity $
176 mkTopDmdType (map newDemand ds) (newRes res cpr)
178 newStrictnessFromOld name arity other cpr
179 = -- Either no strictness info, or arity is too small
180 -- In either case we can't say anything useful
181 mk_strict_sig name arity $
182 mkTopDmdType (replicate arity lazyDmd) (newRes False cpr)
184 mk_strict_sig name arity dmd_ty
185 = WARN( arity /= dmdTypeDepth dmd_ty, ppr name <+> (ppr arity $$ ppr dmd_ty) )
188 newRes True _ = BotRes
189 newRes False ReturnsCPR = retCPR
190 newRes False NoCPRInfo = TopRes
192 newDemand :: Demand.Demand -> NewDemand.Demand
193 newDemand (WwLazy True) = Abs
194 newDemand (WwLazy False) = lazyDmd
195 newDemand WwStrict = evalDmd
196 newDemand (WwUnpack unpk ds) = Eval (Prod (map newDemand ds))
197 newDemand WwPrim = lazyDmd
198 newDemand WwEnum = evalDmd
200 oldDemand :: NewDemand.Demand -> Demand.Demand
201 oldDemand Abs = WwLazy True
202 oldDemand Top = WwLazy False
203 oldDemand Bot = WwStrict
204 oldDemand (Box Bot) = WwStrict
205 oldDemand (Box Abs) = WwLazy False
206 oldDemand (Box (Eval _)) = WwStrict -- Pass box only
207 oldDemand (Defer d) = WwLazy False
208 oldDemand (Eval (Prod ds)) = WwUnpack True (map oldDemand ds)
209 oldDemand (Eval (Poly _)) = WwStrict
210 oldDemand (Call _) = WwStrict
212 #endif /* OLD_STRICTNESS */
217 seqNewDemandInfo :: Maybe Demand -> ()
218 seqNewDemandInfo Nothing = ()
219 seqNewDemandInfo (Just dmd) = seqDemand dmd
223 %************************************************************************
225 \subsection{GlobalIdDetails}
227 %************************************************************************
229 This type is here (rather than in Id.lhs) mainly because there's
230 an IdInfo.hi-boot, but no Id.hi-boot, and GlobalIdDetails is imported
231 (recursively) by Var.lhs.
235 = VanillaGlobal -- Imported from elsewhere, a default method Id.
237 | RecordSelId -- The Id for a record selector
238 { sel_tycon :: TyCon -- For a data type family, this is the *instance* TyCon
239 -- not the family TyCon
240 , sel_label :: FieldLabel
241 , sel_naughty :: Bool -- True <=> naughty
242 } -- See Note [Naughty record selectors]
243 -- with MkId.mkRecordSelectorId
245 | DataConWorkId DataCon -- The Id for a data constructor *worker*
246 | DataConWrapId DataCon -- The Id for a data constructor *wrapper*
247 -- [the only reasons we need to know is so that
248 -- a) to support isImplicitId
249 -- b) when desugaring a RecordCon we can get
250 -- from the Id back to the data con]
252 | ClassOpId Class -- An operation of a class
254 | PrimOpId PrimOp -- The Id for a primitive operator
255 | FCallId ForeignCall -- The Id for a foreign call
257 | TickBoxOpId TickBoxOp -- The Id for a tick box (both traditional and binary)
259 | NotGlobalId -- Used as a convenient extra return value from globalIdDetails
261 notGlobalId :: GlobalIdDetails
262 notGlobalId = NotGlobalId
264 instance Outputable GlobalIdDetails where
265 ppr NotGlobalId = ptext SLIT("[***NotGlobalId***]")
266 ppr VanillaGlobal = ptext SLIT("[GlobalId]")
267 ppr (DataConWorkId _) = ptext SLIT("[DataCon]")
268 ppr (DataConWrapId _) = ptext SLIT("[DataConWrapper]")
269 ppr (ClassOpId _) = ptext SLIT("[ClassOp]")
270 ppr (PrimOpId _) = ptext SLIT("[PrimOp]")
271 ppr (FCallId _) = ptext SLIT("[ForeignCall]")
272 ppr (TickBoxOpId _) = ptext SLIT("[TickBoxOp]")
273 ppr (RecordSelId {}) = ptext SLIT("[RecSel]")
277 %************************************************************************
279 \subsection{The main IdInfo type}
281 %************************************************************************
283 An @IdInfo@ gives {\em optional} information about an @Id@. If
284 present it never lies, but it may not be present, in which case there
285 is always a conservative assumption which can be made.
287 Two @Id@s may have different info even though they have the same
288 @Unique@ (and are hence the same @Id@); for example, one might lack
289 the properties attached to the other.
291 The @IdInfo@ gives information about the value, or definition, of the
292 @Id@. It does {\em not} contain information about the @Id@'s usage
293 (except for @DemandInfo@? ToDo). (@lbvarInfo@ is also a marginal
299 arityInfo :: !ArityInfo, -- Its arity
300 specInfo :: SpecInfo, -- Specialisations of this function which exist
301 #ifdef OLD_STRICTNESS
302 cprInfo :: CprInfo, -- Function always constructs a product result
303 demandInfo :: Demand.Demand, -- Whether or not it is definitely demanded
304 strictnessInfo :: StrictnessInfo, -- Strictness properties
306 workerInfo :: WorkerInfo, -- Pointer to Worker Function
307 -- Within one module this is irrelevant; the
308 -- inlining of a worker is handled via the Unfolding
309 -- WorkerInfo is used *only* to indicate the form of
310 -- the RHS, so that interface files don't actually
311 -- need to contain the RHS; it can be derived from
312 -- the strictness info
314 unfoldingInfo :: Unfolding, -- Its unfolding
315 cafInfo :: CafInfo, -- CAF info
316 lbvarInfo :: LBVarInfo, -- Info about a lambda-bound variable
317 inlinePragInfo :: InlinePragInfo, -- Inline pragma
318 occInfo :: OccInfo, -- How it occurs
320 newStrictnessInfo :: Maybe StrictSig, -- Reason for Maybe: the DmdAnal phase needs to
321 -- know whether whether this is the first visit,
322 -- so it can assign botSig. Other customers want
323 -- topSig. So Nothing is good.
325 newDemandInfo :: Maybe Demand -- Similarly we want to know if there's no
326 -- known demand yet, for when we are looking for
330 seqIdInfo :: IdInfo -> ()
331 seqIdInfo (IdInfo {}) = ()
333 megaSeqIdInfo :: IdInfo -> ()
335 = seqSpecInfo (specInfo info) `seq`
336 seqWorker (workerInfo info) `seq`
338 -- Omitting this improves runtimes a little, presumably because
339 -- some unfoldings are not calculated at all
340 -- seqUnfolding (unfoldingInfo info) `seq`
342 seqNewDemandInfo (newDemandInfo info) `seq`
343 seqNewStrictnessInfo (newStrictnessInfo info) `seq`
345 #ifdef OLD_STRICTNESS
346 Demand.seqDemand (demandInfo info) `seq`
347 seqStrictnessInfo (strictnessInfo info) `seq`
348 seqCpr (cprInfo info) `seq`
351 seqCaf (cafInfo info) `seq`
352 seqLBVar (lbvarInfo info) `seq`
353 seqOccInfo (occInfo info)
359 setWorkerInfo :: IdInfo -> WorkerInfo -> IdInfo
360 setWorkerInfo info wk = wk `seq` info { workerInfo = wk }
361 setSpecInfo :: IdInfo -> SpecInfo -> IdInfo
362 setSpecInfo info sp = sp `seq` info { specInfo = sp }
363 setInlinePragInfo :: IdInfo -> InlinePragInfo -> IdInfo
364 setInlinePragInfo info pr = pr `seq` info { inlinePragInfo = pr }
365 setOccInfo :: IdInfo -> OccInfo -> IdInfo
366 setOccInfo info oc = oc `seq` info { occInfo = oc }
367 #ifdef OLD_STRICTNESS
368 setStrictnessInfo info st = st `seq` info { strictnessInfo = st }
370 -- Try to avoid spack leaks by seq'ing
372 setUnfoldingInfoLazily :: IdInfo -> Unfolding -> IdInfo
373 setUnfoldingInfoLazily info uf -- Lazy variant to avoid looking at the
374 = -- unfolding of an imported Id unless necessary
375 info { unfoldingInfo = uf } -- (In this case the demand-zapping is redundant.)
377 setUnfoldingInfo :: IdInfo -> Unfolding -> IdInfo
378 setUnfoldingInfo info uf
379 -- We do *not* seq on the unfolding info, For some reason, doing so
380 -- actually increases residency significantly.
381 = info { unfoldingInfo = uf }
383 #ifdef OLD_STRICTNESS
384 setDemandInfo info dd = info { demandInfo = dd }
385 setCprInfo info cp = info { cprInfo = cp }
388 setArityInfo :: IdInfo -> ArityInfo -> IdInfo
389 setArityInfo info ar = info { arityInfo = ar }
390 setCafInfo :: IdInfo -> CafInfo -> IdInfo
391 setCafInfo info caf = info { cafInfo = caf }
393 setLBVarInfo :: IdInfo -> LBVarInfo -> IdInfo
394 setLBVarInfo info lb = {-lb `seq`-} info { lbvarInfo = lb }
396 setNewDemandInfo :: IdInfo -> Maybe Demand -> IdInfo
397 setNewDemandInfo info dd = dd `seq` info { newDemandInfo = dd }
398 setNewStrictnessInfo :: IdInfo -> Maybe StrictSig -> IdInfo
399 setNewStrictnessInfo info dd = dd `seq` info { newStrictnessInfo = dd }
404 vanillaIdInfo :: IdInfo
407 cafInfo = vanillaCafInfo,
408 arityInfo = unknownArity,
409 #ifdef OLD_STRICTNESS
412 strictnessInfo = NoStrictnessInfo,
414 specInfo = emptySpecInfo,
415 workerInfo = NoWorker,
416 unfoldingInfo = noUnfolding,
417 lbvarInfo = NoLBVarInfo,
418 inlinePragInfo = AlwaysActive,
420 newDemandInfo = Nothing,
421 newStrictnessInfo = Nothing
424 noCafIdInfo :: IdInfo
425 noCafIdInfo = vanillaIdInfo `setCafInfo` NoCafRefs
426 -- Used for built-in type Ids in MkId.
430 %************************************************************************
432 \subsection[arity-IdInfo]{Arity info about an @Id@}
434 %************************************************************************
436 For locally-defined Ids, the code generator maintains its own notion
437 of their arities; so it should not be asking... (but other things
438 besides the code-generator need arity info!)
441 type ArityInfo = Arity
442 -- A partial application of this Id to up to n-1 value arguments
443 -- does essentially no work. That is not necessarily the
444 -- same as saying that it has n leading lambdas, because coerces
445 -- may get in the way.
447 -- The arity might increase later in the compilation process, if
448 -- an extra lambda floats up to the binding site.
450 unknownArity :: Arity
451 unknownArity = 0 :: Arity
453 ppArityInfo :: Int -> SDoc
454 ppArityInfo 0 = empty
455 ppArityInfo n = hsep [ptext SLIT("Arity"), int n]
458 %************************************************************************
460 \subsection{Inline-pragma information}
462 %************************************************************************
465 type InlinePragInfo = Activation
466 -- Tells when the inlining is active
467 -- When it is active the thing may be inlined, depending on how
470 -- If there was an INLINE pragma, then as a separate matter, the
471 -- RHS will have been made to look small with a CoreSyn Inline Note
473 -- The default InlinePragInfo is AlwaysActive, so the info serves
474 -- entirely as a way to inhibit inlining until we want it
478 %************************************************************************
482 %************************************************************************
488 VarSet -- Locally-defined free vars of *both* LHS and RHS
489 -- of rules. I don't think it needs to include the
491 -- Note [Rule dependency info] in OccurAnal
493 emptySpecInfo :: SpecInfo
494 emptySpecInfo = SpecInfo [] emptyVarSet
496 isEmptySpecInfo :: SpecInfo -> Bool
497 isEmptySpecInfo (SpecInfo rs _) = null rs
499 specInfoFreeVars :: SpecInfo -> VarSet
500 specInfoFreeVars (SpecInfo _ fvs) = fvs
502 specInfoRules :: SpecInfo -> [CoreRule]
503 specInfoRules (SpecInfo rules _) = rules
505 setSpecInfoHead :: Name -> SpecInfo -> SpecInfo
506 setSpecInfoHead fn (SpecInfo rules fvs)
507 = SpecInfo (map set_head rules) fvs
509 set_head rule = rule { ru_fn = fn }
511 seqSpecInfo :: SpecInfo -> ()
512 seqSpecInfo (SpecInfo rules fvs) = seqRules rules `seq` seqVarSet fvs
517 %************************************************************************
519 \subsection[worker-IdInfo]{Worker info about an @Id@}
521 %************************************************************************
523 If this Id has a worker then we store a reference to it. Worker
524 functions are generated by the worker/wrapper pass. This uses
525 information from strictness analysis.
527 There might not be a worker, even for a strict function, because:
528 (a) the function might be small enough to inline, so no need
530 (b) the strictness info might be "SSS" or something, so no w/w split.
532 Sometimes the arity of a wrapper changes from the original arity from
533 which it was generated, so we always emit the "original" arity into
534 the interface file, as part of the worker info.
536 How can this happen? Sometimes we get
537 f = coerce t (\x y -> $wf x y)
538 at the moment of w/w split; but the eta reducer turns it into
540 which is perfectly fine except that the exposed arity so far as
541 the code generator is concerned (zero) differs from the arity
542 when we did the split (2).
544 All this arises because we use 'arity' to mean "exactly how many
545 top level lambdas are there" in interface files; but during the
546 compilation of this module it means "how many things can I apply
551 data WorkerInfo = NoWorker
553 -- The Arity is the arity of the *wrapper* at the moment of the
554 -- w/w split. See notes above.
556 seqWorker :: WorkerInfo -> ()
557 seqWorker (HasWorker id a) = id `seq` a `seq` ()
558 seqWorker NoWorker = ()
560 ppWorkerInfo :: WorkerInfo -> SDoc
561 ppWorkerInfo NoWorker = empty
562 ppWorkerInfo (HasWorker wk_id _) = ptext SLIT("Worker") <+> ppr wk_id
564 workerExists :: WorkerInfo -> Bool
565 workerExists NoWorker = False
566 workerExists (HasWorker _ _) = True
568 workerId :: WorkerInfo -> Id
569 workerId (HasWorker id _) = id
570 workerId NoWorker = panic "workerId: NoWorker"
572 wrapperArity :: WorkerInfo -> Arity
573 wrapperArity (HasWorker _ a) = a
574 wrapperArity NoWorker = panic "wrapperArity: NoWorker"
578 %************************************************************************
580 \subsection[CG-IdInfo]{Code generator-related information}
582 %************************************************************************
585 -- CafInfo is used to build Static Reference Tables (see simplStg/SRT.lhs).
588 = MayHaveCafRefs -- either:
589 -- (1) A function or static constructor
590 -- that refers to one or more CAFs,
591 -- (2) A real live CAF
593 | NoCafRefs -- A function or static constructor
594 -- that refers to no CAFs.
596 vanillaCafInfo :: CafInfo
597 vanillaCafInfo = MayHaveCafRefs -- Definitely safe
599 mayHaveCafRefs :: CafInfo -> Bool
600 mayHaveCafRefs MayHaveCafRefs = True
601 mayHaveCafRefs _ = False
603 seqCaf :: CafInfo -> ()
604 seqCaf c = c `seq` ()
606 ppCafInfo :: CafInfo -> SDoc
607 ppCafInfo NoCafRefs = ptext SLIT("NoCafRefs")
608 ppCafInfo MayHaveCafRefs = empty
611 %************************************************************************
613 \subsection[cpr-IdInfo]{Constructed Product Result info about an @Id@}
615 %************************************************************************
617 If the @Id@ is a function then it may have CPR info. A CPR analysis
618 phase detects whether:
622 The function's return value has a product type, i.e. an algebraic type
623 with a single constructor. Examples of such types are tuples and boxed
626 The function always 'constructs' the value that it is returning. It
627 must do this on every path through, and it's OK if it calls another
628 function which constructs the result.
631 If this is the case then we store a template which tells us the
632 function has the CPR property and which components of the result are
636 #ifdef OLD_STRICTNESS
639 | ReturnsCPR -- Yes, this function returns a constructed product
640 -- Implicitly, this means "after the function has been applied
641 -- to all its arguments", so the worker/wrapper builder in
642 -- WwLib.mkWWcpr checks that that it is indeed saturated before
643 -- making use of the CPR info
645 -- We used to keep nested info about sub-components, but
646 -- we never used it so I threw it away
648 seqCpr :: CprInfo -> ()
649 seqCpr ReturnsCPR = ()
650 seqCpr NoCPRInfo = ()
652 noCprInfo = NoCPRInfo
654 ppCprInfo NoCPRInfo = empty
655 ppCprInfo ReturnsCPR = ptext SLIT("__M")
657 instance Outputable CprInfo where
660 instance Show CprInfo where
661 showsPrec p c = showsPrecSDoc p (ppr c)
666 %************************************************************************
668 \subsection[lbvar-IdInfo]{Lambda-bound var info about an @Id@}
670 %************************************************************************
672 If the @Id@ is a lambda-bound variable then it may have lambda-bound
673 var info. Sometimes we know whether the lambda binding this var is a
674 ``one-shot'' lambda; that is, whether it is applied at most once.
676 This information may be useful in optimisation, as computations may
677 safely be floated inside such a lambda without risk of duplicating
681 data LBVarInfo = NoLBVarInfo
682 | IsOneShotLambda -- The lambda is applied at most once).
684 seqLBVar :: LBVarInfo -> ()
685 seqLBVar l = l `seq` ()
689 hasNoLBVarInfo :: LBVarInfo -> Bool
690 hasNoLBVarInfo NoLBVarInfo = True
691 hasNoLBVarInfo IsOneShotLambda = False
693 noLBVarInfo :: LBVarInfo
694 noLBVarInfo = NoLBVarInfo
696 pprLBVarInfo :: LBVarInfo -> SDoc
697 pprLBVarInfo NoLBVarInfo = empty
698 pprLBVarInfo IsOneShotLambda = ptext SLIT("OneShot")
700 instance Outputable LBVarInfo where
703 instance Show LBVarInfo where
704 showsPrec p c = showsPrecSDoc p (ppr c)
708 %************************************************************************
710 \subsection{Bulk operations on IdInfo}
712 %************************************************************************
714 @zapLamInfo@ is used for lambda binders that turn out to to be
715 part of an unsaturated lambda
718 zapLamInfo :: IdInfo -> Maybe IdInfo
719 zapLamInfo info@(IdInfo {occInfo = occ, newDemandInfo = demand})
720 | is_safe_occ occ && is_safe_dmd demand
723 = Just (info {occInfo = safe_occ, newDemandInfo = Nothing})
725 -- The "unsafe" occ info is the ones that say I'm not in a lambda
726 -- because that might not be true for an unsaturated lambda
727 is_safe_occ (OneOcc in_lam _ _) = in_lam
728 is_safe_occ _other = True
730 safe_occ = case occ of
731 OneOcc _ once int_cxt -> OneOcc insideLam once int_cxt
734 is_safe_dmd Nothing = True
735 is_safe_dmd (Just dmd) = not (isStrictDmd dmd)
739 zapDemandInfo :: IdInfo -> Maybe IdInfo
740 zapDemandInfo info@(IdInfo {newDemandInfo = dmd})
741 | isJust dmd = Just (info {newDemandInfo = Nothing})
742 | otherwise = Nothing
746 zapFragileInfo :: IdInfo -> Maybe IdInfo
747 -- Zap info that depends on free variables
749 = Just (info `setSpecInfo` emptySpecInfo
750 `setWorkerInfo` NoWorker
751 `setUnfoldingInfo` NoUnfolding
752 `setOccInfo` if isFragileOcc occ then NoOccInfo else occ)
757 %************************************************************************
759 \subsection{TickBoxOp}
761 %************************************************************************
767 = TickBox Module {-# UNPACK #-} !TickBoxId
768 -- ^Tick box for Hpc-style coverage
770 instance Outputable TickBoxOp where
771 ppr (TickBox mod n) = ptext SLIT("tick") <+> ppr (mod,n)