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,
19 shortableIdInfo, copyIdInfo,
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, setSpecInfo,
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 BasicTypes ( OccInfo(..), isFragileOcc, isDeadOcc, seqOccInfo, isLoopBreaker,
84 InsideLam, insideLam, notInsideLam,
85 OneBranch, oneBranch, notOneBranch,
89 import DataCon ( DataCon )
90 import TyCon ( TyCon, FieldLabel )
91 import ForeignCall ( ForeignCall )
94 import Maybe ( isJust )
98 import Demand hiding( Demand, seqDemand )
99 import qualified Demand
100 import Util ( listLengthCmp )
101 import List ( replicate )
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 -- setAllStrictnessInfo :: IdInfo -> Maybe StrictSig -> IdInfo
133 -- Set old and new strictness info
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 Nothing = ()
151 seqNewStrictnessInfo (Just ty) = seqStrictSig ty
153 pprNewStrictness Nothing = empty
154 pprNewStrictness (Just sig) = ftext FSLIT("Str:") <+> ppr sig
156 #ifdef OLD_STRICTNESS
157 oldStrictnessFromNew :: StrictSig -> Demand.StrictnessInfo
158 oldStrictnessFromNew sig = mkStrictnessInfo (map oldDemand dmds, isBotRes res_info)
160 (dmds, res_info) = splitStrictSig sig
162 cprInfoFromNewStrictness :: StrictSig -> CprInfo
163 cprInfoFromNewStrictness sig = case strictSigResInfo sig of
167 newStrictnessFromOld :: Name -> Arity -> Demand.StrictnessInfo -> CprInfo -> StrictSig
168 newStrictnessFromOld name arity (Demand.StrictnessInfo ds res) cpr
169 | listLengthCmp ds arity /= GT -- length ds <= arity
170 -- Sometimes the old strictness analyser has more
171 -- demands than the arity justifies
172 = mk_strict_sig name arity $
173 mkTopDmdType (map newDemand ds) (newRes res cpr)
175 newStrictnessFromOld name arity other cpr
176 = -- Either no strictness info, or arity is too small
177 -- In either case we can't say anything useful
178 mk_strict_sig name arity $
179 mkTopDmdType (replicate arity lazyDmd) (newRes False cpr)
181 mk_strict_sig name arity dmd_ty
182 = WARN( arity /= dmdTypeDepth dmd_ty, ppr name <+> (ppr arity $$ ppr dmd_ty) )
185 newRes True _ = BotRes
186 newRes False ReturnsCPR = retCPR
187 newRes False NoCPRInfo = TopRes
189 newDemand :: Demand.Demand -> NewDemand.Demand
190 newDemand (WwLazy True) = Abs
191 newDemand (WwLazy False) = lazyDmd
192 newDemand WwStrict = evalDmd
193 newDemand (WwUnpack unpk ds) = Eval (Prod (map newDemand ds))
194 newDemand WwPrim = lazyDmd
195 newDemand WwEnum = evalDmd
197 oldDemand :: NewDemand.Demand -> Demand.Demand
198 oldDemand Abs = WwLazy True
199 oldDemand Top = WwLazy False
200 oldDemand Bot = WwStrict
201 oldDemand (Box Bot) = WwStrict
202 oldDemand (Box Abs) = WwLazy False
203 oldDemand (Box (Eval _)) = WwStrict -- Pass box only
204 oldDemand (Defer d) = WwLazy False
205 oldDemand (Eval (Prod ds)) = WwUnpack True (map oldDemand ds)
206 oldDemand (Eval (Poly _)) = WwStrict
207 oldDemand (Call _) = WwStrict
209 #endif /* OLD_STRICTNESS */
214 seqNewDemandInfo Nothing = ()
215 seqNewDemandInfo (Just dmd) = seqDemand dmd
219 %************************************************************************
221 \subsection{GlobalIdDetails
223 %************************************************************************
225 This type is here (rather than in Id.lhs) mainly because there's
226 an IdInfo.hi-boot, but no Id.hi-boot, and GlobalIdDetails is imported
227 (recursively) by Var.lhs.
231 = VanillaGlobal -- Imported from elsewhere, a default method Id.
233 | RecordSelId TyCon FieldLabel -- The Id for a record selector
235 | DataConWorkId DataCon -- The Id for a data constructor *worker*
236 | DataConWrapId DataCon -- The Id for a data constructor *wrapper*
237 -- [the only reasons we need to know is so that
238 -- a) we can suppress printing a definition in the interface file
239 -- b) when typechecking a pattern we can get from the
240 -- Id back to the data con]
242 | ClassOpId Class -- An operation of a class
244 | PrimOpId PrimOp -- The Id for a primitive operator
245 | FCallId ForeignCall -- The Id for a foreign call
247 | NotGlobalId -- Used as a convenient extra return value from globalIdDetails
249 notGlobalId = NotGlobalId
251 instance Outputable GlobalIdDetails where
252 ppr NotGlobalId = ptext SLIT("[***NotGlobalId***]")
253 ppr VanillaGlobal = ptext SLIT("[GlobalId]")
254 ppr (DataConWorkId _) = ptext SLIT("[DataCon]")
255 ppr (DataConWrapId _) = ptext SLIT("[DataConWrapper]")
256 ppr (ClassOpId _) = ptext SLIT("[ClassOp]")
257 ppr (PrimOpId _) = ptext SLIT("[PrimOp]")
258 ppr (FCallId _) = ptext SLIT("[ForeignCall]")
259 ppr (RecordSelId _ _) = ptext SLIT("[RecSel]")
263 %************************************************************************
265 \subsection{The main IdInfo type}
267 %************************************************************************
269 An @IdInfo@ gives {\em optional} information about an @Id@. If
270 present it never lies, but it may not be present, in which case there
271 is always a conservative assumption which can be made.
273 Two @Id@s may have different info even though they have the same
274 @Unique@ (and are hence the same @Id@); for example, one might lack
275 the properties attached to the other.
277 The @IdInfo@ gives information about the value, or definition, of the
278 @Id@. It does {\em not} contain information about the @Id@'s usage
279 (except for @DemandInfo@? ToDo). (@lbvarInfo@ is also a marginal
285 arityInfo :: !ArityInfo, -- Its arity
286 specInfo :: CoreRules, -- Specialisations of this function which exist
287 #ifdef OLD_STRICTNESS
288 cprInfo :: CprInfo, -- Function always constructs a product result
289 demandInfo :: Demand.Demand, -- Whether or not it is definitely demanded
290 strictnessInfo :: StrictnessInfo, -- Strictness properties
292 workerInfo :: WorkerInfo, -- Pointer to Worker Function
293 unfoldingInfo :: Unfolding, -- Its unfolding
294 cafInfo :: CafInfo, -- CAF info
295 lbvarInfo :: LBVarInfo, -- Info about a lambda-bound variable
296 inlinePragInfo :: InlinePragInfo, -- Inline pragma
297 occInfo :: OccInfo, -- How it occurs
299 newStrictnessInfo :: Maybe StrictSig, -- Reason for Maybe: the DmdAnal phase needs to
300 -- know whether whether this is the first visit,
301 -- so it can assign botSig. Other customers want
302 -- topSig. So Nothing is good.
304 newDemandInfo :: Maybe Demand -- Similarly we want to know if there's no
305 -- known demand yet, for when we are looking for
309 seqIdInfo :: IdInfo -> ()
310 seqIdInfo (IdInfo {}) = ()
312 megaSeqIdInfo :: IdInfo -> ()
314 = seqRules (specInfo info) `seq`
315 seqWorker (workerInfo info) `seq`
317 -- Omitting this improves runtimes a little, presumably because
318 -- some unfoldings are not calculated at all
319 -- seqUnfolding (unfoldingInfo info) `seq`
321 seqNewDemandInfo (newDemandInfo info) `seq`
322 seqNewStrictnessInfo (newStrictnessInfo info) `seq`
324 #ifdef OLD_STRICTNESS
325 Demand.seqDemand (demandInfo info) `seq`
326 seqStrictnessInfo (strictnessInfo info) `seq`
327 seqCpr (cprInfo info) `seq`
330 seqCaf (cafInfo info) `seq`
331 seqLBVar (lbvarInfo info) `seq`
332 seqOccInfo (occInfo info)
338 setWorkerInfo info wk = wk `seq` info { workerInfo = wk }
339 setSpecInfo info sp = sp `seq` info { specInfo = sp }
340 setInlinePragInfo info pr = pr `seq` info { inlinePragInfo = pr }
341 setOccInfo info oc = oc `seq` info { occInfo = oc }
342 #ifdef OLD_STRICTNESS
343 setStrictnessInfo info st = st `seq` info { strictnessInfo = st }
345 -- Try to avoid spack leaks by seq'ing
347 setUnfoldingInfoLazily info uf -- Lazy variant to avoid looking at the
348 = -- unfolding of an imported Id unless necessary
349 info { unfoldingInfo = uf } -- (In this case the demand-zapping is redundant.)
351 setUnfoldingInfo info uf
352 -- We do *not* seq on the unfolding info, For some reason, doing so
353 -- actually increases residency significantly.
354 = info { unfoldingInfo = uf }
356 #ifdef OLD_STRICTNESS
357 setDemandInfo info dd = info { demandInfo = dd }
358 setCprInfo info cp = info { cprInfo = cp }
361 setArityInfo info ar = info { arityInfo = ar }
362 setCafInfo info caf = info { cafInfo = caf }
364 setLBVarInfo info lb = {-lb `seq`-} info { lbvarInfo = lb }
366 setNewDemandInfo info dd = dd `seq` info { newDemandInfo = dd }
367 setNewStrictnessInfo info dd = dd `seq` info { newStrictnessInfo = dd }
372 vanillaIdInfo :: IdInfo
375 cafInfo = vanillaCafInfo,
376 arityInfo = unknownArity,
377 #ifdef OLD_STRICTNESS
380 strictnessInfo = NoStrictnessInfo,
382 specInfo = emptyCoreRules,
383 workerInfo = NoWorker,
384 unfoldingInfo = noUnfolding,
385 lbvarInfo = NoLBVarInfo,
386 inlinePragInfo = AlwaysActive,
388 newDemandInfo = Nothing,
389 newStrictnessInfo = Nothing
392 noCafIdInfo = vanillaIdInfo `setCafInfo` NoCafRefs
393 -- Used for built-in type Ids in MkId.
397 %************************************************************************
399 \subsection[arity-IdInfo]{Arity info about an @Id@}
401 %************************************************************************
403 For locally-defined Ids, the code generator maintains its own notion
404 of their arities; so it should not be asking... (but other things
405 besides the code-generator need arity info!)
408 type ArityInfo = Arity
409 -- A partial application of this Id to up to n-1 value arguments
410 -- does essentially no work. That is not necessarily the
411 -- same as saying that it has n leading lambdas, because coerces
412 -- may get in the way.
414 -- The arity might increase later in the compilation process, if
415 -- an extra lambda floats up to the binding site.
417 unknownArity = 0 :: Arity
419 ppArityInfo 0 = empty
420 ppArityInfo n = hsep [ptext SLIT("Arity"), int n]
423 %************************************************************************
425 \subsection{Inline-pragma information}
427 %************************************************************************
430 type InlinePragInfo = Activation
431 -- Tells when the inlining is active
432 -- When it is active the thing may be inlined, depending on how
435 -- If there was an INLINE pragma, then as a separate matter, the
436 -- RHS will have been made to look small with a CoreSyn Inline Note
440 %************************************************************************
442 \subsection[worker-IdInfo]{Worker info about an @Id@}
444 %************************************************************************
446 If this Id has a worker then we store a reference to it. Worker
447 functions are generated by the worker/wrapper pass. This uses
448 information from strictness analysis.
450 There might not be a worker, even for a strict function, because:
451 (a) the function might be small enough to inline, so no need
453 (b) the strictness info might be "SSS" or something, so no w/w split.
455 Sometimes the arity of a wrapper changes from the original arity from
456 which it was generated, so we always emit the "original" arity into
457 the interface file, as part of the worker info.
459 How can this happen? Sometimes we get
460 f = coerce t (\x y -> $wf x y)
461 at the moment of w/w split; but the eta reducer turns it into
463 which is perfectly fine except that the exposed arity so far as
464 the code generator is concerned (zero) differs from the arity
465 when we did the split (2).
467 All this arises because we use 'arity' to mean "exactly how many
468 top level lambdas are there" in interface files; but during the
469 compilation of this module it means "how many things can I apply
474 data WorkerInfo = NoWorker
476 -- The Arity is the arity of the *wrapper* at the moment of the
477 -- w/w split. See notes above.
479 seqWorker :: WorkerInfo -> ()
480 seqWorker (HasWorker id a) = id `seq` a `seq` ()
481 seqWorker NoWorker = ()
483 ppWorkerInfo NoWorker = empty
484 ppWorkerInfo (HasWorker wk_id _) = ptext SLIT("__P") <+> ppr wk_id
486 workerExists :: WorkerInfo -> Bool
487 workerExists NoWorker = False
488 workerExists (HasWorker _ _) = True
490 workerId :: WorkerInfo -> Id
491 workerId (HasWorker id _) = id
493 wrapperArity :: WorkerInfo -> Arity
494 wrapperArity (HasWorker _ a) = a
498 %************************************************************************
500 \subsection[CG-IdInfo]{Code generator-related information}
502 %************************************************************************
505 -- CafInfo is used to build Static Reference Tables (see simplStg/SRT.lhs).
508 = MayHaveCafRefs -- either:
509 -- (1) A function or static constructor
510 -- that refers to one or more CAFs,
511 -- (2) A real live CAF
513 | NoCafRefs -- A function or static constructor
514 -- that refers to no CAFs.
516 vanillaCafInfo = MayHaveCafRefs -- Definitely safe
518 mayHaveCafRefs MayHaveCafRefs = True
519 mayHaveCafRefs _ = False
521 seqCaf c = c `seq` ()
523 ppCafInfo NoCafRefs = ptext SLIT("NoCafRefs")
524 ppCafInfo MayHaveCafRefs = empty
527 %************************************************************************
529 \subsection[cpr-IdInfo]{Constructed Product Result info about an @Id@}
531 %************************************************************************
533 If the @Id@ is a function then it may have CPR info. A CPR analysis
534 phase detects whether:
538 The function's return value has a product type, i.e. an algebraic type
539 with a single constructor. Examples of such types are tuples and boxed
542 The function always 'constructs' the value that it is returning. It
543 must do this on every path through, and it's OK if it calls another
544 function which constructs the result.
547 If this is the case then we store a template which tells us the
548 function has the CPR property and which components of the result are
552 #ifdef OLD_STRICTNESS
555 | ReturnsCPR -- Yes, this function returns a constructed product
556 -- Implicitly, this means "after the function has been applied
557 -- to all its arguments", so the worker/wrapper builder in
558 -- WwLib.mkWWcpr checks that that it is indeed saturated before
559 -- making use of the CPR info
561 -- We used to keep nested info about sub-components, but
562 -- we never used it so I threw it away
564 seqCpr :: CprInfo -> ()
565 seqCpr ReturnsCPR = ()
566 seqCpr NoCPRInfo = ()
568 noCprInfo = NoCPRInfo
570 ppCprInfo NoCPRInfo = empty
571 ppCprInfo ReturnsCPR = ptext SLIT("__M")
573 instance Outputable CprInfo where
576 instance Show CprInfo where
577 showsPrec p c = showsPrecSDoc p (ppr c)
582 %************************************************************************
584 \subsection[lbvar-IdInfo]{Lambda-bound var info about an @Id@}
586 %************************************************************************
588 If the @Id@ is a lambda-bound variable then it may have lambda-bound
589 var info. Sometimes we know whether the lambda binding this var is a
590 ``one-shot'' lambda; that is, whether it is applied at most once.
592 This information may be useful in optimisation, as computations may
593 safely be floated inside such a lambda without risk of duplicating
597 data LBVarInfo = NoLBVarInfo
598 | IsOneShotLambda -- The lambda is applied at most once).
600 seqLBVar l = l `seq` ()
604 hasNoLBVarInfo NoLBVarInfo = True
605 hasNoLBVarInfo IsOneShotLambda = False
607 noLBVarInfo = NoLBVarInfo
609 pprLBVarInfo NoLBVarInfo = empty
610 pprLBVarInfo IsOneShotLambda = ptext SLIT("OneShot")
612 instance Outputable LBVarInfo where
615 instance Show LBVarInfo where
616 showsPrec p c = showsPrecSDoc p (ppr c)
620 %************************************************************************
622 \subsection{Bulk operations on IdInfo}
624 %************************************************************************
626 @zapLamInfo@ is used for lambda binders that turn out to to be
627 part of an unsaturated lambda
630 zapLamInfo :: IdInfo -> Maybe IdInfo
631 zapLamInfo info@(IdInfo {occInfo = occ, newDemandInfo = demand})
632 | is_safe_occ occ && is_safe_dmd demand
635 = Just (info {occInfo = safe_occ, newDemandInfo = Nothing})
637 -- The "unsafe" occ info is the ones that say I'm not in a lambda
638 -- because that might not be true for an unsaturated lambda
639 is_safe_occ (OneOcc in_lam once) = in_lam
640 is_safe_occ other = True
642 safe_occ = case occ of
643 OneOcc _ once -> OneOcc insideLam once
646 is_safe_dmd Nothing = True
647 is_safe_dmd (Just dmd) = not (isStrictDmd dmd)
651 zapDemandInfo :: IdInfo -> Maybe IdInfo
652 zapDemandInfo info@(IdInfo {newDemandInfo = dmd})
653 | isJust dmd = Just (info {newDemandInfo = Nothing})
654 | otherwise = Nothing
658 copyIdInfo is used when shorting out a top-level binding
661 where f is exported. We are going to swizzle it around to
665 BUT (a) we must be careful about messing up rules
666 (b) we must ensure f's IdInfo ends up right
668 (a) Messing up the rules
670 The example that went bad on me was this one:
672 iterate :: (a -> a) -> a -> [a]
673 iterate = iterateList
675 iterateFB c f x = x `c` iterateFB c f (f x)
676 iterateList f x = x : iterateList f (f x)
679 "iterate" forall f x. iterate f x = build (\c _n -> iterateFB c f x)
680 "iterateFB" iterateFB (:) = iterateList
683 This got shorted out to:
685 iterateList :: (a -> a) -> a -> [a]
686 iterateList = iterate
688 iterateFB c f x = x `c` iterateFB c f (f x)
689 iterate f x = x : iterate f (f x)
692 "iterate" forall f x. iterate f x = build (\c _n -> iterateFB c f x)
693 "iterateFB" iterateFB (:) = iterate
696 And now we get an infinite loop in the rule system
697 iterate f x -> build (\cn -> iterateFB c f x)
701 Tiresome solution: don't do shorting out if f has rewrite rules.
702 Hence shortableIdInfo.
704 (b) Keeping the IdInfo right
705 ~~~~~~~~~~~~~~~~~~~~~~~~
706 We want to move strictness/worker info from f_local to f, but keep the rest.
710 shortableIdInfo :: IdInfo -> Bool
711 shortableIdInfo info = isEmptyCoreRules (specInfo info)
713 copyIdInfo :: IdInfo -- f_local
714 -> IdInfo -- f (the exported one)
715 -> IdInfo -- New info for f
716 copyIdInfo f_local f = f { newStrictnessInfo = newStrictnessInfo f_local,
717 #ifdef OLD_STRICTNESS
718 strictnessInfo = strictnessInfo f_local,
719 cprInfo = cprInfo f_local,
721 workerInfo = workerInfo f_local