take the analyses out of the FuelUniqSM monad
[ghc.git] / compiler / cmm / CmmPipeline.hs
1 {-# OPTIONS_GHC -XNoMonoLocalBinds #-}
2 -- Norman likes local bindings
3 -- If this module lives on I'd like to get rid of this flag in due course
4
5 module CmmPipeline (
6 -- | Converts C-- with an implicit stack and native C-- calls into
7 -- optimized, CPS converted and native-call-less C--. The latter
8 -- C-- can be used to generate assembly.
9 cmmPipeline
10 ) where
11
12 import CLabel
13 import Cmm
14 import CmmLint
15 import CmmLive
16 import CmmBuildInfoTables
17 import CmmCommonBlockElim
18 import CmmProcPoint
19 import CmmRewriteAssignments
20 import CmmContFlowOpt
21 import OptimizationFuel
22 import CmmLayoutStack
23 import Hoopl
24 import CmmUtils
25
26 import DynFlags
27 import ErrUtils
28 import HscTypes
29 import Data.Maybe
30 import Control.Monad
31 import Data.Map (Map)
32 import qualified Data.Map as Map
33 import Data.Set (Set)
34 import qualified Data.Set as Set
35 import Outputable
36 import StaticFlags
37
38 -----------------------------------------------------------------------------
39 -- | Top level driver for C-- pipeline
40 -----------------------------------------------------------------------------
41 -- There are two complications here:
42 -- 1. We need to compile the procedures in two stages because we need
43 -- an analysis of the procedures to tell us what CAFs they use.
44 -- The first stage returns a map from procedure labels to CAFs,
45 -- along with a closure that will compute SRTs and attach them to
46 -- the compiled procedures.
47 -- The second stage is to combine the CAF information into a top-level
48 -- CAF environment mapping non-static closures to the CAFs they keep live,
49 -- then pass that environment to the closures returned in the first
50 -- stage of compilation.
51 -- 2. We need to thread the module's SRT around when the SRT tables
52 -- are computed for each procedure.
53 -- The SRT needs to be threaded because it is grown lazily.
54 -- 3. We run control flow optimizations twice, once before any pipeline
55 -- work is done, and once again at the very end on all of the
56 -- resulting C-- blocks. EZY: It's unclear whether or not whether
57 -- we actually need to do the initial pass.
58 cmmPipeline :: HscEnv -- Compilation env including
59 -- dynamic flags: -dcmm-lint -ddump-cps-cmm
60 -> TopSRT -- SRT table and accumulating list of compiled procs
61 -> CmmGroup -- Input C-- with Procedures
62 -> IO (TopSRT, CmmGroup) -- Output CPS transformed C--
63 cmmPipeline hsc_env topSRT prog =
64 do let dflags = hsc_dflags hsc_env
65 --
66 showPass dflags "CPSZ"
67
68 (cafEnvs, tops) <- liftM unzip $ mapM (cpsTop hsc_env) prog
69 -- tops :: [[(CmmDecl,CAFSet]] (one list per group)
70
71 let topCAFEnv = mkTopCAFInfo (concat cafEnvs)
72
73 -- folding over the groups
74 (topSRT, tops) <- foldM (toTops hsc_env topCAFEnv) (topSRT, []) tops
75
76 let cmms :: CmmGroup
77 cmms = reverse (concat tops)
78
79 dumpIfSet_dyn dflags Opt_D_dump_cps_cmm "Post CPS Cmm" (pprPlatform (targetPlatform dflags) cmms)
80
81 return (topSRT, cmms)
82
83 {- [Note global fuel]
84 ~~~~~~~~~~~~~~~~~~~~~
85 The identity and the last pass are stored in
86 mutable reference cells in an 'HscEnv' and are
87 global to one compiler session.
88 -}
89
90 -- EZY: It might be helpful to have an easy way of dumping the "pre"
91 -- input for any given phase, besides just turning it all on with
92 -- -ddump-cmmz
93
94 cpsTop :: HscEnv -> CmmDecl -> IO ([(CLabel, CAFSet)], [(CAFSet, CmmDecl)])
95 cpsTop _ p@(CmmData {}) = return ([], [(Set.empty, p)])
96 cpsTop hsc_env (CmmProc h@(TopInfo {stack_info=StackInfo {arg_space=entry_off}}) l g) =
97 do
98 ----------- Control-flow optimisations ---------------
99 g <- {-# SCC "cmmCfgOpts(1)" #-} return $ cmmCfgOpts g
100 dump Opt_D_dump_cmmz_cfg "Post control-flow optimsations" g
101
102 ----------- Eliminate common blocks -------------------
103 g <- {-# SCC "elimCommonBlocks" #-} return $ elimCommonBlocks g
104 dump Opt_D_dump_cmmz_cbe "Post common block elimination" g
105 -- Any work storing block Labels must be performed _after_
106 -- elimCommonBlocks
107
108 ----------- Proc points -------------------
109 let callPPs = {-# SCC "callProcPoints" #-} callProcPoints g
110 procPoints <- {-# SCC "minimalProcPointSet" #-} run $
111 minimalProcPointSet (targetPlatform dflags) callPPs g
112
113 ----------- Layout the stack and manifest Sp ---------------
114 -- (also does: removeDeadAssignments, and lowerSafeForeignCalls)
115 (g, stackmaps) <- {-# SCC "layoutStack" #-}
116 run $ cmmLayoutStack procPoints entry_off g
117 dump Opt_D_dump_cmmz_sp "Layout Stack" g
118
119 -- ----------- Sink and inline assignments -------------------
120 -- g <- {-# SCC "rewriteAssignments" #-} runOptimization $
121 -- rewriteAssignments platform g
122 -- dump Opt_D_dump_cmmz_rewrite "Post rewrite assignments" g
123
124 ------------- Split into separate procedures ------------
125 procPointMap <- {-# SCC "procPointAnalysis" #-} run $
126 procPointAnalysis procPoints g
127 dumpWith dflags ppr Opt_D_dump_cmmz_procmap "procpoint map" procPointMap
128 gs <- {-# SCC "splitAtProcPoints" #-} run $
129 splitAtProcPoints l callPPs procPoints procPointMap (CmmProc h l g)
130 dumps Opt_D_dump_cmmz_split "Post splitting" gs
131
132 ------------- More CAFs ------------------------------
133 let cafEnv = {-# SCC "cafAnal" #-} cafAnal platform g
134 let localCAFs = catMaybes $ map (localCAFInfo platform cafEnv) gs
135 mbpprTrace "localCAFs" (pprPlatform platform localCAFs) $ return ()
136
137 -- NO MORE GRAPH TRANSFORMATION AFTER HERE -- JUST MAKING INFOTABLES
138 gs <- {-# SCC "setInfoTableStackMap" #-}
139 return $ map (setInfoTableStackMap stackmaps) gs
140 dumps Opt_D_dump_cmmz_info "after setInfoTableStackMap" gs
141
142 ----------- Control-flow optimisations ---------------
143 gs <- {-# SCC "cmmCfgOpts(2)" #-} return $ map cmmCfgOptsProc gs
144 dumps Opt_D_dump_cmmz_cfg "Post control-flow optimsations" gs
145
146 gs <- {-# SCC "bundleCAFs" #-} return $ map (bundleCAFs cafEnv) gs
147 dumps Opt_D_dump_cmmz_cafs "after bundleCAFs" gs
148
149 return (localCAFs, gs)
150
151 -- gs :: [ (CAFSet, CmmDecl) ]
152 -- localCAFs :: [ (CLabel, CAFSet) ] -- statics filtered out(?)
153
154 where dflags = hsc_dflags hsc_env
155 platform = targetPlatform dflags
156 mbpprTrace x y z | dopt Opt_D_dump_cmmz dflags = pprTrace x y z
157 | otherwise = z
158 dump = dumpGraph dflags
159
160 dumps flag name
161 = mapM_ (dumpWith dflags (pprPlatform platform) flag name)
162
163 -- Runs a required transformation/analysis
164 run = runInfiniteFuelIO (hsc_OptFuel hsc_env)
165 -- Runs an optional transformation/analysis (and should
166 -- thus be subject to optimization fuel)
167 runOptimization = runFuelIO (hsc_OptFuel hsc_env)
168
169
170 dumpGraph :: DynFlags -> DynFlag -> String -> CmmGraph -> IO ()
171 dumpGraph dflags flag name g = do
172 when (dopt Opt_DoCmmLinting dflags) $ do_lint g
173 dumpWith dflags (pprPlatform (targetPlatform dflags)) flag name g
174 where
175 do_lint g = case cmmLintGraph (targetPlatform dflags) g of
176 Just err -> do { printDump err
177 ; ghcExit dflags 1
178 }
179 Nothing -> return ()
180
181 dumpWith :: DynFlags -> (a -> SDoc) -> DynFlag -> String -> a -> IO ()
182 dumpWith dflags pprFun flag txt g = do
183 -- ToDo: No easy way of say "dump all the cmmz, *and* split
184 -- them into files." Also, -ddump-cmmz doesn't play nicely
185 -- with -ddump-to-file, since the headers get omitted.
186 dumpIfSet_dyn dflags flag txt (pprFun g)
187 when (not (dopt flag dflags)) $
188 dumpIfSet_dyn dflags Opt_D_dump_cmmz txt (pprFun g)
189
190 -- This probably belongs in CmmBuildInfoTables?
191 -- We're just finishing the job here: once we know what CAFs are defined
192 -- in non-static closures, we can build the SRTs.
193 toTops :: HscEnv -> Map CLabel CAFSet -> (TopSRT, [[CmmDecl]])
194 -> [(CAFSet, CmmDecl)] -> IO (TopSRT, [[CmmDecl]])
195 toTops hsc_env topCAFEnv (topSRT, tops) gs =
196 do let setSRT (topSRT, rst) g =
197 do (topSRT, gs) <- setInfoTableSRT topCAFEnv topSRT g
198 return (topSRT, gs : rst)
199 (topSRT, gs') <- runFuelIO (hsc_OptFuel hsc_env) $ foldM setSRT (topSRT, []) gs
200 return (topSRT, concat gs' : tops)