3 // Code relating to drop glue.
5 use rustc::ty::{self, Ty};
6 use crate::common::IntPredicate;
10 pub fn size_and_align_of_dst<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
13 info: Option<Bx::Value>,
14 ) -> (Bx::Value, Bx::Value) {
15 let layout = bx.layout_of(t);
16 debug!("size_and_align_of_dst(ty={}, info={:?}): layout: {:?}",
18 if !layout.is_unsized() {
19 let size = bx.const_usize(layout.size.bytes());
20 let align = bx.const_usize(layout.align.abi.bytes());
25 // load size/align from vtable
26 let vtable = info.unwrap();
27 (meth::SIZE.get_usize(bx, vtable), meth::ALIGN.get_usize(bx, vtable))
29 ty::Slice(_) | ty::Str => {
30 let unit = layout.field(bx, 0);
31 // The info in this case is the length of the str, so the size is that
32 // times the unit size.
33 (bx.mul(info.unwrap(), bx.const_usize(unit.size.bytes())),
34 bx.const_usize(unit.align.abi.bytes()))
37 // First get the size of all statically known fields.
38 // Don't use size_of because it also rounds up to alignment, which we
39 // want to avoid, as the unsized field's alignment could be smaller.
40 assert!(!t.is_simd());
41 debug!("DST {} layout: {:?}", t, layout);
43 let i = layout.fields.count() - 1;
44 let sized_size = layout.fields.offset(i).bytes();
45 let sized_align = layout.align.abi.bytes();
46 debug!("DST {} statically sized prefix size: {} align: {}",
47 t, sized_size, sized_align);
48 let sized_size = bx.const_usize(sized_size);
49 let sized_align = bx.const_usize(sized_align);
51 // Recurse to get the size of the dynamically sized field (must be
53 let field_ty = layout.field(bx, i).ty;
54 let (unsized_size, mut unsized_align) = size_and_align_of_dst(bx, field_ty, info);
56 // FIXME (#26403, #27023): We should be adding padding
57 // to `sized_size` (to accommodate the `unsized_align`
58 // required of the unsized field that follows) before
59 // summing it with `sized_size`. (Note that since #26403
60 // is unfixed, we do not yet add the necessary padding
61 // here. But this is where the add would go.)
63 // Return the sum of sizes and max of aligns.
64 let size = bx.add(sized_size, unsized_size);
66 // Packed types ignore the alignment of their fields.
67 if let ty::Adt(def, _) = t.sty {
68 if def.repr.packed() {
69 unsized_align = sized_align;
73 // Choose max of two known alignments (combined value must
74 // be aligned according to more restrictive of the two).
75 let align = match (bx.const_to_opt_u128(sized_align, false),
76 bx.const_to_opt_u128(unsized_align, false)) {
77 (Some(sized_align), Some(unsized_align)) => {
78 // If both alignments are constant, (the sized_align should always be), then
79 // pick the correct alignment statically.
80 bx.const_usize(std::cmp::max(sized_align, unsized_align) as u64)
83 let cmp = bx.icmp(IntPredicate::IntUGT, sized_align, unsized_align);
84 bx.select(cmp, sized_align, unsized_align)
88 // Issue #27023: must add any necessary padding to `size`
89 // (to make it a multiple of `align`) before returning it.
91 // Namely, the returned size should be, in C notation:
93 // `size + ((size & (align-1)) ? align : 0)`
95 // emulated via the semi-standard fast bit trick:
97 // `(size + (align-1)) & -align`
98 let one = bx.const_usize(1);
99 let addend = bx.sub(align, one);
100 let add = bx.add(size, addend);
101 let neg = bx.neg(align);
102 let size = bx.and(add, neg);