--- Be careful not to mark a block as a lone branch if it carries
--- important information about incoming arguments or the update frame.
-isLoneBranchZ :: CmmBlock -> Either (BlockId, BlockId) CmmBlock
-isLoneBranchZ (G.Block id (StackInfo {argBytes = Nothing, returnOff = Nothing})
- (G.ZLast (G.LastOther (LastBranch target))))
- | id /= target = Left (id,target)
-isLoneBranchZ other = Right other
- -- An infinite loop is not a link in a branch chain!
+ call_succs = foldl add emptyBlockSet blocks
+ where add succs b =
+ case G.last (G.unzip b) of
+ LastOther (LastCall _ (Just k) _ _ _) -> extendBlockSet succs k
+ _ -> succs
+ isLoneBranchZ :: CmmBlock -> Either (BlockId, BlockId) CmmBlock
+ isLoneBranchZ (G.Block id (G.ZLast (G.LastOther (LastBranch target))))
+ | id /= target && not (elemBlockSet id call_succs) = Left (id,target)
+ isLoneBranchZ other = Right other
+ -- An infinite loop is not a link in a branch chain!
+
+maybeReplaceLabels :: (Last -> Bool) -> BlockEnv BlockId -> CmmGraph -> CmmGraph
+maybeReplaceLabels lpred env =
+ replace_eid . G.map_nodes id middle last
+ where
+ replace_eid (G.LGraph eid blocks) = G.LGraph (lookup eid) blocks
+ middle = mapExpDeepMiddle exp
+ last l = if lpred l then mapExpDeepLast exp (last' l) else l
+ last' (LastBranch bid) = LastBranch (lookup bid)
+ last' (LastCondBranch p t f) = LastCondBranch p (lookup t) (lookup f)
+ last' (LastSwitch e arms) = LastSwitch e (map (liftM lookup) arms)
+ last' (LastCall t k a res r) = LastCall t (liftM lookup k) a res r
+ exp (CmmLit (CmmBlock bid)) = CmmLit (CmmBlock (lookup bid))
+ exp (CmmStackSlot (CallArea (Young id)) i) =
+ CmmStackSlot (CallArea (Young (lookup id))) i
+ exp e = e
+ lookup id = fmap lookup (lookupBlockEnv env id) `orElse` id