Skip to main content

rucc_regalloc/
live.rs

1//! Where every value in a machine function is live.
2//!
3//! Design: `spec/10-backend.md` section 10.4.
4//!
5//! A register can be given to two values at once exactly when the two are never both wanted, so
6//! this is the question every allocator asks first and the one both of ours will read the answer
7//! to from here. It is asked of the machine IR while it is still in SSA form, which is what makes
8//! the answer cheap: a value is written once, so its live range is one interval from where it is
9//! written to the last place it is read, and there is no need to ask which of several definitions
10//! a use is reading from.
11//!
12//! # What the answer is
13//!
14//! A list of pieces per virtual register, one for each run of blocks the value is live over, and
15//! the interval around them for anyone who only wants to know where a value starts and stops.
16//!
17//! The pieces are what it takes to say that a value live in one loop and live again in a later one
18//! is not live in between. Both loops are in the same line of points, so an interval that covered
19//! them both would cover everything laid out between them and every value in there would look like
20//! it was competing for a register with one it never meets. Twelve such values in a row are twelve
21//! registers gone on a machine that has twelve, which is how a function using half the machine
22//! ended up spilling. tamnd/rucc#982.
23//!
24//! Being dead in a piece's hole means dead for good rather than dead for a while. A value is live
25//! in a block when a use of it can still be reached from there, so a block it is not live in is
26//! one that no execution reaching it ever reads the value again. That is what makes a hole safe to
27//! hand to somebody else without splitting anything: whoever gets the register in there is not
28//! borrowing it, and nothing has to be put back afterwards.
29//!
30//! Physical registers in the operands are not in the answer. Nothing writes one before allocation
31//! except an instruction that must, and what a call destroys is a separate question that the ABI
32//! lowering asks, so a pass that reads this is reading about the values the allocator places.
33//!
34//! # How it is computed
35//!
36//! Which values arrive live in each block and which leave live is found one value at a time, by
37//! walking backwards from the blocks that read it through their predecessors until a block that
38//! writes it. Backwards because liveness flows backwards, and a walk rather than one pass over the
39//! blocks because a loop carries a value from the end of a block round to a block in front of it.
40//! The pieces then come from one walk over the instructions, a block at a time.
41//!
42//! What each block arrives holding is kept as the register numbers rather than as a bit each, and
43//! `Rows` in this module says why. The short of it is that a block is live in a handful of values
44//! whatever the function has in it, so a bit per value per block is the size of the function
45//! squared for an answer that is not.
46//!
47//! Inside one block a value's live points are one stretch and never two, because the machine IR is
48//! in SSA form and a value is written once. The stretch runs from the start of the block if the
49//! value arrives live and from where it is written otherwise, and to the end of the block if it
50//! leaves live and to its last read otherwise. Two stretches join into one piece when the blocks
51//! they are in are next to each other in the line, which is what makes a value carried round a loop
52//! one piece over the whole loop rather than one per block in it.
53
54use rucc_mir::{Block, Func, Reg, Role};
55
56use crate::order::{Order, Point};
57
58/// The stretch of the function a value is live over.
59///
60/// Both ends are included: a value written at a point and read at a later one is live at both,
61/// and one written and never read is live where it was written, because the register it was
62/// written to is not free at the instant it was written to. A value written early is written
63/// before the instruction reads its operands and is still written when the instruction is done,
64/// so even one nothing reads covers the whole of the instruction that wrote it.
65#[derive(Debug, Clone, Copy, PartialEq, Eq)]
66pub struct Range {
67    /// Where the value is written.
68    pub start: Point,
69    /// The last place it is read, or where it is written if nothing reads it.
70    pub end: Point,
71}
72
73impl Range {
74    /// Whether the value is live at that point.
75    #[must_use]
76    pub fn covers(self, point: Point) -> bool {
77        self.start <= point && point <= self.end
78    }
79
80    /// Whether two values are both live anywhere, which is what stops them sharing a register.
81    #[must_use]
82    pub fn overlaps(self, other: Self) -> bool {
83        self.start <= other.end && other.start <= self.end
84    }
85
86    /// The smallest range covering both, which is how a range grows as more of the function is
87    /// read.
88    fn with(self, point: Point) -> Self {
89        Self { start: self.start.min(point), end: self.end.max(point) }
90    }
91}
92
93/// Everywhere one value is live, which is one or more pieces and at most one more point in front
94/// of the piece that follows it.
95///
96/// That one extra point is the only thing about a live area anybody adjusts. A value a two address
97/// instruction writes into a register it read is really live from where that instruction reads its
98/// operands, which is one point in front of where it is written, and both the allocator and the
99/// checker add that point before asking anything. It is one point rather than a new start because
100/// a value can be live in several pieces and the one to stretch is the piece the instruction
101/// writes, which is not always the first. Reading an area this way only ever makes it bigger, so
102/// it is still an area and every answer below still holds of it.
103#[derive(Debug, Clone, Copy)]
104pub struct Area<'a> {
105    pieces: &'a [Range],
106    also: Option<Point>,
107}
108
109impl<'a> Area<'a> {
110    /// The same area with one more point in it, joined to the piece that starts just after it.
111    ///
112    /// A point already inside a piece changes nothing, which is what a value a loop carries round
113    /// looks like: it is live on the way into the instruction that writes it anyway.
114    #[must_use]
115    pub fn with(self, point: Point) -> Self {
116        Self { also: Some(point), ..self }
117    }
118
119    /// The interval around the whole area, holes and all, which is what a sweep in the order
120    /// values start reads.
121    #[must_use]
122    pub fn hull(self) -> Range {
123        Range { start: self.piece(0).start, end: self.pieces[self.pieces.len() - 1].end }
124    }
125
126    /// Whether the value is live at that point.
127    ///
128    /// The pieces are in order and the extra point only moves a start down to just past the end
129    /// of the piece in front, so the first piece that ends at or after the point is the only one
130    /// that can hold it, and a search finds that one without looking at the rest.
131    #[must_use]
132    pub fn covers(self, point: Point) -> bool {
133        let first = self.pieces.partition_point(|piece| piece.end < point);
134        first < self.pieces.len() && self.piece(first).covers(point)
135    }
136
137    /// Whether two values are both live somewhere, which is what stops them sharing a register.
138    ///
139    /// Both lists are in order and neither is long, so this walks them together and stops at the
140    /// first pair that touches rather than comparing every piece with every other.
141    #[must_use]
142    pub fn overlaps(self, other: Self) -> bool {
143        let (mut mine, mut theirs) = (0, 0);
144        while mine < self.pieces.len() && theirs < other.pieces.len() {
145            let (one, two) = (self.piece(mine), other.piece(theirs));
146            if one.overlaps(two) {
147                return true;
148            }
149            // Whichever stops first cannot reach anything further along the other list.
150            if one.end < two.end {
151                mine += 1;
152            } else {
153                theirs += 1;
154            }
155        }
156        false
157    }
158
159    /// The pieces themselves, in order.
160    pub fn pieces(self) -> impl Iterator<Item = Range> + 'a {
161        (0..self.pieces.len()).map(move |piece| self.piece(piece))
162    }
163
164    /// One piece, stretched down over the extra point when that point is the one just in front of
165    /// it.
166    fn piece(self, index: usize) -> Range {
167        let piece = self.pieces[index];
168        match self.also {
169            Some(also) if also + 1 == piece.start => Range { start: also, end: piece.end },
170            _ => piece,
171        }
172    }
173}
174
175/// What is live where.
176#[derive(Debug, Clone)]
177pub struct Live {
178    live_in: Rows,
179    live_out: Rows,
180    /// Every value's pieces end to end, since a vector per value would be a vector per value.
181    pieces: Vec<Range>,
182    /// Where each value's pieces are in that vector, by register number.
183    spans: Vec<(usize, usize)>,
184}
185
186impl Live {
187    /// Works it out for a function laid out in that order.
188    #[must_use]
189    pub fn of(func: &Func, order: &Order) -> Self {
190        let vregs = func.vregs();
191        let (used, defined) = exposed(func, order);
192        let (live_in, live_out) = flow(func, order, &used, &defined);
193        let (pieces, spans) = carve(func, order, &live_in, &live_out, vregs);
194        Self { live_in, live_out, pieces, spans }
195    }
196
197    /// Everywhere a virtual register is live, or `None` for one this function never mentions and
198    /// for a physical register.
199    #[must_use]
200    pub fn area(&self, reg: Reg) -> Option<Area<'_>> {
201        let pieces = self.pieces(reg);
202        if pieces.is_empty() {
203            return None;
204        }
205        Some(Area { pieces, also: None })
206    }
207
208    /// The interval a virtual register is live over, holes and all.
209    #[must_use]
210    pub fn range(&self, reg: Reg) -> Option<Range> {
211        self.area(reg).map(Area::hull)
212    }
213
214    /// Every virtual register that arrives in a block already holding a value.
215    ///
216    /// The block's own parameters are not among them. A parameter is written where it arrives,
217    /// which makes it a value the block defines rather than one it inherits.
218    pub fn live_in(&self, block: Block) -> impl Iterator<Item = Reg> + '_ {
219        self.live_in.iter(block.index())
220    }
221
222    /// Every virtual register that is still wanted after a block, which is what its successors
223    /// and the arguments its terminator carries between them ask for.
224    pub fn live_out(&self, block: Block) -> impl Iterator<Item = Reg> + '_ {
225        self.live_out.iter(block.index())
226    }
227
228    /// Everywhere a virtual register is live, as it is stored.
229    fn pieces(&self, reg: Reg) -> &[Range] {
230        let number = reg.number().and_then(|number| usize::try_from(number).ok());
231        let Some(&(from, to)) = number.and_then(|number| self.spans.get(number)) else {
232            return &[];
233        };
234        &self.pieces[from..to]
235    }
236}
237
238/// The pieces, from the blocks and from the instructions in them.
239///
240/// One block at a time, because a value's live points inside one block are one stretch and the
241/// whole job is working out where one stretch stops and the next begins. What comes back is every
242/// value's pieces end to end, and where each value's are.
243fn carve(
244    func: &Func,
245    order: &Order,
246    live_in: &Rows,
247    live_out: &Rows,
248    vregs: usize,
249) -> (Vec<Range>, Vec<(usize, usize)>) {
250    let mut lists: Vec<Vec<Range>> = vec![Vec::new(); vregs];
251    let mut here: Vec<Option<Range>> = vec![None; vregs];
252    let mut touched: Vec<usize> = Vec::new();
253
254    for &block in order.blocks() {
255        // A block a value arrives in and leaves is one it is live through, whether or not
256        // anything in it says the value's name.
257        for reg in live_in.iter(block.index()) {
258            note(&mut here, &mut touched, reg, order.start(block));
259        }
260        for reg in live_out.iter(block.index()) {
261            note(&mut here, &mut touched, reg, order.end(block));
262        }
263        for param in &func[block].params {
264            note(&mut here, &mut touched, param.reg, order.start(block));
265        }
266        for inst in func.insts(block) {
267            for operand in &func[func[inst].operands] {
268                match operand.role {
269                    Role::Use => note(&mut here, &mut touched, operand.reg, order.early(inst)),
270                    Role::Def => note(&mut here, &mut touched, operand.reg, order.late(inst)),
271                    // A register written early is taken from before the operands are read, which
272                    // is the whole of what makes it different from a plain definition, and it is
273                    // still taken when the instruction is done. Both ends have to be said. Saying
274                    // only the first would leave a value nothing reads live at a point in front of
275                    // everything else the instruction writes, and the register it went to would
276                    // look free to them.
277                    Role::EarlyDef => {
278                        note(&mut here, &mut touched, operand.reg, order.early(inst));
279                        note(&mut here, &mut touched, operand.reg, order.late(inst));
280                    }
281                }
282            }
283        }
284        for call in &func[block].succs {
285            for &arg in &call.args {
286                note(&mut here, &mut touched, arg, order.end(block));
287            }
288        }
289
290        for &number in &touched {
291            let Some(piece) = here[number].take() else { continue };
292            match lists[number].last_mut() {
293                // The points run on from one block into the next, so a stretch that begins where
294                // the last one stopped is the same run of blocks carried on. A gap of even one
295                // point means a block in between that the value is not live in.
296                Some(last) if last.end + 1 == piece.start => last.end = piece.end,
297                _ => lists[number].push(piece),
298            }
299        }
300        touched.clear();
301    }
302
303    let mut pieces = Vec::new();
304    let mut spans = Vec::with_capacity(vregs);
305    for list in &lists {
306        let from = pieces.len();
307        pieces.extend_from_slice(list);
308        spans.push((from, pieces.len()));
309    }
310    (pieces, spans)
311}
312
313/// Says that a value is live at a point of the block being carved.
314fn note(here: &mut [Option<Range>], touched: &mut Vec<usize>, reg: Reg, point: Point) {
315    let Some(number) = reg.number().and_then(|number| usize::try_from(number).ok()) else {
316        return;
317    };
318    let Some(slot) = here.get_mut(number) else { return };
319    match slot {
320        Some(range) => *range = range.with(point),
321        None => {
322            *slot = Some(Range { start: point, end: point });
323            touched.push(number);
324        }
325    }
326}
327
328/// What each block reads before writing, and what it writes.
329///
330/// The first is read backwards, because a value a block writes and then reads is one it does not
331/// want from anybody, while one it reads and then writes is.
332fn exposed(func: &Func, order: &Order) -> (Rows, Rows) {
333    let vregs = func.vregs();
334    let mut used = Vec::new();
335    let mut defined = Vec::new();
336    let mut reads = Building::new(vregs);
337    let mut writes = Building::new(vregs);
338    for &block in order.blocks() {
339        let row = block.index();
340        for call in &func[block].succs {
341            for &arg in &call.args {
342                reads.insert(arg);
343            }
344        }
345        let insts: Vec<_> = func.insts(block).collect();
346        for &inst in insts.iter().rev() {
347            let operands = &func[func[inst].operands];
348            for operand in operands.iter().filter(|operand| operand.role.is_def()) {
349                reads.remove(operand.reg);
350                writes.insert(operand.reg);
351            }
352            for operand in operands.iter().filter(|operand| !operand.role.is_def()) {
353                reads.insert(operand.reg);
354            }
355        }
356        for param in &func[block].params {
357            reads.remove(param.reg);
358            writes.insert(param.reg);
359        }
360        used.extend(reads.take().into_iter().map(|number| (row_of(row), number)));
361        defined.extend(writes.take().into_iter().map(|number| (row_of(row), number)));
362    }
363    (Rows::gather(func.block_count(), &used), Rows::gather(func.block_count(), &defined))
364}
365
366/// The fixpoint: what arrives live in each block, and what leaves live.
367///
368/// One value at a time rather than one block at a time. A value is live into every block a read of
369/// it can be reached from without passing something that writes it, so starting from the blocks
370/// that read it first and walking back through their predecessors until a block that writes it
371/// finds exactly those, and it visits each block the value is live in once. What that costs is the
372/// size of the answer. The list of blocks it replaced looked at a block again whenever anything
373/// arriving live after it changed and merged whole rows each time, and on jtckdint's function of
374/// 22000 blocks, with a few thousand values live across most of it, that merging was half of the
375/// whole `-O2` compile.
376///
377/// The rows come out in order for free, because the values are walked in the order their numbers
378/// sort in and each is only ever added to the end of a row.
379fn flow(func: &Func, order: &Order, used: &Rows, defined: &Rows) -> (Rows, Rows) {
380    let count = func.block_count();
381    let vregs = func.vregs();
382    let mut preds: Vec<Vec<usize>> = vec![Vec::new(); count];
383    for &block in order.blocks() {
384        for call in &func[block].succs {
385            preds[call.block.index()].push(block.index());
386        }
387    }
388    // The blocks that read each value before writing it, by register number, end to end.
389    let mut starts = vec![0usize; vregs + 1];
390    for &block in order.blocks() {
391        for &number in used.row(block.index()) {
392            starts[number as usize + 1] += 1;
393        }
394    }
395    for number in 0..vregs {
396        starts[number + 1] += starts[number];
397    }
398    let mut readers = vec![0usize; starts[vregs]];
399    let mut filled = starts.clone();
400    for &block in order.blocks() {
401        for &number in used.row(block.index()) {
402            readers[filled[number as usize]] = block.index();
403            filled[number as usize] += 1;
404        }
405    }
406    // And the blocks that write each one, the same way, so that whether a block stops the walk is
407    // a mark made once per value rather than a search of the block's row for every edge crossed.
408    let mut ends = vec![0usize; vregs + 1];
409    for &block in order.blocks() {
410        for &number in defined.row(block.index()) {
411            ends[number as usize + 1] += 1;
412        }
413    }
414    for number in 0..vregs {
415        ends[number + 1] += ends[number];
416    }
417    let mut writers = vec![0usize; ends[vregs]];
418    let mut filled = ends.clone();
419    for &block in order.blocks() {
420        for &number in defined.row(block.index()) {
421            writers[filled[number as usize]] = block.index();
422            filled[number as usize] += 1;
423        }
424    }
425
426    let mut live_in = Vec::new();
427    let mut live_out = Vec::new();
428    // The last value each block was found live into and live out of, which is all a block needs
429    // to remember when the values come one at a time.
430    let mut arrived = vec![u32::MAX; count];
431    let mut left = vec![u32::MAX; count];
432    let mut wrote = vec![u32::MAX; count];
433    let mut waiting = Vec::new();
434    for number in 0..vregs {
435        let value = u32::try_from(number).expect("a register number");
436        for &row in &writers[ends[number]..ends[number + 1]] {
437            wrote[row] = value;
438        }
439        for &row in &readers[starts[number]..starts[number + 1]] {
440            if arrived[row] != value {
441                arrived[row] = value;
442                live_in.push((row_of(row), value));
443                waiting.push(row);
444            }
445        }
446        while let Some(row) = waiting.pop() {
447            for &pred in &preds[row] {
448                if left[pred] != value {
449                    left[pred] = value;
450                    live_out.push((row_of(pred), value));
451                }
452                if arrived[pred] != value && wrote[pred] != value {
453                    arrived[pred] = value;
454                    live_in.push((row_of(pred), value));
455                    waiting.push(pred);
456                }
457            }
458        }
459    }
460    (Rows::transpose(count, &live_in), Rows::transpose(count, &live_out))
461}
462
463/// A set of virtual registers for each block, held as the numbers in it.
464///
465/// A bit per register per block is the obvious way to hold this and is what it was. The trouble is
466/// that a row is then as wide as the function has values however few of them the block is about,
467/// and every step of the fixpoint reads and writes every word of every row. A function with a lot
468/// of values in it has a lot of blocks too, so that is the size of the function squared, in memory
469/// as well as in time: jtckdint from the real corpus has one function with 190084 instructions and
470/// 22000 blocks, and four of these rows came to about two gigabytes of the compiler's footprint,
471/// with the fixpoint over them taking a third of the whole compile at `-O1`.
472///
473/// What is actually true of the answer is that a block is live in a handful of values and not in
474/// the other two hundred thousand, so the numbers themselves are smaller than the bits. They are
475/// kept in the order a register number sorts in rather than any order of the program.
476/// tamnd/rucc#1072.
477///
478/// The rows are one list end to end, laid out once every number is known. They used to be a list
479/// per block that each number was pushed onto as the fixpoint found it, and on jtckdint, with
480/// thousands of values live across most of 22000 blocks, growing those lists one number at a time
481/// and copying them every time one ran out of room was about half of working out liveness.
482#[derive(Debug, Clone)]
483struct Rows {
484    /// Where each row starts in `numbers`, with one more at the end for where the last one stops.
485    starts: Vec<usize>,
486    numbers: Vec<u32>,
487}
488
489impl Rows {
490    /// The rows out of a list of which row each number goes in. The numbers keep the order they
491    /// come in within a row, so a row is in order when the caller hands its numbers over in order.
492    ///
493    /// This is for a list that comes a row at a time, which puts each row's numbers down together.
494    /// One that comes a value at a time wants [`Rows::transpose`].
495    fn gather(rows: usize, pairs: &[(u32, u32)]) -> Self {
496        let (starts, mut filled) = Self::starts(rows, pairs);
497        let mut numbers = vec![0u32; pairs.len()];
498        for &(row, number) in pairs {
499            let at = &mut filled[row as usize];
500            numbers[*at] = number;
501            *at += 1;
502        }
503        Self { starts, numbers }
504    }
505
506    /// The same rows as [`Rows::gather`] makes, out of a list that comes a value at a time.
507    ///
508    /// Putting each number straight into its row writes to every row by turns, and with 22000
509    /// rows that is 22000 places being written at once, far more than stay in the cache, so nearly
510    /// every write missed. So the list is first sorted into at most [`Rows::WAYS`] runs of
511    /// neighbouring rows, and then each run into its rows, which is two passes that each write to
512    /// few enough places at a time to stay in the cache. Both keep the order the pairs came in, so
513    /// the rows are the same.
514    fn transpose(rows: usize, pairs: &[(u32, u32)]) -> Self {
515        let mut shift = 0;
516        while rows > Self::WAYS << shift {
517            shift += 1;
518        }
519        if shift == 0 {
520            return Self::gather(rows, pairs);
521        }
522        let (starts, mut filled) = Self::starts(rows, pairs);
523        // Where each run starts in `runs`, which is where the first row in it starts in `numbers`.
524        let mut next: Vec<usize> =
525            (0..rows.div_ceil(1 << shift)).map(|run| starts[run << shift]).collect();
526        let mut runs = vec![(0u32, 0u32); pairs.len()];
527        for &pair in pairs {
528            let at = &mut next[(pair.0 >> shift) as usize];
529            runs[*at] = pair;
530            *at += 1;
531        }
532        let mut numbers = vec![0u32; pairs.len()];
533        for &(row, number) in &runs {
534            let at = &mut filled[row as usize];
535            numbers[*at] = number;
536            *at += 1;
537        }
538        Self { starts, numbers }
539    }
540
541    /// How many runs of rows [`Rows::transpose`] sorts a list into before the rows themselves.
542    const WAYS: usize = 256;
543
544    /// Where each row starts, with one more at the end for where the last one stops, and a copy
545    /// to count up as the rows are filled.
546    fn starts(rows: usize, pairs: &[(u32, u32)]) -> (Vec<usize>, Vec<usize>) {
547        let mut starts = vec![0usize; rows + 1];
548        for &(row, _) in pairs {
549            starts[row as usize + 1] += 1;
550        }
551        for row in 0..rows {
552            starts[row + 1] += starts[row];
553        }
554        let filled = starts.clone();
555        (starts, filled)
556    }
557
558    fn row(&self, row: usize) -> &[u32] {
559        &self.numbers[self.starts[row]..self.starts[row + 1]]
560    }
561
562    fn iter(&self, row: usize) -> impl Iterator<Item = Reg> + '_ {
563        self.row(row).iter().copied().map(Reg::virtual_reg)
564    }
565}
566
567/// A block's index as the row it is in [`Rows`].
568fn row_of(index: usize) -> u32 {
569    u32::try_from(index).expect("a block number")
570}
571
572/// One block's set while it is being worked out, as a flag per register and a list of which to
573/// look at.
574///
575/// A row is held as the numbers in it, so putting a register into one twice would be a search and
576/// a shift of everything above it, and taking one out again would be another. Here both are a load
577/// and a store. What makes it affordable is the clear: the flags are as many as the function has
578/// values and the blocks are as many as it has blocks, so clearing all of the first for each of
579/// the second would be the cost this whole representation is here to avoid, and instead only what
580/// was set is walked. tamnd/rucc#1072.
581struct Building {
582    flags: Vec<bool>,
583    /// Every number set since the last [`Building::take`], which may name one twice when a
584    /// register was taken out and put back. The take drops the repeat rather than the caller
585    /// having to care.
586    touched: Vec<u32>,
587}
588
589impl Building {
590    fn new(vregs: usize) -> Self {
591        Self { flags: vec![false; vregs], touched: Vec::new() }
592    }
593
594    /// The register's number, or nothing for a physical register, which this does not track, and
595    /// nothing for a number this function has no value at, which cannot happen and is not worth a
596    /// panic if it does.
597    fn number(&self, reg: Reg) -> Option<usize> {
598        let number = usize::try_from(reg.number()?).ok()?;
599        (number < self.flags.len()).then_some(number)
600    }
601
602    fn insert(&mut self, reg: Reg) {
603        let Some(number) = self.number(reg) else { return };
604        if !self.flags[number] {
605            self.flags[number] = true;
606            self.touched.push(u32::try_from(number).expect("a register number"));
607        }
608    }
609
610    fn remove(&mut self, reg: Reg) {
611        if let Some(number) = self.number(reg) {
612            self.flags[number] = false;
613        }
614    }
615
616    /// What is in the set, in order, leaving it empty for the next block.
617    fn take(&mut self) -> Vec<u32> {
618        let flags = &mut self.flags;
619        let mut out: Vec<u32> = self
620            .touched
621            .drain(..)
622            .filter(|&number| std::mem::replace(&mut flags[number as usize], false))
623            .collect();
624        out.sort_unstable();
625        out
626    }
627}
628
629#[cfg(test)]
630mod tests {
631    use rucc_base::Interner;
632    use rucc_mir::{BlockCall, Constraint, Opcode, Operand};
633    use rucc_target::x86_64::GPR;
634
635    use super::*;
636
637    /// The registers live in or out of a block, in order, which is what an assertion reads.
638    fn regs(of: impl Iterator<Item = Reg>) -> Vec<u32> {
639        of.filter_map(Reg::number).collect()
640    }
641
642    #[test]
643    fn a_value_is_live_from_where_it_is_written_to_where_it_is_last_read() {
644        let mut names = Interner::new();
645        let mut func = Func::new(names.intern("f"));
646        let opcode = Opcode::new(names.intern("x64.nop"));
647        let block = func.create_block();
648        let value = func.new_vreg(GPR);
649        let other = func.new_vreg(GPR);
650        let write = func.build(block, opcode).def(value, GPR).finish();
651        let idle = func.build(block, opcode).def(other, GPR).finish();
652        let read = func.build(block, opcode).uses(value, GPR).finish();
653
654        let order = Order::of(&func);
655        let live = Live::of(&func, &order);
656        let range = live.range(value).expect("the value is live somewhere");
657        assert_eq!(range, Range { start: order.late(write), end: order.early(read) });
658        assert!(range.covers(order.early(idle)));
659        // A value nothing reads is live where it was written and nowhere else, because the
660        // register it went to was not free at that instant either.
661        assert_eq!(
662            live.range(other),
663            Some(Range { start: order.late(idle), end: order.late(idle) })
664        );
665        assert!(!range.overlaps(Range { start: order.late(read), end: order.late(read) }));
666        assert_eq!(regs(live.live_in(block)), Vec::<u32>::new());
667    }
668
669    #[test]
670    fn a_value_read_in_another_block_is_live_between_them() {
671        let mut names = Interner::new();
672        let mut func = Func::new(names.intern("f"));
673        let opcode = Opcode::new(names.intern("x64.nop"));
674        let head = func.create_block();
675        let middle = func.create_block();
676        let tail = func.create_block();
677        let value = func.new_vreg(GPR);
678        func.build(head, opcode).def(value, GPR).finish();
679        *func.succs_mut(head) = vec![BlockCall::to(middle)];
680        *func.succs_mut(middle) = vec![BlockCall::to(tail)];
681        let read = func.build(tail, opcode).uses(value, GPR).finish();
682
683        let order = Order::of(&func);
684        let live = Live::of(&func, &order);
685        // The block in between never mentions it and it is live all the way through, which is
686        // the whole reason this walks the blocks and not only the code that names it.
687        assert_eq!(regs(live.live_in(middle)), vec![0]);
688        assert_eq!(regs(live.live_out(middle)), vec![0]);
689        assert!(live.range(value).expect("live somewhere").covers(order.start(middle)));
690        assert_eq!(live.range(value).expect("live somewhere").end, order.early(read));
691    }
692
693    #[test]
694    fn a_block_the_value_never_reaches_is_a_hole_between_two_pieces() {
695        let mut names = Interner::new();
696        let mut func = Func::new(names.intern("f"));
697        let opcode = Opcode::new(names.intern("x64.nop"));
698        let entry = func.create_block();
699        let arm = func.create_block();
700        let tail = func.create_block();
701        let value = func.new_vreg(GPR);
702        let write = func.build(entry, opcode).def(value, GPR).finish();
703        *func.succs_mut(entry) = vec![BlockCall::to(arm), BlockCall::to(tail)];
704        let idle = func.build(arm, opcode).finish();
705        let read = func.build(tail, opcode).uses(value, GPR).finish();
706
707        let order = Order::of(&func);
708        let live = Live::of(&func, &order);
709        let area = live.area(value).expect("live somewhere");
710        // The arm is written between the two blocks the value is live in, so the interval around
711        // it covers the arm and the pieces do not. Both are true and they answer different
712        // questions, and it is the pieces that decide who may have a register.
713        assert!(live.range(value).expect("live somewhere").covers(order.early(idle)));
714        assert!(!area.covers(order.early(idle)));
715        assert_eq!(
716            area.pieces().collect::<Vec<_>>(),
717            vec![
718                Range { start: order.late(write), end: order.end(entry) },
719                Range { start: order.start(tail), end: order.early(read) },
720            ]
721        );
722    }
723
724    #[test]
725    fn a_value_in_a_hole_of_another_may_have_its_register() {
726        let mut names = Interner::new();
727        let mut func = Func::new(names.intern("f"));
728        let opcode = Opcode::new(names.intern("x64.nop"));
729        let entry = func.create_block();
730        let arm = func.create_block();
731        let tail = func.create_block();
732        let value = func.new_vreg(GPR);
733        let inside = func.new_vreg(GPR);
734        func.build(entry, opcode).def(value, GPR).finish();
735        *func.succs_mut(entry) = vec![BlockCall::to(arm), BlockCall::to(tail)];
736        func.build(arm, opcode).def(inside, GPR).finish();
737        func.build(arm, opcode).uses(inside, GPR).finish();
738        func.build(tail, opcode).uses(value, GPR).finish();
739
740        let order = Order::of(&func);
741        let live = Live::of(&func, &order);
742        let value = live.area(value).expect("live somewhere");
743        let inside = live.area(inside).expect("live somewhere");
744        // Nothing in the arm can reach the read in the tail, so whichever register the first value
745        // is in is a register the arm may take for as long as it likes. The intervals say the two
746        // are on top of each other and they are not.
747        assert!(value.hull().overlaps(inside.hull()));
748        assert!(!value.overlaps(inside));
749        assert!(!inside.overlaps(value));
750    }
751
752    #[test]
753    fn one_point_added_in_front_of_a_piece_is_part_of_the_area() {
754        let mut names = Interner::new();
755        let mut func = Func::new(names.intern("f"));
756        let opcode = Opcode::new(names.intern("x64.nop"));
757        let block = func.create_block();
758        let first = func.new_vreg(GPR);
759        let second = func.new_vreg(GPR);
760        let write = func.build(block, opcode).def(first, GPR).finish();
761        let both = func.build(block, opcode).def(second, GPR).uses(first, GPR).finish();
762
763        let order = Order::of(&func);
764        let live = Live::of(&func, &order);
765        let first = live.area(first).expect("live somewhere");
766        let second = live.area(second).expect("live somewhere");
767        // A two address instruction writes its answer into the register it read, so the answer is
768        // really in that register from the moment the instruction starts. Read that way the two
769        // values are on top of each other, and read the plain way they are not, which is the whole
770        // reason the extra point is the caller's to add.
771        assert!(!first.overlaps(second));
772        assert!(first.overlaps(second.with(order.early(both))));
773        assert!(second.with(order.early(both)).covers(order.early(both)));
774        assert_eq!(second.with(order.early(both)).hull().start, order.early(both));
775        assert_eq!(first.hull().start, order.late(write));
776    }
777
778    #[test]
779    fn the_point_added_in_front_joins_the_piece_it_belongs_to_and_not_the_first_one() {
780        let mut names = Interner::new();
781        let mut func = Func::new(names.intern("f"));
782        let nop = Opcode::new(names.intern("x64.nop"));
783        let add = Opcode::new(names.intern("x64.add"));
784        let entry = func.create_block();
785        let head = func.create_block();
786        let arm = func.create_block();
787        let latch = func.create_block();
788        let out = func.create_block();
789        let seed = func.new_vreg(GPR);
790        let sum = func.new_vreg(GPR);
791        let inside = func.new_vreg(GPR);
792        let loaded = func.new_vreg(GPR);
793        func.build(entry, nop).def(seed, GPR).finish();
794        func.build(entry, nop).def(sum, GPR).finish();
795        *func.succs_mut(entry) = vec![BlockCall::to(head)];
796        func.build(head, nop).uses(sum, GPR).finish();
797        *func.succs_mut(head) = vec![BlockCall::to(arm), BlockCall::to(latch)];
798        func.build(arm, nop).def(inside, GPR).finish();
799        func.build(arm, nop).uses(inside, GPR).finish();
800        *func.succs_mut(arm) = vec![BlockCall::to(out)];
801        func.build(latch, nop).def(loaded, GPR).finish();
802        let carry = func
803            .build(latch, add)
804            .operand(Operand::write(sum, GPR).with(Constraint::Reuse(1)))
805            .uses(seed, GPR)
806            .uses(loaded, GPR)
807            .finish();
808        *func.succs_mut(latch) = vec![BlockCall::to(head), BlockCall::to(out)];
809
810        let order = Order::of(&func);
811        let live = Live::of(&func, &order);
812        let sum = live.area(sum).expect("live somewhere");
813        let loaded = live.area(loaded).expect("live somewhere");
814        // The answer is live in the entry and the head as well, which the arm is a hole in, so the
815        // piece the addition writes is the second one. Adding the point in front of the first piece
816        // instead would leave the addition reading a register the answer is about to be written to
817        // and nothing saying the two are on top of each other. tamnd/rucc#982.
818        assert_eq!(sum.pieces().count(), 2);
819        assert!(!sum.covers(order.early(carry)));
820        assert!(sum.with(order.early(carry)).covers(order.early(carry)));
821        assert!(!loaded.overlaps(sum));
822        assert!(loaded.overlaps(sum.with(order.early(carry))));
823    }
824
825    #[test]
826    fn a_value_carried_round_a_loop_is_live_round_all_of_it() {
827        let mut names = Interner::new();
828        let mut func = Func::new(names.intern("f"));
829        let opcode = Opcode::new(names.intern("x64.nop"));
830        let header = func.create_block();
831        let body = func.create_block();
832        let carried = func.append_param(header, GPR);
833        let next = func.new_vreg(GPR);
834        *func.succs_mut(header) = vec![BlockCall::to(body)];
835        func.build(body, opcode).def(next, GPR).uses(carried, GPR).finish();
836        *func.succs_mut(body) = vec![BlockCall::with(header, vec![next])];
837
838        let order = Order::of(&func);
839        let live = Live::of(&func, &order);
840        // The parameter arrives in the header, so the header does not want it from anybody, and
841        // the body does.
842        assert_eq!(regs(live.live_in(header)), Vec::<u32>::new());
843        assert_eq!(regs(live.live_in(body)), vec![carried.number().expect("virtual")]);
844        let range = live.range(next).expect("live somewhere");
845        assert_eq!(range.end, order.end(body));
846    }
847
848    #[test]
849    fn two_values_that_are_never_both_wanted_do_not_overlap() {
850        let mut names = Interner::new();
851        let mut func = Func::new(names.intern("f"));
852        let opcode = Opcode::new(names.intern("x64.nop"));
853        let block = func.create_block();
854        let first = func.new_vreg(GPR);
855        let second = func.new_vreg(GPR);
856        let write = func.build(block, opcode).def(first, GPR).finish();
857        func.build(block, opcode).def(second, GPR).uses(first, GPR).finish();
858
859        let order = Order::of(&func);
860        let live = Live::of(&func, &order);
861        let first = live.range(first).expect("live somewhere");
862        let second = live.range(second).expect("live somewhere");
863        // The second instruction reads the first value and writes its own, and it reads before
864        // it writes, so the two can be the same register. That is what a two address instruction
865        // needs to be true and it is a fact about the points rather than about the opcode.
866        assert!(!first.overlaps(second));
867        assert!(first.start > order.start(block));
868        assert_eq!(first.start, order.late(write));
869    }
870
871    #[test]
872    fn an_operand_written_early_is_wanted_where_the_operands_are_read() {
873        let mut names = Interner::new();
874        let mut func = Func::new(names.intern("f"));
875        let opcode = Opcode::new(names.intern("x64.nop"));
876        let block = func.create_block();
877        let source = func.new_vreg(GPR);
878        let early = func.new_vreg(GPR);
879        func.build(block, opcode).def(source, GPR).finish();
880        func.build(block, opcode)
881            .operand(Operand::write_early(early, GPR))
882            .operand(Operand::read(source, GPR))
883            .finish();
884
885        let order = Order::of(&func);
886        let live = Live::of(&func, &order);
887        let source = live.range(source).expect("live somewhere");
888        let early = live.range(early).expect("live somewhere");
889        // This is the difference between a division and an addition. The register the answer is
890        // going to is destroyed before the divisor is read, so the divisor may not be in it.
891        assert!(source.overlaps(early));
892    }
893
894    #[test]
895    fn a_register_a_memory_operand_names_is_read_like_any_other() {
896        use rucc_mir::Mem;
897
898        let mut names = Interner::new();
899        let mut func = Func::new(names.intern("f"));
900        let opcode = Opcode::new(names.intern("x64.nop"));
901        let block = func.create_block();
902        let address = func.new_vreg(GPR);
903        let write = func.build(block, opcode).def(address, GPR).finish();
904        let load = func.build(block, opcode).mem(Mem::at(Operand::read(address, GPR))).finish();
905
906        let order = Order::of(&func);
907        let live = Live::of(&func, &order);
908        assert_eq!(
909            live.range(address),
910            Some(Range { start: order.late(write), end: order.early(load) })
911        );
912    }
913
914    #[test]
915    fn rows_from_a_list_a_value_at_a_time_over_many_blocks_keep_the_order_the_numbers_came_in() {
916        // Far more rows than one pass of runs covers, so the list goes through both passes, and
917        // pairs that jump about the way the fixpoint's do.
918        let rows = 3000;
919        let mut pairs = Vec::new();
920        let mut seed = 7u32;
921        for number in 0..400 {
922            for _ in 0..30 {
923                seed = seed.wrapping_mul(1_103_515_245).wrapping_add(12345);
924                pairs.push(((seed >> 8) % 3000, number));
925            }
926        }
927        let gathered = Rows::transpose(rows, &pairs);
928        for row in 0..rows {
929            let wanted: Vec<u32> = pairs
930                .iter()
931                .filter(|&&(at, _)| at as usize == row)
932                .map(|&(_, number)| number)
933                .collect();
934            assert_eq!(gathered.row(row), wanted, "row {row}");
935        }
936    }
937}