X-Git-Url: http://git.megacz.com/?p=ghc-hetmet.git;a=blobdiff_plain;f=compiler%2Fcmm%2FCmmCPS.hs;h=35eabb331704e26e3a83cb8468b661105e3eeb36;hp=4ec2fc60f15935d29be70ee306d3f850871974af;hb=643397208b83f1654bceeef40c793f11592ef816;hpb=d0f6db5b0cf1189d84e292aeca350211f3b810dd diff --git a/compiler/cmm/CmmCPS.hs b/compiler/cmm/CmmCPS.hs index 4ec2fc6..35eabb3 100644 --- a/compiler/cmm/CmmCPS.hs +++ b/compiler/cmm/CmmCPS.hs @@ -1,36 +1,171 @@ -module CmmCPS (cmmCPS) where +{-# OPTIONS_GHC -XNoMonoLocalBinds #-} +-- Norman likes local bindings +-- If this module lives on I'd like to get rid of this flag in due course -#include "HsVersions.h" +module CmmCPS ( + -- | Converts C-- with full proceedures and parameters + -- to a CPS transformed C-- with the stack made manifest. + -- Well, sort of. + protoCmmCPS +) where +import CLabel import Cmm -import CmmLint -import PprCmm - -import Dataflow (mapCmmTop, onBasicBlock, cmmLivenessComment, cmmLiveness) +import CmmDecl +import CmmBuildInfoTables +import CmmCommonBlockElim +import CmmProcPoint +import CmmSpillReload +import CmmStackLayout +import OptimizationFuel import DynFlags import ErrUtils -import Maybes +import HscTypes +import Data.Maybe +import Control.Monad +import Data.Map (Map) +import qualified Data.Map as Map import Outputable +import StaticFlags + +----------------------------------------------------------------------------- +-- |Top level driver for the CPS pass +----------------------------------------------------------------------------- +-- There are two complications here: +-- 1. We need to compile the procedures in two stages because we need +-- an analysis of the procedures to tell us what CAFs they use. +-- The first stage returns a map from procedure labels to CAFs, +-- along with a closure that will compute SRTs and attach them to +-- the compiled procedures. +-- The second stage is to combine the CAF information into a top-level +-- CAF environment mapping non-static closures to the CAFs they keep live, +-- then pass that environment to the closures returned in the first +-- stage of compilation. +-- 2. We need to thread the module's SRT around when the SRT tables +-- are computed for each procedure. +-- The SRT needs to be threaded because it is grown lazily. +protoCmmCPS :: HscEnv -- Compilation env including + -- dynamic flags: -dcmm-lint -ddump-cps-cmm + -> (TopSRT, [Cmm]) -- SRT table and accumulating list of compiled procs + -> Cmm -- Input C-- with Procedures + -> IO (TopSRT, [Cmm]) -- Output CPS transformed C-- +protoCmmCPS hsc_env (topSRT, rst) (Cmm tops) = + do let dflags = hsc_dflags hsc_env + showPass dflags "CPSZ" + (cafEnvs, tops) <- liftM unzip $ mapM (cpsTop hsc_env) tops + let topCAFEnv = mkTopCAFInfo (concat cafEnvs) + (topSRT, tops) <- foldM (toTops hsc_env topCAFEnv) (topSRT, []) tops + let cmms = Cmm (reverse (concat tops)) + dumpIfSet_dyn dflags Opt_D_dump_cps_cmm "Post CPS Cmm" (ppr cmms) + return (topSRT, cmms : rst) + +{- [Note global fuel] +~~~~~~~~~~~~~~~~~~~~~ +The identity and the last pass are stored in +mutable reference cells in an 'HscEnv' and are +global to one compiler session. +-} + +-- EZY: It might be helpful to have an easy way of dumping the "pre" +-- input for any given phase, besides just turning it all on with +-- -ddump-cmmz + +cpsTop :: HscEnv -> CmmTop -> IO ([(CLabel, CAFSet)], [(CAFSet, CmmTop)]) +cpsTop _ p@(CmmData {}) = return ([], [(Map.empty, p)]) +cpsTop hsc_env (CmmProc h@(TopInfo {stack_info=StackInfo {arg_space=entry_off}}) l g) = + do + -- Why bother doing these early: dualLivenessWithInsertion, + -- insertLateReloads, rewriteAssignments? + + ----------- Eliminate common blocks ------------------- + g <- return $ elimCommonBlocks g + dump Opt_D_dump_cmmz_cbe "Post common block elimination" g + -- Any work storing block Labels must be performed _after_ elimCommonBlocks + + ----------- Proc points ------------------- + let callPPs = callProcPoints g + procPoints <- run $ minimalProcPointSet callPPs g + g <- run $ addProcPointProtocols callPPs procPoints g + dump Opt_D_dump_cmmz_proc "Post Proc Points Added" g + + ----------- Spills and reloads ------------------- + g <- run $ dualLivenessWithInsertion procPoints g + dump Opt_D_dump_cmmz_spills "Post spills and reloads" g + + ----------- Sink and inline assignments ------------------- + g <- runOptimization $ rewriteAssignments g + dump Opt_D_dump_cmmz_rewrite "Post rewrite assignments" g + + ----------- Eliminate dead assignments ------------------- + -- Remove redundant reloads (and any other redundant asst) + g <- runOptimization $ removeDeadAssignmentsAndReloads procPoints g + dump Opt_D_dump_cmmz_dead "Post Dead Assignment Elimination" g + + ----------- Zero dead stack slots (Debug only) --------------- + -- Debugging: stubbing slots on death can cause crashes early + g <- if opt_StubDeadValues + then run $ stubSlotsOnDeath g + else return g + dump Opt_D_dump_cmmz_stub "Post stub dead stack slots" g + + --------------- Stack layout ---------------- + slotEnv <- run $ liveSlotAnal g + let spEntryMap = getSpEntryMap entry_off g + mbpprTrace "live slot analysis results: " (ppr slotEnv) $ return () + let areaMap = layout procPoints spEntryMap slotEnv entry_off g + mbpprTrace "areaMap" (ppr areaMap) $ return () + + ------------ Manifest the stack pointer -------- + g <- run $ manifestSP spEntryMap areaMap entry_off g + dump Opt_D_dump_cmmz_sp "Post manifestSP" g + -- UGH... manifestSP can require updates to the procPointMap. + -- We can probably do something quicker here for the update... + + ------------- Split into separate procedures ------------ + procPointMap <- run $ procPointAnalysis procPoints g + dump Opt_D_dump_cmmz_procmap "procpoint map" procPointMap + gs <- run $ splitAtProcPoints l callPPs procPoints procPointMap + (CmmProc h l g) + mapM_ (dump Opt_D_dump_cmmz_split "Post splitting") gs + + ------------- More CAFs and foreign calls ------------ + cafEnv <- run $ cafAnal g + let localCAFs = catMaybes $ map (localCAFInfo cafEnv) gs + mbpprTrace "localCAFs" (ppr localCAFs) $ return () + + gs <- run $ mapM (lowerSafeForeignCalls areaMap) gs + mapM_ (dump Opt_D_dump_cmmz_lower "Post lowerSafeForeignCalls") gs + + -- NO MORE GRAPH TRANSFORMATION AFTER HERE -- JUST MAKING INFOTABLES + gs <- return $ map (setInfoTableStackMap slotEnv areaMap) gs + mapM_ (dump Opt_D_dump_cmmz_info "after setInfoTableStackMap") gs + gs <- return $ map (bundleCAFs cafEnv) gs + mapM_ (dump Opt_D_dump_cmmz_cafs "after bundleCAFs") gs + return (localCAFs, gs) + where dflags = hsc_dflags hsc_env + mbpprTrace x y z = if dopt Opt_D_dump_cmmz dflags then pprTrace x y z else z + dump f txt g = do + -- 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. + dumpIfSet_dyn dflags f txt (ppr g) + when (not (dopt f dflags)) $ + dumpIfSet_dyn dflags Opt_D_dump_cmmz txt (ppr g) + -- Runs a required transformation/analysis + run = runInfiniteFuelIO (hsc_OptFuel hsc_env) + -- Runs an optional transformation/analysis (and should + -- thus be subject to optimization fuel) + runOptimization = runFuelIO (hsc_OptFuel hsc_env) -import Monad -import IO - -cmmCPS :: DynFlags - -> [Cmm] -- C-- with Proceedures - -> IO [Cmm] -- Output: CPS transformed C-- - -cmmCPS dflags abstractC = do - when (dopt Opt_DoCmmLinting dflags) $ - do showPass dflags "CmmLint" - case firstJust $ map cmmLint abstractC of - Just err -> do printDump err - ghcExit dflags 1 - Nothing -> return () - showPass dflags "CPS" - -- continuationC <- return abstractC - continuationC <- return $ map (mapCmmTop (onBasicBlock (\bs -> map (cmmLivenessComment (cmmLiveness bs)) bs))) abstractC - - dumpIfSet_dyn dflags Opt_D_dump_cps_cmm "CPS Cmm" (pprCmms continuationC) - -- TODO: add option to dump Cmm to file - return continuationC +-- This probably belongs in CmmBuildInfoTables? +-- We're just finishing the job here: once we know what CAFs are defined +-- in non-static closures, we can build the SRTs. +toTops :: HscEnv -> Map CLabel CAFSet -> (TopSRT, [[CmmTop]]) + -> [(CAFSet, CmmTop)] -> IO (TopSRT, [[CmmTop]]) +toTops hsc_env topCAFEnv (topSRT, tops) gs = + do let setSRT (topSRT, rst) g = + do (topSRT, gs) <- setInfoTableSRT topCAFEnv topSRT g + return (topSRT, gs : rst) + (topSRT, gs') <- runFuelIO (hsc_OptFuel hsc_env) $ foldM setSRT (topSRT, []) gs + return (topSRT, concat gs' : tops)