1{-# OPTIONS_GHC -Wall #-}
2{-# LANGUAGE OverloadedStrings #-}
3module Optimize.Inline (inline) where
5import Control.Arrow (second, (***))
6import Control.Monad (foldM)
7import qualified Data.List as List
8import qualified Data.Map as Map
11import AST.Expression.Optimized (Expr(..), Decider(..), Choice(..))
12import qualified AST.Expression.Optimized as Opt
13import qualified AST.Variable as Var
14import qualified Optimize.Environment as Env
18inline :: Map.Map Text Opt.Expr -> Opt.Expr -> Env.Optimizer Opt.Expr
19inline rawSubs expression =
25 Map.toList (Map.intersectionWith (,) counts rawSubs)
27 do (Subs subs defs) <- foldM processSubs (Subs Map.empty []) annotatedSubs
28 let inlinedExpr = replace subs expression
34 Let (map (second Opt.Def) defs) inlinedExpr
39 { _substitutions :: Map.Map Text Opt.Expr
40 , _definitions :: [(Text, Opt.Expr)]
44processSubs :: Subs -> (Text, (Int, Opt.Expr)) -> Env.Optimizer Subs
45processSubs (Subs subs defs) (name, (n, expr)) =
47 return $ Subs (Map.insert name expr subs) defs
50 do uniqueName <- Env.freshName
52 (Map.insert name (Opt.Var (Var.local uniqueName)) subs)
53 ((uniqueName, expr) : defs)
60deleteBatch :: [Text] -> Map.Map Text a -> Map.Map Text a
61deleteBatch names dict =
62 List.foldl' (flip Map.delete) dict names
66-- COUNT VARIABLE APPEARANCES
69count :: Opt.Expr -> Map.Map Text Int
73 Map.unionWith (+) (count a) (count b)
76 Map.unionsWith (+) (map count xs)
82 Var (Var.Canonical home name) ->
99 Binop _op left right ->
102 Function args body ->
103 deleteBatch args (count body)
106 Map.unionWith (+) (count func) (countMany args)
108 TailCall _name _argNames args ->
111 If branches finally ->
112 Map.unionsWith (+) (count finally : map (uncurry count2) branches)
116 countDef (name, def) =
119 Map.delete name (count body)
121 Opt.TailDef argNames body ->
122 deleteBatch (name:argNames) (count body)
125 deleteBatch (map fst defs) (count expr)
127 Map.unionsWith (+) (exprCount : map countDef defs)
129 Case _ decider jumps ->
133 Leaf (Inline expr) ->
139 Chain _ success failure ->
140 Map.unionWith (+) (countDecider success) (countDecider failure)
142 FanOut _path tests fallback ->
143 Map.unionsWith (+) (map countDecider (fallback : map snd tests))
145 Map.unionsWith (+) (countDecider decider : map (count . snd) jumps)
150 CtorAccess root _index ->
153 Access record _field ->
156 Update record fields ->
157 Map.unionWith (+) (count record) (countMany (map snd fields))
160 countMany (map snd fields)
180 Crash _ _ maybeBranchProblem ->
181 maybe Map.empty count maybeBranchProblem
188replace :: Map.Map Text Opt.Expr -> Opt.Expr -> Opt.Expr
189replace substitutions expression =
191 go = replace substitutions
197 Var (Var.Canonical Var.Local name) ->
198 maybe expression id (Map.lookup name substitutions)
206 Binop op left right ->
207 Binop op (go left) (go right)
209 Function args body ->
210 Function args (replace (deleteBatch args substitutions) body)
213 Call (go func) (map go args)
215 TailCall name argNames args ->
216 TailCall name argNames (map go args)
218 If branches finally ->
219 If (map (go *** go) branches) (go finally)
223 replaceDef (name, def) =
227 replace (Map.delete name substitutions) body
229 Opt.TailDef argNames body ->
230 (,) name $ Opt.TailDef argNames $
231 replace (deleteBatch (name:argNames) substitutions) body
237 (map replaceDef defs)
238 (replace (deleteBatch boundNames substitutions) expr)
240 Case exprName decider jumps ->
244 Leaf (Inline expr) ->
245 Leaf (Inline (go expr))
247 Leaf (Jump _target) ->
250 Chain chain success failure ->
251 Chain chain (goDecider success) (goDecider failure)
253 FanOut path tests fallback ->
254 FanOut path (map (second goDecider) tests) (goDecider fallback)
256 Case exprName (goDecider decider) (map (second go) jumps)
259 Ctor tag (map go values)
261 CtorAccess root index ->
262 CtorAccess (go root) index
264 Access record field ->
265 Access (go record) field
267 Update record fields ->
268 Update (go record) (map (second go) fields)
271 Record (map (second go) fields)
286 Program kind (go expr)
291 Crash home region maybeBranchProblem ->
292 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