/compiler/GHC/CmmToAsm/Reg/Linear/StackMap.hs

https://github.com/bgamari/ghc · Haskell · 61 lines · 26 code · 18 blank · 17 comment · 0 complexity · ca7347fc5cb0c9d6d678b7ac88de2f92 MD5 · raw file

  1. -- | The assignment of virtual registers to stack slots
  2. -- We have lots of stack slots. Memory-to-memory moves are a pain on most
  3. -- architectures. Therefore, we avoid having to generate memory-to-memory moves
  4. -- by simply giving every virtual register its own stack slot.
  5. -- The StackMap stack map keeps track of virtual register - stack slot
  6. -- associations and of which stack slots are still free. Once it has been
  7. -- associated, a stack slot is never "freed" or removed from the StackMap again,
  8. -- it remains associated until we are done with the current CmmProc.
  9. --
  10. module GHC.CmmToAsm.Reg.Linear.StackMap (
  11. StackSlot,
  12. StackMap(..),
  13. emptyStackMap,
  14. getStackSlotFor,
  15. getStackUse
  16. )
  17. where
  18. import GHC.Prelude
  19. import GHC.Types.Unique.FM
  20. import GHC.Types.Unique
  21. -- | Identifier for a stack slot.
  22. type StackSlot = Int
  23. data StackMap
  24. = StackMap
  25. { -- | The slots that are still available to be allocated.
  26. stackMapNextFreeSlot :: !Int
  27. -- See Note [UniqFM and the register allocator]
  28. -- | Assignment of vregs to stack slots.
  29. , stackMapAssignment :: UniqFM Unique StackSlot }
  30. -- | An empty stack map, with all slots available.
  31. emptyStackMap :: StackMap
  32. emptyStackMap = StackMap 0 emptyUFM
  33. -- | If this vreg unique already has a stack assignment then return the slot number,
  34. -- otherwise allocate a new slot, and update the map.
  35. --
  36. getStackSlotFor :: StackMap -> Unique -> (StackMap, Int)
  37. getStackSlotFor fs@(StackMap _ reserved) reg
  38. | Just slot <- lookupUFM reserved reg = (fs, slot)
  39. getStackSlotFor (StackMap freeSlot reserved) reg =
  40. (StackMap (freeSlot+1) (addToUFM reserved reg freeSlot), freeSlot)
  41. -- | Return the number of stack slots that were allocated
  42. getStackUse :: StackMap -> Int
  43. getStackUse (StackMap freeSlot _) = freeSlot