From 14c4b2bf87d936f2123c237a26503011ccace963 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Thu, 6 Aug 2009 15:56:44 +0200 Subject: [PATCH] Add letremoveunused transformation. This transformation removes all unused let bindings. --- "c\316\273ash/CLasH/Normalize.hs" | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git "a/c\316\273ash/CLasH/Normalize.hs" "b/c\316\273ash/CLasH/Normalize.hs" index 8b35bb9..647bd19 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 :: -- 2.30.2