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
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)
54deleteBatch :: [String] -> Map.Map String a -> Map.Map String a
55deleteBatch names dict =
56 List.foldl' (flip Map.delete) dict names
59getDefName :: Opt.Def -> String
65 Opt.TailDef _ name _ _ ->
69-- COUNT VARIABLE APPEARANCES
71count :: Opt.Expr -> Map.Map String Int
75 Map.unionWith (+) (count a) (count b)
78 Map.unionsWith (+) (map count xs)
84 Var (Var.Canonical home name) ->
101 ExplicitList exprs ->
104 Binop _op left right ->
107 Function args body ->
108 deleteBatch args (count body)
111 Map.unionWith (+) (count func) (countMany args)
113 TailCall _name _argNames args ->
116 If branches finally ->
117 Map.unionsWith (+) (count finally : map (uncurry count2) branches)
123 Opt.Def _ name body ->
124 Map.delete name (count body)
126 Opt.TailDef _ name argNames body ->
127 deleteBatch (name:argNames) (count body)
130 deleteBatch (map getDefName defs) (count expr)
132 Map.unionsWith (+) (exprCount : map countDef defs)
134 Case _ decider jumps ->
138 Leaf (Inline expr) ->
144 Chain _ success failure ->
145 Map.unionWith (+) (countDecider success) (countDecider failure)
147 FanOut _path tests fallback ->
148 Map.unionsWith (+) (map countDecider (fallback : map snd tests))
150 Map.unionsWith (+) (countDecider decider : map (count . snd) jumps)
155 DataAccess root _index ->
158 Access record _field ->
161 Update record fields ->
162 Map.unionWith (+) (count record) (countMany (map snd fields))
165 countMany (map snd fields)
172 Out _name expr _tipe ->
175 Task _name expr _tipe ->
187replace :: Map.Map String Opt.Expr -> Opt.Expr -> Opt.Expr
188replace substitutions expression =
190 go = replace substitutions
196 Var (Var.Canonical Var.Local name) ->
197 maybe expression id (Map.lookup name substitutions)
203 Range (go lo) (go hi)
205 ExplicitList exprs ->
206 ExplicitList (map go exprs)
208 Binop op left right ->
209 Binop op (go left) (go right)
211 Function args body ->
212 Function args (replace (deleteBatch args substitutions) body)
215 Call (go func) (map go args)
217 TailCall name argNames args ->
218 TailCall name argNames (map go args)
220 If branches finally ->
221 If (map (go *** go) branches) (go finally)
227 Opt.Def facts name body ->
229 (replace (Map.delete name substitutions) body)
231 Opt.TailDef facts name argNames body ->
232 Opt.TailDef facts name argNames
233 (replace (deleteBatch (name:argNames) substitutions) body)
239 (map replaceDef defs)
240 (replace (deleteBatch boundNames substitutions) expr)
242 Case exprName decider jumps ->
246 Leaf (Inline expr) ->
247 Leaf (Inline (go expr))
249 Leaf (Jump _target) ->
252 Chain chain success failure ->
253 Chain chain (goDecider success) (goDecider failure)
255 FanOut path tests fallback ->
256 FanOut path (map (second goDecider) tests) (goDecider fallback)
258 Case exprName (goDecider decider) (map (second go) jumps)
261 Data tag (map go values)
263 DataAccess root index ->
264 DataAccess (go root) index
266 Access record field ->
267 Access (go record) field
269 Update record fields ->
270 Update (go record) (map (second go) fields)
273 Record (map (second go) fields)
281 Out name expr tipe ->
282 Out name (go expr) tipe
284 Task name expr tipe ->
285 Task name (go expr) tipe
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