ProcPoint.hs 20.7 KB
Newer Older
1
{-# LANGUAGE GADTs, DisambiguateRecordFields, BangPatterns #-}
2

3
module GHC.Cmm.ProcPoint
4 5
    ( ProcPointSet, Status(..)
    , callProcPoints, minimalProcPointSet
Simon Marlow's avatar
Simon Marlow committed
6
    , splitAtProcPoints, procPointAnalysis
7
    , attachContInfoTables
8 9
    )
where
10

11
import GhcPrelude hiding (last, unzip, succ, zip)
12

13
import DynFlags
14 15 16 17 18 19 20 21
import GHC.Cmm.BlockId
import GHC.Cmm.CLabel
import GHC.Cmm
import GHC.Cmm.Ppr () -- For Outputable instances
import GHC.Cmm.Utils
import GHC.Cmm.Info
import GHC.Cmm.Liveness
import GHC.Cmm.Switch
David Eichmann's avatar
David Eichmann committed
22
import Data.List (sortBy)
23 24 25
import Maybes
import Control.Monad
import Outputable
John Ericson's avatar
John Ericson committed
26
import GHC.Platform
27
import UniqSupply
28 29 30 31 32
import GHC.Cmm.Dataflow.Block
import GHC.Cmm.Dataflow.Collections
import GHC.Cmm.Dataflow
import GHC.Cmm.Dataflow.Graph
import GHC.Cmm.Dataflow.Label
33 34 35 36

-- Compute a minimal set of proc points for a control-flow graph.

-- Determine a protocol for each proc point (which live variables will
37
-- be passed as arguments and which will be on the stack).
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89

{-
A proc point is a basic block that, after CPS transformation, will
start a new function.  The entry block of the original function is a
proc point, as is the continuation of each function call.
A third kind of proc point arises if we want to avoid copying code.
Suppose we have code like the following:

  f() {
    if (...) { ..1..; call foo(); ..2..}
    else     { ..3..; call bar(); ..4..}
    x = y + z;
    return x;
  }

The statement 'x = y + z' can be reached from two different proc
points: the continuations of foo() and bar().  We would prefer not to
put a copy in each continuation; instead we would like 'x = y + z' to
be the start of a new procedure to which the continuations can jump:

  f_cps () {
    if (...) { ..1..; push k_foo; jump foo_cps(); }
    else     { ..3..; push k_bar; jump bar_cps(); }
  }
  k_foo() { ..2..; jump k_join(y, z); }
  k_bar() { ..4..; jump k_join(y, z); }
  k_join(y, z) { x = y + z; return x; }

You might think then that a criterion to make a node a proc point is
that it is directly reached by two distinct proc points.  (Note
[Direct reachability].)  But this criterion is a bit too simple; for
example, 'return x' is also reached by two proc points, yet there is
no point in pulling it out of k_join.  A good criterion would be to
say that a node should be made a proc point if it is reached by a set
of proc points that is different than its immediate dominator.  NR
believes this criterion can be shown to produce a minimum set of proc
points, and given a dominator tree, the proc points can be chosen in
time linear in the number of blocks.  Lacking a dominator analysis,
however, we turn instead to an iterative solution, starting with no
proc points and adding them according to these rules:

  1. The entry block is a proc point.
  2. The continuation of a call is a proc point.
  3. A node is a proc point if it is directly reached by more proc
     points than one of its predecessors.

Because we don't understand the problem very well, we apply rule 3 at
most once per iteration, then recompute the reachability information.
(See Note [No simple dataflow].)  The choice of the new proc point is
arbitrary, and I don't know if the choice affects the final solution,
so I don't know if the number of proc points chosen is the
minimum---but the set will be minimal.
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116



Note [Proc-point analysis]
~~~~~~~~~~~~~~~~~~~~~~~~~~

Given a specified set of proc-points (a set of block-ids), "proc-point
analysis" figures out, for every block, which proc-point it belongs to.
All the blocks belonging to proc-point P will constitute a single
top-level C procedure.

A non-proc-point block B "belongs to" a proc-point P iff B is
reachable from P without going through another proc-point.

Invariant: a block B should belong to at most one proc-point; if it
belongs to two, that's a bug.

Note [Non-existing proc-points]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

On some architectures it might happen that the list of proc-points
computed before stack layout pass will be invalidated by the stack
layout. This will happen if stack layout removes from the graph
blocks that were determined to be proc-points. Later on in the pipeline
we use list of proc-points to perform [Proc-point analysis], but
if a proc-point does not exist anymore then we will get compiler panic.
See #8205.
117 118
-}

119
type ProcPointSet = LabelSet
120 121 122 123 124 125 126 127 128 129 130 131 132

data Status
  = ReachedBy ProcPointSet  -- set of proc points that directly reach the block
  | ProcPoint               -- this block is itself a proc point

instance Outputable Status where
  ppr (ReachedBy ps)
      | setNull ps = text "<not-reached>"
      | otherwise = text "reached by" <+>
                    (hsep $ punctuate comma $ map ppr $ setElems ps)
  ppr ProcPoint = text "<procpt>"

--------------------------------------------------
133
-- Proc point analysis
134

135 136
-- Once you know what the proc-points are, figure out
-- what proc-points each block is reachable from
137
-- See Note [Proc-point analysis]
138
procPointAnalysis :: ProcPointSet -> CmmGraph -> LabelMap Status
139
procPointAnalysis procPoints cmmGraph@(CmmGraph {g_graph = graph}) =
140
    analyzeCmmFwd procPointLattice procPointTransfer cmmGraph initProcPoints
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
  where
    initProcPoints =
        mkFactBase
            procPointLattice
            [ (id, ProcPoint)
            | id <- setElems procPoints
            -- See Note [Non-existing proc-points]
            , id `setMember` labelsInGraph
            ]
    labelsInGraph = labelsDefined graph

procPointTransfer :: TransferFun Status
procPointTransfer block facts =
    let label = entryLabel block
        !fact = case getFact procPointLattice label facts of
            ProcPoint -> ReachedBy $! setSingleton label
            f -> f
        result = map (\id -> (id, fact)) (successors block)
    in mkFactBase procPointLattice result

procPointLattice :: DataflowLattice Status
procPointLattice = DataflowLattice unreached add_to
  where
    unreached = ReachedBy setEmpty
    add_to (OldFact ProcPoint) _ = NotChanged ProcPoint
    add_to _ (NewFact ProcPoint) = Changed ProcPoint -- because of previous case
    add_to (OldFact (ReachedBy p)) (NewFact (ReachedBy p'))
        | setSize union > setSize p = Changed (ReachedBy union)
        | otherwise = NotChanged (ReachedBy p)
      where
        union = setUnion p' p
172 173 174 175 176 177 178 179

----------------------------------------------------------------------

-- It is worth distinguishing two sets of proc points: those that are
-- induced by calls in the original graph and those that are
-- introduced because they're reachable from multiple proc points.
--
-- Extract the set of Continuation BlockIds, see Note [Continuation BlockIds].
180
callProcPoints      :: CmmGraph -> ProcPointSet
181 182 183
callProcPoints g = foldlGraphBlocks add (setSingleton (g_entry g)) g
  where add :: LabelSet -> CmmBlock -> LabelSet
        add set b = case lastNode b of
184 185 186 187
                      CmmCall {cml_cont = Just k} -> setInsert k set
                      CmmForeignCall {succ=k}     -> setInsert k set
                      _ -> set

188
minimalProcPointSet :: Platform -> ProcPointSet -> CmmGraph
189
                    -> UniqSM ProcPointSet
190 191
-- Given the set of successors of calls (which must be proc-points)
-- figure out the minimal set of necessary proc-points
192
minimalProcPointSet platform callProcPoints g
193
  = extendPPSet platform g (revPostorder g) callProcPoints
194

195 196
extendPPSet
    :: Platform -> CmmGraph -> [CmmBlock] -> ProcPointSet -> UniqSM ProcPointSet
197
extendPPSet platform g blocks procPoints =
198
    let env = procPointAnalysis procPoints g
199
        add pps block = let id = entryLabel block
200 201 202
                        in  case mapLookup id env of
                              Just ProcPoint -> setInsert id pps
                              _ -> pps
203
        procPoints' = foldlGraphBlocks add setEmpty g
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
        newPoints = mapMaybe ppSuccessor blocks
        newPoint  = listToMaybe newPoints
        ppSuccessor b =
            let nreached id = case mapLookup id env `orElse`
                                    pprPanic "no ppt" (ppr id <+> ppr b) of
                                ProcPoint -> 1
                                ReachedBy ps -> setSize ps
                block_procpoints = nreached (entryLabel b)
                -- | Looking for a successor of b that is reached by
                -- more proc points than b and is not already a proc
                -- point.  If found, it can become a proc point.
                newId succ_id = not (setMember succ_id procPoints') &&
                                nreached succ_id > block_procpoints
            in  listToMaybe $ filter newId $ successors b

    in case newPoint of
220 221 222 223 224
         Just id ->
             if setMember id procPoints'
                then panic "added old proc pt"
                else extendPPSet platform g blocks (setInsert id procPoints')
         Nothing -> return procPoints'
225 226 227 228 229 230 231 232 233 234 235 236


-- At this point, we have found a set of procpoints, each of which should be
-- the entry point of a procedure.
-- Now, we create the procedure for each proc point,
-- which requires that we:
-- 1. build a map from proc points to the blocks reachable from the proc point
-- 2. turn each branch to a proc point into a jump
-- 3. turn calls and returns into jumps
-- 4. build info tables for the procedures -- and update the info table for
--    the SRTs in the entry procedure as well.
-- Input invariant: A block should only be reachable from a single ProcPoint.
237 238
-- ToDo: use the _ret naming convention that the old code generator
-- used. -- EZY
239
splitAtProcPoints :: DynFlags -> CLabel -> ProcPointSet-> ProcPointSet -> LabelMap Status ->
240
                     CmmDecl -> UniqSM [CmmDecl]
241
splitAtProcPoints dflags entry_label callPPs procPoints procMap
242
                  (CmmProc (TopInfo {info_tbls = info_tbls})
243
                           top_l _ g@(CmmGraph {g_entry=entry})) =
244
  do -- Build a map from procpoints to the blocks they reach
245
     let add_block
246 247
             :: LabelMap (LabelMap CmmBlock)
             -> CmmBlock
248
             -> LabelMap (LabelMap CmmBlock)
249
         add_block graphEnv b =
250 251 252 253 254
           case mapLookup bid procMap of
             Just ProcPoint -> add graphEnv bid bid b
             Just (ReachedBy set) ->
               case setElems set of
                 []   -> graphEnv
255
                 [id] -> add graphEnv id bid b
256
                 _    -> panic "Each block should be reachable from only one ProcPoint"
257
             Nothing -> graphEnv
258 259 260 261
           where bid = entryLabel b
         add graphEnv procId bid b = mapInsert procId graph' graphEnv
               where graph  = mapLookup procId graphEnv `orElse` mapEmpty
                     graph' = mapInsert bid b graph
262

263 264 265 266 267
     let liveness = cmmGlobalLiveness dflags g
     let ppLiveness pp = filter isArgReg $
                         regSetToList $
                         expectJust "ppLiveness" $ mapLookup pp liveness

268
     graphEnv <- return $ foldlGraphBlocks add_block mapEmpty g
269

270 271
     -- Build a map from proc point BlockId to pairs of:
     --  * Labels for their new procedures
272 273
     --  * Labels for the info tables of their new procedures (only if
     --    the proc point is a callPP)
274
     -- Due to common blockification, we may overestimate the set of procpoints.
275
     let add_label map pp = mapInsert pp lbls map
276 277
           where lbls | pp == entry = (entry_label, fmap cit_lbl (mapLookup entry info_tbls))
                      | otherwise   = (block_lbl, guard (setMember pp callPPs) >>
Moritz Angermann's avatar
Moritz Angermann committed
278 279 280
                                                    Just info_table_lbl)
                      where block_lbl      = blockLbl pp
                            info_table_lbl = infoTblLbl pp
281 282

         procLabels :: LabelMap (CLabel, Maybe CLabel)
283 284
         procLabels = foldl' add_label mapEmpty
                             (filter (flip mapMember (toBlockMap g)) (setElems procPoints))
285

286 287
     -- In each new graph, add blocks jumping off to the new procedures,
     -- and replace branches to procpoints with branches to the jump-off blocks
288 289 290 291 292
     let add_jump_block
             :: (LabelMap Label, [CmmBlock])
             -> (Label, CLabel)
             -> UniqSM (LabelMap Label, [CmmBlock])
         add_jump_block (env, bs) (pp, l) =
293
           do bid <- liftM mkBlockId getUniqueM
Peter Wortmann's avatar
Peter Wortmann committed
294
              let b = blockJoin (CmmEntry bid GlobalScope) emptyBlock jump
295 296
                  live = ppLiveness pp
                  jump = CmmCall (CmmLit (CmmLabel l)) Nothing live 0 0 0
297
              return (mapInsert pp bid env, b : bs)
298

299 300 301 302
         add_jumps
             :: LabelMap CmmGraph
             -> (Label, LabelMap CmmBlock)
             -> UniqSM (LabelMap CmmGraph)
303
         add_jumps newGraphEnv (ppId, blockEnv) =
304
           do let needed_jumps = -- find which procpoints we currently branch to
305
                    mapFoldr add_if_branch_to_pp [] blockEnv
306 307 308 309
                  add_if_branch_to_pp :: CmmBlock -> [(BlockId, CLabel)] -> [(BlockId, CLabel)]
                  add_if_branch_to_pp block rst =
                    case lastNode block of
                      CmmBranch id          -> add_if_pp id rst
310
                      CmmCondBranch _ ti fi _ -> add_if_pp ti (add_if_pp fi rst)
311
                      CmmSwitch _ ids       -> foldr add_if_pp rst $ switchTargetsToList ids
312
                      _                     -> rst
313 314 315 316 317 318 319 320 321 322 323

                  -- when jumping to a PP that has an info table, if
                  -- tablesNextToCode is off we must jump to the entry
                  -- label instead.
                  jump_label (Just info_lbl) _
                             | tablesNextToCode dflags = info_lbl
                             | otherwise               = toEntryLbl info_lbl
                  jump_label Nothing         block_lbl = block_lbl

                  add_if_pp id rst = case mapLookup id procLabels of
                                       Just (lbl, mb_info_lbl) -> (id, jump_label mb_info_lbl lbl) : rst
324
                                       Nothing                 -> rst
325 326 327 328 329 330 331 332
              (jumpEnv, jumpBlocks) <-
                 foldM add_jump_block (mapEmpty, []) needed_jumps
                  -- update the entry block
              let b = expectJust "block in env" $ mapLookup ppId blockEnv
                  blockEnv' = mapInsert ppId b blockEnv
                  -- replace branches to procpoints with branches to jumps
                  blockEnv'' = toBlockMap $ replaceBranches jumpEnv $ ofBlockMap ppId blockEnv'
                  -- add the jump blocks to the graph
333
                  blockEnv''' = foldl' (flip addBlock) blockEnv'' jumpBlocks
334
              let g' = ofBlockMap ppId blockEnv'''
335 336
              -- pprTrace "g' pre jumps" (ppr g') $ do
              return (mapInsert ppId g' newGraphEnv)
337

338
     graphEnv <- foldM add_jumps mapEmpty $ mapToList graphEnv
339

340 341 342 343
     let to_proc (bid, g)
             | bid == entry
             =  CmmProc (TopInfo {info_tbls  = info_tbls,
                                  stack_info = stack_info})
344
                        top_l live g'
345 346 347 348 349
             | otherwise
             = case expectJust "pp label" $ mapLookup bid procLabels of
                 (lbl, Just info_lbl)
                    -> CmmProc (TopInfo { info_tbls = mapSingleton (g_entry g) (mkEmptyContInfoTable info_lbl)
                                        , stack_info=stack_info})
350
                               lbl live g'
351 352
                 (lbl, Nothing)
                    -> CmmProc (TopInfo {info_tbls = mapEmpty, stack_info=stack_info})
353
                               lbl live g'
354
                where
355 356
                 g' = replacePPIds g
                 live = ppLiveness (g_entry g')
357 358 359
                 stack_info = StackInfo { arg_space = 0
                                        , updfr_space =  Nothing
                                        , do_layout = True }
360
                               -- cannot use panic, this is printed by -ddump-cmm
361 362 363 364 365

         -- References to procpoint IDs can now be replaced with the
         -- infotable's label
         replacePPIds g = {-# SCC "replacePPIds" #-}
                          mapGraphNodes (id, mapExp repl, mapExp repl) g
366
           where repl e@(CmmLit (CmmBlock bid)) =
367
                   case mapLookup bid procLabels of
368 369
                     Just (_, Just info_lbl)  -> CmmLit (CmmLabel info_lbl)
                     _ -> e
370
                 repl e = e
371 372 373 374

     -- The C back end expects to see return continuations before the
     -- call sites.  Here, we sort them in reverse order -- it gets
     -- reversed later.
375
     let (_, block_order) =
376
             foldl' add_block_num (0::Int, mapEmpty :: LabelMap Int)
377 378
                   (revPostorder g)
         add_block_num (i, map) block =
379
           (i + 1, mapInsert (entryLabel block) i map)
380 381 382 383 384 385 386
         sort_fn (bid, _) (bid', _) =
           compare (expectJust "block_order" $ mapLookup bid  block_order)
                   (expectJust "block_order" $ mapLookup bid' block_order)
     procs <- return $ map to_proc $ sortBy sort_fn $ mapToList graphEnv
     return -- pprTrace "procLabels" (ppr procLabels)
            -- pprTrace "splitting graphs" (ppr procs)
            procs
387
splitAtProcPoints _ _ _ _ _ t@(CmmData _ _) = return [t]
388

389
-- Only called from GHC.Cmm.ProcPoint.splitAtProcPoints. NB. does a
390
-- recursive lookup, see comment below.
391
replaceBranches :: LabelMap BlockId -> CmmGraph -> CmmGraph
392 393 394
replaceBranches env cmmg
  = {-# SCC "replaceBranches" #-}
    ofBlockMap (g_entry cmmg) $ mapMap f $ toBlockMap cmmg
395
  where
396 397
    f block = replaceLastNode block $ last (lastNode block)

398 399
    last :: CmmNode O C -> CmmNode O C
    last (CmmBranch id)          = CmmBranch (lookup id)
400
    last (CmmCondBranch e ti fi l) = CmmCondBranch e (lookup ti) (lookup fi) l
401
    last (CmmSwitch e ids)       = CmmSwitch e (mapSwitchTargets lookup ids)
402 403 404 405
    last l@(CmmCall {})          = l { cml_cont = Nothing }
            -- NB. remove the continuation of a CmmCall, since this
            -- label will now be in a different CmmProc.  Not only
            -- is this tidier, it stops CmmLint from complaining.
406 407 408 409 410 411
    last l@(CmmForeignCall {})   = l
    lookup id = fmap lookup (mapLookup id env) `orElse` id
            -- XXX: this is a recursive lookup, it follows chains
            -- until the lookup returns Nothing, at which point we
            -- return the last BlockId

412 413 414 415
-- --------------------------------------------------------------
-- Not splitting proc points: add info tables for continuations

attachContInfoTables :: ProcPointSet -> CmmDecl -> CmmDecl
416 417
attachContInfoTables call_proc_points (CmmProc top_info top_l live g)
 = CmmProc top_info{info_tbls = info_tbls'} top_l live g
418 419 420 421 422 423 424 425
 where
   info_tbls' = mapUnion (info_tbls top_info) $
                mapFromList [ (l, mkEmptyContInfoTable (infoTblLbl l))
                            | l <- setElems call_proc_points
                            , l /= g_entry g ]
attachContInfoTables _ other_decl
 = other_decl

426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
----------------------------------------------------------------

{-
Note [Direct reachability]

Block B is directly reachable from proc point P iff control can flow
from P to B without passing through an intervening proc point.
-}

----------------------------------------------------------------

{-
Note [No simple dataflow]

Sadly, it seems impossible to compute the proc points using a single
dataflow pass.  One might attempt to use this simple lattice:

  data Location = Unknown
                | InProc BlockId -- node is in procedure headed by the named proc point
445
                | ProcPoint      -- node is itself a proc point
446

447
At a join, a node in two different blocks becomes a proc point.
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
The difficulty is that the change of information during iterative
computation may promote a node prematurely.  Here's a program that
illustrates the difficulty:

  f () {
  entry:
    ....
  L1:
    if (...) { ... }
    else { ... }

  L2: if (...) { g(); goto L1; }
      return x + y;
  }

The only proc-point needed (besides the entry) is L1.  But in an
iterative analysis, consider what happens to L2.  On the first pass
through, it rises from Unknown to 'InProc entry', but when L1 is
promoted to a proc point (because it's the successor of g()), L1's
successors will be promoted to 'InProc L1'.  The problem hits when the
new fact 'InProc L1' flows into L2 which is already bound to 'InProc entry'.
The join operation makes it a proc point when in fact it needn't be,
because its immediate dominator L1 is already a proc point and there
are no other proc points that directly reach L2.
-}



{- Note [Separate Adams optimization]
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
It may be worthwhile to attempt the Adams optimization by rewriting
the graph before the assignment of proc-point protocols.  Here are a
couple of rules:

482 483 484 485 486 487
  g() returns to k;                    g() returns to L;
  k: CopyIn c ress; goto L:
   ...                        ==>        ...
  L: // no CopyIn node here            L: CopyIn c ress;


488 489
And when c == c' and ress == ress', this also:

490 491 492 493
  g() returns to k;                    g() returns to L;
  k: CopyIn c ress; goto L:
   ...                        ==>        ...
  L: CopyIn c' ress'                   L: CopyIn c' ress' ;
494 495 496

In both cases the goal is to eliminate k.
-}