]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_codegen_ssa/src/glue.rs
Rollup merge of #91828 - oxalica:feat/waker-getters, r=dtolnay
[rust.git] / compiler / rustc_codegen_ssa / src / glue.rs
1 //!
2 //
3 // Code relating to drop glue.
4
5 use crate::common::IntPredicate;
6 use crate::meth;
7 use crate::traits::*;
8 use rustc_middle::ty::{self, Ty};
9 use rustc_target::abi::WrappingRange;
10
11 pub fn size_and_align_of_dst<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
12     bx: &mut Bx,
13     t: Ty<'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_unsized() {
19         let size = bx.const_usize(layout.size.bytes());
20         let align = bx.const_usize(layout.align.abi.bytes());
21         return (size, align);
22     }
23     match t.kind() {
24         ty::Dynamic(..) => {
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);
31
32             // Alignment is always nonzero.
33             bx.range_metadata(align, WrappingRange { start: 1, end: !0 });
34
35             (size, align)
36         }
37         ty::Slice(_) | ty::Str => {
38             let unit = layout.field(bx, 0);
39             // The info in this case is the length of the str, so the size is that
40             // times the unit size.
41             (
42                 bx.mul(info.unwrap(), bx.const_usize(unit.size.bytes())),
43                 bx.const_usize(unit.align.abi.bytes()),
44             )
45         }
46         _ => {
47             // First get the size of all statically known fields.
48             // Don't use size_of because it also rounds up to alignment, which we
49             // want to avoid, as the unsized field's alignment could be smaller.
50             assert!(!t.is_simd());
51             debug!("DST {} layout: {:?}", t, layout);
52
53             let i = layout.fields.count() - 1;
54             let sized_size = layout.fields.offset(i).bytes();
55             let sized_align = layout.align.abi.bytes();
56             debug!("DST {} statically sized prefix size: {} align: {}", t, sized_size, sized_align);
57             let sized_size = bx.const_usize(sized_size);
58             let sized_align = bx.const_usize(sized_align);
59
60             // Recurse to get the size of the dynamically sized field (must be
61             // the last field).
62             let field_ty = layout.field(bx, i).ty;
63             let (unsized_size, mut unsized_align) = size_and_align_of_dst(bx, field_ty, info);
64
65             // FIXME (#26403, #27023): We should be adding padding
66             // to `sized_size` (to accommodate the `unsized_align`
67             // required of the unsized field that follows) before
68             // summing it with `sized_size`. (Note that since #26403
69             // is unfixed, we do not yet add the necessary padding
70             // here. But this is where the add would go.)
71
72             // Return the sum of sizes and max of aligns.
73             let size = bx.add(sized_size, unsized_size);
74
75             // Packed types ignore the alignment of their fields.
76             if let ty::Adt(def, _) = t.kind() {
77                 if def.repr.packed() {
78                     unsized_align = sized_align;
79                 }
80             }
81
82             // Choose max of two known alignments (combined value must
83             // be aligned according to more restrictive of the two).
84             let align = match (
85                 bx.const_to_opt_u128(sized_align, false),
86                 bx.const_to_opt_u128(unsized_align, false),
87             ) {
88                 (Some(sized_align), Some(unsized_align)) => {
89                     // If both alignments are constant, (the sized_align should always be), then
90                     // pick the correct alignment statically.
91                     bx.const_usize(std::cmp::max(sized_align, unsized_align) as u64)
92                 }
93                 _ => {
94                     let cmp = bx.icmp(IntPredicate::IntUGT, sized_align, unsized_align);
95                     bx.select(cmp, sized_align, unsized_align)
96                 }
97             };
98
99             // Issue #27023: must add any necessary padding to `size`
100             // (to make it a multiple of `align`) before returning it.
101             //
102             // Namely, the returned size should be, in C notation:
103             //
104             //   `size + ((size & (align-1)) ? align : 0)`
105             //
106             // emulated via the semi-standard fast bit trick:
107             //
108             //   `(size + (align-1)) & -align`
109             let one = bx.const_usize(1);
110             let addend = bx.sub(align, one);
111             let add = bx.add(size, addend);
112             let neg = bx.neg(align);
113             let size = bx.and(add, neg);
114
115             (size, align)
116         }
117     }
118 }