[project @ 1999-11-01 17:09:54 by simonpj]
[ghc-hetmet.git] / ghc / compiler / basicTypes / IdInfo.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1993-1998
3 %
4 \section[IdInfo]{@IdInfos@: Non-essential information about @Ids@}
5
6 (And a pretty good illustration of quite a few things wrong with
7 Haskell. [WDP 94/11])
8
9 \begin{code}
10 module IdInfo (
11         IdInfo,         -- Abstract
12
13         vanillaIdInfo, mkIdInfo, seqIdInfo, megaSeqIdInfo,
14
15         -- Zapping
16         zapFragileInfo, zapLamInfo, zapSpecPragInfo, copyIdInfo,
17
18         -- Flavour
19         IdFlavour(..), flavourInfo, 
20         setNoDiscardInfo,
21         ppFlavourInfo,
22
23         -- Arity
24         ArityInfo(..),
25         exactArity, atLeastArity, unknownArity, hasArity,
26         arityInfo, setArityInfo, ppArityInfo, arityLowerBound,
27
28         -- Strictness
29         StrictnessInfo(..),                             -- Non-abstract
30         mkStrictnessInfo,
31         noStrictnessInfo, strictnessInfo,
32         ppStrictnessInfo, setStrictnessInfo, 
33         isBottomingStrictness, appIsBottom,
34
35         -- Worker
36         WorkerInfo, workerExists, 
37         workerInfo, setWorkerInfo, ppWorkerInfo,
38
39         -- Unfolding
40         unfoldingInfo, setUnfoldingInfo, 
41
42         -- DemandInfo
43         demandInfo, setDemandInfo, 
44
45         -- Inline prags
46         InlinePragInfo(..), 
47         inlinePragInfo, setInlinePragInfo, pprInlinePragInfo,
48
49         -- Occurrence info
50         OccInfo(..), InsideLam, OneBranch, insideLam, notInsideLam, oneBranch, notOneBranch,
51         occInfo, setOccInfo, isFragileOccInfo,
52
53         -- Specialisation
54         specInfo, setSpecInfo,
55
56         -- Update
57         UpdateInfo, UpdateSpec,
58         mkUpdateInfo, updateInfo, updateInfoMaybe, ppUpdateInfo, setUpdateInfo,
59
60         -- CAF info
61         CafInfo(..), cafInfo, setCafInfo, ppCafInfo,
62
63         -- Constructed Product Result Info
64         CprInfo(..), cprInfo, setCprInfo, ppCprInfo, noCprInfo,
65
66         -- Lambda-bound variable info
67         LBVarInfo(..), lbvarInfo, setLBVarInfo, noLBVarInfo
68     ) where
69
70 #include "HsVersions.h"
71
72
73 import {-# SOURCE #-} CoreUnfold ( Unfolding, noUnfolding, hasUnfolding, seqUnfolding )
74 import {-# SOURCE #-} CoreSyn    ( CoreExpr, CoreRules, emptyCoreRules, isEmptyCoreRules, seqRules )
75 import {-# SOURCE #-} Const      ( Con )
76
77 import Var              ( Id )
78 import VarSet           ( IdOrTyVarSet )
79 import FieldLabel       ( FieldLabel )
80 import Demand           ( Demand, isStrict, isLazy, wwLazy, pprDemands, seqDemand, seqDemands )
81 import Outputable       
82 import Maybe            ( isJust )
83
84 infixl  1 `setUpdateInfo`,
85           `setDemandInfo`,
86           `setStrictnessInfo`,
87           `setSpecInfo`,
88           `setArityInfo`,
89           `setInlinePragInfo`,
90           `setUnfoldingInfo`,
91           `setCprInfo`,
92           `setWorkerInfo`,
93           `setCafInfo`,
94           `setOccInfo`
95         -- infixl so you can say (id `set` a `set` b)
96 \end{code}
97
98 An @IdInfo@ gives {\em optional} information about an @Id@.  If
99 present it never lies, but it may not be present, in which case there
100 is always a conservative assumption which can be made.
101
102         There is one exception: the 'flavour' is *not* optional.
103         You must not discard it.
104         It used to be in Var.lhs, but that seems unclean.
105
106 Two @Id@s may have different info even though they have the same
107 @Unique@ (and are hence the same @Id@); for example, one might lack
108 the properties attached to the other.
109
110 The @IdInfo@ gives information about the value, or definition, of the
111 @Id@.  It does {\em not} contain information about the @Id@'s usage
112 (except for @DemandInfo@? ToDo). (@lbvarInfo@ is also a marginal
113 case.  KSW 1999-04).
114
115 \begin{code}
116 data IdInfo
117   = IdInfo {
118         flavourInfo     :: IdFlavour,           -- NOT OPTIONAL
119         arityInfo       :: ArityInfo,           -- Its arity
120         demandInfo      :: Demand,              -- Whether or not it is definitely demanded
121         specInfo        :: CoreRules,           -- Specialisations of this function which exist
122         strictnessInfo  :: StrictnessInfo,      -- Strictness properties
123         workerInfo      :: WorkerInfo,          -- Pointer to Worker Function
124         unfoldingInfo   :: Unfolding,           -- Its unfolding
125         updateInfo      :: UpdateInfo,          -- Which args should be updated
126         cafInfo         :: CafInfo,
127         cprInfo         :: CprInfo,             -- Function always constructs a product result
128         lbvarInfo       :: LBVarInfo,           -- Info about a lambda-bound variable
129         inlinePragInfo  :: InlinePragInfo,      -- Inline pragma
130         occInfo         :: OccInfo              -- How it occurs
131     }
132
133 seqIdInfo :: IdInfo -> ()
134 seqIdInfo (IdInfo {}) = ()
135
136 megaSeqIdInfo :: IdInfo -> ()
137 megaSeqIdInfo info
138   = seqFlavour (flavourInfo info)       `seq`
139     seqArity (arityInfo info)           `seq`
140     seqDemand (demandInfo info)         `seq`
141     seqRules (specInfo info)            `seq`
142     seqStrictness (strictnessInfo info) `seq`
143     seqWorker (workerInfo info)         `seq`
144
145 --    seqUnfolding (unfoldingInfo info) `seq`
146 -- Omitting this improves runtimes a little, presumably because
147 -- some unfoldings are not calculated at all
148
149     seqCaf (cafInfo info)               `seq`
150     seqCpr (cprInfo info)               `seq`
151     seqLBVar (lbvarInfo info)           `seq`
152     seqOccInfo (occInfo info) 
153 \end{code}
154
155 Setters
156
157 \begin{code}
158 setWorkerInfo     info wk = wk `seq` info { workerInfo = wk }
159 setSpecInfo       info sp = PSEQ sp (info { specInfo = sp })
160 setInlinePragInfo info pr = pr `seq` info { inlinePragInfo = pr }
161 setOccInfo        info oc = oc `seq` info { occInfo = oc }
162 setStrictnessInfo info st = st `seq` info { strictnessInfo = st }
163         -- Try to avoid spack leaks by seq'ing
164
165 setUnfoldingInfo  info uf = info { unfoldingInfo = uf }
166         -- We do *not* seq on the unfolding info, For some reason, doing so 
167         -- actually increases residency significantly. 
168
169 setUpdateInfo     info ud = info { updateInfo = ud }
170 setDemandInfo     info dd = info { demandInfo = dd }
171 setArityInfo      info ar = info { arityInfo = ar  }
172 setCafInfo        info cf = info { cafInfo = cf }
173 setCprInfo        info cp = info { cprInfo = cp }
174 setLBVarInfo      info lb = info { lbvarInfo = lb }
175
176 setNoDiscardInfo  info = case flavourInfo info of
177                                 VanillaId -> info { flavourInfo = NoDiscardId }
178                                 other     -> info
179 zapSpecPragInfo   info = case flavourInfo info of
180                                 SpecPragmaId -> info { flavourInfo = VanillaId }
181                                 other        -> info
182
183 \end{code}
184
185
186 \begin{code}
187 vanillaIdInfo :: IdInfo
188 vanillaIdInfo = mkIdInfo VanillaId
189
190 mkIdInfo :: IdFlavour -> IdInfo
191 mkIdInfo flv = IdInfo {
192                     flavourInfo         = flv,
193                     arityInfo           = UnknownArity,
194                     demandInfo          = wwLazy,
195                     specInfo            = emptyCoreRules,
196                     workerInfo          = Nothing,
197                     strictnessInfo      = NoStrictnessInfo,
198                     unfoldingInfo       = noUnfolding,
199                     updateInfo          = NoUpdateInfo,
200                     cafInfo             = MayHaveCafRefs,
201                     cprInfo             = NoCPRInfo,
202                     lbvarInfo           = NoLBVarInfo,
203                     inlinePragInfo      = NoInlinePragInfo,
204                     occInfo             = NoOccInfo
205            }
206 \end{code}
207
208
209 %************************************************************************
210 %*                                                                      *
211 \subsection{Flavour}
212 %*                                                                      *
213 %************************************************************************
214
215 \begin{code}
216 data IdFlavour
217   = VanillaId                           -- Most Ids are like this
218   | ConstantId Con                      -- The Id for a constant (data constructor or primop)
219   | RecordSelId FieldLabel              -- The Id for a record selector
220   | SpecPragmaId                        -- Don't discard these
221   | NoDiscardId                         -- Don't discard these either
222
223 ppFlavourInfo :: IdFlavour -> SDoc
224 ppFlavourInfo VanillaId       = empty
225 ppFlavourInfo (ConstantId _)  = ptext SLIT("[Constr]")
226 ppFlavourInfo (RecordSelId _) = ptext SLIT("[RecSel]")
227 ppFlavourInfo SpecPragmaId    = ptext SLIT("[SpecPrag]")
228 ppFlavourInfo NoDiscardId     = ptext SLIT("[NoDiscard]")
229
230 seqFlavour :: IdFlavour -> ()
231 seqFlavour f = f `seq` ()
232 \end{code}
233
234 The @SpecPragmaId@ exists only to make Ids that are
235 on the *LHS* of bindings created by SPECIALISE pragmas; 
236 eg:             s = f Int d
237 The SpecPragmaId is never itself mentioned; it
238 exists solely so that the specialiser will find
239 the call to f, and make specialised version of it.
240 The SpecPragmaId binding is discarded by the specialiser
241 when it gathers up overloaded calls.
242 Meanwhile, it is not discarded as dead code.
243
244
245 %************************************************************************
246 %*                                                                      *
247 \subsection[arity-IdInfo]{Arity info about an @Id@}
248 %*                                                                      *
249 %************************************************************************
250
251 For locally-defined Ids, the code generator maintains its own notion
252 of their arities; so it should not be asking...  (but other things
253 besides the code-generator need arity info!)
254
255 \begin{code}
256 data ArityInfo
257   = UnknownArity        -- No idea
258
259   | ArityExactly Int    -- Arity is exactly this.  We use this when importing a
260                         -- function; it's already been compiled and we know its
261                         -- arity for sure.
262
263   | ArityAtLeast Int    -- Arity is this or greater.  We attach this arity to 
264                         -- functions in the module being compiled.  Their arity
265                         -- might increase later in the compilation process, if
266                         -- an extra lambda floats up to the binding site.
267
268 seqArity :: ArityInfo -> ()
269 seqArity a = arityLowerBound a `seq` ()
270
271 exactArity   = ArityExactly
272 atLeastArity = ArityAtLeast
273 unknownArity = UnknownArity
274
275 arityLowerBound :: ArityInfo -> Int
276 arityLowerBound UnknownArity     = 0
277 arityLowerBound (ArityAtLeast n) = n
278 arityLowerBound (ArityExactly n) = n
279
280 hasArity :: ArityInfo -> Bool
281 hasArity UnknownArity = False
282 hasArity other        = True
283
284 ppArityInfo UnknownArity         = empty
285 ppArityInfo (ArityExactly arity) = hsep [ptext SLIT("__A"), int arity]
286 ppArityInfo (ArityAtLeast arity) = hsep [ptext SLIT("__AL"), int arity]
287 \end{code}
288
289 %************************************************************************
290 %*                                                                      *
291 \subsection{Inline-pragma information}
292 %*                                                                      *
293 %************************************************************************
294
295 \begin{code}
296 data InlinePragInfo
297   = NoInlinePragInfo
298   | IMustNotBeINLINEd Bool              -- True <=> came from an INLINE prag, False <=> came from a NOINLINE prag
299                       (Maybe Int)       -- Phase number from pragma, if any
300         -- The True, Nothing case doesn't need to be recorded
301
302 instance Outputable InlinePragInfo where
303   -- This is now parsed in interface files
304   ppr NoInlinePragInfo = empty
305   ppr other_prag       = ptext SLIT("__U") <> pprInlinePragInfo other_prag
306
307 pprInlinePragInfo NoInlinePragInfo                   = empty
308 pprInlinePragInfo (IMustNotBeINLINEd True Nothing)   = empty
309 pprInlinePragInfo (IMustNotBeINLINEd True (Just n))  = brackets (int n)
310 pprInlinePragInfo (IMustNotBeINLINEd False Nothing)  = brackets (char '!')
311 pprInlinePragInfo (IMustNotBeINLINEd False (Just n)) = brackets (char '!' <> int n)
312                                                         
313 instance Show InlinePragInfo where
314   showsPrec p prag = showsPrecSDoc p (ppr prag)
315 \end{code}
316
317
318 %************************************************************************
319 %*                                                                      *
320 \subsection{Occurrence information}
321 %*                                                                      *
322 %************************************************************************
323
324 \begin{code}
325 data OccInfo 
326   = NoOccInfo
327
328   | IAmDead             -- Marks unused variables.  Sometimes useful for
329                         -- lambda and case-bound variables.
330
331   | OneOcc InsideLam
332
333            OneBranch
334
335   | IAmALoopBreaker     -- Used by the occurrence analyser to mark loop-breakers
336                         -- in a group of recursive definitions
337
338 seqOccInfo :: OccInfo -> ()
339 seqOccInfo (OneOcc in_lam once) = in_lam `seq` once `seq` ()
340 seqOccInfo occ                  = ()
341
342 type InsideLam = Bool   -- True <=> Occurs inside a non-linear lambda
343                         -- Substituting a redex for this occurrence is
344                         -- dangerous because it might duplicate work.
345 insideLam    = True
346 notInsideLam = False
347
348 type OneBranch = Bool   -- True <=> Occurs in only one case branch
349                         --      so no code-duplication issue to worry about
350 oneBranch    = True
351 notOneBranch = False
352
353 isFragileOccInfo :: OccInfo -> Bool
354 isFragileOccInfo (OneOcc _ _) = True
355 isFragileOccInfo other        = False
356 \end{code}
357
358 \begin{code}
359 instance Outputable OccInfo where
360   -- only used for debugging; never parsed.  KSW 1999-07
361   ppr NoOccInfo                                   = empty
362   ppr IAmALoopBreaker                             = ptext SLIT("_Kx")
363   ppr IAmDead                                     = ptext SLIT("_Kd")
364   ppr (OneOcc inside_lam one_branch) | inside_lam = ptext SLIT("_Kl")
365                                      | one_branch = ptext SLIT("_Ks")
366                                      | otherwise  = ptext SLIT("_Ks*")
367
368 instance Show OccInfo where
369   showsPrec p occ = showsPrecSDoc p (ppr occ)
370 \end{code}
371
372 %************************************************************************
373 %*                                                                      *
374 \subsection[strictness-IdInfo]{Strictness info about an @Id@}
375 %*                                                                      *
376 %************************************************************************
377
378 We specify the strictness of a function by giving information about
379 each of the ``wrapper's'' arguments (see the description about
380 worker/wrapper-style transformations in the PJ/Launchbury paper on
381 unboxed types).
382
383 The list of @Demands@ specifies: (a)~the strictness properties of a
384 function's arguments; and (b)~the type signature of that worker (if it
385 exists); i.e. its calling convention.
386
387 Note that the existence of a worker function is now denoted by the Id's
388 workerInfo field.
389
390 \begin{code}
391 data StrictnessInfo
392   = NoStrictnessInfo
393
394   | StrictnessInfo [Demand] 
395                    Bool         -- True <=> the function diverges regardless of its arguments
396                                 -- Useful for "error" and other disguised variants thereof.  
397                                 -- BUT NB: f = \x y. error "urk"
398                                 --         will have info  SI [SS] True
399                                 -- but still (f) and (f 2) are not bot; only (f 3 2) is bot
400
401 seqStrictness :: StrictnessInfo -> ()
402 seqStrictness (StrictnessInfo ds b) = b `seq` seqDemands ds
403 seqStrictness other                 = ()
404 \end{code}
405
406 \begin{code}
407 mkStrictnessInfo :: ([Demand], Bool) -> StrictnessInfo
408
409 mkStrictnessInfo (xs, is_bot)
410   | all isLazy xs && not is_bot = NoStrictnessInfo              -- Uninteresting
411   | otherwise                   = StrictnessInfo xs is_bot
412
413 noStrictnessInfo       = NoStrictnessInfo
414
415 isBottomingStrictness (StrictnessInfo _ bot) = bot
416 isBottomingStrictness NoStrictnessInfo       = False
417
418 -- appIsBottom returns true if an application to n args would diverge
419 appIsBottom (StrictnessInfo ds bot)   n = bot && (n >= length ds)
420 appIsBottom  NoStrictnessInfo         n = False
421
422 ppStrictnessInfo NoStrictnessInfo = empty
423 ppStrictnessInfo (StrictnessInfo wrapper_args bot)
424   = hsep [ptext SLIT("__S"), pprDemands wrapper_args bot]
425 \end{code}
426
427 %************************************************************************
428 %*                                                                      *
429 \subsection[worker-IdInfo]{Worker info about an @Id@}
430 %*                                                                      *
431 %************************************************************************
432
433 If this Id has a worker then we store a reference to it. Worker
434 functions are generated by the worker/wrapper pass.  This uses
435 information from the strictness and CPR analyses.
436
437 There might not be a worker, even for a strict function, because:
438 (a) the function might be small enough to inline, so no need 
439     for w/w split
440 (b) the strictness info might be "SSS" or something, so no w/w split.
441
442 \begin{code}
443
444 type WorkerInfo = Maybe Id
445
446 {- UNUSED:
447 mkWorkerInfo :: Id -> WorkerInfo
448 mkWorkerInfo wk_id = Just wk_id
449 -}
450
451 seqWorker :: WorkerInfo -> ()
452 seqWorker (Just id) = id `seq` ()
453 seqWorker Nothing   = ()
454
455 ppWorkerInfo Nothing      = empty
456 ppWorkerInfo (Just wk_id) = ptext SLIT("__P") <+> ppr wk_id
457
458 noWorkerInfo = Nothing
459
460 workerExists :: WorkerInfo -> Bool
461 workerExists = isJust
462 \end{code}
463
464
465 %************************************************************************
466 %*                                                                      *
467 \subsection[update-IdInfo]{Update-analysis info about an @Id@}
468 %*                                                                      *
469 %************************************************************************
470
471 \begin{code}
472 data UpdateInfo
473   = NoUpdateInfo
474   | SomeUpdateInfo UpdateSpec
475   deriving (Eq, Ord)
476       -- we need Eq/Ord to cross-chk update infos in interfaces
477
478 -- the form in which we pass update-analysis info between modules:
479 type UpdateSpec = [Int]
480 \end{code}
481
482 \begin{code}
483 mkUpdateInfo = SomeUpdateInfo
484
485 updateInfoMaybe NoUpdateInfo        = Nothing
486 updateInfoMaybe (SomeUpdateInfo []) = Nothing
487 updateInfoMaybe (SomeUpdateInfo  u) = Just u
488 \end{code}
489
490 Text instance so that the update annotations can be read in.
491
492 \begin{code}
493 ppUpdateInfo NoUpdateInfo          = empty
494 ppUpdateInfo (SomeUpdateInfo [])   = empty
495 ppUpdateInfo (SomeUpdateInfo spec) = (<>) (ptext SLIT("__UA ")) (hcat (map int spec))
496   -- was "__U "; changed to avoid conflict with unfoldings.  KSW 1999-07.
497 \end{code}
498
499 %************************************************************************
500 %*                                                                      *
501 \subsection[CAF-IdInfo]{CAF-related information}
502 %*                                                                      *
503 %************************************************************************
504
505 This information is used to build Static Reference Tables (see
506 simplStg/ComputeSRT.lhs).
507
508 \begin{code}
509 data CafInfo 
510         = MayHaveCafRefs                -- either:
511                                         -- (1) A function or static constructor
512                                         --     that refers to one or more CAFs,
513                                         -- (2) A real live CAF
514
515         | NoCafRefs                     -- A function or static constructor
516                                         -- that refers to no CAFs.
517
518 -- LATER: not sure how easy this is...
519 --      | OneCafRef Id
520
521
522 seqCaf c = c `seq` ()
523
524 ppCafInfo NoCafRefs = ptext SLIT("__C")
525 ppCafInfo MayHaveCafRefs = empty
526 \end{code}
527
528
529 %************************************************************************
530 %*                                                                      *
531 \subsection[cpr-IdInfo]{Constructed Product Result info about an @Id@}
532 %*                                                                      *
533 %************************************************************************
534
535 If the @Id@ is a function then it may have CPR info. A CPR analysis
536 phase detects whether:
537
538 \begin{enumerate}
539 \item
540 The function's return value has a product type, i.e. an algebraic  type 
541 with a single constructor. Examples of such types are tuples and boxed
542 primitive values.
543 \item
544 The function always 'constructs' the value that it is returning.  It
545 must do this on every path through,  and it's OK if it calls another
546 function which constructs the result.
547 \end{enumerate}
548
549 If this is the case then we store a template which tells us the
550 function has the CPR property and which components of the result are
551 also CPRs.   
552
553 \begin{code}
554 data CprInfo
555   = NoCPRInfo
556
557   | CPRInfo [CprInfo] 
558
559 -- e.g. const 5 == CPRInfo [NoCPRInfo]
560 --              == __M(-)
561 --      \x -> (5,
562 --              (x,
563 --               5,
564 --               x)
565 --            ) 
566 --            CPRInfo [CPRInfo [NoCPRInfo], 
567 --                     CPRInfo [NoCprInfo,
568 --                              CPRInfo [NoCPRInfo],
569 --                              NoCPRInfo]
570 --                    ]
571 --            __M((-)(-(-)-)-)
572 \end{code}
573
574 \begin{code}
575 seqCpr :: CprInfo -> ()
576 seqCpr (CPRInfo cs) = seqCprs cs
577 seqCpr NoCPRInfo    = ()
578
579 seqCprs [] = ()
580 seqCprs (c:cs) = seqCpr c `seq` seqCprs cs
581
582
583 noCprInfo       = NoCPRInfo
584
585 ppCprInfo NoCPRInfo = empty
586 ppCprInfo c@(CPRInfo _)
587   = hsep [ptext SLIT("__M"), ppCprInfo' c]
588     where
589     ppCprInfo' NoCPRInfo      = char '-'
590     ppCprInfo' (CPRInfo args) = parens (hcat (map ppCprInfo' args))
591
592 instance Outputable CprInfo where
593     ppr = ppCprInfo
594
595 instance Show CprInfo where
596     showsPrec p c = showsPrecSDoc p (ppr c)
597 \end{code}
598
599
600 %************************************************************************
601 %*                                                                      *
602 \subsection[lbvar-IdInfo]{Lambda-bound var info about an @Id@}
603 %*                                                                      *
604 %************************************************************************
605
606 If the @Id@ is a lambda-bound variable then it may have lambda-bound
607 var info.  The usage analysis (UsageSP) detects whether the lambda
608 binding this var is a ``one-shot'' lambda; that is, whether it is
609 applied at most once.
610
611 This information may be useful in optimisation, as computations may
612 safely be floated inside such a lambda without risk of duplicating
613 work.
614
615 \begin{code}
616 data LBVarInfo
617   = NoLBVarInfo
618
619   | IsOneShotLambda             -- The lambda that binds this Id is applied
620                                 --   at most once
621                                 -- HACK ALERT! placing this info here is a short-term hack,
622                                 --   but it minimises changes to the rest of the compiler.
623                                 --   Hack agreed by SLPJ/KSW 1999-04.
624
625 seqLBVar l = l `seq` ()
626 \end{code}
627
628 \begin{code}
629 noLBVarInfo = NoLBVarInfo
630
631 -- not safe to print or parse LBVarInfo because it is not really a
632 -- property of the definition, but a property of the context.
633 pprLBVarInfo NoLBVarInfo     = empty
634 pprLBVarInfo IsOneShotLambda = getPprStyle $ \ sty ->
635                                if ifaceStyle sty then empty
636                                                  else ptext SLIT("OneShot")
637
638 instance Outputable LBVarInfo where
639     ppr = pprLBVarInfo
640
641 instance Show LBVarInfo where
642     showsPrec p c = showsPrecSDoc p (ppr c)
643 \end{code}
644
645
646 %************************************************************************
647 %*                                                                      *
648 \subsection{Bulk operations on IdInfo}
649 %*                                                                      *
650 %************************************************************************
651
652 zapFragileInfo is used when cloning binders, mainly in the
653 simplifier.  We must forget about used-once information because that
654 isn't necessarily correct in the transformed program.
655 Also forget specialisations and unfoldings because they would need
656 substitution to be correct.  (They get pinned back on separately.)
657
658 \begin{code}
659 zapFragileInfo :: IdInfo -> Maybe IdInfo
660 zapFragileInfo info@(IdInfo {occInfo            = occ, 
661                              workerInfo         = wrkr,
662                              specInfo           = rules, 
663                              unfoldingInfo      = unfolding})
664   |  not (isFragileOccInfo occ)
665         -- We must forget about whether it was marked safe-to-inline,
666         -- because that isn't necessarily true in the simplified expression.
667         -- This is important because expressions may  be re-simplified
668         -- We don't zap deadness or loop-breaker-ness.
669         -- The latter is important because it tells MkIface not to 
670         -- spit out an inlining for the thing.  The former doesn't
671         -- seem so important, but there's no harm.
672
673   && isEmptyCoreRules rules
674         -- Specialisations would need substituting.  They get pinned
675         -- back on separately.
676
677   && not (workerExists wrkr)
678
679   && not (hasUnfolding unfolding)
680         -- This is very important; occasionally a let-bound binder is used
681         -- as a binder in some lambda, in which case its unfolding is utterly
682         -- bogus.  Also the unfolding uses old binders so if we left it we'd
683         -- have to substitute it. Much better simply to give the Id a new
684         -- unfolding each time, which is what the simplifier does.
685   = Nothing
686
687   | otherwise
688   = Just (info {occInfo         = robust_occ_info,
689                 workerInfo      = noWorkerInfo,
690                 specInfo        = emptyCoreRules,
691                 unfoldingInfo   = noUnfolding})
692   where
693         -- It's important to keep the loop-breaker info,
694         -- because the substitution doesn't remember it.
695     robust_occ_info = case occ of
696                         OneOcc _ _ -> NoOccInfo
697                         other      -> occ
698 \end{code}
699
700 @zapLamInfo@ is used for lambda binders that turn out to to be
701 part of an unsaturated lambda
702
703 \begin{code}
704 zapLamInfo :: IdInfo -> Maybe IdInfo
705 zapLamInfo info@(IdInfo {occInfo = occ, demandInfo = demand})
706   | is_safe_occ && not (isStrict demand)
707   = Nothing
708   | otherwise
709   = Just (info {occInfo = safe_occ,
710                 demandInfo = wwLazy})
711   where
712         -- The "unsafe" occ info is the ones that say I'm not in a lambda
713         -- because that might not be true for an unsaturated lambda
714     is_safe_occ = case occ of
715                         OneOcc in_lam once -> in_lam
716                         other              -> True
717
718     safe_occ = case occ of
719                  OneOcc _ once -> OneOcc insideLam once
720                  other         -> occ
721 \end{code}
722
723
724 copyIdInfo is used when shorting out a top-level binding
725         f_local = BIG
726         f = f_local
727 where f is exported.  We are going to swizzle it around to
728         f = BIG
729         f_local = f
730 but we must be careful to combine their IdInfos right.
731 The fact that things can go wrong here is a bad sign, but I can't see
732 how to make it 'patently right', so copyIdInfo is derived (pretty much) by trial and error
733
734 Here 'from' is f_local, 'to' is f, and the result is attached to f
735
736 \begin{code}
737 copyIdInfo :: IdInfo    -- From
738            -> IdInfo    -- To
739            -> IdInfo    -- To, updated with stuff from From; except flavour unchanged
740 copyIdInfo from to = from { flavourInfo = flavourInfo to,
741                             specInfo = specInfo to,
742                             inlinePragInfo = inlinePragInfo to
743                           }
744         -- It's important to preserve the inline pragma on 'f'; e.g. consider
745         --      {-# NOINLINE f #-}
746         --      f = local
747         --
748         -- similarly, transformation rules may be attached to f
749         -- and we want to preserve them.  
750         --
751         -- On the other hand, we want the strictness info from f_local.
752 \end{code}