module CmmMachOp
    ( MachOp(..)
    , pprMachOp, isCommutableMachOp, isAssociativeMachOp
    , isComparisonMachOp, maybeIntComparison, machOpResultType
    , machOpArgReps, maybeInvertComparison, isFloatComparison

    -- MachOp builders
    , mo_wordAdd, mo_wordSub, mo_wordEq, mo_wordNe,mo_wordMul, mo_wordSQuot
    , mo_wordSRem, mo_wordSNeg, mo_wordUQuot, mo_wordURem
    , mo_wordSGe, mo_wordSLe, mo_wordSGt, mo_wordSLt, mo_wordUGe
    , mo_wordULe, mo_wordUGt, mo_wordULt
    , mo_wordAnd, mo_wordOr, mo_wordXor, mo_wordNot
    , mo_wordShl, mo_wordSShr, mo_wordUShr
    , mo_u_8To32, mo_s_8To32, mo_u_16To32, mo_s_16To32
    , mo_u_8ToWord, mo_s_8ToWord, mo_u_16ToWord, mo_s_16ToWord
    , mo_u_32ToWord, mo_s_32ToWord
    , mo_32To8, mo_32To16, mo_WordTo8, mo_WordTo16, mo_WordTo32, mo_WordTo64

    -- CallishMachOp
    , CallishMachOp(..), callishMachOpHints
    , pprCallishMachOp
    , machOpMemcpyishAlign

    -- Atomic read-modify-write
    , AtomicMachOp(..)
   )
where

import GhcPrelude

import CmmType
import Outputable
import DynFlags

-----------------------------------------------------------------------------
--              MachOp
-----------------------------------------------------------------------------

{- |
Machine-level primops; ones which we can reasonably delegate to the
native code generators to handle.

Most operations are parameterised by the 'Width' that they operate on.
Some operations have separate signed and unsigned versions, and float
and integer versions.
-}

data MachOp
  -- Integer operations (insensitive to signed/unsigned)
  = MO_Add Width
  | MO_Sub Width
  | MO_Eq  Width
  | MO_Ne  Width
  | MO_Mul Width                -- low word of multiply

  -- Signed multiply/divide
  | MO_S_MulMayOflo Width       -- nonzero if signed multiply overflows
  | MO_S_Quot Width             -- signed / (same semantics as IntQuotOp)
  | MO_S_Rem  Width             -- signed % (same semantics as IntRemOp)
  | MO_S_Neg  Width             -- unary -

  -- Unsigned multiply/divide
  | MO_U_MulMayOflo Width       -- nonzero if unsigned multiply overflows
  | MO_U_Quot Width             -- unsigned / (same semantics as WordQuotOp)
  | MO_U_Rem  Width             -- unsigned % (same semantics as WordRemOp)

  -- Signed comparisons
  | MO_S_Ge Width
  | MO_S_Le Width
  | MO_S_Gt Width
  | MO_S_Lt Width

  -- Unsigned comparisons
  | MO_U_Ge Width
  | MO_U_Le Width
  | MO_U_Gt Width
  | MO_U_Lt Width

  -- Floating point arithmetic
  | MO_F_Add  Width
  | MO_F_Sub  Width
  | MO_F_Neg  Width             -- unary -
  | MO_F_Mul  Width
  | MO_F_Quot Width

  -- Floating point comparison
  | MO_F_Eq Width
  | MO_F_Ne Width
  | MO_F_Ge Width
  | MO_F_Le Width
  | MO_F_Gt Width
  | MO_F_Lt Width

  -- Bitwise operations.  Not all of these may be supported
  -- at all sizes, and only integral Widths are valid.
  | MO_And   Width
  | MO_Or    Width
  | MO_Xor   Width
  | MO_Not   Width
  | MO_Shl   Width
  | MO_U_Shr Width      -- unsigned shift right
  | MO_S_Shr Width      -- signed shift right

  -- Conversions.  Some of these will be NOPs.
  -- Floating-point conversions use the signed variant.
  | MO_SF_Conv Width Width      -- Signed int -> Float
  | MO_FS_Conv Width Width      -- Float -> Signed int
  | MO_SS_Conv Width Width      -- Signed int -> Signed int
  | MO_UU_Conv Width Width      -- unsigned int -> unsigned int
  | MO_XX_Conv Width Width      -- int -> int; puts no requirements on the
                                -- contents of upper bits when extending;
                                -- narrowing is simply truncation; the only
                                -- expectation is that we can recover the
                                -- original value by applying the opposite
                                -- MO_XX_Conv, e.g.,
                                --   MO_XX_CONV W64 W8 (MO_XX_CONV W8 W64 x)
                                -- is equivalent to just x.
  | MO_FF_Conv Width Width      -- Float -> Float

  -- Vector element insertion and extraction operations
  | MO_V_Insert  Length Width   -- Insert scalar into vector
  | MO_V_Extract Length Width   -- Extract scalar from vector

  -- Integer vector operations
  | MO_V_Add Length Width
  | MO_V_Sub Length Width
  | MO_V_Mul Length Width

  -- Signed vector multiply/divide
  | MO_VS_Quot Length Width
  | MO_VS_Rem  Length Width
  | MO_VS_Neg  Length Width

  -- Unsigned vector multiply/divide
  | MO_VU_Quot Length Width
  | MO_VU_Rem  Length Width

  -- Floting point vector element insertion and extraction operations
  | MO_VF_Insert  Length Width   -- Insert scalar into vector
  | MO_VF_Extract Length Width   -- Extract scalar from vector

  -- Floating point vector operations
  | MO_VF_Add  Length Width
  | MO_VF_Sub  Length Width
  | MO_VF_Neg  Length Width      -- unary negation
  | MO_VF_Mul  Length Width
  | MO_VF_Quot Length Width

  -- Alignment check (for -falignment-sanitisation)
  | MO_AlignmentCheck Int Width
  deriving (MachOp -> MachOp -> Bool
(MachOp -> MachOp -> Bool)
-> (MachOp -> MachOp -> Bool) -> Eq MachOp
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: MachOp -> MachOp -> Bool
$c/= :: MachOp -> MachOp -> Bool
== :: MachOp -> MachOp -> Bool
$c== :: MachOp -> MachOp -> Bool
Eq, Int -> MachOp -> ShowS
[MachOp] -> ShowS
MachOp -> String
(Int -> MachOp -> ShowS)
-> (MachOp -> String) -> ([MachOp] -> ShowS) -> Show MachOp
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [MachOp] -> ShowS
$cshowList :: [MachOp] -> ShowS
show :: MachOp -> String
$cshow :: MachOp -> String
showsPrec :: Int -> MachOp -> ShowS
$cshowsPrec :: Int -> MachOp -> ShowS
Show)

pprMachOp :: MachOp -> SDoc
pprMachOp :: MachOp -> SDoc
pprMachOp mo :: MachOp
mo = String -> SDoc
text (MachOp -> String
forall a. Show a => a -> String
show MachOp
mo)



-- -----------------------------------------------------------------------------
-- Some common MachReps

-- A 'wordRep' is a machine word on the target architecture
-- Specifically, it is the size of an Int#, Word#, Addr#
-- and the unit of allocation on the stack and the heap
-- Any pointer is also guaranteed to be a wordRep.

mo_wordAdd, mo_wordSub, mo_wordEq, mo_wordNe,mo_wordMul, mo_wordSQuot
    , mo_wordSRem, mo_wordSNeg, mo_wordUQuot, mo_wordURem
    , mo_wordSGe, mo_wordSLe, mo_wordSGt, mo_wordSLt, mo_wordUGe
    , mo_wordULe, mo_wordUGt, mo_wordULt
    , mo_wordAnd, mo_wordOr, mo_wordXor, mo_wordNot, mo_wordShl, mo_wordSShr, mo_wordUShr
    , mo_u_8ToWord, mo_s_8ToWord, mo_u_16ToWord, mo_s_16ToWord, mo_u_32ToWord, mo_s_32ToWord
    , mo_WordTo8, mo_WordTo16, mo_WordTo32, mo_WordTo64
    :: DynFlags -> MachOp

mo_u_8To32, mo_s_8To32, mo_u_16To32, mo_s_16To32
    , mo_32To8, mo_32To16
    :: MachOp

mo_wordAdd :: DynFlags -> MachOp
mo_wordAdd      dflags :: DynFlags
dflags = Width -> MachOp
MO_Add (DynFlags -> Width
wordWidth DynFlags
dflags)
mo_wordSub :: DynFlags -> MachOp
mo_wordSub      dflags :: DynFlags
dflags = Width -> MachOp
MO_Sub (DynFlags -> Width
wordWidth DynFlags
dflags)
mo_wordEq :: DynFlags -> MachOp
mo_wordEq       dflags :: DynFlags
dflags = Width -> MachOp
MO_Eq  (DynFlags -> Width
wordWidth DynFlags
dflags)
mo_wordNe :: DynFlags -> MachOp
mo_wordNe       dflags :: DynFlags
dflags = Width -> MachOp
MO_Ne  (DynFlags -> Width
wordWidth DynFlags
dflags)
mo_wordMul :: DynFlags -> MachOp
mo_wordMul      dflags :: DynFlags
dflags = Width -> MachOp
MO_Mul (DynFlags -> Width
wordWidth DynFlags
dflags)
mo_wordSQuot :: DynFlags -> MachOp
mo_wordSQuot    dflags :: DynFlags
dflags = Width -> MachOp
MO_S_Quot (DynFlags -> Width
wordWidth DynFlags
dflags)
mo_wordSRem :: DynFlags -> MachOp
mo_wordSRem     dflags :: DynFlags
dflags = Width -> MachOp
MO_S_Rem (DynFlags -> Width
wordWidth DynFlags
dflags)
mo_wordSNeg :: DynFlags -> MachOp
mo_wordSNeg     dflags :: DynFlags
dflags = Width -> MachOp
MO_S_Neg (DynFlags -> Width
wordWidth DynFlags
dflags)
mo_wordUQuot :: DynFlags -> MachOp
mo_wordUQuot    dflags :: DynFlags
dflags = Width -> MachOp
MO_U_Quot (DynFlags -> Width
wordWidth DynFlags
dflags)
mo_wordURem :: DynFlags -> MachOp
mo_wordURem     dflags :: DynFlags
dflags = Width -> MachOp
MO_U_Rem (DynFlags -> Width
wordWidth DynFlags
dflags)

mo_wordSGe :: DynFlags -> MachOp
mo_wordSGe      dflags :: DynFlags
dflags = Width -> MachOp
MO_S_Ge  (DynFlags -> Width
wordWidth DynFlags
dflags)
mo_wordSLe :: DynFlags -> MachOp
mo_wordSLe      dflags :: DynFlags
dflags = Width -> MachOp
MO_S_Le  (DynFlags -> Width
wordWidth DynFlags
dflags)
mo_wordSGt :: DynFlags -> MachOp
mo_wordSGt      dflags :: DynFlags
dflags = Width -> MachOp
MO_S_Gt  (DynFlags -> Width
wordWidth DynFlags
dflags)
mo_wordSLt :: DynFlags -> MachOp
mo_wordSLt      dflags :: DynFlags
dflags = Width -> MachOp
MO_S_Lt  (DynFlags -> Width
wordWidth DynFlags
dflags)

mo_wordUGe :: DynFlags -> MachOp
mo_wordUGe      dflags :: DynFlags
dflags = Width -> MachOp
MO_U_Ge  (DynFlags -> Width
wordWidth DynFlags
dflags)
mo_wordULe :: DynFlags -> MachOp
mo_wordULe      dflags :: DynFlags
dflags = Width -> MachOp
MO_U_Le  (DynFlags -> Width
wordWidth DynFlags
dflags)
mo_wordUGt :: DynFlags -> MachOp
mo_wordUGt      dflags :: DynFlags
dflags = Width -> MachOp
MO_U_Gt  (DynFlags -> Width
wordWidth DynFlags
dflags)
mo_wordULt :: DynFlags -> MachOp
mo_wordULt      dflags :: DynFlags
dflags = Width -> MachOp
MO_U_Lt  (DynFlags -> Width
wordWidth DynFlags
dflags)

mo_wordAnd :: DynFlags -> MachOp
mo_wordAnd      dflags :: DynFlags
dflags = Width -> MachOp
MO_And (DynFlags -> Width
wordWidth DynFlags
dflags)
mo_wordOr :: DynFlags -> MachOp
mo_wordOr       dflags :: DynFlags
dflags = Width -> MachOp
MO_Or  (DynFlags -> Width
wordWidth DynFlags
dflags)
mo_wordXor :: DynFlags -> MachOp
mo_wordXor      dflags :: DynFlags
dflags = Width -> MachOp
MO_Xor (DynFlags -> Width
wordWidth DynFlags
dflags)
mo_wordNot :: DynFlags -> MachOp
mo_wordNot      dflags :: DynFlags
dflags = Width -> MachOp
MO_Not (DynFlags -> Width
wordWidth DynFlags
dflags)
mo_wordShl :: DynFlags -> MachOp
mo_wordShl      dflags :: DynFlags
dflags = Width -> MachOp
MO_Shl (DynFlags -> Width
wordWidth DynFlags
dflags)
mo_wordSShr :: DynFlags -> MachOp
mo_wordSShr     dflags :: DynFlags
dflags = Width -> MachOp
MO_S_Shr (DynFlags -> Width
wordWidth DynFlags
dflags)
mo_wordUShr :: DynFlags -> MachOp
mo_wordUShr     dflags :: DynFlags
dflags = Width -> MachOp
MO_U_Shr (DynFlags -> Width
wordWidth DynFlags
dflags)

mo_u_8To32 :: MachOp
mo_u_8To32             = Width -> Width -> MachOp
MO_UU_Conv Width
W8 Width
W32
mo_s_8To32 :: MachOp
mo_s_8To32             = Width -> Width -> MachOp
MO_SS_Conv Width
W8 Width
W32
mo_u_16To32 :: MachOp
mo_u_16To32            = Width -> Width -> MachOp
MO_UU_Conv Width
W16 Width
W32
mo_s_16To32 :: MachOp
mo_s_16To32            = Width -> Width -> MachOp
MO_SS_Conv Width
W16 Width
W32

mo_u_8ToWord :: DynFlags -> MachOp
mo_u_8ToWord    dflags :: DynFlags
dflags = Width -> Width -> MachOp
MO_UU_Conv Width
W8  (DynFlags -> Width
wordWidth DynFlags
dflags)
mo_s_8ToWord :: DynFlags -> MachOp
mo_s_8ToWord    dflags :: DynFlags
dflags = Width -> Width -> MachOp
MO_SS_Conv Width
W8  (DynFlags -> Width
wordWidth DynFlags
dflags)
mo_u_16ToWord :: DynFlags -> MachOp
mo_u_16ToWord   dflags :: DynFlags
dflags = Width -> Width -> MachOp
MO_UU_Conv Width
W16 (DynFlags -> Width
wordWidth DynFlags
dflags)
mo_s_16ToWord :: DynFlags -> MachOp
mo_s_16ToWord   dflags :: DynFlags
dflags = Width -> Width -> MachOp
MO_SS_Conv Width
W16 (DynFlags -> Width
wordWidth DynFlags
dflags)
mo_s_32ToWord :: DynFlags -> MachOp
mo_s_32ToWord   dflags :: DynFlags
dflags = Width -> Width -> MachOp
MO_SS_Conv Width
W32 (DynFlags -> Width
wordWidth DynFlags
dflags)
mo_u_32ToWord :: DynFlags -> MachOp
mo_u_32ToWord   dflags :: DynFlags
dflags = Width -> Width -> MachOp
MO_UU_Conv Width
W32 (DynFlags -> Width
wordWidth DynFlags
dflags)

mo_WordTo8 :: DynFlags -> MachOp
mo_WordTo8      dflags :: DynFlags
dflags = Width -> Width -> MachOp
MO_UU_Conv (DynFlags -> Width
wordWidth DynFlags
dflags) Width
W8
mo_WordTo16 :: DynFlags -> MachOp
mo_WordTo16     dflags :: DynFlags
dflags = Width -> Width -> MachOp
MO_UU_Conv (DynFlags -> Width
wordWidth DynFlags
dflags) Width
W16
mo_WordTo32 :: DynFlags -> MachOp
mo_WordTo32     dflags :: DynFlags
dflags = Width -> Width -> MachOp
MO_UU_Conv (DynFlags -> Width
wordWidth DynFlags
dflags) Width
W32
mo_WordTo64 :: DynFlags -> MachOp
mo_WordTo64     dflags :: DynFlags
dflags = Width -> Width -> MachOp
MO_UU_Conv (DynFlags -> Width
wordWidth DynFlags
dflags) Width
W64

mo_32To8 :: MachOp
mo_32To8               = Width -> Width -> MachOp
MO_UU_Conv Width
W32 Width
W8
mo_32To16 :: MachOp
mo_32To16              = Width -> Width -> MachOp
MO_UU_Conv Width
W32 Width
W16


-- ----------------------------------------------------------------------------
-- isCommutableMachOp

{- |
Returns 'True' if the MachOp has commutable arguments.  This is used
in the platform-independent Cmm optimisations.

If in doubt, return 'False'.  This generates worse code on the
native routes, but is otherwise harmless.
-}
isCommutableMachOp :: MachOp -> Bool
isCommutableMachOp :: MachOp -> Bool
isCommutableMachOp mop :: MachOp
mop =
  case MachOp
mop of
        MO_Add _                -> Bool
True
        MO_Eq _                 -> Bool
True
        MO_Ne _                 -> Bool
True
        MO_Mul _                -> Bool
True
        MO_S_MulMayOflo _       -> Bool
True
        MO_U_MulMayOflo _       -> Bool
True
        MO_And _                -> Bool
True
        MO_Or _                 -> Bool
True
        MO_Xor _                -> Bool
True
        MO_F_Add _              -> Bool
True
        MO_F_Mul _              -> Bool
True
        _other :: MachOp
_other                  -> Bool
False

-- ----------------------------------------------------------------------------
-- isAssociativeMachOp

{- |
Returns 'True' if the MachOp is associative (i.e. @(x+y)+z == x+(y+z)@)
This is used in the platform-independent Cmm optimisations.

If in doubt, return 'False'.  This generates worse code on the
native routes, but is otherwise harmless.
-}
isAssociativeMachOp :: MachOp -> Bool
isAssociativeMachOp :: MachOp -> Bool
isAssociativeMachOp mop :: MachOp
mop =
  case MachOp
mop of
        MO_Add {} -> Bool
True       -- NB: does not include
        MO_Mul {} -> Bool
True --     floatint point!
        MO_And {} -> Bool
True
        MO_Or  {} -> Bool
True
        MO_Xor {} -> Bool
True
        _other :: MachOp
_other    -> Bool
False


-- ----------------------------------------------------------------------------
-- isComparisonMachOp

{- |
Returns 'True' if the MachOp is a comparison.

If in doubt, return False.  This generates worse code on the
native routes, but is otherwise harmless.
-}
isComparisonMachOp :: MachOp -> Bool
isComparisonMachOp :: MachOp -> Bool
isComparisonMachOp mop :: MachOp
mop =
  case MachOp
mop of
    MO_Eq   _  -> Bool
True
    MO_Ne   _  -> Bool
True
    MO_S_Ge _  -> Bool
True
    MO_S_Le _  -> Bool
True
    MO_S_Gt _  -> Bool
True
    MO_S_Lt _  -> Bool
True
    MO_U_Ge _  -> Bool
True
    MO_U_Le _  -> Bool
True
    MO_U_Gt _  -> Bool
True
    MO_U_Lt _  -> Bool
True
    MO_F_Eq {} -> Bool
True
    MO_F_Ne {} -> Bool
True
    MO_F_Ge {} -> Bool
True
    MO_F_Le {} -> Bool
True
    MO_F_Gt {} -> Bool
True
    MO_F_Lt {} -> Bool
True
    _other :: MachOp
_other     -> Bool
False

{- |
Returns @Just w@ if the operation is an integer comparison with width
@w@, or @Nothing@ otherwise.
-}
maybeIntComparison :: MachOp -> Maybe Width
maybeIntComparison :: MachOp -> Maybe Width
maybeIntComparison mop :: MachOp
mop =
  case MachOp
mop of
    MO_Eq   w :: Width
w  -> Width -> Maybe Width
forall a. a -> Maybe a
Just Width
w
    MO_Ne   w :: Width
w  -> Width -> Maybe Width
forall a. a -> Maybe a
Just Width
w
    MO_S_Ge w :: Width
w  -> Width -> Maybe Width
forall a. a -> Maybe a
Just Width
w
    MO_S_Le w :: Width
w  -> Width -> Maybe Width
forall a. a -> Maybe a
Just Width
w
    MO_S_Gt w :: Width
w  -> Width -> Maybe Width
forall a. a -> Maybe a
Just Width
w
    MO_S_Lt w :: Width
w  -> Width -> Maybe Width
forall a. a -> Maybe a
Just Width
w
    MO_U_Ge w :: Width
w  -> Width -> Maybe Width
forall a. a -> Maybe a
Just Width
w
    MO_U_Le w :: Width
w  -> Width -> Maybe Width
forall a. a -> Maybe a
Just Width
w
    MO_U_Gt w :: Width
w  -> Width -> Maybe Width
forall a. a -> Maybe a
Just Width
w
    MO_U_Lt w :: Width
w  -> Width -> Maybe Width
forall a. a -> Maybe a
Just Width
w
    _ -> Maybe Width
forall a. Maybe a
Nothing

isFloatComparison :: MachOp -> Bool
isFloatComparison :: MachOp -> Bool
isFloatComparison mop :: MachOp
mop =
  case MachOp
mop of
    MO_F_Eq {} -> Bool
True
    MO_F_Ne {} -> Bool
True
    MO_F_Ge {} -> Bool
True
    MO_F_Le {} -> Bool
True
    MO_F_Gt {} -> Bool
True
    MO_F_Lt {} -> Bool
True
    _other :: MachOp
_other     -> Bool
False

-- -----------------------------------------------------------------------------
-- Inverting conditions

-- Sometimes it's useful to be able to invert the sense of a
-- condition.  Not all conditional tests are invertible: in
-- particular, floating point conditionals cannot be inverted, because
-- there exist floating-point values which return False for both senses
-- of a condition (eg. !(NaN > NaN) && !(NaN /<= NaN)).

maybeInvertComparison :: MachOp -> Maybe MachOp
maybeInvertComparison :: MachOp -> Maybe MachOp
maybeInvertComparison op :: MachOp
op
  = case MachOp
op of  -- None of these Just cases include floating point
        MO_Eq r :: Width
r   -> MachOp -> Maybe MachOp
forall a. a -> Maybe a
Just (Width -> MachOp
MO_Ne Width
r)
        MO_Ne r :: Width
r   -> MachOp -> Maybe MachOp
forall a. a -> Maybe a
Just (Width -> MachOp
MO_Eq Width
r)
        MO_U_Lt r :: Width
r -> MachOp -> Maybe MachOp
forall a. a -> Maybe a
Just (Width -> MachOp
MO_U_Ge Width
r)
        MO_U_Gt r :: Width
r -> MachOp -> Maybe MachOp
forall a. a -> Maybe a
Just (Width -> MachOp
MO_U_Le Width
r)
        MO_U_Le r :: Width
r -> MachOp -> Maybe MachOp
forall a. a -> Maybe a
Just (Width -> MachOp
MO_U_Gt Width
r)
        MO_U_Ge r :: Width
r -> MachOp -> Maybe MachOp
forall a. a -> Maybe a
Just (Width -> MachOp
MO_U_Lt Width
r)
        MO_S_Lt r :: Width
r -> MachOp -> Maybe MachOp
forall a. a -> Maybe a
Just (Width -> MachOp
MO_S_Ge Width
r)
        MO_S_Gt r :: Width
r -> MachOp -> Maybe MachOp
forall a. a -> Maybe a
Just (Width -> MachOp
MO_S_Le Width
r)
        MO_S_Le r :: Width
r -> MachOp -> Maybe MachOp
forall a. a -> Maybe a
Just (Width -> MachOp
MO_S_Gt Width
r)
        MO_S_Ge r :: Width
r -> MachOp -> Maybe MachOp
forall a. a -> Maybe a
Just (Width -> MachOp
MO_S_Lt Width
r)
        _other :: MachOp
_other    -> Maybe MachOp
forall a. Maybe a
Nothing

-- ----------------------------------------------------------------------------
-- machOpResultType

{- |
Returns the MachRep of the result of a MachOp.
-}
machOpResultType :: DynFlags -> MachOp -> [CmmType] -> CmmType
machOpResultType :: DynFlags -> MachOp -> [CmmType] -> CmmType
machOpResultType dflags :: DynFlags
dflags mop :: MachOp
mop tys :: [CmmType]
tys =
  case MachOp
mop of
    MO_Add {}           -> CmmType
ty1  -- Preserve GC-ptr-hood
    MO_Sub {}           -> CmmType
ty1  -- of first arg
    MO_Mul    r :: Width
r         -> Width -> CmmType
cmmBits Width
r
    MO_S_MulMayOflo r :: Width
r   -> Width -> CmmType
cmmBits Width
r
    MO_S_Quot r :: Width
r         -> Width -> CmmType
cmmBits Width
r
    MO_S_Rem  r :: Width
r         -> Width -> CmmType
cmmBits Width
r
    MO_S_Neg  r :: Width
r         -> Width -> CmmType
cmmBits Width
r
    MO_U_MulMayOflo r :: Width
r   -> Width -> CmmType
cmmBits Width
r
    MO_U_Quot r :: Width
r         -> Width -> CmmType
cmmBits Width
r
    MO_U_Rem  r :: Width
r         -> Width -> CmmType
cmmBits Width
r

    MO_Eq {}            -> DynFlags -> CmmType
comparisonResultRep DynFlags
dflags
    MO_Ne {}            -> DynFlags -> CmmType
comparisonResultRep DynFlags
dflags
    MO_S_Ge {}          -> DynFlags -> CmmType
comparisonResultRep DynFlags
dflags
    MO_S_Le {}          -> DynFlags -> CmmType
comparisonResultRep DynFlags
dflags
    MO_S_Gt {}          -> DynFlags -> CmmType
comparisonResultRep DynFlags
dflags
    MO_S_Lt {}          -> DynFlags -> CmmType
comparisonResultRep DynFlags
dflags

    MO_U_Ge {}          -> DynFlags -> CmmType
comparisonResultRep DynFlags
dflags
    MO_U_Le {}          -> DynFlags -> CmmType
comparisonResultRep DynFlags
dflags
    MO_U_Gt {}          -> DynFlags -> CmmType
comparisonResultRep DynFlags
dflags
    MO_U_Lt {}          -> DynFlags -> CmmType
comparisonResultRep DynFlags
dflags

    MO_F_Add r :: Width
r          -> Width -> CmmType
cmmFloat Width
r
    MO_F_Sub r :: Width
r          -> Width -> CmmType
cmmFloat Width
r
    MO_F_Mul r :: Width
r          -> Width -> CmmType
cmmFloat Width
r
    MO_F_Quot r :: Width
r         -> Width -> CmmType
cmmFloat Width
r
    MO_F_Neg r :: Width
r          -> Width -> CmmType
cmmFloat Width
r
    MO_F_Eq  {}         -> DynFlags -> CmmType
comparisonResultRep DynFlags
dflags
    MO_F_Ne  {}         -> DynFlags -> CmmType
comparisonResultRep DynFlags
dflags
    MO_F_Ge  {}         -> DynFlags -> CmmType
comparisonResultRep DynFlags
dflags
    MO_F_Le  {}         -> DynFlags -> CmmType
comparisonResultRep DynFlags
dflags
    MO_F_Gt  {}         -> DynFlags -> CmmType
comparisonResultRep DynFlags
dflags
    MO_F_Lt  {}         -> DynFlags -> CmmType
comparisonResultRep DynFlags
dflags

    MO_And {}           -> CmmType
ty1  -- Used for pointer masking
    MO_Or {}            -> CmmType
ty1
    MO_Xor {}           -> CmmType
ty1
    MO_Not   r :: Width
r          -> Width -> CmmType
cmmBits Width
r
    MO_Shl   r :: Width
r          -> Width -> CmmType
cmmBits Width
r
    MO_U_Shr r :: Width
r          -> Width -> CmmType
cmmBits Width
r
    MO_S_Shr r :: Width
r          -> Width -> CmmType
cmmBits Width
r

    MO_SS_Conv _ to :: Width
to     -> Width -> CmmType
cmmBits Width
to
    MO_UU_Conv _ to :: Width
to     -> Width -> CmmType
cmmBits Width
to
    MO_XX_Conv _ to :: Width
to     -> Width -> CmmType
cmmBits Width
to
    MO_FS_Conv _ to :: Width
to     -> Width -> CmmType
cmmBits Width
to
    MO_SF_Conv _ to :: Width
to     -> Width -> CmmType
cmmFloat Width
to
    MO_FF_Conv _ to :: Width
to     -> Width -> CmmType
cmmFloat Width
to

    MO_V_Insert  l :: Int
l w :: Width
w    -> Int -> CmmType -> CmmType
cmmVec Int
l (Width -> CmmType
cmmBits Width
w)
    MO_V_Extract _ w :: Width
w    -> Width -> CmmType
cmmBits Width
w

    MO_V_Add l :: Int
l w :: Width
w        -> Int -> CmmType -> CmmType
cmmVec Int
l (Width -> CmmType
cmmBits Width
w)
    MO_V_Sub l :: Int
l w :: Width
w        -> Int -> CmmType -> CmmType
cmmVec Int
l (Width -> CmmType
cmmBits Width
w)
    MO_V_Mul l :: Int
l w :: Width
w        -> Int -> CmmType -> CmmType
cmmVec Int
l (Width -> CmmType
cmmBits Width
w)

    MO_VS_Quot l :: Int
l w :: Width
w      -> Int -> CmmType -> CmmType
cmmVec Int
l (Width -> CmmType
cmmBits Width
w)
    MO_VS_Rem  l :: Int
l w :: Width
w      -> Int -> CmmType -> CmmType
cmmVec Int
l (Width -> CmmType
cmmBits Width
w)
    MO_VS_Neg  l :: Int
l w :: Width
w      -> Int -> CmmType -> CmmType
cmmVec Int
l (Width -> CmmType
cmmBits Width
w)

    MO_VU_Quot l :: Int
l w :: Width
w      -> Int -> CmmType -> CmmType
cmmVec Int
l (Width -> CmmType
cmmBits Width
w)
    MO_VU_Rem  l :: Int
l w :: Width
w      -> Int -> CmmType -> CmmType
cmmVec Int
l (Width -> CmmType
cmmBits Width
w)

    MO_VF_Insert  l :: Int
l w :: Width
w   -> Int -> CmmType -> CmmType
cmmVec Int
l (Width -> CmmType
cmmFloat Width
w)
    MO_VF_Extract _ w :: Width
w   -> Width -> CmmType
cmmFloat Width
w

    MO_VF_Add  l :: Int
l w :: Width
w      -> Int -> CmmType -> CmmType
cmmVec Int
l (Width -> CmmType
cmmFloat Width
w)
    MO_VF_Sub  l :: Int
l w :: Width
w      -> Int -> CmmType -> CmmType
cmmVec Int
l (Width -> CmmType
cmmFloat Width
w)
    MO_VF_Mul  l :: Int
l w :: Width
w      -> Int -> CmmType -> CmmType
cmmVec Int
l (Width -> CmmType
cmmFloat Width
w)
    MO_VF_Quot l :: Int
l w :: Width
w      -> Int -> CmmType -> CmmType
cmmVec Int
l (Width -> CmmType
cmmFloat Width
w)
    MO_VF_Neg  l :: Int
l w :: Width
w      -> Int -> CmmType -> CmmType
cmmVec Int
l (Width -> CmmType
cmmFloat Width
w)

    MO_AlignmentCheck _ _ -> CmmType
ty1
  where
    (ty1 :: CmmType
ty1:_) = [CmmType]
tys

comparisonResultRep :: DynFlags -> CmmType
comparisonResultRep :: DynFlags -> CmmType
comparisonResultRep = DynFlags -> CmmType
bWord  -- is it?


-- -----------------------------------------------------------------------------
-- machOpArgReps

-- | This function is used for debugging only: we can check whether an
-- application of a MachOp is "type-correct" by checking that the MachReps of
-- its arguments are the same as the MachOp expects.  This is used when
-- linting a CmmExpr.

machOpArgReps :: DynFlags -> MachOp -> [Width]
machOpArgReps :: DynFlags -> MachOp -> [Width]
machOpArgReps dflags :: DynFlags
dflags op :: MachOp
op =
  case MachOp
op of
    MO_Add    r :: Width
r         -> [Width
r,Width
r]
    MO_Sub    r :: Width
r         -> [Width
r,Width
r]
    MO_Eq     r :: Width
r         -> [Width
r,Width
r]
    MO_Ne     r :: Width
r         -> [Width
r,Width
r]
    MO_Mul    r :: Width
r         -> [Width
r,Width
r]
    MO_S_MulMayOflo r :: Width
r   -> [Width
r,Width
r]
    MO_S_Quot r :: Width
r         -> [Width
r,Width
r]
    MO_S_Rem  r :: Width
r         -> [Width
r,Width
r]
    MO_S_Neg  r :: Width
r         -> [Width
r]
    MO_U_MulMayOflo r :: Width
r   -> [Width
r,Width
r]
    MO_U_Quot r :: Width
r         -> [Width
r,Width
r]
    MO_U_Rem  r :: Width
r         -> [Width
r,Width
r]

    MO_S_Ge r :: Width
r           -> [Width
r,Width
r]
    MO_S_Le r :: Width
r           -> [Width
r,Width
r]
    MO_S_Gt r :: Width
r           -> [Width
r,Width
r]
    MO_S_Lt r :: Width
r           -> [Width
r,Width
r]

    MO_U_Ge r :: Width
r           -> [Width
r,Width
r]
    MO_U_Le r :: Width
r           -> [Width
r,Width
r]
    MO_U_Gt r :: Width
r           -> [Width
r,Width
r]
    MO_U_Lt r :: Width
r           -> [Width
r,Width
r]

    MO_F_Add r :: Width
r          -> [Width
r,Width
r]
    MO_F_Sub r :: Width
r          -> [Width
r,Width
r]
    MO_F_Mul r :: Width
r          -> [Width
r,Width
r]
    MO_F_Quot r :: Width
r         -> [Width
r,Width
r]
    MO_F_Neg r :: Width
r          -> [Width
r]
    MO_F_Eq  r :: Width
r          -> [Width
r,Width
r]
    MO_F_Ne  r :: Width
r          -> [Width
r,Width
r]
    MO_F_Ge  r :: Width
r          -> [Width
r,Width
r]
    MO_F_Le  r :: Width
r          -> [Width
r,Width
r]
    MO_F_Gt  r :: Width
r          -> [Width
r,Width
r]
    MO_F_Lt  r :: Width
r          -> [Width
r,Width
r]

    MO_And   r :: Width
r          -> [Width
r,Width
r]
    MO_Or    r :: Width
r          -> [Width
r,Width
r]
    MO_Xor   r :: Width
r          -> [Width
r,Width
r]
    MO_Not   r :: Width
r          -> [Width
r]
    MO_Shl   r :: Width
r          -> [Width
r, DynFlags -> Width
wordWidth DynFlags
dflags]
    MO_U_Shr r :: Width
r          -> [Width
r, DynFlags -> Width
wordWidth DynFlags
dflags]
    MO_S_Shr r :: Width
r          -> [Width
r, DynFlags -> Width
wordWidth DynFlags
dflags]

    MO_SS_Conv from :: Width
from _   -> [Width
from]
    MO_UU_Conv from :: Width
from _   -> [Width
from]
    MO_XX_Conv from :: Width
from _   -> [Width
from]
    MO_SF_Conv from :: Width
from _   -> [Width
from]
    MO_FS_Conv from :: Width
from _   -> [Width
from]
    MO_FF_Conv from :: Width
from _   -> [Width
from]

    MO_V_Insert  l :: Int
l r :: Width
r    -> [CmmType -> Width
typeWidth (Int -> CmmType -> CmmType
vec Int
l (Width -> CmmType
cmmBits Width
r)),Width
r,DynFlags -> Width
wordWidth DynFlags
dflags]
    MO_V_Extract l :: Int
l r :: Width
r    -> [CmmType -> Width
typeWidth (Int -> CmmType -> CmmType
vec Int
l (Width -> CmmType
cmmBits Width
r)),DynFlags -> Width
wordWidth DynFlags
dflags]

    MO_V_Add _ r :: Width
r        -> [Width
r,Width
r]
    MO_V_Sub _ r :: Width
r        -> [Width
r,Width
r]
    MO_V_Mul _ r :: Width
r        -> [Width
r,Width
r]

    MO_VS_Quot _ r :: Width
r      -> [Width
r,Width
r]
    MO_VS_Rem  _ r :: Width
r      -> [Width
r,Width
r]
    MO_VS_Neg  _ r :: Width
r      -> [Width
r]

    MO_VU_Quot _ r :: Width
r      -> [Width
r,Width
r]
    MO_VU_Rem  _ r :: Width
r      -> [Width
r,Width
r]

    MO_VF_Insert  l :: Int
l r :: Width
r   -> [CmmType -> Width
typeWidth (Int -> CmmType -> CmmType
vec Int
l (Width -> CmmType
cmmFloat Width
r)),Width
r,DynFlags -> Width
wordWidth DynFlags
dflags]
    MO_VF_Extract l :: Int
l r :: Width
r   -> [CmmType -> Width
typeWidth (Int -> CmmType -> CmmType
vec Int
l (Width -> CmmType
cmmFloat Width
r)),DynFlags -> Width
wordWidth DynFlags
dflags]

    MO_VF_Add  _ r :: Width
r      -> [Width
r,Width
r]
    MO_VF_Sub  _ r :: Width
r      -> [Width
r,Width
r]
    MO_VF_Mul  _ r :: Width
r      -> [Width
r,Width
r]
    MO_VF_Quot _ r :: Width
r      -> [Width
r,Width
r]
    MO_VF_Neg  _ r :: Width
r      -> [Width
r]

    MO_AlignmentCheck _ r :: Width
r -> [Width
r]

-----------------------------------------------------------------------------
-- CallishMachOp
-----------------------------------------------------------------------------

-- CallishMachOps tend to be implemented by foreign calls in some backends,
-- so we separate them out.  In Cmm, these can only occur in a
-- statement position, in contrast to an ordinary MachOp which can occur
-- anywhere in an expression.
data CallishMachOp
  = MO_F64_Pwr
  | MO_F64_Sin
  | MO_F64_Cos
  | MO_F64_Tan
  | MO_F64_Sinh
  | MO_F64_Cosh
  | MO_F64_Tanh
  | MO_F64_Asin
  | MO_F64_Acos
  | MO_F64_Atan
  | MO_F64_Asinh
  | MO_F64_Acosh
  | MO_F64_Atanh
  | MO_F64_Log
  | MO_F64_Exp
  | MO_F64_Fabs
  | MO_F64_Sqrt
  | MO_F32_Pwr
  | MO_F32_Sin
  | MO_F32_Cos
  | MO_F32_Tan
  | MO_F32_Sinh
  | MO_F32_Cosh
  | MO_F32_Tanh
  | MO_F32_Asin
  | MO_F32_Acos
  | MO_F32_Atan
  | MO_F32_Asinh
  | MO_F32_Acosh
  | MO_F32_Atanh
  | MO_F32_Log
  | MO_F32_Exp
  | MO_F32_Fabs
  | MO_F32_Sqrt

  | MO_UF_Conv Width

  | MO_S_QuotRem Width
  | MO_U_QuotRem Width
  | MO_U_QuotRem2 Width
  | MO_Add2      Width
  | MO_AddWordC  Width
  | MO_SubWordC  Width
  | MO_AddIntC   Width
  | MO_SubIntC   Width
  | MO_U_Mul2    Width

  | MO_ReadBarrier
  | MO_WriteBarrier
  | MO_Touch         -- Keep variables live (when using interior pointers)

  -- Prefetch
  | MO_Prefetch_Data Int -- Prefetch hint. May change program performance but not
                     -- program behavior.
                     -- the Int can be 0-3. Needs to be known at compile time
                     -- to interact with code generation correctly.
                     --  TODO: add support for prefetch WRITES,
                     --  currently only exposes prefetch reads, which
                     -- would the majority of use cases in ghc anyways


  -- These three MachOps are parameterised by the known alignment
  -- of the destination and source (for memcpy/memmove) pointers.
  -- This information may be used for optimisation in backends.
  | MO_Memcpy Int
  | MO_Memset Int
  | MO_Memmove Int
  | MO_Memcmp Int

  | MO_PopCnt Width
  | MO_Pdep Width
  | MO_Pext Width
  | MO_Clz Width
  | MO_Ctz Width

  | MO_BSwap Width

  -- Atomic read-modify-write.
  | MO_AtomicRMW Width AtomicMachOp
  | MO_AtomicRead Width
  | MO_AtomicWrite Width
  | MO_Cmpxchg Width
  deriving (CallishMachOp -> CallishMachOp -> Bool
(CallishMachOp -> CallishMachOp -> Bool)
-> (CallishMachOp -> CallishMachOp -> Bool) -> Eq CallishMachOp
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CallishMachOp -> CallishMachOp -> Bool
$c/= :: CallishMachOp -> CallishMachOp -> Bool
== :: CallishMachOp -> CallishMachOp -> Bool
$c== :: CallishMachOp -> CallishMachOp -> Bool
Eq, Int -> CallishMachOp -> ShowS
[CallishMachOp] -> ShowS
CallishMachOp -> String
(Int -> CallishMachOp -> ShowS)
-> (CallishMachOp -> String)
-> ([CallishMachOp] -> ShowS)
-> Show CallishMachOp
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [CallishMachOp] -> ShowS
$cshowList :: [CallishMachOp] -> ShowS
show :: CallishMachOp -> String
$cshow :: CallishMachOp -> String
showsPrec :: Int -> CallishMachOp -> ShowS
$cshowsPrec :: Int -> CallishMachOp -> ShowS
Show)

-- | The operation to perform atomically.
data AtomicMachOp =
      AMO_Add
    | AMO_Sub
    | AMO_And
    | AMO_Nand
    | AMO_Or
    | AMO_Xor
      deriving (AtomicMachOp -> AtomicMachOp -> Bool
(AtomicMachOp -> AtomicMachOp -> Bool)
-> (AtomicMachOp -> AtomicMachOp -> Bool) -> Eq AtomicMachOp
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: AtomicMachOp -> AtomicMachOp -> Bool
$c/= :: AtomicMachOp -> AtomicMachOp -> Bool
== :: AtomicMachOp -> AtomicMachOp -> Bool
$c== :: AtomicMachOp -> AtomicMachOp -> Bool
Eq, Int -> AtomicMachOp -> ShowS
[AtomicMachOp] -> ShowS
AtomicMachOp -> String
(Int -> AtomicMachOp -> ShowS)
-> (AtomicMachOp -> String)
-> ([AtomicMachOp] -> ShowS)
-> Show AtomicMachOp
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [AtomicMachOp] -> ShowS
$cshowList :: [AtomicMachOp] -> ShowS
show :: AtomicMachOp -> String
$cshow :: AtomicMachOp -> String
showsPrec :: Int -> AtomicMachOp -> ShowS
$cshowsPrec :: Int -> AtomicMachOp -> ShowS
Show)

pprCallishMachOp :: CallishMachOp -> SDoc
pprCallishMachOp :: CallishMachOp -> SDoc
pprCallishMachOp mo :: CallishMachOp
mo = String -> SDoc
text (CallishMachOp -> String
forall a. Show a => a -> String
show CallishMachOp
mo)

callishMachOpHints :: CallishMachOp -> ([ForeignHint], [ForeignHint])
callishMachOpHints :: CallishMachOp -> ([ForeignHint], [ForeignHint])
callishMachOpHints op :: CallishMachOp
op = case CallishMachOp
op of
  MO_Memcpy _  -> ([], [ForeignHint
AddrHint,ForeignHint
AddrHint,ForeignHint
NoHint])
  MO_Memset _  -> ([], [ForeignHint
AddrHint,ForeignHint
NoHint,ForeignHint
NoHint])
  MO_Memmove _ -> ([], [ForeignHint
AddrHint,ForeignHint
AddrHint,ForeignHint
NoHint])
  MO_Memcmp _  -> ([], [ForeignHint
AddrHint, ForeignHint
AddrHint, ForeignHint
NoHint])
  _            -> ([],[])
  -- empty lists indicate NoHint

-- | The alignment of a 'memcpy'-ish operation.
machOpMemcpyishAlign :: CallishMachOp -> Maybe Int
machOpMemcpyishAlign :: CallishMachOp -> Maybe Int
machOpMemcpyishAlign op :: CallishMachOp
op = case CallishMachOp
op of
  MO_Memcpy  align :: Int
align -> Int -> Maybe Int
forall a. a -> Maybe a
Just Int
align
  MO_Memset  align :: Int
align -> Int -> Maybe Int
forall a. a -> Maybe a
Just Int
align
  MO_Memmove align :: Int
align -> Int -> Maybe Int
forall a. a -> Maybe a
Just Int
align
  MO_Memcmp  align :: Int
align -> Int -> Maybe Int
forall a. a -> Maybe a
Just Int
align
  _                -> Maybe Int
forall a. Maybe a
Nothing