rust/compiler/rustc_mir_transform/src/coverage
Matthias Krüger e0d922842d
Rollup merge of #125106 - Zalathar:expressions, r=davidtwco
coverage: Memoize and simplify counter expressions

When creating coverage counter expressions as part of coverage instrumentation, we often end up creating obviously-redundant expressions like `c1 + (c0 - c1)`, which is equivalent to just `c0`.

To avoid doing so, this PR checks when we would create an expression matching one of 5 patterns, and uses the simplified form instead:
- `(a - b) + b` → `a`.
- `(a + b) - b` → `a`.
- `(a + b) - a` → `b`.
- `a + (b - a)` → `b`.
- `a - (a - b)` → `b`.

Of all the different ways to combine 3 operands and 2 operators, these are the patterns that allow simplification.

(Some of those patterns currently don't occur in practice, but are included anyway for completeness, to avoid having to add them later as branch coverage and MC/DC coverage support expands.)

---

This PR also adds memoization for newly-created (or newly-simplified) counter expressions, to avoid creating duplicates.

This currently makes no difference to the final mappings, but is expected to be useful for MC/DC coverage of match expressions, as proposed by https://github.com/rust-lang/rust/pull/124278#issuecomment-2106754753.
2024-05-20 18:13:47 +02:00
..
spans Remove extern crate rustc_middle from rustc_mir_transform. 2024-05-13 08:20:18 +10:00
counters.rs coverage: Simplify counter expressions using simple algebra 2024-05-14 13:58:40 +10:00
graph.rs Remove extern crate rustc_middle from rustc_mir_transform. 2024-05-13 08:20:18 +10:00
mappings.rs coverage: Rename CoverageSpans to ExtractedMappings 2024-05-06 12:13:30 +10:00
mod.rs coverage: Tidy imports in rustc_mir_transform::coverage 2024-05-06 12:13:31 +10:00
query.rs coverage: CoverageIdsInfo::mcdc_bitmap_bytes is never needed 2024-05-14 16:41:04 +10:00
spans.rs Remove extern crate rustc_middle from rustc_mir_transform. 2024-05-13 08:20:18 +10:00
tests.rs Remove extern crate rustc_middle from rustc_mir_transform. 2024-05-13 08:20:18 +10:00