Skip to content

Commit 2989275

Browse files
authored
Rollup merge of rust-lang#97077 - ouz-a:Optimize-backend, r=oli-obk
Simplify some code that depend on Deref Now that we can assume rust-lang#97025 works, it's safe to expect Deref is always in the first place of projections. With this, I was able to simplify some code that depended on Deref's place in projections. When we are able to move Derefer before `ElaborateDrops` successfully we will be able to optimize more places. r? `@oli-obk`
2 parents c1647e1 + 9f00d83 commit 2989275

File tree

3 files changed

+22
-30
lines changed

3 files changed

+22
-30
lines changed

compiler/rustc_codegen_ssa/src/mir/place.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -435,18 +435,12 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
435435
LocalRef::Place(place) => place,
436436
LocalRef::UnsizedPlace(place) => bx.load_operand(place).deref(cx),
437437
LocalRef::Operand(..) => {
438-
if let Some(elem) = place_ref
439-
.projection
440-
.iter()
441-
.enumerate()
442-
.find(|elem| matches!(elem.1, mir::ProjectionElem::Deref))
443-
{
444-
base = elem.0 + 1;
438+
if place_ref.has_deref() {
439+
base = 1;
445440
let cg_base = self.codegen_consume(
446441
bx,
447-
mir::PlaceRef { projection: &place_ref.projection[..elem.0], ..place_ref },
442+
mir::PlaceRef { projection: &place_ref.projection[..0], ..place_ref },
448443
);
449-
450444
cg_base.deref(bx.cx())
451445
} else {
452446
bug!("using operand local {:?} as place", place_ref);

compiler/rustc_middle/src/mir/mod.rs

+14
Original file line numberDiff line numberDiff line change
@@ -1461,6 +1461,14 @@ impl<'tcx> Place<'tcx> {
14611461
self.projection.iter().any(|elem| elem.is_indirect())
14621462
}
14631463

1464+
/// If MirPhase >= Derefered and if projection contains Deref,
1465+
/// It's guaranteed to be in the first place
1466+
pub fn has_deref(&self) -> bool {
1467+
// To make sure this is not accidently used in wrong mir phase
1468+
debug_assert!(!self.projection[1..].contains(&PlaceElem::Deref));
1469+
self.projection.first() == Some(&PlaceElem::Deref)
1470+
}
1471+
14641472
/// Finds the innermost `Local` from this `Place`, *if* it is either a local itself or
14651473
/// a single deref of a local.
14661474
#[inline(always)]
@@ -1533,6 +1541,12 @@ impl<'tcx> PlaceRef<'tcx> {
15331541
}
15341542
}
15351543

1544+
/// If MirPhase >= Derefered and if projection contains Deref,
1545+
/// It's guaranteed to be in the first place
1546+
pub fn has_deref(&self) -> bool {
1547+
self.projection.first() == Some(&PlaceElem::Deref)
1548+
}
1549+
15361550
/// If this place represents a local variable like `_X` with no
15371551
/// projections, return `Some(_X)`.
15381552
#[inline]

compiler/rustc_mir_transform/src/add_retag.rs

+5-21
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,9 @@ pub struct AddRetag;
1515
/// (Concurrent accesses by other threads are no problem as these are anyway non-atomic
1616
/// copies. Data races are UB.)
1717
fn is_stable(place: PlaceRef<'_>) -> bool {
18-
place.projection.iter().all(|elem| {
19-
match elem {
20-
// Which place this evaluates to can change with any memory write,
21-
// so cannot assume this to be stable.
22-
ProjectionElem::Deref => false,
23-
// Array indices are interesting, but MIR building generates a *fresh*
24-
// temporary for every array access, so the index cannot be changed as
25-
// a side-effect.
26-
ProjectionElem::Index { .. } |
27-
// The rest is completely boring, they just offset by a constant.
28-
ProjectionElem::Field { .. } |
29-
ProjectionElem::ConstantIndex { .. } |
30-
ProjectionElem::Subslice { .. } |
31-
ProjectionElem::Downcast { .. } => true,
32-
}
33-
})
18+
// Which place this evaluates to can change with any memory write,
19+
// so cannot assume deref to be stable.
20+
!place.has_deref()
3421
}
3522

3623
/// Determine whether this type may contain a reference (or box), and thus needs retagging.
@@ -91,11 +78,8 @@ impl<'tcx> MirPass<'tcx> for AddRetag {
9178
};
9279
let place_base_raw = |place: &Place<'tcx>| {
9380
// If this is a `Deref`, get the type of what we are deref'ing.
94-
let deref_base =
95-
place.projection.iter().rposition(|p| matches!(p, ProjectionElem::Deref));
96-
if let Some(deref_base) = deref_base {
97-
let base_proj = &place.projection[..deref_base];
98-
let ty = Place::ty_from(place.local, base_proj, &*local_decls, tcx).ty;
81+
if place.has_deref() {
82+
let ty = &local_decls[place.local].ty;
9983
ty.is_unsafe_ptr()
10084
} else {
10185
// Not a deref, and thus not raw.

0 commit comments

Comments
 (0)