use llvm_pm::inkwell;
use llvm_pm::inkwell::context::Context;
use llvm_pm::inkwell::values::InstructionOpcode;
use llvm_pm::inkwell::IntPredicate;
use llvm_pm::traits::{
LlvmCgsccAnalysis, LlvmCgsccPass, LlvmFunctionPass, LlvmModulePass, PreservedAnalyses,
};
use llvm_pm::{
CgsccAnalysisManager, FunctionAnalysisManager, FunctionPassManager, ModuleAnalysisManager,
ModulePassManager,
};
use std::sync::atomic::{AtomicBool, AtomicU32, Ordering};
use std::sync::Arc;
struct BasicBlockCountAnalysis;
impl LlvmCgsccAnalysis for BasicBlockCountAnalysis {
type Result = u32;
fn run_analysis(
&self,
function: &inkwell::values::FunctionValue<'_>,
_manager: &CgsccAnalysisManager,
) -> Self::Result {
function.count_basic_blocks()
}
fn id() -> *const u8 {
static ID: u8 = 0;
&ID
}
}
struct BasicBlockReportPass {
registered: Arc<AtomicBool>,
}
impl LlvmCgsccPass for BasicBlockReportPass {
fn run_pass(
&self,
function: &mut inkwell::values::FunctionValue<'_>,
manager: &CgsccAnalysisManager,
) -> PreservedAnalyses {
if self
.registered
.compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst)
.is_ok()
{
manager.add_analysis(BasicBlockCountAnalysis);
}
let bb_count = *manager.get_result::<BasicBlockCountAnalysis>(function);
let name = function.get_name().to_string_lossy();
println!(" [CGSCC] {}: {} basic blocks", name, bb_count);
PreservedAnalyses::All
}
}
struct SimpleDCEPass {
removed: Arc<AtomicU32>,
}
fn may_have_side_effects(opcode: InstructionOpcode) -> bool {
matches!(
opcode,
InstructionOpcode::Store
| InstructionOpcode::Call
| InstructionOpcode::Fence
| InstructionOpcode::AtomicCmpXchg
| InstructionOpcode::AtomicRMW
| InstructionOpcode::Resume
| InstructionOpcode::LandingPad
| InstructionOpcode::IndirectBr
| InstructionOpcode::Invoke
| InstructionOpcode::Unreachable
)
}
impl LlvmFunctionPass for SimpleDCEPass {
fn run_pass(
&self,
function: &mut inkwell::values::FunctionValue<'_>,
_manager: &FunctionAnalysisManager,
) -> PreservedAnalyses {
let mut changed = false;
for bb in function.get_basic_blocks() {
let dead: Vec<_> = bb
.get_instructions()
.filter(|inst| {
if inst.is_terminator() {
return false;
}
if may_have_side_effects(inst.get_opcode()) {
return false;
}
if inst.get_opcode() == InstructionOpcode::Alloca {
return false;
}
inst.get_first_use().is_none()
})
.collect();
for inst in dead {
inst.erase_from_basic_block();
self.removed.fetch_add(1, Ordering::SeqCst);
changed = true;
}
}
if changed {
PreservedAnalyses::None
} else {
PreservedAnalyses::All
}
}
}
struct DCEStatsPass {
label: &'static str,
}
impl LlvmModulePass for DCEStatsPass {
fn run_pass(
&self,
module: &mut inkwell::module::Module<'_>,
_manager: &ModuleAnalysisManager,
) -> PreservedAnalyses {
println!("--- {} ---", self.label);
let mut total_insts = 0u32;
let mut total_bbs = 0u32;
for func in module.get_functions() {
if func.count_basic_blocks() == 0 {
continue;
}
let mut insts = 0u32;
for bb in func.get_basic_blocks() {
insts += bb.get_instructions().count() as u32;
}
let bbs = func.count_basic_blocks();
let name = func.get_name().to_string_lossy();
println!(" {}: {} BBs, {} instructions", name, bbs, insts);
total_insts += insts;
total_bbs += bbs;
}
println!(" Total: {} BBs, {} instructions", total_bbs, total_insts);
PreservedAnalyses::All
}
}
fn build_module_with_dead_code() -> inkwell::module::Module<'static> {
let context = Box::leak(Box::new(Context::create()));
let module = context.create_module("dce_demo");
let i32_ty = context.i32_type();
let fn_ty = i32_ty.fn_type(&[i32_ty.into(), i32_ty.into()], false);
let func = module.add_function("add_with_dead_code", fn_ty, None);
let bb = context.append_basic_block(func, "entry");
let b = context.create_builder();
b.position_at_end(bb);
let a = func.get_nth_param(0).unwrap().into_int_value();
let b_param = func.get_nth_param(1).unwrap().into_int_value();
let sum = b.build_int_add(a, b_param, "sum").unwrap();
let _dead_mul = b.build_int_mul(a, b_param, "dead_mul").unwrap();
let _dead_sub = b.build_int_sub(a, b_param, "dead_sub").unwrap();
let _dead_xor = b.build_xor(a, b_param, "dead_xor").unwrap();
b.build_return(Some(&sum)).unwrap();
let fn_ty2 = i32_ty.fn_type(&[i32_ty.into()], false);
let func2 = module.add_function("branch_with_dead_code", fn_ty2, None);
let entry = context.append_basic_block(func2, "entry");
let then_bb = context.append_basic_block(func2, "then");
let else_bb = context.append_basic_block(func2, "else");
let merge = context.append_basic_block(func2, "merge");
let b = context.create_builder();
b.position_at_end(entry);
let x = func2.get_first_param().unwrap().into_int_value();
let cmp = b
.build_int_compare(IntPredicate::SGT, x, i32_ty.const_int(10, false), "cmp")
.unwrap();
b.build_conditional_branch(cmp, then_bb, else_bb).unwrap();
b.position_at_end(then_bb);
let then_val = b
.build_int_mul(x, i32_ty.const_int(2, false), "then_val")
.unwrap();
let _dead_then = b
.build_int_add(x, i32_ty.const_int(99, false), "dead_then")
.unwrap();
b.build_unconditional_branch(merge).unwrap();
b.position_at_end(else_bb);
let else_val = b
.build_int_add(x, i32_ty.const_int(1, false), "else_val")
.unwrap();
let _dead_else = b
.build_int_sub(x, i32_ty.const_int(42, false), "dead_else")
.unwrap();
let _dead_else2 = b.build_int_mul(x, x, "dead_else2").unwrap();
b.build_unconditional_branch(merge).unwrap();
b.position_at_end(merge);
let phi = b.build_phi(i32_ty, "result").unwrap();
phi.add_incoming(&[(&then_val, then_bb), (&else_val, else_bb)]);
let result = phi.as_basic_value().into_int_value();
b.build_return(Some(&result)).unwrap();
module
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let module = build_module_with_dead_code();
module.verify()?;
let removed = Arc::new(AtomicU32::new(0));
{
let mut pm = ModulePassManager::new(None, None)?;
pm.add_pass(DCEStatsPass {
label: "Before DCE",
});
pm.add_cgscc_pass(BasicBlockReportPass {
registered: Arc::new(AtomicBool::new(false)),
});
pm.run(&module)?;
}
{
let mut fpm = FunctionPassManager::new(None, None)?;
fpm.add_pass(SimpleDCEPass {
removed: removed.clone(),
});
for func in module.get_functions() {
if func.count_basic_blocks() == 0 {
continue;
}
fpm.run(func)?;
}
}
module.verify()?;
{
let mut pm = ModulePassManager::new(None, None)?;
pm.add_pass(DCEStatsPass { label: "After DCE" });
pm.run(&module)?;
}
let total_removed = removed.load(Ordering::SeqCst);
println!("\nRemoved {} dead instructions", total_removed);
assert!(
total_removed >= 5,
"Expected to remove at least 5 dead instructions, got {}",
total_removed
);
println!("ok");
Ok(())
}