1 {-# LANGUAGE PackageImports #-}
3 -- Functions to bring a Core expression in normal form. This module provides a
4 -- top level function "normalize", and defines the actual transformation passes that
7 module CLasH.Normalize (getNormalized, normalizeExpr, splitNormalized) where
11 import qualified Maybe
13 import qualified "transformers" Control.Monad.Trans as Trans
14 import qualified Control.Monad as Monad
15 import qualified Control.Monad.Trans.Writer as Writer
16 import qualified Data.Map as Map
17 import qualified Data.Monoid as Monoid
22 import qualified UniqSupply
23 import qualified CoreUtils
25 import qualified TcType
28 import qualified VarSet
29 import qualified NameSet
30 import qualified CoreFVs
31 import qualified CoreUtils
32 import qualified MkCore
33 import qualified HscTypes
34 import Outputable ( showSDoc, ppr, nest )
37 import CLasH.Normalize.NormalizeTypes
38 import CLasH.Translator.TranslatorTypes
39 import CLasH.Normalize.NormalizeTools
40 import CLasH.VHDL.VHDLTypes
41 import qualified CLasH.Utils as Utils
42 import CLasH.Utils.Core.CoreTools
43 import CLasH.Utils.Core.BinderTools
44 import CLasH.Utils.Pretty
46 --------------------------------
47 -- Start of transformations
48 --------------------------------
50 --------------------------------
52 --------------------------------
53 eta, etatop :: Transform
54 eta expr | is_fun expr && not (is_lam expr) = do
55 let arg_ty = (fst . Type.splitFunTy . CoreUtils.exprType) expr
56 id <- Trans.lift $ mkInternalVar "param" arg_ty
57 change (Lam id (App expr (Var id)))
58 -- Leave all other expressions unchanged
60 etatop = notappargs ("eta", eta)
62 --------------------------------
64 --------------------------------
65 beta, betatop :: Transform
66 -- Substitute arg for x in expr
67 beta (App (Lam x expr) arg) = change $ substitute [(x, arg)] expr
68 -- Propagate the application into the let
69 beta (App (Let binds expr) arg) = change $ Let binds (App expr arg)
70 -- Propagate the application into each of the alternatives
71 beta (App (Case scrut b ty alts) arg) = change $ Case scrut b ty' alts'
73 alts' = map (\(con, bndrs, expr) -> (con, bndrs, (App expr arg))) alts
74 ty' = CoreUtils.applyTypeToArg ty arg
75 -- Leave all other expressions unchanged
76 beta expr = return expr
77 -- Perform this transform everywhere
78 betatop = everywhere ("beta", beta)
80 --------------------------------
82 --------------------------------
83 -- Try to move casts as much downward as possible.
84 castprop, castproptop :: Transform
85 castprop (Cast (Let binds expr) ty) = change $ Let binds (Cast expr ty)
86 castprop expr@(Cast (Case scrut b _ alts) ty) = change (Case scrut b ty alts')
88 alts' = map (\(con, bndrs, expr) -> (con, bndrs, (Cast expr ty))) alts
89 -- Leave all other expressions unchanged
90 castprop expr = return expr
91 -- Perform this transform everywhere
92 castproptop = everywhere ("castprop", castprop)
94 --------------------------------
95 -- Cast simplification. Mostly useful for state packing and unpacking, but
96 -- perhaps for others as well.
97 --------------------------------
98 castsimpl, castsimpltop :: Transform
99 castsimpl expr@(Cast val ty) = do
100 -- Don't extract values that are already simpl
101 local_var <- Trans.lift $ is_local_var val
102 -- Don't extract values that are not representable, to prevent loops with
105 if (not local_var) && repr
107 -- Generate a binder for the expression
108 id <- Trans.lift $ mkBinderFor val "castval"
109 -- Extract the expression
110 change $ Let (NonRec id val) (Cast (Var id) ty)
113 -- Leave all other expressions unchanged
114 castsimpl expr = return expr
115 -- Perform this transform everywhere
116 castsimpltop = everywhere ("castsimpl", castsimpl)
118 --------------------------------
119 -- let derecursification
120 --------------------------------
121 letderec, letderectop :: Transform
122 letderec expr@(Let (Rec binds) res) = case liftable of
123 -- Nothing is liftable, just return
125 -- Something can be lifted, generate a new let expression
126 _ -> change $ MkCore.mkCoreLets newbinds res
128 -- Make a list of all the binders bound in this recursive let
129 bndrs = map fst binds
130 -- See which bindings are liftable
131 (liftable, nonliftable) = List.partition canlift binds
132 -- Create nonrec bindings for each liftable binding and a single recursive
133 -- binding for all others
134 newbinds = (map (uncurry NonRec) liftable) ++ [Rec nonliftable]
135 -- Any expression that does not use any of the binders in this recursive let
136 -- can be lifted into a nonrec let. It can't use its own binder either,
137 -- since that would mean the binding is self-recursive and should be in a
138 -- single bind recursive let.
139 canlift (bndr, e) = not $ expr_uses_binders bndrs e
140 -- Leave all other expressions unchanged
141 letderec expr = return expr
142 -- Perform this transform everywhere
143 letderectop = everywhere ("letderec", letderec)
145 --------------------------------
146 -- let simplification
147 --------------------------------
148 letsimpl, letsimpltop :: Transform
149 -- Don't simplify a let that evaluates to another let, since this is already
150 -- normal form (and would cause infinite loops with letflat below).
151 letsimpl expr@(Let _ (Let _ _)) = return expr
152 -- Put the "in ..." value of a let in its own binding, but not when the
153 -- expression is already a local variable, or not representable (to prevent loops with inlinenonrep).
154 letsimpl expr@(Let binds res) = do
156 local_var <- Trans.lift $ is_local_var res
157 if not local_var && repr
159 -- If the result is not a local var already (to prevent loops with
160 -- ourselves), extract it.
161 id <- Trans.lift $ mkBinderFor res "foo"
162 change $ Let binds (Let (NonRec id res) (Var id))
164 -- If the result is already a local var, don't extract it.
167 -- Leave all other expressions unchanged
168 letsimpl expr = return expr
169 -- Perform this transform everywhere
170 letsimpltop = everywhere ("letsimpl", letsimpl)
172 --------------------------------
174 --------------------------------
175 -- Takes a let that binds another let, and turns that into two nested lets.
177 -- let b = (let b' = expr' in res') in res
179 -- let b' = expr' in (let b = res' in res)
180 letflat, letflattop :: Transform
181 letflat (Let (NonRec b (Let (NonRec b' expr') res')) res) =
182 change $ Let (NonRec b' expr') (Let (NonRec b res') res)
183 -- Leave all other expressions unchanged
184 letflat expr = return expr
185 -- Perform this transform everywhere
186 letflattop = everywhere ("letflat", letflat)
188 --------------------------------
190 --------------------------------
191 -- Remove empty (recursive) lets
192 letremove, letremovetop :: Transform
193 letremove (Let (Rec []) res) = change $ res
194 -- Leave all other expressions unchanged
195 letremove expr = return expr
196 -- Perform this transform everywhere
197 letremovetop = everywhere ("letremove", letremove)
199 --------------------------------
200 -- Simple let binding removal
201 --------------------------------
202 -- Remove a = b bindings from let expressions everywhere
203 letremovesimpletop :: Transform
204 letremovesimpletop = everywhere ("letremovesimple", inlinebind (\(b, e) -> Trans.lift $ is_local_var e))
206 --------------------------------
207 -- Unused let binding removal
208 --------------------------------
209 letremoveunused, letremoveunusedtop :: Transform
210 letremoveunused expr@(Let (Rec binds) res) = do
211 -- Filter out all unused binds.
212 let binds' = filter dobind binds
213 -- Only set the changed flag if binds got removed
214 changeif (length binds' /= length binds) (Let (Rec binds') res)
216 bound_exprs = map snd binds
217 -- For each bind check if the bind is used by res or any of the bound
219 dobind (bndr, _) = any (expr_uses_binders [bndr]) (res:bound_exprs)
220 -- Leave all other expressions unchanged
221 letremoveunused expr = return expr
222 letremoveunusedtop = everywhere ("letremoveunused", letremoveunused)
224 --------------------------------
225 -- Identical let binding merging
226 --------------------------------
227 -- Merge two bindings in a let if they are identical
228 -- TODO: We would very much like to use GHC's CSE module for this, but that
229 -- doesn't track if something changed or not, so we can't use it properly.
230 letmerge, letmergetop :: Transform
231 letmerge expr@(Let (Rec binds) res) = do
232 binds' <- domerge binds
233 return (Let (Rec binds') res)
235 domerge :: [(CoreBndr, CoreExpr)] -> TransformMonad [(CoreBndr, CoreExpr)]
236 domerge [] = return []
238 es' <- mapM (mergebinds e) es
242 -- Uses the second bind to simplify the second bind, if applicable.
243 mergebinds :: (CoreBndr, CoreExpr) -> (CoreBndr, CoreExpr) -> TransformMonad (CoreBndr, CoreExpr)
244 mergebinds (b1, e1) (b2, e2)
245 -- Identical expressions? Replace the second binding with a reference to
247 | CoreUtils.cheapEqExpr e1 e2 = change $ (b2, Var b1)
248 -- Different expressions? Don't change
249 | otherwise = return (b2, e2)
250 -- Leave all other expressions unchanged
251 letmerge expr = return expr
252 letmergetop = everywhere ("letmerge", letmerge)
254 --------------------------------
256 --------------------------------
257 -- Remove a = B bindings, with B :: a -> b, or B :: forall x . T, from let
258 -- expressions everywhere. This means that any value that still needs to be
259 -- applied to something else (polymorphic values need to be applied to a
260 -- Type) will be inlined, and will eventually be applied to all their
263 -- This is a tricky function, which is prone to create loops in the
264 -- transformations. To fix this, we make sure that no transformation will
265 -- create a new let binding with a function type. These other transformations
266 -- will just not work on those function-typed values at first, but the other
267 -- transformations (in particular β-reduction) should make sure that the type
268 -- of those values eventually becomes primitive.
269 inlinenonreptop :: Transform
270 inlinenonreptop = everywhere ("inlinenonrep", inlinebind ((Monad.liftM not) . isRepr . snd))
272 --------------------------------
273 -- Scrutinee simplification
274 --------------------------------
275 scrutsimpl,scrutsimpltop :: Transform
276 -- Don't touch scrutinees that are already simple
277 scrutsimpl expr@(Case (Var _) _ _ _) = return expr
278 -- Replace all other cases with a let that binds the scrutinee and a new
279 -- simple scrutinee, but only when the scrutinee is representable (to prevent
280 -- loops with inlinenonrep, though I don't think a non-representable scrutinee
281 -- will be supported anyway...)
282 scrutsimpl expr@(Case scrut b ty alts) = do
286 id <- Trans.lift $ mkBinderFor scrut "scrut"
287 change $ Let (NonRec id scrut) (Case (Var id) b ty alts)
290 -- Leave all other expressions unchanged
291 scrutsimpl expr = return expr
292 -- Perform this transform everywhere
293 scrutsimpltop = everywhere ("scrutsimpl", scrutsimpl)
295 --------------------------------
296 -- Case binder wildening
297 --------------------------------
298 casesimpl, casesimpltop :: Transform
299 -- This is already a selector case (or, if x does not appear in bndrs, a very
300 -- simple case statement that will be removed by caseremove below). Just leave
302 casesimpl expr@(Case scrut b ty [(con, bndrs, Var x)]) = return expr
303 -- Make sure that all case alternatives have only wild binders and simple
305 -- This is done by creating a new let binding for each non-wild binder, which
306 -- is bound to a new simple selector case statement and for each complex
307 -- expression. We do this only for representable types, to prevent loops with
309 casesimpl expr@(Case scrut b ty alts) = do
310 (bindingss, alts') <- (Monad.liftM unzip) $ mapM doalt alts
311 let bindings = concat bindingss
312 -- Replace the case with a let with bindings and a case
313 let newlet = (Let (Rec bindings) (Case scrut b ty alts'))
314 -- If there are no non-wild binders, or this case is already a simple
315 -- selector (i.e., a single alt with exactly one binding), already a simple
316 -- selector altan no bindings (i.e., no wild binders in the original case),
317 -- don't change anything, otherwise, replace the case.
318 if null bindings then return expr else change newlet
320 -- Generate a single wild binder, since they are all the same
321 wild = MkCore.mkWildBinder
322 -- Wilden the binders of one alt, producing a list of bindings as a
324 doalt :: CoreAlt -> TransformMonad ([(CoreBndr, CoreExpr)], CoreAlt)
325 doalt (con, bndrs, expr) = do
326 -- Make each binder wild, if possible
327 bndrs_res <- Monad.zipWithM dobndr bndrs [0..]
328 let (newbndrs, bindings_maybe) = unzip bndrs_res
329 -- Extract a complex expression, if possible. For this we check if any of
330 -- the new list of bndrs are used by expr. We can't use free_vars here,
331 -- since that looks at the old bndrs.
332 let uses_bndrs = not $ VarSet.isEmptyVarSet $ CoreFVs.exprSomeFreeVars (`elem` newbndrs) $ expr
333 (exprbinding_maybe, expr') <- doexpr expr uses_bndrs
334 -- Create a new alternative
335 let newalt = (con, newbndrs, expr')
336 let bindings = Maybe.catMaybes (exprbinding_maybe : bindings_maybe)
337 return (bindings, newalt)
339 -- Make wild alternatives for each binder
340 wildbndrs = map (\bndr -> MkCore.mkWildBinder (Id.idType bndr)) bndrs
341 -- A set of all the binders that are used by the expression
342 free_vars = CoreFVs.exprSomeFreeVars (`elem` bndrs) expr
343 -- Look at the ith binder in the case alternative. Return a new binder
344 -- for it (either the same one, or a wild one) and optionally a let
345 -- binding containing a case expression.
346 dobndr :: CoreBndr -> Int -> TransformMonad (CoreBndr, Maybe (CoreBndr, CoreExpr))
348 repr <- isRepr (Var b)
349 -- Is b wild (e.g., not a free var of expr. Since b is only in scope
350 -- in expr, this means that b is unused if expr does not use it.)
351 let wild = not (VarSet.elemVarSet b free_vars)
352 -- Create a new binding for any representable binder that is not
353 -- already wild and is representable (to prevent loops with
355 if (not wild) && repr
357 -- Create on new binder that will actually capture a value in this
358 -- case statement, and return it.
359 let bty = (Id.idType b)
360 id <- Trans.lift $ mkInternalVar "sel" bty
361 let binders = take i wildbndrs ++ [id] ++ drop (i+1) wildbndrs
362 let caseexpr = Case scrut b bty [(con, binders, Var id)]
363 return (wildbndrs!!i, Just (b, caseexpr))
365 -- Just leave the original binder in place, and don't generate an
366 -- extra selector case.
368 -- Process the expression of a case alternative. Accepts an expression
369 -- and whether this expression uses any of the binders in the
370 -- alternative. Returns an optional new binding and a new expression.
371 doexpr :: CoreExpr -> Bool -> TransformMonad (Maybe (CoreBndr, CoreExpr), CoreExpr)
372 doexpr expr uses_bndrs = do
373 local_var <- Trans.lift $ is_local_var expr
375 -- Extract any expressions that do not use any binders from this
376 -- alternative, is not a local var already and is representable (to
377 -- prevent loops with inlinenonrep).
378 if (not uses_bndrs) && (not local_var) && repr
380 id <- Trans.lift $ mkBinderFor expr "caseval"
381 -- We don't flag a change here, since casevalsimpl will do that above
382 -- based on Just we return here.
383 return $ (Just (id, expr), Var id)
385 -- Don't simplify anything else
386 return (Nothing, expr)
387 -- Leave all other expressions unchanged
388 casesimpl expr = return expr
389 -- Perform this transform everywhere
390 casesimpltop = everywhere ("casesimpl", casesimpl)
392 --------------------------------
394 --------------------------------
395 -- Remove case statements that have only a single alternative and only wild
397 caseremove, caseremovetop :: Transform
398 -- Replace a useless case by the value of its single alternative
399 caseremove (Case scrut b ty [(con, bndrs, expr)]) | not usesvars = change expr
400 -- Find if any of the binders are used by expr
401 where usesvars = (not . VarSet.isEmptyVarSet . (CoreFVs.exprSomeFreeVars (`elem` bndrs))) expr
402 -- Leave all other expressions unchanged
403 caseremove expr = return expr
404 -- Perform this transform everywhere
405 caseremovetop = everywhere ("caseremove", caseremove)
407 --------------------------------
408 -- Argument extraction
409 --------------------------------
410 -- Make sure that all arguments of a representable type are simple variables.
411 appsimpl, appsimpltop :: Transform
412 -- Simplify all representable arguments. Do this by introducing a new Let
413 -- that binds the argument and passing the new binder in the application.
414 appsimpl expr@(App f arg) = do
415 -- Check runtime representability
417 local_var <- Trans.lift $ is_local_var arg
418 if repr && not local_var
419 then do -- Extract representable arguments
420 id <- Trans.lift $ mkBinderFor arg "arg"
421 change $ Let (NonRec id arg) (App f (Var id))
422 else -- Leave non-representable arguments unchanged
424 -- Leave all other expressions unchanged
425 appsimpl expr = return expr
426 -- Perform this transform everywhere
427 appsimpltop = everywhere ("appsimpl", appsimpl)
429 --------------------------------
430 -- Function-typed argument propagation
431 --------------------------------
432 -- Remove all applications to function-typed arguments, by duplication the
433 -- function called with the function-typed parameter replaced by the free
434 -- variables of the argument passed in.
435 argprop, argproptop :: Transform
436 -- Transform any application of a named function (i.e., skip applications of
437 -- lambda's). Also skip applications that have arguments with free type
438 -- variables, since we can't inline those.
439 argprop expr@(App _ _) | is_var fexpr = do
440 -- Find the body of the function called
441 body_maybe <- Trans.lift $ getGlobalBind f
444 -- Process each of the arguments in turn
445 (args', changed) <- Writer.listen $ mapM doarg args
446 -- See if any of the arguments changed
447 case Monoid.getAny changed of
449 let (newargs', newparams', oldargs) = unzip3 args'
450 let newargs = concat newargs'
451 let newparams = concat newparams'
452 -- Create a new body that consists of a lambda for all new arguments and
453 -- the old body applied to some arguments.
454 let newbody = MkCore.mkCoreLams newparams (MkCore.mkCoreApps body oldargs)
455 -- Create a new function with the same name but a new body
456 newf <- Trans.lift $ mkFunction f newbody
457 -- Replace the original application with one of the new function to the
459 change $ MkCore.mkCoreApps (Var newf) newargs
461 -- Don't change the expression if none of the arguments changed
464 -- If we don't have a body for the function called, leave it unchanged (it
465 -- should be a primitive function then).
466 Nothing -> return expr
468 -- Find the function called and the arguments
469 (fexpr, args) = collectArgs expr
472 -- Process a single argument and return (args, bndrs, arg), where args are
473 -- the arguments to replace the given argument in the original
474 -- application, bndrs are the binders to include in the top-level lambda
475 -- in the new function body, and arg is the argument to apply to the old
477 doarg :: CoreExpr -> TransformMonad ([CoreExpr], [CoreBndr], CoreExpr)
480 bndrs <- Trans.lift getGlobalBinders
481 let interesting var = Var.isLocalVar var && (not $ var `elem` bndrs)
482 if not repr && not (is_var arg && interesting (exprToVar arg)) && not (has_free_tyvars arg)
484 -- Propagate all complex arguments that are not representable, but not
485 -- arguments with free type variables (since those would require types
486 -- not known yet, which will always be known eventually).
487 -- Find interesting free variables, each of which should be passed to
488 -- the new function instead of the original function argument.
490 -- Interesting vars are those that are local, but not available from the
491 -- top level scope (functions from this module are defined as local, but
492 -- they're not local to this function, so we can freely move references
493 -- to them into another function).
494 let free_vars = VarSet.varSetElems $ CoreFVs.exprSomeFreeVars interesting arg
495 -- Mark the current expression as changed
497 return (map Var free_vars, free_vars, arg)
499 -- Representable types will not be propagated, and arguments with free
500 -- type variables will be propagated later.
501 -- TODO: preserve original naming?
502 id <- Trans.lift $ mkBinderFor arg "param"
503 -- Just pass the original argument to the new function, which binds it
504 -- to a new id and just pass that new id to the old function body.
505 return ([arg], [id], mkReferenceTo id)
506 -- Leave all other expressions unchanged
507 argprop expr = return expr
508 -- Perform this transform everywhere
509 argproptop = everywhere ("argprop", argprop)
511 --------------------------------
512 -- Function-typed argument extraction
513 --------------------------------
514 -- This transform takes any function-typed argument that cannot be propagated
515 -- (because the function that is applied to it is a builtin function), and
516 -- puts it in a brand new top level binder. This allows us to for example
517 -- apply map to a lambda expression This will not conflict with inlinenonrep,
518 -- since that only inlines local let bindings, not top level bindings.
519 funextract, funextracttop :: Transform
520 funextract expr@(App _ _) | is_var fexpr = do
521 body_maybe <- Trans.lift $ getGlobalBind f
523 -- We don't have a function body for f, so we can perform this transform.
525 -- Find the new arguments
526 args' <- mapM doarg args
527 -- And update the arguments. We use return instead of changed, so the
528 -- changed flag doesn't get set if none of the args got changed.
529 return $ MkCore.mkCoreApps fexpr args'
530 -- We have a function body for f, leave this application to funprop
531 Just _ -> return expr
533 -- Find the function called and the arguments
534 (fexpr, args) = collectArgs expr
536 -- Change any arguments that have a function type, but are not simple yet
537 -- (ie, a variable or application). This means to create a new function
538 -- for map (\f -> ...) b, but not for map (foo a) b.
540 -- We could use is_applicable here instead of is_fun, but I think
541 -- arguments to functions could only have forall typing when existential
542 -- typing is enabled. Not sure, though.
543 doarg arg | not (is_simple arg) && is_fun arg = do
544 -- Create a new top level binding that binds the argument. Its body will
545 -- be extended with lambda expressions, to take any free variables used
546 -- by the argument expression.
547 let free_vars = VarSet.varSetElems $ CoreFVs.exprFreeVars arg
548 let body = MkCore.mkCoreLams free_vars arg
549 id <- Trans.lift $ mkBinderFor body "fun"
550 Trans.lift $ addGlobalBind id body
551 -- Replace the argument with a reference to the new function, applied to
553 change $ MkCore.mkCoreApps (Var id) (map Var free_vars)
554 -- Leave all other arguments untouched
555 doarg arg = return arg
557 -- Leave all other expressions unchanged
558 funextract expr = return expr
559 -- Perform this transform everywhere
560 funextracttop = everywhere ("funextract", funextract)
562 --------------------------------
563 -- End of transformations
564 --------------------------------
569 -- What transforms to run?
570 transforms = [argproptop, funextracttop, etatop, betatop, castproptop, letremovesimpletop, letderectop, letremovetop, letsimpltop, letflattop, scrutsimpltop, casesimpltop, caseremovetop, inlinenonreptop, appsimpltop, letmergetop, letremoveunusedtop, castsimpltop]
572 -- | Returns the normalized version of the given function.
574 CoreBndr -- ^ The function to get
575 -> TranslatorSession CoreExpr -- The normalized function body
577 getNormalized bndr = Utils.makeCached bndr tsNormalized $ do
578 if is_poly (Var bndr)
580 -- This should really only happen at the top level... TODO: Give
581 -- a different error if this happens down in the recursion.
582 error $ "\nNormalize.normalizeBind: Function " ++ show bndr ++ " is polymorphic, can't normalize"
584 expr <- getBinding bndr
585 normalizeExpr (show bndr) expr
587 -- | Normalize an expression
589 String -- ^ What are we normalizing? For debug output only.
590 -> CoreSyn.CoreExpr -- ^ The expression to normalize
591 -> TranslatorSession CoreSyn.CoreExpr -- ^ The normalized expression
593 normalizeExpr what expr = do
594 -- Normalize this expression
595 trace (what ++ " before normalization:\n\n" ++ showSDoc ( ppr expr ) ++ "\n") $ return ()
596 expr' <- dotransforms transforms expr
597 trace ("\n" ++ what ++ " after normalization:\n\n" ++ showSDoc ( ppr expr')) $ return ()
600 -- | Get the value that is bound to the given binder at top level. Fails when
601 -- there is no such binding.
603 CoreBndr -- ^ The binder to get the expression for
604 -> TranslatorSession CoreExpr -- ^ The value bound to the binder
606 getBinding bndr = Utils.makeCached bndr tsBindings $ do
607 -- If the binding isn't in the "cache" (bindings map), then we can't create
608 -- it out of thin air, so return an error.
609 error $ "Normalize.getBinding: Unknown function requested: " ++ show bndr
611 -- | Split a normalized expression into the argument binders, top level
612 -- bindings and the result binder.
614 CoreExpr -- ^ The normalized expression
615 -> ([CoreBndr], [Binding], CoreBndr)
616 splitNormalized expr = (args, binds, res)
618 (args, letexpr) = CoreSyn.collectBinders expr
619 (binds, resexpr) = flattenLets letexpr
620 res = case resexpr of
622 _ -> error $ "Normalize.splitNormalized: Not in normal form: " ++ pprString expr ++ "\n"
624 -- | Flattens nested lets into a single list of bindings. The expression
625 -- passed does not have to be a let expression, if it isn't an empty list of
626 -- bindings is returned.
628 CoreExpr -- ^ The expression to flatten.
629 -> ([Binding], CoreExpr) -- ^ The bindings and resulting expression.
630 flattenLets (Let binds expr) =
631 (bindings ++ bindings', expr')
633 -- Recursively flatten the contained expression
634 (bindings', expr') =flattenLets expr
635 -- Flatten our own bindings to remove the Rec / NonRec constructors
636 bindings = CoreSyn.flattenBinds [binds]
637 flattenLets expr = ([], expr)