root/compiler/coreSyn/CoreUtils.lhs

Revision ebcad7641a1e37e2e4abd7f513feb10c4ee458bc, 71.3 KB (checked in by Simon Peyton Jones <simonpj@…>, 12 days ago)

When comparing Case expressions, take account of empty alternatives

After the recent change that allows empty case alternatives, we
were accidentally saying that these two were equal:

Case x _ Int []
Case x _ Bool []

Usually if the alternatives are equal so is the result type -- but
not if the alternatives are empty!

There are two places to fix:

CoreUtils?.eqExpr
TrieMap? with CoreExpr? key

Fixes #6096, #6097

  • Property mode set to 100644
Line 
1%
2% (c) The University of Glasgow 2006
3% (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4%
5
6Utility functions on @Core@ syntax
7
8\begin{code}
9-- | Commonly useful utilites for manipulating the Core language
10module CoreUtils (
11        -- * Constructing expressions
12        mkCast,
13        mkTick, mkTickNoHNF,
14        bindNonRec, needsCaseBinding,
15        mkAltExpr,
16
17        -- * Taking expressions apart
18        findDefault, findAlt, isDefaultAlt,
19        mergeAlts, trimConArgs, filterAlts,
20
21        -- * Properties of expressions
22        exprType, coreAltType, coreAltsType,
23        exprIsDupable, exprIsTrivial, getIdFromTrivialExpr, exprIsBottom,
24        exprIsCheap, exprIsExpandable, exprIsCheap', CheapAppFun,
25        exprIsHNF, exprOkForSpeculation, exprOkForSideEffects, exprIsWorkFree,
26        exprIsBig, exprIsConLike,
27        rhsIsStatic, isCheapApp, isExpandableApp,
28
29        -- * Expression and bindings size
30        coreBindsSize, exprSize,
31        CoreStats(..), coreBindsStats,
32
33        -- * Hashing
34        hashExpr,
35
36        -- * Equality
37        cheapEqExpr, eqExpr, eqExprX,
38
39        -- * Eta reduction
40        tryEtaReduce,
41
42        -- * Manipulating data constructors and types
43        applyTypeToArgs, applyTypeToArg,
44        dataConRepInstPat, dataConRepFSInstPat
45    ) where
46
47#include "HsVersions.h"
48
49import CoreSyn
50import PprCore
51import Var
52import SrcLoc
53import VarEnv
54import VarSet
55import Name
56import Literal
57import DataCon
58import PrimOp
59import Id
60import IdInfo
61import Type
62import Coercion
63import TyCon
64import Unique
65import Outputable
66import TysPrim
67import FastString
68import Maybes
69import Util
70import Pair
71import Data.Word
72import Data.Bits
73import Data.List
74\end{code}
75
76
77%************************************************************************
78%*                                                                      *
79\subsection{Find the type of a Core atom/expression}
80%*                                                                      *
81%************************************************************************
82
83\begin{code}
84exprType :: CoreExpr -> Type
85-- ^ Recover the type of a well-typed Core expression. Fails when
86-- applied to the actual 'CoreSyn.Type' expression as it cannot
87-- really be said to have a type
88exprType (Var var)           = idType var
89exprType (Lit lit)           = literalType lit
90exprType (Coercion co)       = coercionType co
91exprType (Let _ body)        = exprType body
92exprType (Case _ _ ty _)     = ty
93exprType (Cast _ co)         = pSnd (coercionKind co)
94exprType (Tick _ e)          = exprType e
95exprType (Lam binder expr)   = mkPiType binder (exprType expr)
96exprType e@(App _ _)
97  = case collectArgs e of
98        (fun, args) -> applyTypeToArgs e (exprType fun) args
99
100exprType other = pprTrace "exprType" (pprCoreExpr other) alphaTy
101
102coreAltType :: CoreAlt -> Type
103-- ^ Returns the type of the alternatives right hand side
104coreAltType (_,bs,rhs)
105  | any bad_binder bs = expandTypeSynonyms ty
106  | otherwise         = ty    -- Note [Existential variables and silly type synonyms]
107  where
108    ty           = exprType rhs
109    free_tvs     = tyVarsOfType ty
110    bad_binder b = isTyVar b && b `elemVarSet` free_tvs
111
112coreAltsType :: [CoreAlt] -> Type
113-- ^ Returns the type of the first alternative, which should be the same as for all alternatives
114coreAltsType (alt:_) = coreAltType alt
115coreAltsType []      = panic "corAltsType"
116\end{code}
117
118Note [Existential variables and silly type synonyms]
119~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
120Consider
121        data T = forall a. T (Funny a)
122        type Funny a = Bool
123        f :: T -> Bool
124        f (T x) = x
125
126Now, the type of 'x' is (Funny a), where 'a' is existentially quantified.
127That means that 'exprType' and 'coreAltsType' may give a result that *appears*
128to mention an out-of-scope type variable.  See Trac #3409 for a more real-world
129example.
130
131Various possibilities suggest themselves:
132
133 - Ignore the problem, and make Lint not complain about such variables
134
135 - Expand all type synonyms (or at least all those that discard arguments)
136      This is tricky, because at least for top-level things we want to
137      retain the type the user originally specified.
138
139 - Expand synonyms on the fly, when the problem arises. That is what
140   we are doing here.  It's not too expensive, I think.
141
142\begin{code}
143applyTypeToArg :: Type -> CoreExpr -> Type
144-- ^ Determines the type resulting from applying an expression to a function with the given type
145applyTypeToArg fun_ty (Type arg_ty) = applyTy fun_ty arg_ty
146applyTypeToArg fun_ty _             = funResultTy fun_ty
147
148applyTypeToArgs :: CoreExpr -> Type -> [CoreExpr] -> Type
149-- ^ A more efficient version of 'applyTypeToArg' when we have several arguments.
150-- The first argument is just for debugging, and gives some context
151applyTypeToArgs _ op_ty [] = op_ty
152
153applyTypeToArgs e op_ty (Type ty : args)
154  =     -- Accumulate type arguments so we can instantiate all at once
155    go [ty] args
156  where
157    go rev_tys (Type ty : args) = go (ty:rev_tys) args
158    go rev_tys rest_args         = applyTypeToArgs e op_ty' rest_args
159                                 where
160                                   op_ty' = applyTysD msg op_ty (reverse rev_tys)
161                                   msg = ptext (sLit "applyTypeToArgs") <+>
162                                         panic_msg e op_ty
163
164applyTypeToArgs e op_ty (_ : args)
165  = case (splitFunTy_maybe op_ty) of
166        Just (_, res_ty) -> applyTypeToArgs e res_ty args
167        Nothing -> pprPanic "applyTypeToArgs" (panic_msg e op_ty)
168
169panic_msg :: CoreExpr -> Type -> SDoc
170panic_msg e op_ty = pprCoreExpr e $$ ppr op_ty
171\end{code}
172
173%************************************************************************
174%*                                                                      *
175\subsection{Attaching notes}
176%*                                                                      *
177%************************************************************************
178
179\begin{code}
180-- | Wrap the given expression in the coercion safely, dropping
181-- identity coercions and coalescing nested coercions
182mkCast :: CoreExpr -> Coercion -> CoreExpr
183mkCast e co | isReflCo co = e
184
185mkCast (Coercion e_co) co
186  | isCoVarType (pSnd (coercionKind co))
187       -- The guard here checks that g has a (~#) on both sides,
188       -- otherwise decomposeCo fails.  Can in principle happen
189       -- with unsafeCoerce
190  = Coercion (mkCoCast e_co co)
191
192mkCast (Cast expr co2) co
193  = ASSERT(let { Pair  from_ty  _to_ty  = coercionKind co;
194                 Pair _from_ty2  to_ty2 = coercionKind co2} in
195           from_ty `eqType` to_ty2 )
196    mkCast expr (mkTransCo co2 co)
197
198mkCast expr co
199  = let Pair from_ty _to_ty = coercionKind co in
200--    if to_ty `eqType` from_ty
201--    then expr
202--    else
203        WARN(not (from_ty `eqType` exprType expr), text "Trying to coerce" <+> text "(" <> ppr expr $$ text "::" <+> ppr (exprType expr) <> text ")" $$ ppr co $$ pprEqPred (coercionKind co))
204         (Cast expr co)
205\end{code}
206
207\begin{code}
208-- | Wraps the given expression in the source annotation, dropping the
209-- annotation if possible.
210mkTick :: Tickish Id -> CoreExpr -> CoreExpr
211
212mkTick t (Var x)
213  | isFunTy (idType x) = Tick t (Var x)
214  | otherwise
215  = if tickishCounts t
216       then if tickishScoped t && tickishCanSplit t
217               then Tick (mkNoScope t) (Var x)
218               else Tick t (Var x)
219       else Var x
220
221mkTick t (Cast e co)
222  = Cast (mkTick t e) co -- Move tick inside cast
223
224mkTick _ (Coercion co) = Coercion co
225
226mkTick t (Lit l)
227  | not (tickishCounts t) = Lit l
228
229mkTick t expr@(App f arg)
230  | not (isRuntimeArg arg) = App (mkTick t f) arg
231  | isSaturatedConApp expr
232    = if not (tickishCounts t)
233         then tickHNFArgs t expr
234         else if tickishScoped t && tickishCanSplit t
235                 then Tick (mkNoScope t) (tickHNFArgs (mkNoTick t) expr)
236                 else Tick t expr
237
238mkTick t (Lam x e)
239     -- if this is a type lambda, or the tick does not count entries,
240     -- then we can push the tick inside:
241  | not (isRuntimeVar x) || not (tickishCounts t) = Lam x (mkTick t e)
242     -- if it is both counting and scoped, we split the tick into its
243     -- two components, keep the counting tick on the outside of the lambda
244     -- and push the scoped tick inside.  The point of this is that the
245     -- counting tick can probably be floated, and the lambda may then be
246     -- in a position to be beta-reduced.
247  | tickishScoped t && tickishCanSplit t
248         = Tick (mkNoScope t) (Lam x (mkTick (mkNoTick t) e))
249     -- just a counting tick: leave it on the outside
250  | otherwise        = Tick t (Lam x e)
251
252mkTick t other = Tick t other
253
254isSaturatedConApp :: CoreExpr -> Bool
255isSaturatedConApp e = go e []
256  where go (App f a) as = go f (a:as)
257        go (Var fun) args
258           = isConLikeId fun && idArity fun == valArgCount args
259        go (Cast f _) as = go f as
260        go _ _ = False
261
262mkTickNoHNF :: Tickish Id -> CoreExpr -> CoreExpr
263mkTickNoHNF t e
264  | exprIsHNF e = tickHNFArgs t e
265  | otherwise   = mkTick t e
266
267-- push a tick into the arguments of a HNF (call or constructor app)
268tickHNFArgs :: Tickish Id -> CoreExpr -> CoreExpr
269tickHNFArgs t e = push t e
270 where
271  push t (App f (Type u)) = App (push t f) (Type u)
272  push t (App f arg) = App (push t f) (mkTick t arg)
273  push _t e = e
274\end{code}
275
276%************************************************************************
277%*                                                                      *
278\subsection{Other expression construction}
279%*                                                                      *
280%************************************************************************
281
282\begin{code}
283bindNonRec :: Id -> CoreExpr -> CoreExpr -> CoreExpr
284-- ^ @bindNonRec x r b@ produces either:
285--
286-- > let x = r in b
287--
288-- or:
289--
290-- > case r of x { _DEFAULT_ -> b }
291--
292-- depending on whether we have to use a @case@ or @let@
293-- binding for the expression (see 'needsCaseBinding').
294-- It's used by the desugarer to avoid building bindings
295-- that give Core Lint a heart attack, although actually
296-- the simplifier deals with them perfectly well. See
297-- also 'MkCore.mkCoreLet'
298bindNonRec bndr rhs body
299  | needsCaseBinding (idType bndr) rhs = Case rhs bndr (exprType body) [(DEFAULT, [], body)]
300  | otherwise                          = Let (NonRec bndr rhs) body
301
302-- | Tests whether we have to use a @case@ rather than @let@ binding for this expression
303-- as per the invariants of 'CoreExpr': see "CoreSyn#let_app_invariant"
304needsCaseBinding :: Type -> CoreExpr -> Bool
305needsCaseBinding ty rhs = isUnLiftedType ty && not (exprOkForSpeculation rhs)
306        -- Make a case expression instead of a let
307        -- These can arise either from the desugarer,
308        -- or from beta reductions: (\x.e) (x +# y)
309\end{code}
310
311\begin{code}
312mkAltExpr :: AltCon     -- ^ Case alternative constructor
313          -> [CoreBndr] -- ^ Things bound by the pattern match
314          -> [Type]     -- ^ The type arguments to the case alternative
315          -> CoreExpr
316-- ^ This guy constructs the value that the scrutinee must have
317-- given that you are in one particular branch of a case
318mkAltExpr (DataAlt con) args inst_tys
319  = mkConApp con (map Type inst_tys ++ varsToCoreExprs args)
320mkAltExpr (LitAlt lit) [] []
321  = Lit lit
322mkAltExpr (LitAlt _) _ _ = panic "mkAltExpr LitAlt"
323mkAltExpr DEFAULT _ _ = panic "mkAltExpr DEFAULT"
324\end{code}
325
326
327%************************************************************************
328%*                                                                      *
329\subsection{Taking expressions apart}
330%*                                                                      *
331%************************************************************************
332
333The default alternative must be first, if it exists at all.
334This makes it easy to find, though it makes matching marginally harder.
335
336\begin{code}
337-- | Extract the default case alternative
338findDefault :: [(AltCon, [a], b)] -> ([(AltCon, [a], b)], Maybe b)
339findDefault ((DEFAULT,args,rhs) : alts) = ASSERT( null args ) (alts, Just rhs)
340findDefault alts                        =                     (alts, Nothing)
341
342isDefaultAlt :: (AltCon, a, b) -> Bool
343isDefaultAlt (DEFAULT, _, _) = True
344isDefaultAlt _               = False
345
346
347-- | Find the case alternative corresponding to a particular
348-- constructor: panics if no such constructor exists
349findAlt :: AltCon -> [(AltCon, a, b)] -> Maybe (AltCon, a, b)
350    -- A "Nothing" result *is* legitmiate
351    -- See Note [Unreachable code]
352findAlt con alts
353  = case alts of
354        (deflt@(DEFAULT,_,_):alts) -> go alts (Just deflt)
355        _                          -> go alts Nothing
356  where
357    go []                     deflt = deflt
358    go (alt@(con1,_,_) : alts) deflt
359      = case con `cmpAltCon` con1 of
360          LT -> deflt   -- Missed it already; the alts are in increasing order
361          EQ -> Just alt
362          GT -> ASSERT( not (con1 == DEFAULT) ) go alts deflt
363
364---------------------------------
365mergeAlts :: [(AltCon, a, b)] -> [(AltCon, a, b)] -> [(AltCon, a, b)]
366-- ^ Merge alternatives preserving order; alternatives in
367-- the first argument shadow ones in the second
368mergeAlts [] as2 = as2
369mergeAlts as1 [] = as1
370mergeAlts (a1:as1) (a2:as2)
371  = case a1 `cmpAlt` a2 of
372        LT -> a1 : mergeAlts as1      (a2:as2)
373        EQ -> a1 : mergeAlts as1      as2       -- Discard a2
374        GT -> a2 : mergeAlts (a1:as1) as2
375
376
377---------------------------------
378trimConArgs :: AltCon -> [CoreArg] -> [CoreArg]
379-- ^ Given:
380--
381-- > case (C a b x y) of
382-- >        C b x y -> ...
383--
384-- We want to drop the leading type argument of the scrutinee
385-- leaving the arguments to match agains the pattern
386
387trimConArgs DEFAULT      args = ASSERT( null args ) []
388trimConArgs (LitAlt _)   args = ASSERT( null args ) []
389trimConArgs (DataAlt dc) args = dropList (dataConUnivTyVars dc) args
390\end{code}
391
392\begin{code}
393filterAlts :: [Unique]             -- ^ Supply of uniques used in case we have to manufacture a new AltCon
394           -> Type                 -- ^ Type of scrutinee (used to prune possibilities)
395           -> [AltCon]             -- ^ 'imposs_cons': constructors known to be impossible due to the form of the scrutinee
396           -> [(AltCon, [Var], a)] -- ^ Alternatives
397           -> ([AltCon], Bool, [(AltCon, [Var], a)])
398             -- Returns:
399             --  1. Constructors that will never be encountered by the
400             --     *default* case (if any).  A superset of imposs_cons
401             --  2. Whether we managed to refine the default alternative into a specific constructor (for statistics only)
402             --  3. The new alternatives, trimmed by
403             --        a) remove imposs_cons
404             --        b) remove constructors which can't match because of GADTs
405             --      and with the DEFAULT expanded to a DataAlt if there is exactly
406             --      remaining constructor that can match
407             --
408             -- NB: the final list of alternatives may be empty:
409             -- This is a tricky corner case.  If the data type has no constructors,
410             -- which GHC allows, or if the imposs_cons covers all constructors (after taking
411             -- account of GADTs), then no alternatives can match.
412             --
413             -- If callers need to preserve the invariant that there is always at least one branch
414             -- in a "case" statement then they will need to manually add a dummy case branch that just
415             -- calls "error" or similar.
416filterAlts us ty imposs_cons alts = (imposs_deflt_cons, refined_deflt, merged_alts)
417  where
418    (alts_wo_default, maybe_deflt) = findDefault alts
419    alt_cons = [con | (con,_,_) <- alts_wo_default]
420    imposs_deflt_cons = nub (imposs_cons ++ alt_cons)
421      -- "imposs_deflt_cons" are handled
422      --   EITHER by the context,
423      --   OR by a non-DEFAULT branch in this case expression.
424
425    trimmed_alts = filterOut impossible_alt alts_wo_default
426    merged_alts  = mergeAlts trimmed_alts (maybeToList maybe_deflt')
427      -- We need the mergeAlts in case the new default_alt
428      -- has turned into a constructor alternative.
429      -- The merge keeps the inner DEFAULT at the front, if there is one
430      -- and interleaves the alternatives in the right order
431
432    (refined_deflt, maybe_deflt') = case maybe_deflt of
433      Just deflt_rhs -> case mb_tc_app of
434        Just (tycon, inst_tys)
435          |     -- This branch handles the case where we are
436                -- scrutinisng an algebraic data type
437            isAlgTyCon tycon            -- It's a data type, tuple, or unboxed tuples. 
438          , not (isNewTyCon tycon)      -- We can have a newtype, if we are just doing an eval:
439                                        --      case x of { DEFAULT -> e }
440                                        -- and we don't want to fill in a default for them!
441          , Just all_cons <- tyConDataCons_maybe tycon
442          , let imposs_data_cons = [con | DataAlt con <- imposs_deflt_cons]   -- We now know it's a data type
443                impossible con   = con `elem` imposs_data_cons || dataConCannotMatch inst_tys con
444          -> case filterOut impossible all_cons of
445               -- Eliminate the default alternative
446               -- altogether if it can't match:
447               []    -> (False, Nothing)
448               -- It matches exactly one constructor, so fill it in:
449               [con] -> (True, Just (DataAlt con, ex_tvs ++ arg_ids, deflt_rhs))
450                 where (ex_tvs, arg_ids) = dataConRepInstPat us con inst_tys
451               _     -> (False, Just (DEFAULT, [], deflt_rhs))
452
453          | debugIsOn, isAlgTyCon tycon
454          , null (tyConDataCons tycon)
455          , not (isFamilyTyCon tycon || isAbstractTyCon tycon)
456                -- Check for no data constructors
457                -- This can legitimately happen for abstract types and type families,
458                -- so don't report that
459          -> pprTrace "prepareDefault" (ppr tycon)
460             (False, Just (DEFAULT, [], deflt_rhs))
461
462        _ -> (False, Just (DEFAULT, [], deflt_rhs))
463      Nothing -> (False, Nothing)
464 
465    mb_tc_app = splitTyConApp_maybe ty
466    Just (_, inst_tys) = mb_tc_app
467
468    impossible_alt :: (AltCon, a, b) -> Bool
469    impossible_alt (con, _, _) | con `elem` imposs_cons = True
470    impossible_alt (DataAlt con, _, _) = dataConCannotMatch inst_tys con
471    impossible_alt _                   = False
472\end{code}
473
474Note [Unreachable code]
475~~~~~~~~~~~~~~~~~~~~~~~
476It is possible (although unusual) for GHC to find a case expression
477that cannot match.  For example:
478
479     data Col = Red | Green | Blue
480     x = Red
481     f v = case x of
482              Red -> ...
483              _ -> ...(case x of { Green -> e1; Blue -> e2 })...
484
485Suppose that for some silly reason, x isn't substituted in the case
486expression.  (Perhaps there's a NOINLINE on it, or profiling SCC stuff
487gets in the way; cf Trac #3118.)  Then the full-lazines pass might produce
488this
489
490     x = Red
491     lvl = case x of { Green -> e1; Blue -> e2 })
492     f v = case x of
493             Red -> ...
494             _ -> ...lvl...
495
496Now if x gets inlined, we won't be able to find a matching alternative
497for 'Red'.  That's because 'lvl' is unreachable.  So rather than crashing
498we generate (error "Inaccessible alternative").
499
500Similar things can happen (augmented by GADTs) when the Simplifier
501filters down the matching alternatives in Simplify.rebuildCase.
502
503
504%************************************************************************
505%*                                                                      *
506             exprIsTrivial
507%*                                                                      *
508%************************************************************************
509
510Note [exprIsTrivial]
511~~~~~~~~~~~~~~~~~~~~
512@exprIsTrivial@ is true of expressions we are unconditionally happy to
513                duplicate; simple variables and constants, and type
514                applications.  Note that primop Ids aren't considered
515                trivial unless
516
517Note [Variable are trivial]
518~~~~~~~~~~~~~~~~~~~~~~~~~~~
519There used to be a gruesome test for (hasNoBinding v) in the
520Var case:
521        exprIsTrivial (Var v) | hasNoBinding v = idArity v == 0
522The idea here is that a constructor worker, like \$wJust, is
523really short for (\x -> \$wJust x), becuase \$wJust has no binding.
524So it should be treated like a lambda.  Ditto unsaturated primops.
525But now constructor workers are not "have-no-binding" Ids.  And
526completely un-applied primops and foreign-call Ids are sufficiently
527rare that I plan to allow them to be duplicated and put up with
528saturating them.
529
530Note [Tick trivial]
531~~~~~~~~~~~~~~~~~~~
532Ticks are not trivial.  If we treat "tick<n> x" as trivial, it will be
533inlined inside lambdas and the entry count will be skewed, for
534example.  Furthermore "scc<n> x" will turn into just "x" in mkTick.
535
536\begin{code}
537exprIsTrivial :: CoreExpr -> Bool
538exprIsTrivial (Var _)          = True        -- See Note [Variables are trivial]
539exprIsTrivial (Type _)        = True
540exprIsTrivial (Coercion _)     = True
541exprIsTrivial (Lit lit)        = litIsTrivial lit
542exprIsTrivial (App e arg)      = not (isRuntimeArg arg) && exprIsTrivial e
543exprIsTrivial (Tick _ _)       = False  -- See Note [Tick trivial]
544exprIsTrivial (Cast e _)       = exprIsTrivial e
545exprIsTrivial (Lam b body)     = not (isRuntimeVar b) && exprIsTrivial body
546exprIsTrivial _                = False
547\end{code}
548
549When substituting in a breakpoint we need to strip away the type cruft
550from a trivial expression and get back to the Id.  The invariant is
551that the expression we're substituting was originally trivial
552according to exprIsTrivial.
553
554\begin{code}
555getIdFromTrivialExpr :: CoreExpr -> Id
556getIdFromTrivialExpr e = go e
557  where go (Var v) = v
558        go (App f t) | not (isRuntimeArg t) = go f
559        go (Cast e _) = go e
560        go (Lam b e) | not (isRuntimeVar b) = go e
561        go e = pprPanic "getIdFromTrivialExpr" (ppr e)
562\end{code}
563
564exprIsBottom is a very cheap and cheerful function; it may return
565False for bottoming expressions, but it never costs much to ask.
566See also CoreArity.exprBotStrictness_maybe, but that's a bit more
567expensive.
568
569\begin{code}
570exprIsBottom :: CoreExpr -> Bool
571exprIsBottom e
572  = go 0 e
573  where
574    go n (Var v) = isBottomingId v &&  n >= idArity v
575    go n (App e a) | isTypeArg a = go n e
576                   | otherwise   = go (n+1) e
577    go n (Tick _ e)              = go n e
578    go n (Cast e _)              = go n e
579    go n (Let _ e)               = go n e
580    go _ _                       = False
581\end{code}
582
583
584%************************************************************************
585%*                                                                      *
586             exprIsDupable
587%*                                                                      *
588%************************************************************************
589
590Note [exprIsDupable]
591~~~~~~~~~~~~~~~~~~~~
592@exprIsDupable@ is true of expressions that can be duplicated at a modest
593                cost in code size.  This will only happen in different case
594                branches, so there's no issue about duplicating work.
595
596                That is, exprIsDupable returns True of (f x) even if
597                f is very very expensive to call.
598
599                Its only purpose is to avoid fruitless let-binding
600                and then inlining of case join points
601
602
603\begin{code}
604exprIsDupable :: CoreExpr -> Bool
605exprIsDupable e
606  = isJust (go dupAppSize e)
607  where
608    go :: Int -> CoreExpr -> Maybe Int
609    go n (Type {})     = Just n
610    go n (Coercion {}) = Just n
611    go n (Var {})      = decrement n
612    go n (Tick _ e)    = go n e
613    go n (Cast e _)    = go n e
614    go n (App f a) | Just n' <- go n a = go n' f
615    go n (Lit lit) | litIsDupable lit = decrement n
616    go _ _ = Nothing
617
618    decrement :: Int -> Maybe Int
619    decrement 0 = Nothing
620    decrement n = Just (n-1)
621
622dupAppSize :: Int
623dupAppSize = 8   -- Size of term we are prepared to duplicate
624                 -- This is *just* big enough to make test MethSharing
625                 -- inline enough join points.  Really it should be
626                 -- smaller, and could be if we fixed Trac #4960.
627\end{code}
628
629%************************************************************************
630%*                                                                      *
631             exprIsCheap, exprIsExpandable
632%*                                                                      *
633%************************************************************************
634
635Note [exprIsWorkFree]
636~~~~~~~~~~~~~~~~~~~~~
637exprIsWorkFree is used when deciding whether to inline something; we
638don't inline it if doing so might duplicate work, by peeling off a
639complete copy of the expression.  Here we do not want even to
640duplicate a primop (Trac #5623):
641   eg   let x = a #+ b in x +# x
642   we do not want to inline/duplicate x
643
644Previously we were a bit more liberal, which led to the primop-duplicating
645problem.  However, being more conservative did lead to a big regression in
646one nofib benchmark, wheel-sieve1.  The situation looks like this:
647
648   let noFactor_sZ3 :: GHC.Types.Int -> GHC.Types.Bool
649       noFactor_sZ3 = case s_adJ of _ { GHC.Types.I# x_aRs ->
650         case GHC.Prim.<=# x_aRs 2 of _ {
651           GHC.Types.False -> notDivBy ps_adM qs_adN;
652           GHC.Types.True -> lvl_r2Eb }}
653       go = \x. ...(noFactor (I# y))....(go x')...
654
655The function 'noFactor' is heap-allocated and then called.  Turns out
656that 'notDivBy' is strict in its THIRD arg, but that is invisible to
657the caller of noFactor, which therefore cannot do w/w and
658heap-allocates noFactor's argument.  At the moment (May 12) we are just
659going to put up with this, because the previous more aggressive inlining
660(which treated 'noFactor' as work-free) was duplicating primops, which
661in turn was making inner loops of array calculations runs slow (#5623)
662
663\begin{code}
664exprIsWorkFree :: CoreExpr -> Bool
665-- See Note [exprIsWorkFree]
666exprIsWorkFree e = go 0 e
667  where    -- n is the number of value arguments
668    go _ (Lit {})                     = True
669    go _ (Type {})                    = True
670    go _ (Coercion {})                = True
671    go n (Cast e _)                   = go n e
672    go n (Case scrut _ _ alts)        = foldl (&&) (exprIsWorkFree scrut) 
673                                              [ go n rhs | (_,_,rhs) <- alts ]
674         -- See Note [Case expressions are work-free]
675    go _ (Let {})                     = False
676    go n (Var v)                      = n==0 || n < idArity v
677    go n (Tick t e) | tickishCounts t = False
678                    | otherwise       = go n e
679    go n (Lam x e)  | isRuntimeVar x = n==0 || go (n-1) e
680                    | otherwise      = go n e
681    go n (App f e)  | isRuntimeArg e = exprIsWorkFree e && go (n+1) f
682                    | otherwise      = go n f
683\end{code}
684
685Note [Case expressions are work-free]
686~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
687Are case-expressions work-free?  Consider
688    let v = case x of (p,q) -> p
689        go = \y -> ...case v of ...
690Should we inline 'v' at its use site inside the loop?  At the moment
691we do.  I experimented with saying that case are *not* work-free, but
692that increased allocation slightly.  It's a fairly small effect, and at
693the moment we go for the slightly more aggressive version which treats
694(case x of ....) as work-free if the alterantives are.
695
696
697Note [exprIsCheap]   See also Note [Interaction of exprIsCheap and lone variables]
698~~~~~~~~~~~~~~~~~~   in CoreUnfold.lhs
699@exprIsCheap@ looks at a Core expression and returns \tr{True} if
700it is obviously in weak head normal form, or is cheap to get to WHNF.
701[Note that that's not the same as exprIsDupable; an expression might be
702big, and hence not dupable, but still cheap.]
703
704By ``cheap'' we mean a computation we're willing to:
705        push inside a lambda, or
706        inline at more than one place
707That might mean it gets evaluated more than once, instead of being
708shared.  The main examples of things which aren't WHNF but are
709``cheap'' are:
710
711  *     case e of
712          pi -> ei
713        (where e, and all the ei are cheap)
714
715  *     let x = e in b
716        (where e and b are cheap)
717
718  *     op x1 ... xn
719        (where op is a cheap primitive operator)
720
721  *     error "foo"
722        (because we are happy to substitute it inside a lambda)
723
724Notice that a variable is considered 'cheap': we can push it inside a lambda,
725because sharing will make sure it is only evaluated once.
726
727Note [exprIsCheap and exprIsHNF]
728~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
729Note that exprIsHNF does not imply exprIsCheap.  Eg
730        let x = fac 20 in Just x
731This responds True to exprIsHNF (you can discard a seq), but
732False to exprIsCheap.
733
734\begin{code}
735exprIsCheap :: CoreExpr -> Bool
736exprIsCheap = exprIsCheap' isCheapApp
737
738exprIsExpandable :: CoreExpr -> Bool
739exprIsExpandable = exprIsCheap' isExpandableApp -- See Note [CONLIKE pragma] in BasicTypes
740
741type CheapAppFun = Id -> Int -> Bool
742exprIsCheap' :: CheapAppFun -> CoreExpr -> Bool
743exprIsCheap' _        (Lit _)      = True
744exprIsCheap' _        (Type _)    = True
745exprIsCheap' _        (Coercion _) = True
746exprIsCheap' _        (Var _)      = True
747exprIsCheap' good_app (Cast e _)   = exprIsCheap' good_app e
748exprIsCheap' good_app (Lam x e)    = isRuntimeVar x
749                                  || exprIsCheap' good_app e
750
751exprIsCheap' good_app (Case e _ _ alts) = exprIsCheap' good_app e &&
752                                          and [exprIsCheap' good_app rhs | (_,_,rhs) <- alts]
753        -- Experimentally, treat (case x of ...) as cheap
754        -- (and case __coerce x etc.)
755        -- This improves arities of overloaded functions where
756        -- there is only dictionary selection (no construction) involved
757
758exprIsCheap' good_app (Tick t e)
759  | tickishCounts t = False
760  | otherwise       = exprIsCheap' good_app e
761     -- never duplicate ticks.  If we get this wrong, then HPC's entry
762     -- counts will be off (check test in libraries/hpc/tests/raytrace)
763
764exprIsCheap' good_app (Let (NonRec x _) e)
765  | isUnLiftedType (idType x) = exprIsCheap' good_app e
766  | otherwise                 = False
767        -- Strict lets always have cheap right hand sides,
768        -- and do no allocation, so just look at the body
769        -- Non-strict lets do allocation so we don't treat them as cheap
770        -- See also
771
772exprIsCheap' good_app other_expr        -- Applications and variables
773  = go other_expr []
774  where
775        -- Accumulate value arguments, then decide
776    go (Cast e _) val_args                 = go e val_args
777    go (App f a) val_args | isRuntimeArg a = go f (a:val_args)
778                          | otherwise      = go f val_args
779
780    go (Var _) [] = True        -- Just a type application of a variable
781                                -- (f t1 t2 t3) counts as WHNF
782    go (Var f) args
783        = case idDetails f of
784                RecSelId {}                  -> go_sel args
785                ClassOpId {}                 -> go_sel args
786                PrimOpId op                  -> go_primop op args
787                _ | good_app f (length args) -> go_pap args
788                  | isBottomingId f          -> True
789                  | otherwise                -> False
790                        -- Application of a function which
791                        -- always gives bottom; we treat this as cheap
792                        -- because it certainly doesn't need to be shared!
793
794    go _ _ = False
795
796    --------------
797    go_pap args = all (exprIsCheap' good_app) args
798        -- Used to be "all exprIsTrivial args" due to concerns about
799        -- duplicating nested constructor applications, but see #4978.
800        -- The principle here is that
801        --    let x = a +# b in c *# x
802        -- should behave equivalently to
803        --    c *# (a +# b)
804        -- Since lets with cheap RHSs are accepted,
805        -- so should paps with cheap arguments
806
807    --------------
808    go_primop op args = primOpIsCheap op && all (exprIsCheap' good_app) args
809        -- In principle we should worry about primops
810        -- that return a type variable, since the result
811        -- might be applied to something, but I'm not going
812        -- to bother to check the number of args
813
814    --------------
815    go_sel [arg] = exprIsCheap' good_app arg    -- I'm experimenting with making record selection
816    go_sel _     = False                -- look cheap, so we will substitute it inside a
817                                        -- lambda.  Particularly for dictionary field selection.
818                -- BUT: Take care with (sel d x)!  The (sel d) might be cheap, but
819                --      there's no guarantee that (sel d x) will be too.  Hence (n_val_args == 1)
820
821isCheapApp :: CheapAppFun
822isCheapApp fn n_val_args
823  = isDataConWorkId fn
824  || n_val_args < idArity fn
825
826isExpandableApp :: CheapAppFun
827isExpandableApp fn n_val_args
828  =  isConLikeId fn
829  || n_val_args < idArity fn
830  || go n_val_args (idType fn)
831  where
832  -- See if all the arguments are PredTys (implicit params or classes)
833  -- If so we'll regard it as expandable; see Note [Expandable overloadings]
834     go 0 _ = True
835     go n_val_args ty
836       | Just (_, ty) <- splitForAllTy_maybe ty   = go n_val_args ty
837       | Just (arg, ty) <- splitFunTy_maybe ty
838       , isPredTy arg                             = go (n_val_args-1) ty
839       | otherwise                                = False
840\end{code}
841
842Note [Expandable overloadings]
843~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
844Suppose the user wrote this
845   {-# RULE  forall x. foo (negate x) = h x #-}
846   f x = ....(foo (negate x))....
847He'd expect the rule to fire. But since negate is overloaded, we might
848get this:
849    f = \d -> let n = negate d in \x -> ...foo (n x)...
850So we treat the application of a function (negate in this case) to a
851*dictionary* as expandable.  In effect, every function is CONLIKE when
852it's applied only to dictionaries.
853
854
855%************************************************************************
856%*                                                                      *
857             exprOkForSpeculation
858%*                                                                      *
859%************************************************************************
860
861\begin{code}
862-----------------------------
863-- | 'exprOkForSpeculation' returns True of an expression that is:
864--
865--  * Safe to evaluate even if normal order eval might not
866--    evaluate the expression at all, or
867--
868--  * Safe /not/ to evaluate even if normal order would do so
869--
870-- It is usually called on arguments of unlifted type, but not always
871-- In particular, Simplify.rebuildCase calls it on lifted types
872-- when a 'case' is a plain 'seq'. See the example in
873-- Note [exprOkForSpeculation: case expressions] below
874--
875-- Precisely, it returns @True@ iff:
876--
877--  * The expression guarantees to terminate,
878--  * soon,
879--  * without raising an exception,
880--  * without causing a side effect (e.g. writing a mutable variable)
881--
882-- Note that if @exprIsHNF e@, then @exprOkForSpecuation e@.
883-- As an example of the considerations in this test, consider:
884--
885-- > let x = case y# +# 1# of { r# -> I# r# }
886-- > in E
887--
888-- being translated to:
889--
890-- > case y# +# 1# of { r# ->
891-- >    let x = I# r#
892-- >    in E
893-- > }
894--
895-- We can only do this if the @y + 1@ is ok for speculation: it has no
896-- side effects, and can't diverge or raise an exception.
897exprOkForSpeculation, exprOkForSideEffects :: Expr b -> Bool
898exprOkForSpeculation = expr_ok primOpOkForSpeculation
899exprOkForSideEffects = expr_ok primOpOkForSideEffects
900  -- Polymorphic in binder type
901  -- There is one call at a non-Id binder type, in SetLevels
902
903expr_ok :: (PrimOp -> Bool) -> Expr b -> Bool
904expr_ok _ (Lit _)      = True
905expr_ok _ (Type _)     = True
906expr_ok _ (Coercion _) = True
907expr_ok primop_ok (Var v)      = app_ok primop_ok v []
908expr_ok primop_ok (Cast e _)   = expr_ok primop_ok e
909
910-- Tick annotations that *tick* cannot be speculated, because these
911-- are meant to identify whether or not (and how often) the particular
912-- source expression was evaluated at runtime.
913expr_ok primop_ok (Tick tickish e)
914   | tickishCounts tickish = False
915   | otherwise             = expr_ok primop_ok e
916
917expr_ok primop_ok (Case e _ _ alts)
918  =  expr_ok primop_ok e  -- Note [exprOkForSpeculation: case expressions]
919  && all (\(_,_,rhs) -> expr_ok primop_ok rhs) alts
920  && altsAreExhaustive alts     -- Note [Exhaustive alts]
921
922expr_ok primop_ok other_expr
923  = case collectArgs other_expr of
924        (Var f, args) -> app_ok primop_ok f args
925        _             -> False
926
927-----------------------------
928app_ok :: (PrimOp -> Bool) -> Id -> [Expr b] -> Bool
929app_ok primop_ok fun args
930  = case idDetails fun of
931      DFunId new_type ->  not new_type
932         -- DFuns terminate, unless the dict is implemented
933         -- with a newtype in which case they may not
934
935      DataConWorkId {} -> True
936                -- The strictness of the constructor has already
937                -- been expressed by its "wrapper", so we don't need
938                -- to take the arguments into account
939
940      PrimOpId op
941        | isDivOp op              -- Special case for dividing operations that fail
942        , [arg1, Lit lit] <- args -- only if the divisor is zero
943        -> not (isZeroLit lit) && expr_ok primop_ok arg1
944                  -- Often there is a literal divisor, and this
945                  -- can get rid of a thunk in an inner looop
946
947        | DataToTagOp <- op      -- See Note [dataToTag speculation]
948        -> True
949
950        | otherwise
951        -> primop_ok op        -- A bit conservative: we don't really need
952        && all (expr_ok primop_ok) args
953                                 
954                                  -- to care about lazy arguments, but this is easy
955
956      _other -> isUnLiftedType (idType fun)          -- c.f. the Var case of exprIsHNF
957             || idArity fun > n_val_args             -- Partial apps
958             || (n_val_args == 0 && 
959                 isEvaldUnfolding (idUnfolding fun)) -- Let-bound values
960             where
961               n_val_args = valArgCount args
962
963-----------------------------
964altsAreExhaustive :: [Alt b] -> Bool
965-- True  <=> the case alterantives are definiely exhaustive
966-- False <=> they may or may not be
967altsAreExhaustive []
968  = False    -- Should not happen
969altsAreExhaustive ((con1,_,_) : alts)
970  = case con1 of
971      DEFAULT   -> True
972      LitAlt {} -> False
973      DataAlt c -> 1 + length alts == tyConFamilySize (dataConTyCon c)
974      -- It is possible to have an exhaustive case that does not
975      -- enumerate all constructors, notably in a GADT match, but
976      -- we behave conservatively here -- I don't think it's important
977      -- enough to deserve special treatment
978
979-- | True of dyadic operators that can fail only if the second arg is zero!
980isDivOp :: PrimOp -> Bool
981-- This function probably belongs in PrimOp, or even in
982-- an automagically generated file.. but it's such a
983-- special case I thought I'd leave it here for now.
984isDivOp IntQuotOp        = True
985isDivOp IntRemOp         = True
986isDivOp WordQuotOp       = True
987isDivOp WordRemOp        = True
988isDivOp FloatDivOp       = True
989isDivOp DoubleDivOp      = True
990isDivOp _                = False
991\end{code}
992
993Note [exprOkForSpeculation: case expressions]
994~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
995It's always sound for exprOkForSpeculation to return False, and we
996don't want it to take too long, so it bales out on complicated-looking
997terms.  Notably lets, which can be stacked very deeply; and in any
998case the argument of exprOkForSpeculation is usually in a strict context,
999so any lets will have been floated away.
1000
1001However, we keep going on case-expressions.  An example like this one
1002showed up in DPH code (Trac #3717):
1003    foo :: Int -> Int
1004    foo 0 = 0
1005    foo n = (if n < 5 then 1 else 2) `seq` foo (n-1)
1006
1007If exprOkForSpeculation doesn't look through case expressions, you get this:
1008    T.$wfoo =
1009      \ (ww :: GHC.Prim.Int#) ->
1010        case ww of ds {
1011          __DEFAULT -> case (case <# ds 5 of _ {
1012                          GHC.Types.False -> lvl1;
1013                          GHC.Types.True -> lvl})
1014                       of _ { __DEFAULT ->
1015                       T.$wfoo (GHC.Prim.-# ds_XkE 1) };
1016          0 -> 0
1017        }
1018
1019The inner case is redundant, and should be nuked.
1020
1021Note [Exhaustive alts]
1022~~~~~~~~~~~~~~~~~~~~~~
1023We might have something like
1024  case x of {
1025    A -> ...
1026    _ -> ...(case x of { B -> ...; C -> ... })...
1027Here, the inner case is fine, because the A alternative
1028can't happen, but it's not ok to float the inner case outside
1029the outer one (even if we know x is evaluated outside), because
1030then it would be non-exhaustive. See Trac #5453.
1031
1032Similarly, this is a valid program (albeit a slightly dodgy one)
1033   let v = case x of { B -> ...; C -> ... }
1034   in case x of
1035         A -> ...
1036         _ ->  ...v...v....
1037But we don't want to speculate the v binding.
1038
1039One could try to be clever, but the easy fix is simpy to regard
1040a non-exhaustive case as *not* okForSpeculation.
1041
1042
1043Note [dataToTag speculation]
1044~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1045Is this OK?
1046   f x = let v::Int# = dataToTag# x
1047         in ...
1048We say "yes", even though 'x' may not be evaluated.  Reasons
1049
1050  * dataToTag#'s strictness means that its argument often will be
1051    evaluated, but FloatOut makes that temporarily untrue
1052         case x of y -> let v = dataToTag# y in ...
1053    -->
1054         case x of y -> let v = dataToTag# x in ...
1055    Note that we look at 'x' instead of 'y' (this is to improve
1056    floating in FloatOut).  So Lint complains.
1057
1058    Moreover, it really *might* improve floating to let the
1059    v-binding float out
1060
1061  * CorePrep makes sure dataToTag#'s argument is evaluated, just
1062    before code gen.  Until then, it's not guaranteed
1063
1064
1065%************************************************************************
1066%*                                                                      *
1067             exprIsHNF, exprIsConLike
1068%*                                                                      *
1069%************************************************************************
1070
1071\begin{code}
1072-- Note [exprIsHNF]             See also Note [exprIsCheap and exprIsHNF]
1073-- ~~~~~~~~~~~~~~~~
1074-- | exprIsHNF returns true for expressions that are certainly /already/
1075-- evaluated to /head/ normal form.  This is used to decide whether it's ok
1076-- to change:
1077--
1078-- > case x of _ -> e
1079--
1080--    into:
1081--
1082-- > e
1083--
1084-- and to decide whether it's safe to discard a 'seq'.
1085--
1086-- So, it does /not/ treat variables as evaluated, unless they say they are.
1087-- However, it /does/ treat partial applications and constructor applications
1088-- as values, even if their arguments are non-trivial, provided the argument
1089-- type is lifted. For example, both of these are values:
1090--
1091-- > (:) (f x) (map f xs)
1092-- > map (...redex...)
1093--
1094-- because 'seq' on such things completes immediately.
1095--
1096-- For unlifted argument types, we have to be careful:
1097--
1098-- > C (f x :: Int#)
1099--
1100-- Suppose @f x@ diverges; then @C (f x)@ is not a value. However this can't
1101-- happen: see "CoreSyn#let_app_invariant". This invariant states that arguments of
1102-- unboxed type must be ok-for-speculation (or trivial).
1103exprIsHNF :: CoreExpr -> Bool           -- True => Value-lambda, constructor, PAP
1104exprIsHNF = exprIsHNFlike isDataConWorkId isEvaldUnfolding
1105\end{code}
1106
1107\begin{code}
1108-- | Similar to 'exprIsHNF' but includes CONLIKE functions as well as
1109-- data constructors. Conlike arguments are considered interesting by the
1110-- inliner.
1111exprIsConLike :: CoreExpr -> Bool       -- True => lambda, conlike, PAP
1112exprIsConLike = exprIsHNFlike isConLikeId isConLikeUnfolding
1113
1114-- | Returns true for values or value-like expressions. These are lambdas,
1115-- constructors / CONLIKE functions (as determined by the function argument)
1116-- or PAPs.
1117--
1118exprIsHNFlike :: (Var -> Bool) -> (Unfolding -> Bool) -> CoreExpr -> Bool
1119exprIsHNFlike is_con is_con_unf = is_hnf_like
1120  where
1121    is_hnf_like (Var v) -- NB: There are no value args at this point
1122      =  is_con v       -- Catches nullary constructors,
1123                        --      so that [] and () are values, for example
1124      || idArity v > 0  -- Catches (e.g.) primops that don't have unfoldings
1125      || is_con_unf (idUnfolding v)
1126        -- Check the thing's unfolding; it might be bound to a value
1127        -- We don't look through loop breakers here, which is a bit conservative
1128        -- but otherwise I worry that if an Id's unfolding is just itself,
1129        -- we could get an infinite loop
1130
1131    is_hnf_like (Lit _)          = True
1132    is_hnf_like (Type _)         = True       -- Types are honorary Values;
1133                                              -- we don't mind copying them
1134    is_hnf_like (Coercion _)     = True       -- Same for coercions
1135    is_hnf_like (Lam b e)        = isRuntimeVar b || is_hnf_like e
1136    is_hnf_like (Tick tickish e) = not (tickishCounts tickish)
1137                                      && is_hnf_like e
1138                                      -- See Note [exprIsHNF Tick]
1139    is_hnf_like (Cast e _)           = is_hnf_like e
1140    is_hnf_like (App e (Type _))     = is_hnf_like e
1141    is_hnf_like (App e (Coercion _)) = is_hnf_like e
1142    is_hnf_like (App e a)            = app_is_value e [a]
1143    is_hnf_like (Let _ e)            = is_hnf_like e  -- Lazy let(rec)s don't affect us
1144    is_hnf_like _                    = False
1145
1146    -- There is at least one value argument
1147    app_is_value :: CoreExpr -> [CoreArg] -> Bool
1148    app_is_value (Var fun) args
1149      = idArity fun > valArgCount args    -- Under-applied function
1150        || is_con fun                     --  or constructor-like
1151    app_is_value (Tick _ f) as = app_is_value f as
1152    app_is_value (Cast f _) as = app_is_value f as
1153    app_is_value (App f a)  as = app_is_value f (a:as)
1154    app_is_value _          _  = False
1155
1156{-
1157Note [exprIsHNF Tick]
1158
1159We can discard source annotations on HNFs as long as they aren't
1160tick-like:
1161
1162  scc c (\x . e)    =>  \x . e
1163  scc c (C x1..xn)  =>  C x1..xn
1164
1165So we regard these as HNFs.  Tick annotations that tick are not
1166regarded as HNF if the expression they surround is HNF, because the
1167tick is there to tell us that the expression was evaluated, so we
1168don't want to discard a seq on it.
1169-}
1170\end{code}
1171
1172
1173%************************************************************************
1174%*                                                                      *
1175             Instantiating data constructors
1176%*                                                                      *
1177%************************************************************************
1178
1179These InstPat functions go here to avoid circularity between DataCon and Id
1180
1181\begin{code}
1182dataConRepInstPat   ::                 [Unique] -> DataCon -> [Type] -> ([TyVar], [Id])
1183dataConRepFSInstPat :: [FastString] -> [Unique] -> DataCon -> [Type] -> ([TyVar], [Id])
1184
1185dataConRepInstPat   = dataConInstPat (repeat ((fsLit "ipv")))
1186dataConRepFSInstPat = dataConInstPat
1187
1188dataConInstPat :: [FastString]          -- A long enough list of FSs to use for names
1189               -> [Unique]              -- An equally long list of uniques, at least one for each binder
1190               -> DataCon
1191               -> [Type]                -- Types to instantiate the universally quantified tyvars
1192               -> ([TyVar], [Id])          -- Return instantiated variables
1193-- dataConInstPat arg_fun fss us con inst_tys returns a triple
1194-- (ex_tvs, arg_ids),
1195--
1196--   ex_tvs are intended to be used as binders for existential type args
1197--
1198--   arg_ids are indended to be used as binders for value arguments,
1199--     and their types have been instantiated with inst_tys and ex_tys
1200--     The arg_ids include both evidence and
1201--     programmer-specified arguments (both after rep-ing)
1202--
1203-- Example.
1204--  The following constructor T1
1205--
1206--  data T a where
1207--    T1 :: forall b. Int -> b -> T(a,b)
1208--    ...
1209--
1210--  has representation type
1211--   forall a. forall a1. forall b. (a ~ (a1,b)) =>
1212--     Int -> b -> T a
1213--
1214--  dataConInstPat fss us T1 (a1',b') will return
1215--
1216--  ([a1'', b''], [c :: (a1', b')~(a1'', b''), x :: Int, y :: b''])
1217--
1218--  where the double-primed variables are created with the FastStrings and
1219--  Uniques given as fss and us
1220dataConInstPat fss uniqs con inst_tys
1221  = ASSERT( univ_tvs `equalLength` inst_tys )
1222    (ex_bndrs, arg_ids)
1223  where 
1224    univ_tvs = dataConUnivTyVars con
1225    ex_tvs   = dataConExTyVars con
1226    arg_tys  = dataConRepArgTys con
1227
1228    n_ex = length ex_tvs
1229
1230      -- split the Uniques and FastStrings
1231    (ex_uniqs, id_uniqs) = splitAt n_ex uniqs
1232    (ex_fss,   id_fss)   = splitAt n_ex fss
1233
1234      -- Make the instantiating substitution for universals
1235    univ_subst = zipOpenTvSubst univ_tvs inst_tys
1236
1237      -- Make existential type variables, applyingn and extending the substitution
1238    (full_subst, ex_bndrs) = mapAccumL mk_ex_var univ_subst
1239                                       (zip3 ex_tvs ex_fss ex_uniqs)
1240
1241    mk_ex_var :: TvSubst -> (TyVar, FastString, Unique) -> (TvSubst, TyVar)
1242    mk_ex_var subst (tv, fs, uniq) = (Type.extendTvSubst subst tv (mkTyVarTy new_tv)
1243                                     , new_tv)
1244      where
1245        new_tv   = mkTyVar new_name kind
1246        new_name = mkSysTvName uniq fs
1247        kind     = Type.substTy subst (tyVarKind tv)
1248
1249      -- Make value vars, instantiating types
1250    arg_ids = zipWith3 mk_id_var id_uniqs id_fss arg_tys
1251    mk_id_var uniq fs ty = mkUserLocal (mkVarOccFS fs) uniq
1252                                       (Type.substTy full_subst ty) noSrcSpan
1253\end{code}
1254
1255%************************************************************************
1256%*                                                                      *
1257         Equality
1258%*                                                                      *
1259%************************************************************************
1260
1261\begin{code}
1262-- | A cheap equality test which bales out fast!
1263--      If it returns @True@ the arguments are definitely equal,
1264--      otherwise, they may or may not be equal.
1265--
1266-- See also 'exprIsBig'
1267cheapEqExpr :: Expr b -> Expr b -> Bool
1268
1269cheapEqExpr (Var v1)   (Var v2)   = v1==v2
1270cheapEqExpr (Lit lit1) (Lit lit2) = lit1 == lit2
1271cheapEqExpr (Type t1) (Type t2) = t1 `eqType` t2
1272cheapEqExpr (Coercion c1) (Coercion c2) = c1 `coreEqCoercion` c2
1273
1274cheapEqExpr (App f1 a1) (App f2 a2)
1275  = f1 `cheapEqExpr` f2 && a1 `cheapEqExpr` a2
1276
1277cheapEqExpr (Cast e1 t1) (Cast e2 t2)
1278  = e1 `cheapEqExpr` e2 && t1 `coreEqCoercion` t2
1279
1280cheapEqExpr _ _ = False
1281\end{code}
1282
1283\begin{code}
1284exprIsBig :: Expr b -> Bool
1285-- ^ Returns @True@ of expressions that are too big to be compared by 'cheapEqExpr'
1286exprIsBig (Lit _)      = False
1287exprIsBig (Var _)      = False
1288exprIsBig (Type _)    = False
1289exprIsBig (Coercion _) = False
1290exprIsBig (Lam _ e)    = exprIsBig e
1291exprIsBig (App f a)    = exprIsBig f || exprIsBig a
1292exprIsBig (Cast e _)   = exprIsBig e    -- Hopefully coercions are not too big!
1293exprIsBig _            = True
1294\end{code}
1295
1296\begin{code}
1297eqExpr :: InScopeSet -> CoreExpr -> CoreExpr -> Bool
1298-- Compares for equality, modulo alpha
1299eqExpr in_scope e1 e2
1300  = eqExprX id_unf (mkRnEnv2 in_scope) e1 e2
1301  where
1302    id_unf _ = noUnfolding      -- Don't expand
1303\end{code}
1304
1305\begin{code}
1306eqExprX :: IdUnfoldingFun -> RnEnv2 -> CoreExpr -> CoreExpr -> Bool
1307-- ^ Compares expressions for equality, modulo alpha.
1308-- Does /not/ look through newtypes or predicate types
1309-- Used in rule matching, and also CSE
1310
1311eqExprX id_unfolding_fun env e1 e2
1312  = go env e1 e2
1313  where
1314    go env (Var v1) (Var v2)
1315      | rnOccL env v1 == rnOccR env v2
1316      = True
1317
1318    -- The next two rules expand non-local variables
1319    -- C.f. Note [Expanding variables] in Rules.lhs
1320    -- and  Note [Do not expand locally-bound variables] in Rules.lhs
1321    go env (Var v1) e2
1322      | not (locallyBoundL env v1)
1323      , Just e1' <- expandUnfolding_maybe (id_unfolding_fun (lookupRnInScope env v1))
1324      = go (nukeRnEnvL env) e1' e2
1325
1326    go env e1 (Var v2)
1327      | not (locallyBoundR env v2)
1328      , Just e2' <- expandUnfolding_maybe (id_unfolding_fun (lookupRnInScope env v2))
1329      = go (nukeRnEnvR env) e1 e2'
1330
1331    go _   (Lit lit1)    (Lit lit2)      = lit1 == lit2
1332    go env (Type t1)    (Type t2)        = eqTypeX env t1 t2
1333    go env (Coercion co1) (Coercion co2) = coreEqCoercion2 env co1 co2
1334    go env (Cast e1 co1) (Cast e2 co2) = coreEqCoercion2 env co1 co2 && go env e1 e2
1335    go env (App f1 a1)   (App f2 a2)   = go env f1 f2 && go env a1 a2
1336    go env (Tick n1 e1)  (Tick n2 e2)  = go_tickish n1 n2 && go env e1 e2
1337
1338    go env (Lam b1 e1)  (Lam b2 e2)
1339      =  eqTypeX env (varType b1) (varType b2)   -- False for Id/TyVar combination
1340      && go (rnBndr2 env b1 b2) e1 e2
1341
1342    go env (Let (NonRec v1 r1) e1) (Let (NonRec v2 r2) e2)
1343      =  go env r1 r2  -- No need to check binder types, since RHSs match
1344      && go (rnBndr2 env v1 v2) e1 e2
1345
1346    go env (Let (Rec ps1) e1) (Let (Rec ps2) e2)
1347      = all2 (go env') rs1 rs2 && go env' e1 e2
1348      where
1349        (bs1,rs1) = unzip ps1
1350        (bs2,rs2) = unzip ps2
1351        env' = rnBndrs2 env bs1 bs2
1352
1353    go env (Case e1 b1 t1 a1) (Case e2 b2 t2 a2)
1354      | null a1   -- See Note [Empty case alternatives] in TrieMap
1355      = null a2 && go env e1 e2 && eqTypeX env t1 t2
1356      | otherwise
1357      =  go env e1 e2 && all2 (go_alt (rnBndr2 env b1 b2)) a1 a2
1358
1359    go _ _ _ = False
1360
1361    -----------
1362    go_alt env (c1, bs1, e1) (c2, bs2, e2)
1363      = c1 == c2 && go (rnBndrs2 env bs1 bs2) e1 e2
1364
1365    -----------
1366    go_tickish (Breakpoint lid lids) (Breakpoint rid rids)
1367      = lid == rid  &&  map (rnOccL env) lids == map (rnOccR env) rids
1368    go_tickish l r = l == r
1369\end{code}
1370
1371Auxiliary functions
1372
1373\begin{code}
1374locallyBoundL, locallyBoundR :: RnEnv2 -> Var -> Bool
1375locallyBoundL rn_env v = inRnEnvL rn_env v
1376locallyBoundR rn_env v = inRnEnvR rn_env v
1377\end{code}
1378
1379
1380%************************************************************************
1381%*                                                                      *
1382\subsection{The size of an expression}
1383%*                                                                      *
1384%************************************************************************
1385
1386\begin{code}
1387data CoreStats = CS { cs_tm :: Int    -- Terms
1388                    , cs_ty :: Int    -- Types
1389                    , cs_co :: Int }  -- Coercions
1390
1391
1392instance Outputable CoreStats where 
1393 ppr (CS { cs_tm = i1, cs_ty = i2, cs_co = i3 })
1394   = braces (sep [ptext (sLit "terms:")     <+> intWithCommas i1 <> comma,
1395                  ptext (sLit "types:")     <+> intWithCommas i2 <> comma,
1396                  ptext (sLit "coercions:") <+> intWithCommas i3])
1397
1398plusCS :: CoreStats -> CoreStats -> CoreStats
1399plusCS (CS { cs_tm = p1, cs_ty = q1, cs_co = r1 })
1400       (CS { cs_tm = p2, cs_ty = q2, cs_co = r2 })
1401  = CS { cs_tm = p1+p2, cs_ty = q1+q2, cs_co = r1+r2 }
1402
1403zeroCS, oneTM :: CoreStats
1404zeroCS = CS { cs_tm = 0, cs_ty = 0, cs_co = 0 }
1405oneTM  = zeroCS { cs_tm = 1 }
1406
1407sumCS :: (a -> CoreStats) -> [a] -> CoreStats
1408sumCS f = foldr (plusCS . f) zeroCS
1409
1410coreBindsStats :: [CoreBind] -> CoreStats
1411coreBindsStats = sumCS bindStats
1412
1413bindStats :: CoreBind -> CoreStats
1414bindStats (NonRec v r) = bindingStats v r
1415bindStats (Rec prs)    = sumCS (\(v,r) -> bindingStats v r) prs
1416
1417bindingStats :: Var -> CoreExpr -> CoreStats
1418bindingStats v r = bndrStats v `plusCS` exprStats r
1419
1420bndrStats :: Var -> CoreStats
1421bndrStats v = oneTM `plusCS` tyStats (varType v)
1422
1423exprStats :: CoreExpr -> CoreStats
1424exprStats (Var {})        = oneTM
1425exprStats (Lit {})        = oneTM
1426exprStats (Type t)        = tyStats t
1427exprStats (Coercion c)    = coStats c
1428exprStats (App f a)       = exprStats f `plusCS` exprStats a
1429exprStats (Lam b e)       = bndrStats b `plusCS` exprStats e
1430exprStats (Let b e)       = bindStats b `plusCS` exprStats e
1431exprStats (Case e b _ as) = exprStats e `plusCS` bndrStats b `plusCS` sumCS altStats as
1432exprStats (Cast e co)     = coStats co `plusCS` exprStats e
1433exprStats (Tick _ e)      = exprStats e
1434
1435altStats :: CoreAlt -> CoreStats
1436altStats (_, bs, r) = sumCS bndrStats bs `plusCS` exprStats r
1437
1438tyStats :: Type -> CoreStats
1439tyStats ty = zeroCS { cs_ty = typeSize ty }
1440
1441coStats :: Coercion -> CoreStats
1442coStats co = zeroCS { cs_co = coercionSize co }
1443\end{code}
1444
1445
1446\begin{code}
1447coreBindsSize :: [CoreBind] -> Int
1448-- We use coreBindStats for user printout
1449-- but this one is a quick and dirty basis for
1450-- the simplifier's tick limit
1451coreBindsSize bs = foldr ((+) . bindSize) 0 bs
1452
1453exprSize :: CoreExpr -> Int
1454-- ^ A measure of the size of the expressions, strictly greater than 0
1455-- It also forces the expression pretty drastically as a side effect
1456-- Counts *leaves*, not internal nodes. Types and coercions are not counted.
1457exprSize (Var v)         = v `seq` 1
1458exprSize (Lit lit)       = lit `seq` 1
1459exprSize (App f a)       = exprSize f + exprSize a
1460exprSize (Lam b e)       = varSize b + exprSize e
1461exprSize (Let b e)       = bindSize b + exprSize e
1462exprSize (Case e b t as) = seqType t `seq` exprSize e + varSize b + 1 + foldr ((+) . altSize) 0 as
1463exprSize (Cast e co)     = (seqCo co `seq` 1) + exprSize e
1464exprSize (Tick n e)      = tickSize n + exprSize e
1465exprSize (Type t)        = seqType t `seq` 1
1466exprSize (Coercion co)   = seqCo co `seq` 1
1467
1468tickSize :: Tickish Id -> Int
1469tickSize (ProfNote cc _ _) = cc `seq` 1
1470tickSize _ = 1 -- the rest are strict
1471
1472varSize :: Var -> Int
1473varSize b  | isTyVar b = 1
1474           | otherwise = seqType (idType b)             `seq`
1475                         megaSeqIdInfo (idInfo b)       `seq`
1476                         1
1477
1478varsSize :: [Var] -> Int
1479varsSize = sum . map varSize
1480
1481bindSize :: CoreBind -> Int
1482bindSize (NonRec b e) = varSize b + exprSize e
1483bindSize (Rec prs)    = foldr ((+) . pairSize) 0 prs
1484
1485pairSize :: (Var, CoreExpr) -> Int
1486pairSize (b,e) = varSize b + exprSize e
1487
1488altSize :: CoreAlt -> Int
1489altSize (c,bs,e) = c `seq` varsSize bs + exprSize e
1490\end{code}
1491
1492
1493%************************************************************************
1494%*                                                                      *
1495\subsection{Hashing}
1496%*                                                                      *
1497%************************************************************************
1498
1499\begin{code}
1500hashExpr :: CoreExpr -> Int
1501-- ^ Two expressions that hash to the same @Int@ may be equal (but may not be)
1502-- Two expressions that hash to the different Ints are definitely unequal.
1503--
1504-- The emphasis is on a crude, fast hash, rather than on high precision.
1505--
1506-- But unequal here means \"not identical\"; two alpha-equivalent
1507-- expressions may hash to the different Ints.
1508--
1509-- We must be careful that @\\x.x@ and @\\y.y@ map to the same hash code,
1510-- (at least if we want the above invariant to be true).
1511
1512hashExpr e = fromIntegral (hash_expr (1,emptyVarEnv) e .&. 0x7fffffff)
1513             -- UniqFM doesn't like negative Ints
1514
1515type HashEnv = (Int, VarEnv Int)  -- Hash code for bound variables
1516
1517hash_expr :: HashEnv -> CoreExpr -> Word32
1518-- Word32, because we're expecting overflows here, and overflowing
1519-- signed types just isn't cool.  In C it's even undefined.
1520hash_expr env (Tick _ e)              = hash_expr env e
1521hash_expr env (Cast e _)              = hash_expr env e
1522hash_expr env (Var v)                 = hashVar env v
1523hash_expr _   (Lit lit)               = fromIntegral (hashLiteral lit)
1524hash_expr env (App f e)               = hash_expr env f * fast_hash_expr env e
1525hash_expr env (Let (NonRec b r) e)    = hash_expr (extend_env env b) e * fast_hash_expr env r
1526hash_expr env (Let (Rec ((b,_):_)) e) = hash_expr (extend_env env b) e
1527hash_expr _   (Let (Rec []) _)        = panic "hash_expr: Let (Rec []) _"
1528hash_expr env (Case e _ _ _)          = hash_expr env e
1529hash_expr env (Lam b e)               = hash_expr (extend_env env b) e
1530hash_expr env (Coercion co)           = fast_hash_co env co
1531hash_expr _   (Type _)                = WARN(True, text "hash_expr: type") 1
1532-- Shouldn't happen.  Better to use WARN than trace, because trace
1533-- prevents the CPR optimisation kicking in for hash_expr.
1534
1535fast_hash_expr :: HashEnv -> CoreExpr -> Word32
1536fast_hash_expr env (Var v)       = hashVar env v
1537fast_hash_expr env (Type t)      = fast_hash_type env t
1538fast_hash_expr env (Coercion co) = fast_hash_co env co
1539fast_hash_expr _   (Lit lit)     = fromIntegral (hashLiteral lit)
1540fast_hash_expr env (Cast e _)    = fast_hash_expr env e
1541fast_hash_expr env (Tick _ e)    = fast_hash_expr env e
1542fast_hash_expr env (App _ a)     = fast_hash_expr env a -- A bit idiosyncratic ('a' not 'f')!
1543fast_hash_expr _   _             = 1
1544
1545fast_hash_type :: HashEnv -> Type -> Word32
1546fast_hash_type env ty
1547  | Just tv <- getTyVar_maybe ty            = hashVar env tv
1548  | Just (tc,tys) <- splitTyConApp_maybe ty = let hash_tc = fromIntegral (hashName (tyConName tc))
1549                                              in foldr (\t n -> fast_hash_type env t + n) hash_tc tys
1550  | otherwise                               = 1
1551
1552fast_hash_co :: HashEnv -> Coercion -> Word32
1553fast_hash_co env co
1554  | Just cv <- getCoVar_maybe co              = hashVar env cv
1555  | Just (tc,cos) <- splitTyConAppCo_maybe co = let hash_tc = fromIntegral (hashName (tyConName tc))
1556                                                in foldr (\c n -> fast_hash_co env c + n) hash_tc cos
1557  | otherwise                                 = 1
1558
1559extend_env :: HashEnv -> Var -> (Int, VarEnv Int)
1560extend_env (n,env) b = (n+1, extendVarEnv env b n)
1561
1562hashVar :: HashEnv -> Var -> Word32
1563hashVar (_,env) v
1564 = fromIntegral (lookupVarEnv env v `orElse` hashName (idName v))
1565\end{code}
1566
1567
1568%************************************************************************
1569%*                                                                      *
1570                Eta reduction
1571%*                                                                      *
1572%************************************************************************
1573
1574Note [Eta reduction conditions]
1575~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1576We try for eta reduction here, but *only* if we get all the way to an
1577trivial expression.  We don't want to remove extra lambdas unless we
1578are going to avoid allocating this thing altogether.
1579
1580There are some particularly delicate points here:
1581
1582* Eta reduction is not valid in general:
1583        \x. bot  /=  bot
1584  This matters, partly for old-fashioned correctness reasons but,
1585  worse, getting it wrong can yield a seg fault. Consider
1586        f = \x.f x
1587        h y = case (case y of { True -> f `seq` True; False -> False }) of
1588                True -> ...; False -> ...
1589
1590  If we (unsoundly) eta-reduce f to get f=f, the strictness analyser
1591  says f=bottom, and replaces the (f `seq` True) with just
1592  (f `cast` unsafe-co).  BUT, as thing stand, 'f' got arity 1, and it
1593  *keeps* arity 1 (perhaps also wrongly).  So CorePrep eta-expands
1594  the definition again, so that it does not termninate after all.
1595  Result: seg-fault because the boolean case actually gets a function value.
1596  See Trac #1947.
1597
1598  So it's important to to the right thing.
1599
1600* Note [Arity care]: we need to be careful if we just look at f's
1601  arity. Currently (Dec07), f's arity is visible in its own RHS (see
1602  Note [Arity robustness] in SimplEnv) so we must *not* trust the
1603  arity when checking that 'f' is a value.  Otherwise we will
1604  eta-reduce
1605      f = \x. f x
1606  to
1607      f = f
1608  Which might change a terminiating program (think (f `seq` e)) to a
1609  non-terminating one.  So we check for being a loop breaker first.
1610
1611  However for GlobalIds we can look at the arity; and for primops we
1612  must, since they have no unfolding.
1613
1614* Regardless of whether 'f' is a value, we always want to
1615  reduce (/\a -> f a) to f
1616  This came up in a RULE: foldr (build (/\a -> g a))
1617  did not match           foldr (build (/\b -> ...something complex...))
1618  The type checker can insert these eta-expanded versions,
1619  with both type and dictionary lambdas; hence the slightly
1620  ad-hoc isDictId
1621
1622* Never *reduce* arity. For example
1623      f = \xy. g x y
1624  Then if h has arity 1 we don't want to eta-reduce because then
1625  f's arity would decrease, and that is bad
1626
1627These delicacies are why we don't use exprIsTrivial and exprIsHNF here.
1628Alas.
1629
1630Note [Eta reduction with casted arguments]
1631~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1632Consider
1633    (\(x:t3). f (x |> g)) :: t3 -> t2
1634  where
1635    f :: t1 -> t2
1636    g :: t3 ~ t1
1637This should be eta-reduced to
1638
1639    f |> (sym g -> t2)
1640
1641So we need to accumulate a coercion, pushing it inward (past
1642variable arguments only) thus:
1643   f (x |> co_arg) |> co  --(f |> (sym co_arg -> co)) x
1644   f (x:t)         |> co  --(f |> (t -> co)) x
1645   f @ a           |> co  --(f |> (forall a.co)) @ a
1646   f @ (g:t1~t2)   |> co  --(f |> (t1~t2 => co)) @ (g:t1~t2)
1647These are the equations for ok_arg.
1648
1649It's true that we could also hope to eta reduce these:
1650    (\xy. (f x |> g) y)
1651    (\xy. (f x y) |> g)
1652But the simplifier pushes those casts outwards, so we don't
1653need to address that here.
1654
1655\begin{code}
1656tryEtaReduce :: [Var] -> CoreExpr -> Maybe CoreExpr
1657tryEtaReduce bndrs body
1658  = go (reverse bndrs) body (mkReflCo (exprType body))
1659  where
1660    incoming_arity = count isId bndrs
1661
1662    go :: [Var]            -- Binders, innermost first, types [a3,a2,a1]
1663       -> CoreExpr         -- Of type tr
1664       -> Coercion         -- Of type tr ~ ts
1665       -> Maybe CoreExpr   -- Of type a1 -> a2 -> a3 -> ts
1666    -- See Note [Eta reduction with casted arguments]
1667    -- for why we have an accumulating coercion
1668    go [] fun co
1669      | ok_fun fun = Just (mkCast fun co)
1670
1671    go (b : bs) (App fun arg) co
1672      | Just co' <- ok_arg b arg co
1673      = go bs fun co'
1674
1675    go _ _ _  = Nothing         -- Failure!
1676
1677    ---------------
1678    -- Note [Eta reduction conditions]
1679    ok_fun (App fun (Type ty))
1680        | not (any (`elemVarSet` tyVarsOfType ty) bndrs)
1681        =  ok_fun fun
1682    ok_fun (Var fun_id)
1683        =  not (fun_id `elem` bndrs)
1684        && (ok_fun_id fun_id || all ok_lam bndrs)
1685    ok_fun _fun = False
1686
1687    ---------------
1688    ok_fun_id fun = fun_arity fun >= incoming_arity
1689
1690    ---------------
1691    fun_arity fun             -- See Note [Arity care]
1692       | isLocalId fun && isStrongLoopBreaker (idOccInfo fun) = 0
1693       | otherwise = idArity fun
1694
1695    ---------------
1696    ok_lam v = isTyVar v || isEvVar v
1697
1698    ---------------
1699    ok_arg :: Var              -- Of type bndr_t
1700           -> CoreExpr         -- Of type arg_t
1701           -> Coercion         -- Of kind (t1~t2)
1702           -> Maybe Coercion   -- Of type (arg_t -> t1 ~  bndr_t -> t2)
1703                               --   (and similarly for tyvars, coercion args)
1704    -- See Note [Eta reduction with casted arguments]
1705    ok_arg bndr (Type ty) co
1706       | Just tv <- getTyVar_maybe ty
1707       , bndr == tv  = Just (mkForAllCo tv co)
1708    ok_arg bndr (Var v) co
1709       | bndr == v   = Just (mkFunCo (mkReflCo (idType bndr)) co)
1710    ok_arg bndr (Cast (Var v) co_arg) co
1711       | bndr == v  = Just (mkFunCo (mkSymCo co_arg) co)
1712       -- The simplifier combines multiple casts into one,
1713       -- so we can have a simple-minded pattern match here
1714    ok_arg _ _ _ = Nothing
1715\end{code}
1716
1717
1718%************************************************************************
1719%*                                                                      *
1720\subsection{Determining non-updatable right-hand-sides}
1721%*                                                                      *
1722%************************************************************************
1723
1724Top-level constructor applications can usually be allocated
1725statically, but they can't if the constructor, or any of the
1726arguments, come from another DLL (because we can't refer to static
1727labels in other DLLs).
1728
1729If this happens we simply make the RHS into an updatable thunk,
1730and 'execute' it rather than allocating it statically.
1731
1732\begin{code}
1733-- | This function is called only on *top-level* right-hand sides.
1734-- Returns @True@ if the RHS can be allocated statically in the output,
1735-- with no thunks involved at all.
1736rhsIsStatic :: (Name -> Bool) -> CoreExpr -> Bool
1737-- It's called (i) in TidyPgm.hasCafRefs to decide if the rhs is, or
1738-- refers to, CAFs; (ii) in CoreToStg to decide whether to put an
1739-- update flag on it and (iii) in DsExpr to decide how to expand
1740-- list literals
1741--
1742-- The basic idea is that rhsIsStatic returns True only if the RHS is
1743--      (a) a value lambda
1744--      (b) a saturated constructor application with static args
1745--
1746-- BUT watch out for
1747--  (i) Any cross-DLL references kill static-ness completely
1748--      because they must be 'executed' not statically allocated
1749--      ("DLL" here really only refers to Windows DLLs, on other platforms,
1750--      this is not necessary)
1751--
1752-- (ii) We treat partial applications as redexes, because in fact we
1753--      make a thunk for them that runs and builds a PAP
1754--      at run-time.  The only appliations that are treated as
1755--      static are *saturated* applications of constructors.
1756
1757-- We used to try to be clever with nested structures like this:
1758--              ys = (:) w ((:) w [])
1759-- on the grounds that CorePrep will flatten ANF-ise it later.
1760-- But supporting this special case made the function much more
1761-- complicated, because the special case only applies if there are no
1762-- enclosing type lambdas:
1763--              ys = /\ a -> Foo (Baz ([] a))
1764-- Here the nested (Baz []) won't float out to top level in CorePrep.
1765--
1766-- But in fact, even without -O, nested structures at top level are
1767-- flattened by the simplifier, so we don't need to be super-clever here.
1768--
1769-- Examples
1770--
1771--      f = \x::Int. x+7        TRUE
1772--      p = (True,False)        TRUE
1773--
1774--      d = (fst p, False)      FALSE because there's a redex inside
1775--                              (this particular one doesn't happen but...)
1776--
1777--      h = D# (1.0## /## 2.0##)        FALSE (redex again)
1778--      n = /\a. Nil a                  TRUE
1779--
1780--      t = /\a. (:) (case w a of ...) (Nil a)  FALSE (redex)
1781--
1782--
1783-- This is a bit like CoreUtils.exprIsHNF, with the following differences:
1784--    a) scc "foo" (\x -> ...) is updatable (so we catch the right SCC)
1785--
1786--    b) (C x xs), where C is a contructor is updatable if the application is
1787--         dynamic
1788--
1789--    c) don't look through unfolding of f in (f x).
1790
1791rhsIsStatic _is_dynamic_name rhs = is_static False rhs
1792  where
1793  is_static :: Bool     -- True <=> in a constructor argument; must be atomic
1794            -> CoreExpr -> Bool
1795
1796  is_static False (Lam b e)             = isRuntimeVar b || is_static False e
1797  is_static in_arg (Tick n e)           = not (tickishIsCode n)
1798                                            && is_static in_arg e
1799  is_static in_arg (Cast e _)           = is_static in_arg e
1800  is_static _      (Coercion {})        = True   -- Behaves just like a literal
1801  is_static _      (Lit (LitInteger {})) = False
1802  is_static _      (Lit (MachLabel {})) = False
1803  is_static _      (Lit _)              = True
1804        -- A MachLabel (foreign import "&foo") in an argument
1805        -- prevents a constructor application from being static.  The
1806        -- reason is that it might give rise to unresolvable symbols
1807        -- in the object file: under Linux, references to "weak"
1808        -- symbols from the data segment give rise to "unresolvable
1809        -- relocation" errors at link time This might be due to a bug
1810        -- in the linker, but we'll work around it here anyway.
1811        -- SDM 24/2/2004
1812
1813  is_static in_arg other_expr = go other_expr 0
1814   where
1815    go (Var f) n_val_args
1816#if mingw32_TARGET_OS
1817        | not (_is_dynamic_name (idName f))
1818#endif
1819        =  saturated_data_con f n_val_args
1820        || (in_arg && n_val_args == 0)
1821                -- A naked un-applied variable is *not* deemed a static RHS
1822                -- E.g.         f = g
1823                -- Reason: better to update so that the indirection gets shorted
1824                --         out, and the true value will be seen
1825                -- NB: if you change this, you'll break the invariant that THUNK_STATICs
1826                --     are always updatable.  If you do so, make sure that non-updatable
1827                --     ones have enough space for their static link field!
1828
1829    go (App f a) n_val_args
1830        | isTypeArg a                    = go f n_val_args
1831        | not in_arg && is_static True a = go f (n_val_args + 1)
1832        -- The (not in_arg) checks that we aren't in a constructor argument;
1833        -- if we are, we don't allow (value) applications of any sort
1834        --
1835        -- NB. In case you wonder, args are sometimes not atomic.  eg.
1836        --   x = D# (1.0## /## 2.0##)
1837        -- can't float because /## can fail.
1838
1839    go (Tick n f) n_val_args = not (tickishIsCode n) && go f n_val_args
1840    go (Cast e _) n_val_args = go e n_val_args
1841    go _          _          = False
1842
1843    saturated_data_con f n_val_args
1844        = case isDataConWorkId_maybe f of
1845            Just dc -> n_val_args == dataConRepArity dc
1846            Nothing -> False
1847\end{code}
Note: See TracBrowser for help on using the browser.