Replacing copyins and copyouts with data-movement instructions
[ghc-hetmet.git] / compiler / cmm / PprCmmZ.hs
1
2 module PprCmmZ
3     ( pprCmmGraphLikeCmm
4     )
5 where
6
7 import BlockId
8 import Cmm
9 import CmmExpr
10 import ForeignCall
11 import PprCmm
12 import Outputable
13 import qualified ZipCfgCmmRep as G
14 import qualified ZipCfg as Z
15 import CmmZipUtil
16
17 import Maybe
18 import UniqSet
19 import FastString
20
21 ----------------------------------------------------------------
22 -- | The purpose of this function is to print a Cmm zipper graph "as if it were"
23 -- a Cmm program.  The objective is dodgy, so it's unsurprising parts of the
24 -- code are dodgy as well.
25
26 pprCmmGraphLikeCmm :: G.CmmGraph -> SDoc
27 pprCmmGraphLikeCmm g = vcat (swallow blocks)
28     where blocks = Z.postorder_dfs g
29           swallow :: [G.CmmBlock] -> [SDoc]
30           swallow [] = []
31           swallow (Z.Block id t : rest) = tail id [] Nothing t rest
32           tail id prev' out (Z.ZTail (G.CopyOut conv args) t) rest =
33               if isJust out then panic "multiple CopyOut nodes in one basic block"
34               else
35                   tail id (prev') (Just (conv, args)) t rest
36           tail id prev' out (Z.ZTail m t) rest = tail id (mid m : prev') out t rest
37           tail id prev' out (Z.ZLast Z.LastExit)      rest = exit id prev' out rest
38           tail id prev' out (Z.ZLast (Z.LastOther l)) rest = last id prev' out l rest
39           mid (G.CopyIn _ [] _) = text "// proc point (no parameters)"
40           mid m@(G.CopyIn {}) = ppr m <+> text "(proc point)"
41           mid m = ppr m
42           block' id prev'
43               | id == Z.lg_entry g, entry_has_no_pred =
44                             vcat (text "<entry>" : reverse prev')
45               | otherwise = hang (ppr id <> colon) 4 (vcat (reverse prev'))
46           last id prev' out l n =
47               let endblock stmt = block' id (stmt : prev') : swallow n in
48               case l of
49                 G.LastBranch tgt ->
50                     case n of
51                       Z.Block id' t : bs
52                           | tgt == id', unique_pred id' 
53                           -> tail id prev' out t bs  -- optimize out redundant labels
54                       _ -> endblock (ppr $ CmmBranch tgt)
55                 l@(G.LastCondBranch expr tid fid) ->
56                   let ft id = text "// fall through to " <> ppr id in
57                   case n of
58                     Z.Block id' t : bs
59                       | id' == fid, isNothing out ->
60                           tail id (ft fid : ppr (CmmCondBranch expr tid) : prev') Nothing t bs
61                       | id' == tid, Just e' <- maybeInvertCmmExpr expr, isNothing out->
62                           tail id (ft tid : ppr (CmmCondBranch e'   fid) : prev') Nothing t bs
63                     _ -> endblock $ with_out out l
64                 l@(G.LastJump   {}) -> endblock $ with_out out l
65                 l@(G.LastReturn {}) -> endblock $ with_out out l
66                 l@(G.LastSwitch {}) -> endblock $ with_out out l
67                 l@(G.LastCall _ Nothing) -> endblock $ with_out out l
68                 l@(G.LastCall tgt (Just k))
69                    | Z.Block id' (Z.ZTail (G.CopyIn _ ress srt) t) : bs <- n,
70                      Just (conv, args) <- out,
71                      id' == k ->
72                          let call = CmmCall tgt' ress args (CmmSafe srt) CmmMayReturn
73                              tgt' = CmmCallee tgt (cconv_of_conv conv)
74                              ppcall = ppr call <+> parens (text "ret to" <+> ppr k)
75                          in if unique_pred k then
76                                 tail id (ppcall : prev') Nothing t bs
77                             else
78                                 endblock (ppcall)
79                    | Z.Block id' t : bs <- n, id' == k, unique_pred k,
80                      Just (conv, args) <- out,
81                      Just (ress, srt) <- findCopyIn t ->
82                          let call = CmmCall tgt' ress args (CmmSafe srt) CmmMayReturn
83                              tgt' = CmmCallee tgt (cconv_of_conv conv)
84                              delayed =
85                                  ptext (sLit "// delayed CopyIn follows previous call")
86                          in  tail id (delayed : ppr call : prev') Nothing t bs
87                    | otherwise -> endblock $ with_out out l
88           findCopyIn (Z.ZTail (G.CopyIn _ ress srt) _) = Just (ress, srt)
89           findCopyIn (Z.ZTail _ t) = findCopyIn t
90           findCopyIn (Z.ZLast _) = Nothing
91           exit id prev' out n = -- highly irregular (assertion violation?)
92               let endblock stmt = block' id (stmt : prev') : swallow n in
93               case out of Nothing -> endblock (text "// <exit>")
94                           Just (conv, args) -> endblock (ppr (G.CopyOut conv args) $$
95                                                          text "// <exit>")
96           preds = zipPreds g
97           entry_has_no_pred = case lookupBlockEnv preds (Z.lg_entry g) of
98                                 Nothing -> True
99                                 Just s -> isEmptyUniqSet s
100           single_preds =
101               let add b single =
102                     let id = Z.blockId b
103                     in  case lookupBlockEnv preds id of
104                           Nothing -> single
105                           Just s -> if sizeUniqSet s == 1 then
106                                         extendBlockSet single id
107                                     else single
108               in  Z.fold_blocks add emptyBlockSet g
109           unique_pred id = elemBlockSet id single_preds
110           cconv_of_conv (G.ConventionStandard conv _) = conv
111           cconv_of_conv (G.ConventionPrivate {}) = CmmCallConv -- XXX totally bogus
112
113 with_out :: Maybe (G.Convention, CmmActuals) -> G.Last -> SDoc
114 with_out Nothing l = ptext (sLit "??no-arguments??") <+> ppr l
115 with_out (Just (conv, args)) l = last l
116     where last (G.LastCall e k) =
117               hcat [ptext (sLit "... = foreign "),
118                     doubleQuotes(ppr conv), space,
119                     ppr_target e, parens ( commafy $ map ppr args ),
120                     ptext (sLit " \"safe\""),
121                     case k of Nothing -> ptext (sLit " never returns")
122                               Just _ -> empty,
123                     semi ]
124           last (G.LastReturn) = ppr (CmmReturn args)
125           last (G.LastJump e) = ppr (CmmJump e args)
126           last l = ppr (G.CopyOut conv args) $$ ppr l
127           ppr_target (CmmLit lit) = pprLit lit
128           ppr_target fn'          = parens (ppr fn')
129           commafy xs = hsep $ punctuate comma xs