CmmPipeline.hs 6.5 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 ---------------
Simon Marlow's avatar
Simon Marlow committed
59
       g <- {-# SCC "cmmCfgOpts(1)" #-} return $ cmmCfgOpts g
60 61
       dump Opt_D_dump_cmmz_cfg "Post control-flow optimsations" g

62
       ----------- Eliminate common blocks -------------------
Simon Marlow's avatar
Simon Marlow committed
63
       g <- {-# SCC "elimCommonBlocks" #-} return $ elimCommonBlocks g
64 65 66
       dump Opt_D_dump_cmmz_cbe "Post common block elimination" g
       -- Any work storing block Labels must be performed _after_
       -- elimCommonBlocks
67 68

       ----------- Proc points -------------------
69 70 71 72 73 74 75 76 77 78 79
       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 ()
80

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

87 88
       ----------- Sink and inline assignments -------------------
       g <- if dopt Opt_CmmSink dflags
89 90 91 92
               then do g <- {-# SCC "sink" #-} return (cmmSink g)
                       dump Opt_D_dump_cmmz_rewrite "Sink assignments" g
                       return g
               else return g
93

94
       ------------- CAF analysis ------------------------------
Simon Marlow's avatar
Simon Marlow committed
95
       let cafEnv = {-# SCC "cafAnal" #-} cafAnal g
96

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
       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 ---------------
            gs <- {-# SCC "cmmCfgOpts(2)" #-} return $ map cmmCfgOptsProc gs
            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 ---------------
            g <- {-# SCC "cmmCfgOpts(2)" #-} return $ cmmCfgOptsProc g
            dump' Opt_D_dump_cmmz_cfg "Post control-flow optimsations" g

            return (cafEnv, [g])
132

133
  where dflags = hsc_dflags hsc_env
134
        dump = dumpGraph dflags
135
        dump' = dumpWith dflags
136

Simon Marlow's avatar
Simon Marlow committed
137
        dumps flag name
138
           = mapM_ (dumpWith dflags flag name)
Simon Marlow's avatar
Simon Marlow committed
139

140 141 142 143 144 145 146
        -- 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

147 148 149 150
runUniqSM :: UniqSM a -> IO a
runUniqSM m = do
  us <- mkSplitUniqSupply 'u'
  return (initUs_ us m)
151

152

Simon Marlow's avatar
Simon Marlow committed
153 154
dumpGraph :: DynFlags -> DynFlag -> String -> CmmGraph -> IO ()
dumpGraph dflags flag name g = do
155
  when (dopt Opt_DoCmmLinting dflags) $ do_lint g
156
  dumpWith dflags flag name g
157
 where
Simon Marlow's avatar
Simon Marlow committed
158 159
  do_lint g = case cmmLintGraph g of
                 Just err -> do { fatalErrorMsg dflags err
160 161 162
                                ; ghcExit dflags 1
                                }
                 Nothing  -> return ()
Simon Marlow's avatar
Simon Marlow committed
163

164 165
dumpWith :: Outputable a => DynFlags -> DynFlag -> String -> a -> IO ()
dumpWith dflags flag txt g = do
Simon Marlow's avatar
Simon Marlow committed
166 167 168
         -- 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.
169
   dumpIfSet_dyn dflags flag txt (ppr g)
Simon Marlow's avatar
Simon Marlow committed
170
   when (not (dopt flag dflags)) $
171
      dumpIfSet_dyn dflags Opt_D_dump_cmmz txt (ppr g)
172