Skip to content

Commit 42e3457

Browse files
committed
Arity Raising: follow variables.
1 parent a0aa849 commit 42e3457

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

grin/src/Transformations/Optimising/ArityRaising.hs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,26 @@ arityRaising (te, exp) = runVarM te (apoM builder ([], exp))
5252

5353
-- Set of stores in the function body.
5454
collectStores :: Exp -> [(Name, Val)]
55-
collectStores = para $ \case
55+
collectStores e = followVariables $ flip para e $ \case
5656
SBlockF (_, body) -> body
5757
AltF _ (_, body) -> body
5858
ECaseF _ alts -> mconcat $ map snd alts
59-
EBindF (SStore node, _) (Var v) (_, rhs) -> [(v,node)] <> rhs
59+
EBindF (SStore node@(ConstTagNode _ _), _) (Var v) (_, rhs) -> [(v,node)] <> rhs
60+
EBindF (SStore var@(Var _), _) (Var v) (_, rhs) -> [(v,var)] <> rhs
61+
EBindF (SReturn node, _) (Var v) (_, rhs) -> [(v,node)] <> rhs
6062
EBindF (_, lhs) _ (_, rhs) -> lhs <> rhs
6163
_ -> mempty
6264

65+
-- Follow variables in substiotions based on stores.
66+
followVariables :: [(Name, Val)] -> [(Name, Val)]
67+
followVariables susbsts = Map.toList $ go susbstMap where
68+
susbstMap = Map.fromList susbsts
69+
go m =
70+
let m' = flip Map.map m $ \case
71+
(Var v) | Just val <- Map.lookup v susbstMap -> val
72+
rest -> rest
73+
in if m == m' then m else go m'
74+
6375
-- The substituition that contains a Node or a list of new invariant parameters
6476
builder :: ([(Name, Either Val [Name])], Exp) -> VarM (ExpF (Either Exp ([(Name, Either Val [Name])], Exp)))
6577
builder (substs0, exp0) = case exp0 of

0 commit comments

Comments
 (0)