-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
136 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
/target | ||
|
||
/.idea/* | ||
|
||
/.vscode/* | ||
!settings.json | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
use rustc_hash::FxHashSet; | ||
|
||
use crate::{ | ||
collections::linked_list::LinkedListContainerPtr, | ||
ir::{ | ||
passman::{GlobalPassMut, PassResult, TransformPass}, | ||
Context, | ||
InstKind, | ||
Symbol, | ||
SymbolKind, | ||
}, | ||
}; | ||
|
||
pub const GLOBAL_DCE: &str = "global-dce"; | ||
|
||
pub struct GlobalDce; | ||
|
||
impl GlobalPassMut for GlobalDce { | ||
type Output = (); | ||
|
||
fn run(&mut self, ctx: &mut Context) -> PassResult<(Self::Output, bool)> { | ||
use InstKind as Ik; | ||
|
||
let mut func_uses = FxHashSet::default(); | ||
let mut slot_uses = FxHashSet::default(); | ||
|
||
for func in ctx.funcs() { | ||
for block in func.iter(ctx) { | ||
for inst in block.iter(ctx) { | ||
if let Ik::Call(sym) = inst.kind(ctx) { | ||
if let Some(SymbolKind::FuncDef(callee)) = ctx.lookup_symbol(sym) { | ||
if func != *callee { | ||
// ignore recursive calls | ||
func_uses.insert(*callee); | ||
} | ||
} | ||
} else if let Ik::GetGlobal(sym) = inst.kind(ctx) { | ||
match ctx.lookup_symbol(sym) { | ||
Some(SymbolKind::FuncDef(callee)) => { | ||
if func != *callee { | ||
func_uses.insert(*callee); | ||
} | ||
} | ||
Some(SymbolKind::GlobalSlot(slot)) => { | ||
slot_uses.insert(*slot); | ||
} | ||
_ => {} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
let main_func = ctx.lookup_func(&Symbol::from("main")).unwrap(); | ||
func_uses.insert(main_func); | ||
|
||
let mut funcs_to_remove = Vec::new(); | ||
let mut slots_to_remove = Vec::new(); | ||
|
||
for func in ctx.funcs() { | ||
if !func_uses.contains(&func) { | ||
funcs_to_remove.push(func); | ||
} | ||
} | ||
|
||
for slot in ctx.global_slots() { | ||
if !slot_uses.contains(&slot) { | ||
slots_to_remove.push(slot); | ||
} | ||
} | ||
|
||
for func in funcs_to_remove { | ||
func.remove(ctx); | ||
} | ||
|
||
for slot in slots_to_remove { | ||
slot.remove(ctx); | ||
} | ||
|
||
PassResult::Ok(((), false)) // only run once. | ||
} | ||
} | ||
|
||
impl TransformPass for GlobalDce { | ||
fn register(passman: &mut crate::ir::passman::PassManager) | ||
where | ||
Self: Sized, | ||
{ | ||
passman.register_transform(GLOBAL_DCE, GlobalDce, Vec::new()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters