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