Make record selectors into ordinary functions
[ghc-hetmet.git] / compiler / basicTypes / IdInfo.lhs
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 \begin{code}
11 module IdInfo (
12         -- * The IdDetails type
13         IdDetails(..), pprIdDetails,
14
15         -- * The IdInfo type
16         IdInfo,         -- Abstract
17         vanillaIdInfo, noCafIdInfo,
18         seqIdInfo, megaSeqIdInfo,
19
20         -- ** Zapping various forms of Info
21         zapLamInfo, zapDemandInfo, zapFragileInfo,
22
23         -- ** The ArityInfo type
24         ArityInfo,
25         unknownArity, 
26         arityInfo, setArityInfo, ppArityInfo, 
27
28         -- ** Demand and strictness Info
29         newStrictnessInfo, setNewStrictnessInfo, 
30         newDemandInfo, setNewDemandInfo, pprNewStrictness,
31         setAllStrictnessInfo,
32
33 #ifdef OLD_STRICTNESS
34         -- ** Old strictness Info
35         StrictnessInfo(..),
36         mkStrictnessInfo, noStrictnessInfo,
37         ppStrictnessInfo, isBottomingStrictness, 
38         strictnessInfo, setStrictnessInfo,
39         
40         oldStrictnessFromNew, newStrictnessFromOld,
41
42         -- ** Old demand Info
43         demandInfo, setDemandInfo, 
44         oldDemand, newDemand,
45
46         -- ** Old Constructed Product Result Info
47         CprInfo(..), 
48         cprInfo, setCprInfo, ppCprInfo, noCprInfo,
49         cprInfoFromNewStrictness,
50 #endif
51
52         -- ** The WorkerInfo type
53         WorkerInfo(..),
54         workerExists, wrapperArity, workerId,
55         workerInfo, setWorkerInfo, ppWorkerInfo,
56
57         -- ** Unfolding Info
58         unfoldingInfo, setUnfoldingInfo, setUnfoldingInfoLazily,
59
60         -- ** The InlinePragInfo type
61         InlinePragInfo,
62         inlinePragInfo, setInlinePragInfo,
63
64         -- ** The OccInfo type
65         OccInfo(..),
66         isFragileOcc, isDeadOcc, isLoopBreaker,
67         occInfo, setOccInfo,
68
69         InsideLam, OneBranch,
70         insideLam, notInsideLam, oneBranch, notOneBranch,
71         
72         -- ** The SpecInfo type
73         SpecInfo(..),
74         isEmptySpecInfo, specInfoFreeVars,
75         specInfoRules, seqSpecInfo, setSpecInfoHead,
76         specInfo, setSpecInfo,
77
78         -- ** The CAFInfo type
79         CafInfo(..),
80         ppCafInfo, mayHaveCafRefs,
81         cafInfo, setCafInfo,
82
83         -- ** The LBVarInfo type
84         LBVarInfo(..),
85         noLBVarInfo, hasNoLBVarInfo,
86         lbvarInfo, setLBVarInfo,
87
88         -- ** Tick-box Info
89         TickBoxOp(..), TickBoxId,
90     ) where
91
92 import CoreSyn ( CoreRule, setRuleIdName, seqRules, Unfolding, noUnfolding )
93
94 import Class
95 import PrimOp
96 import Name
97 import Var
98 import VarSet
99 import BasicTypes
100 import DataCon
101 import TyCon
102 import ForeignCall
103 import NewDemand
104 import Outputable       
105 import Module
106 import FastString
107
108 import Data.Maybe
109
110 #ifdef OLD_STRICTNESS
111 import Demand
112 import qualified Demand
113 import Util
114 import Data.List
115 #endif
116
117 -- infixl so you can say (id `set` a `set` b)
118 infixl  1 `setSpecInfo`,
119           `setArityInfo`,
120           `setInlinePragInfo`,
121           `setUnfoldingInfo`,
122           `setWorkerInfo`,
123           `setLBVarInfo`,
124           `setOccInfo`,
125           `setCafInfo`,
126           `setNewStrictnessInfo`,
127           `setAllStrictnessInfo`,
128           `setNewDemandInfo`
129 #ifdef OLD_STRICTNESS
130           , `setCprInfo`
131           , `setDemandInfo`
132           , `setStrictnessInfo`
133 #endif
134 \end{code}
135
136 %************************************************************************
137 %*                                                                      *
138 \subsection{New strictness info}
139 %*                                                                      *
140 %************************************************************************
141
142 To be removed later
143
144 \begin{code}
145 -- | Set old and new strictness information together
146 setAllStrictnessInfo :: IdInfo -> Maybe StrictSig -> IdInfo
147 setAllStrictnessInfo info Nothing
148   = info { newStrictnessInfo = Nothing
149 #ifdef OLD_STRICTNESS
150          , strictnessInfo = NoStrictnessInfo
151          , cprInfo = NoCPRInfo
152 #endif
153          }
154
155 setAllStrictnessInfo info (Just sig)
156   = info { newStrictnessInfo = Just sig
157 #ifdef OLD_STRICTNESS
158          , strictnessInfo = oldStrictnessFromNew sig
159          , cprInfo = cprInfoFromNewStrictness sig
160 #endif
161          }
162
163 seqNewStrictnessInfo :: Maybe StrictSig -> ()
164 seqNewStrictnessInfo Nothing = ()
165 seqNewStrictnessInfo (Just ty) = seqStrictSig ty
166
167 pprNewStrictness :: Maybe StrictSig -> SDoc
168 pprNewStrictness Nothing = empty
169 pprNewStrictness (Just sig) = ftext (fsLit "Str:") <+> ppr sig
170
171 #ifdef OLD_STRICTNESS
172 oldStrictnessFromNew :: StrictSig -> Demand.StrictnessInfo
173 oldStrictnessFromNew sig = mkStrictnessInfo (map oldDemand dmds, isBotRes res_info)
174                          where
175                            (dmds, res_info) = splitStrictSig sig
176
177 cprInfoFromNewStrictness :: StrictSig -> CprInfo
178 cprInfoFromNewStrictness sig = case strictSigResInfo sig of
179                                   RetCPR -> ReturnsCPR
180                                   other  -> NoCPRInfo
181
182 newStrictnessFromOld :: Name -> Arity -> Demand.StrictnessInfo -> CprInfo -> StrictSig
183 newStrictnessFromOld name arity (Demand.StrictnessInfo ds res) cpr
184   | listLengthCmp ds arity /= GT -- length ds <= arity
185         -- Sometimes the old strictness analyser has more
186         -- demands than the arity justifies
187   = mk_strict_sig name arity $
188     mkTopDmdType (map newDemand ds) (newRes res cpr)
189
190 newStrictnessFromOld name arity other cpr
191   =     -- Either no strictness info, or arity is too small
192         -- In either case we can't say anything useful
193     mk_strict_sig name arity $
194     mkTopDmdType (replicate arity lazyDmd) (newRes False cpr)
195
196 mk_strict_sig name arity dmd_ty
197   = WARN( arity /= dmdTypeDepth dmd_ty, ppr name <+> (ppr arity $$ ppr dmd_ty) )
198     mkStrictSig dmd_ty
199
200 newRes True  _          = BotRes
201 newRes False ReturnsCPR = retCPR
202 newRes False NoCPRInfo  = TopRes
203
204 newDemand :: Demand.Demand -> NewDemand.Demand
205 newDemand (WwLazy True)      = Abs
206 newDemand (WwLazy False)     = lazyDmd
207 newDemand WwStrict           = evalDmd
208 newDemand (WwUnpack unpk ds) = Eval (Prod (map newDemand ds))
209 newDemand WwPrim             = lazyDmd
210 newDemand WwEnum             = evalDmd
211
212 oldDemand :: NewDemand.Demand -> Demand.Demand
213 oldDemand Abs              = WwLazy True
214 oldDemand Top              = WwLazy False
215 oldDemand Bot              = WwStrict
216 oldDemand (Box Bot)        = WwStrict
217 oldDemand (Box Abs)        = WwLazy False
218 oldDemand (Box (Eval _))   = WwStrict   -- Pass box only
219 oldDemand (Defer d)        = WwLazy False
220 oldDemand (Eval (Prod ds)) = WwUnpack True (map oldDemand ds)
221 oldDemand (Eval (Poly _))  = WwStrict
222 oldDemand (Call _)         = WwStrict
223
224 #endif /* OLD_STRICTNESS */
225 \end{code}
226
227
228 \begin{code}
229 seqNewDemandInfo :: Maybe Demand -> ()
230 seqNewDemandInfo Nothing    = ()
231 seqNewDemandInfo (Just dmd) = seqDemand dmd
232 \end{code}
233
234
235 %************************************************************************
236 %*                                                                      *
237                      IdDetails
238 %*                                                                      *
239 %************************************************************************
240
241 \begin{code}
242 -- | The 'IdDetails' of an 'Id' give stable, and necessary, 
243 -- information about the Id. 
244 data IdDetails
245   = VanillaId   
246
247   -- | The 'Id' for a record selector
248   | RecSelId                 
249     { sel_tycon   :: TyCon      -- ^ For a data type family, this is the /instance/ 'TyCon'
250                                 --   not the family 'TyCon'
251     , sel_naughty :: Bool       -- True <=> a "naughty" selector which can't actually exist, for example @x@ in:
252                                 --    data T = forall a. MkT { x :: a }
253     }                           -- See Note [Naughty record selectors] in TcTyClsDecls
254
255   | DataConWorkId DataCon       -- ^ The 'Id' is for a data constructor /worker/
256   | DataConWrapId DataCon       -- ^ The 'Id' is for a data constructor /wrapper/
257                                 
258                                 -- [the only reasons we need to know is so that
259                                 --  a) to support isImplicitId
260                                 --  b) when desugaring a RecordCon we can get 
261                                 --     from the Id back to the data con]
262
263   | ClassOpId Class             -- ^ The 'Id' is an operation of a class
264
265   | PrimOpId PrimOp             -- ^ The 'Id' is for a primitive operator
266   | FCallId ForeignCall         -- ^ The 'Id' is for a foreign call
267
268   | TickBoxOpId TickBoxOp       -- ^ The 'Id' is for a HPC tick box (both traditional and binary)
269
270   | DFunId                      -- ^ A dictionary function.  We don't use this in an essential way,
271                                 -- currently, but it's kind of nice that we can keep track of
272                                 -- which Ids are DFuns, across module boundaries too
273
274
275 instance Outputable IdDetails where
276     ppr = pprIdDetails
277
278 pprIdDetails :: IdDetails -> SDoc
279 pprIdDetails VanillaId         = empty
280 pprIdDetails (RecSelId {})  = ptext (sLit "[RecSel]")
281 pprIdDetails (DataConWorkId _) = ptext (sLit "[DataCon]")
282 pprIdDetails (DataConWrapId _) = ptext (sLit "[DataConWrapper]")
283 pprIdDetails (ClassOpId _)     = ptext (sLit "[ClassOp]")
284 pprIdDetails (PrimOpId _)      = ptext (sLit "[PrimOp]")
285 pprIdDetails (FCallId _)       = ptext (sLit "[ForeignCall]")
286 pprIdDetails (TickBoxOpId _)   = ptext (sLit "[TickBoxOp]")
287 pprIdDetails DFunId            = ptext (sLit "[DFunId]")
288 \end{code}
289
290
291
292
293 %************************************************************************
294 %*                                                                      *
295 \subsection{The main IdInfo type}
296 %*                                                                      *
297 %************************************************************************
298
299 \begin{code}
300 -- | An 'IdInfo' gives /optional/ information about an 'Id'.  If
301 -- present it never lies, but it may not be present, in which case there
302 -- is always a conservative assumption which can be made.
303 -- 
304 -- Two 'Id's may have different info even though they have the same
305 -- 'Unique' (and are hence the same 'Id'); for example, one might lack
306 -- the properties attached to the other.
307 -- 
308 -- The 'IdInfo' gives information about the value, or definition, of the
309 -- 'Id'.  It does not contain information about the 'Id''s usage,
310 -- except for 'demandInfo' and 'lbvarInfo'.
311 data IdInfo
312   = IdInfo {
313         arityInfo       :: !ArityInfo,          -- ^ 'Id' arity
314         specInfo        :: SpecInfo,            -- ^ Specialisations of the 'Id's function which exist
315 #ifdef OLD_STRICTNESS
316         cprInfo         :: CprInfo,             -- ^ If the 'Id's function always constructs a product result
317         demandInfo      :: Demand.Demand,       -- ^ Whether or not the 'Id' is definitely demanded
318         strictnessInfo  :: StrictnessInfo,      -- ^ 'Id' strictness properties
319 #endif
320         workerInfo      :: WorkerInfo,          -- ^ Pointer to worker function.
321                                                 -- Within one module this is irrelevant; the 
322                                                 -- inlining of a worker is handled via the 'Unfolding'.
323                                                 -- However, when the module is imported by others, the
324                                                 -- 'WorkerInfo' is used /only/ to indicate the form of
325                                                 -- the RHS, so that interface files don't actually 
326                                                 -- need to contain the RHS; it can be derived from
327                                                 -- the strictness info
328
329         unfoldingInfo   :: Unfolding,           -- ^ The 'Id's unfolding
330         cafInfo         :: CafInfo,             -- ^ 'Id' CAF info
331         lbvarInfo       :: LBVarInfo,           -- ^ Info about a lambda-bound variable, if the 'Id' is one
332         inlinePragInfo  :: InlinePragInfo,      -- ^ Any inline pragma atached to the 'Id'
333         occInfo         :: OccInfo,             -- ^ How the 'Id' occurs in the program
334
335         newStrictnessInfo :: Maybe StrictSig,   -- ^ Id strictness information. Reason for Maybe: 
336                                                 -- the DmdAnal phase needs to know whether
337                                                 -- this is the first visit, so it can assign botSig.
338                                                 -- Other customers want topSig.  So @Nothing@ is good.
339
340         newDemandInfo     :: Maybe Demand       -- ^ Id demand information. Similarly we want to know 
341                                                 -- if there's no known demand yet, for when we are looking
342                                                 -- for CPR info
343     }
344
345 -- | Just evaluate the 'IdInfo' to WHNF
346 seqIdInfo :: IdInfo -> ()
347 seqIdInfo (IdInfo {}) = ()
348
349 -- | Evaluate all the fields of the 'IdInfo' that are generally demanded by the
350 -- compiler
351 megaSeqIdInfo :: IdInfo -> ()
352 megaSeqIdInfo info
353   = seqSpecInfo (specInfo info)                 `seq`
354     seqWorker (workerInfo info)                 `seq`
355
356 -- Omitting this improves runtimes a little, presumably because
357 -- some unfoldings are not calculated at all
358 --    seqUnfolding (unfoldingInfo info)         `seq`
359
360     seqNewDemandInfo (newDemandInfo info)       `seq`
361     seqNewStrictnessInfo (newStrictnessInfo info) `seq`
362
363 #ifdef OLD_STRICTNESS
364     Demand.seqDemand (demandInfo info)          `seq`
365     seqStrictnessInfo (strictnessInfo info)     `seq`
366     seqCpr (cprInfo info)                       `seq`
367 #endif
368
369     seqCaf (cafInfo info)                       `seq`
370     seqLBVar (lbvarInfo info)                   `seq`
371     seqOccInfo (occInfo info) 
372 \end{code}
373
374 Setters
375
376 \begin{code}
377 setWorkerInfo :: IdInfo -> WorkerInfo -> IdInfo
378 setWorkerInfo     info wk = wk `seq` info { workerInfo = wk }
379 setSpecInfo :: IdInfo -> SpecInfo -> IdInfo
380 setSpecInfo       info sp = sp `seq` info { specInfo = sp }
381 setInlinePragInfo :: IdInfo -> InlinePragInfo -> IdInfo
382 setInlinePragInfo info pr = pr `seq` info { inlinePragInfo = pr }
383 setOccInfo :: IdInfo -> OccInfo -> IdInfo
384 setOccInfo        info oc = oc `seq` info { occInfo = oc }
385 #ifdef OLD_STRICTNESS
386 setStrictnessInfo info st = st `seq` info { strictnessInfo = st }
387 #endif
388         -- Try to avoid spack leaks by seq'ing
389
390 setUnfoldingInfoLazily :: IdInfo -> Unfolding -> IdInfo
391 setUnfoldingInfoLazily info uf  -- Lazy variant to avoid looking at the
392   =                             -- unfolding of an imported Id unless necessary
393     info { unfoldingInfo = uf } -- (In this case the demand-zapping is redundant.)
394
395 setUnfoldingInfo :: IdInfo -> Unfolding -> IdInfo
396 setUnfoldingInfo info uf 
397         -- We do *not* seq on the unfolding info, For some reason, doing so 
398         -- actually increases residency significantly. 
399   = info { unfoldingInfo = uf }
400
401 #ifdef OLD_STRICTNESS
402 setDemandInfo     info dd = info { demandInfo = dd }
403 setCprInfo        info cp = info { cprInfo = cp }
404 #endif
405
406 setArityInfo :: IdInfo -> ArityInfo -> IdInfo
407 setArityInfo      info ar  = info { arityInfo = ar  }
408 setCafInfo :: IdInfo -> CafInfo -> IdInfo
409 setCafInfo        info caf = info { cafInfo = caf }
410
411 setLBVarInfo :: IdInfo -> LBVarInfo -> IdInfo
412 setLBVarInfo      info lb = {-lb `seq`-} info { lbvarInfo = lb }
413
414 setNewDemandInfo :: IdInfo -> Maybe Demand -> IdInfo
415 setNewDemandInfo     info dd = dd `seq` info { newDemandInfo = dd }
416 setNewStrictnessInfo :: IdInfo -> Maybe StrictSig -> IdInfo
417 setNewStrictnessInfo info dd = dd `seq` info { newStrictnessInfo = dd }
418 \end{code}
419
420
421 \begin{code}
422 -- | Basic 'IdInfo' that carries no useful information whatsoever
423 vanillaIdInfo :: IdInfo
424 vanillaIdInfo 
425   = IdInfo {
426             cafInfo             = vanillaCafInfo,
427             arityInfo           = unknownArity,
428 #ifdef OLD_STRICTNESS
429             cprInfo             = NoCPRInfo,
430             demandInfo          = wwLazy,
431             strictnessInfo      = NoStrictnessInfo,
432 #endif
433             specInfo            = emptySpecInfo,
434             workerInfo          = NoWorker,
435             unfoldingInfo       = noUnfolding,
436             lbvarInfo           = NoLBVarInfo,
437             inlinePragInfo      = AlwaysActive,
438             occInfo             = NoOccInfo,
439             newDemandInfo       = Nothing,
440             newStrictnessInfo   = Nothing
441            }
442
443 -- | More informative 'IdInfo' we can use when we know the 'Id' has no CAF references
444 noCafIdInfo :: IdInfo
445 noCafIdInfo  = vanillaIdInfo `setCafInfo`    NoCafRefs
446         -- Used for built-in type Ids in MkId.
447 \end{code}
448
449
450 %************************************************************************
451 %*                                                                      *
452 \subsection[arity-IdInfo]{Arity info about an @Id@}
453 %*                                                                      *
454 %************************************************************************
455
456 For locally-defined Ids, the code generator maintains its own notion
457 of their arities; so it should not be asking...  (but other things
458 besides the code-generator need arity info!)
459
460 \begin{code}
461 -- | An 'ArityInfo' of @n@ tells us that partial application of this 
462 -- 'Id' to up to @n-1@ value arguments does essentially no work.
463 --
464 -- That is not necessarily the same as saying that it has @n@ leading 
465 -- lambdas, because coerces may get in the way.
466 --
467 -- The arity might increase later in the compilation process, if
468 -- an extra lambda floats up to the binding site.
469 type ArityInfo = Arity
470
471 -- | It is always safe to assume that an 'Id' has an arity of 0
472 unknownArity :: Arity
473 unknownArity = 0 :: Arity
474
475 ppArityInfo :: Int -> SDoc
476 ppArityInfo 0 = empty
477 ppArityInfo n = hsep [ptext (sLit "Arity"), int n]
478 \end{code}
479
480 %************************************************************************
481 %*                                                                      *
482 \subsection{Inline-pragma information}
483 %*                                                                      *
484 %************************************************************************
485
486 \begin{code}
487 -- | Tells when the inlining is active.
488 -- When it is active the thing may be inlined, depending on how
489 -- big it is.
490 --
491 -- If there was an @INLINE@ pragma, then as a separate matter, the
492 -- RHS will have been made to look small with a Core inline 'Note'
493 --
494 -- The default 'InlinePragInfo' is 'AlwaysActive', so the info serves
495 -- entirely as a way to inhibit inlining until we want it
496 type InlinePragInfo = Activation
497 \end{code}
498
499
500 %************************************************************************
501 %*                                                                      *
502         SpecInfo
503 %*                                                                      *
504 %************************************************************************
505
506 \begin{code}
507 -- | Records the specializations of this 'Id' that we know about
508 -- in the form of rewrite 'CoreRule's that target them
509 data SpecInfo 
510   = SpecInfo 
511         [CoreRule] 
512         VarSet          -- Locally-defined free vars of *both* LHS and RHS 
513                         -- of rules.  I don't think it needs to include the
514                         -- ru_fn though.
515                         -- Note [Rule dependency info] in OccurAnal
516
517 -- | Assume that no specilizations exist: always safe
518 emptySpecInfo :: SpecInfo
519 emptySpecInfo = SpecInfo [] emptyVarSet
520
521 isEmptySpecInfo :: SpecInfo -> Bool
522 isEmptySpecInfo (SpecInfo rs _) = null rs
523
524 -- | Retrieve the locally-defined free variables of both the left and
525 -- right hand sides of the specialization rules
526 specInfoFreeVars :: SpecInfo -> VarSet
527 specInfoFreeVars (SpecInfo _ fvs) = fvs
528
529 specInfoRules :: SpecInfo -> [CoreRule]
530 specInfoRules (SpecInfo rules _) = rules
531
532 -- | Change the name of the function the rule is keyed on on all of the 'CoreRule's
533 setSpecInfoHead :: Name -> SpecInfo -> SpecInfo
534 setSpecInfoHead fn (SpecInfo rules fvs)
535   = SpecInfo (map (setRuleIdName fn) rules) fvs
536
537 seqSpecInfo :: SpecInfo -> ()
538 seqSpecInfo (SpecInfo rules fvs) = seqRules rules `seq` seqVarSet fvs
539 \end{code}
540
541 %************************************************************************
542 %*                                                                      *
543 \subsection[worker-IdInfo]{Worker info about an @Id@}
544 %*                                                                      *
545 %************************************************************************
546
547 There might not be a worker, even for a strict function, because:
548 (a) the function might be small enough to inline, so no need 
549     for w/w split
550 (b) the strictness info might be "SSS" or something, so no w/w split.
551
552 Sometimes the arity of a wrapper changes from the original arity from
553 which it was generated, so we always emit the "original" arity into
554 the interface file, as part of the worker info.
555
556 How can this happen?  Sometimes we get
557         f = coerce t (\x y -> $wf x y)
558 at the moment of w/w split; but the eta reducer turns it into
559         f = coerce t $wf
560 which is perfectly fine except that the exposed arity so far as
561 the code generator is concerned (zero) differs from the arity
562 when we did the split (2).  
563
564 All this arises because we use 'arity' to mean "exactly how many
565 top level lambdas are there" in interface files; but during the
566 compilation of this module it means "how many things can I apply
567 this to".
568
569 \begin{code}
570
571 -- | If this Id has a worker then we store a reference to it. Worker
572 -- functions are generated by the worker\/wrapper pass, using information
573 -- information from strictness analysis.
574 data WorkerInfo = NoWorker              -- ^ No known worker function
575                 | HasWorker Id Arity    -- ^ The 'Arity' is the arity of the /wrapper/ at the moment of the
576                                         -- worker\/wrapper split, which may be different from the current 'Id' 'Aritiy'
577
578 seqWorker :: WorkerInfo -> ()
579 seqWorker (HasWorker id a) = id `seq` a `seq` ()
580 seqWorker NoWorker         = ()
581
582 ppWorkerInfo :: WorkerInfo -> SDoc
583 ppWorkerInfo NoWorker            = empty
584 ppWorkerInfo (HasWorker wk_id _) = ptext (sLit "Worker") <+> ppr wk_id
585
586 workerExists :: WorkerInfo -> Bool
587 workerExists NoWorker        = False
588 workerExists (HasWorker _ _) = True
589
590 -- | The 'Id' of the worker function if it exists, or a panic otherwise
591 workerId :: WorkerInfo -> Id
592 workerId (HasWorker id _) = id
593 workerId NoWorker = panic "workerId: NoWorker"
594
595 -- | The 'Arity' of the worker function at the time of the split if it exists, or a panic otherwise
596 wrapperArity :: WorkerInfo -> Arity
597 wrapperArity (HasWorker _ a) = a
598 wrapperArity NoWorker = panic "wrapperArity: NoWorker"
599 \end{code}
600
601
602 %************************************************************************
603 %*                                                                      *
604 \subsection[CG-IdInfo]{Code generator-related information}
605 %*                                                                      *
606 %************************************************************************
607
608 \begin{code}
609 -- CafInfo is used to build Static Reference Tables (see simplStg/SRT.lhs).
610
611 -- | Records whether an 'Id' makes Constant Applicative Form references
612 data CafInfo 
613         = MayHaveCafRefs                -- ^ Indicates that the 'Id' is for either:
614                                         --
615                                         -- 1. A function or static constructor
616                                         --    that refers to one or more CAFs, or
617                                         --
618                                         -- 2. A real live CAF
619
620         | NoCafRefs                     -- ^ A function or static constructor
621                                         -- that refers to no CAFs.
622         deriving (Eq, Ord)
623
624 -- | Assumes that the 'Id' has CAF references: definitely safe
625 vanillaCafInfo :: CafInfo
626 vanillaCafInfo = MayHaveCafRefs
627
628 mayHaveCafRefs :: CafInfo -> Bool
629 mayHaveCafRefs  MayHaveCafRefs = True
630 mayHaveCafRefs _               = False
631
632 seqCaf :: CafInfo -> ()
633 seqCaf c = c `seq` ()
634
635 ppCafInfo :: CafInfo -> SDoc
636 ppCafInfo NoCafRefs = ptext (sLit "NoCafRefs")
637 ppCafInfo MayHaveCafRefs = empty
638 \end{code}
639
640 %************************************************************************
641 %*                                                                      *
642 \subsection[cpr-IdInfo]{Constructed Product Result info about an @Id@}
643 %*                                                                      *
644 %************************************************************************
645
646 \begin{code}
647 #ifdef OLD_STRICTNESS
648 -- | If the @Id@ is a function then it may have Constructed Product Result 
649 -- (CPR) info. A CPR analysis phase detects whether:
650 -- 
651 -- 1. The function's return value has a product type, i.e. an algebraic  type 
652 -- with a single constructor. Examples of such types are tuples and boxed
653 -- primitive values.
654 --
655 -- 2. The function always 'constructs' the value that it is returning.  It
656 -- must do this on every path through,  and it's OK if it calls another
657 -- function which constructs the result.
658 -- 
659 -- If this is the case then we store a template which tells us the
660 -- function has the CPR property and which components of the result are
661 -- also CPRs.
662 data CprInfo
663   = NoCPRInfo   -- ^ No, this function does not return a constructed product
664   | ReturnsCPR  -- ^ Yes, this function returns a constructed product
665                 
666                 -- Implicitly, this means "after the function has been applied
667                 -- to all its arguments", so the worker\/wrapper builder in 
668                 -- WwLib.mkWWcpr checks that that it is indeed saturated before
669                 -- making use of the CPR info
670
671         -- We used to keep nested info about sub-components, but
672         -- we never used it so I threw it away
673
674 -- | It's always safe to assume that an 'Id' does not have the CPR property
675 noCprInfo :: CprInt
676 noCprInfo = NoCPRInfo
677
678 seqCpr :: CprInfo -> ()
679 seqCpr ReturnsCPR = ()
680 seqCpr NoCPRInfo  = ()
681
682 ppCprInfo NoCPRInfo  = empty
683 ppCprInfo ReturnsCPR = ptext (sLit "__M")
684
685 instance Outputable CprInfo where
686     ppr = ppCprInfo
687
688 instance Show CprInfo where
689     showsPrec p c = showsPrecSDoc p (ppr c)
690 #endif
691 \end{code}
692
693 %************************************************************************
694 %*                                                                      *
695 \subsection[lbvar-IdInfo]{Lambda-bound var info about an @Id@}
696 %*                                                                      *
697 %************************************************************************
698
699 \begin{code}
700 -- | If the 'Id' is a lambda-bound variable then it may have lambda-bound
701 -- variable info. Sometimes we know whether the lambda binding this variable
702 -- is a \"one-shot\" lambda; that is, whether it is applied at most once.
703 --
704 -- This information may be useful in optimisation, as computations may
705 -- safely be floated inside such a lambda without risk of duplicating
706 -- work.
707 data LBVarInfo = NoLBVarInfo            -- ^ No information
708                | IsOneShotLambda        -- ^ The lambda is applied at most once).
709
710 -- | It is always safe to assume that an 'Id' has no lambda-bound variable information
711 noLBVarInfo :: LBVarInfo
712 noLBVarInfo = NoLBVarInfo
713
714 hasNoLBVarInfo :: LBVarInfo -> Bool
715 hasNoLBVarInfo NoLBVarInfo     = True
716 hasNoLBVarInfo IsOneShotLambda = False
717
718 seqLBVar :: LBVarInfo -> ()
719 seqLBVar l = l `seq` ()
720
721 pprLBVarInfo :: LBVarInfo -> SDoc
722 pprLBVarInfo NoLBVarInfo     = empty
723 pprLBVarInfo IsOneShotLambda = ptext (sLit "OneShot")
724
725 instance Outputable LBVarInfo where
726     ppr = pprLBVarInfo
727
728 instance Show LBVarInfo where
729     showsPrec p c = showsPrecSDoc p (ppr c)
730 \end{code}
731
732
733 %************************************************************************
734 %*                                                                      *
735 \subsection{Bulk operations on IdInfo}
736 %*                                                                      *
737 %************************************************************************
738
739 \begin{code}
740 -- | This is used to remove information on lambda binders that we have
741 -- setup as part of a lambda group, assuming they will be applied all at once,
742 -- but turn out to be part of an unsaturated lambda as in e.g:
743 --
744 -- > (\x1. \x2. e) arg1
745 zapLamInfo :: IdInfo -> Maybe IdInfo
746 zapLamInfo info@(IdInfo {occInfo = occ, newDemandInfo = demand})
747   | is_safe_occ occ && is_safe_dmd demand
748   = Nothing
749   | otherwise
750   = Just (info {occInfo = safe_occ, newDemandInfo = Nothing})
751   where
752         -- The "unsafe" occ info is the ones that say I'm not in a lambda
753         -- because that might not be true for an unsaturated lambda
754     is_safe_occ (OneOcc in_lam _ _) = in_lam
755     is_safe_occ _other              = True
756
757     safe_occ = case occ of
758                  OneOcc _ once int_cxt -> OneOcc insideLam once int_cxt
759                  _other                -> occ
760
761     is_safe_dmd Nothing    = True
762     is_safe_dmd (Just dmd) = not (isStrictDmd dmd)
763 \end{code}
764
765 \begin{code}
766 -- | Remove demand info on the 'IdInfo' if it is present, otherwise return @Nothing@
767 zapDemandInfo :: IdInfo -> Maybe IdInfo
768 zapDemandInfo info@(IdInfo {newDemandInfo = dmd})
769   | isJust dmd = Just (info {newDemandInfo = Nothing})
770   | otherwise  = Nothing
771 \end{code}
772
773 \begin{code}
774 zapFragileInfo :: IdInfo -> Maybe IdInfo
775 -- ^ Zap info that depends on free variables
776 zapFragileInfo info 
777   = Just (info `setSpecInfo` emptySpecInfo
778                `setWorkerInfo` NoWorker
779                `setUnfoldingInfo` noUnfolding
780                `setOccInfo` if isFragileOcc occ then NoOccInfo else occ)
781   where
782     occ = occInfo info
783 \end{code}
784
785 %************************************************************************
786 %*                                                                      *
787 \subsection{TickBoxOp}
788 %*                                                                      *
789 %************************************************************************
790
791 \begin{code}
792 type TickBoxId = Int
793
794 -- | Tick box for Hpc-style coverage
795 data TickBoxOp 
796    = TickBox Module {-# UNPACK #-} !TickBoxId
797
798 instance Outputable TickBoxOp where
799     ppr (TickBox mod n)         = ptext (sLit "tick") <+> ppr (mod,n)
800 \end{code}