| @@ -17,9 +17,10 @@ |
| 17 | 17 | //! fresh names), so what is known about a variable holds wherever it is |
| 18 | 18 | //! in scope, and one map serves every branch. |
| 19 | 19 | |
| 20 | | −use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet, VecDeque}; |
| 20 | +use std::collections::{BTreeMap, BTreeSet, VecDeque}; |
| 21 | 21 | use std::rc::Rc; |
| 22 | 22 | |
| 23 | +use crate::idhash::{IdMap, IdSet}; |
| 23 | 24 | use crate::ir::{Atom, Expr, FnId, Program, Span, Value, VarId}; |
| 24 | 25 | |
| 25 | 26 | /// A function of at most this many nodes is inlinable at any site. (The |
| @@ -57,12 +58,12 @@ pub struct Budget { |
| 57 | 58 | /// The functions whose body was already inlined as a move (a closure |
| 58 | 59 | /// used once, which may exceed the cap): a second such inlining would |
| 59 | 60 | /// be a copy, so it takes the cap like any other. |
| 60 | | − moved: HashSet<FnId>, |
| 61 | + moved: IdSet<FnId>, |
| 61 | 62 | /// Where each function inlined as a move at its single direct call |
| 62 | 63 | /// site went (per target, the callers it was moved into). A move into |
| 63 | 64 | /// a caller that is now dead (never called, not an entry) left no |
| 64 | 65 | /// live copy: moving the body again is still a move. |
| 65 | | − moved_into: HashMap<FnId, Vec<FnId>>, |
| 66 | + moved_into: IdMap<FnId, Vec<FnId>>, |
| 66 | 67 | } |
| 67 | 68 | |
| 68 | 69 | /// Simplify the functions `live` of `program`, to the fixed point, with a |
| @@ -78,7 +79,7 @@ pub fn run_with_budget(program: &mut Program, live: &[FnId], budget: &mut Budget |
| 78 | 79 | // round. The passes between runs add functions: they start at 0. |
| 79 | 80 | budget.grown.resize(program.fns.len(), 0); |
| 80 | 81 | budget.caps.resize(program.fns.len(), 0); |
| 81 | | − let entries: HashSet<FnId> = program.root_caps.keys().copied().collect(); |
| 82 | + let entries: IdSet<FnId> = program.root_caps.keys().copied().collect(); |
| 82 | 83 | for _ in 0..MAX_ROUNDS { |
| 83 | 84 | let candidates = candidates(program); |
| 84 | 85 | let calls = call_counts(program); |
| @@ -111,13 +112,13 @@ pub fn run_with_budget(program: &mut Program, live: &[FnId], budget: &mut Budget |
| 111 | 112 | fn function( |
| 112 | 113 | program: &mut Program, |
| 113 | 114 | id: FnId, |
| 114 | | − candidates: &HashMap<FnId, Candidate>, |
| 115 | | − calls: &HashMap<FnId, u32>, |
| 116 | | − entries: &HashSet<FnId>, |
| 115 | + candidates: &IdMap<FnId, Candidate>, |
| 116 | + calls: &IdMap<FnId, u32>, |
| 117 | + entries: &IdSet<FnId>, |
| 117 | 118 | grown: &mut u32, |
| 118 | 119 | cap: u32, |
| 119 | | − moved: &mut HashSet<FnId>, |
| 120 | | − moved_into: &mut HashMap<FnId, Vec<FnId>>, |
| 120 | + moved: &mut IdSet<FnId>, |
| 121 | + moved_into: &mut IdMap<FnId, Vec<FnId>>, |
| 121 | 122 | ) -> bool { |
| 122 | 123 | let decl = &mut program.fns[id as usize]; |
| 123 | 124 | let body = std::mem::replace(&mut decl.body, Expr::Return(Atom::Unit)); |
| @@ -127,13 +128,13 @@ fn function( |
| 127 | 128 | entries, |
| 128 | 129 | id, |
| 129 | 130 | module: decl.module.as_deref().map(Rc::from), |
| 130 | | − subst: HashMap::new(), |
| 131 | | − known: HashMap::new(), |
| 132 | | − switch_blocks: HashMap::new(), |
| 133 | | − dead: HashSet::new(), |
| 131 | + subst: IdMap::default(), |
| 132 | + known: IdMap::default(), |
| 133 | + switch_blocks: IdMap::default(), |
| 134 | + dead: IdSet::default(), |
| 134 | 135 | next_var: decl.n_vars, |
| 135 | 136 | shapes: std::mem::take(&mut decl.shapes), |
| 136 | | − inlined: HashSet::from([id]), |
| 137 | + inlined: IdSet::from_iter([id]), |
| 137 | 138 | grown, |
| 138 | 139 | cap, |
| 139 | 140 | moved, |
| @@ -149,7 +150,7 @@ fn function( |
| 149 | 150 | decl.n_vars = s.next_var; |
| 150 | 151 | decl.shapes = s.shapes; |
| 151 | 152 | if changed { |
| 152 | | − let mut present = HashSet::new(); |
| 153 | + let mut present = IdSet::default(); |
| 153 | 154 | visit(&decl.body, &mut |a| { |
| 154 | 155 | if let Atom::Var(v) = a { |
| 155 | 156 | present.insert(*v); |
| @@ -208,7 +209,7 @@ struct Candidate { |
| 208 | 209 | has_exit: bool, |
| 209 | 210 | } |
| 210 | 211 | |
| 211 | | −fn candidates(program: &Program) -> HashMap<FnId, Candidate> { |
| 212 | +fn candidates(program: &Program) -> IdMap<FnId, Candidate> { |
| 212 | 213 | program |
| 213 | 214 | .fns |
| 214 | 215 | .iter() |
| @@ -238,8 +239,8 @@ fn candidates(program: &Program) -> HashMap<FnId, Candidate> { |
| 238 | 239 | |
| 239 | 240 | /// Direct call sites of each function, over the whole program. A closure |
| 240 | 241 | /// of a function may be called anywhere: it counts as more than one site. |
| 241 | | −fn call_counts(program: &Program) -> HashMap<FnId, u32> { |
| 242 | | − let mut counts: HashMap<FnId, u32> = HashMap::new(); |
| 242 | +fn call_counts(program: &Program) -> IdMap<FnId, u32> { |
| 243 | + let mut counts: IdMap<FnId, u32> = IdMap::default(); |
| 243 | 244 | for f in &program.fns { |
| 244 | 245 | visit_exprs(&f.body, &mut |e| { |
| 245 | 246 | let value = match e { |
| @@ -262,29 +263,29 @@ fn call_counts(program: &Program) -> HashMap<FnId, u32> { |
| 262 | 263 | } |
| 263 | 264 | |
| 264 | 265 | struct Simplify<'a> { |
| 265 | | − candidates: &'a HashMap<FnId, Candidate>, |
| 266 | + candidates: &'a IdMap<FnId, Candidate>, |
| 266 | 267 | /// Direct call sites of each function, program-wide (a closure |
| 267 | 268 | /// creation counts as many: the function value may be called |
| 268 | 269 | /// anywhere). A function with exactly one is a move candidate: its |
| 269 | 270 | /// body replaces its one call. |
| 270 | | − calls: &'a HashMap<FnId, u32>, |
| 271 | + calls: &'a IdMap<FnId, u32>, |
| 271 | 272 | /// The entries (`main`, tests): alive however they are called. |
| 272 | | − entries: &'a HashSet<FnId>, |
| 273 | + entries: &'a IdSet<FnId>, |
| 273 | 274 | /// The function being simplified. |
| 274 | 275 | id: FnId, |
| 275 | 276 | /// The function's own module: only same-module callees are inlined |
| 276 | 277 | /// (Candidate::module). |
| 277 | 278 | module: Option<Rc<str>>, |
| 278 | 279 | /// Variables bound to atoms, to substitute (copy propagation). |
| 279 | | − subst: HashMap<VarId, Atom>, |
| 280 | | − known: HashMap<VarId, Known>, |
| 280 | + subst: IdMap<VarId, Atom>, |
| 281 | + known: IdMap<VarId, Known>, |
| 281 | 282 | /// Variables bound to a block that ends in a switch (`let v = { |
| 282 | 283 | /// switch … }`): for case-of-case — a switch on `v` moves into the |
| 283 | 284 | /// inner arms, where the constructor each arm builds is known. |
| 284 | | − switch_blocks: HashMap<VarId, Rc<Expr>>, |
| 285 | + switch_blocks: IdMap<VarId, Rc<Expr>>, |
| 285 | 286 | /// `let`s a rewrite consumed (a switch block folded into its user): |
| 286 | 287 | /// dropped when the chain is rebuilt. |
| 287 | | − dead: HashSet<VarId>, |
| 288 | + dead: IdSet<VarId>, |
| 288 | 289 | /// Fresh variables: the function's `n_vars`, growing as inlining |
| 289 | 290 | /// brings a callee's variables in. |
| 290 | 291 | next_var: u32, |
| @@ -293,14 +294,14 @@ struct Simplify<'a> { |
| 293 | 294 | /// The function itself and the functions already inlined at an |
| 294 | 295 | /// enclosing point of this walk: never inlined again within it, so |
| 295 | 296 | /// mutually recursive pairs terminate. |
| 296 | | − inlined: HashSet<FnId>, |
| 297 | + inlined: IdSet<FnId>, |
| 297 | 298 | /// Nodes inlined so far (over the whole optimisation), and the cap. |
| 298 | 299 | grown: &'a mut u32, |
| 299 | 300 | cap: u32, |
| 300 | 301 | /// Functions already inlined as a move ([`Budget::moved`]). |
| 301 | | − moved: &'a mut HashSet<FnId>, |
| 302 | + moved: &'a mut IdSet<FnId>, |
| 302 | 303 | /// The callers of each single-site move ([`Budget::moved_into`]). |
| 303 | | − moved_into: &'a mut HashMap<FnId, Vec<FnId>>, |
| 304 | + moved_into: &'a mut IdMap<FnId, Vec<FnId>>, |
| 304 | 305 | changed: bool, |
| 305 | 306 | } |
| 306 | 307 | |
| @@ -334,7 +335,7 @@ impl Simplify<'_> { |
| 334 | 335 | /// the function's own (not a block's): an inlined `exit` leaves the |
| 335 | 336 | /// function, so only there is a body's `exit` the caller's (a |
| 336 | 337 | /// `Return` is the enclosing block's value, ir.rs). |
| 337 | | − fn expr(&mut self, e: Expr, uses: &HashMap<VarId, u32>, exits_ok: bool) -> Expr { |
| 338 | + fn expr(&mut self, e: Expr, uses: &IdMap<VarId, u32>, exits_ok: bool) -> Expr { |
| 338 | 339 | let (lets, end) = split(e); |
| 339 | 340 | let mut lets: VecDeque<(VarId, Value, Option<Span>)> = lets.into_iter().collect(); |
| 340 | 341 | let mut end = end; |
| @@ -532,7 +533,7 @@ impl Simplify<'_> { |
| 532 | 533 | /// A chain's end: copy propagation, case-of-known-constructor, and |
| 533 | 534 | /// the same treatment for the branches (which keep the chain's |
| 534 | 535 | /// level: a top-level switch's branches are top-level too). |
| 535 | | − fn end(&mut self, e: Expr, uses: &HashMap<VarId, u32>, exits_ok: bool) -> Expr { |
| 536 | + fn end(&mut self, e: Expr, uses: &IdMap<VarId, u32>, exits_ok: bool) -> Expr { |
| 536 | 537 | match e { |
| 537 | 538 | Expr::Switch { on, cases, default } => { |
| 538 | 539 | let on = self.atom(&on); |
| @@ -727,7 +728,7 @@ impl Simplify<'_> { |
| 727 | 728 | // the lift), then the outer switch: its arms keep |
| 728 | 729 | // working, and copy propagation and the constructor fold |
| 729 | 730 | // take it from there. |
| 730 | | − let mut freshen: HashMap<VarId, VarId> = |
| 731 | + let mut freshen: IdMap<VarId, VarId> = |
| 731 | 732 | bound.iter().map(|w| (*w, self.fresh_var())).collect(); |
| 732 | 733 | let s = self.fresh_var(); |
| 733 | 734 | freshen.insert(scrutinee, s); |
| @@ -785,7 +786,7 @@ impl Simplify<'_> { |
| 785 | 786 | v |
| 786 | 787 | } |
| 787 | 788 | |
| 788 | | − fn value(&mut self, value: Value, uses: &HashMap<VarId, u32>) -> Value { |
| 789 | + fn value(&mut self, value: Value, uses: &IdMap<VarId, u32>) -> Value { |
| 789 | 790 | match value { |
| 790 | 791 | Value::Atom(a) => Value::Atom(self.atom(&a)), |
| 791 | 792 | Value::Call(f, args) => Value::Call(self.atom(&f), self.atoms(&args)), |
| @@ -885,10 +886,10 @@ pub(crate) fn split(mut e: Expr) -> (Vec<(VarId, Value, Option<Span>)>, Expr) { |
| 885 | 886 | /// in exactly one branch of the following switch into it: it is computed |
| 886 | 887 | /// only on that path. |
| 887 | 888 | fn sweep(e: Expr, changed: &mut bool) -> Expr { |
| 888 | | − sweep_in(e, &mut HashSet::new(), changed) |
| 889 | + sweep_in(e, &mut IdSet::default(), changed) |
| 889 | 890 | } |
| 890 | 891 | |
| 891 | | −fn sweep_in(e: Expr, used: &mut HashSet<VarId>, changed: &mut bool) -> Expr { |
| 892 | +fn sweep_in(e: Expr, used: &mut IdSet<VarId>, changed: &mut bool) -> Expr { |
| 892 | 893 | let (mut lets, end) = split(e); |
| 893 | 894 | let mut end = match end { |
| 894 | 895 | Expr::Return(a) => { |
| @@ -905,7 +906,7 @@ fn sweep_in(e: Expr, used: &mut HashSet<VarId>, changed: &mut bool) -> Expr { |
| 905 | 906 | // must keep, so the branches' own set stays apart: sinking a |
| 906 | 907 | // `let` into one branch must see the uses before the switch, |
| 907 | 908 | // not the branches'. |
| 908 | | − let mut inner = HashSet::new(); |
| 909 | + let mut inner = IdSet::default(); |
| 909 | 910 | let switch = with_bodies(switch, |on, bodies| { |
| 910 | 911 | var(on, used); |
| 911 | 912 | let mut frees = sweep_bodies(bodies, &mut inner, changed); |
| @@ -928,7 +929,7 @@ fn sweep_in(e: Expr, used: &mut HashSet<VarId>, changed: &mut bool) -> Expr { |
| 928 | 929 | used.remove(&v); |
| 929 | 930 | let value = match value { |
| 930 | 931 | Value::Block(b) => { |
| 931 | | − let mut b = sweep_in(*b, &mut HashSet::new(), changed); |
| 932 | + let mut b = sweep_in(*b, &mut IdSet::default(), changed); |
| 932 | 933 | sink_into_block(&mut b, &mut lets, used, changed); |
| 933 | 934 | used.extend(free_vars(&b)); |
| 934 | 935 | Value::Block(Box::new(b)) |
| @@ -946,9 +947,9 @@ fn sweep_in(e: Expr, used: &mut HashSet<VarId>, changed: &mut bool) -> Expr { |
| 946 | 947 | /// Sweep each branch body; the free variables of each, after. |
| 947 | 948 | fn sweep_bodies( |
| 948 | 949 | bodies: &mut [Expr], |
| 949 | | − used: &mut HashSet<VarId>, |
| 950 | + used: &mut IdSet<VarId>, |
| 950 | 951 | changed: &mut bool, |
| 951 | | −) -> Vec<HashSet<VarId>> { |
| 952 | +) -> Vec<IdSet<VarId>> { |
| 952 | 953 | let mut frees = Vec::with_capacity(bodies.len()); |
| 953 | 954 | for body in bodies { |
| 954 | 955 | *body = sweep_in( |
| @@ -968,8 +969,8 @@ fn sink( |
| 968 | 969 | lets: &mut Vec<(VarId, Value, Option<Span>)>, |
| 969 | 970 | on: &Atom, |
| 970 | 971 | bodies: &mut [Expr], |
| 971 | | − frees: &mut [HashSet<VarId>], |
| 972 | | − used: &HashSet<VarId>, |
| 972 | + frees: &mut [IdSet<VarId>], |
| 973 | + used: &IdSet<VarId>, |
| 973 | 974 | changed: &mut bool, |
| 974 | 975 | ) { |
| 975 | 976 | loop { |
| @@ -1000,7 +1001,7 @@ fn sink( |
| 1000 | 1001 | fn sink_into_block( |
| 1001 | 1002 | b: &mut Expr, |
| 1002 | 1003 | lets: &mut Vec<(VarId, Value, Option<Span>)>, |
| 1003 | | − used: &HashSet<VarId>, |
| 1004 | + used: &IdSet<VarId>, |
| 1004 | 1005 | changed: &mut bool, |
| 1005 | 1006 | ) { |
| 1006 | 1007 | let (prefix, end) = split(std::mem::replace(b, Expr::Return(Atom::Unit))); |
| @@ -1011,7 +1012,7 @@ fn sink_into_block( |
| 1011 | 1012 | *b = match end { |
| 1012 | 1013 | switch @ (Expr::Switch { .. } | Expr::SwitchLit { .. }) => { |
| 1013 | 1014 | with_bodies(switch, |on, bodies| { |
| 1014 | | − let mut frees: Vec<HashSet<VarId>> = bodies.iter().map(free_vars).collect(); |
| 1015 | + let mut frees: Vec<IdSet<VarId>> = bodies.iter().map(free_vars).collect(); |
| 1015 | 1016 | sink(lets, on, bodies, &mut frees, used, changed); |
| 1016 | 1017 | }) |
| 1017 | 1018 | } |
| @@ -1414,19 +1415,19 @@ pub(crate) fn visit_value(v: &Value, f: &mut impl FnMut(&Atom)) { |
| 1414 | 1415 | } |
| 1415 | 1416 | } |
| 1416 | 1417 | |
| 1417 | | −fn var(a: &Atom, used: &mut HashSet<VarId>) { |
| 1418 | +fn var(a: &Atom, used: &mut IdSet<VarId>) { |
| 1418 | 1419 | if let Atom::Var(v) = a { |
| 1419 | 1420 | used.insert(*v); |
| 1420 | 1421 | } |
| 1421 | 1422 | } |
| 1422 | 1423 | |
| 1423 | | −fn value_vars(value: &Value, used: &mut HashSet<VarId>) { |
| 1424 | +fn value_vars(value: &Value, used: &mut IdSet<VarId>) { |
| 1424 | 1425 | visit_value(value, &mut |a| var(a, used)); |
| 1425 | 1426 | } |
| 1426 | 1427 | |
| 1427 | 1428 | /// The variables used anywhere in `e`, and how often. |
| 1428 | | −pub(crate) fn uses(e: &Expr) -> HashMap<VarId, u32> { |
| 1429 | | − let mut out: HashMap<VarId, u32> = HashMap::new(); |
| 1429 | +pub(crate) fn uses(e: &Expr) -> IdMap<VarId, u32> { |
| 1430 | + let mut out: IdMap<VarId, u32> = IdMap::default(); |
| 1430 | 1431 | visit(e, &mut |a| { |
| 1431 | 1432 | if let Atom::Var(v) = a { |
| 1432 | 1433 | *out.entry(*v).or_insert(0) += 1; |
| @@ -1436,8 +1437,8 @@ pub(crate) fn uses(e: &Expr) -> HashMap<VarId, u32> { |
| 1436 | 1437 | } |
| 1437 | 1438 | |
| 1438 | 1439 | /// The variables used anywhere in `e`. |
| 1439 | | −fn free_vars(e: &Expr) -> HashSet<VarId> { |
| 1440 | | − let mut out = HashSet::new(); |
| 1440 | +fn free_vars(e: &Expr) -> IdSet<VarId> { |
| 1441 | + let mut out = IdSet::default(); |
| 1441 | 1442 | visit(e, &mut |a| var(a, &mut out)); |
| 1442 | 1443 | out |
| 1443 | 1444 | } |