Simplify.lhs 114 KB
Newer Older
1
%
2
% (c) The AQUA Project, Glasgow University, 1993-1998
3 4 5 6
%
\section[Simplify]{The main module of the simplifier}

\begin{code}
7 8
{-# LANGUAGE CPP #-}

9
module Simplify ( simplTopBinds, simplExpr ) where
10

11
#include "HsVersions.h"
12

simonpj@microsoft.com's avatar
Wibble  
simonpj@microsoft.com committed
13
import DynFlags
14
import SimplMonad
15
import Type hiding      ( substTy, extendTvSubst, substTyVar )
Ian Lynagh's avatar
Ian Lynagh committed
16
import SimplEnv
17
import SimplUtils
18
import FamInstEnv       ( FamInstEnv )
19
import Literal          ( litIsLifted ) --, mkMachInt ) -- temporalily commented out. See #8326
20
import Id
21
import MkId             ( seqId, voidPrimId )
22
import MkCore           ( mkImpossibleExpr, castBottomExpr )
23
import IdInfo
24
import Name             ( mkSystemVarName, isExternalName )
25
import Coercion hiding  ( substCo, substTy, substCoVar, extendTvSubst )
26
import OptCoercion      ( optCoercion )
27
import FamInstEnv       ( topNormaliseType_maybe )
28 29 30
import DataCon          ( DataCon, dataConWorkId, dataConRepStrictness
                        , isMarkedStrict ) --, dataConTyCon, dataConTag, fIRST_TAG )
--import TyCon            ( isEnumerationTyCon ) -- temporalily commented out. See #8326
31
import CoreMonad        ( Tick(..), SimplifierMode(..) )
32
import CoreSyn
33
import Demand           ( StrictSig(..), dmdTypeDepth, isStrictDmd )
Ian Lynagh's avatar
Ian Lynagh committed
34
import PprCore          ( pprParendExpr, pprCoreExpr )
35
import CoreUnfold
36
import CoreUtils
37
import CoreArity
38
--import PrimOp           ( tagToEnumKey ) -- temporalily commented out. See #8326
39
import Rules            ( lookupRule, getRules )
40
import TysPrim          ( voidPrimTy ) --, intPrimTy ) -- temporalily commented out. See #8326
41
import BasicTypes       ( TopLevelFlag(..), isTopLevel, RecFlag(..) )
42
import MonadUtils       ( foldlM, mapAccumLM, liftIO )
43
import Maybes           ( orElse )
44
--import Unique           ( hasKey ) -- temporalily commented out. See #8326
ian@well-typed.com's avatar
ian@well-typed.com committed
45
import Control.Monad
Ian Lynagh's avatar
Ian Lynagh committed
46
import Data.List        ( mapAccumL )
47
import Outputable
48
import FastString
49
import Pair
50
import Util
51
import ErrUtils
52 53 54
\end{code}


55 56
The guts of the simplifier is in this module, but the driver loop for
the simplifier is in SimplCore.lhs.
57 58


59
-----------------------------------------
Ian Lynagh's avatar
Ian Lynagh committed
60
        *** IMPORTANT NOTE ***
61 62 63 64 65 66
-----------------------------------------
The simplifier used to guarantee that the output had no shadowing, but
it does not do so any more.   (Actually, it never did!)  The reason is
documented with simplifyArgs.


67
-----------------------------------------
Ian Lynagh's avatar
Ian Lynagh committed
68
        *** IMPORTANT NOTE ***
69 70 71 72 73 74 75 76 77 78
-----------------------------------------
Many parts of the simplifier return a bunch of "floats" as well as an
expression. This is wrapped as a datatype SimplUtils.FloatsWith.

All "floats" are let-binds, not case-binds, but some non-rec lets may
be unlifted (with RHS ok-for-speculation).



-----------------------------------------
Ian Lynagh's avatar
Ian Lynagh committed
79
        ORGANISATION OF FUNCTIONS
80 81 82 83 84 85
-----------------------------------------
simplTopBinds
  - simplify all top-level binders
  - for NonRec, call simplRecOrTopPair
  - for Rec,    call simplRecBind

Ian Lynagh's avatar
Ian Lynagh committed
86 87 88

        ------------------------------
simplExpr (applied lambda)      ==> simplNonRecBind
89 90 91
simplExpr (Let (NonRec ...) ..) ==> simplNonRecBind
simplExpr (Let (Rec ...)    ..) ==> simplify binders; simplRecBind

Ian Lynagh's avatar
Ian Lynagh committed
92 93
        ------------------------------
simplRecBind    [binders already simplfied]
94 95 96 97
  - use simplRecOrTopPair on each pair in turn

simplRecOrTopPair [binder already simplified]
  Used for: recursive bindings (top level and nested)
Ian Lynagh's avatar
Ian Lynagh committed
98 99
            top-level non-recursive bindings
  Returns:
100 101 102 103 104
  - check for PreInlineUnconditionally
  - simplLazyBind

simplNonRecBind
  Used for: non-top-level non-recursive bindings
Ian Lynagh's avatar
Ian Lynagh committed
105 106 107
            beta reductions (which amount to the same thing)
  Because it can deal with strict arts, it takes a
        "thing-inside" and returns an expression
108 109 110 111

  - check for PreInlineUnconditionally
  - simplify binder, including its IdInfo
  - if strict binding
Ian Lynagh's avatar
Ian Lynagh committed
112 113 114
        simplStrictArg
        mkAtomicArgs
        completeNonRecX
115
    else
Ian Lynagh's avatar
Ian Lynagh committed
116 117
        simplLazyBind
        addFloats
118

Ian Lynagh's avatar
Ian Lynagh committed
119
simplNonRecX:   [given a *simplified* RHS, but an *unsimplified* binder]
120 121 122 123
  Used for: binding case-binder and constr args in a known-constructor case
  - check for PreInLineUnconditionally
  - simplify binder
  - completeNonRecX
Ian Lynagh's avatar
Ian Lynagh committed
124 125 126

        ------------------------------
simplLazyBind:  [binder already simplified, RHS not]
127
  Used for: recursive bindings (top level and nested)
Ian Lynagh's avatar
Ian Lynagh committed
128 129 130
            top-level non-recursive bindings
            non-top-level, but *lazy* non-recursive bindings
        [must not be strict or unboxed]
131
  Returns floats + an augmented environment, not an expression
Ian Lynagh's avatar
Ian Lynagh committed
132 133
  - substituteIdInfo and add result to in-scope
        [so that rules are available in rec rhs]
134 135 136
  - simplify rhs
  - mkAtomicArgs
  - float if exposes constructor or PAP
137
  - completeBind
138 139


Ian Lynagh's avatar
Ian Lynagh committed
140
completeNonRecX:        [binder and rhs both simplified]
141
  - if the the thing needs case binding (unlifted and not ok-for-spec)
Ian Lynagh's avatar
Ian Lynagh committed
142
        build a Case
143
   else
Ian Lynagh's avatar
Ian Lynagh committed
144 145
        completeBind
        addFloats
146

Ian Lynagh's avatar
Ian Lynagh committed
147 148
completeBind:   [given a simplified RHS]
        [used for both rec and non-rec bindings, top level and not]
149 150 151 152 153 154 155 156
  - try PostInlineUnconditionally
  - add unfolding [this is the only place we add an unfolding]
  - add arity



Right hand sides and arguments
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Ian Lynagh's avatar
Ian Lynagh committed
157 158 159
In many ways we want to treat
        (a) the right hand side of a let(rec), and
        (b) a function argument
160 161 162
in the same way.  But not always!  In particular, we would
like to leave these arguments exactly as they are, so they
will match a RULE more easily.
Ian Lynagh's avatar
Ian Lynagh committed
163 164 165

        f (g x, h x)
        g (+ x)
166 167 168 169

It's harder to make the rule match if we ANF-ise the constructor,
or eta-expand the PAP:

Ian Lynagh's avatar
Ian Lynagh committed
170 171
        f (let { a = g x; b = h x } in (a,b))
        g (\y. + x y)
172 173 174

On the other hand if we see the let-defns

Ian Lynagh's avatar
Ian Lynagh committed
175 176
        p = (g x, h x)
        q = + x
177 178

then we *do* want to ANF-ise and eta-expand, so that p and q
Ian Lynagh's avatar
Ian Lynagh committed
179
can be safely inlined.
180 181 182 183 184

Even floating lets out is a bit dubious.  For let RHS's we float lets
out if that exposes a value, so that the value can be inlined more vigorously.
For example

Ian Lynagh's avatar
Ian Lynagh committed
185
        r = let x = e in (x,x)
186 187 188 189 190 191 192 193 194 195 196 197 198 199

Here, if we float the let out we'll expose a nice constructor. We did experiments
that showed this to be a generally good thing.  But it was a bad thing to float
lets out unconditionally, because that meant they got allocated more often.

For function arguments, there's less reason to expose a constructor (it won't
get inlined).  Just possibly it might make a rule match, but I'm pretty skeptical.
So for the moment we don't float lets out of function arguments either.


Eta expansion
~~~~~~~~~~~~~~
For eta expansion, we want to catch things like

Ian Lynagh's avatar
Ian Lynagh committed
200
        case e of (a,b) -> \x -> case a of (p,q) -> \y -> r
201 202 203 204 205

If the \x was on the RHS of a let, we'd eta expand to bring the two
lambdas together.  And in general that's a good thing to do.  Perhaps
we should eta expand wherever we find a (value) lambda?  Then the eta
expansion at a let RHS can concentrate solely on the PAP case.
206 207


208
%************************************************************************
Ian Lynagh's avatar
Ian Lynagh committed
209
%*                                                                      *
210
\subsection{Bindings}
Ian Lynagh's avatar
Ian Lynagh committed
211
%*                                                                      *
212 213 214
%************************************************************************

\begin{code}
215
simplTopBinds :: SimplEnv -> [InBind] -> SimplM SimplEnv
216

Ian Lynagh's avatar
Ian Lynagh committed
217
simplTopBinds env0 binds0
Ian Lynagh's avatar
Ian Lynagh committed
218 219 220 221
  = do  {       -- Put all the top-level binders into scope at the start
                -- so that if a transformation rule has unexpectedly brought
                -- anything into scope, then we don't get a complaint about that.
                -- It's rather as if the top-level binders were imported.
222
                -- See note [Glomming] in OccurAnal.
Ian Lynagh's avatar
Ian Lynagh committed
223
        ; env1 <- simplRecBndrs env0 (bindersOfBinds binds0)
224
        ; env2 <- simpl_binds env1 binds0
Ian Lynagh's avatar
Ian Lynagh committed
225
        ; freeTick SimplifierDone
226
        ; return env2 }
227
  where
Ian Lynagh's avatar
Ian Lynagh committed
228 229 230 231
        -- We need to track the zapped top-level binders, because
        -- they should have their fragile IdInfo zapped (notably occurrence info)
        -- That's why we run down binds and bndrs' simultaneously.
        --
232 233 234 235
    simpl_binds :: SimplEnv -> [InBind] -> SimplM SimplEnv
    simpl_binds env []           = return env
    simpl_binds env (bind:binds) = do { env' <- simpl_bind env bind
                                      ; simpl_binds env' binds }
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
236

237
    simpl_bind env (Rec pairs)  = simplRecBind      env  TopLevel pairs
238
    simpl_bind env (NonRec b r) = simplRecOrTopPair env' TopLevel NonRecursive b b' r
Ian Lynagh's avatar
Ian Lynagh committed
239 240
        where
          (env', b') = addBndrRules env b (lookupRecBndr env b)
241 242 243 244
\end{code}


%************************************************************************
Ian Lynagh's avatar
Ian Lynagh committed
245
%*                                                                      *
246
\subsection{Lazy bindings}
Ian Lynagh's avatar
Ian Lynagh committed
247
%*                                                                      *
248 249 250
%************************************************************************

simplRecBind is used for
Ian Lynagh's avatar
Ian Lynagh committed
251
        * recursive bindings only
252 253 254

\begin{code}
simplRecBind :: SimplEnv -> TopLevelFlag
Ian Lynagh's avatar
Ian Lynagh committed
255 256
             -> [(InId, InExpr)]
             -> SimplM SimplEnv
Ian Lynagh's avatar
Ian Lynagh committed
257 258 259 260 261
simplRecBind env0 top_lvl pairs0
  = do  { let (env_with_info, triples) = mapAccumL add_rules env0 pairs0
        ; env1 <- go (zapFloats env_with_info) triples
        ; return (env0 `addRecFloats` env1) }
        -- addFloats adds the floats from env1,
Thomas Schilling's avatar
Thomas Schilling committed
262
        -- _and_ updates env0 with the in-scope set from env1
263
  where
264
    add_rules :: SimplEnv -> (InBndr,InExpr) -> (SimplEnv, (InBndr, OutBndr, InExpr))
Ian Lynagh's avatar
Ian Lynagh committed
265
        -- Add the (substituted) rules to the binder
266
    add_rules env (bndr, rhs) = (env', (bndr, bndr', rhs))
Ian Lynagh's avatar
Ian Lynagh committed
267 268
        where
          (env', bndr') = addBndrRules env bndr (lookupRecBndr env bndr)
269

270
    go env [] = return env
Ian Lynagh's avatar
Ian Lynagh committed
271

272
    go env ((old_bndr, new_bndr, rhs) : pairs)
273
        = do { env' <- simplRecOrTopPair env top_lvl Recursive old_bndr new_bndr rhs
Ian Lynagh's avatar
Ian Lynagh committed
274
             ; go env' pairs }
275 276
\end{code}

277
simplOrTopPair is used for
Ian Lynagh's avatar
Ian Lynagh committed
278 279
        * recursive bindings (whether top level or not)
        * top-level non-recursive bindings
280 281 282 283 284

It assumes the binder has already been simplified, but not its IdInfo.

\begin{code}
simplRecOrTopPair :: SimplEnv
285
                  -> TopLevelFlag -> RecFlag
Ian Lynagh's avatar
Ian Lynagh committed
286 287
                  -> InId -> OutBndr -> InExpr  -- Binder and rhs
                  -> SimplM SimplEnv    -- Returns an env that includes the binding
288

289
simplRecOrTopPair env top_lvl is_rec old_bndr new_bndr rhs
290 291 292 293
  = do { dflags <- getDynFlags
       ; trace_bind dflags $
           if preInlineUnconditionally dflags env top_lvl old_bndr rhs
                    -- Check for unconditional inline
294 295
           then do tick (PreInlineUnconditionally old_bndr)
                   return (extendIdSubst env old_bndr (mkContEx env rhs))
296 297 298 299 300 301 302 303 304
           else simplLazyBind env top_lvl is_rec old_bndr new_bndr rhs env }
  where
    trace_bind dflags thing_inside
      | not (dopt Opt_D_verbose_core2core dflags)
      = thing_inside
      | otherwise
      = pprTrace "SimplBind" (ppr old_bndr) thing_inside
        -- trace_bind emits a trace for each top-level binding, which
        -- helps to locate the tracing for inlining and rule firing
305 306 307 308
\end{code}


simplLazyBind is used for
309 310
  * [simplRecOrTopPair] recursive bindings (whether top level or not)
  * [simplRecOrTopPair] top-level non-recursive bindings
Ian Lynagh's avatar
Ian Lynagh committed
311
  * [simplNonRecE]      non-top-level *lazy* non-recursive bindings
312 313

Nota bene:
Ian Lynagh's avatar
Ian Lynagh committed
314
    1. It assumes that the binder is *already* simplified,
315
       and is in scope, and its IdInfo too, except unfolding
316 317 318

    2. It assumes that the binder type is lifted.

319
    3. It does not check for pre-inline-unconditionally;
320 321 322 323
       that should have been done already.

\begin{code}
simplLazyBind :: SimplEnv
Ian Lynagh's avatar
Ian Lynagh committed
324 325 326 327 328
              -> TopLevelFlag -> RecFlag
              -> InId -> OutId          -- Binder, both pre-and post simpl
                                        -- The OutId has IdInfo, except arity, unfolding
              -> InExpr -> SimplEnv     -- The RHS and its environment
              -> SimplM SimplEnv
329
-- Precondition: rhs obeys the let/app invariant
330
simplLazyBind env top_lvl is_rec bndr bndr1 rhs rhs_se
331 332
  = -- pprTrace "simplLazyBind" ((ppr bndr <+> ppr bndr1) $$ ppr rhs $$ ppr (seIdSubst rhs_se)) $
    do  { let   rhs_env     = rhs_se `setInScope` env
333 334 335 336 337 338
                (tvs, body) = case collectTyBinders rhs of
                                (tvs, body) | not_lam body -> (tvs,body)
                                            | otherwise    -> ([], rhs)
                not_lam (Lam _ _) = False
                not_lam _         = True
                        -- Do not do the "abstract tyyvar" thing if there's
Gabor Greif's avatar
typos  
Gabor Greif committed
339
                        -- a lambda inside, because it defeats eta-reduction
340 341
                        --    f = /\a. \x. g a x
                        -- should eta-reduce
342

343

Ian Lynagh's avatar
Ian Lynagh committed
344
        ; (body_env, tvs') <- simplBinders rhs_env tvs
345
                -- See Note [Floating and type abstraction] in SimplUtils
Ian Lynagh's avatar
Ian Lynagh committed
346

347
        -- Simplify the RHS
348 349
        ; let   rhs_cont = mkRhsStop (substTy body_env (exprType body))
        ; (body_env1, body1) <- simplExprF body_env body rhs_cont
Ian Lynagh's avatar
Ian Lynagh committed
350
        -- ANF-ise a constructor or PAP rhs
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
351
        ; (body_env2, body2) <- prepareRhs top_lvl body_env1 bndr1 body1
Ian Lynagh's avatar
Ian Lynagh committed
352 353 354

        ; (env', rhs')
            <-  if not (doFloatFromRhs top_lvl is_rec False body2 body_env2)
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
355
                then                            -- No floating, revert to body1
356
                     do { rhs' <- mkLam tvs' (wrapFloats body_env1 body1) rhs_cont
Ian Lynagh's avatar
Ian Lynagh committed
357 358 359 360 361 362 363 364 365
                        ; return (env, rhs') }

                else if null tvs then           -- Simple floating
                     do { tick LetFloatFromLet
                        ; return (addFloats env body_env2, body2) }

                else                            -- Do type-abstraction first
                     do { tick LetFloatFromLet
                        ; (poly_binds, body3) <- abstractFloats tvs' body_env2 body2
366
                        ; rhs' <- mkLam tvs' body3 rhs_cont
367
                        ; env' <- foldlM (addPolyBind top_lvl) env poly_binds
368
                        ; return (env', rhs') }
Ian Lynagh's avatar
Ian Lynagh committed
369 370

        ; completeBind env' top_lvl bndr bndr1 rhs' }
371
\end{code}
372

Ian Lynagh's avatar
Ian Lynagh committed
373
A specialised variant of simplNonRec used when the RHS is already simplified,
374 375 376 377
notably in knownCon.  It uses case-binding where necessary.

\begin{code}
simplNonRecX :: SimplEnv
Ian Lynagh's avatar
Ian Lynagh committed
378 379 380
             -> InId            -- Old binder
             -> OutExpr         -- Simplified RHS
             -> SimplM SimplEnv
381
-- Precondition: rhs satisfies the let/app invariant
382
simplNonRecX env bndr new_rhs
383
  | isDeadBinder bndr   -- Not uncommon; e.g. case (a,b) of c { (p,q) -> p }
384 385 386
  = return env    --  Here c is dead, and we avoid creating
                  --   the binding c = (a,b)

387
  | Coercion co <- new_rhs
388
  = return (extendCvSubst env bndr co)
389 390

  | otherwise
Ian Lynagh's avatar
Ian Lynagh committed
391
  = do  { (env', bndr') <- simplBinder env bndr
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
392
        ; completeNonRecX NotTopLevel env' (isStrictId bndr) bndr bndr' new_rhs }
393
                -- simplNonRecX is only used for NotTopLevel things
394

simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
395
completeNonRecX :: TopLevelFlag -> SimplEnv
396
                -> Bool
Ian Lynagh's avatar
Ian Lynagh committed
397 398 399 400
                -> InId                 -- Old binder
                -> OutId                -- New binder
                -> OutExpr              -- Simplified RHS
                -> SimplM SimplEnv
401 402
-- Precondition: rhs satisfies the let/app invariant
--               See Note [CoreSyn let/app invariant] in CoreSyn
403

simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
404 405
completeNonRecX top_lvl env is_strict old_bndr new_bndr new_rhs
  = do  { (env1, rhs1) <- prepareRhs top_lvl (zapFloats env) new_bndr new_rhs
406
        ; (env2, rhs2) <-
407
                if doFloatFromRhs NotTopLevel NonRecursive is_strict rhs1 env1
Ian Lynagh's avatar
Ian Lynagh committed
408 409 410 411
                then do { tick LetFloatFromLet
                        ; return (addFloats env env1, rhs1) }   -- Add the floats to the main env
                else return (env, wrapFloats env1 rhs1)         -- Wrap the floats around the RHS
        ; completeBind env2 NotTopLevel old_bndr new_bndr rhs2 }
412 413 414 415
\end{code}

{- No, no, no!  Do not try preInlineUnconditionally in completeNonRecX
   Doing so risks exponential behaviour, because new_rhs has been simplified once already
Ian Lynagh's avatar
Ian Lynagh committed
416
   In the cases described by the folowing commment, postInlineUnconditionally will
417
   catch many of the relevant cases.
Ian Lynagh's avatar
Ian Lynagh committed
418 419 420 421 422 423 424 425
        -- This happens; for example, the case_bndr during case of
        -- known constructor:  case (a,b) of x { (p,q) -> ... }
        -- Here x isn't mentioned in the RHS, so we don't want to
        -- create the (dead) let-binding  let x = (a,b) in ...
        --
        -- Similarly, single occurrences can be inlined vigourously
        -- e.g.  case (f x, g y) of (a,b) -> ....
        -- If a,b occur once we can avoid constructing the let binding for them.
426

427
   Furthermore in the case-binding case preInlineUnconditionally risks extra thunks
Ian Lynagh's avatar
Ian Lynagh committed
428 429 430 431 432 433
        -- Consider     case I# (quotInt# x y) of
        --                I# v -> let w = J# v in ...
        -- If we gaily inline (quotInt# x y) for v, we end up building an
        -- extra thunk:
        --                let w = J# (quotInt# x y) in ...
        -- because quotInt# can fail.
434

435 436 437 438
  | preInlineUnconditionally env NotTopLevel bndr new_rhs
  = thing_inside (extendIdSubst env bndr (DoneEx new_rhs))
-}

439
----------------------------------
440
prepareRhs takes a putative RHS, checks whether it's a PAP or
Ian Lynagh's avatar
Ian Lynagh committed
441
constructor application and, if so, converts it to ANF, so that the
442
resulting thing can be inlined more easily.  Thus
Ian Lynagh's avatar
Ian Lynagh committed
443
        x = (f a, g b)
444
becomes
Ian Lynagh's avatar
Ian Lynagh committed
445 446 447
        t1 = f a
        t2 = g b
        x = (t1,t2)
448

449
We also want to deal well cases like this
Ian Lynagh's avatar
Ian Lynagh committed
450
        v = (f e1 `cast` co) e2
451
Here we want to make e1,e2 trivial and get
Ian Lynagh's avatar
Ian Lynagh committed
452
        x1 = e1; x2 = e2; v = (f x1 `cast` co) v2
453 454
That's what the 'go' loop in prepareRhs does

455
\begin{code}
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
456
prepareRhs :: TopLevelFlag -> SimplEnv -> OutId -> OutExpr -> SimplM (SimplEnv, OutExpr)
457
-- Adds new floats to the env iff that allows us to return a good RHS
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
458
prepareRhs top_lvl env id (Cast rhs co)    -- Note [Float coercions]
459
  | Pair ty1 _ty2 <- coercionKind co       -- Do *not* do this if rhs has an unlifted type
Ian Lynagh's avatar
Ian Lynagh committed
460
  , not (isUnLiftedType ty1)            -- see Note [Float coercions (unlifted)]
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
461
  = do  { (env', rhs') <- makeTrivialWithInfo top_lvl env sanitised_info rhs
Ian Lynagh's avatar
Ian Lynagh committed
462
        ; return (env', Cast rhs' co) }
463
  where
464
    sanitised_info = vanillaIdInfo `setStrictnessInfo` strictnessInfo info
465
                                   `setDemandInfo` demandInfo info
466
    info = idInfo id
467

simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
468
prepareRhs top_lvl env0 _ rhs0
469
  = do  { (_is_exp, env1, rhs1) <- go 0 env0 rhs0
Ian Lynagh's avatar
Ian Lynagh committed
470
        ; return (env1, rhs1) }
471
  where
472
    go n_val_args env (Cast rhs co)
473 474
        = do { (is_exp, env', rhs') <- go n_val_args env rhs
             ; return (is_exp, env', Cast rhs' co) }
475
    go n_val_args env (App fun (Type ty))
476 477
        = do { (is_exp, env', rhs') <- go n_val_args env fun
             ; return (is_exp, env', App rhs' (Type ty)) }
478
    go n_val_args env (App fun arg)
479 480
        = do { (is_exp, env', fun') <- go (n_val_args+1) env fun
             ; case is_exp of
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
481
                True -> do { (env'', arg') <- makeTrivial top_lvl env' arg
Ian Lynagh's avatar
Ian Lynagh committed
482 483
                           ; return (True, env'', App fun' arg') }
                False -> return (False, env, App fun arg) }
484
    go n_val_args env (Var fun)
485
        = return (is_exp, env, Var fun)
Ian Lynagh's avatar
Ian Lynagh committed
486
        where
487
          is_exp = isExpandableApp fun n_val_args   -- The fun a constructor or PAP
488 489 490
                        -- See Note [CONLIKE pragma] in BasicTypes
                        -- The definition of is_exp should match that in
                        -- OccurAnal.occAnalApp
491

Ian Lynagh's avatar
Ian Lynagh committed
492
    go _ env other
Ian Lynagh's avatar
Ian Lynagh committed
493
        = return (False, env, other)
494 495
\end{code}

496

497 498 499
Note [Float coercions]
~~~~~~~~~~~~~~~~~~~~~~
When we find the binding
Ian Lynagh's avatar
Ian Lynagh committed
500
        x = e `cast` co
501
we'd like to transform it to
Ian Lynagh's avatar
Ian Lynagh committed
502 503
        x' = e
        x = x `cast` co         -- A trivial binding
504 505 506 507 508 509 510 511 512 513 514 515 516
There's a chance that e will be a constructor application or function, or something
like that, so moving the coerion to the usage site may well cancel the coersions
and lead to further optimisation.  Example:

     data family T a :: *
     data instance T Int = T Int

     foo :: Int -> Int -> Int
     foo m n = ...
        where
          x = T m
          go 0 = 0
          go n = case x of { T m -> go (n-m) }
Ian Lynagh's avatar
Ian Lynagh committed
517
                -- This case should optimise
518

519 520 521 522
Note [Preserve strictness when floating coercions]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In the Note [Float coercions] transformation, keep the strictness info.
Eg
523
        f = e `cast` co    -- f has strictness SSL
524
When we transform to
525
        f' = e             -- f' also has strictness SSL
526 527 528 529
        f = f' `cast` co   -- f still has strictness SSL

Its not wrong to drop it on the floor, but better to keep it.

530 531
Note [Float coercions (unlifted)]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Ian Lynagh's avatar
Ian Lynagh committed
532
BUT don't do [Float coercions] if 'e' has an unlifted type.
533 534
This *can* happen:

Ian Lynagh's avatar
Ian Lynagh committed
535 536
     foo :: Int = (error (# Int,Int #) "urk")
                  `cast` CoUnsafe (# Int,Int #) Int
537 538 539

If do the makeTrivial thing to the error call, we'll get
    foo = case error (# Int,Int #) "urk" of v -> v `cast` ...
Ian Lynagh's avatar
Ian Lynagh committed
540
But 'v' isn't in scope!
541 542

These strange casts can happen as a result of case-of-case
Ian Lynagh's avatar
Ian Lynagh committed
543 544
        bar = case (case x of { T -> (# 2,3 #); F -> error "urk" }) of
                (# p,q #) -> p+q
545

546 547

\begin{code}
548 549 550 551 552
makeTrivialArg :: SimplEnv -> ArgSpec -> SimplM (SimplEnv, ArgSpec)
makeTrivialArg env (ValArg e)  = do { (env', e') <- makeTrivial NotTopLevel env e
                                    ; return (env', ValArg e') }
makeTrivialArg env (CastBy co) = return (env, CastBy co)

simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
553
makeTrivial :: TopLevelFlag -> SimplEnv -> OutExpr -> SimplM (SimplEnv, OutExpr)
554
-- Binds the expression to a variable, if it's not trivial, returning the variable
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
555
makeTrivial top_lvl env expr = makeTrivialWithInfo top_lvl env vanillaIdInfo expr
556

557
makeTrivialWithInfo :: TopLevelFlag -> SimplEnv -> IdInfo
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
558
                    -> OutExpr -> SimplM (SimplEnv, OutExpr)
559 560
-- Propagate strictness and demand info to the new binder
-- Note [Preserve strictness when floating coercions]
561
-- Returned SimplEnv has same substitution as incoming one
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
562
makeTrivialWithInfo top_lvl env info expr
563 564 565
  | exprIsTrivial expr                          -- Already trivial
  || not (bindingOk top_lvl expr expr_ty)       -- Cannot trivialise
                                                --   See Note [Cannot trivialise]
566
  = return (env, expr)
Ian Lynagh's avatar
Ian Lynagh committed
567
  | otherwise           -- See Note [Take care] below
568 569
  = do  { uniq <- getUniqueM
        ; let name = mkSystemVarName uniq (fsLit "a")
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
570
              var = mkLocalIdWithInfo name expr_ty info
571
        ; env'  <- completeNonRecX top_lvl env False var var expr
572
        ; expr' <- simplVar env' var
573
        ; return (env', expr') }
574 575 576 577 578 579 580 581 582
        -- The simplVar is needed becase we're constructing a new binding
        --     a = rhs
        -- And if rhs is of form (rhs1 |> co), then we might get
        --     a1 = rhs1
        --     a = a1 |> co
        -- and now a's RHS is trivial and can be substituted out, and that
        -- is what completeNonRecX will do
        -- To put it another way, it's as if we'd simplified
        --    let var = e in var
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
583 584 585 586
  where
    expr_ty = exprType expr

bindingOk :: TopLevelFlag -> CoreExpr -> Type -> Bool
587
-- True iff we can have a binding of this expression at this level
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
588 589
-- Precondition: the type is the type of the expression
bindingOk top_lvl _ expr_ty
590
  | isTopLevel top_lvl = not (isUnLiftedType expr_ty)
simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
591
  | otherwise          = True
592
\end{code}
593

simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
594 595 596 597
Note [Cannot trivialise]
~~~~~~~~~~~~~~~~~~~~~~~~
Consider tih
   f :: Int -> Addr#
598

simonpj@microsoft.com's avatar
simonpj@microsoft.com committed
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614
   foo :: Bar
   foo = Bar (f 3)

Then we can't ANF-ise foo, even though we'd like to, because
we can't make a top-level binding for the Addr# (f 3). And if
so we don't want to turn it into
   foo = let x = f 3 in Bar x
because we'll just end up inlining x back, and that makes the
simplifier loop.  Better not to ANF-ise it at all.

A case in point is literal strings (a MachStr is not regarded as
trivial):

   foo = Ptr "blob"#

We don't want to ANF-ise this.
615

616
%************************************************************************
Ian Lynagh's avatar
Ian Lynagh committed
617
%*                                                                      *
618
\subsection{Completing a lazy binding}
Ian Lynagh's avatar
Ian Lynagh committed
619
%*                                                                      *
620 621
%************************************************************************

622 623 624 625 626
completeBind
  * deals only with Ids, not TyVars
  * takes an already-simplified binder and RHS
  * is used for both recursive and non-recursive bindings
  * is used for both top-level and non-top-level bindings
627 628 629 630 631 632 633 634

It does the following:
  - tries discarding a dead binding
  - tries PostInlineUnconditionally
  - add unfolding [this is the only place we add an unfolding]
  - add arity

It does *not* attempt to do let-to-case.  Why?  Because it is used for
Ian Lynagh's avatar
Ian Lynagh committed
635
  - top-level bindings (when let-to-case is impossible)
636
  - many situations where the "rhs" is known to be a WHNF
Ian Lynagh's avatar
Ian Lynagh committed
637
                (so let-to-case is inappropriate).
638

639 640
Nor does it do the atomic-argument thing

641
\begin{code}
642
completeBind :: SimplEnv
Ian Lynagh's avatar
Ian Lynagh committed
643 644 645 646 647 648 649
             -> TopLevelFlag            -- Flag stuck into unfolding
             -> InId                    -- Old binder
             -> OutId -> OutExpr        -- New binder and RHS
             -> SimplM SimplEnv
-- completeBind may choose to do its work
--      * by extending the substitution (e.g. let x = y in ...)
--      * or by adding to the floats in the envt
650 651
--
-- Precondition: rhs obeys the let/app invariant
652
completeBind env top_lvl old_bndr new_bndr new_rhs
653 654 655 656 657 658
 | isCoVar old_bndr
 = case new_rhs of
     Coercion co -> return (extendCvSubst env old_bndr co)
     _           -> return (addNonRec env new_bndr new_rhs)

 | otherwise
659 660
 = ASSERT( isId new_bndr )
   do { let old_info = idInfo old_bndr
661 662
            old_unf  = unfoldingInfo old_info
            occ_info = occInfo old_info
663

664
        -- Do eta-expansion on the RHS of the binding
665
        -- See Note [Eta-expanding at let bindings] in SimplUtils
666
      ; (new_arity, final_rhs) <- tryEtaExpandRhs env new_bndr new_rhs
667

668
        -- Simplify the unfolding
669
      ; new_unfolding <- simplUnfolding env top_lvl old_bndr final_rhs old_unf
670

671 672
      ; dflags <- getDynFlags
      ; if postInlineUnconditionally dflags env top_lvl new_bndr occ_info
673 674
                                     final_rhs new_unfolding

675 676 677 678 679 680
                        -- Inline and discard the binding
        then do  { tick (PostInlineUnconditionally old_bndr)
                 ; return (extendIdSubst env old_bndr (DoneEx final_rhs)) }
                -- Use the substitution to make quite, quite sure that the
                -- substitution will happen, since we are going to discard the binding
        else
681
   do { let info1 = idInfo new_bndr `setArityInfo` new_arity
682

683
              -- Unfolding info: Note [Setting the new unfolding]
684
            info2 = info1 `setUnfoldingInfo` new_unfolding
685

686
              -- Demand info: Note [Setting the demand info]
687 688 689 690 691 692
              --
              -- We also have to nuke demand info if for some reason
              -- eta-expansion *reduces* the arity of the binding to less
              -- than that of the strictness sig. This can happen: see Note [Arity decrease].
            info3 | isEvaldUnfolding new_unfolding
                    || (case strictnessInfo info2 of
693
                          StrictSig dmd_ty -> new_arity < dmdTypeDepth dmd_ty)
694 695 696
                  = zapDemandInfo info2 `orElse` info2
                  | otherwise
                  = info2
697 698

            final_id = new_bndr `setIdInfo` info3
699

700
      ; -- pprTrace "Binding" (ppr final_id <+> ppr new_unfolding) $
701
        return (addNonRec env final_id final_rhs) } }
702
                -- The addNonRec adds it to the in-scope set too
703 704 705

------------------------------
addPolyBind :: TopLevelFlag -> SimplEnv -> OutBind -> SimplM SimplEnv
706 707 708 709
-- Add a new binding to the environment, complete with its unfolding
-- but *do not* do postInlineUnconditionally, because we have already
-- processed some of the scope of the binding
-- We still want the unfolding though.  Consider
710 711 712
--      let
--            x = /\a. let y = ... in Just y
--      in body
713
-- Then we float the y-binding out (via abstractFloats and addPolyBind)
714
-- but 'x' may well then be inlined in 'body' in which case we'd like the
715
-- opportunity to inline 'y' too.
716 717
--
-- INVARIANT: the arity is correct on the incoming binders
718 719

addPolyBind top_lvl env (NonRec poly_id rhs)
720
  = do  { unfolding <- simplUnfolding env top_lvl poly_id rhs noUnfolding
721 722
                        -- Assumes that poly_id did not have an INLINE prag
                        -- which is perhaps wrong.  ToDo: think about this
723 724
        ; let final_id = setIdInfo poly_id $
                         idInfo poly_id `setUnfoldingInfo` unfolding
725

726
        ; return (addNonRec env final_id rhs) }
727

728
addPolyBind _ env bind@(Rec _)
729
  = return (extendFloats env bind)
730 731 732
        -- Hack: letrecs are more awkward, so we extend "by steam"
        -- without adding unfoldings etc.  At worst this leads to
        -- more simplifier iterations
733 734 735

------------------------------
simplUnfolding :: SimplEnv-> TopLevelFlag
736 737
               -> InId
               -> OutExpr
738
               -> Unfolding -> SimplM Unfolding
739
-- Note [Setting the new unfolding]
740 741 742 743 744 745 746
simplUnfolding env top_lvl id new_rhs unf
  = case unf of
      DFunUnfolding { df_bndrs = bndrs, df_con = con, df_args = args }
        -> do { (env', bndrs') <- simplBinders rule_env bndrs
              ; args' <- mapM (simplExpr env') args
              ; return (mkDFunUnfolding bndrs' con args') }

Simon Peyton Jones's avatar
Simon Peyton Jones committed
747
      CoreUnfolding { uf_tmpl = expr, uf_src = src, uf_guidance = guide }
748 749 750
        | isStableSource src
        -> do { expr' <- simplExpr rule_env expr
              ; case guide of
Simon Peyton Jones's avatar
Simon Peyton Jones committed
751 752 753
                  UnfWhen { ug_arity = arity, ug_unsat_ok = sat_ok }  -- Happens for INLINE things
                     -> let guide' = UnfWhen { ug_arity = arity, ug_unsat_ok = sat_ok
                                             , ug_boring_ok = inlineBoringOk expr' }
754 755 756 757 758
                        -- Refresh the boring-ok flag, in case expr'
                        -- has got small. This happens, notably in the inlinings
                        -- for dfuns for single-method classes; see
                        -- Note [Single-method classes] in TcInstDcls.
                        -- A test case is Trac #4138
Simon Peyton Jones's avatar
Simon Peyton Jones committed
759
                        in return (mkCoreUnfolding src is_top_lvl expr' guide')
760 761 762 763 764 765
                            -- See Note [Top-level flag on inline rules] in CoreUnfold

                  _other              -- Happens for INLINABLE things
                     -> bottoming `seq` -- See Note [Force bottoming field]
                        do { dflags <- getDynFlags
                           ; return (mkUnfolding dflags src is_top_lvl bottoming expr') } }
766 767 768
                -- If the guidance is UnfIfGoodArgs, this is an INLINABLE
                -- unfolding, and we need to make sure the guidance is kept up
                -- to date with respect to any changes in the unfolding.
769 770 771 772 773 774 775 776 777 778

      _other -> bottoming `seq`  -- See Note [Force bottoming field]
                do { dflags <- getDynFlags
                   ; return (mkUnfolding dflags InlineRhs is_top_lvl bottoming new_rhs) }
                     -- We make an  unfolding *even for loop-breakers*.
                     -- Reason: (a) It might be useful to know that they are WHNF
                     --         (b) In TidyPgm we currently assume that, if we want to
                     --             expose the unfolding then indeed we *have* an unfolding
                     --             to expose.  (We could instead use the RHS, but currently
                     --             we don't.)  The simple thing is always to have one.
779
  where
780 781
    bottoming = isBottomingId id
    is_top_lvl = isTopLevel top_lvl
782
    act      = idInlineActivation id
783 784
    rule_env = updMode (updModeForStableUnfoldings act) env
               -- See Note [Simplifying inside stable unfoldings] in SimplUtils
SamB's avatar
SamB committed
785
\end{code}
786

787 788 789 790 791
Note [Force bottoming field]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
We need to force bottoming, or the new unfolding holds
on to the old unfolding (which is part of the id).

792 793
Note [Arity decrease]
~~~~~~~~~~~~~~~~~~~~~
794
Generally speaking the arity of a binding should not decrease.  But it *can*
Gabor Greif's avatar
typos  
Gabor Greif committed
795
legitimately happen because of RULES.  Eg
796
        f = g Int
797
where g has arity 2, will have arity 2.  But if there's a rewrite rule
798
        g Int --> h
799 800 801 802
where h has arity 1, then f's arity will decrease.  Here's a real-life example,
which is in the output of Specialise:

     Rec {
803 804
        $dm {Arity 2} = \d.\x. op d
        {-# RULES forall d. $dm Int d = $s$dm #-}
805

806 807 808 809
        dInt = MkD .... opInt ...
        opInt {Arity 1} = $dm dInt

        $s$dm {Arity 0} = \x. op dInt }
810 811 812 813

Here opInt has arity 1; but when we apply the rule its arity drops to 0.
That's why Specialise goes to a little trouble to pin the right arity
on specialised functions too.
814

815 816 817
Note [Setting the new unfolding]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* If there's an INLINE pragma, we simplify the RHS gently.  Maybe we
818
  should do nothing at all, but simplifying gently might get rid of
819 820 821 822 823 824 825 826
  more crap.

* If not, we make an unfolding from the new RHS.  But *only* for
  non-loop-breakers. Making loop breakers not have an unfolding at all
  means that we can avoid tests in exprIsConApp, for example.  This is
  important: if exprIsConApp says 'yes' for a recursive thing, then we
  can get into an infinite loop

827 828 829
If there's an stable unfolding on a loop breaker (which happens for
INLINEABLE), we hang on to the inlining.  It's pretty dodgy, but the
user did say 'INLINE'.  May need to revisit this choice.
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846

Note [Setting the demand info]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
If the unfolding is a value, the demand info may
go pear-shaped, so we nuke it.  Example:
     let x = (a,b) in
     case x of (p,q) -> h p q x
Here x is certainly demanded. But after we've nuked
the case, we'll get just
     let x = (a,b) in h a b x
and now x is not demanded (I'm assuming h is lazy)
This really happens.  Similarly
     let f = \x -> e in ...f..f...
After inlining f at some of its call sites the original binding may
(for example) be no longer strictly demanded.
The solution here is a bit ad hoc...

847

848
%************************************************************************
Ian Lynagh's avatar
Ian Lynagh committed
849
%*                                                                      *
850
\subsection[Simplify-simplExpr]{The main function: simplExpr}
Ian Lynagh's avatar
Ian Lynagh committed
851
%*                                                                      *
852 853
%************************************************************************

854 855 856 857 858 859
The reason for this OutExprStuff stuff is that we want to float *after*
simplifying a RHS, not before.  If we do so naively we get quadratic
behaviour as things float out.

To see why it's important to do it after, consider this (real) example:

Ian Lynagh's avatar
Ian Lynagh committed
860 861
        let t = f x
        in fst t
862
==>
Ian Lynagh's avatar
Ian Lynagh committed
863 864 865 866
        let t = let a = e1
                    b = e2
                in (a,b)
        in fst t
867
==>
Ian Lynagh's avatar
Ian Lynagh committed
868 869 870 871 872
        let a = e1
            b = e2
            t = (a,b)
        in
        a       -- Can't inline a this round, cos it appears twice
873
==>
Ian Lynagh's avatar
Ian Lynagh committed
874
        e1
875 876 877 878

Each of the ==> steps is a round of simplification.  We'd save a
whole round if we float first.  This can cascade.  Consider

Ian Lynagh's avatar
Ian Lynagh committed
879 880