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(..), SimpleUnfolding(..),
27 okToInline, smallEnoughToInline )
28 import BinderInfo ( BinderInfo, noBinderInfo )
30 import CostCentre ( CostCentre, isCurrentCostCentre )
31 import Id ( idType, getIdInfo, getIdUnfolding, getIdSpecialisation,
32 idMustBeINLINEd, GenId{-instance Outputable-}
34 import SpecEnv ( SpecEnv, lookupSpecEnv )
35 import IdInfo ( DeforestInfo(..) )
36 import Literal ( isNoRepLit )
37 import MagicUFs ( applyMagicUnfoldingFun, MagicUnfoldingFun )
38 import Outputable ( Outputable(..), PprStyle(..) )
39 import PprType ( GenType{-instance Outputable-} )
42 import TyCon ( tyConFamilySize )
43 import Util ( pprTrace, assertPanic, panic )
44 import Maybes ( maybeToBool )
48 %************************************************************************
50 \subsection[Simplify-var]{Completing variables}
52 %************************************************************************
54 This where all the heavy-duty unfolding stuff comes into its own.
57 completeVar env var args result_ty
59 | maybeToBool maybe_magic_result
60 = tick MagicUnfold `thenSmpl_`
63 -- If there's an InUnfolding it means that there's no
64 -- let-binding left for the thing, so we'd better inline it!
67 Just (_, _, InUnfolding rhs_env rhs) = info_from_env
69 unfold var rhs_env rhs args result_ty
72 -- Conditional unfolding. There's a binding for the
73 -- thing, but perhaps we want to inline it anyway
74 | ( maybeToBool maybe_unfolding_info
75 && (not essential_unfoldings_only || idMustBeINLINEd var)
76 -- If "essential_unfoldings_only" is true we do no inlinings at all,
77 -- EXCEPT for things that absolutely have to be done
78 -- (see comments with idMustBeINLINEd)
80 && costCentreOk (getEnclosingCC env) (getEnclosingCC unf_env)
82 = unfold var unf_env unf_template args result_ty
85 | maybeToBool maybe_specialisation
86 = tick SpecialisationDone `thenSmpl_`
87 simplExpr (extendTyEnvList env spec_bindings)
89 (map TyArg leftover_ty_args ++ remaining_args)
93 = returnSmpl (mkGenApp (Var var) args)
96 info_from_env = lookupOutIdEnv env var
97 unfolding_from_id = getIdUnfolding var
99 ---------- Magic unfolding stuff
100 maybe_magic_result = case unfolding_from_id of
101 MagicUnfolding _ magic_fn -> applyMagicUnfoldingFun magic_fn
104 (Just magic_result) = maybe_magic_result
106 ---------- Unfolding stuff
107 must_unfold = case info_from_env of
108 Just (_, _, InUnfolding _ _) -> True
112 = case (info_from_env, unfolding_from_id) of
114 (Just (_, occ_info, OutUnfolding enc_cc unf), _)
115 -> Just (occ_info, setEnclosingCC env enc_cc, unf)
117 (_, CoreUnfolding unf)
118 -> Just (noBinderInfo, env, unf)
122 Just (occ_info, unf_env, simple_unfolding) = maybe_unfolding_info
123 SimpleUnfolding form guidance unf_template = simple_unfolding
125 ---------- Specialisation stuff
126 (ty_args, remaining_args) = initialTyArgs args
127 maybe_specialisation = lookupSpecEnv (getIdSpecialisation var) ty_args
128 (Just (spec_template, (spec_bindings, leftover_ty_args))) = maybe_specialisation
132 sw_chkr = getSwitchChecker env
133 essential_unfoldings_only = switchIsOn sw_chkr EssentialUnfoldingsOnly
134 is_case_scrutinee = switchIsOn sw_chkr SimplCaseScrutinee
135 ok_to_inline = okToInline form occ_info small_enough
136 small_enough = smallEnoughToInline arg_evals is_case_scrutinee guidance
137 arg_evals = [is_evald arg | arg <- args, isValArg arg]
139 is_evald (VarArg v) = isEvaluated (lookupRhsInfo env v)
140 is_evald (LitArg l) = True
143 -- Perform the unfolding
144 unfold var unf_env unf_template args result_ty
147 simplCount `thenSmpl` \ n ->
149 pprTrace "Ticks > 1000 and unfolding" (sep [space, int n, ppr PprDebug var])
154 returnSmpl (mkGenApp (Var var) args)
157 tickUnfold var `thenSmpl_`
158 simplExpr unf_env unf_template args result_ty
161 -- costCentreOk checks that it's ok to inline this thing
162 -- The time it *isn't* is this:
164 -- f x = let y = E in
165 -- scc "foo" (...y...)
167 -- Here y has a "current cost centre", and we can't inline it inside "foo",
168 -- regardless of whether E is a WHNF or not.
170 costCentreOk cc_encl cc_rhs
171 = isCurrentCostCentre cc_encl || not (isCurrentCostCentre cc_rhs)