-do_one env (id, rhs) = case lookupCSEnv env rhs' of
- Just other_id -> (extendSubst env' id other_id, (id', Var other_id))
- Nothing -> (addCSEnvItem env' id' rhs', (id', rhs'))
- where
- (env', id') = addBinder env id
- rhs' = cseExpr env' rhs
+do_one env (id, rhs)
+ = case lookupCSEnv env rhs' of
+ Just (Var other_id) -> (extendSubst env' id other_id, (id', Var other_id))
+ Just other_expr -> (env', (id', other_expr))
+ Nothing -> (addCSEnvItem env' rhs' (Var id'), (id', rhs'))
+ where
+ (env', id') = addBinder env id
+ rhs' | not (workerExists (idWorkerInfo id)) = cseExpr env' rhs
+
+ -- Hack alert: don't do CSE on wrapper RHSs.
+ -- Otherwise we find:
+ -- $wf = h
+ -- f = \x -> ...$wf...
+ -- ===>
+ -- f = \x -> ...h...
+ -- But the WorkerInfo for f still says $wf, which is now dead!
+ | otherwise = rhs
+