3 // Code relating to drop glue.
5 use crate::common::IntPredicate;
8 use rustc_middle::ty::{self, Ty};
9 use rustc_target::abi::WrappingRange;
11 pub fn size_and_align_of_dst<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
14 info: Option<Bx::Value>,
15 ) -> (Bx::Value, Bx::Value) {
16 let layout = bx.layout_of(t);
17 debug!("size_and_align_of_dst(ty={}, info={:?}): layout: {:?}", t, info, layout);
18 if layout.is_sized() {
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 let size = meth::VirtualIndex::from_index(ty::COMMON_VTABLE_ENTRIES_SIZE)
28 .get_usize(bx, vtable);
29 let align = meth::VirtualIndex::from_index(ty::COMMON_VTABLE_ENTRIES_ALIGN)
30 .get_usize(bx, vtable);
32 // Size is always <= isize::MAX.
33 let size_bound = bx.data_layout().ptr_sized_integer().signed_max() as u128;
34 bx.range_metadata(size, WrappingRange { start: 0, end: size_bound });
35 // Alignment is always nonzero.
36 bx.range_metadata(align, WrappingRange { start: 1, end: !0 });
40 ty::Slice(_) | ty::Str => {
41 let unit = layout.field(bx, 0);
42 // The info in this case is the length of the str, so the size is that
43 // times the unit size.
45 // All slice sizes must fit into `isize`, so this multiplication cannot (signed) wrap.
46 // NOTE: ideally, we want the effects of both `unchecked_smul` and `unchecked_umul`
47 // (resulting in `mul nsw nuw` in LLVM IR), since we know that the multiplication
48 // cannot signed wrap, and that both operands are non-negative. But at the time of writing,
49 // `BuilderMethods` can't do this, and it doesn't seem to enable any further optimizations.
50 bx.unchecked_smul(info.unwrap(), bx.const_usize(unit.size.bytes())),
51 bx.const_usize(unit.align.abi.bytes()),
55 // First get the size of all statically known fields.
56 // Don't use size_of because it also rounds up to alignment, which we
57 // want to avoid, as the unsized field's alignment could be smaller.
58 assert!(!t.is_simd());
59 debug!("DST {} layout: {:?}", t, layout);
61 let i = layout.fields.count() - 1;
62 let sized_size = layout.fields.offset(i).bytes();
63 let sized_align = layout.align.abi.bytes();
64 debug!("DST {} statically sized prefix size: {} align: {}", t, sized_size, sized_align);
65 let sized_size = bx.const_usize(sized_size);
66 let sized_align = bx.const_usize(sized_align);
68 // Recurse to get the size of the dynamically sized field (must be
70 let field_ty = layout.field(bx, i).ty;
71 let (unsized_size, mut unsized_align) = size_and_align_of_dst(bx, field_ty, info);
73 // FIXME (#26403, #27023): We should be adding padding
74 // to `sized_size` (to accommodate the `unsized_align`
75 // required of the unsized field that follows) before
76 // summing it with `sized_size`. (Note that since #26403
77 // is unfixed, we do not yet add the necessary padding
78 // here. But this is where the add would go.)
80 // Return the sum of sizes and max of aligns.
81 let size = bx.add(sized_size, unsized_size);
83 // Packed types ignore the alignment of their fields.
84 if let ty::Adt(def, _) = t.kind() {
85 if def.repr().packed() {
86 unsized_align = sized_align;
90 // Choose max of two known alignments (combined value must
91 // be aligned according to more restrictive of the two).
93 bx.const_to_opt_u128(sized_align, false),
94 bx.const_to_opt_u128(unsized_align, false),
96 (Some(sized_align), Some(unsized_align)) => {
97 // If both alignments are constant, (the sized_align should always be), then
98 // pick the correct alignment statically.
99 bx.const_usize(std::cmp::max(sized_align, unsized_align) as u64)
102 let cmp = bx.icmp(IntPredicate::IntUGT, sized_align, unsized_align);
103 bx.select(cmp, sized_align, unsized_align)
107 // Issue #27023: must add any necessary padding to `size`
108 // (to make it a multiple of `align`) before returning it.
110 // Namely, the returned size should be, in C notation:
112 // `size + ((size & (align-1)) ? align : 0)`
114 // emulated via the semi-standard fast bit trick:
116 // `(size + (align-1)) & -align`
117 let one = bx.const_usize(1);
118 let addend = bx.sub(align, one);
119 let add = bx.add(size, addend);
120 let neg = bx.neg(align);
121 let size = bx.and(add, neg);