Skip to content

Commit

Permalink
Remove PlaceBase enum and make Place base field be local: Local
Browse files Browse the repository at this point in the history
  • Loading branch information
spastorino authored and oli-obk committed Jan 10, 2020
1 parent fd5aa32 commit 5d9b399
Show file tree
Hide file tree
Showing 50 changed files with 457 additions and 657 deletions.
64 changes: 13 additions & 51 deletions src/librustc/mir/mod.rs
Expand Up @@ -1655,20 +1655,14 @@ impl Debug for Statement<'_> {
/// changing or disturbing program state.
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, HashStable)]
pub struct Place<'tcx> {
pub base: PlaceBase,
pub local: Local,

/// projection out of a place (access a field, deref a pointer, etc)
pub projection: &'tcx List<PlaceElem<'tcx>>,
}

impl<'tcx> rustc_serialize::UseSpecializedDecodable for Place<'tcx> {}

#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable, HashStable)]
pub enum PlaceBase {
/// local variable
Local(Local),
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[derive(RustcEncodable, RustcDecodable, HashStable)]
pub enum ProjectionElem<V, T> {
Expand Down Expand Up @@ -1756,14 +1750,14 @@ rustc_index::newtype_index! {

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct PlaceRef<'a, 'tcx> {
pub base: &'a PlaceBase,
pub local: &'a Local,
pub projection: &'a [PlaceElem<'tcx>],
}

impl<'tcx> Place<'tcx> {
// FIXME change this to a const fn by also making List::empty a const fn.
pub fn return_place() -> Place<'tcx> {
Place { base: PlaceBase::Local(RETURN_PLACE), projection: List::empty() }
Place { local: RETURN_PLACE, projection: List::empty() }
}

/// Returns `true` if this `Place` contains a `Deref` projection.
Expand All @@ -1780,10 +1774,8 @@ impl<'tcx> Place<'tcx> {
// FIXME: can we safely swap the semantics of `fn base_local` below in here instead?
pub fn local_or_deref_local(&self) -> Option<Local> {
match self.as_ref() {
PlaceRef { base: &PlaceBase::Local(local), projection: &[] }
| PlaceRef { base: &PlaceBase::Local(local), projection: &[ProjectionElem::Deref] } => {
Some(local)
}
PlaceRef { local, projection: &[] }
| PlaceRef { local, projection: &[ProjectionElem::Deref] } => Some(*local),
_ => None,
}
}
Expand All @@ -1795,19 +1787,13 @@ impl<'tcx> Place<'tcx> {
}

pub fn as_ref(&self) -> PlaceRef<'_, 'tcx> {
PlaceRef { base: &self.base, projection: &self.projection }
PlaceRef { local: &self.local, projection: &self.projection }
}
}

impl From<Local> for Place<'_> {
fn from(local: Local) -> Self {
Place { base: local.into(), projection: List::empty() }
}
}

impl From<Local> for PlaceBase {
fn from(local: Local) -> Self {
PlaceBase::Local(local)
Place { local: local.into(), projection: List::empty() }
}
}

Expand All @@ -1818,10 +1804,8 @@ impl<'a, 'tcx> PlaceRef<'a, 'tcx> {
// FIXME: can we safely swap the semantics of `fn base_local` below in here instead?
pub fn local_or_deref_local(&self) -> Option<Local> {
match self {
PlaceRef { base: PlaceBase::Local(local), projection: [] }
| PlaceRef { base: PlaceBase::Local(local), projection: [ProjectionElem::Deref] } => {
Some(*local)
}
PlaceRef { local, projection: [] }
| PlaceRef { local, projection: [ProjectionElem::Deref] } => Some(**local),
_ => None,
}
}
Expand All @@ -1830,7 +1814,7 @@ impl<'a, 'tcx> PlaceRef<'a, 'tcx> {
/// projections, return `Some(_X)`.
pub fn as_local(&self) -> Option<Local> {
match self {
PlaceRef { base: PlaceBase::Local(l), projection: [] } => Some(*l),
PlaceRef { local, projection: [] } => Some(**local),
_ => None,
}
}
Expand All @@ -1852,7 +1836,7 @@ impl Debug for Place<'_> {
}
}

write!(fmt, "{:?}", self.base)?;
write!(fmt, "{:?}", self.local)?;

for elem in self.projection.iter() {
match elem {
Expand Down Expand Up @@ -1896,14 +1880,6 @@ impl Debug for Place<'_> {
}
}

impl Debug for PlaceBase {
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
match *self {
PlaceBase::Local(id) => write!(fmt, "{:?}", id),
}
}
}

///////////////////////////////////////////////////////////////////////////
// Scopes

Expand Down Expand Up @@ -2964,25 +2940,11 @@ impl<'tcx> TypeFoldable<'tcx> for GeneratorKind {

impl<'tcx> TypeFoldable<'tcx> for Place<'tcx> {
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
Place { base: self.base.fold_with(folder), projection: self.projection.fold_with(folder) }
Place { local: self.local.fold_with(folder), projection: self.projection.fold_with(folder) }
}

fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
self.base.visit_with(visitor) || self.projection.visit_with(visitor)
}
}

impl<'tcx> TypeFoldable<'tcx> for PlaceBase {
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
match self {
PlaceBase::Local(local) => PlaceBase::Local(local.fold_with(folder)),
}
}

fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
match self {
PlaceBase::Local(local) => local.visit_with(visitor),
}
self.local.visit_with(visitor) || self.projection.visit_with(visitor)
}
}

Expand Down
19 changes: 5 additions & 14 deletions src/librustc/mir/tcx.rs
Expand Up @@ -114,7 +114,7 @@ impl<'tcx> PlaceTy<'tcx> {

impl<'tcx> Place<'tcx> {
pub fn ty_from<D>(
base: &PlaceBase,
local: &Local,
projection: &[PlaceElem<'tcx>],
local_decls: &D,
tcx: TyCtxt<'tcx>,
Expand All @@ -124,25 +124,16 @@ impl<'tcx> Place<'tcx> {
{
projection
.iter()
.fold(base.ty(local_decls), |place_ty, elem| place_ty.projection_ty(tcx, elem))
.fold(PlaceTy::from_ty(local_decls.local_decls()[*local].ty), |place_ty, elem| {
place_ty.projection_ty(tcx, elem)
})
}

pub fn ty<D>(&self, local_decls: &D, tcx: TyCtxt<'tcx>) -> PlaceTy<'tcx>
where
D: HasLocalDecls<'tcx>,
{
Place::ty_from(&self.base, &self.projection, local_decls, tcx)
}
}

impl<'tcx> PlaceBase {
pub fn ty<D>(&self, local_decls: &D) -> PlaceTy<'tcx>
where
D: HasLocalDecls<'tcx>,
{
match self {
PlaceBase::Local(index) => PlaceTy::from_ty(local_decls.local_decls()[*index].ty),
}
Place::ty_from(&self.local, &self.projection, local_decls, tcx)
}
}

Expand Down
32 changes: 14 additions & 18 deletions src/librustc/mir/visit.rs
Expand Up @@ -164,10 +164,10 @@ macro_rules! make_mir_visitor {
}

fn visit_place_base(&mut self,
base: & $($mutability)? PlaceBase,
local: & $($mutability)? Local,
context: PlaceContext,
location: Location) {
self.super_place_base(base, context, location);
self.super_place_base(local, context, location);
}

visit_place_fns!($($mutability)?);
Expand Down Expand Up @@ -705,14 +705,10 @@ macro_rules! make_mir_visitor {
}

fn super_place_base(&mut self,
place_base: & $($mutability)? PlaceBase,
local: & $($mutability)? Local,
context: PlaceContext,
location: Location) {
match place_base {
PlaceBase::Local(local) => {
self.visit_local(local, context, location);
}
}
self.visit_local(local, context, location);
}

fn super_local_decl(&mut self,
Expand Down Expand Up @@ -845,7 +841,7 @@ macro_rules! visit_place_fns {
context: PlaceContext,
location: Location,
) {
self.visit_place_base(&mut place.base, context, location);
self.visit_place_base(&mut place.local, context, location);

if let Some(new_projection) = self.process_projection(&place.projection) {
place.projection = self.tcx().intern_place_elems(&new_projection);
Expand Down Expand Up @@ -886,23 +882,23 @@ macro_rules! visit_place_fns {
() => (
fn visit_projection(
&mut self,
base: &PlaceBase,
local: &Local,
projection: &[PlaceElem<'tcx>],
context: PlaceContext,
location: Location,
) {
self.super_projection(base, projection, context, location);
self.super_projection(local, projection, context, location);
}

fn visit_projection_elem(
&mut self,
base: &PlaceBase,
local: &Local,
proj_base: &[PlaceElem<'tcx>],
elem: &PlaceElem<'tcx>,
context: PlaceContext,
location: Location,
) {
self.super_projection_elem(base, proj_base, elem, context, location);
self.super_projection_elem(local, proj_base, elem, context, location);
}

fn super_place(
Expand All @@ -921,31 +917,31 @@ macro_rules! visit_place_fns {
};
}

self.visit_place_base(&place.base, context, location);
self.visit_place_base(&place.local, context, location);

self.visit_projection(&place.base,
self.visit_projection(&place.local,
&place.projection,
context,
location);
}

fn super_projection(
&mut self,
base: &PlaceBase,
local: &Local,
projection: &[PlaceElem<'tcx>],
context: PlaceContext,
location: Location,
) {
let mut cursor = projection;
while let [proj_base @ .., elem] = cursor {
cursor = proj_base;
self.visit_projection_elem(base, cursor, elem, context, location);
self.visit_projection_elem(local, cursor, elem, context, location);
}
}

fn super_projection_elem(
&mut self,
_base: &PlaceBase,
_local: &Local,
_proj_base: &[PlaceElem<'tcx>],
elem: &PlaceElem<'tcx>,
_context: PlaceContext,
Expand Down
4 changes: 2 additions & 2 deletions src/librustc/ty/codec.rs
Expand Up @@ -226,11 +226,11 @@ pub fn decode_place<D>(decoder: &mut D) -> Result<mir::Place<'tcx>, D::Error>
where
D: TyDecoder<'tcx>,
{
let base: mir::PlaceBase = Decodable::decode(decoder)?;
let local: mir::Local = Decodable::decode(decoder)?;
let len = decoder.read_usize()?;
let projection: &'tcx List<mir::PlaceElem<'tcx>> =
decoder.tcx().mk_place_elems((0..len).map(|_| Decodable::decode(decoder)))?;
Ok(mir::Place { base, projection })
Ok(mir::Place { local, projection })
}

#[inline]
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/context.rs
Expand Up @@ -2434,7 +2434,7 @@ impl<'tcx> TyCtxt<'tcx> {
let mut projection = place.projection.to_vec();
projection.push(elem);

Place { base: place.base, projection: self.intern_place_elems(&projection) }
Place { local: place.local, projection: self.intern_place_elems(&projection) }
}

pub fn intern_existential_predicates(
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_codegen_llvm/debuginfo/metadata.rs
Expand Up @@ -1286,9 +1286,9 @@ fn generator_layout_and_saved_local_names(
let generator_layout = body.generator_layout.as_ref().unwrap();
let mut generator_saved_local_names = IndexVec::from_elem(None, &generator_layout.field_tys);

let state_arg = mir::PlaceBase::Local(mir::Local::new(1));
let state_arg = mir::Local::new(1);
for var in &body.var_debug_info {
if var.place.base != state_arg {
if var.place.local != state_arg {
continue;
}
match var.place.projection[..] {
Expand Down
18 changes: 6 additions & 12 deletions src/librustc_codegen_ssa/mir/analyze.rs
Expand Up @@ -128,17 +128,13 @@ impl<Bx: BuilderMethods<'a, 'tcx>> LocalAnalyzer<'mir, 'a, 'tcx, Bx> {
};
if is_consume {
let base_ty =
mir::Place::ty_from(place_ref.base, proj_base, *self.fx.mir, cx.tcx());
mir::Place::ty_from(place_ref.local, proj_base, *self.fx.mir, cx.tcx());
let base_ty = self.fx.monomorphize(&base_ty);

// ZSTs don't require any actual memory access.
let elem_ty = base_ty.projection_ty(cx.tcx(), elem).ty;
let elem_ty = self.fx.monomorphize(&elem_ty);
let span = match place_ref.base {
mir::PlaceBase::Local(index) => {
self.fx.mir.local_decls[*index].source_info.span
}
};
let span = self.fx.mir.local_decls[*place_ref.local].source_info.span;
if cx.spanned_layout_of(elem_ty, span).is_zst() {
return;
}
Expand Down Expand Up @@ -178,9 +174,7 @@ impl<Bx: BuilderMethods<'a, 'tcx>> LocalAnalyzer<'mir, 'a, 'tcx, Bx> {
// We use `NonUseContext::VarDebugInfo` for the base,
// which might not force the base local to memory,
// so we have to do it manually.
match place_ref.base {
mir::PlaceBase::Local(local) => self.visit_local(&local, context, location),
}
self.visit_local(place_ref.local, context, location);
}
}

Expand All @@ -191,7 +185,7 @@ impl<Bx: BuilderMethods<'a, 'tcx>> LocalAnalyzer<'mir, 'a, 'tcx, Bx> {
}

self.process_place(
&mir::PlaceRef { base: place_ref.base, projection: proj_base },
&mir::PlaceRef { local: place_ref.local, projection: proj_base },
base_context,
location,
);
Expand All @@ -218,8 +212,8 @@ impl<Bx: BuilderMethods<'a, 'tcx>> LocalAnalyzer<'mir, 'a, 'tcx, Bx> {
};
}

self.visit_place_base(place_ref.base, context, location);
self.visit_projection(place_ref.base, place_ref.projection, context, location);
self.visit_place_base(place_ref.local, context, location);
self.visit_projection(place_ref.local, place_ref.projection, context, location);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_codegen_ssa/mir/block.rs
Expand Up @@ -1109,7 +1109,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
} else {
self.codegen_place(
bx,
&mir::PlaceRef { base: &dest.base, projection: &dest.projection },
&mir::PlaceRef { local: &dest.local, projection: &dest.projection },
)
};
if fn_ret.is_indirect() {
Expand Down
4 changes: 1 addition & 3 deletions src/librustc_codegen_ssa/mir/debuginfo.rs
Expand Up @@ -258,9 +258,7 @@ pub fn per_local_var_debug_info(
if tcx.sess.opts.debuginfo == DebugInfo::Full || !tcx.sess.fewer_names() {
let mut per_local = IndexVec::from_elem(vec![], &body.local_decls);
for var in &body.var_debug_info {
match var.place.base {
mir::PlaceBase::Local(local) => per_local[local].push(var),
}
per_local[var.place.local].push(var);
}
Some(per_local)
} else {
Expand Down

0 comments on commit 5d9b399

Please sign in to comment.