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