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)
190 Crash _ _ maybeBranchProblem ->
191 maybe Map.empty count maybeBranchProblem
198replace :: Map.Map String Opt.Expr -> Opt.Expr -> Opt.Expr
199replace substitutions expression =
201 go = replace substitutions
207 Var (Var.Canonical Var.Local name) ->
208 maybe expression id (Map.lookup name substitutions)
214 Range (go lo) (go hi)
216 ExplicitList exprs ->
217 ExplicitList (map go exprs)
219 Binop op left right ->
220 Binop op (go left) (go right)
222 Function args body ->
223 Function args (replace (deleteBatch args substitutions) body)
226 Call (go func) (map go args)
228 TailCall name argNames args ->
229 TailCall name argNames (map go args)
231 If branches finally ->
232 If (map (go *** go) branches) (go finally)
238 Opt.Def facts name body ->
240 (replace (Map.delete name substitutions) body)
242 Opt.TailDef facts name argNames body ->
243 Opt.TailDef facts name argNames
244 (replace (deleteBatch (name:argNames) substitutions) body)
250 (map replaceDef defs)
251 (replace (deleteBatch boundNames substitutions) expr)
253 Case exprName decider jumps ->
257 Leaf (Inline expr) ->
258 Leaf (Inline (go expr))
260 Leaf (Jump _target) ->
263 Chain chain success failure ->
264 Chain chain (goDecider success) (goDecider failure)
266 FanOut path tests fallback ->
267 FanOut path (map (second goDecider) tests) (goDecider fallback)
269 Case exprName (goDecider decider) (map (second go) jumps)
272 Data tag (map go values)
274 DataAccess root index ->
275 DataAccess (go root) index
277 Access record field ->
278 Access (go record) field
280 Update record fields ->
281 Update (go record) (map (second go) fields)
284 Record (map (second go) fields)
299 Program kind (go expr)
304 Crash home region maybeBranchProblem ->
305 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