Implement unboxed sum primitive type
[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, zapUsageEnvInfo, zapUsedOnceInfo,
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 PatSyn
81 import ForeignCall
82 import Outputable
83 import Module
84 import Demand
85
86 -- infixl so you can say (id `set` a `set` b)
87 infixl 1 `setRuleInfo`,
88 `setArityInfo`,
89 `setInlinePragInfo`,
90 `setUnfoldingInfo`,
91 `setOneShotInfo`,
92 `setOccInfo`,
93 `setCafInfo`,
94 `setStrictnessInfo`,
95 `setDemandInfo`
96
97 {-
98 ************************************************************************
99 * *
100 IdDetails
101 * *
102 ************************************************************************
103 -}
104
105 -- | The 'IdDetails' of an 'Id' give stable, and necessary,
106 -- information about the Id.
107 data IdDetails
108 = VanillaId
109
110 -- | The 'Id' for a record selector
111 | RecSelId
112 { sel_tycon :: RecSelParent
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 | ClassOpId Class -- ^ The 'Id' is a superclass selector,
125 -- 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 | CoVarId -- ^ A coercion variable
138 -- This only covers /un-lifted/ coercions, of type
139 -- (t1 ~# t2) or (t1 ~R# t2), not their lifted variants
140
141 data RecSelParent = RecSelData TyCon | RecSelPatSyn PatSyn deriving Eq
142 -- Either `TyCon` or `PatSyn` depending
143 -- on the origin of the record selector.
144 -- For a data type family, this is the
145 -- /instance/ 'TyCon' not the family 'TyCon'
146
147 instance Outputable RecSelParent where
148 ppr p = case p of
149 RecSelData ty_con -> ppr ty_con
150 RecSelPatSyn ps -> ppr ps
151
152 -- | Just a synonym for 'CoVarId'. Written separately so it can be
153 -- exported in the hs-boot file.
154 coVarDetails :: IdDetails
155 coVarDetails = CoVarId
156
157 -- | Check if an 'IdDetails' says 'CoVarId'.
158 isCoVarDetails :: IdDetails -> Bool
159 isCoVarDetails CoVarId = True
160 isCoVarDetails _ = False
161
162 instance Outputable IdDetails where
163 ppr = pprIdDetails
164
165 pprIdDetails :: IdDetails -> SDoc
166 pprIdDetails VanillaId = empty
167 pprIdDetails other = brackets (pp other)
168 where
169 pp VanillaId = panic "pprIdDetails"
170 pp (DataConWorkId _) = text "DataCon"
171 pp (DataConWrapId _) = text "DataConWrapper"
172 pp (ClassOpId {}) = text "ClassOp"
173 pp (PrimOpId _) = text "PrimOp"
174 pp (FCallId _) = text "ForeignCall"
175 pp (TickBoxOpId _) = text "TickBoxOp"
176 pp (DFunId nt) = text "DFunId" <> ppWhen nt (text "(nt)")
177 pp (RecSelId { sel_naughty = is_naughty })
178 = brackets $ text "RecSel"
179 <> ppWhen is_naughty (text "(naughty)")
180 pp CoVarId = text "CoVarId"
181
182 {-
183 ************************************************************************
184 * *
185 \subsection{The main IdInfo type}
186 * *
187 ************************************************************************
188 -}
189
190 -- | An 'IdInfo' gives /optional/ information about an 'Id'. If
191 -- present it never lies, but it may not be present, in which case there
192 -- is always a conservative assumption which can be made.
193 --
194 -- Two 'Id's may have different info even though they have the same
195 -- 'Unique' (and are hence the same 'Id'); for example, one might lack
196 -- the properties attached to the other.
197 --
198 -- Most of the 'IdInfo' gives information about the value, or definition, of
199 -- the 'Id', independent of its usage. Exceptions to this
200 -- are 'demandInfo', 'occInfo', 'oneShotInfo' and 'callArityInfo'.
201 --
202 -- Performance note: when we update 'IdInfo', we have to reallocate this
203 -- entire record, so it is a good idea not to let this data structure get
204 -- too big.
205 data IdInfo
206 = IdInfo {
207 arityInfo :: !ArityInfo, -- ^ 'Id' arity
208 ruleInfo :: RuleInfo, -- ^ Specialisations of the 'Id's function which exist
209 -- See Note [Specialisations and RULES in IdInfo]
210 unfoldingInfo :: Unfolding, -- ^ The 'Id's unfolding
211 cafInfo :: CafInfo, -- ^ 'Id' CAF info
212 oneShotInfo :: OneShotInfo, -- ^ Info about a lambda-bound variable, if the 'Id' is one
213 inlinePragInfo :: InlinePragma, -- ^ Any inline pragma atached to the 'Id'
214 occInfo :: OccInfo, -- ^ How the 'Id' occurs in the program
215
216 strictnessInfo :: StrictSig, -- ^ A strictness signature
217
218 demandInfo :: Demand, -- ^ ID demand information
219 callArityInfo :: !ArityInfo -- ^ How this is called.
220 -- n <=> all calls have at least n arguments
221 }
222
223 -- Setters
224
225 setRuleInfo :: IdInfo -> RuleInfo -> IdInfo
226 setRuleInfo info sp = sp `seq` info { ruleInfo = sp }
227 setInlinePragInfo :: IdInfo -> InlinePragma -> IdInfo
228 setInlinePragInfo info pr = pr `seq` info { inlinePragInfo = pr }
229 setOccInfo :: IdInfo -> OccInfo -> IdInfo
230 setOccInfo info oc = oc `seq` info { occInfo = oc }
231 -- Try to avoid spack leaks by seq'ing
232
233 setUnfoldingInfoLazily :: IdInfo -> Unfolding -> IdInfo
234 setUnfoldingInfoLazily info uf -- Lazy variant to avoid looking at the
235 = -- unfolding of an imported Id unless necessary
236 info { unfoldingInfo = uf } -- (In this case the demand-zapping is redundant.)
237
238 setUnfoldingInfo :: IdInfo -> Unfolding -> IdInfo
239 setUnfoldingInfo info uf
240 = -- We don't seq the unfolding, as we generate intermediate
241 -- unfoldings which are just thrown away, so evaluating them is a
242 -- waste of time.
243 -- seqUnfolding uf `seq`
244 info { unfoldingInfo = uf }
245
246 setArityInfo :: IdInfo -> ArityInfo -> IdInfo
247 setArityInfo info ar = info { arityInfo = ar }
248 setCallArityInfo :: IdInfo -> ArityInfo -> IdInfo
249 setCallArityInfo info ar = info { callArityInfo = ar }
250 setCafInfo :: IdInfo -> CafInfo -> IdInfo
251 setCafInfo info caf = info { cafInfo = caf }
252
253 setOneShotInfo :: IdInfo -> OneShotInfo -> IdInfo
254 setOneShotInfo info lb = {-lb `seq`-} info { oneShotInfo = lb }
255
256 setDemandInfo :: IdInfo -> Demand -> IdInfo
257 setDemandInfo info dd = dd `seq` info { demandInfo = dd }
258
259 setStrictnessInfo :: IdInfo -> StrictSig -> IdInfo
260 setStrictnessInfo info dd = dd `seq` info { strictnessInfo = dd }
261
262 -- | Basic 'IdInfo' that carries no useful information whatsoever
263 vanillaIdInfo :: IdInfo
264 vanillaIdInfo
265 = IdInfo {
266 cafInfo = vanillaCafInfo,
267 arityInfo = unknownArity,
268 ruleInfo = emptyRuleInfo,
269 unfoldingInfo = noUnfolding,
270 oneShotInfo = NoOneShotInfo,
271 inlinePragInfo = defaultInlinePragma,
272 occInfo = NoOccInfo,
273 demandInfo = topDmd,
274 strictnessInfo = nopSig,
275 callArityInfo = unknownArity
276 }
277
278 -- | More informative 'IdInfo' we can use when we know the 'Id' has no CAF references
279 noCafIdInfo :: IdInfo
280 noCafIdInfo = vanillaIdInfo `setCafInfo` NoCafRefs
281 -- Used for built-in type Ids in MkId.
282
283 {-
284 ************************************************************************
285 * *
286 \subsection[arity-IdInfo]{Arity info about an @Id@}
287 * *
288 ************************************************************************
289
290 For locally-defined Ids, the code generator maintains its own notion
291 of their arities; so it should not be asking... (but other things
292 besides the code-generator need arity info!)
293 -}
294
295 -- | An 'ArityInfo' of @n@ tells us that partial application of this
296 -- 'Id' to up to @n-1@ value arguments does essentially no work.
297 --
298 -- That is not necessarily the same as saying that it has @n@ leading
299 -- lambdas, because coerces may get in the way.
300 --
301 -- The arity might increase later in the compilation process, if
302 -- an extra lambda floats up to the binding site.
303 type ArityInfo = Arity
304
305 -- | It is always safe to assume that an 'Id' has an arity of 0
306 unknownArity :: Arity
307 unknownArity = 0
308
309 ppArityInfo :: Int -> SDoc
310 ppArityInfo 0 = empty
311 ppArityInfo n = hsep [text "Arity", int n]
312
313 {-
314 ************************************************************************
315 * *
316 \subsection{Inline-pragma information}
317 * *
318 ************************************************************************
319 -}
320
321 -- | Tells when the inlining is active.
322 -- When it is active the thing may be inlined, depending on how
323 -- big it is.
324 --
325 -- If there was an @INLINE@ pragma, then as a separate matter, the
326 -- RHS will have been made to look small with a Core inline 'Note'
327 --
328 -- The default 'InlinePragInfo' is 'AlwaysActive', so the info serves
329 -- entirely as a way to inhibit inlining until we want it
330 type InlinePragInfo = InlinePragma
331
332 {-
333 ************************************************************************
334 * *
335 Strictness
336 * *
337 ************************************************************************
338 -}
339
340 pprStrictness :: StrictSig -> SDoc
341 pprStrictness sig = ppr sig
342
343 {-
344 ************************************************************************
345 * *
346 RuleInfo
347 * *
348 ************************************************************************
349
350 Note [Specialisations and RULES in IdInfo]
351 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
352 Generally speaking, a GlobalId has an *empty* RuleInfo. All their
353 RULES are contained in the globally-built rule-base. In principle,
354 one could attach the to M.f the RULES for M.f that are defined in M.
355 But we don't do that for instance declarations and so we just treat
356 them all uniformly.
357
358 The EXCEPTION is PrimOpIds, which do have rules in their IdInfo. That is
359 jsut for convenience really.
360
361 However, LocalIds may have non-empty RuleInfo. We treat them
362 differently because:
363 a) they might be nested, in which case a global table won't work
364 b) the RULE might mention free variables, which we use to keep things alive
365
366 In TidyPgm, when the LocalId becomes a GlobalId, its RULES are stripped off
367 and put in the global list.
368 -}
369
370 -- | Records the specializations of this 'Id' that we know about
371 -- in the form of rewrite 'CoreRule's that target them
372 data RuleInfo
373 = RuleInfo
374 [CoreRule]
375 DVarSet -- Locally-defined free vars of *both* LHS and RHS
376 -- of rules. I don't think it needs to include the
377 -- ru_fn though.
378 -- Note [Rule dependency info] in OccurAnal
379
380 -- | Assume that no specilizations exist: always safe
381 emptyRuleInfo :: RuleInfo
382 emptyRuleInfo = RuleInfo [] emptyDVarSet
383
384 isEmptyRuleInfo :: RuleInfo -> Bool
385 isEmptyRuleInfo (RuleInfo rs _) = null rs
386
387 -- | Retrieve the locally-defined free variables of both the left and
388 -- right hand sides of the specialization rules
389 ruleInfoFreeVars :: RuleInfo -> DVarSet
390 ruleInfoFreeVars (RuleInfo _ fvs) = fvs
391
392 ruleInfoRules :: RuleInfo -> [CoreRule]
393 ruleInfoRules (RuleInfo rules _) = rules
394
395 -- | Change the name of the function the rule is keyed on on all of the 'CoreRule's
396 setRuleInfoHead :: Name -> RuleInfo -> RuleInfo
397 setRuleInfoHead fn (RuleInfo rules fvs)
398 = RuleInfo (map (setRuleIdName fn) rules) fvs
399
400 {-
401 ************************************************************************
402 * *
403 \subsection[CG-IdInfo]{Code generator-related information}
404 * *
405 ************************************************************************
406 -}
407
408 -- CafInfo is used to build Static Reference Tables (see simplStg/SRT.hs).
409
410 -- | Records whether an 'Id' makes Constant Applicative Form references
411 data CafInfo
412 = MayHaveCafRefs -- ^ Indicates that the 'Id' is for either:
413 --
414 -- 1. A function or static constructor
415 -- that refers to one or more CAFs, or
416 --
417 -- 2. A real live CAF
418
419 | NoCafRefs -- ^ A function or static constructor
420 -- that refers to no CAFs.
421 deriving (Eq, Ord)
422
423 -- | Assumes that the 'Id' has CAF references: definitely safe
424 vanillaCafInfo :: CafInfo
425 vanillaCafInfo = MayHaveCafRefs
426
427 mayHaveCafRefs :: CafInfo -> Bool
428 mayHaveCafRefs MayHaveCafRefs = True
429 mayHaveCafRefs _ = False
430
431 instance Outputable CafInfo where
432 ppr = ppCafInfo
433
434 ppCafInfo :: CafInfo -> SDoc
435 ppCafInfo NoCafRefs = text "NoCafRefs"
436 ppCafInfo MayHaveCafRefs = empty
437
438 {-
439 ************************************************************************
440 * *
441 \subsection{Bulk operations on IdInfo}
442 * *
443 ************************************************************************
444 -}
445
446 -- | This is used to remove information on lambda binders that we have
447 -- setup as part of a lambda group, assuming they will be applied all at once,
448 -- but turn out to be part of an unsaturated lambda as in e.g:
449 --
450 -- > (\x1. \x2. e) arg1
451 zapLamInfo :: IdInfo -> Maybe IdInfo
452 zapLamInfo info@(IdInfo {occInfo = occ, demandInfo = demand})
453 | is_safe_occ occ && is_safe_dmd demand
454 = Nothing
455 | otherwise
456 = Just (info {occInfo = safe_occ, demandInfo = topDmd})
457 where
458 -- The "unsafe" occ info is the ones that say I'm not in a lambda
459 -- because that might not be true for an unsaturated lambda
460 is_safe_occ (OneOcc in_lam _ _) = in_lam
461 is_safe_occ _other = True
462
463 safe_occ = case occ of
464 OneOcc _ once int_cxt -> OneOcc insideLam once int_cxt
465 _other -> occ
466
467 is_safe_dmd dmd = not (isStrictDmd dmd)
468
469 -- | Remove all demand info on the 'IdInfo'
470 zapDemandInfo :: IdInfo -> Maybe IdInfo
471 zapDemandInfo info = Just (info {demandInfo = topDmd})
472
473 -- | Remove usage (but not strictness) info on the 'IdInfo'
474 zapUsageInfo :: IdInfo -> Maybe IdInfo
475 zapUsageInfo info = Just (info {demandInfo = zapUsageDemand (demandInfo info)})
476
477 -- | Remove usage environment info from the strictness signature on the 'IdInfo'
478 zapUsageEnvInfo :: IdInfo -> Maybe IdInfo
479 zapUsageEnvInfo info
480 | hasDemandEnvSig (strictnessInfo info)
481 = Just (info {strictnessInfo = zapUsageEnvSig (strictnessInfo info)})
482 | otherwise
483 = Nothing
484
485 zapUsedOnceInfo :: IdInfo -> Maybe IdInfo
486 zapUsedOnceInfo info
487 = Just $ info { strictnessInfo = zapUsedOnceSig (strictnessInfo info)
488 , demandInfo = zapUsedOnceDemand (demandInfo info) }
489
490 zapFragileInfo :: IdInfo -> Maybe IdInfo
491 -- ^ Zap info that depends on free variables
492 zapFragileInfo info
493 = Just (info `setRuleInfo` emptyRuleInfo
494 `setUnfoldingInfo` noUnfolding
495 `setOccInfo` zapFragileOcc occ)
496 where
497 occ = occInfo info
498
499 {-
500 ************************************************************************
501 * *
502 \subsection{TickBoxOp}
503 * *
504 ************************************************************************
505 -}
506
507 type TickBoxId = Int
508
509 -- | Tick box for Hpc-style coverage
510 data TickBoxOp
511 = TickBox Module {-# UNPACK #-} !TickBoxId
512
513 instance Outputable TickBoxOp where
514 ppr (TickBox mod n) = text "tick" <+> ppr (mod,n)