ShortcutJump.hs 1.83 KB
Newer Older
1
module SPARC.ShortcutJump (
2 3 4 5 6
        JumpDest(..), getJumpDestBlockId,
        canShortcut,
        shortcutJump,
        shortcutStatics,
        shortBlockId
7 8 9 10 11 12 13 14 15
)

where

import SPARC.Instr
import SPARC.Imm

import CLabel
import BlockId
16
import Cmm
17 18 19 20

import Panic


21 22 23
data JumpDest
        = DestBlockId BlockId
        | DestImm Imm
24

25 26 27 28
getJumpDestBlockId :: JumpDest -> Maybe BlockId
getJumpDestBlockId (DestBlockId bid) = Just bid
getJumpDestBlockId _                 = Nothing

29 30 31 32 33 34 35 36 37 38

canShortcut :: Instr -> Maybe JumpDest
canShortcut _ = Nothing


shortcutJump :: (BlockId -> Maybe JumpDest) -> Instr -> Instr
shortcutJump _ other = other



39 40 41 42 43
shortcutStatics :: (BlockId -> Maybe JumpDest) -> CmmStatics -> CmmStatics
shortcutStatics fn (Statics lbl statics)
  = Statics lbl $ map (shortcutStatic fn) statics
  -- we need to get the jump tables, so apply the mapping to the entries
  -- of a CmmData too.
44

45 46 47 48
shortcutLabel :: (BlockId -> Maybe JumpDest) -> CLabel -> CLabel
shortcutLabel fn lab
  | Just uq <- maybeAsmTemp lab = shortBlockId fn (mkBlockId uq)
  | otherwise                   = lab
49

50 51
shortcutStatic :: (BlockId -> Maybe JumpDest) -> CmmStatic -> CmmStatic
shortcutStatic fn (CmmStaticLit (CmmLabel lab))
52
        = CmmStaticLit (CmmLabel (shortcutLabel fn lab))
53
shortcutStatic fn (CmmStaticLit (CmmLabelDiffOff lbl1 lbl2 off))
54
        = CmmStaticLit (CmmLabelDiffOff (shortcutLabel fn lbl1) lbl2 off)
55 56 57 58 59 60 61
-- slightly dodgy, we're ignoring the second label, but this
-- works with the way we use CmmLabelDiffOff for jump tables now.
shortcutStatic _ other_static
        = other_static


shortBlockId :: (BlockId -> Maybe JumpDest) -> BlockId -> CLabel
62
shortBlockId fn blockid =
63
   case fn blockid of
64
      Nothing -> blockLbl blockid
65 66 67
      Just (DestBlockId blockid')  -> shortBlockId fn blockid'
      Just (DestImm (ImmCLbl lbl)) -> lbl
      _other -> panic "shortBlockId"