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