MachOp.hs 21.4 KB
Newer Older
1 2
{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}

3
module GHC.Cmm.MachOp
4 5
    ( MachOp(..)
    , pprMachOp, isCommutableMachOp, isAssociativeMachOp
6
    , isComparisonMachOp, maybeIntComparison, machOpResultType
7
    , machOpArgReps, maybeInvertComparison, isFloatComparison
8 9 10 11 12 13

    -- 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
14 15
    , mo_wordAnd, mo_wordOr, mo_wordXor, mo_wordNot
    , mo_wordShl, mo_wordSShr, mo_wordUShr
16
    , mo_u_8To32, mo_s_8To32, mo_u_16To32, mo_s_16To32
17 18
    , mo_u_8ToWord, mo_s_8ToWord, mo_u_16ToWord, mo_s_16ToWord
    , mo_u_32ToWord, mo_s_32ToWord
19
    , mo_32To8, mo_32To16, mo_WordTo8, mo_WordTo16, mo_WordTo32, mo_WordTo64
20 21

    -- CallishMachOp
22
    , CallishMachOp(..), callishMachOpHints
23
    , pprCallishMachOp
24
    , machOpMemcpyishAlign
25 26 27

    -- Atomic read-modify-write
    , AtomicMachOp(..)
28 29 30
   )
where

31
import GHC.Prelude
32

33
import GHC.Platform
34
import GHC.Cmm.Type
35
import GHC.Utils.Outputable
36 37 38 39 40 41 42

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

{- |
Machine-level primops; ones which we can reasonably delegate to the
Simon Marlow's avatar
Simon Marlow committed
43
native code generators to handle.
44

Simon Marlow's avatar
Simon Marlow committed
45 46 47
Most operations are parameterised by the 'Width' that they operate on.
Some operations have separate signed and unsigned versions, and float
and integer versions.
48 49 50

Note that there are variety of places in the native code generator where we
assume that the code produced for a MachOp does not introduce new blocks.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
-}

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
Michal Terepeta's avatar
Michal Terepeta committed
115 116 117 118 119 120 121 122
  | 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.
123
  | MO_FF_Conv Width Width      -- Float -> Float
124 125 126 127

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

129
  -- Integer vector operations
130 131
  | MO_V_Add Length Width
  | MO_V_Sub Length Width
132 133 134 135 136 137
  | MO_V_Mul Length Width

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

139 140 141 142
  -- Unsigned vector multiply/divide
  | MO_VU_Quot Length Width
  | MO_VU_Rem  Length Width

143
  -- Floating point vector element insertion and extraction operations
144 145
  | MO_VF_Insert  Length Width   -- Insert scalar into vector
  | MO_VF_Extract Length Width   -- Extract scalar from vector
146

147
  -- Floating point vector operations
148 149
  | MO_VF_Add  Length Width
  | MO_VF_Sub  Length Width
Ben Gamari's avatar
Ben Gamari committed
150
  | MO_VF_Neg  Length Width      -- unary negation
151 152
  | MO_VF_Mul  Length Width
  | MO_VF_Quot Length Width
Ben Gamari's avatar
Ben Gamari committed
153 154 155

  -- Alignment check (for -falignment-sanitisation)
  | MO_AlignmentCheck Int Width
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
  deriving (Eq, Show)

pprMachOp :: MachOp -> SDoc
pprMachOp mo = text (show 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
177
    , mo_WordTo8, mo_WordTo16, mo_WordTo32, mo_WordTo64
178
    :: Platform -> MachOp
179 180 181

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

184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231
mo_wordAdd      platform = MO_Add (wordWidth platform)
mo_wordSub      platform = MO_Sub (wordWidth platform)
mo_wordEq       platform = MO_Eq  (wordWidth platform)
mo_wordNe       platform = MO_Ne  (wordWidth platform)
mo_wordMul      platform = MO_Mul (wordWidth platform)
mo_wordSQuot    platform = MO_S_Quot (wordWidth platform)
mo_wordSRem     platform = MO_S_Rem (wordWidth platform)
mo_wordSNeg     platform = MO_S_Neg (wordWidth platform)
mo_wordUQuot    platform = MO_U_Quot (wordWidth platform)
mo_wordURem     platform = MO_U_Rem (wordWidth platform)

mo_wordSGe      platform = MO_S_Ge  (wordWidth platform)
mo_wordSLe      platform = MO_S_Le  (wordWidth platform)
mo_wordSGt      platform = MO_S_Gt  (wordWidth platform)
mo_wordSLt      platform = MO_S_Lt  (wordWidth platform)

mo_wordUGe      platform = MO_U_Ge  (wordWidth platform)
mo_wordULe      platform = MO_U_Le  (wordWidth platform)
mo_wordUGt      platform = MO_U_Gt  (wordWidth platform)
mo_wordULt      platform = MO_U_Lt  (wordWidth platform)

mo_wordAnd      platform = MO_And (wordWidth platform)
mo_wordOr       platform = MO_Or  (wordWidth platform)
mo_wordXor      platform = MO_Xor (wordWidth platform)
mo_wordNot      platform = MO_Not (wordWidth platform)
mo_wordShl      platform = MO_Shl (wordWidth platform)
mo_wordSShr     platform = MO_S_Shr (wordWidth platform)
mo_wordUShr     platform = MO_U_Shr (wordWidth platform)

mo_u_8To32               = MO_UU_Conv W8 W32
mo_s_8To32               = MO_SS_Conv W8 W32
mo_u_16To32              = MO_UU_Conv W16 W32
mo_s_16To32              = MO_SS_Conv W16 W32

mo_u_8ToWord    platform = MO_UU_Conv W8  (wordWidth platform)
mo_s_8ToWord    platform = MO_SS_Conv W8  (wordWidth platform)
mo_u_16ToWord   platform = MO_UU_Conv W16 (wordWidth platform)
mo_s_16ToWord   platform = MO_SS_Conv W16 (wordWidth platform)
mo_s_32ToWord   platform = MO_SS_Conv W32 (wordWidth platform)
mo_u_32ToWord   platform = MO_UU_Conv W32 (wordWidth platform)

mo_WordTo8      platform = MO_UU_Conv (wordWidth platform) W8
mo_WordTo16     platform = MO_UU_Conv (wordWidth platform) W16
mo_WordTo32     platform = MO_UU_Conv (wordWidth platform) W32
mo_WordTo64     platform = MO_UU_Conv (wordWidth platform) W64

mo_32To8                 = MO_UU_Conv W32 W8
mo_32To16                = MO_UU_Conv W32 W16
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255


-- ----------------------------------------------------------------------------
-- 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 mop =
  case mop of
        MO_Add _                -> True
        MO_Eq _                 -> True
        MO_Ne _                 -> True
        MO_Mul _                -> True
        MO_S_MulMayOflo _       -> True
        MO_U_MulMayOflo _       -> True
        MO_And _                -> True
        MO_Or _                 -> True
        MO_Xor _                -> True
256 257
        MO_F_Add _              -> True
        MO_F_Mul _              -> True
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
        _other                  -> 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 mop =
  case mop of
        MO_Add {} -> True       -- NB: does not include
        MO_Mul {} -> True --     floatint point!
        MO_And {} -> True
        MO_Or  {} -> True
        MO_Xor {} -> True
        _other    -> False

280

281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
-- ----------------------------------------------------------------------------
-- 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 mop =
  case mop of
    MO_Eq   _  -> True
    MO_Ne   _  -> True
    MO_S_Ge _  -> True
    MO_S_Le _  -> True
    MO_S_Gt _  -> True
    MO_S_Lt _  -> True
    MO_U_Ge _  -> True
    MO_U_Le _  -> True
    MO_U_Gt _  -> True
    MO_U_Lt _  -> True
    MO_F_Eq {} -> True
    MO_F_Ne {} -> True
    MO_F_Ge {} -> True
    MO_F_Le {} -> True
    MO_F_Gt {} -> True
    MO_F_Lt {} -> True
    _other     -> False

311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
{- |
Returns @Just w@ if the operation is an integer comparison with width
@w@, or @Nothing@ otherwise.
-}
maybeIntComparison :: MachOp -> Maybe Width
maybeIntComparison mop =
  case mop of
    MO_Eq   w  -> Just w
    MO_Ne   w  -> Just w
    MO_S_Ge w  -> Just w
    MO_S_Le w  -> Just w
    MO_S_Gt w  -> Just w
    MO_S_Lt w  -> Just w
    MO_U_Ge w  -> Just w
    MO_U_Le w  -> Just w
    MO_U_Gt w  -> Just w
    MO_U_Lt w  -> Just w
    _ -> Nothing

330 331 332 333 334 335 336 337 338 339 340
isFloatComparison :: MachOp -> Bool
isFloatComparison mop =
  case mop of
    MO_F_Eq {} -> True
    MO_F_Ne {} -> True
    MO_F_Ge {} -> True
    MO_F_Le {} -> True
    MO_F_Gt {} -> True
    MO_F_Lt {} -> True
    _other     -> False

341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
-- -----------------------------------------------------------------------------
-- 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 op
  = case op of  -- None of these Just cases include floating point
        MO_Eq r   -> Just (MO_Ne r)
        MO_Ne r   -> Just (MO_Eq r)
        MO_U_Lt r -> Just (MO_U_Ge r)
        MO_U_Gt r -> Just (MO_U_Le r)
        MO_U_Le r -> Just (MO_U_Gt r)
        MO_U_Ge r -> Just (MO_U_Lt r)
        MO_S_Lt r -> Just (MO_S_Ge r)
        MO_S_Gt r -> Just (MO_S_Le r)
        MO_S_Le r -> Just (MO_S_Gt r)
        MO_S_Ge r -> Just (MO_S_Lt r)
        _other    -> Nothing

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

{- |
Returns the MachRep of the result of a MachOp.
-}
371 372
machOpResultType :: Platform -> MachOp -> [CmmType] -> CmmType
machOpResultType platform mop tys =
373 374 375 376 377 378 379 380 381 382 383 384
  case mop of
    MO_Add {}           -> ty1  -- Preserve GC-ptr-hood
    MO_Sub {}           -> ty1  -- of first arg
    MO_Mul    r         -> cmmBits r
    MO_S_MulMayOflo r   -> cmmBits r
    MO_S_Quot r         -> cmmBits r
    MO_S_Rem  r         -> cmmBits r
    MO_S_Neg  r         -> cmmBits r
    MO_U_MulMayOflo r   -> cmmBits r
    MO_U_Quot r         -> cmmBits r
    MO_U_Rem  r         -> cmmBits r

385 386 387 388 389 390
    MO_Eq {}            -> comparisonResultRep platform
    MO_Ne {}            -> comparisonResultRep platform
    MO_S_Ge {}          -> comparisonResultRep platform
    MO_S_Le {}          -> comparisonResultRep platform
    MO_S_Gt {}          -> comparisonResultRep platform
    MO_S_Lt {}          -> comparisonResultRep platform
391

392 393 394 395
    MO_U_Ge {}          -> comparisonResultRep platform
    MO_U_Le {}          -> comparisonResultRep platform
    MO_U_Gt {}          -> comparisonResultRep platform
    MO_U_Lt {}          -> comparisonResultRep platform
396 397 398 399 400 401

    MO_F_Add r          -> cmmFloat r
    MO_F_Sub r          -> cmmFloat r
    MO_F_Mul r          -> cmmFloat r
    MO_F_Quot r         -> cmmFloat r
    MO_F_Neg r          -> cmmFloat r
402 403 404 405 406 407
    MO_F_Eq  {}         -> comparisonResultRep platform
    MO_F_Ne  {}         -> comparisonResultRep platform
    MO_F_Ge  {}         -> comparisonResultRep platform
    MO_F_Le  {}         -> comparisonResultRep platform
    MO_F_Gt  {}         -> comparisonResultRep platform
    MO_F_Lt  {}         -> comparisonResultRep platform
408 409 410 411 412 413 414 415 416 417 418

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

    MO_SS_Conv _ to     -> cmmBits to
    MO_UU_Conv _ to     -> cmmBits to
Michal Terepeta's avatar
Michal Terepeta committed
419
    MO_XX_Conv _ to     -> cmmBits to
420 421 422
    MO_FS_Conv _ to     -> cmmBits to
    MO_SF_Conv _ to     -> cmmFloat to
    MO_FF_Conv _ to     -> cmmFloat to
423

424 425 426 427 428 429 430 431 432 433 434
    MO_V_Insert  l w    -> cmmVec l (cmmBits w)
    MO_V_Extract _ w    -> cmmBits w

    MO_V_Add l w        -> cmmVec l (cmmBits w)
    MO_V_Sub l w        -> cmmVec l (cmmBits w)
    MO_V_Mul l w        -> cmmVec l (cmmBits w)

    MO_VS_Quot l w      -> cmmVec l (cmmBits w)
    MO_VS_Rem  l w      -> cmmVec l (cmmBits w)
    MO_VS_Neg  l w      -> cmmVec l (cmmBits w)

435 436 437
    MO_VU_Quot l w      -> cmmVec l (cmmBits w)
    MO_VU_Rem  l w      -> cmmVec l (cmmBits w)

438 439 440 441 442 443 444 445
    MO_VF_Insert  l w   -> cmmVec l (cmmFloat w)
    MO_VF_Extract _ w   -> cmmFloat w

    MO_VF_Add  l w      -> cmmVec l (cmmFloat w)
    MO_VF_Sub  l w      -> cmmVec l (cmmFloat w)
    MO_VF_Mul  l w      -> cmmVec l (cmmFloat w)
    MO_VF_Quot l w      -> cmmVec l (cmmFloat w)
    MO_VF_Neg  l w      -> cmmVec l (cmmFloat w)
Ben Gamari's avatar
Ben Gamari committed
446 447

    MO_AlignmentCheck _ _ -> ty1
448 449 450
  where
    (ty1:_) = tys

451
comparisonResultRep :: Platform -> CmmType
452 453 454 455 456 457 458 459 460 461 462
comparisonResultRep = 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.

463 464
machOpArgReps :: Platform -> MachOp -> [Width]
machOpArgReps platform op =
465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504
  case op of
    MO_Add    r         -> [r,r]
    MO_Sub    r         -> [r,r]
    MO_Eq     r         -> [r,r]
    MO_Ne     r         -> [r,r]
    MO_Mul    r         -> [r,r]
    MO_S_MulMayOflo r   -> [r,r]
    MO_S_Quot r         -> [r,r]
    MO_S_Rem  r         -> [r,r]
    MO_S_Neg  r         -> [r]
    MO_U_MulMayOflo r   -> [r,r]
    MO_U_Quot r         -> [r,r]
    MO_U_Rem  r         -> [r,r]

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

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

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

    MO_And   r          -> [r,r]
    MO_Or    r          -> [r,r]
    MO_Xor   r          -> [r,r]
    MO_Not   r          -> [r]
505 506 507
    MO_Shl   r          -> [r, wordWidth platform]
    MO_U_Shr r          -> [r, wordWidth platform]
    MO_S_Shr r          -> [r, wordWidth platform]
508 509 510

    MO_SS_Conv from _   -> [from]
    MO_UU_Conv from _   -> [from]
Michal Terepeta's avatar
Michal Terepeta committed
511
    MO_XX_Conv from _   -> [from]
512 513 514 515
    MO_SF_Conv from _   -> [from]
    MO_FS_Conv from _   -> [from]
    MO_FF_Conv from _   -> [from]

516 517
    MO_V_Insert  l r    -> [typeWidth (vec l (cmmBits r)),r,wordWidth platform]
    MO_V_Extract l r    -> [typeWidth (vec l (cmmBits r)),wordWidth platform]
518

519 520 521 522 523 524 525 526
    MO_V_Add _ r        -> [r,r]
    MO_V_Sub _ r        -> [r,r]
    MO_V_Mul _ r        -> [r,r]

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

527 528 529
    MO_VU_Quot _ r      -> [r,r]
    MO_VU_Rem  _ r      -> [r,r]

530 531
    MO_VF_Insert  l r   -> [typeWidth (vec l (cmmFloat r)),r,wordWidth platform]
    MO_VF_Extract l r   -> [typeWidth (vec l (cmmFloat r)),wordWidth platform]
532

533 534 535 536 537
    MO_VF_Add  _ r      -> [r,r]
    MO_VF_Sub  _ r      -> [r,r]
    MO_VF_Mul  _ r      -> [r,r]
    MO_VF_Quot _ r      -> [r,r]
    MO_VF_Neg  _ r      -> [r]
538

Ben Gamari's avatar
Ben Gamari committed
539 540
    MO_AlignmentCheck _ r -> [r]

541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
-----------------------------------------------------------------------------
-- 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
560 561 562
  | MO_F64_Asinh
  | MO_F64_Acosh
  | MO_F64_Atanh
563
  | MO_F64_Log
chessai's avatar
chessai committed
564
  | MO_F64_Log1P
565
  | MO_F64_Exp
chessai's avatar
chessai committed
566
  | MO_F64_ExpM1
567
  | MO_F64_Fabs
568 569 570 571 572 573 574 575 576 577 578
  | 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
579 580 581
  | MO_F32_Asinh
  | MO_F32_Acosh
  | MO_F32_Atanh
582
  | MO_F32_Log
chessai's avatar
chessai committed
583
  | MO_F32_Log1P
584
  | MO_F32_Exp
chessai's avatar
chessai committed
585
  | MO_F32_ExpM1
586
  | MO_F32_Fabs
587
  | MO_F32_Sqrt
588

tibbe's avatar
tibbe committed
589 590
  | MO_UF_Conv Width

Sylvain Henry's avatar
Sylvain Henry committed
591
  | MO_S_Mul2    Width
592
  | MO_S_QuotRem Width
593
  | MO_U_QuotRem Width
Ian Lynagh's avatar
Ian Lynagh committed
594
  | MO_U_QuotRem2 Width
Ian Lynagh's avatar
Ian Lynagh committed
595
  | MO_Add2      Width
Sebastian Graf's avatar
Sebastian Graf committed
596
  | MO_AddWordC  Width
nkaretnikov's avatar
nkaretnikov committed
597
  | MO_SubWordC  Width
598 599
  | MO_AddIntC   Width
  | MO_SubIntC   Width
Ian Lynagh's avatar
Ian Lynagh committed
600
  | MO_U_Mul2    Width
601

602
  | MO_ReadBarrier
603 604
  | MO_WriteBarrier
  | MO_Touch         -- Keep variables live (when using interior pointers)
605

gmainland's avatar
gmainland committed
606
  -- Prefetch
607
  | MO_Prefetch_Data Int -- Prefetch hint. May change program performance but not
gmainland's avatar
gmainland committed
608
                     -- program behavior.
609 610 611 612 613 614
                     -- 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

gmainland's avatar
gmainland committed
615

616 617 618 619 620 621
  -- 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
622
  | MO_Memcmp Int
tibbe's avatar
tibbe committed
623 624

  | MO_PopCnt Width
625 626
  | MO_Pdep Width
  | MO_Pext Width
627 628 629
  | MO_Clz Width
  | MO_Ctz Width

630
  | MO_BSwap Width
631
  | MO_BRev Width
632 633 634 635 636 637

  -- Atomic read-modify-write.
  | MO_AtomicRMW Width AtomicMachOp
  | MO_AtomicRead Width
  | MO_AtomicWrite Width
  | MO_Cmpxchg Width
638 639 640
  -- Should be an AtomicRMW variant eventually.
  -- Sequential consistent.
  | MO_Xchg Width
641 642
  deriving (Eq, Show)

643 644 645 646 647 648 649 650 651 652
-- | The operation to perform atomically.
data AtomicMachOp =
      AMO_Add
    | AMO_Sub
    | AMO_And
    | AMO_Nand
    | AMO_Or
    | AMO_Xor
      deriving (Eq, Show)

653 654
pprCallishMachOp :: CallishMachOp -> SDoc
pprCallishMachOp mo = text (show mo)
655

656 657
callishMachOpHints :: CallishMachOp -> ([ForeignHint], [ForeignHint])
callishMachOpHints op = case op of
658 659 660
  MO_Memcpy _  -> ([], [AddrHint,AddrHint,NoHint])
  MO_Memset _  -> ([], [AddrHint,NoHint,NoHint])
  MO_Memmove _ -> ([], [AddrHint,AddrHint,NoHint])
661
  MO_Memcmp _  -> ([], [AddrHint, AddrHint, NoHint])
662
  _            -> ([],[])
663
  -- empty lists indicate NoHint
664 665 666 667 668 669 670

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