1 {-# LANGUAGE PatternGuards #-}
4 ( cmmToZgraph, cmmOfZgraph )
10 import MkZipCfgCmm hiding (CmmGraph)
11 import ZipCfgCmmRep -- imported for reverse conversion
15 import qualified ZipCfg as G
26 cmmToZgraph :: GenCmm d h (ListGraph CmmStmt) -> UniqSM (GenCmm d h CmmGraph)
27 cmmOfZgraph :: GenCmm d h (CmmGraph) -> GenCmm d h (ListGraph CmmStmt)
29 cmmToZgraph (Cmm tops) = liftM Cmm $ mapM mapTop tops
30 where mapTop (CmmProc h l args g) =
31 toZgraph (showSDoc $ ppr l) args g >>= return . CmmProc h l args
32 mapTop (CmmData s ds) = return $ CmmData s ds
33 cmmOfZgraph = cmmMapGraph ofZgraph
36 toZgraph :: String -> CmmFormalsWithoutKinds -> ListGraph CmmStmt -> UniqSM CmmGraph
37 toZgraph _ _ (ListGraph []) = lgraphOfAGraph emptyAGraph
38 toZgraph fun_name args g@(ListGraph (BasicBlock id ss : other_blocks)) =
39 labelAGraph id $ mkMiddles (mkEntry id undefined args) <*>
40 mkStmts ss <*> foldr addBlock emptyAGraph other_blocks
41 where addBlock (BasicBlock id ss) g = mkLabel id <*> mkStmts ss <*> g
42 mkStmts (CmmNop : ss) = mkNop <*> mkStmts ss
43 mkStmts (CmmComment s : ss) = mkComment s <*> mkStmts ss
44 mkStmts (CmmAssign l r : ss) = mkAssign l r <*> mkStmts ss
45 mkStmts (CmmStore l r : ss) = mkStore l r <*> mkStmts ss
46 mkStmts (CmmCall (CmmCallee f conv) res args (CmmSafe srt) CmmMayReturn : ss) =
47 mkCall f conv res args srt <*> mkStmts ss
48 mkStmts (CmmCall (CmmPrim {}) _ _ (CmmSafe _) _ : _) =
49 panic "safe call to a primitive CmmPrim CallishMachOp"
50 mkStmts (CmmCall f res args CmmUnsafe CmmMayReturn : ss) =
51 mkUnsafeCall f res args <*> mkStmts ss
52 mkStmts (CmmCondBranch e l : fbranch) =
53 mkCmmIfThenElse e (mkBranch l) (mkStmts fbranch)
54 mkStmts (last : []) = mkLast last
55 mkStmts [] = bad "fell off end"
56 mkStmts (_ : _ : _) = bad "last node not at end"
57 bad msg = pprPanic (msg ++ " in function " ++ fun_name) (ppr g)
58 mkLast (CmmCall (CmmCallee f conv) [] args _ CmmNeverReturns) =
59 mkFinalCall f conv args
60 mkLast (CmmCall (CmmPrim {}) _ _ _ CmmNeverReturns) =
61 panic "Call to CmmPrim never returns?!"
62 mkLast (CmmSwitch scrutinee table) = mkSwitch scrutinee table
63 mkLast (CmmJump tgt args) = mkJump tgt args
64 mkLast (CmmReturn ress) = mkReturn ress
65 mkLast (CmmBranch tgt) = mkBranch tgt
66 mkLast (CmmCall _f (_:_) _args _ CmmNeverReturns) =
67 panic "Call never returns but has results?!"
68 mkLast _ = panic "fell off end of block"
70 ofZgraph :: CmmGraph -> ListGraph CmmStmt
71 ofZgraph g = ListGraph $ swallow blocks
72 where blocks = G.postorder_dfs g
73 -- | the next two functions are hooks on which to hang debugging info
74 extend_entry stmts = stmts
75 extend_block _id stmts = stmts
76 _extend_entry stmts = scomment showblocks : scomment cscomm : stmts
77 showblocks = "LGraph has " ++ show (length blocks) ++ " blocks:" ++
78 concat (map (\(G.Block id _) -> " " ++ show id) blocks)
79 cscomm = "Call successors are" ++
80 (concat $ map (\id -> " " ++ show id) $ uniqSetToList call_succs)
82 swallow (G.Block id t : rest) = tail id [] Nothing t rest
83 tail id prev' out (G.ZTail (CopyOut conv actuals) t) rest =
85 Nothing -> tail id prev' (Just (conv, actuals)) t rest
86 Just _ -> panic "multiple CopyOut nodes in one basic block"
87 tail id prev' out (G.ZTail m t) rest = tail id (mid m : prev') out t rest
88 tail id prev' out (G.ZLast G.LastExit) rest = exit id prev' out rest
89 tail id prev' out (G.ZLast (G.LastOther l)) rest = last id prev' out l rest
90 mid (MidComment s) = CmmComment s
91 mid (MidAssign l r) = CmmAssign l r
92 mid (MidStore l r) = CmmStore l r
93 mid (MidUnsafeCall f ress args) = CmmCall f ress args CmmUnsafe CmmMayReturn
94 mid m@(MidAddToContext {}) = pcomment (ppr m)
95 mid m@(CopyOut {}) = pcomment (ppr m)
96 mid m@(CopyIn {}) = pcomment (ppr m <+> text "(proc point)")
97 pcomment p = scomment $ showSDoc p
99 | id == G.lg_entry g = BasicBlock id $ extend_entry (reverse prev')
100 | otherwise = BasicBlock id $ extend_block id (reverse prev')
101 last id prev' out l n =
102 let endblock stmt = block' id (stmt : prev') : swallow n in
107 | tgt == id', unique_pred id'
108 -> tail id prev' out t bs -- optimize out redundant labels
109 _ -> if isNothing out then endblock (CmmBranch tgt)
110 else pprPanic "can't convert LGraph with pending CopyOut"
111 (text "target" <+> ppr tgt <+> ppr g)
112 LastCondBranch expr tid fid ->
113 if isJust out then pprPanic "CopyOut before conditional branch" (ppr g)
117 | id' == fid, unique_pred id' ->
118 tail id (CmmCondBranch expr tid : prev') Nothing t bs
119 | id' == tid, unique_pred id',
120 Just e' <- maybeInvertCmmExpr expr ->
121 tail id (CmmCondBranch e' fid : prev') Nothing t bs
122 _ -> let instrs' = CmmBranch fid : CmmCondBranch expr tid : prev'
123 in block' id instrs' : swallow n
124 LastJump expr -> endblock $ with_out out $ CmmJump expr
125 LastReturn -> endblock $ with_out out $ CmmReturn
126 LastSwitch arg ids -> endblock $ CmmSwitch arg $ ids
128 | Just (conv, args) <- out
129 -> let tgt = CmmCallee e (conv_to_cconv conv) in
132 endblock $ CmmCall tgt [] args CmmUnsafe CmmNeverReturns
134 | G.Block id' (G.ZTail (CopyIn _ ress srt) t) : bs <- n,
135 id' == k, unique_pred k
136 -> let call = CmmCall tgt ress args (CmmSafe srt) CmmMayReturn
137 in tail id (call : prev') Nothing t bs
138 | G.Block id' t : bs <- n, id' == k, unique_pred k
139 -> let (ress, srt) = findCopyIn t
140 call = CmmCall tgt ress args (CmmSafe srt) CmmMayReturn
141 delayed = scomment "delayed CopyIn follows prev. call"
142 in tail id (delayed : call : prev') Nothing t bs
143 | otherwise -> panic "unrepairable call"
144 | otherwise -> panic "call with no CopyOut"
145 with_out (Just (_conv, actuals)) f = f actuals
146 with_out Nothing f = pprPanic "unrepairable data flow to" (ppr $ f [])
147 findCopyIn (G.ZTail (CopyIn _ ress srt) _) = (ress, srt)
148 findCopyIn (G.ZTail _ t) = findCopyIn t
149 findCopyIn (G.ZLast _) = panic "missing CopyIn after call"
150 exit id prev' out n = -- highly irregular (assertion violation?)
151 let endblock stmt = block' id (stmt : prev') : swallow n in
152 case n of [] -> endblock (scomment "procedure falls off end")
153 G.Block id' t : bs ->
154 if unique_pred id' then
155 tail id (scomment "went thru exit" : prev') out t bs
157 endblock (CmmBranch id')
158 conv_to_cconv (ConventionStandard c _) = c
159 conv_to_cconv (ConventionPrivate {}) =
160 panic "tried to convert private calling convention back to Cmm"
165 in case lookupBlockEnv preds id of
167 Just s -> if sizeUniqSet s == 1 then
168 extendBlockSet single id
170 in G.fold_blocks add emptyBlockSet g
171 unique_pred id = elemBlockSet id single_preds
174 case G.last (G.unzip b) of
175 G.LastOther (LastCall _ (Just id)) -> extendBlockSet succs id
177 in G.fold_blocks add emptyBlockSet g
178 _is_call_succ id = elemBlockSet id call_succs
180 scomment :: String -> CmmStmt
181 scomment s = CmmComment $ mkFastString s