1{-# OPTIONS_GHC -Wall #-}
2module Optimize.Case (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.DecisionTree as DT
15-- OPTIMIZE A CASE EXPRESSION
18optimize :: DT.VariantDict -> String -> [(P.CanonicalPattern, Opt.Expr)] -> Opt.Expr
19optimize variantDict exprName optBranches =
21 (patterns, indexedBranches) =
22 unzip (zipWith indexify [0..] optBranches)
25 DT.compile variantDict patterns
27 treeToExpr exprName decisionTree indexedBranches
30indexify :: Int -> (a,b) -> ((a,Int), (Int,b))
31indexify index (pattern, branch) =
38-- CONVERT A TREE TO AN EXPRESSION
41treeToExpr :: String -> DT.DecisionTree -> [(Int, Opt.Expr)] -> Opt.Expr
42treeToExpr name decisionTree allJumps =
45 treeToDecider decisionTree
50 (choices, maybeJumps) =
51 unzip (map (createChoices targetCounts) allJumps)
55 (insertChoices (Map.fromList choices) decider)
56 (Maybe.catMaybes maybeJumps)
62-- Decision trees may have some redundancies, so we convert them to a Decider
63-- which has special constructs to avoid code duplication when possible.
66treeToDecider :: DT.DecisionTree -> Opt.Decider Int
73 DT.Decision _ [] Nothing ->
74 error "compiler bug, somehow created an empty decision tree"
77 DT.Decision _ [(_, subTree)] Nothing ->
80 DT.Decision _ [] (Just subTree) ->
84 DT.Decision path [(test, successTree)] (Just failureTree) ->
85 toChain path test successTree failureTree
87 DT.Decision path [(test, successTree), (_, failureTree)] Nothing ->
88 toChain path test successTree failureTree
91 DT.Decision path edges Nothing ->
93 (necessaryTests, fallback) =
94 (init edges, snd (last edges))
98 (map (second treeToDecider) necessaryTests)
99 (treeToDecider fallback)
101 DT.Decision path edges (Just fallback) ->
102 Opt.FanOut path (map (second treeToDecider) edges) (treeToDecider fallback)
105toChain :: DT.Path -> DT.Test -> DT.DecisionTree -> DT.DecisionTree -> Opt.Decider Int
106toChain path test successTree failureTree =
109 treeToDecider failureTree
111 case treeToDecider successTree of
112 Opt.Chain testChain success subFailure | failure == subFailure ->
113 Opt.Chain ((path, test) : testChain) success failure
116 Opt.Chain [(path, test)] success failure
122-- If a target appears exactly once in a Decider, the corresponding expression
123-- can be inlined. Whether things are inlined or jumps is called a "choice".
126countTargets :: Opt.Decider Int -> Map.Map Int Int
127countTargets decisionTree =
130 Map.singleton target 1
132 Opt.Chain _ success failure ->
133 Map.unionWith (+) (countTargets success) (countTargets failure)
135 Opt.FanOut _ tests fallback ->
136 Map.unionsWith (+) (map countTargets (fallback : map snd tests))
142 -> ( (Int, Opt.Choice), Maybe (Int, Opt.Expr) )
143createChoices targetCounts (target, branch) =
144 if targetCounts ! target == 1 then
145 ( (target, Opt.Inline branch)
150 ( (target, Opt.Jump target)
151 , Just (target, branch)
156 :: Map.Map Int Opt.Choice
158 -> Opt.Decider Opt.Choice
159insertChoices choiceDict decider =
162 insertChoices choiceDict
166 Opt.Leaf (choiceDict ! target)
168 Opt.Chain testChain success failure ->
169 Opt.Chain testChain (go success) (go failure)
171 Opt.FanOut path tests fallback ->
172 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