x64: optimize mov <reg>, 0 (#7733)

pull/7750/head
clubby789 2020-12-31 11:40:09 +00:00 committed by GitHub
parent bf9b0c6345
commit 3e44393152
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 53 additions and 21 deletions

View File

@ -514,6 +514,37 @@ pub fn (mut g Gen) gen_exit() {
} }
fn (mut g Gen) mov(reg Register, val int) { fn (mut g Gen) mov(reg Register, val int) {
if val == 0 {
// Optimise to xor reg, reg when val is 0
match reg {
.eax, .rax {
g.write8(0x31)
g.write8(0xc0)
}
.edi, .rdi {
g.write8(0x31)
g.write8(0xff)
}
.edx {
g.write8(0x31)
g.write8(0xd2)
}
.rsi {
g.write8(0x48)
g.write8(0x31)
g.write8(0xf6)
}
.r12 {
g.write8(0x4d)
g.write8(0x31)
g.write8(0xe4)
}
else {
panic('unhandled mov $reg')
}
}
g.println('xor $reg, $reg')
} else {
match reg { match reg {
.eax, .rax { .eax, .rax {
g.write8(0xb8) g.write8(0xb8)
@ -538,6 +569,7 @@ fn (mut g Gen) mov(reg Register, val int) {
} }
g.write32(val) g.write32(val)
g.println('mov $reg, $val') g.println('mov $reg, $val')
}
} }
fn (mut g Gen) mov_reg(a Register, b Register) { fn (mut g Gen) mov_reg(a Register, b Register) {