refactoring only: use the parameterised InstalledPackageInfo
[ghc-hetmet.git] / compiler / profiling / SCCfinal.lhs
index c95db9c..dd72341 100644 (file)
@@ -23,19 +23,28 @@ This is now a sort-of-normal STG-to-STG pass (WDP 94/06), run by stg2stg.
 * "Distributes" given cost-centres to all as-yet-unmarked RHSs.
 
 \begin{code}
+{-# OPTIONS -w #-}
+-- The above warning supression flag is a temporary kludge.
+-- While working on this module you are encouraged to remove it and fix
+-- any warnings in the module. See
+--     http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
+-- for details
+
 module SCCfinal ( stgMassageForProfiling ) where
 
 #include "HsVersions.h"
 
 import StgSyn
 
-import Packages                ( HomeModules )
 import StaticFlags     ( opt_AutoSccsOnIndividualCafs )
 import CostCentre      -- lots of things
-import Id              ( Id )
-import Module          ( Module )
-import UniqSupply      ( uniqFromSupply, splitUniqSupply, UniqSupply )
-import Unique           ( Unique )
+import Id
+import Name
+import Module
+import UniqSupply      ( splitUniqSupply, UniqSupply )
+#ifdef PROF_DO_BOXING
+import UniqSupply      ( uniqFromSupply )
+#endif
 import VarSet
 import ListSetOps      ( removeDups )
 import Outputable      
@@ -45,13 +54,13 @@ infixr 9 `thenMM`, `thenMM_`
 
 \begin{code}
 stgMassageForProfiling
-       :: HomeModules
+       :: PackageId
        -> Module                       -- module name
        -> UniqSupply                   -- unique supply
        -> [StgBinding]                 -- input
        -> (CollectedCCs, [StgBinding])
 
-stgMassageForProfiling pdeps mod_name us stg_binds
+stgMassageForProfiling this_pkg mod_name us stg_binds
   = let
        ((local_ccs, extern_ccs, cc_stacks),
         stg_binds2)
@@ -102,7 +111,7 @@ stgMassageForProfiling pdeps mod_name us stg_binds
     do_top_rhs :: Id -> StgRhs -> MassageM StgRhs
 
     do_top_rhs binder (StgRhsClosure _ bi fv u srt [] (StgSCC cc (StgConApp con args)))
-      | not (isSccCountCostCentre cc) && not (isDllConApp pdeps con args)
+      | not (isSccCountCostCentre cc) && not (isDllConApp this_pkg con args)
        -- Trivial _scc_ around nothing but static data
        -- Eliminate _scc_ ... and turn into StgRhsCon
 
@@ -125,10 +134,15 @@ stgMassageForProfiling pdeps mod_name us stg_binds
        -- Top level CAF without a cost centre attached
        -- Attach CAF cc (collect if individual CAF ccs)
       = (if opt_AutoSccsOnIndividualCafs 
-               then let cc = mkAutoCC binder mod_name CafCC
+               then let cc = mkAutoCC binder modl CafCC
                         ccs = mkSingletonCCS cc
+                                -- careful: the binder might be :Main.main,
+                                -- which doesn't belong to module mod_name.
+                                -- bug #249, tests prof001, prof002
+                         modl | Just m <- nameModule_maybe (idName binder) = m
+                              | otherwise = mod_name
                     in
-                    collectCC  cc  `thenMM_`
+                    collectNewCC  cc  `thenMM_`
                     collectCCS ccs `thenMM_`
                     returnMM ccs
                else 
@@ -192,6 +206,10 @@ stgMassageForProfiling pdeps mod_name us stg_binds
        = do_let b e `thenMM` \ (b,e) ->
          returnMM (StgLetNoEscape lvs1 lvs2 b e)
 
+    do_expr (StgTick m n expr) 
+        = do_expr expr `thenMM` \ expr' ->
+          returnMM (StgTick m n expr')
+
 #ifdef DEBUG
     do_expr other = pprPanic "SCCfinal.do_expr" (ppr other)
 #endif
@@ -358,8 +376,10 @@ mapAccumMM f b (m:ms)
     mapAccumMM f b2 ms `thenMM` \ (b3, rs) ->
     returnMM (b3, r:rs)
 
+#ifdef PROF_DO_BOXING
 getUniqueMM :: MassageM Unique
 getUniqueMM mod scope_cc us ids ccs = (ccs, uniqFromSupply us)
+#endif
 
 addTopLevelIshId :: Id -> MassageM a -> MassageM a
 addTopLevelIshId id scope mod scope_cc us ids ccs
@@ -403,6 +423,14 @@ collectCC cc mod_name scope_cc us ids (local_ccs, extern_ccs, ccss)
     else -- must declare it "extern"
        ((local_ccs, cc : extern_ccs, ccss), ())
 
+-- Version of collectCC used when we definitely want to declare this
+-- CC as local, even if its module name is not the same as the current
+-- module name (eg. the special :Main module) see bug #249, #1472, 
+-- test prof001,prof002.
+collectNewCC :: CostCentre -> MassageM ()
+collectNewCC cc mod_name scope_cc us ids (local_ccs, extern_ccs, ccss)
+  = ((cc : local_ccs, extern_ccs, ccss), ())
+
 collectCCS :: CostCentreStack -> MassageM ()
 
 collectCCS ccs mod_name scope_cc us ids (local_ccs, extern_ccs, ccss)