1
Fork 0

Auto merge of #94276 - scottmcm:primitive-clone, r=oli-obk

mir-opt: Replace clone on primitives with copy

We can't do it for everything, but it would be nice to at least stop making calls to clone methods in debug from things like derived-clones.

r? `@ghost`
This commit is contained in:
bors 2022-03-11 01:51:49 +00:00
commit c5a43b8d39
17 changed files with 273 additions and 5 deletions

View file

@ -1915,6 +1915,27 @@ impl<'tcx> Place<'tcx> {
(base, proj)
})
}
/// Generates a new place by appending `more_projections` to the existing ones
/// and interning the result.
pub fn project_deeper(self, more_projections: &[PlaceElem<'tcx>], tcx: TyCtxt<'tcx>) -> Self {
if more_projections.is_empty() {
return self;
}
let mut v: Vec<PlaceElem<'tcx>>;
let new_projections = if self.projection.is_empty() {
more_projections
} else {
v = Vec::with_capacity(self.projection.len() + more_projections.len());
v.extend(self.projection);
v.extend(more_projections);
&v
};
Place { local: self.local, projection: tcx.intern_place_elems(new_projections) }
}
}
impl From<Local> for Place<'_> {
@ -2187,6 +2208,15 @@ impl<'tcx> Operand<'tcx> {
Operand::Copy(_) | Operand::Move(_) => None,
}
}
/// Gets the `ty::FnDef` from an operand if it's a constant function item.
///
/// While this is unlikely in general, it's the normal case of what you'll
/// find as the `func` in a [`TerminatorKind::Call`].
pub fn const_fn_def(&self) -> Option<(DefId, SubstsRef<'tcx>)> {
let const_ty = self.constant()?.literal.ty();
if let ty::FnDef(def_id, substs) = *const_ty.kind() { Some((def_id, substs)) } else { None }
}
}
///////////////////////////////////////////////////////////////////////////

View file

@ -2380,6 +2380,57 @@ impl<'tcx> Ty<'tcx> {
}
}
}
/// Fast path helper for primitives which are always `Copy` and which
/// have a side-effect-free `Clone` impl.
///
/// Returning true means the type is known to be pure and `Copy+Clone`.
/// Returning `false` means nothing -- could be `Copy`, might not be.
///
/// This is mostly useful for optimizations, as there are the types
/// on which we can replace cloning with dereferencing.
pub fn is_trivially_pure_clone_copy(self) -> bool {
match self.kind() {
ty::Bool | ty::Char | ty::Never => true,
// These aren't even `Clone`
ty::Str | ty::Slice(..) | ty::Foreign(..) | ty::Dynamic(..) => false,
ty::Int(..) | ty::Uint(..) | ty::Float(..) => true,
// The voldemort ZSTs are fine.
ty::FnDef(..) => true,
ty::Array(element_ty, _len) => element_ty.is_trivially_pure_clone_copy(),
// A 100-tuple isn't "trivial", so doing this only for reasonable sizes.
ty::Tuple(field_tys) => {
field_tys.len() <= 3 && field_tys.iter().all(Self::is_trivially_pure_clone_copy)
}
// Sometimes traits aren't implemented for every ABI or arity,
// because we can't be generic over everything yet.
ty::FnPtr(..) => false,
// Definitely absolutely not copy.
ty::Ref(_, _, hir::Mutability::Mut) => false,
// Thin pointers & thin shared references are pure-clone-copy, but for
// anything with custom metadata it might be more complicated.
ty::Ref(_, _, hir::Mutability::Not) | ty::RawPtr(..) => false,
ty::Generator(..) | ty::GeneratorWitness(..) => false,
// Might be, but not "trivial" so just giving the safe answer.
ty::Adt(..) | ty::Closure(..) | ty::Opaque(..) => false,
ty::Projection(..) | ty::Param(..) | ty::Infer(..) | ty::Error(..) => false,
ty::Bound(..) | ty::Placeholder(..) => {
bug!("`is_trivially_pure_clone_copy` applied to unexpected type: {:?}", self);
}
}
}
}
/// Extra information about why we ended up with a particular variance.

View file

@ -704,7 +704,7 @@ impl<'tcx> Ty<'tcx> {
tcx_at: TyCtxtAt<'tcx>,
param_env: ty::ParamEnv<'tcx>,
) -> bool {
tcx_at.is_copy_raw(param_env.and(self))
self.is_trivially_pure_clone_copy() || tcx_at.is_copy_raw(param_env.and(self))
}
/// Checks whether values of this type `T` have a size known at