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
17 exactArity, atLeastArity, unknownArity,
18 arityInfo, setArityInfo, ppArityInfo, arityLowerBound,
21 StrictnessInfo(..), -- Non-abstract
23 noStrictnessInfo, strictnessInfo,
24 ppStrictnessInfo, setStrictnessInfo,
25 isBottomingStrictness, appIsBottom,
28 WorkerInfo, workerExists,
29 workerInfo, setWorkerInfo,
32 unfoldingInfo, setUnfoldingInfo,
35 demandInfo, setDemandInfo,
38 InlinePragInfo(..), OccInfo(..),
39 inlinePragInfo, setInlinePragInfo, notInsideLambda,
42 IdSpecEnv, specInfo, setSpecInfo,
45 UpdateInfo, UpdateSpec,
46 mkUpdateInfo, updateInfo, updateInfoMaybe, ppUpdateInfo, setUpdateInfo,
49 CafInfo(..), cafInfo, setCafInfo, ppCafInfo,
51 -- Constructed Product Result Info
52 CprInfo(..), cprInfo, setCprInfo, ppCprInfo, noCprInfo,
54 -- Lambda-bound variable info
55 LBVarInfo(..), lbvarInfo, setLBVarInfo, noLBVarInfo,
58 #include "HsVersions.h"
61 import {-# SOURCE #-} CoreUnfold ( Unfolding, noUnfolding )
62 import {-# SOURCE #-} CoreSyn ( CoreExpr )
65 import SpecEnv ( SpecEnv, emptySpecEnv )
66 import Demand ( Demand, isLazy, wwLazy, pprDemands )
67 import Type ( UsageAnn )
70 import Maybe ( isJust )
74 An @IdInfo@ gives {\em optional} information about an @Id@. If
75 present it never lies, but it may not be present, in which case there
76 is always a conservative assumption which can be made.
78 Two @Id@s may have different info even though they have the same
79 @Unique@ (and are hence the same @Id@); for example, one might lack
80 the properties attached to the other.
82 The @IdInfo@ gives information about the value, or definition, of the
83 @Id@. It does {\em not} contain information about the @Id@'s usage
84 (except for @DemandInfo@? ToDo). (@lbvarInfo@ is also a marginal
90 arityInfo :: ArityInfo, -- Its arity
91 demandInfo :: Demand, -- Whether or not it is definitely demanded
92 specInfo :: IdSpecEnv, -- Specialisations of this function which exist
93 strictnessInfo :: StrictnessInfo, -- Strictness properties
94 workerInfo :: WorkerInfo, -- Pointer to Worker Function
95 unfoldingInfo :: Unfolding, -- Its unfolding
96 updateInfo :: UpdateInfo, -- Which args should be updated
98 cprInfo :: CprInfo, -- Function always constructs a product result
99 lbvarInfo :: LBVarInfo, -- Info about a lambda-bound variable
100 inlinePragInfo :: !InlinePragInfo -- Inline pragmas
107 setUpdateInfo ud info = info { updateInfo = ud }
108 setDemandInfo dd info = info { demandInfo = dd }
109 setStrictnessInfo st info = info { strictnessInfo = st }
110 setWorkerInfo wk info = info { workerInfo = wk }
111 setSpecInfo sp info = info { specInfo = sp }
112 setArityInfo ar info = info { arityInfo = ar }
113 setInlinePragInfo pr info = info { inlinePragInfo = pr }
114 setUnfoldingInfo uf info = info { unfoldingInfo = uf }
115 setCafInfo cf info = info { cafInfo = cf }
116 setCprInfo cp info = info { cprInfo = cp }
117 setLBVarInfo lb info = info { lbvarInfo = lb }
123 arityInfo = UnknownArity,
125 specInfo = emptySpecEnv,
126 strictnessInfo = NoStrictnessInfo,
127 workerInfo = noWorkerInfo,
128 unfoldingInfo = noUnfolding,
129 updateInfo = NoUpdateInfo,
130 cafInfo = MayHaveCafRefs,
132 lbvarInfo = NoLBVarInfo,
133 inlinePragInfo = NoInlinePragInfo
137 %************************************************************************
139 \subsection[arity-IdInfo]{Arity info about an @Id@}
141 %************************************************************************
143 For locally-defined Ids, the code generator maintains its own notion
144 of their arities; so it should not be asking... (but other things
145 besides the code-generator need arity info!)
149 = UnknownArity -- No idea
150 | ArityExactly Int -- Arity is exactly this
151 | ArityAtLeast Int -- Arity is this or greater
153 exactArity = ArityExactly
154 atLeastArity = ArityAtLeast
155 unknownArity = UnknownArity
157 arityLowerBound :: ArityInfo -> Int
158 arityLowerBound UnknownArity = 0
159 arityLowerBound (ArityAtLeast n) = n
160 arityLowerBound (ArityExactly n) = n
163 ppArityInfo UnknownArity = empty
164 ppArityInfo (ArityExactly arity) = hsep [ptext SLIT("__A"), int arity]
165 ppArityInfo (ArityAtLeast arity) = hsep [ptext SLIT("__AL"), int arity]
168 %************************************************************************
170 \subsection{Inline-pragma information}
172 %************************************************************************
178 | IAmASpecPragmaId -- Used for spec-pragma Ids; don't discard or inline
180 | IWantToBeINLINEd -- User INLINE pragma
181 | IMustNotBeINLINEd -- User NOINLINE pragma
183 | IAmALoopBreaker -- Used by the occurrence analyser to mark loop-breakers
184 -- in a group of recursive definitions
186 | ICanSafelyBeINLINEd -- Used by the occurrence analyser to mark things
187 -- that manifesly occur once, not inside SCCs,
188 -- not in constructor arguments
190 OccInfo -- Says whether the occurrence is inside a lambda
191 -- If so, must only substitute WHNFs
193 Bool -- False <=> occurs in more than one case branch
194 -- If so, there's a code-duplication issue
196 | IAmDead -- Marks unused variables. Sometimes useful for
197 -- lambda and case-bound variables.
199 | IMustBeINLINEd -- Absolutely must inline; used for PrimOps and
200 -- constructors only.
202 instance Outputable InlinePragInfo where
203 ppr NoInlinePragInfo = empty
204 ppr IMustBeINLINEd = ptext SLIT("__UU")
205 ppr IWantToBeINLINEd = ptext SLIT("__U")
206 ppr IMustNotBeINLINEd = ptext SLIT("__Unot")
207 ppr IAmALoopBreaker = ptext SLIT("__Ux")
208 ppr IAmDead = ptext SLIT("__Ud")
209 ppr (ICanSafelyBeINLINEd InsideLam _) = ptext SLIT("__Ul")
210 ppr (ICanSafelyBeINLINEd _ _) = ptext SLIT("__Us")
211 ppr IAmASpecPragmaId = ptext SLIT("__US")
213 instance Show InlinePragInfo where
214 showsPrec p prag = showsPrecSDoc p (ppr prag)
217 The @IMustNotBeDiscarded@ exists only to make Ids that are
218 on the *LHS* of bindings created by SPECIALISE pragmas;
220 The SpecPragmaId is never itself mentioned; it
221 exists solely so that the specialiser will find
222 the call to f, and make specialised version of it.
223 The SpecPragmaId binding is discarded by the specialiser
224 when it gathers up overloaded calls.
225 Meanwhile, it is not discarded as dead code.
229 = StrictOcc -- Occurs syntactically strictly;
230 -- i.e. in a function position or case scrutinee
232 | LazyOcc -- Not syntactically strict (*even* that of a strict function)
233 -- or in a case branch where there's more than one alternative
235 | InsideLam -- Inside a non-linear lambda (that is, a lambda which
236 -- is sure to be instantiated only once).
237 -- Substituting a redex for this occurrence is
238 -- dangerous because it might duplicate work.
240 instance Outputable OccInfo where
241 ppr StrictOcc = text "s"
243 ppr InsideLam = text "l"
246 notInsideLambda :: OccInfo -> Bool
247 notInsideLambda StrictOcc = True
248 notInsideLambda LazyOcc = True
249 notInsideLambda InsideLam = False
252 %************************************************************************
254 \subsection[specialisation-IdInfo]{Specialisation info about an @Id@}
256 %************************************************************************
258 A @IdSpecEnv@ holds details of an @Id@'s specialisations.
261 type IdSpecEnv = SpecEnv CoreExpr
264 For example, if \tr{f}'s @SpecEnv@ contains the mapping:
266 [List a, b] ===> (\d -> f' a b)
268 then when we find an application of f to matching types, we simply replace
269 it by the matching RHS:
271 f (List Int) Bool ===> (\d -> f' Int Bool)
273 All the stuff about how many dictionaries to discard, and what types
274 to apply the specialised function to, are handled by the fact that the
275 SpecEnv contains a template for the result of the specialisation.
277 There is one more exciting case, which is dealt with in exactly the same
278 way. If the specialised value is unboxed then it is lifted at its
279 definition site and unlifted at its uses. For example:
281 pi :: forall a. Num a => a
283 might have a specialisation
285 [Int#] ===> (case pi' of Lift pi# -> pi#)
287 where pi' :: Lift Int# is the specialised version of pi.
290 %************************************************************************
292 \subsection[strictness-IdInfo]{Strictness info about an @Id@}
294 %************************************************************************
296 We specify the strictness of a function by giving information about
297 each of the ``wrapper's'' arguments (see the description about
298 worker/wrapper-style transformations in the PJ/Launchbury paper on
301 The list of @Demands@ specifies: (a)~the strictness properties of a
302 function's arguments; and (b)~the type signature of that worker (if it
303 exists); i.e. its calling convention.
305 Note that the existence of a worker function is now denoted by the Id's
312 | StrictnessInfo [Demand]
313 Bool -- True <=> the function diverges regardless of its arguments
314 -- Useful for "error" and other disguised variants thereof.
315 -- BUT NB: f = \x y. error "urk"
316 -- will have info SI [SS] True
317 -- but still (f) and (f 2) are not bot; only (f 3 2) is bot
321 mkStrictnessInfo :: ([Demand], Bool) -> StrictnessInfo
323 mkStrictnessInfo (xs, is_bot)
324 | all isLazy xs && not is_bot = NoStrictnessInfo -- Uninteresting
325 | otherwise = StrictnessInfo xs is_bot
327 noStrictnessInfo = NoStrictnessInfo
329 isBottomingStrictness (StrictnessInfo _ bot) = bot
330 isBottomingStrictness NoStrictnessInfo = False
332 -- appIsBottom returns true if an application to n args would diverge
333 appIsBottom (StrictnessInfo ds bot) n = bot && (n >= length ds)
334 appIsBottom NoStrictnessInfo n = False
336 ppStrictnessInfo NoStrictnessInfo = empty
337 ppStrictnessInfo (StrictnessInfo wrapper_args bot)
338 = hsep [ptext SLIT("__S"), pprDemands wrapper_args bot]
341 %************************************************************************
343 \subsection[worker-IdInfo]{Worker info about an @Id@}
345 %************************************************************************
347 If this Id has a worker then we store a reference to it. Worker
348 functions are generated by the worker/wrapper pass. This uses
349 information from the strictness and CPR analyses.
351 There might not be a worker, even for a strict function, because:
352 (a) the function might be small enough to inline, so no need
354 (b) the strictness info might be "SSS" or something, so no w/w split.
358 type WorkerInfo = Maybe Id
361 mkWorkerInfo :: Id -> WorkerInfo
362 mkWorkerInfo wk_id = Just wk_id
364 ppWorkerInfo Nothing = empty
365 ppWorkerInfo (Just wk_id) = ppr wk_id
368 noWorkerInfo = Nothing
370 workerExists :: Maybe Id -> Bool
371 workerExists = isJust
375 %************************************************************************
377 \subsection[update-IdInfo]{Update-analysis info about an @Id@}
379 %************************************************************************
384 | SomeUpdateInfo UpdateSpec
386 -- we need Eq/Ord to cross-chk update infos in interfaces
388 -- the form in which we pass update-analysis info between modules:
389 type UpdateSpec = [Int]
393 mkUpdateInfo = SomeUpdateInfo
395 updateInfoMaybe NoUpdateInfo = Nothing
396 updateInfoMaybe (SomeUpdateInfo []) = Nothing
397 updateInfoMaybe (SomeUpdateInfo u) = Just u
400 Text instance so that the update annotations can be read in.
403 ppUpdateInfo NoUpdateInfo = empty
404 ppUpdateInfo (SomeUpdateInfo []) = empty
405 ppUpdateInfo (SomeUpdateInfo spec) = (<>) (ptext SLIT("__U ")) (hcat (map int spec))
408 %************************************************************************
410 \subsection[CAF-IdInfo]{CAF-related information}
412 %************************************************************************
414 This information is used to build Static Reference Tables (see
415 simplStg/ComputeSRT.lhs).
419 = MayHaveCafRefs -- either:
420 -- (1) A function or static constructor
421 -- that refers to one or more CAFs,
422 -- (2) A real live CAF
424 | NoCafRefs -- A function or static constructor
425 -- that refers to no CAFs.
427 -- LATER: not sure how easy this is...
431 ppCafInfo NoCafRefs = ptext SLIT("__C")
432 ppCafInfo MayHaveCafRefs = empty
435 %************************************************************************
437 \subsection[cpr-IdInfo]{Constructed Product Result info about an @Id@}
439 %************************************************************************
441 If the @Id@ is a function then it may have CPR info. A CPR analysis
442 phase detects whether:
446 The function's return value has a product type, i.e. an algebraic type
447 with a single constructor. Examples of such types are tuples and boxed
450 The function always 'constructs' the value that it is returning. It
451 must do this on every path through, and it's OK if it calls another
452 function which constructs the result.
455 If this is the case then we store a template which tells us the
456 function has the CPR property and which components of the result are
465 -- e.g. const 5 == CPRInfo [NoCPRInfo]
472 -- CPRInfo [CPRInfo [NoCPRInfo],
473 -- CPRInfo [NoCprInfo,
474 -- CPRInfo [NoCPRInfo],
482 noCprInfo = NoCPRInfo
484 ppCprInfo NoCPRInfo = empty
485 ppCprInfo c@(CPRInfo _)
486 = hsep [ptext SLIT("__M"), ppCprInfo' c]
488 ppCprInfo' NoCPRInfo = char '-'
489 ppCprInfo' (CPRInfo args) = parens (hcat (map ppCprInfo' args))
491 instance Outputable CprInfo where
494 instance Show CprInfo where
495 showsPrec p c = showsPrecSDoc p (ppr c)
499 %************************************************************************
501 \subsection[lbvar-IdInfo]{Lambda-bound var info about an @Id@}
503 %************************************************************************
505 If the @Id@ is a lambda-bound variable then it may have lambda-bound
506 var info. The usage analysis (UsageSP) detects whether the lambda
507 binding this var is a ``one-shot'' lambda; that is, whether it is
508 applied at most once.
510 This information may be useful in optimisation, as computations may
511 safely be floated inside such a lambda without risk of duplicating
518 | IsOneShotLambda -- the lambda that binds this Id is applied
520 -- HACK ALERT! placing this info here is a short-term hack,
521 -- but it minimises changes to the rest of the compiler.
522 -- Hack agreed by SLPJ/KSW 1999-04.
527 noLBVarInfo = NoLBVarInfo
529 -- not safe to print or parse LBVarInfo because it is not really a
530 -- property of the definition, but a property of the context.
531 ppLBVarInfo _ = empty
533 instance Outputable LBVarInfo where
536 instance Show LBVarInfo where
537 showsPrec p c = showsPrecSDoc p (ppr c)