X-Git-Url: https://git.stderr.nl/gitweb?a=blobdiff_plain;f=c%CE%BBash%2FCLasH%2FNormalize.hs;h=b2b4bd86f0080693d29f70183ef469083b15bdfc;hb=e17057696a5f74170dad6167867ee24d64d0854b;hp=8b35bb986bd4265df2ec58e6988fc21f04fe64de;hpb=9a431787ceb299b15106b0dfd07701913cf2b515;p=matthijs%2Fmaster-project%2Fc%CE%BBash.git diff --git "a/c\316\273ash/CLasH/Normalize.hs" "b/c\316\273ash/CLasH/Normalize.hs" index 8b35bb9..b2b4bd8 100644 --- "a/c\316\273ash/CLasH/Normalize.hs" +++ "b/c\316\273ash/CLasH/Normalize.hs" @@ -157,6 +157,24 @@ letflattop = everywhere ("letflat", letflat) letremovetop :: Transform letremovetop = everywhere ("letremove", inlinebind (\(b, e) -> Trans.lift $ is_local_var e)) +-------------------------------- +-- Unused let binding removal +-------------------------------- +letremoveunused, letremoveunusedtop :: Transform +letremoveunused expr@(Let (Rec binds) res) = do + -- Filter out all unused binds. + let binds' = filter dobind binds + -- Only set the changed flag if binds got removed + changeif (length binds' /= length binds) (Let (Rec binds') res) + where + bound_exprs = map snd binds + -- For each bind check if the bind is used by res or any of the bound + -- expressions + dobind (bndr, _) = not $ any (expr_uses_binders [bndr]) (res:bound_exprs) +-- Leave all other expressions unchanged +letremoveunused expr = return expr +letremoveunusedtop = everywhere ("letremoveunused", letremoveunused) + -------------------------------- -- Function inlining -------------------------------- @@ -473,7 +491,7 @@ funextracttop = everywhere ("funextract", funextract) -- What transforms to run? -transforms = [argproptop, funextracttop, etatop, betatop, castproptop, letremovetop, letrectop, letsimpltop, letflattop, scrutsimpltop, casesimpltop, caseremovetop, inlinenonreptop, appsimpltop] +transforms = [argproptop, funextracttop, etatop, betatop, castproptop, letremovetop, letrectop, letsimpltop, letflattop, scrutsimpltop, casesimpltop, caseremovetop, inlinenonreptop, appsimpltop, letremoveunusedtop] -- | Returns the normalized version of the given function. getNormalized :: @@ -504,7 +522,7 @@ normalizeExpr what expr = do -- Normalize this expression trace ("Transforming " ++ what ++ "\nBefore:\n\n" ++ showSDoc ( ppr expr' ) ++ "\n") $ return () expr'' <- dotransforms transforms expr' - trace ("\nAfter:\n\n" ++ showSDoc ( ppr expr')) $ return () + trace ("\nAfter:\n\n" ++ showSDoc ( ppr expr'')) $ return () return expr'' -- | Get the value that is bound to the given binder at top level. Fails when