2025-09-08 20:21:36 +10:00
|
|
|
from optimizers.optimizer import Optimizer, PeepholeRule, Instruction
|
2025-09-08 07:49:33 +10:00
|
|
|
|
|
|
|
class X86_64Optimizer(Optimizer):
|
2025-09-08 20:21:36 +10:00
|
|
|
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"])
|
|
|
|
]
|
|
|
|
),
|
2025-09-09 07:20:50 +10:00
|
|
|
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"])
|
|
|
|
]
|
|
|
|
),
|
|
|
|
|
2025-09-08 20:21:36 +10:00
|
|
|
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"])
|
|
|
|
]
|
|
|
|
),
|
2025-09-09 07:20:50 +10:00
|
|
|
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"])
|
|
|
|
]
|
|
|
|
),
|
|
|
|
|
2025-09-08 20:21:36 +10:00
|
|
|
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"])
|
|
|
|
]
|
|
|
|
),
|
2025-09-09 07:20:50 +10:00
|
|
|
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"])
|
|
|
|
]
|
|
|
|
),
|
|
|
|
|
2025-09-08 20:21:36 +10:00
|
|
|
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"])
|
|
|
|
]
|
|
|
|
),
|
2025-09-09 07:20:50 +10:00
|
|
|
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"])
|
|
|
|
]
|
2025-09-09 08:02:09 +10:00
|
|
|
),
|
|
|
|
|
|
|
|
PeepholeRule(
|
|
|
|
match=[
|
2025-09-09 17:47:43 +10:00
|
|
|
Instruction("mov", ["rax", "y"]),
|
|
|
|
Instruction("mov", ["rbx", "x"]),
|
|
|
|
Instruction("cmp", ["rax", "rbx"])
|
2025-09-09 08:02:09 +10:00
|
|
|
],
|
|
|
|
replace=[
|
2025-09-09 17:47:43 +10:00
|
|
|
Instruction("mov", ["rax", "y"]),
|
|
|
|
Instruction("cmp", ["rax", "x"])
|
2025-09-09 08:02:09 +10:00
|
|
|
]
|
2025-09-10 20:45:22 +10:00
|
|
|
),
|
|
|
|
|
|
|
|
PeepholeRule(
|
|
|
|
match=[
|
|
|
|
Instruction("mov", ["x", "y"]),
|
|
|
|
Instruction("mov", ["y", "x"])
|
|
|
|
],
|
|
|
|
replace=[]
|
2025-09-09 07:20:50 +10:00
|
|
|
)
|
2025-09-08 20:21:36 +10:00
|
|
|
]
|