]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/glue.rs
Auto merge of #41168 - Shizmob:jemalloc-musl, r=alexcrichton
[rust.git] / src / librustc_trans / glue.rs
1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //!
12 //
13 // Code relating to drop glue.
14
15 use std;
16
17 use llvm;
18 use llvm::{ValueRef};
19 use rustc::traits;
20 use rustc::ty::{self, Ty, TypeFoldable};
21 use rustc::ty::layout::LayoutTyper;
22 use common::*;
23 use meth;
24 use monomorphize;
25 use value::Value;
26 use builder::Builder;
27
28 pub fn needs_drop_glue<'a, 'tcx>(scx: &SharedCrateContext<'a, 'tcx>, t: Ty<'tcx>) -> bool {
29     assert!(t.is_normalized_for_trans());
30
31     let t = scx.tcx().erase_regions(&t);
32
33     // FIXME (#22815): note that type_needs_drop conservatively
34     // approximates in some cases and may say a type expression
35     // requires drop glue when it actually does not.
36     //
37     // (In this case it is not clear whether any harm is done, i.e.
38     // erroneously returning `true` in some cases where we could have
39     // returned `false` does not appear unsound. The impact on
40     // code quality is unknown at this time.)
41
42     if !scx.type_needs_drop(t) {
43         return false;
44     }
45     match t.sty {
46         ty::TyAdt(def, _) if def.is_box() => {
47             let typ = t.boxed_ty();
48             if !scx.type_needs_drop(typ) && scx.type_is_sized(typ) {
49                 scx.tcx().infer_ctxt((), traits::Reveal::All).enter(|infcx| {
50                     let layout = t.layout(&infcx).unwrap();
51                     if layout.size(scx).bytes() == 0 {
52                         // `Box<ZeroSizeType>` does not allocate.
53                         false
54                     } else {
55                         true
56                     }
57                 })
58             } else {
59                 true
60             }
61         }
62         _ => true
63     }
64 }
65
66 pub fn size_and_align_of_dst<'a, 'tcx>(bcx: &Builder<'a, 'tcx>, t: Ty<'tcx>, info: ValueRef)
67                                        -> (ValueRef, ValueRef) {
68     debug!("calculate size of DST: {}; with lost info: {:?}",
69            t, Value(info));
70     if bcx.ccx.shared().type_is_sized(t) {
71         let size = bcx.ccx.size_of(t);
72         let align = bcx.ccx.align_of(t);
73         debug!("size_and_align_of_dst t={} info={:?} size: {} align: {}",
74                t, Value(info), size, align);
75         let size = C_uint(bcx.ccx, size);
76         let align = C_uint(bcx.ccx, align);
77         return (size, align);
78     }
79     match t.sty {
80         ty::TyAdt(def, substs) => {
81             let ccx = bcx.ccx;
82             // First get the size of all statically known fields.
83             // Don't use size_of because it also rounds up to alignment, which we
84             // want to avoid, as the unsized field's alignment could be smaller.
85             assert!(!t.is_simd());
86             let layout = ccx.layout_of(t);
87             debug!("DST {} layout: {:?}", t, layout);
88
89             let (sized_size, sized_align) = match *layout {
90                 ty::layout::Layout::Univariant { ref variant, .. } => {
91                     (variant.offsets.last().map_or(0, |o| o.bytes()), variant.align.abi())
92                 }
93                 _ => {
94                     bug!("size_and_align_of_dst: expcted Univariant for `{}`, found {:#?}",
95                          t, layout);
96                 }
97             };
98             debug!("DST {} statically sized prefix size: {} align: {}",
99                    t, sized_size, sized_align);
100             let sized_size = C_uint(ccx, sized_size);
101             let sized_align = C_uint(ccx, sized_align);
102
103             // Recurse to get the size of the dynamically sized field (must be
104             // the last field).
105             let last_field = def.struct_variant().fields.last().unwrap();
106             let field_ty = monomorphize::field_ty(bcx.tcx(), substs, last_field);
107             let (unsized_size, unsized_align) = size_and_align_of_dst(bcx, field_ty, info);
108
109             // FIXME (#26403, #27023): We should be adding padding
110             // to `sized_size` (to accommodate the `unsized_align`
111             // required of the unsized field that follows) before
112             // summing it with `sized_size`. (Note that since #26403
113             // is unfixed, we do not yet add the necessary padding
114             // here. But this is where the add would go.)
115
116             // Return the sum of sizes and max of aligns.
117             let size = bcx.add(sized_size, unsized_size);
118
119             // Choose max of two known alignments (combined value must
120             // be aligned according to more restrictive of the two).
121             let align = match (const_to_opt_u128(sized_align, false),
122                                const_to_opt_u128(unsized_align, false)) {
123                 (Some(sized_align), Some(unsized_align)) => {
124                     // If both alignments are constant, (the sized_align should always be), then
125                     // pick the correct alignment statically.
126                     C_uint(ccx, std::cmp::max(sized_align, unsized_align) as u64)
127                 }
128                 _ => bcx.select(bcx.icmp(llvm::IntUGT, sized_align, unsized_align),
129                                 sized_align,
130                                 unsized_align)
131             };
132
133             // Issue #27023: must add any necessary padding to `size`
134             // (to make it a multiple of `align`) before returning it.
135             //
136             // Namely, the returned size should be, in C notation:
137             //
138             //   `size + ((size & (align-1)) ? align : 0)`
139             //
140             // emulated via the semi-standard fast bit trick:
141             //
142             //   `(size + (align-1)) & -align`
143
144             let addend = bcx.sub(align, C_uint(bcx.ccx, 1_u64));
145             let size = bcx.and(bcx.add(size, addend), bcx.neg(align));
146
147             (size, align)
148         }
149         ty::TyDynamic(..) => {
150             // load size/align from vtable
151             (meth::SIZE.get_usize(bcx, info), meth::ALIGN.get_usize(bcx, info))
152         }
153         ty::TySlice(_) | ty::TyStr => {
154             let unit = t.sequence_element_type(bcx.tcx());
155             // The info in this case is the length of the str, so the size is that
156             // times the unit size.
157             (bcx.mul(info, C_uint(bcx.ccx, bcx.ccx.size_of(unit))),
158              C_uint(bcx.ccx, bcx.ccx.align_of(unit)))
159         }
160         _ => bug!("Unexpected unsized type, found {}", t)
161     }
162 }