module DDC.Llvm.Analysis.Defs
( Def (..)
, takeDefOfInstr
, defsOfBlock)
where
import DDC.Llvm.Syntax
import Data.Map (Map)
import qualified Data.Foldable as Seq
import qualified Data.Map as Map
data Def
= DefVar
| DefAlias Var
| DefClosedConstant Exp
deriving Show
defsOfBlock :: Block -> Map Var (Label, Def)
defsOfBlock block
= Map.fromList
$ [ (v, (blockLabel block, def))
| Just (v, def) <- map (takeDefOfInstr . annotInstr)
$ Seq.toList $ blockInstrs block ]
takeDefOfInstr :: Instr -> Maybe (Var, Def)
takeDefOfInstr instr
= case instr of
IComment{}
-> Nothing
ISet v1 x2
| XVar v2 <- x2
-> Just (v1, DefAlias v2)
| isClosedConstantExp x2
-> Just (v1, DefClosedConstant x2)
| otherwise
-> Just (v1, DefVar)
INop -> Nothing
IPhi v _ -> Just (v, DefVar)
IReturn{} -> Nothing
IBranch{} -> Nothing
IBranchIf{} -> Nothing
ISwitch{} -> Nothing
IUnreachable{} -> Nothing
IOp v _ _ _ -> Just (v, DefVar)
IConv v _ _ -> Just (v, DefVar)
IGet v _ _ -> Just (v, DefVar)
ILoad v _ -> Just (v, DefVar)
IStore{} -> Nothing
ICmp v _ _ _ -> Just (v, DefVar)
ICall mv _ _ _ _ _ _
-> case mv of
Just v -> Just (v, DefVar)
_ -> Nothing