Minor refactoring and formatting
[ghc-hetmet.git] / compiler / cmm / CmmBuildInfoTables.hs
index e3d2ded..c2be8c9 100644 (file)
@@ -3,7 +3,9 @@ module CmmBuildInfoTables
     , setInfoTableSRT, setInfoTableStackMap
     , TopSRT, emptySRT, srtToData
     , bundleCAFs
-    , finishInfoTables, lowerSafeForeignCalls, extendEnvsForSafeForeignCalls )
+    , finishInfoTables, lowerSafeForeignCalls
+    , cafTransfers, liveSlotTransfers
+    , extendEnvWithSafeForeignCalls, extendEnvsForSafeForeignCalls )
 where
 
 #include "HsVersions.h"
@@ -18,28 +20,27 @@ import BlockId
 import Bitmap
 import CLabel
 import Cmm hiding (blockId)
-import CmmExpr
 import CmmInfo
 import CmmProcPointZ
 import CmmStackLayout
 import CmmTx
 import DFMonad
+import Module
 import FastString
 import FiniteMap
 import ForeignCall
 import IdInfo
-import List (sortBy)
+import Data.List
 import Maybes
 import MkZipCfg
 import MkZipCfgCmm hiding (CmmAGraph, CmmBlock, CmmTopZ, CmmZ, CmmGraph)
-import Monad
+import Control.Monad
 import Name
 import Outputable
-import Panic
 import SMRep
 import StgCmmClosure
 import StgCmmForeign
-import StgCmmMonad
+-- import StgCmmMonad
 import StgCmmUtils
 import UniqSupply
 import ZipCfg hiding (zip, unzip, last)
@@ -130,35 +131,13 @@ setInfoTableStackMap _ _ t@(NoInfoTable _) = t
 setInfoTableStackMap slotEnv areaMap t@(FloatingInfoTable _ bid updfr_off) =
   updInfo (const (live_ptrs updfr_off slotEnv areaMap bid)) id t
 setInfoTableStackMap slotEnv areaMap
-     t@(ProcInfoTable (CmmProc (CmmInfo _ _ infoTbl) _ _ g@(LGraph _ _ blocks))
-                      procpoints) =
+     t@(ProcInfoTable (CmmProc (CmmInfo _ _ _) _ _ ((_, Just updfr_off), _)) procpoints) =
   case blockSetToList procpoints of
-    [bid] ->
-      let oldByte = case infoTbl of
-                         CmmInfoTable _ _ _ (ContInfo _ _) -> 
-                           case lookupBlockEnv blocks bid of
-                              Just (Block _ (StackInfo {returnOff = Just n}) _) -> n
-                              _ -> pprPanic "misformed graph at procpoint" (ppr g)
-                         _ -> initUpdFrameOff -- entry to top-level function
-          stack_vars = live_ptrs oldByte slotEnv areaMap bid
-      in updInfo (const stack_vars) id t
-    _ -> panic "setInfoTableStackMap: unexpect number of procpoints"
+    [bid] -> updInfo (const (live_ptrs updfr_off slotEnv areaMap bid)) id t
+    _ -> panic "setInfoTableStackMap: unexpected number of procpoints"
            -- until we stop splitting the graphs at procpoints in the native path
-setInfoTableStackMap _ _ _ = panic "unexpected case for setInfoTableStackMap"
-{-
-setInfoTableStackMap slotEnv areaMap
-      (Just bid, p@(CmmProc (CmmInfo _ _ infoTbl) _ _ g@(LGraph entry _ blocks))) =
-  let oldByte = case infoTbl of
-                     CmmInfoTable _ _ _ (ContInfo _ _) -> 
-                       case lookupBlockEnv blocks bid of
-                          Just (Block _ (StackInfo {returnOff = Just n}) _) -> n
-                          _ -> pprPanic "misformed graph at procpoint" (ppr g)
-                     _ -> initUpdFrameOff -- entry to top-level function
-      stack_vars = live_ptrs oldByte slotEnv areaMap bid
-  in (Just bid, upd_info_tbl (const stack_vars) id p)
-setInfoTableStackMap _ _ t@(_, CmmData {}) = t
-setInfoTableStackMap _ _ _ = panic "bad args to setInfoTableStackMap"
--}
+setInfoTableStackMap _ _ t = pprPanic "unexpected case for setInfoTableStackMap" (ppr t)
+                 
 
 
 -----------------------------------------------------------------------
@@ -187,15 +166,15 @@ cafLattice = DataflowLattice "live cafs" emptyFM add False
 
 cafTransfers :: BackwardTransfers Middle Last CAFSet
 cafTransfers = BackwardTransfers first middle last
-    where first  live _ = live
-          middle live m = foldExpDeepMiddle addCaf m live
-          last   env  l = foldExpDeepLast addCaf l (joinOuts cafLattice env l)
-          addCaf e set = case e of
-                 CmmLit (CmmLabel c)              -> add c set
-                 CmmLit (CmmLabelOff c _)         -> add c set
-                 CmmLit (CmmLabelDiffOff c1 c2 _) -> add c1 $ add c2 set
-                 _ -> set
-          add l s = if hasCAF l then addToFM s (cvtToClosureLbl l) () else s
+  where first  _ live = live
+        middle m live = foldExpDeepMiddle addCaf m live
+        last   l env  = foldExpDeepLast   addCaf l (joinOuts cafLattice env l)
+        addCaf e set = case e of
+               CmmLit (CmmLabel c)              -> add c set
+               CmmLit (CmmLabelOff c _)         -> add c set
+               CmmLit (CmmLabelDiffOff c1 c2 _) -> add c1 $ add c2 set
+               _ -> set
+        add l s = if hasCAF l then addToFM s (cvtToClosureLbl l) () else s
 
 type CafFix a = FuelMonad (BackwardFixedPoint Middle Last CAFSet a)
 cafAnal :: LGraph Middle Last -> FuelMonad CAFEnv
@@ -253,6 +232,8 @@ buildSRTs topSRT topCAFMap cafs =
   do let liftCAF lbl () z = -- get CAFs for functions without static closures
            case lookupFM topCAFMap lbl of Just cafs -> z `plusFM` cafs
                                           Nothing   -> addToFM z lbl ()
+         -- For each label referring to a function f without a static closure,
+         -- replace it with the CAFs that are reachable from f.
          sub_srt topSRT localCafs =
            let cafs = keysFM (foldFM liftCAF emptyFM localCafs)
                mkSRT topSRT =
@@ -326,11 +307,11 @@ to_SRT top_srt off len bmp
 -- doesn't have a static closure.
 -- (If it has a static closure, it will already have an SRT to
 --  keep its CAFs live.)
--- Any procedure referring to a non-static CAF c must keep live the
+-- Any procedure referring to a non-static CAF c must keep live
 -- any CAF that is reachable from c.
 localCAFInfo :: CAFEnv -> CmmTopZ -> Maybe (CLabel, CAFSet)
 localCAFInfo _      (CmmData _ _) = Nothing
-localCAFInfo cafEnv (CmmProc (CmmInfo _ _ infoTbl) top_l _ (LGraph entry _ _)) =
+localCAFInfo cafEnv (CmmProc (CmmInfo _ _ infoTbl) top_l _ (_, LGraph entry _)) =
   case infoTbl of
     CmmInfoTable False _ _ _ ->
       Just (cvtToClosureLbl top_l,
@@ -370,7 +351,7 @@ type StackLayout = [Maybe LocalReg]
 bundleCAFs :: CAFEnv -> CmmTopForInfoTables -> (CAFSet, CmmTopForInfoTables)
 bundleCAFs cafEnv t@(ProcInfoTable _ procpoints) =
   case blockSetToList procpoints of
-    [bid] -> (expectJust "bundleCAFs " (lookupBlockEnv cafEnv bid), t)
+    [bid] -> (expectJust "bundleCAFs" (lookupBlockEnv cafEnv bid), t)
     _     -> panic "setInfoTableStackMap: unexpect number of procpoints"
              -- until we stop splitting the graphs at procpoints in the native path
 bundleCAFs cafEnv t@(FloatingInfoTable _ bid _) =
@@ -432,17 +413,33 @@ finishInfoTables (FloatingInfoTable (CmmInfo _ _ infotbl) bid _) =
 -- Our analyses capture the dataflow facts at block boundaries, but we need
 -- to extend the CAF and live-slot analyses to safe foreign calls as well,
 -- which show up as middle nodes.
+extendEnvWithSafeForeignCalls ::
+  BackwardTransfers Middle Last a -> BlockEnv a -> CmmGraph -> BlockEnv a
+extendEnvWithSafeForeignCalls transfers env g = fold_blocks block env g
+  where block b z =
+          tail (bt_last_in transfers l (lookup env)) z head
+           where (head, last) = goto_end (G.unzip b)
+                 l = case last of LastOther l -> l
+                                  LastExit -> panic "extendEnvs lastExit"
+        tail _ z (ZFirst _) = z
+        tail fact env (ZHead h m@(MidForeignCall (Safe bid _) _ _ _)) =
+          tail (mid m fact) (extendBlockEnv env bid fact) h
+        tail fact env (ZHead h m) = tail (mid m fact) env h
+        lookup map k = expectJust "extendEnvWithSafeFCalls" $ lookupBlockEnv map k
+        mid = bt_middle_in transfers
+
+
 extendEnvsForSafeForeignCalls :: CAFEnv -> SlotEnv -> CmmGraph -> (CAFEnv, SlotEnv)
 extendEnvsForSafeForeignCalls cafEnv slotEnv g =
   fold_blocks block (cafEnv, slotEnv) g
     where block b z =
-            tail ( bt_last_in cafTransfers      (lookupFn cafEnv)  l
-                 , bt_last_in liveSlotTransfers (lookupFn slotEnv) l)
+            tail ( bt_last_in cafTransfers      l (lookupFn cafEnv)
+                 , bt_last_in liveSlotTransfers l (lookupFn slotEnv))
                  z head
              where (head, last) = goto_end (G.unzip b)
                    l = case last of LastOther l -> l
                                     LastExit -> panic "extendEnvs lastExit"
-          tail _ z (ZFirst _ _) = z
+          tail _ z (ZFirst _) = z
           tail lives@(cafs, slots) (cafEnv, slotEnv)
                (ZHead h m@(MidForeignCall (Safe bid _) _ _ _)) =
             let slots'   = removeLiveSlotDefs slots m
@@ -452,7 +449,7 @@ extendEnvsForSafeForeignCalls cafEnv slotEnv g =
           tail lives z (ZHead h m) = tail (upd lives m) z h
           lookupFn map k = expectJust "extendEnvsForSafeFCalls" $ lookupBlockEnv map k
           upd (cafs, slots) m =
-            (bt_middle_in cafTransfers cafs m, bt_middle_in liveSlotTransfers slots m)
+            (bt_middle_in cafTransfers m cafs, bt_middle_in liveSlotTransfers m slots)
 
 -- Safe foreign calls: We need to insert the code that suspends and resumes
 -- the thread before and after a safe foreign call.
@@ -489,9 +486,9 @@ data SafeState = State { s_blocks    :: BlockEnv CmmBlock
 lowerSafeForeignCalls
   :: [[CmmTopForInfoTables]] -> CmmTopZ -> FuelMonad [[CmmTopForInfoTables]]
 lowerSafeForeignCalls rst t@(CmmData _ _) = return $ [NoInfoTable t] : rst
-lowerSafeForeignCalls rst (CmmProc info l args g@(LGraph entry off _)) = do
+lowerSafeForeignCalls rst (CmmProc info l args (off, g@(LGraph entry _))) = do
   let init = return $ State emptyBlockEnv emptyBlockSet []
-  let block b@(Block bid _ _) z = do
+  let block b@(Block bid _) z = do
         state@(State {s_pps = ppset, s_blocks = blocks}) <- z
         let ppset' = if bid == entry then extendBlockSet ppset bid else ppset
             state' = state { s_pps = ppset' }
@@ -499,13 +496,15 @@ lowerSafeForeignCalls rst (CmmProc info l args g@(LGraph entry off _)) = do
          then lowerSafeCallBlock state' b
          else return (state' { s_blocks = insertBlock b blocks })
   State blocks' g_procpoints safeCalls <- fold_blocks block init g
-  return $ safeCalls
-           : [ProcInfoTable (CmmProc info l args (LGraph entry off blocks')) g_procpoints]
-           : rst
+  let proc = (CmmProc info l args (off, LGraph entry blocks'))
+      procTable = case off of
+                    (_, Just _) -> [ProcInfoTable proc g_procpoints]
+                    _ -> [NoInfoTable proc] -- not a successor of a call
+  return $ safeCalls : procTable : rst
 
 -- Check for foreign calls -- if none, then we can avoid copying the block.
 hasSafeForeignCall :: CmmBlock -> Bool
-hasSafeForeignCall (Block _ _ t) = tail t
+hasSafeForeignCall (Block _ t) = tail t
   where tail (ZTail (MidForeignCall (Safe _ _) _ _ _) _) = True
         tail (ZTail _ t) = tail t
         tail (ZLast _)   = False
@@ -515,10 +514,10 @@ hasSafeForeignCall (Block _ _ t) = tail t
 lowerSafeCallBlock :: SafeState-> CmmBlock -> FuelMonad SafeState
 lowerSafeCallBlock state b = tail (return state) (ZBlock head (ZLast last))
   where (head, last) = goto_end (G.unzip b)
-        tail s b@(ZBlock (ZFirst _ _) _) =
+        tail s b@(ZBlock (ZFirst _) _) =
           do state <- s
              return $ state { s_blocks = insertBlock (G.zip b) (s_blocks state) }
-        tail  s (ZBlock (ZHead h m@(MidForeignCall (Safe bid updfr_off) _ _ _)) t) =
+        tail s (ZBlock (ZHead h m@(MidForeignCall (Safe bid updfr_off) _ _ _)) t) =
           do state <- s
              let state' = state
                    { s_safeCalls = FloatingInfoTable emptyContInfoTable bid updfr_off :
@@ -540,8 +539,8 @@ lowerSafeForeignCall state m@(MidForeignCall (Safe infotable _) _ _ _) tail = do
     new_base <- newTemp (cmmRegType (CmmGlobal BaseReg))
     let (caller_save, caller_load) = callerSaveVolatileRegs 
     load_tso <- newTemp gcWord -- TODO FIXME NOW
-    let suspendThread = CmmLit (CmmLabel (mkRtsCodeLabel (sLit "suspendThread")))
-        resumeThread  = CmmLit (CmmLabel (mkRtsCodeLabel (sLit "resumeThread")))
+    let suspendThread = CmmLit (CmmLabel (mkCmmCodeLabel rtsPackageId (fsLit "suspendThread")))
+        resumeThread  = CmmLit (CmmLabel (mkCmmCodeLabel rtsPackageId (fsLit "resumeThread")))
         suspend = mkStore (CmmReg spReg) (CmmLit (CmmBlock infotable)) <*>
                   saveThreadState <*>
                   caller_save <*>