Skip to main content

rucc_regalloc/
check.rs

1//! The allocation checker: whether an assignment is one the machine can actually run.
2//!
3//! Design: `spec/10-backend.md` section 10.4, which asks for this in debug and CI builds.
4//!
5//! A register allocator is the pass whose bugs are hardest to find from the outside. It does not
6//! change what a program means, so a wrong allocation compiles, links and runs, and then produces
7//! the wrong number in one function of one program under one register pressure. The stack trace
8//! points at the arithmetic, the arithmetic is right, and the value it read was overwritten four
9//! instructions earlier by something unrelated. A checker turns all of that into an assertion at
10//! the point the mistake was made, naming the two values and the register they were both put in.
11//!
12//! # What it asks
13//!
14//! Five questions, and they are the whole of what an assignment has to get right.
15//!
16//! Every value the function reads is written first, on every path that reaches the read. Every
17//! value the function uses has somewhere to live. Two values that are both wanted at the same
18//! point are not in the same register or the same slot. Nothing is sitting in a register that an
19//! instruction insists on for itself, because that register belongs to the instruction for as long
20//! as it runs. A value an instruction can only read from memory is in memory.
21//!
22//! The first of those is not about the allocation at all, since the value would be read before it
23//! was written whatever register it went to. It is asked here because this is where the answer is
24//! already computed: a value read before it is written is a value live on the way into the entry
25//! block, and the liveness the allocator needs anyway says which those are. A function that gets
26//! this wrong is one the allocator will happily place, and what comes out reads a stack slot
27//! nothing ever stored to.
28//!
29//! # What it does not ask
30//!
31//! Whether the allocation is any good. A function with every value on the stack passes, and so it
32//! should: it is slow and it is correct, and this is the thing that says which of the two a
33//! problem is. Quality is what the numbers in `spec/14-target-ladder.md` are for.
34//!
35//! It also does not read the rewrite. It runs on the assignment, before [`crate::rewrite`] has
36//! touched the function, because the assignment is the decision and the rewrite is a
37//! transcription of it. A rewrite that transcribes a good decision badly is a different bug and
38//! the tests in that file are what catch it.
39//!
40//! # Why it repeats work
41//!
42//! The two address instructions are worked out again here rather than borrowed from
43//! [`crate::assign`], and that is deliberate. A checker that shares its reasoning with the thing
44//! it checks agrees with it about everything, including the mistakes, and the one bug it can never
45//! find is the one in the code they share. Fifteen lines is a cheap price for a second opinion.
46//!
47//! It is also allowed to be slow. Looking for a value in a register an instruction wants is the
48//! plain product of the values and the constrained operands, with no index over either, because a
49//! checker runs in debug builds and in CI and the thing it is checking is the thing that has to be
50//! fast.
51
52use std::fmt;
53
54use rucc_mir::{Constraint, Func, Inst, Reg, Role};
55use rucc_target::{PhysReg, RegClass};
56
57use crate::assign::{Assignment, Place};
58use crate::live::{Area, Live, Range};
59use crate::order::{Order, Point};
60
61/// One thing wrong with an allocation.
62#[derive(Debug, Clone, Copy, PartialEq, Eq)]
63pub enum Problem {
64    /// A value the function reads or writes was given no place at all.
65    Nowhere {
66        /// The value with nowhere to be.
67        reg: Reg,
68    },
69    /// Two values that are both live somewhere were put in the same place, so whichever is written
70    /// second destroys the other.
71    Shared {
72        /// The value that was there first.
73        first: Reg,
74        /// The value that was put on top of it.
75        second: Reg,
76        /// The place they were both given.
77        place: Place,
78    },
79    /// A value was left in a register an instruction claims for itself, over the instruction that
80    /// claims it, so the moves around that instruction overwrite the value.
81    InTheWay {
82        /// The value in the way.
83        reg: Reg,
84        /// The register the instruction insists on.
85        at: PhysReg,
86        /// The instruction that insists on it.
87        inst: Inst,
88    },
89    /// A value an instruction can only read from memory was put in a register.
90    NotOnTheStack {
91        /// The value that has to be in memory.
92        reg: Reg,
93        /// The instruction that says so.
94        inst: Inst,
95    },
96    /// A value is read on some path from the entry block without anything on that path having
97    /// written it, so what the instruction reading it gets is whatever was left there.
98    NeverWritten {
99        /// The value nothing writes.
100        reg: Reg,
101    },
102}
103
104impl fmt::Display for Problem {
105    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
106        match self {
107            Problem::Nowhere { reg } => write!(f, "{} has nowhere to live", name(*reg)),
108            Problem::Shared { first, second, place } => {
109                let (first, second) = (name(*first), name(*second));
110                write!(f, "{first} and {second} are both live and both in {}", place_name(*place))
111            }
112            Problem::InTheWay { reg, at, inst } => {
113                let reg = name(*reg);
114                let inst = inst.index();
115                write!(f, "{reg} is in register {}, which instruction {inst} wants", at.number())
116            }
117            Problem::NotOnTheStack { reg, inst } => {
118                let reg = name(*reg);
119                write!(f, "{reg} is not on the stack, and instruction {} needs it", inst.index())
120            }
121            Problem::NeverWritten { reg } => {
122                write!(f, "{} is read before anything writes it", name(*reg))
123            }
124        }
125    }
126}
127
128/// Everything wrong with an allocation, in an order a person can read.
129///
130/// An empty answer is the one every allocation is supposed to give. Anything else is a compiler
131/// bug rather than a program the compiler cannot handle, which is why [`crate::run`] asserts on it
132/// instead of reporting it as a diagnostic.
133///
134/// # Panics
135///
136/// Panics on a function with two billion virtual registers in it, which is a function no machine
137/// has the memory to hold.
138#[must_use]
139pub fn check(func: &Func, order: &Order, live: &Live, assignment: &Assignment) -> Vec<Problem> {
140    let mut problems = Vec::new();
141    // What arrives live in the entry block is what the function reads without writing, since
142    // nothing runs in front of the entry block to have written it.
143    if let Some(entry) = func.entry() {
144        for reg in live.live_in(entry) {
145            problems.push(Problem::NeverWritten { reg });
146        }
147    }
148    let reuses = reuses(func, order);
149    let mut values = Vec::new();
150    for (number, reuse) in reuses.iter().enumerate() {
151        let reg = Reg::virtual_reg(u32::try_from(number).expect("a register number"));
152        let (Some(mut area), Some(class)) = (live.area(reg), func.class_of(reg)) else {
153            continue;
154        };
155        let Some(place) = assignment.place(reg) else {
156            problems.push(Problem::Nowhere { reg });
157            continue;
158        };
159        // A two address instruction writes its answer into a register it read, so the answer is
160        // really in that register from the moment the instruction starts and not from the moment
161        // it ends. Reading its area any other way lets it share the register with something the
162        // same instruction is still reading.
163        if let Some(reuse) = reuse {
164            area = area.with(reuse.at);
165        }
166        values.push(Value { reg, class, range: area.hull(), area, place });
167    }
168    overlaps(&values, &reuses, live, &mut problems);
169    instructions(func, order, assignment, &values, &reuses, &mut problems);
170    problems
171}
172
173/// Everything wrong with an allocation, as an assertion message.
174#[must_use]
175pub fn report(problems: &[Problem]) -> String {
176    let places = if problems.len() == 1 { "place" } else { "places" };
177    let mut report = format!("the allocation is wrong in {} {places}", problems.len());
178    for problem in problems {
179        report.push_str("\n  ");
180        report.push_str(&problem.to_string());
181    }
182    report
183}
184
185/// One value, where it is wanted and where it was put.
186#[derive(Debug, Clone, Copy)]
187struct Value<'a> {
188    reg: Reg,
189    class: RegClass,
190    /// The interval around the area, which is what the sweep below reads.
191    range: Range,
192    /// Everywhere the value is really live, which is what says whether sharing a place with
193    /// another value is a mistake.
194    area: Area<'a>,
195    place: Place,
196}
197
198/// A value written into the register another operand of the same instruction was read from.
199#[derive(Debug, Clone, Copy)]
200struct Reuse {
201    source: Reg,
202    at: Point,
203}
204
205/// Looks for two values that are both live somewhere and were put in the same place.
206///
207/// A sweep in the order the values start, holding the ones whose interval still reaches this one,
208/// so the pairs it compares are the pairs that can be wrong rather than all of them. The interval
209/// is generous, so a pair that survives the sweep is then asked whether the areas inside those
210/// intervals really meet.
211fn overlaps(
212    values: &[Value<'_>],
213    reuses: &[Option<Reuse>],
214    live: &Live,
215    problems: &mut Vec<Problem>,
216) {
217    let mut sorted = values.to_vec();
218    sorted.sort_by_key(|value| (value.range.start, value.reg));
219    let mut active: Vec<Value<'_>> = Vec::new();
220    for value in sorted {
221        active.retain(|held| held.range.end >= value.range.start);
222        for held in &active {
223            if !together(*held, value)
224                || !held.area.overlaps(value.area)
225                || coalesced(*held, value, reuses, live)
226            {
227                continue;
228            }
229            problems.push(Problem::Shared {
230                first: held.reg,
231                second: value.reg,
232                place: value.place,
233            });
234        }
235        active.push(value);
236    }
237}
238
239/// Whether two values were put in the same place.
240///
241/// Two registers of different classes are different registers even when they are the same number,
242/// which is what a class is. Two slots are the same slot whatever is in them, because a frame is
243/// one piece of memory.
244fn together(first: Value<'_>, second: Value<'_>) -> bool {
245    match (first.place, second.place) {
246        (Place::Reg(first_at), Place::Reg(second_at)) => {
247            first_at == second_at && first.class == second.class
248        }
249        (Place::Slot(first_slot), Place::Slot(second_slot)) => first_slot == second_slot,
250        _ => false,
251    }
252}
253
254/// Whether one of the two is the answer a two address instruction wrote into the register it read
255/// the other from, which is the one overlap that is not a mistake.
256///
257/// It only holds when the value being read is finished with at that instruction. A value read
258/// again afterwards needs its register afterwards, so writing over it is the plain bug this whole
259/// file exists to find.
260///
261/// It also only holds when the value being written is not already live where the instruction
262/// reads. The area read here is the one liveness worked out, without the extra point the reuse
263/// adds, so a value that covers the reuse point on its own is one that was already live on the way
264/// in. That is what a loop carrying its own answer round looks like: written at the bottom and read
265/// by the next turn. Such a value is wanted where the instruction reads as well as after it, so it
266/// is genuinely on top of the one it reuses and no excuse at the one instruction they share makes
267/// them fit in a single register.
268fn coalesced(first: Value<'_>, second: Value<'_>, reuses: &[Option<Reuse>], live: &Live) -> bool {
269    let pair = |source: Value<'_>, dest: Value<'_>| {
270        let Some(reuse) = reuses[index(dest.reg)] else { return false };
271        reuse.source == source.reg
272            && live.area(dest.reg).is_some_and(|area| !area.covers(reuse.at))
273            && live.range(source.reg).is_some_and(|r| r.end == reuse.at)
274    };
275    pair(first, second) || pair(second, first)
276}
277
278/// Looks for a value in a register an instruction wants, and for a value that had to be in memory
279/// and is not.
280fn instructions(
281    func: &Func,
282    order: &Order,
283    assignment: &Assignment,
284    values: &[Value<'_>],
285    reuses: &[Option<Reuse>],
286    problems: &mut Vec<Problem>,
287) {
288    for block in func.blocks() {
289        for inst in func.insts(block) {
290            for operand in &func[func[inst].operands] {
291                if operand.constraint == Constraint::Stack
292                    && matches!(assignment.place(operand.reg), Some(Place::Reg(_)))
293                {
294                    problems.push(Problem::NotOnTheStack { reg: operand.reg, inst });
295                }
296                // A physical register an operand names outright is claimed exactly as firmly as
297                // one a constraint asks for, since nothing before allocation writes one except an
298                // instruction that has no choice.
299                let at = match operand.constraint {
300                    Constraint::Fixed(at) => Some(at),
301                    _ => operand.reg.phys(),
302                };
303                let Some(at) = at else { continue };
304                let early = order.early(inst);
305                let point = if operand.role == Role::Def { order.late(inst) } else { early };
306                for value in values {
307                    let mine = value.reg == operand.reg
308                        || reuses[index(value.reg)].is_some_and(|reuse| {
309                            reuse.source == operand.reg
310                                && reuse.at == early
311                                && value.place == Place::Reg(at)
312                        });
313                    if mine || value.class != operand.class {
314                        continue;
315                    }
316                    // The interval around a value covers blocks the value never reaches, so what
317                    // decides this is the area inside it, which says whether the value is live at
318                    // this point rather than whether the point is between its ends.
319                    // tamnd/rucc#982.
320                    if value.place == Place::Reg(at) && value.area.covers(point) {
321                        problems.push(Problem::InTheWay { reg: value.reg, at, inst });
322                    }
323                }
324            }
325        }
326    }
327}
328
329/// The value each two address instruction reuses, by the virtual register it writes.
330fn reuses(func: &Func, order: &Order) -> Vec<Option<Reuse>> {
331    let mut reuses = vec![None; func.vregs()];
332    for block in func.blocks() {
333        for inst in func.insts(block) {
334            let operands = &func[func[inst].operands];
335            for operand in operands {
336                let Constraint::Reuse(other) = operand.constraint else { continue };
337                let number = operand.reg.number().and_then(|number| usize::try_from(number).ok());
338                let Some(number) = number else { continue };
339                let source = operands[usize::from(other)].reg;
340                reuses[number] = Some(Reuse { source, at: order.early(inst) });
341            }
342        }
343    }
344    reuses
345}
346
347/// A virtual register's number as a table index, and zero for a physical one, which never has an
348/// entry of its own and is never what a reuse writes.
349fn index(reg: Reg) -> usize {
350    reg.number().and_then(|number| usize::try_from(number).ok()).unwrap_or(0)
351}
352
353/// What a value is called in a report.
354fn name(reg: Reg) -> String {
355    match reg.number() {
356        Some(number) => format!("%{number}"),
357        None => format!("register {}", reg.phys().expect("a physical register").number()),
358    }
359}
360
361/// What a place is called in a report, without the target's name for it, since this crate holds
362/// nothing of any target.
363fn place_name(place: Place) -> String {
364    match place {
365        Place::Reg(at) => format!("register {}", at.number()),
366        Place::Slot(slot) => format!("slot {slot}"),
367    }
368}
369
370#[cfg(test)]
371mod tests {
372    use rucc_base::Interner;
373    use rucc_mir::{BlockCall, Opcode, Operand};
374    use rucc_target::x86_64::{GPR, RAX, RCX, RDX, SYSV};
375
376    use super::*;
377    use crate::assign::{Env, assign};
378
379    /// The x86-64 environment, with the last three of the allocation order held back as scratch.
380    fn env() -> Env {
381        let (order, scratch) = SYSV.int_order.split_at(SYSV.int_order.len() - 3);
382        Env::new().with(GPR, order, scratch)
383    }
384
385    /// What the checker says about the allocation the single pass allocator works out, which is
386    /// supposed to be nothing at all.
387    fn allocated(func: &Func) -> Vec<String> {
388        let order = Order::of(func);
389        let live = Live::of(func, &order);
390        let assignment = assign(func, &order, &live, &env());
391        said(func, &order, &live, &assignment)
392    }
393
394    /// What the checker says about an allocation somebody wrote by hand.
395    fn said(func: &Func, order: &Order, live: &Live, assignment: &Assignment) -> Vec<String> {
396        check(func, order, live, assignment).iter().map(ToString::to_string).collect()
397    }
398
399    /// The order and the liveness of a function, which every hand written case needs both of.
400    fn read(func: &Func) -> (Order, Live) {
401        let order = Order::of(func);
402        let live = Live::of(func, &order);
403        (order, live)
404    }
405
406    #[test]
407    fn an_allocation_the_allocator_worked_out_has_nothing_wrong_with_it() {
408        let mut names = Interner::new();
409        let mut func = Func::new(names.intern("f"));
410        let opcode = Opcode::new(names.intern("x64.nop"));
411        let block = func.create_block();
412        let first = func.new_vreg(GPR);
413        let second = func.new_vreg(GPR);
414        func.build(block, opcode).def(first, GPR).finish();
415        func.build(block, opcode).def(second, GPR).finish();
416        func.build(block, opcode).uses(first, GPR).uses(second, GPR).finish();
417
418        assert_eq!(allocated(&func), Vec::<String>::new());
419    }
420
421    #[test]
422    fn a_value_with_nowhere_to_live_is_found() {
423        let mut names = Interner::new();
424        let mut func = Func::new(names.intern("f"));
425        let opcode = Opcode::new(names.intern("x64.nop"));
426        let block = func.create_block();
427        let only = func.new_vreg(GPR);
428        func.build(block, opcode).def(only, GPR).finish();
429        func.build(block, opcode).uses(only, GPR).finish();
430
431        let (order, live) = read(&func);
432        let assignment = Assignment::empty(func.vregs());
433
434        assert_eq!(said(&func, &order, &live, &assignment), ["%0 has nowhere to live"]);
435    }
436
437    #[test]
438    fn a_value_read_before_anything_writes_it_is_found() {
439        let mut names = Interner::new();
440        let mut func = Func::new(names.intern("f"));
441        let opcode = Opcode::new(names.intern("x64.nop"));
442        let block = func.create_block();
443        let never = func.new_vreg(GPR);
444        func.build(block, opcode).uses(never, GPR).finish();
445
446        let (order, live) = read(&func);
447        let mut assignment = Assignment::empty(func.vregs());
448        assignment.put(never, Place::Reg(RAX));
449
450        assert_eq!(
451            said(&func, &order, &live, &assignment),
452            ["%0 is read before anything writes it"]
453        );
454    }
455
456    #[test]
457    fn two_values_that_are_both_wanted_and_share_a_register_are_found() {
458        let mut names = Interner::new();
459        let mut func = Func::new(names.intern("f"));
460        let opcode = Opcode::new(names.intern("x64.nop"));
461        let block = func.create_block();
462        let first = func.new_vreg(GPR);
463        let second = func.new_vreg(GPR);
464        func.build(block, opcode).def(first, GPR).finish();
465        func.build(block, opcode).def(second, GPR).finish();
466        func.build(block, opcode).uses(first, GPR).uses(second, GPR).finish();
467
468        let (order, live) = read(&func);
469        let mut assignment = Assignment::empty(func.vregs());
470        assignment.put(first, Place::Reg(RAX));
471        assignment.put(second, Place::Reg(RAX));
472
473        let said = said(&func, &order, &live, &assignment);
474        assert_eq!(said, ["%0 and %1 are both live and both in register 0"]);
475    }
476
477    #[test]
478    fn a_value_that_lives_in_a_hole_of_another_may_share_its_register() {
479        let mut names = Interner::new();
480        let mut func = Func::new(names.intern("f"));
481        let opcode = Opcode::new(names.intern("x64.nop"));
482        let entry = func.create_block();
483        let arm = func.create_block();
484        let tail = func.create_block();
485        let across = func.new_vreg(GPR);
486        let inside = func.new_vreg(GPR);
487        func.build(entry, opcode).def(across, GPR).finish();
488        *func.succs_mut(entry) = vec![BlockCall::to(arm), BlockCall::to(tail)];
489        func.build(arm, opcode).def(inside, GPR).finish();
490        func.build(arm, opcode).uses(inside, GPR).finish();
491        func.build(tail, opcode).uses(across, GPR).finish();
492
493        let (order, live) = read(&func);
494        let mut assignment = Assignment::empty(func.vregs());
495        assignment.put(across, Place::Reg(RAX));
496        assignment.put(inside, Place::Reg(RAX));
497
498        // The arm is written between the two blocks the first value is live in and is a block that
499        // value's own path never goes through, so the second is not sitting on top of it and the
500        // interval around the first saying so is not what decides this. A checker that read the
501        // intervals would call every register the allocator has learned to share a value written
502        // over another. tamnd/rucc#982.
503        assert_eq!(said(&func, &order, &live, &assignment), Vec::<String>::new());
504    }
505
506    #[test]
507    fn two_values_that_are_both_wanted_and_share_a_slot_are_found() {
508        let mut names = Interner::new();
509        let mut func = Func::new(names.intern("f"));
510        let opcode = Opcode::new(names.intern("x64.nop"));
511        let block = func.create_block();
512        let first = func.new_vreg(GPR);
513        let second = func.new_vreg(GPR);
514        func.build(block, opcode).def(first, GPR).finish();
515        func.build(block, opcode).def(second, GPR).finish();
516        func.build(block, opcode).uses(first, GPR).uses(second, GPR).finish();
517
518        let (order, live) = read(&func);
519        let mut assignment = Assignment::empty(func.vregs());
520        let slot = assignment.take_slot(GPR);
521        assignment.put(first, Place::Slot(slot));
522        assignment.put(second, Place::Slot(slot));
523
524        let said = said(&func, &order, &live, &assignment);
525        assert_eq!(said, ["%0 and %1 are both live and both in slot 0"]);
526    }
527
528    #[test]
529    fn two_values_that_are_never_both_wanted_may_share_anything() {
530        let mut names = Interner::new();
531        let mut func = Func::new(names.intern("f"));
532        let opcode = Opcode::new(names.intern("x64.nop"));
533        let block = func.create_block();
534        let first = func.new_vreg(GPR);
535        let second = func.new_vreg(GPR);
536        func.build(block, opcode).def(first, GPR).finish();
537        func.build(block, opcode).uses(first, GPR).finish();
538        func.build(block, opcode).def(second, GPR).finish();
539        func.build(block, opcode).uses(second, GPR).finish();
540
541        let (order, live) = read(&func);
542        let mut assignment = Assignment::empty(func.vregs());
543        assignment.put(first, Place::Reg(RAX));
544        assignment.put(second, Place::Reg(RAX));
545
546        assert_eq!(said(&func, &order, &live, &assignment), Vec::<String>::new());
547    }
548
549    #[test]
550    fn a_value_left_in_a_register_an_instruction_wants_is_found() {
551        let mut names = Interner::new();
552        let mut func = Func::new(names.intern("f"));
553        let nop = Opcode::new(names.intern("x64.nop"));
554        let divide = Opcode::new(names.intern("x64.idiv"));
555        let block = func.create_block();
556        let held = func.new_vreg(GPR);
557        let dividend = func.new_vreg(GPR);
558        func.build(block, nop).def(held, GPR).finish();
559        func.build(block, nop).def(dividend, GPR).finish();
560        // The division reads its dividend out of one register and no other, so anything still
561        // wanted afterwards has to be somewhere else while it runs.
562        func.build(block, divide)
563            .operand(Operand::read(dividend, GPR).with(Constraint::Fixed(RAX)))
564            .finish();
565        func.build(block, nop).uses(held, GPR).finish();
566
567        let (order, live) = read(&func);
568        let mut assignment = Assignment::empty(func.vregs());
569        assignment.put(held, Place::Reg(RAX));
570        assignment.put(dividend, Place::Reg(RCX));
571
572        let said = said(&func, &order, &live, &assignment);
573        assert_eq!(said, ["%0 is in register 0, which instruction 2 wants"]);
574    }
575
576    #[test]
577    fn the_value_an_instruction_wants_a_register_for_may_be_in_it_already() {
578        let mut names = Interner::new();
579        let mut func = Func::new(names.intern("f"));
580        let nop = Opcode::new(names.intern("x64.nop"));
581        let divide = Opcode::new(names.intern("x64.idiv"));
582        let block = func.create_block();
583        let dividend = func.new_vreg(GPR);
584        func.build(block, nop).def(dividend, GPR).finish();
585        func.build(block, divide)
586            .operand(Operand::read(dividend, GPR).with(Constraint::Fixed(RAX)))
587            .finish();
588
589        let (order, live) = read(&func);
590        let mut assignment = Assignment::empty(func.vregs());
591        assignment.put(dividend, Place::Reg(RAX));
592
593        // Being in the register the instruction wanted is the best answer, not a problem, and the
594        // rewrite writes no move at all for it.
595        assert_eq!(said(&func, &order, &live, &assignment), Vec::<String>::new());
596    }
597
598    #[test]
599    fn a_value_that_can_only_be_read_from_memory_and_is_in_a_register_is_found() {
600        let mut names = Interner::new();
601        let mut func = Func::new(names.intern("f"));
602        let nop = Opcode::new(names.intern("x64.nop"));
603        let wide = Opcode::new(names.intern("x64.wide"));
604        let block = func.create_block();
605        let only = func.new_vreg(GPR);
606        func.build(block, nop).def(only, GPR).finish();
607        func.build(block, wide).operand(Operand::read(only, GPR).with(Constraint::Stack)).finish();
608
609        let (order, live) = read(&func);
610        let mut assignment = Assignment::empty(func.vregs());
611        assignment.put(only, Place::Reg(RAX));
612
613        let said = said(&func, &order, &live, &assignment);
614        assert_eq!(said, ["%0 is not on the stack, and instruction 1 needs it"]);
615    }
616
617    #[test]
618    fn a_two_address_instruction_may_write_the_register_it_read_a_finished_value_from() {
619        let mut names = Interner::new();
620        let mut func = Func::new(names.intern("f"));
621        let nop = Opcode::new(names.intern("x64.nop"));
622        let add = Opcode::new(names.intern("x64.add"));
623        let block = func.create_block();
624        let left = func.new_vreg(GPR);
625        let right = func.new_vreg(GPR);
626        let sum = func.new_vreg(GPR);
627        func.build(block, nop).def(left, GPR).finish();
628        func.build(block, nop).def(right, GPR).finish();
629        func.build(block, add)
630            .operand(Operand::write(sum, GPR).with(Constraint::Reuse(1)))
631            .uses(left, GPR)
632            .uses(right, GPR)
633            .finish();
634        func.build(block, nop).uses(sum, GPR).finish();
635
636        let (order, live) = read(&func);
637        let mut assignment = Assignment::empty(func.vregs());
638        assignment.put(left, Place::Reg(RAX));
639        assignment.put(right, Place::Reg(RCX));
640        assignment.put(sum, Place::Reg(RAX));
641
642        // The left operand is finished with at the addition, so the sum takes its register and
643        // the addition is the one instruction rather than a move and an instruction.
644        assert_eq!(said(&func, &order, &live, &assignment), Vec::<String>::new());
645    }
646
647    #[test]
648    fn a_two_address_instruction_may_not_write_over_a_value_wanted_afterwards() {
649        let mut names = Interner::new();
650        let mut func = Func::new(names.intern("f"));
651        let nop = Opcode::new(names.intern("x64.nop"));
652        let add = Opcode::new(names.intern("x64.add"));
653        let block = func.create_block();
654        let left = func.new_vreg(GPR);
655        let right = func.new_vreg(GPR);
656        let sum = func.new_vreg(GPR);
657        func.build(block, nop).def(left, GPR).finish();
658        func.build(block, nop).def(right, GPR).finish();
659        func.build(block, add)
660            .operand(Operand::write(sum, GPR).with(Constraint::Reuse(1)))
661            .uses(left, GPR)
662            .uses(right, GPR)
663            .finish();
664        func.build(block, nop).uses(sum, GPR).uses(left, GPR).finish();
665
666        let (order, live) = read(&func);
667        let mut assignment = Assignment::empty(func.vregs());
668        assignment.put(left, Place::Reg(RAX));
669        assignment.put(right, Place::Reg(RCX));
670        assignment.put(sum, Place::Reg(RAX));
671
672        // The left operand is read again after the addition, so the addition may not have its
673        // register even though it is the one the addition reads.
674        let said = said(&func, &order, &live, &assignment);
675        assert_eq!(said, ["%0 and %2 are both live and both in register 0"]);
676    }
677
678    #[test]
679    fn a_two_address_instruction_may_not_write_the_register_it_reads_its_other_operand_from() {
680        let mut names = Interner::new();
681        let mut func = Func::new(names.intern("f"));
682        let nop = Opcode::new(names.intern("x64.nop"));
683        let add = Opcode::new(names.intern("x64.add"));
684        let block = func.create_block();
685        let left = func.new_vreg(GPR);
686        let right = func.new_vreg(GPR);
687        let sum = func.new_vreg(GPR);
688        func.build(block, nop).def(left, GPR).finish();
689        func.build(block, nop).def(right, GPR).finish();
690        func.build(block, add)
691            .operand(Operand::write(sum, GPR).with(Constraint::Reuse(1)))
692            .uses(left, GPR)
693            .uses(right, GPR)
694            .finish();
695        func.build(block, nop).uses(sum, GPR).finish();
696
697        let (order, live) = read(&func);
698        let mut assignment = Assignment::empty(func.vregs());
699        assignment.put(left, Place::Reg(RAX));
700        assignment.put(right, Place::Reg(RCX));
701        assignment.put(sum, Place::Reg(RCX));
702
703        // Copying the left operand into the sum's register would destroy the right operand before
704        // the addition has read it, even though both are finished with at the addition.
705        let said = said(&func, &order, &live, &assignment);
706        assert_eq!(said, ["%1 and %2 are both live and both in register 1"]);
707    }
708
709    #[test]
710    fn a_two_address_instruction_may_not_write_the_register_it_read_over_its_own_last_answer() {
711        let mut names = Interner::new();
712        let mut func = Func::new(names.intern("f"));
713        let nop = Opcode::new(names.intern("x64.nop"));
714        let add = Opcode::new(names.intern("x64.add"));
715        let head = func.create_block();
716        let latch = func.create_block();
717        let out = func.create_block();
718        let source = func.new_vreg(GPR);
719        let carried = func.new_vreg(GPR);
720        func.build(head, nop).def(source, GPR).finish();
721        func.build(head, nop).def(carried, GPR).finish();
722        *func.succs_mut(head) = vec![BlockCall::to(latch)];
723        func.build(latch, add)
724            .operand(Operand::write(carried, GPR).with(Constraint::Reuse(1)))
725            .uses(source, GPR)
726            .uses(carried, GPR)
727            .finish();
728        *func.succs_mut(latch) = vec![BlockCall::to(head), BlockCall::to(out)];
729        func.build(out, nop).uses(carried, GPR).finish();
730
731        let (order, live) = read(&func);
732        let mut assignment = Assignment::empty(func.vregs());
733        assignment.put(source, Place::Reg(RAX));
734        assignment.put(carried, Place::Reg(RAX));
735
736        // The source is finished with at the addition, which is what would normally let the answer
737        // have its register. It does not here, because the answer is the one the last turn round
738        // the loop wrote and the addition reads it too, so both are wanted where it reads.
739        let said = said(&func, &order, &live, &assignment);
740        assert_eq!(said, ["%0 and %1 are both live and both in register 0"]);
741    }
742
743    #[test]
744    fn a_two_address_answer_with_a_hole_in_front_of_it_still_may_not_take_the_other_operand() {
745        let mut names = Interner::new();
746        let mut func = Func::new(names.intern("f"));
747        let nop = Opcode::new(names.intern("x64.nop"));
748        let add = Opcode::new(names.intern("x64.add"));
749        let entry = func.create_block();
750        let head = func.create_block();
751        let arm = func.create_block();
752        let latch = func.create_block();
753        let out = func.create_block();
754        let seed = func.new_vreg(GPR);
755        let sum = func.new_vreg(GPR);
756        let inside = func.new_vreg(GPR);
757        let loaded = func.new_vreg(GPR);
758        func.build(entry, nop).def(seed, GPR).finish();
759        func.build(entry, nop).def(sum, GPR).finish();
760        *func.succs_mut(entry) = vec![BlockCall::to(head)];
761        func.build(head, nop).uses(sum, GPR).finish();
762        *func.succs_mut(head) = vec![BlockCall::to(arm), BlockCall::to(latch)];
763        func.build(arm, nop).def(inside, GPR).finish();
764        func.build(arm, nop).uses(inside, GPR).finish();
765        *func.succs_mut(arm) = vec![BlockCall::to(out)];
766        func.build(latch, nop).def(loaded, GPR).finish();
767        func.build(latch, add)
768            .operand(Operand::write(sum, GPR).with(Constraint::Reuse(1)))
769            .uses(seed, GPR)
770            .uses(loaded, GPR)
771            .finish();
772        *func.succs_mut(latch) = vec![BlockCall::to(head), BlockCall::to(out)];
773
774        let (order, live) = read(&func);
775        let mut assignment = Assignment::empty(func.vregs());
776        assignment.put(seed, Place::Reg(RCX));
777        assignment.put(sum, Place::Reg(RAX));
778        assignment.put(inside, Place::Reg(RDX));
779        assignment.put(loaded, Place::Reg(RAX));
780
781        // The answer is live in the entry and the head too, so the piece the addition writes is not
782        // the first one and the arm in between is a hole. Copying the left operand into the answer's
783        // register still destroys the right operand before the addition reads it, and a checker that
784        // added the extra point to the first piece rather than the piece the addition writes saw
785        // nothing wrong with any of it. tamnd/rucc#982.
786        let said = said(&func, &order, &live, &assignment);
787        assert_eq!(said, ["%1 and %3 are both live and both in register 0"]);
788    }
789
790    #[test]
791    fn a_report_names_every_problem() {
792        let mut names = Interner::new();
793        let mut func = Func::new(names.intern("f"));
794        let opcode = Opcode::new(names.intern("x64.nop"));
795        let block = func.create_block();
796        let first = func.new_vreg(GPR);
797        let second = func.new_vreg(GPR);
798        func.build(block, opcode).def(first, GPR).finish();
799        func.build(block, opcode).def(second, GPR).finish();
800        func.build(block, opcode).uses(first, GPR).uses(second, GPR).finish();
801
802        let (order, live) = read(&func);
803        let mut assignment = Assignment::empty(func.vregs());
804        assignment.put(first, Place::Reg(RAX));
805        assignment.put(second, Place::Reg(RAX));
806
807        let problems = check(&func, &order, &live, &assignment);
808        assert_eq!(
809            report(&problems),
810            "the allocation is wrong in 1 place\n  %0 and %1 are both live and both in register 0"
811        );
812        assert_eq!(report(&[]), "the allocation is wrong in 0 places");
813    }
814}