@@ -28,8 +28,13 @@ import Data.IntMap (IntMap)
28-- UseMap maps names to the set of used (reachable) argument positions.
28-- UseMap maps names to the set of used (reachable) argument positions.
29type UseMap = Map Name IntSet
29type UseMap = Map Name IntSet
31+-- Deps augmented with cached info.
33+-- snd = all names that the function depends on
34+type Deps' = (Deps, Set Name)
31−-- DepMap maps functions (only functions) to their Deps.
36+-- DepMap maps functions (only functions) to their Deps'.
32−type DepMap = Map Name Deps
37+type DepMap = Map Name Deps'
34-- Deps of a function is a map from argument index
39-- Deps of a function is a map from argument index
35-- to all usages of the argument, including conditions
40-- to all usages of the argument, including conditions
@@ -54,8 +59,24 @@ type PatVar = Ctors -> Var
54type PatVars = Map Name PatVar
59type PatVars = Map Name PatVar
56findUsed :: Context -> Ctxt CGInfo -> [Name] -> DepMap
61findUsed :: Context -> Ctxt CGInfo -> [Name] -> DepMap
57−findUsed ctx cg ns = M.fromList [(n, getDeps n) | n <- ns]
62+findUsed ctx cg ns = dfs M.empty ns
64+ dfs :: DepMap -> [Name] -> DepMap
67+ | n `M.member` dmap = dfs dmap ns
68+ | otherwise = dfs (M.insert n (deps, depn) dmap) ns
70+ next = [n | n <- S.toList depn, n `M.notMember` dmap]
74+ depNames :: Deps -> Set Name
75+ depNames = S.unions . map (S.unions . map f . S.toList) . IM.elems
77+ f :: (Ctors, Cond) -> Set Name
78+ f (ctors, cond) = S.map fst cond -- let's ignore ctors for now
59 getDeps :: Name -> Deps
80 getDeps :: Name -> Deps
60 getDeps n = case lookupDef n ctx of
81 getDeps n = case lookupDef n ctx of
61 [def] -> getDepsDef def
82 [def] -> getDepsDef def