| @@ -152,6 +152,10 @@ impl Range { |
| 152 | 152 | /// The proven ranges of a program: every variable's, per function. |
| 153 | 153 | pub(crate) struct Ranges { |
| 154 | 154 | vars: Vec<Vec<Range>>, |
| 155 | + /// The join of each variable's range at each use as an operand of an |
| 156 | + /// add/sub/mul, per function: use sites see the arm refinements (a |
| 157 | + /// guard's bound on a counter), which the binding site does not. |
| 158 | + arith_uses: Vec<HashMap<VarId, Range>>, |
| 155 | 159 | } |
| 156 | 160 | |
| 157 | 161 | impl Ranges { |
| @@ -161,7 +165,7 @@ impl Ranges { |
| 161 | 165 | let (Some(a), Some(b)) = (args.first(), args.get(1)) else { |
| 162 | 166 | return None; |
| 163 | 167 | }; |
| 164 | | − let (a, b) = (self.atom(f, a), self.atom(f, b)); |
| 168 | + let (a, b) = (self.use_(f, a), self.use_(f, b)); |
| 165 | 169 | match sym { |
| 166 | 170 | "tl_int_add" if a.add_safe(b) => Some("tl_int_add_unchecked"), |
| 167 | 171 | "tl_int_sub" if a.add_safe(b) => Some("tl_int_sub_unchecked"), |
| @@ -177,10 +181,34 @@ impl Ranges { |
| 177 | 181 | matches!(self.atom(f, a), Range::Small(..)) |
| 178 | 182 | } |
| 179 | 183 | |
| 184 | + /// Whether `a` is proven a small integer in `f` (at its uses in |
| 185 | + /// arithmetic, where guards have refined it). |
| 186 | + pub(crate) fn small(&self, f: FnId, a: &Atom) -> bool { |
| 187 | + matches!(self.use_(f, a), Range::Small(..)) |
| 188 | + } |
| 189 | + |
| 190 | + /// The range of an operand at its arithmetic uses, falling back to |
| 191 | + /// its binding's. |
| 192 | + fn use_(&self, f: FnId, a: &Atom) -> Range { |
| 193 | + match a { |
| 194 | + Atom::Lit(Lit::Int(n)) => Range::of(*n), |
| 195 | + Atom::Var(v) => self |
| 196 | + .arith_uses |
| 197 | + .get(f as usize) |
| 198 | + .and_then(|uses| uses.get(v)) |
| 199 | + .copied() |
| 200 | + .unwrap_or_else(|| self.atom(f, a)), |
| 201 | + _ => Range::Big, |
| 202 | + } |
| 203 | + } |
| 204 | + |
| 180 | 205 | /// The range analysis that proves nothing (the `LOUISE_NO_FAST_INTS` |
| 181 | 206 | /// build). |
| 182 | 207 | pub(crate) fn empty() -> Ranges { |
| 183 | | − Ranges { vars: Vec::new() } |
| 208 | + Ranges { |
| 209 | + vars: Vec::new(), |
| 210 | + arith_uses: Vec::new(), |
| 211 | + } |
| 184 | 212 | } |
| 185 | 213 | |
| 186 | 214 | fn atom(&self, f: FnId, a: &Atom) -> Range { |
| @@ -250,19 +278,21 @@ pub(crate) fn analyse(program: &Program, entries: &[FnId], reachable: &BTreeSet< |
| 250 | 278 | } |
| 251 | 279 | } |
| 252 | 280 | // The locals, with the parameters' final ranges. |
| 281 | + let mut arith_uses: Vec<HashMap<VarId, Range>> = (0..program.fns.len()).map(|_| HashMap::new()).collect(); |
| 253 | 282 | let vars = (0..program.fns.len() as FnId) |
| 254 | 283 | .map(|f| { |
| 255 | 284 | let decl = &program.fns[f as usize]; |
| 256 | 285 | if reachable.contains(&f) { |
| 257 | 286 | let mut local = Local::new(decl, ¶ms, f); |
| 258 | 287 | local.walk(&decl.body); |
| 288 | + arith_uses[f as usize] = std::mem::take(&mut local.arith_uses); |
| 259 | 289 | local.env |
| 260 | 290 | } else { |
| 261 | 291 | vec![Range::Big; decl.n_vars as usize] |
| 262 | 292 | } |
| 263 | 293 | }) |
| 264 | 294 | .collect(); |
| 265 | | − Ranges { vars } |
| 295 | + Ranges { vars, arith_uses } |
| 266 | 296 | } |
| 267 | 297 | |
| 268 | 298 | /// Snap a growing bound to the next widening threshold, or `Big` past |
| @@ -348,10 +378,18 @@ struct Local { |
| 348 | 378 | env: Vec<Range>, |
| 349 | 379 | /// The call sites: callee, then its arguments' ranges. |
| 350 | 380 | sites: Vec<(FnId, Vec<Range>)>, |
| 381 | + /// The join of each variable's range at its uses as an operand of an |
| 382 | + /// add/sub/mul: the use sees the switch arm's refinement, which the |
| 383 | + /// binding's range does not. |
| 384 | + arith_uses: HashMap<VarId, Range>, |
| 351 | 385 | /// `let c = tl_int_compare(x, y)` (operands may be literals). |
| 352 | 386 | compares: HashMap<VarId, (Atom, Atom)>, |
| 387 | + /// `let e = tl_int_eq(x, y)`. |
| 388 | + eqs: HashMap<VarId, (Atom, Atom)>, |
| 353 | 389 | /// `let b = TagIs(t)(c)`. |
| 354 | 390 | tag_is: HashMap<VarId, (u32, VarId)>, |
| 391 | + /// `let b = Not(c)`. |
| 392 | + nots: HashMap<VarId, VarId>, |
| 355 | 393 | } |
| 356 | 394 | |
| 357 | 395 | impl Local { |
| @@ -366,8 +404,11 @@ impl Local { |
| 366 | 404 | f, |
| 367 | 405 | env, |
| 368 | 406 | sites: Vec::new(), |
| 407 | + arith_uses: HashMap::new(), |
| 369 | 408 | compares: HashMap::new(), |
| 409 | + eqs: HashMap::new(), |
| 370 | 410 | tag_is: HashMap::new(), |
| 411 | + nots: HashMap::new(), |
| 371 | 412 | } |
| 372 | 413 | } |
| 373 | 414 | |
| @@ -378,13 +419,22 @@ impl Local { |
| 378 | 419 | f: self.f, |
| 379 | 420 | env: self.env.clone(), |
| 380 | 421 | sites: Vec::new(), |
| 422 | + arith_uses: HashMap::new(), |
| 381 | 423 | compares: self.compares.clone(), |
| 424 | + eqs: self.eqs.clone(), |
| 382 | 425 | tag_is: self.tag_is.clone(), |
| 426 | + nots: self.nots.clone(), |
| 383 | 427 | } |
| 384 | 428 | } |
| 385 | 429 | |
| 386 | 430 | fn absorb(&mut self, arm: Local) { |
| 387 | 431 | self.sites.extend(arm.sites); |
| 432 | + for (v, r) in arm.arith_uses { |
| 433 | + self.arith_uses |
| 434 | + .entry(v) |
| 435 | + .and_modify(|e| *e = e.join(r)) |
| 436 | + .or_insert(r); |
| 437 | + } |
| 388 | 438 | } |
| 389 | 439 | |
| 390 | 440 | fn atom(&self, a: &Atom) -> Range { |
| @@ -403,9 +453,26 @@ impl Local { |
| 403 | 453 | let a = args.first().map(|a| self.atom(a)).unwrap_or(Range::Big); |
| 404 | 454 | let b = args.get(1).map(|b| self.atom(b)).unwrap_or(Range::Big); |
| 405 | 455 | match sym.as_str() { |
| 406 | | − "tl_int_add" => a.add(b), |
| 407 | | − "tl_int_sub" => a.sub(b), |
| 408 | | − "tl_int_mul" => a.mul(b), |
| 456 | + "tl_int_add" | "tl_int_sub" | "tl_int_mul" => { |
| 457 | + // The use site's ranges (a guard's arm refines |
| 458 | + // them), for `Ranges::unchecked`. |
| 459 | + for atom in args.iter().take(2) { |
| 460 | + if let Atom::Var(v) = atom { |
| 461 | + let r = self.atom(atom); |
| 462 | + self.arith_uses |
| 463 | + .entry(*v) |
| 464 | + .and_modify(|e| *e = e.join(r)) |
| 465 | + .or_insert(r); |
| 466 | + } |
| 467 | + } |
| 468 | + if sym == "tl_int_add" { |
| 469 | + a.add(b) |
| 470 | + } else if sym == "tl_int_sub" { |
| 471 | + a.sub(b) |
| 472 | + } else { |
| 473 | + a.mul(b) |
| 474 | + } |
| 475 | + } |
| 409 | 476 | "tl_int_div" | "tl_int_rem" => a.shrink(), |
| 410 | 477 | "tl_int_eq" => Range::Small(0, 1), |
| 411 | 478 | "tl_int_compare" => Range::Small(0, 2), |
| @@ -440,18 +507,31 @@ impl Local { |
| 440 | 507 | Expr::Let(v, value, rest, _) => { |
| 441 | 508 | let r = self.value(value); |
| 442 | 509 | // Guard chains: `let c = compare(x, y)`, then |
| 443 | | − // `let b = TagIs(t)(c)`, then `switch b`. |
| 510 | + // `let b = TagIs(t)(c)` (maybe `let n = Not(b)`), |
| 511 | + // then `switch b` — or a `switch` on the comparison |
| 512 | + // itself (fusion's counting loop). |
| 444 | 513 | match &**value { |
| 445 | | − Value::CallExtern(sym, args) |
| 446 | | − if sym == "tl_int_compare" && args.len() == 2 => |
| 447 | | − { |
| 448 | | − self.compares.insert(*v, (args[0].clone(), args[1].clone())); |
| 514 | + Value::CallExtern(sym, args) if args.len() == 2 => { |
| 515 | + match sym.as_str() { |
| 516 | + "tl_int_compare" => { |
| 517 | + self.compares.insert(*v, (args[0].clone(), args[1].clone())); |
| 518 | + } |
| 519 | + "tl_int_eq" => { |
| 520 | + self.eqs.insert(*v, (args[0].clone(), args[1].clone())); |
| 521 | + } |
| 522 | + _ => {} |
| 523 | + } |
| 449 | 524 | } |
| 450 | 525 | Value::Prim(Prim::TagIs(t), args) => { |
| 451 | 526 | if let Some(Atom::Var(c)) = args.first() { |
| 452 | 527 | self.tag_is.insert(*v, (*t, *c)); |
| 453 | 528 | } |
| 454 | 529 | } |
| 530 | + Value::Prim(Prim::Not, args) => { |
| 531 | + if let Some(Atom::Var(c)) = args.first() { |
| 532 | + self.nots.insert(*v, *c); |
| 533 | + } |
| 534 | + } |
| 455 | 535 | _ => {} |
| 456 | 536 | } |
| 457 | 537 | self.env[*v as usize] = r; |
| @@ -460,20 +540,23 @@ impl Local { |
| 460 | 540 | Expr::Switch { |
| 461 | 541 | on, cases, default, .. |
| 462 | 542 | } => { |
| 463 | | − // The arm tagged 1 is the condition holding; the |
| 464 | | − // default, after tag 1 was listed, its not holding. |
| 465 | | − let true_arm = cases.iter().any(|(t, _)| *t == 1); |
| 543 | + // An arm's refinement is keyed by the tags it covers: |
| 544 | + // a case its own, the default the rest. |
| 545 | + let mut listed = 0u32; |
| 546 | + for (t, _) in cases { |
| 547 | + listed |= 1 << t; |
| 548 | + } |
| 466 | 549 | let mut out = Range::Bottom; |
| 467 | 550 | for (t, b) in cases { |
| 468 | 551 | let mut arm = self.fork(); |
| 469 | | − arm.refine(on, *t == 1); |
| 552 | + arm.refine(on, 1 << t); |
| 470 | 553 | let r = arm.walk(b); |
| 471 | 554 | out = out.join(r); |
| 472 | 555 | self.absorb(arm); |
| 473 | 556 | } |
| 474 | 557 | if let Some(d) = default { |
| 475 | 558 | let mut arm = self.fork(); |
| 476 | | − arm.refine(on, !true_arm); |
| 559 | + arm.refine(on, !listed); |
| 477 | 560 | let r = arm.walk(d); |
| 478 | 561 | out = out.join(r); |
| 479 | 562 | self.absorb(arm); |
| @@ -510,30 +593,19 @@ impl Local { |
| 510 | 593 | } |
| 511 | 594 | } |
| 512 | 595 | |
| 513 | | − /// Refine the environment for a `switch` arm: `on` is |
| 514 | | − /// `TagIs(t)(compare(x, y))`; `matched` is whether the arm is the |
| 515 | | − /// one where the condition holds. A literal side refines the other. |
| 516 | | − fn refine(&mut self, on: &Atom, matched: bool) { |
| 517 | | − let Atom::Var(b) = on else { return }; |
| 518 | | − let Some(&(t, c)) = self.tag_is.get(b) else { |
| 519 | | − return; |
| 520 | | − }; |
| 521 | | − let Some((x, y)) = self.compares.get(&c).cloned() else { |
| 596 | + /// Refine the environment for a `switch` arm covering the tags in |
| 597 | + /// `covers` (bit per tag). The scrutinee may be a comparison's |
| 598 | + /// result directly (`tl_int_compare`'s `Order`, tags 0/1/2, or |
| 599 | + /// `tl_int_eq`'s Bool) or a `TagIs`/`Not` chain over one. A literal |
| 600 | + /// side refines the other. |
| 601 | + fn refine(&mut self, on: &Atom, covers: u32) { |
| 602 | + let Some((cond, x, y)) = self.condition(on, covers) else { |
| 522 | 603 | return; |
| 523 | 604 | }; |
| 524 | 605 | let (Range::Small(xlo, xhi), Range::Small(ylo, yhi)) = (self.atom(&x), self.atom(&y)) |
| 525 | 606 | else { |
| 526 | 607 | return; |
| 527 | 608 | }; |
| 528 | | − // tl_int_compare's results: 0 is Lt, 1 is Eq, 2 is Gt. |
| 529 | | − let cond = match (t, matched) { |
| 530 | | − (2, true) => Cmp::Gt, |
| 531 | | − (2, false) => Cmp::Le, |
| 532 | | − (0, true) => Cmp::Lt, |
| 533 | | − (0, false) => Cmp::Ge, |
| 534 | | − (1, true) => Cmp::Eq, |
| 535 | | − _ => return, // `!=` refines nothing on an interval |
| 536 | | − }; |
| 537 | 609 | let (nx, ny) = cond.refine(xlo, xhi, ylo, yhi); |
| 538 | 610 | if let Atom::Var(xv) = x { |
| 539 | 611 | self.env[xv as usize] = self.env[xv as usize].meet(nx.0, nx.1); |
| @@ -542,6 +614,54 @@ impl Local { |
| 542 | 614 | self.env[yv as usize] = self.env[yv as usize].meet(ny.0, ny.1); |
| 543 | 615 | } |
| 544 | 616 | } |
| 617 | + |
| 618 | + /// The comparison a switch arm's tags amount to, over the compared |
| 619 | + /// operands. `covers` is the arm's tag set, a bit per tag. |
| 620 | + fn condition(&self, on: &Atom, covers: u32) -> Option<(Cmp, Atom, Atom)> { |
| 621 | + let Atom::Var(b) = on else { return None }; |
| 622 | + // A switch on the `Order` itself: Lt, Eq, Gt are tags 0, 1, 2. |
| 623 | + if let Some((x, y)) = self.compares.get(b) { |
| 624 | + let cond = match covers & 0b111 { |
| 625 | + 0b001 => Cmp::Lt, |
| 626 | + 0b010 => Cmp::Eq, |
| 627 | + 0b100 => Cmp::Gt, |
| 628 | + 0b011 => Cmp::Le, |
| 629 | + 0b110 => Cmp::Ge, |
| 630 | + _ => return None, // `!=`, everything, nothing: no interval |
| 631 | + }; |
| 632 | + return Some((cond, x.clone(), y.clone())); |
| 633 | + } |
| 634 | + // A switch on `tl_int_eq`'s Bool: only the equal arm refines. |
| 635 | + if let Some((x, y)) = self.eqs.get(b) { |
| 636 | + return match covers & 0b11 { |
| 637 | + 0b10 => Some((Cmp::Eq, x.clone(), y.clone())), |
| 638 | + _ => None, |
| 639 | + }; |
| 640 | + } |
| 641 | + // A switch on `Not(c)`: the negation of c's condition. |
| 642 | + if let Some(&c) = self.nots.get(b) { |
| 643 | + let (cond, x, y) = self.condition(&Atom::Var(c), covers)?; |
| 644 | + return Some((cond.negate()?, x, y)); |
| 645 | + } |
| 646 | + // A switch on `TagIs(t)(c)`: the arm covering tag 1 (and not 0) |
| 647 | + // is where tag `t` holds; the one covering 0 and not 1, where it |
| 648 | + // does not. |
| 649 | + if let Some(&(t, c)) = self.tag_is.get(b) { |
| 650 | + let cond = match t { |
| 651 | + 0 => Cmp::Lt, |
| 652 | + 1 => Cmp::Eq, |
| 653 | + 2 => Cmp::Gt, |
| 654 | + _ => return None, |
| 655 | + }; |
| 656 | + let (x, y) = self.compares.get(&c)?.clone(); |
| 657 | + return match covers & 0b11 { |
| 658 | + 0b10 => Some((cond, x, y)), |
| 659 | + 0b01 => cond.negate().map(|n| (n, x, y)), |
| 660 | + _ => None, |
| 661 | + }; |
| 662 | + } |
| 663 | + None |
| 664 | + } |
| 545 | 665 | } |
| 546 | 666 | |
| 547 | 667 | /// A comparison's condition on `x` against `y`. |
| @@ -554,6 +674,18 @@ enum Cmp { |
| 554 | 674 | } |
| 555 | 675 | |
| 556 | 676 | impl Cmp { |
| 677 | + /// The condition of the arm where this one does not hold. `Eq`'s |
| 678 | + /// negation (`!=`) refines no interval. |
| 679 | + fn negate(self) -> Option<Cmp> { |
| 680 | + match self { |
| 681 | + Cmp::Gt => Some(Cmp::Le), |
| 682 | + Cmp::Le => Some(Cmp::Gt), |
| 683 | + Cmp::Lt => Some(Cmp::Ge), |
| 684 | + Cmp::Ge => Some(Cmp::Lt), |
| 685 | + Cmp::Eq => None, |
| 686 | + } |
| 687 | + } |
| 688 | + |
| 557 | 689 | /// The intervals of x and y in an arm where the condition holds. |
| 558 | 690 | fn refine(self, xlo: i64, xhi: i64, ylo: i64, yhi: i64) -> ((i64, i64), (i64, i64)) { |
| 559 | 691 | match self { |