Skip to content

Commit 47b0479

Browse files
committed
Replace SingleUseConsts by GVN.
1 parent fccc37f commit 47b0479

File tree

123 files changed

+558
-599
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

123 files changed

+558
-599
lines changed

compiler/rustc_mir_transform/src/gvn.rs

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,10 @@ impl<'tcx> crate::MirPass<'tcx> for GVN {
145145
state.visit_basic_block_data(bb, data);
146146
}
147147

148+
for var_debug_info in body.var_debug_info.iter_mut() {
149+
state.visit_var_debug_info(var_debug_info);
150+
}
151+
148152
// For each local that is reused (`y` above), we remove its storage statements do avoid any
149153
// difficulty. Those locals are SSA, so should be easy to optimize by LLVM without storage
150154
// statements.
@@ -866,10 +870,11 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
866870

867871
/// Simplify the projection chain if we know better.
868872
#[instrument(level = "trace", skip(self))]
869-
fn simplify_place_projection(&mut self, place: &mut Place<'tcx>, location: Location) {
873+
fn simplify_place_projection(&mut self, place: &mut Place<'tcx>, location: Option<Location>) {
870874
// If the projection is indirect, we treat the local as a value, so can replace it with
871875
// another local.
872-
if place.is_indirect_first_projection()
876+
if let Some(location) = location
877+
&& place.is_indirect_first_projection()
873878
&& let Some(base) = self.locals[place.local]
874879
&& let Some(new_local) = self.try_as_local(base, location)
875880
&& place.local != new_local
@@ -891,7 +896,8 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
891896
{
892897
projection.to_mut()[i] =
893898
ProjectionElem::ConstantIndex { offset, min_length, from_end: false };
894-
} else if let Some(new_idx_local) = self.try_as_local(idx, location)
899+
} else if let Some(location) = location
900+
&& let Some(new_idx_local) = self.try_as_local(idx, location)
895901
&& idx_local != new_idx_local
896902
{
897903
projection.to_mut()[i] = ProjectionElem::Index(new_idx_local);
@@ -913,7 +919,7 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
913919
fn compute_place_value(
914920
&mut self,
915921
place: Place<'tcx>,
916-
location: Location,
922+
location: Option<Location>,
917923
) -> Result<VnIndex, PlaceRef<'tcx>> {
918924
// Invariant: `place` and `place_ref` point to the same value, even if they point to
919925
// different memory locations.
@@ -924,7 +930,9 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
924930
// Invariant: `value` has type `place_ty`, with optional downcast variant if needed.
925931
let mut place_ty = PlaceTy::from_ty(self.local_decls[place.local].ty);
926932
for (index, proj) in place.projection.iter().enumerate() {
927-
if let Some(local) = self.try_as_local(value, location) {
933+
if let Some(location) = location
934+
&& let Some(local) = self.try_as_local(value, location)
935+
{
928936
// Both `local` and `Place { local: place.local, projection: projection[..index] }`
929937
// hold the same value. Therefore, following place holds the value in the original
930938
// `place`.
@@ -949,13 +957,14 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
949957
fn simplify_place_value(
950958
&mut self,
951959
place: &mut Place<'tcx>,
952-
location: Location,
960+
location: Option<Location>,
953961
) -> Option<VnIndex> {
954962
self.simplify_place_projection(place, location);
955963

956964
match self.compute_place_value(*place, location) {
957965
Ok(value) => {
958-
if let Some(new_place) = self.try_as_place(value, location, true)
966+
if let Some(location) = location
967+
&& let Some(new_place) = self.try_as_place(value, location, true)
959968
&& (new_place.local != place.local
960969
|| new_place.projection.len() < place.projection.len())
961970
{
@@ -986,7 +995,7 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
986995
let value = match *operand {
987996
Operand::Constant(ref constant) => self.insert_constant(constant.const_),
988997
Operand::Copy(ref mut place) | Operand::Move(ref mut place) => {
989-
self.simplify_place_value(place, location)?
998+
self.simplify_place_value(place, Some(location))?
990999
}
9911000
};
9921001
if let Some(const_) = self.try_as_constant(value) {
@@ -1020,11 +1029,11 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
10201029
Rvalue::NullaryOp(op, ty) => Value::NullaryOp(op, ty),
10211030
Rvalue::Aggregate(..) => return self.simplify_aggregate(lhs, rvalue, location),
10221031
Rvalue::Ref(_, borrow_kind, ref mut place) => {
1023-
self.simplify_place_projection(place, location);
1032+
self.simplify_place_projection(place, Some(location));
10241033
return self.new_pointer(*place, AddressKind::Ref(borrow_kind));
10251034
}
10261035
Rvalue::RawPtr(mutbl, ref mut place) => {
1027-
self.simplify_place_projection(place, location);
1036+
self.simplify_place_projection(place, Some(location));
10281037
return self.new_pointer(*place, AddressKind::Address(mutbl));
10291038
}
10301039
Rvalue::WrapUnsafeBinder(ref mut op, _) => {
@@ -1043,7 +1052,7 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
10431052
return self.simplify_unary(op, arg_op, location);
10441053
}
10451054
Rvalue::Discriminant(ref mut place) => {
1046-
let place = self.simplify_place_value(place, location)?;
1055+
let place = self.simplify_place_value(place, Some(location))?;
10471056
if let Some(discr) = self.simplify_discriminant(place) {
10481057
return Some(discr);
10491058
}
@@ -1853,8 +1862,21 @@ impl<'tcx> MutVisitor<'tcx> for VnState<'_, '_, 'tcx> {
18531862
self.tcx
18541863
}
18551864

1865+
fn visit_var_debug_info(&mut self, var_debug_info: &mut VarDebugInfo<'tcx>) {
1866+
match &mut var_debug_info.value {
1867+
VarDebugInfoContents::Const(_) => {}
1868+
VarDebugInfoContents::Place(place) => {
1869+
if let Some(value) = self.simplify_place_value(place, None)
1870+
&& let Some(constant) = self.try_as_constant(value)
1871+
{
1872+
var_debug_info.value = VarDebugInfoContents::Const(constant);
1873+
}
1874+
}
1875+
}
1876+
}
1877+
18561878
fn visit_place(&mut self, place: &mut Place<'tcx>, context: PlaceContext, location: Location) {
1857-
self.simplify_place_projection(place, location);
1879+
self.simplify_place_projection(place, Some(location));
18581880
if context.is_mutating_use() && place.is_indirect() {
18591881
// Non-local mutation maybe invalidate deref.
18601882
self.invalidate_derefs();
@@ -1873,7 +1895,7 @@ impl<'tcx> MutVisitor<'tcx> for VnState<'_, '_, 'tcx> {
18731895
rvalue: &mut Rvalue<'tcx>,
18741896
location: Location,
18751897
) {
1876-
self.simplify_place_projection(lhs, location);
1898+
self.simplify_place_projection(lhs, Some(location));
18771899

18781900
let value = self.simplify_rvalue(lhs, rvalue, location);
18791901
if let Some(value) = value {

compiler/rustc_mir_transform/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,6 @@ declare_passes! {
191191
Final
192192
};
193193
mod simplify_comparison_integral : SimplifyComparisonIntegral;
194-
mod single_use_consts : SingleUseConsts;
195194
mod sroa : ScalarReplacementOfAggregates;
196195
mod strip_debuginfo : StripDebugInfo;
197196
mod unreachable_enum_branching : UnreachableEnumBranching;
@@ -709,7 +708,6 @@ pub(crate) fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'
709708
&simplify::SimplifyLocals::AfterGVN,
710709
&match_branches::MatchBranchSimplification,
711710
&dataflow_const_prop::DataflowConstProp,
712-
&single_use_consts::SingleUseConsts,
713711
&o1(simplify_branches::SimplifyConstCondition::AfterConstProp),
714712
&jump_threading::JumpThreading,
715713
&early_otherwise_branch::EarlyOtherwiseBranch,

compiler/rustc_mir_transform/src/single_use_consts.rs

Lines changed: 0 additions & 205 deletions
This file was deleted.

0 commit comments

Comments
 (0)