From c977daf97cd7aa8b964b9b89e7ebd2da26022b2a Mon Sep 17 00:00:00 2001 From: Eduard-Mihai Burtescu Date: Thu, 2 Mar 2017 05:35:25 +0200 Subject: [PATCH] rustc_trans: avoid sizing_type_of everywhere possible. --- src/librustc_trans/abi.rs | 12 +++--------- src/librustc_trans/adt.rs | 20 ++++++-------------- src/librustc_trans/base.rs | 10 ++++------ src/librustc_trans/common.rs | 6 ++---- src/librustc_trans/consts.rs | 2 +- src/librustc_trans/debuginfo/metadata.rs | 2 +- src/librustc_trans/debuginfo/mod.rs | 2 +- src/librustc_trans/glue.rs | 21 +++++++-------------- src/librustc_trans/intrinsic.rs | 14 +++++++------- src/librustc_trans/meth.rs | 9 ++------- src/librustc_trans/mir/block.rs | 4 ++-- src/librustc_trans/mir/constant.rs | 17 ++++++----------- src/librustc_trans/mir/rvalue.rs | 2 +- src/librustc_trans/type_of.rs | 14 ++++++++++---- 14 files changed, 53 insertions(+), 82 deletions(-) diff --git a/src/librustc_trans/abi.rs b/src/librustc_trans/abi.rs index 453f65eb762..550f337d9b1 100644 --- a/src/librustc_trans/abi.rs +++ b/src/librustc_trans/abi.rs @@ -439,14 +439,10 @@ fn unadjusted<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, match ret_ty.sty { // These are not really pointers but pairs, (pointer, len) ty::TyRef(_, ty::TypeAndMut { ty, .. }) => { - let llty = type_of::sizing_type_of(ccx, ty); - let llsz = llsize_of_alloc(ccx, llty); - ret.attrs.set_dereferenceable(llsz); + ret.attrs.set_dereferenceable(ccx.size_of(ty)); } ty::TyAdt(def, _) if def.is_box() => { - let llty = type_of::sizing_type_of(ccx, ret_ty.boxed_ty()); - let llsz = llsize_of_alloc(ccx, llty); - ret.attrs.set_dereferenceable(llsz); + ret.attrs.set_dereferenceable(ccx.size_of(ret_ty.boxed_ty())); } _ => {} } @@ -517,9 +513,7 @@ fn unadjusted<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, args.push(info); } else { if let Some(inner) = rust_ptr_attrs(ty, &mut arg) { - let llty = type_of::sizing_type_of(ccx, inner); - let llsz = llsize_of_alloc(ccx, llty); - arg.attrs.set_dereferenceable(llsz); + arg.attrs.set_dereferenceable(ccx.size_of(inner)); } args.push(arg); } diff --git a/src/librustc_trans/adt.rs b/src/librustc_trans/adt.rs index 5c1ced57340..fb7ddd2bf42 100644 --- a/src/librustc_trans/adt.rs +++ b/src/librustc_trans/adt.rs @@ -47,7 +47,7 @@ use llvm::{ValueRef, True, IntEQ, IntNE}; use rustc::ty::layout; -use rustc::ty::{self, Ty, AdtKind}; +use rustc::ty::{self, Ty}; use common::*; use builder::Builder; use base; @@ -285,11 +285,6 @@ pub fn trans_get_discr<'a, 'tcx>( cast_to: Option, range_assert: bool ) -> ValueRef { - let (def, substs) = match t.sty { - ty::TyAdt(ref def, substs) if def.adt_kind() == AdtKind::Enum => (def, substs), - _ => bug!("{} is not an enum", t) - }; - debug!("trans_get_discr t: {:?}", t); let l = bcx.ccx.layout_of(t); @@ -297,19 +292,17 @@ pub fn trans_get_discr<'a, 'tcx>( layout::CEnum { discr, min, max, .. } => { load_discr(bcx, discr, scrutinee, alignment, min, max, range_assert) } - layout::General { discr, .. } => { + layout::General { discr, ref variants, .. } => { let ptr = bcx.struct_gep(scrutinee, 0); load_discr(bcx, discr, ptr, alignment, - 0, def.variants.len() as u64 - 1, + 0, variants.len() as u64 - 1, range_assert) } layout::Univariant { .. } | layout::UntaggedUnion { .. } => C_u8(bcx.ccx, 0), layout::RawNullablePointer { nndiscr, .. } => { let cmp = if nndiscr == 0 { IntEQ } else { IntNE }; - let llptrty = type_of::sizing_type_of(bcx.ccx, - monomorphize::field_ty(bcx.tcx(), substs, - &def.variants[nndiscr as usize].fields[0])); - bcx.icmp(cmp, bcx.load(scrutinee, alignment.to_align()), C_null(llptrty)) + let discr = bcx.load(scrutinee, alignment.to_align()); + bcx.icmp(cmp, discr, C_null(val_ty(discr))) } layout::StructWrappedNullablePointer { nndiscr, ref discrfield, .. } => { 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 assert_eq!(to, Disr(0)); } layout::RawNullablePointer { nndiscr, .. } => { - let nnty = compute_fields(bcx.ccx, t, nndiscr as usize, false)[0]; if to.0 != nndiscr { - let llptrty = type_of::sizing_type_of(bcx.ccx, nnty); + let llptrty = val_ty(val).element_type(); bcx.store(C_null(llptrty), val, None); } } diff --git a/src/librustc_trans/base.rs b/src/librustc_trans/base.rs index d204703b775..0b6d9cfd96f 100644 --- a/src/librustc_trans/base.rs +++ b/src/librustc_trans/base.rs @@ -59,7 +59,6 @@ use debuginfo; use declare; use machine; -use machine::llsize_of; use meth; use mir; use monomorphize::{self, Instance}; @@ -534,14 +533,13 @@ pub fn memcpy_ty<'a, 'tcx>( ) { let ccx = bcx.ccx; - if type_is_zero_size(ccx, t) { + let size = ccx.size_of(t); + if size == 0 { return; } - let llty = type_of::type_of(ccx, t); - let llsz = llsize_of(ccx, llty); - let llalign = align.unwrap_or_else(|| type_of::align_of(ccx, t)); - call_memcpy(bcx, dst, src, llsz, llalign as u32); + let align = align.unwrap_or_else(|| ccx.align_of(t)); + call_memcpy(bcx, dst, src, C_uint(ccx, size), align); } pub fn call_memset<'a, 'tcx>(b: &Builder<'a, 'tcx>, diff --git a/src/librustc_trans/common.rs b/src/librustc_trans/common.rs index a0906bb02f5..cd963536368 100644 --- a/src/librustc_trans/common.rs +++ b/src/librustc_trans/common.rs @@ -125,10 +125,8 @@ pub fn type_is_imm_pair<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, ty: Ty<'tcx>) /// Identify types which have size zero at runtime. pub fn type_is_zero_size<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, ty: Ty<'tcx>) -> bool { - use machine::llsize_of_alloc; - use type_of::sizing_type_of; - let llty = sizing_type_of(ccx, ty); - llsize_of_alloc(ccx, llty) == 0 + let layout = ccx.layout_of(ty); + !layout.is_unsized() && layout.size(&ccx.tcx().data_layout).bytes() == 0 } /* diff --git a/src/librustc_trans/consts.rs b/src/librustc_trans/consts.rs index daf1a1ba95f..6b6fa538dc0 100644 --- a/src/librustc_trans/consts.rs +++ b/src/librustc_trans/consts.rs @@ -255,7 +255,7 @@ pub fn trans_static<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, ccx.statics_to_rauw().borrow_mut().push((g, new_g)); new_g }; - llvm::LLVMSetAlignment(g, type_of::align_of(ccx, ty)); + llvm::LLVMSetAlignment(g, ccx.align_of(ty)); llvm::LLVMSetInitializer(g, v); // As an optimization, all shared statics which do not have interior diff --git a/src/librustc_trans/debuginfo/metadata.rs b/src/librustc_trans/debuginfo/metadata.rs index 93798e7bd33..f1e1a3bb221 100644 --- a/src/librustc_trans/debuginfo/metadata.rs +++ b/src/librustc_trans/debuginfo/metadata.rs @@ -1772,7 +1772,7 @@ pub fn create_global_var_metadata(cx: &CrateContext, let var_name = CString::new(var_name).unwrap(); let linkage_name = CString::new(linkage_name).unwrap(); - let global_align = type_of::align_of(cx, variable_type); + let global_align = cx.align_of(variable_type); unsafe { llvm::LLVMRustDIBuilderCreateStaticVariable(DIB(cx), diff --git a/src/librustc_trans/debuginfo/mod.rs b/src/librustc_trans/debuginfo/mod.rs index 8e86b50b3f7..1b7cf26853b 100644 --- a/src/librustc_trans/debuginfo/mod.rs +++ b/src/librustc_trans/debuginfo/mod.rs @@ -449,7 +449,7 @@ pub fn declare_local<'a, 'tcx>(bcx: &Builder<'a, 'tcx>, LocalVariable | CapturedVariable => (0, DW_TAG_auto_variable) }; - let align = ::type_of::align_of(cx, variable_type); + let align = cx.align_of(variable_type); let name = CString::new(variable_name.as_str().as_bytes()).unwrap(); match (variable_access, &[][..]) { diff --git a/src/librustc_trans/glue.rs b/src/librustc_trans/glue.rs index 41a9ab2842d..c2aa6c82941 100644 --- a/src/librustc_trans/glue.rs +++ b/src/librustc_trans/glue.rs @@ -19,10 +19,8 @@ use rustc::traits; use rustc::ty::{self, Ty, TypeFoldable}; use common::*; -use machine::*; use meth; use monomorphize; -use type_of::{sizing_type_of, align_of}; use value::Value; use builder::Builder; @@ -69,9 +67,8 @@ pub fn size_and_align_of_dst<'a, 'tcx>(bcx: &Builder<'a, 'tcx>, t: Ty<'tcx>, inf debug!("calculate size of DST: {}; with lost info: {:?}", t, Value(info)); if bcx.ccx.shared().type_is_sized(t) { - let sizing_type = sizing_type_of(bcx.ccx, t); - let size = llsize_of_alloc(bcx.ccx, sizing_type); - let align = align_of(bcx.ccx, t); + let size = bcx.ccx.size_of(t); + let align = bcx.ccx.align_of(t); debug!("size_and_align_of_dst t={} info={:?} size: {} align: {}", t, Value(info), size, align); 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 ty::TyAdt(def, substs) => { let ccx = bcx.ccx; // First get the size of all statically known fields. - // Don't use type_of::sizing_type_of because that expects t to be sized, - // and it also rounds up to alignment, which we want to avoid, - // as the unsized field's alignment could be smaller. + // Don't use size_of because it also rounds up to alignment, which we + // want to avoid, as the unsized field's alignment could be smaller. assert!(!t.is_simd()); let layout = ccx.layout_of(t); 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 (meth::SIZE.get_usize(bcx, info), meth::ALIGN.get_usize(bcx, info)) } ty::TySlice(_) | ty::TyStr => { - let unit_ty = t.sequence_element_type(bcx.tcx()); + let unit = t.sequence_element_type(bcx.tcx()); // The info in this case is the length of the str, so the size is that // times the unit size. - let llunit_ty = sizing_type_of(bcx.ccx, unit_ty); - let unit_align = llalign_of_min(bcx.ccx, llunit_ty); - let unit_size = llsize_of_alloc(bcx.ccx, llunit_ty); - (bcx.mul(info, C_uint(bcx.ccx, unit_size)), - C_uint(bcx.ccx, unit_align)) + (bcx.mul(info, C_uint(bcx.ccx, bcx.ccx.size_of(unit))), + C_uint(bcx.ccx, bcx.ccx.align_of(unit))) } _ => bug!("Unexpected unsized type, found {}", t) } diff --git a/src/librustc_trans/intrinsic.rs b/src/librustc_trans/intrinsic.rs index 762bf8592ff..5e7d612d17f 100644 --- a/src/librustc_trans/intrinsic.rs +++ b/src/librustc_trans/intrinsic.rs @@ -151,7 +151,7 @@ pub fn trans_intrinsic_call<'a, 'tcx>(bcx: &Builder<'a, 'tcx>, } "min_align_of" => { let tp_ty = substs.type_at(0); - C_uint(ccx, type_of::align_of(ccx, tp_ty)) + C_uint(ccx, ccx.align_of(tp_ty)) } "min_align_of_val" => { let tp_ty = substs.type_at(0); @@ -160,7 +160,7 @@ pub fn trans_intrinsic_call<'a, 'tcx>(bcx: &Builder<'a, 'tcx>, glue::size_and_align_of_dst(bcx, tp_ty, llargs[1]); llalign } else { - C_uint(ccx, type_of::align_of(ccx, tp_ty)) + C_uint(ccx, ccx.align_of(tp_ty)) } } "pref_align_of" => { @@ -234,7 +234,7 @@ pub fn trans_intrinsic_call<'a, 'tcx>(bcx: &Builder<'a, 'tcx>, } let load = bcx.volatile_load(ptr); unsafe { - llvm::LLVMSetAlignment(load, type_of::align_of(ccx, tp_ty)); + llvm::LLVMSetAlignment(load, ccx.align_of(tp_ty)); } to_immediate(bcx, load, tp_ty) }, @@ -252,7 +252,7 @@ pub fn trans_intrinsic_call<'a, 'tcx>(bcx: &Builder<'a, 'tcx>, let ptr = bcx.pointercast(llargs[0], val_ty(val).ptr_to()); let store = bcx.volatile_store(val, ptr); unsafe { - llvm::LLVMSetAlignment(store, type_of::align_of(ccx, tp_ty)); + llvm::LLVMSetAlignment(store, ccx.align_of(tp_ty)); } } C_nil(ccx) @@ -634,7 +634,7 @@ fn modify_as_needed<'a, 'tcx>(bcx: &Builder<'a, 'tcx>, if val_ty(llval) != Type::void(ccx) && machine::llsize_of_alloc(ccx, val_ty(llval)) != 0 { if let Some(ty) = fn_ty.ret.cast { let ptr = bcx.pointercast(llresult, ty.ptr_to()); - bcx.store(llval, ptr, Some(type_of::align_of(ccx, ret_ty))); + bcx.store(llval, ptr, Some(ccx.align_of(ret_ty))); } else { store_ty(bcx, llval, llresult, Alignment::AbiAligned, ret_ty); } @@ -651,7 +651,7 @@ fn copy_intrinsic<'a, 'tcx>(bcx: &Builder<'a, 'tcx>, -> ValueRef { let ccx = bcx.ccx; let lltp_ty = type_of::type_of(ccx, tp_ty); - let align = C_i32(ccx, type_of::align_of(ccx, tp_ty) as i32); + let align = C_i32(ccx, ccx.align_of(tp_ty) as i32); let size = machine::llsize_of(ccx, lltp_ty); let int_size = machine::llbitsize_of_real(ccx, ccx.int_type()); @@ -685,7 +685,7 @@ fn memset_intrinsic<'a, 'tcx>( count: ValueRef ) -> ValueRef { let ccx = bcx.ccx; - let align = C_i32(ccx, type_of::align_of(ccx, ty) as i32); + let align = C_i32(ccx, ccx.align_of(ty) as i32); let lltp_ty = type_of::type_of(ccx, ty); let size = machine::llsize_of(ccx, lltp_ty); let dst = bcx.pointercast(dst, Type::i8p(ccx)); diff --git a/src/librustc_trans/meth.rs b/src/librustc_trans/meth.rs index 75ab4076140..f5f92417858 100644 --- a/src/librustc_trans/meth.rs +++ b/src/librustc_trans/meth.rs @@ -17,7 +17,6 @@ use machine; use monomorphize; use type_::Type; -use type_of::*; use value::Value; use rustc::ty; @@ -80,14 +79,10 @@ pub fn get_vtable<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, // Not in the cache. Build it. let nullptr = C_null(Type::nil(ccx).ptr_to()); - let size_ty = sizing_type_of(ccx, ty); - let size = machine::llsize_of_alloc(ccx, size_ty); - let align = align_of(ccx, ty); - let mut components: Vec<_> = [ callee::get_fn(ccx, monomorphize::resolve_drop_in_place(ccx.shared(), ty)), - C_uint(ccx, size), - C_uint(ccx, align) + C_uint(ccx, ccx.size_of(ty)), + C_uint(ccx, ccx.align_of(ty)) ].iter().cloned().collect(); if let Some(trait_ref) = trait_ref { diff --git a/src/librustc_trans/mir/block.rs b/src/librustc_trans/mir/block.rs index d69f31a4504..84ae8143b30 100644 --- a/src/librustc_trans/mir/block.rs +++ b/src/librustc_trans/mir/block.rs @@ -24,8 +24,8 @@ use machine::llalign_of_min; use meth; use monomorphize; +use type_of; use tvec; -use type_of::{self, align_of}; use type_::Type; use rustc_data_structures::indexed_vec::IndexVec; @@ -910,7 +910,7 @@ fn trans_transmute_into(&mut self, bcx: &Builder<'a, 'tcx>, let cast_ptr = bcx.pointercast(dst.llval, llty.ptr_to()); let in_type = val.ty; let out_type = dst.ty.to_ty(bcx.tcx());; - let llalign = cmp::min(align_of(bcx.ccx, in_type), align_of(bcx.ccx, out_type)); + let llalign = cmp::min(bcx.ccx.align_of(in_type), bcx.ccx.align_of(out_type)); self.store_operand(bcx, cast_ptr, Some(llalign), val); } diff --git a/src/librustc_trans/mir/constant.rs b/src/librustc_trans/mir/constant.rs index dbd928194c0..bd58178b074 100644 --- a/src/librustc_trans/mir/constant.rs +++ b/src/librustc_trans/mir/constant.rs @@ -148,7 +148,7 @@ pub fn to_operand<'a>(&self, ccx: &CrateContext<'a, 'tcx>) -> OperandRef<'tcx> { } else { // Otherwise, or if the value is not immediate, we create // a constant LLVM global and cast its address if necessary. - let align = type_of::align_of(ccx, self.ty); + let align = ccx.align_of(self.ty); let ptr = consts::addr_of(ccx, self.llval, align, "const"); OperandValue::Ref(consts::ptrcast(ptr, llty.ptr_to()), Alignment::AbiAligned) }; @@ -717,7 +717,7 @@ fn const_rvalue(&self, rvalue: &mir::Rvalue<'tcx>, Base::Value(llval) => { // FIXME: may be wrong for &*(&simd_vec as &fmt::Debug) let align = if self.ccx.shared().type_is_sized(ty) { - type_of::align_of(self.ccx, ty) + self.ccx.align_of(ty) } else { self.ccx.tcx().data_layout.pointer_align.abi() as machine::llalign }; @@ -1022,25 +1022,20 @@ fn trans_const<'a, 'tcx>( C_vector(vals) } layout::RawNullablePointer { nndiscr, .. } => { - let nnty = adt::compute_fields(ccx, t, nndiscr as usize, false)[0]; if variant_index as u64 == nndiscr { assert_eq!(vals.len(), 1); vals[0] } else { - C_null(type_of::sizing_type_of(ccx, nnty)) + C_null(type_of::type_of(ccx, t)) } } layout::StructWrappedNullablePointer { ref nonnull, nndiscr, .. } => { if variant_index as u64 == nndiscr { C_struct(ccx, &build_const_struct(ccx, &nonnull, vals), false) } else { - let fields = adt::compute_fields(ccx, t, nndiscr as usize, false); - let vals = fields.iter().map(|&ty| { - // Always use null even if it's not the `discrfield`th - // field; see #8506. - C_null(type_of::sizing_type_of(ccx, ty)) - }).collect::>(); - C_struct(ccx, &build_const_struct(ccx, &nonnull, &vals[..]), false) + // Always use null even if it's not the `discrfield`th + // field; see #8506. + C_null(type_of::type_of(ccx, t)) } } _ => bug!("trans_const: cannot handle type {} repreented as {:#?}", t, l) diff --git a/src/librustc_trans/mir/rvalue.rs b/src/librustc_trans/mir/rvalue.rs index d487aa6cd5b..12633720ef8 100644 --- a/src/librustc_trans/mir/rvalue.rs +++ b/src/librustc_trans/mir/rvalue.rs @@ -438,7 +438,7 @@ pub fn trans_rvalue_operand(&mut self, let content_ty: Ty<'tcx> = self.monomorphize(&content_ty); let llty = type_of::type_of(bcx.ccx, content_ty); let llsize = machine::llsize_of(bcx.ccx, llty); - let align = type_of::align_of(bcx.ccx, content_ty); + let align = bcx.ccx.align_of(content_ty); let llalign = C_uint(bcx.ccx, align); let llty_ptr = llty.ptr_to(); let box_ty = bcx.tcx().mk_box(content_ty); diff --git a/src/librustc_trans/type_of.rs b/src/librustc_trans/type_of.rs index a5722e6e520..b195429711d 100644 --- a/src/librustc_trans/type_of.rs +++ b/src/librustc_trans/type_of.rs @@ -322,10 +322,16 @@ pub fn in_memory_type_of<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>) -> llty } -pub fn align_of<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>) - -> machine::llalign { - let layout = cx.layout_of(t); - layout.align(&cx.tcx().data_layout).abi() as machine::llalign +impl<'a, 'tcx> CrateContext<'a, 'tcx> { + pub fn align_of(&self, ty: Ty<'tcx>) -> machine::llalign { + let layout = self.layout_of(ty); + layout.align(&self.tcx().data_layout).abi() as machine::llalign + } + + pub fn size_of(&self, ty: Ty<'tcx>) -> machine::llsize { + let layout = self.layout_of(ty); + layout.size(&self.tcx().data_layout).bytes() as machine::llsize + } } fn llvm_type_name<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, ty: Ty<'tcx>) -> String { -- 2.44.0