| 1 | % |
|---|
| 2 | % (c) The University of Glasgow 2006 |
|---|
| 3 | % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998 |
|---|
| 4 | % |
|---|
| 5 | Taken quite directly from the Peyton Jones/Lester paper. |
|---|
| 6 | |
|---|
| 7 | \begin{code} |
|---|
| 8 | -- | A module concerned with finding the free variables of an expression. |
|---|
| 9 | module CoreFVs ( |
|---|
| 10 | -- * Free variables of expressions and binding groups |
|---|
| 11 | exprFreeVars, -- CoreExpr -> VarSet -- Find all locally-defined free Ids or tyvars |
|---|
| 12 | exprFreeIds, -- CoreExpr -> IdSet -- Find all locally-defined free Ids |
|---|
| 13 | exprsFreeVars, -- [CoreExpr] -> VarSet |
|---|
| 14 | bindFreeVars, -- CoreBind -> VarSet |
|---|
| 15 | |
|---|
| 16 | -- * Selective free variables of expressions |
|---|
| 17 | InterestingVarFun, |
|---|
| 18 | exprSomeFreeVars, exprsSomeFreeVars, |
|---|
| 19 | |
|---|
| 20 | -- * Free variables of Rules, Vars and Ids |
|---|
| 21 | varTypeTyVars, |
|---|
| 22 | idUnfoldingVars, idFreeVars, idRuleAndUnfoldingVars, |
|---|
| 23 | idRuleVars, idRuleRhsVars, stableUnfoldingVars, |
|---|
| 24 | ruleRhsFreeVars, rulesFreeVars, |
|---|
| 25 | ruleLhsOrphNames, ruleLhsFreeIds, |
|---|
| 26 | vectsFreeVars, |
|---|
| 27 | |
|---|
| 28 | -- * Core syntax tree annotation with free variables |
|---|
| 29 | CoreExprWithFVs, -- = AnnExpr Id VarSet |
|---|
| 30 | CoreBindWithFVs, -- = AnnBind Id VarSet |
|---|
| 31 | freeVars, -- CoreExpr -> CoreExprWithFVs |
|---|
| 32 | freeVarsOf -- CoreExprWithFVs -> IdSet |
|---|
| 33 | ) where |
|---|
| 34 | |
|---|
| 35 | #include "HsVersions.h" |
|---|
| 36 | |
|---|
| 37 | import CoreSyn |
|---|
| 38 | import Id |
|---|
| 39 | import IdInfo |
|---|
| 40 | import NameSet |
|---|
| 41 | import UniqFM |
|---|
| 42 | import Name |
|---|
| 43 | import VarSet |
|---|
| 44 | import Var |
|---|
| 45 | import TcType |
|---|
| 46 | import Coercion |
|---|
| 47 | import Maybes( orElse ) |
|---|
| 48 | import Util |
|---|
| 49 | import BasicTypes( Activation ) |
|---|
| 50 | import Outputable |
|---|
| 51 | \end{code} |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | %************************************************************************ |
|---|
| 55 | %* * |
|---|
| 56 | \section{Finding the free variables of an expression} |
|---|
| 57 | %* * |
|---|
| 58 | %************************************************************************ |
|---|
| 59 | |
|---|
| 60 | This function simply finds the free variables of an expression. |
|---|
| 61 | So far as type variables are concerned, it only finds tyvars that are |
|---|
| 62 | |
|---|
| 63 | * free in type arguments, |
|---|
| 64 | * free in the type of a binder, |
|---|
| 65 | |
|---|
| 66 | but not those that are free in the type of variable occurrence. |
|---|
| 67 | |
|---|
| 68 | \begin{code} |
|---|
| 69 | -- | Find all locally-defined free Ids or type variables in an expression |
|---|
| 70 | exprFreeVars :: CoreExpr -> VarSet |
|---|
| 71 | exprFreeVars = exprSomeFreeVars isLocalVar |
|---|
| 72 | |
|---|
| 73 | -- | Find all locally-defined free Ids in an expression |
|---|
| 74 | exprFreeIds :: CoreExpr -> IdSet -- Find all locally-defined free Ids |
|---|
| 75 | exprFreeIds = exprSomeFreeVars isLocalId |
|---|
| 76 | |
|---|
| 77 | -- | Find all locally-defined free Ids or type variables in several expressions |
|---|
| 78 | exprsFreeVars :: [CoreExpr] -> VarSet |
|---|
| 79 | exprsFreeVars = foldr (unionVarSet . exprFreeVars) emptyVarSet |
|---|
| 80 | |
|---|
| 81 | -- | Find all locally defined free Ids in a binding group |
|---|
| 82 | bindFreeVars :: CoreBind -> VarSet |
|---|
| 83 | bindFreeVars (NonRec _ r) = exprFreeVars r |
|---|
| 84 | bindFreeVars (Rec prs) = addBndrs (map fst prs) |
|---|
| 85 | (foldr (union . rhs_fvs) noVars prs) |
|---|
| 86 | isLocalVar emptyVarSet |
|---|
| 87 | |
|---|
| 88 | -- | Finds free variables in an expression selected by a predicate |
|---|
| 89 | exprSomeFreeVars :: InterestingVarFun -- ^ Says which 'Var's are interesting |
|---|
| 90 | -> CoreExpr |
|---|
| 91 | -> VarSet |
|---|
| 92 | exprSomeFreeVars fv_cand e = expr_fvs e fv_cand emptyVarSet |
|---|
| 93 | |
|---|
| 94 | -- | Finds free variables in several expressions selected by a predicate |
|---|
| 95 | exprsSomeFreeVars :: InterestingVarFun -- Says which 'Var's are interesting |
|---|
| 96 | -> [CoreExpr] |
|---|
| 97 | -> VarSet |
|---|
| 98 | exprsSomeFreeVars fv_cand = foldr (unionVarSet . exprSomeFreeVars fv_cand) emptyVarSet |
|---|
| 99 | |
|---|
| 100 | -- | Predicate on possible free variables: returns @True@ iff the variable is interesting |
|---|
| 101 | type InterestingVarFun = Var -> Bool |
|---|
| 102 | \end{code} |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | \begin{code} |
|---|
| 106 | type FV = InterestingVarFun |
|---|
| 107 | -> VarSet -- In scope |
|---|
| 108 | -> VarSet -- Free vars |
|---|
| 109 | |
|---|
| 110 | union :: FV -> FV -> FV |
|---|
| 111 | union fv1 fv2 fv_cand in_scope = fv1 fv_cand in_scope `unionVarSet` fv2 fv_cand in_scope |
|---|
| 112 | |
|---|
| 113 | noVars :: FV |
|---|
| 114 | noVars _ _ = emptyVarSet |
|---|
| 115 | |
|---|
| 116 | -- Comment about obselete code |
|---|
| 117 | -- We used to gather the free variables the RULES at a variable occurrence |
|---|
| 118 | -- with the following cryptic comment: |
|---|
| 119 | -- "At a variable occurrence, add in any free variables of its rule rhss |
|---|
| 120 | -- Curiously, we gather the Id's free *type* variables from its binding |
|---|
| 121 | -- site, but its free *rule-rhs* variables from its usage sites. This |
|---|
| 122 | -- is a little weird. The reason is that the former is more efficient, |
|---|
| 123 | -- but the latter is more fine grained, and a makes a difference when |
|---|
| 124 | -- a variable mentions itself one of its own rule RHSs" |
|---|
| 125 | -- Not only is this "weird", but it's also pretty bad because it can make |
|---|
| 126 | -- a function seem more recursive than it is. Suppose |
|---|
| 127 | -- f = ...g... |
|---|
| 128 | -- g = ... |
|---|
| 129 | -- RULE g x = ...f... |
|---|
| 130 | -- Then f is not mentioned in its own RHS, and needn't be a loop breaker |
|---|
| 131 | -- (though g may be). But if we collect the rule fvs from g's occurrence, |
|---|
| 132 | -- it looks as if f mentions itself. (This bites in the eftInt/eftIntFB |
|---|
| 133 | -- code in GHC.Enum.) |
|---|
| 134 | -- |
|---|
| 135 | -- Anyway, it seems plain wrong. The RULE is like an extra RHS for the |
|---|
| 136 | -- function, so its free variables belong at the definition site. |
|---|
| 137 | -- |
|---|
| 138 | -- Deleted code looked like |
|---|
| 139 | -- foldVarSet add_rule_var var_itself_set (idRuleVars var) |
|---|
| 140 | -- add_rule_var var set | keep_it fv_cand in_scope var = extendVarSet set var |
|---|
| 141 | -- | otherwise = set |
|---|
| 142 | -- SLPJ Feb06 |
|---|
| 143 | |
|---|
| 144 | oneVar :: Id -> FV |
|---|
| 145 | oneVar var fv_cand in_scope |
|---|
| 146 | = ASSERT( isId var ) |
|---|
| 147 | if keep_it fv_cand in_scope var |
|---|
| 148 | then unitVarSet var |
|---|
| 149 | else emptyVarSet |
|---|
| 150 | |
|---|
| 151 | someVars :: VarSet -> FV |
|---|
| 152 | someVars vars fv_cand in_scope |
|---|
| 153 | = filterVarSet (keep_it fv_cand in_scope) vars |
|---|
| 154 | |
|---|
| 155 | keep_it :: InterestingVarFun -> VarSet -> Var -> Bool |
|---|
| 156 | keep_it fv_cand in_scope var |
|---|
| 157 | | var `elemVarSet` in_scope = False |
|---|
| 158 | | fv_cand var = True |
|---|
| 159 | | otherwise = False |
|---|
| 160 | |
|---|
| 161 | |
|---|
| 162 | addBndr :: CoreBndr -> FV -> FV |
|---|
| 163 | addBndr bndr fv fv_cand in_scope |
|---|
| 164 | = someVars (varTypeTyVars bndr) fv_cand in_scope |
|---|
| 165 | -- Include type varibles in the binder's type |
|---|
| 166 | -- (not just Ids; coercion variables too!) |
|---|
| 167 | `unionVarSet` fv fv_cand (in_scope `extendVarSet` bndr) |
|---|
| 168 | |
|---|
| 169 | addBndrs :: [CoreBndr] -> FV -> FV |
|---|
| 170 | addBndrs bndrs fv = foldr addBndr fv bndrs |
|---|
| 171 | \end{code} |
|---|
| 172 | |
|---|
| 173 | |
|---|
| 174 | \begin{code} |
|---|
| 175 | expr_fvs :: CoreExpr -> FV |
|---|
| 176 | |
|---|
| 177 | expr_fvs (Type ty) = someVars (tyVarsOfType ty) |
|---|
| 178 | expr_fvs (Coercion co) = someVars (tyCoVarsOfCo co) |
|---|
| 179 | expr_fvs (Var var) = oneVar var |
|---|
| 180 | expr_fvs (Lit _) = noVars |
|---|
| 181 | expr_fvs (Tick t expr) = tickish_fvs t `union` expr_fvs expr |
|---|
| 182 | expr_fvs (App fun arg) = expr_fvs fun `union` expr_fvs arg |
|---|
| 183 | expr_fvs (Lam bndr body) = addBndr bndr (expr_fvs body) |
|---|
| 184 | expr_fvs (Cast expr co) = expr_fvs expr `union` someVars (tyCoVarsOfCo co) |
|---|
| 185 | |
|---|
| 186 | expr_fvs (Case scrut bndr ty alts) |
|---|
| 187 | = expr_fvs scrut `union` someVars (tyVarsOfType ty) `union` addBndr bndr |
|---|
| 188 | (foldr (union . alt_fvs) noVars alts) |
|---|
| 189 | where |
|---|
| 190 | alt_fvs (_, bndrs, rhs) = addBndrs bndrs (expr_fvs rhs) |
|---|
| 191 | |
|---|
| 192 | expr_fvs (Let (NonRec bndr rhs) body) |
|---|
| 193 | = rhs_fvs (bndr, rhs) `union` addBndr bndr (expr_fvs body) |
|---|
| 194 | |
|---|
| 195 | expr_fvs (Let (Rec pairs) body) |
|---|
| 196 | = addBndrs (map fst pairs) |
|---|
| 197 | (foldr (union . rhs_fvs) (expr_fvs body) pairs) |
|---|
| 198 | |
|---|
| 199 | --------- |
|---|
| 200 | rhs_fvs :: (Id,CoreExpr) -> FV |
|---|
| 201 | rhs_fvs (bndr, rhs) = expr_fvs rhs `union` |
|---|
| 202 | someVars (bndrRuleAndUnfoldingVars bndr) |
|---|
| 203 | -- Treat any RULES as extra RHSs of the binding |
|---|
| 204 | |
|---|
| 205 | --------- |
|---|
| 206 | exprs_fvs :: [CoreExpr] -> FV |
|---|
| 207 | exprs_fvs exprs = foldr (union . expr_fvs) noVars exprs |
|---|
| 208 | |
|---|
| 209 | tickish_fvs :: Tickish Id -> FV |
|---|
| 210 | tickish_fvs (Breakpoint _ ids) = someVars (mkVarSet ids) |
|---|
| 211 | tickish_fvs _ = noVars |
|---|
| 212 | \end{code} |
|---|
| 213 | |
|---|
| 214 | |
|---|
| 215 | %************************************************************************ |
|---|
| 216 | %* * |
|---|
| 217 | \section{Free names} |
|---|
| 218 | %* * |
|---|
| 219 | %************************************************************************ |
|---|
| 220 | |
|---|
| 221 | \begin{code} |
|---|
| 222 | -- | ruleLhsOrphNames is used when deciding whether |
|---|
| 223 | -- a rule is an orphan. In particular, suppose that T is defined in this |
|---|
| 224 | -- module; we want to avoid declaring that a rule like: |
|---|
| 225 | -- |
|---|
| 226 | -- > fromIntegral T = fromIntegral_T |
|---|
| 227 | -- |
|---|
| 228 | -- is an orphan. Of course it isn't, and declaring it an orphan would |
|---|
| 229 | -- make the whole module an orphan module, which is bad. |
|---|
| 230 | ruleLhsOrphNames :: CoreRule -> NameSet |
|---|
| 231 | ruleLhsOrphNames (BuiltinRule { ru_fn = fn }) = unitNameSet fn |
|---|
| 232 | ruleLhsOrphNames (Rule { ru_fn = fn, ru_args = tpl_args }) |
|---|
| 233 | = addOneToNameSet (exprsOrphNames tpl_args) fn |
|---|
| 234 | -- No need to delete bndrs, because |
|---|
| 235 | -- exprsOrphNames finds only External names |
|---|
| 236 | |
|---|
| 237 | -- | Finds the free /external/ names of an expression, notably |
|---|
| 238 | -- including the names of type constructors (which of course do not show |
|---|
| 239 | -- up in 'exprFreeVars'). |
|---|
| 240 | exprOrphNames :: CoreExpr -> NameSet |
|---|
| 241 | -- There's no need to delete local binders, because they will all |
|---|
| 242 | -- be /internal/ names. |
|---|
| 243 | exprOrphNames e |
|---|
| 244 | = go e |
|---|
| 245 | where |
|---|
| 246 | go (Var v) |
|---|
| 247 | | isExternalName n = unitNameSet n |
|---|
| 248 | | otherwise = emptyNameSet |
|---|
| 249 | where n = idName v |
|---|
| 250 | go (Lit _) = emptyNameSet |
|---|
| 251 | go (Type ty) = orphNamesOfType ty -- Don't need free tyvars |
|---|
| 252 | go (Coercion co) = orphNamesOfCo co |
|---|
| 253 | go (App e1 e2) = go e1 `unionNameSets` go e2 |
|---|
| 254 | go (Lam v e) = go e `delFromNameSet` idName v |
|---|
| 255 | go (Tick _ e) = go e |
|---|
| 256 | go (Cast e co) = go e `unionNameSets` orphNamesOfCo co |
|---|
| 257 | go (Let (NonRec _ r) e) = go e `unionNameSets` go r |
|---|
| 258 | go (Let (Rec prs) e) = exprsOrphNames (map snd prs) `unionNameSets` go e |
|---|
| 259 | go (Case e _ ty as) = go e `unionNameSets` orphNamesOfType ty |
|---|
| 260 | `unionNameSets` unionManyNameSets (map go_alt as) |
|---|
| 261 | |
|---|
| 262 | go_alt (_,_,r) = go r |
|---|
| 263 | |
|---|
| 264 | -- | Finds the free /external/ names of several expressions: see 'exprOrphNames' for details |
|---|
| 265 | exprsOrphNames :: [CoreExpr] -> NameSet |
|---|
| 266 | exprsOrphNames es = foldr (unionNameSets . exprOrphNames) emptyNameSet es |
|---|
| 267 | \end{code} |
|---|
| 268 | |
|---|
| 269 | %************************************************************************ |
|---|
| 270 | %* * |
|---|
| 271 | \section[freevars-everywhere]{Attaching free variables to every sub-expression} |
|---|
| 272 | %* * |
|---|
| 273 | %************************************************************************ |
|---|
| 274 | |
|---|
| 275 | \begin{code} |
|---|
| 276 | -- | Those variables free in the right hand side of a rule |
|---|
| 277 | ruleRhsFreeVars :: CoreRule -> VarSet |
|---|
| 278 | ruleRhsFreeVars (BuiltinRule {}) = noFVs |
|---|
| 279 | ruleRhsFreeVars (Rule { ru_fn = _, ru_bndrs = bndrs, ru_rhs = rhs }) |
|---|
| 280 | = addBndrs bndrs (expr_fvs rhs) isLocalVar emptyVarSet |
|---|
| 281 | -- See Note [Rule free var hack] |
|---|
| 282 | |
|---|
| 283 | -- | Those variables free in the both the left right hand sides of a rule |
|---|
| 284 | ruleFreeVars :: CoreRule -> VarSet |
|---|
| 285 | ruleFreeVars (BuiltinRule {}) = noFVs |
|---|
| 286 | ruleFreeVars (Rule { ru_fn = _, ru_bndrs = bndrs, ru_rhs = rhs, ru_args = args }) |
|---|
| 287 | = addBndrs bndrs (exprs_fvs (rhs:args)) isLocalVar emptyVarSet |
|---|
| 288 | -- See Note [Rule free var hack] |
|---|
| 289 | |
|---|
| 290 | idRuleRhsVars :: (Activation -> Bool) -> Id -> VarSet |
|---|
| 291 | -- Just the variables free on the *rhs* of a rule |
|---|
| 292 | idRuleRhsVars is_active id |
|---|
| 293 | = foldr (unionVarSet . get_fvs) emptyVarSet (idCoreRules id) |
|---|
| 294 | where |
|---|
| 295 | get_fvs (Rule { ru_fn = fn, ru_bndrs = bndrs |
|---|
| 296 | , ru_rhs = rhs, ru_act = act }) |
|---|
| 297 | | is_active act |
|---|
| 298 | -- See Note [Finding rule RHS free vars] in OccAnal.lhs |
|---|
| 299 | = delFromUFM fvs fn -- Note [Rule free var hack] |
|---|
| 300 | where |
|---|
| 301 | fvs = addBndrs bndrs (expr_fvs rhs) isLocalVar emptyVarSet |
|---|
| 302 | get_fvs _ = noFVs |
|---|
| 303 | |
|---|
| 304 | -- | Those variables free in the right hand side of several rules |
|---|
| 305 | rulesFreeVars :: [CoreRule] -> VarSet |
|---|
| 306 | rulesFreeVars rules = foldr (unionVarSet . ruleFreeVars) emptyVarSet rules |
|---|
| 307 | |
|---|
| 308 | ruleLhsFreeIds :: CoreRule -> VarSet |
|---|
| 309 | -- ^ This finds all locally-defined free Ids on the left hand side of a rule |
|---|
| 310 | ruleLhsFreeIds (BuiltinRule {}) = noFVs |
|---|
| 311 | ruleLhsFreeIds (Rule { ru_bndrs = bndrs, ru_args = args }) |
|---|
| 312 | = addBndrs bndrs (exprs_fvs args) isLocalId emptyVarSet |
|---|
| 313 | \end{code} |
|---|
| 314 | |
|---|
| 315 | Note [Rule free var hack] (Not a hack any more) |
|---|
| 316 | ~~~~~~~~~~~~~~~~~~~~~~~~~ |
|---|
| 317 | We used not to include the Id in its own rhs free-var set. |
|---|
| 318 | Otherwise the occurrence analyser makes bindings recursive: |
|---|
| 319 | f x y = x+y |
|---|
| 320 | RULE: f (f x y) z ==> f x (f y z) |
|---|
| 321 | However, the occurrence analyser distinguishes "non-rule loop breakers" |
|---|
| 322 | from "rule-only loop breakers" (see BasicTypes.OccInfo). So it will |
|---|
| 323 | put this 'f' in a Rec block, but will mark the binding as a non-rule loop |
|---|
| 324 | breaker, which is perfectly inlinable. |
|---|
| 325 | |
|---|
| 326 | \begin{code} |
|---|
| 327 | -- |Free variables of a vectorisation declaration |
|---|
| 328 | vectsFreeVars :: [CoreVect] -> VarSet |
|---|
| 329 | vectsFreeVars = foldr (unionVarSet . vectFreeVars) emptyVarSet |
|---|
| 330 | where |
|---|
| 331 | vectFreeVars (Vect _ Nothing) = noFVs |
|---|
| 332 | vectFreeVars (Vect _ (Just rhs)) = expr_fvs rhs isLocalId emptyVarSet |
|---|
| 333 | vectFreeVars (NoVect _) = noFVs |
|---|
| 334 | vectFreeVars (VectType _ _ _) = noFVs |
|---|
| 335 | vectFreeVars (VectClass _) = noFVs |
|---|
| 336 | vectFreeVars (VectInst _) = noFVs |
|---|
| 337 | -- this function is only concerned with values, not types |
|---|
| 338 | \end{code} |
|---|
| 339 | |
|---|
| 340 | |
|---|
| 341 | %************************************************************************ |
|---|
| 342 | %* * |
|---|
| 343 | \section[freevars-everywhere]{Attaching free variables to every sub-expression} |
|---|
| 344 | %* * |
|---|
| 345 | %************************************************************************ |
|---|
| 346 | |
|---|
| 347 | The free variable pass annotates every node in the expression with its |
|---|
| 348 | NON-GLOBAL free variables and type variables. |
|---|
| 349 | |
|---|
| 350 | \begin{code} |
|---|
| 351 | -- | Every node in a binding group annotated with its |
|---|
| 352 | -- (non-global) free variables, both Ids and TyVars |
|---|
| 353 | type CoreBindWithFVs = AnnBind Id VarSet |
|---|
| 354 | -- | Every node in an expression annotated with its |
|---|
| 355 | -- (non-global) free variables, both Ids and TyVars |
|---|
| 356 | type CoreExprWithFVs = AnnExpr Id VarSet |
|---|
| 357 | |
|---|
| 358 | freeVarsOf :: CoreExprWithFVs -> IdSet |
|---|
| 359 | -- ^ Inverse function to 'freeVars' |
|---|
| 360 | freeVarsOf (free_vars, _) = free_vars |
|---|
| 361 | |
|---|
| 362 | noFVs :: VarSet |
|---|
| 363 | noFVs = emptyVarSet |
|---|
| 364 | |
|---|
| 365 | aFreeVar :: Var -> VarSet |
|---|
| 366 | aFreeVar = unitVarSet |
|---|
| 367 | |
|---|
| 368 | unionFVs :: VarSet -> VarSet -> VarSet |
|---|
| 369 | unionFVs = unionVarSet |
|---|
| 370 | |
|---|
| 371 | delBindersFV :: [Var] -> VarSet -> VarSet |
|---|
| 372 | delBindersFV bs fvs = foldr delBinderFV fvs bs |
|---|
| 373 | |
|---|
| 374 | delBinderFV :: Var -> VarSet -> VarSet |
|---|
| 375 | -- This way round, so we can do it multiple times using foldr |
|---|
| 376 | |
|---|
| 377 | -- (b `delBinderFV` s) removes the binder b from the free variable set s, |
|---|
| 378 | -- but *adds* to s |
|---|
| 379 | -- |
|---|
| 380 | -- the free variables of b's type |
|---|
| 381 | -- |
|---|
| 382 | -- This is really important for some lambdas: |
|---|
| 383 | -- In (\x::a -> x) the only mention of "a" is in the binder. |
|---|
| 384 | -- |
|---|
| 385 | -- Also in |
|---|
| 386 | -- let x::a = b in ... |
|---|
| 387 | -- we should really note that "a" is free in this expression. |
|---|
| 388 | -- It'll be pinned inside the /\a by the binding for b, but |
|---|
| 389 | -- it seems cleaner to make sure that a is in the free-var set |
|---|
| 390 | -- when it is mentioned. |
|---|
| 391 | -- |
|---|
| 392 | -- This also shows up in recursive bindings. Consider: |
|---|
| 393 | -- /\a -> letrec x::a = x in E |
|---|
| 394 | -- Now, there are no explicit free type variables in the RHS of x, |
|---|
| 395 | -- but nevertheless "a" is free in its definition. So we add in |
|---|
| 396 | -- the free tyvars of the types of the binders, and include these in the |
|---|
| 397 | -- free vars of the group, attached to the top level of each RHS. |
|---|
| 398 | -- |
|---|
| 399 | -- This actually happened in the defn of errorIO in IOBase.lhs: |
|---|
| 400 | -- errorIO (ST io) = case (errorIO# io) of |
|---|
| 401 | -- _ -> bottom |
|---|
| 402 | -- where |
|---|
| 403 | -- bottom = bottom -- Never evaluated |
|---|
| 404 | |
|---|
| 405 | delBinderFV b s = (s `delVarSet` b) `unionFVs` varTypeTyVars b |
|---|
| 406 | -- Include coercion variables too! |
|---|
| 407 | |
|---|
| 408 | varTypeTyVars :: Var -> TyVarSet |
|---|
| 409 | -- Find the type/kind variables free in the type of the id/tyvar |
|---|
| 410 | varTypeTyVars var = tyVarsOfType (varType var) |
|---|
| 411 | |
|---|
| 412 | idFreeVars :: Id -> VarSet |
|---|
| 413 | -- Type variables, rule variables, and inline variables |
|---|
| 414 | idFreeVars id = ASSERT( isId id) |
|---|
| 415 | varTypeTyVars id `unionVarSet` |
|---|
| 416 | idRuleAndUnfoldingVars id |
|---|
| 417 | |
|---|
| 418 | bndrRuleAndUnfoldingVars ::Var -> VarSet |
|---|
| 419 | -- A 'let' can bind a type variable, and idRuleVars assumes |
|---|
| 420 | -- it's seeing an Id. This function tests first. |
|---|
| 421 | bndrRuleAndUnfoldingVars v | isTyVar v = emptyVarSet |
|---|
| 422 | | otherwise = idRuleAndUnfoldingVars v |
|---|
| 423 | |
|---|
| 424 | idRuleAndUnfoldingVars :: Id -> VarSet |
|---|
| 425 | idRuleAndUnfoldingVars id = ASSERT( isId id) |
|---|
| 426 | idRuleVars id `unionVarSet` |
|---|
| 427 | idUnfoldingVars id |
|---|
| 428 | |
|---|
| 429 | idRuleVars ::Id -> VarSet -- Does *not* include CoreUnfolding vars |
|---|
| 430 | idRuleVars id = ASSERT( isId id) specInfoFreeVars (idSpecialisation id) |
|---|
| 431 | |
|---|
| 432 | idUnfoldingVars :: Id -> VarSet |
|---|
| 433 | -- Produce free vars for an unfolding, but NOT for an ordinary |
|---|
| 434 | -- (non-inline) unfolding, since it is a dup of the rhs |
|---|
| 435 | -- and we'll get exponential behaviour if we look at both unf and rhs! |
|---|
| 436 | -- But do look at the *real* unfolding, even for loop breakers, else |
|---|
| 437 | -- we might get out-of-scope variables |
|---|
| 438 | idUnfoldingVars id = stableUnfoldingVars isLocalId (realIdUnfolding id) `orElse` emptyVarSet |
|---|
| 439 | |
|---|
| 440 | stableUnfoldingVars :: InterestingVarFun -> Unfolding -> Maybe VarSet |
|---|
| 441 | stableUnfoldingVars fv_cand unf |
|---|
| 442 | = case unf of |
|---|
| 443 | CoreUnfolding { uf_tmpl = rhs, uf_src = src } |
|---|
| 444 | | isStableSource src -> Just (exprSomeFreeVars fv_cand rhs) |
|---|
| 445 | DFunUnfolding _ _ args -> Just (exprsSomeFreeVars fv_cand args) |
|---|
| 446 | _other -> Nothing |
|---|
| 447 | \end{code} |
|---|
| 448 | |
|---|
| 449 | |
|---|
| 450 | %************************************************************************ |
|---|
| 451 | %* * |
|---|
| 452 | \subsection{Free variables (and types)} |
|---|
| 453 | %* * |
|---|
| 454 | %************************************************************************ |
|---|
| 455 | |
|---|
| 456 | \begin{code} |
|---|
| 457 | freeVars :: CoreExpr -> CoreExprWithFVs |
|---|
| 458 | -- ^ Annotate a 'CoreExpr' with its (non-global) free type and value variables at every tree node |
|---|
| 459 | freeVars (Var v) |
|---|
| 460 | = (fvs, AnnVar v) |
|---|
| 461 | where |
|---|
| 462 | -- ToDo: insert motivating example for why we *need* |
|---|
| 463 | -- to include the idSpecVars in the FV list. |
|---|
| 464 | -- Actually [June 98] I don't think it's necessary |
|---|
| 465 | -- fvs = fvs_v `unionVarSet` idSpecVars v |
|---|
| 466 | |
|---|
| 467 | fvs | isLocalVar v = aFreeVar v |
|---|
| 468 | | otherwise = noFVs |
|---|
| 469 | |
|---|
| 470 | freeVars (Lit lit) = (noFVs, AnnLit lit) |
|---|
| 471 | freeVars (Lam b body) |
|---|
| 472 | = (b `delBinderFV` freeVarsOf body', AnnLam b body') |
|---|
| 473 | where |
|---|
| 474 | body' = freeVars body |
|---|
| 475 | |
|---|
| 476 | freeVars (App fun arg) |
|---|
| 477 | = (freeVarsOf fun2 `unionFVs` freeVarsOf arg2, AnnApp fun2 arg2) |
|---|
| 478 | where |
|---|
| 479 | fun2 = freeVars fun |
|---|
| 480 | arg2 = freeVars arg |
|---|
| 481 | |
|---|
| 482 | freeVars (Case scrut bndr ty alts) |
|---|
| 483 | = ((bndr `delBinderFV` alts_fvs) `unionFVs` freeVarsOf scrut2 `unionFVs` tyVarsOfType ty, |
|---|
| 484 | AnnCase scrut2 bndr ty alts2) |
|---|
| 485 | where |
|---|
| 486 | scrut2 = freeVars scrut |
|---|
| 487 | |
|---|
| 488 | (alts_fvs_s, alts2) = mapAndUnzip fv_alt alts |
|---|
| 489 | alts_fvs = foldr unionFVs noFVs alts_fvs_s |
|---|
| 490 | |
|---|
| 491 | fv_alt (con,args,rhs) = (delBindersFV args (freeVarsOf rhs2), |
|---|
| 492 | (con, args, rhs2)) |
|---|
| 493 | where |
|---|
| 494 | rhs2 = freeVars rhs |
|---|
| 495 | |
|---|
| 496 | freeVars (Let (NonRec binder rhs) body) |
|---|
| 497 | = (freeVarsOf rhs2 |
|---|
| 498 | `unionFVs` body_fvs |
|---|
| 499 | `unionFVs` bndrRuleAndUnfoldingVars binder, |
|---|
| 500 | -- Remember any rules; cf rhs_fvs above |
|---|
| 501 | AnnLet (AnnNonRec binder rhs2) body2) |
|---|
| 502 | where |
|---|
| 503 | rhs2 = freeVars rhs |
|---|
| 504 | body2 = freeVars body |
|---|
| 505 | body_fvs = binder `delBinderFV` freeVarsOf body2 |
|---|
| 506 | |
|---|
| 507 | freeVars (Let (Rec binds) body) |
|---|
| 508 | = (delBindersFV binders all_fvs, |
|---|
| 509 | AnnLet (AnnRec (binders `zip` rhss2)) body2) |
|---|
| 510 | where |
|---|
| 511 | (binders, rhss) = unzip binds |
|---|
| 512 | |
|---|
| 513 | rhss2 = map freeVars rhss |
|---|
| 514 | rhs_body_fvs = foldr (unionFVs . freeVarsOf) body_fvs rhss2 |
|---|
| 515 | all_fvs = foldr (unionFVs . idRuleAndUnfoldingVars) rhs_body_fvs binders |
|---|
| 516 | -- The "delBinderFV" happens after adding the idSpecVars, |
|---|
| 517 | -- since the latter may add some of the binders as fvs |
|---|
| 518 | |
|---|
| 519 | body2 = freeVars body |
|---|
| 520 | body_fvs = freeVarsOf body2 |
|---|
| 521 | |
|---|
| 522 | freeVars (Cast expr co) |
|---|
| 523 | = (freeVarsOf expr2 `unionFVs` cfvs, AnnCast expr2 (cfvs, co)) |
|---|
| 524 | where |
|---|
| 525 | expr2 = freeVars expr |
|---|
| 526 | cfvs = tyCoVarsOfCo co |
|---|
| 527 | |
|---|
| 528 | freeVars (Tick tickish expr) |
|---|
| 529 | = (tickishFVs tickish `unionFVs` freeVarsOf expr2, AnnTick tickish expr2) |
|---|
| 530 | where |
|---|
| 531 | expr2 = freeVars expr |
|---|
| 532 | tickishFVs (Breakpoint _ ids) = mkVarSet ids |
|---|
| 533 | tickishFVs _ = emptyVarSet |
|---|
| 534 | |
|---|
| 535 | freeVars (Type ty) = (tyVarsOfType ty, AnnType ty) |
|---|
| 536 | |
|---|
| 537 | freeVars (Coercion co) = (tyCoVarsOfCo co, AnnCoercion co) |
|---|
| 538 | \end{code} |
|---|