pub mod coalesce;
pub mod copies;
pub mod cse;
pub mod dce;
pub mod deobfuscation;
pub mod division;
pub mod frame;
pub mod frame_slots;
pub mod layout;
mod mba;
pub mod memory;
pub mod read_once;
pub mod simplify;
pub mod widths;
use vlk_ir::ir::FunctionIr;
#[inline]
pub(crate) fn checked<R>(
site: &'static str,
ir: &mut FunctionIr,
run: impl FnOnce(&mut FunctionIr) -> R,
) -> R {
checked_assuming(site, ir, &Callees::default(), run)
}
#[derive(Debug, Default)]
pub struct Callees {
pub narrow_results: Vec<u64>,
pub without_result_address: Vec<u64>,
}
#[inline]
pub(crate) fn checked_assuming<R>(
site: &'static str,
ir: &mut FunctionIr,
callees: &Callees,
run: impl FnOnce(&mut FunctionIr) -> R,
) -> R {
#[cfg(feature = "validate")]
if let Some(validation) = VALIDATION.get()
&& (validation.enabled)()
{
let before = ir.blocks.clone();
let result = run(ir);
(validation.applied)(site, before, ir, callees);
return result;
}
#[cfg(not(feature = "validate"))]
let _ = (site, callees);
run(ir)
}
#[cfg(feature = "validate")]
pub struct Validation {
pub enabled: fn() -> bool,
pub applied: fn(&'static str, Vec<vlk_ir::ir::Block>, &FunctionIr, &Callees),
}
#[cfg(feature = "validate")]
pub static VALIDATION: std::sync::OnceLock<Validation> = std::sync::OnceLock::new();
#[cfg(test)]
pub(crate) fn test_call_abi(
dependencies: Vec<u16>,
deferred_memory: bool,
) -> std::sync::Arc<vlk_types::types::calls::CallAbi> {
use std::sync::Arc;
use vlk_types::types::calls::{CallAbi, CallDeclaration, CallFallback};
use vlk_types::types::{AbiIssue, CType, Convention, DeclaredSignature};
Arc::new(CallAbi {
kind: vlk_types::types::CallKind::Invoke,
declaration: Arc::new(CallDeclaration {
signature: DeclaredSignature {
params: Vec::new(),
ret: CType::Void,
variadic: false,
abi: Err(AbiIssue::UnknownLayout),
},
sources: Vec::new(),
}),
target: 0,
convention: Convention::Aapcs64,
arguments: Vec::new(),
hidden_result: None,
dependencies,
deferred_memory,
fallback: CallFallback::Named {
count: 0,
floats_named: true,
},
})
}
pub fn optimize(ir: &mut FunctionIr, has_jump_tables: bool) -> deobfuscation::Simplification {
cse::cse(ir);
simplify::simplify(ir);
division::recover_divisions(ir);
simplify::simplify(ir);
frame::elide_frame_pointer(ir);
frame::elide_callee_saves(ir);
copies::copy_propagate(ir);
dce::dce(ir);
frame::drop_alignment_pushes(ir);
if !has_jump_tables {
dce::dce_flow(ir);
}
simplify::simplify(ir);
copies::copy_propagate_guarded(ir);
dce::dce(ir);
if !has_jump_tables {
dce::dce_flow(ir);
memory::forward_stored_values(ir);
copies::inline_local_copies(ir);
copies::copy_propagate_guarded(ir);
simplify::simplify(ir);
dce::dce(ir);
}
memory::fuse_load_op_store(ir);
let mut changes = deobfuscation::predicates(ir);
changes.mba_expressions_simplified = mba::simplify(ir);
changes.simplified |= changes.mba_expressions_simplified != 0;
simplify::simplify(ir);
dce::dce(ir);
changes
}