plonky2/evm/src/cpu/kernel/optimizer.rs

235 lines
7.1 KiB
Rust
Raw Normal View History

use ethereum_types::U256;
use Item::{Push, StandardOp};
use PushTarget::Literal;
2022-07-31 13:13:39 -07:00
use crate::cpu::kernel::ast::Item::{GlobalLabelDeclaration, LocalLabelDeclaration};
use crate::cpu::kernel::ast::PushTarget::Label;
use crate::cpu::kernel::ast::{Item, PushTarget};
2022-08-01 09:22:01 -07:00
use crate::cpu::kernel::utils::{replace_windows, u256_from_bool};
pub(crate) fn optimize_asm(code: &mut Vec<Item>) {
2022-07-31 13:13:39 -07:00
// Run the optimizer until nothing changes.
loop {
let old_code = code.clone();
optimize_asm_once(code);
if code == &old_code {
break;
}
2022-07-31 13:13:39 -07:00
}
}
2022-07-31 13:13:39 -07:00
/// A single optimization pass.
fn optimize_asm_once(code: &mut Vec<Item>) {
constant_propagation(code);
no_op_jumps(code);
2022-08-01 09:02:39 -07:00
remove_swapped_pushes(code);
remove_swaps_commutative(code);
2022-08-01 08:47:07 -07:00
remove_ignored_values(code);
}
2022-07-31 13:13:39 -07:00
/// Constant propagation.
fn constant_propagation(code: &mut Vec<Item>) {
2022-07-31 13:13:39 -07:00
// Constant propagation for unary ops: `[PUSH x, UNARYOP] -> [PUSH UNARYOP(x)]`
replace_windows(code, |window| {
if let [Push(Literal(x)), StandardOp(op)] = window {
match op.as_str() {
2022-08-01 09:22:01 -07:00
"ISZERO" => Some(vec![Push(Literal(u256_from_bool(x.is_zero())))]),
"NOT" => Some(vec![Push(Literal(!x))]),
_ => None,
}
} else {
None
}
});
2022-07-31 20:37:05 -07:00
// Constant propagation for binary ops: `[PUSH y, PUSH x, BINOP] -> [PUSH BINOP(x, y)]`
replace_windows(code, |window| {
2022-07-31 20:37:05 -07:00
if let [Push(Literal(y)), Push(Literal(x)), StandardOp(op)] = window {
match op.as_str() {
2022-08-01 09:22:01 -07:00
"ADD" => Some(x.overflowing_add(y).0),
"SUB" => Some(x.overflowing_sub(y).0),
"MUL" => Some(x.overflowing_mul(y).0),
"DIV" => Some(x.checked_div(y).unwrap_or(U256::zero())),
"SHL" => Some(x << y),
"SHR" => Some(x >> y),
"AND" => Some(x & y),
"OR" => Some(x | y),
"XOR" => Some(x ^ y),
"LT" => Some(u256_from_bool(x < y)),
"GT" => Some(u256_from_bool(x > y)),
"EQ" => Some(u256_from_bool(x == y)),
_ => None,
}
2022-08-01 09:22:01 -07:00
.map(|res| vec![Push(Literal(res))])
} else {
None
}
});
}
2022-08-01 08:47:07 -07:00
/// Remove no-op jumps: `[PUSH label, JUMP, label:] -> [label:]`.
2022-07-31 13:13:39 -07:00
fn no_op_jumps(code: &mut Vec<Item>) {
replace_windows(code, |window| {
if let [Push(Label(l)), StandardOp(jump), decl] = window
&& &jump == "JUMP"
&& (decl == LocalLabelDeclaration(l.clone()) || decl == GlobalLabelDeclaration(l.clone()))
{
Some(vec![LocalLabelDeclaration(l)])
} else {
None
}
});
}
2022-08-01 08:47:07 -07:00
/// Remove swaps: `[PUSH x, PUSH y, SWAP1] -> [PUSH y, PUSH x]`.
2022-08-01 09:02:39 -07:00
fn remove_swapped_pushes(code: &mut Vec<Item>) {
2022-07-31 13:13:39 -07:00
replace_windows(code, |window| {
if let [Push(x), Push(y), StandardOp(swap1)] = window
&& &swap1 == "SWAP1" {
Some(vec![Push(y), Push(x)])
} else {
None
}
});
}
2022-08-01 09:02:39 -07:00
/// Remove SWAP1 before a commutative function.
fn remove_swaps_commutative(code: &mut Vec<Item>) {
replace_windows(code, |window| {
if let [StandardOp(swap1), StandardOp(f)] = window && &swap1 == "SWAP1" {
2022-08-02 09:22:06 -07:00
let commutative = matches!(f.as_str(), "ADD" | "MUL" | "AND" | "OR" | "XOR" | "EQ");
2022-08-01 09:02:39 -07:00
commutative.then_some(vec![StandardOp(f)])
} else {
None
}
});
}
2022-08-01 08:47:07 -07:00
/// Remove push-pop type patterns, such as: `[DUP1, POP]`.
fn remove_ignored_values(code: &mut Vec<Item>) {
replace_windows(code, |[a, b]| {
if let StandardOp(pop) = b && &pop == "POP" {
match a {
Push(_) => Some(vec![]),
StandardOp(dup) if dup.starts_with("DUP") => Some(vec![]),
_ => None,
}
} else {
None
}
});
}
#[cfg(test)]
2022-07-31 13:13:39 -07:00
mod tests {
use super::*;
#[test]
fn test_constant_propagation_iszero() {
let mut code = vec![Push(Literal(3.into())), StandardOp("ISZERO".into())];
constant_propagation(&mut code);
assert_eq!(code, vec![Push(Literal(0.into()))]);
}
2022-07-31 20:43:58 -07:00
#[test]
fn test_constant_propagation_add_overflowing() {
let mut code = vec![
Push(Literal(U256::max_value())),
Push(Literal(U256::max_value())),
StandardOp("ADD".into()),
];
constant_propagation(&mut code);
assert_eq!(code, vec![Push(Literal(U256::max_value() - 1))]);
}
#[test]
fn test_constant_propagation_sub_underflowing() {
let mut code = vec![
Push(Literal(U256::one())),
Push(Literal(U256::zero())),
StandardOp("SUB".into()),
];
constant_propagation(&mut code);
assert_eq!(code, vec![Push(Literal(U256::max_value()))]);
}
2022-07-31 13:13:39 -07:00
#[test]
fn test_constant_propagation_mul() {
let mut code = vec![
Push(Literal(3.into())),
Push(Literal(4.into())),
StandardOp("MUL".into()),
];
constant_propagation(&mut code);
assert_eq!(code, vec![Push(Literal(12.into()))]);
}
2022-07-31 20:37:05 -07:00
#[test]
fn test_constant_propagation_div() {
let mut code = vec![
Push(Literal(3.into())),
Push(Literal(8.into())),
StandardOp("DIV".into()),
];
constant_propagation(&mut code);
assert_eq!(code, vec![Push(Literal(2.into()))]);
}
#[test]
fn test_constant_propagation_div_zero() {
let mut code = vec![
Push(Literal(0.into())),
Push(Literal(1.into())),
StandardOp("DIV".into()),
];
constant_propagation(&mut code);
assert_eq!(code, vec![Push(Literal(0.into()))]);
}
2022-07-31 13:13:39 -07:00
#[test]
fn test_no_op_jump() {
let mut code = vec![
Push(Label("mylabel".into())),
StandardOp("JUMP".into()),
LocalLabelDeclaration("mylabel".into()),
];
no_op_jumps(&mut code);
assert_eq!(code, vec![LocalLabelDeclaration("mylabel".into())]);
}
#[test]
2022-08-01 09:02:39 -07:00
fn test_remove_swapped_pushes() {
2022-07-31 13:13:39 -07:00
let mut code = vec![
Push(Literal("42".into())),
Push(Label("mylabel".into())),
StandardOp("SWAP1".into()),
];
2022-08-01 09:02:39 -07:00
remove_swapped_pushes(&mut code);
2022-07-31 13:13:39 -07:00
assert_eq!(
code,
vec![Push(Label("mylabel".into())), Push(Literal("42".into()))]
);
}
2022-08-01 08:47:07 -07:00
2022-08-01 09:02:39 -07:00
#[test]
fn test_remove_swap_mul() {
let mut code = vec![StandardOp("SWAP1".into()), StandardOp("MUL".into())];
remove_swaps_commutative(&mut code);
assert_eq!(code, vec![StandardOp("MUL".into())]);
}
2022-08-01 08:47:07 -07:00
#[test]
fn test_remove_push_pop() {
let mut code = vec![Push(Literal("42".into())), StandardOp("POP".into())];
remove_ignored_values(&mut code);
assert_eq!(code, vec![]);
}
#[test]
fn test_remove_dup_pop() {
let mut code = vec![StandardOp("DUP5".into()), StandardOp("POP".into())];
remove_ignored_values(&mut code);
assert_eq!(code, vec![]);
}
2022-07-31 13:13:39 -07:00
}