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) ->
103 ExplicitList exprs ->
106 Binop _op left right ->
109 Function args body ->
110 deleteBatch args (count body)
113 Map.unionWith (+) (count func) (countMany args)
115 TailCall _name _argNames args ->
118 If branches finally ->
119 Map.unionsWith (+) (count finally : map (uncurry count2) branches)
125 Opt.Def _ name body ->
126 Map.delete name (count body)
128 Opt.TailDef _ name argNames body ->
129 deleteBatch (name:argNames) (count body)
132 deleteBatch (map getDefName defs) (count expr)
134 Map.unionsWith (+) (exprCount : map countDef defs)
136 Case _ decider jumps ->
140 Leaf (Inline expr) ->
146 Chain _ success failure ->
147 Map.unionWith (+) (countDecider success) (countDecider failure)
149 FanOut _path tests fallback ->
150 Map.unionsWith (+) (map countDecider (fallback : map snd tests))
152 Map.unionsWith (+) (countDecider decider : map (count . snd) jumps)
157 DataAccess root _index ->
160 Access record _field ->
163 Update record fields ->
164 Map.unionWith (+) (count record) (countMany (map snd fields))
167 countMany (map snd fields)
187 Crash _ _ maybeBranchProblem ->
188 maybe Map.empty count maybeBranchProblem
195replace :: Map.Map String Opt.Expr -> Opt.Expr -> Opt.Expr
196replace substitutions expression =
198 go = replace substitutions
204 Var (Var.Canonical Var.Local name) ->
205 maybe expression id (Map.lookup name substitutions)
210 ExplicitList exprs ->
211 ExplicitList (map go exprs)
213 Binop op left right ->
214 Binop op (go left) (go right)
216 Function args body ->
217 Function args (replace (deleteBatch args substitutions) body)
220 Call (go func) (map go args)
222 TailCall name argNames args ->
223 TailCall name argNames (map go args)
225 If branches finally ->
226 If (map (go *** go) branches) (go finally)
232 Opt.Def facts name body ->
234 (replace (Map.delete name substitutions) body)
236 Opt.TailDef facts name argNames body ->
237 Opt.TailDef facts name argNames
238 (replace (deleteBatch (name:argNames) substitutions) body)
244 (map replaceDef defs)
245 (replace (deleteBatch boundNames substitutions) expr)
247 Case exprName decider jumps ->
251 Leaf (Inline expr) ->
252 Leaf (Inline (go expr))
254 Leaf (Jump _target) ->
257 Chain chain success failure ->
258 Chain chain (goDecider success) (goDecider failure)
260 FanOut path tests fallback ->
261 FanOut path (map (second goDecider) tests) (goDecider fallback)
263 Case exprName (goDecider decider) (map (second go) jumps)
266 Data tag (map go values)
268 DataAccess root index ->
269 DataAccess (go root) index
271 Access record field ->
272 Access (go record) field
274 Update record fields ->
275 Update (go record) (map (second go) fields)
278 Record (map (second go) fields)
293 Program kind (go expr)
298 Crash home region maybeBranchProblem ->
299 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