rustc_codegen_ssa/mir/operand.rs
1use std::fmt;
2
3use itertools::Either;
4use rustc_abi as abi;
5use rustc_abi::{
6 Align, BackendRepr, FIRST_VARIANT, FieldIdx, Primitive, Size, TagEncoding, VariantIdx, Variants,
7};
8use rustc_middle::mir::interpret::{Pointer, Scalar, alloc_range};
9use rustc_middle::mir::{self, ConstValue};
10use rustc_middle::ty::Ty;
11use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
12use rustc_middle::{bug, span_bug};
13use rustc_session::config::OptLevel;
14use tracing::{debug, instrument};
15
16use super::place::{PlaceRef, PlaceValue};
17use super::rvalue::transmute_scalar;
18use super::{FunctionCx, LocalRef};
19use crate::MemFlags;
20use crate::common::IntPredicate;
21use crate::traits::*;
22
23/// The representation of a Rust value. The enum variant is in fact
24/// uniquely determined by the value's type, but is kept as a
25/// safety check.
26#[derive(Copy, Clone, Debug)]
27pub enum OperandValue<V> {
28 /// A reference to the actual operand. The data is guaranteed
29 /// to be valid for the operand's lifetime.
30 /// The second value, if any, is the extra data (vtable or length)
31 /// which indicates that it refers to an unsized rvalue.
32 ///
33 /// An `OperandValue` *must* be this variant for any type for which
34 /// [`LayoutTypeCodegenMethods::is_backend_ref`] returns `true`.
35 /// (That basically amounts to "isn't one of the other variants".)
36 ///
37 /// This holds a [`PlaceValue`] (like a [`PlaceRef`] does) with a pointer
38 /// to the location holding the value. The type behind that pointer is the
39 /// one returned by [`LayoutTypeCodegenMethods::backend_type`].
40 Ref(PlaceValue<V>),
41 /// A single LLVM immediate value.
42 ///
43 /// An `OperandValue` *must* be this variant for any type for which
44 /// [`LayoutTypeCodegenMethods::is_backend_immediate`] returns `true`.
45 /// The backend value in this variant must be the *immediate* backend type,
46 /// as returned by [`LayoutTypeCodegenMethods::immediate_backend_type`].
47 Immediate(V),
48 /// A pair of immediate LLVM values. Used by wide pointers too.
49 ///
50 /// # Invariants
51 /// - For `Pair(a, b)`, `a` is always at offset 0, but may have `FieldIdx(1..)`
52 /// - `b` is not at offset 0, because `V` is not a 1ZST type.
53 /// - `a` and `b` will have a different FieldIdx, but otherwise `b`'s may be lower
54 /// or they may not be adjacent, due to arbitrary numbers of 1ZST fields that
55 /// will not affect the shape of the data which determines if `Pair` will be used.
56 /// - An `OperandValue` *must* be this variant for any type for which
57 /// [`LayoutTypeCodegenMethods::is_backend_scalar_pair`] returns `true`.
58 /// - The backend values in this variant must be the *immediate* backend types,
59 /// as returned by [`LayoutTypeCodegenMethods::scalar_pair_element_backend_type`]
60 /// with `immediate: true`.
61 Pair(V, V),
62 /// A value taking no bytes, and which therefore needs no LLVM value at all.
63 ///
64 /// If you ever need a `V` to pass to something, get a fresh poison value
65 /// from [`ConstCodegenMethods::const_poison`].
66 ///
67 /// An `OperandValue` *must* be this variant for any type for which
68 /// `is_zst` on its `Layout` returns `true`. Note however that
69 /// these values can still require alignment.
70 ZeroSized,
71}
72
73impl<V: CodegenObject> OperandValue<V> {
74 /// Treat this value as a pointer and return the data pointer and
75 /// optional metadata as backend values.
76 ///
77 /// If you're making a place, use [`Self::deref`] instead.
78 pub(crate) fn pointer_parts(self) -> (V, Option<V>) {
79 match self {
80 OperandValue::Immediate(llptr) => (llptr, None),
81 OperandValue::Pair(llptr, llextra) => (llptr, Some(llextra)),
82 _ => bug!("OperandValue cannot be a pointer: {self:?}"),
83 }
84 }
85
86 /// Treat this value as a pointer and return the place to which it points.
87 ///
88 /// The pointer immediate doesn't inherently know its alignment,
89 /// so you need to pass it in. If you want to get it from a type's ABI
90 /// alignment, then maybe you want [`OperandRef::deref`] instead.
91 ///
92 /// This is the inverse of [`PlaceValue::address`].
93 pub(crate) fn deref(self, align: Align) -> PlaceValue<V> {
94 let (llval, llextra) = self.pointer_parts();
95 PlaceValue { llval, llextra, align }
96 }
97
98 pub(crate) fn is_expected_variant_for_type<'tcx, Cx: LayoutTypeCodegenMethods<'tcx>>(
99 &self,
100 cx: &Cx,
101 ty: TyAndLayout<'tcx>,
102 ) -> bool {
103 match self {
104 OperandValue::ZeroSized => ty.is_zst(),
105 OperandValue::Immediate(_) => cx.is_backend_immediate(ty),
106 OperandValue::Pair(_, _) => cx.is_backend_scalar_pair(ty),
107 OperandValue::Ref(_) => cx.is_backend_ref(ty),
108 }
109 }
110}
111
112/// An `OperandRef` is an "SSA" reference to a Rust value, along with
113/// its type.
114///
115/// NOTE: unless you know a value's type exactly, you should not
116/// generate LLVM opcodes acting on it and instead act via methods,
117/// to avoid nasty edge cases. In particular, using `Builder::store`
118/// directly is sure to cause problems -- use `OperandRef::store`
119/// instead.
120#[derive(Copy, Clone)]
121pub struct OperandRef<'tcx, V> {
122 /// The value.
123 pub val: OperandValue<V>,
124
125 /// The layout of value, based on its Rust type.
126 pub layout: TyAndLayout<'tcx>,
127}
128
129impl<V: CodegenObject> fmt::Debug for OperandRef<'_, V> {
130 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
131 write!(f, "OperandRef({:?} @ {:?})", self.val, self.layout)
132 }
133}
134
135impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
136 pub fn zero_sized(layout: TyAndLayout<'tcx>) -> OperandRef<'tcx, V> {
137 assert!(layout.is_zst());
138 OperandRef { val: OperandValue::ZeroSized, layout }
139 }
140
141 pub(crate) fn from_const<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
142 bx: &mut Bx,
143 val: mir::ConstValue,
144 ty: Ty<'tcx>,
145 ) -> Self {
146 let layout = bx.layout_of(ty);
147
148 let val = match val {
149 ConstValue::Scalar(x) => {
150 let BackendRepr::Scalar(scalar) = layout.backend_repr else {
151 bug!("from_const: invalid ByVal layout: {:#?}", layout);
152 };
153 let llval = bx.scalar_to_backend(x, scalar, bx.immediate_backend_type(layout));
154 OperandValue::Immediate(llval)
155 }
156 ConstValue::ZeroSized => return OperandRef::zero_sized(layout),
157 ConstValue::Slice { alloc_id, meta } => {
158 let BackendRepr::ScalarPair(a_scalar, _) = layout.backend_repr else {
159 bug!("from_const: invalid ScalarPair layout: {:#?}", layout);
160 };
161 let a = Scalar::from_pointer(Pointer::new(alloc_id.into(), Size::ZERO), &bx.tcx());
162 let a_llval = bx.scalar_to_backend(
163 a,
164 a_scalar,
165 bx.scalar_pair_element_backend_type(layout, 0, true),
166 );
167 let b_llval = bx.const_usize(meta);
168 OperandValue::Pair(a_llval, b_llval)
169 }
170 ConstValue::Indirect { alloc_id, offset } => {
171 let alloc = bx.tcx().global_alloc(alloc_id).unwrap_memory();
172 return Self::from_const_alloc(bx, layout, alloc, offset);
173 }
174 };
175
176 OperandRef { val, layout }
177 }
178
179 fn from_const_alloc<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
180 bx: &mut Bx,
181 layout: TyAndLayout<'tcx>,
182 alloc: rustc_middle::mir::interpret::ConstAllocation<'tcx>,
183 offset: Size,
184 ) -> Self {
185 let alloc_align = alloc.inner().align;
186 assert!(alloc_align >= layout.align.abi, "{alloc_align:?} < {:?}", layout.align.abi);
187
188 let read_scalar = |start, size, s: abi::Scalar, ty| {
189 match alloc.0.read_scalar(
190 bx,
191 alloc_range(start, size),
192 /*read_provenance*/ matches!(s.primitive(), abi::Primitive::Pointer(_)),
193 ) {
194 Ok(val) => bx.scalar_to_backend(val, s, ty),
195 Err(_) => bx.const_poison(ty),
196 }
197 };
198
199 // It may seem like all types with `Scalar` or `ScalarPair` ABI are fair game at this point.
200 // However, `MaybeUninit<u64>` is considered a `Scalar` as far as its layout is concerned --
201 // and yet cannot be represented by an interpreter `Scalar`, since we have to handle the
202 // case where some of the bytes are initialized and others are not. So, we need an extra
203 // check that walks over the type of `mplace` to make sure it is truly correct to treat this
204 // like a `Scalar` (or `ScalarPair`).
205 match layout.backend_repr {
206 BackendRepr::Scalar(s @ abi::Scalar::Initialized { .. }) => {
207 let size = s.size(bx);
208 assert_eq!(size, layout.size, "abi::Scalar size does not match layout size");
209 let val = read_scalar(offset, size, s, bx.immediate_backend_type(layout));
210 OperandRef { val: OperandValue::Immediate(val), layout }
211 }
212 BackendRepr::ScalarPair(
213 a @ abi::Scalar::Initialized { .. },
214 b @ abi::Scalar::Initialized { .. },
215 ) => {
216 let (a_size, b_size) = (a.size(bx), b.size(bx));
217 let b_offset = (offset + a_size).align_to(b.align(bx).abi);
218 assert!(b_offset.bytes() > 0);
219 let a_val = read_scalar(
220 offset,
221 a_size,
222 a,
223 bx.scalar_pair_element_backend_type(layout, 0, true),
224 );
225 let b_val = read_scalar(
226 b_offset,
227 b_size,
228 b,
229 bx.scalar_pair_element_backend_type(layout, 1, true),
230 );
231 OperandRef { val: OperandValue::Pair(a_val, b_val), layout }
232 }
233 _ if layout.is_zst() => OperandRef::zero_sized(layout),
234 _ => {
235 // Neither a scalar nor scalar pair. Load from a place
236 // FIXME: should we cache `const_data_from_alloc` to avoid repeating this for the
237 // same `ConstAllocation`?
238 let init = bx.const_data_from_alloc(alloc);
239 let base_addr = bx.static_addr_of(init, alloc_align, None);
240
241 let llval = bx.const_ptr_byte_offset(base_addr, offset);
242 bx.load_operand(PlaceRef::new_sized(llval, layout))
243 }
244 }
245 }
246
247 /// Asserts that this operand refers to a scalar and returns
248 /// a reference to its value.
249 pub fn immediate(self) -> V {
250 match self.val {
251 OperandValue::Immediate(s) => s,
252 _ => bug!("not immediate: {:?}", self),
253 }
254 }
255
256 /// Asserts that this operand is a pointer (or reference) and returns
257 /// the place to which it points. (This requires no code to be emitted
258 /// as we represent places using the pointer to the place.)
259 ///
260 /// This uses [`Ty::builtin_deref`] to include the type of the place and
261 /// assumes the place is aligned to the pointee's usual ABI alignment.
262 ///
263 /// If you don't need the type, see [`OperandValue::pointer_parts`]
264 /// or [`OperandValue::deref`].
265 pub fn deref<Cx: CodegenMethods<'tcx>>(self, cx: &Cx) -> PlaceRef<'tcx, V> {
266 if self.layout.ty.is_box() {
267 // Derefer should have removed all Box derefs
268 bug!("dereferencing {:?} in codegen", self.layout.ty);
269 }
270
271 let projected_ty = self
272 .layout
273 .ty
274 .builtin_deref(true)
275 .unwrap_or_else(|| bug!("deref of non-pointer {:?}", self));
276
277 let layout = cx.layout_of(projected_ty);
278 self.val.deref(layout.align.abi).with_type(layout)
279 }
280
281 /// If this operand is a `Pair`, we return an aggregate with the two values.
282 /// For other cases, see `immediate`.
283 pub fn immediate_or_packed_pair<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
284 self,
285 bx: &mut Bx,
286 ) -> V {
287 if let OperandValue::Pair(a, b) = self.val {
288 let llty = bx.cx().immediate_backend_type(self.layout);
289 debug!("Operand::immediate_or_packed_pair: packing {:?} into {:?}", self, llty);
290 // Reconstruct the immediate aggregate.
291 let mut llpair = bx.cx().const_poison(llty);
292 llpair = bx.insert_value(llpair, a, 0);
293 llpair = bx.insert_value(llpair, b, 1);
294 llpair
295 } else {
296 self.immediate()
297 }
298 }
299
300 /// If the type is a pair, we return a `Pair`, otherwise, an `Immediate`.
301 pub fn from_immediate_or_packed_pair<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
302 bx: &mut Bx,
303 llval: V,
304 layout: TyAndLayout<'tcx>,
305 ) -> Self {
306 let val = if let BackendRepr::ScalarPair(..) = layout.backend_repr {
307 debug!("Operand::from_immediate_or_packed_pair: unpacking {:?} @ {:?}", llval, layout);
308
309 // Deconstruct the immediate aggregate.
310 let a_llval = bx.extract_value(llval, 0);
311 let b_llval = bx.extract_value(llval, 1);
312 OperandValue::Pair(a_llval, b_llval)
313 } else {
314 OperandValue::Immediate(llval)
315 };
316 OperandRef { val, layout }
317 }
318
319 pub(crate) fn extract_field<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
320 &self,
321 fx: &mut FunctionCx<'a, 'tcx, Bx>,
322 bx: &mut Bx,
323 i: usize,
324 ) -> Self {
325 let field = self.layout.field(bx.cx(), i);
326 let offset = self.layout.fields.offset(i);
327
328 if !bx.is_backend_ref(self.layout) && bx.is_backend_ref(field) {
329 // Part of https://github.com/rust-lang/compiler-team/issues/838
330 span_bug!(
331 fx.mir.span,
332 "Non-ref type {self:?} cannot project to ref field type {field:?}",
333 );
334 }
335
336 let val = if field.is_zst() {
337 OperandValue::ZeroSized
338 } else if field.size == self.layout.size {
339 assert_eq!(offset.bytes(), 0);
340 fx.codegen_transmute_operand(bx, *self, field)
341 } else {
342 let (in_scalar, imm) = match (self.val, self.layout.backend_repr) {
343 // Extract a scalar component from a pair.
344 (OperandValue::Pair(a_llval, b_llval), BackendRepr::ScalarPair(a, b)) => {
345 if offset.bytes() == 0 {
346 assert_eq!(field.size, a.size(bx.cx()));
347 (Some(a), a_llval)
348 } else {
349 assert_eq!(offset, a.size(bx.cx()).align_to(b.align(bx.cx()).abi));
350 assert_eq!(field.size, b.size(bx.cx()));
351 (Some(b), b_llval)
352 }
353 }
354
355 _ => {
356 span_bug!(fx.mir.span, "OperandRef::extract_field({:?}): not applicable", self)
357 }
358 };
359 OperandValue::Immediate(match field.backend_repr {
360 BackendRepr::SimdVector { .. } => imm,
361 BackendRepr::Scalar(out_scalar) => {
362 let Some(in_scalar) = in_scalar else {
363 span_bug!(
364 fx.mir.span,
365 "OperandRef::extract_field({:?}): missing input scalar for output scalar",
366 self
367 )
368 };
369 if in_scalar != out_scalar {
370 // If the backend and backend_immediate types might differ,
371 // flip back to the backend type then to the new immediate.
372 // This avoids nop truncations, but still handles things like
373 // Bools in union fields needs to be truncated.
374 let backend = bx.from_immediate(imm);
375 bx.to_immediate_scalar(backend, out_scalar)
376 } else {
377 imm
378 }
379 }
380 BackendRepr::ScalarPair(_, _) | BackendRepr::Memory { .. } => bug!(),
381 })
382 };
383
384 OperandRef { val, layout: field }
385 }
386
387 /// Obtain the actual discriminant of a value.
388 #[instrument(level = "trace", skip(fx, bx))]
389 pub fn codegen_get_discr<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
390 self,
391 fx: &mut FunctionCx<'a, 'tcx, Bx>,
392 bx: &mut Bx,
393 cast_to: Ty<'tcx>,
394 ) -> V {
395 let dl = &bx.tcx().data_layout;
396 let cast_to_layout = bx.cx().layout_of(cast_to);
397 let cast_to = bx.cx().immediate_backend_type(cast_to_layout);
398
399 // We check uninhabitedness separately because a type like
400 // `enum Foo { Bar(i32, !) }` is still reported as `Variants::Single`,
401 // *not* as `Variants::Empty`.
402 if self.layout.is_uninhabited() {
403 return bx.cx().const_poison(cast_to);
404 }
405
406 let (tag_scalar, tag_encoding, tag_field) = match self.layout.variants {
407 Variants::Empty => unreachable!("we already handled uninhabited types"),
408 Variants::Single { index } => {
409 let discr_val =
410 if let Some(discr) = self.layout.ty.discriminant_for_variant(bx.tcx(), index) {
411 discr.val
412 } else {
413 // This arm is for types which are neither enums nor coroutines,
414 // and thus for which the only possible "variant" should be the first one.
415 assert_eq!(index, FIRST_VARIANT);
416 // There's thus no actual discriminant to return, so we return
417 // what it would have been if this was a single-variant enum.
418 0
419 };
420 return bx.cx().const_uint_big(cast_to, discr_val);
421 }
422 Variants::Multiple { tag, ref tag_encoding, tag_field, .. } => {
423 (tag, tag_encoding, tag_field)
424 }
425 };
426
427 // Read the tag/niche-encoded discriminant from memory.
428 let tag_op = match self.val {
429 OperandValue::ZeroSized => bug!(),
430 OperandValue::Immediate(_) | OperandValue::Pair(_, _) => {
431 self.extract_field(fx, bx, tag_field.as_usize())
432 }
433 OperandValue::Ref(place) => {
434 let tag = place.with_type(self.layout).project_field(bx, tag_field.as_usize());
435 bx.load_operand(tag)
436 }
437 };
438 let tag_imm = tag_op.immediate();
439
440 // Decode the discriminant (specifically if it's niche-encoded).
441 match *tag_encoding {
442 TagEncoding::Direct => {
443 let signed = match tag_scalar.primitive() {
444 // We use `i1` for bytes that are always `0` or `1`,
445 // e.g., `#[repr(i8)] enum E { A, B }`, but we can't
446 // let LLVM interpret the `i1` as signed, because
447 // then `i1 1` (i.e., `E::B`) is effectively `i8 -1`.
448 Primitive::Int(_, signed) => !tag_scalar.is_bool() && signed,
449 _ => false,
450 };
451 bx.intcast(tag_imm, cast_to, signed)
452 }
453 TagEncoding::Niche { untagged_variant, ref niche_variants, niche_start } => {
454 // Cast to an integer so we don't have to treat a pointer as a
455 // special case.
456 let (tag, tag_llty) = match tag_scalar.primitive() {
457 // FIXME(erikdesjardins): handle non-default addrspace ptr sizes
458 Primitive::Pointer(_) => {
459 let t = bx.type_from_integer(dl.ptr_sized_integer());
460 let tag = bx.ptrtoint(tag_imm, t);
461 (tag, t)
462 }
463 _ => (tag_imm, bx.cx().immediate_backend_type(tag_op.layout)),
464 };
465
466 // `layout_sanity_check` ensures that we only get here for cases where the discriminant
467 // value and the variant index match, since that's all `Niche` can encode.
468
469 let relative_max = niche_variants.end().as_u32() - niche_variants.start().as_u32();
470 let niche_start_const = bx.cx().const_uint_big(tag_llty, niche_start);
471
472 // We have a subrange `niche_start..=niche_end` inside `range`.
473 // If the value of the tag is inside this subrange, it's a
474 // "niche value", an increment of the discriminant. Otherwise it
475 // indicates the untagged variant.
476 // A general algorithm to extract the discriminant from the tag
477 // is:
478 // relative_tag = tag - niche_start
479 // is_niche = relative_tag <= (ule) relative_max
480 // discr = if is_niche {
481 // cast(relative_tag) + niche_variants.start()
482 // } else {
483 // untagged_variant
484 // }
485 // However, we will likely be able to emit simpler code.
486 let (is_niche, tagged_discr, delta) = if relative_max == 0 {
487 // Best case scenario: only one tagged variant. This will
488 // likely become just a comparison and a jump.
489 // The algorithm is:
490 // is_niche = tag == niche_start
491 // discr = if is_niche {
492 // niche_start
493 // } else {
494 // untagged_variant
495 // }
496 let is_niche = bx.icmp(IntPredicate::IntEQ, tag, niche_start_const);
497 let tagged_discr =
498 bx.cx().const_uint(cast_to, niche_variants.start().as_u32() as u64);
499 (is_niche, tagged_discr, 0)
500 } else {
501 // Thanks to parameter attributes and load metadata, LLVM already knows
502 // the general valid range of the tag. It's possible, though, for there
503 // to be an impossible value *in the middle*, which those ranges don't
504 // communicate, so it's worth an `assume` to let the optimizer know.
505 // Most importantly, this means when optimizing a variant test like
506 // `SELECT(is_niche, complex, CONST) == CONST` it's ok to simplify that
507 // to `!is_niche` because the `complex` part can't possibly match.
508 //
509 // This was previously asserted on `tagged_discr` below, where the
510 // impossible value is more obvious, but that caused an intermediate
511 // value to become multi-use and thus not optimize, so instead this
512 // assumes on the original input which is always multi-use. See
513 // <https://github.com/llvm/llvm-project/issues/134024#issuecomment-3131782555>
514 //
515 // FIXME: If we ever get range assume operand bundles in LLVM (so we
516 // don't need the `icmp`s in the instruction stream any more), it
517 // might be worth moving this back to being on the switch argument
518 // where it's more obviously applicable.
519 if niche_variants.contains(&untagged_variant)
520 && bx.cx().sess().opts.optimize != OptLevel::No
521 {
522 let impossible = niche_start
523 .wrapping_add(u128::from(untagged_variant.as_u32()))
524 .wrapping_sub(u128::from(niche_variants.start().as_u32()));
525 let impossible = bx.cx().const_uint_big(tag_llty, impossible);
526 let ne = bx.icmp(IntPredicate::IntNE, tag, impossible);
527 bx.assume(ne);
528 }
529
530 // With multiple niched variants we'll have to actually compute
531 // the variant index from the stored tag.
532 //
533 // However, there's still one small optimization we can often do for
534 // determining *whether* a tag value is a natural value or a niched
535 // variant. The general algorithm involves a subtraction that often
536 // wraps in practice, making it tricky to analyse. However, in cases
537 // where there are few enough possible values of the tag that it doesn't
538 // need to wrap around, we can instead just look for the contiguous
539 // tag values on the end of the range with a single comparison.
540 //
541 // For example, take the type `enum Demo { A, B, Untagged(bool) }`.
542 // The `bool` is {0, 1}, and the two other variants are given the
543 // tags {2, 3} respectively. That means the `tag_range` is
544 // `[0, 3]`, which doesn't wrap as unsigned (nor as signed), so
545 // we can test for the niched variants with just `>= 2`.
546 //
547 // That means we're looking either for the niche values *above*
548 // the natural values of the untagged variant:
549 //
550 // niche_start niche_end
551 // | |
552 // v v
553 // MIN -------------+---------------------------+---------- MAX
554 // ^ | is niche |
555 // | +---------------------------+
556 // | |
557 // tag_range.start tag_range.end
558 //
559 // Or *below* the natural values:
560 //
561 // niche_start niche_end
562 // | |
563 // v v
564 // MIN ----+-----------------------+---------------------- MAX
565 // | is niche | ^
566 // +-----------------------+ |
567 // | |
568 // tag_range.start tag_range.end
569 //
570 // With those two options and having the flexibility to choose
571 // between a signed or unsigned comparison on the tag, that
572 // covers most realistic scenarios. The tests have a (contrived)
573 // example of a 1-byte enum with over 128 niched variants which
574 // wraps both as signed as unsigned, though, and for something
575 // like that we're stuck with the general algorithm.
576
577 let tag_range = tag_scalar.valid_range(&dl);
578 let tag_size = tag_scalar.size(&dl);
579 let niche_end = u128::from(relative_max).wrapping_add(niche_start);
580 let niche_end = tag_size.truncate(niche_end);
581
582 let relative_discr = bx.sub(tag, niche_start_const);
583 let cast_tag = bx.intcast(relative_discr, cast_to, false);
584 let is_niche = if tag_range.no_unsigned_wraparound(tag_size) == Ok(true) {
585 if niche_start == tag_range.start {
586 let niche_end_const = bx.cx().const_uint_big(tag_llty, niche_end);
587 bx.icmp(IntPredicate::IntULE, tag, niche_end_const)
588 } else {
589 assert_eq!(niche_end, tag_range.end);
590 bx.icmp(IntPredicate::IntUGE, tag, niche_start_const)
591 }
592 } else if tag_range.no_signed_wraparound(tag_size) == Ok(true) {
593 if niche_start == tag_range.start {
594 let niche_end_const = bx.cx().const_uint_big(tag_llty, niche_end);
595 bx.icmp(IntPredicate::IntSLE, tag, niche_end_const)
596 } else {
597 assert_eq!(niche_end, tag_range.end);
598 bx.icmp(IntPredicate::IntSGE, tag, niche_start_const)
599 }
600 } else {
601 bx.icmp(
602 IntPredicate::IntULE,
603 relative_discr,
604 bx.cx().const_uint(tag_llty, relative_max as u64),
605 )
606 };
607
608 (is_niche, cast_tag, niche_variants.start().as_u32() as u128)
609 };
610
611 let tagged_discr = if delta == 0 {
612 tagged_discr
613 } else {
614 bx.add(tagged_discr, bx.cx().const_uint_big(cast_to, delta))
615 };
616
617 let untagged_variant_const =
618 bx.cx().const_uint(cast_to, u64::from(untagged_variant.as_u32()));
619
620 let discr = bx.select(is_niche, tagged_discr, untagged_variant_const);
621
622 // In principle we could insert assumes on the possible range of `discr`, but
623 // currently in LLVM this isn't worth it because the original `tag` will
624 // have either a `range` parameter attribute or `!range` metadata,
625 // or come from a `transmute` that already `assume`d it.
626
627 discr
628 }
629 }
630 }
631}
632
633/// Each of these variants starts out as `Either::Right` when it's uninitialized,
634/// then setting the field changes that to `Either::Left` with the backend value.
635#[derive(Debug, Copy, Clone)]
636enum OperandValueBuilder<V> {
637 ZeroSized,
638 Immediate(Either<V, abi::Scalar>),
639 Pair(Either<V, abi::Scalar>, Either<V, abi::Scalar>),
640 /// `repr(simd)` types need special handling because they each have a non-empty
641 /// array field (which uses [`OperandValue::Ref`]) despite the SIMD type itself
642 /// using [`OperandValue::Immediate`] which for any other kind of type would
643 /// mean that its one non-ZST field would also be [`OperandValue::Immediate`].
644 Vector(Either<V, ()>),
645}
646
647/// Allows building up an `OperandRef` by setting fields one at a time.
648#[derive(Debug, Copy, Clone)]
649pub(super) struct OperandRefBuilder<'tcx, V> {
650 val: OperandValueBuilder<V>,
651 layout: TyAndLayout<'tcx>,
652}
653
654impl<'a, 'tcx, V: CodegenObject> OperandRefBuilder<'tcx, V> {
655 /// Creates an uninitialized builder for an instance of the `layout`.
656 ///
657 /// ICEs for [`BackendRepr::Memory`] types (other than ZSTs), which should
658 /// be built up inside a [`PlaceRef`] instead as they need an allocated place
659 /// into which to write the values of the fields.
660 pub(super) fn new(layout: TyAndLayout<'tcx>) -> Self {
661 let val = match layout.backend_repr {
662 BackendRepr::Memory { .. } if layout.is_zst() => OperandValueBuilder::ZeroSized,
663 BackendRepr::Scalar(s) => OperandValueBuilder::Immediate(Either::Right(s)),
664 BackendRepr::ScalarPair(a, b) => {
665 OperandValueBuilder::Pair(Either::Right(a), Either::Right(b))
666 }
667 BackendRepr::SimdVector { .. } => OperandValueBuilder::Vector(Either::Right(())),
668 BackendRepr::Memory { .. } => {
669 bug!("Cannot use non-ZST Memory-ABI type in operand builder: {layout:?}");
670 }
671 };
672 OperandRefBuilder { val, layout }
673 }
674
675 pub(super) fn insert_field<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
676 &mut self,
677 bx: &mut Bx,
678 variant: VariantIdx,
679 field: FieldIdx,
680 field_operand: OperandRef<'tcx, V>,
681 ) {
682 if let OperandValue::ZeroSized = field_operand.val {
683 // A ZST never adds any state, so just ignore it.
684 // This special-casing is worth it because of things like
685 // `Result<!, !>` where `Ok(never)` is legal to write,
686 // but the type shows as FieldShape::Primitive so we can't
687 // actually look at the layout for the field being set.
688 return;
689 }
690
691 let is_zero_offset = if let abi::FieldsShape::Primitive = self.layout.fields {
692 // The other branch looking at field layouts ICEs for primitives,
693 // so we need to handle them separately.
694 // Because we handled ZSTs above (like the metadata in a thin pointer),
695 // the only possibility is that we're setting the one-and-only field.
696 assert!(!self.layout.is_zst());
697 assert_eq!(variant, FIRST_VARIANT);
698 assert_eq!(field, FieldIdx::ZERO);
699 true
700 } else {
701 let variant_layout = self.layout.for_variant(bx.cx(), variant);
702 let field_offset = variant_layout.fields.offset(field.as_usize());
703 field_offset == Size::ZERO
704 };
705
706 let mut update = |tgt: &mut Either<V, abi::Scalar>, src, from_scalar| {
707 let to_scalar = tgt.unwrap_right();
708 // We transmute here (rather than just `from_immediate`) because in
709 // `Result<usize, *const ()>` the field of the `Ok` is an integer,
710 // but the corresponding scalar in the enum is a pointer.
711 let imm = transmute_scalar(bx, src, from_scalar, to_scalar);
712 *tgt = Either::Left(imm);
713 };
714
715 match (field_operand.val, field_operand.layout.backend_repr) {
716 (OperandValue::ZeroSized, _) => unreachable!("Handled above"),
717 (OperandValue::Immediate(v), BackendRepr::Scalar(from_scalar)) => match &mut self.val {
718 OperandValueBuilder::Immediate(val @ Either::Right(_)) if is_zero_offset => {
719 update(val, v, from_scalar);
720 }
721 OperandValueBuilder::Pair(fst @ Either::Right(_), _) if is_zero_offset => {
722 update(fst, v, from_scalar);
723 }
724 OperandValueBuilder::Pair(_, snd @ Either::Right(_)) if !is_zero_offset => {
725 update(snd, v, from_scalar);
726 }
727 _ => {
728 bug!("Tried to insert {field_operand:?} into {variant:?}.{field:?} of {self:?}")
729 }
730 },
731 (OperandValue::Immediate(v), BackendRepr::SimdVector { .. }) => match &mut self.val {
732 OperandValueBuilder::Vector(val @ Either::Right(())) if is_zero_offset => {
733 *val = Either::Left(v);
734 }
735 _ => {
736 bug!("Tried to insert {field_operand:?} into {variant:?}.{field:?} of {self:?}")
737 }
738 },
739 (OperandValue::Pair(a, b), BackendRepr::ScalarPair(from_sa, from_sb)) => {
740 match &mut self.val {
741 OperandValueBuilder::Pair(fst @ Either::Right(_), snd @ Either::Right(_)) => {
742 update(fst, a, from_sa);
743 update(snd, b, from_sb);
744 }
745 _ => bug!(
746 "Tried to insert {field_operand:?} into {variant:?}.{field:?} of {self:?}"
747 ),
748 }
749 }
750 (OperandValue::Ref(place), BackendRepr::Memory { .. }) => match &mut self.val {
751 OperandValueBuilder::Vector(val @ Either::Right(())) => {
752 let ibty = bx.cx().immediate_backend_type(self.layout);
753 let simd = bx.load_from_place(ibty, place);
754 *val = Either::Left(simd);
755 }
756 _ => {
757 bug!("Tried to insert {field_operand:?} into {variant:?}.{field:?} of {self:?}")
758 }
759 },
760 _ => bug!("Operand cannot be used with `insert_field`: {field_operand:?}"),
761 }
762 }
763
764 /// Insert the immediate value `imm` for field `f` in the *type itself*,
765 /// rather than into one of the variants.
766 ///
767 /// Most things want [`Self::insert_field`] instead, but this one is
768 /// necessary for writing things like enum tags that aren't in any variant.
769 pub(super) fn insert_imm(&mut self, f: FieldIdx, imm: V) {
770 let field_offset = self.layout.fields.offset(f.as_usize());
771 let is_zero_offset = field_offset == Size::ZERO;
772 match &mut self.val {
773 OperandValueBuilder::Immediate(val @ Either::Right(_)) if is_zero_offset => {
774 *val = Either::Left(imm);
775 }
776 OperandValueBuilder::Pair(fst @ Either::Right(_), _) if is_zero_offset => {
777 *fst = Either::Left(imm);
778 }
779 OperandValueBuilder::Pair(_, snd @ Either::Right(_)) if !is_zero_offset => {
780 *snd = Either::Left(imm);
781 }
782 _ => bug!("Tried to insert {imm:?} into field {f:?} of {self:?}"),
783 }
784 }
785
786 /// After having set all necessary fields, this converts the builder back
787 /// to the normal `OperandRef`.
788 ///
789 /// ICEs if any required fields were not set.
790 pub(super) fn build(&self, cx: &impl CodegenMethods<'tcx, Value = V>) -> OperandRef<'tcx, V> {
791 let OperandRefBuilder { val, layout } = *self;
792
793 // For something like `Option::<u32>::None`, it's expected that the
794 // payload scalar will not actually have been set, so this converts
795 // unset scalars to corresponding `undef` values so long as the scalar
796 // from the layout allows uninit.
797 let unwrap = |r: Either<V, abi::Scalar>| match r {
798 Either::Left(v) => v,
799 Either::Right(s) if s.is_uninit_valid() => {
800 let bty = cx.type_from_scalar(s);
801 cx.const_undef(bty)
802 }
803 Either::Right(_) => bug!("OperandRef::build called while fields are missing {self:?}"),
804 };
805
806 let val = match val {
807 OperandValueBuilder::ZeroSized => OperandValue::ZeroSized,
808 OperandValueBuilder::Immediate(v) => OperandValue::Immediate(unwrap(v)),
809 OperandValueBuilder::Pair(a, b) => OperandValue::Pair(unwrap(a), unwrap(b)),
810 OperandValueBuilder::Vector(v) => match v {
811 Either::Left(v) => OperandValue::Immediate(v),
812 Either::Right(())
813 if let BackendRepr::SimdVector { element, .. } = layout.backend_repr
814 && element.is_uninit_valid() =>
815 {
816 let bty = cx.immediate_backend_type(layout);
817 OperandValue::Immediate(cx.const_undef(bty))
818 }
819 Either::Right(()) => {
820 bug!("OperandRef::build called while fields are missing {self:?}")
821 }
822 },
823 };
824 OperandRef { val, layout }
825 }
826}
827
828impl<'a, 'tcx, V: CodegenObject> OperandValue<V> {
829 /// Returns an `OperandValue` that's generally UB to use in any way.
830 ///
831 /// Depending on the `layout`, returns `ZeroSized` for ZSTs, an `Immediate` or
832 /// `Pair` containing poison value(s), or a `Ref` containing a poison pointer.
833 ///
834 /// Supports sized types only.
835 pub fn poison<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
836 bx: &mut Bx,
837 layout: TyAndLayout<'tcx>,
838 ) -> OperandValue<V> {
839 assert!(layout.is_sized());
840 if layout.is_zst() {
841 OperandValue::ZeroSized
842 } else if bx.cx().is_backend_immediate(layout) {
843 let ibty = bx.cx().immediate_backend_type(layout);
844 OperandValue::Immediate(bx.const_poison(ibty))
845 } else if bx.cx().is_backend_scalar_pair(layout) {
846 let ibty0 = bx.cx().scalar_pair_element_backend_type(layout, 0, true);
847 let ibty1 = bx.cx().scalar_pair_element_backend_type(layout, 1, true);
848 OperandValue::Pair(bx.const_poison(ibty0), bx.const_poison(ibty1))
849 } else {
850 let ptr = bx.cx().type_ptr();
851 OperandValue::Ref(PlaceValue::new_sized(bx.const_poison(ptr), layout.align.abi))
852 }
853 }
854
855 pub fn store<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
856 self,
857 bx: &mut Bx,
858 dest: PlaceRef<'tcx, V>,
859 ) {
860 self.store_with_flags(bx, dest, MemFlags::empty());
861 }
862
863 pub fn volatile_store<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
864 self,
865 bx: &mut Bx,
866 dest: PlaceRef<'tcx, V>,
867 ) {
868 self.store_with_flags(bx, dest, MemFlags::VOLATILE);
869 }
870
871 pub fn unaligned_volatile_store<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
872 self,
873 bx: &mut Bx,
874 dest: PlaceRef<'tcx, V>,
875 ) {
876 self.store_with_flags(bx, dest, MemFlags::VOLATILE | MemFlags::UNALIGNED);
877 }
878
879 pub fn nontemporal_store<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
880 self,
881 bx: &mut Bx,
882 dest: PlaceRef<'tcx, V>,
883 ) {
884 self.store_with_flags(bx, dest, MemFlags::NONTEMPORAL);
885 }
886
887 pub(crate) fn store_with_flags<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
888 self,
889 bx: &mut Bx,
890 dest: PlaceRef<'tcx, V>,
891 flags: MemFlags,
892 ) {
893 debug!("OperandRef::store: operand={:?}, dest={:?}", self, dest);
894 match self {
895 OperandValue::ZeroSized => {
896 // Avoid generating stores of zero-sized values, because the only way to have a
897 // zero-sized value is through `undef`/`poison`, and the store itself is useless.
898 }
899 OperandValue::Ref(val) => {
900 assert!(dest.layout.is_sized(), "cannot directly store unsized values");
901 if val.llextra.is_some() {
902 bug!("cannot directly store unsized values");
903 }
904 bx.typed_place_copy_with_flags(dest.val, val, dest.layout, flags);
905 }
906 OperandValue::Immediate(s) => {
907 let val = bx.from_immediate(s);
908 bx.store_with_flags(val, dest.val.llval, dest.val.align, flags);
909 }
910 OperandValue::Pair(a, b) => {
911 let BackendRepr::ScalarPair(a_scalar, b_scalar) = dest.layout.backend_repr else {
912 bug!("store_with_flags: invalid ScalarPair layout: {:#?}", dest.layout);
913 };
914 let b_offset = a_scalar.size(bx).align_to(b_scalar.align(bx).abi);
915
916 let val = bx.from_immediate(a);
917 let align = dest.val.align;
918 bx.store_with_flags(val, dest.val.llval, align, flags);
919
920 let llptr = bx.inbounds_ptradd(dest.val.llval, bx.const_usize(b_offset.bytes()));
921 let val = bx.from_immediate(b);
922 let align = dest.val.align.restrict_for_offset(b_offset);
923 bx.store_with_flags(val, llptr, align, flags);
924 }
925 }
926 }
927}
928
929impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
930 fn maybe_codegen_consume_direct(
931 &mut self,
932 bx: &mut Bx,
933 place_ref: mir::PlaceRef<'tcx>,
934 ) -> Option<OperandRef<'tcx, Bx::Value>> {
935 debug!("maybe_codegen_consume_direct(place_ref={:?})", place_ref);
936
937 match self.locals[place_ref.local] {
938 LocalRef::Operand(mut o) => {
939 // We only need to handle the projections that
940 // `LocalAnalyzer::process_place` let make it here.
941 for elem in place_ref.projection {
942 match *elem {
943 mir::ProjectionElem::Field(f, _) => {
944 assert!(
945 !o.layout.ty.is_any_ptr(),
946 "Bad PlaceRef: destructing pointers should use cast/PtrMetadata, \
947 but tried to access field {f:?} of pointer {o:?}",
948 );
949 o = o.extract_field(self, bx, f.index());
950 }
951 mir::PlaceElem::Downcast(_, vidx) => {
952 debug_assert_eq!(
953 o.layout.variants,
954 abi::Variants::Single { index: vidx },
955 );
956 let layout = o.layout.for_variant(bx.cx(), vidx);
957 o = OperandRef { val: o.val, layout }
958 }
959 mir::PlaceElem::Subtype(subtype_ty) => {
960 let subtype_ty = self.monomorphize(subtype_ty);
961 let layout = self.cx.layout_of(subtype_ty);
962 o = OperandRef { val: o.val, layout }
963 }
964 _ => return None,
965 }
966 }
967
968 Some(o)
969 }
970 LocalRef::PendingOperand => {
971 bug!("use of {:?} before def", place_ref);
972 }
973 LocalRef::Place(..) | LocalRef::UnsizedPlace(..) => {
974 // watch out for locals that do not have an
975 // alloca; they are handled somewhat differently
976 None
977 }
978 }
979 }
980
981 pub fn codegen_consume(
982 &mut self,
983 bx: &mut Bx,
984 place_ref: mir::PlaceRef<'tcx>,
985 ) -> OperandRef<'tcx, Bx::Value> {
986 debug!("codegen_consume(place_ref={:?})", place_ref);
987
988 let ty = self.monomorphized_place_ty(place_ref);
989 let layout = bx.cx().layout_of(ty);
990
991 // ZSTs don't require any actual memory access.
992 if layout.is_zst() {
993 return OperandRef::zero_sized(layout);
994 }
995
996 if let Some(o) = self.maybe_codegen_consume_direct(bx, place_ref) {
997 return o;
998 }
999
1000 // for most places, to consume them we just load them
1001 // out from their home
1002 let place = self.codegen_place(bx, place_ref);
1003 bx.load_operand(place)
1004 }
1005
1006 pub fn codegen_operand(
1007 &mut self,
1008 bx: &mut Bx,
1009 operand: &mir::Operand<'tcx>,
1010 ) -> OperandRef<'tcx, Bx::Value> {
1011 debug!("codegen_operand(operand={:?})", operand);
1012
1013 match *operand {
1014 mir::Operand::Copy(ref place) | mir::Operand::Move(ref place) => {
1015 self.codegen_consume(bx, place.as_ref())
1016 }
1017
1018 mir::Operand::Constant(ref constant) => {
1019 let constant_ty = self.monomorphize(constant.ty());
1020 // Most SIMD vector constants should be passed as immediates.
1021 // (In particular, some intrinsics really rely on this.)
1022 if constant_ty.is_simd() {
1023 // However, some SIMD types do not actually use the vector ABI
1024 // (in particular, packed SIMD types do not). Ensure we exclude those.
1025 let layout = bx.layout_of(constant_ty);
1026 if let BackendRepr::SimdVector { .. } = layout.backend_repr {
1027 let (llval, ty) = self.immediate_const_vector(bx, constant);
1028 return OperandRef {
1029 val: OperandValue::Immediate(llval),
1030 layout: bx.layout_of(ty),
1031 };
1032 }
1033 }
1034 self.eval_mir_constant_to_operand(bx, constant)
1035 }
1036 }
1037 }
1038}