Skip to content

Commit d0ab6e8

Browse files
committed
rustc_trans: compute LLVM types from type layouts, not Rust types.
1 parent fad9954 commit d0ab6e8

File tree

7 files changed

+184
-380
lines changed

7 files changed

+184
-380
lines changed

src/librustc/ty/layout.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1541,10 +1541,10 @@ impl<'a, 'tcx> Layout {
15411541
discr_range: (min as u64)..=(max as u64),
15421542
variants
15431543
},
1544-
fields: FieldPlacement::Arbitrary {
1545-
offsets: vec![Size::from_bytes(0)],
1546-
memory_index: vec![0]
1547-
},
1544+
// FIXME(eddyb): using `FieldPlacement::Arbitrary` here results
1545+
// in lost optimizations, specifically around allocations, see
1546+
// `test/codegen/{alloc-optimisation,vec-optimizes-away}.rs`.
1547+
fields: FieldPlacement::Union(1),
15481548
abi: if discr.size(dl) == size {
15491549
Abi::Scalar(discr)
15501550
} else {

src/librustc_trans/adt.rs

Lines changed: 0 additions & 196 deletions
This file was deleted.

src/librustc_trans/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,6 @@ pub mod back {
104104
}
105105

106106
mod abi;
107-
mod adt;
108107
mod allocator;
109108
mod asm;
110109
mod assert_module_sources;

src/librustc_trans/meth.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ pub fn get_vtable<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
7777
}
7878

7979
// Not in the cache. Build it.
80-
let nullptr = C_null(Type::nil(ccx).ptr_to());
80+
let nullptr = C_null(Type::i8p(ccx));
8181

8282
let (size, align) = ccx.size_and_align_of(ty);
8383
let mut components: Vec<_> = [

src/librustc_trans/mir/lvalue.rs

Lines changed: 19 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,16 @@
1010

1111
use llvm::{self, ValueRef};
1212
use rustc::ty::{self, Ty, TypeFoldable};
13-
use rustc::ty::layout::{self, Align, FullLayout, Layout, LayoutOf};
13+
use rustc::ty::layout::{self, Align, FullLayout, LayoutOf};
1414
use rustc::mir;
1515
use rustc::mir::tcx::LvalueTy;
1616
use rustc_data_structures::indexed_vec::Idx;
1717
use abi;
18-
use adt;
1918
use base;
2019
use builder::Builder;
2120
use common::{self, CrateContext, C_usize, C_u8, C_u32, C_uint, C_int, C_null, val_ty};
2221
use consts;
23-
use type_of::LayoutLlvmExt;
22+
use type_of::{self, LayoutLlvmExt};
2423
use type_::Type;
2524
use value::Value;
2625
use glue;
@@ -206,52 +205,26 @@ impl<'a, 'tcx> LvalueRef<'tcx> {
206205
let alignment = self.alignment | Alignment::from(l);
207206

208207
// Unions and newtypes only use an offset of 0.
209-
match *l.layout {
210-
// FIXME(eddyb) The fields of a fat pointer aren't correct, especially
211-
// to unsized structs, we can't represent their pointee types in `Ty`.
212-
Layout::FatPointer { .. } => {}
213-
214-
_ if offset == 0 => {
215-
let ty = ccx.llvm_type_of(field.ty);
216-
return LvalueRef {
217-
llval: bcx.pointercast(self.llval, ty.ptr_to()),
218-
llextra: if field.is_unsized() {
219-
self.llextra
220-
} else {
221-
ptr::null_mut()
222-
},
223-
ty: LvalueTy::from_ty(field.ty),
224-
alignment,
225-
};
226-
}
227-
228-
_ => {}
229-
}
230-
231-
// Discriminant field of enums.
232-
if let layout::NullablePointer { .. } = *l.layout {
233-
let ty = ccx.llvm_type_of(field.ty);
234-
let size = field.size(ccx).bytes();
235-
236-
// If the discriminant is not on a multiple of the primitive's size,
237-
// we need to go through i8*. Also assume the worst alignment.
238-
if offset % size != 0 {
239-
let byte_ptr = bcx.pointercast(self.llval, Type::i8p(ccx));
240-
let byte_ptr = bcx.inbounds_gep(byte_ptr, &[C_usize(ccx, offset)]);
241-
let byte_align = Alignment::Packed(Align::from_bytes(1, 1).unwrap());
242-
return LvalueRef::new_sized(
243-
bcx.pointercast(byte_ptr, ty.ptr_to()), field.ty, byte_align);
208+
let has_llvm_fields = match *l.fields {
209+
layout::FieldPlacement::Union(_) => false,
210+
layout::FieldPlacement::Array { .. } => true,
211+
layout::FieldPlacement::Arbitrary { .. } => {
212+
match l.abi {
213+
layout::Abi::Scalar(_) | layout::Abi::Vector { .. } => false,
214+
layout::Abi::Aggregate { .. } => true
215+
}
244216
}
245-
246-
let discr_ptr = bcx.pointercast(self.llval, ty.ptr_to());
247-
return LvalueRef::new_sized(
248-
bcx.inbounds_gep(discr_ptr, &[C_usize(ccx, offset / size)]),
249-
field.ty, alignment);
250-
}
217+
};
251218

252219
let simple = || {
253220
LvalueRef {
254-
llval: bcx.struct_gep(self.llval, l.llvm_field_index(ix)),
221+
llval: if has_llvm_fields {
222+
bcx.struct_gep(self.llval, l.llvm_field_index(ix))
223+
} else {
224+
assert_eq!(offset, 0);
225+
let ty = ccx.llvm_type_of(field.ty);
226+
bcx.pointercast(self.llval, ty.ptr_to())
227+
},
255228
llextra: if ccx.shared().type_has_metadata(field.ty) {
256229
self.llextra
257230
} else {
@@ -460,7 +433,7 @@ impl<'a, 'tcx> LvalueRef<'tcx> {
460433
layout::General { .. } => {
461434
let variant_layout = layout.for_variant(variant_index);
462435
let variant_ty = Type::struct_(bcx.ccx,
463-
&adt::struct_llfields(bcx.ccx, variant_layout),
436+
&type_of::struct_llfields(bcx.ccx, variant_layout),
464437
variant_layout.is_packed());
465438
downcast.llval = bcx.pointercast(downcast.llval, variant_ty.ptr_to());
466439
}

src/librustc_trans/type_.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,6 @@ impl Type {
6666
ty!(llvm::LLVMVoidTypeInContext(ccx.llcx()))
6767
}
6868

69-
pub fn nil(ccx: &CrateContext) -> Type {
70-
Type::empty_struct(ccx)
71-
}
72-
7369
pub fn metadata(ccx: &CrateContext) -> Type {
7470
ty!(llvm::LLVMRustMetadataTypeInContext(ccx.llcx()))
7571
}
@@ -202,9 +198,6 @@ impl Type {
202198
ty!(llvm::LLVMStructCreateNamed(ccx.llcx(), name.as_ptr()))
203199
}
204200

205-
pub fn empty_struct(ccx: &CrateContext) -> Type {
206-
Type::struct_(ccx, &[], false)
207-
}
208201

209202
pub fn array(ty: &Type, len: u64) -> Type {
210203
ty!(llvm::LLVMRustArrayType(ty.to_ref(), len))

0 commit comments

Comments
 (0)