CmmCommonBlockElim.hs 9.39 KB
Newer Older
1 2
{-# LANGUAGE GADTs #-}
module CmmCommonBlockElim
dias@eecs.harvard.edu's avatar
dias@eecs.harvard.edu committed
3 4 5 6 7
  ( elimCommonBlocks
  )
where


8
import BlockId
9
import Cmm
10
import CmmUtils
Simon Marlow's avatar
Simon Marlow committed
11
import CmmContFlowOpt
12
import Prelude hiding (iterate, succ, unzip, zip)
dias@eecs.harvard.edu's avatar
dias@eecs.harvard.edu committed
13

Simon Marlow's avatar
Simon Marlow committed
14
import Hoopl hiding (ChangeFlag)
15
import Data.Bits
Peter Wortmann's avatar
Peter Wortmann committed
16
import Data.Maybe (mapMaybe)
Ian Lynagh's avatar
Ian Lynagh committed
17
import qualified Data.List as List
18
import Data.Word
Peter Wortmann's avatar
Peter Wortmann committed
19
import qualified Data.Map as M
dias@eecs.harvard.edu's avatar
dias@eecs.harvard.edu committed
20 21 22 23
import Outputable
import UniqFM

my_trace :: String -> SDoc -> a -> a
24
my_trace = if False then pprTrace else \_ _ a -> a
dias@eecs.harvard.edu's avatar
dias@eecs.harvard.edu committed
25

26 27 28
-- -----------------------------------------------------------------------------
-- Eliminate common blocks

dias@eecs.harvard.edu's avatar
dias@eecs.harvard.edu committed
29 30 31 32 33 34 35 36 37 38 39 40 41
-- If two blocks are identical except for the label on the first node,
-- then we can eliminate one of the blocks. To ensure that the semantics
-- of the program are preserved, we have to rewrite each predecessor of the
-- eliminated block to proceed with the block we keep.

-- The algorithm iterates over the blocks in the graph,
-- checking whether it has seen another block that is equal modulo labels.
-- If so, then it adds an entry in a map indicating that the new block
-- is made redundant by the old block.
-- Otherwise, it is added to the useful blocks.

-- TODO: Use optimization fuel
elimCommonBlocks :: CmmGraph -> CmmGraph
Peter Wortmann's avatar
Peter Wortmann committed
42
elimCommonBlocks g = replaceLabels env $ copyTicks env g
43 44 45
  where
     env = iterate hashed_blocks mapEmpty
     hashed_blocks = map (\b -> (hash_block b, b)) $ postorderDfs g
dias@eecs.harvard.edu's avatar
dias@eecs.harvard.edu committed
46 47

-- Iterate over the blocks until convergence
48 49 50 51 52 53 54 55 56 57 58
iterate :: [(HashCode,CmmBlock)] -> BlockEnv BlockId -> BlockEnv BlockId
iterate blocks subst =
  case foldl common_block (False, emptyUFM, subst) blocks of
    (changed,  _, subst)
       | changed   -> iterate blocks subst
       | otherwise -> subst

type State  = (ChangeFlag, UniqFM [CmmBlock], BlockEnv BlockId)

type ChangeFlag = Bool
type HashCode = Int
dias@eecs.harvard.edu's avatar
dias@eecs.harvard.edu committed
59 60

-- Try to find a block that is equal (or ``common'') to b.
61 62
common_block :: State -> (HashCode, CmmBlock) -> State
common_block (old_change, bmap, subst) (hash, b) =
63
  case lookupUFM bmap hash of
Ian Lynagh's avatar
Ian Lynagh committed
64
    Just bs -> case (List.find (eqBlockBodyWith (eqBid subst) b) bs,
65 66 67
                     mapLookup bid subst) of
                 (Just b', Nothing)                         -> addSubst b'
                 (Just b', Just b'') | entryLabel b' /= b'' -> addSubst b'
68
                                     | otherwise -> (old_change, bmap, subst)
69
                 _ -> (old_change, addToUFM bmap hash (b : bs), subst)
Simon Marlow's avatar
Simon Marlow committed
70
    Nothing -> (old_change, addToUFM bmap hash [b], subst)
71
  where bid = entryLabel b
72
        addSubst b' = my_trace "found new common block" (ppr bid <> char '=' <> ppr (entryLabel b')) $
73 74 75 76 77 78 79 80
                      (True, bmap, mapInsert bid (entryLabel b') subst)


-- -----------------------------------------------------------------------------
-- Hashing and equality on blocks

-- Below here is mostly boilerplate: hashing blocks ignoring labels,
-- and comparing blocks modulo a label mapping.
dias@eecs.harvard.edu's avatar
dias@eecs.harvard.edu committed
81 82 83 84

-- To speed up comparisons, we hash each basic block modulo labels.
-- The hashing is a bit arbitrary (the numbers are completely arbitrary),
-- but it should be fast and good enough.
85
hash_block :: CmmBlock -> HashCode
86 87
hash_block block =
  fromIntegral (foldBlockNodesB3 (hash_fst, hash_mid, hash_lst) block (0 :: Word32) .&. (0x7fffffff :: Word32))
88
  -- UniqFM doesn't like negative Ints
89 90 91 92 93
  where hash_fst _ h = h
        hash_mid m h = hash_node m + h `shiftL` 1
        hash_lst m h = hash_node m + h `shiftL` 1

        hash_node :: CmmNode O x -> Word32
Peter Wortmann's avatar
Peter Wortmann committed
94
        hash_node n | dont_care n = 0 -- don't care
Peter Wortmann's avatar
Peter Wortmann committed
95
        hash_node (CmmUnwind _ e) = hash_e e
96 97 98
        hash_node (CmmAssign r e) = hash_reg r + hash_e e
        hash_node (CmmStore e e') = hash_e e + hash_e e'
        hash_node (CmmUnsafeForeignCall t _ as) = hash_tgt t + hash_list hash_e as
99
        hash_node (CmmBranch _) = 23 -- NB. ignore the label
100
        hash_node (CmmCondBranch p _ _) = hash_e p
101
        hash_node (CmmCall e _ _ _ _ _) = hash_e e
102
        hash_node (CmmForeignCall t _ _ _ _ _ _) = hash_tgt t
103
        hash_node (CmmSwitch e _) = hash_e e
Peter Wortmann's avatar
Peter Wortmann committed
104
        hash_node _ = error "hash_node: unknown Cmm node!"
105

106
        hash_reg :: CmmReg -> Word32
107
        hash_reg   (CmmLocal _) = 117
dias@eecs.harvard.edu's avatar
dias@eecs.harvard.edu committed
108
        hash_reg   (CmmGlobal _)    = 19
109

110
        hash_e :: CmmExpr -> Word32
dias@eecs.harvard.edu's avatar
dias@eecs.harvard.edu committed
111 112 113
        hash_e (CmmLit l) = hash_lit l
        hash_e (CmmLoad e _) = 67 + hash_e e
        hash_e (CmmReg r) = hash_reg r
114
        hash_e (CmmMachOp _ es) = hash_list hash_e es -- pessimal - no operator check
115
        hash_e (CmmRegOff r i) = hash_reg r + cvt i
116
        hash_e (CmmStackSlot _ _) = 13
117

118
        hash_lit :: CmmLit -> Word32
dias@eecs.harvard.edu's avatar
dias@eecs.harvard.edu committed
119 120
        hash_lit (CmmInt i _) = fromInteger i
        hash_lit (CmmFloat r _) = truncate r
121
        hash_lit (CmmVec ls) = hash_list hash_lit ls
dias@eecs.harvard.edu's avatar
dias@eecs.harvard.edu committed
122
        hash_lit (CmmLabel _) = 119 -- ugh
123 124
        hash_lit (CmmLabelOff _ i) = cvt $ 199 + i
        hash_lit (CmmLabelDiffOff _ _ i) = cvt $ 299 + i
125
        hash_lit (CmmBlock _) = 191 -- ugh
126
        hash_lit (CmmHighStackMark) = cvt 313
127

128 129
        hash_tgt (ForeignTarget e _) = hash_e e
        hash_tgt (PrimTarget _) = 31 -- lots of these
130 131 132

        hash_list f = foldl (\z x -> f x + z) (0::Word32)

133
        cvt = fromInteger . toInteger
Peter Wortmann's avatar
Peter Wortmann committed
134 135 136 137 138 139 140

-- | Ignore these node types for equality
dont_care :: CmmNode O x -> Bool
dont_care CmmComment {}  = True
dont_care CmmTick {}     = True
dont_care _other         = False

dias@eecs.harvard.edu's avatar
dias@eecs.harvard.edu committed
141 142 143
-- Utilities: equality and substitution on the graph.

-- Given a map ``subst'' from BlockID -> BlockID, we define equality.
144
eqBid :: BlockEnv BlockId -> BlockId -> BlockId -> Bool
dias@eecs.harvard.edu's avatar
dias@eecs.harvard.edu committed
145
eqBid subst bid bid' = lookupBid subst bid == lookupBid subst bid'
146
lookupBid :: BlockEnv BlockId -> BlockId -> BlockId
147
lookupBid subst bid = case mapLookup bid subst of
dias@eecs.harvard.edu's avatar
dias@eecs.harvard.edu committed
148 149 150
                        Just bid  -> lookupBid subst bid
                        Nothing -> bid

151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
-- Middle nodes and expressions can contain BlockIds, in particular in
-- CmmStackSlot and CmmBlock, so we have to use a special equality for
-- these.
--
eqMiddleWith :: (BlockId -> BlockId -> Bool)
             -> CmmNode O O -> CmmNode O O -> Bool
eqMiddleWith eqBid (CmmAssign r1 e1) (CmmAssign r2 e2)
  = r1 == r2 && eqExprWith eqBid e1 e2
eqMiddleWith eqBid (CmmStore l1 r1) (CmmStore l2 r2)
  = eqExprWith eqBid l1 l2 && eqExprWith eqBid r1 r2
eqMiddleWith eqBid (CmmUnsafeForeignCall t1 r1 a1)
                   (CmmUnsafeForeignCall t2 r2 a2)
  = t1 == t2 && r1 == r2 && and (zipWith (eqExprWith eqBid) a1 a2)
eqMiddleWith _ _ _ = False

eqExprWith :: (BlockId -> BlockId -> Bool)
           -> CmmExpr -> CmmExpr -> Bool
eqExprWith eqBid = eq
 where
  CmmLit l1          `eq` CmmLit l2          = eqLit l1 l2
  CmmLoad e1 _       `eq` CmmLoad e2 _       = e1 `eq` e2
  CmmReg r1          `eq` CmmReg r2          = r1==r2
  CmmRegOff r1 i1    `eq` CmmRegOff r2 i2    = r1==r2 && i1==i2
  CmmMachOp op1 es1  `eq` CmmMachOp op2 es2  = op1==op2 && es1 `eqs` es2
  CmmStackSlot a1 i1 `eq` CmmStackSlot a2 i2 = eqArea a1 a2 && i1==i2
  _e1                `eq` _e2                = False

  xs `eqs` ys = and (zipWith eq xs ys)

  eqLit (CmmBlock id1) (CmmBlock id2) = eqBid id1 id2
  eqLit l1 l2 = l1 == l2

  eqArea Old Old = True
  eqArea (Young id1) (Young id2) = eqBid id1 id2
  eqArea _ _ = False

Simon Marlow's avatar
Simon Marlow committed
187 188
-- Equality on the body of a block, modulo a function mapping block
-- IDs to block IDs.
dias@eecs.harvard.edu's avatar
dias@eecs.harvard.edu committed
189
eqBlockBodyWith :: (BlockId -> BlockId -> Bool) -> CmmBlock -> CmmBlock -> Bool
Simon Marlow's avatar
Simon Marlow committed
190
eqBlockBodyWith eqBid block block'
Peter Wortmann's avatar
Peter Wortmann committed
191
  = and (zipWith (eqMiddleWith eqBid) nodes nodes') &&
192
    eqLastWith eqBid l l'
Simon Marlow's avatar
Simon Marlow committed
193
  where (_,m,l)   = blockSplit block
Peter Wortmann's avatar
Peter Wortmann committed
194
        nodes     = filter (not . dont_care) (blockToList m)
Simon Marlow's avatar
Simon Marlow committed
195
        (_,m',l') = blockSplit block'
Peter Wortmann's avatar
Peter Wortmann committed
196
        nodes'    = filter (not . dont_care) (blockToList m')
197

198 199


200 201 202
eqLastWith :: (BlockId -> BlockId -> Bool) -> CmmNode O C -> CmmNode O C -> Bool
eqLastWith eqBid (CmmBranch bid1) (CmmBranch bid2) = eqBid bid1 bid2
eqLastWith eqBid (CmmCondBranch c1 t1 f1) (CmmCondBranch c2 t2 f2) =
203
  c1 == c2 && eqBid t1 t2 && eqBid f1 f2
204 205
eqLastWith eqBid (CmmCall t1 c1 g1 a1 r1 u1) (CmmCall t2 c2 g2 a2 r2 u2) =
  t1 == t2 && eqMaybeWith eqBid c1 c2 && a1 == a2 && r1 == r2 && u1 == u2 && g1 == g2
206 207
eqLastWith eqBid (CmmSwitch e1 bs1) (CmmSwitch e2 bs2) =
  e1 == e2 && eqListWith (eqMaybeWith eqBid) bs1 bs2
dias@eecs.harvard.edu's avatar
dias@eecs.harvard.edu committed
208 209
eqLastWith _ _ _ = False

210 211
eqListWith :: (a -> b -> Bool) -> [a] -> [b] -> Bool
eqListWith eltEq es es' = all (uncurry eltEq) (List.zip es es')
dias@eecs.harvard.edu's avatar
dias@eecs.harvard.edu committed
212 213 214 215 216

eqMaybeWith :: (a -> b -> Bool) -> Maybe a -> Maybe b -> Bool
eqMaybeWith eltEq (Just e) (Just e') = eltEq e e'
eqMaybeWith _ Nothing Nothing = True
eqMaybeWith _ _ _ = False
Peter Wortmann's avatar
Peter Wortmann committed
217 218 219 220 221 222 223 224

-- | Given a block map, ensure that all "target" blocks are covered by
-- the same ticks as the respective "source" blocks. This not only
-- means copying ticks, but also adjusting tick scopes where
-- necessary.
copyTicks :: BlockEnv BlockId -> CmmGraph -> CmmGraph
copyTicks env g
  | mapNull env = g
Peter Wortmann's avatar
Peter Wortmann committed
225 226 227
  | otherwise   = ofBlockMap (g_entry g) $ mapMap copyTo blockMap
  where -- Reverse block merge map
        blockMap = toBlockMap g
Peter Wortmann's avatar
Peter Wortmann committed
228 229
        revEnv = mapFoldWithKey insertRev M.empty env
        insertRev k x = M.insertWith (const (k:)) x [k]
Peter Wortmann's avatar
Peter Wortmann committed
230 231
        -- Copy ticks and scopes into the given block
        copyTo block = case M.lookup (entryLabel block) revEnv of
Peter Wortmann's avatar
Peter Wortmann committed
232
          Nothing -> block
Peter Wortmann's avatar
Peter Wortmann committed
233 234 235 236 237 238 239
          Just ls -> foldr copy block $ mapMaybe (flip mapLookup blockMap) ls
        copy from to =
          let ticks = blockTicks from
              CmmEntry  _   scp0        = firstNode from
              (CmmEntry lbl scp1, code) = blockSplitHead to
          in CmmEntry lbl (combineTickScopes scp0 scp1) `blockJoinHead`
             foldr blockCons code (map CmmTick ticks)