use std::collections::{BTreeMap, BTreeSet, HashMap};
use binbun_semantics::core::Arena;
use crate::cfg::recover::successors;
use crate::cfg::{
Block, BlockId, Destination, EdgeKind, Exit, Frame, Function, SeedKind, Table, Unwind,
};
pub(crate) struct Graph<'a> {
pub(crate) blocks: &'a Arena<BlockId, Block>,
pub(crate) by_start: &'a BTreeMap<u64, BlockId>,
pub(crate) tables: &'a BTreeMap<u64, Table>,
pub(crate) extents: &'a BTreeMap<u64, u64>,
pub(crate) frames: &'a BTreeMap<u64, Frame>,
pub(crate) splits: &'a BTreeMap<u64, (u64, Option<u64>)>,
pub(crate) unwind: &'a Unwind,
pub(crate) ends: &'a BTreeMap<u64, u64>,
}
#[derive(Clone, Debug, Default)]
struct Entered {
from: Vec<(BlockId, bool)>,
called: bool,
}
struct Join<'f, 'g> {
functions: &'f [Function],
graph: &'g Graph<'g>,
by_entry: HashMap<u64, usize>,
owners: HashMap<BlockId, Vec<usize>>,
entered: Vec<Entered>,
parent: Vec<usize>,
}
pub(crate) fn join(functions: &mut Vec<Function>, graph: &Graph<'_>) {
let (parent, part_of) = {
let mut join = Join::new(functions, graph);
while join.round() {}
let part_of = join.keep_unreached();
(join.parent, part_of)
};
merge(functions, graph, &parent, &part_of);
}
impl<'f, 'g> Join<'f, 'g> {
fn new(functions: &'f [Function], graph: &'g Graph<'g>) -> Self {
let by_entry = functions
.iter()
.enumerate()
.map(|(index, function)| (function.entry, index))
.collect();
let mut owners: HashMap<BlockId, Vec<usize>> = HashMap::new();
for (index, function) in functions.iter().enumerate() {
for &block in &function.blocks {
owners.entry(block).or_default().push(index);
}
}
let mut join = Self {
functions,
graph,
by_entry,
owners,
entered: vec![Entered::default(); functions.len()],
parent: (0..functions.len()).collect(),
};
join.find_entries();
join
}
fn find_entries(&mut self) {
let graph = self.graph;
for (id, block) in graph.blocks.iter() {
if let Exit::Call {
callee: Destination::Address(target),
..
} = block.exit
&& let Some(entered) = self
.by_entry
.get(&target)
.and_then(|&index| self.entered.get_mut(index))
{
entered.called = true;
}
let Some(from) = self.owners.get(&id) else {
continue;
};
for (kind, target) in successors(graph.tables, graph.unwind, *block) {
let Some(&index) = self.by_entry.get(&target) else {
continue;
};
if from.contains(&index) {
continue;
}
if let Some(entered) = self.entered.get_mut(index) {
entered.from.push((id, kind == EdgeKind::Taken));
}
}
}
}
fn root(&self, mut index: usize) -> usize {
while let Some(&next) = self.parent.get(index)
&& next != index
{
index = next;
}
index
}
fn round(&mut self) -> bool {
let mut changed = false;
for index in 0..self.functions.len() {
if self.root(index) != index || !self.may_be_part(index) {
continue;
}
if let Some(owner) = self.split_owner(index)
&& let Some(parent) = self.parent.get_mut(index)
{
*parent = owner;
changed = true;
continue;
}
if self
.entered
.get(index)
.is_none_or(|entered| entered.from.is_empty())
{
continue;
}
let Some(owner) = self.single_owner(index) else {
continue;
};
if self
.functions
.get(owner)
.is_some_and(|f| self.graph.ends.contains_key(&f.entry))
{
continue;
}
if self.evidence(index, owner)
&& let Some(parent) = self.parent.get_mut(index)
{
*parent = owner;
changed = true;
}
}
changed
}
fn may_be_part(&self, index: usize) -> bool {
let (Some(function), Some(entered)) = (self.functions.get(index), self.entered.get(index))
else {
return false;
};
let seeds = function.seeds;
!entered.called
&& self.graph.frames.get(&function.entry) != Some(&Frame::Outermost)
&& !seeds.contains(SeedKind::Entry)
&& !seeds.contains(SeedKind::Export)
&& !seeds.contains(SeedKind::Pointer)
&& !seeds.contains(SeedKind::Reference)
&& !seeds.contains(SeedKind::User)
}
fn single_owner(&self, index: usize) -> Option<usize> {
let entered = self.entered.get(index)?;
let mut owner = None;
for (block, _) in &entered.from {
for &from in self.owners.get(block).into_iter().flatten() {
let root = self.root(from);
if root == index {
continue;
}
match owner {
None => owner = Some(root),
Some(known) if known == root => {}
Some(_) => return None,
}
}
}
owner
}
fn evidence(&self, index: usize, owner: usize) -> bool {
let Some(function) = self.functions.get(index) else {
return false;
};
let conditional = self
.entered
.get(index)
.is_some_and(|entered| entered.from.iter().any(|&(_, conditional)| conditional));
conditional
|| self.graph.frames.get(&function.entry) == Some(&Frame::Inside)
|| self.split_off(function.entry)
|| self.jumps_back(function, owner)
}
fn split_off(&self, address: u64) -> bool {
self.split_at(address).is_some()
}
fn split_at(&self, address: u64) -> Option<(u64, Option<u64>)> {
self.graph
.splits
.range(..=address)
.next_back()
.filter(|&(_, &(end, _))| address < end)
.map(|(&start, &(_, owner))| (start, owner))
}
fn split_owner(&self, index: usize) -> Option<usize> {
let entry = self.functions.get(index)?.entry;
let (_, owner) = self.split_at(entry)?;
let root = self.root(*self.by_entry.get(&owner?)?);
(root != index).then_some(root)
}
fn jumps_back(&self, function: &Function, owner: usize) -> bool {
let graph = self.graph;
function
.blocks
.iter()
.filter_map(|&id| graph.blocks.get(id))
.flat_map(|&block| successors(graph.tables, graph.unwind, block))
.any(|(_, target)| self.inside(target, owner))
}
fn inside(&self, address: u64, owner: usize) -> bool {
let graph = self.graph;
let by_extent = graph
.extents
.range(..=address)
.next_back()
.filter(|&(_, &end)| address < end)
.and_then(|(start, _)| self.by_entry.get(start).copied());
let by_block = graph
.by_start
.get(&address)
.and_then(|block| self.owners.get(block))
.into_iter()
.flatten()
.copied();
by_extent.into_iter().chain(by_block).any(|index| {
self.root(index) == owner
&& self
.functions
.get(index)
.is_some_and(|function| function.entry != address)
})
}
fn keep_unreached(&mut self) -> Vec<Option<u64>> {
let mut groups: BTreeMap<usize, Vec<usize>> = BTreeMap::new();
for index in 0..self.functions.len() {
let root = self.root(index);
if root != index {
groups.entry(root).or_default().push(index);
}
}
let mut part_of = vec![None; self.functions.len()];
for (owner, members) in groups {
let Some(entry) = self.functions.get(owner).map(|function| function.entry) else {
continue;
};
let reached = self.reached(owner, entry);
for member in members {
let unreached = self
.functions
.get(member)
.is_some_and(|function| !reached.contains(&function.entry));
if unreached && let Some(slot) = part_of.get_mut(member) {
*slot = Some(entry);
}
}
}
for (index, owner) in part_of.iter().enumerate() {
if owner.is_some()
&& let Some(parent) = self.parent.get_mut(index)
{
*parent = index;
}
}
part_of
}
fn reached(&self, owner: usize, entry: u64) -> BTreeSet<u64> {
let graph = self.graph;
let mut reached = BTreeSet::new();
let mut work = vec![entry];
while let Some(address) = work.pop() {
let Some(&id) = graph.by_start.get(&address) else {
continue;
};
let ours = self
.owners
.get(&id)
.is_some_and(|indices| indices.iter().any(|&index| self.root(index) == owner));
if !ours || !reached.insert(address) {
continue;
}
if let Some(&block) = graph.blocks.get(id) {
work.extend(
successors(graph.tables, graph.unwind, block).map(|(_, target)| target),
);
}
}
reached
}
}
pub(crate) fn funclets(functions: &mut [Function], funclets: &BTreeMap<u64, u64>) {
let mut owners: HashMap<u64, usize> = HashMap::new();
for (index, function) in functions.iter().enumerate() {
owners.insert(function.entry, index);
for &part in &function.parts {
owners.entry(part).or_insert(index);
}
}
let root = |mut address: u64| {
for _ in 0..funclets.len() {
match funclets.get(&address) {
Some(&function) if function != address => address = function,
_ => break,
}
}
address
};
for &address in funclets.keys() {
let (Some(&funclet), Some(&owner)) = (owners.get(&address), owners.get(&root(address)))
else {
continue;
};
let Some(entry) = functions.get(owner).map(|f| f.entry) else {
continue;
};
if funclet == owner || functions.get(funclet).is_none_or(|f| f.entry != address) {
continue;
}
if let Some(function) = functions.get_mut(funclet) {
function.part_of = Some(entry);
}
if let Some(function) = functions.get_mut(owner) {
function.funclets.push(address);
}
}
for function in functions {
function.funclets.sort_unstable();
function.funclets.dedup();
}
}
fn merge(
functions: &mut Vec<Function>,
graph: &Graph<'_>,
parent: &[usize],
part_of: &[Option<u64>],
) {
let root = |mut index: usize| {
while let Some(&next) = parent.get(index)
&& next != index
{
index = next;
}
index
};
for (index, &owner) in part_of.iter().enumerate() {
if let Some(function) = functions.get_mut(index) {
function.part_of = owner;
}
}
for index in 0..functions.len() {
let owner = root(index);
if owner == index {
continue;
}
let Some(part) = functions.get_mut(index) else {
continue;
};
let entry = part.entry;
let returns = part.returns;
let blocks = core::mem::take(&mut part.blocks);
let parts = core::mem::take(&mut part.parts);
let pads = core::mem::take(&mut part.landing_pads);
if let Some(owner) = functions.get_mut(owner) {
owner.blocks.extend(blocks);
owner.parts.push(entry);
owner.parts.extend(parts);
owner.landing_pads.extend(pads);
owner.returns |= returns;
}
}
let mut index = 0;
functions.retain(|_| {
let keep = root(index) == index;
index += 1;
keep
});
for function in functions.iter_mut().filter(|f| !f.parts.is_empty()) {
function
.blocks
.sort_unstable_by_key(|&id| graph.blocks.get(id).map_or(u64::MAX, |b| b.start));
function.blocks.dedup();
function.parts.sort_unstable();
function.landing_pads.sort_unstable();
function.landing_pads.dedup();
}
}