eliminate the last vestige of UniqSM from ZipCfg
[ghc-hetmet.git] / compiler / cmm / ZipCfg.hs
1 {-# LANGUAGE ScopedTypeVariables #-}
2 module ZipCfg
3     (   -- These data types and names are carefully thought out
4       BlockId(..)       -- ToDo: BlockId should be abstract, but it isn't yet
5     , BlockEnv, emptyBlockEnv, lookupBlockEnv, extendBlockEnv, insertBlock, mkBlockEnv
6     , BlockSet, emptyBlockSet, elemBlockSet, extendBlockSet, mkBlockSet
7     , Graph(..), LGraph(..), FGraph(..)
8     , Block(..), ZBlock(..), ZHead(..), ZTail(..), ZLast(..)
9     , HavingSuccessors, succs, fold_succs
10     , LastNode, mkBranchNode, isBranchNode, branchNodeTarget
11
12         -- Observers and transformers
13         -- (open to renaming suggestions here)
14     , blockId, zip, unzip, last, goto_end, zipht, tailOfLast
15     , splice_tail, splice_head, splice_head_only', splice_head'
16     , of_block_list, to_block_list
17     , map_nodes
18     , postorder_dfs, postorder_dfs_from, postorder_dfs_from_except
19     , fold_layout
20     , fold_blocks
21     , translate
22
23     , pprLgraph, pprGraph
24
25     , entry -- exported for the convenience of ZipDataflow, at least for now
26
27     {-
28     -- the following functions might one day be useful and can be found
29     -- either below or in ZipCfgExtras:
30     , entry, exit, focus, focusp, unfocus
31     , ht_to_block, ht_to_last, 
32     , splice_focus_entry, splice_focus_exit
33     , fold_fwd_block, foldM_fwd_block
34     -}
35
36     )
37 where
38
39 #include "HsVersions.h"
40
41 import Outputable hiding (empty)
42 import Panic
43 import Unique
44 import UniqFM
45 import UniqSet
46
47 import Maybe
48 import Prelude hiding (zip, unzip, last)
49
50 -------------------------------------------------------------------------
51 --               GENERIC ZIPPER-BASED CONTROL-FLOW GRAPH               --
52 -------------------------------------------------------------------------
53 {-
54
55 This module defines datatypes used to represent control-flow graphs,
56 along with some functions for analyzing and splicing graphs.
57 Functions for building graphs are found in a separate module 'MkZipCfg'.
58
59 Every graph has a distinguished entry point.  A graph has at least one
60 exit; most exits are instructions (or statements) like 'jump' or
61 'return', which transfer control to other procedures, but a graph may
62 have up to one 'fall through' exit.  (A graph that represents an
63 entire Haskell or C-- procedure does not have a 'fall through' exit.)
64
65 A graph is a collection of basic blocks.  A basic block begins with a
66 label (unique id; see Note [Unique BlockId]) which is followed by a
67 sequence of zero or more 'middle' nodes; the basic block ends with a
68 'last' node.  Each 'middle' node is a single-entry, single-exit,
69 uninterruptible computation.  A 'last' node is a single-entry,
70 multiple-exit computation.  A last node may have zero or more successors,
71 which are identified by their unique ids.
72
73 A special case of last node is the ``default exit,'' which represents
74 'falling off the end' of the graph.  Such a node is always represented by
75 the data constructor 'LastExit'.  A graph may contain at most one
76 'LastExit' node, and a graph representing a full procedure should not
77 contain any 'LastExit' nodes.  'LastExit' nodes are used only to splice
78 graphs together, either during graph construction (see module 'MkZipCfg')
79 or during optimization (see module 'ZipDataflow').
80
81 A graph is parameterized over the types of middle and last nodes.  Each of
82 these types will typically be instantiated with a subset of C-- statements
83 (see module 'ZipCfgCmmRep') or a subset of machine instructions (yet to be
84 implemented as of August 2007).
85
86
87 Note [Kinds of Graphs]
88 ~~~~~~~~~~~~~~~~~~~~~~
89 This module exposes three representations of graphs.  In order of
90 increasing complexity, they are:
91
92   Graph  m l      The basic graph with its distinguished entry point
93
94   LGraph m l      A graph with a *labelled* entry point
95
96   FGraph m l      A labelled graph with the *focus* on a particular edge
97
98 There are three types because each type offers a slightly different
99 invariant or cost model.  
100
101   * The distinguished entry of a Graph has no label.  Because labels must be
102     unique, acquiring one requires a supply of Unique labels (BlockId's).
103     The primary advantage of the Graph representation is that we can build a
104     small Graph purely functionally, without needing a fresh BlockId or
105     Unique.  For example, during optimization we can easily rewrite a single
106     middle node into a Graph containing a sequence of two middle nodes
107     followed by LastExit.
108
109   * In an LGraph, every basic block is labelled.  The primary advantage of
110     this representation is its simplicity: each basic block can be treated
111     like any other.  This representation is used for mapping, folding, and
112     translation, as well as layout.
113
114     Like any graph, an LGraph still has a distinguished entry point, 
115     which you can discover using 'lg_entry'.
116
117   * An FGraph is an LGraph with the *focus* on one particular edge.  The
118     primary advantage of this representation is that it provides
119     constant-time access to the nodes connected by that edge, and it also
120     allows constant-time, functional *replacement* of those nodes---in the
121     style of Huet's 'zipper'.
122
123 None of these representations is ideally suited to the incremental
124 construction of large graphs.  A separate module, 'MkZipCfg', provides a
125 fourth representation that is asymptotically optimal for such construction.
126     
127 -}
128
129 --------------- Representation --------------------
130
131 -- | A basic block is a 'first' node, followed by zero or more 'middle'
132 -- nodes, followed by a 'last' node.
133
134 -- eventually this module should probably replace the original Cmm, but for
135 -- now we leave it to dynamic invariants what can be found where
136
137 data ZLast l
138   = LastExit     -- fall through; used for the block that has no last node
139                  -- LastExit is a device used only for graphs under 
140                  -- construction, or framgments of graph under optimisation,
141                  -- so we don't want to pollute the 'l' type parameter with it
142   | LastOther l
143
144 data ZHead m   = ZFirst BlockId  | ZHead (ZHead m) m
145     -- ZHead is a (reversed) sequence of middle nodes labeled by a BlockId
146 data ZTail m l = ZLast (ZLast l) | ZTail m (ZTail m l)
147     -- ZTail is a sequence of middle nodes followed by a last node
148
149 -- | Blocks and flow graphs; see Note [Kinds of graphs]
150 data Block m l = Block BlockId (ZTail m l)
151
152 data Graph m l = Graph { g_entry :: (ZTail m l), g_blocks :: (BlockEnv (Block m l)) }
153
154 data LGraph m l = LGraph  { lg_entry  :: BlockId
155                           , lg_blocks :: BlockEnv (Block m l) }
156         -- Invariant: lg_entry is in domain( lg_blocks )
157
158 -- | And now the zipper.  The focus is between the head and tail.
159 -- We cannot ever focus on an inter-block edge.
160 data ZBlock m l = ZBlock (ZHead m) (ZTail m l)
161 data FGraph m l = FGraph { fg_entry  :: BlockId
162                          , fg_focus  :: ZBlock m l
163                          , fg_others :: BlockEnv (Block m l) }
164                     -- Invariant: the block represented by 'fg_focus' is *not*
165                     -- in the map 'fg_others'
166
167 ----  Utility functions ---
168
169 blockId   :: Block  m l -> BlockId
170 zip       :: ZBlock m l -> Block  m l
171 unzip     :: Block  m l -> ZBlock m l
172
173 last      :: ZBlock m l -> ZLast l
174 goto_end  :: ZBlock m l -> (ZHead m, ZLast l)
175
176 tailOfLast :: l -> ZTail m l
177
178 -- | Take a head and tail and go to beginning or end.  The asymmetry
179 -- in the types and names is a bit unfortunate, but 'Block m l' is
180 -- effectively '(BlockId, ZTail m l)' and is accepted in many more places.
181
182 ht_to_block, zipht :: ZHead m -> ZTail m l -> Block m l
183 ht_to_last         :: ZHead m -> ZTail m l -> (ZHead m, ZLast l)
184
185 -- | We can splice a single-entry, single-exit LGraph onto a head or a tail.
186 -- For a head, we have a head 'h' followed by a LGraph 'g'.
187 -- The entry node of 'g' gets joined to 'h', forming the entry into
188 -- the new LGraph.  The exit of 'g' becomes the new head.
189 -- For both arguments and results, the order of values is the order of
190 -- control flow: before splicing, the head flows into the LGraph; after
191 -- splicing, the LGraph flows into the head.
192 -- Splicing a tail is the dual operation.
193 -- (In order to maintain the order-means-control-flow convention, the
194 -- orders are reversed.)
195 --
196 -- For example, assume
197 --      head = [L: x:=0]
198 --      grph = (M, [M: <stuff>,
199 --                  <blocks>,
200 --                  N: y:=x; LastExit])
201 --      tail = [return (y,x)]
202 --
203 -- Then         splice_head head grph
204 --              = ((L, [L: x:=0; goto M,
205 --                      M: <stuff>,
206 --                      <blocks>])
207 --                 , N: y:=x)
208 --
209 -- Then         splice_tail grph tail
210 --              = ( <stuff>
211 --                , (???, [<blocks>,
212 --                         N: y:=x; return (y,x)])
213
214 splice_head  :: ZHead m    -> LGraph m l -> (LGraph m l, ZHead  m)
215 splice_head' :: ZHead m -> Graph m l -> (BlockEnv (Block m l), ZHead m)
216 splice_tail  :: Graph m l -> ZTail  m l -> Graph m l
217
218 -- | We can also splice a single-entry, no-exit Graph into a head.
219 splice_head_only :: ZHead m -> LGraph m l -> LGraph m l
220 splice_head_only' :: ZHead m -> Graph m l -> LGraph m l
221
222
223 -- | A safe operation 
224
225 -- | Conversion to and from the environment form is convenient.  For
226 -- layout or dataflow, however, one will want to use 'postorder_dfs'
227 -- in order to get the blocks in an order that relates to the control
228 -- flow in the procedure.
229 of_block_list :: BlockId -> [Block m l] -> LGraph m l  -- N log N
230 to_block_list :: LGraph m l -> [Block m l]  -- N log N
231
232 -- | Traversal: 'postorder_dfs' returns a list of blocks reachable
233 -- from the entry node.  This list has the following property:
234 --
235 --      Say a "back reference" exists if one of a block's
236 --      control-flow successors precedes it in the output list
237 --
238 --      Then there are as few back references as possible
239 --
240 -- The output is suitable for use in
241 -- a forward dataflow problem.  For a backward problem, simply reverse
242 -- the list.  ('postorder_dfs' is sufficiently tricky to implement that
243 -- one doesn't want to try and maintain both forward and backward
244 -- versions.)
245
246 postorder_dfs :: LastNode l => LGraph m l -> [Block m l]
247
248 -- | For layout, we fold over pairs of 'Block m l' and 'Maybe BlockId'
249 -- in layout order.  The 'Maybe BlockId', if present, identifies the
250 -- block that will be the layout successor of the current block.  This
251 -- may be useful to help an emitter omit the final 'goto' of a block
252 -- that flows directly to its layout successor.
253 --
254 -- For example: fold_layout f z [ L1:B1, L2:B2, L3:B3 ]
255 --              = z <$> f (L1:B1) (Just L2)
256 --                  <$> f (L2:B2) (Just L3)
257 --                  <$> f (L3:B3) Nothing
258 -- where a <$> f = f a
259 fold_layout ::
260     LastNode l => (Block m l -> Maybe BlockId -> a -> a) -> a -> LGraph m l-> a
261
262 -- | We can also fold over blocks in an unspecified order.  The
263 -- 'ZipCfgExtras' module provides a monadic version, which we
264 -- haven't needed (else it would be here).
265 fold_blocks :: (Block m l -> a -> a) -> a -> LGraph m l -> a
266
267 map_nodes :: (BlockId -> BlockId) -> (m -> m') -> (l -> l') -> LGraph m l -> LGraph m' l'
268    -- mapping includes the entry id!
269
270 map_blocks :: (Block m l -> Block m' l') -> LGraph m' l' -> LGraph m' l'
271
272 -- | These translation functions are speculative.  I hope eventually
273 -- they will be used in the native-code back ends ---NR
274 translate :: Monad tm =>
275              (m          -> tm (LGraph m' l')) ->
276              (l          -> tm (LGraph m' l')) ->
277              (LGraph m l -> tm (LGraph m' l'))
278
279 {-
280 -- | It's possible that another form of translation would be more suitable:
281 translateA :: (m -> Agraph m' l') -> (l -> AGraph m' l') -> LGraph m l -> LGraph m' l'
282 -}
283
284 ------------------- Last nodes
285
286 -- | We can't make a graph out of just any old 'last node' type.  A last node
287 -- has to be able to find its successors, and we need to be able to create and
288 -- identify unconditional branches.  We put these capabilities in a type class.
289 -- Moreover, the property of having successors is also shared by 'Block's and
290 -- 'ZTails', so it is useful to have that property in a type class of its own.
291
292 class HavingSuccessors b where
293     succs :: b -> [BlockId]
294     fold_succs :: (BlockId -> a -> a) -> b -> a -> a
295
296     fold_succs add l z = foldr add z $ succs l
297
298 class HavingSuccessors l => LastNode l where
299     mkBranchNode     :: BlockId -> l
300     isBranchNode     :: l -> Bool
301     branchNodeTarget :: l -> BlockId  -- panics if not branch node
302       -- ^ N.B. This interface seems to make for more congenial clients than a
303       -- single function of type 'l -> Maybe BlockId'
304
305 instance HavingSuccessors l => HavingSuccessors (ZLast l) where
306     succs LastExit = []
307     succs (LastOther l) = succs l
308     fold_succs _ LastExit z = z
309     fold_succs f (LastOther l) z = fold_succs f l z
310
311 instance LastNode l => LastNode (ZLast l) where
312     mkBranchNode id = LastOther $ mkBranchNode id
313     isBranchNode LastExit = False
314     isBranchNode (LastOther l) = isBranchNode l
315     branchNodeTarget LastExit = panic "branchNodeTarget LastExit"
316     branchNodeTarget (LastOther l) = branchNodeTarget l
317
318 instance LastNode l => HavingSuccessors (ZBlock m l) where
319     succs b = succs (last b)
320
321 instance LastNode l => HavingSuccessors (Block m l) where
322     succs b = succs (unzip b)
323
324 instance LastNode l => HavingSuccessors (ZTail m l) where
325     succs b = succs (lastTail b)
326
327
328
329 -- ================ IMPLEMENTATION ================--
330
331 ----- block manipulations
332
333 blockId (Block id _) = id
334
335 -- | Convert block between forms.
336 -- These functions are tail-recursive, so we can go as deep as we like
337 -- without fear of stack overflow.  
338
339 ht_to_block head tail = case head of
340   ZFirst id -> Block id tail
341   ZHead h m -> ht_to_block h (ZTail m tail) 
342
343 ht_to_last head (ZLast l)   = (head, l)
344 ht_to_last head (ZTail m t) = ht_to_last (ZHead head m) t 
345
346 zipht            h t  = ht_to_block h t
347 zip      (ZBlock h t) = ht_to_block h t
348 goto_end (ZBlock h t) = ht_to_last  h t
349
350 unzip (Block id t) = ZBlock (ZFirst id) t
351
352 head_id :: ZHead m -> BlockId
353 head_id (ZFirst id) = id
354 head_id (ZHead h _) = head_id h
355
356 last (ZBlock _ t) = lastTail t
357
358 lastTail :: ZTail m l -> ZLast l
359 lastTail (ZLast l) = l
360 lastTail (ZTail _ t) = lastTail t
361
362 tailOfLast l = ZLast (LastOther l) -- ^ tedious to write in every client
363
364
365 ------------------ simple graph manipulations
366
367 focus :: BlockId -> LGraph m l -> FGraph m l -- focus on edge out of node with id 
368 focus id (LGraph entry blocks) =
369     case lookupBlockEnv blocks id of
370       Just b -> FGraph entry (unzip b) (delFromUFM blocks id)
371       Nothing -> panic "asked for nonexistent block in flow graph"
372
373 entry   :: LGraph m l -> FGraph m l         -- focus on edge out of entry node 
374 entry g@(LGraph eid _) = focus eid g
375
376 -- | pull out a block satisfying the predicate, if any
377 splitp_blocks :: (Block m l -> Bool) -> BlockEnv (Block m l) ->
378                  Maybe (Block m l, BlockEnv (Block m l))
379 splitp_blocks p blocks = lift $ foldUFM scan (Nothing, emptyBlockEnv) blocks 
380     where scan b (yes, no) =
381               case yes of
382                 Nothing | p b -> (Just b, no)
383                         | otherwise -> (yes, insertBlock b no)
384                 Just _ -> (yes, insertBlock b no)
385           lift (Nothing, _) = Nothing
386           lift (Just b, bs) = Just (b, bs)
387
388 -- | 'insertBlock' should not be used to *replace* an existing block
389 -- but only to insert a new one
390 insertBlock :: Block m l -> BlockEnv (Block m l) -> BlockEnv (Block m l)
391 insertBlock b bs =
392       ASSERT (isNothing $ lookupBlockEnv bs id)
393       extendBlockEnv bs id b
394     where id = blockId b
395
396 -- | Used in assertions; tells if a graph has exactly one exit
397 single_exit :: LGraph l m -> Bool
398 single_exit g = foldUFM check 0 (lg_blocks g) == 1
399     where check block count = case last (unzip block) of
400                                 LastExit -> count + (1 :: Int)
401                                 _ -> count
402
403 -- | Used in assertions; tells if a graph has exactly one exit
404 single_exitg :: Graph l m -> Bool
405 single_exitg (Graph tail blocks) = foldUFM add (exit_count (lastTail tail)) blocks == 1
406     where add block count = count + exit_count (last (unzip block))
407           exit_count LastExit = 1 :: Int
408           exit_count _        = 0
409
410 ------------------ graph traversals
411
412 -- | This is the most important traversal over this data structure.  It drops
413 -- unreachable code and puts blocks in an order that is good for solving forward
414 -- dataflow problems quickly.  The reverse order is good for solving backward
415 -- dataflow problems quickly.  The forward order is also reasonably good for
416 -- emitting instructions, except that it will not usually exploit Forrest
417 -- Baskett's trick of eliminating the unconditional branch from a loop.  For
418 -- that you would need a more serious analysis, probably based on dominators, to
419 -- identify loop headers.
420 --
421 -- The ubiquity of 'postorder_dfs' is one reason for the ubiquity of the 'LGraph'
422 -- representation, when for most purposes the plain 'Graph' representation is
423 -- more mathematically elegant (but results in more complicated code).
424 --
425 -- Here's an easy way to go wrong!  Consider
426 --      A -> [B,C]
427 --      B -> D
428 --      C -> D
429 -- Then ordinary dfs would give [A,B,D,C] which has a back ref from C to D.
430 -- Better to geot [A,B,C,D]
431
432
433 postorder_dfs' :: LastNode l => LGraph m l -> [Block m l]
434 postorder_dfs' g@(LGraph _ blocks) =
435   let FGraph _ eblock _ = entry g
436   in  vnode (zip eblock) (\acc _visited -> acc) [] emptyBlockSet
437   where
438     -- vnode ::
439     --    Block m l -> ([Block m l] -> BlockSet -> a) -> [Block m l] -> BlockSet -> a
440     vnode block@(Block id _) cont acc visited =
441         if elemBlockSet id visited then
442             cont acc visited
443         else
444             vchildren block (get_children block) cont acc (extendBlockSet visited id)
445     vchildren block bs cont acc visited =
446         let next children acc visited =
447                 case children of []     -> cont (block : acc) visited
448                                  (b:bs) -> vnode b (next bs) acc visited
449         in next bs acc visited
450     get_children block = foldl add_id [] (succs block)
451     add_id rst id = case lookupBlockEnv blocks id of
452                       Just b -> b : rst
453                       Nothing -> rst
454
455 postorder_dfs g@(LGraph _ blockenv) =
456     let FGraph id eblock _ = entry g
457         dfs1 = zip eblock :
458                postorder_dfs_from_except blockenv eblock (unitUniqSet id)
459         dfs2 = postorder_dfs' g
460 --    in  ASSERT (map blockId dfs1 == map blockId dfs2) dfs2
461     in  if (map blockId dfs1 == map blockId dfs2) then dfs2 else panic "inconsistent DFS"
462
463 postorder_dfs_from
464     :: (HavingSuccessors b, LastNode l) => BlockEnv (Block m l) -> b -> [Block m l]
465 postorder_dfs_from blocks b = postorder_dfs_from_except blocks b emptyBlockSet
466
467 postorder_dfs_from_except :: forall b m l . (HavingSuccessors b, LastNode l) => BlockEnv (Block m l) -> b -> BlockSet -> [Block m l]
468 postorder_dfs_from_except blocks b visited =
469   vchildren (get_children b) (\acc _visited -> acc) [] visited
470   where
471     -- vnode ::
472     --    Block m l -> ([Block m l] -> BlockSet -> a) -> [Block m l] -> BlockSet -> a
473     vnode block@(Block id _) cont acc visited =
474         if elemBlockSet id visited then
475             cont acc visited
476         else
477             let cont' acc visited = cont (block:acc) visited in
478             vchildren (get_children block) cont' acc (extendBlockSet visited id)
479     vchildren bs cont acc visited =
480         let next children acc visited =
481                 case children of []     -> cont acc visited
482                                  (b:bs) -> vnode b (next bs) acc visited
483         in next bs acc visited
484     get_children block = foldl add_id [] (succs block)
485     add_id rst id = case lookupBlockEnv blocks id of
486                       Just b -> b : rst
487                       Nothing -> rst
488
489
490 -- | Slightly more complicated than the usual fold because we want to tell block
491 -- 'b1' what its inline successor is going to be, so that if 'b1' ends with
492 -- 'goto b2', the goto can be omitted.
493
494 fold_layout f z g@(LGraph eid _) = fold (postorder_dfs g) z
495   where fold blocks z =
496             case blocks of [] -> z
497                            [b] -> f b Nothing z
498                            b1 : b2 : bs -> fold (b2 : bs) (f b1 (nextlabel b2) z)
499         nextlabel (Block id _) =
500             if id == eid then panic "entry as successor"
501             else Just id
502
503 -- | The rest of the traversals are straightforward
504
505 map_blocks f (LGraph eid blocks) = LGraph eid (mapUFM f blocks)
506
507 map_nodes idm middle last (LGraph eid blocks) = LGraph (idm eid) (mapUFM block blocks)
508     where block (Block id t) = Block (idm id) (tail t)
509           tail (ZTail m t) = ZTail (middle m) (tail t)
510           tail (ZLast LastExit) = ZLast LastExit
511           tail (ZLast (LastOther l)) = ZLast (LastOther (last l))
512
513 fold_blocks f z (LGraph _ blocks) = foldUFM f z blocks
514
515 of_block_list e blocks = LGraph e $ foldr insertBlock emptyBlockEnv blocks 
516 to_block_list (LGraph _ blocks) = eltsUFM blocks
517
518
519
520
521 -- We want to be able to scrutinize a single-entry, single-exit 'LGraph' for
522 -- splicing purposes.  There are two useful cases: the 'LGraph' is a single block
523 -- or it isn't.  We use continuation-passing style.
524
525 prepare_for_splicing ::
526   LGraph m l -> (ZTail m l -> a) -> (ZTail m l -> ZHead m -> BlockEnv (Block m l) -> a)
527   -> a
528 prepare_for_splicing g single multi =
529   let FGraph _ gentry gblocks = entry g 
530       ZBlock _ etail = gentry
531   in if isNullUFM gblocks then
532          case last gentry of
533            LastExit -> single etail
534            _ -> panic "bad single block"
535      else
536        case splitp_blocks is_exit gblocks of
537          Nothing -> panic "Can't find an exit block"
538          Just (gexit, gblocks) ->
539               let (gh, gl) = goto_end $ unzip gexit in
540               case gl of LastExit -> multi etail gh gblocks
541                          _ -> panic "exit is not exit?!"
542
543 prepare_for_splicing' ::
544   Graph m l -> (ZTail m l -> a) -> (ZTail m l -> ZHead m -> BlockEnv (Block m l) -> a)
545   -> a
546 prepare_for_splicing' (Graph etail gblocks) single multi =
547    if isNullUFM gblocks then
548        case lastTail etail of
549          LastExit -> single etail
550          _ -> panic "bad single block"
551    else
552      case splitp_blocks is_exit gblocks of
553        Nothing -> panic "Can't find an exit block"
554        Just (gexit, gblocks) ->
555             let (gh, gl) = goto_end $ unzip gexit in
556             case gl of LastExit -> multi etail gh gblocks
557                        _ -> panic "exit is not exit?!"
558
559 is_exit :: Block m l -> Bool
560 is_exit b = case last (unzip b) of { LastExit -> True; _ -> False }
561
562 splice_head head g = 
563   ASSERT (single_exit g) prepare_for_splicing g splice_one_block splice_many_blocks
564    where eid = head_id head
565          splice_one_block tail' =
566              case ht_to_last head tail' of
567                (head, LastExit) -> (LGraph eid emptyBlockEnv, head)
568                _ -> panic "spliced LGraph without exit" 
569          splice_many_blocks entry exit others =
570              (LGraph eid (insertBlock (zipht head entry) others), exit)
571
572 splice_head' head g = 
573   ASSERT (single_exitg g) prepare_for_splicing' g splice_one_block splice_many_blocks
574    where splice_one_block tail' = 
575              case ht_to_last head tail' of
576                (head, LastExit) -> (emptyBlockEnv, head)
577                _ -> panic "spliced LGraph without exit" 
578          splice_many_blocks entry exit others =
579              (insertBlock (zipht head entry) others, exit)
580
581 -- splice_tail :: Graph m l -> ZTail m l -> Graph m l
582 splice_tail g tail =
583   ASSERT (single_exitg g) prepare_for_splicing' g splice_one_block splice_many_blocks
584     where splice_one_block tail' = Graph (tail' `append_tails` tail) emptyBlockEnv
585           append_tails (ZLast LastExit) tail = tail
586           append_tails (ZLast _) _ = panic "spliced single block without LastExit"
587           append_tails (ZTail m t) tail = ZTail m (append_tails t tail)
588           splice_many_blocks entry exit others =
589               Graph entry (insertBlock (zipht exit tail) others)
590
591 {-
592 splice_tail g tail =
593   AS SERT (single_exit g) prepare_for_splicing g splice_one_block splice_many_blocks
594     where splice_one_block tail' =  -- return tail' .. tail 
595             case ht_to_last (ZFirst (lg_entry g)) tail' of
596               (head', LastExit) ->
597                   case ht_to_block head' tail of
598                      Block id t | id == lg_entry g -> (t, LGraph id emptyBlockEnv)
599                      _ -> panic "entry in; garbage out"
600               _ -> panic "spliced single block without Exit" 
601           splice_many_blocks entry exit others =
602               (entry, LGraph (lg_entry g) (insertBlock (zipht exit tail) others))
603 -}
604
605 splice_head_only head g =
606   let FGraph eid gentry gblocks = entry g
607   in case gentry of
608        ZBlock (ZFirst _) tail -> LGraph eid (insertBlock (zipht head tail) gblocks)
609        _ -> panic "entry not at start of block?!"
610
611 splice_head_only' head (Graph tail gblocks) =
612   let eblock = zipht head tail in
613   LGraph (blockId eblock) (insertBlock eblock gblocks)
614
615
616 --- Translation
617
618 translate txm txl (LGraph eid blocks) =
619     do blocks' <- foldUFM txblock (return emptyBlockEnv) blocks
620        return $ LGraph eid blocks'
621     where
622       -- txblock ::
623       -- Block m l -> tm (BlockEnv (Block m' l')) -> tm (BlockEnv (Block m' l'))
624       txblock (Block id t) expanded =
625         do blocks' <- expanded
626            txtail (ZFirst id) t blocks'
627       -- txtail :: ZHead m' -> ZTail m l -> BlockEnv (Block m' l') ->
628       --           tm (BlockEnv (Block m' l'))
629       txtail h (ZTail m t) blocks' =
630         do m' <- txm m 
631            let (g, h') = splice_head h m' 
632            txtail h' t (plusUFM (lg_blocks g) blocks')
633       txtail h (ZLast (LastOther l)) blocks' =
634         do l' <- txl l
635            return $ plusUFM (lg_blocks (splice_head_only h l')) blocks'
636       txtail h (ZLast LastExit) blocks' =
637         return $ insertBlock (zipht h (ZLast LastExit)) blocks'
638
639 ----------------------------------------------------------------
640 --- Block Ids, their environments, and their sets
641
642 {- Note [Unique BlockId]
643 ~~~~~~~~~~~~~~~~~~~~~~~~
644 Although a 'BlockId' is a local label, for reasons of implementation,
645 'BlockId's must be unique within an entire compilation unit.  The reason
646 is that each local label is mapped to an assembly-language label, and in
647 most assembly languages allow, a label is visible throughout the enitre
648 compilation unit in which it appears.
649 -}
650
651 newtype BlockId = BlockId Unique
652   deriving (Eq,Ord)
653
654 instance Uniquable BlockId where
655   getUnique (BlockId u) = u
656
657 instance Show BlockId where
658   show (BlockId u) = show u
659
660 instance Outputable BlockId where
661   ppr = ppr . getUnique
662
663
664 type BlockEnv a = UniqFM {- BlockId -} a
665 emptyBlockEnv :: BlockEnv a
666 emptyBlockEnv = emptyUFM
667 lookupBlockEnv :: BlockEnv a -> BlockId -> Maybe a
668 lookupBlockEnv = lookupUFM
669 extendBlockEnv :: BlockEnv a -> BlockId -> a -> BlockEnv a
670 extendBlockEnv = addToUFM
671 mkBlockEnv :: [(BlockId,a)] -> BlockEnv a
672 mkBlockEnv = listToUFM
673
674 type BlockSet = UniqSet BlockId
675 emptyBlockSet :: BlockSet
676 emptyBlockSet = emptyUniqSet
677 elemBlockSet :: BlockId -> BlockSet -> Bool
678 elemBlockSet = elementOfUniqSet
679 extendBlockSet :: BlockSet -> BlockId -> BlockSet
680 extendBlockSet = addOneToUniqSet
681 mkBlockSet :: [BlockId] -> BlockSet
682 mkBlockSet = mkUniqSet
683
684 ----------------------------------------------------------------
685 ---- Prettyprinting
686 ----------------------------------------------------------------
687
688 -- putting this code in PprCmmZ leads to circular imports :-(
689
690 instance (Outputable m, Outputable l) => Outputable (ZTail m l) where
691     ppr = pprTail
692
693 pprTail :: (Outputable m, Outputable l) => ZTail m l -> SDoc 
694 pprTail (ZTail m t) = ppr m $$ ppr t
695 pprTail (ZLast LastExit) = text "<exit>"
696 pprTail (ZLast (LastOther l)) = ppr l
697
698 pprLgraph :: (Outputable m, Outputable l, LastNode l) => LGraph m l -> SDoc
699 pprLgraph g = text "{" $$ nest 2 (vcat $ map pprBlock blocks) $$ text "}"
700     where pprBlock (Block id tail) = ppr id <> colon $$ ppr tail
701           blocks = postorder_dfs g
702
703 pprGraph :: (Outputable m, Outputable l, LastNode l) => Graph m l -> SDoc
704 pprGraph (Graph tail blockenv) =
705         text "{" $$ nest 2 (ppr tail $$ (vcat $ map pprBlock blocks)) $$ text "}"
706     where pprBlock (Block id tail) = ppr id <> colon $$ ppr tail
707           blocks = postorder_dfs_from blockenv tail
708
709 _unused :: FS.FastString
710 _unused = undefined