-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSSA.hs
More file actions
72 lines (56 loc) · 1.93 KB
/
SSA.hs
File metadata and controls
72 lines (56 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
{-# LANGUAGE TemplateHaskell #-}
module SSA
( initSSAState
, convertToSSA
, _uses
, _body --
, PC
, UsePoints
) where
-- MLA is not in SSA form. (hi lo += s * t)
-- We maybe use "Pseudo-SSA" form, in which each live interval contains
-- only one range, e.g. has no holes, because in straight-line code, there
-- is no phi functions.
import qualified Data.Map as M
import Data.List (nub)
import Insn
import Control.Monad.State (State)
import Control.Monad (when, liftM, forM_)
import Control.Applicative ((<$>))
import Data.Maybe (fromMaybe, isNothing)
-- MonadState lenses
import Data.Label (mkLabels)
import Data.Label.PureM
type PC = Int
type UsePoints = [PC]
data SSAState = SSAState { _uses :: M.Map String UsePoints
, _count :: M.Map String Int
, _body :: [Insn] -- Transformed body
} deriving (Show)
$(mkLabels [''SSAState])
initSSAState :: SSAState
initSSAState = SSAState M.empty M.empty []
convertToSSA :: [Insn] -> State SSAState ()
convertToSSA insns = do
forM_ (zip [1 :: PC ..] insns) $ \(pc, insn) -> do
forUsed_ insn $ \v -> do
c <- M.lookup v <$> gets count
when (isNothing c) $ count =. M.insert v 0
uses =. M.insertWith (++) (append v c) [pc]
c1 <- gets count
let ir1 = mapUsed (\v -> append v (M.lookup v c1)) insn
forDefd_ insn $ \v -> do
c <- liftM (+1) <$> M.lookup v <$> gets count
uses =. M.insertWith (++) (append v c) [pc]
count =. M.insert v (fromMaybe 0 c)
c2 <- gets count
let ir2 = mapDefd (\v -> append v (M.lookup v c2)) ir1
body =. (ir2:)
-- reverse the order
body =. reverse
uses =. M.map (nub . reverse) -- Note: nub is O(n^2)
where
append :: String -> Maybe Int -> String
append v (Just 0) = v
append v (Just n) = v ++ "_" ++ show n
append v Nothing = v