-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #504 from AeneasVerif/son/cfg
Improve the CFG reconstruction
- Loading branch information
Showing
16 changed files
with
346 additions
and
102 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 |
---|---|---|
@@ -0,0 +1,79 @@ | ||
//! The MIR uses a unique `return` node, which can be an issue when reconstructing | ||
//! the control-flow. | ||
//! | ||
//! For instance, it often leads to code of the following shape: | ||
//! ```text | ||
//! if b { | ||
//! ... | ||
//! x = 0; | ||
//! } | ||
//! else { | ||
//! ... | ||
//! x = 1; | ||
//! } | ||
//! return x; | ||
//! ``` | ||
//! | ||
//! while a more natural reconstruction would be: | ||
//! ```text | ||
//! if b { | ||
//! ... | ||
//! return 0; | ||
//! } | ||
//! else { | ||
//! ... | ||
//! return 1; | ||
//! } | ||
//! ``` | ||
use crate::ids::Generator; | ||
use crate::transform::TransformCtx; | ||
use crate::ullbc_ast::*; | ||
use derive_visitor::{visitor_enter_fn_mut, DriveMut}; | ||
use std::collections::HashMap; | ||
|
||
use super::ctx::UllbcPass; | ||
|
||
pub struct Transform; | ||
impl UllbcPass for Transform { | ||
fn transform_body(&self, _ctx: &mut TransformCtx, b: &mut ExprBody) { | ||
// Find the return block id (there should be one). | ||
let returns: HashMap<BlockId, Span> = b | ||
.body | ||
.iter_indexed() | ||
.filter_map(|(bid, block)| { | ||
if block.statements.is_empty() && block.terminator.content.is_return() { | ||
Some((bid, block.terminator.span)) | ||
} else { | ||
None | ||
} | ||
}) | ||
.collect(); | ||
|
||
// Whenever we find a goto the return block, introduce an auxiliary block | ||
// for this (remark: in the end, it makes the return block dangling). | ||
// We do this in two steps. | ||
// First, introduce fresh ids. | ||
assert!(usize::from(b.body.next_id()) == b.body.len()); | ||
let mut generator = Generator::new_with_init_value(b.body.len()); | ||
let mut new_spans = Vec::new(); | ||
b.body | ||
.drive_mut(&mut visitor_enter_fn_mut(|bid: &mut BlockId| { | ||
if let Some(span) = returns.get(bid) { | ||
*bid = generator.fresh_id(); | ||
new_spans.push(*span); | ||
} | ||
})); | ||
|
||
// Then introduce the new blocks | ||
for span in new_spans { | ||
let _ = b.body.push(BlockData { | ||
statements: Vec::new(), | ||
terminator: Terminator { | ||
span, | ||
content: RawTerminator::Return, | ||
}, | ||
}); | ||
} | ||
} | ||
} |
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,42 @@ | ||
//! Some passes like [`reconstruct_assert`] lead to the apparition of "dangling" blocks, | ||
//! which are referenced nowhere and thus become useless. This pass filters those out. | ||
use std::collections::{HashMap, HashSet}; | ||
|
||
use crate::transform::TransformCtx; | ||
use crate::ullbc_ast::*; | ||
use derive_visitor::{visitor_enter_fn_mut, DriveMut}; | ||
|
||
use super::ctx::UllbcPass; | ||
|
||
pub struct Transform; | ||
impl UllbcPass for Transform { | ||
fn transform_body(&self, _ctx: &mut TransformCtx, b: &mut ExprBody) { | ||
// Perform a depth-first search to identify all the blocks reachable | ||
// from the first block. | ||
let mut explored: HashSet<BlockId> = HashSet::new(); | ||
let mut to_explore: Vec<BlockId> = vec![BlockId::from_usize(0)]; | ||
while let Some(bid) = to_explore.pop() { | ||
if explored.contains(&bid) { | ||
continue; | ||
} | ||
explored.insert(bid); | ||
to_explore.append(&mut b.body[bid].targets()) | ||
} | ||
|
||
// Renumerotate | ||
let mut bid_map: HashMap<BlockId, BlockId> = HashMap::new(); | ||
for (bid, block) in std::mem::take(&mut b.body).into_iter_indexed() { | ||
if explored.contains(&bid) { | ||
let nbid = b.body.push(block); | ||
bid_map.insert(bid, nbid); | ||
} | ||
} | ||
|
||
// Update all block ids | ||
b.body | ||
.drive_mut(&mut visitor_enter_fn_mut(|bid: &mut BlockId| { | ||
*bid = *bid_map.get(bid).unwrap(); | ||
})); | ||
} | ||
} |
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
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
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.