Files
GroundPY/optimizers/x86_64.py
2025-09-12 06:28:45 +10:00

165 lines
3.6 KiB
Python

from optimizers.optimizer import Optimizer, PeepholeRule, Instruction
class X86_64Optimizer(Optimizer):
def __init__(self, lines, window_size = 5):
super().__init__(lines, window_size)
self.rules = [
PeepholeRule(
match=[
Instruction("setg",["al"]),
Instruction("movzx", ["rax", "al"]),
Instruction("push", ["rax"]),
Instruction("mov", ["eax", "x"]),
Instruction("test", ["eax", "eax"]),
Instruction("jnz", ["y"])
],
replace=[
Instruction("jg", ["y"])
]
),
PeepholeRule(
match=[
Instruction("setg",["al"]),
Instruction("movzx", ["rax", "al"]),
Instruction("mov", ["z", "rax"]),
Instruction("mov", ["eax", "x"]),
Instruction("test", ["eax", "eax"]),
Instruction("jnz", ["y"])
],
replace=[
Instruction("jg", ["y"])
]
),
PeepholeRule(
match=[
Instruction("setl",["al"]),
Instruction("movzx", ["rax", "al"]),
Instruction("push", ["rax"]),
Instruction("mov", ["eax", "x"]),
Instruction("test", ["eax", "eax"]),
Instruction("jnz", ["y"])
],
replace=[
Instruction("jl", ["y"])
]
),
PeepholeRule(
match=[
Instruction("setl",["al"]),
Instruction("movzx", ["rax", "al"]),
Instruction("mov", ["z", "rax"]),
Instruction("mov", ["eax", "x"]),
Instruction("test", ["eax", "eax"]),
Instruction("jnz", ["y"])
],
replace=[
Instruction("jl", ["y"])
]
),
PeepholeRule(
match=[
Instruction("sete",["al"]),
Instruction("movzx", ["rax", "al"]),
Instruction("push", ["rax"]),
Instruction("mov", ["eax", "x"]),
Instruction("test", ["eax", "eax"]),
Instruction("jnz", ["y"])
],
replace=[
Instruction("je", ["y"])
]
),
PeepholeRule(
match=[
Instruction("sete",["al"]),
Instruction("movzx", ["rax", "al"]),
Instruction("mov", ["z", "rax"]),
Instruction("mov", ["eax", "x"]),
Instruction("test", ["eax", "eax"]),
Instruction("jnz", ["y"])
],
replace=[
Instruction("je", ["y"])
]
),
PeepholeRule(
match=[
Instruction("setne",["al"]),
Instruction("movzx", ["rax", "al"]),
Instruction("push", ["rax"]),
Instruction("mov", ["eax", "x"]),
Instruction("test", ["eax", "eax"]),
Instruction("jnz", ["y"])
],
replace=[
Instruction("jne", ["y"])
]
),
PeepholeRule(
match=[
Instruction("setne",["al"]),
Instruction("movzx", ["rax", "al"]),
Instruction("mov", ["z", "rax"]),
Instruction("mov", ["eax", "x"]),
Instruction("test", ["eax", "eax"]),
Instruction("jnz", ["y"])
],
replace=[
Instruction("jne", ["y"])
]
),
PeepholeRule(
match=[
Instruction("mov", ["rax", "x"]),
Instruction("push", ["rax"])
],
replace=[
Instruction("push", ["x"])
]
),
PeepholeRule(
match=[
Instruction("mov", ["rax", "y"]),
Instruction("mov", ["rbx", "x"]),
Instruction("cmp", ["rax", "rbx"])
],
replace=[
Instruction("mov", ["rax", "y"]),
Instruction("cmp", ["rax", "x"])
]
),
PeepholeRule(
match=[
Instruction("mov", ["x", "y"]),
Instruction("mov", ["y", "x"])
],
replace=[]
),
PeepholeRule(
match=[
Instruction("mov", ["x", "y"]),
Instruction("add", ["z", "x"])
],
replace=[
Instruction("add", ["z", "y"])
]
),
PeepholeRule(
match=[
Instruction("add", ["x", "0"])
],
replace=[]
),
PeepholeRule(
match=[Instruction("mov", ["x", "x"])],
replace=[]
)
]