2 ( ProcPointSet, Status(..)
3 , callProcPoints, minimalProcPointSet
4 , addProcPointProtocols, splitAtProcPoints, procPointAnalysis
8 import qualified Prelude as P
9 import Prelude hiding (zip, unzip, last)
13 import Cmm hiding (blockId)
24 import MkZipCfgCmm hiding (CmmBlock, CmmGraph, CmmTopZ)
34 -- Compute a minimal set of proc points for a control-flow graph.
36 -- Determine a protocol for each proc point (which live variables will
37 -- be passed as arguments and which will be on the stack).
40 A proc point is a basic block that, after CPS transformation, will
41 start a new function. The entry block of the original function is a
42 proc point, as is the continuation of each function call.
43 A third kind of proc point arises if we want to avoid copying code.
44 Suppose we have code like the following:
47 if (...) { ..1..; call foo(); ..2..}
48 else { ..3..; call bar(); ..4..}
53 The statement 'x = y + z' can be reached from two different proc
54 points: the continuations of foo() and bar(). We would prefer not to
55 put a copy in each continuation; instead we would like 'x = y + z' to
56 be the start of a new procedure to which the continuations can jump:
59 if (...) { ..1..; push k_foo; jump foo_cps(); }
60 else { ..3..; push k_bar; jump bar_cps(); }
62 k_foo() { ..2..; jump k_join(y, z); }
63 k_bar() { ..4..; jump k_join(y, z); }
64 k_join(y, z) { x = y + z; return x; }
66 You might think then that a criterion to make a node a proc point is
67 that it is directly reached by two distinct proc points. (Note
68 [Direct reachability].) But this criterion is a bit too simple; for
69 example, 'return x' is also reached by two proc points, yet there is
70 no point in pulling it out of k_join. A good criterion would be to
71 say that a node should be made a proc point if it is reached by a set
72 of proc points that is different than its immediate dominator. NR
73 believes this criterion can be shown to produce a minimum set of proc
74 points, and given a dominator tree, the proc points can be chosen in
75 time linear in the number of blocks. Lacking a dominator analysis,
76 however, we turn instead to an iterative solution, starting with no
77 proc points and adding them according to these rules:
79 1. The entry block is a proc point.
80 2. The continuation of a call is a proc point.
81 3. A node is a proc point if it is directly reached by more proc
82 points than one of its predecessors.
84 Because we don't understand the problem very well, we apply rule 3 at
85 most once per iteration, then recompute the reachability information.
86 (See Note [No simple dataflow].) The choice of the new proc point is
87 arbitrary, and I don't know if the choice affects the final solution,
88 so I don't know if the number of proc points chosen is the
89 minimum---but the set will be minimal.
92 type ProcPointSet = BlockSet
95 = ReachedBy ProcPointSet -- set of proc points that directly reach the block
96 | ProcPoint -- this block is itself a proc point
98 instance Outputable Status where
100 | isEmptyBlockSet ps = text "<not-reached>"
101 | otherwise = text "reached by" <+>
102 (hsep $ punctuate comma $ map ppr $ blockSetToList ps)
103 ppr ProcPoint = text "<procpt>"
106 lattice :: DataflowLattice Status
107 lattice = DataflowLattice "direct proc-point reachability" unreached add_to False
108 where unreached = ReachedBy emptyBlockSet
109 add_to _ ProcPoint = noTx ProcPoint
110 add_to ProcPoint _ = aTx ProcPoint -- aTx because of previous case again
111 add_to (ReachedBy p) (ReachedBy p') =
112 let union = unionBlockSets p p'
113 in if sizeBlockSet union > sizeBlockSet p' then
114 aTx (ReachedBy union)
117 --------------------------------------------------
118 -- transfer equations
120 forward :: ForwardTransfers Middle Last Status
121 forward = ForwardTransfers first middle last exit
122 where first ProcPoint id = ReachedBy $ unitBlockSet id
125 last _ (LastCall _ (Just id) _ _) = LastOutFacts [(id, ProcPoint)]
126 last x l = LastOutFacts $ map (\id -> (id, x)) (succs l)
129 -- It is worth distinguishing two sets of proc points:
130 -- those that are induced by calls in the original graph
131 -- and those that are introduced because they're reachable from multiple proc points.
132 callProcPoints :: CmmGraph -> ProcPointSet
133 minimalProcPointSet :: ProcPointSet -> CmmGraph -> FuelMonad ProcPointSet
135 callProcPoints g = fold_blocks add (unitBlockSet (lg_entry g)) g
136 where add b set = case last $ unzip b of
137 LastOther (LastCall _ (Just k) _ _) -> extendBlockSet set k
140 minimalProcPointSet callProcPoints g = extendPPSet g (postorder_dfs g) callProcPoints
142 type PPFix = FuelMonad (ForwardFixedPoint Middle Last Status ())
144 procPointAnalysis :: ProcPointSet -> CmmGraph -> FuelMonad (BlockEnv Status)
145 procPointAnalysis procPoints g =
146 let addPP env id = extendBlockEnv env id ProcPoint
147 initProcPoints = foldl addPP emptyBlockEnv (blockSetToList procPoints)
148 in liftM zdfFpFacts $
149 (zdfSolveFrom initProcPoints "proc-point reachability" lattice
150 forward (fact_bot lattice) $ graphOfLGraph g :: PPFix)
152 extendPPSet :: CmmGraph -> [CmmBlock] -> ProcPointSet -> FuelMonad ProcPointSet
153 extendPPSet g blocks procPoints =
154 do env <- procPointAnalysis procPoints g
155 let add block pps = let id = blockId block
156 in case lookupBlockEnv env id of
157 Just ProcPoint -> extendBlockSet pps id
159 procPoints' = fold_blocks add emptyBlockSet g
160 newPoints = mapMaybe ppSuccessor blocks
161 newPoint = listToMaybe newPoints
162 ppSuccessor b@(Block bid _ _) =
163 let nreached id = case lookupBlockEnv env id `orElse`
164 pprPanic "no ppt" (ppr id <+> ppr b) of
166 ReachedBy ps -> sizeBlockSet ps
167 block_procpoints = nreached bid
168 -- | Looking for a successor of b that is reached by
169 -- more proc points than b and is not already a proc
170 -- point. If found, it can become a proc point.
171 newId succ_id = not (elemBlockSet succ_id procPoints') &&
172 nreached succ_id > block_procpoints
173 in listToMaybe $ filter newId $ succs b
176 [] -> return procPoints'
177 pps -> extendPPSet g blocks
178 (foldl extendBlockSet procPoints' pps)
180 case newPoint of Just id ->
181 if elemBlockSet id procPoints' then panic "added old proc pt"
182 else extendPPSet g blocks (extendBlockSet procPoints' id)
183 Nothing -> return procPoints'
186 ------------------------------------------------------------------------
187 -- Computing Proc-Point Protocols --
188 ------------------------------------------------------------------------
192 There is one major trick, discovered by Michael Adams, which is that
193 we want to choose protocols in a way that enables us to optimize away
194 some continuations. The optimization is very much like branch-chain
195 elimination, except that it involves passing results as well as
196 control. The idea is that if a call's continuation k does nothing but
197 CopyIn its results and then goto proc point P, the call's continuation
198 may be changed to P, *provided* P's protocol is identical to the
199 protocol for the CopyIn. We choose protocols to make this so.
201 Here's an explanatory example; we begin with the source code (lines
202 separate basic blocks):
210 Zipperization converts this code as follows:
213 call g() returns to k;
220 What we'd like to do is assign P the same CopyIn protocol as k, so we
224 call g() returns to P;
226 P: CopyIn(x, y); ..2..;
228 Of course, P may be the target of more than one continuation, and
229 different continuations may have different protocols. Michael Adams
230 implemented a voting mechanism, but he thinks a simple greedy
231 algorithm would be just as good, so that's what we do.
235 data Protocol = Protocol Convention CmmFormals Area
237 instance Outputable Protocol where
238 ppr (Protocol c fs a) = text "Protocol" <+> ppr c <+> ppr fs <+> ppr a
240 -- | Function 'optimize_calls' chooses protocols only for those proc
241 -- points that are relevant to the optimization explained above.
242 -- The others are assigned by 'add_unassigned', which is not yet clever.
244 addProcPointProtocols :: ProcPointSet -> ProcPointSet -> CmmGraph -> FuelMonad CmmGraph
245 addProcPointProtocols callPPs procPoints g =
246 do liveness <- cmmLivenessZ g
247 (protos, g') <- optimize_calls liveness g
248 blocks'' <- add_CopyOuts protos procPoints g'
249 return $ LGraph (lg_entry g) (lg_argoffset g) blocks''
250 where optimize_calls liveness g = -- see Note [Separate Adams optimization]
251 do let (protos, blocks') =
252 fold_blocks maybe_add_call (init_protocols, emptyBlockEnv) g
253 protos' = add_unassigned liveness procPoints protos
254 blocks <- add_CopyIns callPPs protos' blocks'
255 let g' = LGraph (lg_entry g) (lg_argoffset g)
256 (mkBlockEnv (map withKey (concat blocks)))
257 withKey b@(Block bid _ _) = (bid, b)
258 return (protos', runTx removeUnreachableBlocksZ g')
259 maybe_add_call :: CmmBlock -> (BlockEnv Protocol, BlockEnv CmmBlock)
260 -> (BlockEnv Protocol, BlockEnv CmmBlock)
261 -- ^ If the block is a call whose continuation goes to a proc point
262 -- whose protocol either matches the continuation's or is not yet set,
263 -- redirect the call (cf 'newblock') and set the protocol if necessary
264 maybe_add_call block (protos, blocks) =
265 case goto_end $ unzip block of
266 (h, LastOther (LastCall tgt (Just k) u s))
267 | Just proto <- lookupBlockEnv protos k,
268 Just pee <- branchesToProcPoint k
269 -> let newblock = zipht h (tailOfLast (LastCall tgt (Just pee) u s))
270 changed_blocks = insertBlock newblock blocks
271 unchanged_blocks = insertBlock block blocks
272 in case lookupBlockEnv protos pee of
273 Nothing -> (extendBlockEnv protos pee proto,changed_blocks)
275 if proto == proto' then (protos, changed_blocks)
276 else (protos, unchanged_blocks)
277 _ -> (protos, insertBlock block blocks)
279 branchesToProcPoint :: BlockId -> Maybe BlockId
280 -- ^ Tells whether the named block is just a branch to a proc point
281 branchesToProcPoint id =
282 let (Block _ _ t) = lookupBlockEnv (lg_blocks g) id `orElse`
283 panic "branch out of graph"
285 ZLast (LastOther (LastBranch pee))
286 | elemBlockSet pee procPoints -> Just pee
288 init_protocols = fold_blocks maybe_add_proto emptyBlockEnv g
289 maybe_add_proto :: CmmBlock -> BlockEnv Protocol -> BlockEnv Protocol
290 --maybe_add_proto (Block id (ZTail (CopyIn c _ fs _srt) _)) env =
291 -- extendBlockEnv env id (Protocol c fs $ toArea id fs)
292 maybe_add_proto _ env = env
294 -- | For now, following a suggestion by Ben Lippmeier, we pass all
295 -- live variables as arguments, hoping that a clever register
296 -- allocator might help.
298 add_unassigned :: BlockEnv CmmLive -> ProcPointSet -> BlockEnv Protocol ->
300 add_unassigned = pass_live_vars_as_args
302 pass_live_vars_as_args :: BlockEnv CmmLive -> ProcPointSet ->
303 BlockEnv Protocol -> BlockEnv Protocol
304 pass_live_vars_as_args _liveness procPoints protos = protos'
305 where protos' = foldBlockSet addLiveVars protos procPoints
306 addLiveVars :: BlockId -> BlockEnv Protocol -> BlockEnv Protocol
307 addLiveVars id protos =
308 case lookupBlockEnv protos id of
310 Nothing -> let live = emptyRegSet
311 --lookupBlockEnv _liveness id `orElse`
312 --panic ("no liveness at block " ++ show id)
313 formals = uniqSetToList live
314 prot = Protocol Private formals $ CallArea $ Young id
315 in extendBlockEnv protos id prot
318 -- | Add copy-in instructions to each proc point that did not arise from a call
319 -- instruction. (Proc-points that arise from calls already have their copy-in instructions.)
321 add_CopyIns :: ProcPointSet -> BlockEnv Protocol -> BlockEnv CmmBlock ->
322 FuelMonad [[CmmBlock]]
323 add_CopyIns callPPs protos blocks =
324 liftUniq $ mapM maybe_insert_CopyIns (blockEnvToList blocks)
325 where maybe_insert_CopyIns (_, b@(Block id stackInfo t))
326 | not $ elemBlockSet id callPPs
327 = case (argBytes stackInfo, lookupBlockEnv protos id) of
328 (Just _, _) -> panic "shouldn't copy arguments twice into a block"
329 (_, Just (Protocol c fs area)) ->
330 do let (off, copies) = copyIn c False area fs
331 stackInfo' = stackInfo {argBytes = Just off}
333 lgraphOfAGraph 0 (mkLabel id stackInfo' <*>
334 copies <*> mkZTail t)
335 return (map snd $ blockEnvToList blocks)
336 (_, Nothing) -> return [b]
337 | otherwise = return [b]
339 -- | Add a CopyOut node before each procpoint.
340 -- If the predecessor is a call, then the copy outs should already be done by the callee.
341 -- Note: If we need to add copy-out instructions, they may require stack space,
342 -- so we accumulate a map from the successors to the necessary stack space,
343 -- then update the successors after we have finished inserting the copy-outs.
345 add_CopyOuts :: BlockEnv Protocol -> ProcPointSet -> CmmGraph ->
346 FuelMonad (BlockEnv CmmBlock)
347 add_CopyOuts protos procPoints g = fold_blocks mb_copy_out (return emptyBlockEnv) g
348 where mb_copy_out :: CmmBlock -> FuelMonad (BlockEnv CmmBlock) ->
349 FuelMonad (BlockEnv CmmBlock)
350 mb_copy_out b@(Block bid _ _) z | bid == lg_entry g = skip b z
352 case last $ unzip b of
353 LastOther (LastCall _ _ _ _) -> skip b z -- copy out done by callee
354 _ -> mb_copy_out' b z
355 mb_copy_out' b z = fold_succs trySucc b init >>= finish
356 where init = z >>= (\bmap -> return (b, bmap))
358 if elemBlockSet succId procPoints then
359 case lookupBlockEnv protos succId of
361 Just (Protocol c fs area) ->
363 copyOut c Jump area (map (CmmReg . CmmLocal) fs) 0
364 in insert z succId copies
368 (b, bs) <- insertBetween b m succId
369 pprTrace "insert for succ" (ppr succId <> ppr m) $
370 return $ (b, foldl (flip insertBlock) bmap bs)
371 finish (b@(Block bid _ _), bmap) =
372 return $ (extendBlockEnv bmap bid b)
373 skip b@(Block bid _ _) bs =
374 bs >>= (\bmap -> return (extendBlockEnv bmap bid b))
376 -- At this point, we have found a set of procpoints, each of which should be
377 -- the entry point of a procedure.
378 -- Now, we create the procedure for each proc point,
379 -- which requires that we:
380 -- 1. build a map from proc points to the blocks reachable from the proc point
381 -- 2. turn each branch to a proc point into a jump
382 -- 3. turn calls and returns into jumps
383 -- 4. build info tables for the procedures -- and update the info table for
384 -- the SRTs in the entry procedure as well.
385 -- Input invariant: A block should only be reachable from a single ProcPoint.
386 splitAtProcPoints :: CLabel -> ProcPointSet-> ProcPointSet -> BlockEnv Status ->
387 AreaMap -> CmmTopZ -> FuelMonad [CmmTopZ]
388 splitAtProcPoints entry_label callPPs procPoints procMap areaMap
389 (CmmProc (CmmInfo gc upd_fr info_tbl) top_l top_args
390 g@(LGraph entry e_off blocks)) =
391 do -- Build a map from procpoints to the blocks they reach
392 let addBlock b@(Block bid _ _) graphEnv =
393 case lookupBlockEnv procMap bid of
394 Just ProcPoint -> add graphEnv bid bid b
395 Just (ReachedBy set) ->
396 case blockSetToList set of
398 [id] -> add graphEnv id bid b
399 _ -> panic "Each block should be reachable from only one ProcPoint"
400 Nothing -> pprPanic "block not reached by a proc point?" (ppr bid)
401 add graphEnv procId bid b = extendBlockEnv graphEnv procId graph'
402 where graph = lookupBlockEnv graphEnv procId `orElse` emptyBlockEnv
403 graph' = extendBlockEnv graph bid b
404 graphEnv_pre <- return $ fold_blocks addBlock emptyBlockEnv g
405 graphEnv <- return $ pprTrace "graphEnv" (ppr graphEnv_pre) graphEnv_pre
406 -- Build a map from proc point BlockId to labels for their new procedures
407 let add_label map pp = return $ addToFM map pp lbl
408 where lbl = if pp == entry then entry_label else blockLbl pp
409 -- Due to common blockification, we may overestimate the set of procpoints.
410 procLabels <- foldM add_label emptyFM
411 (filter (elemBlockEnv blocks) (blockSetToList procPoints))
412 -- In each new graph, add blocks jumping off to the new procedures,
413 -- and replace branches to procpoints with branches to the jump-off blocks
414 let add_jump_block (env, bs) (pp, l) =
415 do bid <- liftM mkBlockId getUniqueM
416 let b = Block bid emptyStackInfo (ZLast (LastOther jump))
418 case lookupBlockEnv blocks pp of
419 Just (Block _ (StackInfo {argBytes = Just s}) _) -> s
420 Just (Block _ _ _) -> panic "no args at procpoint"
421 _ -> panic "can't find procpoint block"
422 jump = LastCall (CmmLit (CmmLabel l')) Nothing argSpace Nothing
423 l' = if elemBlockSet pp callPPs then entryLblToInfoLbl l else l
424 return (extendBlockEnv env pp bid, b : bs)
425 add_jumps (newGraphEnv) (ppId, blockEnv) =
426 do (jumpEnv, jumpBlocks) <-
427 foldM add_jump_block (emptyBlockEnv, []) (fmToList procLabels)
428 let (b_off, b) = -- get the stack offset on entry into the block and
429 -- remove the offset from the block (it goes in new graph)
430 case lookupBlockEnv blockEnv ppId of -- get the procpoint block
431 Just (Block id sinfo@(StackInfo {argBytes = Just b_off}) t) ->
432 (b_off, Block id (sinfo {argBytes = Nothing}) t)
433 Just b@(Block _ _ _) -> (0, b)
434 Nothing -> panic "couldn't find entry block while splitting"
435 blockEnv' = extendBlockEnv blockEnv ppId b
436 off = if ppId == entry then e_off else b_off
437 LGraph _ _ blockEnv'' =
438 replaceBranches jumpEnv $ LGraph ppId off blockEnv'
439 blockEnv''' = foldl (flip insertBlock) blockEnv'' jumpBlocks
440 let g' = LGraph ppId off blockEnv'''
441 pprTrace "g' pre jumps" (ppr g') $
442 return (extendBlockEnv newGraphEnv ppId g')
443 graphEnv_pre <- foldM add_jumps emptyBlockEnv $ blockEnvToList graphEnv
444 graphEnv <- return $ pprTrace "graphEnv with jump blocks" (ppr graphEnv_pre)
446 let to_proc (bid, g@(LGraph g_entry _ blocks)) | elemBlockSet bid callPPs =
448 CmmProc (CmmInfo gc upd_fr info_tbl) top_l top_args g
450 CmmProc emptyContInfoTable lbl [] g
451 where lbl = expectJust "pp label" $ lookupFM procLabels bid
453 CmmProc (CmmInfo Nothing Nothing CmmNonInfoTable) lbl [] g
454 where lbl = expectJust "pp label" $ lookupFM procLabels bid
455 -- The C back end expects to see return continuations before the call sites.
456 -- Here, we sort them in reverse order -- it gets reversed later.
457 let (_, block_order) = foldl add_block_num (0::Int, emptyBlockEnv) (postorder_dfs g)
458 add_block_num (i, map) (Block bid _ _) = (i+1, extendBlockEnv map bid i)
459 sort_fn (bid, _) (bid', _) =
460 compare (expectJust "block_order" $ lookupBlockEnv block_order bid)
461 (expectJust "block_order" $ lookupBlockEnv block_order bid')
462 procs <- return $ map to_proc $ sortBy sort_fn $ blockEnvToList graphEnv
463 return $ pprTrace "procLabels" (ppr procLabels)
464 $ pprTrace "splitting graphs" (ppr procs)
466 splitAtProcPoints _ _ _ _ _ t@(CmmData _ _) = return [t]
468 ----------------------------------------------------------------
471 Note [Direct reachability]
473 Block B is directly reachable from proc point P iff control can flow
474 from P to B without passing through an intervening proc point.
477 ----------------------------------------------------------------
480 Note [No simple dataflow]
482 Sadly, it seems impossible to compute the proc points using a single
483 dataflow pass. One might attempt to use this simple lattice:
485 data Location = Unknown
486 | InProc BlockId -- node is in procedure headed by the named proc point
487 | ProcPoint -- node is itself a proc point
489 At a join, a node in two different blocks becomes a proc point.
490 The difficulty is that the change of information during iterative
491 computation may promote a node prematurely. Here's a program that
492 illustrates the difficulty:
501 L2: if (...) { g(); goto L1; }
505 The only proc-point needed (besides the entry) is L1. But in an
506 iterative analysis, consider what happens to L2. On the first pass
507 through, it rises from Unknown to 'InProc entry', but when L1 is
508 promoted to a proc point (because it's the successor of g()), L1's
509 successors will be promoted to 'InProc L1'. The problem hits when the
510 new fact 'InProc L1' flows into L2 which is already bound to 'InProc entry'.
511 The join operation makes it a proc point when in fact it needn't be,
512 because its immediate dominator L1 is already a proc point and there
513 are no other proc points that directly reach L2.
518 {- Note [Separate Adams optimization]
519 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
520 It may be worthwhile to attempt the Adams optimization by rewriting
521 the graph before the assignment of proc-point protocols. Here are a
524 g() returns to k; g() returns to L;
525 k: CopyIn c ress; goto L:
527 L: // no CopyIn node here L: CopyIn c ress;
530 And when c == c' and ress == ress', this also:
532 g() returns to k; g() returns to L;
533 k: CopyIn c ress; goto L:
535 L: CopyIn c' ress' L: CopyIn c' ress' ;
537 In both cases the goal is to eliminate k.