Skip to main content

rucc_regalloc/
assign.rs

1//! Which register each value lives in, and which values live on the stack instead.
2//!
3//! Design: `spec/10-backend.md` section 10.4.
4//!
5//! This is the `-O0` allocator's decision and nothing else. It is linear scan over the line
6//! [`crate::order`] lays the function out in: the values are taken in the order they are written,
7//! each is given a register that nothing else live at the same time is in, and when there is no
8//! such register one of the values in flight goes to the stack instead. There is no splitting and
9//! no coalescing, so a value gets one place for the whole of its range and keeps it. That produces
10//! mediocre code quickly, which is what `-O0` is for, and the allocator that produces good code
11//! slowly is a separate one, in M4.
12//!
13//! Which value is sent to the stack is the one whose range ends last, counting the value being
14//! placed among the candidates. A value wanted for a long time is the cheapest to spill per
15//! instruction it frees a register over, and it is the only heuristic here.
16//!
17//! # What it does with a register an instruction insists on
18//!
19//! Two things. It stays out of that register for everybody else, and it tries that register first
20//! for the value the operand names. A division wants its dividend in `rax`, so `rax` is
21//! unavailable to every other value that is live where the division reads, and it is the first
22//! register offered to the dividend itself. When the dividend gets it there is no move on the way
23//! in, and when it does not the rewrite writes one and nothing else changes.
24//!
25//! That second half is the hint, and without it the register an instruction insists on is the one
26//! register the value in it can never have, since the value's own operand is what makes the
27//! register look busy. The effect is largest on returns, because a function that gives a value
28//! back has an operand fixed to `rax` at the end of it and most functions give a value back.
29//!
30//! What makes the hint safe is asking about the register at each of the instruction's two points
31//! rather than across the whole of it. An instruction reads at the first and writes at the second,
32//! so a register it insists on is one value's at the first, another value's at the second, and
33//! nobody else's at either. A division reads its dividend from `rax` and writes its quotient to
34//! `rax`, and those are different values that can both live there. A value passed to a call in
35//! `rdi` and wanted again afterwards cannot, because nothing writes `rdi` at the second point and
36//! a register the call does not write is a register the call is assumed to destroy.
37//!
38//! An operand that has to be in memory is the other way round. The value it names goes on the
39//! stack whatever else is true of it, because that is the only place the instruction could read it
40//! from.
41//!
42//! # What it does with a two address instruction
43//!
44//! An `add` on x86-64 writes one of the registers it reads, which the operand says as a reuse of
45//! another operand. The rewrite can always make that true by copying the source into the
46//! destination first, but only if the destination is a register the instruction does not otherwise
47//! read, so a value written by a reuse is treated here as live from where the instruction reads
48//! rather than from where it writes. Then the copy is always safe.
49//!
50//! The copy is also usually unnecessary, and the one place this looks past the interval it is
51//! placing is to see that: if the value being reused is read here for the last time, the value
52//! being written may have its register, and the instruction is already two address without
53//! anything being moved anywhere. That is the whole of the coalescing this allocator does, and it
54//! is worth the dozen lines, because otherwise every piece of arithmetic in the output carries a
55//! move in front of it.
56//!
57//! # What it does not do
58//!
59//! It does not touch the function. What comes out is a table saying where each value went, and the
60//! pass that rewrites the operands and writes the moves reads it. Keeping the decision and the
61//! rewrite apart is what lets the decision be checked by looking at it, and it is the shape
62//! `spec/10-backend.md` section 10.4 asks for: an allocator is a function from a program to an
63//! assignment and the moves that make it true.
64
65use rucc_mir::{Constraint, Func, Operand, Reg, Role};
66use rucc_target::{PhysReg, RegClass};
67
68use crate::live::{Live, Range};
69use crate::order::{Order, Point};
70
71/// Where a value lives.
72#[derive(Debug, Clone, Copy, PartialEq, Eq)]
73pub enum Place {
74    /// In a register, for the whole of its range.
75    Reg(PhysReg),
76    /// In a slot of the frame, which is what a value the allocator ran out of registers for gets,
77    /// and what a value an instruction can only read from memory gets.
78    Slot(u32),
79}
80
81/// What the allocator is allowed to use.
82///
83/// The order is the calling convention's, because which register to hand out first follows from
84/// which ones a call destroys, and `rucc-target` is where a convention says so. The scratch
85/// registers are held back out of the order and are what a spilled value is read into at each
86/// instruction that wants it, so a class needs as many of them as one of its instructions has
87/// register operands. Nothing here uses them, since a spilled value is only read once the rewrite
88/// is writing the instruction that reads it, but they are held back here because this is what
89/// decides what everything else may have.
90#[derive(Debug, Clone, Default)]
91pub struct Env {
92    classes: Vec<Class>,
93}
94
95/// What one class of registers offers.
96#[derive(Debug, Clone, Default)]
97struct Class {
98    order: Vec<PhysReg>,
99    scratch: Vec<PhysReg>,
100}
101
102impl Env {
103    /// An environment offering nothing, which is what a target that has said nothing offers.
104    #[must_use]
105    pub fn new() -> Self {
106        Self::default()
107    }
108
109    /// The same environment, with that class described.
110    #[must_use]
111    pub fn with(mut self, class: RegClass, order: &[PhysReg], scratch: &[PhysReg]) -> Self {
112        let index = usize::from(class.number());
113        if self.classes.len() <= index {
114            self.classes.resize(index + 1, Class::default());
115        }
116        self.classes[index] = Class { order: order.to_vec(), scratch: scratch.to_vec() };
117        self
118    }
119
120    /// The registers it may hand out in a class, in the order it prefers them.
121    #[must_use]
122    pub fn order(&self, class: RegClass) -> &[PhysReg] {
123        self.classes.get(usize::from(class.number())).map_or(&[], |class| &class.order)
124    }
125
126    /// The registers held back in a class for reading a spilled value into.
127    #[must_use]
128    pub fn scratch(&self, class: RegClass) -> &[PhysReg] {
129        self.classes.get(usize::from(class.number())).map_or(&[], |class| &class.scratch)
130    }
131}
132
133/// Where every value in a function went.
134#[derive(Debug, Clone)]
135pub struct Assignment {
136    places: Vec<Option<Place>>,
137    slots: Vec<RegClass>,
138}
139
140impl Assignment {
141    /// An assignment that says nothing yet about a function with that many values.
142    ///
143    /// This and [`Assignment::put`] and [`Assignment::take_slot`] are how an allocator says what
144    /// it decided. There will be a second one in M4 and it will not reach its answer this way, so
145    /// what an assignment is has to be separable from how this file arrives at one, and the
146    /// checker in [`crate::check`] reads an assignment without caring which allocator wrote it.
147    #[must_use]
148    pub fn empty(vregs: usize) -> Self {
149        Self { places: vec![None; vregs], slots: Vec::new() }
150    }
151
152    /// Records where a value went.
153    ///
154    /// # Panics
155    ///
156    /// Panics on a physical register, which is somewhere already, and on a virtual one the
157    /// function never handed out.
158    pub fn put(&mut self, reg: Reg, place: Place) {
159        self.places[index(reg)] = Some(place);
160    }
161
162    /// Takes a slot of the frame, of that class, and gives back which one it is.
163    ///
164    /// # Panics
165    ///
166    /// Panics past four billion slots, which is a frame no machine has room for.
167    pub fn take_slot(&mut self, class: RegClass) -> u32 {
168        let slot = u32::try_from(self.slots.len()).expect("too many spilled values");
169        self.slots.push(class);
170        slot
171    }
172
173    /// Where a value lives, or `None` for a virtual register this function never mentions and for
174    /// a physical one, which is already where it is.
175    #[must_use]
176    pub fn place(&self, reg: Reg) -> Option<Place> {
177        self.places.get(usize::try_from(reg.number()?).ok()?).copied().flatten()
178    }
179
180    /// The class of each slot of the frame, which is what says how wide it has to be.
181    #[must_use]
182    pub fn slots(&self) -> &[RegClass] {
183        &self.slots
184    }
185
186    /// How many values went to the stack.
187    #[must_use]
188    pub fn spilled(&self) -> usize {
189        self.slots.len()
190    }
191
192    /// Puts a value on the stack, in a slot of its own.
193    fn spill(&mut self, reg: Reg, class: RegClass) {
194        let slot = self.take_slot(class);
195        self.put(reg, Place::Slot(slot));
196    }
197}
198
199/// One value waiting for a place.
200#[derive(Debug, Clone, Copy)]
201struct Interval {
202    reg: Reg,
203    class: RegClass,
204    range: Range,
205}
206
207/// One value that has a register, for as long as it still wants it.
208#[derive(Debug, Clone, Copy)]
209struct Held {
210    reg: Reg,
211    class: RegClass,
212    range: Range,
213    at: PhysReg,
214}
215
216/// A register an instruction insists on, and where it insists on it.
217#[derive(Debug, Clone, Copy)]
218struct Blocked {
219    class: RegClass,
220    at: PhysReg,
221    /// One of the instruction's two points. Every register an instruction insists on has an entry
222    /// at each of them, because a register held at one of the two is a register nothing else may
223    /// be in across the instruction.
224    point: Point,
225    /// The one value that may be in it there, which is the value of an operand the instruction
226    /// reads at that point or writes at it. `None` means nothing may: an operand naming a physical
227    /// register outright claims it against everything, and a point no operand covers is a point
228    /// the instruction has the register to itself at.
229    by: Option<Reg>,
230}
231
232/// A value written into the register another operand of the same instruction was read from.
233#[derive(Debug, Clone, Copy)]
234struct Reuse {
235    /// The value being read, which is the one whose register would do.
236    source: Reg,
237    /// Where the instruction reads it.
238    at: Point,
239}
240
241/// Decides where every value in a function lives.
242///
243/// # Panics
244///
245/// Panics if a class has no registers to hand out and something in the function is in that class,
246/// since that is a target description that does not describe the target the function is for.
247#[must_use]
248pub fn assign(func: &Func, order: &Order, live: &Live, env: &Env) -> Assignment {
249    let blocked = blocked(func, order);
250    let forced = forced(func);
251    let reuses = reuses(func, order);
252    let hints = hints(func);
253
254    let mut intervals = Vec::with_capacity(func.vregs());
255    for (number, reuse) in reuses.iter().enumerate() {
256        let reg = Reg::virtual_reg(u32::try_from(number).expect("a register number"));
257        let (Some(mut range), Some(class)) = (live.range(reg), func.class_of(reg)) else {
258            continue;
259        };
260        if let Some(reuse) = reuse {
261            range.start = range.start.min(reuse.at);
262        }
263        intervals.push(Interval { reg, class, range });
264    }
265    intervals.sort_by_key(|interval| (interval.range.start, interval.reg));
266
267    let mut assignment = Assignment::empty(func.vregs());
268    let mut active: Vec<Held> = Vec::new();
269    for interval in intervals {
270        active.retain(|held| held.range.end >= interval.range.start);
271        if forced.contains(&interval.reg) {
272            assignment.spill(interval.reg, interval.class);
273            continue;
274        }
275        // A class with no order is one the target says nothing allocates from, which on x86-64 is
276        // the x87 stack. A value of such a class is a mistake at the point it was made rather than
277        // a value with nowhere to go: what the target means is that the value lives in memory and
278        // that whatever operates on it takes an address. See `ClassInfo::allocatable`.
279        assert!(
280            !env.order(interval.class).is_empty(),
281            "a value in class {}, which the target hands out no registers from",
282            interval.class.number()
283        );
284        let two_address = reuses[index(interval.reg)]
285            .and_then(|reuse| coalesce(&assignment, &active, &blocked, interval, reuse));
286        // The reuse comes first, because a two address instruction that has to copy its left
287        // operand in pays for the copy whatever the hint says, and taking the hint here would buy
288        // one move at the cost of another.
289        let hinted = hints[index(interval.reg)].filter(|&at| {
290            env.order(interval.class).contains(&at)
291                && available(&active, &blocked, interval, at, None)
292        });
293        let chosen = two_address.or(hinted).or_else(|| {
294            env.order(interval.class)
295                .iter()
296                .copied()
297                .find(|&at| available(&active, &blocked, interval, at, None))
298        });
299        match chosen {
300            Some(at) => {
301                assignment.places[index(interval.reg)] = Some(Place::Reg(at));
302                let reg = interval.reg;
303                active.push(Held { reg, class: interval.class, range: interval.range, at });
304            }
305            None => spill_one(&mut assignment, &mut active, &blocked, interval),
306        }
307    }
308    assignment
309}
310
311/// Whether a register is one this interval could have.
312///
313/// The exception is the value a reuse is coalescing with, which holds the register right up to the
314/// point the new value takes it over and is the one thing that may overlap.
315fn available(
316    active: &[Held],
317    blocked: &[Blocked],
318    interval: Interval,
319    at: PhysReg,
320    except: Option<Reg>,
321) -> bool {
322    let taken = active
323        .iter()
324        .any(|held| held.at == at && held.class == interval.class && Some(held.reg) != except);
325    let insisted = blocked.iter().any(|one| {
326        one.at == at
327            && one.class == interval.class
328            && one.by != Some(interval.reg)
329            && interval.range.covers(one.point)
330    });
331    !taken && !insisted
332}
333
334/// The register the value being reused is in, when this instruction is the last thing that reads
335/// it and the register is otherwise free.
336fn coalesce(
337    assignment: &Assignment,
338    active: &[Held],
339    blocked: &[Blocked],
340    interval: Interval,
341    reuse: Reuse,
342) -> Option<PhysReg> {
343    let Some(Place::Reg(at)) = assignment.place(reuse.source) else { return None };
344    let source = active.iter().find(|held| held.reg == reuse.source)?;
345    // A value read again later needs its register after this instruction would have overwritten
346    // it, so the two really do have to be different and the rewrite really does have to copy.
347    let dies = source.range.end == reuse.at;
348    (dies && available(active, blocked, interval, at, Some(reuse.source))).then_some(at)
349}
350
351/// Sends one value to the stack: the one wanted for longest, since its register pays for itself
352/// over the most instructions.
353fn spill_one(
354    assignment: &mut Assignment,
355    active: &mut Vec<Held>,
356    blocked: &[Blocked],
357    interval: Interval,
358) {
359    // A value whose register the instructions in the way insist on for themselves is no use as a
360    // victim, because taking it over would put this value in a register it may not have.
361    let victim = active
362        .iter()
363        .enumerate()
364        .filter(|(_, held)| held.class == interval.class)
365        .filter(|(_, held)| available(&[], blocked, interval, held.at, None))
366        .max_by_key(|(_, held)| held.range.end)
367        .map(|(at, held)| (at, held.at, held.range.end));
368    match victim {
369        Some((victim, at, end)) if end > interval.range.end => {
370            let held = active.remove(victim);
371            assignment.spill(held.reg, held.class);
372            assignment.places[index(interval.reg)] = Some(Place::Reg(at));
373            let reg = interval.reg;
374            active.push(Held { reg, class: interval.class, range: interval.range, at });
375        }
376        _ => assignment.spill(interval.reg, interval.class),
377    }
378}
379
380/// The registers the instructions insist on, and where.
381///
382/// A physical register an operand names outright counts the same way. Nothing before allocation
383/// writes one except an instruction that has to, and it has to for the length of that one
384/// instruction, which is the same statement a fixed constraint makes.
385fn blocked(func: &Func, order: &Order) -> Vec<Blocked> {
386    let mut blocked = Vec::new();
387    let mut claimed: Vec<(RegClass, PhysReg)> = Vec::new();
388    for block in func.blocks() {
389        for inst in func.insts(block) {
390            let operands = &func[func[inst].operands];
391            claimed.clear();
392            for operand in operands {
393                if let Some(at) = insisted(operand) {
394                    let key = (operand.class, at);
395                    if !claimed.contains(&key) {
396                        claimed.push(key);
397                    }
398                }
399            }
400            for &(class, at) in &claimed {
401                // Both points, whether or not an operand is at them. A register an instruction
402                // reads and does not write is destroyed by the time the instruction is done as far
403                // as anything here knows, which is what stops the value a call is passed in `rdi`
404                // from staying in `rdi` over the call.
405                for (point, role) in [(order.early(inst), Role::Use), (order.late(inst), Role::Def)]
406                {
407                    let mut named = false;
408                    for operand in operands {
409                        let mine = insisted(operand) == Some(at) && operand.class == class;
410                        if !mine || !(operand.role == role || operand.role == Role::EarlyDef) {
411                            continue;
412                        }
413                        named = true;
414                        let by = operand.reg.is_virtual().then_some(operand.reg);
415                        blocked.push(Blocked { class, at, point, by });
416                    }
417                    if !named {
418                        blocked.push(Blocked { class, at, point, by: None });
419                    }
420                }
421            }
422        }
423    }
424    blocked
425}
426
427/// The register an operand has to be in, which is the one a constraint asks for or the one the
428/// operand names outright.
429fn insisted(operand: &Operand) -> Option<PhysReg> {
430    match operand.constraint {
431        Constraint::Fixed(at) => Some(at),
432        _ => operand.reg.phys(),
433    }
434}
435
436/// The register each value would rather be in, which is the one an operand naming it insists on.
437///
438/// A value with two of them keeps the first the function writes down, which is the definition when
439/// there is one, since a value written into a fixed register and then moved somewhere else pays
440/// for the move at the top of its life rather than at the bottom. Two different fixed registers on
441/// one value is rare enough that the second is not worth carrying a list for.
442fn hints(func: &Func) -> Vec<Option<PhysReg>> {
443    let mut hints = vec![None; func.vregs()];
444    for block in func.blocks() {
445        for inst in func.insts(block) {
446            for operand in &func[func[inst].operands] {
447                let Constraint::Fixed(at) = operand.constraint else { continue };
448                let number = operand.reg.number().and_then(|number| usize::try_from(number).ok());
449                let Some(number) = number else { continue };
450                if func.class_of(operand.reg) == Some(operand.class) && hints[number].is_none() {
451                    hints[number] = Some(at);
452                }
453            }
454        }
455    }
456    hints
457}
458
459/// The values that have to be on the stack whatever else is true of them.
460fn forced(func: &Func) -> Vec<Reg> {
461    let mut forced = Vec::new();
462    for block in func.blocks() {
463        for inst in func.insts(block) {
464            for operand in &func[func[inst].operands] {
465                if operand.constraint == Constraint::Stack
466                    && operand.reg.is_virtual()
467                    && !forced.contains(&operand.reg)
468                {
469                    forced.push(operand.reg);
470                }
471            }
472        }
473    }
474    forced
475}
476
477/// The value each two address instruction reuses, by the virtual register it writes.
478fn reuses(func: &Func, order: &Order) -> Vec<Option<Reuse>> {
479    let mut reuses = vec![None; func.vregs()];
480    for block in func.blocks() {
481        for inst in func.insts(block) {
482            let operands = &func[func[inst].operands];
483            for operand in operands {
484                let Constraint::Reuse(other) = operand.constraint else { continue };
485                let number = operand.reg.number().and_then(|number| usize::try_from(number).ok());
486                let Some(number) = number else { continue };
487                let source = operands[usize::from(other)].reg;
488                reuses[number] = Some(Reuse { source, at: order.early(inst) });
489            }
490        }
491    }
492    reuses
493}
494
495/// A virtual register's number as a table index.
496fn index(reg: Reg) -> usize {
497    usize::try_from(reg.number().expect("a virtual register")).expect("a register number")
498}
499
500#[cfg(test)]
501mod tests {
502    use rucc_base::Interner;
503    use rucc_mir::{BlockCall, Opcode, Operand};
504    use rucc_target::x86_64::{GPR, R13, R14, R15, RAX, RCX, RDX, REGS, SYSV};
505
506    use super::*;
507
508    /// The x86-64 environment, with the last three of the allocation order held back as scratch.
509    fn env() -> Env {
510        let (order, scratch) = SYSV.int_order.split_at(SYSV.int_order.len() - 3);
511        Env::new().with(GPR, order, scratch)
512    }
513
514    /// An environment with that many general purpose registers, for putting a function under
515    /// pressure without writing a hundred instructions.
516    fn narrow(count: usize) -> Env {
517        Env::new().with(GPR, &SYSV.int_order[..count], &SYSV.int_order[count..count + 1])
518    }
519
520    /// What a place is called, which is what an assertion reads.
521    fn named(place: Option<Place>) -> String {
522        match place {
523            Some(Place::Reg(reg)) => REGS.name(GPR, reg).expect("a register").to_string(),
524            Some(Place::Slot(slot)) => format!("slot {slot}"),
525            None => "nowhere".to_string(),
526        }
527    }
528
529    /// Where every value in a function went.
530    fn places(func: &Func, env: &Env) -> Vec<String> {
531        let order = Order::of(func);
532        let live = Live::of(func, &order);
533        let assignment = assign(func, &order, &live, env);
534        (0..func.vregs())
535            .map(|number| {
536                let reg = Reg::virtual_reg(u32::try_from(number).expect("a register number"));
537                named(assignment.place(reg))
538            })
539            .collect()
540    }
541
542    #[test]
543    fn two_values_that_are_never_both_wanted_share_a_register() {
544        let mut names = Interner::new();
545        let mut func = Func::new(names.intern("f"));
546        let opcode = Opcode::new(names.intern("x64.nop"));
547        let block = func.create_block();
548        let first = func.new_vreg(GPR);
549        let second = func.new_vreg(GPR);
550        func.build(block, opcode).def(first, GPR).finish();
551        func.build(block, opcode).uses(first, GPR).finish();
552        func.build(block, opcode).def(second, GPR).finish();
553        func.build(block, opcode).uses(second, GPR).finish();
554
555        // The first register in the order, twice, because the first value is finished with before
556        // the second one is written.
557        assert_eq!(places(&func, &env()), ["rax", "rax"]);
558    }
559
560    #[test]
561    fn two_values_that_are_both_wanted_do_not() {
562        let mut names = Interner::new();
563        let mut func = Func::new(names.intern("f"));
564        let opcode = Opcode::new(names.intern("x64.nop"));
565        let block = func.create_block();
566        let first = func.new_vreg(GPR);
567        let second = func.new_vreg(GPR);
568        func.build(block, opcode).def(first, GPR).finish();
569        func.build(block, opcode).def(second, GPR).finish();
570        func.build(block, opcode).uses(first, GPR).finish();
571        func.build(block, opcode).uses(second, GPR).finish();
572
573        assert_eq!(places(&func, &env()), ["rax", "rcx"]);
574    }
575
576    #[test]
577    fn a_value_written_early_that_nothing_reads_still_holds_its_register() {
578        let mut names = Interner::new();
579        let mut func = Func::new(names.intern("f"));
580        let opcode = Opcode::new(names.intern("x64.nop"));
581        let block = func.create_block();
582        let wanted = func.new_vreg(GPR);
583        let spare = func.new_vreg(GPR);
584        // A division: a remainder somebody wants, and a quotient nobody does. Both are written by
585        // the one instruction and the quotient is written before the operands have been read.
586        func.build(block, opcode)
587            .def(wanted, GPR)
588            .operand(Operand::write_early(spare, GPR))
589            .finish();
590        func.build(block, opcode).uses(wanted, GPR).finish();
591
592        // Two registers, not one. A value nothing reads is still somewhere, and the instruction
593        // that wrote it wrote the other one too, so the two cannot be the same place. Handing them
594        // the same register loses the remainder, because the copy that takes the quotient out of
595        // the register the machine insisted on goes on top of it. The quotient gets the first
596        // register because it is written first, which is the whole of what early means.
597        assert_eq!(places(&func, &env()), ["rcx", "rax"]);
598    }
599
600    #[test]
601    fn the_value_wanted_longest_is_the_one_that_goes_to_the_stack() {
602        let mut names = Interner::new();
603        let mut func = Func::new(names.intern("f"));
604        let opcode = Opcode::new(names.intern("x64.nop"));
605        let block = func.create_block();
606        let long = func.new_vreg(GPR);
607        let short = func.new_vreg(GPR);
608        let third = func.new_vreg(GPR);
609        func.build(block, opcode).def(long, GPR).finish();
610        func.build(block, opcode).def(short, GPR).finish();
611        func.build(block, opcode).def(third, GPR).finish();
612        func.build(block, opcode).uses(short, GPR).finish();
613        func.build(block, opcode).uses(third, GPR).finish();
614        func.build(block, opcode).uses(long, GPR).finish();
615
616        // Two registers between three values. The one still wanted at the end of the function is
617        // the one whose register is worth the most to everybody else, so it is the one that goes.
618        assert_eq!(places(&func, &narrow(2)), ["slot 0", "rcx", "rax"]);
619    }
620
621    #[test]
622    fn a_register_an_instruction_insists_on_goes_to_the_values_that_asked_for_it() {
623        let mut names = Interner::new();
624        let mut func = Func::new(names.intern("f"));
625        let opcode = Opcode::new(names.intern("x64.nop"));
626        let block = func.create_block();
627        let across = func.new_vreg(GPR);
628        let dividend = func.new_vreg(GPR);
629        let quotient = func.new_vreg(GPR);
630        let remainder = func.new_vreg(GPR);
631        func.build(block, opcode).def(across, GPR).finish();
632        func.build(block, opcode).def(dividend, GPR).finish();
633        func.build(block, opcode)
634            .operand(Operand::write(quotient, GPR).with(Constraint::Fixed(RAX)))
635            .operand(Operand::write_early(remainder, GPR).with(Constraint::Fixed(RDX)))
636            .operand(Operand::read(dividend, GPR).with(Constraint::Fixed(RAX)))
637            .finish();
638        func.build(block, opcode).uses(across, GPR).finish();
639
640        // The value that has to be across the division is nowhere near `rax` or `rdx`, and each of
641        // the three the division names is in the register the division asked for it in. The
642        // dividend and the quotient share `rax` because the first is read where the second is
643        // written, which is what a division does.
644        assert_eq!(places(&func, &env()), ["rcx", "rax", "rax", "rdx"]);
645    }
646
647    #[test]
648    fn a_value_wanted_after_the_instruction_that_insists_does_not_get_that_register() {
649        let mut names = Interner::new();
650        let mut func = Func::new(names.intern("f"));
651        let opcode = Opcode::new(names.intern("x64.nop"));
652        let block = func.create_block();
653        let dividend = func.new_vreg(GPR);
654        let quotient = func.new_vreg(GPR);
655        func.build(block, opcode).def(dividend, GPR).finish();
656        func.build(block, opcode)
657            .operand(Operand::write(quotient, GPR).with(Constraint::Fixed(RAX)))
658            .operand(Operand::read(dividend, GPR).with(Constraint::Fixed(RAX)))
659            .finish();
660        func.build(block, opcode).uses(dividend, GPR).finish();
661
662        // The hint is a preference and not a claim. The dividend would rather be in `rax` and
663        // cannot be, because the division writes `rax` and the dividend is wanted afterwards, so
664        // it takes the next register and the quotient keeps the one it was promised.
665        assert_eq!(places(&func, &env()), ["rcx", "rax"]);
666    }
667
668    #[test]
669    fn a_value_an_instruction_can_only_read_from_memory_is_on_the_stack() {
670        let mut names = Interner::new();
671        let mut func = Func::new(names.intern("f"));
672        let opcode = Opcode::new(names.intern("x64.nop"));
673        let block = func.create_block();
674        let value = func.new_vreg(GPR);
675        func.build(block, opcode).def(value, GPR).finish();
676        func.build(block, opcode)
677            .operand(Operand::read(value, GPR).with(Constraint::Stack))
678            .finish();
679
680        assert_eq!(places(&func, &env()), ["slot 0"]);
681    }
682
683    #[test]
684    fn a_two_address_instruction_writes_the_register_it_read_when_it_can() {
685        let mut names = Interner::new();
686        let mut func = Func::new(names.intern("f"));
687        let opcode = Opcode::new(names.intern("x64.nop"));
688        let block = func.create_block();
689        let left = func.new_vreg(GPR);
690        let right = func.new_vreg(GPR);
691        let sum = func.new_vreg(GPR);
692        func.build(block, opcode).def(left, GPR).finish();
693        func.build(block, opcode).def(right, GPR).finish();
694        func.build(block, opcode)
695            .operand(Operand::write(sum, GPR).with(Constraint::Reuse(1)))
696            .uses(left, GPR)
697            .uses(right, GPR)
698            .finish();
699        func.build(block, opcode).uses(right, GPR).finish();
700
701        // The addition reads the left value for the last time, so the answer goes where that was
702        // and the instruction is two address without a move in front of it.
703        assert_eq!(places(&func, &env()), ["rax", "rcx", "rax"]);
704    }
705
706    #[test]
707    fn a_two_address_instruction_that_cannot_gets_a_register_nothing_it_reads_is_in() {
708        let mut names = Interner::new();
709        let mut func = Func::new(names.intern("f"));
710        let opcode = Opcode::new(names.intern("x64.nop"));
711        let block = func.create_block();
712        let left = func.new_vreg(GPR);
713        let right = func.new_vreg(GPR);
714        let sum = func.new_vreg(GPR);
715        func.build(block, opcode).def(left, GPR).finish();
716        func.build(block, opcode).def(right, GPR).finish();
717        func.build(block, opcode)
718            .operand(Operand::write(sum, GPR).with(Constraint::Reuse(1)))
719            .uses(left, GPR)
720            .uses(right, GPR)
721            .finish();
722        func.build(block, opcode).uses(left, GPR).finish();
723
724        // The left value is wanted afterwards, so the answer cannot have its register. It cannot
725        // have the right one's either, because the rewrite is about to write a move into it before
726        // the addition has read anything.
727        assert_eq!(places(&func, &env()), ["rax", "rcx", "rdx"]);
728    }
729
730    #[test]
731    fn a_value_live_across_a_whole_loop_holds_its_register_over_all_of_it() {
732        let mut names = Interner::new();
733        let mut func = Func::new(names.intern("f"));
734        let opcode = Opcode::new(names.intern("x64.nop"));
735        let head = func.create_block();
736        let body = func.create_block();
737        let carried = func.new_vreg(GPR);
738        let inside = func.new_vreg(GPR);
739        func.build(head, opcode).def(carried, GPR).finish();
740        *func.succs_mut(head) = vec![BlockCall::to(body)];
741        func.build(body, opcode).def(inside, GPR).finish();
742        func.build(body, opcode).uses(inside, GPR).uses(carried, GPR).finish();
743        *func.succs_mut(body) = vec![BlockCall::to(body)];
744
745        // The value inside the loop cannot have the carried one's register, even though nothing
746        // between the two definitions says so.
747        assert_eq!(places(&func, &env()), ["rax", "rcx"]);
748    }
749
750    #[test]
751    fn a_frame_says_what_each_of_its_slots_is_for() {
752        let mut names = Interner::new();
753        let mut func = Func::new(names.intern("f"));
754        let opcode = Opcode::new(names.intern("x64.nop"));
755        let block = func.create_block();
756        let first = func.new_vreg(GPR);
757        let second = func.new_vreg(GPR);
758        func.build(block, opcode).def(first, GPR).finish();
759        func.build(block, opcode).def(second, GPR).finish();
760        func.build(block, opcode).uses(first, GPR).uses(second, GPR).finish();
761
762        let order = Order::of(&func);
763        let live = Live::of(&func, &order);
764        let assignment = assign(&func, &order, &live, &narrow(1));
765        assert_eq!(assignment.spilled(), 1);
766        assert_eq!(assignment.slots(), [GPR]);
767        // A register that is already a register is where it is, and this has nothing to say about
768        // it.
769        assert_eq!(assignment.place(Reg::physical(RCX)), None);
770        assert_eq!(env().scratch(GPR), [R13, R14, R15]);
771    }
772}