1 #if __GLASGOW_HASKELL__ >= 611
2 {-# OPTIONS_GHC -XNoMonoLocalBinds #-}
4 -- Norman likes local bindings
5 -- If this module lives on I'd like to get rid of this flag in due course
8 -- | Converts C-- with full proceedures and parameters
9 -- to a CPS transformed C-- with the stack made manifest.
16 import CmmBuildInfoTables
17 import CmmCommonBlockElimZ
34 -----------------------------------------------------------------------------
35 -- |Top level driver for the CPS pass
36 -----------------------------------------------------------------------------
37 -- There are two complications here:
38 -- 1. We need to compile the procedures in two stages because we need
39 -- an analysis of the procedures to tell us what CAFs they use.
40 -- The first stage returns a map from procedure labels to CAFs,
41 -- along with a closure that will compute SRTs and attach them to
42 -- the compiled procedures.
43 -- The second stage is to combine the CAF information into a top-level
44 -- CAF environment mapping non-static closures to the CAFs they keep live,
45 -- then pass that environment to the closures returned in the first
46 -- stage of compilation.
47 -- 2. We need to thread the module's SRT around when the SRT tables
48 -- are computed for each procedure.
49 -- The SRT needs to be threaded because it is grown lazily.
50 protoCmmCPSZ :: HscEnv -- Compilation env including
51 -- dynamic flags: -dcmm-lint -ddump-cps-cmm
52 -> (TopSRT, [CmmZ]) -- SRT table and accumulating list of compiled procs
53 -> CmmZ -- Input C-- with Procedures
54 -> IO (TopSRT, [CmmZ]) -- Output CPS transformed C--
55 protoCmmCPSZ hsc_env (topSRT, rst) (Cmm tops) =
56 do let dflags = hsc_dflags hsc_env
57 showPass dflags "CPSZ"
58 (cafEnvs, tops) <- liftM unzip $ mapM (cpsTop hsc_env) tops
59 let topCAFEnv = mkTopCAFInfo (concat cafEnvs)
60 (topSRT, tops) <- foldM (toTops hsc_env topCAFEnv) (topSRT, []) tops
61 -- (topSRT, tops) <- foldM (\ z f -> f topCAFEnv z) (topSRT, []) toTops
62 let cmms = Cmm (reverse (concat tops))
63 dumpIfSet_dyn dflags Opt_D_dump_cps_cmm "Post CPS Cmm" (ppr cmms)
64 return (topSRT, cmms : rst)
68 The identity and the last pass are stored in
69 mutable reference cells in an 'HscEnv' and are
70 global to one compiler session.
73 cpsTop :: HscEnv -> CmmTopZ ->
74 IO ([(CLabel, CAFSet)],
75 [(CAFSet, CmmTopForInfoTables)])
76 cpsTop _ p@(CmmData {}) = return ([], [(emptyFM, NoInfoTable p)])
77 cpsTop hsc_env (CmmProc h l args (stackInfo@(entry_off, _), g)) =
79 dump Opt_D_dump_cmmz "Pre Proc Points Added" g
80 let callPPs = callProcPoints g
81 -- Why bother doing it this early?
82 -- g <- dual_rewrite Opt_D_dump_cmmz "spills and reloads"
83 -- (dualLivenessWithInsertion callPPs) g
84 -- g <- run $ insertLateReloads g -- Duplicate reloads just before uses
85 -- g <- dual_rewrite Opt_D_dump_cmmz "Dead Assignment Elimination"
86 -- (removeDeadAssignmentsAndReloads callPPs) g
87 dump Opt_D_dump_cmmz "Pre common block elimination" g
88 g <- return $ elimCommonBlocks g
89 dump Opt_D_dump_cmmz "Post common block elimination" g
91 ----------- Proc points -------------------
92 procPoints <- run $ minimalProcPointSet callPPs g
93 g <- run $ addProcPointProtocols callPPs procPoints g
94 dump Opt_D_dump_cmmz "Post Proc Points Added" g
96 ----------- Spills and reloads -------------------
98 -- pprTrace "pre Spills" (ppr g) $
99 dual_rewrite Opt_D_dump_cmmz "spills and reloads"
100 (dualLivenessWithInsertion procPoints) g
101 -- Insert spills at defns; reloads at return points
103 -- pprTrace "pre insertLateReloads" (ppr g) $
104 run $ insertLateReloads g -- Duplicate reloads just before uses
105 dump Opt_D_dump_cmmz "Post late reloads" g
107 -- pprTrace "post insertLateReloads" (ppr g) $
108 dual_rewrite Opt_D_dump_cmmz "Dead Assignment Elimination"
109 (removeDeadAssignmentsAndReloads procPoints) g
110 -- Remove redundant reloads (and any other redundant asst)
112 ----------- Debug only: add code to put zero in dead stack slots----
113 -- Debugging: stubbing slots on death can cause crashes early
115 -- trace "post dead-assign elim" $
116 if opt_StubDeadValues then run $ stubSlotsOnDeath g else return g
119 --------------- Stack layout ----------------
120 slotEnv <- run $ liveSlotAnal g
121 mbpprTrace "live slot analysis results: " (ppr slotEnv) $ return ()
122 -- cafEnv <- -- trace "post liveSlotAnal" $ run $ cafAnal g
123 -- (cafEnv, slotEnv) <-
124 -- -- trace "post print cafAnal" $
125 -- return $ extendEnvsForSafeForeignCalls cafEnv slotEnv g
126 slotEnv <- return $ extendEnvWithSafeForeignCalls liveSlotTransfers slotEnv g
127 mbpprTrace "slotEnv extended for safe foreign calls: " (ppr slotEnv) $ return ()
128 let areaMap = layout procPoints slotEnv entry_off g
129 mbpprTrace "areaMap" (ppr areaMap) $ return ()
131 ------------ Manifest the the stack pointer --------
132 g <- run $ manifestSP areaMap entry_off g
133 dump Opt_D_dump_cmmz "after manifestSP" g
134 -- UGH... manifestSP can require updates to the procPointMap.
135 -- We can probably do something quicker here for the update...
137 ------------- Split into separate procedures ------------
138 procPointMap <- run $ procPointAnalysis procPoints g
139 dump Opt_D_dump_cmmz "procpoint map" procPointMap
140 gs <- run $ splitAtProcPoints l callPPs procPoints procPointMap
141 (CmmProc h l args (stackInfo, g))
142 mapM_ (dump Opt_D_dump_cmmz "after splitting") gs
144 ------------- More CAFs and foreign calls ------------
145 cafEnv <- run $ cafAnal g
146 cafEnv <- return $ extendEnvWithSafeForeignCalls cafTransfers cafEnv g
147 let localCAFs = catMaybes $ map (localCAFInfo cafEnv) gs
148 mbpprTrace "localCAFs" (ppr localCAFs) $ return ()
150 gs <- liftM concat $ run $ foldM lowerSafeForeignCalls [] gs
151 mapM_ (dump Opt_D_dump_cmmz "after lowerSafeForeignCalls") gs
153 -- NO MORE GRAPH TRANSFORMATION AFTER HERE -- JUST MAKING INFOTABLES
154 let gs' = map (setInfoTableStackMap slotEnv areaMap) gs
155 mapM_ (dump Opt_D_dump_cmmz "after setInfoTableStackMap") gs'
156 let gs'' = map (bundleCAFs cafEnv) gs'
157 mapM_ (dump Opt_D_dump_cmmz "after bundleCAFs") gs''
158 return (localCAFs, gs'')
159 where dflags = hsc_dflags hsc_env
160 mbpprTrace x y z = if dopt Opt_D_dump_cmmz dflags then pprTrace x y z else z
161 dump f txt g = dumpIfSet_dyn dflags f txt (ppr g)
163 run :: FuelMonad a -> IO a
164 run = runFuelIO (hsc_OptFuel hsc_env)
166 dual_rewrite flag txt pass g =
167 do dump flag ("Pre " ++ txt) g
169 dump flag ("Post " ++ txt) $ g
172 -- This probably belongs in CmmBuildInfoTables?
173 -- We're just finishing the job here: once we know what CAFs are defined
174 -- in non-static closures, we can build the SRTs.
175 toTops :: HscEnv -> FiniteMap CLabel CAFSet -> (TopSRT, [[CmmTopZ]])
176 -> [(CAFSet, CmmTopForInfoTables)] -> IO (TopSRT, [[CmmTopZ]])
178 toTops hsc_env topCAFEnv (topSRT, tops) gs =
179 do let setSRT (topSRT, rst) g =
180 do (topSRT, gs) <- setInfoTableSRT topCAFEnv topSRT g
181 return (topSRT, gs : rst)
182 (topSRT, gs') <- runFuelIO (hsc_OptFuel hsc_env) $ foldM setSRT (topSRT, []) gs
183 gs' <- mapM finishInfoTables (concat gs')
184 return (topSRT, concat gs' : tops)