A few bug fixes; some improvements spurred by paper writing
[ghc-hetmet.git] / compiler / cmm / CmmContFlowOpt.hs
index a3239b9..c4d048d 100644 (file)
@@ -16,7 +16,6 @@ import ZipCfgCmmRep
 import Maybes
 import Monad
 import Outputable
-import Panic
 import Prelude hiding (unzip, zip)
 import Util
 
@@ -27,20 +26,25 @@ runCmmContFlowOptsZs prog
     | cmm_top <- prog ]
 
 cmmCfgOpts  :: Tx (ListGraph CmmStmt)
-cmmCfgOptsZ :: Tx CmmGraph
+cmmCfgOptsZ :: Tx (a, CmmGraph)
 
 cmmCfgOpts  = branchChainElim  -- boring, but will get more exciting later
 cmmCfgOptsZ g =
+  optGraph
     (branchChainElimZ `seqTx` blockConcatZ `seqTx` removeUnreachableBlocksZ) g
         -- Here branchChainElim can ultimately be replaced
         -- with a more exciting combination of optimisations
 
 runCmmOpts :: Tx g -> Tx (GenCmm d h g)
-runCmmOpts opt = mapProcs (optGraph opt)
+runCmmOpts opt = mapProcs (optProc opt)
 
-optGraph :: Tx g -> Tx (GenCmmTop d h g)
-optGraph _   top@(CmmData {}) = noTx top
-optGraph opt (CmmProc info lbl formals g) = fmap (CmmProc info lbl formals) (opt g)
+optProc :: Tx g -> Tx (GenCmmTop d h g)
+optProc _   top@(CmmData {}) = noTx top
+optProc opt (CmmProc info lbl formals g) =
+  fmap (CmmProc info lbl formals) (opt g)
+
+optGraph :: Tx g -> Tx (a, g)
+optGraph opt (a, g) = fmap (\g' -> (a, g')) (opt g)
 
 ------------------------------------
 mapProcs :: Tx (GenCmmTop d h s) -> Tx (GenCmm d h s)
@@ -80,28 +84,25 @@ replaceLabels env (BasicBlock id stmts)
 branchChainElimZ :: Tx CmmGraph
 -- Remove any basic block of the form L: goto L',
 -- and replace L with L' everywhere else
-branchChainElimZ g@(G.LGraph eid args _)
+branchChainElimZ g@(G.LGraph eid _)
   | null lone_branch_blocks     -- No blocks to remove
   = noTx g
   | otherwise
-  = aTx $ replaceLabelsZ env $ G.of_block_list eid args (self_branches ++ others)
+  = aTx $ replaceLabelsZ env $ G.of_block_list eid (self_branches ++ others)
   where
     (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 emptyStackInfo (G.ZLast (G.mkBranchNode id)))
+              Just (G.Block id (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 (StackInfo {argBytes = Nothing, returnOff = Nothing})
-              (G.ZLast (G.LastOther (LastBranch target))))
+isLoneBranchZ (G.Block id (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!
@@ -109,13 +110,13 @@ isLoneBranchZ other = Right other
 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
+    replace_eid (G.LGraph eid blocks) = G.LGraph (lookup eid) blocks
     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
+    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
@@ -136,7 +137,7 @@ replaceBranches env g = map_nodes id id last g
 predMap :: G.LastNode l => G.LGraph m l -> BlockEnv BlockSet
 predMap g = G.fold_blocks add_preds emptyBlockEnv g -- find the back edges
   where add_preds b env = foldl (add b) env (G.succs b)
-        add (G.Block bid _ _) env b' =
+        add (G.Block bid _) env b' =
           extendBlockEnv env b' $
                 extendBlockSet (lookupBlockEnv env b' `orElse` emptyBlockSet) bid
 ----------------------------------------------------------------
@@ -153,11 +154,11 @@ predMap g = G.fold_blocks add_preds emptyBlockEnv g -- find the back edges
 blockConcatZ  :: Tx CmmGraph
 blockConcatZ = removeUnreachableBlocksZ `seqTx` blockConcatZ'
 blockConcatZ' :: Tx CmmGraph
-blockConcatZ' g@(G.LGraph eid off blocks) =
-  tx $ replaceLabelsZ concatMap $ G.LGraph eid off blocks'
+blockConcatZ' g@(G.LGraph eid blocks) =
+  tx $ replaceLabelsZ concatMap $ G.LGraph eid blocks'
   where (changed, blocks', concatMap) =
            foldr maybe_concat (False, blocks, emptyBlockEnv) $ G.postorder_dfs g
-        maybe_concat b@(G.Block bid _ _) (changed, blocks', concatMap) =
+        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')) ->
@@ -167,17 +168,11 @@ blockConcatZ' g@(G.LGraph eid off blocks) =
                   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
+        canConcatWith b' = num_preds b' == 1
         backEdges = predMap g
         splice blocks' h bid' =
           case lookupBlockEnv blocks' bid' of
-            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"
+            Just (G.Block _ t) -> G.zip $ G.ZBlock h t
             Nothing -> pprPanic "unknown successor block" (ppr bid' <+> ppr blocks' <+> ppr blocks)
         tx = if changed then aTx else noTx
 ----------------------------------------------------------------
@@ -197,7 +192,7 @@ mkClosureBlockEnvZ blocks = mkBlockEnv $ map follow blocks
                                _ -> id
 ----------------------------------------------------------------
 removeUnreachableBlocksZ :: Tx CmmGraph
-removeUnreachableBlocksZ g@(G.LGraph id off blocks) =
-  if length blocks' < sizeBEnv blocks then aTx $ G.of_block_list id off blocks'
+removeUnreachableBlocksZ g@(G.LGraph id blocks) =
+  if length blocks' < sizeBEnv blocks then aTx $ G.of_block_list id blocks'
   else noTx g
     where blocks' = G.postorder_dfs g