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
11 GlobalIdDetails(..), notGlobalId, -- Not abstract
14 vanillaIdInfo, noCafIdInfo,
15 seqIdInfo, megaSeqIdInfo,
18 zapLamInfo, zapDemandInfo,
23 arityInfo, setArityInfo, ppArityInfo,
25 -- New demand and strictness info
26 newStrictnessInfo, setNewStrictnessInfo,
27 newDemandInfo, setNewDemandInfo, pprNewStrictness,
31 -- Strictness; imported from Demand
33 mkStrictnessInfo, noStrictnessInfo,
34 ppStrictnessInfo,isBottomingStrictness,
38 WorkerInfo(..), workerExists, wrapperArity, workerId,
39 workerInfo, setWorkerInfo, ppWorkerInfo,
42 unfoldingInfo, setUnfoldingInfo, setUnfoldingInfoLazily,
45 -- Old DemandInfo and StrictnessInfo
46 demandInfo, setDemandInfo,
47 strictnessInfo, setStrictnessInfo,
48 cprInfoFromNewStrictness,
49 oldStrictnessFromNew, newStrictnessFromOld,
52 -- Constructed Product Result Info
53 CprInfo(..), cprInfo, setCprInfo, ppCprInfo, noCprInfo,
58 inlinePragInfo, setInlinePragInfo,
61 OccInfo(..), isFragileOcc, isDeadOcc, isLoopBreaker,
62 InsideLam, OneBranch, insideLam, notInsideLam, oneBranch, notOneBranch,
66 SpecInfo(..), specInfo, setSpecInfo, isEmptySpecInfo,
67 specInfoFreeVars, specInfoRules, seqSpecInfo,
70 CafInfo(..), cafInfo, ppCafInfo, setCafInfo, mayHaveCafRefs,
72 -- Lambda-bound variable info
73 LBVarInfo(..), lbvarInfo, setLBVarInfo, noLBVarInfo, hasNoLBVarInfo
76 #include "HsVersions.h"
80 import Class ( Class )
81 import PrimOp ( PrimOp )
83 import VarSet ( VarSet, emptyVarSet, seqVarSet )
84 import BasicTypes ( OccInfo(..), isFragileOcc, isDeadOcc, seqOccInfo, isLoopBreaker,
85 InsideLam, insideLam, notInsideLam,
86 OneBranch, oneBranch, notOneBranch,
90 import DataCon ( DataCon )
91 import TyCon ( TyCon, FieldLabel )
92 import ForeignCall ( ForeignCall )
95 import Maybe ( isJust )
99 import Demand hiding( Demand, seqDemand )
100 import qualified Demand
101 import Util ( listLengthCmp )
102 import List ( replicate )
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 -- setAllStrictnessInfo :: IdInfo -> Maybe StrictSig -> IdInfo
134 -- Set old and new strictness info
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 Nothing = ()
152 seqNewStrictnessInfo (Just ty) = seqStrictSig ty
154 pprNewStrictness Nothing = empty
155 pprNewStrictness (Just sig) = ftext FSLIT("Str:") <+> ppr sig
157 #ifdef OLD_STRICTNESS
158 oldStrictnessFromNew :: StrictSig -> Demand.StrictnessInfo
159 oldStrictnessFromNew sig = mkStrictnessInfo (map oldDemand dmds, isBotRes res_info)
161 (dmds, res_info) = splitStrictSig sig
163 cprInfoFromNewStrictness :: StrictSig -> CprInfo
164 cprInfoFromNewStrictness sig = case strictSigResInfo sig of
168 newStrictnessFromOld :: Name -> Arity -> Demand.StrictnessInfo -> CprInfo -> StrictSig
169 newStrictnessFromOld name arity (Demand.StrictnessInfo ds res) cpr
170 | listLengthCmp ds arity /= GT -- length ds <= arity
171 -- Sometimes the old strictness analyser has more
172 -- demands than the arity justifies
173 = mk_strict_sig name arity $
174 mkTopDmdType (map newDemand ds) (newRes res cpr)
176 newStrictnessFromOld name arity other cpr
177 = -- Either no strictness info, or arity is too small
178 -- In either case we can't say anything useful
179 mk_strict_sig name arity $
180 mkTopDmdType (replicate arity lazyDmd) (newRes False cpr)
182 mk_strict_sig name arity dmd_ty
183 = WARN( arity /= dmdTypeDepth dmd_ty, ppr name <+> (ppr arity $$ ppr dmd_ty) )
186 newRes True _ = BotRes
187 newRes False ReturnsCPR = retCPR
188 newRes False NoCPRInfo = TopRes
190 newDemand :: Demand.Demand -> NewDemand.Demand
191 newDemand (WwLazy True) = Abs
192 newDemand (WwLazy False) = lazyDmd
193 newDemand WwStrict = evalDmd
194 newDemand (WwUnpack unpk ds) = Eval (Prod (map newDemand ds))
195 newDemand WwPrim = lazyDmd
196 newDemand WwEnum = evalDmd
198 oldDemand :: NewDemand.Demand -> Demand.Demand
199 oldDemand Abs = WwLazy True
200 oldDemand Top = WwLazy False
201 oldDemand Bot = WwStrict
202 oldDemand (Box Bot) = WwStrict
203 oldDemand (Box Abs) = WwLazy False
204 oldDemand (Box (Eval _)) = WwStrict -- Pass box only
205 oldDemand (Defer d) = WwLazy False
206 oldDemand (Eval (Prod ds)) = WwUnpack True (map oldDemand ds)
207 oldDemand (Eval (Poly _)) = WwStrict
208 oldDemand (Call _) = WwStrict
210 #endif /* OLD_STRICTNESS */
215 seqNewDemandInfo Nothing = ()
216 seqNewDemandInfo (Just dmd) = seqDemand dmd
220 %************************************************************************
222 \subsection{GlobalIdDetails
224 %************************************************************************
226 This type is here (rather than in Id.lhs) mainly because there's
227 an IdInfo.hi-boot, but no Id.hi-boot, and GlobalIdDetails is imported
228 (recursively) by Var.lhs.
232 = VanillaGlobal -- Imported from elsewhere, a default method Id.
234 | RecordSelId -- The Id for a record selector
236 , sel_label :: FieldLabel
237 , sel_naughty :: Bool -- True <=> naughty
238 } -- See Note [Naughty record selectors]
239 -- with MkId.mkRecordSelectorId
241 | DataConWorkId DataCon -- The Id for a data constructor *worker*
242 | DataConWrapId DataCon -- The Id for a data constructor *wrapper*
243 -- [the only reasons we need to know is so that
244 -- a) to support isImplicitId
245 -- b) when desugaring a RecordCon we can get
246 -- from the Id back to the data con]
248 | ClassOpId Class -- An operation of a class
250 | PrimOpId PrimOp -- The Id for a primitive operator
251 | FCallId ForeignCall -- The Id for a foreign call
253 | NotGlobalId -- Used as a convenient extra return value from globalIdDetails
255 notGlobalId = NotGlobalId
257 instance Outputable GlobalIdDetails where
258 ppr NotGlobalId = ptext SLIT("[***NotGlobalId***]")
259 ppr VanillaGlobal = ptext SLIT("[GlobalId]")
260 ppr (DataConWorkId _) = ptext SLIT("[DataCon]")
261 ppr (DataConWrapId _) = ptext SLIT("[DataConWrapper]")
262 ppr (ClassOpId _) = ptext SLIT("[ClassOp]")
263 ppr (PrimOpId _) = ptext SLIT("[PrimOp]")
264 ppr (FCallId _) = ptext SLIT("[ForeignCall]")
265 ppr (RecordSelId {}) = ptext SLIT("[RecSel]")
269 %************************************************************************
271 \subsection{The main IdInfo type}
273 %************************************************************************
275 An @IdInfo@ gives {\em optional} information about an @Id@. If
276 present it never lies, but it may not be present, in which case there
277 is always a conservative assumption which can be made.
279 Two @Id@s may have different info even though they have the same
280 @Unique@ (and are hence the same @Id@); for example, one might lack
281 the properties attached to the other.
283 The @IdInfo@ gives information about the value, or definition, of the
284 @Id@. It does {\em not} contain information about the @Id@'s usage
285 (except for @DemandInfo@? ToDo). (@lbvarInfo@ is also a marginal
291 arityInfo :: !ArityInfo, -- Its arity
292 specInfo :: SpecInfo, -- Specialisations of this function which exist
293 #ifdef OLD_STRICTNESS
294 cprInfo :: CprInfo, -- Function always constructs a product result
295 demandInfo :: Demand.Demand, -- Whether or not it is definitely demanded
296 strictnessInfo :: StrictnessInfo, -- Strictness properties
298 workerInfo :: WorkerInfo, -- Pointer to Worker Function
299 -- Within one module this is irrelevant; the
300 -- inlining of a worker is handled via the Unfolding
301 -- WorkerInfo is used *only* to indicate the form of
302 -- the RHS, so that interface files don't actually
303 -- need to contain the RHS; it can be derived from
304 -- the strictness info
306 unfoldingInfo :: Unfolding, -- Its unfolding
307 cafInfo :: CafInfo, -- CAF info
308 lbvarInfo :: LBVarInfo, -- Info about a lambda-bound variable
309 inlinePragInfo :: InlinePragInfo, -- Inline pragma
310 occInfo :: OccInfo, -- How it occurs
312 newStrictnessInfo :: Maybe StrictSig, -- Reason for Maybe: the DmdAnal phase needs to
313 -- know whether whether this is the first visit,
314 -- so it can assign botSig. Other customers want
315 -- topSig. So Nothing is good.
317 newDemandInfo :: Maybe Demand -- Similarly we want to know if there's no
318 -- known demand yet, for when we are looking for
322 seqIdInfo :: IdInfo -> ()
323 seqIdInfo (IdInfo {}) = ()
325 megaSeqIdInfo :: IdInfo -> ()
327 = seqSpecInfo (specInfo info) `seq`
328 seqWorker (workerInfo info) `seq`
330 -- Omitting this improves runtimes a little, presumably because
331 -- some unfoldings are not calculated at all
332 -- seqUnfolding (unfoldingInfo info) `seq`
334 seqNewDemandInfo (newDemandInfo info) `seq`
335 seqNewStrictnessInfo (newStrictnessInfo info) `seq`
337 #ifdef OLD_STRICTNESS
338 Demand.seqDemand (demandInfo info) `seq`
339 seqStrictnessInfo (strictnessInfo info) `seq`
340 seqCpr (cprInfo info) `seq`
343 seqCaf (cafInfo info) `seq`
344 seqLBVar (lbvarInfo info) `seq`
345 seqOccInfo (occInfo info)
351 setWorkerInfo info wk = wk `seq` info { workerInfo = wk }
352 setSpecInfo info sp = sp `seq` info { specInfo = sp }
353 setInlinePragInfo info pr = pr `seq` info { inlinePragInfo = pr }
354 setOccInfo info oc = oc `seq` info { occInfo = oc }
355 #ifdef OLD_STRICTNESS
356 setStrictnessInfo info st = st `seq` info { strictnessInfo = st }
358 -- Try to avoid spack leaks by seq'ing
360 setUnfoldingInfoLazily info uf -- Lazy variant to avoid looking at the
361 = -- unfolding of an imported Id unless necessary
362 info { unfoldingInfo = uf } -- (In this case the demand-zapping is redundant.)
364 setUnfoldingInfo info uf
365 -- We do *not* seq on the unfolding info, For some reason, doing so
366 -- actually increases residency significantly.
367 = info { unfoldingInfo = uf }
369 #ifdef OLD_STRICTNESS
370 setDemandInfo info dd = info { demandInfo = dd }
371 setCprInfo info cp = info { cprInfo = cp }
374 setArityInfo info ar = info { arityInfo = ar }
375 setCafInfo info caf = info { cafInfo = caf }
377 setLBVarInfo info lb = {-lb `seq`-} info { lbvarInfo = lb }
379 setNewDemandInfo info dd = dd `seq` info { newDemandInfo = dd }
380 setNewStrictnessInfo info dd = dd `seq` info { newStrictnessInfo = dd }
385 vanillaIdInfo :: IdInfo
388 cafInfo = vanillaCafInfo,
389 arityInfo = unknownArity,
390 #ifdef OLD_STRICTNESS
393 strictnessInfo = NoStrictnessInfo,
395 specInfo = emptySpecInfo,
396 workerInfo = NoWorker,
397 unfoldingInfo = noUnfolding,
398 lbvarInfo = NoLBVarInfo,
399 inlinePragInfo = AlwaysActive,
401 newDemandInfo = Nothing,
402 newStrictnessInfo = Nothing
405 noCafIdInfo = vanillaIdInfo `setCafInfo` NoCafRefs
406 -- Used for built-in type Ids in MkId.
410 %************************************************************************
412 \subsection[arity-IdInfo]{Arity info about an @Id@}
414 %************************************************************************
416 For locally-defined Ids, the code generator maintains its own notion
417 of their arities; so it should not be asking... (but other things
418 besides the code-generator need arity info!)
421 type ArityInfo = Arity
422 -- A partial application of this Id to up to n-1 value arguments
423 -- does essentially no work. That is not necessarily the
424 -- same as saying that it has n leading lambdas, because coerces
425 -- may get in the way.
427 -- The arity might increase later in the compilation process, if
428 -- an extra lambda floats up to the binding site.
430 unknownArity = 0 :: Arity
432 ppArityInfo 0 = empty
433 ppArityInfo n = hsep [ptext SLIT("Arity"), int n]
436 %************************************************************************
438 \subsection{Inline-pragma information}
440 %************************************************************************
443 type InlinePragInfo = Activation
444 -- Tells when the inlining is active
445 -- When it is active the thing may be inlined, depending on how
448 -- If there was an INLINE pragma, then as a separate matter, the
449 -- RHS will have been made to look small with a CoreSyn Inline Note
451 -- The default InlinePragInfo is AlwaysActive, so the info serves
452 -- entirely as a way to inhibit inlining until we want it
456 %************************************************************************
460 %************************************************************************
463 -- CoreRules is used only in an idSpecialisation (move to IdInfo?)
465 = SpecInfo [CoreRule] VarSet -- Locally-defined free vars of RHSs
467 emptySpecInfo :: SpecInfo
468 emptySpecInfo = SpecInfo [] emptyVarSet
470 isEmptySpecInfo :: SpecInfo -> Bool
471 isEmptySpecInfo (SpecInfo rs _) = null rs
473 specInfoFreeVars :: SpecInfo -> VarSet
474 specInfoFreeVars (SpecInfo _ fvs) = fvs
476 specInfoRules :: SpecInfo -> [CoreRule]
477 specInfoRules (SpecInfo rules _) = rules
479 seqSpecInfo (SpecInfo rules fvs) = seqRules rules `seq` seqVarSet fvs
483 %************************************************************************
485 \subsection[worker-IdInfo]{Worker info about an @Id@}
487 %************************************************************************
489 If this Id has a worker then we store a reference to it. Worker
490 functions are generated by the worker/wrapper pass. This uses
491 information from strictness analysis.
493 There might not be a worker, even for a strict function, because:
494 (a) the function might be small enough to inline, so no need
496 (b) the strictness info might be "SSS" or something, so no w/w split.
498 Sometimes the arity of a wrapper changes from the original arity from
499 which it was generated, so we always emit the "original" arity into
500 the interface file, as part of the worker info.
502 How can this happen? Sometimes we get
503 f = coerce t (\x y -> $wf x y)
504 at the moment of w/w split; but the eta reducer turns it into
506 which is perfectly fine except that the exposed arity so far as
507 the code generator is concerned (zero) differs from the arity
508 when we did the split (2).
510 All this arises because we use 'arity' to mean "exactly how many
511 top level lambdas are there" in interface files; but during the
512 compilation of this module it means "how many things can I apply
517 data WorkerInfo = NoWorker
519 -- The Arity is the arity of the *wrapper* at the moment of the
520 -- w/w split. See notes above.
522 seqWorker :: WorkerInfo -> ()
523 seqWorker (HasWorker id a) = id `seq` a `seq` ()
524 seqWorker NoWorker = ()
526 ppWorkerInfo NoWorker = empty
527 ppWorkerInfo (HasWorker wk_id _) = ptext SLIT("Worker") <+> ppr wk_id
529 workerExists :: WorkerInfo -> Bool
530 workerExists NoWorker = False
531 workerExists (HasWorker _ _) = True
533 workerId :: WorkerInfo -> Id
534 workerId (HasWorker id _) = id
536 wrapperArity :: WorkerInfo -> Arity
537 wrapperArity (HasWorker _ a) = a
541 %************************************************************************
543 \subsection[CG-IdInfo]{Code generator-related information}
545 %************************************************************************
548 -- CafInfo is used to build Static Reference Tables (see simplStg/SRT.lhs).
551 = MayHaveCafRefs -- either:
552 -- (1) A function or static constructor
553 -- that refers to one or more CAFs,
554 -- (2) A real live CAF
556 | NoCafRefs -- A function or static constructor
557 -- that refers to no CAFs.
559 vanillaCafInfo = MayHaveCafRefs -- Definitely safe
561 mayHaveCafRefs MayHaveCafRefs = True
562 mayHaveCafRefs _ = False
564 seqCaf c = c `seq` ()
566 ppCafInfo NoCafRefs = ptext SLIT("NoCafRefs")
567 ppCafInfo MayHaveCafRefs = empty
570 %************************************************************************
572 \subsection[cpr-IdInfo]{Constructed Product Result info about an @Id@}
574 %************************************************************************
576 If the @Id@ is a function then it may have CPR info. A CPR analysis
577 phase detects whether:
581 The function's return value has a product type, i.e. an algebraic type
582 with a single constructor. Examples of such types are tuples and boxed
585 The function always 'constructs' the value that it is returning. It
586 must do this on every path through, and it's OK if it calls another
587 function which constructs the result.
590 If this is the case then we store a template which tells us the
591 function has the CPR property and which components of the result are
595 #ifdef OLD_STRICTNESS
598 | ReturnsCPR -- Yes, this function returns a constructed product
599 -- Implicitly, this means "after the function has been applied
600 -- to all its arguments", so the worker/wrapper builder in
601 -- WwLib.mkWWcpr checks that that it is indeed saturated before
602 -- making use of the CPR info
604 -- We used to keep nested info about sub-components, but
605 -- we never used it so I threw it away
607 seqCpr :: CprInfo -> ()
608 seqCpr ReturnsCPR = ()
609 seqCpr NoCPRInfo = ()
611 noCprInfo = NoCPRInfo
613 ppCprInfo NoCPRInfo = empty
614 ppCprInfo ReturnsCPR = ptext SLIT("__M")
616 instance Outputable CprInfo where
619 instance Show CprInfo where
620 showsPrec p c = showsPrecSDoc p (ppr c)
625 %************************************************************************
627 \subsection[lbvar-IdInfo]{Lambda-bound var info about an @Id@}
629 %************************************************************************
631 If the @Id@ is a lambda-bound variable then it may have lambda-bound
632 var info. Sometimes we know whether the lambda binding this var is a
633 ``one-shot'' lambda; that is, whether it is applied at most once.
635 This information may be useful in optimisation, as computations may
636 safely be floated inside such a lambda without risk of duplicating
640 data LBVarInfo = NoLBVarInfo
641 | IsOneShotLambda -- The lambda is applied at most once).
643 seqLBVar l = l `seq` ()
647 hasNoLBVarInfo NoLBVarInfo = True
648 hasNoLBVarInfo IsOneShotLambda = False
650 noLBVarInfo = NoLBVarInfo
652 pprLBVarInfo NoLBVarInfo = empty
653 pprLBVarInfo IsOneShotLambda = ptext SLIT("OneShot")
655 instance Outputable LBVarInfo where
658 instance Show LBVarInfo where
659 showsPrec p c = showsPrecSDoc p (ppr c)
663 %************************************************************************
665 \subsection{Bulk operations on IdInfo}
667 %************************************************************************
669 @zapLamInfo@ is used for lambda binders that turn out to to be
670 part of an unsaturated lambda
673 zapLamInfo :: IdInfo -> Maybe IdInfo
674 zapLamInfo info@(IdInfo {occInfo = occ, newDemandInfo = demand})
675 | is_safe_occ occ && is_safe_dmd demand
678 = Just (info {occInfo = safe_occ, newDemandInfo = Nothing})
680 -- The "unsafe" occ info is the ones that say I'm not in a lambda
681 -- because that might not be true for an unsaturated lambda
682 is_safe_occ (OneOcc in_lam _ _) = in_lam
683 is_safe_occ other = True
685 safe_occ = case occ of
686 OneOcc _ once int_cxt -> OneOcc insideLam once int_cxt
689 is_safe_dmd Nothing = True
690 is_safe_dmd (Just dmd) = not (isStrictDmd dmd)
694 zapDemandInfo :: IdInfo -> Maybe IdInfo
695 zapDemandInfo info@(IdInfo {newDemandInfo = dmd})
696 | isJust dmd = Just (info {newDemandInfo = Nothing})
697 | otherwise = Nothing