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