[project @ 1998-05-04 13:24:42 by simonpj]
[ghc-hetmet.git] / ghc / compiler / simplCore / SimplUtils.lhs
index ba1cc4e..8856a64 100644 (file)
@@ -4,47 +4,81 @@
 \section[SimplUtils]{The simplifier utilities}
 
 \begin{code}
-#include "HsVersions.h"
-
 module SimplUtils (
 
+       newId, newIds,
+
        floatExposesHNF,
 
-       mkTyLamTryingEta, mkValLamTryingEta,
+       etaCoreExpr, mkRhsTyLam,
 
        etaExpandCount,
 
-       mkIdentityAlts,
-
        simplIdWantsToBeINLINEd,
 
-       type_ok_for_let_to_case
+       singleConstructorType, typeOkForCase,
+
+       substSpecEnvRhs
     ) where
 
-import Ubiq{-uitous-}
+#include "HsVersions.h"
 
 import BinderInfo
-import CmdLineOpts     ( SimplifierSwitch(..) )
+import CmdLineOpts     ( opt_DoEtaReduction, SimplifierSwitch(..) )
 import CoreSyn
-import CoreUtils       ( manifestlyWHNF )
-import Id              ( idType, isBottomingId, idWantsToBeINLINEd, dataConArgTys,
-                         getIdArity, GenId{-instance Eq-}
+import CoreUnfold      ( mkFormSummary, exprIsTrivial, FormSummary(..) )
+import MkId            ( mkSysLocal )
+import Id              ( idType, isBottomingId, getIdArity,
+                         addInlinePragma, addIdDemandInfo,
+                         idWantsToBeINLINEd, dataConArgTys, Id,
+                         lookupIdEnv, delOneFromIdEnv
                        )
-import IdInfo          ( arityMaybe )
+import IdInfo          ( ArityInfo(..), DemandInfo )
 import Maybes          ( maybeToBool )
-import PrelInfo                ( augmentId, buildId, realWorldStateTy )
+import PrelVals                ( augmentId, buildId )
 import PrimOp          ( primOpIsCheap )
 import SimplEnv
 import SimplMonad
-import Type            ( eqTy, isPrimType, maybeAppDataTyConExpandingDicts, getTyVar_maybe )
-import TyVar           ( GenTyVar{-instance Eq-} )
-import Util            ( isIn, panic )
+import Type            ( tyVarsOfType, tyVarsOfTypes, mkForAllTys, mkTyVarTys, getTyVar_maybe,
+                         splitAlgTyConApp_maybe, instantiateTy, Type
+                       )
+import TyCon           ( isDataTyCon )
+import TyVar           ( mkTyVarSet, intersectTyVarSets, elementOfTyVarSet, tyVarSetToList,
+                         delFromTyVarEnv
+                       )
+import SrcLoc          ( noSrcLoc )
+import Util            ( isIn, zipWithEqual, panic, assertPanic )
+
+\end{code}
+
 
+%************************************************************************
+%*                                                                     *
+\subsection{New ids}
+%*                                                                     *
+%************************************************************************
+
+\begin{code}
+newId :: Type -> SmplM Id
+newId ty
+  = getUniqueSmpl     `thenSmpl`  \ uniq ->
+    returnSmpl (mkSysLocal SLIT("s") uniq ty noSrcLoc)
+
+newIds :: [Type] -> SmplM [Id]
+newIds tys
+  = getUniquesSmpl (length tys)    `thenSmpl`  \ uniqs ->
+    returnSmpl (zipWithEqual "newIds" mk_id tys uniqs)
+  where
+    mk_id ty uniq = mkSysLocal SLIT("s") uniq ty noSrcLoc
 \end{code}
 
 
-Floating
-~~~~~~~~
+%************************************************************************
+%*                                                                     *
+\subsection{Floating}
+%*                                                                     *
+%************************************************************************
+
 The function @floatExposesHNF@ tells whether let/case floating will
 expose a head normal form.  It is passed booleans indicating the
 desired strategy.
@@ -53,15 +87,14 @@ desired strategy.
 floatExposesHNF
        :: Bool                 -- Float let(rec)s out of rhs
        -> Bool                 -- Float cheap primops out of rhs
-       -> Bool                 -- OK to duplicate code
-       -> GenCoreExpr bdr Id tyvar uvar
+       -> GenCoreExpr bdr Id flexi
        -> Bool
 
-floatExposesHNF float_lets float_primops ok_to_dup rhs
+floatExposesHNF float_lets float_primops rhs
   = try rhs
   where
     try (Case (Prim _ _) (PrimAlts alts deflt) )
-      | float_primops && (null alts || ok_to_dup)
+      | float_primops && null alts
       = or (try_deflt deflt : map try_alt alts)
 
     try (Let bind body) | float_lets = try body
@@ -74,8 +107,11 @@ floatExposesHNF float_lets float_primops ok_to_dup rhs
     try (App (App (Var bld) _) _)        | bld == buildId   = True
     try (App (App (App (Var aug) _) _) _) | aug == augmentId = True
 
-    try other = manifestlyWHNF other
-       {- but *not* necessarily "manifestlyBottom other"...
+    try other = case mkFormSummary other of
+                       VarForm   -> True
+                       ValueForm -> True
+                       other     -> False
+       {- but *not* necessarily "BottomForm"...
 
           We may want to float a let out of a let to expose WHNFs,
            but to do that to expose a "bottom" is a Bad Idea:
@@ -98,11 +134,114 @@ floatExposesHNF float_lets float_primops ok_to_dup rhs
 \end{code}
 
 
-Eta reduction on ordinary lambdas
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-We have a go at doing
+Local tyvar-lifting
+~~~~~~~~~~~~~~~~~~~
+mkRhsTyLam tries this transformation, when the big lambda appears as
+the RHS of a let(rec) binding:
+
+       /\abc -> let(rec) x = e in b
+   ==>
+       let(rec) x' = /\abc -> let x = x' a b c in e
+       in 
+       /\abc -> let x = x' a b c in b
+
+This is good because it can turn things like:
+
+       let f = /\a -> letrec g = ... g ... in g
+into
+       letrec g' = /\a -> ... g' a ...
+       in
+       let f = /\ a -> f a
+
+which is better.  In effect, it means that big lambdas don't impede
+let-floating.
+
+This optimisation is CRUCIAL in eliminating the junk introduced by
+desugaring mutually recursive definitions.  Don't eliminate it lightly!
+
+So far as the implemtation is concerned:
+
+       Invariant: go F e = /\tvs -> F e
+       
+       Equalities:
+               go F (Let x=e in b)
+               = Let x' = /\tvs -> F e 
+                 in 
+                 go G b
+               where
+                   G = F . Let x = x' tvs
+       
+               go F (Letrec xi=ei in b)
+               = Letrec {xi' = /\tvs -> G ei} 
+                 in
+                 go G b
+               where
+                 G = F . Let {xi = xi' tvs}
+
+\begin{code}
+mkRhsTyLam [] body = returnSmpl body
 
-       \ x y -> f x y  ===>  f
+mkRhsTyLam tyvars body
+  = go (\x -> x) body
+  where
+    main_tyvar_set = mkTyVarSet tyvars
+
+    go fn (Let bind@(NonRec var rhs) body) | exprIsTrivial rhs
+      = go (fn . Let bind) body
+
+    go fn (Let bind@(NonRec var rhs) body)
+      = mk_poly tyvars_here var_ty                     `thenSmpl` \ (var', rhs') ->
+       go (fn . Let (mk_silly_bind var rhs')) body     `thenSmpl` \ body' ->
+       returnSmpl (Let (NonRec var' (mkTyLam tyvars_here (fn rhs))) body')
+      where
+       tyvars_here = tyVarSetToList (main_tyvar_set `intersectTyVarSets` tyVarsOfType var_ty)
+       var_ty = idType var
+
+    go fn (Let (Rec prs) body)
+       = mapAndUnzipSmpl (mk_poly tyvars_here) var_tys `thenSmpl` \ (vars', rhss') ->
+        let
+           gn body = fn $ foldr Let body (zipWith mk_silly_bind vars rhss')
+        in
+        go gn body                             `thenSmpl` \ body' ->
+        returnSmpl (Let (Rec (vars' `zip` [mkTyLam tyvars_here (gn rhs) | rhs <- rhss])) body')
+       where
+        (vars,rhss) = unzip prs
+        tyvars_here = tyVarSetToList (main_tyvar_set `intersectTyVarSets` tyVarsOfTypes var_tys)
+        var_tys     = map idType vars
+
+    go fn body = returnSmpl (mkTyLam tyvars (fn body))
+
+    mk_poly tyvars_here var_ty
+      = newId (mkForAllTys tyvars_here var_ty) `thenSmpl` \ poly_id ->
+       returnSmpl (poly_id, mkTyApp (Var poly_id) (mkTyVarTys tyvars_here))
+
+    mk_silly_bind var rhs = NonRec (addInlinePragma var) rhs
+               -- The addInlinePragma is really important!  If we don't say 
+               -- INLINE on these silly little bindings then look what happens!
+               -- Suppose we start with:
+               --
+               --      x = let g = /\a -> \x -> f x x
+               --          in 
+               --          /\ b -> let g* = g b in E
+               --
+               -- Then:        * the binding for g gets floated out
+               --              * but then it gets inlined into the rhs of g*
+               --              * then the binding for g* is floated out of the /\b
+               --              * so we're back to square one
+               -- The silly binding for g* must be INLINE, so that no inlining
+               -- will happen in its RHS.
+\end{code}
+
+Eta reduction
+~~~~~~~~~~~~~
+@etaCoreExpr@ trys an eta reduction at the top level of a Core Expr.
+
+e.g.   \ x y -> f x y  ===>  f
+
+It is used
+       a) Before constructing an Unfolding, to 
+          try to make the unfolding smaller;
+       b) In tidyCoreExpr, which is done just before converting to STG.
 
 But we only do this if it gets rid of a whole lambda, not part.
 The idea is that lambdas are often quite helpful: they indicate
@@ -118,43 +257,82 @@ It does arise:
 gives rise to a recursive function for the list comprehension, and
 f turns out to be just a single call to this recursive function.
 
-\begin{code}
-mkValLamTryingEta :: [Id]              -- Args to the lambda
-              -> CoreExpr              -- Lambda body
-              -> CoreExpr
+Doing eta on type lambdas is useful too:
 
-mkValLamTryingEta [] body = body
+       /\a -> <expr> a    ===>     <expr>
 
-mkValLamTryingEta orig_ids body
-  = reduce_it (reverse orig_ids) body
-  where
-    bale_out = mkValLam orig_ids body
+where <expr> doesn't mention a.
+This is sometimes quite useful, because we can get the sequence:
+
+       f ab d = let d1 = ...d... in
+                letrec f' b x = ...d...(f' b)... in
+                f' b
+specialise ==>
+
+       f.Int b = letrec f' b x = ...dInt...(f' b)... in
+                 f' b
+
+float ==>
+
+       f' b x = ...dInt...(f' b)...
+       f.Int b = f' b
 
-    reduce_it [] residual
-      | residual_ok residual = residual
-      | otherwise           = bale_out
+Now we really want to simplify to
 
-    reduce_it (id:ids) (App fun (VarArg arg))
-      | id == arg
-      && not (idType id `eqTy` realWorldStateTy)
-        -- *never* eta-reduce away a PrimIO state token! (WDP 94/11)
-      = reduce_it ids fun
+       f.Int = f'
 
-    reduce_it ids other = bale_out
+and then replace all the f's with f.Ints.
+
+N.B. We are careful not to partially eta-reduce a sequence of type
+applications since this breaks the specialiser:
 
-    is_elem = isIn "mkValLamTryingEta"
+       /\ a -> f Char# a       =NO=> f Char#
+
+\begin{code}
+etaCoreExpr :: CoreExpr -> CoreExpr
+
+
+etaCoreExpr expr@(Lam bndr body)
+  | opt_DoEtaReduction
+  = case etaCoreExpr body of
+       App fun arg | eta_match bndr arg &&
+                     residual_ok fun
+                   -> fun                      -- Eta
+       other       -> expr                     -- Can't eliminate it, so do nothing at all
+  where
+    eta_match (ValBinder v) (VarArg v') = v == v'
+    eta_match (TyBinder tv) (TyArg  ty) = case getTyVar_maybe ty of
+                                               Nothing  -> False
+                                               Just tv' -> tv == tv'
+    eta_match bndr         arg         = False
 
-    -----------
     residual_ok :: CoreExpr -> Bool    -- Checks for type application
                                        -- and function not one of the
                                        -- bound vars
 
-    residual_ok (Var v)        = not (v `is_elem` orig_ids)
-                         -- Fun mustn't be one of the bound ids
+    (VarArg v) `mentions` (ValBinder v') = v == v'
+    (TyArg ty) `mentions` (TyBinder tv)  = tv `elementOfTyVarSet` tyVarsOfType ty
+    bndr       `mentions` arg           = False
+
+    residual_ok (Var v)
+       = not (VarArg v `mentions` bndr)
     residual_ok (App fun arg)
-      | notValArg arg  = residual_ok fun
-    residual_ok other  = False
+       | arg `mentions` bndr = False
+       | otherwise           = residual_ok fun
+    residual_ok (Note (Coerce to_ty from_ty) body)
+       |  TyArg to_ty   `mentions` bndr 
+       || TyArg from_ty `mentions` bndr = False
+       | otherwise                      = residual_ok body
+
+    residual_ok other       = False            -- Safe answer
+       -- This last clause may seem conservative, but consider:
+       --      primops, constructors, and literals, are impossible here
+       --      let and case are unlikely (the argument would have been floated inside)
+       --      SCCs we probably want to be conservative about (not sure, but it's safe to be)
+       
+etaCoreExpr expr = expr                -- The common case
 \end{code}
+       
 
 Eta expansion
 ~~~~~~~~~~~~~
@@ -175,7 +353,7 @@ arguments as you care to give it.  For this special case we return
 100, to represent "infinity", which is a bit of a hack.
 
 \begin{code}
-etaExpandCount :: GenCoreExpr bdr Id tyvar uvar
+etaExpandCount :: GenCoreExpr bdr Id flexi
               -> Int   -- Number of extra args you can safely abstract
 
 etaExpandCount (Lam (ValBinder _) body)
@@ -204,7 +382,7 @@ etaExpandCount other = 0    -- Give up
        -- Case with non-whnf scrutinee
 
 -----------------------------
-eta_fun :: GenCoreExpr bdr Id tv uv -- The function
+eta_fun :: GenCoreExpr bdr Id flexi -- The function
        -> Int                      -- How many args it can safely be applied to
 
 eta_fun (App fun arg) | notValArg arg = eta_fun fun
@@ -213,12 +391,7 @@ eta_fun expr@(Var v)
   | isBottomingId v            -- Bottoming ids have "infinite arity"
   = 10000                      -- Blargh.  Infinite enough!
 
-eta_fun expr@(Var v)
-  | maybeToBool arity_maybe    -- We know the arity
-  = arity
-  where
-    arity_maybe = arityMaybe (getIdArity v)
-    arity      = case arity_maybe of { Just arity -> arity }
+eta_fun expr@(Var v) = idMinArity v
 
 eta_fun other = 0              -- Give up
 \end{code}
@@ -244,15 +417,14 @@ which aren't WHNF but are ``cheap'' are:
        where op is a cheap primitive operator
 
 \begin{code}
-manifestlyCheap :: GenCoreExpr bndr Id tv uv -> Bool
+manifestlyCheap :: GenCoreExpr bndr Id flexi -> Bool
 
-manifestlyCheap (Var _)        = True
-manifestlyCheap (Lit _)        = True
-manifestlyCheap (Con _ _)      = True
-manifestlyCheap (SCC _ e)      = manifestlyCheap e
-manifestlyCheap (Coerce _ _ e) = manifestlyCheap e
-manifestlyCheap (Lam x e)      = if isValBinder x then True else manifestlyCheap e
-manifestlyCheap (Prim op _)    = primOpIsCheap op
+manifestlyCheap (Var _)      = True
+manifestlyCheap (Lit _)      = True
+manifestlyCheap (Con _ _)    = True
+manifestlyCheap (Note _ e)   = manifestlyCheap e
+manifestlyCheap (Lam x e)    = if isValBinder x then True else manifestlyCheap e
+manifestlyCheap (Prim op _)  = primOpIsCheap op
 
 manifestlyCheap (Let bind body)
   = manifestlyCheap body && all manifestlyCheap (rhssOfBind bind)
@@ -261,7 +433,7 @@ manifestlyCheap (Case scrut alts)
   = manifestlyCheap scrut && all manifestlyCheap (rhssOfAlts alts)
 
 manifestlyCheap other_expr   -- look for manifest partial application
-  = case (collectArgs other_expr) of { (fun, _, _, vargs) ->
+  = case (collectArgs other_expr) of { (fun, _, vargs) ->
     case fun of
 
       Var f | isBottomingId f -> True  -- Application of a function which
@@ -275,139 +447,92 @@ manifestlyCheap other_expr   -- look for manifest partial application
               num_val_args == 0 ||     -- Just a type application of
                                        -- a variable (f t1 t2 t3)
                                        -- counts as WHNF
-              case (arityMaybe (getIdArity f)) of
-                Nothing     -> False
-                Just arity  -> num_val_args < arity
+              num_val_args < idMinArity f
 
       _ -> False
     }
-\end{code}
-
-Eta reduction on type lambdas
-~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
-We have a go at doing
-
-       /\a -> <expr> a    ===>     <expr>
-
-where <expr> doesn't mention a.
-This is sometimes quite useful, because we can get the sequence:
-
-       f ab d = let d1 = ...d... in
-                letrec f' b x = ...d...(f' b)... in
-                f' b
-specialise ==>
-
-       f.Int b = letrec f' b x = ...dInt...(f' b)... in
-                 f' b
-
-float ==>
-
-       f' b x = ...dInt...(f' b)...
-       f.Int b = f' b
-
-Now we really want to simplify to
-
-       f.Int = f'
 
-and then replace all the f's with f.Ints.
-
-N.B. We are careful not to partially eta-reduce a sequence of type
-applications since this breaks the specialiser:
+\end{code}
 
-       /\ a -> f Char# a       =NO=> f Char#
 
 \begin{code}
-mkTyLamTryingEta :: [TyVar] -> CoreExpr -> CoreExpr
-
-mkTyLamTryingEta tyvars tylam_body
-  = if
-       tyvars == tyvar_args && -- Same args in same order
-       check_fun fun           -- Function left is ok
-    then
-       -- Eta reduction worked
-       fun
-    else
-       -- The vastly common case
-       mkTyLam tyvars tylam_body
-  where
-    (tyvar_args, fun) = strip_tyvar_args [] tylam_body
-
-    strip_tyvar_args args_so_far tyapp@(App fun (TyArg ty))
-      = case getTyVar_maybe ty of
-         Just tyvar_arg -> strip_tyvar_args (tyvar_arg:args_so_far) fun
-         Nothing        -> (args_so_far, tyapp)
-
-    strip_tyvar_args args_so_far (App _ (UsageArg _))
-      = panic "SimplUtils.mkTyLamTryingEta: strip_tyvar_args UsageArg"
-
-    strip_tyvar_args args_so_far fun
-      = (args_so_far, fun)
+simplIdWantsToBeINLINEd :: Id -> SimplEnv -> Bool
 
-    check_fun (Var f) = True    -- Claim: tyvars not mentioned by type of f
-    check_fun other     = False
+simplIdWantsToBeINLINEd id env
+  = {- We used to arrange that in the final simplification pass we'd switch
+       off all INLINE pragmas, so that we'd inline workers back into the
+       body of their wrapper if the wrapper hadn't itself been inlined by then.
+       This occurred especially for methods in dictionaries.
+
+       We no longer do this:
+               a) there's a good chance that the exported wrapper will get
+               inlined in some importing scope, in which case we don't 
+               want to lose the w/w idea.
+
+               b) The occurrence analyser must agree about what has an
+               INLINE pragma.  Not hard, but delicate.
+       
+               c) if the worker gets inlined we have to tell the wrapepr
+               that it's no longer a wrapper, else the interface file stuff
+               asks for a worker that no longer exists.
+                 
+    if switchIsSet env IgnoreINLINEPragma
+    then False
+    else 
+    -}
+
+    idWantsToBeINLINEd id
+
+idMinArity id = case getIdArity id of
+                       UnknownArity   -> 0
+                       ArityAtLeast n -> n
+                       ArityExactly n -> n
+
+singleConstructorType :: Type -> Bool
+singleConstructorType ty
+  = case (splitAlgTyConApp_maybe ty) of
+      Just (tycon, ty_args, [con]) | isDataTyCon tycon -> True
+      other                                           -> False
+
+typeOkForCase :: Type -> Bool
+typeOkForCase ty
+  = case (splitAlgTyConApp_maybe ty) of
+      Just (tycon, ty_args, [])                                    -> False
+      Just (tycon, ty_args, non_null_data_cons) | isDataTyCon tycon -> True
+      other                                                        -> False
+      -- Null data cons => type is abstract, which code gen can't 
+      -- currently handle.  (ToDo: when return-in-heap is universal we
+      -- don't need to worry about this.)
 \end{code}
 
-Let to case
-~~~~~~~~~~~
 
-Given a type generate the case alternatives
-
-       C a b -> C a b
-
-if there's one constructor, or
-
-       x -> x
-
-if there's many, or if it's a primitive type.
 
+substSpecEnvRhs applies a substitution to the RHS's of a SpecEnv
+It exploits the known structure of a SpecEnv's RHS to have fewer
+equations.
 
 \begin{code}
-mkIdentityAlts
-       :: Type         -- type of RHS
-       -> SmplM InAlts         -- result
-
-mkIdentityAlts rhs_ty
-  | isPrimType rhs_ty
-  = newId rhs_ty       `thenSmpl` \ binder ->
-    returnSmpl (PrimAlts [] (BindDefault (binder, bad_occ_info) (Var binder)))
-
-  | otherwise
-  = case (maybeAppDataTyConExpandingDicts rhs_ty) of
-       Just (tycon, ty_args, [data_con]) ->  -- algebraic type suitable for unpacking
-           let
-               inst_con_arg_tys = dataConArgTys data_con ty_args
-           in
-           newIds inst_con_arg_tys     `thenSmpl` \ new_bindees ->
-           let
-               new_binders = [ (b, bad_occ_info) | b <- new_bindees ]
-           in
-           returnSmpl (
-             AlgAlts
-               [(data_con, new_binders, mkCon data_con [] ty_args (map VarArg new_bindees))]
-               NoDefault
-           )
-
-       _ -> -- Multi-constructor or abstract algebraic type
-            newId rhs_ty       `thenSmpl` \ binder ->
-            returnSmpl (AlgAlts [] (BindDefault (binder,bad_occ_info) (Var binder)))
+substSpecEnvRhs te ve rhs
+  = go te ve rhs
   where
-    bad_occ_info = ManyOcc 0   -- Non-committal!
-\end{code}
-
-\begin{code}
-simplIdWantsToBeINLINEd :: Id -> SimplEnv -> Bool
-
-simplIdWantsToBeINLINEd id env
-  = if switchIsSet env IgnoreINLINEPragma
-    then False
-    else idWantsToBeINLINEd id
-
-type_ok_for_let_to_case :: Type -> Bool
-
-type_ok_for_let_to_case ty
-  = case (maybeAppDataTyConExpandingDicts ty) of
-      Nothing                                   -> False
-      Just (tycon, ty_args, [])                 -> False
-      Just (tycon, ty_args, non_null_data_cons) -> True
-      -- Null data cons => type is abstract
+    go te ve (App f (TyArg ty)) = App (go te ve f) (TyArg (instantiateTy te ty))
+    go te ve (App f (VarArg v)) = App (go te ve f) (case lookupIdEnv ve v of
+                                                       Just (SubstVar v') -> VarArg v'
+                                                       Just (SubstLit l)  -> LitArg l
+                                                       Nothing            -> VarArg v)
+    go te ve (Var v)             = case lookupIdEnv ve v of
+                                               Just (SubstVar v') -> Var v'
+                                               Just (SubstLit l)  -> Lit l
+                                               Nothing            -> Var v
+
+       -- These equations are a bit half baked, because
+       -- they don't deal properly wih capture.
+       -- But I'm sure it'll never matter... sigh.
+    go te ve (Lam b@(TyBinder tyvar) e) = Lam b (go te' ve e)
+                                       where
+                                         te' = delFromTyVarEnv te tyvar
+
+    go te ve (Lam b@(ValBinder v) e) = Lam b (go te ve' e)
+                                    where
+                                      ve' = delOneFromIdEnv ve v
 \end{code}