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 Constants ( uNFOLDING_USE_THRESHOLD,
16 uNFOLDING_CON_DISCOUNT_WEIGHT
18 import CmdLineOpts ( switchIsOn, SimplifierSwitch(..) )
20 import CoreUnfold ( Unfolding(..), UfExpr, RdrName, UnfoldingGuidance(..),
22 FormSummary, whnfOrBottom,
24 import BinderInfo ( BinderInfo, noBinderInfo, okToInline )
26 import CostCentre ( CostCentre, isCurrentCostCentre )
27 import Id ( idType, getIdInfo, getIdUnfolding, getIdSpecialisation,
28 idMustBeINLINEd, GenId{-instance Outputable-}
30 import SpecEnv ( matchSpecEnv )
31 import Literal ( isNoRepLit )
32 import MagicUFs ( applyMagicUnfoldingFun, MagicUnfoldingFun )
33 import PprType ( GenType{-instance Outputable-} )
36 import TyCon ( tyConFamilySize )
37 import Maybes ( maybeToBool )
41 %************************************************************************
43 \subsection[Simplify-var]{Completing variables}
45 %************************************************************************
47 This where all the heavy-duty unfolding stuff comes into its own.
50 completeVar env var args result_ty
52 | maybeToBool maybe_magic_result
53 = tick MagicUnfold `thenSmpl_`
56 -- If there's an InUnfolding it means that there's no
57 -- let-binding left for the thing, so we'd better inline it!
60 Just (_, _, InUnfolding rhs_env rhs) = info_from_env
62 unfold var rhs_env rhs args result_ty
65 -- Conditional unfolding. There's a binding for the
66 -- thing, but perhaps we want to inline it anyway
67 | ( maybeToBool maybe_unfolding_info
68 && (not essential_unfoldings_only || idMustBeINLINEd var)
69 -- If "essential_unfoldings_only" is true we do no inlinings at all,
70 -- EXCEPT for things that absolutely have to be done
71 -- (see comments with idMustBeINLINEd)
73 && costCentreOk (getEnclosingCC env) (getEnclosingCC unf_env)
75 = unfold var unf_env unf_template args result_ty
78 | maybeToBool maybe_specialisation
79 = tick SpecialisationDone `thenSmpl_`
80 simplExpr (extendTyEnvEnv env spec_bindings)
86 = returnSmpl (mkGenApp (Var var) args)
89 info_from_env = lookupOutIdEnv env var
90 unfolding_from_id = getIdUnfolding var
92 ---------- Magic unfolding stuff
93 maybe_magic_result = case unfolding_from_id of
94 MagicUnfolding _ magic_fn -> applyMagicUnfoldingFun magic_fn
97 (Just magic_result) = maybe_magic_result
99 ---------- Unfolding stuff
100 must_unfold = case info_from_env of
101 Just (_, _, InUnfolding _ _) -> True
105 = case (info_from_env, unfolding_from_id) of
107 (Just (_, occ_info, OutUnfolding enc_cc unf), _)
108 -> Just (occ_info, setEnclosingCC env enc_cc, unf)
110 (_, CoreUnfolding unf)
111 -> Just (noBinderInfo, env, unf)
115 Just (occ_info, unf_env, simple_unfolding) = maybe_unfolding_info
116 SimpleUnfolding form guidance unf_template = simple_unfolding
118 ---------- Specialisation stuff
119 (ty_args, remaining_args) = initialTyArgs args
120 maybe_specialisation = matchSpecEnv (getIdSpecialisation var) ty_args
121 Just (spec_bindings, spec_template) = maybe_specialisation
125 sw_chkr = getSwitchChecker env
126 essential_unfoldings_only = switchIsOn sw_chkr EssentialUnfoldingsOnly
127 is_case_scrutinee = switchIsOn sw_chkr SimplCaseScrutinee
128 ok_to_inline = okToInline (whnfOrBottom form) small_enough occ_info
129 small_enough = smallEnoughToInline arg_evals is_case_scrutinee guidance
130 arg_evals = [is_evald arg | arg <- args, isValArg arg]
132 is_evald (VarArg v) = isEvaluated (lookupRhsInfo env v)
133 is_evald (LitArg l) = True
136 -- Perform the unfolding
137 unfold var unf_env unf_template args result_ty
140 simplCount `thenSmpl` \ n ->
142 pprTrace "Ticks > 1000 and unfolding" (sep [space, int n, ppr var])
147 returnSmpl (mkGenApp (Var var) args)
150 tickUnfold var `thenSmpl_`
151 simplExpr unf_env unf_template args result_ty
154 -- costCentreOk checks that it's ok to inline this thing
155 -- The time it *isn't* is this:
157 -- f x = let y = E in
158 -- scc "foo" (...y...)
160 -- Here y has a "current cost centre", and we can't inline it inside "foo",
161 -- regardless of whether E is a WHNF or not.
163 costCentreOk cc_encl cc_rhs
164 = isCurrentCostCentre cc_encl || not (isCurrentCostCentre cc_rhs)