use super::{forward_stored_values, fuse_load_op_store};
use vlk_base::target::Isa;
use vlk_ir::ir::{BinOp, Expr, FunctionIr, RegRef, Stmt};
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 an_update_read_again_after_its_store_is_not_fused() {
let assign = |value| Stmt::Assign {
dest: RegRef {
index: 0,
width: 64,
},
value,
at: None,
};
let mut ir = FunctionIr::single_block(
Isa::AArch64,
vec![
assign(load(-8, 8)),
assign(Expr::bin(BinOp::Add, Expr::reg(0, 64), Expr::Const(1))),
store(-8, Expr::reg(0, 64), 8),
assign(Expr::bin(BinOp::And, Expr::reg(0, 64), Expr::Const(0xff))),
Stmt::Ret {
value: Expr::reg(0, 64),
at: None,
},
],
);
fuse_load_op_store(&mut ir);
assert_eq!(ir.blocks[0].stmts.len(), 5, "{}", ir.render());
}
#[test]
fn a_value_that_loads_its_own_slot_is_not_forwarded() {
let increment = Expr::bin(BinOp::Add, load(-8, 8), Expr::Const(1));
let mut ir = FunctionIr::single_block(
Isa::AArch64,
vec![
store(-8, increment, 8),
Stmt::Ret {
value: load(-8, 8),
at: None,
},
],
);
forward_stored_values(&mut ir);
assert!(
matches!(&ir.blocks[0].stmts[1], Stmt::Ret { value, .. } if *value == load(-8, 8)),
"{}",
ir.render()
);
}
#[test]
fn a_forwarded_byte_is_extended_like_the_load() {
let byte = |signed| Expr::Load {
addr: Box::new(slot(-1)),
width: 1,
signed,
};
let mut ir = FunctionIr::single_block(
Isa::AArch64,
vec![
store(-1, Expr::reg(0, 64), 1),
Stmt::Ret {
value: Expr::bin(BinOp::Add, byte(true), byte(false)),
at: None,
},
],
);
forward_stored_values(&mut ir);
let expected = Expr::bin(
BinOp::Add,
Expr::SignExtend {
value: Box::new(Expr::reg(0, 64)),
width: 1,
},
Expr::bin(BinOp::And, Expr::reg(0, 64), Expr::Const(0xff)),
);
assert!(
matches!(&ir.blocks[0].stmts[1], Stmt::Ret { value, .. } if *value == expected),
"{}",
ir.render()
);
}
#[test]
fn a_value_whose_register_changes_is_not_forwarded() {
let mut ir = FunctionIr::single_block(
Isa::AArch64,
vec![
store(-8, Expr::reg(0, 64), 8),
Stmt::Assign {
dest: RegRef {
index: 0,
width: 64,
},
value: Expr::Const(1),
at: None,
},
Stmt::Ret {
value: load(-8, 8),
at: None,
},
],
);
forward_stored_values(&mut ir);
assert!(
matches!(&ir.blocks[0].stmts[2], Stmt::Ret { value, .. } if *value == load(-8, 8)),
"{}",
ir.render()
);
}
#[test]
fn a_store_over_part_of_a_slot_stops_forwarding_it() {
let mut ir = FunctionIr::single_block(
Isa::AArch64,
vec![
store(-8, Expr::reg(0, 64), 8),
store(-4, Expr::Const(5), 4),
Stmt::Ret {
value: load(-8, 8),
at: None,
},
],
);
forward_stored_values(&mut ir);
assert!(
matches!(&ir.blocks[0].stmts[2], Stmt::Ret { value, .. } if *value == load(-8, 8)),
"{}",
ir.render()
);
}
#[test]
fn a_variable_kept_in_a_slot_across_blocks_reads_as_itself() {
let v1 = vlk_ir::ir::VARIABLE_BASE;
let call = Stmt::Call {
kind: vlk_types::types::CallKind::Invoke,
target: Some(0x1000),
through: None,
convention: vlk_types::types::Convention::Aapcs64,
args: Some(Vec::new()),
floats_named: false,
abi: None,
outputs: vlk_ir::ir::fallback_call_outputs(vlk_types::types::Convention::Aapcs64, None),
clobbers: None,
via: None,
at: None,
};
let build = |second_store: bool| {
let mut first = vec![
Stmt::Assign {
dest: RegRef {
index: v1,
width: 64,
},
value: Expr::Load {
addr: Box::new(Expr::reg(0, 64)),
width: 8,
signed: false,
},
at: None,
},
store(-8, Expr::reg(v1, 64), 8),
call.clone(),
];
if second_store {
first.push(store(-8, Expr::Const(0), 8));
}
first.push(Stmt::Brif {
cond: vlk_ir::ir::Cond::Eq,
lhs: Expr::reg(2, 64),
rhs: Expr::Const(0),
target: 0x20,
at: None,
});
let mut ir = FunctionIr::single_block(Isa::AArch64, first);
ir.blocks.push(vlk_ir::ir::Block {
start: 0x10,
stmts: vec![Stmt::Ret {
value: Expr::Load {
addr: Box::new(slot(-8)),
width: 8,
signed: false,
},
at: None,
}],
});
ir.blocks.push(vlk_ir::ir::Block {
start: 0x20,
stmts: vec![Stmt::Ret {
value: Expr::Const(0),
at: None,
}],
});
ir
};
let mut ir = build(false);
super::forward_spilled_variables(&mut ir);
assert!(
matches!(&ir.blocks[1].stmts[0], Stmt::Ret { value: Expr::Reg { index, .. }, .. } if *index == v1),
"{}",
ir.render()
);
assert!(
!ir.blocks[0]
.stmts
.iter()
.any(|stmt| matches!(stmt, Stmt::Store { .. })),
"{}",
ir.render()
);
let mut ir = build(true);
super::forward_spilled_variables(&mut ir);
assert!(
matches!(
&ir.blocks[1].stmts[0],
Stmt::Ret {
value: Expr::Load { .. },
..
}
),
"{}",
ir.render()
);
}
#[test]
fn call_forwarding_uses_all_semantic_outputs_not_renamed_physical_output_registers() {
let variable = vlk_ir::ir::VARIABLE_BASE;
let mut outputs = vlk_ir::ir::fallback_call_outputs(
vlk_types::types::Convention::Aapcs64,
Some([1].into_iter().collect()),
);
outputs[0].dest.index = variable;
let mut ir = FunctionIr::single_block(
Isa::AArch64,
vec![
store(16, Expr::reg(variable, 64), 8),
store(24, Expr::reg(1, 64), 8),
Stmt::Call {
kind: vlk_types::types::CallKind::Invoke,
target: Some(0x1000),
through: None,
convention: vlk_types::types::Convention::Aapcs64,
args: Some(Vec::new()),
floats_named: true,
abi: None,
clobbers: Some([1].into_iter().collect()),
outputs,
via: None,
at: None,
},
Stmt::Ret {
value: Expr::bin(vlk_ir::ir::BinOp::Xor, load(16, 8), load(24, 8)),
at: None,
},
],
);
forward_stored_values(&mut ir);
assert!(
matches!(ir.blocks[0].stmts.last(), Some(Stmt::Ret { value, .. }) if *value == Expr::bin(vlk_ir::ir::BinOp::Xor, load(16, 8), Expr::reg(1, 64))),
"{}",
ir.render()
);
}
#[test]
fn a_stored_load_is_not_forwarded_past_a_call_that_may_change_what_it_loads() {
let call = Stmt::Call {
kind: vlk_types::types::CallKind::Invoke,
target: Some(0x1000),
through: None,
convention: vlk_types::types::Convention::Aapcs64,
args: Some(Vec::new()),
floats_named: true,
abi: None,
clobbers: Some(Default::default()),
outputs: Vec::new(),
via: None,
at: None,
};
let mut ir = FunctionIr::single_block(
Isa::AArch64,
vec![
store(32, load(0, 8), 8),
call,
Stmt::Ret {
value: load(32, 8),
at: None,
},
],
);
forward_stored_values(&mut ir);
assert!(
matches!(ir.blocks[0].stmts.last(), Some(Stmt::Ret { value, .. }) if *value == load(32, 8)),
"{}",
ir.render()
);
}
#[test]
fn a_slot_is_not_forwarded_past_a_call_that_may_write_its_results_there() {
let call = Stmt::Call {
kind: vlk_types::types::CallKind::Invoke,
target: Some(0x1000),
through: None,
convention: vlk_types::types::Convention::GoAbi0,
args: Some(vec![Expr::Const(2)]),
floats_named: true,
abi: None,
clobbers: Some(Default::default()),
outputs: Vec::new(),
via: None,
at: None,
};
let mut ir = FunctionIr::single_block(
Isa::AArch64,
vec![
store(32, Expr::Const(13), 8),
call,
Stmt::Ret {
value: load(32, 8),
at: None,
},
],
);
ir.convention = vlk_types::types::Convention::GoAmd64;
forward_stored_values(&mut ir);
assert!(
matches!(ir.blocks[0].stmts.last(), Some(Stmt::Ret { value, .. }) if *value == load(32, 8)),
"{}",
ir.render()
);
}
fn deferred_call(args: Vec<Expr>) -> Stmt {
Stmt::Call {
kind: vlk_types::types::CallKind::Invoke,
target: Some(0x1000),
through: None,
convention: vlk_types::types::Convention::Aapcs64,
args: Some(args),
floats_named: true,
clobbers: Some(Default::default()),
outputs: Vec::new(),
abi: Some(crate::passes::test_call_abi(vec![31], true)),
via: None,
at: None,
}
}
#[test]
fn deferred_call_memory_blocks_forwarding_and_keeps_fallback_loads() {
let mut ir = FunctionIr::single_block(
Isa::AArch64,
vec![
store(16, Expr::Const(9), 8),
deferred_call(vec![load(16, 8)]),
Stmt::Ret {
value: load(16, 8),
at: None,
},
],
);
let original = ir.blocks[0].stmts.clone();
forward_stored_values(&mut ir);
assert_eq!(format!("{:?}", ir.blocks[0].stmts), format!("{original:?}"));
if let Stmt::Call { abi, .. } = &mut ir.blocks[0].stmts[1] {
*abi = None;
}
forward_stored_values(&mut ir);
assert!(
matches!(&ir.blocks[0].stmts[1], Stmt::Call { args: Some(args), .. } if args == &[Expr::Const(9)])
);
assert!(matches!(
ir.blocks[0].stmts[2],
Stmt::Ret {
value: Expr::Const(9),
..
}
));
}
#[test]
fn deferred_call_memory_keeps_a_spilled_variable_store() {
let variable = vlk_ir::ir::VARIABLE_BASE;
let mut ir = FunctionIr::single_block(
Isa::AArch64,
vec![
Stmt::Assign {
dest: RegRef {
index: variable,
width: 64,
},
value: Expr::Const(9),
at: None,
},
store(16, Expr::reg(variable, 64), 8),
deferred_call(Vec::new()),
Stmt::Ret {
value: load(16, 8),
at: None,
},
],
);
let original = ir.blocks[0].stmts.clone();
super::forward_spilled_variables(&mut ir);
assert_eq!(format!("{:?}", ir.blocks[0].stmts), format!("{original:?}"));
}
#[test]
fn deferred_call_memory_blocks_a_load_update_store_fusion() {
let temporary = vlk_ir::ir::VARIABLE_BASE;
let assign = |value| Stmt::Assign {
dest: RegRef {
index: temporary,
width: 64,
},
value,
at: None,
};
let update = || {
assign(Expr::bin(
BinOp::Add,
Expr::reg(temporary, 64),
Expr::Const(1),
))
};
let write = || store(16, Expr::reg(temporary, 64), 8);
let call = || deferred_call(Vec::new());
for mut stmts in [
vec![assign(load(16, 8)), call(), update(), write()],
vec![assign(load(16, 8)), update(), call(), write()],
vec![
assign(Expr::bin(BinOp::Add, load(16, 8), Expr::Const(1))),
call(),
write(),
],
] {
stmts.push(Stmt::Ret {
value: Expr::Const(0),
at: None,
});
let original = format!("{stmts:?}");
let mut ir = FunctionIr::single_block(Isa::AArch64, stmts);
fuse_load_op_store(&mut ir);
assert_eq!(format!("{:?}", ir.blocks[0].stmts), original);
}
}