[project @ 1996-12-19 09:10:02 by simonpj]
[ghc-hetmet.git] / ghc / compiler / simplCore / OccurAnal.lhs
index 8a91871..3ed4f73 100644 (file)
@@ -25,7 +25,6 @@ import CmdLineOpts    ( opt_D_dump_occur_anal, SimplifierSwitch(..) )
 import CoreSyn
 import Digraph         ( stronglyConnComp )
 import Id              ( idWantsToBeINLINEd, isConstMethodId,
-                         externallyVisibleId,
                          emptyIdSet, unionIdSets, mkIdSet,
                          unitIdSet, elementOfIdSet,
                          addOneToIdSet, SYN_IE(IdSet),
@@ -34,6 +33,7 @@ import Id             ( idWantsToBeINLINEd, isConstMethodId,
                          mapIdEnv, lookupIdEnv, SYN_IE(IdEnv),
                          GenId{-instance Eq-}
                        )
+import Name            ( isExported )
 import Maybes          ( maybeToBool )
 import Outputable      ( Outputable(..){-instance * (,) -} )
 import PprCore
@@ -44,7 +44,7 @@ import TyVar          ( GenTyVar{-instance Eq-} )
 import Unique          ( Unique{-instance Eq-} )
 import Util            ( assoc, zipEqual, pprTrace, panic )
 
-isSpecPragmaId_maybe = panic "OccurAnal.isSpecPragmaId_maybe (ToDo)"
+isSpecPragmaId_maybe x = Nothing -- ToDo:!trace "OccurAnal.isSpecPragmaId_maybe"
 \end{code}
 
 
@@ -138,7 +138,7 @@ tagBinder usage binder
     )
 
 usage_of usage binder
-  | externallyVisibleId binder = ManyOcc 0 -- Visible-elsewhere things count as many
+  | isExported binder = ManyOcc 0 -- Visible-elsewhere things count as many
   | otherwise
   = case (lookupIdEnv usage binder) of
       Nothing   -> DeadCode
@@ -380,8 +380,27 @@ occAnal env (Var v)
   = (emptyDetails, Var v)
 
 occAnal env (Lit lit)     = (emptyDetails, Lit lit)
-occAnal env (Con con args) = (occAnalArgs env args, Con con args)
 occAnal env (Prim op args) = (occAnalArgs env args, Prim op args)
+\end{code}
+
+We regard variables that occur as constructor arguments as "dangerousToDup":
+
+\begin{verbatim}
+module A where
+f x = let y = expensive x in 
+      let z = (True,y) in 
+      (case z of {(p,q)->q}, case z of {(p,q)->q})
+\end{verbatim}
+
+We feel free to duplicate the WHNF (True,y), but that means
+that y may be duplicated thereby.
+
+If we aren't careful we duplicate the (expensive x) call!
+Constructors are rather like lambdas in this way.
+
+\begin{code}
+occAnal env (Con con args) = (mapIdEnv markDangerousToDup (occAnalArgs env args), 
+                             Con con args)
 
 occAnal env (SCC cc body)
   = (mapIdEnv markInsideSCC usage, SCC cc body')
@@ -399,12 +418,21 @@ occAnal env (App fun arg)
     (fun_usage, fun') = occAnal    env fun
     arg_usage        = occAnalArg env arg
 
-occAnal env (Lam (ValBinder binder) body)
+-- For value lambdas we do a special hack.  Consider
+--     (\x. \y. ...x...)
+-- If we did nothing, x is used inside the \y, so would be marked
+-- as dangerous to dup.  But in the common case where the abstraction
+-- is applied to two arguments this is over-pessimistic.
+-- So instead we don't take account of the \y when dealing with x's usage;
+-- instead, the simplifier is careful when partially applying lambdas
+
+occAnal env expr@(Lam (ValBinder binder) body)
   = (mapIdEnv markDangerousToDup final_usage,
-     Lam (ValBinder tagged_binder) body')
+     foldr ( \v b -> Lam (ValBinder v) b) body' tagged_binders)
   where
-    (body_usage, body')         = occAnal (env `addNewCand` binder) body
-    (final_usage, tagged_binder) = tagBinder body_usage binder
+    (binders,body)               = collectValBinders expr
+    (body_usage, body')          = occAnal (env `addNewCands` binders) body
+    (final_usage, tagged_binders) = tagBinders body_usage binders
 
 -- ANDY: WE MUST THINK ABOUT THIS! (ToDo)
 occAnal env (Lam (TyBinder tyvar) body)