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_`
64 maybeToBool maybe_unfolding_info &&
65 (not essential_unfoldings_only || idMustBeINLINEd var) &&
66 -- If "essential_unfolds_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 unfold_env)
74 simplCount `thenSmpl` \ n ->
76 pprTrace "Ticks > 1000 and unfolding" (sep [space, int n, ppr PprDebug var])
81 returnSmpl (mkGenApp (Var var) args)
85 tickUnfold var `thenSmpl_`
86 simplExpr unfold_env unf_template args result_ty
88 | maybeToBool maybe_specialisation
89 = tick SpecialisationDone `thenSmpl_`
90 simplExpr (extendTyEnvList env spec_bindings)
92 (map TyArg leftover_ty_args ++ remaining_args)
96 = returnSmpl (mkGenApp (Var var) args)
99 unfolding_from_id = getIdUnfolding var
101 ---------- Magic unfolding stuff
102 maybe_magic_result = case unfolding_from_id of
103 MagicUnfolding _ magic_fn -> applyMagicUnfoldingFun magic_fn
106 (Just magic_result) = maybe_magic_result
108 ---------- Unfolding stuff
110 = case (lookupOutIdEnv env var, unfolding_from_id) of
112 (Just (_, occ_info, OutUnfolding enc_cc unf), _)
113 -> Just (occ_info, setEnclosingCC env enc_cc, unf)
115 (Just (_, occ_info, InUnfolding env_unf unf), _)
116 -> -- pprTrace ("InUnfolding for ") (ppr PprDebug var) $
117 Just (occ_info, env_unf, unf)
119 (_, CoreUnfolding unf)
120 -> -- pprTrace ("CoreUnfolding for ") (ppr PprDebug var) $
121 Just (noBinderInfo, env, unf)
125 Just (occ_info, unfold_env, simple_unfolding) = maybe_unfolding_info
126 SimpleUnfolding form guidance unf_template = simple_unfolding
128 ---------- Specialisation stuff
129 (ty_args, remaining_args) = initialTyArgs args
130 maybe_specialisation = lookupSpecEnv (getIdSpecialisation var) ty_args
131 (Just (spec_template, (spec_bindings, leftover_ty_args))) = maybe_specialisation
135 sw_chkr = getSwitchChecker env
136 essential_unfoldings_only = switchIsOn sw_chkr EssentialUnfoldingsOnly
137 is_case_scrutinee = switchIsOn sw_chkr SimplCaseScrutinee
138 ok_to_inline = okToInline form occ_info small_enough
139 small_enough = smallEnoughToInline arg_evals is_case_scrutinee guidance
140 arg_evals = [is_evald arg | arg <- args, isValArg arg]
142 is_evald (VarArg v) = isEvaluated (lookupRhsInfo env v)
143 is_evald (LitArg l) = True
148 do_deforest = case (getDeforestInfo (getIdInfo var)) of { DoDeforest -> True; _ -> False }
152 -- costCentreOk checks that it's ok to inline this thing
153 -- The time it *isn't* is this:
155 -- f x = let y = E in
156 -- scc "foo" (...y...)
158 -- Here y has a "current cost centre", and we can't inline it inside "foo",
159 -- regardless of whether E is a WHNF or not.
161 costCentreOk cc_encl cc_rhs
162 = isCurrentCostCentre cc_encl || not (isCurrentCostCentre cc_rhs)