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.General (PortImpl(..))
10import AST.Expression.Optimized (Expr(..), Decider(..), Choice(..))
11import qualified AST.Expression.Optimized as Opt
12import qualified AST.Variable as Var
13import qualified Optimize.Environment as Env
17inline :: Map.Map String Opt.Expr -> Opt.Expr -> Env.Optimizer Opt.Expr
18inline rawSubs expression =
24 Map.toList (Map.intersectionWith (,) counts rawSubs)
26 do (Subs subs defs) <- foldM processSubs (Subs Map.empty []) annotatedSubs
27 let inlinedExpr = replace subs expression
33 Let (map (\(name, expr) -> Opt.Def Opt.dummyFacts name expr) defs) inlinedExpr
37 { _substitutions :: Map.Map String Opt.Expr
38 , _definitions :: [(String, Opt.Expr)]
42processSubs :: Subs -> (String, (Int, Opt.Expr)) -> Env.Optimizer Subs
43processSubs (Subs subs defs) (name, (n, expr)) =
45 return $ Subs (Map.insert name expr subs) defs
48 do uniqueName <- Env.freshName
50 (Map.insert name (Opt.Var (Var.local uniqueName)) subs)
51 ((uniqueName, expr) : defs)
55deleteBatch :: [String] -> Map.Map String a -> Map.Map String a
56deleteBatch names dict =
57 List.foldl' (flip Map.delete) dict names
60getDefName :: Opt.Def -> String
66 Opt.TailDef _ name _ _ ->
70-- COUNT VARIABLE APPEARANCES
72count :: Opt.Expr -> Map.Map String Int
76 Map.unionWith (+) (count a) (count b)
79 Map.unionsWith (+) (map count xs)
85 Var (Var.Canonical home name) ->
102 ExplicitList exprs ->
105 Binop _op left right ->
108 Function args body ->
109 deleteBatch args (count body)
112 Map.unionWith (+) (count func) (countMany args)
114 TailCall _name _argNames args ->
117 If branches finally ->
118 Map.unionsWith (+) (count finally : map (uncurry count2) branches)
124 Opt.Def _ name body ->
125 Map.delete name (count body)
127 Opt.TailDef _ name argNames body ->
128 deleteBatch (name:argNames) (count body)
131 deleteBatch (map getDefName defs) (count expr)
133 Map.unionsWith (+) (exprCount : map countDef defs)
135 Case _ decider jumps ->
139 Leaf (Inline expr) ->
145 Chain _ success failure ->
146 Map.unionWith (+) (countDecider success) (countDecider failure)
148 FanOut _path tests fallback ->
149 Map.unionsWith (+) (map countDecider (fallback : map snd tests))
151 Map.unionsWith (+) (countDecider decider : map (count . snd) jumps)
156 DataAccess root _index ->
159 Access record _field ->
162 Update record fields ->
163 Map.unionWith (+) (count record) (countMany (map snd fields))
166 countMany (map snd fields)
173 Out _name expr _tipe ->
176 Task _name expr _tipe ->
182 Crash _ _ maybeBranchProblem ->
183 maybe Map.empty count maybeBranchProblem
190replace :: Map.Map String Opt.Expr -> Opt.Expr -> Opt.Expr
191replace substitutions expression =
193 go = replace substitutions
199 Var (Var.Canonical Var.Local name) ->
200 maybe expression id (Map.lookup name substitutions)
206 Range (go lo) (go hi)
208 ExplicitList exprs ->
209 ExplicitList (map go exprs)
211 Binop op left right ->
212 Binop op (go left) (go right)
214 Function args body ->
215 Function args (replace (deleteBatch args substitutions) body)
218 Call (go func) (map go args)
220 TailCall name argNames args ->
221 TailCall name argNames (map go args)
223 If branches finally ->
224 If (map (go *** go) branches) (go finally)
230 Opt.Def facts name body ->
232 (replace (Map.delete name substitutions) body)
234 Opt.TailDef facts name argNames body ->
235 Opt.TailDef facts name argNames
236 (replace (deleteBatch (name:argNames) substitutions) body)
242 (map replaceDef defs)
243 (replace (deleteBatch boundNames substitutions) expr)
245 Case exprName decider jumps ->
249 Leaf (Inline expr) ->
250 Leaf (Inline (go expr))
252 Leaf (Jump _target) ->
255 Chain chain success failure ->
256 Chain chain (goDecider success) (goDecider failure)
258 FanOut path tests fallback ->
259 FanOut path (map (second goDecider) tests) (goDecider fallback)
261 Case exprName (goDecider decider) (map (second go) jumps)
264 Data tag (map go values)
266 DataAccess root index ->
267 DataAccess (go root) index
269 Access record field ->
270 Access (go record) field
272 Update record fields ->
273 Update (go record) (map (second go) fields)
276 Record (map (second go) fields)
284 Out name expr tipe ->
285 Out name (go expr) tipe
287 Task name expr tipe ->
288 Task name (go expr) tipe
293 Crash home region maybeBranchProblem ->
294 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