{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE UndecidableInstances #-}

module CmmExpr
    ( CmmExpr(..), cmmExprType, cmmExprWidth, maybeInvertCmmExpr
    , CmmReg(..), cmmRegType, cmmRegWidth
    , CmmLit(..), cmmLitType
    , LocalReg(..), localRegType
    , GlobalReg(..), isArgReg, globalRegType
    , spReg, hpReg, spLimReg, hpLimReg, nodeReg
    , currentTSOReg, currentNurseryReg, hpAllocReg, cccsReg
    , node, baseReg
    , VGcPtr(..)

    , DefinerOfRegs, UserOfRegs
    , foldRegsDefd, foldRegsUsed
    , foldLocalRegsDefd, foldLocalRegsUsed

    , RegSet, LocalRegSet, GlobalRegSet
    , emptyRegSet, elemRegSet, extendRegSet, deleteFromRegSet, mkRegSet
    , plusRegSet, minusRegSet, timesRegSet, sizeRegSet, nullRegSet
    , regSetToList

    , Area(..)
    , module CmmMachOp
    , module CmmType
    )
where

import GhcPrelude

import BlockId
import CLabel
import CmmMachOp
import CmmType
import DynFlags
import Outputable (panic)
import Unique

import Data.Set (Set)
import qualified Data.Set as Set

-----------------------------------------------------------------------------
--              CmmExpr
-- An expression.  Expressions have no side effects.
-----------------------------------------------------------------------------

data CmmExpr
  = CmmLit CmmLit               -- Literal
  | CmmLoad !CmmExpr !CmmType   -- Read memory location
  | CmmReg !CmmReg              -- Contents of register
  | CmmMachOp MachOp [CmmExpr]  -- Machine operation (+, -, *, etc.)
  | CmmStackSlot Area {-# UNPACK #-} !Int
                                -- addressing expression of a stack slot
                                -- See Note [CmmStackSlot aliasing]
  | CmmRegOff !CmmReg Int
        -- CmmRegOff reg i
        --        ** is shorthand only, meaning **
        -- CmmMachOp (MO_Add rep) [x, CmmLit (CmmInt (fromIntegral i) rep)]
        --      where rep = typeWidth (cmmRegType reg)

instance Eq CmmExpr where       -- Equality ignores the types
  CmmLit l1 :: CmmLit
l1          == :: CmmExpr -> CmmExpr -> Bool
== CmmLit l2 :: CmmLit
l2          = CmmLit
l1CmmLit -> CmmLit -> Bool
forall a. Eq a => a -> a -> Bool
==CmmLit
l2
  CmmLoad e1 :: CmmExpr
e1 _       == CmmLoad e2 :: CmmExpr
e2 _       = CmmExpr
e1CmmExpr -> CmmExpr -> Bool
forall a. Eq a => a -> a -> Bool
==CmmExpr
e2
  CmmReg r1 :: CmmReg
r1          == CmmReg r2 :: CmmReg
r2          = CmmReg
r1CmmReg -> CmmReg -> Bool
forall a. Eq a => a -> a -> Bool
==CmmReg
r2
  CmmRegOff r1 :: CmmReg
r1 i1 :: Int
i1    == CmmRegOff r2 :: CmmReg
r2 i2 :: Int
i2    = CmmReg
r1CmmReg -> CmmReg -> Bool
forall a. Eq a => a -> a -> Bool
==CmmReg
r2 Bool -> Bool -> Bool
&& Int
i1Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
==Int
i2
  CmmMachOp op1 :: MachOp
op1 es1 :: [CmmExpr]
es1  == CmmMachOp op2 :: MachOp
op2 es2 :: [CmmExpr]
es2  = MachOp
op1MachOp -> MachOp -> Bool
forall a. Eq a => a -> a -> Bool
==MachOp
op2 Bool -> Bool -> Bool
&& [CmmExpr]
es1[CmmExpr] -> [CmmExpr] -> Bool
forall a. Eq a => a -> a -> Bool
==[CmmExpr]
es2
  CmmStackSlot a1 :: Area
a1 i1 :: Int
i1 == CmmStackSlot a2 :: Area
a2 i2 :: Int
i2 = Area
a1Area -> Area -> Bool
forall a. Eq a => a -> a -> Bool
==Area
a2 Bool -> Bool -> Bool
&& Int
i1Int -> Int -> Bool
forall a. Eq a => a -> a -> Bool
==Int
i2
  _e1 :: CmmExpr
_e1                == _e2 :: CmmExpr
_e2                = Bool
False

data CmmReg
  = CmmLocal  {-# UNPACK #-} !LocalReg
  | CmmGlobal GlobalReg
  deriving( CmmReg -> CmmReg -> Bool
(CmmReg -> CmmReg -> Bool)
-> (CmmReg -> CmmReg -> Bool) -> Eq CmmReg
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CmmReg -> CmmReg -> Bool
$c/= :: CmmReg -> CmmReg -> Bool
== :: CmmReg -> CmmReg -> Bool
$c== :: CmmReg -> CmmReg -> Bool
Eq, Eq CmmReg
Eq CmmReg =>
(CmmReg -> CmmReg -> Ordering)
-> (CmmReg -> CmmReg -> Bool)
-> (CmmReg -> CmmReg -> Bool)
-> (CmmReg -> CmmReg -> Bool)
-> (CmmReg -> CmmReg -> Bool)
-> (CmmReg -> CmmReg -> CmmReg)
-> (CmmReg -> CmmReg -> CmmReg)
-> Ord CmmReg
CmmReg -> CmmReg -> Bool
CmmReg -> CmmReg -> Ordering
CmmReg -> CmmReg -> CmmReg
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 :: CmmReg -> CmmReg -> CmmReg
$cmin :: CmmReg -> CmmReg -> CmmReg
max :: CmmReg -> CmmReg -> CmmReg
$cmax :: CmmReg -> CmmReg -> CmmReg
>= :: CmmReg -> CmmReg -> Bool
$c>= :: CmmReg -> CmmReg -> Bool
> :: CmmReg -> CmmReg -> Bool
$c> :: CmmReg -> CmmReg -> Bool
<= :: CmmReg -> CmmReg -> Bool
$c<= :: CmmReg -> CmmReg -> Bool
< :: CmmReg -> CmmReg -> Bool
$c< :: CmmReg -> CmmReg -> Bool
compare :: CmmReg -> CmmReg -> Ordering
$ccompare :: CmmReg -> CmmReg -> Ordering
$cp1Ord :: Eq CmmReg
Ord )

-- | A stack area is either the stack slot where a variable is spilled
-- or the stack space where function arguments and results are passed.
data Area
  = Old            -- See Note [Old Area]
  | Young {-# UNPACK #-} !BlockId  -- Invariant: must be a continuation BlockId
                   -- See Note [Continuation BlockId] in CmmNode.
  deriving (Area -> Area -> Bool
(Area -> Area -> Bool) -> (Area -> Area -> Bool) -> Eq Area
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Area -> Area -> Bool
$c/= :: Area -> Area -> Bool
== :: Area -> Area -> Bool
$c== :: Area -> Area -> Bool
Eq, Eq Area
Eq Area =>
(Area -> Area -> Ordering)
-> (Area -> Area -> Bool)
-> (Area -> Area -> Bool)
-> (Area -> Area -> Bool)
-> (Area -> Area -> Bool)
-> (Area -> Area -> Area)
-> (Area -> Area -> Area)
-> Ord Area
Area -> Area -> Bool
Area -> Area -> Ordering
Area -> Area -> Area
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 :: Area -> Area -> Area
$cmin :: Area -> Area -> Area
max :: Area -> Area -> Area
$cmax :: Area -> Area -> Area
>= :: Area -> Area -> Bool
$c>= :: Area -> Area -> Bool
> :: Area -> Area -> Bool
$c> :: Area -> Area -> Bool
<= :: Area -> Area -> Bool
$c<= :: Area -> Area -> Bool
< :: Area -> Area -> Bool
$c< :: Area -> Area -> Bool
compare :: Area -> Area -> Ordering
$ccompare :: Area -> Area -> Ordering
$cp1Ord :: Eq Area
Ord)

{- Note [Old Area]
~~~~~~~~~~~~~~~~~~
There is a single call area 'Old', allocated at the extreme old
end of the stack frame (ie just younger than the return address)
which holds:
  * incoming (overflow) parameters,
  * outgoing (overflow) parameter to tail calls,
  * outgoing (overflow) result values
  * the update frame (if any)

Its size is the max of all these requirements.  On entry, the stack
pointer will point to the youngest incoming parameter, which is not
necessarily at the young end of the Old area.

End of note -}


{- Note [CmmStackSlot aliasing]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
When do two CmmStackSlots alias?

 - T[old+N] aliases with U[young(L)+M] for all T, U, L, N and M
 - T[old+N] aliases with U[old+M] only if the areas actually overlap

Or more informally, different Areas may overlap with each other.

An alternative semantics, that we previously had, was that different
Areas do not overlap.  The problem that lead to redefining the
semantics of stack areas is described below.

e.g. if we had

    x = Sp[old + 8]
    y = Sp[old + 16]

    Sp[young(L) + 8]  = L
    Sp[young(L) + 16] = y
    Sp[young(L) + 24] = x
    call f() returns to L

if areas semantically do not overlap, then we might optimise this to

    Sp[young(L) + 8]  = L
    Sp[young(L) + 16] = Sp[old + 8]
    Sp[young(L) + 24] = Sp[old + 16]
    call f() returns to L

and now young(L) cannot be allocated at the same place as old, and we
are doomed to use more stack.

  - old+8  conflicts with young(L)+8
  - old+16 conflicts with young(L)+16 and young(L)+8

so young(L)+8 == old+24 and we get

    Sp[-8]  = L
    Sp[-16] = Sp[8]
    Sp[-24] = Sp[0]
    Sp -= 24
    call f() returns to L

However, if areas are defined to be "possibly overlapping" in the
semantics, then we cannot commute any loads/stores of old with
young(L), and we will be able to re-use both old+8 and old+16 for
young(L).

    x = Sp[8]
    y = Sp[0]

    Sp[8] = L
    Sp[0] = y
    Sp[-8] = x
    Sp = Sp - 8
    call f() returns to L

Now, the assignments of y go away,

    x = Sp[8]
    Sp[8] = L
    Sp[-8] = x
    Sp = Sp - 8
    call f() returns to L
-}

data CmmLit
  = CmmInt !Integer  Width
        -- Interpretation: the 2's complement representation of the value
        -- is truncated to the specified size.  This is easier than trying
        -- to keep the value within range, because we don't know whether
        -- it will be used as a signed or unsigned value (the CmmType doesn't
        -- distinguish between signed & unsigned).
  | CmmFloat  Rational Width
  | CmmVec [CmmLit]                     -- Vector literal
  | CmmLabel    CLabel                  -- Address of label
  | CmmLabelOff CLabel Int              -- Address of label + byte offset

        -- Due to limitations in the C backend, the following
        -- MUST ONLY be used inside the info table indicated by label2
        -- (label2 must be the info label), and label1 must be an
        -- SRT, a slow entrypoint or a large bitmap (see the Mangler)
        -- Don't use it at all unless tablesNextToCode.
        -- It is also used inside the NCG during when generating
        -- position-independent code.
  | CmmLabelDiffOff CLabel CLabel Int Width -- label1 - label2 + offset
        -- In an expression, the width just has the effect of MO_SS_Conv
        -- from wordWidth to the desired width.
        --
        -- In a static literal, the supported Widths depend on the
        -- architecture: wordWidth is supported on all
        -- architectures. Additionally W32 is supported on x86_64 when
        -- using the small memory model.

  | CmmBlock {-# UNPACK #-} !BlockId     -- Code label
        -- Invariant: must be a continuation BlockId
        -- See Note [Continuation BlockId] in CmmNode.

  | CmmHighStackMark -- A late-bound constant that stands for the max
                     -- #bytes of stack space used during a procedure.
                     -- During the stack-layout pass, CmmHighStackMark
                     -- is replaced by a CmmInt for the actual number
                     -- of bytes used
  deriving CmmLit -> CmmLit -> Bool
(CmmLit -> CmmLit -> Bool)
-> (CmmLit -> CmmLit -> Bool) -> Eq CmmLit
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: CmmLit -> CmmLit -> Bool
$c/= :: CmmLit -> CmmLit -> Bool
== :: CmmLit -> CmmLit -> Bool
$c== :: CmmLit -> CmmLit -> Bool
Eq

cmmExprType :: DynFlags -> CmmExpr -> CmmType
cmmExprType :: DynFlags -> CmmExpr -> CmmType
cmmExprType dflags :: DynFlags
dflags (CmmLit lit :: CmmLit
lit)        = DynFlags -> CmmLit -> CmmType
cmmLitType DynFlags
dflags CmmLit
lit
cmmExprType _      (CmmLoad _ rep :: CmmType
rep)     = CmmType
rep
cmmExprType dflags :: DynFlags
dflags (CmmReg reg :: CmmReg
reg)        = DynFlags -> CmmReg -> CmmType
cmmRegType DynFlags
dflags CmmReg
reg
cmmExprType dflags :: DynFlags
dflags (CmmMachOp op :: MachOp
op args :: [CmmExpr]
args) = DynFlags -> MachOp -> [CmmType] -> CmmType
machOpResultType DynFlags
dflags MachOp
op ((CmmExpr -> CmmType) -> [CmmExpr] -> [CmmType]
forall a b. (a -> b) -> [a] -> [b]
map (DynFlags -> CmmExpr -> CmmType
cmmExprType DynFlags
dflags) [CmmExpr]
args)
cmmExprType dflags :: DynFlags
dflags (CmmRegOff reg :: CmmReg
reg _)   = DynFlags -> CmmReg -> CmmType
cmmRegType DynFlags
dflags CmmReg
reg
cmmExprType dflags :: DynFlags
dflags (CmmStackSlot _ _)  = DynFlags -> CmmType
bWord DynFlags
dflags -- an address
-- Careful though: what is stored at the stack slot may be bigger than
-- an address

cmmLitType :: DynFlags -> CmmLit -> CmmType
cmmLitType :: DynFlags -> CmmLit -> CmmType
cmmLitType _      (CmmInt _ width :: Width
width)     = Width -> CmmType
cmmBits  Width
width
cmmLitType _      (CmmFloat _ width :: Width
width)   = Width -> CmmType
cmmFloat Width
width
cmmLitType _      (CmmVec [])          = String -> CmmType
forall a. String -> a
panic "cmmLitType: CmmVec []"
cmmLitType cflags :: DynFlags
cflags (CmmVec (l :: CmmLit
l:ls :: [CmmLit]
ls))      = let ty :: CmmType
ty = DynFlags -> CmmLit -> CmmType
cmmLitType DynFlags
cflags CmmLit
l
                                         in if (CmmType -> Bool) -> [CmmType] -> Bool
forall (t :: * -> *) a. Foldable t => (a -> Bool) -> t a -> Bool
all (CmmType -> CmmType -> Bool
`cmmEqType` CmmType
ty) ((CmmLit -> CmmType) -> [CmmLit] -> [CmmType]
forall a b. (a -> b) -> [a] -> [b]
map (DynFlags -> CmmLit -> CmmType
cmmLitType DynFlags
cflags) [CmmLit]
ls)
                                            then Int -> CmmType -> CmmType
cmmVec (1Int -> Int -> Int
forall a. Num a => a -> a -> a
+[CmmLit] -> Int
forall (t :: * -> *) a. Foldable t => t a -> Int
length [CmmLit]
ls) CmmType
ty
                                            else String -> CmmType
forall a. String -> a
panic "cmmLitType: CmmVec"
cmmLitType dflags :: DynFlags
dflags (CmmLabel lbl :: CLabel
lbl)       = DynFlags -> CLabel -> CmmType
cmmLabelType DynFlags
dflags CLabel
lbl
cmmLitType dflags :: DynFlags
dflags (CmmLabelOff lbl :: CLabel
lbl _)  = DynFlags -> CLabel -> CmmType
cmmLabelType DynFlags
dflags CLabel
lbl
cmmLitType _      (CmmLabelDiffOff _ _ _ width :: Width
width) = Width -> CmmType
cmmBits Width
width
cmmLitType dflags :: DynFlags
dflags (CmmBlock _)         = DynFlags -> CmmType
bWord DynFlags
dflags
cmmLitType dflags :: DynFlags
dflags (CmmLit
CmmHighStackMark)   = DynFlags -> CmmType
bWord DynFlags
dflags

cmmLabelType :: DynFlags -> CLabel -> CmmType
cmmLabelType :: DynFlags -> CLabel -> CmmType
cmmLabelType dflags :: DynFlags
dflags lbl :: CLabel
lbl
 | CLabel -> Bool
isGcPtrLabel CLabel
lbl = DynFlags -> CmmType
gcWord DynFlags
dflags
 | Bool
otherwise        = DynFlags -> CmmType
bWord DynFlags
dflags

cmmExprWidth :: DynFlags -> CmmExpr -> Width
cmmExprWidth :: DynFlags -> CmmExpr -> Width
cmmExprWidth dflags :: DynFlags
dflags e :: CmmExpr
e = CmmType -> Width
typeWidth (DynFlags -> CmmExpr -> CmmType
cmmExprType DynFlags
dflags CmmExpr
e)

--------
--- Negation for conditional branches

maybeInvertCmmExpr :: CmmExpr -> Maybe CmmExpr
maybeInvertCmmExpr :: CmmExpr -> Maybe CmmExpr
maybeInvertCmmExpr (CmmMachOp op :: MachOp
op args :: [CmmExpr]
args) = do MachOp
op' <- MachOp -> Maybe MachOp
maybeInvertComparison MachOp
op
                                            CmmExpr -> Maybe CmmExpr
forall (m :: * -> *) a. Monad m => a -> m a
return (MachOp -> [CmmExpr] -> CmmExpr
CmmMachOp MachOp
op' [CmmExpr]
args)
maybeInvertCmmExpr _ = Maybe CmmExpr
forall a. Maybe a
Nothing

-----------------------------------------------------------------------------
--              Local registers
-----------------------------------------------------------------------------

data LocalReg
  = LocalReg {-# UNPACK #-} !Unique CmmType
    -- ^ Parameters:
    --   1. Identifier
    --   2. Type

instance Eq LocalReg where
  (LocalReg u1 :: Unique
u1 _) == :: LocalReg -> LocalReg -> Bool
== (LocalReg u2 :: Unique
u2 _) = Unique
u1 Unique -> Unique -> Bool
forall a. Eq a => a -> a -> Bool
== Unique
u2

-- This is non-deterministic but we do not currently support deterministic
-- code-generation. See Note [Unique Determinism and code generation]
-- See Note [No Ord for Unique]
instance Ord LocalReg where
  compare :: LocalReg -> LocalReg -> Ordering
compare (LocalReg u1 :: Unique
u1 _) (LocalReg u2 :: Unique
u2 _) = Unique -> Unique -> Ordering
nonDetCmpUnique Unique
u1 Unique
u2

instance Uniquable LocalReg where
  getUnique :: LocalReg -> Unique
getUnique (LocalReg uniq :: Unique
uniq _) = Unique
uniq

cmmRegType :: DynFlags -> CmmReg -> CmmType
cmmRegType :: DynFlags -> CmmReg -> CmmType
cmmRegType _      (CmmLocal  reg :: LocalReg
reg) = LocalReg -> CmmType
localRegType LocalReg
reg
cmmRegType dflags :: DynFlags
dflags (CmmGlobal reg :: GlobalReg
reg) = DynFlags -> GlobalReg -> CmmType
globalRegType DynFlags
dflags GlobalReg
reg

cmmRegWidth :: DynFlags -> CmmReg -> Width
cmmRegWidth :: DynFlags -> CmmReg -> Width
cmmRegWidth dflags :: DynFlags
dflags = CmmType -> Width
typeWidth (CmmType -> Width) -> (CmmReg -> CmmType) -> CmmReg -> Width
forall b c a. (b -> c) -> (a -> b) -> a -> c
. DynFlags -> CmmReg -> CmmType
cmmRegType DynFlags
dflags

localRegType :: LocalReg -> CmmType
localRegType :: LocalReg -> CmmType
localRegType (LocalReg _ rep :: CmmType
rep) = CmmType
rep

-----------------------------------------------------------------------------
--    Register-use information for expressions and other types
-----------------------------------------------------------------------------

-- | Sets of registers

-- These are used for dataflow facts, and a common operation is taking
-- the union of two RegSets and then asking whether the union is the
-- same as one of the inputs.  UniqSet isn't good here, because
-- sizeUniqSet is O(n) whereas Set.size is O(1), so we use ordinary
-- Sets.

type RegSet r     = Set r
type LocalRegSet  = RegSet LocalReg
type GlobalRegSet = RegSet GlobalReg

emptyRegSet             :: RegSet r
nullRegSet              :: RegSet r -> Bool
elemRegSet              :: Ord r => r -> RegSet r -> Bool
extendRegSet            :: Ord r => RegSet r -> r -> RegSet r
deleteFromRegSet        :: Ord r => RegSet r -> r -> RegSet r
mkRegSet                :: Ord r => [r] -> RegSet r
minusRegSet, plusRegSet, timesRegSet :: Ord r => RegSet r -> RegSet r -> RegSet r
sizeRegSet              :: RegSet r -> Int
regSetToList            :: RegSet r -> [r]

emptyRegSet :: RegSet r
emptyRegSet      = RegSet r
forall a. Set a
Set.empty
nullRegSet :: RegSet r -> Bool
nullRegSet       = RegSet r -> Bool
forall a. Set a -> Bool
Set.null
elemRegSet :: r -> RegSet r -> Bool
elemRegSet       = r -> RegSet r -> Bool
forall a. Ord a => a -> Set a -> Bool
Set.member
extendRegSet :: RegSet r -> r -> RegSet r
extendRegSet     = (r -> RegSet r -> RegSet r) -> RegSet r -> r -> RegSet r
forall a b c. (a -> b -> c) -> b -> a -> c
flip r -> RegSet r -> RegSet r
forall a. Ord a => a -> Set a -> Set a
Set.insert
deleteFromRegSet :: RegSet r -> r -> RegSet r
deleteFromRegSet = (r -> RegSet r -> RegSet r) -> RegSet r -> r -> RegSet r
forall a b c. (a -> b -> c) -> b -> a -> c
flip r -> RegSet r -> RegSet r
forall a. Ord a => a -> Set a -> Set a
Set.delete
mkRegSet :: [r] -> RegSet r
mkRegSet         = [r] -> RegSet r
forall a. Ord a => [a] -> Set a
Set.fromList
minusRegSet :: RegSet r -> RegSet r -> RegSet r
minusRegSet      = RegSet r -> RegSet r -> RegSet r
forall a. Ord a => Set a -> Set a -> Set a
Set.difference
plusRegSet :: RegSet r -> RegSet r -> RegSet r
plusRegSet       = RegSet r -> RegSet r -> RegSet r
forall a. Ord a => Set a -> Set a -> Set a
Set.union
timesRegSet :: RegSet r -> RegSet r -> RegSet r
timesRegSet      = RegSet r -> RegSet r -> RegSet r
forall a. Ord a => Set a -> Set a -> Set a
Set.intersection
sizeRegSet :: RegSet r -> Int
sizeRegSet       = RegSet r -> Int
forall a. Set a -> Int
Set.size
regSetToList :: RegSet r -> [r]
regSetToList     = RegSet r -> [r]
forall a. Set a -> [a]
Set.toList

class Ord r => UserOfRegs r a where
  foldRegsUsed :: DynFlags -> (b -> r -> b) -> b -> a -> b

foldLocalRegsUsed :: UserOfRegs LocalReg a
                  => DynFlags -> (b -> LocalReg -> b) -> b -> a -> b
foldLocalRegsUsed :: DynFlags -> (b -> LocalReg -> b) -> b -> a -> b
foldLocalRegsUsed = DynFlags -> (b -> LocalReg -> b) -> b -> a -> b
forall r a b.
UserOfRegs r a =>
DynFlags -> (b -> r -> b) -> b -> a -> b
foldRegsUsed

class Ord r => DefinerOfRegs r a where
  foldRegsDefd :: DynFlags -> (b -> r -> b) -> b -> a -> b

foldLocalRegsDefd :: DefinerOfRegs LocalReg a
                  => DynFlags -> (b -> LocalReg -> b) -> b -> a -> b
foldLocalRegsDefd :: DynFlags -> (b -> LocalReg -> b) -> b -> a -> b
foldLocalRegsDefd = DynFlags -> (b -> LocalReg -> b) -> b -> a -> b
forall r a b.
DefinerOfRegs r a =>
DynFlags -> (b -> r -> b) -> b -> a -> b
foldRegsDefd

instance UserOfRegs LocalReg CmmReg where
    foldRegsUsed :: DynFlags -> (b -> LocalReg -> b) -> b -> CmmReg -> b
foldRegsUsed _ f :: b -> LocalReg -> b
f z :: b
z (CmmLocal reg :: LocalReg
reg) = b -> LocalReg -> b
f b
z LocalReg
reg
    foldRegsUsed _ _ z :: b
z (CmmGlobal _)  = b
z

instance DefinerOfRegs LocalReg CmmReg where
    foldRegsDefd :: DynFlags -> (b -> LocalReg -> b) -> b -> CmmReg -> b
foldRegsDefd _ f :: b -> LocalReg -> b
f z :: b
z (CmmLocal reg :: LocalReg
reg) = b -> LocalReg -> b
f b
z LocalReg
reg
    foldRegsDefd _ _ z :: b
z (CmmGlobal _)  = b
z

instance UserOfRegs GlobalReg CmmReg where
    foldRegsUsed :: DynFlags -> (b -> GlobalReg -> b) -> b -> CmmReg -> b
foldRegsUsed _ _ z :: b
z (CmmLocal _)    = b
z
    foldRegsUsed _ f :: b -> GlobalReg -> b
f z :: b
z (CmmGlobal reg :: GlobalReg
reg) = b -> GlobalReg -> b
f b
z GlobalReg
reg

instance DefinerOfRegs GlobalReg CmmReg where
    foldRegsDefd :: DynFlags -> (b -> GlobalReg -> b) -> b -> CmmReg -> b
foldRegsDefd _ _ z :: b
z (CmmLocal _)    = b
z
    foldRegsDefd _ f :: b -> GlobalReg -> b
f z :: b
z (CmmGlobal reg :: GlobalReg
reg) = b -> GlobalReg -> b
f b
z GlobalReg
reg

instance Ord r => UserOfRegs r r where
    foldRegsUsed :: DynFlags -> (b -> r -> b) -> b -> r -> b
foldRegsUsed _ f :: b -> r -> b
f z :: b
z r :: r
r = b -> r -> b
f b
z r
r

instance Ord r => DefinerOfRegs r r where
    foldRegsDefd :: DynFlags -> (b -> r -> b) -> b -> r -> b
foldRegsDefd _ f :: b -> r -> b
f z :: b
z r :: r
r = b -> r -> b
f b
z r
r

instance (Ord r, UserOfRegs r CmmReg) => UserOfRegs r CmmExpr where
  -- The (Ord r) in the context is necessary here
  -- See Note [Recursive superclasses] in TcInstDcls
  foldRegsUsed :: DynFlags -> (b -> r -> b) -> b -> CmmExpr -> b
foldRegsUsed dflags :: DynFlags
dflags f :: b -> r -> b
f !b
z e :: CmmExpr
e = b -> CmmExpr -> b
expr b
z CmmExpr
e
    where expr :: b -> CmmExpr -> b
expr z :: b
z (CmmLit _)          = b
z
          expr z :: b
z (CmmLoad addr :: CmmExpr
addr _)    = DynFlags -> (b -> r -> b) -> b -> CmmExpr -> b
forall r a b.
UserOfRegs r a =>
DynFlags -> (b -> r -> b) -> b -> a -> b
foldRegsUsed DynFlags
dflags b -> r -> b
f b
z CmmExpr
addr
          expr z :: b
z (CmmReg r :: CmmReg
r)          = DynFlags -> (b -> r -> b) -> b -> CmmReg -> b
forall r a b.
UserOfRegs r a =>
DynFlags -> (b -> r -> b) -> b -> a -> b
foldRegsUsed DynFlags
dflags b -> r -> b
f b
z CmmReg
r
          expr z :: b
z (CmmMachOp _ exprs :: [CmmExpr]
exprs) = DynFlags -> (b -> r -> b) -> b -> [CmmExpr] -> b
forall r a b.
UserOfRegs r a =>
DynFlags -> (b -> r -> b) -> b -> a -> b
foldRegsUsed DynFlags
dflags b -> r -> b
f b
z [CmmExpr]
exprs
          expr z :: b
z (CmmRegOff r :: CmmReg
r _)     = DynFlags -> (b -> r -> b) -> b -> CmmReg -> b
forall r a b.
UserOfRegs r a =>
DynFlags -> (b -> r -> b) -> b -> a -> b
foldRegsUsed DynFlags
dflags b -> r -> b
f b
z CmmReg
r
          expr z :: b
z (CmmStackSlot _ _)  = b
z

instance UserOfRegs r a => UserOfRegs r [a] where
  foldRegsUsed :: DynFlags -> (b -> r -> b) -> b -> [a] -> b
foldRegsUsed dflags :: DynFlags
dflags f :: b -> r -> b
f set :: b
set as :: [a]
as = (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (DynFlags -> (b -> r -> b) -> b -> a -> b
forall r a b.
UserOfRegs r a =>
DynFlags -> (b -> r -> b) -> b -> a -> b
foldRegsUsed DynFlags
dflags b -> r -> b
f) b
set [a]
as
  {-# INLINABLE foldRegsUsed #-}

instance DefinerOfRegs r a => DefinerOfRegs r [a] where
  foldRegsDefd :: DynFlags -> (b -> r -> b) -> b -> [a] -> b
foldRegsDefd dflags :: DynFlags
dflags f :: b -> r -> b
f set :: b
set as :: [a]
as = (b -> a -> b) -> b -> [a] -> b
forall (t :: * -> *) b a.
Foldable t =>
(b -> a -> b) -> b -> t a -> b
foldl' (DynFlags -> (b -> r -> b) -> b -> a -> b
forall r a b.
DefinerOfRegs r a =>
DynFlags -> (b -> r -> b) -> b -> a -> b
foldRegsDefd DynFlags
dflags b -> r -> b
f) b
set [a]
as
  {-# INLINABLE foldRegsDefd #-}

-----------------------------------------------------------------------------
--              Global STG registers
-----------------------------------------------------------------------------

data VGcPtr = VGcPtr | VNonGcPtr deriving( VGcPtr -> VGcPtr -> Bool
(VGcPtr -> VGcPtr -> Bool)
-> (VGcPtr -> VGcPtr -> Bool) -> Eq VGcPtr
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: VGcPtr -> VGcPtr -> Bool
$c/= :: VGcPtr -> VGcPtr -> Bool
== :: VGcPtr -> VGcPtr -> Bool
$c== :: VGcPtr -> VGcPtr -> Bool
Eq, Int -> VGcPtr -> ShowS
[VGcPtr] -> ShowS
VGcPtr -> String
(Int -> VGcPtr -> ShowS)
-> (VGcPtr -> String) -> ([VGcPtr] -> ShowS) -> Show VGcPtr
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [VGcPtr] -> ShowS
$cshowList :: [VGcPtr] -> ShowS
show :: VGcPtr -> String
$cshow :: VGcPtr -> String
showsPrec :: Int -> VGcPtr -> ShowS
$cshowsPrec :: Int -> VGcPtr -> ShowS
Show )

-----------------------------------------------------------------------------
--              Global STG registers
-----------------------------------------------------------------------------
{-
Note [Overlapping global registers]

The backend might not faithfully implement the abstraction of the STG
machine with independent registers for different values of type
GlobalReg. Specifically, certain pairs of registers (r1, r2) may
overlap in the sense that a store to r1 invalidates the value in r2,
and vice versa.

Currently this occurs only on the x86_64 architecture where FloatReg n
and DoubleReg n are assigned the same microarchitectural register, in
order to allow functions to receive more Float# or Double# arguments
in registers (as opposed to on the stack).

There are no specific rules about which registers might overlap with
which other registers, but presumably it's safe to assume that nothing
will overlap with special registers like Sp or BaseReg.

Use CmmUtils.regsOverlap to determine whether two GlobalRegs overlap
on a particular platform. The instance Eq GlobalReg is syntactic
equality of STG registers and does not take overlap into
account. However it is still used in UserOfRegs/DefinerOfRegs and
there are likely still bugs there, beware!
-}

data GlobalReg
  -- Argument and return registers
  = VanillaReg                  -- pointers, unboxed ints and chars
        {-# UNPACK #-} !Int     -- its number
        VGcPtr

  | FloatReg            -- single-precision floating-point registers
        {-# UNPACK #-} !Int     -- its number

  | DoubleReg           -- double-precision floating-point registers
        {-# UNPACK #-} !Int     -- its number

  | LongReg             -- long int registers (64-bit, really)
        {-# UNPACK #-} !Int     -- its number

  | XmmReg                      -- 128-bit SIMD vector register
        {-# UNPACK #-} !Int     -- its number

  | YmmReg                      -- 256-bit SIMD vector register
        {-# UNPACK #-} !Int     -- its number

  | ZmmReg                      -- 512-bit SIMD vector register
        {-# UNPACK #-} !Int     -- its number

  -- STG registers
  | Sp                  -- Stack ptr; points to last occupied stack location.
  | SpLim               -- Stack limit
  | Hp                  -- Heap ptr; points to last occupied heap location.
  | HpLim               -- Heap limit register
  | CCCS                -- Current cost-centre stack
  | CurrentTSO          -- pointer to current thread's TSO
  | CurrentNursery      -- pointer to allocation area
  | HpAlloc             -- allocation count for heap check failure

                -- We keep the address of some commonly-called
                -- functions in the register table, to keep code
                -- size down:
  | EagerBlackholeInfo  -- stg_EAGER_BLACKHOLE_info
  | GCEnter1            -- stg_gc_enter_1
  | GCFun               -- stg_gc_fun

  -- Base offset for the register table, used for accessing registers
  -- which do not have real registers assigned to them.  This register
  -- will only appear after we have expanded GlobalReg into memory accesses
  -- (where necessary) in the native code generator.
  | BaseReg

  -- The register used by the platform for the C stack pointer. This is
  -- a break in the STG abstraction used exclusively to setup stack unwinding
  -- information.
  | MachSp

  -- The is a dummy register used to indicate to the stack unwinder where
  -- a routine would return to.
  | UnwindReturnReg

  -- Base Register for PIC (position-independent code) calculations
  -- Only used inside the native code generator. It's exact meaning differs
  -- from platform to platform (see module PositionIndependentCode).
  | PicBaseReg

  deriving( Int -> GlobalReg -> ShowS
[GlobalReg] -> ShowS
GlobalReg -> String
(Int -> GlobalReg -> ShowS)
-> (GlobalReg -> String)
-> ([GlobalReg] -> ShowS)
-> Show GlobalReg
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [GlobalReg] -> ShowS
$cshowList :: [GlobalReg] -> ShowS
show :: GlobalReg -> String
$cshow :: GlobalReg -> String
showsPrec :: Int -> GlobalReg -> ShowS
$cshowsPrec :: Int -> GlobalReg -> ShowS
Show )

instance Eq GlobalReg where
   VanillaReg i :: Int
i _ == :: GlobalReg -> GlobalReg -> Bool
== VanillaReg j :: Int
j _ = Int
iInt -> Int -> Bool
forall a. Eq a => a -> a -> Bool
==Int
j -- Ignore type when seeking clashes
   FloatReg i :: Int
i == FloatReg j :: Int
j = Int
iInt -> Int -> Bool
forall a. Eq a => a -> a -> Bool
==Int
j
   DoubleReg i :: Int
i == DoubleReg j :: Int
j = Int
iInt -> Int -> Bool
forall a. Eq a => a -> a -> Bool
==Int
j
   LongReg i :: Int
i == LongReg j :: Int
j = Int
iInt -> Int -> Bool
forall a. Eq a => a -> a -> Bool
==Int
j
   XmmReg i :: Int
i == XmmReg j :: Int
j = Int
iInt -> Int -> Bool
forall a. Eq a => a -> a -> Bool
==Int
j
   YmmReg i :: Int
i == YmmReg j :: Int
j = Int
iInt -> Int -> Bool
forall a. Eq a => a -> a -> Bool
==Int
j
   ZmmReg i :: Int
i == ZmmReg j :: Int
j = Int
iInt -> Int -> Bool
forall a. Eq a => a -> a -> Bool
==Int
j
   Sp == Sp = Bool
True
   SpLim == SpLim = Bool
True
   Hp == Hp = Bool
True
   HpLim == HpLim = Bool
True
   CCCS == CCCS = Bool
True
   CurrentTSO == CurrentTSO = Bool
True
   CurrentNursery == CurrentNursery = Bool
True
   HpAlloc == HpAlloc = Bool
True
   EagerBlackholeInfo == EagerBlackholeInfo = Bool
True
   GCEnter1 == GCEnter1 = Bool
True
   GCFun == GCFun = Bool
True
   BaseReg == BaseReg = Bool
True
   MachSp == MachSp = Bool
True
   UnwindReturnReg == UnwindReturnReg = Bool
True
   PicBaseReg == PicBaseReg = Bool
True
   _r1 :: GlobalReg
_r1 == _r2 :: GlobalReg
_r2 = Bool
False

instance Ord GlobalReg where
   compare :: GlobalReg -> GlobalReg -> Ordering
compare (VanillaReg i :: Int
i _) (VanillaReg j :: Int
j _) = Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int
i Int
j
     -- Ignore type when seeking clashes
   compare (FloatReg i :: Int
i)  (FloatReg  j :: Int
j) = Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int
i Int
j
   compare (DoubleReg i :: Int
i) (DoubleReg j :: Int
j) = Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int
i Int
j
   compare (LongReg i :: Int
i)   (LongReg   j :: Int
j) = Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int
i Int
j
   compare (XmmReg i :: Int
i)    (XmmReg    j :: Int
j) = Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int
i Int
j
   compare (YmmReg i :: Int
i)    (YmmReg    j :: Int
j) = Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int
i Int
j
   compare (ZmmReg i :: Int
i)    (ZmmReg    j :: Int
j) = Int -> Int -> Ordering
forall a. Ord a => a -> a -> Ordering
compare Int
i Int
j
   compare Sp Sp = Ordering
EQ
   compare SpLim SpLim = Ordering
EQ
   compare Hp Hp = Ordering
EQ
   compare HpLim HpLim = Ordering
EQ
   compare CCCS CCCS = Ordering
EQ
   compare CurrentTSO CurrentTSO = Ordering
EQ
   compare CurrentNursery CurrentNursery = Ordering
EQ
   compare HpAlloc HpAlloc = Ordering
EQ
   compare EagerBlackholeInfo EagerBlackholeInfo = Ordering
EQ
   compare GCEnter1 GCEnter1 = Ordering
EQ
   compare GCFun GCFun = Ordering
EQ
   compare BaseReg BaseReg = Ordering
EQ
   compare MachSp MachSp = Ordering
EQ
   compare UnwindReturnReg UnwindReturnReg = Ordering
EQ
   compare PicBaseReg PicBaseReg = Ordering
EQ
   compare (VanillaReg _ _) _ = Ordering
LT
   compare _ (VanillaReg _ _) = Ordering
GT
   compare (FloatReg _) _     = Ordering
LT
   compare _ (FloatReg _)     = Ordering
GT
   compare (DoubleReg _) _    = Ordering
LT
   compare _ (DoubleReg _)    = Ordering
GT
   compare (LongReg _) _      = Ordering
LT
   compare _ (LongReg _)      = Ordering
GT
   compare (XmmReg _) _       = Ordering
LT
   compare _ (XmmReg _)       = Ordering
GT
   compare (YmmReg _) _       = Ordering
LT
   compare _ (YmmReg _)       = Ordering
GT
   compare (ZmmReg _) _       = Ordering
LT
   compare _ (ZmmReg _)       = Ordering
GT
   compare Sp _ = Ordering
LT
   compare _ Sp = Ordering
GT
   compare SpLim _ = Ordering
LT
   compare _ SpLim = Ordering
GT
   compare Hp _ = Ordering
LT
   compare _ Hp = Ordering
GT
   compare HpLim _ = Ordering
LT
   compare _ HpLim = Ordering
GT
   compare CCCS _ = Ordering
LT
   compare _ CCCS = Ordering
GT
   compare CurrentTSO _ = Ordering
LT
   compare _ CurrentTSO = Ordering
GT
   compare CurrentNursery _ = Ordering
LT
   compare _ CurrentNursery = Ordering
GT
   compare HpAlloc _ = Ordering
LT
   compare _ HpAlloc = Ordering
GT
   compare GCEnter1 _ = Ordering
LT
   compare _ GCEnter1 = Ordering
GT
   compare GCFun _ = Ordering
LT
   compare _ GCFun = Ordering
GT
   compare BaseReg _ = Ordering
LT
   compare _ BaseReg = Ordering
GT
   compare MachSp _ = Ordering
LT
   compare _ MachSp = Ordering
GT
   compare UnwindReturnReg _ = Ordering
LT
   compare _ UnwindReturnReg = Ordering
GT
   compare EagerBlackholeInfo _ = Ordering
LT
   compare _ EagerBlackholeInfo = Ordering
GT

-- convenient aliases
baseReg, spReg, hpReg, spLimReg, hpLimReg, nodeReg,
  currentTSOReg, currentNurseryReg, hpAllocReg, cccsReg  :: CmmReg
baseReg :: CmmReg
baseReg = GlobalReg -> CmmReg
CmmGlobal GlobalReg
BaseReg
spReg :: CmmReg
spReg = GlobalReg -> CmmReg
CmmGlobal GlobalReg
Sp
hpReg :: CmmReg
hpReg = GlobalReg -> CmmReg
CmmGlobal GlobalReg
Hp
hpLimReg :: CmmReg
hpLimReg = GlobalReg -> CmmReg
CmmGlobal GlobalReg
HpLim
spLimReg :: CmmReg
spLimReg = GlobalReg -> CmmReg
CmmGlobal GlobalReg
SpLim
nodeReg :: CmmReg
nodeReg = GlobalReg -> CmmReg
CmmGlobal GlobalReg
node
currentTSOReg :: CmmReg
currentTSOReg = GlobalReg -> CmmReg
CmmGlobal GlobalReg
CurrentTSO
currentNurseryReg :: CmmReg
currentNurseryReg = GlobalReg -> CmmReg
CmmGlobal GlobalReg
CurrentNursery
hpAllocReg :: CmmReg
hpAllocReg = GlobalReg -> CmmReg
CmmGlobal GlobalReg
HpAlloc
cccsReg :: CmmReg
cccsReg = GlobalReg -> CmmReg
CmmGlobal GlobalReg
CCCS

node :: GlobalReg
node :: GlobalReg
node = Int -> VGcPtr -> GlobalReg
VanillaReg 1 VGcPtr
VGcPtr

globalRegType :: DynFlags -> GlobalReg -> CmmType
globalRegType :: DynFlags -> GlobalReg -> CmmType
globalRegType dflags :: DynFlags
dflags (VanillaReg _ VGcPtr)    = DynFlags -> CmmType
gcWord DynFlags
dflags
globalRegType dflags :: DynFlags
dflags (VanillaReg _ VNonGcPtr) = DynFlags -> CmmType
bWord DynFlags
dflags
globalRegType _      (FloatReg _)      = Width -> CmmType
cmmFloat Width
W32
globalRegType _      (DoubleReg _)     = Width -> CmmType
cmmFloat Width
W64
globalRegType _      (LongReg _)       = Width -> CmmType
cmmBits Width
W64
globalRegType _      (XmmReg _)        = Int -> CmmType -> CmmType
cmmVec 4 (Width -> CmmType
cmmBits Width
W32)
globalRegType _      (YmmReg _)        = Int -> CmmType -> CmmType
cmmVec 8 (Width -> CmmType
cmmBits Width
W32)
globalRegType _      (ZmmReg _)        = Int -> CmmType -> CmmType
cmmVec 16 (Width -> CmmType
cmmBits Width
W32)

globalRegType dflags :: DynFlags
dflags Hp                = DynFlags -> CmmType
gcWord DynFlags
dflags
                                            -- The initialiser for all
                                            -- dynamically allocated closures
globalRegType dflags :: DynFlags
dflags _                 = DynFlags -> CmmType
bWord DynFlags
dflags

isArgReg :: GlobalReg -> Bool
isArgReg :: GlobalReg -> Bool
isArgReg (VanillaReg {}) = Bool
True
isArgReg (FloatReg {})   = Bool
True
isArgReg (DoubleReg {})  = Bool
True
isArgReg (LongReg {})    = Bool
True
isArgReg (XmmReg {})     = Bool
True
isArgReg (YmmReg {})     = Bool
True
isArgReg (ZmmReg {})     = Bool
True
isArgReg _               = Bool
False