2016-05-07 19:14:28 +03:00
|
|
|
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
//! A pass that promotes borrows of constant rvalues.
|
|
|
|
//!
|
|
|
|
//! The rvalues considered constant are trees of temps,
|
|
|
|
//! each with exactly one initialization, and holding
|
|
|
|
//! a constant value with no interior mutability.
|
|
|
|
//! They are placed into a new MIR constant body in
|
|
|
|
//! `promoted` and the borrow rvalue is replaced with
|
|
|
|
//! a `Literal::Promoted` using the index into `promoted`
|
|
|
|
//! of that constant MIR.
|
|
|
|
//!
|
|
|
|
//! This pass assumes that every use is dominated by an
|
|
|
|
//! initialization and can otherwise silence errors, if
|
|
|
|
//! move analysis runs after promotion on broken MIR.
|
|
|
|
|
2016-09-19 23:50:00 +03:00
|
|
|
use rustc::mir::*;
|
2017-12-01 14:31:47 +02:00
|
|
|
use rustc::mir::visit::{PlaceContext, MutVisitor, Visitor};
|
2016-06-05 19:38:22 +03:00
|
|
|
use rustc::mir::traversal::ReversePostorder;
|
2016-07-31 22:33:41 +08:00
|
|
|
use rustc::ty::TyCtxt;
|
2016-06-21 18:08:13 -04:00
|
|
|
use syntax_pos::Span;
|
2016-05-07 19:14:28 +03:00
|
|
|
|
2016-06-07 17:28:36 +03:00
|
|
|
use rustc_data_structures::indexed_vec::{IndexVec, Idx};
|
|
|
|
|
2016-09-25 01:38:27 +02:00
|
|
|
use std::iter;
|
2016-05-07 19:14:28 +03:00
|
|
|
use std::mem;
|
2016-08-08 18:46:06 -07:00
|
|
|
use std::usize;
|
2016-05-07 19:14:28 +03:00
|
|
|
|
|
|
|
/// State of a temporary during collection and promotion.
|
|
|
|
#[derive(Copy, Clone, PartialEq, Eq, Debug)]
|
|
|
|
pub enum TempState {
|
|
|
|
/// No references to this temp.
|
|
|
|
Undefined,
|
|
|
|
/// One direct assignment and any number of direct uses.
|
|
|
|
/// A borrow of this temp is promotable if the assigned
|
|
|
|
/// value is qualified as constant.
|
|
|
|
Defined {
|
|
|
|
location: Location,
|
|
|
|
uses: usize
|
|
|
|
},
|
|
|
|
/// Any other combination of assignments/uses.
|
|
|
|
Unpromotable,
|
|
|
|
/// This temp was part of an rvalue which got extracted
|
|
|
|
/// during promotion and needs cleanup.
|
|
|
|
PromotedOut
|
|
|
|
}
|
|
|
|
|
|
|
|
impl TempState {
|
|
|
|
pub fn is_promotable(&self) -> bool {
|
|
|
|
if let TempState::Defined { uses, .. } = *self {
|
|
|
|
uses > 0
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A "root candidate" for promotion, which will become the
|
|
|
|
/// returned value in a promoted MIR, unless it's a subset
|
|
|
|
/// of a larger candidate.
|
2017-01-05 01:19:54 +02:00
|
|
|
#[derive(Debug)]
|
2016-05-07 19:14:28 +03:00
|
|
|
pub enum Candidate {
|
|
|
|
/// Borrow of a constant temporary.
|
|
|
|
Ref(Location),
|
|
|
|
|
2018-02-05 10:48:22 -08:00
|
|
|
/// Currently applied to function calls where the callee has the unstable
|
|
|
|
/// `#[rustc_args_required_const]` attribute as well as the SIMD shuffle
|
|
|
|
/// intrinsic. The intrinsic requires the arguments are indeed constant and
|
|
|
|
/// the attribute currently provides the semantic requirement that arguments
|
|
|
|
/// must be constant.
|
|
|
|
Argument { bb: BasicBlock, index: usize },
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
|
|
|
|
2016-09-25 01:38:27 +02:00
|
|
|
struct TempCollector<'tcx> {
|
|
|
|
temps: IndexVec<Local, TempState>,
|
|
|
|
span: Span,
|
|
|
|
mir: &'tcx Mir<'tcx>,
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
|
|
|
|
2016-09-25 01:38:27 +02:00
|
|
|
impl<'tcx> Visitor<'tcx> for TempCollector<'tcx> {
|
2017-09-03 19:14:31 +03:00
|
|
|
fn visit_local(&mut self,
|
|
|
|
&index: &Local,
|
2017-12-01 14:31:47 +02:00
|
|
|
context: PlaceContext<'tcx>,
|
2017-09-03 19:14:31 +03:00
|
|
|
location: Location) {
|
|
|
|
// We're only interested in temporaries
|
|
|
|
if self.mir.local_kind(index) != LocalKind::Temp {
|
|
|
|
return;
|
|
|
|
}
|
2016-09-25 01:38:27 +02:00
|
|
|
|
2017-09-03 19:14:31 +03:00
|
|
|
// Ignore drops, if the temp gets promoted,
|
|
|
|
// then it's constant and thus drop is noop.
|
|
|
|
// Storage live ranges are also irrelevant.
|
|
|
|
if context.is_drop() || context.is_storage_marker() {
|
|
|
|
return;
|
|
|
|
}
|
2016-05-07 19:14:28 +03:00
|
|
|
|
2017-09-03 19:14:31 +03:00
|
|
|
let temp = &mut self.temps[index];
|
|
|
|
if *temp == TempState::Undefined {
|
|
|
|
match context {
|
2017-12-01 14:31:47 +02:00
|
|
|
PlaceContext::Store |
|
2017-12-19 17:05:14 -05:00
|
|
|
PlaceContext::AsmOutput |
|
2017-12-01 14:31:47 +02:00
|
|
|
PlaceContext::Call => {
|
2017-09-03 19:14:31 +03:00
|
|
|
*temp = TempState::Defined {
|
|
|
|
location,
|
|
|
|
uses: 0
|
|
|
|
};
|
2017-01-05 02:33:09 +02:00
|
|
|
return;
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
2017-09-03 19:14:31 +03:00
|
|
|
_ => { /* mark as unpromotable below */ }
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
2017-09-03 19:14:31 +03:00
|
|
|
} else if let TempState::Defined { ref mut uses, .. } = *temp {
|
|
|
|
// We always allow borrows, even mutable ones, as we need
|
|
|
|
// to promote mutable borrows of some ZSTs e.g. `&mut []`.
|
|
|
|
let allowed_use = match context {
|
2017-12-01 14:31:47 +02:00
|
|
|
PlaceContext::Borrow {..} => true,
|
2017-09-03 19:14:31 +03:00
|
|
|
_ => context.is_nonmutating_use()
|
|
|
|
};
|
|
|
|
if allowed_use {
|
|
|
|
*uses += 1;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* mark as unpromotable below */
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
2017-09-03 19:14:31 +03:00
|
|
|
*temp = TempState::Unpromotable;
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
|
|
|
|
2016-06-07 19:21:56 +03:00
|
|
|
fn visit_source_info(&mut self, source_info: &SourceInfo) {
|
|
|
|
self.span = source_info.span;
|
|
|
|
}
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
|
|
|
|
2016-09-25 01:38:27 +02:00
|
|
|
pub fn collect_temps(mir: &Mir, rpo: &mut ReversePostorder) -> IndexVec<Local, TempState> {
|
2016-05-07 19:14:28 +03:00
|
|
|
let mut collector = TempCollector {
|
2016-09-25 01:38:27 +02:00
|
|
|
temps: IndexVec::from_elem(TempState::Undefined, &mir.local_decls),
|
|
|
|
span: mir.span,
|
2017-08-06 22:54:09 -07:00
|
|
|
mir,
|
2016-05-07 19:14:28 +03:00
|
|
|
};
|
|
|
|
for (bb, data) in rpo {
|
|
|
|
collector.visit_basic_block_data(bb, data);
|
|
|
|
}
|
|
|
|
collector.temps
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Promoter<'a, 'tcx: 'a> {
|
|
|
|
source: &'a mut Mir<'tcx>,
|
|
|
|
promoted: Mir<'tcx>,
|
2016-09-25 01:38:27 +02:00
|
|
|
temps: &'a mut IndexVec<Local, TempState>,
|
2016-05-07 19:14:28 +03:00
|
|
|
|
|
|
|
/// If true, all nested temps are also kept in the
|
|
|
|
/// source MIR, not moved to the promoted MIR.
|
|
|
|
keep_original: bool
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, 'tcx> Promoter<'a, 'tcx> {
|
|
|
|
fn new_block(&mut self) -> BasicBlock {
|
2016-06-07 21:20:50 +03:00
|
|
|
let span = self.promoted.span;
|
|
|
|
self.promoted.basic_blocks_mut().push(BasicBlockData {
|
2016-05-07 19:14:28 +03:00
|
|
|
statements: vec![],
|
|
|
|
terminator: Some(Terminator {
|
2016-06-07 19:21:56 +03:00
|
|
|
source_info: SourceInfo {
|
2017-08-06 22:54:09 -07:00
|
|
|
span,
|
2016-06-07 19:21:56 +03:00
|
|
|
scope: ARGUMENT_VISIBILITY_SCOPE
|
|
|
|
},
|
2016-05-07 19:14:28 +03:00
|
|
|
kind: TerminatorKind::Return
|
|
|
|
}),
|
|
|
|
is_cleanup: false
|
2016-06-07 17:28:36 +03:00
|
|
|
})
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
|
|
|
|
2016-09-26 22:53:22 +02:00
|
|
|
fn assign(&mut self, dest: Local, rvalue: Rvalue<'tcx>, span: Span) {
|
2016-06-07 21:20:50 +03:00
|
|
|
let last = self.promoted.basic_blocks().last().unwrap();
|
|
|
|
let data = &mut self.promoted[last];
|
2016-05-07 19:14:28 +03:00
|
|
|
data.statements.push(Statement {
|
2016-06-07 19:21:56 +03:00
|
|
|
source_info: SourceInfo {
|
2017-08-06 22:54:09 -07:00
|
|
|
span,
|
2016-06-07 19:21:56 +03:00
|
|
|
scope: ARGUMENT_VISIBILITY_SCOPE
|
|
|
|
},
|
2017-12-01 14:31:47 +02:00
|
|
|
kind: StatementKind::Assign(Place::Local(dest), rvalue)
|
2016-05-07 19:14:28 +03:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Copy the initialization of this temp to the
|
|
|
|
/// promoted MIR, recursing through temps.
|
2016-09-25 01:38:27 +02:00
|
|
|
fn promote_temp(&mut self, temp: Local) -> Local {
|
2016-05-07 19:14:28 +03:00
|
|
|
let old_keep_original = self.keep_original;
|
2017-01-05 01:19:54 +02:00
|
|
|
let loc = match self.temps[temp] {
|
|
|
|
TempState::Defined { location, uses } if uses > 0 => {
|
2016-05-07 19:14:28 +03:00
|
|
|
if uses > 1 {
|
|
|
|
self.keep_original = true;
|
|
|
|
}
|
2017-01-05 01:19:54 +02:00
|
|
|
location
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
2016-06-07 17:28:36 +03:00
|
|
|
state => {
|
|
|
|
span_bug!(self.promoted.span, "{:?} not promotable: {:?}",
|
|
|
|
temp, state);
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
if !self.keep_original {
|
2016-06-07 17:28:36 +03:00
|
|
|
self.temps[temp] = TempState::PromotedOut;
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
|
|
|
|
2017-01-05 01:19:54 +02:00
|
|
|
let no_stmts = self.source[loc.block].statements.len();
|
|
|
|
let new_temp = self.promoted.local_decls.push(
|
2017-04-11 23:52:51 +03:00
|
|
|
LocalDecl::new_temp(self.source.local_decls[temp].ty,
|
|
|
|
self.source.local_decls[temp].source_info.span));
|
2017-01-05 01:19:54 +02:00
|
|
|
|
|
|
|
debug!("promote({:?} @ {:?}/{:?}, {:?})",
|
|
|
|
temp, loc, no_stmts, self.keep_original);
|
2016-05-07 19:14:28 +03:00
|
|
|
|
|
|
|
// First, take the Rvalue or Call out of the source MIR,
|
|
|
|
// or duplicate it, depending on keep_original.
|
2017-01-05 01:19:54 +02:00
|
|
|
if loc.statement_index < no_stmts {
|
|
|
|
let (mut rvalue, source_info) = {
|
|
|
|
let statement = &mut self.source[loc.block].statements[loc.statement_index];
|
|
|
|
let rhs = match statement.kind {
|
|
|
|
StatementKind::Assign(_, ref mut rhs) => rhs,
|
|
|
|
_ => {
|
|
|
|
span_bug!(statement.source_info.span, "{:?} is not an assignment",
|
|
|
|
statement);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
(if self.keep_original {
|
|
|
|
rhs.clone()
|
|
|
|
} else {
|
2017-05-12 01:38:26 +03:00
|
|
|
let unit = Rvalue::Aggregate(box AggregateKind::Tuple, vec![]);
|
2017-01-05 01:19:54 +02:00
|
|
|
mem::replace(rhs, unit)
|
|
|
|
}, statement.source_info)
|
2016-08-04 16:14:33 -07:00
|
|
|
};
|
2017-01-05 01:19:54 +02:00
|
|
|
|
|
|
|
self.visit_rvalue(&mut rvalue, loc);
|
|
|
|
self.assign(new_temp, rvalue, source_info.span);
|
2016-05-07 19:14:28 +03:00
|
|
|
} else {
|
2017-01-11 09:50:24 +02:00
|
|
|
let terminator = if self.keep_original {
|
2017-01-05 01:19:54 +02:00
|
|
|
self.source[loc.block].terminator().clone()
|
|
|
|
} else {
|
|
|
|
let terminator = self.source[loc.block].terminator_mut();
|
|
|
|
let target = match terminator.kind {
|
|
|
|
TerminatorKind::Call { destination: Some((_, target)), .. } => target,
|
|
|
|
ref kind => {
|
|
|
|
span_bug!(terminator.source_info.span, "{:?} not promotable", kind);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Terminator {
|
|
|
|
source_info: terminator.source_info,
|
|
|
|
kind: mem::replace(&mut terminator.kind, TerminatorKind::Goto {
|
2017-08-06 22:54:09 -07:00
|
|
|
target,
|
2017-01-05 01:19:54 +02:00
|
|
|
})
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-01-11 09:50:24 +02:00
|
|
|
match terminator.kind {
|
2017-01-05 01:19:54 +02:00
|
|
|
TerminatorKind::Call { mut func, mut args, .. } => {
|
|
|
|
self.visit_operand(&mut func, loc);
|
|
|
|
for arg in &mut args {
|
|
|
|
self.visit_operand(arg, loc);
|
|
|
|
}
|
2017-01-11 09:50:24 +02:00
|
|
|
|
|
|
|
let last = self.promoted.basic_blocks().last().unwrap();
|
|
|
|
let new_target = self.new_block();
|
|
|
|
|
|
|
|
*self.promoted[last].terminator_mut() = Terminator {
|
|
|
|
kind: TerminatorKind::Call {
|
2017-08-06 22:54:09 -07:00
|
|
|
func,
|
|
|
|
args,
|
2017-01-11 09:50:24 +02:00
|
|
|
cleanup: None,
|
2017-12-01 14:31:47 +02:00
|
|
|
destination: Some((Place::Local(new_temp), new_target))
|
2017-01-11 09:50:24 +02:00
|
|
|
},
|
|
|
|
..terminator
|
|
|
|
};
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
2017-01-05 01:19:54 +02:00
|
|
|
ref kind => {
|
|
|
|
span_bug!(terminator.source_info.span, "{:?} not promotable", kind);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|
2016-05-07 19:14:28 +03:00
|
|
|
|
2017-01-05 01:19:54 +02:00
|
|
|
self.keep_original = old_keep_original;
|
2016-06-07 17:28:36 +03:00
|
|
|
new_temp
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn promote_candidate(mut self, candidate: Candidate) {
|
|
|
|
let span = self.promoted.span;
|
2017-05-12 01:38:26 +03:00
|
|
|
let new_operand = Operand::Constant(box Constant {
|
2017-08-06 22:54:09 -07:00
|
|
|
span,
|
2017-11-18 10:27:24 +03:00
|
|
|
ty: self.promoted.return_ty(),
|
2016-05-07 19:14:28 +03:00
|
|
|
literal: Literal::Promoted {
|
2016-06-07 17:28:36 +03:00
|
|
|
index: Promoted::new(self.source.promoted.len())
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
let mut rvalue = match candidate {
|
|
|
|
Candidate::Ref(Location { block: bb, statement_index: stmt_idx }) => {
|
2016-08-04 16:14:33 -07:00
|
|
|
let ref mut statement = self.source[bb].statements[stmt_idx];
|
|
|
|
match statement.kind {
|
2016-05-07 19:14:28 +03:00
|
|
|
StatementKind::Assign(_, ref mut rvalue) => {
|
|
|
|
mem::replace(rvalue, Rvalue::Use(new_operand))
|
|
|
|
}
|
2016-08-14 06:34:14 +03:00
|
|
|
_ => bug!()
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
|
|
|
}
|
2018-02-05 10:48:22 -08:00
|
|
|
Candidate::Argument { bb, index } => {
|
2016-05-07 19:14:28 +03:00
|
|
|
match self.source[bb].terminator_mut().kind {
|
|
|
|
TerminatorKind::Call { ref mut args, .. } => {
|
2018-02-05 10:48:22 -08:00
|
|
|
Rvalue::Use(mem::replace(&mut args[index], new_operand))
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
|
|
|
_ => bug!()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2016-09-25 01:38:27 +02:00
|
|
|
self.visit_rvalue(&mut rvalue, Location {
|
2016-08-08 18:46:06 -07:00
|
|
|
block: BasicBlock::new(0),
|
|
|
|
statement_index: usize::MAX
|
|
|
|
});
|
2016-09-25 01:38:27 +02:00
|
|
|
|
2017-12-01 14:39:51 +02:00
|
|
|
self.assign(RETURN_PLACE, rvalue, span);
|
2016-05-07 19:14:28 +03:00
|
|
|
self.source.promoted.push(self.promoted);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Replaces all temporaries with their promoted counterparts.
|
|
|
|
impl<'a, 'tcx> MutVisitor<'tcx> for Promoter<'a, 'tcx> {
|
2017-09-03 19:14:31 +03:00
|
|
|
fn visit_local(&mut self,
|
|
|
|
local: &mut Local,
|
2017-12-01 14:31:47 +02:00
|
|
|
_: PlaceContext<'tcx>,
|
2017-09-03 19:14:31 +03:00
|
|
|
_: Location) {
|
|
|
|
if self.source.local_kind(*local) == LocalKind::Temp {
|
|
|
|
*local = self.promote_temp(*local);
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-03 04:56:42 +03:00
|
|
|
pub fn promote_candidates<'a, 'tcx>(mir: &mut Mir<'tcx>,
|
2016-05-03 05:23:22 +03:00
|
|
|
tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2016-09-25 01:38:27 +02:00
|
|
|
mut temps: IndexVec<Local, TempState>,
|
2016-05-03 04:56:42 +03:00
|
|
|
candidates: Vec<Candidate>) {
|
2016-05-07 19:14:28 +03:00
|
|
|
// Visit candidates in reverse, in case they're nested.
|
2017-01-05 01:19:54 +02:00
|
|
|
debug!("promote_candidates({:?})", candidates);
|
2016-05-07 19:14:28 +03:00
|
|
|
for candidate in candidates.into_iter().rev() {
|
|
|
|
let (span, ty) = match candidate {
|
|
|
|
Candidate::Ref(Location { block: bb, statement_index: stmt_idx }) => {
|
|
|
|
let statement = &mir[bb].statements[stmt_idx];
|
2016-08-04 16:14:33 -07:00
|
|
|
let dest = match statement.kind {
|
|
|
|
StatementKind::Assign(ref dest, _) => dest,
|
2016-08-14 06:34:14 +03:00
|
|
|
_ => {
|
|
|
|
span_bug!(statement.source_info.span,
|
|
|
|
"expected assignment to promote");
|
|
|
|
}
|
2016-08-04 16:14:33 -07:00
|
|
|
};
|
2017-12-01 14:31:47 +02:00
|
|
|
if let Place::Local(index) = *dest {
|
2016-06-07 17:28:36 +03:00
|
|
|
if temps[index] == TempState::PromotedOut {
|
2016-05-07 19:14:28 +03:00
|
|
|
// Already promoted.
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
2016-08-05 15:59:51 -07:00
|
|
|
(statement.source_info.span, dest.ty(mir, tcx).to_ty(tcx))
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
2018-02-05 10:48:22 -08:00
|
|
|
Candidate::Argument { bb, index } => {
|
2016-05-07 19:14:28 +03:00
|
|
|
let terminator = mir[bb].terminator();
|
|
|
|
let ty = match terminator.kind {
|
|
|
|
TerminatorKind::Call { ref args, .. } => {
|
2018-02-05 10:48:22 -08:00
|
|
|
args[index].ty(mir, tcx)
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
|
|
|
_ => {
|
2016-06-07 19:21:56 +03:00
|
|
|
span_bug!(terminator.source_info.span,
|
2018-02-05 10:48:22 -08:00
|
|
|
"expected call argument to promote");
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
|
|
|
};
|
2016-06-07 19:21:56 +03:00
|
|
|
(terminator.source_info.span, ty)
|
2016-05-07 19:14:28 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-12-01 14:39:51 +02:00
|
|
|
// Declare return place local
|
|
|
|
let initial_locals = iter::once(LocalDecl::new_return_place(ty, span))
|
2017-04-11 23:52:51 +03:00
|
|
|
.collect();
|
2016-09-25 01:38:27 +02:00
|
|
|
|
2016-05-07 19:14:28 +03:00
|
|
|
let mut promoter = Promoter {
|
2016-06-07 21:20:50 +03:00
|
|
|
promoted: Mir::new(
|
|
|
|
IndexVec::new(),
|
2017-09-13 22:33:07 +03:00
|
|
|
// FIXME: maybe try to filter this to avoid blowing up
|
|
|
|
// memory usage?
|
|
|
|
mir.visibility_scopes.clone(),
|
|
|
|
mir.visibility_scope_info.clone(),
|
2016-06-07 21:20:50 +03:00
|
|
|
IndexVec::new(),
|
2016-12-26 14:34:03 +01:00
|
|
|
None,
|
2016-09-25 01:38:27 +02:00
|
|
|
initial_locals,
|
|
|
|
0,
|
2016-06-07 21:20:50 +03:00
|
|
|
vec![],
|
|
|
|
span
|
|
|
|
),
|
2016-09-25 01:38:27 +02:00
|
|
|
source: mir,
|
2016-05-07 19:14:28 +03:00
|
|
|
temps: &mut temps,
|
|
|
|
keep_original: false
|
|
|
|
};
|
|
|
|
assert_eq!(promoter.new_block(), START_BLOCK);
|
|
|
|
promoter.promote_candidate(candidate);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Eliminate assignments to, and drops of promoted temps.
|
2016-09-25 01:38:27 +02:00
|
|
|
let promoted = |index: Local| temps[index] == TempState::PromotedOut;
|
2016-06-07 21:20:50 +03:00
|
|
|
for block in mir.basic_blocks_mut() {
|
2016-05-07 19:14:28 +03:00
|
|
|
block.statements.retain(|statement| {
|
|
|
|
match statement.kind {
|
2017-12-01 14:31:47 +02:00
|
|
|
StatementKind::Assign(Place::Local(index), _) |
|
2017-09-04 08:01:46 +03:00
|
|
|
StatementKind::StorageLive(index) |
|
|
|
|
StatementKind::StorageDead(index) => {
|
2016-05-07 19:14:28 +03:00
|
|
|
!promoted(index)
|
|
|
|
}
|
|
|
|
_ => true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
let terminator = block.terminator_mut();
|
|
|
|
match terminator.kind {
|
2017-12-01 14:31:47 +02:00
|
|
|
TerminatorKind::Drop { location: Place::Local(index), target, .. } => {
|
2016-05-07 19:14:28 +03:00
|
|
|
if promoted(index) {
|
|
|
|
terminator.kind = TerminatorKind::Goto {
|
2017-08-06 22:54:09 -07:00
|
|
|
target,
|
2016-05-07 19:14:28 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|