2 % (c) The AQUA Project, Glasgow University, 1996
4 \section[CmdLineOpts]{Things to do with command-line options}
7 #include "HsVersions.h"
19 maybe_CompilingGhcInternals,
22 opt_AutoSccsOnAllToplevs,
23 opt_AutoSccsOnExportedToplevs,
24 opt_AutoSccsOnIndividualCafs,
25 opt_CompilingGhcInternals,
32 opt_D_dump_occur_anal,
43 opt_D_simplifier_stats,
45 opt_D_verbose_core2core,
46 opt_D_verbose_stg2stg,
50 opt_EnsureSplittableC,
58 opt_IgnoreIfacePragmas,
59 opt_IgnoreStrictnessPragmas,
60 opt_IrrefutableEverything,
61 opt_IrrefutableTuples,
62 opt_LiberateCaseThreshold,
65 opt_OmitDefaultInstanceMethods,
66 opt_OmitInterfacePragmas,
73 opt_ReportWhyUnfoldingsDisallowed,
74 opt_ReturnInRegsThreshold,
78 opt_ShowPragmaNameErrs,
82 opt_SpecialiseImports,
83 opt_SpecialiseOverloaded,
85 opt_SpecialiseUnboxed,
86 opt_StgDoLetNoEscapes,
88 opt_InterfaceUnfoldThreshold,
89 opt_UnfoldingCreationThreshold,
90 opt_UnfoldingConDiscount,
91 opt_UnfoldingUseThreshold,
97 IMPORT_1_3(Array(array, (//)))
98 import PreludeGlaST -- bad bad bad boy, Will (_Array internals)
101 CHK_Ubiq() -- debugging consistency check
103 import Constants -- Default values for some flags
105 import Maybes ( assocMaybe, firstJust, maybeToBool )
106 import Util ( startsWith, panic, panic#, assertPanic )
109 A command-line {\em switch} is (generally) either on or off; e.g., the
110 ``verbose'' (-v) switch is either on or off. (The \tr{-G<group>}
111 switch is an exception; it's set to a string, or nothing.)
113 A list of {\em ToDo}s is things to be done in a particular part of
114 processing. A (fictitious) example for the Core-to-Core simplifier
115 might be: run the simplifier, then run the strictness analyser, then
116 run the simplifier again (three ``todos'').
118 There are three ``to-do processing centers'' at the moment. In the
119 main loop (\tr{main/Main.lhs}), in the Core-to-Core processing loop
120 (\tr{simplCore/SimplCore.lhs), and in the STG-to-STG processing loop
121 (\tr{simplStg/SimplStg.lhs}).
123 %************************************************************************
125 \subsection{Datatypes associated with command-line options}
127 %************************************************************************
131 = SwBool Bool -- on/off
132 | SwString FAST_STRING -- nothing or a String
133 | SwInt Int -- nothing or an Int
137 data CoreToDo -- These are diff core-to-core passes,
138 -- which may be invoked in any order,
139 -- as many times as you like.
141 = CoreDoSimplify -- The core-to-core simplifier.
142 (SimplifierSwitch -> SwitchResult)
143 -- Each run of the simplifier can take a different
144 -- set of simplifier-specific flags.
145 | CoreDoCalcInlinings1
146 | CoreDoCalcInlinings2
155 | CoreDoFoldrBuildWorkerWrapper
156 | CoreDoFoldrBuildWWAnal
162 | StgDoUpdateAnalysis
164 | StgDoMassageForProfiling -- should be (next to) last
165 -- There's also setStgVarInfo, but its absolute "lastness"
166 -- is so critical that it is hardwired in (no flag).
171 data SimplifierSwitch
173 | SimplFloatLetsExposingWHNF
174 | SimplOkToFloatPrimOps
175 | SimplAlwaysFloatLetsFromLets
180 | SimplMayDeleteConjurableIds
181 | SimplPedanticBottoms -- see Simplifier for an explanation
182 | SimplDoArityExpand -- expand arity of bindings
183 | SimplDoFoldrBuild -- This is the per-simplification flag;
184 -- see also FoldrBuildOn, used elsewhere
186 | SimplDoInlineFoldrBuild
187 -- inline foldr/build (*after* f/b rule is used)
190 | SimplDoLambdaEtaExpansion
191 | SimplDoEtaReduction
193 | EssentialUnfoldingsOnly -- never mind the thresholds, only
194 -- do unfoldings that *must* be done
195 -- (to saturate constructors and primitives)
197 | ShowSimplifierProgress -- report counts on every interation
199 | MaxSimplifierIterations Int
201 | KeepSpecPragmaIds -- We normally *toss* Ids we can do without
204 | SimplNoLetFromCase -- used when turning off floating entirely
205 | SimplNoLetFromApp -- (for experimentation only) WDP 95/10
206 | SimplNoLetFromStrictLet
208 | SimplDontFoldBackAppend
209 -- we fold `foldr (:)' back into flip (++),
210 -- but we *don't* want to do it when compiling
211 -- List.hs, otherwise
212 -- xs ++ ys = foldr (:) ys xs
213 -- {- via our loopback -}
214 -- xs ++ ys = xs ++ ys
216 -- So only use this flag inside List.hs
217 -- (Sigh, what a HACK, Andy. WDP 96/01)
222 %************************************************************************
224 \subsection{Classifying command-line options}
226 %************************************************************************
229 lookUp :: FAST_STRING -> Bool
230 lookup_int :: String -> Maybe Int
231 lookup_def_int :: String -> Int -> Int
232 lookup_str :: String -> Maybe String
234 lookUp sw = maybeToBool (assoc_opts sw)
236 lookup_str sw = firstJust (map (startsWith sw) unpacked_opts)
238 lookup_int sw = case (lookup_str sw) of
240 Just xx -> Just (read xx)
242 lookup_def_int sw def = case (lookup_str sw) of
243 Nothing -> def -- Use default
246 assoc_opts = assocMaybe [ (a, True) | a <- argv ]
247 unpacked_opts = map _UNPK_ argv
251 opt_AllDemanded = lookUp SLIT("-fall-demanded")
252 opt_AllStrict = lookUp SLIT("-fall-strict")
253 opt_AutoSccsOnAllToplevs = lookUp SLIT("-fauto-sccs-on-all-toplevs")
254 opt_AutoSccsOnExportedToplevs = lookUp SLIT("-fauto-sccs-on-exported-toplevs")
255 opt_AutoSccsOnIndividualCafs = lookUp SLIT("-fauto-sccs-on-individual-cafs")
256 opt_CompilingGhcInternals = maybeToBool maybe_CompilingGhcInternals
257 maybe_CompilingGhcInternals = lookup_str "-fcompiling-ghc-internals="
258 opt_D_dump_absC = lookUp SLIT("-ddump-absC")
259 opt_D_dump_asm = lookUp SLIT("-ddump-asm")
260 opt_D_dump_deforest = lookUp SLIT("-ddump-deforest")
261 opt_D_dump_deriv = lookUp SLIT("-ddump-deriv")
262 opt_D_dump_ds = lookUp SLIT("-ddump-ds")
263 opt_D_dump_flatC = lookUp SLIT("-ddump-flatC")
264 opt_D_dump_occur_anal = lookUp SLIT("-ddump-occur-anal")
265 opt_D_dump_rdr = lookUp SLIT("-ddump-rdr")
266 opt_D_dump_realC = lookUp SLIT("-ddump-realC")
267 opt_D_dump_rn = lookUp SLIT("-ddump-rn")
268 opt_D_dump_simpl = lookUp SLIT("-ddump-simpl")
269 opt_D_dump_spec = lookUp SLIT("-ddump-spec")
270 opt_D_dump_stg = lookUp SLIT("-ddump-stg")
271 opt_D_dump_stranal = lookUp SLIT("-ddump-stranal")
272 opt_D_dump_tc = lookUp SLIT("-ddump-tc")
273 opt_D_show_passes = lookUp SLIT("-dshow-passes")
274 opt_D_show_rn_trace = lookUp SLIT("-dshow-rn-trace")
275 opt_D_simplifier_stats = lookUp SLIT("-dsimplifier-stats")
276 opt_D_source_stats = lookUp SLIT("-dsource-stats")
277 opt_D_verbose_core2core = lookUp SLIT("-dverbose-simpl")
278 opt_D_verbose_stg2stg = lookUp SLIT("-dverbose-stg")
279 opt_DoCoreLinting = lookUp SLIT("-dcore-lint")
280 opt_DoSemiTagging = lookUp SLIT("-fsemi-tagging")
281 opt_DoTickyProfiling = lookUp SLIT("-fticky-ticky")
282 opt_EnsureSplittableC = lookUp SLIT("-fglobalise-toplev-names")
283 opt_FoldrBuildOn = lookUp SLIT("-ffoldr-build-on")
284 opt_FoldrBuildTrace = lookUp SLIT("-ffoldr-build-trace")
285 opt_ForConcurrent = lookUp SLIT("-fconcurrent")
286 opt_GranMacros = lookUp SLIT("-fgransim")
287 opt_GlasgowExts = lookUp SLIT("-fglasgow-exts")
288 opt_Haskell_1_3 = lookUp SLIT("-fhaskell-1.3")
289 opt_HiMap = lookup_str "-himap=" -- file saying where to look for .hi files
290 opt_IgnoreIfacePragmas = lookUp SLIT("-fignore-interface-pragmas")
291 opt_IgnoreStrictnessPragmas = lookUp SLIT("-fignore-strictness-pragmas")
292 opt_IrrefutableEverything = lookUp SLIT("-firrefutable-everything")
293 opt_IrrefutableTuples = lookUp SLIT("-firrefutable-tuples")
294 opt_NumbersStrict = lookUp SLIT("-fnumbers-strict")
295 opt_OmitBlackHoling = lookUp SLIT("-dno-black-holing")
296 opt_OmitDefaultInstanceMethods = lookUp SLIT("-fomit-default-instance-methods")
297 opt_OmitInterfacePragmas = lookUp SLIT("-fomit-interface-pragmas")
298 opt_PprStyle_All = lookUp SLIT("-dppr-all")
299 opt_PprStyle_Debug = lookUp SLIT("-dppr-debug")
300 opt_PprStyle_User = lookUp SLIT("-dppr-user")
301 opt_ProduceC = lookup_str "-C="
302 opt_ProduceS = lookup_str "-S="
303 opt_ProduceHi = lookup_str "-hifile=" -- the one to produce this time
304 opt_ReportWhyUnfoldingsDisallowed= lookUp SLIT("-freport-disallowed-unfoldings")
305 opt_SccProfilingOn = lookUp SLIT("-fscc-profiling")
306 opt_ShowImportSpecs = lookUp SLIT("-fshow-import-specs")
307 opt_ShowPragmaNameErrs = lookUp SLIT("-fshow-pragma-name-errs")
308 opt_SigsRequired = lookUp SLIT("-fsignatures-required")
309 opt_SourceUnchanged = lookUp SLIT("-fsource-unchanged")
310 opt_SpecialiseAll = lookUp SLIT("-fspecialise-all")
311 opt_SpecialiseImports = lookUp SLIT("-fspecialise-imports")
312 opt_SpecialiseOverloaded = lookUp SLIT("-fspecialise-overloaded")
313 opt_SpecialiseTrace = lookUp SLIT("-ftrace-specialisation")
314 opt_SpecialiseUnboxed = lookUp SLIT("-fspecialise-unboxed")
315 opt_StgDoLetNoEscapes = lookUp SLIT("-flet-no-escape")
316 opt_ReturnInRegsThreshold = lookup_int "-freturn-in-regs-threshold"
317 opt_SccGroup = lookup_str "-G="
318 opt_Verbose = lookUp SLIT("-v")
320 opt_InterfaceUnfoldThreshold = lookup_def_int "-funfolding-interface-threshold" iNTERFACE_UNFOLD_THRESHOLD
321 opt_UnfoldingCreationThreshold = lookup_def_int "-funfolding-creation-threshold" uNFOLDING_CREATION_THRESHOLD
322 opt_UnfoldingUseThreshold = lookup_def_int "-funfolding-use-threshold" uNFOLDING_USE_THRESHOLD
323 opt_UnfoldingConDiscount = lookup_def_int "-funfolding-con-discount" uNFOLDING_CON_DISCOUNT_WEIGHT
325 opt_LiberateCaseThreshold = lookup_def_int "-fliberate-case-threshold" lIBERATE_CASE_THRESHOLD
326 opt_WarnNameShadowing = lookUp SLIT("-fwarn-name-shadowing")
328 -- opt_UnfoldingUseThreshold = lookup_int "-funfolding-use-threshold"
329 -- opt_UnfoldingOverrideThreshold = lookup_int "-funfolding-override-threshold"
333 classifyOpts :: ([CoreToDo], -- Core-to-Core processing spec
334 [StgToDo]) -- STG-to-STG processing spec
336 classifyOpts = sep argv [] [] -- accumulators...
338 sep :: [FAST_STRING] -- cmd-line opts (input)
339 -> [CoreToDo] -> [StgToDo] -- to_do accumulators
340 -> ([CoreToDo], [StgToDo]) -- result
342 sep [] core_td stg_td -- all done!
343 = (reverse core_td, reverse stg_td)
345 # define CORE_TD(to_do) sep opts (to_do:core_td) stg_td
346 # define STG_TD(to_do) sep opts core_td (to_do:stg_td)
347 # define IGNORE_ARG() sep opts core_td stg_td
349 sep (opt1:opts) core_td stg_td
351 case (_UNPK_ opt1) of -- the non-"just match a string" options are at the end...
353 ',' : _ -> IGNORE_ARG() -- it is for the parser
355 "-fsimplify" -> -- gather up SimplifierSwitches specially...
356 simpl_sep opts defaultSimplSwitches core_td stg_td
358 "-fcalc-inlinings1"-> CORE_TD(CoreDoCalcInlinings1)
359 "-fcalc-inlinings2"-> CORE_TD(CoreDoCalcInlinings2)
360 "-ffloat-inwards" -> CORE_TD(CoreDoFloatInwards)
361 "-ffull-laziness" -> CORE_TD(CoreDoFullLaziness)
362 "-fliberate-case" -> CORE_TD(CoreLiberateCase)
363 "-fprint-core" -> CORE_TD(CoreDoPrintCore)
364 "-fstatic-args" -> CORE_TD(CoreDoStaticArgs)
365 "-fstrictness" -> CORE_TD(CoreDoStrictness)
366 "-fspecialise" -> CORE_TD(CoreDoSpecialising)
367 "-fdeforest" -> CORE_TD(CoreDoDeforest)
368 "-ffoldr-build-worker-wrapper" -> CORE_TD(CoreDoFoldrBuildWorkerWrapper)
369 "-ffoldr-build-ww-anal" -> CORE_TD(CoreDoFoldrBuildWWAnal)
371 "-fstg-static-args" -> STG_TD(StgDoStaticArgs)
372 "-fupdate-analysis" -> STG_TD(StgDoUpdateAnalysis)
373 "-dstg-stats" -> STG_TD(D_stg_stats)
374 "-flambda-lift" -> STG_TD(StgDoLambdaLift)
375 "-fmassage-stg-for-profiling" -> STG_TD(StgDoMassageForProfiling)
377 _ -> -- NB: the driver is really supposed to handle bad options
382 simpl_sep :: [FAST_STRING] -- cmd-line opts (input)
383 -> [SimplifierSwitch] -- simplifier-switch accumulator
384 -> [CoreToDo] -> [StgToDo] -- to_do accumulators
385 -> ([CoreToDo], [StgToDo]) -- result
387 -- "simpl_sep" tailcalls "sep" once it's seen one set
388 -- of SimplifierSwitches for a CoreDoSimplify.
391 simpl_sep input@[] simpl_sw core_td stg_td
392 = panic "simpl_sep []"
395 -- The SimplifierSwitches should be delimited by "(" and ")".
397 simpl_sep (opt1:opts) simpl_sw core_td stg_td
398 = case (_UNPK_ opt1) of
399 "(" -> simpl_sep opts simpl_sw core_td stg_td
401 this_simpl = CoreDoSimplify (isAmongSimpl simpl_sw)
403 sep opts (this_simpl : core_td) stg_td
405 # define SIMPL_SW(sw) simpl_sep opts (sw:simpl_sw) core_td stg_td
407 -- the non-"just match a string" options are at the end...
408 "-fshow-simplifier-progress" -> SIMPL_SW(ShowSimplifierProgress)
409 "-fcode-duplication-ok" -> SIMPL_SW(SimplOkToDupCode)
410 "-ffloat-lets-exposing-whnf" -> SIMPL_SW(SimplFloatLetsExposingWHNF)
411 "-ffloat-primops-ok" -> SIMPL_SW(SimplOkToFloatPrimOps)
412 "-falways-float-lets-from-lets" -> SIMPL_SW(SimplAlwaysFloatLetsFromLets)
413 "-fdo-case-elim" -> SIMPL_SW(SimplDoCaseElim)
414 "-fdo-eta-reduction" -> SIMPL_SW(SimplDoEtaReduction)
415 "-fdo-lambda-eta-expansion" -> SIMPL_SW(SimplDoLambdaEtaExpansion)
416 "-fdo-foldr-build" -> SIMPL_SW(SimplDoFoldrBuild)
417 "-fdo-not-fold-back-append" -> SIMPL_SW(SimplDontFoldBackAppend)
418 "-fdo-arity-expand" -> SIMPL_SW(SimplDoArityExpand)
419 "-fdo-inline-foldr-build" -> SIMPL_SW(SimplDoInlineFoldrBuild)
420 "-freuse-con" -> SIMPL_SW(SimplReuseCon)
421 "-fcase-of-case" -> SIMPL_SW(SimplCaseOfCase)
422 "-fcase-merge" -> SIMPL_SW(SimplCaseMerge)
423 "-flet-to-case" -> SIMPL_SW(SimplLetToCase)
424 "-fpedantic-bottoms" -> SIMPL_SW(SimplPedanticBottoms)
425 "-fkeep-spec-pragma-ids" -> SIMPL_SW(KeepSpecPragmaIds)
426 "-fkeep-unused-bindings" -> SIMPL_SW(KeepUnusedBindings)
427 "-fmay-delete-conjurable-ids" -> SIMPL_SW(SimplMayDeleteConjurableIds)
428 "-fessential-unfoldings-only" -> SIMPL_SW(EssentialUnfoldingsOnly)
429 "-fignore-inline-pragma" -> SIMPL_SW(IgnoreINLINEPragma)
430 "-fno-let-from-case" -> SIMPL_SW(SimplNoLetFromCase)
431 "-fno-let-from-app" -> SIMPL_SW(SimplNoLetFromApp)
432 "-fno-let-from-strict-let" -> SIMPL_SW(SimplNoLetFromStrictLet)
434 o | starts_with_msi -> SIMPL_SW(MaxSimplifierIterations (read after_msi))
436 maybe_msi = startsWith "-fmax-simplifier-iterations" o
437 starts_with_msi = maybeToBool maybe_msi
438 (Just after_msi) = maybe_msi
440 _ -> -- NB: the driver is really supposed to handle bad options
441 simpl_sep opts simpl_sw core_td stg_td
444 %************************************************************************
446 \subsection{Switch ordering}
448 %************************************************************************
450 In spite of the @Produce*@ and @SccGroup@ constructors, these things
451 behave just like enumeration types.
454 instance Eq SimplifierSwitch where
455 a == b = tagOf_SimplSwitch a _EQ_ tagOf_SimplSwitch b
457 instance Ord SimplifierSwitch where
458 a < b = tagOf_SimplSwitch a _LT_ tagOf_SimplSwitch b
459 a <= b = tagOf_SimplSwitch a _LE_ tagOf_SimplSwitch b
461 tagOf_SimplSwitch SimplOkToDupCode =(ILIT(0) :: FAST_INT)
462 tagOf_SimplSwitch SimplFloatLetsExposingWHNF = ILIT(1)
463 tagOf_SimplSwitch SimplOkToFloatPrimOps = ILIT(2)
464 tagOf_SimplSwitch SimplAlwaysFloatLetsFromLets = ILIT(3)
465 tagOf_SimplSwitch SimplDoCaseElim = ILIT(4)
466 tagOf_SimplSwitch SimplReuseCon = ILIT(5)
467 tagOf_SimplSwitch SimplCaseOfCase = ILIT(6)
468 tagOf_SimplSwitch SimplLetToCase = ILIT(7)
469 tagOf_SimplSwitch SimplMayDeleteConjurableIds = ILIT(9)
470 tagOf_SimplSwitch SimplPedanticBottoms = ILIT(10)
471 tagOf_SimplSwitch SimplDoArityExpand = ILIT(11)
472 tagOf_SimplSwitch SimplDoFoldrBuild = ILIT(12)
473 tagOf_SimplSwitch SimplDoInlineFoldrBuild = ILIT(14)
474 tagOf_SimplSwitch IgnoreINLINEPragma = ILIT(15)
475 tagOf_SimplSwitch SimplDoLambdaEtaExpansion = ILIT(16)
476 tagOf_SimplSwitch SimplDoEtaReduction = ILIT(18)
477 tagOf_SimplSwitch EssentialUnfoldingsOnly = ILIT(19)
478 tagOf_SimplSwitch ShowSimplifierProgress = ILIT(20)
479 tagOf_SimplSwitch (MaxSimplifierIterations _) = ILIT(21)
480 tagOf_SimplSwitch KeepSpecPragmaIds = ILIT(25)
481 tagOf_SimplSwitch KeepUnusedBindings = ILIT(26)
482 tagOf_SimplSwitch SimplNoLetFromCase = ILIT(27)
483 tagOf_SimplSwitch SimplNoLetFromApp = ILIT(28)
484 tagOf_SimplSwitch SimplNoLetFromStrictLet = ILIT(29)
485 tagOf_SimplSwitch SimplDontFoldBackAppend = ILIT(30)
486 tagOf_SimplSwitch SimplCaseMerge = ILIT(31)
487 -- If you add anything here, be sure to change lAST_SIMPL_SWITCH_TAG, too!
489 tagOf_SimplSwitch _ = panic# "tagOf_SimplSwitch"
491 lAST_SIMPL_SWITCH_TAG = IBOX(tagOf_SimplSwitch SimplCaseMerge)
494 %************************************************************************
496 \subsection{Switch lookup}
498 %************************************************************************
501 #if __GLASGOW_HASKELL__ >= 200
503 # define LIFT GHCbase.Lift
507 # define ARRAY _Array
512 isAmongSimpl :: [SimplifierSwitch] -> SimplifierSwitch -> SwitchResult
514 isAmongSimpl on_switches -- Switches mentioned later occur *earlier*
515 -- in the list; defaults right at the end.
517 tidied_on_switches = foldl rm_dups [] on_switches
518 -- The fold*l* ensures that we keep the latest switches;
519 -- ie the ones that occur earliest in the list.
521 sw_tbl :: Array Int SwitchResult
523 sw_tbl = (array (0, lAST_SIMPL_SWITCH_TAG) -- bounds...
527 all_undefined = [ i SET_TO SwBool False | i <- [0 .. lAST_SIMPL_SWITCH_TAG ] ]
529 defined_elems = map mk_assoc_elem tidied_on_switches
531 -- (avoid some unboxing, bounds checking, and other horrible things:)
532 case sw_tbl of { ARRAY bounds_who_needs_'em stuff ->
534 case (indexArray# stuff (tagOf_SimplSwitch switch)) of
538 mk_assoc_elem k@(MaxSimplifierIterations lvl) = IBOX(tagOf_SimplSwitch k) SET_TO SwInt lvl
540 mk_assoc_elem k = IBOX(tagOf_SimplSwitch k) SET_TO SwBool True -- I'm here, Mom!
542 -- cannot have duplicates if we are going to use the array thing
543 rm_dups switches_so_far switch
544 = if switch `is_elem` switches_so_far
546 else switch : switches_so_far
548 sw `is_elem` [] = False
549 sw `is_elem` (s:ss) = (tagOf_SimplSwitch sw) _EQ_ (tagOf_SimplSwitch s)
553 Default settings for simplifier switches
556 defaultSimplSwitches = [MaxSimplifierIterations 1
560 %************************************************************************
562 \subsection{Misc functions for command-line options}
564 %************************************************************************
568 switchIsOn :: (switch -> SwitchResult) -> switch -> Bool
570 switchIsOn lookup_fn switch
571 = case (lookup_fn switch) of
572 SwBool False -> False
575 stringSwitchSet :: (switch -> SwitchResult)
576 -> (FAST_STRING -> switch)
579 stringSwitchSet lookup_fn switch
580 = case (lookup_fn (switch (panic "stringSwitchSet"))) of
581 SwString str -> Just str
584 intSwitchSet :: (switch -> SwitchResult)
588 intSwitchSet lookup_fn switch
589 = case (lookup_fn (switch (panic "intSwitchSet"))) of
590 SwInt int -> Just int