Skip to content

Commit 2ad7bc9

Browse files
committed
Replace SingleUseConsts by GVN.
1 parent b8033b4 commit 2ad7bc9

File tree

126 files changed

+561
-602
lines changed

Some content is hidden

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

126 files changed

+561
-602
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.
@@ -865,10 +869,11 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
865869

866870
/// Simplify the projection chain if we know better.
867871
#[instrument(level = "trace", skip(self))]
868-
fn simplify_place_projection(&mut self, place: &mut Place<'tcx>, location: Location) {
872+
fn simplify_place_projection(&mut self, place: &mut Place<'tcx>, location: Option<Location>) {
869873
// If the projection is indirect, we treat the local as a value, so can replace it with
870874
// another local.
871-
if place.is_indirect_first_projection()
875+
if let Some(location) = location
876+
&& place.is_indirect_first_projection()
872877
&& let Some(base) = self.locals[place.local]
873878
&& let Some(new_local) = self.try_as_local(base, location)
874879
&& place.local != new_local
@@ -890,7 +895,8 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
890895
{
891896
projection.to_mut()[i] =
892897
ProjectionElem::ConstantIndex { offset, min_length, from_end: false };
893-
} else if let Some(new_idx_local) = self.try_as_local(idx, location)
898+
} else if let Some(location) = location
899+
&& let Some(new_idx_local) = self.try_as_local(idx, location)
894900
&& idx_local != new_idx_local
895901
{
896902
projection.to_mut()[i] = ProjectionElem::Index(new_idx_local);
@@ -912,7 +918,7 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
912918
fn compute_place_value(
913919
&mut self,
914920
place: Place<'tcx>,
915-
location: Location,
921+
location: Option<Location>,
916922
) -> Result<VnIndex, PlaceRef<'tcx>> {
917923
// Invariant: `place` and `place_ref` point to the same value, even if they point to
918924
// different memory locations.
@@ -923,7 +929,9 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
923929
// Invariant: `value` has type `place_ty`, with optional downcast variant if needed.
924930
let mut place_ty = PlaceTy::from_ty(self.local_decls[place.local].ty);
925931
for (index, proj) in place.projection.iter().enumerate() {
926-
if let Some(local) = self.try_as_local(value, location) {
932+
if let Some(location) = location
933+
&& let Some(local) = self.try_as_local(value, location)
934+
{
927935
// Both `local` and `Place { local: place.local, projection: projection[..index] }`
928936
// hold the same value. Therefore, following place holds the value in the original
929937
// `place`.
@@ -948,13 +956,14 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
948956
fn simplify_place_value(
949957
&mut self,
950958
place: &mut Place<'tcx>,
951-
location: Location,
959+
location: Option<Location>,
952960
) -> Option<VnIndex> {
953961
self.simplify_place_projection(place, location);
954962

955963
match self.compute_place_value(*place, location) {
956964
Ok(value) => {
957-
if let Some(new_place) = self.try_as_place(value, location, true)
965+
if let Some(location) = location
966+
&& let Some(new_place) = self.try_as_place(value, location, true)
958967
&& (new_place.local != place.local
959968
|| new_place.projection.len() < place.projection.len())
960969
{
@@ -985,7 +994,7 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
985994
let value = match *operand {
986995
Operand::Constant(ref constant) => self.insert_constant(constant.const_),
987996
Operand::Copy(ref mut place) | Operand::Move(ref mut place) => {
988-
self.simplify_place_value(place, location)?
997+
self.simplify_place_value(place, Some(location))?
989998
}
990999
};
9911000
if let Some(const_) = self.try_as_constant(value) {
@@ -1019,11 +1028,11 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
10191028
Rvalue::NullaryOp(op, ty) => Value::NullaryOp(op, ty),
10201029
Rvalue::Aggregate(..) => return self.simplify_aggregate(lhs, rvalue, location),
10211030
Rvalue::Ref(_, borrow_kind, ref mut place) => {
1022-
self.simplify_place_projection(place, location);
1031+
self.simplify_place_projection(place, Some(location));
10231032
return self.new_pointer(*place, AddressKind::Ref(borrow_kind));
10241033
}
10251034
Rvalue::RawPtr(mutbl, ref mut place) => {
1026-
self.simplify_place_projection(place, location);
1035+
self.simplify_place_projection(place, Some(location));
10271036
return self.new_pointer(*place, AddressKind::Address(mutbl));
10281037
}
10291038
Rvalue::WrapUnsafeBinder(ref mut op, _) => {
@@ -1042,7 +1051,7 @@ impl<'body, 'a, 'tcx> VnState<'body, 'a, 'tcx> {
10421051
return self.simplify_unary(op, arg_op, location);
10431052
}
10441053
Rvalue::Discriminant(ref mut place) => {
1045-
let place = self.simplify_place_value(place, location)?;
1054+
let place = self.simplify_place_value(place, Some(location))?;
10461055
if let Some(discr) = self.simplify_discriminant(place) {
10471056
return Some(discr);
10481057
}
@@ -1852,8 +1861,21 @@ impl<'tcx> MutVisitor<'tcx> for VnState<'_, '_, 'tcx> {
18521861
self.tcx
18531862
}
18541863

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

18771899
let value = self.simplify_rvalue(lhs, rvalue, location);
18781900
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.

tests/codegen-llvm/amdgpu-addrspacecast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ extern crate minicore;
1010

1111
// CHECK-LABEL: @ref_of_local
1212
// CHECK: [[alloca:%[0-9]]] = alloca
13-
// CHECK: %i = addrspacecast ptr addrspace(5) [[alloca]] to ptr
13+
// CHECK: {{%.*}} = addrspacecast ptr addrspace(5) [[alloca]] to ptr
1414
#[no_mangle]
1515
pub fn ref_of_local(f: fn(&i32)) {
1616
let i = 0;

tests/incremental/hashes/enum_constructors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ pub fn change_field_order_struct_like() -> Enum {
6464
#[cfg(not(any(cfail1,cfail4)))]
6565
#[rustc_clean(cfg="cfail2", except="opt_hir_owner_nodes,typeck")]
6666
#[rustc_clean(cfg="cfail3")]
67-
#[rustc_clean(cfg="cfail5", except="opt_hir_owner_nodes,typeck,optimized_mir")]
67+
#[rustc_clean(cfg="cfail5", except="opt_hir_owner_nodes,typeck")]
6868
#[rustc_clean(cfg="cfail6")]
6969
// FIXME(michaelwoerister):Interesting. I would have thought that that changes the MIR. And it
7070
// would if it were not all constants

tests/incremental/hashes/struct_constructors.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ pub fn change_field_order_regular_struct() -> RegularStruct {
6161
#[cfg(not(any(cfail1,cfail4)))]
6262
#[rustc_clean(cfg="cfail2", except="opt_hir_owner_nodes,typeck")]
6363
#[rustc_clean(cfg="cfail3")]
64-
#[rustc_clean(cfg="cfail5", except="opt_hir_owner_nodes,typeck,optimized_mir")]
64+
#[rustc_clean(cfg="cfail5", except="opt_hir_owner_nodes,typeck")]
6565
#[rustc_clean(cfg="cfail6")]
6666
pub fn change_field_order_regular_struct() -> RegularStruct {
6767
RegularStruct {

0 commit comments

Comments
 (0)