]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_ssa/mir/operand.rs
Auto merge of #65013 - petertodd:2019-maybeuninit-debug, r=sfackler
[rust.git] / src / librustc_codegen_ssa / mir / operand.rs
1 use super::{FunctionCx, LocalRef};
2 use super::place::PlaceRef;
3
4 use crate::MemFlags;
5 use crate::base;
6 use crate::glue;
7 use crate::traits::*;
8
9 use rustc::mir::interpret::{ConstValue, ErrorHandled, Pointer, Scalar};
10 use rustc::mir;
11 use rustc::ty;
12 use rustc::ty::layout::{self, Align, LayoutOf, TyLayout, Size};
13
14 use std::fmt;
15
16 /// The representation of a Rust value. The enum variant is in fact
17 /// uniquely determined by the value's type, but is kept as a
18 /// safety check.
19 #[derive(Copy, Clone, Debug)]
20 pub enum OperandValue<V> {
21     /// A reference to the actual operand. The data is guaranteed
22     /// to be valid for the operand's lifetime.
23     /// The second value, if any, is the extra data (vtable or length)
24     /// which indicates that it refers to an unsized rvalue.
25     Ref(V, Option<V>, Align),
26     /// A single LLVM value.
27     Immediate(V),
28     /// A pair of immediate LLVM values. Used by fat pointers too.
29     Pair(V, V)
30 }
31
32 /// An `OperandRef` is an "SSA" reference to a Rust value, along with
33 /// its type.
34 ///
35 /// NOTE: unless you know a value's type exactly, you should not
36 /// generate LLVM opcodes acting on it and instead act via methods,
37 /// to avoid nasty edge cases. In particular, using `Builder::store`
38 /// directly is sure to cause problems -- use `OperandRef::store`
39 /// instead.
40 #[derive(Copy, Clone)]
41 pub struct OperandRef<'tcx, V> {
42     // The value.
43     pub val: OperandValue<V>,
44
45     // The layout of value, based on its Rust type.
46     pub layout: TyLayout<'tcx>,
47 }
48
49 impl<V: CodegenObject> fmt::Debug for OperandRef<'tcx, V> {
50     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51         write!(f, "OperandRef({:?} @ {:?})", self.val, self.layout)
52     }
53 }
54
55 impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
56     pub fn new_zst<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
57         bx: &mut Bx,
58         layout: TyLayout<'tcx>
59     ) -> OperandRef<'tcx, V> {
60         assert!(layout.is_zst());
61         OperandRef {
62             val: OperandValue::Immediate(bx.const_undef(bx.immediate_backend_type(layout))),
63             layout
64         }
65     }
66
67     pub fn from_const<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
68         bx: &mut Bx,
69         val: &'tcx ty::Const<'tcx>
70     ) -> Self {
71         let layout = bx.layout_of(val.ty);
72
73         if layout.is_zst() {
74             return OperandRef::new_zst(bx, layout);
75         }
76
77         let val_val = match val.val {
78             ty::ConstKind::Value(val_val) => val_val,
79             _ => bug!("encountered bad ConstKind in codegen"),
80         };
81
82         let val = match val_val {
83             ConstValue::Scalar(x) => {
84                 let scalar = match layout.abi {
85                     layout::Abi::Scalar(ref x) => x,
86                     _ => bug!("from_const: invalid ByVal layout: {:#?}", layout)
87                 };
88                 let llval = bx.scalar_to_backend(
89                     x,
90                     scalar,
91                     bx.immediate_backend_type(layout),
92                 );
93                 OperandValue::Immediate(llval)
94             },
95             ConstValue::Slice { data, start, end } => {
96                 let a_scalar = match layout.abi {
97                     layout::Abi::ScalarPair(ref a, _) => a,
98                     _ => bug!("from_const: invalid ScalarPair layout: {:#?}", layout)
99                 };
100                 let a = Scalar::from(Pointer::new(
101                     bx.tcx().alloc_map.lock().create_memory_alloc(data),
102                     Size::from_bytes(start as u64),
103                 )).into();
104                 let a_llval = bx.scalar_to_backend(
105                     a,
106                     a_scalar,
107                     bx.scalar_pair_element_backend_type(layout, 0, true),
108                 );
109                 let b_llval = bx.const_usize((end - start) as u64);
110                 OperandValue::Pair(a_llval, b_llval)
111             },
112             ConstValue::ByRef { alloc, offset } => {
113                 return bx.load_operand(bx.from_const_alloc(layout, alloc, offset));
114             },
115         };
116
117         OperandRef {
118             val,
119             layout
120         }
121     }
122
123     /// Asserts that this operand refers to a scalar and returns
124     /// a reference to its value.
125     pub fn immediate(self) -> V {
126         match self.val {
127             OperandValue::Immediate(s) => s,
128             _ => bug!("not immediate: {:?}", self)
129         }
130     }
131
132     pub fn deref<Cx: LayoutTypeMethods<'tcx>>(
133         self,
134         cx: &Cx
135     ) -> PlaceRef<'tcx, V> {
136         let projected_ty = self.layout.ty.builtin_deref(true)
137             .unwrap_or_else(|| bug!("deref of non-pointer {:?}", self)).ty;
138         let (llptr, llextra) = match self.val {
139             OperandValue::Immediate(llptr) => (llptr, None),
140             OperandValue::Pair(llptr, llextra) => (llptr, Some(llextra)),
141             OperandValue::Ref(..) => bug!("Deref of by-Ref operand {:?}", self)
142         };
143         let layout = cx.layout_of(projected_ty);
144         PlaceRef {
145             llval: llptr,
146             llextra,
147             layout,
148             align: layout.align.abi,
149         }
150     }
151
152     /// If this operand is a `Pair`, we return an aggregate with the two values.
153     /// For other cases, see `immediate`.
154     pub fn immediate_or_packed_pair<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
155         self,
156         bx: &mut Bx
157     ) -> V {
158         if let OperandValue::Pair(a, b) = self.val {
159             let llty = bx.cx().backend_type(self.layout);
160             debug!("Operand::immediate_or_packed_pair: packing {:?} into {:?}",
161                    self, llty);
162             // Reconstruct the immediate aggregate.
163             let mut llpair = bx.cx().const_undef(llty);
164             let imm_a = base::from_immediate(bx, a);
165             let imm_b = base::from_immediate(bx, b);
166             llpair = bx.insert_value(llpair, imm_a, 0);
167             llpair = bx.insert_value(llpair, imm_b, 1);
168             llpair
169         } else {
170             self.immediate()
171         }
172     }
173
174     /// If the type is a pair, we return a `Pair`, otherwise, an `Immediate`.
175     pub fn from_immediate_or_packed_pair<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
176         bx: &mut Bx,
177         llval: V,
178         layout: TyLayout<'tcx>
179     ) -> Self {
180         let val = if let layout::Abi::ScalarPair(ref a, ref b) = layout.abi {
181             debug!("Operand::from_immediate_or_packed_pair: unpacking {:?} @ {:?}",
182                     llval, layout);
183
184             // Deconstruct the immediate aggregate.
185             let a_llval = bx.extract_value(llval, 0);
186             let a_llval = base::to_immediate_scalar(bx, a_llval, a);
187             let b_llval = bx.extract_value(llval, 1);
188             let b_llval = base::to_immediate_scalar(bx, b_llval, b);
189             OperandValue::Pair(a_llval, b_llval)
190         } else {
191             OperandValue::Immediate(llval)
192         };
193         OperandRef { val, layout }
194     }
195
196     pub fn extract_field<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
197         &self,
198         bx: &mut Bx,
199         i: usize
200     ) -> Self {
201         let field = self.layout.field(bx.cx(), i);
202         let offset = self.layout.fields.offset(i);
203
204         let mut val = match (self.val, &self.layout.abi) {
205             // If the field is ZST, it has no data.
206             _ if field.is_zst() => {
207                 return OperandRef::new_zst(bx, field);
208             }
209
210             // Newtype of a scalar, scalar pair or vector.
211             (OperandValue::Immediate(_), _) |
212             (OperandValue::Pair(..), _) if field.size == self.layout.size => {
213                 assert_eq!(offset.bytes(), 0);
214                 self.val
215             }
216
217             // Extract a scalar component from a pair.
218             (OperandValue::Pair(a_llval, b_llval), &layout::Abi::ScalarPair(ref a, ref b)) => {
219                 if offset.bytes() == 0 {
220                     assert_eq!(field.size, a.value.size(bx.cx()));
221                     OperandValue::Immediate(a_llval)
222                 } else {
223                     assert_eq!(offset, a.value.size(bx.cx())
224                         .align_to(b.value.align(bx.cx()).abi));
225                     assert_eq!(field.size, b.value.size(bx.cx()));
226                     OperandValue::Immediate(b_llval)
227                 }
228             }
229
230             // `#[repr(simd)]` types are also immediate.
231             (OperandValue::Immediate(llval), &layout::Abi::Vector { .. }) => {
232                 OperandValue::Immediate(
233                     bx.extract_element(llval, bx.cx().const_usize(i as u64)))
234             }
235
236             _ => bug!("OperandRef::extract_field({:?}): not applicable", self)
237         };
238
239         // HACK(eddyb) have to bitcast pointers until LLVM removes pointee types.
240         // Bools in union fields needs to be truncated.
241         let to_immediate_or_cast = |bx: &mut Bx, val, ty| {
242             if ty == bx.cx().type_i1() {
243                 bx.trunc(val, ty)
244             } else {
245                 bx.bitcast(val, ty)
246             }
247         };
248
249         match val {
250             OperandValue::Immediate(ref mut llval) => {
251                 *llval = to_immediate_or_cast(bx, *llval, bx.cx().immediate_backend_type(field));
252             }
253             OperandValue::Pair(ref mut a, ref mut b) => {
254                 *a = to_immediate_or_cast(bx, *a, bx.cx()
255                     .scalar_pair_element_backend_type(field, 0, true));
256                 *b = to_immediate_or_cast(bx, *b, bx.cx()
257                     .scalar_pair_element_backend_type(field, 1, true));
258             }
259             OperandValue::Ref(..) => bug!()
260         }
261
262         OperandRef {
263             val,
264             layout: field
265         }
266     }
267 }
268
269 impl<'a, 'tcx, V: CodegenObject> OperandValue<V> {
270     pub fn store<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
271         self,
272         bx: &mut Bx,
273         dest: PlaceRef<'tcx, V>
274     ) {
275         self.store_with_flags(bx, dest, MemFlags::empty());
276     }
277
278     pub fn volatile_store<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
279         self,
280         bx: &mut Bx,
281         dest: PlaceRef<'tcx, V>
282     ) {
283         self.store_with_flags(bx, dest, MemFlags::VOLATILE);
284     }
285
286     pub fn unaligned_volatile_store<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
287         self,
288         bx: &mut Bx,
289         dest: PlaceRef<'tcx, V>,
290     ) {
291         self.store_with_flags(bx, dest, MemFlags::VOLATILE | MemFlags::UNALIGNED);
292     }
293
294     pub fn nontemporal_store<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
295         self,
296         bx: &mut Bx,
297         dest: PlaceRef<'tcx, V>
298     ) {
299         self.store_with_flags(bx, dest, MemFlags::NONTEMPORAL);
300     }
301
302     fn store_with_flags<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
303         self,
304         bx: &mut Bx,
305         dest: PlaceRef<'tcx, V>,
306         flags: MemFlags,
307     ) {
308         debug!("OperandRef::store: operand={:?}, dest={:?}", self, dest);
309         // Avoid generating stores of zero-sized values, because the only way to have a zero-sized
310         // value is through `undef`, and store itself is useless.
311         if dest.layout.is_zst() {
312             return;
313         }
314         match self {
315             OperandValue::Ref(r, None, source_align) => {
316                 base::memcpy_ty(bx, dest.llval, dest.align, r, source_align,
317                                 dest.layout, flags)
318             }
319             OperandValue::Ref(_, Some(_), _) => {
320                 bug!("cannot directly store unsized values");
321             }
322             OperandValue::Immediate(s) => {
323                 let val = base::from_immediate(bx, s);
324                 bx.store_with_flags(val, dest.llval, dest.align, flags);
325             }
326             OperandValue::Pair(a, b) => {
327                 let (a_scalar, b_scalar) = match dest.layout.abi {
328                     layout::Abi::ScalarPair(ref a, ref b) => (a, b),
329                     _ => bug!("store_with_flags: invalid ScalarPair layout: {:#?}", dest.layout)
330                 };
331                 let b_offset = a_scalar.value.size(bx).align_to(b_scalar.value.align(bx).abi);
332
333                 let llptr = bx.struct_gep(dest.llval, 0);
334                 let val = base::from_immediate(bx, a);
335                 let align = dest.align;
336                 bx.store_with_flags(val, llptr, align, flags);
337
338                 let llptr = bx.struct_gep(dest.llval, 1);
339                 let val = base::from_immediate(bx, b);
340                 let align = dest.align.restrict_for_offset(b_offset);
341                 bx.store_with_flags(val, llptr, align, flags);
342             }
343         }
344     }
345
346     pub fn store_unsized<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
347         self,
348         bx: &mut Bx,
349         indirect_dest: PlaceRef<'tcx, V>
350     ) {
351         debug!("OperandRef::store_unsized: operand={:?}, indirect_dest={:?}", self, indirect_dest);
352         let flags = MemFlags::empty();
353
354         // `indirect_dest` must have `*mut T` type. We extract `T` out of it.
355         let unsized_ty = indirect_dest.layout.ty.builtin_deref(true)
356             .unwrap_or_else(|| bug!("indirect_dest has non-pointer type: {:?}", indirect_dest)).ty;
357
358         let (llptr, llextra) =
359             if let OperandValue::Ref(llptr, Some(llextra), _) = self {
360                 (llptr, llextra)
361             } else {
362                 bug!("store_unsized called with a sized value")
363             };
364
365         // FIXME: choose an appropriate alignment, or use dynamic align somehow
366         let max_align = Align::from_bits(128).unwrap();
367         let min_align = Align::from_bits(8).unwrap();
368
369         // Allocate an appropriate region on the stack, and copy the value into it
370         let (llsize, _) = glue::size_and_align_of_dst(bx, unsized_ty, Some(llextra));
371         let lldst = bx.array_alloca(bx.cx().type_i8(), llsize, max_align);
372         bx.memcpy(lldst, max_align, llptr, min_align, llsize, flags);
373
374         // Store the allocated region and the extra to the indirect place.
375         let indirect_operand = OperandValue::Pair(lldst, llextra);
376         indirect_operand.store(bx, indirect_dest);
377     }
378 }
379
380 impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
381     fn maybe_codegen_consume_direct(
382         &mut self,
383         bx: &mut Bx,
384         place_ref: &mir::PlaceRef<'_, 'tcx>
385     ) -> Option<OperandRef<'tcx, Bx::Value>> {
386         debug!("maybe_codegen_consume_direct(place_ref={:?})", place_ref);
387
388         if let mir::PlaceBase::Local(index) = place_ref.base {
389             match self.locals[*index] {
390                 LocalRef::Operand(Some(mut o)) => {
391                     // Moves out of scalar and scalar pair fields are trivial.
392                     for elem in place_ref.projection.iter() {
393                         match elem {
394                             mir::ProjectionElem::Field(ref f, _) => {
395                                 o = o.extract_field(bx, f.index());
396                             }
397                             mir::ProjectionElem::Index(_) |
398                             mir::ProjectionElem::ConstantIndex { .. } => {
399                                 // ZSTs don't require any actual memory access.
400                                 // FIXME(eddyb) deduplicate this with the identical
401                                 // checks in `codegen_consume` and `extract_field`.
402                                 let elem = o.layout.field(bx.cx(), 0);
403                                 if elem.is_zst() {
404                                     o = OperandRef::new_zst(bx, elem);
405                                 } else {
406                                     return None;
407                                 }
408                             }
409                             _ => return None,
410                         }
411                     }
412
413                     Some(o)
414                 }
415                 LocalRef::Operand(None) => {
416                     bug!("use of {:?} before def", place_ref);
417                 }
418                 LocalRef::Place(..) | LocalRef::UnsizedPlace(..) => {
419                     // watch out for locals that do not have an
420                     // alloca; they are handled somewhat differently
421                     None
422                 }
423             }
424         } else {
425             None
426         }
427     }
428
429     pub fn codegen_consume(
430         &mut self,
431         bx: &mut Bx,
432         place_ref: &mir::PlaceRef<'_, 'tcx>
433     ) -> OperandRef<'tcx, Bx::Value> {
434         debug!("codegen_consume(place_ref={:?})", place_ref);
435
436         let ty = self.monomorphized_place_ty(place_ref);
437         let layout = bx.cx().layout_of(ty);
438
439         // ZSTs don't require any actual memory access.
440         if layout.is_zst() {
441             return OperandRef::new_zst(bx, layout);
442         }
443
444         if let Some(o) = self.maybe_codegen_consume_direct(bx, place_ref) {
445             return o;
446         }
447
448         // for most places, to consume them we just load them
449         // out from their home
450         let place = self.codegen_place(bx, place_ref);
451         bx.load_operand(place)
452     }
453
454     pub fn codegen_operand(
455         &mut self,
456         bx: &mut Bx,
457         operand: &mir::Operand<'tcx>
458     ) -> OperandRef<'tcx, Bx::Value> {
459         debug!("codegen_operand(operand={:?})", operand);
460
461         match *operand {
462             mir::Operand::Copy(ref place) |
463             mir::Operand::Move(ref place) => {
464                 self.codegen_consume(bx, &place.as_ref())
465             }
466
467             mir::Operand::Constant(ref constant) => {
468                 self.eval_mir_constant_to_operand(bx, constant)
469                     .unwrap_or_else(|err| {
470                         match err {
471                             // errored or at least linted
472                             ErrorHandled::Reported => {},
473                             ErrorHandled::TooGeneric => {
474                                 bug!("codgen encountered polymorphic constant")
475                             },
476                         }
477                         // Allow RalfJ to sleep soundly knowing that even refactorings that remove
478                         // the above error (or silence it under some conditions) will not cause UB
479                         bx.abort();
480                         // We've errored, so we don't have to produce working code.
481                         let ty = self.monomorphize(&constant.literal.ty);
482                         let layout = bx.cx().layout_of(ty);
483                         bx.load_operand(PlaceRef::new_sized(
484                             bx.cx().const_undef(bx.cx().type_ptr_to(bx.cx().backend_type(layout))),
485                             layout,
486                         ))
487                     })
488             }
489         }
490     }
491 }