Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Perform opportunistic simplifications during value numbering #111344

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 54 additions & 6 deletions compiler/rustc_mir_transform/src/gvn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,9 @@ fn propagate_ssa<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
state.next_opaque = None;

let reverse_postorder = body.basic_blocks.reverse_postorder().to_vec();
for dbg in body.var_debug_info.iter_mut() {
state.visit_var_debug_info(dbg);
}
for bb in reverse_postorder {
let data = &mut body.basic_blocks.as_mut_preserves_cfg()[bb];
state.visit_basic_block_data(bb, data);
Expand Down Expand Up @@ -893,6 +896,12 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
}
}

let fields: Option<Vec<_>> = fields
.iter_mut()
.map(|op| self.simplify_operand(op, location).or_else(|| self.new_opaque()))
.collect();
let fields = fields?;

let (ty, variant_index) = match *kind {
AggregateKind::Array(..) => {
assert!(!fields.is_empty());
Expand All @@ -912,12 +921,6 @@ impl<'body, 'tcx> VnState<'body, 'tcx> {
AggregateKind::Adt(_, _, _, _, Some(_)) => return None,
};

let fields: Option<Vec<_>> = fields
.iter_mut()
.map(|op| self.simplify_operand(op, location).or_else(|| self.new_opaque()))
.collect();
let fields = fields?;

if let AggregateTy::Array = ty
&& fields.len() > 4
{
Expand Down Expand Up @@ -1238,6 +1241,51 @@ impl<'tcx> MutVisitor<'tcx> for VnState<'_, 'tcx> {
self.tcx
}

fn visit_var_debug_info(&mut self, var_debug_info: &mut VarDebugInfo<'tcx>) {
let mut replace_dereffed = |place: &mut Place<'tcx>| -> Option<!> {
let last_deref = place.projection.iter().rposition(|e| e == PlaceElem::Deref)?;

// Another place that holds the same value.
let mut place_ref = place.as_ref();
let mut value = self.locals[place.local]?;

for (index, &proj) in place.projection[..last_deref].iter().enumerate() {
if let Some(candidates) = self.rev_locals.get(value)
&& let Some(&local) = candidates.first()
{
place_ref = PlaceRef { local, projection: &place.projection[index..] };
}

let place_upto =
PlaceRef { local: place.local, projection: &place.projection[..index] };
if let Some(projected) = self.project(place_upto, value, proj) {
value = projected;
} else {
if place_ref.projection.len() < place.projection.len() {
*place = place_ref.project_deeper(&[], self.tcx);
}
return None;
}
}

if let Some(candidates) = self.rev_locals.get(value)
&& let Some(&local) = candidates.first()
{
let place_ref = PlaceRef { local, projection: &place.projection[last_deref..] };
*place = place_ref.project_deeper(&[], self.tcx);
}

return None;
};

match &mut var_debug_info.value {
VarDebugInfoContents::Const(_) => {}
VarDebugInfoContents::Place(place) => {
replace_dereffed(place);
}
}
}

fn visit_place(&mut self, place: &mut Place<'tcx>, _: PlaceContext, location: Location) {
self.simplify_place_projection(place, location);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
StorageLive(_1);
StorageLive(_2);
- _2 = ();
- _1 = Union32 { value: move _2 };
+ _2 = const ();
_1 = Union32 { value: move _2 };
+ _1 = Union32 { value: const () };
StorageDead(_2);
_0 = move _1 as u32 (Transmute);
StorageDead(_1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@
StorageLive(_1);
StorageLive(_2);
- _2 = ();
- _1 = Union32 { value: move _2 };
+ _2 = const ();
_1 = Union32 { value: move _2 };
+ _1 = Union32 { value: const () };
StorageDead(_2);
_0 = move _1 as u32 (Transmute);
StorageDead(_1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,21 @@
let mut _0: ();
let _1: main::Un;
let mut _2: u32;
let mut _3: u32;
scope 1 {
debug un => _1;
scope 3 (inlined std::mem::drop::<u32>) {
debug _x => _3;
debug _x => _2;
}
}
scope 2 (inlined val) {
}

bb0: {
StorageLive(_1);
StorageLive(_2);
_2 = const 1_u32;
_1 = Un { us: const 1_u32 };
StorageLive(_2);
_2 = (_1.0: u32);
StorageDead(_2);
StorageLive(_3);
_3 = (_1.0: u32);
StorageDead(_3);
StorageDead(_1);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,21 @@
let mut _0: ();
let _1: main::Un;
let mut _2: u32;
let mut _3: u32;
scope 1 {
debug un => _1;
scope 3 (inlined std::mem::drop::<u32>) {
debug _x => _3;
debug _x => _2;
}
}
scope 2 (inlined val) {
}

bb0: {
StorageLive(_1);
StorageLive(_2);
_2 = const 1_u32;
_1 = Un { us: const 1_u32 };
StorageLive(_2);
_2 = (_1.0: u32);
StorageDead(_2);
StorageLive(_3);
_3 = (_1.0: u32);
StorageDead(_3);
StorageDead(_1);
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ fn foo(_1: T, _2: i32) -> (i32, T) {
debug x => _3;
scope 2 (inlined foo::<T>::{closure#0}) {
debug _q => _9;
debug q => (*((*_6).0: &i32));
debug t => (*((*_6).1: &T));
debug q => (*_10);
debug t => (*_12);
let mut _10: &i32;
let mut _11: i32;
let mut _12: &T;
Expand Down