2020-03-29 15:19:48 +00:00
|
|
|
use rustc_index::vec::{Idx, IndexVec};
|
2020-03-29 14:41:09 +00:00
|
|
|
use rustc_middle::mir::*;
|
|
|
|
use rustc_middle::ty::Ty;
|
2019-12-31 17:15:40 +00:00
|
|
|
use rustc_span::Span;
|
2016-05-16 23:26:18 +00:00
|
|
|
|
|
|
|
/// This struct represents a patch to MIR, which can add
|
|
|
|
/// new statements and basic blocks and patch over block
|
|
|
|
/// terminators.
|
|
|
|
pub struct MirPatch<'tcx> {
|
2016-06-07 14:28:36 +00:00
|
|
|
patch_map: IndexVec<BasicBlock, Option<TerminatorKind<'tcx>>>,
|
2016-05-16 23:26:18 +00:00
|
|
|
new_blocks: Vec<BasicBlockData<'tcx>>,
|
|
|
|
new_statements: Vec<(Location, StatementKind<'tcx>)>,
|
2016-09-24 23:38:27 +00:00
|
|
|
new_locals: Vec<LocalDecl<'tcx>>,
|
2016-05-16 23:26:18 +00:00
|
|
|
resume_block: BasicBlock,
|
2016-09-24 23:38:27 +00:00
|
|
|
next_local: usize,
|
2016-05-16 23:26:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<'tcx> MirPatch<'tcx> {
|
2019-06-03 22:26:48 +00:00
|
|
|
pub fn new(body: &Body<'tcx>) -> Self {
|
2016-05-16 23:26:18 +00:00
|
|
|
let mut result = MirPatch {
|
2019-06-03 22:26:48 +00:00
|
|
|
patch_map: IndexVec::from_elem(None, body.basic_blocks()),
|
2016-05-16 23:26:18 +00:00
|
|
|
new_blocks: vec![],
|
|
|
|
new_statements: vec![],
|
2016-09-24 23:38:27 +00:00
|
|
|
new_locals: vec![],
|
2019-06-03 22:26:48 +00:00
|
|
|
next_local: body.local_decls.len(),
|
2018-01-31 20:34:13 +00:00
|
|
|
resume_block: START_BLOCK,
|
2016-05-16 23:26:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// make sure the MIR we create has a resume block. It is
|
|
|
|
// completely legal to convert jumps to the resume block
|
|
|
|
// to jumps to None, but we occasionally have to add
|
|
|
|
// instructions just before that.
|
|
|
|
|
|
|
|
let mut resume_block = None;
|
|
|
|
let mut resume_stmt_block = None;
|
2019-06-03 22:26:48 +00:00
|
|
|
for (bb, block) in body.basic_blocks().iter_enumerated() {
|
2016-06-07 18:20:50 +00:00
|
|
|
if let TerminatorKind::Resume = block.terminator().kind {
|
2020-02-28 13:20:33 +00:00
|
|
|
if !block.statements.is_empty() {
|
Add `EndRegion` statement kind to MIR.
* Emit `EndRegion` for every code-extent for which we observe a
borrow. To do this, we needed to thread source info back through
to `fn in_scope`, which makes this commit a bit more painful than
one might have expected.
* There is `end_region` emission in `Builder::pop_scope` and in
`Builder::exit_scope`; the first handles falling out of a scope
normally, the second handles e.g. `break`.
* Remove `EndRegion` statements during the erase_regions mir
transformation.
* Preallocate the terminator block, and throw an `Unreachable` marker
on it from the outset. Then overwrite that Terminator as necessary
on demand.
* Instead of marking the scope as needs_cleanup after seeing a
borrow, just treat every scope in the chain as being part of the
diverge_block (after any *one* of them has separately signalled
that it needs cleanup, e.g. due to having a destructor to run).
* Allow for resume terminators to be patched when looking up drop flags.
(In particular, `MirPatch::new` has an explicit code path,
presumably previously unreachable, that patches up such resume
terminators.)
* Make `Scope` implement `Debug` trait.
* Expanded a stray comment: we do not emit StorageDead on diverging
paths, but that end behavior might not be desirable.
2017-02-17 12:38:42 +00:00
|
|
|
assert!(resume_stmt_block.is_none());
|
2016-06-07 18:20:50 +00:00
|
|
|
resume_stmt_block = Some(bb);
|
2016-05-16 23:26:18 +00:00
|
|
|
} else {
|
2016-06-07 18:20:50 +00:00
|
|
|
resume_block = Some(bb);
|
2016-05-16 23:26:18 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
let resume_block = resume_block.unwrap_or_else(|| {
|
|
|
|
result.new_block(BasicBlockData {
|
|
|
|
statements: vec![],
|
|
|
|
terminator: Some(Terminator {
|
2020-05-06 00:30:11 +00:00
|
|
|
source_info: SourceInfo::outermost(body.span),
|
2016-05-16 23:26:18 +00:00
|
|
|
kind: TerminatorKind::Resume,
|
|
|
|
}),
|
|
|
|
is_cleanup: true,
|
|
|
|
})
|
|
|
|
});
|
|
|
|
result.resume_block = resume_block;
|
|
|
|
if let Some(resume_stmt_block) = resume_stmt_block {
|
|
|
|
result
|
|
|
|
.patch_terminator(resume_stmt_block, TerminatorKind::Goto { target: resume_block });
|
|
|
|
}
|
|
|
|
result
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn resume_block(&self) -> BasicBlock {
|
|
|
|
self.resume_block
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_patched(&self, bb: BasicBlock) -> bool {
|
2016-06-07 14:28:36 +00:00
|
|
|
self.patch_map[bb].is_some()
|
2016-05-16 23:26:18 +00:00
|
|
|
}
|
|
|
|
|
2019-06-03 22:26:48 +00:00
|
|
|
pub fn terminator_loc(&self, body: &Body<'tcx>, bb: BasicBlock) -> Location {
|
|
|
|
let offset = match bb.index().checked_sub(body.basic_blocks().len()) {
|
2016-05-16 23:26:18 +00:00
|
|
|
Some(index) => self.new_blocks[index].statements.len(),
|
2019-06-03 22:26:48 +00:00
|
|
|
None => body[bb].statements.len(),
|
2016-05-16 23:26:18 +00:00
|
|
|
};
|
2016-08-09 01:46:06 +00:00
|
|
|
Location { block: bb, statement_index: offset }
|
2016-05-16 23:26:18 +00:00
|
|
|
}
|
|
|
|
|
2022-05-01 12:38:22 +00:00
|
|
|
pub fn new_local_with_info(
|
|
|
|
&mut self,
|
|
|
|
ty: Ty<'tcx>,
|
|
|
|
span: Span,
|
|
|
|
local_info: Option<Box<LocalInfo<'tcx>>>,
|
|
|
|
) -> Local {
|
2022-04-27 22:03:07 +00:00
|
|
|
let index = self.next_local;
|
|
|
|
self.next_local += 1;
|
|
|
|
let mut new_decl = LocalDecl::new(ty, span);
|
2022-05-01 12:38:22 +00:00
|
|
|
new_decl.local_info = local_info;
|
2022-04-27 22:03:07 +00:00
|
|
|
self.new_locals.push(new_decl);
|
|
|
|
Local::new(index as usize)
|
|
|
|
}
|
|
|
|
|
2017-04-11 20:52:51 +00:00
|
|
|
pub fn new_temp(&mut self, ty: Ty<'tcx>, span: Span) -> Local {
|
2022-05-01 12:38:22 +00:00
|
|
|
self.new_local_with_info(ty, span, None)
|
2016-05-16 23:26:18 +00:00
|
|
|
}
|
|
|
|
|
2017-07-15 20:41:33 +00:00
|
|
|
pub fn new_internal(&mut self, ty: Ty<'tcx>, span: Span) -> Local {
|
|
|
|
let index = self.next_local;
|
|
|
|
self.next_local += 1;
|
2020-05-06 00:17:38 +00:00
|
|
|
self.new_locals.push(LocalDecl::new(ty, span).internal());
|
2017-07-15 20:41:33 +00:00
|
|
|
Local::new(index as usize)
|
|
|
|
}
|
|
|
|
|
2016-05-16 23:26:18 +00:00
|
|
|
pub fn new_block(&mut self, data: BasicBlockData<'tcx>) -> BasicBlock {
|
|
|
|
let block = BasicBlock::new(self.patch_map.len());
|
|
|
|
debug!("MirPatch: new_block: {:?}: {:?}", block, data);
|
|
|
|
self.new_blocks.push(data);
|
|
|
|
self.patch_map.push(None);
|
|
|
|
block
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn patch_terminator(&mut self, block: BasicBlock, new: TerminatorKind<'tcx>) {
|
2016-06-07 14:28:36 +00:00
|
|
|
assert!(self.patch_map[block].is_none());
|
2016-05-16 23:26:18 +00:00
|
|
|
debug!("MirPatch: patch_terminator({:?}, {:?})", block, new);
|
2016-06-07 14:28:36 +00:00
|
|
|
self.patch_map[block] = Some(new);
|
2016-05-16 23:26:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_statement(&mut self, loc: Location, stmt: StatementKind<'tcx>) {
|
|
|
|
debug!("MirPatch: add_statement({:?}, {:?})", loc, stmt);
|
|
|
|
self.new_statements.push((loc, stmt));
|
|
|
|
}
|
|
|
|
|
2017-12-01 12:39:51 +00:00
|
|
|
pub fn add_assign(&mut self, loc: Location, place: Place<'tcx>, rv: Rvalue<'tcx>) {
|
2021-08-05 03:36:38 +00:00
|
|
|
self.add_statement(loc, StatementKind::Assign(Box::new((place, rv))));
|
2016-05-16 23:26:18 +00:00
|
|
|
}
|
|
|
|
|
2020-04-12 17:31:00 +00:00
|
|
|
pub fn apply(self, body: &mut Body<'tcx>) {
|
2016-05-16 23:26:18 +00:00
|
|
|
debug!(
|
|
|
|
"MirPatch: {:?} new temps, starting from index {}: {:?}",
|
2019-11-06 17:00:46 +00:00
|
|
|
self.new_locals.len(),
|
|
|
|
body.local_decls.len(),
|
|
|
|
self.new_locals
|
|
|
|
);
|
2016-05-16 23:26:18 +00:00
|
|
|
debug!(
|
|
|
|
"MirPatch: {} new blocks, starting from index {}",
|
2019-11-06 17:00:46 +00:00
|
|
|
self.new_blocks.len(),
|
|
|
|
body.basic_blocks().len()
|
|
|
|
);
|
|
|
|
body.basic_blocks_mut().extend(self.new_blocks);
|
|
|
|
body.local_decls.extend(self.new_locals);
|
2016-06-07 14:28:36 +00:00
|
|
|
for (src, patch) in self.patch_map.into_iter_enumerated() {
|
2016-05-16 23:26:18 +00:00
|
|
|
if let Some(patch) = patch {
|
|
|
|
debug!("MirPatch: patching block {:?}", src);
|
2019-11-06 17:00:46 +00:00
|
|
|
body[src].terminator_mut().kind = patch;
|
2016-05-16 23:26:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut new_statements = self.new_statements;
|
2018-07-25 08:36:57 +00:00
|
|
|
new_statements.sort_by_key(|s| s.0);
|
2016-05-16 23:26:18 +00:00
|
|
|
|
|
|
|
let mut delta = 0;
|
|
|
|
let mut last_bb = START_BLOCK;
|
2022-04-17 19:16:12 +00:00
|
|
|
let mut stmts_and_targets: Vec<(Statement<'_>, BasicBlock)> = Vec::new();
|
2016-05-16 23:26:18 +00:00
|
|
|
for (mut loc, stmt) in new_statements {
|
|
|
|
if loc.block != last_bb {
|
|
|
|
delta = 0;
|
|
|
|
last_bb = loc.block;
|
|
|
|
}
|
|
|
|
debug!("MirPatch: adding statement {:?} at loc {:?}+{}", stmt, loc, delta);
|
2016-08-09 01:46:06 +00:00
|
|
|
loc.statement_index += delta;
|
2016-06-07 16:21:56 +00:00
|
|
|
let source_info = Self::source_info_for_index(&body[loc.block], loc);
|
2022-04-16 13:03:14 +00:00
|
|
|
|
|
|
|
// For mir-opt `Derefer` to work in all cases we need to
|
|
|
|
// get terminator's targets and apply the statement to all of them.
|
|
|
|
if loc.statement_index > body[loc.block].statements.len() {
|
|
|
|
let term = body[loc.block].terminator();
|
2022-05-17 00:41:01 +00:00
|
|
|
for i in term.successors() {
|
2022-04-17 19:16:12 +00:00
|
|
|
stmts_and_targets
|
|
|
|
.push((Statement { source_info, kind: stmt.clone() }, i.clone()));
|
2022-04-16 13:03:14 +00:00
|
|
|
}
|
|
|
|
delta += 1;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2019-11-06 17:00:46 +00:00
|
|
|
body[loc.block]
|
|
|
|
.statements
|
2016-08-09 01:46:06 +00:00
|
|
|
.insert(loc.statement_index, Statement { source_info, kind: stmt });
|
2016-05-16 23:26:18 +00:00
|
|
|
delta += 1;
|
|
|
|
}
|
2022-04-16 13:03:14 +00:00
|
|
|
|
2022-04-18 19:05:16 +00:00
|
|
|
for (stmt, target) in stmts_and_targets.into_iter().rev() {
|
|
|
|
body[target].statements.insert(0, stmt);
|
2022-04-16 13:03:14 +00:00
|
|
|
}
|
2016-05-16 23:26:18 +00:00
|
|
|
}
|
|
|
|
|
2019-02-07 21:28:15 +00:00
|
|
|
pub fn source_info_for_index(data: &BasicBlockData<'_>, loc: Location) -> SourceInfo {
|
2016-08-09 01:46:06 +00:00
|
|
|
match data.statements.get(loc.statement_index) {
|
2016-06-07 16:21:56 +00:00
|
|
|
Some(stmt) => stmt.source_info,
|
|
|
|
None => data.terminator().source_info,
|
2016-05-16 23:26:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-03 22:26:48 +00:00
|
|
|
pub fn source_info_for_location(&self, body: &Body<'_>, loc: Location) -> SourceInfo {
|
|
|
|
let data = match loc.block.index().checked_sub(body.basic_blocks().len()) {
|
2016-05-16 23:26:18 +00:00
|
|
|
Some(new) => &self.new_blocks[new],
|
2019-06-03 22:26:48 +00:00
|
|
|
None => &body[loc.block],
|
2016-05-16 23:26:18 +00:00
|
|
|
};
|
2016-06-07 16:21:56 +00:00
|
|
|
Self::source_info_for_index(data, loc)
|
2016-05-16 23:26:18 +00:00
|
|
|
}
|
|
|
|
}
|