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