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 $ mkNonRecLets liftable (Let (Rec nonliftable) 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 -- Any expression that does not use any of the binders in this recursive let
133 -- can be lifted into a nonrec let. It can't use its own binder either,
134 -- since that would mean the binding is self-recursive and should be in a
135 -- single bind recursive let.
136 canlift (bndr, e) = not $ expr_uses_binders bndrs e
137 -- Leave all other expressions unchanged
138 letderec expr = return expr
139 -- Perform this transform everywhere
140 letderectop = everywhere ("letderec", letderec)
142 --------------------------------
143 -- let simplification
144 --------------------------------
145 letsimpl, letsimpltop :: Transform
146 -- Don't simplify a let that evaluates to another let, since this is already
147 -- normal form (and would cause infinite loops with letflat below).
148 letsimpl expr@(Let _ (Let _ _)) = return expr
149 -- Put the "in ..." value of a let in its own binding, but not when the
150 -- expression is already a local variable, or not representable (to prevent loops with inlinenonrep).
151 letsimpl expr@(Let binds res) = do
153 local_var <- Trans.lift $ is_local_var res
154 if not local_var && repr
156 -- If the result is not a local var already (to prevent loops with
157 -- ourselves), extract it.
158 id <- Trans.lift $ mkBinderFor res "foo"
159 change $ Let binds (Let (NonRec id res) (Var id))
161 -- If the result is already a local var, don't extract it.
164 -- Leave all other expressions unchanged
165 letsimpl expr = return expr
166 -- Perform this transform everywhere
167 letsimpltop = everywhere ("letsimpl", letsimpl)
169 --------------------------------
171 --------------------------------
172 -- Takes a let that binds another let, and turns that into two nested lets.
174 -- let b = (let b' = expr' in res') in res
176 -- let b' = expr' in (let b = res' in res)
177 letflat, letflattop :: Transform
178 letflat (Let (NonRec b (Let (NonRec b' expr') res')) res) =
179 change $ Let (NonRec b' expr') (Let (NonRec b res') res)
180 -- Leave all other expressions unchanged
181 letflat expr = return expr
182 -- Perform this transform everywhere
183 letflattop = everywhere ("letflat", letflat)
185 --------------------------------
187 --------------------------------
188 -- Remove empty (recursive) lets
189 letremove, letremovetop :: Transform
190 letremove (Let (Rec []) res) = change $ res
191 -- Leave all other expressions unchanged
192 letremove expr = return expr
193 -- Perform this transform everywhere
194 letremovetop = everywhere ("letremove", letremove)
196 --------------------------------
197 -- Simple let binding removal
198 --------------------------------
199 -- Remove a = b bindings from let expressions everywhere
200 letremovesimpletop :: Transform
201 letremovesimpletop = everywhere ("letremovesimple", inlinebind (\(b, e) -> Trans.lift $ is_local_var e))
203 --------------------------------
204 -- Unused let binding removal
205 --------------------------------
206 letremoveunused, letremoveunusedtop :: Transform
207 letremoveunused expr@(Let (Rec binds) res) = do
208 -- Filter out all unused binds.
209 let binds' = filter dobind binds
210 -- Only set the changed flag if binds got removed
211 changeif (length binds' /= length binds) (Let (Rec binds') res)
213 bound_exprs = map snd binds
214 -- For each bind check if the bind is used by res or any of the bound
216 dobind (bndr, _) = any (expr_uses_binders [bndr]) (res:bound_exprs)
217 -- Leave all other expressions unchanged
218 letremoveunused expr = return expr
219 letremoveunusedtop = everywhere ("letremoveunused", letremoveunused)
221 --------------------------------
222 -- Identical let binding merging
223 --------------------------------
224 -- Merge two bindings in a let if they are identical
225 -- TODO: We would very much like to use GHC's CSE module for this, but that
226 -- doesn't track if something changed or not, so we can't use it properly.
227 letmerge, letmergetop :: Transform
228 letmerge expr@(Let _ _) = do
229 let (binds, res) = flattenLets expr
230 binds' <- domerge binds
231 return $ mkNonRecLets binds' res
233 domerge :: [(CoreBndr, CoreExpr)] -> TransformMonad [(CoreBndr, CoreExpr)]
234 domerge [] = return []
236 es' <- mapM (mergebinds e) es
240 -- Uses the second bind to simplify the second bind, if applicable.
241 mergebinds :: (CoreBndr, CoreExpr) -> (CoreBndr, CoreExpr) -> TransformMonad (CoreBndr, CoreExpr)
242 mergebinds (b1, e1) (b2, e2)
243 -- Identical expressions? Replace the second binding with a reference to
245 | CoreUtils.cheapEqExpr e1 e2 = change $ (b2, Var b1)
246 -- Different expressions? Don't change
247 | otherwise = return (b2, e2)
248 -- Leave all other expressions unchanged
249 letmerge expr = return expr
250 letmergetop = everywhere ("letmerge", letmerge)
252 --------------------------------
254 --------------------------------
255 -- Remove a = B bindings, with B :: a -> b, or B :: forall x . T, from let
256 -- expressions everywhere. This means that any value that still needs to be
257 -- applied to something else (polymorphic values need to be applied to a
258 -- Type) will be inlined, and will eventually be applied to all their
261 -- This is a tricky function, which is prone to create loops in the
262 -- transformations. To fix this, we make sure that no transformation will
263 -- create a new let binding with a function type. These other transformations
264 -- will just not work on those function-typed values at first, but the other
265 -- transformations (in particular β-reduction) should make sure that the type
266 -- of those values eventually becomes primitive.
267 inlinenonreptop :: Transform
268 inlinenonreptop = everywhere ("inlinenonrep", inlinebind ((Monad.liftM not) . isRepr . snd))
270 --------------------------------
271 -- Scrutinee simplification
272 --------------------------------
273 scrutsimpl,scrutsimpltop :: Transform
274 -- Don't touch scrutinees that are already simple
275 scrutsimpl expr@(Case (Var _) _ _ _) = return expr
276 -- Replace all other cases with a let that binds the scrutinee and a new
277 -- simple scrutinee, but only when the scrutinee is representable (to prevent
278 -- loops with inlinenonrep, though I don't think a non-representable scrutinee
279 -- will be supported anyway...)
280 scrutsimpl expr@(Case scrut b ty alts) = do
284 id <- Trans.lift $ mkBinderFor scrut "scrut"
285 change $ Let (NonRec id scrut) (Case (Var id) b ty alts)
288 -- Leave all other expressions unchanged
289 scrutsimpl expr = return expr
290 -- Perform this transform everywhere
291 scrutsimpltop = everywhere ("scrutsimpl", scrutsimpl)
293 --------------------------------
294 -- Case binder wildening
295 --------------------------------
296 casesimpl, casesimpltop :: Transform
297 -- This is already a selector case (or, if x does not appear in bndrs, a very
298 -- simple case statement that will be removed by caseremove below). Just leave
300 casesimpl expr@(Case scrut b ty [(con, bndrs, Var x)]) = return expr
301 -- Make sure that all case alternatives have only wild binders and simple
303 -- This is done by creating a new let binding for each non-wild binder, which
304 -- is bound to a new simple selector case statement and for each complex
305 -- expression. We do this only for representable types, to prevent loops with
307 casesimpl expr@(Case scrut b ty alts) = do
308 (bindingss, alts') <- (Monad.liftM unzip) $ mapM doalt alts
309 let bindings = concat bindingss
310 -- Replace the case with a let with bindings and a case
311 let newlet = (Let (Rec bindings) (Case scrut b ty alts'))
312 -- If there are no non-wild binders, or this case is already a simple
313 -- selector (i.e., a single alt with exactly one binding), already a simple
314 -- selector altan no bindings (i.e., no wild binders in the original case),
315 -- don't change anything, otherwise, replace the case.
316 if null bindings then return expr else change newlet
318 -- Generate a single wild binder, since they are all the same
319 wild = MkCore.mkWildBinder
320 -- Wilden the binders of one alt, producing a list of bindings as a
322 doalt :: CoreAlt -> TransformMonad ([(CoreBndr, CoreExpr)], CoreAlt)
323 doalt (con, bndrs, expr) = do
324 -- Make each binder wild, if possible
325 bndrs_res <- Monad.zipWithM dobndr bndrs [0..]
326 let (newbndrs, bindings_maybe) = unzip bndrs_res
327 -- Extract a complex expression, if possible. For this we check if any of
328 -- the new list of bndrs are used by expr. We can't use free_vars here,
329 -- since that looks at the old bndrs.
330 let uses_bndrs = not $ VarSet.isEmptyVarSet $ CoreFVs.exprSomeFreeVars (`elem` newbndrs) $ expr
331 (exprbinding_maybe, expr') <- doexpr expr uses_bndrs
332 -- Create a new alternative
333 let newalt = (con, newbndrs, expr')
334 let bindings = Maybe.catMaybes (exprbinding_maybe : bindings_maybe)
335 return (bindings, newalt)
337 -- Make wild alternatives for each binder
338 wildbndrs = map (\bndr -> MkCore.mkWildBinder (Id.idType bndr)) bndrs
339 -- A set of all the binders that are used by the expression
340 free_vars = CoreFVs.exprSomeFreeVars (`elem` bndrs) expr
341 -- Look at the ith binder in the case alternative. Return a new binder
342 -- for it (either the same one, or a wild one) and optionally a let
343 -- binding containing a case expression.
344 dobndr :: CoreBndr -> Int -> TransformMonad (CoreBndr, Maybe (CoreBndr, CoreExpr))
346 repr <- isRepr (Var b)
347 -- Is b wild (e.g., not a free var of expr. Since b is only in scope
348 -- in expr, this means that b is unused if expr does not use it.)
349 let wild = not (VarSet.elemVarSet b free_vars)
350 -- Create a new binding for any representable binder that is not
351 -- already wild and is representable (to prevent loops with
353 if (not wild) && repr
355 -- Create on new binder that will actually capture a value in this
356 -- case statement, and return it.
357 let bty = (Id.idType b)
358 id <- Trans.lift $ mkInternalVar "sel" bty
359 let binders = take i wildbndrs ++ [id] ++ drop (i+1) wildbndrs
360 let caseexpr = Case scrut b bty [(con, binders, Var id)]
361 return (wildbndrs!!i, Just (b, caseexpr))
363 -- Just leave the original binder in place, and don't generate an
364 -- extra selector case.
366 -- Process the expression of a case alternative. Accepts an expression
367 -- and whether this expression uses any of the binders in the
368 -- alternative. Returns an optional new binding and a new expression.
369 doexpr :: CoreExpr -> Bool -> TransformMonad (Maybe (CoreBndr, CoreExpr), CoreExpr)
370 doexpr expr uses_bndrs = do
371 local_var <- Trans.lift $ is_local_var expr
373 -- Extract any expressions that do not use any binders from this
374 -- alternative, is not a local var already and is representable (to
375 -- prevent loops with inlinenonrep).
376 if (not uses_bndrs) && (not local_var) && repr
378 id <- Trans.lift $ mkBinderFor expr "caseval"
379 -- We don't flag a change here, since casevalsimpl will do that above
380 -- based on Just we return here.
381 return $ (Just (id, expr), Var id)
383 -- Don't simplify anything else
384 return (Nothing, expr)
385 -- Leave all other expressions unchanged
386 casesimpl expr = return expr
387 -- Perform this transform everywhere
388 casesimpltop = everywhere ("casesimpl", casesimpl)
390 --------------------------------
392 --------------------------------
393 -- Remove case statements that have only a single alternative and only wild
395 caseremove, caseremovetop :: Transform
396 -- Replace a useless case by the value of its single alternative
397 caseremove (Case scrut b ty [(con, bndrs, expr)]) | not usesvars = change expr
398 -- Find if any of the binders are used by expr
399 where usesvars = (not . VarSet.isEmptyVarSet . (CoreFVs.exprSomeFreeVars (`elem` bndrs))) expr
400 -- Leave all other expressions unchanged
401 caseremove expr = return expr
402 -- Perform this transform everywhere
403 caseremovetop = everywhere ("caseremove", caseremove)
405 --------------------------------
406 -- Argument extraction
407 --------------------------------
408 -- Make sure that all arguments of a representable type are simple variables.
409 appsimpl, appsimpltop :: Transform
410 -- Simplify all representable arguments. Do this by introducing a new Let
411 -- that binds the argument and passing the new binder in the application.
412 appsimpl expr@(App f arg) = do
413 -- Check runtime representability
415 local_var <- Trans.lift $ is_local_var arg
416 if repr && not local_var
417 then do -- Extract representable arguments
418 id <- Trans.lift $ mkBinderFor arg "arg"
419 change $ Let (NonRec id arg) (App f (Var id))
420 else -- Leave non-representable arguments unchanged
422 -- Leave all other expressions unchanged
423 appsimpl expr = return expr
424 -- Perform this transform everywhere
425 appsimpltop = everywhere ("appsimpl", appsimpl)
427 --------------------------------
428 -- Function-typed argument propagation
429 --------------------------------
430 -- Remove all applications to function-typed arguments, by duplication the
431 -- function called with the function-typed parameter replaced by the free
432 -- variables of the argument passed in.
433 argprop, argproptop :: Transform
434 -- Transform any application of a named function (i.e., skip applications of
435 -- lambda's). Also skip applications that have arguments with free type
436 -- variables, since we can't inline those.
437 argprop expr@(App _ _) | is_var fexpr = do
438 -- Find the body of the function called
439 body_maybe <- Trans.lift $ getGlobalBind f
442 -- Process each of the arguments in turn
443 (args', changed) <- Writer.listen $ mapM doarg args
444 -- See if any of the arguments changed
445 case Monoid.getAny changed of
447 let (newargs', newparams', oldargs) = unzip3 args'
448 let newargs = concat newargs'
449 let newparams = concat newparams'
450 -- Create a new body that consists of a lambda for all new arguments and
451 -- the old body applied to some arguments.
452 let newbody = MkCore.mkCoreLams newparams (MkCore.mkCoreApps body oldargs)
453 -- Create a new function with the same name but a new body
454 newf <- Trans.lift $ mkFunction f newbody
455 -- Replace the original application with one of the new function to the
457 change $ MkCore.mkCoreApps (Var newf) newargs
459 -- Don't change the expression if none of the arguments changed
462 -- If we don't have a body for the function called, leave it unchanged (it
463 -- should be a primitive function then).
464 Nothing -> return expr
466 -- Find the function called and the arguments
467 (fexpr, args) = collectArgs expr
470 -- Process a single argument and return (args, bndrs, arg), where args are
471 -- the arguments to replace the given argument in the original
472 -- application, bndrs are the binders to include in the top-level lambda
473 -- in the new function body, and arg is the argument to apply to the old
475 doarg :: CoreExpr -> TransformMonad ([CoreExpr], [CoreBndr], CoreExpr)
478 bndrs <- Trans.lift getGlobalBinders
479 let interesting var = Var.isLocalVar var && (not $ var `elem` bndrs)
480 if not repr && not (is_var arg && interesting (exprToVar arg)) && not (has_free_tyvars arg)
482 -- Propagate all complex arguments that are not representable, but not
483 -- arguments with free type variables (since those would require types
484 -- not known yet, which will always be known eventually).
485 -- Find interesting free variables, each of which should be passed to
486 -- the new function instead of the original function argument.
488 -- Interesting vars are those that are local, but not available from the
489 -- top level scope (functions from this module are defined as local, but
490 -- they're not local to this function, so we can freely move references
491 -- to them into another function).
492 let free_vars = VarSet.varSetElems $ CoreFVs.exprSomeFreeVars interesting arg
493 -- Mark the current expression as changed
495 return (map Var free_vars, free_vars, arg)
497 -- Representable types will not be propagated, and arguments with free
498 -- type variables will be propagated later.
499 -- TODO: preserve original naming?
500 id <- Trans.lift $ mkBinderFor arg "param"
501 -- Just pass the original argument to the new function, which binds it
502 -- to a new id and just pass that new id to the old function body.
503 return ([arg], [id], mkReferenceTo id)
504 -- Leave all other expressions unchanged
505 argprop expr = return expr
506 -- Perform this transform everywhere
507 argproptop = everywhere ("argprop", argprop)
509 --------------------------------
510 -- Function-typed argument extraction
511 --------------------------------
512 -- This transform takes any function-typed argument that cannot be propagated
513 -- (because the function that is applied to it is a builtin function), and
514 -- puts it in a brand new top level binder. This allows us to for example
515 -- apply map to a lambda expression This will not conflict with inlinenonrep,
516 -- since that only inlines local let bindings, not top level bindings.
517 funextract, funextracttop :: Transform
518 funextract expr@(App _ _) | is_var fexpr = do
519 body_maybe <- Trans.lift $ getGlobalBind f
521 -- We don't have a function body for f, so we can perform this transform.
523 -- Find the new arguments
524 args' <- mapM doarg args
525 -- And update the arguments. We use return instead of changed, so the
526 -- changed flag doesn't get set if none of the args got changed.
527 return $ MkCore.mkCoreApps fexpr args'
528 -- We have a function body for f, leave this application to funprop
529 Just _ -> return expr
531 -- Find the function called and the arguments
532 (fexpr, args) = collectArgs expr
534 -- Change any arguments that have a function type, but are not simple yet
535 -- (ie, a variable or application). This means to create a new function
536 -- for map (\f -> ...) b, but not for map (foo a) b.
538 -- We could use is_applicable here instead of is_fun, but I think
539 -- arguments to functions could only have forall typing when existential
540 -- typing is enabled. Not sure, though.
541 doarg arg | not (is_simple arg) && is_fun arg = do
542 -- Create a new top level binding that binds the argument. Its body will
543 -- be extended with lambda expressions, to take any free variables used
544 -- by the argument expression.
545 let free_vars = VarSet.varSetElems $ CoreFVs.exprFreeVars arg
546 let body = MkCore.mkCoreLams free_vars arg
547 id <- Trans.lift $ mkBinderFor body "fun"
548 Trans.lift $ addGlobalBind id body
549 -- Replace the argument with a reference to the new function, applied to
551 change $ MkCore.mkCoreApps (Var id) (map Var free_vars)
552 -- Leave all other arguments untouched
553 doarg arg = return arg
555 -- Leave all other expressions unchanged
556 funextract expr = return expr
557 -- Perform this transform everywhere
558 funextracttop = everywhere ("funextract", funextract)
560 --------------------------------
561 -- End of transformations
562 --------------------------------
567 -- What transforms to run?
568 transforms = [argproptop, funextracttop, etatop, betatop, castproptop, letremovesimpletop, letderectop, letremovetop, letsimpltop, letflattop, scrutsimpltop, casesimpltop, caseremovetop, inlinenonreptop, appsimpltop, letmergetop, letremoveunusedtop, castsimpltop]
570 -- | Returns the normalized version of the given function.
572 CoreBndr -- ^ The function to get
573 -> TranslatorSession CoreExpr -- The normalized function body
575 getNormalized bndr = Utils.makeCached bndr tsNormalized $ do
576 if is_poly (Var bndr)
578 -- This should really only happen at the top level... TODO: Give
579 -- a different error if this happens down in the recursion.
580 error $ "\nNormalize.normalizeBind: Function " ++ show bndr ++ " is polymorphic, can't normalize"
582 expr <- getBinding bndr
583 normalizeExpr (show bndr) expr
585 -- | Normalize an expression
587 String -- ^ What are we normalizing? For debug output only.
588 -> CoreSyn.CoreExpr -- ^ The expression to normalize
589 -> TranslatorSession CoreSyn.CoreExpr -- ^ The normalized expression
591 normalizeExpr what expr = do
592 -- Normalize this expression
593 trace (what ++ " before normalization:\n\n" ++ showSDoc ( ppr expr ) ++ "\n") $ return ()
594 expr' <- dotransforms transforms expr
595 trace ("\n" ++ what ++ " after normalization:\n\n" ++ showSDoc ( ppr expr')) $ return ()
598 -- | Get the value that is bound to the given binder at top level. Fails when
599 -- there is no such binding.
601 CoreBndr -- ^ The binder to get the expression for
602 -> TranslatorSession CoreExpr -- ^ The value bound to the binder
604 getBinding bndr = Utils.makeCached bndr tsBindings $ do
605 -- If the binding isn't in the "cache" (bindings map), then we can't create
606 -- it out of thin air, so return an error.
607 error $ "Normalize.getBinding: Unknown function requested: " ++ show bndr
609 -- | Split a normalized expression into the argument binders, top level
610 -- bindings and the result binder.
612 CoreExpr -- ^ The normalized expression
613 -> ([CoreBndr], [Binding], CoreBndr)
614 splitNormalized expr = (args, binds, res)
616 (args, letexpr) = CoreSyn.collectBinders expr
617 (binds, resexpr) = flattenLets letexpr
618 res = case resexpr of
620 _ -> error $ "Normalize.splitNormalized: Not in normal form: " ++ pprString expr ++ "\n"