]> git.lizzy.rs Git - rust.git/blob - src/librustc_codegen_ssa/mir/operand.rs
Rollup merge of #61499 - varkor:issue-53457, r=oli-obk
[rust.git] / src / librustc_codegen_ssa / mir / operand.rs
1 use rustc::mir::interpret::{ConstValue, ErrorHandled, Pointer, Scalar};
2 use rustc::mir;
3 use rustc::ty;
4 use rustc::ty::layout::{self, Align, LayoutOf, TyLayout, Size};
5
6 use crate::base;
7 use crate::MemFlags;
8 use crate::glue;
9
10 use crate::traits::*;
11
12 use std::fmt;
13
14 use super::{FunctionCx, LocalRef};
15 use super::place::PlaceRef;
16
17 /// The representation of a Rust value. The enum variant is in fact
18 /// uniquely determined by the value's type, but is kept as a
19 /// safety check.
20 #[derive(Copy, Clone, Debug)]
21 pub enum OperandValue<V> {
22     /// A reference to the actual operand. The data is guaranteed
23     /// to be valid for the operand's lifetime.
24     /// The second value, if any, is the extra data (vtable or length)
25     /// which indicates that it refers to an unsized rvalue.
26     Ref(V, Option<V>, Align),
27     /// A single LLVM value.
28     Immediate(V),
29     /// A pair of immediate LLVM values. Used by fat pointers too.
30     Pair(V, V)
31 }
32
33 /// An `OperandRef` is an "SSA" reference to a Rust value, along with
34 /// its type.
35 ///
36 /// NOTE: unless you know a value's type exactly, you should not
37 /// generate LLVM opcodes acting on it and instead act via methods,
38 /// to avoid nasty edge cases. In particular, using `Builder::store`
39 /// directly is sure to cause problems -- use `OperandRef::store`
40 /// instead.
41 #[derive(Copy, Clone)]
42 pub struct OperandRef<'tcx, V> {
43     // The value.
44     pub val: OperandValue<V>,
45
46     // The layout of value, based on its Rust type.
47     pub layout: TyLayout<'tcx>,
48 }
49
50 impl<V: CodegenObject> fmt::Debug for OperandRef<'tcx, V> {
51     fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52         write!(f, "OperandRef({:?} @ {:?})", self.val, self.layout)
53     }
54 }
55
56 impl<'a, 'tcx: 'a, V: CodegenObject> OperandRef<'tcx, V> {
57     pub fn new_zst<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
58         bx: &mut Bx,
59         layout: TyLayout<'tcx>
60     ) -> OperandRef<'tcx, V> {
61         assert!(layout.is_zst());
62         OperandRef {
63             val: OperandValue::Immediate(bx.const_undef(bx.immediate_backend_type(layout))),
64             layout
65         }
66     }
67
68     pub fn from_const<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
69         bx: &mut Bx,
70         val: &'tcx ty::Const<'tcx>
71     ) -> Self {
72         let layout = bx.layout_of(val.ty);
73
74         if layout.is_zst() {
75             return OperandRef::new_zst(bx, layout);
76         }
77
78         let val = match val.val {
79             ConstValue::Unevaluated(..) => bug!("unevaluated constant in `OperandRef::from_const`"),
80             ConstValue::Param(_) => bug!("encountered a ConstValue::Param in codegen"),
81             ConstValue::Infer(_) => bug!("encountered a ConstValue::Infer in codegen"),
82             ConstValue::Placeholder(_) => bug!("encountered a ConstValue::Placeholder in codegen"),
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(ptr, alloc) => {
113                 return bx.load_operand(bx.from_const_alloc(layout, alloc, ptr.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: 'a, 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     pub fn store_unsized<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
346         self,
347         bx: &mut Bx,
348         indirect_dest: PlaceRef<'tcx, V>
349     ) {
350         debug!("OperandRef::store_unsized: operand={:?}, indirect_dest={:?}", self, indirect_dest);
351         let flags = MemFlags::empty();
352
353         // `indirect_dest` must have `*mut T` type. We extract `T` out of it.
354         let unsized_ty = indirect_dest.layout.ty.builtin_deref(true)
355             .unwrap_or_else(|| bug!("indirect_dest has non-pointer type: {:?}", indirect_dest)).ty;
356
357         let (llptr, llextra) =
358             if let OperandValue::Ref(llptr, Some(llextra), _) = self {
359                 (llptr, llextra)
360             } else {
361                 bug!("store_unsized called with a sized value")
362             };
363
364         // FIXME: choose an appropriate alignment, or use dynamic align somehow
365         let max_align = Align::from_bits(128).unwrap();
366         let min_align = Align::from_bits(8).unwrap();
367
368         // Allocate an appropriate region on the stack, and copy the value into it
369         let (llsize, _) = glue::size_and_align_of_dst(bx, unsized_ty, Some(llextra));
370         let lldst = bx.array_alloca(bx.cx().type_i8(), llsize, "unsized_tmp", max_align);
371         bx.memcpy(lldst, max_align, llptr, min_align, llsize, flags);
372
373         // Store the allocated region and the extra to the indirect place.
374         let indirect_operand = OperandValue::Pair(lldst, llextra);
375         indirect_operand.store(bx, indirect_dest);
376     }
377 }
378
379 impl<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
380     fn maybe_codegen_consume_direct(
381         &mut self,
382         bx: &mut Bx,
383         place: &mir::Place<'tcx>
384     ) -> Option<OperandRef<'tcx, Bx::Value>> {
385         debug!("maybe_codegen_consume_direct(place={:?})", place);
386
387         place.iterate(|place_base, place_projection| {
388             if let mir::PlaceBase::Local(index) = place_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 proj in place_projection {
393                             match proj.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);
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
430     pub fn codegen_consume(
431         &mut self,
432         bx: &mut Bx,
433         place: &mir::Place<'tcx>
434     ) -> OperandRef<'tcx, Bx::Value> {
435         debug!("codegen_consume(place={:?})", place);
436
437         let ty = self.monomorphized_place_ty(place);
438         let layout = bx.cx().layout_of(ty);
439
440         // ZSTs don't require any actual memory access.
441         if layout.is_zst() {
442             return OperandRef::new_zst(bx, layout);
443         }
444
445         if let Some(o) = self.maybe_codegen_consume_direct(bx, place) {
446             return o;
447         }
448
449         // for most places, to consume them we just load them
450         // out from their home
451         let place = self.codegen_place(bx, place);
452         bx.load_operand(place)
453     }
454
455     pub fn codegen_operand(
456         &mut self,
457         bx: &mut Bx,
458         operand: &mir::Operand<'tcx>
459     ) -> OperandRef<'tcx, Bx::Value> {
460         debug!("codegen_operand(operand={:?})", operand);
461
462         match *operand {
463             mir::Operand::Copy(ref place) |
464             mir::Operand::Move(ref place) => {
465                 self.codegen_consume(bx, place)
466             }
467
468             mir::Operand::Constant(ref constant) => {
469                 let ty = self.monomorphize(&constant.ty);
470                 self.eval_mir_constant(constant)
471                     .map(|c| OperandRef::from_const(bx, c))
472                     .unwrap_or_else(|err| {
473                         match err {
474                             // errored or at least linted
475                             ErrorHandled::Reported => {},
476                             ErrorHandled::TooGeneric => {
477                                 bug!("codgen encountered polymorphic constant")
478                             },
479                         }
480                         // Allow RalfJ to sleep soundly knowing that even refactorings that remove
481                         // the above error (or silence it under some conditions) will not cause UB
482                         bx.abort();
483                         // We've errored, so we don't have to produce working code.
484                         let layout = bx.cx().layout_of(ty);
485                         bx.load_operand(PlaceRef::new_sized(
486                             bx.cx().const_undef(bx.cx().type_ptr_to(bx.cx().backend_type(layout))),
487                             layout,
488                             layout.align.abi,
489                         ))
490                     })
491             }
492         }
493     }
494 }