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,
99 import qualified Demand
104 -- infixl so you can say (id `set` a `set` b)
105 infixl 1 `setSpecInfo`,
113 `setNewStrictnessInfo`,
114 `setAllStrictnessInfo`,
116 #ifdef OLD_STRICTNESS
119 , `setStrictnessInfo`
123 %************************************************************************
125 \subsection{New strictness info}
127 %************************************************************************
132 -- Set old and new strictness info
133 setAllStrictnessInfo :: IdInfo -> Maybe StrictSig -> IdInfo
134 setAllStrictnessInfo info Nothing
135 = info { newStrictnessInfo = Nothing
136 #ifdef OLD_STRICTNESS
137 , strictnessInfo = NoStrictnessInfo
138 , cprInfo = NoCPRInfo
142 setAllStrictnessInfo info (Just sig)
143 = info { newStrictnessInfo = Just sig
144 #ifdef OLD_STRICTNESS
145 , strictnessInfo = oldStrictnessFromNew sig
146 , cprInfo = cprInfoFromNewStrictness sig
150 seqNewStrictnessInfo :: Maybe StrictSig -> ()
151 seqNewStrictnessInfo Nothing = ()
152 seqNewStrictnessInfo (Just ty) = seqStrictSig ty
154 pprNewStrictness :: Maybe StrictSig -> SDoc
155 pprNewStrictness Nothing = empty
156 pprNewStrictness (Just sig) = ftext (fsLit "Str:") <+> ppr sig
158 #ifdef OLD_STRICTNESS
159 oldStrictnessFromNew :: StrictSig -> Demand.StrictnessInfo
160 oldStrictnessFromNew sig = mkStrictnessInfo (map oldDemand dmds, isBotRes res_info)
162 (dmds, res_info) = splitStrictSig sig
164 cprInfoFromNewStrictness :: StrictSig -> CprInfo
165 cprInfoFromNewStrictness sig = case strictSigResInfo sig of
169 newStrictnessFromOld :: Name -> Arity -> Demand.StrictnessInfo -> CprInfo -> StrictSig
170 newStrictnessFromOld name arity (Demand.StrictnessInfo ds res) cpr
171 | listLengthCmp ds arity /= GT -- length ds <= arity
172 -- Sometimes the old strictness analyser has more
173 -- demands than the arity justifies
174 = mk_strict_sig name arity $
175 mkTopDmdType (map newDemand ds) (newRes res cpr)
177 newStrictnessFromOld name arity other cpr
178 = -- Either no strictness info, or arity is too small
179 -- In either case we can't say anything useful
180 mk_strict_sig name arity $
181 mkTopDmdType (replicate arity lazyDmd) (newRes False cpr)
183 mk_strict_sig name arity dmd_ty
184 = WARN( arity /= dmdTypeDepth dmd_ty, ppr name <+> (ppr arity $$ ppr dmd_ty) )
187 newRes True _ = BotRes
188 newRes False ReturnsCPR = retCPR
189 newRes False NoCPRInfo = TopRes
191 newDemand :: Demand.Demand -> NewDemand.Demand
192 newDemand (WwLazy True) = Abs
193 newDemand (WwLazy False) = lazyDmd
194 newDemand WwStrict = evalDmd
195 newDemand (WwUnpack unpk ds) = Eval (Prod (map newDemand ds))
196 newDemand WwPrim = lazyDmd
197 newDemand WwEnum = evalDmd
199 oldDemand :: NewDemand.Demand -> Demand.Demand
200 oldDemand Abs = WwLazy True
201 oldDemand Top = WwLazy False
202 oldDemand Bot = WwStrict
203 oldDemand (Box Bot) = WwStrict
204 oldDemand (Box Abs) = WwLazy False
205 oldDemand (Box (Eval _)) = WwStrict -- Pass box only
206 oldDemand (Defer d) = WwLazy False
207 oldDemand (Eval (Prod ds)) = WwUnpack True (map oldDemand ds)
208 oldDemand (Eval (Poly _)) = WwStrict
209 oldDemand (Call _) = WwStrict
211 #endif /* OLD_STRICTNESS */
216 seqNewDemandInfo :: Maybe Demand -> ()
217 seqNewDemandInfo Nothing = ()
218 seqNewDemandInfo (Just dmd) = seqDemand dmd
222 %************************************************************************
224 \subsection{GlobalIdDetails}
226 %************************************************************************
228 This type is here (rather than in Id.lhs) mainly because there's
229 an IdInfo.hi-boot, but no Id.hi-boot, and GlobalIdDetails is imported
230 (recursively) by Var.lhs.
234 = VanillaGlobal -- Imported from elsewhere, a default method Id.
236 | RecordSelId -- The Id for a record selector
237 { sel_tycon :: TyCon -- For a data type family, this is the *instance* TyCon
238 -- not the family TyCon
239 , sel_label :: FieldLabel
240 , sel_naughty :: Bool -- True <=> naughty
241 } -- See Note [Naughty record selectors]
242 -- with MkId.mkRecordSelectorId
244 | DataConWorkId DataCon -- The Id for a data constructor *worker*
245 | DataConWrapId DataCon -- The Id for a data constructor *wrapper*
246 -- [the only reasons we need to know is so that
247 -- a) to support isImplicitId
248 -- b) when desugaring a RecordCon we can get
249 -- from the Id back to the data con]
251 | ClassOpId Class -- An operation of a class
253 | PrimOpId PrimOp -- The Id for a primitive operator
254 | FCallId ForeignCall -- The Id for a foreign call
256 | TickBoxOpId TickBoxOp -- The Id for a tick box (both traditional and binary)
258 | NotGlobalId -- Used as a convenient extra return value from globalIdDetails
260 notGlobalId :: GlobalIdDetails
261 notGlobalId = NotGlobalId
263 instance Outputable GlobalIdDetails where
264 ppr NotGlobalId = ptext (sLit "[***NotGlobalId***]")
265 ppr VanillaGlobal = ptext (sLit "[GlobalId]")
266 ppr (DataConWorkId _) = ptext (sLit "[DataCon]")
267 ppr (DataConWrapId _) = ptext (sLit "[DataConWrapper]")
268 ppr (ClassOpId _) = ptext (sLit "[ClassOp]")
269 ppr (PrimOpId _) = ptext (sLit "[PrimOp]")
270 ppr (FCallId _) = ptext (sLit "[ForeignCall]")
271 ppr (TickBoxOpId _) = ptext (sLit "[TickBoxOp]")
272 ppr (RecordSelId {}) = ptext (sLit "[RecSel]")
276 %************************************************************************
278 \subsection{The main IdInfo type}
280 %************************************************************************
282 An @IdInfo@ gives {\em optional} information about an @Id@. If
283 present it never lies, but it may not be present, in which case there
284 is always a conservative assumption which can be made.
286 Two @Id@s may have different info even though they have the same
287 @Unique@ (and are hence the same @Id@); for example, one might lack
288 the properties attached to the other.
290 The @IdInfo@ gives information about the value, or definition, of the
291 @Id@. It does {\em not} contain information about the @Id@'s usage
292 (except for @DemandInfo@? ToDo). (@lbvarInfo@ is also a marginal
298 arityInfo :: !ArityInfo, -- Its arity
299 specInfo :: SpecInfo, -- Specialisations of this function which exist
300 #ifdef OLD_STRICTNESS
301 cprInfo :: CprInfo, -- Function always constructs a product result
302 demandInfo :: Demand.Demand, -- Whether or not it is definitely demanded
303 strictnessInfo :: StrictnessInfo, -- Strictness properties
305 workerInfo :: WorkerInfo, -- Pointer to Worker Function
306 -- Within one module this is irrelevant; the
307 -- inlining of a worker is handled via the Unfolding
308 -- WorkerInfo is used *only* to indicate the form of
309 -- the RHS, so that interface files don't actually
310 -- need to contain the RHS; it can be derived from
311 -- the strictness info
313 unfoldingInfo :: Unfolding, -- Its unfolding
314 cafInfo :: CafInfo, -- CAF info
315 lbvarInfo :: LBVarInfo, -- Info about a lambda-bound variable
316 inlinePragInfo :: InlinePragInfo, -- Inline pragma
317 occInfo :: OccInfo, -- How it occurs
319 newStrictnessInfo :: Maybe StrictSig, -- Reason for Maybe: the DmdAnal phase needs to
320 -- know whether whether this is the first visit,
321 -- so it can assign botSig. Other customers want
322 -- topSig. So Nothing is good.
324 newDemandInfo :: Maybe Demand -- Similarly we want to know if there's no
325 -- known demand yet, for when we are looking for
329 seqIdInfo :: IdInfo -> ()
330 seqIdInfo (IdInfo {}) = ()
332 megaSeqIdInfo :: IdInfo -> ()
334 = seqSpecInfo (specInfo info) `seq`
335 seqWorker (workerInfo info) `seq`
337 -- Omitting this improves runtimes a little, presumably because
338 -- some unfoldings are not calculated at all
339 -- seqUnfolding (unfoldingInfo info) `seq`
341 seqNewDemandInfo (newDemandInfo info) `seq`
342 seqNewStrictnessInfo (newStrictnessInfo info) `seq`
344 #ifdef OLD_STRICTNESS
345 Demand.seqDemand (demandInfo info) `seq`
346 seqStrictnessInfo (strictnessInfo info) `seq`
347 seqCpr (cprInfo info) `seq`
350 seqCaf (cafInfo info) `seq`
351 seqLBVar (lbvarInfo info) `seq`
352 seqOccInfo (occInfo info)
358 setWorkerInfo :: IdInfo -> WorkerInfo -> IdInfo
359 setWorkerInfo info wk = wk `seq` info { workerInfo = wk }
360 setSpecInfo :: IdInfo -> SpecInfo -> IdInfo
361 setSpecInfo info sp = sp `seq` info { specInfo = sp }
362 setInlinePragInfo :: IdInfo -> InlinePragInfo -> IdInfo
363 setInlinePragInfo info pr = pr `seq` info { inlinePragInfo = pr }
364 setOccInfo :: IdInfo -> OccInfo -> IdInfo
365 setOccInfo info oc = oc `seq` info { occInfo = oc }
366 #ifdef OLD_STRICTNESS
367 setStrictnessInfo info st = st `seq` info { strictnessInfo = st }
369 -- Try to avoid spack leaks by seq'ing
371 setUnfoldingInfoLazily :: IdInfo -> Unfolding -> IdInfo
372 setUnfoldingInfoLazily info uf -- Lazy variant to avoid looking at the
373 = -- unfolding of an imported Id unless necessary
374 info { unfoldingInfo = uf } -- (In this case the demand-zapping is redundant.)
376 setUnfoldingInfo :: IdInfo -> Unfolding -> IdInfo
377 setUnfoldingInfo info uf
378 -- We do *not* seq on the unfolding info, For some reason, doing so
379 -- actually increases residency significantly.
380 = info { unfoldingInfo = uf }
382 #ifdef OLD_STRICTNESS
383 setDemandInfo info dd = info { demandInfo = dd }
384 setCprInfo info cp = info { cprInfo = cp }
387 setArityInfo :: IdInfo -> ArityInfo -> IdInfo
388 setArityInfo info ar = info { arityInfo = ar }
389 setCafInfo :: IdInfo -> CafInfo -> IdInfo
390 setCafInfo info caf = info { cafInfo = caf }
392 setLBVarInfo :: IdInfo -> LBVarInfo -> IdInfo
393 setLBVarInfo info lb = {-lb `seq`-} info { lbvarInfo = lb }
395 setNewDemandInfo :: IdInfo -> Maybe Demand -> IdInfo
396 setNewDemandInfo info dd = dd `seq` info { newDemandInfo = dd }
397 setNewStrictnessInfo :: IdInfo -> Maybe StrictSig -> IdInfo
398 setNewStrictnessInfo info dd = dd `seq` info { newStrictnessInfo = dd }
403 vanillaIdInfo :: IdInfo
406 cafInfo = vanillaCafInfo,
407 arityInfo = unknownArity,
408 #ifdef OLD_STRICTNESS
411 strictnessInfo = NoStrictnessInfo,
413 specInfo = emptySpecInfo,
414 workerInfo = NoWorker,
415 unfoldingInfo = noUnfolding,
416 lbvarInfo = NoLBVarInfo,
417 inlinePragInfo = AlwaysActive,
419 newDemandInfo = Nothing,
420 newStrictnessInfo = Nothing
423 noCafIdInfo :: IdInfo
424 noCafIdInfo = vanillaIdInfo `setCafInfo` NoCafRefs
425 -- Used for built-in type Ids in MkId.
429 %************************************************************************
431 \subsection[arity-IdInfo]{Arity info about an @Id@}
433 %************************************************************************
435 For locally-defined Ids, the code generator maintains its own notion
436 of their arities; so it should not be asking... (but other things
437 besides the code-generator need arity info!)
440 type ArityInfo = Arity
441 -- A partial application of this Id to up to n-1 value arguments
442 -- does essentially no work. That is not necessarily the
443 -- same as saying that it has n leading lambdas, because coerces
444 -- may get in the way.
446 -- The arity might increase later in the compilation process, if
447 -- an extra lambda floats up to the binding site.
449 unknownArity :: Arity
450 unknownArity = 0 :: Arity
452 ppArityInfo :: Int -> SDoc
453 ppArityInfo 0 = empty
454 ppArityInfo n = hsep [ptext (sLit "Arity"), int n]
457 %************************************************************************
459 \subsection{Inline-pragma information}
461 %************************************************************************
464 type InlinePragInfo = Activation
465 -- Tells when the inlining is active
466 -- When it is active the thing may be inlined, depending on how
469 -- If there was an INLINE pragma, then as a separate matter, the
470 -- RHS will have been made to look small with a CoreSyn Inline Note
472 -- The default InlinePragInfo is AlwaysActive, so the info serves
473 -- entirely as a way to inhibit inlining until we want it
477 %************************************************************************
481 %************************************************************************
487 VarSet -- Locally-defined free vars of *both* LHS and RHS
488 -- of rules. I don't think it needs to include the
490 -- Note [Rule dependency info] in OccurAnal
492 emptySpecInfo :: SpecInfo
493 emptySpecInfo = SpecInfo [] emptyVarSet
495 isEmptySpecInfo :: SpecInfo -> Bool
496 isEmptySpecInfo (SpecInfo rs _) = null rs
498 specInfoFreeVars :: SpecInfo -> VarSet
499 specInfoFreeVars (SpecInfo _ fvs) = fvs
501 specInfoRules :: SpecInfo -> [CoreRule]
502 specInfoRules (SpecInfo rules _) = rules
504 setSpecInfoHead :: Name -> SpecInfo -> SpecInfo
505 setSpecInfoHead fn (SpecInfo rules fvs)
506 = SpecInfo (map set_head rules) fvs
508 set_head rule = rule { ru_fn = fn }
510 seqSpecInfo :: SpecInfo -> ()
511 seqSpecInfo (SpecInfo rules fvs) = seqRules rules `seq` seqVarSet fvs
516 %************************************************************************
518 \subsection[worker-IdInfo]{Worker info about an @Id@}
520 %************************************************************************
522 If this Id has a worker then we store a reference to it. Worker
523 functions are generated by the worker/wrapper pass. This uses
524 information from strictness analysis.
526 There might not be a worker, even for a strict function, because:
527 (a) the function might be small enough to inline, so no need
529 (b) the strictness info might be "SSS" or something, so no w/w split.
531 Sometimes the arity of a wrapper changes from the original arity from
532 which it was generated, so we always emit the "original" arity into
533 the interface file, as part of the worker info.
535 How can this happen? Sometimes we get
536 f = coerce t (\x y -> $wf x y)
537 at the moment of w/w split; but the eta reducer turns it into
539 which is perfectly fine except that the exposed arity so far as
540 the code generator is concerned (zero) differs from the arity
541 when we did the split (2).
543 All this arises because we use 'arity' to mean "exactly how many
544 top level lambdas are there" in interface files; but during the
545 compilation of this module it means "how many things can I apply
550 data WorkerInfo = NoWorker
552 -- The Arity is the arity of the *wrapper* at the moment of the
553 -- w/w split. See notes above.
555 seqWorker :: WorkerInfo -> ()
556 seqWorker (HasWorker id a) = id `seq` a `seq` ()
557 seqWorker NoWorker = ()
559 ppWorkerInfo :: WorkerInfo -> SDoc
560 ppWorkerInfo NoWorker = empty
561 ppWorkerInfo (HasWorker wk_id _) = ptext (sLit "Worker") <+> ppr wk_id
563 workerExists :: WorkerInfo -> Bool
564 workerExists NoWorker = False
565 workerExists (HasWorker _ _) = True
567 workerId :: WorkerInfo -> Id
568 workerId (HasWorker id _) = id
569 workerId NoWorker = panic "workerId: NoWorker"
571 wrapperArity :: WorkerInfo -> Arity
572 wrapperArity (HasWorker _ a) = a
573 wrapperArity NoWorker = panic "wrapperArity: NoWorker"
577 %************************************************************************
579 \subsection[CG-IdInfo]{Code generator-related information}
581 %************************************************************************
584 -- CafInfo is used to build Static Reference Tables (see simplStg/SRT.lhs).
587 = MayHaveCafRefs -- either:
588 -- (1) A function or static constructor
589 -- that refers to one or more CAFs,
590 -- (2) A real live CAF
592 | NoCafRefs -- A function or static constructor
593 -- that refers to no CAFs.
595 vanillaCafInfo :: CafInfo
596 vanillaCafInfo = MayHaveCafRefs -- Definitely safe
598 mayHaveCafRefs :: CafInfo -> Bool
599 mayHaveCafRefs MayHaveCafRefs = True
600 mayHaveCafRefs _ = False
602 seqCaf :: CafInfo -> ()
603 seqCaf c = c `seq` ()
605 ppCafInfo :: CafInfo -> SDoc
606 ppCafInfo NoCafRefs = ptext (sLit "NoCafRefs")
607 ppCafInfo MayHaveCafRefs = empty
610 %************************************************************************
612 \subsection[cpr-IdInfo]{Constructed Product Result info about an @Id@}
614 %************************************************************************
616 If the @Id@ is a function then it may have CPR info. A CPR analysis
617 phase detects whether:
621 The function's return value has a product type, i.e. an algebraic type
622 with a single constructor. Examples of such types are tuples and boxed
625 The function always 'constructs' the value that it is returning. It
626 must do this on every path through, and it's OK if it calls another
627 function which constructs the result.
630 If this is the case then we store a template which tells us the
631 function has the CPR property and which components of the result are
635 #ifdef OLD_STRICTNESS
638 | ReturnsCPR -- Yes, this function returns a constructed product
639 -- Implicitly, this means "after the function has been applied
640 -- to all its arguments", so the worker/wrapper builder in
641 -- WwLib.mkWWcpr checks that that it is indeed saturated before
642 -- making use of the CPR info
644 -- We used to keep nested info about sub-components, but
645 -- we never used it so I threw it away
647 seqCpr :: CprInfo -> ()
648 seqCpr ReturnsCPR = ()
649 seqCpr NoCPRInfo = ()
651 noCprInfo = NoCPRInfo
653 ppCprInfo NoCPRInfo = empty
654 ppCprInfo ReturnsCPR = ptext (sLit "__M")
656 instance Outputable CprInfo where
659 instance Show CprInfo where
660 showsPrec p c = showsPrecSDoc p (ppr c)
665 %************************************************************************
667 \subsection[lbvar-IdInfo]{Lambda-bound var info about an @Id@}
669 %************************************************************************
671 If the @Id@ is a lambda-bound variable then it may have lambda-bound
672 var info. Sometimes we know whether the lambda binding this var is a
673 ``one-shot'' lambda; that is, whether it is applied at most once.
675 This information may be useful in optimisation, as computations may
676 safely be floated inside such a lambda without risk of duplicating
680 data LBVarInfo = NoLBVarInfo
681 | IsOneShotLambda -- The lambda is applied at most once).
683 seqLBVar :: LBVarInfo -> ()
684 seqLBVar l = l `seq` ()
688 hasNoLBVarInfo :: LBVarInfo -> Bool
689 hasNoLBVarInfo NoLBVarInfo = True
690 hasNoLBVarInfo IsOneShotLambda = False
692 noLBVarInfo :: LBVarInfo
693 noLBVarInfo = NoLBVarInfo
695 pprLBVarInfo :: LBVarInfo -> SDoc
696 pprLBVarInfo NoLBVarInfo = empty
697 pprLBVarInfo IsOneShotLambda = ptext (sLit "OneShot")
699 instance Outputable LBVarInfo where
702 instance Show LBVarInfo where
703 showsPrec p c = showsPrecSDoc p (ppr c)
707 %************************************************************************
709 \subsection{Bulk operations on IdInfo}
711 %************************************************************************
713 @zapLamInfo@ is used for lambda binders that turn out to to be
714 part of an unsaturated lambda
717 zapLamInfo :: IdInfo -> Maybe IdInfo
718 zapLamInfo info@(IdInfo {occInfo = occ, newDemandInfo = demand})
719 | is_safe_occ occ && is_safe_dmd demand
722 = Just (info {occInfo = safe_occ, newDemandInfo = Nothing})
724 -- The "unsafe" occ info is the ones that say I'm not in a lambda
725 -- because that might not be true for an unsaturated lambda
726 is_safe_occ (OneOcc in_lam _ _) = in_lam
727 is_safe_occ _other = True
729 safe_occ = case occ of
730 OneOcc _ once int_cxt -> OneOcc insideLam once int_cxt
733 is_safe_dmd Nothing = True
734 is_safe_dmd (Just dmd) = not (isStrictDmd dmd)
738 zapDemandInfo :: IdInfo -> Maybe IdInfo
739 zapDemandInfo info@(IdInfo {newDemandInfo = dmd})
740 | isJust dmd = Just (info {newDemandInfo = Nothing})
741 | otherwise = Nothing
745 zapFragileInfo :: IdInfo -> Maybe IdInfo
746 -- Zap info that depends on free variables
748 = Just (info `setSpecInfo` emptySpecInfo
749 `setWorkerInfo` NoWorker
750 `setUnfoldingInfo` NoUnfolding
751 `setOccInfo` if isFragileOcc occ then NoOccInfo else occ)
756 %************************************************************************
758 \subsection{TickBoxOp}
760 %************************************************************************
766 = TickBox Module {-# UNPACK #-} !TickBoxId
767 -- ^Tick box for Hpc-style coverage
769 instance Outputable TickBoxOp where
770 ppr (TickBox mod n) = ptext (sLit "tick") <+> ppr (mod,n)