From: Matthijs Kooijman Date: Wed, 12 Aug 2009 13:08:02 +0000 (+0200) Subject: Add cast simplification normalization pass. X-Git-Url: https://git.stderr.nl/gitweb?p=matthijs%2Fmaster-project%2Fc%CE%BBash.git;a=commitdiff_plain;h=1b6abfb6c5970df03f9a2f3ae6d69e077032b30e Add cast simplification normalization pass. This ensures that the casts used for packing and unpacking State variables only operate on simple variables. --- diff --git "a/c\316\273ash/CLasH/Normalize.hs" "b/c\316\273ash/CLasH/Normalize.hs" index 90cbbc7..bd0ec97 100644 --- "a/c\316\273ash/CLasH/Normalize.hs" +++ "b/c\316\273ash/CLasH/Normalize.hs" @@ -90,6 +90,30 @@ castprop expr = return expr -- Perform this transform everywhere castproptop = everywhere ("castprop", castprop) +-------------------------------- +-- Cast simplification. Mostly useful for state packing and unpacking, but +-- perhaps for others as well. +-------------------------------- +castsimpl, castsimpltop :: Transform +castsimpl expr@(Cast val ty) = do + -- Don't extract values that are already simpl + local_var <- Trans.lift $ is_local_var val + -- Don't extract values that are not representable, to prevent loops with + -- inlinenonrep + repr <- isRepr val + if (not local_var) && repr + then do + -- Generate a binder for the expression + id <- Trans.lift $ mkBinderFor val "castval" + -- Extract the expression + change $ Let (Rec [(id, val)]) (Cast (Var id) ty) + else + return expr +-- Leave all other expressions unchanged +castsimpl expr = return expr +-- Perform this transform everywhere +castsimpltop = everywhere ("castsimpl", castsimpl) + -------------------------------- -- let recursification -------------------------------- @@ -491,7 +515,7 @@ funextracttop = everywhere ("funextract", funextract) -- What transforms to run? -transforms = [argproptop, funextracttop, etatop, betatop, castproptop, letremovetop, letrectop, letsimpltop, letflattop, scrutsimpltop, casesimpltop, caseremovetop, inlinenonreptop, appsimpltop, letremoveunusedtop] +transforms = [argproptop, funextracttop, etatop, betatop, castproptop, letremovetop, letrectop, letsimpltop, letflattop, scrutsimpltop, casesimpltop, caseremovetop, inlinenonreptop, appsimpltop, letremoveunusedtop, castsimpltop] -- | Returns the normalized version of the given function. getNormalized ::