1{-# OPTIONS_GHC -Wall #-}
2module Optimize.Inline (inline) where
4import Control.Arrow (second, (***))
5import Control.Monad (foldM)
6import qualified Data.List as List
7import qualified Data.Map as Map
9import AST.Expression.Optimized (Expr(..), Decider(..), Choice(..))
10import qualified AST.Expression.Optimized as Opt
11import qualified AST.Variable as Var
12import qualified Optimize.Environment as Env
16inline :: Map.Map String Opt.Expr -> Opt.Expr -> Env.Optimizer Opt.Expr
17inline rawSubs expression =
23 Map.toList (Map.intersectionWith (,) counts rawSubs)
25 do (Subs subs defs) <- foldM processSubs (Subs Map.empty []) annotatedSubs
26 let inlinedExpr = replace subs expression
32 Let (map (\(name, expr) -> Opt.Def Opt.dummyFacts name expr) defs) inlinedExpr
36 { _substitutions :: Map.Map String Opt.Expr
37 , _definitions :: [(String, Opt.Expr)]
41processSubs :: Subs -> (String, (Int, Opt.Expr)) -> Env.Optimizer Subs
42processSubs (Subs subs defs) (name, (n, expr)) =
44 return $ Subs (Map.insert name expr subs) defs
47 do uniqueName <- Env.freshName
49 (Map.insert name (Opt.Var (Var.local uniqueName)) subs)
50 ((uniqueName, expr) : defs)
57deleteBatch :: [String] -> Map.Map String a -> Map.Map String a
58deleteBatch names dict =
59 List.foldl' (flip Map.delete) dict names
62getDefName :: Opt.Def -> String
68 Opt.TailDef _ name _ _ ->
73-- COUNT VARIABLE APPEARANCES
76count :: Opt.Expr -> Map.Map String Int
80 Map.unionWith (+) (count a) (count b)
83 Map.unionsWith (+) (map count xs)
89 Var (Var.Canonical home name) ->
106 ExplicitList exprs ->
109 Binop _op left right ->
112 Function args body ->
113 deleteBatch args (count body)
116 Map.unionWith (+) (count func) (countMany args)
118 TailCall _name _argNames args ->
121 If branches finally ->
122 Map.unionsWith (+) (count finally : map (uncurry count2) branches)
128 Opt.Def _ name body ->
129 Map.delete name (count body)
131 Opt.TailDef _ name argNames body ->
132 deleteBatch (name:argNames) (count body)
135 deleteBatch (map getDefName defs) (count expr)
137 Map.unionsWith (+) (exprCount : map countDef defs)
139 Case _ decider jumps ->
143 Leaf (Inline expr) ->
149 Chain _ success failure ->
150 Map.unionWith (+) (countDecider success) (countDecider failure)
152 FanOut _path tests fallback ->
153 Map.unionsWith (+) (map countDecider (fallback : map snd tests))
155 Map.unionsWith (+) (countDecider decider : map (count . snd) jumps)
160 DataAccess root _index ->
163 Access record _field ->
166 Update record fields ->
167 Map.unionWith (+) (count record) (countMany (map snd fields))
170 countMany (map snd fields)
181 Crash _ _ maybeBranchProblem ->
182 maybe Map.empty count maybeBranchProblem
189replace :: Map.Map String Opt.Expr -> Opt.Expr -> Opt.Expr
190replace substitutions expression =
192 go = replace substitutions
198 Var (Var.Canonical Var.Local name) ->
199 maybe expression id (Map.lookup name substitutions)
205 Range (go lo) (go hi)
207 ExplicitList exprs ->
208 ExplicitList (map go exprs)
210 Binop op left right ->
211 Binop op (go left) (go right)
213 Function args body ->
214 Function args (replace (deleteBatch args substitutions) body)
217 Call (go func) (map go args)
219 TailCall name argNames args ->
220 TailCall name argNames (map go args)
222 If branches finally ->
223 If (map (go *** go) branches) (go finally)
229 Opt.Def facts name body ->
231 (replace (Map.delete name substitutions) body)
233 Opt.TailDef facts name argNames body ->
234 Opt.TailDef facts name argNames
235 (replace (deleteBatch (name:argNames) substitutions) body)
241 (map replaceDef defs)
242 (replace (deleteBatch boundNames substitutions) expr)
244 Case exprName decider jumps ->
248 Leaf (Inline expr) ->
249 Leaf (Inline (go expr))
251 Leaf (Jump _target) ->
254 Chain chain success failure ->
255 Chain chain (goDecider success) (goDecider failure)
257 FanOut path tests fallback ->
258 FanOut path (map (second goDecider) tests) (goDecider fallback)
260 Case exprName (goDecider decider) (map (second go) jumps)
263 Data tag (map go values)
265 DataAccess root index ->
266 DataAccess (go root) index
268 Access record field ->
269 Access (go record) field
271 Update record fields ->
272 Update (go record) (map (second go) fields)
275 Record (map (second go) fields)
286 Crash home region maybeBranchProblem ->
287 Crash home region (fmap go maybeBranchProblem)
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list