Add kind equalities to GHC.
[ghc.git] / compiler / basicTypes / IdInfo.hs
1 {-
2 (c) The University of Glasgow 2006
3 (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
4
5 \section[IdInfo]{@IdInfos@: Non-essential information about @Ids@}
6
7 (And a pretty good illustration of quite a few things wrong with
8 Haskell. [WDP 94/11])
9 -}
10
11 module IdInfo (
12 -- * The IdDetails type
13 IdDetails(..), pprIdDetails, coVarDetails, isCoVarDetails,
14 RecSelParent(..),
15
16 -- * The IdInfo type
17 IdInfo, -- Abstract
18 vanillaIdInfo, noCafIdInfo,
19
20 -- ** The OneShotInfo type
21 OneShotInfo(..),
22 oneShotInfo, noOneShotInfo, hasNoOneShotInfo,
23 setOneShotInfo,
24
25 -- ** Zapping various forms of Info
26 zapLamInfo, zapFragileInfo,
27 zapDemandInfo, zapUsageInfo,
28
29 -- ** The ArityInfo type
30 ArityInfo,
31 unknownArity,
32 arityInfo, setArityInfo, ppArityInfo,
33
34 callArityInfo, setCallArityInfo,
35
36 -- ** Demand and strictness Info
37 strictnessInfo, setStrictnessInfo,
38 demandInfo, setDemandInfo, pprStrictness,
39
40 -- ** Unfolding Info
41 unfoldingInfo, setUnfoldingInfo, setUnfoldingInfoLazily,
42
43 -- ** The InlinePragInfo type
44 InlinePragInfo,
45 inlinePragInfo, setInlinePragInfo,
46
47 -- ** The OccInfo type
48 OccInfo(..),
49 isDeadOcc, isStrongLoopBreaker, isWeakLoopBreaker,
50 occInfo, setOccInfo,
51
52 InsideLam, OneBranch,
53 insideLam, notInsideLam, oneBranch, notOneBranch,
54
55 -- ** The RuleInfo type
56 RuleInfo(..),
57 emptyRuleInfo,
58 isEmptyRuleInfo, ruleInfoFreeVars,
59 ruleInfoRules, setRuleInfoHead,
60 ruleInfo, setRuleInfo,
61
62 -- ** The CAFInfo type
63 CafInfo(..),
64 ppCafInfo, mayHaveCafRefs,
65 cafInfo, setCafInfo,
66
67 -- ** Tick-box Info
68 TickBoxOp(..), TickBoxId,
69 ) where
70
71 import CoreSyn
72
73 import Class
74 import {-# SOURCE #-} PrimOp (PrimOp)
75 import Name
76 import VarSet
77 import BasicTypes
78 import DataCon
79 import TyCon
80 import {-# SOURCE #-} PatSyn
81 import ForeignCall
82 import Outputable
83 import Module
84 import FastString
85 import Demand
86
87 -- infixl so you can say (id `set` a `set` b)
88 infixl 1 `setRuleInfo`,
89 `setArityInfo`,
90 `setInlinePragInfo`,
91 `setUnfoldingInfo`,
92 `setOneShotInfo`,
93 `setOccInfo`,
94 `setCafInfo`,
95 `setStrictnessInfo`,
96 `setDemandInfo`
97
98 {-
99 ************************************************************************
100 * *
101 IdDetails
102 * *
103 ************************************************************************
104 -}
105
106 -- | The 'IdDetails' of an 'Id' give stable, and necessary,
107 -- information about the Id.
108 data IdDetails
109 = VanillaId
110
111 -- | The 'Id' for a record selector
112 | RecSelId
113 { sel_tycon :: RecSelParent
114 , sel_naughty :: Bool -- True <=> a "naughty" selector which can't actually exist, for example @x@ in:
115 -- data T = forall a. MkT { x :: a }
116 } -- See Note [Naughty record selectors] in TcTyClsDecls
117
118 | DataConWorkId DataCon -- ^ The 'Id' is for a data constructor /worker/
119 | DataConWrapId DataCon -- ^ The 'Id' is for a data constructor /wrapper/
120
121 -- [the only reasons we need to know is so that
122 -- a) to support isImplicitId
123 -- b) when desugaring a RecordCon we can get
124 -- from the Id back to the data con]
125 | ClassOpId Class -- ^ The 'Id' is a superclass selector,
126 -- or class operation of a class
127
128 | PrimOpId PrimOp -- ^ The 'Id' is for a primitive operator
129 | FCallId ForeignCall -- ^ The 'Id' is for a foreign call
130
131 | TickBoxOpId TickBoxOp -- ^ The 'Id' is for a HPC tick box (both traditional and binary)
132
133 | DFunId Bool -- ^ A dictionary function.
134 -- Bool = True <=> the class has only one method, so may be
135 -- implemented with a newtype, so it might be bad
136 -- to be strict on this dictionary
137
138 | CoVarId -- ^ A coercion variable
139
140 -- The rest are distinguished only for debugging reasons
141 -- e.g. to suppress them in -ddump-types
142 -- Currently we don't persist these through interface file
143 -- (see MkIface.toIfaceIdDetails), but we easily could if it mattered
144
145 | ReflectionId -- ^ A top-level Id to support runtime reflection
146 -- e.g. $trModule, or $tcT
147
148 | PatSynId -- ^ A top-level Id to support pattern synonyms;
149 -- the builder or matcher for the patern synonym
150
151 data RecSelParent = RecSelData TyCon | RecSelPatSyn PatSyn deriving Eq
152 -- Either `TyCon` or `PatSyn` depending
153 -- on the origin of the record selector.
154 -- For a data type family, this is the
155 -- /instance/ 'TyCon' not the family 'TyCon'
156
157 instance Outputable RecSelParent where
158 ppr p = case p of
159 RecSelData ty_con -> ppr ty_con
160 RecSelPatSyn ps -> ppr ps
161
162 -- | Just a synonym for 'CoVarId'. Written separately so it can be
163 -- exported in the hs-boot file.
164 coVarDetails :: IdDetails
165 coVarDetails = CoVarId
166
167 -- | Check if an 'IdDetails' says 'CoVarId'.
168 isCoVarDetails :: IdDetails -> Bool
169 isCoVarDetails CoVarId = True
170 isCoVarDetails _ = False
171
172 instance Outputable IdDetails where
173 ppr = pprIdDetails
174
175 pprIdDetails :: IdDetails -> SDoc
176 pprIdDetails VanillaId = empty
177 pprIdDetails other = brackets (pp other)
178 where
179 pp VanillaId = panic "pprIdDetails"
180 pp ReflectionId = ptext (sLit "ReflectionId")
181 pp PatSynId = ptext (sLit "PatSynId")
182 pp (DataConWorkId _) = ptext (sLit "DataCon")
183 pp (DataConWrapId _) = ptext (sLit "DataConWrapper")
184 pp (ClassOpId {}) = ptext (sLit "ClassOp")
185 pp (PrimOpId _) = ptext (sLit "PrimOp")
186 pp (FCallId _) = ptext (sLit "ForeignCall")
187 pp (TickBoxOpId _) = ptext (sLit "TickBoxOp")
188 pp (DFunId nt) = ptext (sLit "DFunId") <> ppWhen nt (ptext (sLit "(nt)"))
189 pp (RecSelId { sel_naughty = is_naughty })
190 = brackets $ ptext (sLit "RecSel")
191 <> ppWhen is_naughty (ptext (sLit "(naughty)"))
192 pp CoVarId = ptext (sLit "CoVarId")
193
194 {-
195 ************************************************************************
196 * *
197 \subsection{The main IdInfo type}
198 * *
199 ************************************************************************
200 -}
201
202 -- | An 'IdInfo' gives /optional/ information about an 'Id'. If
203 -- present it never lies, but it may not be present, in which case there
204 -- is always a conservative assumption which can be made.
205 --
206 -- Two 'Id's may have different info even though they have the same
207 -- 'Unique' (and are hence the same 'Id'); for example, one might lack
208 -- the properties attached to the other.
209 --
210 -- Most of the 'IdInfo' gives information about the value, or definition, of
211 -- the 'Id', independent of its usage. Exceptions to this
212 -- are 'demandInfo', 'occInfo', 'oneShotInfo' and 'callArityInfo'.
213 data IdInfo
214 = IdInfo {
215 arityInfo :: !ArityInfo, -- ^ 'Id' arity
216 ruleInfo :: RuleInfo, -- ^ Specialisations of the 'Id's function which exist
217 -- See Note [Specialisations and RULES in IdInfo]
218 unfoldingInfo :: Unfolding, -- ^ The 'Id's unfolding
219 cafInfo :: CafInfo, -- ^ 'Id' CAF info
220 oneShotInfo :: OneShotInfo, -- ^ Info about a lambda-bound variable, if the 'Id' is one
221 inlinePragInfo :: InlinePragma, -- ^ Any inline pragma atached to the 'Id'
222 occInfo :: OccInfo, -- ^ How the 'Id' occurs in the program
223
224 strictnessInfo :: StrictSig, -- ^ A strictness signature
225
226 demandInfo :: Demand, -- ^ ID demand information
227 callArityInfo :: !ArityInfo -- ^ How this is called.
228 -- n <=> all calls have at least n arguments
229 }
230
231 -- Setters
232
233 setRuleInfo :: IdInfo -> RuleInfo -> IdInfo
234 setRuleInfo info sp = sp `seq` info { ruleInfo = sp }
235 setInlinePragInfo :: IdInfo -> InlinePragma -> IdInfo
236 setInlinePragInfo info pr = pr `seq` info { inlinePragInfo = pr }
237 setOccInfo :: IdInfo -> OccInfo -> IdInfo
238 setOccInfo info oc = oc `seq` info { occInfo = oc }
239 -- Try to avoid spack leaks by seq'ing
240
241 setUnfoldingInfoLazily :: IdInfo -> Unfolding -> IdInfo
242 setUnfoldingInfoLazily info uf -- Lazy variant to avoid looking at the
243 = -- unfolding of an imported Id unless necessary
244 info { unfoldingInfo = uf } -- (In this case the demand-zapping is redundant.)
245
246 setUnfoldingInfo :: IdInfo -> Unfolding -> IdInfo
247 setUnfoldingInfo info uf
248 = -- We don't seq the unfolding, as we generate intermediate
249 -- unfoldings which are just thrown away, so evaluating them is a
250 -- waste of time.
251 -- seqUnfolding uf `seq`
252 info { unfoldingInfo = uf }
253
254 setArityInfo :: IdInfo -> ArityInfo -> IdInfo
255 setArityInfo info ar = info { arityInfo = ar }
256 setCallArityInfo :: IdInfo -> ArityInfo -> IdInfo
257 setCallArityInfo info ar = info { callArityInfo = ar }
258 setCafInfo :: IdInfo -> CafInfo -> IdInfo
259 setCafInfo info caf = info { cafInfo = caf }
260
261 setOneShotInfo :: IdInfo -> OneShotInfo -> IdInfo
262 setOneShotInfo info lb = {-lb `seq`-} info { oneShotInfo = lb }
263
264 setDemandInfo :: IdInfo -> Demand -> IdInfo
265 setDemandInfo info dd = dd `seq` info { demandInfo = dd }
266
267 setStrictnessInfo :: IdInfo -> StrictSig -> IdInfo
268 setStrictnessInfo info dd = dd `seq` info { strictnessInfo = dd }
269
270 -- | Basic 'IdInfo' that carries no useful information whatsoever
271 vanillaIdInfo :: IdInfo
272 vanillaIdInfo
273 = IdInfo {
274 cafInfo = vanillaCafInfo,
275 arityInfo = unknownArity,
276 ruleInfo = emptyRuleInfo,
277 unfoldingInfo = noUnfolding,
278 oneShotInfo = NoOneShotInfo,
279 inlinePragInfo = defaultInlinePragma,
280 occInfo = NoOccInfo,
281 demandInfo = topDmd,
282 strictnessInfo = nopSig,
283 callArityInfo = unknownArity
284 }
285
286 -- | More informative 'IdInfo' we can use when we know the 'Id' has no CAF references
287 noCafIdInfo :: IdInfo
288 noCafIdInfo = vanillaIdInfo `setCafInfo` NoCafRefs
289 -- Used for built-in type Ids in MkId.
290
291 {-
292 ************************************************************************
293 * *
294 \subsection[arity-IdInfo]{Arity info about an @Id@}
295 * *
296 ************************************************************************
297
298 For locally-defined Ids, the code generator maintains its own notion
299 of their arities; so it should not be asking... (but other things
300 besides the code-generator need arity info!)
301 -}
302
303 -- | An 'ArityInfo' of @n@ tells us that partial application of this
304 -- 'Id' to up to @n-1@ value arguments does essentially no work.
305 --
306 -- That is not necessarily the same as saying that it has @n@ leading
307 -- lambdas, because coerces may get in the way.
308 --
309 -- The arity might increase later in the compilation process, if
310 -- an extra lambda floats up to the binding site.
311 type ArityInfo = Arity
312
313 -- | It is always safe to assume that an 'Id' has an arity of 0
314 unknownArity :: Arity
315 unknownArity = 0 :: Arity
316
317 ppArityInfo :: Int -> SDoc
318 ppArityInfo 0 = empty
319 ppArityInfo n = hsep [ptext (sLit "Arity"), int n]
320
321 {-
322 ************************************************************************
323 * *
324 \subsection{Inline-pragma information}
325 * *
326 ************************************************************************
327 -}
328
329 -- | Tells when the inlining is active.
330 -- When it is active the thing may be inlined, depending on how
331 -- big it is.
332 --
333 -- If there was an @INLINE@ pragma, then as a separate matter, the
334 -- RHS will have been made to look small with a Core inline 'Note'
335 --
336 -- The default 'InlinePragInfo' is 'AlwaysActive', so the info serves
337 -- entirely as a way to inhibit inlining until we want it
338 type InlinePragInfo = InlinePragma
339
340 {-
341 ************************************************************************
342 * *
343 Strictness
344 * *
345 ************************************************************************
346 -}
347
348 pprStrictness :: StrictSig -> SDoc
349 pprStrictness sig = ppr sig
350
351 {-
352 ************************************************************************
353 * *
354 RuleInfo
355 * *
356 ************************************************************************
357
358 Note [Specialisations and RULES in IdInfo]
359 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
360 Generally speaking, a GlobalId has an *empty* RuleInfo. All their
361 RULES are contained in the globally-built rule-base. In principle,
362 one could attach the to M.f the RULES for M.f that are defined in M.
363 But we don't do that for instance declarations and so we just treat
364 them all uniformly.
365
366 The EXCEPTION is PrimOpIds, which do have rules in their IdInfo. That is
367 jsut for convenience really.
368
369 However, LocalIds may have non-empty RuleInfo. We treat them
370 differently because:
371 a) they might be nested, in which case a global table won't work
372 b) the RULE might mention free variables, which we use to keep things alive
373
374 In TidyPgm, when the LocalId becomes a GlobalId, its RULES are stripped off
375 and put in the global list.
376 -}
377
378 -- | Records the specializations of this 'Id' that we know about
379 -- in the form of rewrite 'CoreRule's that target them
380 data RuleInfo
381 = RuleInfo
382 [CoreRule]
383 DVarSet -- Locally-defined free vars of *both* LHS and RHS
384 -- of rules. I don't think it needs to include the
385 -- ru_fn though.
386 -- Note [Rule dependency info] in OccurAnal
387
388 -- | Assume that no specilizations exist: always safe
389 emptyRuleInfo :: RuleInfo
390 emptyRuleInfo = RuleInfo [] emptyDVarSet
391
392 isEmptyRuleInfo :: RuleInfo -> Bool
393 isEmptyRuleInfo (RuleInfo rs _) = null rs
394
395 -- | Retrieve the locally-defined free variables of both the left and
396 -- right hand sides of the specialization rules
397 ruleInfoFreeVars :: RuleInfo -> DVarSet
398 ruleInfoFreeVars (RuleInfo _ fvs) = fvs
399
400 ruleInfoRules :: RuleInfo -> [CoreRule]
401 ruleInfoRules (RuleInfo rules _) = rules
402
403 -- | Change the name of the function the rule is keyed on on all of the 'CoreRule's
404 setRuleInfoHead :: Name -> RuleInfo -> RuleInfo
405 setRuleInfoHead fn (RuleInfo rules fvs)
406 = RuleInfo (map (setRuleIdName fn) rules) fvs
407
408 {-
409 ************************************************************************
410 * *
411 \subsection[CG-IdInfo]{Code generator-related information}
412 * *
413 ************************************************************************
414 -}
415
416 -- CafInfo is used to build Static Reference Tables (see simplStg/SRT.hs).
417
418 -- | Records whether an 'Id' makes Constant Applicative Form references
419 data CafInfo
420 = MayHaveCafRefs -- ^ Indicates that the 'Id' is for either:
421 --
422 -- 1. A function or static constructor
423 -- that refers to one or more CAFs, or
424 --
425 -- 2. A real live CAF
426
427 | NoCafRefs -- ^ A function or static constructor
428 -- that refers to no CAFs.
429 deriving (Eq, Ord)
430
431 -- | Assumes that the 'Id' has CAF references: definitely safe
432 vanillaCafInfo :: CafInfo
433 vanillaCafInfo = MayHaveCafRefs
434
435 mayHaveCafRefs :: CafInfo -> Bool
436 mayHaveCafRefs MayHaveCafRefs = True
437 mayHaveCafRefs _ = False
438
439 instance Outputable CafInfo where
440 ppr = ppCafInfo
441
442 ppCafInfo :: CafInfo -> SDoc
443 ppCafInfo NoCafRefs = ptext (sLit "NoCafRefs")
444 ppCafInfo MayHaveCafRefs = empty
445
446 {-
447 ************************************************************************
448 * *
449 \subsection{Bulk operations on IdInfo}
450 * *
451 ************************************************************************
452 -}
453
454 -- | This is used to remove information on lambda binders that we have
455 -- setup as part of a lambda group, assuming they will be applied all at once,
456 -- but turn out to be part of an unsaturated lambda as in e.g:
457 --
458 -- > (\x1. \x2. e) arg1
459 zapLamInfo :: IdInfo -> Maybe IdInfo
460 zapLamInfo info@(IdInfo {occInfo = occ, demandInfo = demand})
461 | is_safe_occ occ && is_safe_dmd demand
462 = Nothing
463 | otherwise
464 = Just (info {occInfo = safe_occ, demandInfo = topDmd})
465 where
466 -- The "unsafe" occ info is the ones that say I'm not in a lambda
467 -- because that might not be true for an unsaturated lambda
468 is_safe_occ (OneOcc in_lam _ _) = in_lam
469 is_safe_occ _other = True
470
471 safe_occ = case occ of
472 OneOcc _ once int_cxt -> OneOcc insideLam once int_cxt
473 _other -> occ
474
475 is_safe_dmd dmd = not (isStrictDmd dmd)
476
477 -- | Remove all demand info on the 'IdInfo'
478 zapDemandInfo :: IdInfo -> Maybe IdInfo
479 zapDemandInfo info = Just (info {demandInfo = topDmd})
480
481 -- | Remove usage (but not strictness) info on the 'IdInfo'
482 zapUsageInfo :: IdInfo -> Maybe IdInfo
483 zapUsageInfo info = Just (info {demandInfo = zapUsageDemand (demandInfo info)})
484
485 zapFragileInfo :: IdInfo -> Maybe IdInfo
486 -- ^ Zap info that depends on free variables
487 zapFragileInfo info
488 = Just (info `setRuleInfo` emptyRuleInfo
489 `setUnfoldingInfo` noUnfolding
490 `setOccInfo` zapFragileOcc occ)
491 where
492 occ = occInfo info
493
494 {-
495 ************************************************************************
496 * *
497 \subsection{TickBoxOp}
498 * *
499 ************************************************************************
500 -}
501
502 type TickBoxId = Int
503
504 -- | Tick box for Hpc-style coverage
505 data TickBoxOp
506 = TickBox Module {-# UNPACK #-} !TickBoxId
507
508 instance Outputable TickBoxOp where
509 ppr (TickBox mod n) = ptext (sLit "tick") <+> ppr (mod,n)