[project @ 2000-12-20 18:32:00 by qrczak]
[ghc-hetmet.git] / ghc / compiler / coreSyn / CoreSat.lhs
index 900f24f..0544875 100644 (file)
@@ -18,14 +18,15 @@ import Type
 import Demand
 import Var     ( TyVar, setTyVarUnique )
 import VarSet
-import PrimOp
 import IdInfo
 import Id
+import PrimOp
 import UniqSupply
 import Maybes
 import ErrUtils
 import CmdLineOpts
 import Outputable
+import PprCore
 \end{code}
 
 -- ---------------------------------------------------------------------------
@@ -47,7 +48,7 @@ primary goals here are:
      simplifier, but it's better done here.  It does mean that f needs
      to have its strictness info correct!.]
 
-2.  Similarly, convert any unboxed let's into cases.
+2.  Similarly, convert any unboxed lets into cases.
     [I'm experimenting with leaving 'ok-for-speculation' rhss in let-form
      right up to this point.]
 
@@ -107,9 +108,9 @@ coreSatBinds (b:bs)
 
                   mkBinds floats rhs           `thenUs` \ new_rhs ->
                   returnUs (NonRec bndr new_rhs : new_bs)
-                                       -- Keep all the floats inside...
-                                       -- Some might be cases etc
-                                       -- We might want to revisit this decision
+                               -- Keep all the floats inside...
+                               -- Some might be cases etc
+                               -- We might want to revisit this decision
 
        RecF prs -> returnUs (Rec prs : new_bs)
 
@@ -199,14 +200,19 @@ coreSatExprFloat expr@(App _ _)
 
        -- Now deal with the function
     case head of
-      Var fn_id
-        -> maybeSaturate fn_id app depth ty `thenUs` \ app' -> 
-           returnUs (floats, app')
-      _other
-        -> returnUs (floats, app)
+      Var fn_id -> maybeSaturate fn_id app depth ty `thenUs` \ app' -> 
+                  returnUs (floats, app')
+
+      _other    -> returnUs (floats, app)
 
   where
 
+    -- Deconstruct and rebuild the application, floating any non-atomic
+    -- arguments to the outside.  We collect the type of the expression,
+    -- the head of the applicaiton, and the number of actual value arguments,
+    -- all of which are used to possibly saturate this application if it
+    -- has a constructor or primop at the head.
+
     collect_args
        :: CoreExpr
        -> Int                          -- current app depth
@@ -288,64 +294,23 @@ cloneTyVar tv
 -- Building the saturated syntax
 -- ---------------------------------------------------------------------------
 
+-- maybeSaturate deals with saturating primops and constructors
+-- The type is the type of the entire application
 maybeSaturate :: Id -> CoreExpr -> Int -> Type -> UniqSM CoreExpr
-       -- mkApp deals with saturating primops and constructors
-       -- The type is the type of the entire application
 maybeSaturate fn expr n_args ty
- = case idFlavour fn of
-      PrimOpId (CCallOp ccall)
-               -- Sigh...make a guaranteed unique name for a dynamic ccall
-               -- Done here, not earlier, because it's a code-gen thing
-       -> getUniqueUs                  `thenUs` \ uniq ->
-           let 
-            flavour = PrimOpId (CCallOp (setCCallUnique ccall uniq))
-            fn' = modifyIdInfo (`setFlavourInfo` flavour) fn
-          in
-          saturate fn' expr n_args ty
-          
-      PrimOpId op  -> saturate fn expr n_args ty
-      DataConId dc -> saturate fn expr n_args ty
+  = case idFlavour fn of
+      PrimOpId op  -> saturate_it
+      DataConId dc -> saturate_it
       other       -> returnUs expr
-
-saturate :: Id -> CoreExpr -> Int -> Type -> UniqSM CoreExpr
-       -- The type should be the type of (id args)
-       -- The returned expression should also have this type
-saturate fn expr n_args ty
-  = go excess_arity expr ty
   where
     fn_arity    = idArity fn
     excess_arity = fn_arity - n_args
+    saturate_it  = getUs       `thenUs` \ us ->
+                  returnUs (etaExpand excess_arity us expr ty)
 
-    go n expr ty
-      | n == 0 -- Saturated, so nothing to do
-      = returnUs expr
-
-      | otherwise      -- An unsaturated constructor or primop; eta expand it
-      = case splitForAllTy_maybe ty of { 
-         Just (tv,ty') -> go n (App expr (Type (mkTyVarTy tv))) ty' `thenUs` \ expr' ->
-                          returnUs (Lam tv expr') ;
-         Nothing ->
-  
-       case splitFunTy_maybe ty of {
-         Just (arg_ty, res_ty) 
-               -> newVar arg_ty                                `thenUs` \ arg' ->
-                  go (n-1) (App expr (Var arg')) res_ty        `thenUs` \ expr' ->
-                  returnUs (Lam arg' expr') ;
-         Nothing -> 
-  
-       case splitNewType_maybe ty of {
-         Just ty' -> go n (mkCoerce ty' ty expr) ty'   `thenUs` \ expr' ->
-                     returnUs (mkCoerce ty ty' expr') ;
-  
-         Nothing -> pprTrace "Bad saturate" ((ppr fn <+> ppr expr) $$ ppr ty)
-                    returnUs expr
-       }}}
-
-    
-
------------------------------------------------------------------------------
+-- ---------------------------------------------------------------------------
 -- Eliminate Lam as a non-rhs (STG doesn't have such a thing)
------------------------------------------------------------------------------
+-- ---------------------------------------------------------------------------
 
 deLam (Note n e)
   = deLam e `thenUs` \ e ->
@@ -370,7 +335,8 @@ deLam expr@(Lam _ _)
     (bndrs, body) = collectBinders expr
 
     eta expr@(App _ _)
-       | n_remaining >= 0 &&
+       | ok_to_eta_reduce f &&
+         n_remaining >= 0 &&
          and (zipWith ok bndrs last_args) &&
          not (any (`elemVarSet` fvs_remaining) bndrs)
        = Just remaining_expr
@@ -384,6 +350,14 @@ deLam expr@(Lam _ _)
          ok bndr (Var arg) = bndr == arg
          ok bndr other     = False
 
+         -- we can't eta reduce something which must be saturated.
+         ok_to_eta_reduce (Var f)
+                = case idFlavour f of
+                     PrimOpId op  -> False
+                     DataConId dc -> False
+                     other        -> True
+         ok_to_eta_reduce _ = False --safe. ToDo: generalise
+
     eta (Let bind@(NonRec b r) body)
        | not (any (`elemVarSet` fvs) bndrs)
                 = case eta body of