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 Normalize (normalizeModule) where
11 import qualified Maybe
12 import qualified "transformers" Control.Monad.Trans as Trans
13 import qualified Control.Monad as Monad
14 import qualified Control.Monad.Trans.Writer as Writer
15 import qualified Data.Map as Map
16 import qualified Data.Monoid as Monoid
21 import qualified UniqSupply
22 import qualified CoreUtils
24 import qualified TcType
27 import qualified VarSet
28 import qualified NameSet
29 import qualified CoreFVs
30 import qualified CoreUtils
31 import qualified MkCore
32 import qualified HscTypes
33 import Outputable ( showSDoc, ppr, nest )
42 --------------------------------
43 -- Start of transformations
44 --------------------------------
46 --------------------------------
48 --------------------------------
49 eta, etatop :: Transform
50 eta expr | is_fun expr && not (is_lam expr) = do
51 let arg_ty = (fst . Type.splitFunTy . CoreUtils.exprType) expr
52 id <- mkInternalVar "param" arg_ty
53 change (Lam id (App expr (Var id)))
54 -- Leave all other expressions unchanged
56 etatop = notappargs ("eta", eta)
58 --------------------------------
60 --------------------------------
61 beta, betatop :: Transform
62 -- Substitute arg for x in expr
63 beta (App (Lam x expr) arg) = change $ substitute [(x, arg)] expr
64 -- Propagate the application into the let
65 beta (App (Let binds expr) arg) = change $ Let binds (App expr arg)
66 -- Propagate the application into each of the alternatives
67 beta (App (Case scrut b ty alts) arg) = change $ Case scrut b ty' alts'
69 alts' = map (\(con, bndrs, expr) -> (con, bndrs, (App expr arg))) alts
70 ty' = CoreUtils.applyTypeToArg ty arg
71 -- Leave all other expressions unchanged
72 beta expr = return expr
73 -- Perform this transform everywhere
74 betatop = everywhere ("beta", beta)
76 --------------------------------
78 --------------------------------
79 -- Try to move casts as much downward as possible.
80 castprop, castproptop :: Transform
81 castprop (Cast (Let binds expr) ty) = change $ Let binds (Cast expr ty)
82 castprop expr@(Cast (Case scrut b _ alts) ty) = change (Case scrut b ty alts')
84 alts' = map (\(con, bndrs, expr) -> (con, bndrs, (Cast expr ty))) alts
85 -- Leave all other expressions unchanged
86 castprop expr = return expr
87 -- Perform this transform everywhere
88 castproptop = everywhere ("castprop", castprop)
90 --------------------------------
91 -- let recursification
92 --------------------------------
93 letrec, letrectop :: Transform
94 letrec (Let (NonRec b expr) res) = change $ Let (Rec [(b, expr)]) res
95 -- Leave all other expressions unchanged
96 letrec expr = return expr
97 -- Perform this transform everywhere
98 letrectop = everywhere ("letrec", letrec)
100 --------------------------------
101 -- let simplification
102 --------------------------------
103 letsimpl, letsimpltop :: Transform
104 -- Don't simplifiy lets that are already simple
105 letsimpl expr@(Let _ (Var _)) = return expr
106 -- Put the "in ..." value of a let in its own binding, but not when the
107 -- expression is applicable (to prevent loops with inlinefun).
108 letsimpl (Let (Rec binds) expr) | not $ is_applicable expr = do
109 id <- mkInternalVar "foo" (CoreUtils.exprType expr)
110 let bind = (id, expr)
111 change $ Let (Rec (bind:binds)) (Var id)
112 -- Leave all other expressions unchanged
113 letsimpl expr = return expr
114 -- Perform this transform everywhere
115 letsimpltop = everywhere ("letsimpl", letsimpl)
117 --------------------------------
119 --------------------------------
120 letflat, letflattop :: Transform
121 letflat (Let (Rec binds) expr) = do
122 -- Turn each binding into a list of bindings (possibly containing just one
123 -- element, of course)
124 bindss <- Monad.mapM flatbind binds
125 -- Concat all the bindings
126 let binds' = concat bindss
127 -- Return the new let. We don't use change here, since possibly nothing has
128 -- changed. If anything has changed, flatbind has already flagged that
130 return $ Let (Rec binds') expr
132 -- Turns a binding of a let into a multiple bindings, or any other binding
133 -- into a list with just that binding
134 flatbind :: (CoreBndr, CoreExpr) -> TransformMonad [(CoreBndr, CoreExpr)]
135 flatbind (b, Let (Rec binds) expr) = change ((b, expr):binds)
136 flatbind (b, expr) = return [(b, expr)]
137 -- Leave all other expressions unchanged
138 letflat expr = return expr
139 -- Perform this transform everywhere
140 letflattop = everywhere ("letflat", letflat)
142 --------------------------------
143 -- Simple let binding removal
144 --------------------------------
145 -- Remove a = b bindings from let expressions everywhere
146 letremovetop :: Transform
147 letremovetop = everywhere ("letremove", inlinebind (\(b, e) -> case e of (Var v) | not $ Id.isDataConWorkId v -> return True; otherwise -> return False))
149 --------------------------------
151 --------------------------------
152 -- Remove a = B bindings, with B :: a -> b, or B :: forall x . T, from let
153 -- expressions everywhere. This means that any value that still needs to be
154 -- applied to something else (polymorphic values need to be applied to a
155 -- Type) will be inlined, and will eventually be applied to all their
158 -- This is a tricky function, which is prone to create loops in the
159 -- transformations. To fix this, we make sure that no transformation will
160 -- create a new let binding with a function type. These other transformations
161 -- will just not work on those function-typed values at first, but the other
162 -- transformations (in particular β-reduction) should make sure that the type
163 -- of those values eventually becomes primitive.
164 inlinenonreptop :: Transform
165 inlinenonreptop = everywhere ("inlinenonrep", inlinebind ((Monad.liftM not) . isRepr . snd))
167 --------------------------------
168 -- Scrutinee simplification
169 --------------------------------
170 scrutsimpl,scrutsimpltop :: Transform
171 -- Don't touch scrutinees that are already simple
172 scrutsimpl expr@(Case (Var _) _ _ _) = return expr
173 -- Replace all other cases with a let that binds the scrutinee and a new
174 -- simple scrutinee, but not when the scrutinee is applicable (to prevent
175 -- loops with inlinefun, though I don't think a scrutinee can be
177 scrutsimpl (Case scrut b ty alts) | not $ is_applicable scrut = do
178 id <- mkInternalVar "scrut" (CoreUtils.exprType scrut)
179 change $ Let (Rec [(id, scrut)]) (Case (Var id) b ty alts)
180 -- Leave all other expressions unchanged
181 scrutsimpl expr = return expr
182 -- Perform this transform everywhere
183 scrutsimpltop = everywhere ("scrutsimpl", scrutsimpl)
185 --------------------------------
186 -- Case binder wildening
187 --------------------------------
188 casewild, casewildtop :: Transform
189 casewild expr@(Case scrut b ty alts) = do
190 (bindingss, alts') <- (Monad.liftM unzip) $ mapM doalt alts
191 let bindings = concat bindingss
192 -- Replace the case with a let with bindings and a case
193 let newlet = (Let (Rec bindings) (Case scrut b ty alts'))
194 -- If there are no non-wild binders, or this case is already a simple
195 -- selector (i.e., a single alt with exactly one binding), already a simple
196 -- selector altan no bindings (i.e., no wild binders in the original case),
197 -- don't change anything, otherwise, replace the case.
198 if null bindings || length alts == 1 && length bindings == 1 then return expr else change newlet
200 -- Generate a single wild binder, since they are all the same
202 -- Wilden the binders of one alt, producing a list of bindings as a
204 doalt :: CoreAlt -> TransformMonad ([(CoreBndr, CoreExpr)], CoreAlt)
205 doalt (con, bndrs, expr) = do
206 bindings_maybe <- Monad.zipWithM mkextracts bndrs [0..]
207 let bindings = Maybe.catMaybes bindings_maybe
208 -- We replace the binders with wild binders only. We can leave expr
209 -- unchanged, since the new bindings bind the same vars as the original
211 let newalt = (con, wildbndrs, expr)
212 return (bindings, newalt)
214 -- Make all binders wild
215 wildbndrs = map (\bndr -> Id.mkWildId (Id.idType bndr)) bndrs
216 -- Creates a case statement to retrieve the ith element from the scrutinee
217 -- and binds that to b.
218 mkextracts :: CoreBndr -> Int -> TransformMonad (Maybe (CoreBndr, CoreExpr))
220 -- TODO: Use free variables instead of is_wild. is_wild is a hack.
221 if is_wild b || Type.isFunTy (Id.idType b)
222 -- Don't create extra bindings for binders that are already wild, or
223 -- for binders that bind function types (to prevent loops with
227 -- Create on new binder that will actually capture a value in this
228 -- case statement, and return it
229 let bty = (Id.idType b)
230 id <- mkInternalVar "sel" bty
231 let binders = take i wildbndrs ++ [id] ++ drop (i+1) wildbndrs
232 return $ Just (b, Case scrut b bty [(con, binders, Var id)])
233 -- Leave all other expressions unchanged
234 casewild expr = return expr
235 -- Perform this transform everywhere
236 casewildtop = everywhere ("casewild", casewild)
238 --------------------------------
239 -- Case value simplification
240 --------------------------------
241 casevalsimpl, casevalsimpltop :: Transform
242 casevalsimpl expr@(Case scrut b ty alts) = do
243 -- Try to simplify each alternative, resulting in an optional binding and a
245 (bindings_maybe, alts') <- (Monad.liftM unzip) $ mapM doalt alts
246 let bindings = Maybe.catMaybes bindings_maybe
247 -- Create a new let around the case, that binds of the cases values.
248 let newlet = Let (Rec bindings) (Case scrut b ty alts')
249 -- If there were no values that needed and allowed simplification, don't
251 if null bindings then return expr else change newlet
253 doalt :: CoreAlt -> TransformMonad (Maybe (CoreBndr, CoreExpr), CoreAlt)
254 -- Don't simplify values that are already simple
255 doalt alt@(con, bndrs, Var _) = return (Nothing, alt)
256 -- Simplify each alt by creating a new id, binding the case value to it and
257 -- replacing the case value with that id. Only do this when the case value
258 -- does not use any of the binders bound by this alternative, for that would
259 -- cause those binders to become unbound when moving the value outside of
260 -- the case statement. Also, don't create a binding for applicable
261 -- expressions, to prevent loops with inlinefun.
262 doalt (con, bndrs, expr) | (not usesvars) && (not $ is_applicable expr) = do
263 id <- mkInternalVar "caseval" (CoreUtils.exprType expr)
264 -- We don't flag a change here, since casevalsimpl will do that above
265 -- based on Just we return here.
266 return $ (Just (id, expr), (con, bndrs, Var id))
267 -- Find if any of the binders are used by expr
268 where usesvars = (not . VarSet.isEmptyVarSet . (CoreFVs.exprSomeFreeVars (`elem` bndrs))) expr
269 -- Don't simplify anything else
270 doalt alt = return (Nothing, alt)
271 -- Leave all other expressions unchanged
272 casevalsimpl expr = return expr
273 -- Perform this transform everywhere
274 casevalsimpltop = everywhere ("casevalsimpl", casevalsimpl)
276 --------------------------------
278 --------------------------------
279 -- Remove case statements that have only a single alternative and only wild
281 caseremove, caseremovetop :: Transform
282 -- Replace a useless case by the value of its single alternative
283 caseremove (Case scrut b ty [(con, bndrs, expr)]) | not usesvars = change expr
284 -- Find if any of the binders are used by expr
285 where usesvars = (not . VarSet.isEmptyVarSet . (CoreFVs.exprSomeFreeVars (`elem` bndrs))) expr
286 -- Leave all other expressions unchanged
287 caseremove expr = return expr
288 -- Perform this transform everywhere
289 caseremovetop = everywhere ("caseremove", caseremove)
291 --------------------------------
292 -- Argument extraction
293 --------------------------------
294 -- Make sure that all arguments of a representable type are simple variables.
295 appsimpl, appsimpltop :: Transform
296 -- Don't simplify arguments that are already simple.
297 appsimpl expr@(App f (Var v)) = return expr
298 -- Simplify all representable arguments. Do this by introducing a new Let
299 -- that binds the argument and passing the new binder in the application.
300 appsimpl expr@(App f arg) = do
301 -- Check runtime representability
304 then do -- Extract representable arguments
305 id <- mkInternalVar "arg" (CoreUtils.exprType arg)
306 change $ Let (Rec [(id, arg)]) (App f (Var id))
307 else -- Leave non-representable arguments unchanged
309 -- Leave all other expressions unchanged
310 appsimpl expr = return expr
311 -- Perform this transform everywhere
312 appsimpltop = everywhere ("appsimpl", appsimpl)
314 --------------------------------
315 -- Function-typed argument propagation
316 --------------------------------
317 -- Remove all applications to function-typed arguments, by duplication the
318 -- function called with the function-typed parameter replaced by the free
319 -- variables of the argument passed in.
320 argprop, argproptop :: Transform
321 -- Transform any application of a named function (i.e., skip applications of
322 -- lambda's). Also skip applications that have arguments with free type
323 -- variables, since we can't inline those.
324 argprop expr@(App _ _) | is_var fexpr = do
325 -- Find the body of the function called
326 body_maybe <- Trans.lift $ getGlobalBind f
329 -- Process each of the arguments in turn
330 (args', changed) <- Writer.listen $ mapM doarg args
331 -- See if any of the arguments changed
332 case Monoid.getAny changed of
334 let (newargs', newparams', oldargs) = unzip3 args'
335 let newargs = concat newargs'
336 let newparams = concat newparams'
337 -- Create a new body that consists of a lambda for all new arguments and
338 -- the old body applied to some arguments.
339 let newbody = MkCore.mkCoreLams newparams (MkCore.mkCoreApps body oldargs)
340 -- Create a new function with the same name but a new body
341 newf <- mkFunction f newbody
342 -- Replace the original application with one of the new function to the
344 change $ MkCore.mkCoreApps (Var newf) newargs
346 -- Don't change the expression if none of the arguments changed
349 -- If we don't have a body for the function called, leave it unchanged (it
350 -- should be a primitive function then).
351 Nothing -> return expr
353 -- Find the function called and the arguments
354 (fexpr, args) = collectArgs expr
357 -- Process a single argument and return (args, bndrs, arg), where args are
358 -- the arguments to replace the given argument in the original
359 -- application, bndrs are the binders to include in the top-level lambda
360 -- in the new function body, and arg is the argument to apply to the old
362 doarg :: CoreExpr -> TransformMonad ([CoreExpr], [CoreBndr], CoreExpr)
365 bndrs <- Trans.lift getGlobalBinders
366 let interesting var = Var.isLocalVar var && (not $ var `elem` bndrs)
367 if not repr && not (is_var arg && interesting (exprToVar arg)) && not (has_free_tyvars arg)
369 -- Propagate all complex arguments that are not representable, but not
370 -- arguments with free type variables (since those would require types
371 -- not known yet, which will always be known eventually).
372 -- Find interesting free variables, each of which should be passed to
373 -- the new function instead of the original function argument.
375 -- Interesting vars are those that are local, but not available from the
376 -- top level scope (functions from this module are defined as local, but
377 -- they're not local to this function, so we can freely move references
378 -- to them into another function).
379 let free_vars = VarSet.varSetElems $ CoreFVs.exprSomeFreeVars interesting arg
380 -- Mark the current expression as changed
382 return (map Var free_vars, free_vars, arg)
384 -- Representable types will not be propagated, and arguments with free
385 -- type variables will be propagated later.
386 -- TODO: preserve original naming?
387 id <- mkBinderFor arg "param"
388 -- Just pass the original argument to the new function, which binds it
389 -- to a new id and just pass that new id to the old function body.
390 return ([arg], [id], mkReferenceTo id)
391 -- Leave all other expressions unchanged
392 argprop expr = return expr
393 -- Perform this transform everywhere
394 argproptop = everywhere ("argprop", argprop)
396 --------------------------------
397 -- Function-typed argument extraction
398 --------------------------------
399 -- This transform takes any function-typed argument that cannot be propagated
400 -- (because the function that is applied to it is a builtin function), and
401 -- puts it in a brand new top level binder. This allows us to for example
402 -- apply map to a lambda expression This will not conflict with inlinefun,
403 -- since that only inlines local let bindings, not top level bindings.
404 funextract, funextracttop :: Transform
405 funextract expr@(App _ _) | is_var fexpr = do
406 body_maybe <- Trans.lift $ getGlobalBind f
408 -- We don't have a function body for f, so we can perform this transform.
410 -- Find the new arguments
411 args' <- mapM doarg args
412 -- And update the arguments. We use return instead of changed, so the
413 -- changed flag doesn't get set if none of the args got changed.
414 return $ MkCore.mkCoreApps fexpr args'
415 -- We have a function body for f, leave this application to funprop
416 Just _ -> return expr
418 -- Find the function called and the arguments
419 (fexpr, args) = collectArgs expr
421 -- Change any arguments that have a function type, but are not simple yet
422 -- (ie, a variable or application). This means to create a new function
423 -- for map (\f -> ...) b, but not for map (foo a) b.
425 -- We could use is_applicable here instead of is_fun, but I think
426 -- arguments to functions could only have forall typing when existential
427 -- typing is enabled. Not sure, though.
428 doarg arg | not (is_simple arg) && is_fun arg = do
429 -- Create a new top level binding that binds the argument. Its body will
430 -- be extended with lambda expressions, to take any free variables used
431 -- by the argument expression.
432 let free_vars = VarSet.varSetElems $ CoreFVs.exprFreeVars arg
433 let body = MkCore.mkCoreLams free_vars arg
434 id <- mkBinderFor body "fun"
435 Trans.lift $ addGlobalBind id body
436 -- Replace the argument with a reference to the new function, applied to
438 change $ MkCore.mkCoreApps (Var id) (map Var free_vars)
439 -- Leave all other arguments untouched
440 doarg arg = return arg
442 -- Leave all other expressions unchanged
443 funextract expr = return expr
444 -- Perform this transform everywhere
445 funextracttop = everywhere ("funextract", funextract)
447 --------------------------------
448 -- End of transformations
449 --------------------------------
454 -- What transforms to run?
455 transforms = [argproptop, funextracttop, etatop, betatop, castproptop, letremovetop, letrectop, letsimpltop, letflattop, casewildtop, scrutsimpltop, casevalsimpltop, caseremovetop, inlinenonreptop, appsimpltop]
457 -- Turns the given bind into VHDL
460 -> UniqSupply.UniqSupply -- ^ A UniqSupply we can use
461 -> [(CoreBndr, CoreExpr)] -- ^ All bindings we know (i.e., in the current module)
462 -> [CoreBndr] -- ^ The bindings to generate VHDL for (i.e., the top level bindings)
463 -> [Bool] -- ^ For each of the bindings to generate VHDL for, if it is stateful
464 -> ([(CoreBndr, CoreExpr)], TypeState) -- ^ The resulting VHDL
466 normalizeModule env uniqsupply bindings generate_for statefuls = runTransformSession env uniqsupply $ do
467 -- Put all the bindings in this module in the tsBindings map
468 putA tsBindings (Map.fromList bindings)
469 -- (Recursively) normalize each of the requested bindings
470 mapM normalizeBind generate_for
471 -- Get all initial bindings and the ones we produced
472 bindings_map <- getA tsBindings
473 let bindings = Map.assocs bindings_map
474 normalized_bindings <- getA tsNormalized
475 typestate <- getA tsType
476 -- But return only the normalized bindings
477 return $ (filter ((flip VarSet.elemVarSet normalized_bindings) . fst) bindings, typestate)
479 normalizeBind :: CoreBndr -> TransformSession ()
481 -- Don't normalize global variables, these should be either builtin
482 -- functions or data constructors.
483 Monad.when (Var.isLocalIdVar bndr) $ do
484 -- Skip binders that have a polymorphic type, since it's impossible to
485 -- create polymorphic hardware.
486 if is_poly (Var bndr)
488 -- This should really only happen at the top level... TODO: Give
489 -- a different error if this happens down in the recursion.
490 error $ "\nNormalize.normalizeBind: Function " ++ show bndr ++ " is polymorphic, can't normalize"
492 normalized_funcs <- getA tsNormalized
493 -- See if this function was normalized already
494 if VarSet.elemVarSet bndr normalized_funcs
496 -- Yup, don't do it again
499 -- Nope, note that it has been and do it.
500 modA tsNormalized (flip VarSet.extendVarSet bndr)
501 expr_maybe <- getGlobalBind bndr
504 -- Introduce an empty Let at the top level, so there will always be
505 -- a let in the expression (none of the transformations will remove
507 let expr' = Let (Rec []) expr
508 -- Normalize this expression
509 trace ("Transforming " ++ (show bndr) ++ "\nBefore:\n\n" ++ showSDoc ( ppr expr' ) ++ "\n") $ return ()
510 expr' <- dotransforms transforms expr'
511 trace ("\nAfter:\n\n" ++ showSDoc ( ppr expr')) $ return ()
512 -- And store the normalized version in the session
513 modA tsBindings (Map.insert bndr expr')
514 -- Find all vars used with a function type. All of these should be global
515 -- binders (i.e., functions used), since any local binders with a function
516 -- type should have been inlined already.
517 let used_funcs_set = CoreFVs.exprSomeFreeVars (\v -> (Type.isFunTy . snd . Type.splitForAllTys . Id.idType) v) expr'
518 let used_funcs = VarSet.varSetElems used_funcs_set
519 -- Process each of the used functions recursively
520 mapM normalizeBind used_funcs
521 -- FIXME: Can't we inline these 'implicit' function calls or something?
522 -- TODO: Add an extra let expression to the current finding, so the VHDL
523 -- Will make a signa assignment for this 'implicit' function call
525 -- Find all the other free variables used that are used. This applies to
526 -- variables that are actually a reference to a Class function. Example:
528 -- functiontest :: SizedInt D8 -> SizedInt D8
529 -- functiontest = \a -> let r = a + 1 in r
531 -- The literal(Lit) '1' will be turned into a variable (Var)
532 -- As it will call the 'fromInteger' class function that belongs
533 -- to the Num class. So we need to translate the refenced function
534 -- let used_vars_set = CoreFVs.exprSomeFreeVars (\v -> (Type.isAlgType . snd . Type.splitForAllTys . Id.idType) v) expr'
535 -- let used_vars = VarSet.varSetElems used_vars_set
536 -- -- Filter for dictionary args, they should not be translated
537 -- -- FIXME: check for other non-translatable stuff as well
538 -- let trans_vars = filter (\v -> (not . TcType.isDictTy . Id.idType) v) used_vars
539 -- mapM normalizeBind trans_vars
541 -- We don't have a value for this binder. This really shouldn't
542 -- happen for local id's...
543 Nothing -> error $ "\nNormalize.normalizeBind: No value found for binder " ++ pprString bndr ++ "? This should not happen!"