1 {-# LANGUAGE PackageImports #-}
3 -- This module provides functions for program transformations.
5 module CLasH.Normalize.NormalizeTools where
8 import qualified Data.Monoid as Monoid
9 import qualified Control.Monad as Monad
10 import qualified Control.Monad.Trans.Writer as Writer
11 import qualified "transformers" Control.Monad.Trans as Trans
12 import qualified Data.Accessor.Monad.Trans.State as MonadState
19 import qualified CoreSubst
21 -- import qualified CoreUtils
22 -- import Outputable ( showSDoc, ppr, nest )
25 import CLasH.Normalize.NormalizeTypes
26 import CLasH.Translator.TranslatorTypes
27 import CLasH.VHDL.Constants (builtinIds)
29 import qualified CLasH.Utils.Core.CoreTools as CoreTools
30 import qualified CLasH.VHDL.VHDLTools as VHDLTools
32 -- Apply the given transformation to all expressions in the given expression,
33 -- including the expression itself.
34 everywhere :: (String, Transform) -> Transform
35 everywhere trans = applyboth (subeverywhere (everywhere trans)) trans
37 -- Apply the first transformation, followed by the second transformation, and
38 -- keep applying both for as long as expression still changes.
39 applyboth :: Transform -> (String, Transform) -> Transform
40 applyboth first (name, second) context expr = do
42 expr' <- first context expr
44 (expr'', changed) <- Writer.listen $ second context expr'
46 -- trace ("Trying to apply transform " ++ name ++ " to:\n" ++ showSDoc (nest 4 $ ppr expr') ++ "\nType: \n" ++ (showSDoc $ nest 4 $ ppr $ CoreUtils.exprType expr') ++ "\n")
49 -- trace ("Applying transform " ++ name ++ " to:\n" ++ showSDoc (nest 4 $ ppr expr') ++ "\nType: \n" ++ (showSDoc $ nest 4 $ ppr $ CoreUtils.exprType expr') ++ "\n"
50 -- ++ "Context: " ++ show context ++ "\n"
51 -- ++ "Result of applying " ++ name ++ ":\n" ++ showSDoc (nest 4 $ ppr expr'') ++ "\n" ++ "Type: \n" ++ (showSDoc $ nest 4 $ ppr $ CoreUtils.exprType expr'') ++ "\n" ) $
52 applyboth first (name, second) context expr''
54 -- trace ("No changes") $
57 -- Apply the given transformation to all direct subexpressions (only), not the
59 subeverywhere :: Transform -> Transform
60 subeverywhere trans c (App a b) = do
61 a' <- trans (AppFirst:c) a
62 b' <- trans (AppSecond:c) b
65 subeverywhere trans c (Let (NonRec b bexpr) expr) = do
66 bexpr' <- trans (LetBinding:c) bexpr
67 expr' <- trans (LetBody:c) expr
68 return $ Let (NonRec b bexpr') expr'
70 subeverywhere trans c (Let (Rec binds) expr) = do
71 expr' <- trans (LetBody:c) expr
72 binds' <- mapM transbind binds
73 return $ Let (Rec binds') expr'
75 transbind :: (CoreBndr, CoreExpr) -> TransformMonad (CoreBndr, CoreExpr)
77 e' <- trans (LetBinding:c) e
80 subeverywhere trans c (Lam x expr) = do
81 expr' <- trans (LambdaBody:c) expr
84 subeverywhere trans c (Case scrut b t alts) = do
85 scrut' <- trans (Other:c) scrut
86 alts' <- mapM transalt alts
87 return $ Case scrut' b t alts'
89 transalt :: CoreAlt -> TransformMonad CoreAlt
90 transalt (con, binders, expr) = do
91 expr' <- trans (Other:c) expr
92 return (con, binders, expr')
94 subeverywhere trans c (Var x) = return $ Var x
95 subeverywhere trans c (Lit x) = return $ Lit x
96 subeverywhere trans c (Type x) = return $ Type x
98 subeverywhere trans c (Cast expr ty) = do
99 expr' <- trans (Other:c) expr
100 return $ Cast expr' ty
102 subeverywhere trans c expr = error $ "\nNormalizeTools.subeverywhere: Unsupported expression: " ++ show expr
104 -- Runs each of the transforms repeatedly inside the State monad.
105 dotransforms :: [Transform] -> CoreExpr -> TranslatorSession CoreExpr
106 dotransforms transs expr = do
107 (expr', changed) <- Writer.runWriterT $ Monad.foldM (\e trans -> trans [] e) expr transs
108 if Monoid.getAny changed then dotransforms transs expr' else return expr'
110 -- Inline all let bindings that satisfy the given condition
111 inlinebind :: ((CoreBndr, CoreExpr) -> TransformMonad Bool) -> Transform
112 inlinebind condition context expr@(Let (NonRec bndr expr') res) = do
113 applies <- condition (bndr, expr')
116 -- Substitute the binding in res and return that
117 res' <- substitute_clone bndr expr' context res
120 -- Don't change this let
122 -- Leave all other expressions unchanged
123 inlinebind _ context expr = return expr
125 -- Sets the changed flag in the TransformMonad, to signify that some
126 -- transform has changed the result
127 setChanged :: TransformMonad ()
128 setChanged = Writer.tell (Monoid.Any True)
130 -- Sets the changed flag and returns the given value.
131 change :: a -> TransformMonad a
136 -- Returns the given value and sets the changed flag if the bool given is
137 -- True. Note that this will not unset the changed flag if the bool is False.
138 changeif :: Bool -> a -> TransformMonad a
139 changeif True val = change val
140 changeif False val = return val
142 -- | Creates a transformation that substitutes the given binder with the given
143 -- expression (This can be a type variable, replace by a Type expression).
144 -- Does not set the changed flag.
145 substitute :: CoreBndr -> CoreExpr -> Transform
146 -- Use CoreSubst to subst a type var in an expression
147 substitute find repl context expr = do
148 let subst = CoreSubst.extendSubst CoreSubst.emptySubst find repl
149 return $ CoreSubst.substExpr subst expr
151 -- | Creates a transformation that substitutes the given binder with the given
152 -- expression. This does only work for value expressions! All binders in the
153 -- expression are cloned before the replacement, to guarantee uniqueness.
154 substitute_clone :: CoreBndr -> CoreExpr -> Transform
155 -- If we see the var to find, replace it by a uniqued version of repl
156 substitute_clone find repl context (Var var) | find == var = do
157 repl' <- Trans.lift $ CoreTools.genUniques repl
160 -- For all other expressions, just look in subexpressions
161 substitute_clone find repl context expr = subeverywhere (substitute_clone find repl) context expr
163 -- Is the given expression representable at runtime, based on the type?
164 isRepr :: (CoreTools.TypedThing t) => t -> TransformMonad Bool
165 isRepr tything = Trans.lift (isRepr' tything)
167 isRepr' :: (CoreTools.TypedThing t) => t -> TranslatorSession Bool
168 isRepr' tything = case CoreTools.getType tything of
169 Nothing -> return False
170 Just ty -> MonadState.lift tsType $ VHDLTools.isReprType ty
172 is_local_var :: CoreSyn.CoreExpr -> TranslatorSession Bool
173 is_local_var (CoreSyn.Var v) = do
174 bndrs <- getGlobalBinders
175 return $ v `notElem` bndrs
176 is_local_var _ = return False
178 -- Is the given binder defined by the user?
179 isUserDefined :: CoreSyn.CoreBndr -> Bool
180 -- System names are certain to not be user defined
181 isUserDefined bndr | Name.isSystemName (Id.idName bndr) = False
182 -- Builtin functions are usually not user-defined either (and would
183 -- break currently if they are...)
184 isUserDefined bndr = str `notElem` builtinIds
186 str = Name.getOccString bndr
188 -- Is the given binder normalizable? This means that its type signature can be
189 -- represented in hardware, which should (?) guarantee that it can be made
190 -- into hardware. Note that if a binder is not normalizable, it might become
191 -- so using argument propagation.
192 isNormalizeable :: CoreBndr -> TransformMonad Bool
193 isNormalizeable bndr = Trans.lift (isNormalizeable' bndr)
195 isNormalizeable' :: CoreBndr -> TranslatorSession Bool
196 isNormalizeable' bndr = do
197 let ty = Id.idType bndr
198 let (arg_tys, res_ty) = Type.splitFunTys ty
199 -- This function is normalizable if all its arguments and return value are
201 andM $ mapM isRepr' (res_ty:arg_tys)