Cranelift: Redundant stack-slot-to-stack-slot copy removal. PR#773.
This is also https://bugzilla.mozilla.org/show_bug.cgi?id=1552737. Cranelift currently has a tendency to create redundant copies (self-copies) of values from a stack slot back to the same stack slot. This generates a pointless load and store and an unnecessary register use. The copies are created by `visit_inst` in regalloc/reload.rs. They appear to occur mostly, but not exclusively, at loop heads. It's unclear why this happens. This patch adds a special case to `visit_inst` to find such copies. They are converted into a new instruction, `copy_nop`, which takes and produces the same SSA names, so as not to break any of the SSA invariants, but which has a zero-length encoding, hence removing the copy at emission time. `copy_nop`s source and destination operands must be stack slots and of course the *same* stack slot. The verifier has been enhanced to check this, since misuse of `copy_nop` will likely lead to hard-to-find incorrect-code bugs. Attempts were made to write a standalone .clif test case. But these failed because it appears the .clif parser accepts but ignores location hints that are stack slots. So it's impossible to write, in clif, the exact form of `copy` instruction that triggers the transformation.
This commit is contained in:
committed by
Benjamin Bouvier
parent
6935033c9e
commit
03368895fe
@@ -341,6 +341,13 @@ enc_x86_64(x86.pop.i64, r.popq, 0x58)
|
||||
X86_64.enc(base.copy_special, *r.copysp.rex(0x89, w=1))
|
||||
X86_32.enc(base.copy_special, *r.copysp(0x89))
|
||||
|
||||
# Stack-slot-to-the-same-stack-slot copy, which is guaranteed to turn
|
||||
# into a no-op.
|
||||
X86_64.enc(base.copy_nop.i64, r.stacknull, 0)
|
||||
X86_64.enc(base.copy_nop.i32, r.stacknull, 0)
|
||||
X86_64.enc(base.copy_nop.f64, r.stacknull, 0)
|
||||
X86_64.enc(base.copy_nop.f32, r.stacknull, 0)
|
||||
|
||||
# Adjust SP down by a dynamic value (or up, with a negative operand).
|
||||
X86_32.enc(base.adjust_sp_down.i32, *r.adjustsp(0x29))
|
||||
X86_64.enc(base.adjust_sp_down.i64, *r.adjustsp.rex(0x29, w=1))
|
||||
|
||||
Reference in New Issue
Block a user