Skip to content

Commit d8250d0

Browse files
committed
ES: factored out rhs block removal
1 parent fa804a6 commit d8250d0

File tree

1 file changed

+7
-33
lines changed

1 file changed

+7
-33
lines changed

grin/src/Transformations/ExtendedSyntax/Optimising/CopyPropagation.hs

Lines changed: 7 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ type Aliases = Map Name Name
3232
type Env = (OriginalValues, Aliases)
3333

3434
copyPropagation :: Exp -> Exp
35-
copyPropagation e = hylo folder builder (mempty, e) where
35+
copyPropagation e = skipRhsBlocks $ hylo folder builder (mempty, e) where
3636

3737
builder :: (Env, Exp) -> ExpF (Env, Exp)
3838
builder (env@(origVals, aliases), exp) = let e = substVarRefExp aliases $ exp in case e of
@@ -82,47 +82,21 @@ copyPropagation e = hylo folder builder (mempty, e) where
8282
-- NOTE: This cleans up the left-over produced by the above transformation.
8383
folder :: ExpF Exp -> Exp
8484
folder = \case
85-
EBindF lhs bpat (SBlock rhs)
86-
-> EBind lhs bpat rhs
87-
88-
-- NOTE: already handled in the builder
89-
-- right unit law
90-
-- EBindF leftExp (VarPat patVar) (SReturn (Var valVar))
91-
-- | patVar == valVar -> leftExp
92-
9385
-- <patVal> @ <var> <- pure <retVal>
9486
-- where retVal is a basic value (lit or unit)
9587
EBindF (SReturn retVal) (AsPat var patVal) rightExp
9688
| isBasicValue retVal
9789
, retVal == patVal
9890
-> EBind (SReturn retVal) (VarPat var) rightExp
9991

100-
-- NOTE: already handled in the builder
101-
-- <patVal> @ <var> <- pure <retVal>
102-
-- where retVal is a node
103-
-- EBindF (SReturn retVal) (AsPat var patVal) rightExp
104-
-- | ConstTagNode retTag _ <- retVal
105-
-- , ConstTagNode patTag _ <- patVal
106-
-- , retTag == patTag
107-
-- -> EBind (SReturn retVal) (VarPat var) rightExp
108-
109-
-- NOTE: already handled in the builder
110-
{- left unit law ; cleanup x <- pure y copies
111-
112-
NOTE: This case could be handled by SDVE as well, however
113-
performing it locally saves us an effect tracking analysis.
114-
This is because here, we have more information about variable
115-
bidnings. We know for sure that such copying bindings are not needed
116-
since all the occurences of the left-hand side have been replaced with
117-
the variable on the right-hand side.
118-
-}
119-
-- EBindF (SReturn Var{}) VarPat{} rightExp
120-
-- -> rightExp
121-
122-
SBlockF exp@SBlock{} -> exp
123-
12492
exp -> embed exp
12593

94+
skipRhsBlocks :: Exp -> Exp
95+
skipRhsBlocks exp = flip cata exp $ \case
96+
EBindF lhs bpat (SBlock rhs) -> EBind lhs bpat rhs
97+
SBlockF exp@SBlock{} -> exp
98+
exp -> embed exp
99+
126100
getAlias :: Name -> Aliases -> Name
127101
getAlias var aliases = Map.findWithDefault var var aliases
128102

0 commit comments

Comments
 (0)