1{-# OPTIONS_GHC -Wall #-}
2module Optimize.Patterns (optimize) where
4import Control.Arrow (second)
5import qualified Data.Map as Map
7import qualified Data.Maybe as Maybe
9import qualified AST.Expression.Optimized as Opt
10import qualified AST.Pattern as P
11import qualified Optimize.Environment as Env
12import qualified Optimize.Patterns.DecisionTree as DT
13import qualified Reporting.Annotation as A
14import qualified Reporting.Region as R
18-- OPTIMIZE A CASE EXPRESSION
25 -> [(P.CanonicalPattern, Opt.Expr)]
26 -> Env.Optimizer Opt.Expr
27optimize variantDict region exprName optBranches =
29 mapM (tagCrashBranches region) optBranches
31 let (patterns, indexedBranches) =
32 unzip (zipWith indexify [0..] crashBranches)
35 DT.compile variantDict patterns
37 return (treeToExpr exprName decisionTree indexedBranches)
42 -> (P.CanonicalPattern, Opt.Expr)
43 -> Env.Optimizer (P.CanonicalPattern, Opt.Expr)
44tagCrashBranches region branch@(pattern@(A.A pr _), expr) =
46 Opt.Call (Opt.Crash _ _) [arg] ->
47 do name <- Env.freshName
49 ( A.A pr (P.Alias name pattern)
50 , Opt.Call (Opt.Crash region (Just name)) [arg]
57indexify :: Int -> (a,b) -> ((a,Int), (Int,b))
58indexify index (pattern, branch) =
65-- CONVERT A TREE TO AN EXPRESSION
68treeToExpr :: String -> DT.DecisionTree -> [(Int, Opt.Expr)] -> Opt.Expr
69treeToExpr name decisionTree allJumps =
72 treeToDecider decisionTree
77 (choices, maybeJumps) =
78 unzip (map (createChoices targetCounts) allJumps)
82 (insertChoices (Map.fromList choices) decider)
83 (Maybe.catMaybes maybeJumps)
89-- Decision trees may have some redundancies, so we convert them to a Decider
90-- which has special constructs to avoid code duplication when possible.
93treeToDecider :: DT.DecisionTree -> Opt.Decider Int
100 DT.Decision _ [] Nothing ->
101 error "compiler bug, somehow created an empty decision tree"
104 DT.Decision _ [(_, subTree)] Nothing ->
105 treeToDecider subTree
107 DT.Decision _ [] (Just subTree) ->
108 treeToDecider subTree
111 DT.Decision path [(test, successTree)] (Just failureTree) ->
112 toChain path test successTree failureTree
114 DT.Decision path [(test, successTree), (_, failureTree)] Nothing ->
115 toChain path test successTree failureTree
118 DT.Decision path edges Nothing ->
120 (necessaryTests, fallback) =
121 (init edges, snd (last edges))
125 (map (second treeToDecider) necessaryTests)
126 (treeToDecider fallback)
128 DT.Decision path edges (Just fallback) ->
129 Opt.FanOut path (map (second treeToDecider) edges) (treeToDecider fallback)
132toChain :: DT.Path -> DT.Test -> DT.DecisionTree -> DT.DecisionTree -> Opt.Decider Int
133toChain path test successTree failureTree =
136 treeToDecider failureTree
138 case treeToDecider successTree of
139 Opt.Chain testChain success subFailure | failure == subFailure ->
140 Opt.Chain ((path, test) : testChain) success failure
143 Opt.Chain [(path, test)] success failure
149-- If a target appears exactly once in a Decider, the corresponding expression
150-- can be inlined. Whether things are inlined or jumps is called a "choice".
153countTargets :: Opt.Decider Int -> Map.Map Int Int
154countTargets decisionTree =
157 Map.singleton target 1
159 Opt.Chain _ success failure ->
160 Map.unionWith (+) (countTargets success) (countTargets failure)
162 Opt.FanOut _ tests fallback ->
163 Map.unionsWith (+) (map countTargets (fallback : map snd tests))
169 -> ( (Int, Opt.Choice), Maybe (Int, Opt.Expr) )
170createChoices targetCounts (target, branch) =
171 if targetCounts ! target == 1 then
172 ( (target, Opt.Inline branch)
177 ( (target, Opt.Jump target)
178 , Just (target, branch)
183 :: Map.Map Int Opt.Choice
185 -> Opt.Decider Opt.Choice
186insertChoices choiceDict decider =
189 insertChoices choiceDict
193 Opt.Leaf (choiceDict ! target)
195 Opt.Chain testChain success failure ->
196 Opt.Chain testChain (go success) (go failure)
198 Opt.FanOut path tests fallback ->
199 Opt.FanOut path (map (second go) tests) (go fallback)
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