{-# LANGUAGE Strict #-}
module Futhark.Analysis.UsageTable
( UsageTable,
without,
lookup,
used,
expand,
isConsumed,
isInResult,
isUsedDirectly,
isSize,
usages,
usage,
consumedUsage,
inResultUsage,
sizeUsage,
sizeUsages,
Usages,
usageInStm,
)
where
import Data.Bits
import qualified Data.Foldable as Foldable
import qualified Data.IntMap.Strict as IM
import Data.List (foldl')
import Futhark.IR
import Futhark.IR.Prop.Aliases
import Prelude hiding (lookup)
newtype UsageTable = UsageTable (IM.IntMap Usages)
deriving (UsageTable -> UsageTable -> Bool
(UsageTable -> UsageTable -> Bool)
-> (UsageTable -> UsageTable -> Bool) -> Eq UsageTable
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: UsageTable -> UsageTable -> Bool
$c/= :: UsageTable -> UsageTable -> Bool
== :: UsageTable -> UsageTable -> Bool
$c== :: UsageTable -> UsageTable -> Bool
Eq, Int -> UsageTable -> ShowS
[UsageTable] -> ShowS
UsageTable -> String
(Int -> UsageTable -> ShowS)
-> (UsageTable -> String)
-> ([UsageTable] -> ShowS)
-> Show UsageTable
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [UsageTable] -> ShowS
$cshowList :: [UsageTable] -> ShowS
show :: UsageTable -> String
$cshow :: UsageTable -> String
showsPrec :: Int -> UsageTable -> ShowS
$cshowsPrec :: Int -> UsageTable -> ShowS
Show)
instance Semigroup UsageTable where
UsageTable IntMap Usages
table1 <> :: UsageTable -> UsageTable -> UsageTable
<> UsageTable IntMap Usages
table2 =
IntMap Usages -> UsageTable
UsageTable (IntMap Usages -> UsageTable) -> IntMap Usages -> UsageTable
forall a b. (a -> b) -> a -> b
$ (Usages -> Usages -> Usages)
-> IntMap Usages -> IntMap Usages -> IntMap Usages
forall a. (a -> a -> a) -> IntMap a -> IntMap a -> IntMap a
IM.unionWith Usages -> Usages -> Usages
forall a. Semigroup a => a -> a -> a
(<>) IntMap Usages
table1 IntMap Usages
table2
instance Monoid UsageTable where
mempty :: UsageTable
mempty = IntMap Usages -> UsageTable
UsageTable IntMap Usages
forall a. Monoid a => a
mempty
without :: UsageTable -> [VName] -> UsageTable
without :: UsageTable -> [VName] -> UsageTable
without (UsageTable IntMap Usages
table) =
IntMap Usages -> UsageTable
UsageTable (IntMap Usages -> UsageTable)
-> ([VName] -> IntMap Usages) -> [VName] -> UsageTable
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (IntMap Usages -> Int -> IntMap Usages)
-> IntMap Usages -> [Int] -> IntMap Usages
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
Foldable.foldl ((Int -> IntMap Usages -> IntMap Usages)
-> IntMap Usages -> Int -> IntMap Usages
forall a b c. (a -> b -> c) -> b -> a -> c
flip Int -> IntMap Usages -> IntMap Usages
forall a. Int -> IntMap a -> IntMap a
IM.delete) IntMap Usages
table ([Int] -> IntMap Usages)
-> ([VName] -> [Int]) -> [VName] -> IntMap Usages
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (VName -> Int) -> [VName] -> [Int]
forall a b. (a -> b) -> [a] -> [b]
map VName -> Int
baseTag
lookup :: VName -> UsageTable -> Maybe Usages
lookup :: VName -> UsageTable -> Maybe Usages
lookup VName
name (UsageTable IntMap Usages
table) = Int -> IntMap Usages -> Maybe Usages
forall a. Int -> IntMap a -> Maybe a
IM.lookup (VName -> Int
baseTag VName
name) IntMap Usages
table
lookupPred :: (Usages -> Bool) -> VName -> UsageTable -> Bool
lookupPred :: (Usages -> Bool) -> VName -> UsageTable -> Bool
lookupPred Usages -> Bool
f VName
name = Bool -> (Usages -> Bool) -> Maybe Usages -> Bool
forall b a. b -> (a -> b) -> Maybe a -> b
maybe Bool
False Usages -> Bool
f (Maybe Usages -> Bool)
-> (UsageTable -> Maybe Usages) -> UsageTable -> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. VName -> UsageTable -> Maybe Usages
lookup VName
name
used :: VName -> UsageTable -> Bool
used :: VName -> UsageTable -> Bool
used = (Usages -> Bool) -> VName -> UsageTable -> Bool
lookupPred ((Usages -> Bool) -> VName -> UsageTable -> Bool)
-> (Usages -> Bool) -> VName -> UsageTable -> Bool
forall a b. (a -> b) -> a -> b
$ Bool -> Usages -> Bool
forall a b. a -> b -> a
const Bool
True
expand :: (VName -> Names) -> UsageTable -> UsageTable
expand :: (VName -> Names) -> UsageTable -> UsageTable
expand VName -> Names
look (UsageTable IntMap Usages
m) = IntMap Usages -> UsageTable
UsageTable (IntMap Usages -> UsageTable) -> IntMap Usages -> UsageTable
forall a b. (a -> b) -> a -> b
$ (IntMap Usages -> (Int, Usages) -> IntMap Usages)
-> IntMap Usages -> [(Int, Usages)] -> IntMap Usages
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' IntMap Usages -> (Int, Usages) -> IntMap Usages
grow IntMap Usages
m ([(Int, Usages)] -> IntMap Usages)
-> [(Int, Usages)] -> IntMap Usages
forall a b. (a -> b) -> a -> b
$ IntMap Usages -> [(Int, Usages)]
forall a. IntMap a -> [(Int, a)]
IM.toList IntMap Usages
m
where
grow :: IntMap Usages -> (Int, Usages) -> IntMap Usages
grow IntMap Usages
m' (Int
k, Usages
v) =
(IntMap Usages -> VName -> IntMap Usages)
-> IntMap Usages -> IntMap VName -> IntMap Usages
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (Usages -> IntMap Usages -> VName -> IntMap Usages
forall {a}. Semigroup a => a -> IntMap a -> VName -> IntMap a
grow'' (Usages -> IntMap Usages -> VName -> IntMap Usages)
-> Usages -> IntMap Usages -> VName -> IntMap Usages
forall a b. (a -> b) -> a -> b
$ Usages
v Usages -> Usages -> Usages
`withoutU` Usages
presentU) IntMap Usages
m' (IntMap VName -> IntMap Usages) -> IntMap VName -> IntMap Usages
forall a b. (a -> b) -> a -> b
$
Names -> IntMap VName
namesIntMap (Names -> IntMap VName) -> Names -> IntMap VName
forall a b. (a -> b) -> a -> b
$ VName -> Names
look (VName -> Names) -> VName -> Names
forall a b. (a -> b) -> a -> b
$ Name -> Int -> VName
VName (String -> Name
nameFromString String
"") Int
k
grow'' :: a -> IntMap a -> VName -> IntMap a
grow'' a
v IntMap a
m'' VName
k = (a -> a -> a) -> Int -> a -> IntMap a -> IntMap a
forall a. (a -> a -> a) -> Int -> a -> IntMap a -> IntMap a
IM.insertWith a -> a -> a
forall a. Semigroup a => a -> a -> a
(<>) (VName -> Int
baseTag VName
k) a
v IntMap a
m''
is :: Usages -> VName -> UsageTable -> Bool
is :: Usages -> VName -> UsageTable -> Bool
is = (Usages -> Bool) -> VName -> UsageTable -> Bool
lookupPred ((Usages -> Bool) -> VName -> UsageTable -> Bool)
-> (Usages -> Usages -> Bool)
-> Usages
-> VName
-> UsageTable
-> Bool
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Usages -> Usages -> Bool
matches
isConsumed :: VName -> UsageTable -> Bool
isConsumed :: VName -> UsageTable -> Bool
isConsumed = Usages -> VName -> UsageTable -> Bool
is Usages
consumedU
isInResult :: VName -> UsageTable -> Bool
isInResult :: VName -> UsageTable -> Bool
isInResult = Usages -> VName -> UsageTable -> Bool
is Usages
inResultU
isUsedDirectly :: VName -> UsageTable -> Bool
isUsedDirectly :: VName -> UsageTable -> Bool
isUsedDirectly = Usages -> VName -> UsageTable -> Bool
is Usages
presentU
isSize :: VName -> UsageTable -> Bool
isSize :: VName -> UsageTable -> Bool
isSize = Usages -> VName -> UsageTable -> Bool
is Usages
sizeU
usages :: Names -> UsageTable
usages :: Names -> UsageTable
usages = IntMap Usages -> UsageTable
UsageTable (IntMap Usages -> UsageTable)
-> (Names -> IntMap Usages) -> Names -> UsageTable
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (VName -> Usages) -> IntMap VName -> IntMap Usages
forall a b. (a -> b) -> IntMap a -> IntMap b
IM.map (Usages -> VName -> Usages
forall a b. a -> b -> a
const Usages
presentU) (IntMap VName -> IntMap Usages)
-> (Names -> IntMap VName) -> Names -> IntMap Usages
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Names -> IntMap VName
namesIntMap
usage :: VName -> Usages -> UsageTable
usage :: VName -> Usages -> UsageTable
usage VName
name Usages
uses = IntMap Usages -> UsageTable
UsageTable (IntMap Usages -> UsageTable) -> IntMap Usages -> UsageTable
forall a b. (a -> b) -> a -> b
$ Int -> Usages -> IntMap Usages
forall a. Int -> a -> IntMap a
IM.singleton (VName -> Int
baseTag VName
name) Usages
uses
consumedUsage :: VName -> UsageTable
consumedUsage :: VName -> UsageTable
consumedUsage VName
name = IntMap Usages -> UsageTable
UsageTable (IntMap Usages -> UsageTable) -> IntMap Usages -> UsageTable
forall a b. (a -> b) -> a -> b
$ Int -> Usages -> IntMap Usages
forall a. Int -> a -> IntMap a
IM.singleton (VName -> Int
baseTag VName
name) Usages
consumedU
inResultUsage :: VName -> UsageTable
inResultUsage :: VName -> UsageTable
inResultUsage VName
name = IntMap Usages -> UsageTable
UsageTable (IntMap Usages -> UsageTable) -> IntMap Usages -> UsageTable
forall a b. (a -> b) -> a -> b
$ Int -> Usages -> IntMap Usages
forall a. Int -> a -> IntMap a
IM.singleton (VName -> Int
baseTag VName
name) Usages
inResultU
sizeUsage :: VName -> UsageTable
sizeUsage :: VName -> UsageTable
sizeUsage VName
name = IntMap Usages -> UsageTable
UsageTable (IntMap Usages -> UsageTable) -> IntMap Usages -> UsageTable
forall a b. (a -> b) -> a -> b
$ Int -> Usages -> IntMap Usages
forall a. Int -> a -> IntMap a
IM.singleton (VName -> Int
baseTag VName
name) Usages
sizeU
sizeUsages :: Names -> UsageTable
sizeUsages :: Names -> UsageTable
sizeUsages = IntMap Usages -> UsageTable
UsageTable (IntMap Usages -> UsageTable)
-> (Names -> IntMap Usages) -> Names -> UsageTable
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (VName -> Usages) -> IntMap VName -> IntMap Usages
forall a b. (a -> b) -> IntMap a -> IntMap b
IM.map (Usages -> VName -> Usages
forall a b. a -> b -> a
const Usages
sizeU) (IntMap VName -> IntMap Usages)
-> (Names -> IntMap VName) -> Names -> IntMap Usages
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Names -> IntMap VName
namesIntMap
newtype Usages = Usages Int
deriving (Usages -> Usages -> Bool
(Usages -> Usages -> Bool)
-> (Usages -> Usages -> Bool) -> Eq Usages
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Usages -> Usages -> Bool
$c/= :: Usages -> Usages -> Bool
== :: Usages -> Usages -> Bool
$c== :: Usages -> Usages -> Bool
Eq, Eq Usages
Eq Usages
-> (Usages -> Usages -> Ordering)
-> (Usages -> Usages -> Bool)
-> (Usages -> Usages -> Bool)
-> (Usages -> Usages -> Bool)
-> (Usages -> Usages -> Bool)
-> (Usages -> Usages -> Usages)
-> (Usages -> Usages -> Usages)
-> Ord Usages
Usages -> Usages -> Bool
Usages -> Usages -> Ordering
Usages -> Usages -> Usages
forall a.
Eq a
-> (a -> a -> Ordering)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> Bool)
-> (a -> a -> a)
-> (a -> a -> a)
-> Ord a
min :: Usages -> Usages -> Usages
$cmin :: Usages -> Usages -> Usages
max :: Usages -> Usages -> Usages
$cmax :: Usages -> Usages -> Usages
>= :: Usages -> Usages -> Bool
$c>= :: Usages -> Usages -> Bool
> :: Usages -> Usages -> Bool
$c> :: Usages -> Usages -> Bool
<= :: Usages -> Usages -> Bool
$c<= :: Usages -> Usages -> Bool
< :: Usages -> Usages -> Bool
$c< :: Usages -> Usages -> Bool
compare :: Usages -> Usages -> Ordering
$ccompare :: Usages -> Usages -> Ordering
Ord, Int -> Usages -> ShowS
[Usages] -> ShowS
Usages -> String
(Int -> Usages -> ShowS)
-> (Usages -> String) -> ([Usages] -> ShowS) -> Show Usages
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Usages] -> ShowS
$cshowList :: [Usages] -> ShowS
show :: Usages -> String
$cshow :: Usages -> String
showsPrec :: Int -> Usages -> ShowS
$cshowsPrec :: Int -> Usages -> ShowS
Show)
instance Semigroup Usages where
Usages Int
x <> :: Usages -> Usages -> Usages
<> Usages Int
y = Int -> Usages
Usages (Int -> Usages) -> Int -> Usages
forall a b. (a -> b) -> a -> b
$ Int
x Int -> Int -> Int
forall a. Bits a => a -> a -> a
.|. Int
y
instance Monoid Usages where
mempty :: Usages
mempty = Int -> Usages
Usages Int
0
consumedU, inResultU, presentU, sizeU :: Usages
consumedU :: Usages
consumedU = Int -> Usages
Usages Int
1
inResultU :: Usages
inResultU = Int -> Usages
Usages Int
2
presentU :: Usages
presentU = Int -> Usages
Usages Int
4
sizeU :: Usages
sizeU = Int -> Usages
Usages Int
8
matches :: Usages -> Usages -> Bool
matches :: Usages -> Usages -> Bool
matches (Usages Int
x) (Usages Int
y) = Int
x Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
== (Int
x Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. Int
y)
withoutU :: Usages -> Usages -> Usages
withoutU :: Usages -> Usages -> Usages
withoutU (Usages Int
x) (Usages Int
y) = Int -> Usages
Usages (Int -> Usages) -> Int -> Usages
forall a b. (a -> b) -> a -> b
$ Int
x Int -> Int -> Int
forall a. Bits a => a -> a -> a
.&. Int -> Int
forall a. Bits a => a -> a
complement Int
y
usageInStm :: (ASTLore lore, Aliased lore) => Stm lore -> UsageTable
usageInStm :: forall lore. (ASTLore lore, Aliased lore) => Stm lore -> UsageTable
usageInStm (Let Pattern lore
pat StmAux (ExpDec lore)
lore Exp lore
e) =
[UsageTable] -> UsageTable
forall a. Monoid a => [a] -> a
mconcat
[ UsageTable
usageInPat,
UsageTable
usageInExpLore,
Exp lore -> UsageTable
forall lore. Aliased lore => Exp lore -> UsageTable
usageInExp Exp lore
e,
Names -> UsageTable
usages (Exp lore -> Names
forall a. FreeIn a => a -> Names
freeIn Exp lore
e)
]
where
usageInPat :: UsageTable
usageInPat =
Names -> UsageTable
usages
( [Names] -> Names
forall a. Monoid a => [a] -> a
mconcat ((PatElemT (LetDec lore) -> Names)
-> [PatElemT (LetDec lore)] -> [Names]
forall a b. (a -> b) -> [a] -> [b]
map PatElemT (LetDec lore) -> Names
forall a. FreeIn a => a -> Names
freeIn ([PatElemT (LetDec lore)] -> [Names])
-> [PatElemT (LetDec lore)] -> [Names]
forall a b. (a -> b) -> a -> b
$ Pattern lore -> [PatElemT (LetDec lore)]
forall dec. PatternT dec -> [PatElemT dec]
patternElements Pattern lore
pat)
Names -> Names -> Names
`namesSubtract` [VName] -> Names
namesFromList (Pattern lore -> [VName]
forall dec. PatternT dec -> [VName]
patternNames Pattern lore
pat)
)
UsageTable -> UsageTable -> UsageTable
forall a. Semigroup a => a -> a -> a
<> Names -> UsageTable
sizeUsages ((PatElemT (LetDec lore) -> Names)
-> [PatElemT (LetDec lore)] -> Names
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap (Type -> Names
forall a. FreeIn a => a -> Names
freeIn (Type -> Names)
-> (PatElemT (LetDec lore) -> Type)
-> PatElemT (LetDec lore)
-> Names
forall b c a. (b -> c) -> (a -> b) -> a -> c
. PatElemT (LetDec lore) -> Type
forall dec. Typed dec => PatElemT dec -> Type
patElemType) (Pattern lore -> [PatElemT (LetDec lore)]
forall dec. PatternT dec -> [PatElemT dec]
patternElements Pattern lore
pat))
usageInExpLore :: UsageTable
usageInExpLore =
Names -> UsageTable
usages (Names -> UsageTable) -> Names -> UsageTable
forall a b. (a -> b) -> a -> b
$ StmAux (ExpDec lore) -> Names
forall a. FreeIn a => a -> Names
freeIn StmAux (ExpDec lore)
lore
usageInExp :: Aliased lore => Exp lore -> UsageTable
usageInExp :: forall lore. Aliased lore => Exp lore -> UsageTable
usageInExp (Apply Name
_ [(SubExp, Diet)]
args [RetType lore]
_ (Safety, SrcLoc, [SrcLoc])
_) =
[UsageTable] -> UsageTable
forall a. Monoid a => [a] -> a
mconcat
[ [UsageTable] -> UsageTable
forall a. Monoid a => [a] -> a
mconcat ([UsageTable] -> UsageTable) -> [UsageTable] -> UsageTable
forall a b. (a -> b) -> a -> b
$
(VName -> UsageTable) -> [VName] -> [UsageTable]
forall a b. (a -> b) -> [a] -> [b]
map VName -> UsageTable
consumedUsage ([VName] -> [UsageTable]) -> [VName] -> [UsageTable]
forall a b. (a -> b) -> a -> b
$
Names -> [VName]
namesToList (Names -> [VName]) -> Names -> [VName]
forall a b. (a -> b) -> a -> b
$ SubExp -> Names
subExpAliases SubExp
arg
| (SubExp
arg, Diet
d) <- [(SubExp, Diet)]
args,
Diet
d Diet -> Diet -> Bool
forall a. Eq a => a -> a -> Bool
== Diet
Consume
]
usageInExp (DoLoop [(FParam lore, SubExp)]
_ [(FParam lore, SubExp)]
merge LoopForm lore
_ BodyT lore
_) =
[UsageTable] -> UsageTable
forall a. Monoid a => [a] -> a
mconcat
[ [UsageTable] -> UsageTable
forall a. Monoid a => [a] -> a
mconcat ([UsageTable] -> UsageTable) -> [UsageTable] -> UsageTable
forall a b. (a -> b) -> a -> b
$
(VName -> UsageTable) -> [VName] -> [UsageTable]
forall a b. (a -> b) -> [a] -> [b]
map VName -> UsageTable
consumedUsage ([VName] -> [UsageTable]) -> [VName] -> [UsageTable]
forall a b. (a -> b) -> a -> b
$
Names -> [VName]
namesToList (Names -> [VName]) -> Names -> [VName]
forall a b. (a -> b) -> a -> b
$ SubExp -> Names
subExpAliases SubExp
se
| (FParam lore
v, SubExp
se) <- [(FParam lore, SubExp)]
merge,
TypeBase (ShapeBase SubExp) Uniqueness -> Bool
forall shape. TypeBase shape Uniqueness -> Bool
unique (TypeBase (ShapeBase SubExp) Uniqueness -> Bool)
-> TypeBase (ShapeBase SubExp) Uniqueness -> Bool
forall a b. (a -> b) -> a -> b
$ FParam lore -> TypeBase (ShapeBase SubExp) Uniqueness
forall dec.
DeclTyped dec =>
Param dec -> TypeBase (ShapeBase SubExp) Uniqueness
paramDeclType FParam lore
v
]
usageInExp (If SubExp
_ BodyT lore
tbranch BodyT lore
fbranch IfDec (BranchType lore)
_) =
(VName -> UsageTable) -> [VName] -> UsageTable
forall (t :: * -> *) m a.
(Foldable t, Monoid m) =>
(a -> m) -> t a -> m
foldMap VName -> UsageTable
consumedUsage ([VName] -> UsageTable) -> [VName] -> UsageTable
forall a b. (a -> b) -> a -> b
$
Names -> [VName]
namesToList (Names -> [VName]) -> Names -> [VName]
forall a b. (a -> b) -> a -> b
$
BodyT lore -> Names
forall lore. Aliased lore => Body lore -> Names
consumedInBody BodyT lore
tbranch Names -> Names -> Names
forall a. Semigroup a => a -> a -> a
<> BodyT lore -> Names
forall lore. Aliased lore => Body lore -> Names
consumedInBody BodyT lore
fbranch
usageInExp (BasicOp (Update VName
src Slice SubExp
_ SubExp
_)) =
VName -> UsageTable
consumedUsage VName
src
usageInExp (Op Op lore
op) =
[UsageTable] -> UsageTable
forall a. Monoid a => [a] -> a
mconcat ([UsageTable] -> UsageTable) -> [UsageTable] -> UsageTable
forall a b. (a -> b) -> a -> b
$ (VName -> UsageTable) -> [VName] -> [UsageTable]
forall a b. (a -> b) -> [a] -> [b]
map VName -> UsageTable
consumedUsage (Names -> [VName]
namesToList (Names -> [VName]) -> Names -> [VName]
forall a b. (a -> b) -> a -> b
$ Op lore -> Names
forall op. AliasedOp op => op -> Names
consumedInOp Op lore
op)
usageInExp ExpT lore
_ = UsageTable
forall a. Monoid a => a
mempty