Skip to content

Commit

Permalink
Remove Static from PlaceBase
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 9e70c47 commit fd5aa32
Show file tree
Hide file tree
Showing 37 changed files with 193 additions and 452 deletions.
54 changes: 6 additions & 48 deletions src/librustc/mir/mod.rs
Expand Up @@ -1655,7 +1655,7 @@ 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<'tcx>,
pub base: PlaceBase,

/// projection out of a place (access a field, deref a pointer, etc)
pub projection: &'tcx List<PlaceElem<'tcx>>,
Expand All @@ -1664,34 +1664,9 @@ pub struct Place<'tcx> {
impl<'tcx> rustc_serialize::UseSpecializedDecodable for Place<'tcx> {}

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

/// static or static mut variable
Static(Box<Static<'tcx>>),
}

/// We store the normalized type to avoid requiring normalization when reading MIR
#[derive(
Clone,
Debug,
PartialEq,
Eq,
PartialOrd,
Ord,
Hash,
RustcEncodable,
RustcDecodable,
HashStable
)]
pub struct Static<'tcx> {
pub ty: Ty<'tcx>,
/// The `DefId` of the item this static was declared in. For promoted values, usually, this is
/// the same as the `DefId` of the `mir::Body` containing the `Place` this promoted appears in.
/// However, after inlining, that might no longer be the case as inlined `Place`s are copied
/// into the calling frame.
pub def_id: DefId,
}

#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
Expand Down Expand Up @@ -1781,7 +1756,7 @@ rustc_index::newtype_index! {

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

Expand Down Expand Up @@ -1830,7 +1805,7 @@ impl From<Local> for Place<'_> {
}
}

impl From<Local> for PlaceBase<'_> {
impl From<Local> for PlaceBase {
fn from(local: Local) -> Self {
PlaceBase::Local(local)
}
Expand Down Expand Up @@ -1921,13 +1896,10 @@ impl Debug for Place<'_> {
}
}

impl Debug for PlaceBase<'_> {
impl Debug for PlaceBase {
fn fmt(&self, fmt: &mut Formatter<'_>) -> fmt::Result {
match *self {
PlaceBase::Local(id) => write!(fmt, "{:?}", id),
PlaceBase::Static(box self::Static { ty, def_id }) => {
write!(fmt, "({}: {:?})", ty::tls::with(|tcx| tcx.def_path_str(def_id)), ty)
}
}
}
}
Expand Down Expand Up @@ -3000,18 +2972,16 @@ impl<'tcx> TypeFoldable<'tcx> for Place<'tcx> {
}
}

impl<'tcx> TypeFoldable<'tcx> for PlaceBase<'tcx> {
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)),
PlaceBase::Static(static_) => PlaceBase::Static(static_.fold_with(folder)),
}
}

fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
match self {
PlaceBase::Local(local) => local.visit_with(visitor),
PlaceBase::Static(static_) => (**static_).visit_with(visitor),
}
}
}
Expand All @@ -3027,18 +2997,6 @@ impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<PlaceElem<'tcx>> {
}
}

impl<'tcx> TypeFoldable<'tcx> for Static<'tcx> {
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
Static { ty: self.ty.fold_with(folder), def_id: self.def_id }
}

fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
let Static { ty, def_id: _ } = self;

ty.visit_with(visitor)
}
}

impl<'tcx> TypeFoldable<'tcx> for Rvalue<'tcx> {
fn super_fold_with<F: TypeFolder<'tcx>>(&self, folder: &mut F) -> Self {
use crate::mir::Rvalue::*;
Expand Down
5 changes: 2 additions & 3 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<'tcx>,
base: &PlaceBase,
projection: &[PlaceElem<'tcx>],
local_decls: &D,
tcx: TyCtxt<'tcx>,
Expand All @@ -135,14 +135,13 @@ impl<'tcx> Place<'tcx> {
}
}

impl<'tcx> PlaceBase<'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),
PlaceBase::Static(data) => PlaceTy::from_ty(data.ty),
}
}
}
Expand Down
15 changes: 6 additions & 9 deletions src/librustc/mir/visit.rs
Expand Up @@ -164,7 +164,7 @@ macro_rules! make_mir_visitor {
}

fn visit_place_base(&mut self,
base: & $($mutability)? PlaceBase<'tcx>,
base: & $($mutability)? PlaceBase,
context: PlaceContext,
location: Location) {
self.super_place_base(base, context, location);
Expand Down Expand Up @@ -705,16 +705,13 @@ macro_rules! make_mir_visitor {
}

fn super_place_base(&mut self,
place_base: & $($mutability)? PlaceBase<'tcx>,
place_base: & $($mutability)? PlaceBase,
context: PlaceContext,
location: Location) {
match place_base {
PlaceBase::Local(local) => {
self.visit_local(local, context, location);
}
PlaceBase::Static(box Static { ty, def_id: _ }) => {
self.visit_ty(& $($mutability)? *ty, TyContext::Location(location));
}
}
}

Expand Down Expand Up @@ -889,7 +886,7 @@ macro_rules! visit_place_fns {
() => (
fn visit_projection(
&mut self,
base: &PlaceBase<'tcx>,
base: &PlaceBase,
projection: &[PlaceElem<'tcx>],
context: PlaceContext,
location: Location,
Expand All @@ -899,7 +896,7 @@ macro_rules! visit_place_fns {

fn visit_projection_elem(
&mut self,
base: &PlaceBase<'tcx>,
base: &PlaceBase,
proj_base: &[PlaceElem<'tcx>],
elem: &PlaceElem<'tcx>,
context: PlaceContext,
Expand Down Expand Up @@ -934,7 +931,7 @@ macro_rules! visit_place_fns {

fn super_projection(
&mut self,
base: &PlaceBase<'tcx>,
base: &PlaceBase,
projection: &[PlaceElem<'tcx>],
context: PlaceContext,
location: Location,
Expand All @@ -948,7 +945,7 @@ macro_rules! visit_place_fns {

fn super_projection_elem(
&mut self,
_base: &PlaceBase<'tcx>,
_base: &PlaceBase,
_proj_base: &[PlaceElem<'tcx>],
elem: &PlaceElem<'tcx>,
_context: PlaceContext,
Expand Down
2 changes: 1 addition & 1 deletion src/librustc/ty/codec.rs
Expand Up @@ -226,7 +226,7 @@ pub fn decode_place<D>(decoder: &mut D) -> Result<mir::Place<'tcx>, D::Error>
where
D: TyDecoder<'tcx>,
{
let base: mir::PlaceBase<'tcx> = Decodable::decode(decoder)?;
let base: mir::PlaceBase = 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)))?;
Expand Down
13 changes: 6 additions & 7 deletions src/librustc_codegen_ssa/mir/analyze.rs
Expand Up @@ -14,7 +14,6 @@ use rustc::ty::layout::{HasTyCtxt, LayoutOf};
use rustc_data_structures::graph::dominators::Dominators;
use rustc_index::bit_set::BitSet;
use rustc_index::vec::{Idx, IndexVec};
use rustc_span::DUMMY_SP;

pub fn non_ssa_locals<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
fx: &FunctionCx<'a, 'tcx, Bx>,
Expand Down Expand Up @@ -135,10 +134,10 @@ impl<Bx: BuilderMethods<'a, 'tcx>> LocalAnalyzer<'mir, 'a, 'tcx, Bx> {
// 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 = if let mir::PlaceBase::Local(index) = place_ref.base {
self.fx.mir.local_decls[*index].source_info.span
} else {
DUMMY_SP
let span = match place_ref.base {
mir::PlaceBase::Local(index) => {
self.fx.mir.local_decls[*index].source_info.span
}
};
if cx.spanned_layout_of(elem_ty, span).is_zst() {
return;
Expand Down Expand Up @@ -179,8 +178,8 @@ 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.
if let mir::PlaceBase::Local(local) = place_ref.base {
self.visit_local(&local, context, location);
match place_ref.base {
mir::PlaceBase::Local(local) => self.visit_local(&local, context, location),
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_codegen_ssa/mir/debuginfo.rs
Expand Up @@ -258,8 +258,8 @@ 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 {
if let mir::PlaceBase::Local(local) = var.place.base {
per_local[local].push(var);
match var.place.base {
mir::PlaceBase::Local(local) => per_local[local].push(var),
}
}
Some(per_local)
Expand Down
64 changes: 32 additions & 32 deletions src/librustc_codegen_ssa/mir/operand.rs
Expand Up @@ -373,44 +373,44 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
) -> Option<OperandRef<'tcx, Bx::Value>> {
debug!("maybe_codegen_consume_direct(place_ref={:?})", place_ref);

if let mir::PlaceBase::Local(index) = place_ref.base {
match self.locals[*index] {
LocalRef::Operand(Some(mut o)) => {
// Moves out of scalar and scalar pair fields are trivial.
for elem in place_ref.projection.iter() {
match elem {
mir::ProjectionElem::Field(ref f, _) => {
o = o.extract_field(bx, f.index());
}
mir::ProjectionElem::Index(_)
| mir::ProjectionElem::ConstantIndex { .. } => {
// ZSTs don't require any actual memory access.
// FIXME(eddyb) deduplicate this with the identical
// checks in `codegen_consume` and `extract_field`.
let elem = o.layout.field(bx.cx(), 0);
if elem.is_zst() {
o = OperandRef::new_zst(bx, elem);
} else {
return None;
match place_ref.base {
mir::PlaceBase::Local(index) => {
match self.locals[*index] {
LocalRef::Operand(Some(mut o)) => {
// Moves out of scalar and scalar pair fields are trivial.
for elem in place_ref.projection.iter() {
match elem {
mir::ProjectionElem::Field(ref f, _) => {
o = o.extract_field(bx, f.index());
}
mir::ProjectionElem::Index(_)
| mir::ProjectionElem::ConstantIndex { .. } => {
// ZSTs don't require any actual memory access.
// FIXME(eddyb) deduplicate this with the identical
// checks in `codegen_consume` and `extract_field`.
let elem = o.layout.field(bx.cx(), 0);
if elem.is_zst() {
o = OperandRef::new_zst(bx, elem);
} else {
return None;
}
}
_ => return None,
}
_ => return None,
}
}

Some(o)
}
LocalRef::Operand(None) => {
bug!("use of {:?} before def", place_ref);
}
LocalRef::Place(..) | LocalRef::UnsizedPlace(..) => {
// watch out for locals that do not have an
// alloca; they are handled somewhat differently
None
Some(o)
}
LocalRef::Operand(None) => {
bug!("use of {:?} before def", place_ref);
}
LocalRef::Place(..) | LocalRef::UnsizedPlace(..) => {
// watch out for locals that do not have an
// alloca; they are handled somewhat differently
None
}
}
}
} else {
None
}
}

Expand Down
19 changes: 0 additions & 19 deletions src/librustc_codegen_ssa/mir/place.rs
Expand Up @@ -37,15 +37,6 @@ impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
PlaceRef { llval, llextra: None, layout, align }
}

fn new_thin_place<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
bx: &mut Bx,
llval: V,
layout: TyLayout<'tcx>,
) -> PlaceRef<'tcx, V> {
assert!(!bx.cx().type_has_metadata(layout.ty));
PlaceRef { llval, llextra: None, layout, align: layout.align.abi }
}

// FIXME(eddyb) pass something else for the name so no work is done
// unless LLVM IR names are turned on (e.g. for `--emit=llvm-ir`).
pub fn alloca<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
Expand Down Expand Up @@ -437,16 +428,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
}
}
}
mir::PlaceRef {
base: mir::PlaceBase::Static(box mir::Static { ty, def_id }),
projection: [],
} => {
// NB: The layout of a static may be unsized as is the case when working
// with a static that is an extern_type.
let layout = cx.layout_of(self.monomorphize(&ty));
let static_ = bx.get_static(*def_id);
PlaceRef::new_thin_place(bx, static_, layout)
}
mir::PlaceRef { base, projection: [proj_base @ .., mir::ProjectionElem::Deref] } => {
// Load the pointer from its location.
self.codegen_consume(bx, &mir::PlaceRef { base, projection: proj_base })
Expand Down
6 changes: 4 additions & 2 deletions src/librustc_mir/borrow_check/borrow_set.rs
Expand Up @@ -208,8 +208,10 @@ impl<'a, 'tcx> Visitor<'tcx> for GatherBorrows<'a, 'tcx> {

self.insert_as_pending_if_two_phase(location, &assigned_place, kind, idx);

if let mir::PlaceBase::Local(local) = borrowed_place.base {
self.local_map.entry(local).or_default().insert(idx);
match borrowed_place.base {
mir::PlaceBase::Local(local) => {
self.local_map.entry(local).or_default().insert(idx);
}
}
}

Expand Down
4 changes: 0 additions & 4 deletions src/librustc_mir/borrow_check/constraint_generation.rs
Expand Up @@ -207,10 +207,6 @@ impl<'cx, 'cg, 'tcx> ConstraintGeneration<'cx, 'cg, 'tcx> {
);
}

PlaceRef { base: &PlaceBase::Static(_), .. } => {
// Ignore kills of static or static mut variables.
}

PlaceRef { base: &PlaceBase::Local(local), projection: &[.., _] } => {
// Kill conflicting borrows of the innermost local.
debug!(
Expand Down

0 comments on commit fd5aa32

Please sign in to comment.