Skip to main content

offsets

Function offsets 

Source
pub fn offsets(
    func: &mut Func,
    insts: &FrameInsts,
    machine: &MachineInsts,
    names: &mut Interner,
) -> usize
Expand description

Moves a constant added to an address’s index into the address’s displacement, and gives back how many.

p[i + 3] on an int is an index of i + 3 scaled by four, and the optimizer leaves it that way because in the IR it is one value multiplied by one number. Selection then writes the add on its own and the address takes what it wrote as the index, so the read comes out as an addq $3 and a load, where the address could have been 12(%rdi,%rsi,4) and the add need not be there at all. This is the rewrite from one to the other: the address reads what the add read, and its displacement grows by the constant times the scale.

Only when the address is the one read of what the add wrote, since otherwise the add stays for its other readers and nothing is saved, and only in the block the add is in, for the reason addresses has for staying in one. An address with no index is left to the selector, which already writes p + 3 as a base and a displacement, and one with nowhere to put a displacement, a jump table or a place in this function, is left as it is. A displacement that would not fit in its field leaves the pair alone too.

Run before addresses, so a lea that has taken the constant in is what gets handed on to its readers.