The non-recursive version is also changed to work for each nested let in
turn, instead of running on a bunch of them at the same time.
-- Unused let binding removal
--------------------------------
letremoveunused, letremoveunusedtop :: Transform
-- Unused let binding removal
--------------------------------
letremoveunused, letremoveunusedtop :: Transform
-letremoveunused expr@(Let _ _) = do
+letremoveunused expr@(Let (NonRec b bound) res) = do
+ let used = expr_uses_binders [b] res
+ if used
+ then return expr
+ else change res
+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
-- Filter out all unused binds.
let binds' = filter dobind binds
-- Only set the changed flag if binds got removed
- changeif (length binds' /= length binds) (mkNonRecLets binds' res)
+ changeif (length binds' /= length binds) (Let (Rec binds') res)
- (binds, res) = flattenLets expr
bound_exprs = map snd binds
-- For each bind check if the bind is used by res or any of the bound
-- expressions
bound_exprs = map snd binds
-- For each bind check if the bind is used by res or any of the bound
-- expressions