Skip to content

Commit

Permalink
[hoopl] refactor the passes, add helper mapE and insnToGraph functions
Browse files Browse the repository at this point in the history
  • Loading branch information
pmurias committed Apr 15, 2011
1 parent bdcd777 commit 4b7a361
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
12 changes: 5 additions & 7 deletions hoopl/ConstProp.hs
Expand Up @@ -57,15 +57,13 @@ constPropPass = FwdPass
initFact :: ConstFact
initFact = Map.fromList []

-- Rewriting: replace constant variables
singleInsn = return . Just . insnToGraph

constProp :: FuelMonad m => FwdRewrite m Insn ConstFact
constProp = mkFRewrite cp
where
cp :: (Monad m) => Insn e x -> Map.Map Int (WithTop Expr) -> m (Maybe (Graph Insn e x))
cp (BifPlus reg a b) f = return $ Just $ mkMiddle (BifPlus reg (lookup f a) (lookup f b))
cp (Subcall reg args) f = return $ Just $ mkMiddle (Subcall reg (map (lookup f) args))
cp _ _ = return Nothing
cp :: (Monad m) => Insn e x -> ConstFact -> m (Maybe (Graph Insn e x))
cp insn f = singleInsn $ mapE (lookup f) insn
lookup f reg@(Reg r) = case Map.lookup r f of
Just (PElem c) -> c
_ -> reg
Expand All @@ -79,7 +77,7 @@ simplify :: FuelMonad m => FwdRewrite m Insn ConstFact
simplify = mkFRewrite s
where
s :: (Monad m) => Insn e x -> a -> m (Maybe (Graph Insn e x))
s (BifPlus reg (Double a) (Double b)) _ = return $ Just $ mkMiddle $ RegSet reg (Double (a+b))
s (BifPlus reg (Double a) (Double b)) _ = singleInsn $ RegSet reg (Double (a+b))
s _ _ = return Nothing


14 changes: 13 additions & 1 deletion hoopl/Insn.hs
@@ -1,5 +1,5 @@
{-# LANGUAGE ViewPatterns,GADTs,StandaloneDeriving,NoMonomorphismRestriction #-}
module Insn (Insn(..),Expr(..)) where
module Insn (Insn(..),Expr(..),mapE,insnToGraph) where
import Compiler.Hoopl
-- a side effect free expression
-- FIXME handle Box and Ann smartly
Expand All @@ -11,3 +11,15 @@ data Insn e x where
Subcall :: Int -> [Expr] -> Insn O O
BifPlus :: Int -> Expr -> Expr -> Insn O O
RegSet :: Int -> Expr -> Insn O O

mapE :: (Expr -> Expr) -> Insn e x -> Insn e x
mapE func (BifPlus reg a b) = BifPlus reg (func a) (func b)
mapE func (Subcall reg args) = Subcall reg (map func args)
mapE func (RegSet reg a) = RegSet reg (func a)
mapE func (Fetch reg a) = Fetch reg (func a)

insnToGraph :: Insn e x -> Graph Insn e x
insnToGraph n@(Fetch _ _) = mkMiddle n
insnToGraph n@(Subcall _ _) = mkMiddle n
insnToGraph n@(BifPlus _ _ _) = mkMiddle n
insnToGraph n@(RegSet _ _) = mkMiddle n

0 comments on commit 4b7a361

Please sign in to comment.