Big collection of patches for the new codegen branch.
[ghc-hetmet.git] / compiler / cmm / CmmContFlowOpt.hs
index 320b1e7..a3239b9 100644 (file)
@@ -2,7 +2,7 @@
 module CmmContFlowOpt
     ( runCmmOpts, cmmCfgOpts, cmmCfgOptsZ
     , branchChainElimZ, removeUnreachableBlocksZ, predMap
-    , replaceLabelsZ, runCmmContFlowOptsZs
+    , replaceLabelsZ, replaceBranches, runCmmContFlowOptsZs
     )
 where
 
@@ -19,7 +19,6 @@ import Outputable
 import Panic
 import Prelude hiding (unzip, zip)
 import Util
-import UniqFM
 
 ------------------------------------
 runCmmContFlowOptsZs :: [CmmZ] -> [CmmZ]
@@ -31,7 +30,8 @@ cmmCfgOpts  :: Tx (ListGraph CmmStmt)
 cmmCfgOptsZ :: Tx CmmGraph
 
 cmmCfgOpts  = branchChainElim  -- boring, but will get more exciting later
-cmmCfgOptsZ = branchChainElimZ `seqTx` blockConcatZ `seqTx` removeUnreachableBlocksZ
+cmmCfgOptsZ g =
+    (branchChainElimZ `seqTx` blockConcatZ `seqTx` removeUnreachableBlocksZ) g
         -- Here branchChainElim can ultimately be replaced
         -- with a more exciting combination of optimisations
 
@@ -89,16 +89,19 @@ branchChainElimZ g@(G.LGraph eid args _)
     (lone_branch_blocks, others) = partitionWith isLoneBranchZ (G.to_block_list g)
     env = mkClosureBlockEnvZ lone_branch_blocks
     self_branches =
-        let loop_to (id, _) =
-                if lookup id == id then
-                    Just (G.Block id Nothing (G.ZLast (G.mkBranchNode id)))
-                else
-                    Nothing
-        in  mapMaybe loop_to lone_branch_blocks
+      let loop_to (id, _) =
+            if lookup id == id then
+              Just (G.Block id emptyStackInfo (G.ZLast (G.mkBranchNode id)))
+            else
+              Nothing
+      in  mapMaybe loop_to lone_branch_blocks
     lookup id = lookupBlockEnv env id `orElse` id 
 
+-- 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 Nothing (G.ZLast (G.LastOther (LastBranch target))))
+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!
@@ -107,27 +110,25 @@ replaceLabelsZ :: BlockEnv BlockId -> CmmGraph -> CmmGraph
 replaceLabelsZ env = replace_eid . G.map_nodes id middle last
   where
     replace_eid (G.LGraph eid off blocks) = G.LGraph (lookup eid) off blocks
-    middle m@(MidComment _)            = m
-    middle   (MidAssign r e)           = MidAssign r (exp e)
-    middle   (MidStore addr e)         = MidStore (exp addr) (exp e)
-    middle   (MidUnsafeCall tgt fs as) = MidUnsafeCall (midcall tgt) fs (map exp as)
-    middle   (MidAddToContext e es)    = MidAddToContext (exp e) (map exp es)
-    last (LastBranch id)             = LastBranch (lookup id)
-    last (LastCondBranch e ti fi)    = LastCondBranch (exp e) (lookup ti) (lookup fi)
-    last (LastSwitch e tbl)          = LastSwitch (exp e) (map (fmap lookup) tbl)
-    last (LastCall tgt mb_id s)      = LastCall (exp tgt) (fmap lookup mb_id) s
-    last (LastJump e s)              = LastJump (exp e) s
-    last (LastReturn s)              = LastReturn s
-    midcall   (ForeignTarget e c)    = ForeignTarget (exp e) c
-    midcall m@(PrimTarget _)         = m
-    exp e@(CmmLit _)         = e
-    exp   (CmmLoad addr ty)  = CmmLoad (exp addr) ty
-    exp e@(CmmReg _)         = e
-    exp   (CmmMachOp op es)  = CmmMachOp op $ map exp es
-    exp e@(CmmRegOff _ _)    = e
+    middle = mapExpDeepMiddle exp
+    last l = mapExpDeepLast   exp (last' 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 r) = LastCall t (liftM lookup k) a r
+    exp (CmmLit (CmmBlock bid)) = CmmLit (CmmBlock (lookup bid))
     exp   (CmmStackSlot (CallArea (Young id)) i) =
       CmmStackSlot (CallArea (Young (lookup id))) i
-    exp e@(CmmStackSlot _ _) = e
+    exp e = e
+    lookup id = fmap lookup (lookupBlockEnv env id) `orElse` id 
+
+replaceBranches :: BlockEnv BlockId -> CmmGraph -> CmmGraph
+replaceBranches env g = map_nodes id id last g
+  where
+    last (LastBranch id)          = LastBranch (lookup id)
+    last (LastCondBranch e ti fi) = LastCondBranch e (lookup ti) (lookup fi)
+    last (LastSwitch e tbl)       = LastSwitch e (map (fmap lookup) tbl)
+    last l@(LastCall {})          = l
     lookup id = fmap lookup (lookupBlockEnv env id) `orElse` id 
 
 ----------------------------------------------------------------
@@ -146,35 +147,38 @@ predMap g = G.fold_blocks add_preds emptyBlockEnv g -- find the back edges
 -- Order matters, so we work bottom up (reverse postorder DFS).
 --
 -- To ensure correctness, we have to make sure that the BlockId of the block
--- we are about to eliminate is not named in another instruction
--- (except an adjacent stack pointer adjustment, which we expect and also eliminate).
--- For 
+-- we are about to eliminate is not named in another instruction.
 --
 -- Note: This optimization does _not_ subsume branch chain elimination.
 blockConcatZ  :: Tx CmmGraph
 blockConcatZ = removeUnreachableBlocksZ `seqTx` blockConcatZ'
 blockConcatZ' :: Tx CmmGraph
 blockConcatZ' g@(G.LGraph eid off blocks) =
-  tx $ pprTrace "concatMap" (ppr concatMap) $ replaceLabelsZ concatMap $ G.LGraph eid off blocks'
+  tx $ replaceLabelsZ concatMap $ G.LGraph eid off blocks'
   where (changed, blocks', concatMap) =
            foldr maybe_concat (False, blocks, emptyBlockEnv) $ G.postorder_dfs g
         maybe_concat b@(G.Block bid _ _) (changed, blocks', concatMap) =
           let unchanged = (changed, extendBlockEnv blocks' bid b, concatMap)
           in case G.goto_end $ G.unzip b of
                (h, G.LastOther (LastBranch b')) ->
-                  if num_preds b' == 1 then
+                  if canConcatWith b' then
                     (True, extendBlockEnv blocks' bid $ splice blocks' h b',
                      extendBlockEnv concatMap b' bid)
                   else unchanged
                _ -> unchanged
         num_preds bid = liftM sizeBlockSet (lookupBlockEnv backEdges bid) `orElse` 0
+        canConcatWith b' =
+          case lookupBlockEnv blocks b' of
+            Just (G.Block _ (StackInfo {returnOff = Nothing}) _) -> num_preds b' == 1
+            _ -> False
         backEdges = predMap g
         splice blocks' h bid' =
           case lookupBlockEnv blocks' bid' of
-            Just (G.Block _ Nothing t) -> G.zip $ G.ZBlock h t
-            Just (G.Block _ (Just _) _) ->
+            Just (G.Block _ (StackInfo {returnOff = Nothing}) t) ->
+              G.zip $ G.ZBlock h t
+            Just (G.Block _ _ _) ->
               panic "trying to concatenate but successor block has incoming args"
-            Nothing -> panic "unknown successor block"
+            Nothing -> pprPanic "unknown successor block" (ppr bid' <+> ppr blocks' <+> ppr blocks)
         tx = if changed then aTx else noTx
 ----------------------------------------------------------------
 mkClosureBlockEnv :: [(BlockId, BlockId)] -> BlockEnv BlockId
@@ -194,6 +198,6 @@ mkClosureBlockEnvZ blocks = mkBlockEnv $ map follow blocks
 ----------------------------------------------------------------
 removeUnreachableBlocksZ :: Tx CmmGraph
 removeUnreachableBlocksZ g@(G.LGraph id off blocks) =
-      if length blocks' < sizeUFM blocks then aTx $ G.of_block_list id off blocks'
-      else noTx g
+  if length blocks' < sizeBEnv blocks then aTx $ G.of_block_list id off blocks'
+  else noTx g
     where blocks' = G.postorder_dfs g