499b7f756a2bfb78e226e2f729ed7eff43d8fe9d
[ghc-hetmet.git] / ghc / compiler / main / CmdLineOpts.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1996
3 %
4 \section[CmdLineOpts]{Things to do with command-line options}
5
6 \begin{code}
7 module CmdLineOpts (
8         CoreToDo(..),
9         SimplifierSwitch(..),
10         StgToDo(..),
11         SwitchResult(..),
12         classifyOpts,
13
14         intSwitchSet,
15         switchIsOn,
16
17         maybe_CompilingGhcInternals,
18         opt_AllStrict,
19         opt_AllowOverlappingInstances,
20         opt_AutoSccsOnAllToplevs,
21         opt_AutoSccsOnExportedToplevs,
22         opt_AutoSccsOnIndividualCafs,
23         opt_CompilingGhcInternals,
24         opt_D_dump_absC,
25         opt_D_dump_asm,
26         opt_D_dump_deriv,
27         opt_D_dump_ds,
28         opt_D_dump_flatC,
29         opt_D_dump_occur_anal,
30         opt_D_dump_rdr,
31         opt_D_dump_realC,
32         opt_D_dump_rn,
33         opt_D_dump_simpl,
34         opt_D_dump_simpl_iterations,
35         opt_D_dump_spec,
36         opt_D_dump_stg,
37         opt_D_dump_stranal,
38         opt_D_dump_tc,
39         opt_D_show_passes,
40         opt_D_show_rn_trace,
41         opt_D_show_rn_imports,
42         opt_D_simplifier_stats,
43         opt_D_source_stats,
44         opt_D_verbose_core2core,
45         opt_D_verbose_stg2stg,
46         opt_DoCoreLinting,
47         opt_DoStgLinting,
48         opt_DoSemiTagging,
49         opt_DoEtaReduction,
50         opt_DoTickyProfiling,
51         opt_EnsureSplittableC,
52         opt_FoldrBuildOn,
53         opt_ForConcurrent,
54         opt_GlasgowExts,
55         opt_GranMacros,
56         opt_HiMap,
57         opt_IgnoreIfacePragmas,
58         opt_IrrefutableTuples,
59         opt_LiberateCaseThreshold,
60         opt_MultiParamClasses,
61         opt_NoImplicitPrelude,
62         opt_NumbersStrict,
63         opt_OmitBlackHoling,
64         opt_OmitInterfacePragmas,
65         opt_PprStyle_All,
66         opt_PprStyle_Debug,
67         opt_PprStyle_User,              -- ToDo: rm
68         opt_PprUserLength,
69         opt_ProduceC,
70         opt_ProduceHi,
71         opt_ProduceS,
72         opt_ReportWhyUnfoldingsDisallowed,
73         opt_ReturnInRegsThreshold,
74         opt_SccGroup,
75         opt_SccProfilingOn,
76         opt_ShowImportSpecs,
77         opt_SigsRequired,
78         opt_SourceUnchanged,
79         opt_SpecialiseAll,
80         opt_SpecialiseImports,
81         opt_SpecialiseOverloaded,
82         opt_SpecialiseTrace,
83         opt_SpecialiseUnboxed,
84         opt_StgDoLetNoEscapes,
85
86         opt_InterfaceUnfoldThreshold,
87         opt_UnfoldingCreationThreshold,
88         opt_UnfoldingConDiscount,
89         opt_UnfoldingUseThreshold,
90         opt_UnfoldingKeenessFactor,
91
92         opt_Verbose,
93         opt_WarnNameShadowing,
94         opt_WarnUnusedMatches,
95         opt_WarnUnusedBinds,
96         opt_WarnUnusedImports,
97         opt_WarnIncompletePatterns,
98         opt_WarnOverlappingPatterns,
99         opt_WarnSimplePatterns,
100         opt_WarnMissingMethods,
101         opt_WarnDuplicateExports,
102         opt_WarnHiShadows,
103         opt_PruneTyDecls, opt_PruneInstDecls,
104         opt_D_show_rn_stats
105     ) where
106
107 #include "HsVersions.h"
108
109 import Array    ( array, (//) )
110 import GlaExts
111 import Argv
112 import Constants        -- Default values for some flags
113
114 import Maybes           ( assocMaybe, firstJust, maybeToBool )
115 import Util             ( startsWith, panic, panic# )
116
117 #if __GLASGOW_HASKELL__ < 301
118 import ArrBase  ( Array(..) )
119 #else
120 import PrelArr  ( Array(..) )
121 #endif
122 \end{code}
123
124 A command-line {\em switch} is (generally) either on or off; e.g., the
125 ``verbose'' (-v) switch is either on or off.  (The \tr{-G<group>}
126 switch is an exception; it's set to a string, or nothing.)
127
128 A list of {\em ToDo}s is things to be done in a particular part of
129 processing.  A (fictitious) example for the Core-to-Core simplifier
130 might be: run the simplifier, then run the strictness analyser, then
131 run the simplifier again (three ``todos'').
132
133 There are three ``to-do processing centers'' at the moment.  In the
134 main loop (\tr{main/Main.lhs}), in the Core-to-Core processing loop
135 (\tr{simplCore/SimplCore.lhs), and in the STG-to-STG processing loop
136 (\tr{simplStg/SimplStg.lhs}).
137
138 %************************************************************************
139 %*                                                                      *
140 \subsection{Datatypes associated with command-line options}
141 %*                                                                      *
142 %************************************************************************
143
144 \begin{code}
145 data SwitchResult
146   = SwBool      Bool            -- on/off
147   | SwString    FAST_STRING     -- nothing or a String
148   | SwInt       Int             -- nothing or an Int
149 \end{code}
150
151 \begin{code}
152 data CoreToDo           -- These are diff core-to-core passes,
153                         -- which may be invoked in any order,
154                         -- as many times as you like.
155
156   = CoreDoSimplify      -- The core-to-core simplifier.
157         (SimplifierSwitch -> SwitchResult)
158                         -- Each run of the simplifier can take a different
159                         -- set of simplifier-specific flags.
160   | CoreDoCalcInlinings1
161   | CoreDoCalcInlinings2
162   | CoreDoFloatInwards
163   | CoreDoFullLaziness
164   | CoreLiberateCase
165   | CoreDoPrintCore
166   | CoreDoStaticArgs
167   | CoreDoStrictness
168   | CoreDoSpecialising
169   | CoreDoFoldrBuildWorkerWrapper
170   | CoreDoFoldrBuildWWAnal
171 \end{code}
172
173 \begin{code}
174 data StgToDo
175   = StgDoStaticArgs
176   | StgDoUpdateAnalysis
177   | StgDoLambdaLift
178   | StgDoMassageForProfiling  -- should be (next to) last
179   -- There's also setStgVarInfo, but its absolute "lastness"
180   -- is so critical that it is hardwired in (no flag).
181   | D_stg_stats
182 \end{code}
183
184 \begin{code}
185 data SimplifierSwitch
186   = SimplOkToDupCode
187   | SimplFloatLetsExposingWHNF
188   | SimplOkToFloatPrimOps
189   | SimplAlwaysFloatLetsFromLets
190   | SimplDoCaseElim
191   | SimplReuseCon
192   | SimplCaseOfCase
193   | SimplLetToCase
194   | SimplMayDeleteConjurableIds
195   | SimplPedanticBottoms -- see Simplifier for an explanation
196   | SimplDoArityExpand   -- expand arity of bindings
197   | SimplDoFoldrBuild    -- This is the per-simplification flag;
198                          -- see also FoldrBuildOn, used elsewhere
199                          -- in the compiler.
200   | SimplDoInlineFoldrBuild
201                          -- inline foldr/build (*after* f/b rule is used)
202
203   | IgnoreINLINEPragma
204   | SimplDoLambdaEtaExpansion
205
206   | EssentialUnfoldingsOnly -- never mind the thresholds, only
207                             -- do unfoldings that *must* be done
208                             -- (to saturate constructors and primitives)
209
210   | ShowSimplifierProgress  -- report counts on every interation
211
212   | MaxSimplifierIterations Int
213
214   | SimplNoLetFromCase      -- used when turning off floating entirely
215   | SimplNoLetFromApp       -- (for experimentation only) WDP 95/10
216   | SimplNoLetFromStrictLet
217
218   | SimplDontFoldBackAppend
219                         -- we fold `foldr (:)' back into flip (++),
220                         -- but we *don't* want to do it when compiling
221                         -- List.hs, otherwise
222                         -- xs ++ ys = foldr (:) ys xs
223                         -- {- via our loopback -}
224                         -- xs ++ ys = xs ++ ys
225                         -- Oops!
226                         -- So only use this flag inside List.hs
227                         -- (Sigh, what a HACK, Andy.  WDP 96/01)
228
229   | SimplCaseMerge
230   | SimplCaseScrutinee  -- This flag tells that the expression being simplified is
231                         -- the scrutinee of a case expression, so we should
232                         -- apply the scrutinee discount when considering inlinings.
233                         -- See SimplVar.lhs
234
235   | SimplCloneBinds     -- This flag controls whether the simplifier should 
236                         -- always clone binder ids when creating expression 
237                         -- copies. The default is NO, but it needs to be turned on
238                         -- prior to floating binders outwards.
239                         -- (see comment inside SimplVar.simplBinder)
240 \end{code}
241
242 %************************************************************************
243 %*                                                                      *
244 \subsection{Classifying command-line options}
245 %*                                                                      *
246 %************************************************************************
247
248 \begin{code}
249 lookUp           :: FAST_STRING -> Bool
250 lookup_int       :: String -> Maybe Int
251 lookup_def_int   :: String -> Int -> Int
252 lookup_def_float :: String -> Float -> Float
253 lookup_str       :: String -> Maybe String
254
255 lookUp     sw = maybeToBool (assoc_opts sw)
256         
257 lookup_str sw = firstJust (map (startsWith sw) unpacked_opts)
258
259 lookup_int sw = case (lookup_str sw) of
260                   Nothing -> Nothing
261                   Just xx -> Just (read xx)
262
263 lookup_def_int sw def = case (lookup_str sw) of
264                             Nothing -> def              -- Use default
265                             Just xx -> read xx
266
267 lookup_def_float sw def = case (lookup_str sw) of
268                             Nothing -> def              -- Use default
269                             Just xx -> read xx
270
271 assoc_opts    = assocMaybe [ (a, True) | a <- argv ]
272 unpacked_opts = map _UNPK_ argv
273 \end{code}
274
275 \begin{code}
276 opt_AllStrict                   = lookUp  SLIT("-fall-strict")
277 opt_AllowOverlappingInstances   = lookUp  SLIT("-fallow-overlapping-instances")
278 opt_AutoSccsOnAllToplevs        = lookUp  SLIT("-fauto-sccs-on-all-toplevs")
279 opt_AutoSccsOnExportedToplevs   = lookUp  SLIT("-fauto-sccs-on-exported-toplevs")
280 opt_AutoSccsOnIndividualCafs    = lookUp  SLIT("-fauto-sccs-on-individual-cafs")
281 opt_CompilingGhcInternals       = maybeToBool maybe_CompilingGhcInternals
282 maybe_CompilingGhcInternals     = lookup_str "-fcompiling-ghc-internals="
283 opt_D_dump_absC                 = lookUp  SLIT("-ddump-absC")
284 opt_D_dump_asm                  = lookUp  SLIT("-ddump-asm")
285 opt_D_dump_deriv                = lookUp  SLIT("-ddump-deriv")
286 opt_D_dump_ds                   = lookUp  SLIT("-ddump-ds")
287 opt_D_dump_flatC                = lookUp  SLIT("-ddump-flatC")
288 opt_D_dump_occur_anal           = lookUp  SLIT("-ddump-occur-anal")
289 opt_D_dump_rdr                  = lookUp  SLIT("-ddump-rdr")
290 opt_D_dump_realC                = lookUp  SLIT("-ddump-realC")
291 opt_D_dump_rn                   = lookUp  SLIT("-ddump-rn")
292 opt_D_dump_simpl                = lookUp  SLIT("-ddump-simpl")
293 opt_D_dump_simpl_iterations     = lookUp  SLIT("-ddump-simpl-iterations")
294 opt_D_dump_spec                 = lookUp  SLIT("-ddump-spec")
295 opt_D_dump_stg                  = lookUp  SLIT("-ddump-stg")
296 opt_D_dump_stranal              = lookUp  SLIT("-ddump-stranal")
297 opt_D_dump_tc                   = lookUp  SLIT("-ddump-tc")
298 opt_D_show_passes               = lookUp  SLIT("-dshow-passes")
299 opt_D_show_rn_trace             = lookUp  SLIT("-dshow-rn-trace")
300 opt_D_show_rn_imports           = lookUp  SLIT("-dshow-rn-imports")
301 opt_D_simplifier_stats          = lookUp  SLIT("-dsimplifier-stats")
302 opt_D_source_stats              = lookUp  SLIT("-dsource-stats")
303 opt_D_verbose_core2core         = lookUp  SLIT("-dverbose-simpl")
304 opt_D_verbose_stg2stg           = lookUp  SLIT("-dverbose-stg")
305 opt_DoCoreLinting               = lookUp  SLIT("-dcore-lint")
306 opt_DoStgLinting                = lookUp  SLIT("-dstg-lint")
307 opt_DoSemiTagging               = lookUp  SLIT("-fsemi-tagging")
308 opt_DoTickyProfiling            = lookUp  SLIT("-fticky-ticky")
309 opt_DoEtaReduction              = lookUp  SLIT("-fdo-eta-reduction")
310 opt_EnsureSplittableC           = lookUp  SLIT("-fglobalise-toplev-names")
311 opt_FoldrBuildOn                = lookUp  SLIT("-ffoldr-build-on")
312 opt_ForConcurrent               = lookUp  SLIT("-fconcurrent")
313 opt_GranMacros                  = lookUp  SLIT("-fgransim")
314 opt_GlasgowExts                 = lookUp  SLIT("-fglasgow-exts")
315 opt_HiMap                       = lookup_str "-himap="  -- file saying where to look for .hi files
316 opt_IgnoreIfacePragmas          = lookUp  SLIT("-fignore-interface-pragmas")
317 opt_IrrefutableTuples           = lookUp  SLIT("-firrefutable-tuples")
318 opt_MultiParamClasses           = opt_GlasgowExts
319 opt_NoImplicitPrelude           = lookUp  SLIT("-fno-implicit-prelude")
320 opt_NoHiCheck                   = lookUp  SLIT("-fno-hi-version-check")
321 opt_NumbersStrict               = lookUp  SLIT("-fnumbers-strict")
322 opt_OmitBlackHoling             = lookUp  SLIT("-dno-black-holing")
323 opt_OmitInterfacePragmas        = lookUp  SLIT("-fomit-interface-pragmas")
324 opt_PprStyle_All                = lookUp  SLIT("-dppr-all")
325 opt_PprStyle_Debug              = lookUp  SLIT("-dppr-debug")
326 opt_PprStyle_User               = lookUp  SLIT("-dppr-user")
327 opt_PprUserLength               = lookup_def_int "-dppr-user-length" 5 --ToDo: give this a name
328 opt_ProduceC                    = lookup_str "-C="
329 opt_ProduceS                    = lookup_str "-S="
330 opt_ProduceHi                   = lookup_str "-hifile=" -- the one to produce this time 
331 opt_ReportWhyUnfoldingsDisallowed= lookUp SLIT("-freport-disallowed-unfoldings")
332 opt_SccProfilingOn              = lookUp  SLIT("-fscc-profiling")
333 opt_ShowImportSpecs             = lookUp  SLIT("-fshow-import-specs")
334 opt_SigsRequired                = lookUp  SLIT("-fsignatures-required")
335 opt_SourceUnchanged             = lookUp  SLIT("-fsource-unchanged")
336 opt_SpecialiseAll               = lookUp  SLIT("-fspecialise-all")
337 opt_SpecialiseImports           = lookUp  SLIT("-fspecialise-imports")
338 opt_SpecialiseOverloaded        = lookUp  SLIT("-fspecialise-overloaded")
339 opt_SpecialiseTrace             = lookUp  SLIT("-ftrace-specialisation")
340 opt_SpecialiseUnboxed           = lookUp  SLIT("-fspecialise-unboxed")
341 opt_StgDoLetNoEscapes           = lookUp  SLIT("-flet-no-escape")
342 opt_ReturnInRegsThreshold       = lookup_int "-freturn-in-regs-threshold"
343 opt_SccGroup                    = lookup_str "-G="
344 opt_Verbose                     = lookUp  SLIT("-v")
345
346 opt_InterfaceUnfoldThreshold    = lookup_def_int "-funfolding-interface-threshold" iNTERFACE_UNFOLD_THRESHOLD
347 opt_UnfoldingCreationThreshold  = lookup_def_int "-funfolding-creation-threshold"  uNFOLDING_CREATION_THRESHOLD
348 opt_UnfoldingUseThreshold       = lookup_def_int "-funfolding-use-threshold"       uNFOLDING_USE_THRESHOLD
349 opt_UnfoldingConDiscount        = lookup_def_int "-funfolding-con-discount"        uNFOLDING_CON_DISCOUNT_WEIGHT
350                         
351 opt_LiberateCaseThreshold       = lookup_def_int "-fliberate-case-threshold"       lIBERATE_CASE_THRESHOLD
352 opt_UnfoldingKeenessFactor      = lookup_def_float "-funfolding-keeness-factor"    uNFOLDING_KEENESS_FACTOR
353 opt_WarnNameShadowing           = lookUp  SLIT("-fwarn-name-shadowing")
354 opt_WarnHiShadows               = lookUp  SLIT("-fwarn-hi-shadowing")
355 opt_WarnIncompletePatterns      = lookUp  SLIT("-fwarn-incomplete-patterns")
356 opt_WarnOverlappingPatterns     = lookUp  SLIT("-fwarn-overlapping-patterns")
357 opt_WarnSimplePatterns          = lookUp  SLIT("-fwarn-simple-patterns")
358 opt_WarnUnusedMatches           = lookUp  SLIT("-fwarn-unused-matches")
359 opt_WarnUnusedBinds             = lookUp  SLIT("-fwarn-unused-binds")
360 opt_WarnUnusedImports           = lookUp  SLIT("-fwarn-unused-imports")
361 opt_WarnMissingMethods          = lookUp  SLIT("-fwarn-missing-methods")
362 opt_WarnDuplicateExports        = lookUp  SLIT("-fwarn-duplicate-exports")
363 opt_PruneTyDecls                = not (lookUp SLIT("-fno-prune-tydecls"))
364 opt_PruneInstDecls              = not (lookUp SLIT("-fno-prune-instdecls"))
365 opt_D_show_rn_stats             = lookUp SLIT("-dshow-rn-stats")
366
367 -- opt_UnfoldingOverrideThreshold       = lookup_int "-funfolding-override-threshold"
368 \end{code}
369
370 \begin{code}
371 classifyOpts :: ([CoreToDo],    -- Core-to-Core processing spec
372                  [StgToDo])     -- STG-to-STG   processing spec
373
374 classifyOpts = sep argv [] [] -- accumulators...
375   where
376     sep :: [FAST_STRING]                         -- cmd-line opts (input)
377         -> [CoreToDo] -> [StgToDo]       -- to_do accumulators
378         -> ([CoreToDo], [StgToDo])       -- result
379
380     sep [] core_td stg_td -- all done!
381       = (reverse core_td, reverse stg_td)
382
383 #       define CORE_TD(to_do) sep opts (to_do:core_td) stg_td
384 #       define STG_TD(to_do)  sep opts core_td (to_do:stg_td)
385 #       define IGNORE_ARG()   sep opts core_td stg_td
386
387     sep (opt1:opts) core_td stg_td
388       =
389         case (_UNPK_ opt1) of -- the non-"just match a string" options are at the end...
390
391           ',' : _       -> IGNORE_ARG() -- it is for the parser
392
393           "-fsimplify"  -> -- gather up SimplifierSwitches specially...
394                            simpl_sep opts defaultSimplSwitches core_td stg_td
395
396           "-fcalc-inlinings1"-> CORE_TD(CoreDoCalcInlinings1)
397           "-fcalc-inlinings2"-> CORE_TD(CoreDoCalcInlinings2)
398           "-ffloat-inwards"  -> CORE_TD(CoreDoFloatInwards)
399           "-ffull-laziness"  -> CORE_TD(CoreDoFullLaziness)
400           "-fliberate-case"  -> CORE_TD(CoreLiberateCase)
401           "-fprint-core"     -> CORE_TD(CoreDoPrintCore)
402           "-fstatic-args"    -> CORE_TD(CoreDoStaticArgs)
403           "-fstrictness"     -> CORE_TD(CoreDoStrictness)
404           "-fspecialise"     -> CORE_TD(CoreDoSpecialising)
405           "-ffoldr-build-worker-wrapper"  -> CORE_TD(CoreDoFoldrBuildWorkerWrapper)
406           "-ffoldr-build-ww-anal"  -> CORE_TD(CoreDoFoldrBuildWWAnal)
407
408           "-fstg-static-args" -> STG_TD(StgDoStaticArgs)
409           "-fupdate-analysis" -> STG_TD(StgDoUpdateAnalysis)
410           "-dstg-stats"       -> STG_TD(D_stg_stats)
411           "-flambda-lift"     -> STG_TD(StgDoLambdaLift)
412           "-fmassage-stg-for-profiling" -> STG_TD(StgDoMassageForProfiling)
413
414           _ -> -- NB: the driver is really supposed to handle bad options
415                IGNORE_ARG()
416
417     ----------------
418
419     simpl_sep :: [FAST_STRING]      -- cmd-line opts (input)
420         -> [SimplifierSwitch]       -- simplifier-switch accumulator
421         -> [CoreToDo] -> [StgToDo]  -- to_do accumulators
422         -> ([CoreToDo], [StgToDo])  -- result
423
424         -- "simpl_sep" tailcalls "sep" once it's seen one set
425         -- of SimplifierSwitches for a CoreDoSimplify.
426
427 #ifdef DEBUG
428     simpl_sep input@[] simpl_sw core_td stg_td
429       = panic "simpl_sep []"
430 #endif
431
432         -- The SimplifierSwitches should be delimited by "[" and "]".
433
434     simpl_sep (opt1:opts) simpl_sw core_td stg_td
435       = case (_UNPK_ opt1) of
436           "[" -> simpl_sep opts simpl_sw core_td stg_td
437           "]" -> let
438                     this_simpl = CoreDoSimplify (isAmongSimpl simpl_sw)
439                  in
440                  sep opts (this_simpl : core_td) stg_td
441
442 #         define SIMPL_SW(sw) simpl_sep opts (sw:simpl_sw) core_td stg_td
443
444           -- the non-"just match a string" options are at the end...
445           "-fshow-simplifier-progress"      -> SIMPL_SW(ShowSimplifierProgress)
446           "-fcode-duplication-ok"           -> SIMPL_SW(SimplOkToDupCode)
447           "-ffloat-lets-exposing-whnf"      -> SIMPL_SW(SimplFloatLetsExposingWHNF)
448           "-ffloat-primops-ok"              -> SIMPL_SW(SimplOkToFloatPrimOps)
449           "-falways-float-lets-from-lets"   -> SIMPL_SW(SimplAlwaysFloatLetsFromLets)
450           "-fdo-case-elim"                  -> SIMPL_SW(SimplDoCaseElim)
451           "-fdo-lambda-eta-expansion"       -> SIMPL_SW(SimplDoLambdaEtaExpansion)
452           "-fdo-foldr-build"                -> SIMPL_SW(SimplDoFoldrBuild)
453           "-fdo-not-fold-back-append"       -> SIMPL_SW(SimplDontFoldBackAppend)
454           "-fdo-arity-expand"               -> SIMPL_SW(SimplDoArityExpand)
455           "-fdo-inline-foldr-build"         -> SIMPL_SW(SimplDoInlineFoldrBuild)
456           "-freuse-con"                     -> SIMPL_SW(SimplReuseCon)
457           "-fcase-of-case"                  -> SIMPL_SW(SimplCaseOfCase)
458           "-fcase-merge"                    -> SIMPL_SW(SimplCaseMerge)
459           "-flet-to-case"                   -> SIMPL_SW(SimplLetToCase)
460           "-fpedantic-bottoms"              -> SIMPL_SW(SimplPedanticBottoms)
461           "-fmay-delete-conjurable-ids"     -> SIMPL_SW(SimplMayDeleteConjurableIds)
462           "-fessential-unfoldings-only"     -> SIMPL_SW(EssentialUnfoldingsOnly)
463           "-fignore-inline-pragma"          -> SIMPL_SW(IgnoreINLINEPragma)
464           "-fno-let-from-case"              -> SIMPL_SW(SimplNoLetFromCase)
465           "-fno-let-from-app"               -> SIMPL_SW(SimplNoLetFromApp)
466           "-fno-let-from-strict-let"        -> SIMPL_SW(SimplNoLetFromStrictLet)
467           "-fclone-binds"                   -> SIMPL_SW(SimplCloneBinds)
468
469           o | starts_with_msi  -> SIMPL_SW(MaxSimplifierIterations (read after_msi))
470            where
471             maybe_msi           = startsWith "-fmax-simplifier-iterations"   o
472             starts_with_msi     = maybeToBool maybe_msi
473             (Just after_msi)    = maybe_msi
474
475           _ -> -- NB: the driver is really supposed to handle bad options
476                simpl_sep opts simpl_sw core_td stg_td
477 \end{code}
478
479 %************************************************************************
480 %*                                                                      *
481 \subsection{Switch ordering}
482 %*                                                                      *
483 %************************************************************************
484
485 In spite of the @Produce*@ and @SccGroup@ constructors, these things
486 behave just like enumeration types.
487
488 \begin{code}
489 instance Eq SimplifierSwitch where
490     a == b = tagOf_SimplSwitch a _EQ_ tagOf_SimplSwitch b
491
492 instance Ord SimplifierSwitch where
493     a <  b  = tagOf_SimplSwitch a _LT_ tagOf_SimplSwitch b
494     a <= b  = tagOf_SimplSwitch a _LE_ tagOf_SimplSwitch b
495
496 tagOf_SimplSwitch SimplOkToDupCode              =(ILIT(0) :: FAST_INT)
497 tagOf_SimplSwitch SimplFloatLetsExposingWHNF    = ILIT(1)
498 tagOf_SimplSwitch SimplOkToFloatPrimOps         = ILIT(2)
499 tagOf_SimplSwitch SimplAlwaysFloatLetsFromLets  = ILIT(3)
500 tagOf_SimplSwitch SimplDoCaseElim               = ILIT(4)
501 tagOf_SimplSwitch SimplReuseCon                 = ILIT(5)
502 tagOf_SimplSwitch SimplCaseOfCase               = ILIT(6)
503 tagOf_SimplSwitch SimplLetToCase                = ILIT(7)
504 tagOf_SimplSwitch SimplMayDeleteConjurableIds   = ILIT(9)
505 tagOf_SimplSwitch SimplPedanticBottoms          = ILIT(10)
506 tagOf_SimplSwitch SimplDoArityExpand            = ILIT(11)
507 tagOf_SimplSwitch SimplDoFoldrBuild             = ILIT(12)
508 tagOf_SimplSwitch SimplDoInlineFoldrBuild       = ILIT(14)
509 tagOf_SimplSwitch IgnoreINLINEPragma            = ILIT(15)
510 tagOf_SimplSwitch SimplDoLambdaEtaExpansion     = ILIT(16)
511 tagOf_SimplSwitch EssentialUnfoldingsOnly       = ILIT(19)
512 tagOf_SimplSwitch ShowSimplifierProgress        = ILIT(20)
513 tagOf_SimplSwitch (MaxSimplifierIterations _)   = ILIT(21)
514 tagOf_SimplSwitch SimplNoLetFromCase            = ILIT(27)
515 tagOf_SimplSwitch SimplNoLetFromApp             = ILIT(28)
516 tagOf_SimplSwitch SimplNoLetFromStrictLet       = ILIT(29)
517 tagOf_SimplSwitch SimplDontFoldBackAppend       = ILIT(30)
518 tagOf_SimplSwitch SimplCaseMerge                = ILIT(31)
519 tagOf_SimplSwitch SimplCaseScrutinee            = ILIT(32)
520 tagOf_SimplSwitch SimplCloneBinds               = ILIT(33)
521
522 -- If you add anything here, be sure to change lAST_SIMPL_SWITCH_TAG, too!
523
524 tagOf_SimplSwitch _ = panic# "tagOf_SimplSwitch"
525
526 lAST_SIMPL_SWITCH_TAG = IBOX(tagOf_SimplSwitch SimplCloneBinds)
527 \end{code}
528
529 %************************************************************************
530 %*                                                                      *
531 \subsection{Switch lookup}
532 %*                                                                      *
533 %************************************************************************
534
535 \begin{code}
536 # define ARRAY      Array
537 # define LIFT       Lift
538 # define SET_TO     =:
539 (=:) a b = (a,b)
540
541 isAmongSimpl :: [SimplifierSwitch] -> SimplifierSwitch -> SwitchResult
542
543 isAmongSimpl on_switches                -- Switches mentioned later occur *earlier*
544                                         -- in the list; defaults right at the end.
545   = let
546         tidied_on_switches = foldl rm_dups [] on_switches
547                 -- The fold*l* ensures that we keep the latest switches;
548                 -- ie the ones that occur earliest in the list.
549
550         sw_tbl :: Array Int SwitchResult
551
552         sw_tbl = (array (0, lAST_SIMPL_SWITCH_TAG) -- bounds...
553                         all_undefined)
554                  // defined_elems
555
556         all_undefined = [ i SET_TO SwBool False | i <- [0 .. lAST_SIMPL_SWITCH_TAG ] ]
557
558         defined_elems = map mk_assoc_elem tidied_on_switches
559     in
560     -- (avoid some unboxing, bounds checking, and other horrible things:)
561     case sw_tbl of { ARRAY bounds_who_needs_'em stuff ->
562     \ switch ->
563         case (indexArray# stuff (tagOf_SimplSwitch switch)) of
564           LIFT v -> v
565     }
566   where
567     mk_assoc_elem k@(MaxSimplifierIterations lvl)       = IBOX(tagOf_SimplSwitch k) SET_TO SwInt lvl
568
569     mk_assoc_elem k = IBOX(tagOf_SimplSwitch k) SET_TO SwBool   True -- I'm here, Mom!
570
571     -- cannot have duplicates if we are going to use the array thing
572     rm_dups switches_so_far switch
573       = if switch `is_elem` switches_so_far
574         then switches_so_far
575         else switch : switches_so_far
576       where
577         sw `is_elem` []     = False
578         sw `is_elem` (s:ss) = (tagOf_SimplSwitch sw) _EQ_ (tagOf_SimplSwitch s)
579                             || sw `is_elem` ss
580 \end{code}
581
582 Default settings for simplifier switches
583
584 \begin{code}
585 defaultSimplSwitches = [MaxSimplifierIterations         1
586                        ]
587 \end{code}
588
589 %************************************************************************
590 %*                                                                      *
591 \subsection{Misc functions for command-line options}
592 %*                                                                      *
593 %************************************************************************
594
595
596 \begin{code}
597 switchIsOn :: (switch -> SwitchResult) -> switch -> Bool
598
599 switchIsOn lookup_fn switch
600   = case (lookup_fn switch) of
601       SwBool False -> False
602       _            -> True
603
604 intSwitchSet :: (switch -> SwitchResult)
605              -> (Int -> switch)
606              -> Maybe Int
607
608 intSwitchSet lookup_fn switch
609   = case (lookup_fn (switch (panic "intSwitchSet"))) of
610       SwInt int -> Just int
611       _         -> Nothing
612 \end{code}