Skip to content

Commit 4038189

Browse files
committed
Optimize struct_field_ptr
1 parent f16068e commit 4038189

File tree

3 files changed

+11
-77
lines changed

3 files changed

+11
-77
lines changed

src/librustc/ty/layout.rs

+9
Original file line numberDiff line numberDiff line change
@@ -650,6 +650,15 @@ impl<'a, 'gcx, 'tcx> Struct {
650650
}
651651
Ok(None)
652652
}
653+
654+
pub fn offset_of_field(&self, index: usize) -> Size {
655+
assert!(index < self.offset_after_field.len());
656+
if index == 0 {
657+
Size::from_bytes(0)
658+
} else {
659+
self.offset_after_field[index-1]
660+
}
661+
}
653662
}
654663

655664
/// An untagged union.

src/librustc_trans/adt.rs

+2-70
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
//! used unboxed and any field can have pointers (including mutable)
4242
//! taken to it, implementing them for Rust seems difficult.
4343
44-
pub use self::Repr::*;
4544
use super::Disr;
4645

4746
use std;
@@ -50,7 +49,6 @@ use llvm::{ValueRef, True, IntEQ, IntNE};
5049
use rustc::ty::layout;
5150
use rustc::ty::{self, Ty, AdtKind};
5251
use syntax::attr;
53-
use syntax::attr::IntType;
5452
use build::*;
5553
use common::*;
5654
use debuginfo::DebugLoc;
@@ -70,66 +68,6 @@ pub enum BranchKind {
7068

7169
type Hint = attr::ReprAttr;
7270

73-
/// Representations.
74-
#[derive(Eq, PartialEq, Debug)]
75-
pub enum Repr<'tcx> {
76-
/// C-like enums; basically an int.
77-
CEnum(IntType, Disr, Disr), // discriminant range (signedness based on the IntType)
78-
/// Single-case variants, and structs/tuples/records.
79-
Univariant(Struct<'tcx>),
80-
/// Untagged unions.
81-
UntaggedUnion(Union<'tcx>),
82-
/// General-case enums: for each case there is a struct, and they
83-
/// all start with a field for the discriminant.
84-
General(IntType, Vec<Struct<'tcx>>),
85-
/// Two cases distinguished by a nullable pointer: the case with discriminant
86-
/// `nndiscr` must have single field which is known to be nonnull due to its type.
87-
/// The other case is known to be zero sized. Hence we represent the enum
88-
/// as simply a nullable pointer: if not null it indicates the `nndiscr` variant,
89-
/// otherwise it indicates the other case.
90-
RawNullablePointer {
91-
nndiscr: Disr,
92-
nnty: Ty<'tcx>,
93-
nullfields: Vec<Ty<'tcx>>
94-
},
95-
/// Two cases distinguished by a nullable pointer: the case with discriminant
96-
/// `nndiscr` is represented by the struct `nonnull`, where the `discrfield`th
97-
/// field is known to be nonnull due to its type; if that field is null, then
98-
/// it represents the other case, which is inhabited by at most one value
99-
/// (and all other fields are undefined/unused).
100-
///
101-
/// For example, `std::option::Option` instantiated at a safe pointer type
102-
/// is represented such that `None` is a null pointer and `Some` is the
103-
/// identity function.
104-
StructWrappedNullablePointer {
105-
nonnull: Struct<'tcx>,
106-
nndiscr: Disr,
107-
discrfield: DiscrField,
108-
nullfields: Vec<Ty<'tcx>>,
109-
}
110-
}
111-
112-
/// For structs, and struct-like parts of anything fancier.
113-
#[derive(Eq, PartialEq, Debug)]
114-
pub struct Struct<'tcx> {
115-
// If the struct is DST, then the size and alignment do not take into
116-
// account the unsized fields of the struct.
117-
pub size: u64,
118-
pub align: u32,
119-
pub sized: bool,
120-
pub packed: bool,
121-
pub fields: Vec<Ty<'tcx>>,
122-
}
123-
124-
/// For untagged unions.
125-
#[derive(Eq, PartialEq, Debug)]
126-
pub struct Union<'tcx> {
127-
pub min_size: u64,
128-
pub align: u32,
129-
pub packed: bool,
130-
pub fields: Vec<Ty<'tcx>>,
131-
}
132-
13371
#[derive(Copy, Clone)]
13472
pub struct MaybeSizedValue {
13573
pub value: ValueRef,
@@ -696,14 +634,8 @@ fn struct_field_ptr<'blk, 'tcx>(bcx: &BlockAndBuilder<'blk, 'tcx>,
696634

697635
let meta = val.meta;
698636

699-
// Calculate the unaligned offset of the unsized field.
700-
let mut offset = 0;
701-
for &ty in &fields[0..ix] {
702-
let llty = type_of::sizing_type_of(ccx, ty);
703-
let type_align = type_of::align_of(ccx, ty);
704-
offset = roundup(offset, type_align);
705-
offset += machine::llsize_of_alloc(ccx, llty);
706-
}
637+
638+
let offset = st.offset_of_field(ix).bytes();
707639
let unaligned_offset = C_uint(bcx.ccx(), offset);
708640

709641
// Get the alignment of the field

src/librustc_trans/context.rs

-7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use rustc::hir::def_id::DefId;
1717
use rustc::traits;
1818
use rustc::mir::mir_map::MirMap;
1919
use rustc::mir::repr as mir;
20-
use adt;
2120
use base;
2221
use builder::Builder;
2322
use common::BuilderRef_res;
@@ -142,7 +141,6 @@ pub struct LocalCrateContext<'tcx> {
142141

143142
lltypes: RefCell<FnvHashMap<Ty<'tcx>, Type>>,
144143
llsizingtypes: RefCell<FnvHashMap<Ty<'tcx>, Type>>,
145-
adt_reprs: RefCell<FnvHashMap<Ty<'tcx>, Rc<adt::Repr<'tcx>>>>,
146144
type_hashcodes: RefCell<FnvHashMap<Ty<'tcx>, String>>,
147145
int_type: Type,
148146
opaque_vec_type: Type,
@@ -677,7 +675,6 @@ impl<'tcx> LocalCrateContext<'tcx> {
677675
statics_to_rauw: RefCell::new(Vec::new()),
678676
lltypes: RefCell::new(FnvHashMap()),
679677
llsizingtypes: RefCell::new(FnvHashMap()),
680-
adt_reprs: RefCell::new(FnvHashMap()),
681678
type_hashcodes: RefCell::new(FnvHashMap()),
682679
int_type: Type::from_ref(ptr::null_mut()),
683680
opaque_vec_type: Type::from_ref(ptr::null_mut()),
@@ -918,10 +915,6 @@ impl<'b, 'tcx> CrateContext<'b, 'tcx> {
918915
&self.local().llsizingtypes
919916
}
920917

921-
pub fn adt_reprs<'a>(&'a self) -> &'a RefCell<FnvHashMap<Ty<'tcx>, Rc<adt::Repr<'tcx>>>> {
922-
&self.local().adt_reprs
923-
}
924-
925918
pub fn symbol_hasher<'a>(&'a self) -> &'a RefCell<Sha256> {
926919
&self.shared.symbol_hasher
927920
}

0 commit comments

Comments
 (0)