Optimize addition/subtraction of immediate 1 and zeroring registers

This commit is contained in:
Bananymous 2024-04-29 21:34:44 +03:00
parent 2141a17d1e
commit 1e8bdf6cd2
1 changed files with 30 additions and 0 deletions

View File

@ -341,6 +341,7 @@ class CompileData:
continue
instructions.pop(i)
changed = True
if changed: continue
i = 0
# Optimize movq to register followed by pushq register
@ -355,6 +356,7 @@ class CompileData:
instructions.pop(i + 1)
i -= 1
changed = True
if changed: continue
i = 0
# Optimize movq to rax followed by movq from rax
@ -375,6 +377,34 @@ class CompileData:
instructions.pop(i + 1)
i -= 1
changed = True
if changed: continue
i = 0
# Optimize addq/subq for immediate 1
while i < len(instructions):
if instructions[i].opcode not in ['addq', 'subq']:
i += 1
continue
if instructions[i].operands[0] != '$1':
i += 1
continue
new_opcode = 'incq' if instructions[i].opcode == 'addq' else 'decq'
instructions[i] = Instruction(new_opcode, [instructions[i].operands[1]])
changed = True
if changed: continue
i = 0
# Optimize zeroing of register
while i < len(instructions):
if instructions[i].opcode != 'movq':
i += 1
continue
if instructions[i].operands[0] != '$0' or instructions[i].operands[1][0] != '%':
i += 1
continue
instructions[i] = Instruction('xorq', [instructions[i].operands[1], instructions[i].operands[1]])
changed = True
if changed: continue
def add_builtin_functions(self) -> None:
today = []