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,30 +514,62 @@ pub fn (mut g Gen) gen_exit() {
}
fn (mut g Gen) mov(reg Register, val int) {
match reg {
.eax, .rax {
g.write8(0xb8)
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')
}
}
.edi, .rdi {
g.write8(0xbf)
}
.edx {
g.write8(0xba)
}
.rsi {
g.write8(0x48)
g.write8(0xbe)
}
.r12 {
g.write8(0x41)
g.write8(0xbc) // r11 is 0xbb etc
}
else {
panic('unhandled mov $reg')
g.println('xor $reg, $reg')
} else {
match reg {
.eax, .rax {
g.write8(0xb8)
}
.edi, .rdi {
g.write8(0xbf)
}
.edx {
g.write8(0xba)
}
.rsi {
g.write8(0x48)
g.write8(0xbe)
}
.r12 {
g.write8(0x41)
g.write8(0xbc) // r11 is 0xbb etc
}
else {
panic('unhandled mov $reg')
}
}
g.write32(val)
g.println('mov $reg, $val')
}
g.write32(val)
g.println('mov $reg, $val')
}
fn (mut g Gen) mov_reg(a Register, b Register) {