[project @ 1998-03-19 23:54:49 by simonpj]
[ghc-hetmet.git] / ghc / compiler / simplCore / Simplify.lhs
index 97b698f..a4f7a79 100644 (file)
@@ -11,7 +11,7 @@ module Simplify ( simplTopBinds, simplExpr, simplBind ) where
 import BinderInfo
 import CmdLineOpts     ( SimplifierSwitch(..) )
 import ConFold         ( completePrim )
-import CoreUnfold      ( Unfolding, SimpleUnfolding, mkFormSummary, 
+import CoreUnfold      ( Unfolding, mkFormSummary, 
                          exprIsTrivial, whnfOrBottom, inlineUnconditionally,
                          FormSummary(..)
                        )
@@ -22,23 +22,21 @@ import CoreUtils    ( coreExprType, nonErrorRHSs, maybeErrorApp,
                        )
 import Id              ( idType, idMustBeINLINEd, idWantsToBeINLINEd, idMustNotBeINLINEd, 
                          addIdArity, getIdArity,
-                         getIdDemandInfo, addIdDemandInfo,
-                         GenId{-instance NamedThing-}
+                         getIdDemandInfo, addIdDemandInfo
                        )
-import Name            ( isExported )
+import Name            ( isExported, isLocallyDefined )
 import IdInfo          ( willBeDemanded, noDemandInfo, DemandInfo, ArityInfo(..),
                          atLeastArity, unknownArity )
 import Literal         ( isNoRepLit )
 import Maybes          ( maybeToBool )
-import PprType         ( GenType{-instance Outputable-}, GenTyVar{- instance Outputable -} )
 import PrimOp          ( primOpOkForSpeculation, PrimOp(..) )
 import SimplCase       ( simplCase, bindLargeRhs )
 import SimplEnv
 import SimplMonad
-import SimplVar                ( completeVar )
-import Unique          ( Unique )
+import SimplVar                ( completeVar, simplBinder, simplBinders, simplTyBinder, simplTyBinders )
 import SimplUtils
-import Type            ( mkTyVarTy, mkTyVarTys, mkAppTy, applyTy, mkFunTys, splitAlgTyConApp_maybe,
+import Type            ( mkTyVarTy, mkTyVarTys, mkAppTy, applyTy, applyTys,
+                         mkFunTys, splitAlgTyConApp_maybe,
                          splitFunTys, splitFunTy_maybe, isUnpointedType
                        )
 import TysPrim         ( realWorldStatePrimTy )
@@ -196,8 +194,9 @@ simplTopBinds env binds
 
     simpl_top_binds env (NonRec binder@(in_id,occ_info) rhs : binds)
       =                --- No cloning necessary at top level
-        simplRhsExpr env binder rhs in_id                              `thenSmpl` \ (rhs',arity) ->
-        completeNonRec env binder (in_id `withArity` arity) rhs'       `thenSmpl` \ (new_env, binds1') ->
+        simplBinder env binder                                         `thenSmpl` \ (env1, out_id) ->
+        simplRhsExpr env binder rhs out_id                             `thenSmpl` \ (rhs',arity) ->
+        completeNonRec env1 binder (out_id `withArity` arity) rhs'     `thenSmpl` \ (new_env, binds1') ->
         simpl_top_binds new_env binds                                  `thenSmpl` \ binds2' ->
         returnSmpl (binds1' ++ binds2')
 
@@ -217,15 +216,10 @@ simplTopBinds env binds
                --
                -- Sure we could have made the indirection-shorting a bit cleverer, but
                -- propagating pragma info is a Good Idea anyway.
-       let
-           env1 = extendIdEnvWithClones env binders ids
-       in
-        simplRecursiveGroup env1 ids pairs     `thenSmpl` \ (bind', new_env) ->
+       simplBinders env (map fst pairs)        `thenSmpl` \ (env1, out_ids) ->
+        simplRecursiveGroup env1 out_ids pairs         `thenSmpl` \ (bind', new_env) ->
         simpl_top_binds new_env binds          `thenSmpl` \ binds' ->
         returnSmpl (Rec bind' : binds')
-      where
-       binders = map fst pairs
-        ids     = map fst binders
 \end{code}
 
 %************************************************************************
@@ -248,21 +242,10 @@ applied to the specified arguments.
 
 Variables
 ~~~~~~~~~
-Check if there's a macro-expansion, and if so rattle on.  Otherwise do
-the more sophisticated stuff.
 
 \begin{code}
-simplExpr env (Var v) args result_ty
-  = case (runEager $ lookupId env v) of
-      LitArg lit               -- A boring old literal
-       -> ASSERT( null args )
-          returnSmpl (Lit lit)
-
-      VarArg var       -- More interesting!  An id!
-       -> completeVar env var args result_ty
-                               -- Either Id is in the local envt, or it's a global.
-                               -- In either case we don't need to apply the type
-                               -- environment to it.
+simplExpr env (Var var) args result_ty
+  = simplVar env False {- No InlineCall -} var args result_ty
 \end{code}
 
 Literals
@@ -329,16 +312,14 @@ First the case when it's applied to an argument.
 \begin{code}
 simplExpr env (Lam (TyBinder tyvar) body) (TyArg ty : args) result_ty
   = tick TyBetaReduction       `thenSmpl_`
-    simplExpr (extendTyEnv env tyvar ty) body args result_ty
+    simplExpr (bindTyVar env tyvar ty) body args result_ty
 \end{code}
 
 \begin{code}
 simplExpr env tylam@(Lam (TyBinder tyvar) body) [] result_ty
-  = cloneTyVarSmpl tyvar               `thenSmpl` \ tyvar' ->
+  = simplTyBinder env tyvar    `thenSmpl` \ (new_env, tyvar') ->
     let
-       new_ty  = mkTyVarTy tyvar'
-       new_env = extendTyEnv env tyvar new_ty
-       new_result_ty = applyTy result_ty new_ty
+       new_result_ty = applyTy result_ty (mkTyVarTy tyvar')
     in
     simplExpr new_env body [] new_result_ty            `thenSmpl` \ body' ->
     returnSmpl (Lam (TyBinder tyvar') body')
@@ -371,14 +352,14 @@ simplExpr env expr@(Lam (ValBinder binder) body) orig_args result_ty
     go n env (Lam (ValBinder binder) body) (val_arg : args)
       | isValArg val_arg               -- The lambda has an argument
       = tick BetaReduction     `thenSmpl_`
-        go (n+1) (extendIdEnvWithAtom env binder val_arg) body args
+        go (n+1) (bindIdToAtom env binder val_arg) body args
 
     go n env expr@(Lam (ValBinder binder) body) args
        -- The lambda is un-saturated, so we must zap the occurrence info
        -- on the arguments we've already beta-reduced into the body of the lambda
       = ASSERT( null args )    -- Value lambda must match value argument!
         let
-           new_env = markDangerousOccs env (take n orig_args)
+           new_env = markDangerousOccs env orig_args
         in
         simplValLam new_env expr 0 {- Guaranteed applied to at least 0 args! -} result_ty 
                                `thenSmpl` \ (expr', arity) ->
@@ -402,69 +383,39 @@ Case expressions
 
 \begin{code}
 simplExpr env expr@(Case scrut alts) args result_ty
-  = simplCase env scrut alts (\env rhs -> simplExpr env rhs args result_ty) result_ty
+  = simplCase env scrut
+             (getSubstEnvs env, alts)
+             (\env rhs -> simplExpr env rhs args result_ty)
+             result_ty
 \end{code}
 
 
 Coercions
 ~~~~~~~~~
 \begin{code}
-simplExpr env (Coerce coercion ty body) args result_ty
-  = simplCoerce env coercion ty body args result_ty
-\end{code}
-
-
-Set-cost-centre
-~~~~~~~~~~~~~~~
-
-1) Eliminating nested sccs ...
-We must be careful to maintain the scc counts ...
-
-\begin{code}
-simplExpr env (SCC cc1 (SCC cc2 expr)) args result_ty
-  | not (isSccCountCostCentre cc2) && case cmpCostCentre cc1 cc2 of { EQ -> True; _ -> False }
-       -- eliminate inner scc if no call counts and same cc as outer
-  = simplExpr env (SCC cc1 expr) args result_ty
+simplExpr env (Note (Coerce to_ty from_ty) body) args result_ty
+  = simplCoerce env to_ty from_ty body args result_ty
 
-  | not (isSccCountCostCentre cc2) && not (isSccCountCostCentre cc1)
-       -- eliminate outer scc if no call counts associated with either ccs
-  = simplExpr env (SCC cc2 expr) args result_ty
-\end{code}
+simplExpr env (Note (SCC cc) body) args result_ty
+  = simplSCC env cc body args result_ty
 
-2) Moving sccs inside lambdas ...
-  
-\begin{code}
-simplExpr env (SCC cc (Lam binder@(ValBinder _) body)) args result_ty
-  | not (isSccCountCostCentre cc)
-       -- move scc inside lambda only if no call counts
-  = simplExpr env (Lam binder (SCC cc body)) args result_ty
-
-simplExpr env (SCC cc (Lam binder body)) args result_ty
-       -- always ok to move scc inside type/usage lambda
-  = simplExpr env (Lam binder (SCC cc body)) args result_ty
-\end{code}
+-- InlineCall is simple enough to deal with on the spot
+-- The only complication is that we slide the InlineCall
+-- inwards past any function arguments
+simplExpr env (Note InlineCall expr) args result_ty
+  = go expr args
+  where
+    go (Var v) args      = simplVar env True {- InlineCall -} v args result_ty
 
-3) Eliminating dict sccs ...
+    go (App fun arg) args = simplArg env arg   `appEager` \ arg' ->
+                           go fun (arg' : args)
 
-\begin{code}
-simplExpr env (SCC cc expr) args result_ty
-  | squashableDictishCcExpr cc expr
-       -- eliminate dict cc if trivial dict expression
-  = simplExpr env expr args result_ty
+    go other args        =     -- Unexpected discard; report it
+                           pprTrace "simplExpr: discarding InlineCall" (ppr expr) $
+                           simplExpr env other args result_ty
 \end{code}
 
-4) Moving arguments inside the body of an scc ...
-This moves the cost of doing the application inside the scc
-(which may include the cost of extracting methods etc)
 
-\begin{code}
-simplExpr env (SCC cost_centre body) args result_ty
-  = let
-       new_env = setEnclosingCC env cost_centre
-    in
-    simplExpr new_env body args result_ty              `thenSmpl` \ body' ->
-    returnSmpl (SCC cost_centre body')
-\end{code}
 
 %************************************************************************
 %*                                                                     *
@@ -504,11 +455,9 @@ simplRhsExpr env binder@(id,occ_info) rhs new_id
 
   | otherwise  -- OK, use the big hammer
   =    -- Deal with the big lambda part
-    mapSmpl cloneTyVarSmpl tyvars                      `thenSmpl` \ tyvars' ->
+    simplTyBinders rhs_env tyvars                      `thenSmpl` \ (lam_env, tyvars') ->
     let
-       new_tys  = mkTyVarTys tyvars'
-       body_ty  = foldl applyTy rhs_ty new_tys
-       lam_env  = extendTyEnvList rhs_env (zipEqual "simplRhsExpr" tyvars new_tys)
+       body_ty  = applyTys rhs_ty (mkTyVarTys tyvars')
     in
        -- Deal with the little lambda part
        -- Note that we call simplLam even if there are no binders,
@@ -634,11 +583,8 @@ simplValLam env expr min_no_of_args expr_ty
 
     null potential_extra_binder_tys                ||  -- or ain't a function
     no_of_extra_binders <= 0                           -- or no extra binders needed
-  = cloneIds env binders               `thenSmpl` \ binders' ->
-    let
-       new_env = extendIdEnvWithClones env binders binders'
-    in
-    simplExpr new_env body [] body_ty          `thenSmpl` \ body' ->
+  = simplBinders env binders           `thenSmpl` \ (new_env, binders') ->
+    simplExpr new_env body [] body_ty  `thenSmpl` \ body' ->
     returnSmpl (mkValLam binders' body', final_arity)
 
   | otherwise                          -- Eta expansion possible
@@ -652,10 +598,7 @@ simplValLam env expr min_no_of_args expr_ty
     else \x -> x) $
 
     tick EtaExpansion                  `thenSmpl_`
-    cloneIds env binders               `thenSmpl` \ binders' ->
-    let
-       new_env = extendIdEnvWithClones env binders binders'
-    in
+    simplBinders env binders           `thenSmpl` \ (new_env, binders') ->
     newIds extra_binder_tys                                            `thenSmpl` \ extra_binders' ->
     simplExpr new_env body (map VarArg extra_binders') etad_body_ty    `thenSmpl` \ body' ->
     returnSmpl (
@@ -711,6 +654,33 @@ simplValLam env expr min_no_of_args expr_ty
 \end{code}
 
 
+%************************************************************************
+%*                                                                     *
+\subsection[Simplify-var]{Variables}
+%*                                                                     *
+%************************************************************************
+
+Check if there's a macro-expansion, and if so rattle on.  Otherwise do
+the more sophisticated stuff.
+
+\begin{code}
+simplVar env inline_call var args result_ty
+  = case lookupIdSubst env var of
+  
+      Just (SubstExpr ty_subst id_subst expr)
+       -> simplExpr (setSubstEnvs env (ty_subst, id_subst)) expr args result_ty
+
+      Just (SubstLit lit)              -- A boring old literal
+       -> ASSERT( null args )
+          returnSmpl (Lit lit)
+
+      Just (SubstVar var')             -- More interesting!  An id!
+       -> completeVar env inline_call var' args result_ty
+
+      Nothing  -- Not in the substitution; hand off to completeVar
+       -> completeVar env inline_call var args result_ty 
+\end{code}
+
 
 %************************************************************************
 %*                                                                     *
@@ -720,26 +690,88 @@ simplValLam env expr min_no_of_args expr_ty
 
 \begin{code}
 -- (coerce (case s of p -> r)) args ==> case s of p -> (coerce r) args
-simplCoerce env coercion ty expr@(Case scrut alts) args result_ty
-  = simplCase env scrut alts (\env rhs -> simplCoerce env coercion ty rhs args result_ty) result_ty
+simplCoerce env to_ty from_ty expr@(Case scrut alts) args result_ty
+  = simplCase env scrut (getSubstEnvs env, alts)
+             (\env rhs -> simplCoerce env to_ty from_ty rhs args result_ty)
+             result_ty
 
 -- (coerce (let defns in b)) args  ==> let defns' in (coerce b) args
-simplCoerce env coercion ty (Let bind body) args result_ty
-  = simplBind env bind (\env -> simplCoerce env coercion ty body args result_ty) result_ty
+simplCoerce env to_ty from_ty (Let bind body) args result_ty
+  = simplBind env bind (\env -> simplCoerce env to_ty from_ty body args result_ty) result_ty
 
 -- Default case
-simplCoerce env coercion ty expr args result_ty
-  = simplTy env ty                     `appEager` \ ty' ->
-    simplTy env expr_ty                        `appEager` \ expr_ty' ->
-    simplExpr env expr [] expr_ty'     `thenSmpl` \ expr' ->
-    returnSmpl (mkGenApp (mkCoerce coercion ty' expr') args)
-  where
-    expr_ty = coreExprType (unTagBinders expr) -- Rather like simplCase other_scrut
+-- NB: we do *not* push the argments inside the coercion
 
+simplCoerce env to_ty from_ty expr args result_ty
+  = simplTy env to_ty                  `appEager` \ to_ty' ->
+    simplTy env from_ty                        `appEager` \ from_ty' ->
+    simplExpr env expr [] from_ty'     `thenSmpl` \ expr' ->
+    returnSmpl (mkGenApp (mkCoerce to_ty' from_ty' expr') args)
+  where
        -- Try cancellation; we do this "on the way up" because
        -- I think that's where it'll bite best
-    mkCoerce (CoerceOut con1) ty1 (Coerce (CoerceIn  con2) ty2 body) | con1 == con2 = body
-    mkCoerce coercion ty  body = Coerce coercion ty body
+    mkCoerce to_ty1 from_ty1 (Note (Coerce to_ty2 from_ty2) body)
+       = ASSERT( from_ty1 == to_ty2 )
+         mkCoerce to_ty1 from_ty2 body
+    mkCoerce to_ty from_ty body
+       | to_ty == from_ty = body
+       | otherwise        = Note (Coerce to_ty from_ty) body
+\end{code}
+
+
+%************************************************************************
+%*                                                                     *
+\subsection[Simplify-scc]{SCC expressions
+%*                                                                     *
+%************************************************************************
+
+1) Eliminating nested sccs ...
+We must be careful to maintain the scc counts ...
+
+\begin{code}
+simplSCC env cc1 (Note (SCC cc2) expr) args result_ty
+  | not (isSccCountCostCentre cc2) && case cmpCostCentre cc1 cc2 of { EQ -> True; _ -> False }
+       -- eliminate inner scc if no call counts and same cc as outer
+  = simplSCC env cc1 expr args result_ty
+
+  | not (isSccCountCostCentre cc2) && not (isSccCountCostCentre cc1)
+       -- eliminate outer scc if no call counts associated with either ccs
+  = simplSCC env cc2 expr args result_ty
+\end{code}
+
+2) Moving sccs inside lambdas ...
+  
+\begin{code}
+simplSCC env cc (Lam binder@(ValBinder _) body) args result_ty
+  | not (isSccCountCostCentre cc)
+       -- move scc inside lambda only if no call counts
+  = simplExpr env (Lam binder (Note (SCC cc) body)) args result_ty
+
+simplSCC env cc (Lam binder body) args result_ty
+       -- always ok to move scc inside type/usage lambda
+  = simplExpr env (Lam binder (Note (SCC cc) body)) args result_ty
+\end{code}
+
+3) Eliminating dict sccs ...
+
+\begin{code}
+simplSCC env cc expr args result_ty
+  | squashableDictishCcExpr cc expr
+       -- eliminate dict cc if trivial dict expression
+  = simplExpr env expr args result_ty
+\end{code}
+
+4) Moving arguments inside the body of an scc ...
+This moves the cost of doing the application inside the scc
+(which may include the cost of extracting methods etc)
+
+\begin{code}
+simplSCC env cc body args result_ty
+  = let
+       new_env = setEnclosingCC env cc
+    in
+    simplExpr new_env body args result_ty              `thenSmpl` \ body' ->
+    returnSmpl (Note (SCC cc) body')
 \end{code}
 
 
@@ -900,11 +932,11 @@ Notice that let to case occurs only if x is used strictly in its body
 \begin{code}
 -- Dead code is now discarded by the occurrence analyser,
 
-simplNonRec env binder@(id,occ_info) rhs body_c body_ty
-  | inlineUnconditionally ok_to_dup id occ_info
+simplNonRec env binder@(id,_) rhs body_c body_ty
+  | inlineUnconditionally binder
   =    -- The binder is used in definitely-inline way in the body
        -- So add it to the environment, drop the binding, and continue
-    body_c (extendEnvGivenInlining env id occ_info rhs)
+    body_c (bindIdToExpr env binder rhs)
 
   | idWantsToBeINLINEd id
   = complete_bind env rhs      -- Don't mess about with floating or let-to-case on
@@ -916,7 +948,7 @@ simplNonRec env binder@(id,occ_info) rhs body_c body_ty
        -- we can't trivially do let-to-case (because there may be some unboxed
        -- things bound in letrecs that aren't really recursive).
   | isUnpointedType rhs_ty && not rhs_is_whnf
-  = simplCase env rhs (PrimAlts [] (BindDefault binder (Var id)))
+  = simplCase env rhs (getSubstEnvs env, PrimAlts [] (BindDefault binder (Var id)))
                      (\env rhs -> complete_bind env rhs) body_ty
 
        -- Try let-to-case; see notes below about let-to-case
@@ -930,7 +962,7 @@ simplNonRec env binder@(id,occ_info) rhs body_c body_ty
                -- the end of simplification.
     )
   = tick Let2Case                              `thenSmpl_`
-    simplCase env rhs (AlgAlts [] (BindDefault binder (Var id)))
+    simplCase env rhs (getSubstEnvs env, AlgAlts [] (BindDefault binder (Var id)))
                      (\env rhs -> complete_bind env rhs) body_ty
                -- OLD COMMENT:  [now the new RHS is only "x" so there's less worry]
                -- NB: it's tidier to call complete_bind not simpl_bind, else
@@ -947,7 +979,8 @@ simplNonRec env binder@(id,occ_info) rhs body_c body_ty
     -- Try let-from-let
     simpl_bind env (Let bind rhs) | let_floating_ok
       = tick LetFloatFromLet                    `thenSmpl_`
-       simplBind env (fix_up_demandedness will_be_demanded bind)
+       simplBind env (if will_be_demanded then bind 
+                                          else un_demandify_bind bind)
                      (\env -> simpl_bind env rhs) body_ty
 
     -- Try case-from-let; this deals with a strict let of error too
@@ -955,25 +988,26 @@ simplNonRec env binder@(id,occ_info) rhs body_c body_ty
       = tick CaseFloatFromLet                          `thenSmpl_`
 
        -- First, bind large let-body if necessary
-       if ok_to_dup || isSingleton (nonErrorRHSs alts)
+       if isSingleton (nonErrorRHSs alts)
        then
-           simplCase env scrut alts (\env rhs -> simpl_bind env rhs) body_ty
+           simplCase env scrut (getSubstEnvs env, alts) 
+                     (\env rhs -> simpl_bind env rhs) body_ty
        else
            bindLargeRhs env [binder] body_ty body_c    `thenSmpl` \ (extra_binding, new_body) ->
            let
                body_c' = \env -> simplExpr env new_body [] body_ty
                case_c  = \env rhs -> simplNonRec env binder rhs body_c' body_ty
            in
-           simplCase env scrut alts case_c body_ty     `thenSmpl` \ case_expr ->
+           simplCase env scrut (getSubstEnvs env, alts) case_c body_ty `thenSmpl` \ case_expr ->
            returnSmpl (Let extra_binding case_expr)
 
     -- None of the above; simplify rhs and tidy up
     simpl_bind env rhs = complete_bind env rhs
  
     complete_bind env rhs
-      = cloneId env binder                     `thenSmpl` \ new_id ->
+      = simplBinder env binder                 `thenSmpl` \ (env_w_clone, new_id) ->
        simplRhsExpr env binder rhs new_id      `thenSmpl` \ (rhs',arity) ->
-       completeNonRec env binder 
+       completeNonRec env_w_clone binder 
                (new_id `withArity` arity) rhs' `thenSmpl` \ (new_env, binds) ->
         body_c new_env                         `thenSmpl` \ body' ->
         returnSmpl (mkCoLetsAny binds body')
@@ -982,7 +1016,6 @@ simplNonRec env binder@(id,occ_info) rhs body_c body_ty
        -- All this stuff is computed at the start of the simpl_bind loop
     float_lets               = switchIsSet env SimplFloatLetsExposingWHNF
     float_primops            = switchIsSet env SimplOkToFloatPrimOps
-    ok_to_dup                = switchIsSet env SimplOkToDupCode
     always_float_let_from_let = switchIsSet env SimplAlwaysFloatLetsFromLets
     try_let_to_case           = switchIsSet env SimplLetToCase
     no_float                 = switchIsSet env SimplNoLetFromStrictLet
@@ -1000,7 +1033,7 @@ simplNonRec env binder@(id,occ_info) rhs body_c body_ty
                        ValueForm -> True
                        other -> False
 
-    float_exposes_hnf = floatExposesHNF float_lets float_primops ok_to_dup rhs
+    float_exposes_hnf = floatExposesHNF float_lets float_primops rhs
 
     let_floating_ok  = (will_be_demanded && not no_float) ||
                       always_float_let_from_let ||
@@ -1036,44 +1069,53 @@ Because then we can't remove the x=y binding, in which case we
 have just made things worse, perhaps a lot worse.
 
 \begin{code}
-       -- Right hand sides that are constructors
-       --      let v = C args
-       --      in
-       --- ...(let w = C same-args in ...)...
-       -- Then use v instead of w.      This may save
-       -- re-constructing an existing constructor.
 completeNonRec env binder new_id new_rhs
-  |  not (isExported new_id)           -- Don't bother for exported things
-                                       -- because we won't be able to drop
-                                       -- its binding.
-  && maybeToBool maybe_atomic_rhs
-  = tick tick_type     `thenSmpl_`
-    returnSmpl (extendIdEnvWithAtom env binder rhs_arg, [])
+  = returnSmpl (env', [NonRec b r | (b,r) <- binds])
   where
-    Just (rhs_arg, tick_type) = maybe_atomic_rhs
-    maybe_atomic_rhs 
-      =                -- Try first for an existing constructor application
-       case maybe_con new_rhs of {
-       Just con -> Just (VarArg con, ConReused);
-
-       Nothing  ->     -- No good; try eta-reduction
-       case etaCoreExpr new_rhs of {
-       Var v -> Just (VarArg v, AtomicRhs);
-       Lit l -> Just (LitArg l, AtomicRhs);
-
-       other -> Nothing -- Neither worked, so return Nothing
-       }}
-       
+    (env', binds) = completeBind env binder new_id new_rhs
 
-    maybe_con (Con con con_args) | switchIsSet env SimplReuseCon
-                                = lookForConstructor env con con_args 
-    maybe_con other_rhs                 = Nothing
 
-completeNonRec env binder@(id,occ_info) new_id new_rhs
-  = returnSmpl (new_env , [NonRec new_id new_rhs])
+completeBind :: SimplEnv 
+            -> InBinder -> OutId -> OutExpr            -- Id and RHS
+            -> (SimplEnv, [(OutId, OutExpr)])          -- Final envt and binding(s)
+
+completeBind env binder@(_,occ_info) new_id new_rhs
+  | idMustNotBeINLINEd new_id          -- Occurrence analyser says "don't inline"
+  = (env, new_binds)
+
+  |  atomic_rhs                        -- If rhs (after eta reduction) is atomic
+  && not (isExported new_id)   -- and binder isn't exported
+  =    -- Drop the binding completely
+    let
+        env1 = notInScope env new_id
+       env2 = bindIdToAtom env1 binder the_arg
+    in
+    (env2, [])
+
+  |  atomic_rhs                -- Rhs is atomic, and new_id is exported
+  && case eta'd_rhs of { Var v -> isLocallyDefined v && not (isExported v); other -> False }
+  =    -- The local variable v will be eliminated next time round
+       -- in favour of new_id, so it's a waste to replace all new_id's with v's
+       -- this time round.
+       -- This case is an optional improvement; saves a simplifier iteration
+    (env, [(new_id, eta'd_rhs)])
+
+  | otherwise                          -- Non-atomic
+  = let
+       env1 = extendEnvGivenBinding env occ_info new_id new_rhs
+    in 
+    (env1, new_binds)
+            
   where
-    new_env = extendEnvGivenBinding (extendIdEnvWithClone env binder new_id)
-                                   occ_info new_id new_rhs
+    new_binds  = [(new_id, new_rhs)]
+    atomic_rhs = is_atomic eta'd_rhs
+    eta'd_rhs  = case lookForConstructor env new_rhs of 
+                  Just v -> Var v
+                  other  -> etaCoreExpr new_rhs
+
+    the_arg    = case eta'd_rhs of
+                         Var v -> VarArg v
+                         Lit l -> LitArg l
 \end{code}
 
 ----------------------------------------------------------------------------
@@ -1183,13 +1225,10 @@ simplRec env pairs body_c body_ty
     let
        binders = map fst pairs'
     in
-    cloneIds env binders                       `thenSmpl` \ ids' ->
-    let
-       env_w_clones = extendIdEnvWithClones env binders ids'
-    in
+    simplBinders env binders                           `thenSmpl` \ (env_w_clones, ids') ->
     simplRecursiveGroup env_w_clones ids' pairs'       `thenSmpl` \ (pairs', new_env) ->
 
-    body_c new_env                             `thenSmpl` \ body' ->
+    body_c new_env                                     `thenSmpl` \ body' ->
 
     returnSmpl (Let (Rec pairs') body')
 \end{code}
@@ -1200,8 +1239,8 @@ simplRec env pairs body_c body_ty
 simplRecursiveGroup env new_ids []
   = returnSmpl ([], env)
 
-simplRecursiveGroup env (new_id : new_ids) ((binder@(id, occ_info), rhs) : pairs)
-  | inlineUnconditionally ok_to_dup id occ_info
+simplRecursiveGroup env (new_id : new_ids) ((binder, rhs) : pairs)
+  | inlineUnconditionally binder
   =    -- Single occurrence, so drop binding and extend env with the inlining
        -- This is a little delicate, because what if the unique occurrence
        -- is *before* this binding?  This'll never happen, because
@@ -1211,37 +1250,18 @@ simplRecursiveGroup env (new_id : new_ids) ((binder@(id, occ_info), rhs) : pairs
        -- If these claims aren't right Core Lint will spot an unbound
        -- variable.  A quick fix is to delete this clause for simplRecursiveGroup
     let
-       new_env = extendEnvGivenInlining env new_id occ_info rhs
+       new_env = bindIdToExpr env binder rhs
     in
     simplRecursiveGroup new_env new_ids pairs
 
   | otherwise
   = simplRhsExpr env binder rhs new_id         `thenSmpl` \ (new_rhs, arity) ->
     let
-       new_id' = new_id `withArity` arity
-    
-       -- ToDo: this next bit could usefully share code with completeNonRec
-
-        new_env 
-         | idMustNotBeINLINEd new_id           -- Occurrence analyser says "don't inline"
-         = env
-
-         | is_atomic eta'd_rhs                 -- If rhs (after eta reduction) is atomic
-         = extendIdEnvWithAtom env binder the_arg
-
-         | otherwise                           -- Non-atomic
-         = extendEnvGivenBinding env occ_info new_id new_rhs
-                                               -- Don't eta if it doesn't eliminate the binding
-
-        eta'd_rhs = etaCoreExpr new_rhs
-        the_arg   = case eta'd_rhs of
-                         Var v -> VarArg v
-                         Lit l -> LitArg l
+       new_id'   = new_id `withArity` arity
+        (new_env, new_binds') = completeBind env binder new_id' new_rhs
     in
     simplRecursiveGroup new_env new_ids pairs  `thenSmpl` \ (new_pairs, final_env) ->
-    returnSmpl ((new_id', new_rhs) : new_pairs, final_env)   
-  where
-    ok_to_dup = switchIsSet env SimplOkToDupCode
+    returnSmpl (new_binds' ++ new_pairs, final_env)   
 \end{code}
 
 
@@ -1276,7 +1296,8 @@ floatBind env top_level bind
     returnSmpl binds'
 
   where
-    (binds', _, n_extras) = fltBind bind       
+    binds'   = fltBind bind
+    n_extras = sum (map no_of_binds binds') - no_of_binds bind 
 
     float_lets               = switchIsSet env SimplFloatLetsExposingWHNF
     always_float_let_from_let = switchIsSet env SimplAlwaysFloatLetsFromLets
@@ -1284,32 +1305,27 @@ floatBind env top_level bind
        -- fltBind guarantees not to return leaky floats
        -- and all the binders of the floats have had their demand-info zapped
     fltBind (NonRec bndr rhs)
-      = (binds ++ [NonRec (un_demandify bndr) rhs'], 
-        leakFree bndr rhs', 
-        length binds)
+      = binds ++ [NonRec bndr rhs'] 
       where
         (binds, rhs') = fltRhs rhs
     
     fltBind (Rec pairs)
-      = ([Rec (extras
-              ++
-              binders `zip` rhss')],
-         and (zipWith leakFree binders rhss'),
-        length extras
-        )
-    
+      = [Rec pairs']
       where
-        (binders, rhss)  = unzip pairs
-        (binds_s, rhss') = mapAndUnzip fltRhs rhss
-       extras           = concat (map get_pairs (concat binds_s))
-
-        get_pairs (NonRec bndr rhs) = [(bndr,rhs)]
-        get_pairs (Rec pairs)       = pairs
+        pairs' = concat [ let
+                               (binds, rhs') = fltRhs rhs
+                         in
+                         foldr get_pairs [(bndr, rhs')] binds
+                       | (bndr, rhs) <- pairs
+                       ]
+
+        get_pairs (NonRec bndr rhs) rest = (bndr,rhs) :  rest
+        get_pairs (Rec pairs)       rest = pairs      ++ rest
     
        -- fltRhs has same invariant as fltBind
     fltRhs rhs
       |  (always_float_let_from_let ||
-          floatExposesHNF True False False rhs)
+          floatExposesHNF True False rhs)
       = fltExpr rhs
     
       | otherwise
@@ -1322,12 +1338,19 @@ floatBind env top_level bind
             -- fltExpr guarantees not to return leaky floats
       = (binds' ++ body_binds, body')
       where
-        (body_binds, body')         = fltExpr body
-        (binds', binds_wont_leak, _) = fltBind bind
+        binds_wont_leak     = all leakFreeBind binds'
+        (body_binds, body') = fltExpr body
+        binds'             = fltBind (un_demandify_bind bind)
     
     fltExpr expr = ([], expr)
 
 -- Crude but effective
+no_of_binds (NonRec _ _) = 1
+no_of_binds (Rec pairs)  = length pairs
+
+leakFreeBind (NonRec bndr rhs) = leakFree bndr rhs
+leakFreeBind (Rec pairs)       = and [leakFree bndr rhs | (bndr, rhs) <- pairs]
+
 leakFree (id,_) rhs = case getIdArity id of
                        ArityAtLeast n | n > 0 -> True
                        ArityExactly n | n > 0 -> True
@@ -1347,7 +1370,14 @@ simplArg :: SimplEnv -> InArg -> Eager ans OutArg
 simplArg env (LitArg lit) = returnEager (LitArg lit)
 simplArg env (TyArg  ty)  = simplTy env ty     `appEager` \ ty' -> 
                            returnEager (TyArg ty')
-simplArg env (VarArg id)  = lookupId env id
+simplArg env arg@(VarArg id)
+  = case lookupIdSubst env id of
+       Just (SubstVar id')   -> returnEager (VarArg id')
+       Just (SubstLit lit)   -> returnEager (LitArg lit)
+       Just (SubstExpr _ __) -> panic "simplArg"
+       Nothing               -> case lookupOutIdEnv env id of
+                                 Just (id', _, _) -> returnEager (VarArg id')
+                                 Nothing          -> returnEager arg
 \end{code}
 
 %************************************************************************
@@ -1358,16 +1388,14 @@ simplArg env (VarArg id)  = lookupId env id
 
 
 \begin{code}
--- fix_up_demandedness switches off the willBeDemanded Info field
+-- un_demandify_bind switches off the willBeDemanded Info field
 -- for bindings floated out of a non-demanded let
-fix_up_demandedness True {- Will be demanded -} bind
-   = bind      -- Simple; no change to demand info needed
-fix_up_demandedness False {- May not be demanded -} (NonRec binder rhs)
-   = NonRec (un_demandify binder) rhs
-fix_up_demandedness False {- May not be demanded -} (Rec pairs)
-   = Rec [(un_demandify binder, rhs) | (binder,rhs) <- pairs]
-
-un_demandify (id, occ_info) = (id `addIdDemandInfo` noDemandInfo, occ_info)
+un_demandify_bind (NonRec binder rhs)
+   = NonRec (un_demandify_bndr binder) rhs
+un_demandify_bind (Rec pairs)
+   = Rec [(un_demandify_bndr binder, rhs) | (binder,rhs) <- pairs]
+
+un_demandify_bndr (id, occ_info) = (id `addIdDemandInfo` noDemandInfo, occ_info)
 
 is_cheap_prim_app (Prim op _) = primOpOkForSpeculation op
 is_cheap_prim_app other              = False