Improve the runRW magic in CorePrep
[ghc.git] / compiler / coreSyn / CorePrep.hs
1 {-
2 (c) The University of Glasgow, 1994-2006
3
4
5 Core pass to saturate constructors and PrimOps
6 -}
7
8 {-# LANGUAGE BangPatterns, CPP #-}
9
10 module CorePrep (
11 corePrepPgm, corePrepExpr, cvtLitInteger,
12 lookupMkIntegerName, lookupIntegerSDataConName
13 ) where
14
15 #include "HsVersions.h"
16
17 import OccurAnal
18
19 import HscTypes
20 import PrelNames
21 import MkId ( realWorldPrimId )
22 import CoreUtils
23 import CoreArity
24 import CoreFVs
25 import CoreMonad ( CoreToDo(..) )
26 import CoreLint ( endPassIO )
27 import CoreSyn
28 import CoreSubst
29 import MkCore hiding( FloatBind(..) ) -- We use our own FloatBind here
30 import Type
31 import Literal
32 import Coercion
33 import TcEnv
34 import TyCon
35 import Demand
36 import Var
37 import VarSet
38 import VarEnv
39 import Id
40 import IdInfo
41 import TysWiredIn
42 import DataCon
43 import PrimOp
44 import BasicTypes
45 import Module
46 import UniqSupply
47 import Maybes
48 import OrdList
49 import ErrUtils
50 import DynFlags
51 import Util
52 import Pair
53 import Outputable
54 import Platform
55 import FastString
56 import Config
57 import Name ( NamedThing(..), nameSrcSpan )
58 import SrcLoc ( SrcSpan(..), realSrcLocSpan, mkRealSrcLoc )
59 import Data.Bits
60 import MonadUtils ( mapAccumLM )
61 import Data.List ( mapAccumL )
62 import Control.Monad
63
64 #if __GLASGOW_HASKELL__ < 710
65 import Control.Applicative
66 #endif
67
68 {-
69 -- ---------------------------------------------------------------------------
70 -- Overview
71 -- ---------------------------------------------------------------------------
72
73 The goal of this pass is to prepare for code generation.
74
75 1. Saturate constructor and primop applications.
76
77 2. Convert to A-normal form; that is, function arguments
78 are always variables.
79
80 * Use case for strict arguments:
81 f E ==> case E of x -> f x
82 (where f is strict)
83
84 * Use let for non-trivial lazy arguments
85 f E ==> let x = E in f x
86 (were f is lazy and x is non-trivial)
87
88 3. Similarly, convert any unboxed lets into cases.
89 [I'm experimenting with leaving 'ok-for-speculation'
90 rhss in let-form right up to this point.]
91
92 4. Ensure that *value* lambdas only occur as the RHS of a binding
93 (The code generator can't deal with anything else.)
94 Type lambdas are ok, however, because the code gen discards them.
95
96 5. [Not any more; nuked Jun 2002] Do the seq/par munging.
97
98 6. Clone all local Ids.
99 This means that all such Ids are unique, rather than the
100 weaker guarantee of no clashes which the simplifier provides.
101 And that is what the code generator needs.
102
103 We don't clone TyVars or CoVars. The code gen doesn't need that,
104 and doing so would be tiresome because then we'd need
105 to substitute in types and coercions.
106
107 7. Give each dynamic CCall occurrence a fresh unique; this is
108 rather like the cloning step above.
109
110 8. Inject bindings for the "implicit" Ids:
111 * Constructor wrappers
112 * Constructor workers
113 We want curried definitions for all of these in case they
114 aren't inlined by some caller.
115
116 9. Replace (lazy e) by e. See Note [lazyId magic] in MkId.hs
117
118 10. Convert (LitInteger i t) into the core representation
119 for the Integer i. Normally this uses mkInteger, but if
120 we are using the integer-gmp implementation then there is a
121 special case where we use the S# constructor for Integers that
122 are in the range of Int.
123
124 11. Uphold tick consistency while doing this: We move ticks out of
125 (non-type) applications where we can, and make sure that we
126 annotate according to scoping rules when floating.
127
128 This is all done modulo type applications and abstractions, so that
129 when type erasure is done for conversion to STG, we don't end up with
130 any trivial or useless bindings.
131
132
133 Invariants
134 ~~~~~~~~~~
135 Here is the syntax of the Core produced by CorePrep:
136
137 Trivial expressions
138 triv ::= lit | var
139 | triv ty | /\a. triv
140 | truv co | /\c. triv | triv |> co
141
142 Applications
143 app ::= lit | var | app triv | app ty | app co | app |> co
144
145 Expressions
146 body ::= app
147 | let(rec) x = rhs in body -- Boxed only
148 | case body of pat -> body
149 | /\a. body | /\c. body
150 | body |> co
151
152 Right hand sides (only place where value lambdas can occur)
153 rhs ::= /\a.rhs | \x.rhs | body
154
155 We define a synonym for each of these non-terminals. Functions
156 with the corresponding name produce a result in that syntax.
157 -}
158
159 type CpeTriv = CoreExpr -- Non-terminal 'triv'
160 type CpeApp = CoreExpr -- Non-terminal 'app'
161 type CpeBody = CoreExpr -- Non-terminal 'body'
162 type CpeRhs = CoreExpr -- Non-terminal 'rhs'
163
164 {-
165 ************************************************************************
166 * *
167 Top level stuff
168 * *
169 ************************************************************************
170 -}
171
172 corePrepPgm :: HscEnv -> ModLocation -> CoreProgram -> [TyCon] -> IO CoreProgram
173 corePrepPgm hsc_env mod_loc binds data_tycons = do
174 let dflags = hsc_dflags hsc_env
175 showPass dflags "CorePrep"
176 us <- mkSplitUniqSupply 's'
177 initialCorePrepEnv <- mkInitialCorePrepEnv dflags hsc_env
178
179 let implicit_binds = mkDataConWorkers dflags mod_loc data_tycons
180 -- NB: we must feed mkImplicitBinds through corePrep too
181 -- so that they are suitably cloned and eta-expanded
182
183 binds_out = initUs_ us $ do
184 floats1 <- corePrepTopBinds initialCorePrepEnv binds
185 floats2 <- corePrepTopBinds initialCorePrepEnv implicit_binds
186 return (deFloatTop (floats1 `appendFloats` floats2))
187
188 endPassIO hsc_env alwaysQualify CorePrep binds_out []
189 return binds_out
190
191 corePrepExpr :: DynFlags -> HscEnv -> CoreExpr -> IO CoreExpr
192 corePrepExpr dflags hsc_env expr = do
193 showPass dflags "CorePrep"
194 us <- mkSplitUniqSupply 's'
195 initialCorePrepEnv <- mkInitialCorePrepEnv dflags hsc_env
196 let new_expr = initUs_ us (cpeBodyNF initialCorePrepEnv expr)
197 dumpIfSet_dyn dflags Opt_D_dump_prep "CorePrep" (ppr new_expr)
198 return new_expr
199
200 corePrepTopBinds :: CorePrepEnv -> [CoreBind] -> UniqSM Floats
201 -- Note [Floating out of top level bindings]
202 corePrepTopBinds initialCorePrepEnv binds
203 = go initialCorePrepEnv binds
204 where
205 go _ [] = return emptyFloats
206 go env (bind : binds) = do (env', bind') <- cpeBind TopLevel env bind
207 binds' <- go env' binds
208 return (bind' `appendFloats` binds')
209
210 mkDataConWorkers :: DynFlags -> ModLocation -> [TyCon] -> [CoreBind]
211 -- See Note [Data constructor workers]
212 -- c.f. Note [Injecting implicit bindings] in TidyPgm
213 mkDataConWorkers dflags mod_loc data_tycons
214 = [ NonRec id (tick_it (getName data_con) (Var id))
215 -- The ice is thin here, but it works
216 | tycon <- data_tycons, -- CorePrep will eta-expand it
217 data_con <- tyConDataCons tycon,
218 let id = dataConWorkId data_con
219 ]
220 where
221 -- If we want to generate debug info, we put a source note on the
222 -- worker. This is useful, especially for heap profiling.
223 tick_it name
224 | debugLevel dflags == 0 = id
225 | RealSrcSpan span <- nameSrcSpan name = tick span
226 | Just file <- ml_hs_file mod_loc = tick (span1 file)
227 | otherwise = tick (span1 "???")
228 where tick span = Tick (SourceNote span $ showSDoc dflags (ppr name))
229 span1 file = realSrcLocSpan $ mkRealSrcLoc (mkFastString file) 1 1
230
231 {-
232 Note [Floating out of top level bindings]
233 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
234 NB: we do need to float out of top-level bindings
235 Consider x = length [True,False]
236 We want to get
237 s1 = False : []
238 s2 = True : s1
239 x = length s2
240
241 We return a *list* of bindings, because we may start with
242 x* = f (g y)
243 where x is demanded, in which case we want to finish with
244 a = g y
245 x* = f a
246 And then x will actually end up case-bound
247
248 Note [CafInfo and floating]
249 ~~~~~~~~~~~~~~~~~~~~~~~~~~~
250 What happens when we try to float bindings to the top level? At this
251 point all the CafInfo is supposed to be correct, and we must make certain
252 that is true of the new top-level bindings. There are two cases
253 to consider
254
255 a) The top-level binding is marked asCafRefs. In that case we are
256 basically fine. The floated bindings had better all be lazy lets,
257 so they can float to top level, but they'll all have HasCafRefs
258 (the default) which is safe.
259
260 b) The top-level binding is marked NoCafRefs. This really happens
261 Example. CoreTidy produces
262 $fApplicativeSTM [NoCafRefs] = D:Alternative retry# ...blah...
263 Now CorePrep has to eta-expand to
264 $fApplicativeSTM = let sat = \xy. retry x y
265 in D:Alternative sat ...blah...
266 So what we *want* is
267 sat [NoCafRefs] = \xy. retry x y
268 $fApplicativeSTM [NoCafRefs] = D:Alternative sat ...blah...
269
270 So, gruesomely, we must set the NoCafRefs flag on the sat bindings,
271 *and* substutite the modified 'sat' into the old RHS.
272
273 It should be the case that 'sat' is itself [NoCafRefs] (a value, no
274 cafs) else the original top-level binding would not itself have been
275 marked [NoCafRefs]. The DEBUG check in CoreToStg for
276 consistentCafInfo will find this.
277
278 This is all very gruesome and horrible. It would be better to figure
279 out CafInfo later, after CorePrep. We'll do that in due course.
280 Meanwhile this horrible hack works.
281
282
283 Note [Data constructor workers]
284 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
285 Create any necessary "implicit" bindings for data con workers. We
286 create the rather strange (non-recursive!) binding
287
288 $wC = \x y -> $wC x y
289
290 i.e. a curried constructor that allocates. This means that we can
291 treat the worker for a constructor like any other function in the rest
292 of the compiler. The point here is that CoreToStg will generate a
293 StgConApp for the RHS, rather than a call to the worker (which would
294 give a loop). As Lennart says: the ice is thin here, but it works.
295
296 Hmm. Should we create bindings for dictionary constructors? They are
297 always fully applied, and the bindings are just there to support
298 partial applications. But it's easier to let them through.
299
300
301 Note [Dead code in CorePrep]
302 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
303 Imagine that we got an input program like this (see Trac #4962):
304
305 f :: Show b => Int -> (Int, b -> Maybe Int -> Int)
306 f x = (g True (Just x) + g () (Just x), g)
307 where
308 g :: Show a => a -> Maybe Int -> Int
309 g _ Nothing = x
310 g y (Just z) = if z > 100 then g y (Just (z + length (show y))) else g y unknown
311
312 After specialisation and SpecConstr, we would get something like this:
313
314 f :: Show b => Int -> (Int, b -> Maybe Int -> Int)
315 f x = (g$Bool_True_Just x + g$Unit_Unit_Just x, g)
316 where
317 {-# RULES g $dBool = g$Bool
318 g $dUnit = g$Unit #-}
319 g = ...
320 {-# RULES forall x. g$Bool True (Just x) = g$Bool_True_Just x #-}
321 g$Bool = ...
322 {-# RULES forall x. g$Unit () (Just x) = g$Unit_Unit_Just x #-}
323 g$Unit = ...
324 g$Bool_True_Just = ...
325 g$Unit_Unit_Just = ...
326
327 Note that the g$Bool and g$Unit functions are actually dead code: they
328 are only kept alive by the occurrence analyser because they are
329 referred to by the rules of g, which is being kept alive by the fact
330 that it is used (unspecialised) in the returned pair.
331
332 However, at the CorePrep stage there is no way that the rules for g
333 will ever fire, and it really seems like a shame to produce an output
334 program that goes to the trouble of allocating a closure for the
335 unreachable g$Bool and g$Unit functions.
336
337 The way we fix this is to:
338 * In cloneBndr, drop all unfoldings/rules
339
340 * In deFloatTop, run a simple dead code analyser on each top-level
341 RHS to drop the dead local bindings. For that call to OccAnal, we
342 disable the binder swap, else the occurrence analyser sometimes
343 introduces new let bindings for cased binders, which lead to the bug
344 in #5433.
345
346 The reason we don't just OccAnal the whole output of CorePrep is that
347 the tidier ensures that all top-level binders are GlobalIds, so they
348 don't show up in the free variables any longer. So if you run the
349 occurrence analyser on the output of CoreTidy (or later) you e.g. turn
350 this program:
351
352 Rec {
353 f = ... f ...
354 }
355
356 Into this one:
357
358 f = ... f ...
359
360 (Since f is not considered to be free in its own RHS.)
361
362
363 ************************************************************************
364 * *
365 The main code
366 * *
367 ************************************************************************
368 -}
369
370 cpeBind :: TopLevelFlag -> CorePrepEnv -> CoreBind
371 -> UniqSM (CorePrepEnv, Floats)
372 cpeBind top_lvl env (NonRec bndr rhs)
373 = do { (_, bndr1) <- cpCloneBndr env bndr
374 ; let dmd = idDemandInfo bndr
375 is_unlifted = isUnLiftedType (idType bndr)
376 ; (floats, bndr2, rhs2) <- cpePair top_lvl NonRecursive
377 dmd
378 is_unlifted
379 env bndr1 rhs
380 ; let new_float = mkFloat dmd is_unlifted bndr2 rhs2
381
382 -- We want bndr'' in the envt, because it records
383 -- the evaluated-ness of the binder
384 ; return (extendCorePrepEnv env bndr bndr2,
385 addFloat floats new_float) }
386
387 cpeBind top_lvl env (Rec pairs)
388 = do { let (bndrs,rhss) = unzip pairs
389 ; (env', bndrs1) <- cpCloneBndrs env (map fst pairs)
390 ; stuff <- zipWithM (cpePair top_lvl Recursive topDmd False env') bndrs1 rhss
391
392 ; let (floats_s, bndrs2, rhss2) = unzip3 stuff
393 all_pairs = foldrOL add_float (bndrs2 `zip` rhss2)
394 (concatFloats floats_s)
395 ; return (extendCorePrepEnvList env (bndrs `zip` bndrs2),
396 unitFloat (FloatLet (Rec all_pairs))) }
397 where
398 -- Flatten all the floats, and the currrent
399 -- group into a single giant Rec
400 add_float (FloatLet (NonRec b r)) prs2 = (b,r) : prs2
401 add_float (FloatLet (Rec prs1)) prs2 = prs1 ++ prs2
402 add_float b _ = pprPanic "cpeBind" (ppr b)
403
404 ---------------
405 cpePair :: TopLevelFlag -> RecFlag -> Demand -> Bool
406 -> CorePrepEnv -> Id -> CoreExpr
407 -> UniqSM (Floats, Id, CpeRhs)
408 -- Used for all bindings
409 cpePair top_lvl is_rec dmd is_unlifted env bndr rhs
410 = do { (floats1, rhs1) <- cpeRhsE env rhs
411
412 -- See if we are allowed to float this stuff out of the RHS
413 ; (floats2, rhs2) <- float_from_rhs floats1 rhs1
414
415 -- Make the arity match up
416 ; (floats3, rhs3)
417 <- if manifestArity rhs1 <= arity
418 then return (floats2, cpeEtaExpand arity rhs2)
419 else WARN(True, text "CorePrep: silly extra arguments:" <+> ppr bndr)
420 -- Note [Silly extra arguments]
421 (do { v <- newVar (idType bndr)
422 ; let float = mkFloat topDmd False v rhs2
423 ; return ( addFloat floats2 float
424 , cpeEtaExpand arity (Var v)) })
425
426 -- Wrap floating ticks
427 ; let (floats4, rhs4) = wrapTicks floats3 rhs3
428
429 -- Record if the binder is evaluated
430 -- and otherwise trim off the unfolding altogether
431 -- It's not used by the code generator; getting rid of it reduces
432 -- heap usage and, since we may be changing uniques, we'd have
433 -- to substitute to keep it right
434 ; let bndr' | exprIsHNF rhs3 = bndr `setIdUnfolding` evaldUnfolding
435 | otherwise = bndr `setIdUnfolding` noUnfolding
436
437 ; return (floats4, bndr', rhs4) }
438 where
439 is_strict_or_unlifted = (isStrictDmd dmd) || is_unlifted
440
441 platform = targetPlatform (cpe_dynFlags env)
442
443 arity = idArity bndr -- We must match this arity
444
445 ---------------------
446 float_from_rhs floats rhs
447 | isEmptyFloats floats = return (emptyFloats, rhs)
448 | isTopLevel top_lvl = float_top floats rhs
449 | otherwise = float_nested floats rhs
450
451 ---------------------
452 float_nested floats rhs
453 | wantFloatNested is_rec is_strict_or_unlifted floats rhs
454 = return (floats, rhs)
455 | otherwise = dont_float floats rhs
456
457 ---------------------
458 float_top floats rhs -- Urhgh! See Note [CafInfo and floating]
459 | mayHaveCafRefs (idCafInfo bndr)
460 , allLazyTop floats
461 = return (floats, rhs)
462
463 -- So the top-level binding is marked NoCafRefs
464 | Just (floats', rhs') <- canFloatFromNoCaf platform floats rhs
465 = return (floats', rhs')
466
467 | otherwise
468 = dont_float floats rhs
469
470 ---------------------
471 dont_float floats rhs
472 -- Non-empty floats, but do not want to float from rhs
473 -- So wrap the rhs in the floats
474 -- But: rhs1 might have lambdas, and we can't
475 -- put them inside a wrapBinds
476 = do { body <- rhsToBodyNF rhs
477 ; return (emptyFloats, wrapBinds floats body) }
478
479 {- Note [Silly extra arguments]
480 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
481 Suppose we had this
482 f{arity=1} = \x\y. e
483 We *must* match the arity on the Id, so we have to generate
484 f' = \x\y. e
485 f = \x. f' x
486
487 It's a bizarre case: why is the arity on the Id wrong? Reason
488 (in the days of __inline_me__):
489 f{arity=0} = __inline_me__ (let v = expensive in \xy. e)
490 When InlineMe notes go away this won't happen any more. But
491 it seems good for CorePrep to be robust.
492 -}
493
494 -- ---------------------------------------------------------------------------
495 -- CpeRhs: produces a result satisfying CpeRhs
496 -- ---------------------------------------------------------------------------
497
498 cpeRhsE :: CorePrepEnv -> CoreExpr -> UniqSM (Floats, CpeRhs)
499 -- If
500 -- e ===> (bs, e')
501 -- then
502 -- e = let bs in e' (semantically, that is!)
503 --
504 -- For example
505 -- f (g x) ===> ([v = g x], f v)
506
507 cpeRhsE _env expr@(Type {}) = return (emptyFloats, expr)
508 cpeRhsE _env expr@(Coercion {}) = return (emptyFloats, expr)
509 cpeRhsE env (Lit (LitInteger i _))
510 = cpeRhsE env (cvtLitInteger (cpe_dynFlags env) (getMkIntegerId env)
511 (cpe_integerSDataCon env) i)
512 cpeRhsE _env expr@(Lit {}) = return (emptyFloats, expr)
513 cpeRhsE env expr@(Var {}) = cpeApp env expr
514
515 cpeRhsE env (Var f `App` _{-type-} `App` arg)
516 | f `hasKey` lazyIdKey -- Replace (lazy a) by a
517 = cpeRhsE env arg -- See Note [lazyId magic] in MkId
518
519 cpeRhsE env (Var f `App` _levity `App` _type `App` arg)
520 -- See Note [runRW magic] in MkId
521 | f `hasKey` runRWKey -- Replace (runRW# f) by (f realWorld#),
522 = case arg of -- beta reducing if possible
523 Lam s body -> cpeRhsE (extendCorePrepEnv env s realWorldPrimId) body
524 _ -> cpeRhsE env (arg `App` Var realWorldPrimId)
525
526 cpeRhsE env expr@(App {}) = cpeApp env expr
527
528 cpeRhsE env (Let bind expr)
529 = do { (env', new_binds) <- cpeBind NotTopLevel env bind
530 ; (floats, body) <- cpeRhsE env' expr
531 ; return (new_binds `appendFloats` floats, body) }
532
533 cpeRhsE env (Tick tickish expr)
534 | tickishPlace tickish == PlaceNonLam && tickish `tickishScopesLike` SoftScope
535 = do { (floats, body) <- cpeRhsE env expr
536 -- See [Floating Ticks in CorePrep]
537 ; return (unitFloat (FloatTick tickish) `appendFloats` floats, body) }
538 | otherwise
539 = do { body <- cpeBodyNF env expr
540 ; return (emptyFloats, mkTick tickish' body) }
541 where
542 tickish' | Breakpoint n fvs <- tickish
543 = Breakpoint n (map (lookupCorePrepEnv env) fvs)
544 | otherwise
545 = tickish
546
547 cpeRhsE env (Cast expr co)
548 = do { (floats, expr') <- cpeRhsE env expr
549 ; return (floats, Cast expr' co) }
550
551 cpeRhsE env expr@(Lam {})
552 = do { let (bndrs,body) = collectBinders expr
553 ; (env', bndrs') <- cpCloneBndrs env bndrs
554 ; body' <- cpeBodyNF env' body
555 ; return (emptyFloats, mkLams bndrs' body') }
556
557 cpeRhsE env (Case scrut bndr ty alts)
558 = do { (floats, scrut') <- cpeBody env scrut
559 ; let bndr1 = bndr `setIdUnfolding` evaldUnfolding
560 -- Record that the case binder is evaluated in the alternatives
561 ; (env', bndr2) <- cpCloneBndr env bndr1
562 ; alts' <- mapM (sat_alt env') alts
563 ; return (floats, Case scrut' bndr2 ty alts') }
564 where
565 sat_alt env (con, bs, rhs)
566 = do { (env2, bs') <- cpCloneBndrs env bs
567 ; rhs' <- cpeBodyNF env2 rhs
568 ; return (con, bs', rhs') }
569
570 cvtLitInteger :: DynFlags -> Id -> Maybe DataCon -> Integer -> CoreExpr
571 -- Here we convert a literal Integer to the low-level
572 -- represenation. Exactly how we do this depends on the
573 -- library that implements Integer. If it's GMP we
574 -- use the S# data constructor for small literals.
575 -- See Note [Integer literals] in Literal
576 cvtLitInteger dflags _ (Just sdatacon) i
577 | inIntRange dflags i -- Special case for small integers
578 = mkConApp sdatacon [Lit (mkMachInt dflags i)]
579
580 cvtLitInteger dflags mk_integer _ i
581 = mkApps (Var mk_integer) [isNonNegative, ints]
582 where isNonNegative = if i < 0 then mkConApp falseDataCon []
583 else mkConApp trueDataCon []
584 ints = mkListExpr intTy (f (abs i))
585 f 0 = []
586 f x = let low = x .&. mask
587 high = x `shiftR` bits
588 in mkConApp intDataCon [Lit (mkMachInt dflags low)] : f high
589 bits = 31
590 mask = 2 ^ bits - 1
591
592 -- ---------------------------------------------------------------------------
593 -- CpeBody: produces a result satisfying CpeBody
594 -- ---------------------------------------------------------------------------
595
596 cpeBodyNF :: CorePrepEnv -> CoreExpr -> UniqSM CpeBody
597 cpeBodyNF env expr
598 = do { (floats, body) <- cpeBody env expr
599 ; return (wrapBinds floats body) }
600
601 --------
602 cpeBody :: CorePrepEnv -> CoreExpr -> UniqSM (Floats, CpeBody)
603 cpeBody env expr
604 = do { (floats1, rhs) <- cpeRhsE env expr
605 ; (floats2, body) <- rhsToBody rhs
606 ; return (floats1 `appendFloats` floats2, body) }
607
608 --------
609 rhsToBodyNF :: CpeRhs -> UniqSM CpeBody
610 rhsToBodyNF rhs = do { (floats,body) <- rhsToBody rhs
611 ; return (wrapBinds floats body) }
612
613 --------
614 rhsToBody :: CpeRhs -> UniqSM (Floats, CpeBody)
615 -- Remove top level lambdas by let-binding
616
617 rhsToBody (Tick t expr)
618 | tickishScoped t == NoScope -- only float out of non-scoped annotations
619 = do { (floats, expr') <- rhsToBody expr
620 ; return (floats, mkTick t expr') }
621
622 rhsToBody (Cast e co)
623 -- You can get things like
624 -- case e of { p -> coerce t (\s -> ...) }
625 = do { (floats, e') <- rhsToBody e
626 ; return (floats, Cast e' co) }
627
628 rhsToBody expr@(Lam {})
629 | Just no_lam_result <- tryEtaReducePrep bndrs body
630 = return (emptyFloats, no_lam_result)
631 | all isTyVar bndrs -- Type lambdas are ok
632 = return (emptyFloats, expr)
633 | otherwise -- Some value lambdas
634 = do { fn <- newVar (exprType expr)
635 ; let rhs = cpeEtaExpand (exprArity expr) expr
636 float = FloatLet (NonRec fn rhs)
637 ; return (unitFloat float, Var fn) }
638 where
639 (bndrs,body) = collectBinders expr
640
641 rhsToBody expr = return (emptyFloats, expr)
642
643
644
645 -- ---------------------------------------------------------------------------
646 -- CpeApp: produces a result satisfying CpeApp
647 -- ---------------------------------------------------------------------------
648
649 cpeApp :: CorePrepEnv -> CoreExpr -> UniqSM (Floats, CpeRhs)
650 -- May return a CpeRhs because of saturating primops
651 cpeApp env expr
652 = do { (app, (head,depth), _, floats, ss) <- collect_args expr 0
653 ; MASSERT(null ss) -- make sure we used all the strictness info
654
655 -- Now deal with the function
656 ; case head of
657 Var fn_id -> do { sat_app <- maybeSaturate fn_id app depth
658 ; return (floats, sat_app) }
659 _other -> return (floats, app) }
660
661 where
662 -- Deconstruct and rebuild the application, floating any non-atomic
663 -- arguments to the outside. We collect the type of the expression,
664 -- the head of the application, and the number of actual value arguments,
665 -- all of which are used to possibly saturate this application if it
666 -- has a constructor or primop at the head.
667
668 collect_args
669 :: CoreExpr
670 -> Int -- Current app depth
671 -> UniqSM (CpeApp, -- The rebuilt expression
672 (CoreExpr,Int), -- The head of the application,
673 -- and no. of args it was applied to
674 Type, -- Type of the whole expr
675 Floats, -- Any floats we pulled out
676 [Demand]) -- Remaining argument demands
677
678 collect_args (App fun arg@(Type arg_ty)) depth
679 = do { (fun',hd,fun_ty,floats,ss) <- collect_args fun depth
680 ; return (App fun' arg, hd, piResultTy fun_ty arg_ty, floats, ss) }
681
682 collect_args (App fun arg@(Coercion {})) depth
683 = do { (fun',hd,fun_ty,floats,ss) <- collect_args fun depth
684 ; return (App fun' arg, hd, funResultTy fun_ty, floats, ss) }
685
686 collect_args (App fun arg) depth
687 = do { (fun',hd,fun_ty,floats,ss) <- collect_args fun (depth+1)
688 ; let
689 (ss1, ss_rest) = case ss of
690 (ss1:ss_rest) -> (ss1, ss_rest)
691 [] -> (topDmd, [])
692 (arg_ty, res_ty) = expectJust "cpeBody:collect_args" $
693 splitFunTy_maybe fun_ty
694
695 ; (fs, arg') <- cpeArg env ss1 arg arg_ty
696 ; return (App fun' arg', hd, res_ty, fs `appendFloats` floats, ss_rest) }
697
698 collect_args (Var v) depth
699 = do { v1 <- fiddleCCall v
700 ; let v2 = lookupCorePrepEnv env v1
701 ; return (Var v2, (Var v2, depth), idType v2, emptyFloats, stricts) }
702 where
703 stricts = case idStrictness v of
704 StrictSig (DmdType _ demands _)
705 | listLengthCmp demands depth /= GT -> demands
706 -- length demands <= depth
707 | otherwise -> []
708 -- If depth < length demands, then we have too few args to
709 -- satisfy strictness info so we have to ignore all the
710 -- strictness info, e.g. + (error "urk")
711 -- Here, we can't evaluate the arg strictly, because this
712 -- partial application might be seq'd
713
714 collect_args (Cast fun co) depth
715 = do { let Pair _ty1 ty2 = coercionKind co
716 ; (fun', hd, _, floats, ss) <- collect_args fun depth
717 ; return (Cast fun' co, hd, ty2, floats, ss) }
718
719 collect_args (Tick tickish fun) depth
720 | tickishPlace tickish == PlaceNonLam
721 && tickish `tickishScopesLike` SoftScope
722 = do { (fun',hd,fun_ty,floats,ss) <- collect_args fun depth
723 -- See [Floating Ticks in CorePrep]
724 ; return (fun',hd,fun_ty,addFloat floats (FloatTick tickish),ss) }
725
726 -- N-variable fun, better let-bind it
727 collect_args fun depth
728 = do { (fun_floats, fun') <- cpeArg env evalDmd fun ty
729 -- The evalDmd says that it's sure to be evaluated,
730 -- so we'll end up case-binding it
731 ; return (fun', (fun', depth), ty, fun_floats, []) }
732 where
733 ty = exprType fun
734
735 -- ---------------------------------------------------------------------------
736 -- CpeArg: produces a result satisfying CpeArg
737 -- ---------------------------------------------------------------------------
738
739 -- This is where we arrange that a non-trivial argument is let-bound
740 cpeArg :: CorePrepEnv -> Demand
741 -> CoreArg -> Type -> UniqSM (Floats, CpeTriv)
742 cpeArg env dmd arg arg_ty
743 = do { (floats1, arg1) <- cpeRhsE env arg -- arg1 can be a lambda
744 ; (floats2, arg2) <- if want_float floats1 arg1
745 then return (floats1, arg1)
746 else do { body1 <- rhsToBodyNF arg1
747 ; return (emptyFloats, wrapBinds floats1 body1) }
748 -- Else case: arg1 might have lambdas, and we can't
749 -- put them inside a wrapBinds
750
751 ; if cpe_ExprIsTrivial arg2 -- Do not eta expand a trivial argument
752 then return (floats2, arg2)
753 else do
754 { v <- newVar arg_ty
755 ; let arg3 = cpeEtaExpand (exprArity arg2) arg2
756 arg_float = mkFloat dmd is_unlifted v arg3
757 ; return (addFloat floats2 arg_float, varToCoreExpr v) } }
758 where
759 is_unlifted = isUnLiftedType arg_ty
760 is_strict = isStrictDmd dmd
761 want_float = wantFloatNested NonRecursive (is_strict || is_unlifted)
762
763 {-
764 Note [Floating unlifted arguments]
765 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
766 Consider C (let v* = expensive in v)
767
768 where the "*" indicates "will be demanded". Usually v will have been
769 inlined by now, but let's suppose it hasn't (see Trac #2756). Then we
770 do *not* want to get
771
772 let v* = expensive in C v
773
774 because that has different strictness. Hence the use of 'allLazy'.
775 (NB: the let v* turns into a FloatCase, in mkLocalNonRec.)
776
777
778 ------------------------------------------------------------------------------
779 -- Building the saturated syntax
780 -- ---------------------------------------------------------------------------
781
782 maybeSaturate deals with saturating primops and constructors
783 The type is the type of the entire application
784 -}
785
786 maybeSaturate :: Id -> CpeApp -> Int -> UniqSM CpeRhs
787 maybeSaturate fn expr n_args
788 | Just DataToTagOp <- isPrimOpId_maybe fn -- DataToTag must have an evaluated arg
789 -- A gruesome special case
790 = saturateDataToTag sat_expr
791
792 | hasNoBinding fn -- There's no binding
793 = return sat_expr
794
795 | otherwise
796 = return expr
797 where
798 fn_arity = idArity fn
799 excess_arity = fn_arity - n_args
800 sat_expr = cpeEtaExpand excess_arity expr
801
802 -------------
803 saturateDataToTag :: CpeApp -> UniqSM CpeApp
804 -- See Note [dataToTag magic]
805 saturateDataToTag sat_expr
806 = do { let (eta_bndrs, eta_body) = collectBinders sat_expr
807 ; eta_body' <- eval_data2tag_arg eta_body
808 ; return (mkLams eta_bndrs eta_body') }
809 where
810 eval_data2tag_arg :: CpeApp -> UniqSM CpeBody
811 eval_data2tag_arg app@(fun `App` arg)
812 | exprIsHNF arg -- Includes nullary constructors
813 = return app -- The arg is evaluated
814 | otherwise -- Arg not evaluated, so evaluate it
815 = do { arg_id <- newVar (exprType arg)
816 ; let arg_id1 = setIdUnfolding arg_id evaldUnfolding
817 ; return (Case arg arg_id1 (exprType app)
818 [(DEFAULT, [], fun `App` Var arg_id1)]) }
819
820 eval_data2tag_arg (Tick t app) -- Scc notes can appear
821 = do { app' <- eval_data2tag_arg app
822 ; return (Tick t app') }
823
824 eval_data2tag_arg other -- Should not happen
825 = pprPanic "eval_data2tag" (ppr other)
826
827 {-
828 Note [dataToTag magic]
829 ~~~~~~~~~~~~~~~~~~~~~~
830 Horrid: we must ensure that the arg of data2TagOp is evaluated
831 (data2tag x) --> (case x of y -> data2tag y)
832 (yuk yuk) take into account the lambdas we've now introduced
833
834 How might it not be evaluated? Well, we might have floated it out
835 of the scope of a `seq`, or dropped the `seq` altogether.
836
837
838 ************************************************************************
839 * *
840 Simple CoreSyn operations
841 * *
842 ************************************************************************
843 -}
844
845 cpe_ExprIsTrivial :: CoreExpr -> Bool
846 -- Version that doesn't consider an scc annotation to be trivial.
847 cpe_ExprIsTrivial (Var _) = True
848 cpe_ExprIsTrivial (Type _) = True
849 cpe_ExprIsTrivial (Coercion _) = True
850 cpe_ExprIsTrivial (Lit _) = True
851 cpe_ExprIsTrivial (App e arg) = not (isRuntimeArg arg) && cpe_ExprIsTrivial e
852 cpe_ExprIsTrivial (Lam b e) = not (isRuntimeVar b) && cpe_ExprIsTrivial e
853 cpe_ExprIsTrivial (Tick t e) = not (tickishIsCode t) && cpe_ExprIsTrivial e
854 cpe_ExprIsTrivial (Cast e _) = cpe_ExprIsTrivial e
855 cpe_ExprIsTrivial (Case e _ _ []) = cpe_ExprIsTrivial e
856 -- See Note [Empty case is trivial] in CoreUtils
857 cpe_ExprIsTrivial _ = False
858
859 {-
860 -- -----------------------------------------------------------------------------
861 -- Eta reduction
862 -- -----------------------------------------------------------------------------
863
864 Note [Eta expansion]
865 ~~~~~~~~~~~~~~~~~~~~~
866 Eta expand to match the arity claimed by the binder Remember,
867 CorePrep must not change arity
868
869 Eta expansion might not have happened already, because it is done by
870 the simplifier only when there at least one lambda already.
871
872 NB1:we could refrain when the RHS is trivial (which can happen
873 for exported things). This would reduce the amount of code
874 generated (a little) and make things a little words for
875 code compiled without -O. The case in point is data constructor
876 wrappers.
877
878 NB2: we have to be careful that the result of etaExpand doesn't
879 invalidate any of the assumptions that CorePrep is attempting
880 to establish. One possible cause is eta expanding inside of
881 an SCC note - we're now careful in etaExpand to make sure the
882 SCC is pushed inside any new lambdas that are generated.
883
884 Note [Eta expansion and the CorePrep invariants]
885 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
886 It turns out to be much much easier to do eta expansion
887 *after* the main CorePrep stuff. But that places constraints
888 on the eta expander: given a CpeRhs, it must return a CpeRhs.
889
890 For example here is what we do not want:
891 f = /\a -> g (h 3) -- h has arity 2
892 After ANFing we get
893 f = /\a -> let s = h 3 in g s
894 and now we do NOT want eta expansion to give
895 f = /\a -> \ y -> (let s = h 3 in g s) y
896
897 Instead CoreArity.etaExpand gives
898 f = /\a -> \y -> let s = h 3 in g s y
899 -}
900
901 cpeEtaExpand :: Arity -> CpeRhs -> CpeRhs
902 cpeEtaExpand arity expr
903 | arity == 0 = expr
904 | otherwise = etaExpand arity expr
905
906 {-
907 -- -----------------------------------------------------------------------------
908 -- Eta reduction
909 -- -----------------------------------------------------------------------------
910
911 Why try eta reduction? Hasn't the simplifier already done eta?
912 But the simplifier only eta reduces if that leaves something
913 trivial (like f, or f Int). But for deLam it would be enough to
914 get to a partial application:
915 case x of { p -> \xs. map f xs }
916 ==> case x of { p -> map f }
917 -}
918
919 tryEtaReducePrep :: [CoreBndr] -> CoreExpr -> Maybe CoreExpr
920 tryEtaReducePrep bndrs expr@(App _ _)
921 | ok_to_eta_reduce f
922 , n_remaining >= 0
923 , and (zipWith ok bndrs last_args)
924 , not (any (`elemVarSet` fvs_remaining) bndrs)
925 , exprIsHNF remaining_expr -- Don't turn value into a non-value
926 -- else the behaviour with 'seq' changes
927 = Just remaining_expr
928 where
929 (f, args) = collectArgs expr
930 remaining_expr = mkApps f remaining_args
931 fvs_remaining = exprFreeVars remaining_expr
932 (remaining_args, last_args) = splitAt n_remaining args
933 n_remaining = length args - length bndrs
934
935 ok bndr (Var arg) = bndr == arg
936 ok _ _ = False
937
938 -- We can't eta reduce something which must be saturated.
939 ok_to_eta_reduce (Var f) = not (hasNoBinding f)
940 ok_to_eta_reduce _ = False -- Safe. ToDo: generalise
941
942 tryEtaReducePrep bndrs (Let bind@(NonRec _ r) body)
943 | not (any (`elemVarSet` fvs) bndrs)
944 = case tryEtaReducePrep bndrs body of
945 Just e -> Just (Let bind e)
946 Nothing -> Nothing
947 where
948 fvs = exprFreeVars r
949
950 tryEtaReducePrep bndrs (Tick tickish e)
951 = fmap (mkTick tickish) $ tryEtaReducePrep bndrs e
952
953 tryEtaReducePrep _ _ = Nothing
954
955 {-
956 ************************************************************************
957 * *
958 Floats
959 * *
960 ************************************************************************
961
962 Note [Pin demand info on floats]
963 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
964 We pin demand info on floated lets so that we can see the one-shot thunks.
965 -}
966
967 data FloatingBind
968 = FloatLet CoreBind -- Rhs of bindings are CpeRhss
969 -- They are always of lifted type;
970 -- unlifted ones are done with FloatCase
971
972 | FloatCase
973 Id CpeBody
974 Bool -- The bool indicates "ok-for-speculation"
975
976 -- | See Note [Floating Ticks in CorePrep]
977 | FloatTick (Tickish Id)
978
979 data Floats = Floats OkToSpec (OrdList FloatingBind)
980
981 instance Outputable FloatingBind where
982 ppr (FloatLet b) = ppr b
983 ppr (FloatCase b r ok) = brackets (ppr ok) <+> ppr b <+> equals <+> ppr r
984 ppr (FloatTick t) = ppr t
985
986 instance Outputable Floats where
987 ppr (Floats flag fs) = ptext (sLit "Floats") <> brackets (ppr flag) <+>
988 braces (vcat (map ppr (fromOL fs)))
989
990 instance Outputable OkToSpec where
991 ppr OkToSpec = ptext (sLit "OkToSpec")
992 ppr IfUnboxedOk = ptext (sLit "IfUnboxedOk")
993 ppr NotOkToSpec = ptext (sLit "NotOkToSpec")
994
995 -- Can we float these binds out of the rhs of a let? We cache this decision
996 -- to avoid having to recompute it in a non-linear way when there are
997 -- deeply nested lets.
998 data OkToSpec
999 = OkToSpec -- Lazy bindings of lifted type
1000 | IfUnboxedOk -- A mixture of lazy lifted bindings and n
1001 -- ok-to-speculate unlifted bindings
1002 | NotOkToSpec -- Some not-ok-to-speculate unlifted bindings
1003
1004 mkFloat :: Demand -> Bool -> Id -> CpeRhs -> FloatingBind
1005 mkFloat dmd is_unlifted bndr rhs
1006 | use_case = FloatCase bndr rhs (exprOkForSpeculation rhs)
1007 | is_hnf = FloatLet (NonRec bndr rhs)
1008 | otherwise = FloatLet (NonRec (setIdDemandInfo bndr dmd) rhs)
1009 -- See Note [Pin demand info on floats]
1010 where
1011 is_hnf = exprIsHNF rhs
1012 is_strict = isStrictDmd dmd
1013 use_case = is_unlifted || is_strict && not is_hnf
1014 -- Don't make a case for a value binding,
1015 -- even if it's strict. Otherwise we get
1016 -- case (\x -> e) of ...!
1017
1018 emptyFloats :: Floats
1019 emptyFloats = Floats OkToSpec nilOL
1020
1021 isEmptyFloats :: Floats -> Bool
1022 isEmptyFloats (Floats _ bs) = isNilOL bs
1023
1024 wrapBinds :: Floats -> CpeBody -> CpeBody
1025 wrapBinds (Floats _ binds) body
1026 = foldrOL mk_bind body binds
1027 where
1028 mk_bind (FloatCase bndr rhs _) body = Case rhs bndr (exprType body) [(DEFAULT, [], body)]
1029 mk_bind (FloatLet bind) body = Let bind body
1030 mk_bind (FloatTick tickish) body = mkTick tickish body
1031
1032 addFloat :: Floats -> FloatingBind -> Floats
1033 addFloat (Floats ok_to_spec floats) new_float
1034 = Floats (combine ok_to_spec (check new_float)) (floats `snocOL` new_float)
1035 where
1036 check (FloatLet _) = OkToSpec
1037 check (FloatCase _ _ ok_for_spec)
1038 | ok_for_spec = IfUnboxedOk
1039 | otherwise = NotOkToSpec
1040 check FloatTick{} = OkToSpec
1041 -- The ok-for-speculation flag says that it's safe to
1042 -- float this Case out of a let, and thereby do it more eagerly
1043 -- We need the top-level flag because it's never ok to float
1044 -- an unboxed binding to the top level
1045
1046 unitFloat :: FloatingBind -> Floats
1047 unitFloat = addFloat emptyFloats
1048
1049 appendFloats :: Floats -> Floats -> Floats
1050 appendFloats (Floats spec1 floats1) (Floats spec2 floats2)
1051 = Floats (combine spec1 spec2) (floats1 `appOL` floats2)
1052
1053 concatFloats :: [Floats] -> OrdList FloatingBind
1054 concatFloats = foldr (\ (Floats _ bs1) bs2 -> appOL bs1 bs2) nilOL
1055
1056 combine :: OkToSpec -> OkToSpec -> OkToSpec
1057 combine NotOkToSpec _ = NotOkToSpec
1058 combine _ NotOkToSpec = NotOkToSpec
1059 combine IfUnboxedOk _ = IfUnboxedOk
1060 combine _ IfUnboxedOk = IfUnboxedOk
1061 combine _ _ = OkToSpec
1062
1063 deFloatTop :: Floats -> [CoreBind]
1064 -- For top level only; we don't expect any FloatCases
1065 deFloatTop (Floats _ floats)
1066 = foldrOL get [] floats
1067 where
1068 get (FloatLet b) bs = occurAnalyseRHSs b : bs
1069 get b _ = pprPanic "corePrepPgm" (ppr b)
1070
1071 -- See Note [Dead code in CorePrep]
1072 occurAnalyseRHSs (NonRec x e) = NonRec x (occurAnalyseExpr_NoBinderSwap e)
1073 occurAnalyseRHSs (Rec xes) = Rec [(x, occurAnalyseExpr_NoBinderSwap e) | (x, e) <- xes]
1074
1075 ---------------------------------------------------------------------------
1076
1077 canFloatFromNoCaf :: Platform -> Floats -> CpeRhs -> Maybe (Floats, CpeRhs)
1078 -- Note [CafInfo and floating]
1079 canFloatFromNoCaf platform (Floats ok_to_spec fs) rhs
1080 | OkToSpec <- ok_to_spec -- Worth trying
1081 , Just (subst, fs') <- go (emptySubst, nilOL) (fromOL fs)
1082 = Just (Floats OkToSpec fs', subst_expr subst rhs)
1083 | otherwise
1084 = Nothing
1085 where
1086 subst_expr = substExpr (text "CorePrep")
1087
1088 go :: (Subst, OrdList FloatingBind) -> [FloatingBind]
1089 -> Maybe (Subst, OrdList FloatingBind)
1090
1091 go (subst, fbs_out) [] = Just (subst, fbs_out)
1092
1093 go (subst, fbs_out) (FloatLet (NonRec b r) : fbs_in)
1094 | rhs_ok r
1095 = go (subst', fbs_out `snocOL` new_fb) fbs_in
1096 where
1097 (subst', b') = set_nocaf_bndr subst b
1098 new_fb = FloatLet (NonRec b' (subst_expr subst r))
1099
1100 go (subst, fbs_out) (FloatLet (Rec prs) : fbs_in)
1101 | all rhs_ok rs
1102 = go (subst', fbs_out `snocOL` new_fb) fbs_in
1103 where
1104 (bs,rs) = unzip prs
1105 (subst', bs') = mapAccumL set_nocaf_bndr subst bs
1106 rs' = map (subst_expr subst') rs
1107 new_fb = FloatLet (Rec (bs' `zip` rs'))
1108
1109 go (subst, fbs_out) (ft@FloatTick{} : fbs_in)
1110 = go (subst, fbs_out `snocOL` ft) fbs_in
1111
1112 go _ _ = Nothing -- Encountered a caffy binding
1113
1114 ------------
1115 set_nocaf_bndr subst bndr
1116 = (extendIdSubst subst bndr (Var bndr'), bndr')
1117 where
1118 bndr' = bndr `setIdCafInfo` NoCafRefs
1119
1120 ------------
1121 rhs_ok :: CoreExpr -> Bool
1122 -- We can only float to top level from a NoCaf thing if
1123 -- the new binding is static. However it can't mention
1124 -- any non-static things or it would *already* be Caffy
1125 rhs_ok = rhsIsStatic platform (\_ -> False)
1126 (\i -> pprPanic "rhsIsStatic" (integer i))
1127 -- Integer literals should not show up
1128
1129 wantFloatNested :: RecFlag -> Bool -> Floats -> CpeRhs -> Bool
1130 wantFloatNested is_rec strict_or_unlifted floats rhs
1131 = isEmptyFloats floats
1132 || strict_or_unlifted
1133 || (allLazyNested is_rec floats && exprIsHNF rhs)
1134 -- Why the test for allLazyNested?
1135 -- v = f (x `divInt#` y)
1136 -- we don't want to float the case, even if f has arity 2,
1137 -- because floating the case would make it evaluated too early
1138
1139 allLazyTop :: Floats -> Bool
1140 allLazyTop (Floats OkToSpec _) = True
1141 allLazyTop _ = False
1142
1143 allLazyNested :: RecFlag -> Floats -> Bool
1144 allLazyNested _ (Floats OkToSpec _) = True
1145 allLazyNested _ (Floats NotOkToSpec _) = False
1146 allLazyNested is_rec (Floats IfUnboxedOk _) = isNonRec is_rec
1147
1148 {-
1149 ************************************************************************
1150 * *
1151 Cloning
1152 * *
1153 ************************************************************************
1154 -}
1155
1156 -- ---------------------------------------------------------------------------
1157 -- The environment
1158 -- ---------------------------------------------------------------------------
1159
1160 data CorePrepEnv
1161 = CPE { cpe_dynFlags :: DynFlags
1162 , cpe_env :: IdEnv Id -- Clone local Ids
1163 , cpe_mkIntegerId :: Id
1164 , cpe_integerSDataCon :: Maybe DataCon
1165 }
1166
1167 lookupMkIntegerName :: DynFlags -> HscEnv -> IO Id
1168 lookupMkIntegerName dflags hsc_env
1169 = guardIntegerUse dflags $ liftM tyThingId $
1170 lookupGlobal hsc_env mkIntegerName
1171
1172 lookupIntegerSDataConName :: DynFlags -> HscEnv -> IO (Maybe DataCon)
1173 lookupIntegerSDataConName dflags hsc_env = case cIntegerLibraryType of
1174 IntegerGMP -> guardIntegerUse dflags $ liftM (Just . tyThingDataCon) $
1175 lookupGlobal hsc_env integerSDataConName
1176 IntegerSimple -> return Nothing
1177
1178 -- | Helper for 'lookupMkIntegerName' and 'lookupIntegerSDataConName'
1179 guardIntegerUse :: DynFlags -> IO a -> IO a
1180 guardIntegerUse dflags act
1181 | thisPackage dflags == primUnitId
1182 = return $ panic "Can't use Integer in ghc-prim"
1183 | thisPackage dflags == integerUnitId
1184 = return $ panic "Can't use Integer in integer-*"
1185 | otherwise = act
1186
1187 mkInitialCorePrepEnv :: DynFlags -> HscEnv -> IO CorePrepEnv
1188 mkInitialCorePrepEnv dflags hsc_env
1189 = do mkIntegerId <- lookupMkIntegerName dflags hsc_env
1190 integerSDataCon <- lookupIntegerSDataConName dflags hsc_env
1191 return $ CPE {
1192 cpe_dynFlags = dflags,
1193 cpe_env = emptyVarEnv,
1194 cpe_mkIntegerId = mkIntegerId,
1195 cpe_integerSDataCon = integerSDataCon
1196 }
1197
1198 extendCorePrepEnv :: CorePrepEnv -> Id -> Id -> CorePrepEnv
1199 extendCorePrepEnv cpe id id'
1200 = cpe { cpe_env = extendVarEnv (cpe_env cpe) id id' }
1201
1202 extendCorePrepEnvList :: CorePrepEnv -> [(Id,Id)] -> CorePrepEnv
1203 extendCorePrepEnvList cpe prs
1204 = cpe { cpe_env = extendVarEnvList (cpe_env cpe) prs }
1205
1206 lookupCorePrepEnv :: CorePrepEnv -> Id -> Id
1207 lookupCorePrepEnv cpe id
1208 = case lookupVarEnv (cpe_env cpe) id of
1209 Nothing -> id
1210 Just id' -> id'
1211
1212 getMkIntegerId :: CorePrepEnv -> Id
1213 getMkIntegerId = cpe_mkIntegerId
1214
1215 ------------------------------------------------------------------------------
1216 -- Cloning binders
1217 -- ---------------------------------------------------------------------------
1218
1219 cpCloneBndrs :: CorePrepEnv -> [Var] -> UniqSM (CorePrepEnv, [Var])
1220 cpCloneBndrs env bs = mapAccumLM cpCloneBndr env bs
1221
1222 cpCloneBndr :: CorePrepEnv -> Var -> UniqSM (CorePrepEnv, Var)
1223 cpCloneBndr env bndr
1224 | isLocalId bndr, not (isCoVar bndr)
1225 = do bndr' <- setVarUnique bndr <$> getUniqueM
1226
1227 -- We are going to OccAnal soon, so drop (now-useless) rules/unfoldings
1228 -- so that we can drop more stuff as dead code.
1229 -- See also Note [Dead code in CorePrep]
1230 let bndr'' = bndr' `setIdUnfolding` noUnfolding
1231 `setIdSpecialisation` emptyRuleInfo
1232 return (extendCorePrepEnv env bndr bndr'', bndr'')
1233
1234 | otherwise -- Top level things, which we don't want
1235 -- to clone, have become GlobalIds by now
1236 -- And we don't clone tyvars, or coercion variables
1237 = return (env, bndr)
1238
1239
1240 ------------------------------------------------------------------------------
1241 -- Cloning ccall Ids; each must have a unique name,
1242 -- to give the code generator a handle to hang it on
1243 -- ---------------------------------------------------------------------------
1244
1245 fiddleCCall :: Id -> UniqSM Id
1246 fiddleCCall id
1247 | isFCallId id = (id `setVarUnique`) <$> getUniqueM
1248 | otherwise = return id
1249
1250 ------------------------------------------------------------------------------
1251 -- Generating new binders
1252 -- ---------------------------------------------------------------------------
1253
1254 newVar :: Type -> UniqSM Id
1255 newVar ty
1256 = seqType ty `seq` do
1257 uniq <- getUniqueM
1258 return (mkSysLocalOrCoVar (fsLit "sat") uniq ty)
1259
1260
1261 ------------------------------------------------------------------------------
1262 -- Floating ticks
1263 -- ---------------------------------------------------------------------------
1264 --
1265 -- Note [Floating Ticks in CorePrep]
1266 --
1267 -- It might seem counter-intuitive to float ticks by default, given
1268 -- that we don't actually want to move them if we can help it. On the
1269 -- other hand, nothing gets very far in CorePrep anyway, and we want
1270 -- to preserve the order of let bindings and tick annotations in
1271 -- relation to each other. For example, if we just wrapped let floats
1272 -- when they pass through ticks, we might end up performing the
1273 -- following transformation:
1274 --
1275 -- src<...> let foo = bar in baz
1276 -- ==> let foo = src<...> bar in src<...> baz
1277 --
1278 -- Because the let-binding would float through the tick, and then
1279 -- immediately materialize, achieving nothing but decreasing tick
1280 -- accuracy. The only special case is the following scenario:
1281 --
1282 -- let foo = src<...> (let a = b in bar) in baz
1283 -- ==> let foo = src<...> bar; a = src<...> b in baz
1284 --
1285 -- Here we would not want the source tick to end up covering "baz" and
1286 -- therefore refrain from pushing ticks outside. Instead, we copy them
1287 -- into the floating binds (here "a") in cpePair. Note that where "b"
1288 -- or "bar" are (value) lambdas we have to push the annotations
1289 -- further inside in order to uphold our rules.
1290 --
1291 -- All of this is implemented below in @wrapTicks@.
1292
1293 -- | Like wrapFloats, but only wraps tick floats
1294 wrapTicks :: Floats -> CoreExpr -> (Floats, CoreExpr)
1295 wrapTicks (Floats flag floats0) expr = (Floats flag floats1, expr')
1296 where (floats1, expr') = foldrOL go (nilOL, expr) floats0
1297 go (FloatTick t) (fs, e) = ASSERT(tickishPlace t == PlaceNonLam)
1298 (mapOL (wrap t) fs, mkTick t e)
1299 go other (fs, e) = (other `consOL` fs, e)
1300 wrap t (FloatLet bind) = FloatLet (wrapBind t bind)
1301 wrap t (FloatCase b r ok) = FloatCase b (mkTick t r) ok
1302 wrap _ other = pprPanic "wrapTicks: unexpected float!"
1303 (ppr other)
1304 wrapBind t (NonRec binder rhs) = NonRec binder (mkTick t rhs)
1305 wrapBind t (Rec pairs) = Rec (mapSnd (mkTick t) pairs)