2 % (c) The AQUA Project, Glasgow University, 1993-1996
4 \section[SimplVar]{Simplifier stuff related to variables}
11 #include "HsVersions.h"
13 import {-# SOURCE #-} Simplify ( simplExpr )
15 import CmdLineOpts ( switchIsOn, SimplifierSwitch(..) )
17 import CoreUnfold ( Unfolding(..), UnfoldingGuidance(..),
19 FormSummary, whnfOrBottom,
21 import BinderInfo ( BinderInfo, noBinderInfo, okToInline )
23 import CostCentre ( CostCentre, isCurrentCostCentre )
24 import Id ( idType, getIdInfo, getIdUnfolding, getIdSpecialisation,
25 idMustBeINLINEd, GenId{-instance Outputable-}
27 import SpecEnv ( matchSpecEnv )
28 import Literal ( isNoRepLit )
29 import MagicUFs ( applyMagicUnfoldingFun, MagicUnfoldingFun )
30 import PprType ( GenType{-instance Outputable-} )
33 import TyCon ( tyConFamilySize )
34 import Maybes ( maybeToBool )
38 %************************************************************************
40 \subsection[Simplify-var]{Completing variables}
42 %************************************************************************
44 This where all the heavy-duty unfolding stuff comes into its own.
47 completeVar env var args result_ty
49 | maybeToBool maybe_magic_result
50 = tick MagicUnfold `thenSmpl_`
53 -- If there's an InUnfolding it means that there's no
54 -- let-binding left for the thing, so we'd better inline it!
57 Just (_, _, InUnfolding rhs_env rhs) = info_from_env
59 unfold var rhs_env rhs args result_ty
62 -- Conditional unfolding. There's a binding for the
63 -- thing, but perhaps we want to inline it anyway
64 | ( maybeToBool maybe_unfolding_info
65 && (not essential_unfoldings_only || idMustBeINLINEd var)
66 -- If "essential_unfoldings_only" is true we do no inlinings at all,
67 -- EXCEPT for things that absolutely have to be done
68 -- (see comments with idMustBeINLINEd)
70 && costCentreOk (getEnclosingCC env) (getEnclosingCC unf_env)
72 = unfold var unf_env unf_template args result_ty
75 | maybeToBool maybe_specialisation
76 = tick SpecialisationDone `thenSmpl_`
77 simplExpr (extendTyEnvEnv env spec_bindings)
83 = returnSmpl (mkGenApp (Var var) args)
86 info_from_env = lookupOutIdEnv env var
87 unfolding_from_id = getIdUnfolding var
89 ---------- Magic unfolding stuff
90 maybe_magic_result = case unfolding_from_id of
91 MagicUnfolding _ magic_fn -> applyMagicUnfoldingFun magic_fn
94 (Just magic_result) = maybe_magic_result
96 ---------- Unfolding stuff
97 must_unfold = case info_from_env of
98 Just (_, _, InUnfolding _ _) -> True
102 = case (info_from_env, unfolding_from_id) of
104 (Just (_, occ_info, OutUnfolding enc_cc unf), _)
105 -> Just (occ_info, setEnclosingCC env enc_cc, unf)
107 (_, CoreUnfolding unf)
108 -> Just (noBinderInfo, env, unf)
112 Just (occ_info, unf_env, simple_unfolding) = maybe_unfolding_info
113 SimpleUnfolding form guidance unf_template = simple_unfolding
115 ---------- Specialisation stuff
116 (ty_args, remaining_args) = initialTyArgs args
117 maybe_specialisation = matchSpecEnv (getIdSpecialisation var) ty_args
118 Just (spec_bindings, spec_template) = maybe_specialisation
122 sw_chkr = getSwitchChecker env
123 essential_unfoldings_only = switchIsOn sw_chkr EssentialUnfoldingsOnly
124 is_case_scrutinee = switchIsOn sw_chkr SimplCaseScrutinee
125 ok_to_inline = okToInline (whnfOrBottom form) small_enough occ_info
126 small_enough = smallEnoughToInline arg_evals is_case_scrutinee guidance
127 arg_evals = [is_evald arg | arg <- args, isValArg arg]
129 is_evald (VarArg v) = isEvaluated (lookupRhsInfo env v)
130 is_evald (LitArg l) = True
133 -- Perform the unfolding
134 unfold var unf_env unf_template args result_ty
137 simplCount `thenSmpl` \ n ->
139 pprTrace "Ticks > 1000 and unfolding" (sep [space, int n, ppr var])
144 returnSmpl (mkGenApp (Var var) args)
147 tickUnfold var `thenSmpl_`
148 simplExpr unf_env unf_template args result_ty
151 -- costCentreOk checks that it's ok to inline this thing
152 -- The time it *isn't* is this:
154 -- f x = let y = E in
155 -- scc "foo" (...y...)
157 -- Here y has a "current cost centre", and we can't inline it inside "foo",
158 -- regardless of whether E is a WHNF or not.
160 costCentreOk cc_encl cc_rhs
161 = isCurrentCostCentre cc_encl || not (isCurrentCostCentre cc_rhs)