-
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
1 parent
f0a5936
commit ca9cf03
Showing
22 changed files
with
942 additions
and
1,540 deletions.
There are no files selected for viewing
78 changes: 0 additions & 78 deletions
78
compilers/rustyc/optimizer/src/ir_optimizer/anaylsis/dfs_ordering.rs
This file was deleted.
Oops, something went wrong.
25 changes: 25 additions & 0 deletions
25
compilers/rustyc/optimizer/src/ir_optimizer/anaylsis/dfs_ordering/debugger.rs
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,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 | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
compilers/rustyc/optimizer/src/ir_optimizer/anaylsis/dfs_ordering/mod.rs
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,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); | ||
// } | ||
} |
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
Oops, something went wrong.