2 % (c) The AQUA Project, Glasgow University, 1993-1996
4 \section[SimplVar]{Simplifier stuff related to variables}
7 #include "HsVersions.h"
14 IMPORT_DELOOPER(SmplLoop) ( simplExpr )
16 import CgCompInfo ( uNFOLDING_USE_THRESHOLD,
17 uNFOLDING_CON_DISCOUNT_WEIGHT
19 import CmdLineOpts ( switchIsOn, SimplifierSwitch(..) )
21 import CoreUnfold ( Unfolding(..), UnfoldingGuidance(..), SimpleUnfolding(..),
24 import BinderInfo ( BinderInfo, noBinderInfo, okToInline )
26 import CostCentre ( CostCentre, noCostCentreAttached )
27 import Id ( idType, getIdInfo, getIdUnfolding, getIdSpecialisation,
28 GenId{-instance Outputable-}
30 import SpecEnv ( SpecEnv, lookupSpecEnv )
31 import IdInfo ( DeforestInfo(..) )
32 import Literal ( isNoRepLit )
33 import MagicUFs ( applyMagicUnfoldingFun, MagicUnfoldingFun )
34 import PprStyle ( PprStyle(..) )
35 import PprType ( GenType{-instance Outputable-} )
36 import Pretty ( ppBesides, ppStr )
39 import TyCon ( tyConFamilySize )
40 import Util ( pprTrace, assertPanic, panic )
41 import Maybes ( maybeToBool )
44 %************************************************************************
46 \subsection[Simplify-var]{Completing variables}
48 %************************************************************************
50 This where all the heavy-duty unfolding stuff comes into its own.
53 completeVar env var args
55 | maybeToBool maybe_magic_result
56 = tick MagicUnfold `thenSmpl_`
60 maybeToBool maybe_unfolding_info &&
61 (always_inline || (ok_to_inline && not essential_unfoldings_only)) &&
62 costCentreOk (getEnclosingCC env) (getEnclosingCC unfold_env)
63 = tick UnfoldingDone `thenSmpl_`
64 simplExpr unfold_env unf_template args
66 | maybeToBool maybe_specialisation
67 = tick SpecialisationDone `thenSmpl_`
68 simplExpr (extendTyEnvList env spec_bindings)
70 (map TyArg leftover_ty_args ++ remaining_args)
73 = returnSmpl (mkGenApp (Var var) args)
76 unfolding_from_id = getIdUnfolding var
78 ---------- Magic unfolding stuff
79 maybe_magic_result = case unfolding_from_id of
80 MagicUnfolding _ magic_fn -> applyMagicUnfoldingFun magic_fn
83 (Just magic_result) = maybe_magic_result
85 ---------- Unfolding stuff
87 = case (lookupOutIdEnv env var, unfolding_from_id) of
88 (Just (_, occ_info, OutUnfolding enc_cc unf), _)
89 -> Just (occ_info, setEnclosingCC env enc_cc, unf)
90 (Just (_, occ_info, InUnfolding env_unf unf), _)
91 -> Just (occ_info, combineSimplEnv env env_unf, unf)
92 (_, CoreUnfolding unf)
93 -> Just (noBinderInfo, env, unf)
97 Just (occ_info, unfold_env, simple_unfolding) = maybe_unfolding_info
98 SimpleUnfolding form guidance unf_template = simple_unfolding
100 ---------- Specialisation stuff
101 (ty_args, remaining_args) = initialTyArgs args
102 maybe_specialisation = lookupSpecEnv (getIdSpecialisation var) ty_args
103 (Just (spec_template, (spec_bindings, leftover_ty_args))) = maybe_specialisation
107 sw_chkr = getSwitchChecker env
108 essential_unfoldings_only = switchIsOn sw_chkr EssentialUnfoldingsOnly
109 always_inline = case guidance of {UnfoldAlways -> True; other -> False}
110 ok_to_inline = okToInline form
113 small_enough = smallEnoughToInline con_disc unf_thresh arg_evals guidance
114 arg_evals = [is_evald arg | arg <- args, isValArg arg]
116 is_evald (VarArg v) = isEvaluated (lookupRhsInfo env v)
117 is_evald (LitArg l) = True
119 con_disc = getSimplIntSwitch sw_chkr SimplUnfoldingConDiscount
120 unf_thresh = getSimplIntSwitch sw_chkr SimplUnfoldingUseThreshold
125 do_deforest = case (getInfo (getIdInfo var)) of { DoDeforest -> True; _ -> False }
129 -- costCentreOk checks that it's ok to inline this thing
130 -- The time it *isn't* is this:
132 -- f x = let y = E in
133 -- scc "foo" (...y...)
135 -- Here y has a subsumed cost centre, and we can't inline it inside "foo",
136 -- regardless of whether E is a WHNF or not.
138 costCentreOk cc_encl cc_rhs
139 = noCostCentreAttached cc_encl || not (noCostCentreAttached cc_rhs)