use super::{drop_dead_frame_stores, elide_callee_saves};
use vlk_base::target::Isa;
use vlk_ir::ir::{BinOp, Block, Cond, Expr, FunctionIr, RegRef, Stmt};
use vlk_types::types::Convention;
const RSP: u16 = 4;
const RBP: u16 = 5;
#[test]
fn stores_crossing_abi_spill_bounds_remain() {
use vlk_types::types::Convention;
for (isa, convention, spills, start, end) in [
(Isa::X86_64, Convention::Win64, None, 8_i64, 40_i64),
(Isa::X86_64, Convention::GoAmd64, Some((24, 40)), 24, 40),
(Isa::AArch64, Convention::Aapcs64, Some((24, 40)), 24, 40),
] {
let sp = vlk_arch::arch::stack_pointer(isa);
for (offset, bits) in [(start - 1, 64), (end - 1, 64), (start, 32)] {
let mut ir = FunctionIr::single_block(
isa,
vec![
Stmt::Store {
addr: Expr::bin(
BinOp::Add,
Expr::reg(sp, bits),
Expr::Const(offset as u64),
),
value: Expr::Const(42),
width: 2,
at: None,
},
Stmt::Ret {
value: Expr::Const(0),
at: None,
},
],
);
ir.convention = convention;
ir.stack_spills = spills;
assert!(!crate::passes::memory::private_slot(
end,
0,
crate::passes::memory::spill_range(&ir)
));
assert!(!crate::passes::memory::private_slot(0, 0, None));
drop_dead_frame_stores(&mut ir);
crate::passes::memory::forward_spilled_variables(&mut ir);
assert_eq!(
ir.blocks[0].stmts.len(),
2,
"{convention:?}/{offset}/{bits}: {}",
ir.render()
);
ir.blocks[0].stmts.insert(
0,
assign(
sp,
Expr::bin(BinOp::Add, Expr::reg(sp, 64), Expr::Const(64)),
),
);
drop_dead_frame_stores(&mut ir);
crate::passes::memory::forward_spilled_variables(&mut ir);
assert_eq!(ir.blocks[0].stmts.len(), 3, "{}", ir.render());
}
}
}
#[test]
fn abi_spills_are_private_but_stack_outputs_remain() {
use vlk_types::types::Convention;
for (isa, convention, spills, spill, output) in [
(Isa::X86_64, Convention::Win64, None, 8, 40),
(Isa::X86_64, Convention::GoAmd64, Some((24, 40)), 24, 8),
(Isa::AArch64, Convention::Aapcs64, Some((24, 40)), 24, 8),
(Isa::X86_64, Convention::SysV, None, -8, 8),
(Isa::I386, Convention::Cdecl, None, -8, 4),
(Isa::I386, Convention::Stdcall, None, -8, 4),
(Isa::Arm32, Convention::Aapcs32, None, -8, 0),
] {
let sp = vlk_arch::arch::stack_pointer(isa);
for width in [1, 2, 4, 8] {
let address =
|offset: i64| Expr::bin(BinOp::Add, Expr::reg(sp, 64), Expr::Const(offset as u64));
for read in [false, true] {
let mut ir = FunctionIr::single_block(
isa,
vec![
Stmt::Store {
addr: address(spill),
value: Expr::Const(42),
width,
at: None,
},
Stmt::Store {
addr: address(output),
value: Expr::Const(17),
width,
at: None,
},
Stmt::Ret {
value: if read {
Expr::Load {
addr: Box::new(address(spill)),
width,
signed: false,
}
} else {
Expr::Const(0)
},
at: None,
},
],
);
ir.convention = convention;
ir.stack_spills = spills;
drop_dead_frame_stores(&mut ir);
assert_eq!(
ir.blocks[0].stmts.len(),
if read { 3 } else { 2 },
"{convention:?}/{width}: {}",
ir.render()
);
assert!(ir.blocks[0].stmts.iter().any(
|stmt| matches!(stmt, Stmt::Store { addr, .. } if *addr == address(output))
));
}
}
}
}
#[test]
fn unread_stores_outside_the_private_frame_remain() {
for isa in [Isa::X86_64, Isa::I386, Isa::AArch64, Isa::Arm32] {
let sp = vlk_arch::arch::stack_pointer(isa);
for width in [1, 2, 4, 8] {
for offset in [-8_i64, -1, 0, 8, i64::MAX] {
let addr = Expr::bin(BinOp::Add, Expr::reg(sp, 64), Expr::Const(offset as u64));
let mut ir = FunctionIr::single_block(
isa,
vec![
Stmt::Store {
addr,
value: Expr::Const(42),
width,
at: None,
},
Stmt::Ret {
value: Expr::Const(0),
at: None,
},
],
);
drop_dead_frame_stores(&mut ir);
let private = offset
.checked_add(i64::from(width))
.is_some_and(|end| end <= 0);
assert_eq!(
ir.blocks[0].stmts.len(),
if private { 1 } else { 2 },
"{isa:?}/{width}/{offset}: {}",
ir.render()
);
}
}
}
}
fn assign(index: u16, value: Expr) -> Stmt {
Stmt::Assign {
dest: RegRef { index, width: 64 },
value,
at: None,
}
}
fn adjust(delta: i64) -> Stmt {
assign(
RSP,
Expr::bin(BinOp::Add, Expr::reg(RSP, 64), Expr::Const(delta as u64)),
)
}
fn restore() -> Vec<Stmt> {
vec![
assign(
RBP,
Expr::Load {
addr: Box::new(Expr::reg(RSP, 64)),
width: 8,
signed: false,
},
),
adjust(8),
Stmt::Ret {
value: Expr::reg(0, 64),
at: None,
},
]
}
fn frame_pointer_function(other: Vec<Stmt>) -> FunctionIr {
let mut ir = FunctionIr::single_block(
Isa::X86_64,
vec![
adjust(-8),
Stmt::Store {
addr: Expr::reg(RSP, 64),
value: Expr::reg(RBP, 64),
width: 8,
at: None,
},
assign(RBP, Expr::reg(RSP, 64)),
Stmt::Brif {
cond: Cond::Eq,
lhs: Expr::reg(7, 64),
rhs: Expr::Const(0),
target: 0x20,
at: None,
},
],
);
ir.blocks.push(Block {
start: 0x10,
stmts: restore(),
});
ir.blocks.push(Block {
start: 0x20,
stmts: other,
});
ir
}
#[test]
fn keeps_a_save_the_body_reads_through_the_frame_pointer() {
let mut ir = frame_pointer_function(vec![
assign(
3,
Expr::Load {
addr: Box::new(Expr::reg(RBP, 64)),
width: 8,
signed: false,
},
),
assign(RBP, Expr::reg(3, 64)),
Stmt::Store {
addr: Expr::Const(0x1000),
value: Expr::reg(RBP, 64),
width: 8,
at: None,
},
]);
ir.blocks[2].stmts.extend(restore());
let before = ir.render();
elide_callee_saves(&mut ir);
assert_eq!(ir.render(), before);
}
#[test]
fn elides_a_save_restored_on_every_return_path() {
let mut ir = frame_pointer_function(restore());
elide_callee_saves(&mut ir);
let left = ir.blocks.iter().flat_map(|block| &block.stmts).count();
assert_eq!(
left,
3,
"only the branch and the returns stay:\n{}",
ir.render()
);
}
#[test]
fn elides_a_callee_saved_register_the_body_uses() {
const RBX: u16 = 3;
let mut ir = FunctionIr::single_block(
Isa::X86_64,
vec![
adjust(-8),
Stmt::Store {
addr: Expr::reg(RSP, 64),
value: Expr::reg(RBX, 64),
width: 8,
at: None,
},
assign(RBX, Expr::reg(7, 64)),
Stmt::Call {
kind: vlk_types::types::CallKind::Invoke,
target: Some(0x100),
through: None,
convention: Convention::SysV,
args: None,
floats_named: false,
abi: None,
clobbers: None,
outputs: vlk_ir::ir::fallback_call_outputs(Convention::SysV, None),
via: None,
at: None,
},
assign(0, Expr::reg(RBX, 64)),
assign(
RBX,
Expr::Load {
addr: Box::new(Expr::reg(RSP, 64)),
width: 8,
signed: false,
},
),
adjust(8),
Stmt::Ret {
value: Expr::reg(0, 64),
at: None,
},
],
);
elide_callee_saves(&mut ir);
let touches_stack = ir.blocks[0].stmts.iter().any(|stmt| {
matches!(stmt, Stmt::Store { .. })
|| matches!(
stmt,
Stmt::Assign {
value: Expr::Load { .. },
..
}
)
});
assert!(!touches_stack, "{}", ir.render());
}
#[test]
fn keeps_a_spill_a_later_path_clobbers() {
let mut ir = FunctionIr::single_block(
Isa::X86_64,
vec![
Stmt::Store {
addr: Expr::reg(RSP, 64),
value: Expr::reg(0, 64),
width: 8,
at: None,
},
Stmt::Br {
target: Some(0x10),
at: None,
},
],
);
ir.blocks.push(Block {
start: 0x10,
stmts: vec![
assign(
0,
Expr::Load {
addr: Box::new(Expr::reg(RSP, 64)),
width: 8,
signed: false,
},
),
Stmt::Brif {
cond: Cond::Eq,
lhs: Expr::reg(7, 64),
rhs: Expr::Const(0),
target: 0x30,
at: None,
},
],
});
ir.blocks.push(Block {
start: 0x18,
stmts: vec![
Stmt::Call {
kind: vlk_types::types::CallKind::Invoke,
target: Some(0x100),
through: None,
convention: Convention::SysV,
args: None,
floats_named: false,
abi: None,
clobbers: None,
outputs: vlk_ir::ir::fallback_call_outputs(Convention::SysV, None),
via: None,
at: None,
},
Stmt::Br {
target: Some(0x10),
at: None,
},
],
});
ir.blocks.push(Block {
start: 0x30,
stmts: vec![Stmt::Ret {
value: Expr::reg(0, 64),
at: None,
}],
});
elide_callee_saves(&mut ir);
assert!(
matches!(ir.blocks[1].stmts.first(), Some(Stmt::Assign { dest, .. }) if dest.index == 0),
"the reload after the call stays:\n{}",
ir.render()
);
}
#[test]
fn keeps_a_frame_pointer_another_path_reads() {
let mut ir = frame_pointer_function(vec![
assign(7, Expr::reg(RBP, 64)),
Stmt::Call {
kind: vlk_types::types::CallKind::Invoke,
target: Some(0x100),
through: None,
convention: Convention::SysV,
args: None,
floats_named: false,
abi: None,
clobbers: None,
outputs: vlk_ir::ir::fallback_call_outputs(Convention::SysV, None),
via: None,
at: None,
},
Stmt::Trap { at: None },
]);
elide_callee_saves(&mut ir);
assert!(
ir.blocks[0]
.stmts
.iter()
.any(|stmt| matches!(stmt, Stmt::Assign { dest, .. } if dest.index == RBP)),
"{}",
ir.render()
);
}
fn slot(offset: i64) -> Expr {
Expr::bin(BinOp::Add, Expr::reg(31, 64), Expr::Const(offset as u64))
}
fn store(offset: i64, value: Expr, width: u8) -> Stmt {
Stmt::Store {
addr: slot(offset),
value,
width,
at: None,
}
}
fn load(offset: i64, width: u8) -> Expr {
Expr::Load {
addr: Box::new(slot(offset)),
width,
signed: false,
}
}
#[test]
fn keeps_a_store_a_wider_load_reads() {
let mut ir = FunctionIr::single_block(
Isa::AArch64,
vec![
store(-4, Expr::Const(5), 4),
Stmt::Ret {
value: load(-8, 8),
at: None,
},
],
);
drop_dead_frame_stores(&mut ir);
assert_eq!(ir.blocks[0].stmts.len(), 2, "{}", ir.render());
}
#[test]
fn drops_a_store_later_stores_cover() {
let mut ir = FunctionIr::single_block(
Isa::AArch64,
vec![
store(-8, Expr::reg(0, 64), 8),
store(-8, Expr::Const(1), 4),
store(-4, Expr::Const(2), 4),
Stmt::Ret {
value: load(-8, 8),
at: None,
},
],
);
drop_dead_frame_stores(&mut ir);
assert_eq!(ir.blocks[0].stmts.len(), 3, "{}", ir.render());
}
#[test]
fn keeps_a_store_an_address_reads_back() {
let mut ir = FunctionIr::single_block(
Isa::AArch64,
vec![
store(-8, Expr::reg(0, 64), 8),
Stmt::Store {
addr: load(-8, 8),
value: Expr::Const(1),
width: 1,
at: None,
},
Stmt::Ret {
value: Expr::reg(0, 64),
at: None,
},
],
);
drop_dead_frame_stores(&mut ir);
assert_eq!(ir.blocks[0].stmts.len(), 3, "{}", ir.render());
}
#[test]
fn keeps_a_store_a_callee_may_read() {
let mut ir = FunctionIr::single_block(
Isa::AArch64,
vec![
store(0, Expr::Const(9), 8),
Stmt::Call {
kind: vlk_types::types::CallKind::Invoke,
target: Some(0x100),
through: None,
convention: Convention::Aapcs64,
args: None,
floats_named: false,
abi: None,
clobbers: None,
outputs: vlk_ir::ir::fallback_call_outputs(Convention::Aapcs64, None),
via: None,
at: None,
},
Stmt::Ret {
value: Expr::reg(0, 64),
at: None,
},
],
);
drop_dead_frame_stores(&mut ir);
assert_eq!(ir.blocks[0].stmts.len(), 3, "{}", ir.render());
}
#[test]
fn declared_register_calls_preserve_stack_height_after_save_elimination() {
for declared in [false, true] {
let mut ir = FunctionIr::single_block(
Isa::X86_64,
vec![
adjust(-16),
Stmt::Store {
addr: Expr::reg(RSP, 64),
value: Expr::reg(3, 64),
width: 8,
at: None,
},
Stmt::Call {
kind: vlk_types::types::CallKind::Invoke,
target: Some(0x1000),
through: None,
convention: Convention::SysV,
args: Some(Vec::new()),
floats_named: true,
clobbers: Some(Default::default()),
outputs: Vec::new(),
abi: declared.then(|| crate::passes::test_call_abi(vec![RSP], false)),
via: None,
at: None,
},
assign(
3,
Expr::Load {
addr: Box::new(Expr::reg(RSP, 64)),
width: 8,
signed: false,
},
),
adjust(16),
Stmt::Ret {
value: Expr::Const(0),
at: None,
},
],
);
elide_callee_saves(&mut ir);
assert!(!ir.blocks[0].stmts.iter().any(|stmt| matches!(
stmt,
Stmt::Store { .. }
| Stmt::Assign {
dest: RegRef { index: 3, .. },
..
}
)));
assert_eq!(
ir.blocks[0]
.stmts
.iter()
.filter(|stmt| matches!(stmt,
Stmt::Assign { dest, .. } if dest.index == RSP))
.count(),
if declared { 2 } else { 0 }
);
}
}
#[test]
fn deferred_call_memory_keeps_an_otherwise_dead_frame_store() {
let mut ir = FunctionIr::single_block(
Isa::AArch64,
vec![
store(-8, Expr::Const(9), 8),
Stmt::Call {
kind: vlk_types::types::CallKind::Invoke,
target: Some(0x1000),
through: None,
convention: Convention::Aapcs64,
args: Some(Vec::new()),
floats_named: true,
clobbers: Some(Default::default()),
outputs: Vec::new(),
abi: Some(crate::passes::test_call_abi(vec![31], true)),
via: None,
at: None,
},
Stmt::Ret {
value: Expr::Const(0),
at: None,
},
],
);
let original = ir.blocks[0].stmts.clone();
drop_dead_frame_stores(&mut ir);
assert_eq!(format!("{:?}", ir.blocks[0].stmts), format!("{original:?}"));
if let Stmt::Call { abi, .. } = &mut ir.blocks[0].stmts[1] {
*abi = None;
}
drop_dead_frame_stores(&mut ir);
assert_eq!(ir.blocks[0].stmts.len(), 2);
assert!(matches!(ir.blocks[0].stmts[0], Stmt::Call { .. }));
}