Skip to content

Commit 03ce886

Browse files
committed
rustc_trans: avoid sizing_type_of everywhere possible.
1 parent 9768ef5 commit 03ce886

File tree

14 files changed

+53
-82
lines changed

14 files changed

+53
-82
lines changed

src/librustc_trans/abi.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -439,14 +439,10 @@ impl FnType {
439439
match ret_ty.sty {
440440
// These are not really pointers but pairs, (pointer, len)
441441
ty::TyRef(_, ty::TypeAndMut { ty, .. }) => {
442-
let llty = type_of::sizing_type_of(ccx, ty);
443-
let llsz = llsize_of_alloc(ccx, llty);
444-
ret.attrs.set_dereferenceable(llsz);
442+
ret.attrs.set_dereferenceable(ccx.size_of(ty));
445443
}
446444
ty::TyAdt(def, _) if def.is_box() => {
447-
let llty = type_of::sizing_type_of(ccx, ret_ty.boxed_ty());
448-
let llsz = llsize_of_alloc(ccx, llty);
449-
ret.attrs.set_dereferenceable(llsz);
445+
ret.attrs.set_dereferenceable(ccx.size_of(ret_ty.boxed_ty()));
450446
}
451447
_ => {}
452448
}
@@ -517,9 +513,7 @@ impl FnType {
517513
args.push(info);
518514
} else {
519515
if let Some(inner) = rust_ptr_attrs(ty, &mut arg) {
520-
let llty = type_of::sizing_type_of(ccx, inner);
521-
let llsz = llsize_of_alloc(ccx, llty);
522-
arg.attrs.set_dereferenceable(llsz);
516+
arg.attrs.set_dereferenceable(ccx.size_of(inner));
523517
}
524518
args.push(arg);
525519
}

src/librustc_trans/adt.rs

+6-14
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ use std;
4747

4848
use llvm::{ValueRef, True, IntEQ, IntNE};
4949
use rustc::ty::layout;
50-
use rustc::ty::{self, Ty, AdtKind};
50+
use rustc::ty::{self, Ty};
5151
use common::*;
5252
use builder::Builder;
5353
use base;
@@ -285,31 +285,24 @@ pub fn trans_get_discr<'a, 'tcx>(
285285
cast_to: Option<Type>,
286286
range_assert: bool
287287
) -> ValueRef {
288-
let (def, substs) = match t.sty {
289-
ty::TyAdt(ref def, substs) if def.adt_kind() == AdtKind::Enum => (def, substs),
290-
_ => bug!("{} is not an enum", t)
291-
};
292-
293288
debug!("trans_get_discr t: {:?}", t);
294289
let l = bcx.ccx.layout_of(t);
295290

296291
let val = match *l {
297292
layout::CEnum { discr, min, max, .. } => {
298293
load_discr(bcx, discr, scrutinee, alignment, min, max, range_assert)
299294
}
300-
layout::General { discr, .. } => {
295+
layout::General { discr, ref variants, .. } => {
301296
let ptr = bcx.struct_gep(scrutinee, 0);
302297
load_discr(bcx, discr, ptr, alignment,
303-
0, def.variants.len() as u64 - 1,
298+
0, variants.len() as u64 - 1,
304299
range_assert)
305300
}
306301
layout::Univariant { .. } | layout::UntaggedUnion { .. } => C_u8(bcx.ccx, 0),
307302
layout::RawNullablePointer { nndiscr, .. } => {
308303
let cmp = if nndiscr == 0 { IntEQ } else { IntNE };
309-
let llptrty = type_of::sizing_type_of(bcx.ccx,
310-
monomorphize::field_ty(bcx.tcx(), substs,
311-
&def.variants[nndiscr as usize].fields[0]));
312-
bcx.icmp(cmp, bcx.load(scrutinee, alignment.to_align()), C_null(llptrty))
304+
let discr = bcx.load(scrutinee, alignment.to_align());
305+
bcx.icmp(cmp, discr, C_null(val_ty(discr)))
313306
}
314307
layout::StructWrappedNullablePointer { nndiscr, ref discrfield, .. } => {
315308
struct_wrapped_nullable_bitdiscr(bcx, nndiscr, discrfield, scrutinee, alignment)
@@ -383,9 +376,8 @@ pub fn trans_set_discr<'a, 'tcx>(bcx: &Builder<'a, 'tcx>, t: Ty<'tcx>, val: Valu
383376
assert_eq!(to, Disr(0));
384377
}
385378
layout::RawNullablePointer { nndiscr, .. } => {
386-
let nnty = compute_fields(bcx.ccx, t, nndiscr as usize, false)[0];
387379
if to.0 != nndiscr {
388-
let llptrty = type_of::sizing_type_of(bcx.ccx, nnty);
380+
let llptrty = val_ty(val).element_type();
389381
bcx.store(C_null(llptrty), val, None);
390382
}
391383
}

src/librustc_trans/base.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ use context::{SharedCrateContext, CrateContextList};
5959
use debuginfo;
6060
use declare;
6161
use machine;
62-
use machine::llsize_of;
6362
use meth;
6463
use mir;
6564
use monomorphize::{self, Instance};
@@ -534,14 +533,13 @@ pub fn memcpy_ty<'a, 'tcx>(
534533
) {
535534
let ccx = bcx.ccx;
536535

537-
if type_is_zero_size(ccx, t) {
536+
let size = ccx.size_of(t);
537+
if size == 0 {
538538
return;
539539
}
540540

541-
let llty = type_of::type_of(ccx, t);
542-
let llsz = llsize_of(ccx, llty);
543-
let llalign = align.unwrap_or_else(|| type_of::align_of(ccx, t));
544-
call_memcpy(bcx, dst, src, llsz, llalign as u32);
541+
let align = align.unwrap_or_else(|| ccx.align_of(t));
542+
call_memcpy(bcx, dst, src, C_uint(ccx, size), align);
545543
}
546544

547545
pub fn call_memset<'a, 'tcx>(b: &Builder<'a, 'tcx>,

src/librustc_trans/common.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,8 @@ pub fn type_is_imm_pair<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, ty: Ty<'tcx>)
125125

126126
/// Identify types which have size zero at runtime.
127127
pub fn type_is_zero_size<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, ty: Ty<'tcx>) -> bool {
128-
use machine::llsize_of_alloc;
129-
use type_of::sizing_type_of;
130-
let llty = sizing_type_of(ccx, ty);
131-
llsize_of_alloc(ccx, llty) == 0
128+
let layout = ccx.layout_of(ty);
129+
!layout.is_unsized() && layout.size(&ccx.tcx().data_layout).bytes() == 0
132130
}
133131

134132
/*

src/librustc_trans/consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ pub fn trans_static<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
255255
ccx.statics_to_rauw().borrow_mut().push((g, new_g));
256256
new_g
257257
};
258-
llvm::LLVMSetAlignment(g, type_of::align_of(ccx, ty));
258+
llvm::LLVMSetAlignment(g, ccx.align_of(ty));
259259
llvm::LLVMSetInitializer(g, v);
260260

261261
// As an optimization, all shared statics which do not have interior

src/librustc_trans/debuginfo/metadata.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1772,7 +1772,7 @@ pub fn create_global_var_metadata(cx: &CrateContext,
17721772
let var_name = CString::new(var_name).unwrap();
17731773
let linkage_name = CString::new(linkage_name).unwrap();
17741774

1775-
let global_align = type_of::align_of(cx, variable_type);
1775+
let global_align = cx.align_of(variable_type);
17761776

17771777
unsafe {
17781778
llvm::LLVMRustDIBuilderCreateStaticVariable(DIB(cx),

src/librustc_trans/debuginfo/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ pub fn declare_local<'a, 'tcx>(bcx: &Builder<'a, 'tcx>,
449449
LocalVariable |
450450
CapturedVariable => (0, DW_TAG_auto_variable)
451451
};
452-
let align = ::type_of::align_of(cx, variable_type);
452+
let align = cx.align_of(variable_type);
453453

454454
let name = CString::new(variable_name.as_str().as_bytes()).unwrap();
455455
match (variable_access, &[][..]) {

src/librustc_trans/glue.rs

+7-14
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@ use llvm::{ValueRef};
1919
use rustc::traits;
2020
use rustc::ty::{self, Ty, TypeFoldable};
2121
use common::*;
22-
use machine::*;
2322
use meth;
2423
use monomorphize;
25-
use type_of::{sizing_type_of, align_of};
2624
use value::Value;
2725
use builder::Builder;
2826

@@ -69,9 +67,8 @@ pub fn size_and_align_of_dst<'a, 'tcx>(bcx: &Builder<'a, 'tcx>, t: Ty<'tcx>, inf
6967
debug!("calculate size of DST: {}; with lost info: {:?}",
7068
t, Value(info));
7169
if bcx.ccx.shared().type_is_sized(t) {
72-
let sizing_type = sizing_type_of(bcx.ccx, t);
73-
let size = llsize_of_alloc(bcx.ccx, sizing_type);
74-
let align = align_of(bcx.ccx, t);
70+
let size = bcx.ccx.size_of(t);
71+
let align = bcx.ccx.align_of(t);
7572
debug!("size_and_align_of_dst t={} info={:?} size: {} align: {}",
7673
t, Value(info), size, align);
7774
let size = C_uint(bcx.ccx, size);
@@ -82,9 +79,8 @@ pub fn size_and_align_of_dst<'a, 'tcx>(bcx: &Builder<'a, 'tcx>, t: Ty<'tcx>, inf
8279
ty::TyAdt(def, substs) => {
8380
let ccx = bcx.ccx;
8481
// First get the size of all statically known fields.
85-
// Don't use type_of::sizing_type_of because that expects t to be sized,
86-
// and it also rounds up to alignment, which we want to avoid,
87-
// as the unsized field's alignment could be smaller.
82+
// Don't use size_of because it also rounds up to alignment, which we
83+
// want to avoid, as the unsized field's alignment could be smaller.
8884
assert!(!t.is_simd());
8985
let layout = ccx.layout_of(t);
9086
debug!("DST {} layout: {:?}", t, layout);
@@ -154,14 +150,11 @@ pub fn size_and_align_of_dst<'a, 'tcx>(bcx: &Builder<'a, 'tcx>, t: Ty<'tcx>, inf
154150
(meth::SIZE.get_usize(bcx, info), meth::ALIGN.get_usize(bcx, info))
155151
}
156152
ty::TySlice(_) | ty::TyStr => {
157-
let unit_ty = t.sequence_element_type(bcx.tcx());
153+
let unit = t.sequence_element_type(bcx.tcx());
158154
// The info in this case is the length of the str, so the size is that
159155
// times the unit size.
160-
let llunit_ty = sizing_type_of(bcx.ccx, unit_ty);
161-
let unit_align = llalign_of_min(bcx.ccx, llunit_ty);
162-
let unit_size = llsize_of_alloc(bcx.ccx, llunit_ty);
163-
(bcx.mul(info, C_uint(bcx.ccx, unit_size)),
164-
C_uint(bcx.ccx, unit_align))
156+
(bcx.mul(info, C_uint(bcx.ccx, bcx.ccx.size_of(unit))),
157+
C_uint(bcx.ccx, bcx.ccx.align_of(unit)))
165158
}
166159
_ => bug!("Unexpected unsized type, found {}", t)
167160
}

src/librustc_trans/intrinsic.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ pub fn trans_intrinsic_call<'a, 'tcx>(bcx: &Builder<'a, 'tcx>,
151151
}
152152
"min_align_of" => {
153153
let tp_ty = substs.type_at(0);
154-
C_uint(ccx, type_of::align_of(ccx, tp_ty))
154+
C_uint(ccx, ccx.align_of(tp_ty))
155155
}
156156
"min_align_of_val" => {
157157
let tp_ty = substs.type_at(0);
@@ -160,7 +160,7 @@ pub fn trans_intrinsic_call<'a, 'tcx>(bcx: &Builder<'a, 'tcx>,
160160
glue::size_and_align_of_dst(bcx, tp_ty, llargs[1]);
161161
llalign
162162
} else {
163-
C_uint(ccx, type_of::align_of(ccx, tp_ty))
163+
C_uint(ccx, ccx.align_of(tp_ty))
164164
}
165165
}
166166
"pref_align_of" => {
@@ -234,7 +234,7 @@ pub fn trans_intrinsic_call<'a, 'tcx>(bcx: &Builder<'a, 'tcx>,
234234
}
235235
let load = bcx.volatile_load(ptr);
236236
unsafe {
237-
llvm::LLVMSetAlignment(load, type_of::align_of(ccx, tp_ty));
237+
llvm::LLVMSetAlignment(load, ccx.align_of(tp_ty));
238238
}
239239
to_immediate(bcx, load, tp_ty)
240240
},
@@ -252,7 +252,7 @@ pub fn trans_intrinsic_call<'a, 'tcx>(bcx: &Builder<'a, 'tcx>,
252252
let ptr = bcx.pointercast(llargs[0], val_ty(val).ptr_to());
253253
let store = bcx.volatile_store(val, ptr);
254254
unsafe {
255-
llvm::LLVMSetAlignment(store, type_of::align_of(ccx, tp_ty));
255+
llvm::LLVMSetAlignment(store, ccx.align_of(tp_ty));
256256
}
257257
}
258258
C_nil(ccx)
@@ -634,7 +634,7 @@ pub fn trans_intrinsic_call<'a, 'tcx>(bcx: &Builder<'a, 'tcx>,
634634
if val_ty(llval) != Type::void(ccx) && machine::llsize_of_alloc(ccx, val_ty(llval)) != 0 {
635635
if let Some(ty) = fn_ty.ret.cast {
636636
let ptr = bcx.pointercast(llresult, ty.ptr_to());
637-
bcx.store(llval, ptr, Some(type_of::align_of(ccx, ret_ty)));
637+
bcx.store(llval, ptr, Some(ccx.align_of(ret_ty)));
638638
} else {
639639
store_ty(bcx, llval, llresult, Alignment::AbiAligned, ret_ty);
640640
}
@@ -651,7 +651,7 @@ fn copy_intrinsic<'a, 'tcx>(bcx: &Builder<'a, 'tcx>,
651651
-> ValueRef {
652652
let ccx = bcx.ccx;
653653
let lltp_ty = type_of::type_of(ccx, tp_ty);
654-
let align = C_i32(ccx, type_of::align_of(ccx, tp_ty) as i32);
654+
let align = C_i32(ccx, ccx.align_of(tp_ty) as i32);
655655
let size = machine::llsize_of(ccx, lltp_ty);
656656
let int_size = machine::llbitsize_of_real(ccx, ccx.int_type());
657657

@@ -685,7 +685,7 @@ fn memset_intrinsic<'a, 'tcx>(
685685
count: ValueRef
686686
) -> ValueRef {
687687
let ccx = bcx.ccx;
688-
let align = C_i32(ccx, type_of::align_of(ccx, ty) as i32);
688+
let align = C_i32(ccx, ccx.align_of(ty) as i32);
689689
let lltp_ty = type_of::type_of(ccx, ty);
690690
let size = machine::llsize_of(ccx, lltp_ty);
691691
let dst = bcx.pointercast(dst, Type::i8p(ccx));

src/librustc_trans/meth.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use consts;
1717
use machine;
1818
use monomorphize;
1919
use type_::Type;
20-
use type_of::*;
2120
use value::Value;
2221
use rustc::ty;
2322

@@ -80,14 +79,10 @@ pub fn get_vtable<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
8079
// Not in the cache. Build it.
8180
let nullptr = C_null(Type::nil(ccx).ptr_to());
8281

83-
let size_ty = sizing_type_of(ccx, ty);
84-
let size = machine::llsize_of_alloc(ccx, size_ty);
85-
let align = align_of(ccx, ty);
86-
8782
let mut components: Vec<_> = [
8883
callee::get_fn(ccx, monomorphize::resolve_drop_in_place(ccx.shared(), ty)),
89-
C_uint(ccx, size),
90-
C_uint(ccx, align)
84+
C_uint(ccx, ccx.size_of(ty)),
85+
C_uint(ccx, ccx.align_of(ty))
9186
].iter().cloned().collect();
9287

9388
if let Some(trait_ref) = trait_ref {

src/librustc_trans/mir/block.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ use consts;
2424
use machine::llalign_of_min;
2525
use meth;
2626
use monomorphize;
27+
use type_of;
2728
use tvec;
28-
use type_of::{self, align_of};
2929
use type_::Type;
3030

3131
use rustc_data_structures::indexed_vec::IndexVec;
@@ -910,7 +910,7 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> {
910910
let cast_ptr = bcx.pointercast(dst.llval, llty.ptr_to());
911911
let in_type = val.ty;
912912
let out_type = dst.ty.to_ty(bcx.tcx());;
913-
let llalign = cmp::min(align_of(bcx.ccx, in_type), align_of(bcx.ccx, out_type));
913+
let llalign = cmp::min(bcx.ccx.align_of(in_type), bcx.ccx.align_of(out_type));
914914
self.store_operand(bcx, cast_ptr, Some(llalign), val);
915915
}
916916

src/librustc_trans/mir/constant.rs

+6-11
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ impl<'tcx> Const<'tcx> {
145145
} else {
146146
// Otherwise, or if the value is not immediate, we create
147147
// a constant LLVM global and cast its address if necessary.
148-
let align = type_of::align_of(ccx, self.ty);
148+
let align = ccx.align_of(self.ty);
149149
let ptr = consts::addr_of(ccx, self.llval, align, "const");
150150
OperandValue::Ref(consts::ptrcast(ptr, llty.ptr_to()), Alignment::AbiAligned)
151151
};
@@ -721,7 +721,7 @@ impl<'a, 'tcx> MirConstContext<'a, 'tcx> {
721721
Base::Value(llval) => {
722722
// FIXME: may be wrong for &*(&simd_vec as &fmt::Debug)
723723
let align = if self.ccx.shared().type_is_sized(ty) {
724-
type_of::align_of(self.ccx, ty)
724+
self.ccx.align_of(ty)
725725
} else {
726726
self.ccx.tcx().data_layout.pointer_align.abi() as machine::llalign
727727
};
@@ -1033,25 +1033,20 @@ fn trans_const<'a, 'tcx>(
10331033
C_vector(vals)
10341034
}
10351035
layout::RawNullablePointer { nndiscr, .. } => {
1036-
let nnty = adt::compute_fields(ccx, t, nndiscr as usize, false)[0];
10371036
if variant_index as u64 == nndiscr {
10381037
assert_eq!(vals.len(), 1);
10391038
vals[0]
10401039
} else {
1041-
C_null(type_of::sizing_type_of(ccx, nnty))
1040+
C_null(type_of::type_of(ccx, t))
10421041
}
10431042
}
10441043
layout::StructWrappedNullablePointer { ref nonnull, nndiscr, .. } => {
10451044
if variant_index as u64 == nndiscr {
10461045
C_struct(ccx, &build_const_struct(ccx, &nonnull, vals), false)
10471046
} else {
1048-
let fields = adt::compute_fields(ccx, t, nndiscr as usize, false);
1049-
let vals = fields.iter().map(|&ty| {
1050-
// Always use null even if it's not the `discrfield`th
1051-
// field; see #8506.
1052-
C_null(type_of::sizing_type_of(ccx, ty))
1053-
}).collect::<Vec<ValueRef>>();
1054-
C_struct(ccx, &build_const_struct(ccx, &nonnull, &vals[..]), false)
1047+
// Always use null even if it's not the `discrfield`th
1048+
// field; see #8506.
1049+
C_null(type_of::type_of(ccx, t))
10551050
}
10561051
}
10571052
_ => bug!("trans_const: cannot handle type {} repreented as {:#?}", t, l)

src/librustc_trans/mir/rvalue.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ impl<'a, 'tcx> MirContext<'a, 'tcx> {
438438
let content_ty: Ty<'tcx> = self.monomorphize(&content_ty);
439439
let llty = type_of::type_of(bcx.ccx, content_ty);
440440
let llsize = machine::llsize_of(bcx.ccx, llty);
441-
let align = type_of::align_of(bcx.ccx, content_ty);
441+
let align = bcx.ccx.align_of(content_ty);
442442
let llalign = C_uint(bcx.ccx, align);
443443
let llty_ptr = llty.ptr_to();
444444
let box_ty = bcx.tcx().mk_box(content_ty);

src/librustc_trans/type_of.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -322,10 +322,16 @@ pub fn in_memory_type_of<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>) ->
322322
llty
323323
}
324324

325-
pub fn align_of<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>)
326-
-> machine::llalign {
327-
let layout = cx.layout_of(t);
328-
layout.align(&cx.tcx().data_layout).abi() as machine::llalign
325+
impl<'a, 'tcx> CrateContext<'a, 'tcx> {
326+
pub fn align_of(&self, ty: Ty<'tcx>) -> machine::llalign {
327+
let layout = self.layout_of(ty);
328+
layout.align(&self.tcx().data_layout).abi() as machine::llalign
329+
}
330+
331+
pub fn size_of(&self, ty: Ty<'tcx>) -> machine::llsize {
332+
let layout = self.layout_of(ty);
333+
layout.size(&self.tcx().data_layout).bytes() as machine::llsize
334+
}
329335
}
330336

331337
fn llvm_type_name<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, ty: Ty<'tcx>) -> String {

0 commit comments

Comments
 (0)