git.iko.soy/rowtype-yoga/purescript-native
on golang help sign in
Some optimizer improvements
commit3f20972841f2444388dba2779e66137b0286da82 authorPhil Freeman <paf31@cantab.net> dateSat 29 Mar 2014, 12:55 (13 years ago) parent5bbabd9
3 files changed+24−4
src/Language/PureScript/Optimizer.hs+4−2 src/Language/PureScript/Optimizer/Inliner.hs+9−1 src/Language/PureScript/Optimizer/Unused.hs+11−1
src/Language/PureScript/Optimizer.hs+4−2
@@ -53,19 +53,21 @@ import Language.PureScript.Optimizer.Blocks
5353--
5454optimize :: Options -> JS -> JS
5555optimize opts | optionsNoOptimizations opts = id
56− | otherwise = untilFixedPoint $ applyAll
56+ | otherwise = untilFixedPoint (applyAll
5757 [ collapseNestedBlocks
5858 , tco opts
5959 , magicDo opts
6060 , removeUnusedVariables
61+ , removeCodeAfterReturnStatements
6162 , unThunk
6263 , etaConvert
64+ , evaluateIifes
6365 , inlineVariables
6466 , inlineOperator (C.$) $ \f x -> JSApp f [x]
6567 , inlineOperator (C.#) $ \x f -> JSApp f [x]
6668 , inlineOperator (C.!!) $ flip JSIndexer
6769 , inlineOperator (C.++) $ JSBinary Add
68− , inlineCommonOperators ]
70+ , inlineCommonOperators ])
6971
7072untilFixedPoint :: (Eq a) => (a -> a) -> a -> a
7173untilFixedPoint f = go
src/Language/PureScript/Optimizer/Inliner.hs+9−1
@@ -18,7 +18,8 @@ module Language.PureScript.Optimizer.Inliner (
1818 inlineOperator,
1919 inlineCommonOperators,
2020 etaConvert,
21− unThunk
21+ unThunk,
22+ evaluateIifes
2223) where
2324
2425import Data.Generics
@@ -57,6 +58,13 @@ unThunk = everywhere (mkT convert)
5758 convert (JSBlock [JSReturn (JSApp (JSFunction Nothing [] (JSBlock body)) [])]) = JSBlock body
5859 convert js = js
5960
61+evaluateIifes :: JS -> JS
62+evaluateIifes = everywhere (mkT convert)
63+ where
64+ convert :: JS -> JS
65+ convert (JSApp (JSFunction Nothing [] (JSBlock [JSReturn ret])) []) = ret
66+ convert js = js
67+
6068inlineVariables :: JS -> JS
6169inlineVariables = everywhere (mkT $ removeFromBlock go)
6270 where
src/Language/PureScript/Optimizer/Unused.hs+11−1
@@ -14,7 +14,8 @@
1414-----------------------------------------------------------------------------
1515
1616module Language.PureScript.Optimizer.Unused (
17− removeUnusedVariables
17+ removeUnusedVariables,
18+ removeCodeAfterReturnStatements
1819) where
1920
2021import Data.Generics
@@ -29,3 +30,12 @@ removeUnusedVariables = everywhere (mkT $ removeFromBlock go)
2930 go [] = []
3031 go (JSVariableIntroduction var _ : sts) | not (isUsed var sts) = go sts
3132 go (s:sts) = s : go sts
33+
34+removeCodeAfterReturnStatements :: JS -> JS
35+removeCodeAfterReturnStatements = everywhere (mkT $ removeFromBlock go)
36+ where
37+ go :: [JS] -> [JS]
38+ go jss | not (any isJSReturn jss) = jss
39+ | otherwise = let (body, ret : _) = span (not . isJSReturn) jss in body ++ [ret]
40+ isJSReturn (JSReturn _) = True
41+ isJSReturn _ = False