{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE GADTs #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
module GHC.Cmm.Dataflow
( C, O, Block
, lastNode, entryLabel
, foldNodesBwdOO
, foldRewriteNodesBwdOO
, DataflowLattice(..), OldFact(..), NewFact(..), JoinedFact(..)
, TransferFun, RewriteFun
, Fact, FactBase
, getFact, mkFactBase
, analyzeCmmFwd, analyzeCmmBwd
, rewriteCmmBwd
, changedIf
, joinOutFacts
, joinFacts
)
where
import GHC.Prelude
import GHC.Cmm
import GHC.Types.Unique.Supply
import Data.Array
import Data.Maybe
import Data.IntSet (IntSet)
import qualified Data.IntSet as IntSet
import Data.Kind (Type)
import GHC.Cmm.Dataflow.Block
import GHC.Cmm.Dataflow.Graph
import GHC.Cmm.Dataflow.Collections
import GHC.Cmm.Dataflow.Label
type family Fact (x :: Extensibility) f :: Type
type instance Fact C f = FactBase f
type instance Fact O f = f
newtype OldFact a = OldFact a
newtype NewFact a = NewFact a
data JoinedFact a
= Changed !a
| NotChanged !a
getJoined :: JoinedFact a -> a
getJoined :: forall a. JoinedFact a -> a
getJoined (Changed a
a) = a
a
getJoined (NotChanged a
a) = a
a
changedIf :: Bool -> a -> JoinedFact a
changedIf :: forall a. Bool -> a -> JoinedFact a
changedIf Bool
True = forall a. a -> JoinedFact a
Changed
changedIf Bool
False = forall a. a -> JoinedFact a
NotChanged
type JoinFun a = OldFact a -> NewFact a -> JoinedFact a
data DataflowLattice a = DataflowLattice
{ forall a. DataflowLattice a -> a
fact_bot :: a
, forall a. DataflowLattice a -> JoinFun a
fact_join :: JoinFun a
}
data Direction = Fwd | Bwd
type TransferFun f = CmmBlock -> FactBase f -> FactBase f
type RewriteFun f = CmmBlock -> FactBase f -> UniqSM (CmmBlock, FactBase f)
analyzeCmmBwd, analyzeCmmFwd
:: DataflowLattice f
-> TransferFun f
-> CmmGraph
-> FactBase f
-> FactBase f
analyzeCmmBwd :: forall f.
DataflowLattice f
-> TransferFun f -> CmmGraph -> FactBase f -> FactBase f
analyzeCmmBwd = forall f.
Direction
-> DataflowLattice f
-> TransferFun f
-> CmmGraph
-> FactBase f
-> FactBase f
analyzeCmm Direction
Bwd
analyzeCmmFwd :: forall f.
DataflowLattice f
-> TransferFun f -> CmmGraph -> FactBase f -> FactBase f
analyzeCmmFwd = forall f.
Direction
-> DataflowLattice f
-> TransferFun f
-> CmmGraph
-> FactBase f
-> FactBase f
analyzeCmm Direction
Fwd
analyzeCmm
:: Direction
-> DataflowLattice f
-> TransferFun f
-> CmmGraph
-> FactBase f
-> FactBase f
analyzeCmm :: forall f.
Direction
-> DataflowLattice f
-> TransferFun f
-> CmmGraph
-> FactBase f
-> FactBase f
analyzeCmm Direction
dir DataflowLattice f
lattice TransferFun f
transfer CmmGraph
cmmGraph FactBase f
initFact =
{-# SCC analyzeCmm #-}
let entry :: Label
entry = forall (n :: Extensibility -> Extensibility -> *).
GenCmmGraph n -> Label
g_entry CmmGraph
cmmGraph
hooplGraph :: Graph CmmNode C C
hooplGraph = forall (n :: Extensibility -> Extensibility -> *).
GenCmmGraph n -> Graph n C C
g_graph CmmGraph
cmmGraph
blockMap :: LabelMap CmmBlock
blockMap =
case Graph CmmNode C C
hooplGraph of
GMany MaybeO C (Block CmmNode O C)
NothingO LabelMap CmmBlock
bm MaybeO C (Block CmmNode C O)
NothingO -> LabelMap CmmBlock
bm
in forall f.
Direction
-> DataflowLattice f
-> TransferFun f
-> Label
-> LabelMap CmmBlock
-> FactBase f
-> FactBase f
fixpointAnalysis Direction
dir DataflowLattice f
lattice TransferFun f
transfer Label
entry LabelMap CmmBlock
blockMap FactBase f
initFact
fixpointAnalysis
:: forall f.
Direction
-> DataflowLattice f
-> TransferFun f
-> Label
-> LabelMap CmmBlock
-> FactBase f
-> FactBase f
fixpointAnalysis :: forall f.
Direction
-> DataflowLattice f
-> TransferFun f
-> Label
-> LabelMap CmmBlock
-> FactBase f
-> FactBase f
fixpointAnalysis Direction
direction DataflowLattice f
lattice TransferFun f
do_block Label
entry LabelMap CmmBlock
blockmap = IntHeap -> FactBase f -> FactBase f
loop IntHeap
start
where
blocks :: [CmmBlock]
blocks = forall (n :: Extensibility -> Extensibility -> *).
NonLocal n =>
Direction -> Label -> LabelMap (Block n C C) -> [Block n C C]
sortBlocks Direction
direction Label
entry LabelMap CmmBlock
blockmap
num_blocks :: Key
num_blocks = forall (t :: * -> *) a. Foldable t => t a -> Key
length [CmmBlock]
blocks
block_arr :: Array Key CmmBlock
block_arr = {-# SCC "block_arr" #-} forall i e. Ix i => (i, i) -> [e] -> Array i e
listArray (Key
0, Key
num_blocks forall a. Num a => a -> a -> a
- Key
1) [CmmBlock]
blocks
start :: IntHeap
start = {-# SCC "start" #-} [Key] -> IntHeap
IntSet.fromDistinctAscList
[Key
0 .. Key
num_blocks forall a. Num a => a -> a -> a
- Key
1]
dep_blocks :: LabelMap IntHeap
dep_blocks = {-# SCC "dep_blocks" #-} Direction -> [CmmBlock] -> LabelMap IntHeap
mkDepBlocks Direction
direction [CmmBlock]
blocks
join :: JoinFun f
join = forall a. DataflowLattice a -> JoinFun a
fact_join DataflowLattice f
lattice
loop
:: IntHeap
-> FactBase f
-> FactBase f
loop :: IntHeap -> FactBase f -> FactBase f
loop IntHeap
todo !FactBase f
fbase1 | Just (Key
index, IntHeap
todo1) <- IntHeap -> Maybe (Key, IntHeap)
IntSet.minView IntHeap
todo =
let block :: CmmBlock
block = Array Key CmmBlock
block_arr forall i e. Ix i => Array i e -> i -> e
! Key
index
out_facts :: FactBase f
out_facts = {-# SCC "do_block" #-} TransferFun f
do_block CmmBlock
block FactBase f
fbase1
(IntHeap
todo2, FactBase f
fbase2) = {-# SCC "mapFoldWithKey" #-}
forall (map :: * -> *) b a.
IsMap map =>
(b -> KeyOf map -> a -> b) -> b -> map a -> b
mapFoldlWithKey
(forall f.
JoinFun f
-> LabelMap IntHeap
-> (IntHeap, FactBase f)
-> Label
-> f
-> (IntHeap, FactBase f)
updateFact JoinFun f
join LabelMap IntHeap
dep_blocks) (IntHeap
todo1, FactBase f
fbase1) FactBase f
out_facts
in IntHeap -> FactBase f -> FactBase f
loop IntHeap
todo2 FactBase f
fbase2
loop IntHeap
_ !FactBase f
fbase1 = FactBase f
fbase1
rewriteCmmBwd
:: DataflowLattice f
-> RewriteFun f
-> CmmGraph
-> FactBase f
-> UniqSM (CmmGraph, FactBase f)
rewriteCmmBwd :: forall f.
DataflowLattice f
-> RewriteFun f
-> CmmGraph
-> FactBase f
-> UniqSM (CmmGraph, FactBase f)
rewriteCmmBwd = forall f.
Direction
-> DataflowLattice f
-> RewriteFun f
-> CmmGraph
-> FactBase f
-> UniqSM (CmmGraph, FactBase f)
rewriteCmm Direction
Bwd
rewriteCmm
:: Direction
-> DataflowLattice f
-> RewriteFun f
-> CmmGraph
-> FactBase f
-> UniqSM (CmmGraph, FactBase f)
rewriteCmm :: forall f.
Direction
-> DataflowLattice f
-> RewriteFun f
-> CmmGraph
-> FactBase f
-> UniqSM (CmmGraph, FactBase f)
rewriteCmm Direction
dir DataflowLattice f
lattice RewriteFun f
rwFun CmmGraph
cmmGraph FactBase f
initFact = {-# SCC rewriteCmm #-} do
let entry :: Label
entry = forall (n :: Extensibility -> Extensibility -> *).
GenCmmGraph n -> Label
g_entry CmmGraph
cmmGraph
hooplGraph :: Graph CmmNode C C
hooplGraph = forall (n :: Extensibility -> Extensibility -> *).
GenCmmGraph n -> Graph n C C
g_graph CmmGraph
cmmGraph
blockMap1 :: LabelMap CmmBlock
blockMap1 =
case Graph CmmNode C C
hooplGraph of
GMany MaybeO C (Block CmmNode O C)
NothingO LabelMap CmmBlock
bm MaybeO C (Block CmmNode C O)
NothingO -> LabelMap CmmBlock
bm
(LabelMap CmmBlock
blockMap2, FactBase f
facts) <-
forall f.
Direction
-> DataflowLattice f
-> RewriteFun f
-> Label
-> LabelMap CmmBlock
-> FactBase f
-> UniqSM (LabelMap CmmBlock, FactBase f)
fixpointRewrite Direction
dir DataflowLattice f
lattice RewriteFun f
rwFun Label
entry LabelMap CmmBlock
blockMap1 FactBase f
initFact
forall (m :: * -> *) a. Monad m => a -> m a
return (CmmGraph
cmmGraph {g_graph :: Graph CmmNode C C
g_graph = forall (e :: Extensibility)
(block :: (Extensibility -> Extensibility -> *)
-> Extensibility -> Extensibility -> *)
(n :: Extensibility -> Extensibility -> *) (x :: Extensibility).
MaybeO e (block n O C)
-> Body' block n -> MaybeO x (block n C O) -> Graph' block n e x
GMany forall t. MaybeO C t
NothingO LabelMap CmmBlock
blockMap2 forall t. MaybeO C t
NothingO}, FactBase f
facts)
fixpointRewrite
:: forall f.
Direction
-> DataflowLattice f
-> RewriteFun f
-> Label
-> LabelMap CmmBlock
-> FactBase f
-> UniqSM (LabelMap CmmBlock, FactBase f)
fixpointRewrite :: forall f.
Direction
-> DataflowLattice f
-> RewriteFun f
-> Label
-> LabelMap CmmBlock
-> FactBase f
-> UniqSM (LabelMap CmmBlock, FactBase f)
fixpointRewrite Direction
dir DataflowLattice f
lattice RewriteFun f
do_block Label
entry LabelMap CmmBlock
blockmap = IntHeap
-> LabelMap CmmBlock
-> FactBase f
-> UniqSM (LabelMap CmmBlock, FactBase f)
loop IntHeap
start LabelMap CmmBlock
blockmap
where
blocks :: [CmmBlock]
blocks = forall (n :: Extensibility -> Extensibility -> *).
NonLocal n =>
Direction -> Label -> LabelMap (Block n C C) -> [Block n C C]
sortBlocks Direction
dir Label
entry LabelMap CmmBlock
blockmap
num_blocks :: Key
num_blocks = forall (t :: * -> *) a. Foldable t => t a -> Key
length [CmmBlock]
blocks
block_arr :: Array Key CmmBlock
block_arr = {-# SCC "block_arr_rewrite" #-}
forall i e. Ix i => (i, i) -> [e] -> Array i e
listArray (Key
0, Key
num_blocks forall a. Num a => a -> a -> a
- Key
1) [CmmBlock]
blocks
start :: IntHeap
start = {-# SCC "start_rewrite" #-}
[Key] -> IntHeap
IntSet.fromDistinctAscList [Key
0 .. Key
num_blocks forall a. Num a => a -> a -> a
- Key
1]
dep_blocks :: LabelMap IntHeap
dep_blocks = {-# SCC "dep_blocks_rewrite" #-} Direction -> [CmmBlock] -> LabelMap IntHeap
mkDepBlocks Direction
dir [CmmBlock]
blocks
join :: JoinFun f
join = forall a. DataflowLattice a -> JoinFun a
fact_join DataflowLattice f
lattice
loop
:: IntHeap
-> LabelMap CmmBlock
-> FactBase f
-> UniqSM (LabelMap CmmBlock, FactBase f)
loop :: IntHeap
-> LabelMap CmmBlock
-> FactBase f
-> UniqSM (LabelMap CmmBlock, FactBase f)
loop IntHeap
todo !LabelMap CmmBlock
blocks1 !FactBase f
fbase1
| Just (Key
index, IntHeap
todo1) <- IntHeap -> Maybe (Key, IntHeap)
IntSet.minView IntHeap
todo = do
let block :: CmmBlock
block = Array Key CmmBlock
block_arr forall i e. Ix i => Array i e -> i -> e
! Key
index
(CmmBlock
new_block, FactBase f
out_facts) <- {-# SCC "do_block_rewrite" #-}
RewriteFun f
do_block CmmBlock
block FactBase f
fbase1
let blocks2 :: LabelMap CmmBlock
blocks2 = forall (map :: * -> *) a.
IsMap map =>
KeyOf map -> a -> map a -> map a
mapInsert (forall (thing :: Extensibility -> Extensibility -> *)
(x :: Extensibility).
NonLocal thing =>
thing C x -> Label
entryLabel CmmBlock
new_block) CmmBlock
new_block LabelMap CmmBlock
blocks1
(IntHeap
todo2, FactBase f
fbase2) = {-# SCC "mapFoldWithKey_rewrite" #-}
forall (map :: * -> *) b a.
IsMap map =>
(b -> KeyOf map -> a -> b) -> b -> map a -> b
mapFoldlWithKey
(forall f.
JoinFun f
-> LabelMap IntHeap
-> (IntHeap, FactBase f)
-> Label
-> f
-> (IntHeap, FactBase f)
updateFact JoinFun f
join LabelMap IntHeap
dep_blocks) (IntHeap
todo1, FactBase f
fbase1) FactBase f
out_facts
IntHeap
-> LabelMap CmmBlock
-> FactBase f
-> UniqSM (LabelMap CmmBlock, FactBase f)
loop IntHeap
todo2 LabelMap CmmBlock
blocks2 FactBase f
fbase2
loop IntHeap
_ !LabelMap CmmBlock
blocks1 !FactBase f
fbase1 = forall (m :: * -> *) a. Monad m => a -> m a
return (LabelMap CmmBlock
blocks1, FactBase f
fbase1)
sortBlocks
:: NonLocal n
=> Direction -> Label -> LabelMap (Block n C C) -> [Block n C C]
sortBlocks :: forall (n :: Extensibility -> Extensibility -> *).
NonLocal n =>
Direction -> Label -> LabelMap (Block n C C) -> [Block n C C]
sortBlocks Direction
direction Label
entry LabelMap (Block n C C)
blockmap =
case Direction
direction of
Direction
Fwd -> [Block n C C]
fwd
Direction
Bwd -> forall a. [a] -> [a]
reverse [Block n C C]
fwd
where
fwd :: [Block n C C]
fwd = forall (block :: Extensibility -> Extensibility -> *).
NonLocal block =>
LabelMap (block C C) -> Label -> [block C C]
revPostorderFrom LabelMap (Block n C C)
blockmap Label
entry
mkDepBlocks :: Direction -> [CmmBlock] -> LabelMap IntSet
mkDepBlocks :: Direction -> [CmmBlock] -> LabelMap IntHeap
mkDepBlocks Direction
Fwd [CmmBlock]
blocks = forall {map :: * -> *}
{thing :: Extensibility -> Extensibility -> *}
{x :: Extensibility}.
(KeyOf map ~ Label, IsMap map, NonLocal thing) =>
[thing C x] -> Key -> map IntHeap -> map IntHeap
go [CmmBlock]
blocks Key
0 forall (map :: * -> *) a. IsMap map => map a
mapEmpty
where
go :: [thing C x] -> Key -> map IntHeap -> map IntHeap
go [] !Key
_ !map IntHeap
dep_map = map IntHeap
dep_map
go (thing C x
b:[thing C x]
bs) !Key
n !map IntHeap
dep_map =
[thing C x] -> Key -> map IntHeap -> map IntHeap
go [thing C x]
bs (Key
n forall a. Num a => a -> a -> a
+ Key
1) forall a b. (a -> b) -> a -> b
$ forall (map :: * -> *) a.
IsMap map =>
KeyOf map -> a -> map a -> map a
mapInsert (forall (thing :: Extensibility -> Extensibility -> *)
(x :: Extensibility).
NonLocal thing =>
thing C x -> Label
entryLabel thing C x
b) (Key -> IntHeap
IntSet.singleton Key
n) map IntHeap
dep_map
mkDepBlocks Direction
Bwd [CmmBlock]
blocks = forall {map :: * -> *}
{thing :: Extensibility -> Extensibility -> *}
{e :: Extensibility}.
(KeyOf map ~ Label, IsMap map, NonLocal thing) =>
[thing e C] -> Key -> map IntHeap -> map IntHeap
go [CmmBlock]
blocks Key
0 forall (map :: * -> *) a. IsMap map => map a
mapEmpty
where
go :: [thing e C] -> Key -> map IntHeap -> map IntHeap
go [] !Key
_ !map IntHeap
dep_map = map IntHeap
dep_map
go (thing e C
b:[thing e C]
bs) !Key
n !map IntHeap
dep_map =
let insert :: map IntHeap -> Label -> map IntHeap
insert map IntHeap
m Label
l = forall (map :: * -> *) a.
IsMap map =>
(a -> a -> a) -> KeyOf map -> a -> map a -> map a
mapInsertWith IntHeap -> IntHeap -> IntHeap
IntSet.union Label
l (Key -> IntHeap
IntSet.singleton Key
n) map IntHeap
m
in [thing e C] -> Key -> map IntHeap -> map IntHeap
go [thing e C]
bs (Key
n forall a. Num a => a -> a -> a
+ Key
1) forall a b. (a -> b) -> a -> b
$ forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' map IntHeap -> Label -> map IntHeap
insert map IntHeap
dep_map (forall (thing :: Extensibility -> Extensibility -> *)
(e :: Extensibility).
NonLocal thing =>
thing e C -> [Label]
successors thing e C
b)
updateFact
:: JoinFun f
-> LabelMap IntSet
-> (IntHeap, FactBase f)
-> Label
-> f
-> (IntHeap, FactBase f)
updateFact :: forall f.
JoinFun f
-> LabelMap IntHeap
-> (IntHeap, FactBase f)
-> Label
-> f
-> (IntHeap, FactBase f)
updateFact JoinFun f
fact_join LabelMap IntHeap
dep_blocks (IntHeap
todo, FactBase f
fbase) Label
lbl f
new_fact
= case forall f. Label -> FactBase f -> Maybe f
lookupFact Label
lbl FactBase f
fbase of
Maybe f
Nothing ->
let !z :: FactBase f
z = forall (map :: * -> *) a.
IsMap map =>
KeyOf map -> a -> map a -> map a
mapInsert Label
lbl f
new_fact FactBase f
fbase in (IntHeap
changed, FactBase f
z)
Just f
old_fact ->
case JoinFun f
fact_join (forall a. a -> OldFact a
OldFact f
old_fact) (forall a. a -> NewFact a
NewFact f
new_fact) of
(NotChanged f
_) -> (IntHeap
todo, FactBase f
fbase)
(Changed f
f) -> let !z :: FactBase f
z = forall (map :: * -> *) a.
IsMap map =>
KeyOf map -> a -> map a -> map a
mapInsert Label
lbl f
f FactBase f
fbase in (IntHeap
changed, FactBase f
z)
where
changed :: IntHeap
changed = IntHeap
todo IntHeap -> IntHeap -> IntHeap
`IntSet.union`
forall (map :: * -> *) a. IsMap map => a -> KeyOf map -> map a -> a
mapFindWithDefault IntHeap
IntSet.empty Label
lbl LabelMap IntHeap
dep_blocks
getFact :: DataflowLattice f -> Label -> FactBase f -> f
getFact :: forall f. DataflowLattice f -> Label -> FactBase f -> f
getFact DataflowLattice f
lat Label
l FactBase f
fb = case forall f. Label -> FactBase f -> Maybe f
lookupFact Label
l FactBase f
fb of Just f
f -> f
f
Maybe f
Nothing -> forall a. DataflowLattice a -> a
fact_bot DataflowLattice f
lat
joinOutFacts :: (NonLocal n) => DataflowLattice f -> n e C -> FactBase f -> f
joinOutFacts :: forall (n :: Extensibility -> Extensibility -> *) f
(e :: Extensibility).
NonLocal n =>
DataflowLattice f -> n e C -> FactBase f -> f
joinOutFacts DataflowLattice f
lattice n e C
nonLocal FactBase f
fact_base = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' f -> f -> f
join (forall a. DataflowLattice a -> a
fact_bot DataflowLattice f
lattice) [f]
facts
where
join :: f -> f -> f
join f
new f
old = forall a. JoinedFact a -> a
getJoined forall a b. (a -> b) -> a -> b
$ forall a. DataflowLattice a -> JoinFun a
fact_join DataflowLattice f
lattice (forall a. a -> OldFact a
OldFact f
old) (forall a. a -> NewFact a
NewFact f
new)
facts :: [f]
facts =
[ forall a. HasCallStack => Maybe a -> a
fromJust Maybe f
fact
| Label
s <- forall (thing :: Extensibility -> Extensibility -> *)
(e :: Extensibility).
NonLocal thing =>
thing e C -> [Label]
successors n e C
nonLocal
, let fact :: Maybe f
fact = forall f. Label -> FactBase f -> Maybe f
lookupFact Label
s FactBase f
fact_base
, forall a. Maybe a -> Bool
isJust Maybe f
fact
]
joinFacts :: DataflowLattice f -> [f] -> f
joinFacts :: forall f. DataflowLattice f -> [f] -> f
joinFacts DataflowLattice f
lattice [f]
facts = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' f -> f -> f
join (forall a. DataflowLattice a -> a
fact_bot DataflowLattice f
lattice) [f]
facts
where
join :: f -> f -> f
join f
new f
old = forall a. JoinedFact a -> a
getJoined forall a b. (a -> b) -> a -> b
$ forall a. DataflowLattice a -> JoinFun a
fact_join DataflowLattice f
lattice (forall a. a -> OldFact a
OldFact f
old) (forall a. a -> NewFact a
NewFact f
new)
mkFactBase :: DataflowLattice f -> [(Label, f)] -> FactBase f
mkFactBase :: forall f. DataflowLattice f -> [(Label, f)] -> FactBase f
mkFactBase DataflowLattice f
lattice = forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' LabelMap f -> (Label, f) -> LabelMap f
add forall (map :: * -> *) a. IsMap map => map a
mapEmpty
where
join :: JoinFun f
join = forall a. DataflowLattice a -> JoinFun a
fact_join DataflowLattice f
lattice
add :: LabelMap f -> (Label, f) -> LabelMap f
add LabelMap f
result (Label
l, f
f1) =
let !newFact :: f
newFact =
case forall (map :: * -> *) a.
IsMap map =>
KeyOf map -> map a -> Maybe a
mapLookup Label
l LabelMap f
result of
Maybe f
Nothing -> f
f1
Just f
f2 -> forall a. JoinedFact a -> a
getJoined forall a b. (a -> b) -> a -> b
$ JoinFun f
join (forall a. a -> OldFact a
OldFact f
f1) (forall a. a -> NewFact a
NewFact f
f2)
in forall (map :: * -> *) a.
IsMap map =>
KeyOf map -> a -> map a -> map a
mapInsert Label
l f
newFact LabelMap f
result
foldNodesBwdOO :: (CmmNode O O -> f -> f) -> Block CmmNode O O -> f -> f
foldNodesBwdOO :: forall f. (CmmNode O O -> f -> f) -> Block CmmNode O O -> f -> f
foldNodesBwdOO CmmNode O O -> f -> f
funOO = Block CmmNode O O -> f -> f
go
where
go :: Block CmmNode O O -> f -> f
go (BCat Block CmmNode O O
b1 Block CmmNode O O
b2) f
f = Block CmmNode O O -> f -> f
go Block CmmNode O O
b1 forall a b. (a -> b) -> a -> b
$! Block CmmNode O O -> f -> f
go Block CmmNode O O
b2 f
f
go (BSnoc Block CmmNode O O
h CmmNode O O
n) f
f = Block CmmNode O O -> f -> f
go Block CmmNode O O
h forall a b. (a -> b) -> a -> b
$! CmmNode O O -> f -> f
funOO CmmNode O O
n f
f
go (BCons CmmNode O O
n Block CmmNode O O
t) f
f = CmmNode O O -> f -> f
funOO CmmNode O O
n forall a b. (a -> b) -> a -> b
$! Block CmmNode O O -> f -> f
go Block CmmNode O O
t f
f
go (BMiddle CmmNode O O
n) f
f = CmmNode O O -> f -> f
funOO CmmNode O O
n f
f
go Block CmmNode O O
BNil f
f = f
f
{-# INLINABLE foldNodesBwdOO #-}
foldRewriteNodesBwdOO
:: forall f.
(CmmNode O O -> f -> UniqSM (Block CmmNode O O, f))
-> Block CmmNode O O
-> f
-> UniqSM (Block CmmNode O O, f)
foldRewriteNodesBwdOO :: forall f.
(CmmNode O O -> f -> UniqSM (Block CmmNode O O, f))
-> Block CmmNode O O -> f -> UniqSM (Block CmmNode O O, f)
foldRewriteNodesBwdOO CmmNode O O -> f -> UniqSM (Block CmmNode O O, f)
rewriteOO Block CmmNode O O
initBlock f
initFacts = Block CmmNode O O -> f -> UniqSM (Block CmmNode O O, f)
go Block CmmNode O O
initBlock f
initFacts
where
go :: Block CmmNode O O -> f -> UniqSM (Block CmmNode O O, f)
go (BCons CmmNode O O
node1 Block CmmNode O O
block1) !f
fact1 = (CmmNode O O -> f -> UniqSM (Block CmmNode O O, f)
rewriteOO CmmNode O O
node1 forall {m :: * -> *} {t} {n :: Extensibility -> Extensibility -> *}
{b} {t}.
Monad m =>
(t -> m (Block n O O, b))
-> (t -> m (Block n O O, t)) -> t -> m (Block n O O, b)
`comp` Block CmmNode O O -> f -> UniqSM (Block CmmNode O O, f)
go Block CmmNode O O
block1) f
fact1
go (BSnoc Block CmmNode O O
block1 CmmNode O O
node1) !f
fact1 = (Block CmmNode O O -> f -> UniqSM (Block CmmNode O O, f)
go Block CmmNode O O
block1 forall {m :: * -> *} {t} {n :: Extensibility -> Extensibility -> *}
{b} {t}.
Monad m =>
(t -> m (Block n O O, b))
-> (t -> m (Block n O O, t)) -> t -> m (Block n O O, b)
`comp` CmmNode O O -> f -> UniqSM (Block CmmNode O O, f)
rewriteOO CmmNode O O
node1) f
fact1
go (BCat Block CmmNode O O
blockA1 Block CmmNode O O
blockB1) !f
fact1 = (Block CmmNode O O -> f -> UniqSM (Block CmmNode O O, f)
go Block CmmNode O O
blockA1 forall {m :: * -> *} {t} {n :: Extensibility -> Extensibility -> *}
{b} {t}.
Monad m =>
(t -> m (Block n O O, b))
-> (t -> m (Block n O O, t)) -> t -> m (Block n O O, b)
`comp` Block CmmNode O O -> f -> UniqSM (Block CmmNode O O, f)
go Block CmmNode O O
blockB1) f
fact1
go (BMiddle CmmNode O O
node) !f
fact1 = CmmNode O O -> f -> UniqSM (Block CmmNode O O, f)
rewriteOO CmmNode O O
node f
fact1
go Block CmmNode O O
BNil !f
fact = forall (m :: * -> *) a. Monad m => a -> m a
return (forall (n :: Extensibility -> Extensibility -> *). Block n O O
BNil, f
fact)
comp :: (t -> m (Block n O O, b))
-> (t -> m (Block n O O, t)) -> t -> m (Block n O O, b)
comp t -> m (Block n O O, b)
rew1 t -> m (Block n O O, t)
rew2 = \t
f1 -> do
(Block n O O
b, t
f2) <- t -> m (Block n O O, t)
rew2 t
f1
(Block n O O
a, !b
f3) <- t -> m (Block n O O, b)
rew1 t
f2
let !c :: Block n O O
c = forall (n :: Extensibility -> Extensibility -> *).
Block n O O -> Block n O O -> Block n O O
joinBlocksOO Block n O O
a Block n O O
b
forall (m :: * -> *) a. Monad m => a -> m a
return (Block n O O
c, b
f3)
{-# INLINE comp #-}
{-# INLINABLE foldRewriteNodesBwdOO #-}
joinBlocksOO :: Block n O O -> Block n O O -> Block n O O
joinBlocksOO :: forall (n :: Extensibility -> Extensibility -> *).
Block n O O -> Block n O O -> Block n O O
joinBlocksOO Block n O O
BNil Block n O O
b = Block n O O
b
joinBlocksOO Block n O O
b Block n O O
BNil = Block n O O
b
joinBlocksOO (BMiddle n O O
n) Block n O O
b = forall (n :: Extensibility -> Extensibility -> *)
(x :: Extensibility).
n O O -> Block n O x -> Block n O x
blockCons n O O
n Block n O O
b
joinBlocksOO Block n O O
b (BMiddle n O O
n) = forall (n :: Extensibility -> Extensibility -> *)
(e :: Extensibility).
Block n e O -> n O O -> Block n e O
blockSnoc Block n O O
b n O O
n
joinBlocksOO Block n O O
b1 Block n O O
b2 = forall (n :: Extensibility -> Extensibility -> *).
Block n O O -> Block n O O -> Block n O O
BCat Block n O O
b1 Block n O O
b2
type IntHeap = IntSet