Skip to content

Commit

Permalink
feat: add test case for ir anaylsis
Browse files Browse the repository at this point in the history
  • Loading branch information
Solo-steven committed Sep 22, 2024
1 parent f0a5936 commit ca9cf03
Show file tree
Hide file tree
Showing 22 changed files with 942 additions and 1,540 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
use super::DFSOrdering;
use crate::ir::function::BasicBlock;
use crate::ir::function::Function;
use crate::ir_optimizer::anaylsis::DebuggerAnaylsis;
use crate::ir_optimizer::print_table_helper::{get_max_block_id_len, print_divider, print_header};

impl DebuggerAnaylsis<Vec<BasicBlock>> for DFSOrdering {
fn debugger(&mut self, function: &Function, table: &Vec<BasicBlock>) -> String {
let mut output_string = String::new();
let max_block_id_len = get_max_block_id_len(function);
let row_len = if "DFS Ordering".len() > max_block_id_len {
"DFS Ordering".len() + 2
} else {
max_block_id_len + 2
};
output_string.push_str(print_divider(row_len).as_str());
output_string.push_str(print_header("DFS Ordering", row_len).as_str());
output_string.push_str(print_divider(row_len).as_str());
for block in table {
output_string.push_str(&print_header(format!("BB {}", block.0).as_str(), row_len));
}
output_string.push_str(print_divider(row_len).as_str());
output_string
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
mod debugger;

use crate::ir::function::*;
use crate::ir_optimizer::anaylsis::OptimizerAnaylsis;
/// This module implement depth frist search ordering algorithm
/// this algorithm can construct the order of CFG even if there
/// are cycle in CFG, the order can be use to iterative data flow
/// algorithm to reduce the time of iteration
use std::mem::take;
pub struct DFSOrdering {
index: usize,
marks: Vec<bool>,
orders: Vec<BasicBlock>,
}

impl OptimizerAnaylsis<Vec<BasicBlock>> for DFSOrdering {
fn anaylsis(&mut self, function: &Function) -> Vec<BasicBlock> {
let entry_id = function.entry_block[0];
let blocks = &function.blocks;
// init data
self.index = blocks.len();
self.marks = vec![false; blocks.len()];
// dfs ordering
self.dfs_visit(entry_id, blocks);
// take ownership of orders.
let mut orders = take(&mut self.orders);
orders.reverse();
orders
}
}

impl DFSOrdering {
/// Create a DFS ordering struct to get the order of blocks in a cfg.
pub fn new() -> Self {
Self {
index: 0,
marks: Vec::new(),
orders: Vec::new(),
}
}
fn dfs_visit(&mut self, block: BasicBlock, blocks: &BasicBlockMap) {
if self.marks[block.0 - 1] == true {
return;
}
self.marks[block.0 - 1] = true;
for sucessor in &blocks.get(&block).unwrap().successor {
self.dfs_visit(sucessor.clone(), blocks);
}
self.orders.push(block);
}
// pub fn get_order_with_map(
// &mut self,
// entry_id: BasicBlock,
// blocks: &BasicBlockMap,
// ) -> (Vec<BasicBlock>, HashMap<BasicBlock, usize>) {
// let mut map = HashMap::new();
// // init data
// self.index = blocks.len();
// self.marks = vec![false; blocks.len()];
// // dfs ordering
// self.dfs_visit_with_map(entry_id, blocks, &mut map);
// // take ownership of orders.
// let mut orders = take(&mut self.orders);
// orders.reverse();
// (orders, map)
// }
// fn dfs_visit_with_map(
// &mut self,
// block: BasicBlock,
// blocks: &BasicBlockMap,
// map: &mut HashMap<BasicBlock, usize>,
// ) {
// if self.marks[block.0 - 1] == true {
// return;
// }
// self.marks[block.0 - 1] = true;
// for sucessor in &blocks.get(&block).unwrap().successor {
// self.dfs_visit_with_map(sucessor.clone(), blocks, map);
// }
// map.insert(block, self.index);
// self.orders.push(block);
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl DomAnaylsier {
// iterative algorithm for dom data flow analysis
let mut is_change = true;
let mut dfs_ordering_anaylsiser = DFSOrdering::new();
let ordering = dfs_ordering_anaylsiser.get_order(function.entry_block[0], &function.blocks);
let ordering = dfs_ordering_anaylsiser.anaylsis(function);
while is_change {
is_change = false;
for block_id in &ordering {
Expand Down
Loading

0 comments on commit ca9cf03

Please sign in to comment.