Skip to content

Commit e912a26

Browse files
committed
Trip unneeded has_no_projection checking
1 parent 6045a8a commit e912a26

File tree

23 files changed

+589
-625
lines changed

23 files changed

+589
-625
lines changed

src/librustc/mir/tcx.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,8 @@ impl<'tcx> Place<'tcx> {
136136
let mut place_ty = PlaceTy::from(self.base.ty(local_decls));
137137

138138
// apply .projection_ty() to all elems but only returns the final one.
139-
if !self.has_no_projection() {
140-
for elem in self.elems.iter() {
141-
place_ty = place_ty.projection_ty(tcx, elem);
142-
}
139+
for elem in self.elems.iter() {
140+
place_ty = place_ty.projection_ty(tcx, elem);
143141
}
144142

145143
place_ty

src/librustc_codegen_llvm/mir/analyze.rs

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -162,43 +162,41 @@ impl Visitor<'tcx> for LocalAnalyzer<'mir, 'a, 'll, 'tcx> {
162162
mut context: PlaceContext<'tcx>,
163163
location: Location) {
164164
let cx = self.fx.cx;
165+
let mut base_ty = place.base.ty(self.fx.mir);
165166

166-
if !place.has_no_projection() {
167-
let mut base_ty = place.base.ty(self.fx.mir);
168-
for elem in place.elems.iter().cloned() {
169-
debug!("visit_place(place={:?}, context={:?})", place, context);
167+
for elem in place.elems.iter().cloned() {
168+
debug!("visit_place(place={:?}, context={:?})", place, context);
170169

171-
// Allow uses of projections that are ZSTs or from scalar fields.
172-
let is_consume = match context {
173-
PlaceContext::Copy | PlaceContext::Move => true,
174-
_ => false
175-
};
170+
// Allow uses of projections that are ZSTs or from scalar fields.
171+
let is_consume = match context {
172+
PlaceContext::Copy | PlaceContext::Move => true,
173+
_ => false
174+
};
176175

177-
if is_consume {
178-
base_ty = self.fx.monomorphize(&base_ty);
176+
if is_consume {
177+
base_ty = self.fx.monomorphize(&base_ty);
179178

180-
if cx.layout_of(base_ty).is_zst() {
181-
return;
182-
}
179+
if cx.layout_of(base_ty).is_zst() {
180+
return;
181+
}
183182

184-
if let mir::ProjectionElem::Field(..) = elem {
185-
let layout = cx.layout_of(base_ty);
186-
if layout.is_llvm_immediate() || layout.is_llvm_scalar_pair() {
187-
// Recurse with the same context, instead of `Projection`,
188-
// potentially stopping at non-operand projections,
189-
// which would trigger `not_ssa` on locals.
190-
continue;
191-
}
183+
if let mir::ProjectionElem::Field(..) = elem {
184+
let layout = cx.layout_of(base_ty);
185+
if layout.is_llvm_immediate() || layout.is_llvm_scalar_pair() {
186+
// Recurse with the same context, instead of `Projection`,
187+
// potentially stopping at non-operand projections,
188+
// which would trigger `not_ssa` on locals.
189+
continue;
192190
}
193191
}
192+
}
194193

195-
// A deref projection only reads the pointer, never needs the place.
196-
if let mir::ProjectionElem::Deref = elem {
197-
context = PlaceContext::Copy;
198-
continue;
199-
}
200-
base_ty = PlaceTy::from(base_ty).projection_ty(cx.tcx, &elem).to_ty(cx.tcx);
194+
// A deref projection only reads the pointer, never needs the place.
195+
if let mir::ProjectionElem::Deref = elem {
196+
context = PlaceContext::Copy;
197+
continue;
201198
}
199+
base_ty = PlaceTy::from(base_ty).projection_ty(cx.tcx, &elem).to_ty(cx.tcx);
202200
}
203201

204202
self.super_place(place, context, location);

src/librustc_codegen_llvm/mir/operand.rs

Lines changed: 31 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -313,43 +313,42 @@ impl FunctionCx<'a, 'll, 'tcx> {
313313
let mut result = None;
314314
// watch out for locals that do not have an
315315
// alloca; they are handled somewhat differently
316-
if place.has_no_projection() {
317-
if let mir::PlaceBase::Local(index) = place.base {
318-
match self.locals[index] {
319-
LocalRef::Operand(Some(o)) => {
320-
result = Some(o);
321-
}
322-
LocalRef::Operand(None) => {
323-
bug!("use of {:?} before def", place);
316+
if let mir::PlaceBase::Local(index) = place.base {
317+
match self.locals[index] {
318+
LocalRef::Operand(Some(o)) => {
319+
result = Some(o);
320+
}
321+
LocalRef::Operand(None) => {
322+
bug!("use of {:?} before def", place);
323+
}
324+
LocalRef::Place(..) => {
325+
// use path below
326+
}
327+
};
328+
}
329+
330+
// Moves out of scalar and scalar pair fields are trivial.
331+
for e in place.elems.iter() {
332+
if let Some(o) = result {
333+
match e {
334+
mir::ProjectionElem::Field(ref f, _) => {
335+
result = Some(o.extract_field(bx, f.index()));
324336
}
325-
LocalRef::Place(..) => {
326-
// use path below
337+
mir::ProjectionElem::Index(_) |
338+
mir::ProjectionElem::ConstantIndex { .. } => {
339+
// ZSTs don't require any actual memory access.
340+
// FIXME(eddyb) deduplicate this with the identical
341+
// checks in `codegen_consume` and `extract_field`.
342+
let elem = o.layout.field(bx.cx, 0);
343+
if elem.is_zst() {
344+
result = Some(OperandRef::new_zst(bx.cx, elem));
345+
}
327346
}
347+
_ => {}
328348
};
329349
}
330-
} else {
331-
// Moves out of scalar and scalar pair fields are trivial.
332-
for e in place.elems.iter() {
333-
if let Some(o) = result {
334-
match e {
335-
mir::ProjectionElem::Field(ref f, _) => {
336-
result = Some(o.extract_field(bx, f.index()));
337-
}
338-
mir::ProjectionElem::Index(_) |
339-
mir::ProjectionElem::ConstantIndex { .. } => {
340-
// ZSTs don't require any actual memory access.
341-
// FIXME(eddyb) deduplicate this with the identical
342-
// checks in `codegen_consume` and `extract_field`.
343-
let elem = o.layout.field(bx.cx, 0);
344-
if elem.is_zst() {
345-
result = Some(OperandRef::new_zst(bx.cx, elem));
346-
}
347-
}
348-
_ => {}
349-
};
350-
}
351-
}
352350
}
351+
353352
result
354353
}
355354

src/librustc_codegen_llvm/mir/place.rs

Lines changed: 51 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -463,62 +463,60 @@ impl FunctionCx<'a, 'll, 'tcx> {
463463
}
464464
};
465465

466-
if !place.has_no_projection() {
467-
let tcx = cx.tcx;
468-
for (i, elem) in place.elems.iter().cloned().enumerate().rev() {
469-
let base = place.elem_base(tcx, i);
470-
result = match elem {
471-
mir::ProjectionElem::Deref => {
472-
// Load the pointer from its location.
473-
self.codegen_consume(bx, &base).deref(bx.cx)
474-
}
475-
mir::ProjectionElem::Field(ref field, _) => {
476-
result.project_field(bx, field.index())
477-
}
478-
mir::ProjectionElem::Index(index) => {
479-
let index = &mir::Operand::Copy(mir::Place::local(index));
480-
let index = self.codegen_operand(bx, index);
481-
let llindex = index.immediate();
482-
result.project_index(bx, llindex)
483-
}
484-
mir::ProjectionElem::ConstantIndex { offset,
485-
from_end: false,
486-
min_length: _ } => {
487-
let lloffset = C_usize(bx.cx, offset as u64);
488-
result.project_index(bx, lloffset)
489-
}
490-
mir::ProjectionElem::ConstantIndex { offset,
491-
from_end: true,
492-
min_length: _ } => {
493-
let lloffset = C_usize(bx.cx, offset as u64);
494-
let lllen = result.len(bx.cx);
495-
let llindex = bx.sub(lllen, lloffset);
496-
result.project_index(bx, llindex)
466+
let tcx = cx.tcx;
467+
for (i, elem) in place.elems.iter().cloned().enumerate().rev() {
468+
let base = place.elem_base(tcx, i);
469+
result = match elem {
470+
mir::ProjectionElem::Deref => {
471+
// Load the pointer from its location.
472+
self.codegen_consume(bx, &base).deref(bx.cx)
473+
}
474+
mir::ProjectionElem::Field(ref field, _) => {
475+
result.project_field(bx, field.index())
476+
}
477+
mir::ProjectionElem::Index(index) => {
478+
let index = &mir::Operand::Copy(mir::Place::local(index));
479+
let index = self.codegen_operand(bx, index);
480+
let llindex = index.immediate();
481+
result.project_index(bx, llindex)
482+
}
483+
mir::ProjectionElem::ConstantIndex { offset,
484+
from_end: false,
485+
min_length: _ } => {
486+
let lloffset = C_usize(bx.cx, offset as u64);
487+
result.project_index(bx, lloffset)
488+
}
489+
mir::ProjectionElem::ConstantIndex { offset,
490+
from_end: true,
491+
min_length: _ } => {
492+
let lloffset = C_usize(bx.cx, offset as u64);
493+
let lllen = result.len(bx.cx);
494+
let llindex = bx.sub(lllen, lloffset);
495+
result.project_index(bx, llindex)
496+
}
497+
mir::ProjectionElem::Subslice { from, to } => {
498+
let mut subslice = result.project_index(bx,
499+
C_usize(bx.cx, from as u64));
500+
let projected_ty = PlaceTy::Ty { ty: result.layout.ty }
501+
.projection_ty(tcx, &elem).to_ty(bx.tcx());
502+
subslice.layout = bx.cx.layout_of(self.monomorphize(&projected_ty));
503+
504+
if subslice.layout.is_unsized() {
505+
subslice.llextra = Some(bx.sub(result.llextra.unwrap(),
506+
C_usize(bx.cx, (from as u64) + (to as u64))));
497507
}
498-
mir::ProjectionElem::Subslice { from, to } => {
499-
let mut subslice = result.project_index(bx,
500-
C_usize(bx.cx, from as u64));
501-
let projected_ty = PlaceTy::Ty { ty: result.layout.ty }
502-
.projection_ty(tcx, &elem).to_ty(bx.tcx());
503-
subslice.layout = bx.cx.layout_of(self.monomorphize(&projected_ty));
504-
505-
if subslice.layout.is_unsized() {
506-
subslice.llextra = Some(bx.sub(result.llextra.unwrap(),
507-
C_usize(bx.cx, (from as u64) + (to as u64))));
508-
}
509508

510-
// Cast the place pointer type to the new
511-
// array or slice type (*[%_; new_len]).
512-
subslice.llval = bx.pointercast(subslice.llval,
513-
subslice.layout.llvm_type(bx.cx).ptr_to());
509+
// Cast the place pointer type to the new
510+
// array or slice type (*[%_; new_len]).
511+
subslice.llval = bx.pointercast(subslice.llval,
512+
subslice.layout.llvm_type(bx.cx).ptr_to());
514513

515-
subslice
516-
}
517-
mir::ProjectionElem::Downcast(_, v) => {
518-
result.project_downcast(bx, v)
519-
}
520-
};
521-
}
514+
subslice
515+
}
516+
mir::ProjectionElem::Downcast(_, v) => {
517+
result.project_downcast(bx, v)
518+
}
519+
};
522520
}
523521
debug!("codegen_place(place={:?}) => {:?}", place, result);
524522
result

src/librustc_mir/borrow_check/error_reporting.rs

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -634,11 +634,16 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
634634
including_downcast: &IncludingDowncast,
635635
) -> Result<(), ()> {
636636
self.append_place_base_to_string(&place.base, buf)?;
637-
if !place.has_no_projection() {
638-
for elem in place.elems.iter() {
639-
self.append_place_projection_to_string(place, elem, buf, including_downcast)?;
640-
}
637+
638+
for elem in place.elems.iter() {
639+
self.append_place_projection_to_string(
640+
place,
641+
elem,
642+
buf,
643+
including_downcast
644+
)?;
641645
}
646+
642647
Ok(())
643648
}
644649

@@ -738,24 +743,24 @@ impl<'cx, 'gcx, 'tcx> MirBorrowckCtxt<'cx, 'gcx, 'tcx> {
738743
PlaceBase::Promoted(ref prom) => self.describe_field_from_ty(&prom.1, field),
739744
PlaceBase::Static(ref static_) => self.describe_field_from_ty(&static_.ty, field),
740745
};
741-
if !place.has_no_projection() {
742-
for elem in place.elems.iter() {
743-
let proj_str = match elem {
744-
ProjectionElem::Downcast(def, variant_index) => format!(
745-
"{}",
746-
def.variants[*variant_index].fields[field.index()].ident
747-
).to_string(),
748-
ProjectionElem::Field(_, field_type) => {
749-
self.describe_field_from_ty(&field_type, field).to_string()
750-
}
751-
ProjectionElem::Index(..)
752-
| ProjectionElem::Deref
753-
| ProjectionElem::ConstantIndex { .. }
754-
| ProjectionElem::Subslice { .. } => continue,
755-
};
756-
string.push_str(proj_str.as_str());
757-
}
746+
747+
for elem in place.elems.iter() {
748+
let proj_str = match elem {
749+
ProjectionElem::Downcast(def, variant_index) => format!(
750+
"{}",
751+
def.variants[*variant_index].fields[field.index()].ident
752+
).to_string(),
753+
ProjectionElem::Field(_, field_type) => {
754+
self.describe_field_from_ty(&field_type, field).to_string()
755+
}
756+
ProjectionElem::Index(..)
757+
| ProjectionElem::Deref
758+
| ProjectionElem::ConstantIndex { .. }
759+
| ProjectionElem::Subslice { .. } => continue,
760+
};
761+
string.push_str(proj_str.as_str());
758762
}
763+
759764
string
760765
}
761766

0 commit comments

Comments
 (0)