Skip to content

Commit fa95297

Browse files
Format
1 parent 5e6c097 commit fa95297

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed

src/librustc_mir/transform/dag_nrvo.rs

+35-17
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,14 @@
112112
use crate::transform::{MirPass, MirSource};
113113
use rustc_index::{bit_set::BitSet, vec::IndexVec};
114114
use rustc_middle::mir::tcx::PlaceTy;
115-
use rustc_middle::mir::visit::{MutVisitor, PlaceContext, Visitor, MutatingUseContext};
115+
use rustc_middle::mir::visit::{MutVisitor, MutatingUseContext, PlaceContext, Visitor};
116116
use rustc_middle::mir::{
117-
read_only, Body, BodyAndCache, Local, LocalKind, Location, Operand, Place, PlaceElem,
118-
ReadOnlyBodyAndCache, Rvalue, Statement, StatementKind, Terminator, TerminatorKind,
119-
BasicBlock,
117+
read_only, BasicBlock, Body, BodyAndCache, Local, LocalKind, Location, Operand, Place,
118+
PlaceElem, ReadOnlyBodyAndCache, Rvalue, Statement, StatementKind, Terminator, TerminatorKind,
120119
};
121120
use rustc_middle::ty::{self, Ty, TyCtxt};
122-
use std::collections::VecDeque;
123121
use rustc_span::def_id::LOCAL_CRATE;
122+
use std::collections::VecDeque;
124123

125124
pub struct Nrvo;
126125

@@ -166,11 +165,27 @@ impl<'tcx> MirPass<'tcx> for Nrvo {
166165
debug!("{:?} = {:?} at {:?}", dest, src, loc);
167166
debug!("usage_map[src] = {:?}", usage_map[src]);
168167
debug!("usage_map[dest.local] = {:?}", usage_map[dest.local]);
169-
if expect_uses_relative_to(src, loc, Direction::Backward, &usage_map[src], &read_only!(body)).is_err() {
168+
if expect_uses_relative_to(
169+
src,
170+
loc,
171+
Direction::Backward,
172+
&usage_map[src],
173+
&read_only!(body),
174+
)
175+
.is_err()
176+
{
170177
debug!("(ineligible, src used after assignment)");
171178
return false;
172179
}
173-
if expect_uses_relative_to(dest.local, loc, Direction::Forward, &usage_map[dest.local], &read_only!(body)).is_err() {
180+
if expect_uses_relative_to(
181+
dest.local,
182+
loc,
183+
Direction::Forward,
184+
&usage_map[dest.local],
185+
&read_only!(body),
186+
)
187+
.is_err()
188+
{
174189
debug!("(ineligible, dest used before assignment)");
175190
return false;
176191
}
@@ -382,16 +397,18 @@ fn expect_uses_relative_to(
382397
// We're interested in uses of `local` basically before or after the `=` sign of the assignment.
383398
// That mean we have to visit one half of the assign statement here.
384399
match &statements[loc.statement_index].kind {
385-
StatementKind::Assign(box (place, rvalue)) => {
386-
match dir {
387-
Direction::Backward => {
388-
collector.visit_rvalue(rvalue, loc);
389-
}
390-
Direction::Forward => {
391-
collector.visit_place(place, PlaceContext::MutatingUse(MutatingUseContext::Store), loc);
392-
}
400+
StatementKind::Assign(box (place, rvalue)) => match dir {
401+
Direction::Backward => {
402+
collector.visit_rvalue(rvalue, loc);
393403
}
394-
}
404+
Direction::Forward => {
405+
collector.visit_place(
406+
place,
407+
PlaceContext::MutatingUse(MutatingUseContext::Store),
408+
loc,
409+
);
410+
}
411+
},
395412
_ => bug!("{:?} should be an assignment", loc),
396413
}
397414

@@ -463,7 +480,8 @@ type UsageMap = IndexVec<Local, BitSet<BasicBlock>>;
463480

464481
/// Builds a usage map, mapping `Local`s to the `BasicBlock`s using them.
465482
fn usage_map(body: &Body<'_>) -> UsageMap {
466-
let mut map = IndexVec::from_elem_n(BitSet::new_empty(body.basic_blocks().len()), body.local_decls.len());
483+
let mut map =
484+
IndexVec::from_elem_n(BitSet::new_empty(body.basic_blocks().len()), body.local_decls.len());
467485
let mut collector = UseCollector {
468486
callback: |local, location: Location| {
469487
map[local].insert(location.block);

0 commit comments

Comments
 (0)