/compiler/GHC/CmmToAsm/Reg/Utils.hs

https://github.com/bgamari/ghc · Haskell · 58 lines · 9 code · 5 blank · 44 comment · 0 complexity · cf135cfbde189b0e52f1520b667d19f4 MD5 · raw file

  1. module GHC.CmmToAsm.Reg.Utils
  2. ( toRegMap, toVRegMap )
  3. where
  4. {- Note [UniqFM and the register allocator]
  5. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  6. Before UniqFM had a key type the register allocator
  7. wasn't picky about key types, using VirtualReg, Reg
  8. and Unique at various use sites for the same map.
  9. This is safe.
  10. * The Unique values come from registers at various
  11. points where we lose a reference to the original
  12. register value, but the unique is still valid.
  13. * VirtualReg is a subset of the registers in Reg's type.
  14. Making a value of VirtualReg into a Reg in fact doesn't
  15. change its unique. This is because Reg consists of virtual
  16. regs and real regs, whose unique values do not overlap.
  17. * Since the code was written in the assumption that keys are
  18. not typed it's hard to reverse this assumption now. So we get
  19. some gnarly but correct code where we often pass around Uniques
  20. and switch between using Uniques, VirtualReg and RealReg as keys
  21. of the same map. These issues were always there. But with the
  22. now-typed keys they become visible. It's a classic case of not all
  23. correct programs type checking.
  24. We reduce some of the burden by providing a way to cast
  25. UniqFM VirtualReg a
  26. to
  27. UniqFM Reg a
  28. in this module. This is safe as Reg is the sum of VirtualReg and
  29. RealReg. With each kind of register keeping the same unique when
  30. treated as Reg.
  31. TODO: If you take offense to this I encourage you to refactor this
  32. code. I'm sure we can do with less casting of keys and direct use
  33. of uniques. It might also be reasonable to just use a IntMap directly
  34. instead of dealing with UniqFM at all.
  35. -}
  36. import GHC.Types.Unique.FM
  37. import GHC.Platform.Reg
  38. -- These should hopefully be zero cost.
  39. toRegMap :: UniqFM VirtualReg elt -> UniqFM Reg elt
  40. toRegMap = unsafeCastUFMKey
  41. toVRegMap :: UniqFM Reg elt -> UniqFM VirtualReg elt
  42. toVRegMap = unsafeCastUFMKey