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 TyCon ( TyCon )
81 import Class ( Class )
82 import PrimOp ( PrimOp )
84 import BasicTypes ( OccInfo(..), isFragileOcc, isDeadOcc, seqOccInfo, isLoopBreaker,
85 InsideLam, insideLam, notInsideLam,
86 OneBranch, oneBranch, notOneBranch,
90 import DataCon ( DataCon )
91 import ForeignCall ( ForeignCall )
92 import FieldLabel ( FieldLabel )
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 | GenericOpId TyCon -- The to/from operations of a
235 | RecordSelId FieldLabel -- The Id for a record selector
236 | DataConWorkId DataCon -- The Id for a data constructor *worker*
237 | DataConWrapId DataCon -- The Id for a data constructor *wrapper*
238 -- [the only reasons we need to know is so that
239 -- a) we can suppress printing a definition in the interface file
240 -- b) when typechecking a pattern we can get from the
241 -- Id back to the data con]
243 | ClassOpId Class -- An operation of a class
245 | PrimOpId PrimOp -- The Id for a primitive operator
246 | FCallId ForeignCall -- The Id for a foreign call
248 | NotGlobalId -- Used as a convenient extra return value from globalIdDetails
250 notGlobalId = NotGlobalId
252 instance Outputable GlobalIdDetails where
253 ppr NotGlobalId = ptext SLIT("[***NotGlobalId***]")
254 ppr VanillaGlobal = ptext SLIT("[GlobalId]")
255 ppr (GenericOpId _) = ptext SLIT("[GenericOp]")
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 :: CoreRules, -- 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 unfoldingInfo :: Unfolding, -- Its unfolding
296 cafInfo :: CafInfo, -- CAF info
297 lbvarInfo :: LBVarInfo, -- Info about a lambda-bound variable
298 inlinePragInfo :: InlinePragInfo, -- Inline pragma
299 occInfo :: OccInfo, -- How it occurs
301 newStrictnessInfo :: Maybe StrictSig, -- Reason for Maybe: the DmdAnal phase needs to
302 -- know whether whether this is the first visit,
303 -- so it can assign botSig. Other customers want
304 -- topSig. So Nothing is good.
306 newDemandInfo :: Maybe Demand -- Similarly we want to know if there's no
307 -- known demand yet, for when we are looking for
311 seqIdInfo :: IdInfo -> ()
312 seqIdInfo (IdInfo {}) = ()
314 megaSeqIdInfo :: IdInfo -> ()
316 = seqRules (specInfo info) `seq`
317 seqWorker (workerInfo info) `seq`
319 -- Omitting this improves runtimes a little, presumably because
320 -- some unfoldings are not calculated at all
321 -- seqUnfolding (unfoldingInfo info) `seq`
323 seqNewDemandInfo (newDemandInfo info) `seq`
324 seqNewStrictnessInfo (newStrictnessInfo info) `seq`
326 #ifdef OLD_STRICTNESS
327 Demand.seqDemand (demandInfo info) `seq`
328 seqStrictnessInfo (strictnessInfo info) `seq`
329 seqCpr (cprInfo info) `seq`
332 seqCaf (cafInfo info) `seq`
333 seqLBVar (lbvarInfo info) `seq`
334 seqOccInfo (occInfo info)
340 setWorkerInfo info wk = wk `seq` info { workerInfo = wk }
341 setSpecInfo info sp = sp `seq` info { specInfo = sp }
342 setInlinePragInfo info pr = pr `seq` info { inlinePragInfo = pr }
343 setOccInfo info oc = oc `seq` info { occInfo = oc }
344 #ifdef OLD_STRICTNESS
345 setStrictnessInfo info st = st `seq` info { strictnessInfo = st }
347 -- Try to avoid spack leaks by seq'ing
349 setUnfoldingInfoLazily info uf -- Lazy variant to avoid looking at the
350 = -- unfolding of an imported Id unless necessary
351 info { unfoldingInfo = uf } -- (In this case the demand-zapping is redundant.)
353 setUnfoldingInfo info uf
354 -- We do *not* seq on the unfolding info, For some reason, doing so
355 -- actually increases residency significantly.
356 = info { unfoldingInfo = uf }
358 #ifdef OLD_STRICTNESS
359 setDemandInfo info dd = info { demandInfo = dd }
360 setCprInfo info cp = info { cprInfo = cp }
363 setArityInfo info ar = info { arityInfo = ar }
364 setCafInfo info caf = info { cafInfo = caf }
366 setLBVarInfo info lb = {-lb `seq`-} info { lbvarInfo = lb }
368 setNewDemandInfo info dd = dd `seq` info { newDemandInfo = dd }
369 setNewStrictnessInfo info dd = dd `seq` info { newStrictnessInfo = dd }
374 vanillaIdInfo :: IdInfo
377 cafInfo = vanillaCafInfo,
378 arityInfo = unknownArity,
379 #ifdef OLD_STRICTNESS
382 strictnessInfo = NoStrictnessInfo,
384 specInfo = emptyCoreRules,
385 workerInfo = NoWorker,
386 unfoldingInfo = noUnfolding,
387 lbvarInfo = NoLBVarInfo,
388 inlinePragInfo = AlwaysActive,
390 newDemandInfo = Nothing,
391 newStrictnessInfo = Nothing
394 noCafIdInfo = vanillaIdInfo `setCafInfo` NoCafRefs
395 -- Used for built-in type Ids in MkId.
399 %************************************************************************
401 \subsection[arity-IdInfo]{Arity info about an @Id@}
403 %************************************************************************
405 For locally-defined Ids, the code generator maintains its own notion
406 of their arities; so it should not be asking... (but other things
407 besides the code-generator need arity info!)
410 type ArityInfo = Arity
411 -- A partial application of this Id to up to n-1 value arguments
412 -- does essentially no work. That is not necessarily the
413 -- same as saying that it has n leading lambdas, because coerces
414 -- may get in the way.
416 -- The arity might increase later in the compilation process, if
417 -- an extra lambda floats up to the binding site.
419 unknownArity = 0 :: Arity
421 ppArityInfo 0 = empty
422 ppArityInfo n = hsep [ptext SLIT("Arity"), int n]
425 %************************************************************************
427 \subsection{Inline-pragma information}
429 %************************************************************************
432 type InlinePragInfo = Activation
433 -- Tells when the inlining is active
434 -- When it is active the thing may be inlined, depending on how
437 -- If there was an INLINE pragma, then as a separate matter, the
438 -- RHS will have been made to look small with a CoreSyn Inline Note
442 %************************************************************************
444 \subsection[worker-IdInfo]{Worker info about an @Id@}
446 %************************************************************************
448 If this Id has a worker then we store a reference to it. Worker
449 functions are generated by the worker/wrapper pass. This uses
450 information from strictness analysis.
452 There might not be a worker, even for a strict function, because:
453 (a) the function might be small enough to inline, so no need
455 (b) the strictness info might be "SSS" or something, so no w/w split.
457 Sometimes the arity of a wrapper changes from the original arity from
458 which it was generated, so we always emit the "original" arity into
459 the interface file, as part of the worker info.
461 How can this happen? Sometimes we get
462 f = coerce t (\x y -> $wf x y)
463 at the moment of w/w split; but the eta reducer turns it into
465 which is perfectly fine except that the exposed arity so far as
466 the code generator is concerned (zero) differs from the arity
467 when we did the split (2).
469 All this arises because we use 'arity' to mean "exactly how many
470 top level lambdas are there" in interface files; but during the
471 compilation of this module it means "how many things can I apply
476 data WorkerInfo = NoWorker
478 -- The Arity is the arity of the *wrapper* at the moment of the
479 -- w/w split. See comments in MkIface.ifaceId, with the 'Worker' code.
481 seqWorker :: WorkerInfo -> ()
482 seqWorker (HasWorker id a) = id `seq` a `seq` ()
483 seqWorker NoWorker = ()
485 ppWorkerInfo NoWorker = empty
486 ppWorkerInfo (HasWorker wk_id _) = ptext SLIT("__P") <+> ppr wk_id
488 workerExists :: WorkerInfo -> Bool
489 workerExists NoWorker = False
490 workerExists (HasWorker _ _) = True
492 workerId :: WorkerInfo -> Id
493 workerId (HasWorker id _) = id
495 wrapperArity :: WorkerInfo -> Arity
496 wrapperArity (HasWorker _ a) = a
500 %************************************************************************
502 \subsection[CG-IdInfo]{Code generator-related information}
504 %************************************************************************
507 -- CafInfo is used to build Static Reference Tables (see simplStg/SRT.lhs).
510 = MayHaveCafRefs -- either:
511 -- (1) A function or static constructor
512 -- that refers to one or more CAFs,
513 -- (2) A real live CAF
515 | NoCafRefs -- A function or static constructor
516 -- that refers to no CAFs.
518 vanillaCafInfo = MayHaveCafRefs -- Definitely safe
520 mayHaveCafRefs MayHaveCafRefs = True
521 mayHaveCafRefs _ = False
523 seqCaf c = c `seq` ()
525 ppCafInfo NoCafRefs = ptext SLIT("NoCafRefs")
526 ppCafInfo MayHaveCafRefs = empty
529 %************************************************************************
531 \subsection[cpr-IdInfo]{Constructed Product Result info about an @Id@}
533 %************************************************************************
535 If the @Id@ is a function then it may have CPR info. A CPR analysis
536 phase detects whether:
540 The function's return value has a product type, i.e. an algebraic type
541 with a single constructor. Examples of such types are tuples and boxed
544 The function always 'constructs' the value that it is returning. It
545 must do this on every path through, and it's OK if it calls another
546 function which constructs the result.
549 If this is the case then we store a template which tells us the
550 function has the CPR property and which components of the result are
554 #ifdef OLD_STRICTNESS
557 | ReturnsCPR -- Yes, this function returns a constructed product
558 -- Implicitly, this means "after the function has been applied
559 -- to all its arguments", so the worker/wrapper builder in
560 -- WwLib.mkWWcpr checks that that it is indeed saturated before
561 -- making use of the CPR info
563 -- We used to keep nested info about sub-components, but
564 -- we never used it so I threw it away
566 seqCpr :: CprInfo -> ()
567 seqCpr ReturnsCPR = ()
568 seqCpr NoCPRInfo = ()
570 noCprInfo = NoCPRInfo
572 ppCprInfo NoCPRInfo = empty
573 ppCprInfo ReturnsCPR = ptext SLIT("__M")
575 instance Outputable CprInfo where
578 instance Show CprInfo where
579 showsPrec p c = showsPrecSDoc p (ppr c)
584 %************************************************************************
586 \subsection[lbvar-IdInfo]{Lambda-bound var info about an @Id@}
588 %************************************************************************
590 If the @Id@ is a lambda-bound variable then it may have lambda-bound
591 var info. Sometimes we know whether the lambda binding this var is a
592 ``one-shot'' lambda; that is, whether it is applied at most once.
594 This information may be useful in optimisation, as computations may
595 safely be floated inside such a lambda without risk of duplicating
599 data LBVarInfo = NoLBVarInfo
600 | IsOneShotLambda -- The lambda is applied at most once).
602 seqLBVar l = l `seq` ()
606 hasNoLBVarInfo NoLBVarInfo = True
607 hasNoLBVarInfo IsOneShotLambda = False
609 noLBVarInfo = NoLBVarInfo
611 pprLBVarInfo NoLBVarInfo = empty
612 pprLBVarInfo IsOneShotLambda = ptext SLIT("OneShot")
614 instance Outputable LBVarInfo where
617 instance Show LBVarInfo where
618 showsPrec p c = showsPrecSDoc p (ppr c)
622 %************************************************************************
624 \subsection{Bulk operations on IdInfo}
626 %************************************************************************
628 @zapLamInfo@ is used for lambda binders that turn out to to be
629 part of an unsaturated lambda
632 zapLamInfo :: IdInfo -> Maybe IdInfo
633 zapLamInfo info@(IdInfo {occInfo = occ, newDemandInfo = demand})
634 | is_safe_occ occ && is_safe_dmd demand
637 = Just (info {occInfo = safe_occ, newDemandInfo = Nothing})
639 -- The "unsafe" occ info is the ones that say I'm not in a lambda
640 -- because that might not be true for an unsaturated lambda
641 is_safe_occ (OneOcc in_lam once) = in_lam
642 is_safe_occ other = True
644 safe_occ = case occ of
645 OneOcc _ once -> OneOcc insideLam once
648 is_safe_dmd Nothing = True
649 is_safe_dmd (Just dmd) = not (isStrictDmd dmd)
653 zapDemandInfo :: IdInfo -> Maybe IdInfo
654 zapDemandInfo info@(IdInfo {newDemandInfo = dmd})
655 | isJust dmd = Just (info {newDemandInfo = Nothing})
656 | otherwise = Nothing
660 copyIdInfo is used when shorting out a top-level binding
663 where f is exported. We are going to swizzle it around to
667 BUT (a) we must be careful about messing up rules
668 (b) we must ensure f's IdInfo ends up right
670 (a) Messing up the rules
672 The example that went bad on me was this one:
674 iterate :: (a -> a) -> a -> [a]
675 iterate = iterateList
677 iterateFB c f x = x `c` iterateFB c f (f x)
678 iterateList f x = x : iterateList f (f x)
681 "iterate" forall f x. iterate f x = build (\c _n -> iterateFB c f x)
682 "iterateFB" iterateFB (:) = iterateList
685 This got shorted out to:
687 iterateList :: (a -> a) -> a -> [a]
688 iterateList = iterate
690 iterateFB c f x = x `c` iterateFB c f (f x)
691 iterate f x = x : iterate f (f x)
694 "iterate" forall f x. iterate f x = build (\c _n -> iterateFB c f x)
695 "iterateFB" iterateFB (:) = iterate
698 And now we get an infinite loop in the rule system
699 iterate f x -> build (\cn -> iterateFB c f x)
703 Tiresome solution: don't do shorting out if f has rewrite rules.
704 Hence shortableIdInfo.
706 (b) Keeping the IdInfo right
707 ~~~~~~~~~~~~~~~~~~~~~~~~
708 We want to move strictness/worker info from f_local to f, but keep the rest.
712 shortableIdInfo :: IdInfo -> Bool
713 shortableIdInfo info = isEmptyCoreRules (specInfo info)
715 copyIdInfo :: IdInfo -- f_local
716 -> IdInfo -- f (the exported one)
717 -> IdInfo -- New info for f
718 copyIdInfo f_local f = f { newStrictnessInfo = newStrictnessInfo f_local,
719 #ifdef OLD_STRICTNESS
720 strictnessInfo = strictnessInfo f_local,
721 cprInfo = cprInfo f_local,
723 workerInfo = workerInfo f_local