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
6 -- | Converts C-- with full proceedures and parameters
7 -- to a CPS transformed C-- with the stack made manifest.
15 import CmmBuildInfoTables
16 import CmmCommonBlockElim
20 import OptimizationFuel
28 import qualified Data.Map as Map
32 -----------------------------------------------------------------------------
33 -- |Top level driver for the CPS pass
34 -----------------------------------------------------------------------------
35 -- There are two complications here:
36 -- 1. We need to compile the procedures in two stages because we need
37 -- an analysis of the procedures to tell us what CAFs they use.
38 -- The first stage returns a map from procedure labels to CAFs,
39 -- along with a closure that will compute SRTs and attach them to
40 -- the compiled procedures.
41 -- The second stage is to combine the CAF information into a top-level
42 -- CAF environment mapping non-static closures to the CAFs they keep live,
43 -- then pass that environment to the closures returned in the first
44 -- stage of compilation.
45 -- 2. We need to thread the module's SRT around when the SRT tables
46 -- are computed for each procedure.
47 -- The SRT needs to be threaded because it is grown lazily.
48 protoCmmCPS :: HscEnv -- Compilation env including
49 -- dynamic flags: -dcmm-lint -ddump-cps-cmm
50 -> (TopSRT, [Cmm]) -- SRT table and accumulating list of compiled procs
51 -> Cmm -- Input C-- with Procedures
52 -> IO (TopSRT, [Cmm]) -- Output CPS transformed C--
53 protoCmmCPS hsc_env (topSRT, rst) (Cmm tops) =
54 do let dflags = hsc_dflags hsc_env
55 showPass dflags "CPSZ"
56 (cafEnvs, tops) <- liftM unzip $ mapM (cpsTop hsc_env) tops
57 let topCAFEnv = mkTopCAFInfo (concat cafEnvs)
58 (topSRT, tops) <- foldM (toTops hsc_env topCAFEnv) (topSRT, []) tops
59 let cmms = Cmm (reverse (concat tops))
60 dumpIfSet_dyn dflags Opt_D_dump_cps_cmm "Post CPS Cmm" (ppr cmms)
61 return (topSRT, cmms : rst)
65 The identity and the last pass are stored in
66 mutable reference cells in an 'HscEnv' and are
67 global to one compiler session.
70 -- EZY: It might be helpful to have an easy way of dumping the "pre"
71 -- input for any given phase, besides just turning it all on with
74 cpsTop :: HscEnv -> CmmTop -> IO ([(CLabel, CAFSet)], [(CAFSet, CmmTop)])
75 cpsTop _ p@(CmmData {}) = return ([], [(Map.empty, p)])
76 cpsTop hsc_env (CmmProc h@(TopInfo {stack_info=StackInfo {arg_space=entry_off}}) l g) =
78 -- Why bother doing these early: dualLivenessWithInsertion,
79 -- insertLateReloads, rewriteAssignments?
81 ----------- Eliminate common blocks -------------------
82 g <- return $ elimCommonBlocks g
83 dump Opt_D_dump_cmmz_cbe "Post common block elimination" g
84 -- Any work storing block Labels must be performed _after_ elimCommonBlocks
86 ----------- Proc points -------------------
87 let callPPs = callProcPoints g
88 procPoints <- run $ minimalProcPointSet callPPs g
89 g <- run $ addProcPointProtocols callPPs procPoints g
90 dump Opt_D_dump_cmmz_proc "Post Proc Points Added" g
92 ----------- Spills and reloads -------------------
93 g <- run $ dualLivenessWithInsertion procPoints g
94 dump Opt_D_dump_cmmz_spills "Post spills and reloads" g
96 ----------- Sink and inline assignments -------------------
97 g <- runOptimization $ rewriteAssignments g
98 dump Opt_D_dump_cmmz_rewrite "Post rewrite assignments" g
100 ----------- Eliminate dead assignments -------------------
101 -- Remove redundant reloads (and any other redundant asst)
102 g <- runOptimization $ removeDeadAssignmentsAndReloads procPoints g
103 dump Opt_D_dump_cmmz_dead "Post Dead Assignment Elimination" g
105 ----------- Zero dead stack slots (Debug only) ---------------
106 -- Debugging: stubbing slots on death can cause crashes early
107 g <- if opt_StubDeadValues
108 then run $ stubSlotsOnDeath g
110 dump Opt_D_dump_cmmz_stub "Post stub dead stack slots" g
112 --------------- Stack layout ----------------
113 slotEnv <- run $ liveSlotAnal g
114 let spEntryMap = getSpEntryMap entry_off g
115 mbpprTrace "live slot analysis results: " (ppr slotEnv) $ return ()
116 let areaMap = layout procPoints spEntryMap slotEnv entry_off g
117 mbpprTrace "areaMap" (ppr areaMap) $ return ()
119 ------------ Manifest the stack pointer --------
120 g <- run $ manifestSP spEntryMap areaMap entry_off g
121 dump Opt_D_dump_cmmz_sp "Post manifestSP" g
122 -- UGH... manifestSP can require updates to the procPointMap.
123 -- We can probably do something quicker here for the update...
125 ------------- Split into separate procedures ------------
126 procPointMap <- run $ procPointAnalysis procPoints g
127 dump Opt_D_dump_cmmz_procmap "procpoint map" procPointMap
128 gs <- run $ splitAtProcPoints l callPPs procPoints procPointMap
130 mapM_ (dump Opt_D_dump_cmmz_split "Post splitting") gs
132 ------------- More CAFs and foreign calls ------------
133 cafEnv <- run $ cafAnal g
134 let localCAFs = catMaybes $ map (localCAFInfo cafEnv) gs
135 mbpprTrace "localCAFs" (ppr localCAFs) $ return ()
137 gs <- run $ mapM (lowerSafeForeignCalls areaMap) gs
138 mapM_ (dump Opt_D_dump_cmmz_lower "Post lowerSafeForeignCalls") gs
140 -- NO MORE GRAPH TRANSFORMATION AFTER HERE -- JUST MAKING INFOTABLES
141 gs <- return $ map (setInfoTableStackMap slotEnv areaMap) gs
142 mapM_ (dump Opt_D_dump_cmmz_info "after setInfoTableStackMap") gs
143 gs <- return $ map (bundleCAFs cafEnv) gs
144 mapM_ (dump Opt_D_dump_cmmz_cafs "after bundleCAFs") gs
145 return (localCAFs, gs)
146 where dflags = hsc_dflags hsc_env
147 mbpprTrace x y z = if dopt Opt_D_dump_cmmz dflags then pprTrace x y z else z
149 -- ToDo: No easy way of say "dump all the cmmz, *and* split
150 -- them into files." Also, -ddump-cmmz doesn't play nicely
151 -- with -ddump-to-file, since the headers get omitted.
152 dumpIfSet_dyn dflags f txt (ppr g)
153 when (not (dopt f dflags)) $
154 dumpIfSet_dyn dflags Opt_D_dump_cmmz txt (ppr g)
155 -- Runs a required transformation/analysis
156 run = runInfiniteFuelIO (hsc_OptFuel hsc_env)
157 -- Runs an optional transformation/analysis (and should
158 -- thus be subject to optimization fuel)
159 runOptimization = runFuelIO (hsc_OptFuel hsc_env)
161 -- This probably belongs in CmmBuildInfoTables?
162 -- We're just finishing the job here: once we know what CAFs are defined
163 -- in non-static closures, we can build the SRTs.
164 toTops :: HscEnv -> Map CLabel CAFSet -> (TopSRT, [[CmmTop]])
165 -> [(CAFSet, CmmTop)] -> IO (TopSRT, [[CmmTop]])
166 toTops hsc_env topCAFEnv (topSRT, tops) gs =
167 do let setSRT (topSRT, rst) g =
168 do (topSRT, gs) <- setInfoTableSRT topCAFEnv topSRT g
169 return (topSRT, gs : rst)
170 (topSRT, gs') <- runFuelIO (hsc_OptFuel hsc_env) $ foldM setSRT (topSRT, []) gs
171 return (topSRT, concat gs' : tops)