Skip to content

Commit ed971db

Browse files
committed
Apply optimization recursively
1 parent b6d186e commit ed971db

13 files changed

Lines changed: 449 additions & 427 deletions

lambdacomp.cabal

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ library
7777
LambdaComp.AM.Eval
7878
LambdaComp.AM.Syntax
7979
LambdaComp.CBPV.ArityAnalysis
80+
LambdaComp.CBPV.Optimization.BetaReduction
8081
LambdaComp.CBPV.Optimization.BindingConversion
8182
LambdaComp.CBPV.Optimization.DeadBindingElimination
8283
LambdaComp.CBPV.Optimization.InlineBinding
8384
LambdaComp.CBPV.Optimization.Local
84-
LambdaComp.CBPV.Optimization.SkipReturn
8585
LambdaComp.CBPV.Syntax
8686
LambdaComp.CBPV.ToAM
8787
LambdaComp.CBPV.ToC
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
module LambdaComp.CBPV.Optimization.BetaReduction
2+
( runBetaReduction
3+
) where
4+
5+
import LambdaComp.CBPV.Syntax
6+
7+
runBetaReduction :: Tm Com -> Tm Com
8+
runBetaReduction = betaReduction
9+
10+
betaReduction :: Tm c -> Tm c
11+
betaReduction tm@(TmVar _) = tm
12+
betaReduction tm@(TmGlobal _) = tm
13+
betaReduction tm@TmUnit = tm
14+
betaReduction tm@TmTrue = tm
15+
betaReduction tm@TmFalse = tm
16+
betaReduction tm@(TmInt _) = tm
17+
betaReduction tm@(TmDouble _) = tm
18+
betaReduction (TmThunk tm) = TmThunk $ betaReduction tm
19+
betaReduction (TmIf tm0 tm1 tm2) =
20+
case betaReduction tm0 of
21+
TmTrue -> tm1'
22+
TmFalse -> tm2'
23+
tm0' -> TmIf tm0' tm1' tm2'
24+
where
25+
tm1' = betaReduction tm1
26+
tm2' = betaReduction tm2
27+
betaReduction (TmLam p tm) = TmLam p $ betaReduction tm
28+
betaReduction (tmf `TmApp` tma) =
29+
case betaReduction tmf of
30+
TmLam Param{..} tmf' -> TmLet paramName (betaReduction tma) tmf'
31+
tmf' -> tmf' `TmApp` betaReduction tma
32+
betaReduction (TmForce tm) =
33+
case betaReduction tm of
34+
TmThunk tm' -> tm'
35+
tm' -> TmForce tm'
36+
betaReduction (TmReturn tm) = TmReturn $ betaReduction tm
37+
betaReduction (TmTo tm0 x tm1) =
38+
case betaReduction tm0 of
39+
TmReturn tm0' -> TmLet x tm0' (betaReduction tm1)
40+
tm0' -> TmTo tm0' x (betaReduction tm1)
41+
betaReduction (TmLet x tm0 tm1) = TmLet x (betaReduction tm0) (betaReduction tm1)
42+
betaReduction (TmPrimBinOp op tm0 tm1) = TmPrimBinOp op (betaReduction tm0) (betaReduction tm1)
43+
betaReduction (TmPrimUnOp op tm) = TmPrimUnOp op $ betaReduction tm
44+
betaReduction (TmPrintInt tm0 tm1) = TmPrintInt (betaReduction tm0) (betaReduction tm1)
45+
betaReduction (TmPrintDouble tm0 tm1) = TmPrintDouble (betaReduction tm0) (betaReduction tm1)
46+
betaReduction (TmRec p tm) = TmRec p (betaReduction tm)

src/LambdaComp/CBPV/Optimization/Local.hs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ module LambdaComp.CBPV.Optimization.Local
22
( runLocalOptDefault
33
) where
44

5+
import Control.Monad.Identity (Identity (Identity, runIdentity))
6+
7+
import LambdaComp.CBPV.Optimization.BetaReduction (runBetaReduction)
58
import LambdaComp.CBPV.Optimization.BindingConversion (runCommutingTo, runLiftingLet)
69
import LambdaComp.CBPV.Optimization.DeadBindingElimination (runDeadLetElimination)
710
import LambdaComp.CBPV.Optimization.InlineBinding (runInlineSimpleLet)
8-
import LambdaComp.CBPV.Optimization.SkipReturn (runSkipReturn)
911
import LambdaComp.CBPV.Syntax
1012

1113
runLocalOptDefault :: Program -> Program
@@ -15,4 +17,11 @@ runLocalOptDefaultTop :: Top -> Top
1517
runLocalOptDefaultTop m = m{ tmDefBody = runLocalOptDefaultTm $ tmDefBody m }
1618

1719
runLocalOptDefaultTm :: Tm Com -> Tm Com
18-
runLocalOptDefaultTm = runDeadLetElimination . runInlineSimpleLet . runLiftingLet . runSkipReturn . runCommutingTo
20+
runLocalOptDefaultTm = runIdentity . repeatUntilFix (Identity . runDeadLetElimination . runInlineSimpleLet . runLiftingLet . runBetaReduction . runCommutingTo)
21+
22+
repeatUntilFix :: (Monad m, Eq a) => (a -> m a) -> a -> m a
23+
repeatUntilFix f a = do
24+
a' <- f a
25+
if a == a'
26+
then pure a'
27+
else repeatUntilFix f a'

src/LambdaComp/CBPV/Optimization/SkipReturn.hs

Lines changed: 0 additions & 33 deletions
This file was deleted.

test/golden/Fib10.lc.code.gen.am

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,12 @@
106106
, ThunkCodeSection
107107
{ thunkCodeSectionName = "sys_thunk_4"
108108
, thunkCode =
109-
[ IScope
109+
[ IAssign
110+
( AIdent "var_c_f0_6" )
111+
( VaThunk "sys_thunk_3"
112+
[ ALocalEnv 2 ]
113+
)
114+
, IScope
110115
, IPush
111116
( VaAddr
112117
( ALocalEnv 1 )
@@ -121,11 +126,6 @@
121126
( AIdent "var_c_c_1" )
122127
, IPop
123128
( AIdent "var_e_y" )
124-
, IAssign
125-
( AIdent "var_c_f0_6" )
126-
( VaThunk "sys_thunk_3"
127-
[ ALocalEnv 2 ]
128-
)
129129
, ICondJump
130130
( VaAddr
131131
( AIdent "var_c_c_1" )
@@ -555,11 +555,11 @@
555555
, ThunkCodeSection
556556
{ thunkCodeSectionName = "sys_thunk_19"
557557
, thunkCode =
558-
[ IPop
559-
( AIdent "var_e_n" )
560-
, IAssign
558+
[ IAssign
561559
( AIdent "var_c_f0_8" )
562560
( VaThunk "sys_thunk_18" [] )
561+
, IPop
562+
( AIdent "var_e_n" )
563563
, IScope
564564
, IPush
565565
( VaAddr

0 commit comments

Comments
 (0)