| @@ -11,25 +11,35 @@ |
| 11 | 11 | //! (no count changes on the fields); otherwise they are dup'd and `x` is |
| 12 | 12 | //! dropped, as before. |
| 13 | 13 | //! |
| 14 | | −//! **Reuse:** if `x` was matched by a `switch` (it is a constructor cell) |
| 15 | | −//! and a constructor or tuple at least as large as the fields read is |
| 16 | | −//! built after the release, the release keeps a unique cell as a *token* |
| 17 | | −//! (`release ... reuse as t`) and that construction becomes `reuse t in |
| 18 | | −//! C(...)`: it writes the cell in place when the sizes match. Every path |
| 19 | | −//! after the release consumes the token once: the first suitable |
| 20 | | −//! construction on the path reuses it, and a branch without one drops it |
| 21 | | −//! on entry. |
| 14 | +//! **Reuse:** if `x` is a cell (matched by a `switch`, or a tuple or |
| 15 | +//! single-constructor value, which lowering records in the function's |
| 16 | +//! shapes) and a constructor or tuple of a fitting size is built after the |
| 17 | +//! release, the release keeps a unique cell as a *token* (`release ... |
| 18 | +//! reuse as t`) and that construction becomes `reuse t in C(...)`: it |
| 19 | +//! writes the cell in place. Every path after the release consumes the |
| 20 | +//! token once: the first fitting construction on the path reuses it, and a |
| 21 | +//! branch without one drops it on entry. A token is also dropped before |
| 22 | +//! each `exit` (an early `return`) that comes before its construction, in |
| 23 | +//! a block the placement passes over. |
| 22 | 24 | //! |
| 23 | | −//! A token is also dropped before each `exit` (an early `return`) that |
| 24 | | −//! comes before its construction, in a block the placement passes over. |
| 25 | +//! **Arities (ADR 0040):** when the cell's arity is known (the function's |
| 26 | +//! shapes, and which constructor the `switch` branch is on), only a |
| 27 | +//! construction of exactly that many fields fits, and its reuse is *sized*: |
| 28 | +//! no size check at run time. Otherwise one at least as large as the |
| 29 | +//! fields read fits, and the run time checks the size, as before. |
| 30 | +//! |
| 31 | +//! **Tokens across calls:** a path with no fitting construction gives the |
| 32 | +//! token to the first direct call on it of a function that builds a cell |
| 33 | +//! of that size, through a copy of that function taking the token |
| 34 | +//! ([`Sinks`]). |
| 25 | 35 | //! |
| 26 | 36 | //! The specialisation only looks at the statements just before the drop |
| 27 | 37 | //! (projections, `dup`s and other `drop`s, which cannot unwind), so it |
| 28 | 38 | //! never moves code across a call. |
| 29 | 39 | |
| 30 | | −use std::collections::BTreeSet; |
| 40 | +use std::collections::{BTreeMap, BTreeSet, HashMap}; |
| 31 | 41 | |
| 32 | | −use louise_core::ir::{Atom, Expr, Span, Value, VarId}; |
| 42 | +use louise_core::ir::{Atom, Expr, FnId, Span, Value, VarId}; |
| 33 | 43 | |
| 34 | 44 | /// A statement of a straight-line sequence. |
| 35 | 45 | enum Stmt { |
| @@ -44,90 +54,411 @@ enum Stmt { |
| 44 | 54 | }, |
| 45 | 55 | } |
| 46 | 56 | |
| 47 | | −type Set = BTreeSet<VarId>; |
| 57 | +/// What a token's cell can become. |
| 58 | +#[derive(Clone, Copy)] |
| 59 | +pub(crate) enum Fit { |
| 60 | + /// A construction of exactly this many fields (the cell's arity). |
| 61 | + Exact(u32), |
| 62 | + /// One of at least this many fields (the arity is unknown: the run |
| 63 | + /// time checks the size). |
| 64 | + AtLeast(usize), |
| 65 | +} |
| 48 | 66 | |
| 49 | | −/// Specialise the drops of `e` and reuse cells; new variables (tokens) |
| 50 | | −/// are numbered from `n_vars`, which is updated. |
| 51 | | −pub(crate) fn specialise(e: Expr, n_vars: &mut u32) -> Expr { |
| 52 | | − specialise_in(e, n_vars, &Set::new()) |
| 67 | +impl Fit { |
| 68 | + fn fits(self, value: &Value) -> bool { |
| 69 | + match value { |
| 70 | + Value::Construct { args, .. } | Value::Tuple(args) if !args.is_empty() => match self { |
| 71 | + Fit::Exact(n) => args.len() == n as usize, |
| 72 | + Fit::AtLeast(m) => args.len() >= m, |
| 73 | + }, |
| 74 | + _ => false, |
| 75 | + } |
| 76 | + } |
| 53 | 77 | } |
| 54 | 78 | |
| 55 | | −/// [`specialise`], where `matched` are the variables a `switch` around `e` |
| 56 | | −/// inspects: constructor cells, whose cells can be reused. (A tuple, or a |
| 57 | | −/// dictionary, is projected without a `switch`; a constant dictionary is |
| 58 | | −/// immortal, never unique.) |
| 59 | | −fn specialise_in(e: Expr, n_vars: &mut u32, matched: &Set) -> Expr { |
| 60 | | − let (stmts, tail) = peel(e); |
| 61 | | − let stmts: Vec<Stmt> = stmts |
| 62 | | − .into_iter() |
| 63 | | − .map(|s| match s { |
| 64 | | − Stmt::Let(v, Value::Block(inner), span) => Stmt::Let( |
| 65 | | − v, |
| 66 | | − Value::Block(Box::new(specialise_in(*inner, n_vars, matched))), |
| 67 | | − span, |
| 68 | | − ), |
| 69 | | − s => s, |
| 70 | | − }) |
| 71 | | − .collect(); |
| 72 | | − let mut e = match tail { |
| 73 | | − Expr::Switch { on, cases, default } => { |
| 74 | | − let mut matched = matched.clone(); |
| 75 | | − if let Atom::Var(x) = on { |
| 76 | | − matched.insert(x); |
| 79 | +/// What a branch knows of a variable's constructor. |
| 80 | +#[derive(Clone)] |
| 81 | +enum Tag { |
| 82 | + Is(u32), |
| 83 | + /// None of these (a `switch`'s default branch). |
| 84 | + Not(Vec<u32>), |
| 85 | +} |
| 86 | + |
| 87 | +/// Tokens given to callees: the functions that build cells without reusing |
| 88 | +/// one (by size), and the copies taking a token that the callers asked for. |
| 89 | +pub(crate) struct Sinks { |
| 90 | + free: HashMap<FnId, BTreeSet<u32>>, |
| 91 | + /// (function, arity) to its copy that takes a token of that arity. |
| 92 | + pub(crate) copies: BTreeMap<(FnId, u32), FnId>, |
| 93 | + next: FnId, |
| 94 | +} |
| 95 | + |
| 96 | +impl Sinks { |
| 97 | + /// No sinks yet; copies are numbered from `next` (the program's size). |
| 98 | + pub(crate) fn new(next: FnId) -> Sinks { |
| 99 | + Sinks { |
| 100 | + free: HashMap::new(), |
| 101 | + copies: BTreeMap::new(), |
| 102 | + next, |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + /// `f`, specialised to `body`, is a sink for the sizes it builds |
| 107 | + /// without reusing a cell. |
| 108 | + pub(crate) fn record(&mut self, f: FnId, body: &Expr) { |
| 109 | + let mut sizes = BTreeSet::new(); |
| 110 | + free_sizes(body, &mut sizes); |
| 111 | + self.free.insert(f, sizes); |
| 112 | + } |
| 113 | + |
| 114 | + /// The copy of `g` taking a token of `arity`, if `g` builds such a cell. |
| 115 | + fn copy_of(&mut self, g: FnId, arity: u32) -> Option<FnId> { |
| 116 | + if !self.free.get(&g)?.contains(&arity) { |
| 117 | + return None; |
| 118 | + } |
| 119 | + let next = &mut self.next; |
| 120 | + Some(*self.copies.entry((g, arity)).or_insert_with(|| { |
| 121 | + let id = *next; |
| 122 | + *next += 1; |
| 123 | + id |
| 124 | + })) |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +/// The sizes of the constructions in `e` that reuse no cell. |
| 129 | +fn free_sizes(e: &Expr, out: &mut BTreeSet<u32>) { |
| 130 | + match e { |
| 131 | + Expr::Let(_, value, rest, _) => { |
| 132 | + match &**value { |
| 133 | + Value::Construct { args, .. } | Value::Tuple(args) if !args.is_empty() => { |
| 134 | + out.insert(args.len() as u32); |
| 135 | + } |
| 136 | + Value::Block(inner) => free_sizes(inner, out), |
| 137 | + _ => {} |
| 138 | + } |
| 139 | + free_sizes(rest, out); |
| 140 | + } |
| 141 | + Expr::Dup(_, rest) | Expr::Drop(_, rest) | Expr::Release { rest, .. } => { |
| 142 | + free_sizes(rest, out) |
| 143 | + } |
| 144 | + Expr::Switch { cases, default, .. } => { |
| 145 | + for (_, b) in cases { |
| 146 | + free_sizes(b, out); |
| 147 | + } |
| 148 | + if let Some(d) = default { |
| 149 | + free_sizes(d, out); |
| 150 | + } |
| 151 | + } |
| 152 | + Expr::SwitchLit { cases, default, .. } => { |
| 153 | + for (_, b) in cases { |
| 154 | + free_sizes(b, out); |
| 77 | 155 | } |
| 78 | | − Expr::Switch { |
| 156 | + free_sizes(default, out); |
| 157 | + } |
| 158 | + Expr::Return(_) | Expr::Exit(_) | Expr::Panic(_) => {} |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +/// Specialising the drops of one function. |
| 163 | +pub(crate) struct Specialiser<'a> { |
| 164 | + /// The function's shapes ([`louise_core::ir::FnDecl::shapes`]). |
| 165 | + pub(crate) shapes: &'a BTreeMap<VarId, Vec<u32>>, |
| 166 | + /// The next variable number (tokens are new variables). |
| 167 | + pub(crate) n_vars: u32, |
| 168 | + pub(crate) sinks: &'a mut Sinks, |
| 169 | +} |
| 170 | + |
| 171 | +impl Specialiser<'_> { |
| 172 | + /// Specialise the drops of `e` and reuse cells. |
| 173 | + pub(crate) fn specialise(&mut self, e: Expr) -> Expr { |
| 174 | + self.specialise_in(e, &HashMap::new()) |
| 175 | + } |
| 176 | + |
| 177 | + fn fresh(&mut self) -> VarId { |
| 178 | + let t = self.n_vars; |
| 179 | + self.n_vars += 1; |
| 180 | + t |
| 181 | + } |
| 182 | + |
| 183 | + /// The exact field count of `x`'s cell, if known here. |
| 184 | + fn arity_of(&self, x: VarId, tags: &HashMap<VarId, Tag>) -> Option<u32> { |
| 185 | + let shape = self.shapes.get(&x)?; |
| 186 | + if let [n] = shape.as_slice() { |
| 187 | + return Some(*n); |
| 188 | + } |
| 189 | + match tags.get(&x)? { |
| 190 | + Tag::Is(t) => shape.get(*t as usize).copied(), |
| 191 | + Tag::Not(excluded) => { |
| 192 | + let mut rest = (0..shape.len() as u32) |
| 193 | + .filter(|t| !excluded.contains(t)) |
| 194 | + .map(|t| shape[t as usize]); |
| 195 | + let first = rest.next()?; |
| 196 | + rest.all(|n| n == first).then_some(first) |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + /// [`Self::specialise`], where `tags` says what the `switch`es around |
| 202 | + /// `e` found out about their variables. |
| 203 | + fn specialise_in(&mut self, e: Expr, tags: &HashMap<VarId, Tag>) -> Expr { |
| 204 | + let (stmts, tail) = peel(e); |
| 205 | + let stmts: Vec<Stmt> = stmts |
| 206 | + .into_iter() |
| 207 | + .map(|s| match s { |
| 208 | + Stmt::Let(v, Value::Block(inner), span) => Stmt::Let( |
| 209 | + v, |
| 210 | + Value::Block(Box::new(self.specialise_in(*inner, tags))), |
| 211 | + span, |
| 212 | + ), |
| 213 | + s => s, |
| 214 | + }) |
| 215 | + .collect(); |
| 216 | + let mut e = match tail { |
| 217 | + Expr::Switch { on, cases, default } => { |
| 218 | + let x = match on { |
| 219 | + Atom::Var(x) => Some(x), |
| 220 | + _ => None, |
| 221 | + }; |
| 222 | + let narrowed = |tag: Tag| { |
| 223 | + let mut tags = tags.clone(); |
| 224 | + if let Some(x) = x { |
| 225 | + tags.insert(x, tag); |
| 226 | + } |
| 227 | + tags |
| 228 | + }; |
| 229 | + let seen: Vec<u32> = cases.iter().map(|(t, _)| *t).collect(); |
| 230 | + let cases = cases |
| 231 | + .into_iter() |
| 232 | + .map(|(t, b)| (t, self.specialise_in(b, &narrowed(Tag::Is(t))))) |
| 233 | + .collect(); |
| 234 | + let mut excluded = seen; |
| 235 | + if let Some(Tag::Not(before)) = x.and_then(|x| tags.get(&x)) { |
| 236 | + excluded.extend(before); |
| 237 | + } |
| 238 | + let default = default |
| 239 | + .map(|d| Box::new(self.specialise_in(*d, &narrowed(Tag::Not(excluded))))); |
| 240 | + Expr::Switch { on, cases, default } |
| 241 | + } |
| 242 | + Expr::SwitchLit { on, cases, default } => Expr::SwitchLit { |
| 79 | 243 | on, |
| 80 | 244 | cases: cases |
| 81 | 245 | .into_iter() |
| 82 | | − .map(|(t, b)| (t, specialise_in(b, n_vars, &matched))) |
| 246 | + .map(|(l, b)| (l, self.specialise_in(b, tags))) |
| 83 | 247 | .collect(), |
| 84 | | − default: default.map(|d| Box::new(specialise_in(*d, n_vars, &matched))), |
| 85 | | − } |
| 248 | + default: Box::new(self.specialise_in(*default, tags)), |
| 249 | + }, |
| 250 | + tail => tail, |
| 251 | + }; |
| 252 | + // Rebuild from the end, so the rest of a release is final when it |
| 253 | + // looks for a construction to reuse its cell. |
| 254 | + for s in release_drops(stmts).into_iter().rev() { |
| 255 | + e = match s { |
| 256 | + Stmt::Let(v, value, span) => Expr::Let(v, Box::new(value), Box::new(e), span), |
| 257 | + Stmt::Dup(v) => Expr::Dup(v, Box::new(e)), |
| 258 | + Stmt::Drop(v) => Expr::Drop(v, Box::new(e)), |
| 259 | + Stmt::Release { |
| 260 | + var, |
| 261 | + fields, |
| 262 | + min_fields, |
| 263 | + } => { |
| 264 | + let cell = tags.contains_key(&var) || self.shapes.contains_key(&var); |
| 265 | + let arity = self.arity_of(var, tags); |
| 266 | + let fit = arity.map_or(Fit::AtLeast(min_fields), Fit::Exact); |
| 267 | + let mut token = None; |
| 268 | + if cell && has_candidate(&e, fit) { |
| 269 | + let t = self.fresh(); |
| 270 | + e = self.place(e, t, fit); |
| 271 | + token = Some(t); |
| 272 | + } else if cell && let Some(n) = arity { |
| 273 | + let t = self.fresh(); |
| 274 | + match self.pass_to_call(e, t, n) { |
| 275 | + Ok(passed) => { |
| 276 | + e = passed; |
| 277 | + token = Some(t); |
| 278 | + } |
| 279 | + Err(unchanged) => { |
| 280 | + e = unchanged; |
| 281 | + self.n_vars -= 1; |
| 282 | + } |
| 283 | + } |
| 284 | + } |
| 285 | + if fields.is_empty() && token.is_none() { |
| 286 | + Expr::Drop(var, Box::new(e)) |
| 287 | + } else { |
| 288 | + Expr::Release { |
| 289 | + var, |
| 290 | + fields, |
| 291 | + token, |
| 292 | + rest: Box::new(e), |
| 293 | + } |
| 294 | + } |
| 295 | + } |
| 296 | + }; |
| 86 | 297 | } |
| 87 | | − Expr::SwitchLit { on, cases, default } => Expr::SwitchLit { |
| 88 | | − on, |
| 89 | | − cases: cases |
| 90 | | − .into_iter() |
| 91 | | − .map(|(l, b)| (l, specialise_in(b, n_vars, matched))) |
| 92 | | − .collect(), |
| 93 | | − default: Box::new(specialise_in(*default, n_vars, matched)), |
| 94 | | − }, |
| 95 | | − tail => tail, |
| 96 | | − }; |
| 97 | | − // Rebuild from the end, so the rest of a release is final when it |
| 98 | | − // looks for a construction to reuse its cell. |
| 99 | | − for s in release_drops(stmts).into_iter().rev() { |
| 100 | | − e = match s { |
| 101 | | − Stmt::Let(v, value, span) => Expr::Let(v, Box::new(value), Box::new(e), span), |
| 102 | | − Stmt::Dup(v) => Expr::Dup(v, Box::new(e)), |
| 103 | | − Stmt::Drop(v) => Expr::Drop(v, Box::new(e)), |
| 104 | | − Stmt::Release { |
| 298 | + e |
| 299 | + } |
| 300 | + |
| 301 | + /// Make the first fitting construction on each path of `e` reuse the |
| 302 | + /// token `t`; on a path without one, give it to a call, or drop it on |
| 303 | + /// entry. `e` must have one |
| 304 | + /// ([`has_candidate`]). |
| 305 | + pub(crate) fn place(&mut self, e: Expr, t: VarId, fit: Fit) -> Expr { |
| 306 | + match e { |
| 307 | + Expr::Let(v, value, rest, span) => match *value { |
| 308 | + value if fit.fits(&value) => Expr::Let( |
| 309 | + v, |
| 310 | + Box::new(Value::Reuse { |
| 311 | + token: t, |
| 312 | + value: Box::new(value), |
| 313 | + sized: matches!(fit, Fit::Exact(_)), |
| 314 | + }), |
| 315 | + rest, |
| 316 | + span, |
| 317 | + ), |
| 318 | + Value::Block(inner) if has_candidate(&inner, fit) => Expr::Let( |
| 319 | + v, |
| 320 | + Box::new(Value::Block(Box::new(self.place(*inner, t, fit)))), |
| 321 | + rest, |
| 322 | + span, |
| 323 | + ), |
| 324 | + // A block without a construction: the paths that leave the |
| 325 | + // function from it drop the token first. |
| 326 | + Value::Block(inner) => Expr::Let( |
| 327 | + v, |
| 328 | + Box::new(Value::Block(Box::new(drop_on_exit(*inner, t)))), |
| 329 | + Box::new(self.place(*rest, t, fit)), |
| 330 | + span, |
| 331 | + ), |
| 332 | + value => Expr::Let( |
| 333 | + v, |
| 334 | + Box::new(value), |
| 335 | + Box::new(self.place(*rest, t, fit)), |
| 336 | + span, |
| 337 | + ), |
| 338 | + }, |
| 339 | + Expr::Dup(v, rest) => Expr::Dup(v, Box::new(self.place(*rest, t, fit))), |
| 340 | + Expr::Drop(v, rest) => Expr::Drop(v, Box::new(self.place(*rest, t, fit))), |
| 341 | + Expr::Release { |
| 105 | 342 | var, |
| 106 | 343 | fields, |
| 107 | | − min_fields, |
| 108 | | − } => { |
| 109 | | − let token = if matched.contains(&var) && has_candidate(&e, min_fields) { |
| 110 | | − let t = *n_vars; |
| 111 | | − *n_vars += 1; |
| 112 | | − e = place(e, t, min_fields); |
| 113 | | − Some(t) |
| 114 | | − } else { |
| 115 | | − None |
| 116 | | − }; |
| 117 | | − if fields.is_empty() && token.is_none() { |
| 118 | | − Expr::Drop(var, Box::new(e)) |
| 119 | | − } else { |
| 120 | | − Expr::Release { |
| 121 | | − var, |
| 122 | | − fields, |
| 123 | | − token, |
| 124 | | − rest: Box::new(e), |
| 344 | + token, |
| 345 | + rest, |
| 346 | + } => Expr::Release { |
| 347 | + var, |
| 348 | + fields, |
| 349 | + token, |
| 350 | + rest: Box::new(self.place(*rest, t, fit)), |
| 351 | + }, |
| 352 | + Expr::Switch { on, cases, default } => Expr::Switch { |
| 353 | + on, |
| 354 | + cases: cases |
| 355 | + .into_iter() |
| 356 | + .map(|(c, b)| (c, self.branch(b, t, fit))) |
| 357 | + .collect(), |
| 358 | + default: default.map(|d| Box::new(self.branch(*d, t, fit))), |
| 359 | + }, |
| 360 | + Expr::SwitchLit { on, cases, default } => Expr::SwitchLit { |
| 361 | + on, |
| 362 | + cases: cases |
| 363 | + .into_iter() |
| 364 | + .map(|(l, b)| (l, self.branch(b, t, fit))) |
| 365 | + .collect(), |
| 366 | + default: Box::new(self.branch(*default, t, fit)), |
| 367 | + }, |
| 368 | + Expr::Exit(_) | Expr::Return(_) | Expr::Panic(_) => { |
| 369 | + unreachable!("no construction to reuse") |
| 370 | + } |
| 371 | + } |
| 372 | + } |
| 373 | + |
| 374 | + /// A branch of a placement: its construction, or a call, or a drop. |
| 375 | + fn branch(&mut self, b: Expr, t: VarId, fit: Fit) -> Expr { |
| 376 | + if has_candidate(&b, fit) { |
| 377 | + return self.place(b, t, fit); |
| 378 | + } |
| 379 | + let b = match fit { |
| 380 | + Fit::Exact(n) => match self.pass_to_call(b, t, n) { |
| 381 | + Ok(passed) => return passed, |
| 382 | + Err(b) => b, |
| 383 | + }, |
| 384 | + Fit::AtLeast(_) => b, |
| 385 | + }; |
| 386 | + Expr::Drop(t, Box::new(b)) |
| 387 | + } |
| 388 | + |
| 389 | + /// Give the token `t` (a cell of `arity` fields) to the first direct |
| 390 | + /// call on `e`'s straight path of a function that builds such a cell: |
| 391 | + /// to its copy that takes it. `Err(e)` if there is none. |
| 392 | + fn pass_to_call(&mut self, e: Expr, t: VarId, arity: u32) -> Result<Expr, Expr> { |
| 393 | + let found = { |
| 394 | + let mut at = &e; |
| 395 | + let mut k = 0; |
| 396 | + loop { |
| 397 | + match at { |
| 398 | + Expr::Let(_, value, rest, _) => { |
| 399 | + if let Value::CallFn(g, _) = &**value |
| 400 | + && self.sinks.free.get(g).is_some_and(|s| s.contains(&arity)) |
| 401 | + { |
| 402 | + break Some(k); |
| 403 | + } |
| 404 | + at = rest; |
| 125 | 405 | } |
| 406 | + Expr::Dup(_, rest) | Expr::Drop(_, rest) | Expr::Release { rest, .. } => { |
| 407 | + at = rest |
| 408 | + } |
| 409 | + _ => break None, |
| 126 | 410 | } |
| 411 | + k += 1; |
| 127 | 412 | } |
| 128 | 413 | }; |
| 414 | + let Some(k) = found else { |
| 415 | + return Err(e); |
| 416 | + }; |
| 417 | + Ok(self.rewrite_call(e, k, t, arity)) |
| 418 | + } |
| 419 | + |
| 420 | + /// `e` with its `k`-th statement, a call, calling the copy that takes |
| 421 | + /// `t`; the blocks before it drop `t` before they exit. |
| 422 | + fn rewrite_call(&mut self, e: Expr, k: usize, t: VarId, arity: u32) -> Expr { |
| 423 | + match e { |
| 424 | + Expr::Let(v, value, rest, span) if k == 0 => { |
| 425 | + let Value::CallFn(g, mut args) = *value else { |
| 426 | + unreachable!("found a call") |
| 427 | + }; |
| 428 | + let copy = self.sinks.copy_of(g, arity).expect("a sink"); |
| 429 | + args.push(Atom::Var(t)); |
| 430 | + Expr::Let(v, Box::new(Value::CallFn(copy, args)), rest, span) |
| 431 | + } |
| 432 | + Expr::Let(v, value, rest, span) => { |
| 433 | + let value = match *value { |
| 434 | + Value::Block(inner) => Value::Block(Box::new(drop_on_exit(*inner, t))), |
| 435 | + value => value, |
| 436 | + }; |
| 437 | + Expr::Let( |
| 438 | + v, |
| 439 | + Box::new(value), |
| 440 | + Box::new(self.rewrite_call(*rest, k - 1, t, arity)), |
| 441 | + span, |
| 442 | + ) |
| 443 | + } |
| 444 | + Expr::Dup(v, rest) => Expr::Dup(v, Box::new(self.rewrite_call(*rest, k - 1, t, arity))), |
| 445 | + Expr::Drop(v, rest) => { |
| 446 | + Expr::Drop(v, Box::new(self.rewrite_call(*rest, k - 1, t, arity))) |
| 447 | + } |
| 448 | + Expr::Release { |
| 449 | + var, |
| 450 | + fields, |
| 451 | + token, |
| 452 | + rest, |
| 453 | + } => Expr::Release { |
| 454 | + var, |
| 455 | + fields, |
| 456 | + token, |
| 457 | + rest: Box::new(self.rewrite_call(*rest, k - 1, t, arity)), |
| 458 | + }, |
| 459 | + _ => unreachable!("the call is on the straight path"), |
| 460 | + } |
| 129 | 461 | } |
| 130 | | − e |
| 131 | 462 | } |
| 132 | 463 | |
| 133 | 464 | /// Split `e` into its leading statements and what ends them. |
| @@ -237,109 +568,28 @@ fn release(stmts: &mut Vec<Stmt>, x: VarId) { |
| 237 | 568 | stmts.extend(drops); |
| 238 | 569 | } |
| 239 | 570 | |
| 240 | | −/// Whether a construction with at least `min_fields` fields is built |
| 241 | | −/// somewhere in `e`. |
| 242 | | −fn has_candidate(e: &Expr, min_fields: usize) -> bool { |
| 571 | +/// Whether a construction that fits is built somewhere in `e`. |
| 572 | +pub(crate) fn has_candidate(e: &Expr, fit: Fit) -> bool { |
| 243 | 573 | match e { |
| 244 | 574 | Expr::Let(_, value, rest, _) => { |
| 245 | | − fits(value, min_fields) |
| 246 | | − || matches!(&**value, Value::Block(inner) if has_candidate(inner, min_fields)) |
| 247 | | − || has_candidate(rest, min_fields) |
| 575 | + fit.fits(value) |
| 576 | + || matches!(&**value, Value::Block(inner) if has_candidate(inner, fit)) |
| 577 | + || has_candidate(rest, fit) |
| 248 | 578 | } |
| 249 | 579 | Expr::Dup(_, rest) | Expr::Drop(_, rest) | Expr::Release { rest, .. } => { |
| 250 | | − has_candidate(rest, min_fields) |
| 580 | + has_candidate(rest, fit) |
| 251 | 581 | } |
| 252 | 582 | Expr::Switch { cases, default, .. } => { |
| 253 | | − cases.iter().any(|(_, b)| has_candidate(b, min_fields)) |
| 254 | | − || default |
| 255 | | − .as_ref() |
| 256 | | − .is_some_and(|d| has_candidate(d, min_fields)) |
| 583 | + cases.iter().any(|(_, b)| has_candidate(b, fit)) |
| 584 | + || default.as_ref().is_some_and(|d| has_candidate(d, fit)) |
| 257 | 585 | } |
| 258 | 586 | Expr::SwitchLit { cases, default, .. } => { |
| 259 | | − cases.iter().any(|(_, b)| has_candidate(b, min_fields)) |
| 260 | | − || has_candidate(default, min_fields) |
| 587 | + cases.iter().any(|(_, b)| has_candidate(b, fit)) || has_candidate(default, fit) |
| 261 | 588 | } |
| 262 | 589 | Expr::Return(_) | Expr::Exit(_) | Expr::Panic(_) => false, |
| 263 | 590 | } |
| 264 | 591 | } |
| 265 | 592 | |
| 266 | | −fn fits(value: &Value, min_fields: usize) -> bool { |
| 267 | | − matches!(value, Value::Construct { args, .. } | Value::Tuple(args) |
| 268 | | − if !args.is_empty() && args.len() >= min_fields) |
| 269 | | −} |
| 270 | | − |
| 271 | | −/// Make the first suitable construction on each path of `e` reuse the |
| 272 | | −/// token `t`, and drop `t` on entry to the branches without one. `e` must |
| 273 | | −/// have one ([`has_candidate`]). |
| 274 | | −fn place(e: Expr, t: VarId, min_fields: usize) -> Expr { |
| 275 | | − let branch = |b: Expr| { |
| 276 | | − if has_candidate(&b, min_fields) { |
| 277 | | − place(b, t, min_fields) |
| 278 | | − } else { |
| 279 | | − Expr::Drop(t, Box::new(b)) |
| 280 | | − } |
| 281 | | − }; |
| 282 | | − match e { |
| 283 | | − Expr::Let(v, value, rest, span) => match *value { |
| 284 | | − value if fits(&value, min_fields) => Expr::Let( |
| 285 | | − v, |
| 286 | | − Box::new(Value::Reuse { |
| 287 | | − token: t, |
| 288 | | − value: Box::new(value), |
| 289 | | − }), |
| 290 | | − rest, |
| 291 | | − span, |
| 292 | | − ), |
| 293 | | − Value::Block(inner) if has_candidate(&inner, min_fields) => Expr::Let( |
| 294 | | − v, |
| 295 | | − Box::new(Value::Block(Box::new(place(*inner, t, min_fields)))), |
| 296 | | − rest, |
| 297 | | − span, |
| 298 | | − ), |
| 299 | | − // A block without a construction: the paths that leave the |
| 300 | | − // function from it drop the token first. |
| 301 | | − Value::Block(inner) => Expr::Let( |
| 302 | | − v, |
| 303 | | − Box::new(Value::Block(Box::new(drop_on_exit(*inner, t)))), |
| 304 | | − Box::new(place(*rest, t, min_fields)), |
| 305 | | − span, |
| 306 | | − ), |
| 307 | | − value => Expr::Let( |
| 308 | | − v, |
| 309 | | − Box::new(value), |
| 310 | | − Box::new(place(*rest, t, min_fields)), |
| 311 | | − span, |
| 312 | | − ), |
| 313 | | − }, |
| 314 | | − Expr::Dup(v, rest) => Expr::Dup(v, Box::new(place(*rest, t, min_fields))), |
| 315 | | − Expr::Drop(v, rest) => Expr::Drop(v, Box::new(place(*rest, t, min_fields))), |
| 316 | | − Expr::Release { |
| 317 | | − var, |
| 318 | | − fields, |
| 319 | | − token, |
| 320 | | − rest, |
| 321 | | − } => Expr::Release { |
| 322 | | − var, |
| 323 | | − fields, |
| 324 | | − token, |
| 325 | | − rest: Box::new(place(*rest, t, min_fields)), |
| 326 | | − }, |
| 327 | | − Expr::Switch { on, cases, default } => Expr::Switch { |
| 328 | | − on, |
| 329 | | − cases: cases.into_iter().map(|(c, b)| (c, branch(b))).collect(), |
| 330 | | − default: default.map(|d| Box::new(branch(*d))), |
| 331 | | − }, |
| 332 | | − Expr::SwitchLit { on, cases, default } => Expr::SwitchLit { |
| 333 | | − on, |
| 334 | | − cases: cases.into_iter().map(|(l, b)| (l, branch(b))).collect(), |
| 335 | | − default: Box::new(branch(*default)), |
| 336 | | − }, |
| 337 | | − Expr::Exit(_) | Expr::Return(_) | Expr::Panic(_) => { |
| 338 | | − unreachable!("no construction to reuse") |
| 339 | | − } |
| 340 | | − } |
| 341 | | −} |
| 342 | | − |
| 343 | 593 | /// `e` with the token `t` dropped before each `exit`. |
| 344 | 594 | fn drop_on_exit(e: Expr, t: VarId) -> Expr { |
| 345 | 595 | match e { |