2 % (c) The AQUA Project, Glasgow University, 1993-1996
4 \section[SimplVar]{Simplifier stuff related to variables}
7 #include "HsVersions.h"
14 #if defined(__GLASGOW_HASKELL__) && __GLASGOW_HASKELL__ <= 201
15 IMPORT_DELOOPER(SmplLoop) ( simplExpr )
17 import {-# SOURCE #-} Simplify ( simplExpr )
20 import Constants ( uNFOLDING_USE_THRESHOLD,
21 uNFOLDING_CON_DISCOUNT_WEIGHT
23 import CmdLineOpts ( switchIsOn, SimplifierSwitch(..) )
25 import CoreUnfold ( Unfolding(..), UfExpr, RdrName, UnfoldingGuidance(..),
27 FormSummary, whnfOrBottom,
29 import BinderInfo ( BinderInfo, noBinderInfo, okToInline )
31 import CostCentre ( CostCentre, isCurrentCostCentre )
32 import Id ( idType, getIdInfo, getIdUnfolding, getIdSpecialisation,
33 idMustBeINLINEd, GenId{-instance Outputable-}
35 import SpecEnv ( SpecEnv, lookupSpecEnv )
36 import IdInfo ( DeforestInfo(..) )
37 import Literal ( isNoRepLit )
38 import MagicUFs ( applyMagicUnfoldingFun, MagicUnfoldingFun )
39 import Outputable ( Outputable(..), PprStyle(..) )
40 import PprType ( GenType{-instance Outputable-} )
43 import TyCon ( tyConFamilySize )
44 import Util ( pprTrace, assertPanic, panic )
45 import Maybes ( maybeToBool )
49 %************************************************************************
51 \subsection[Simplify-var]{Completing variables}
53 %************************************************************************
55 This where all the heavy-duty unfolding stuff comes into its own.
58 completeVar env var args result_ty
60 | maybeToBool maybe_magic_result
61 = tick MagicUnfold `thenSmpl_`
64 -- If there's an InUnfolding it means that there's no
65 -- let-binding left for the thing, so we'd better inline it!
68 Just (_, _, InUnfolding rhs_env rhs) = info_from_env
70 unfold var rhs_env rhs args result_ty
73 -- Conditional unfolding. There's a binding for the
74 -- thing, but perhaps we want to inline it anyway
75 | ( maybeToBool maybe_unfolding_info
76 && (not essential_unfoldings_only || idMustBeINLINEd var)
77 -- If "essential_unfoldings_only" is true we do no inlinings at all,
78 -- EXCEPT for things that absolutely have to be done
79 -- (see comments with idMustBeINLINEd)
81 && costCentreOk (getEnclosingCC env) (getEnclosingCC unf_env)
83 = unfold var unf_env unf_template args result_ty
86 | maybeToBool maybe_specialisation
87 = tick SpecialisationDone `thenSmpl_`
88 simplExpr (extendTyEnvList env spec_bindings)
90 (map TyArg leftover_ty_args ++ remaining_args)
94 = returnSmpl (mkGenApp (Var var) args)
97 info_from_env = lookupOutIdEnv env var
98 unfolding_from_id = getIdUnfolding var
100 ---------- Magic unfolding stuff
101 maybe_magic_result = case unfolding_from_id of
102 MagicUnfolding _ magic_fn -> applyMagicUnfoldingFun magic_fn
105 (Just magic_result) = maybe_magic_result
107 ---------- Unfolding stuff
108 must_unfold = case info_from_env of
109 Just (_, _, InUnfolding _ _) -> True
113 = case (info_from_env, unfolding_from_id) of
115 (Just (_, occ_info, OutUnfolding enc_cc unf), _)
116 -> Just (occ_info, setEnclosingCC env enc_cc, unf)
118 (_, CoreUnfolding unf)
119 -> Just (noBinderInfo, env, unf)
123 Just (occ_info, unf_env, simple_unfolding) = maybe_unfolding_info
124 SimpleUnfolding form guidance unf_template = simple_unfolding
126 ---------- Specialisation stuff
127 (ty_args, remaining_args) = initialTyArgs args
128 maybe_specialisation = lookupSpecEnv (getIdSpecialisation var) ty_args
129 (Just (spec_template, (spec_bindings, leftover_ty_args))) = maybe_specialisation
133 sw_chkr = getSwitchChecker env
134 essential_unfoldings_only = switchIsOn sw_chkr EssentialUnfoldingsOnly
135 is_case_scrutinee = switchIsOn sw_chkr SimplCaseScrutinee
136 ok_to_inline = okToInline (whnfOrBottom form) small_enough occ_info
137 small_enough = smallEnoughToInline arg_evals is_case_scrutinee guidance
138 arg_evals = [is_evald arg | arg <- args, isValArg arg]
140 is_evald (VarArg v) = isEvaluated (lookupRhsInfo env v)
141 is_evald (LitArg l) = True
144 -- Perform the unfolding
145 unfold var unf_env unf_template args result_ty
148 simplCount `thenSmpl` \ n ->
150 pprTrace "Ticks > 1000 and unfolding" (sep [space, int n, ppr PprDebug var])
155 returnSmpl (mkGenApp (Var var) args)
158 tickUnfold var `thenSmpl_`
159 simplExpr unf_env unf_template args result_ty
162 -- costCentreOk checks that it's ok to inline this thing
163 -- The time it *isn't* is this:
165 -- f x = let y = E in
166 -- scc "foo" (...y...)
168 -- Here y has a "current cost centre", and we can't inline it inside "foo",
169 -- regardless of whether E is a WHNF or not.
171 costCentreOk cc_encl cc_rhs
172 = isCurrentCostCentre cc_encl || not (isCurrentCostCentre cc_rhs)