CmmPipeline.hs 6.76 KB
Newer Older
1
{-# LANGUAGE NoMonoLocalBinds #-}
2
-- Norman likes local bindings
3
-- If this module lives on I'd like to get rid of this extension in due course
4

Edward Z. Yang's avatar
Edward Z. Yang committed
5
module CmmPipeline (
6 7 8 9
  -- | Converts C-- with an implicit stack and native C-- calls into
  -- optimized, CPS converted and native-call-less C--.  The latter
  -- C-- can be used to generate assembly.
  cmmPipeline
10
) where
11 12

import Cmm
Simon Marlow's avatar
Simon Marlow committed
13
import CmmLint
14 15
import CmmBuildInfoTables
import CmmCommonBlockElim
16
import CmmProcPoint
17
import CmmContFlowOpt
Simon Marlow's avatar
Simon Marlow committed
18
import CmmLayoutStack
19
import CmmSink
20
import Hoopl
21

22
import UniqSupply
23 24
import DynFlags
import ErrUtils
25
import HscTypes
Ian Lynagh's avatar
Ian Lynagh committed
26
import Control.Monad
27
import Outputable
28
import StaticFlags
Simon Marlow's avatar
Simon Marlow committed
29

30
-----------------------------------------------------------------------------
31
-- | Top level driver for C-- pipeline
32
-----------------------------------------------------------------------------
33

34
cmmPipeline  :: HscEnv -- Compilation env including
35
                       -- dynamic flags: -dcmm-lint -ddump-cps-cmm
36
             -> TopSRT     -- SRT table and accumulating list of compiled procs
Simon Peyton Jones's avatar
Simon Peyton Jones committed
37
             -> CmmGroup             -- Input C-- with Procedures
38
             -> IO (TopSRT, CmmGroup) -- Output CPS transformed C--
39

40
cmmPipeline hsc_env topSRT prog =
41
  do let dflags = hsc_dflags hsc_env
42

43
     showPass dflags "CPSZ"
44

45
     tops <- {-# SCC "tops" #-} mapM (cpsTop hsc_env) prog
46

47
     (topSRT, cmms) <- {-# SCC "toTops" #-} doSRTs topSRT tops
Ian Lynagh's avatar
Ian Lynagh committed
48
     dumpIfSet_dyn dflags Opt_D_dump_cps_cmm "Post CPS Cmm" (ppr cmms)
49

50
     return (topSRT, cmms)
51 52


53

54 55
cpsTop :: HscEnv -> CmmDecl -> IO (CAFEnv, [CmmDecl])
cpsTop _ p@(CmmData {}) = return (mapEmpty, [p])
56 57
cpsTop hsc_env (CmmProc h@(TopInfo {stack_info=StackInfo {arg_space=entry_off}}) l g) =
    do
58
       ----------- Control-flow optimisations ---------------
59 60
       g <- {-# SCC "cmmCfgOpts(1)" #-}
            return $ cmmCfgOpts splitting_proc_points g
61 62
       dump Opt_D_dump_cmmz_cfg "Post control-flow optimsations" g

63
       ----------- Eliminate common blocks -------------------
64 65 66 67 68 69
       g <- if dopt Opt_CmmElimCommonBlocks dflags
               then do g <- {-# SCC "elimCommonBlocks" #-} return $ elimCommonBlocks g
                       dump Opt_D_dump_cmmz_cbe "Post common block elimination" g
                       return g
               else return g

70 71
       -- Any work storing block Labels must be performed _after_
       -- elimCommonBlocks
72 73

       ----------- Proc points -------------------
74 75 76 77 78 79 80 81 82 83 84
       let call_pps = {-# SCC "callProcPoints" #-} callProcPoints g
       proc_points <-
          if splitting_proc_points
             then {-# SCC "minimalProcPointSet" #-} runUniqSM $
                  minimalProcPointSet (targetPlatform dflags) call_pps g
             else
                  return call_pps

       let noncall_pps = proc_points `setDifference` call_pps
       when (not (setNull noncall_pps)) $
         pprTrace "Non-call proc points: " (ppr noncall_pps) $ return ()
85

86 87
       ----------- Layout the stack and manifest Sp ---------------
       -- (also does: removeDeadAssignments, and lowerSafeForeignCalls)
88
       (g, stackmaps) <- {-# SCC "layoutStack" #-}
89
                         runUniqSM $ cmmLayoutStack dflags proc_points entry_off g
Simon Marlow's avatar
Simon Marlow committed
90 91
       dump Opt_D_dump_cmmz_sp "Layout Stack" g

92 93
       ----------- Sink and inline assignments -------------------
       g <- if dopt Opt_CmmSink dflags
94 95 96 97
               then do g <- {-# SCC "sink" #-} return (cmmSink g)
                       dump Opt_D_dump_cmmz_rewrite "Sink assignments" g
                       return g
               else return g
98

99
       ------------- CAF analysis ------------------------------
Simon Marlow's avatar
Simon Marlow committed
100
       let cafEnv = {-# SCC "cafAnal" #-} cafAnal g
101

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
       if splitting_proc_points
          then do
            ------------- Split into separate procedures ------------
            pp_map  <- {-# SCC "procPointAnalysis" #-} runUniqSM $
                             procPointAnalysis proc_points g
            dumpWith dflags Opt_D_dump_cmmz_procmap "procpoint map" pp_map
            gs <- {-# SCC "splitAtProcPoints" #-} runUniqSM $
                  splitAtProcPoints l call_pps proc_points pp_map (CmmProc h l g)
            dumps Opt_D_dump_cmmz_split "Post splitting" gs
     
            ------------- Populate info tables with stack info ------
            gs <- {-# SCC "setInfoTableStackMap" #-}
                  return $ map (setInfoTableStackMap stackmaps) gs
            dumps Opt_D_dump_cmmz_info "after setInfoTableStackMap" gs
     
            ----------- Control-flow optimisations ---------------
118 119
            gs <- {-# SCC "cmmCfgOpts(2)" #-}
                  return $ map (cmmCfgOptsProc splitting_proc_points) gs
120 121 122 123 124 125 126 127 128 129 130 131 132 133
            dumps Opt_D_dump_cmmz_cfg "Post control-flow optimsations" gs

            return (cafEnv, gs)

          else do
            -- attach info tables to return points
            g <- return $ attachContInfoTables call_pps (CmmProc h l g)

            ------------- Populate info tables with stack info ------
            g <- {-# SCC "setInfoTableStackMap" #-}
                  return $ setInfoTableStackMap stackmaps g
            dump' Opt_D_dump_cmmz_info "after setInfoTableStackMap" g
     
            ----------- Control-flow optimisations ---------------
134 135
            g <- {-# SCC "cmmCfgOpts(2)" #-}
                 return $ cmmCfgOptsProc splitting_proc_points g
136 137 138
            dump' Opt_D_dump_cmmz_cfg "Post control-flow optimsations" g

            return (cafEnv, [g])
139

140
  where dflags = hsc_dflags hsc_env
141
        dump = dumpGraph dflags
142
        dump' = dumpWith dflags
143

Simon Marlow's avatar
Simon Marlow committed
144
        dumps flag name
145
           = mapM_ (dumpWith dflags flag name)
Simon Marlow's avatar
Simon Marlow committed
146

147 148 149 150 151 152 153
        -- we don't need to split proc points for the NCG, unless
        -- tablesNextToCode is off.  The latter is because we have no
        -- label to put on info tables for basic blocks that are not
        -- the entry point.
        splitting_proc_points = hscTarget dflags /= HscAsm
                             || not tablesNextToCode

154 155 156 157
runUniqSM :: UniqSM a -> IO a
runUniqSM m = do
  us <- mkSplitUniqSupply 'u'
  return (initUs_ us m)
158

159

Simon Marlow's avatar
Simon Marlow committed
160 161
dumpGraph :: DynFlags -> DynFlag -> String -> CmmGraph -> IO ()
dumpGraph dflags flag name g = do
162
  when (dopt Opt_DoCmmLinting dflags) $ do_lint g
163
  dumpWith dflags flag name g
164
 where
Simon Marlow's avatar
Simon Marlow committed
165 166
  do_lint g = case cmmLintGraph g of
                 Just err -> do { fatalErrorMsg dflags err
167 168 169
                                ; ghcExit dflags 1
                                }
                 Nothing  -> return ()
Simon Marlow's avatar
Simon Marlow committed
170

171 172
dumpWith :: Outputable a => DynFlags -> DynFlag -> String -> a -> IO ()
dumpWith dflags flag txt g = do
Simon Marlow's avatar
Simon Marlow committed
173 174 175
         -- ToDo: No easy way of say "dump all the cmmz, *and* split
         -- them into files."  Also, -ddump-cmmz doesn't play nicely
         -- with -ddump-to-file, since the headers get omitted.
176
   dumpIfSet_dyn dflags flag txt (ppr g)
Simon Marlow's avatar
Simon Marlow committed
177
   when (not (dopt flag dflags)) $
178
      dumpIfSet_dyn dflags Opt_D_dump_cmmz txt (ppr g)
179