Skip to main content

rustc_codegen_ssa/mir/
place.rs

1use std::ops::Deref as _;
2
3use rustc_abi::{
4    Align, BackendRepr, FieldIdx, FieldsShape, Size, TagEncoding, VariantIdx, Variants,
5};
6use rustc_middle::mir::PlaceTy;
7use rustc_middle::mir::interpret::Scalar;
8use rustc_middle::ty::layout::{HasTyCtxt, HasTypingEnv, LayoutOf, TyAndLayout};
9use rustc_middle::ty::{self, Ty};
10use rustc_middle::{bug, mir};
11use tracing::{debug, instrument};
12
13use super::operand::OperandValue;
14use super::{FunctionCx, LocalRef};
15use crate::common::IntPredicate;
16use crate::size_of_val;
17use crate::traits::*;
18
19/// The location and extra runtime properties of the place.
20///
21/// Typically found in a [`PlaceRef`] or an [`OperandValue::Ref`].
22///
23/// As a location in memory, this has no specific type. If you want to
24/// load or store it using a typed operation, use [`Self::with_type`].
25#[derive(#[automatically_derived]
impl<V: ::core::marker::Copy> ::core::marker::Copy for PlaceValue<V> { }Copy, #[automatically_derived]
impl<V: ::core::clone::Clone> ::core::clone::Clone for PlaceValue<V> {
    #[inline]
    fn clone(&self) -> PlaceValue<V> {
        PlaceValue {
            llval: ::core::clone::Clone::clone(&self.llval),
            llextra: ::core::clone::Clone::clone(&self.llextra),
            align: ::core::clone::Clone::clone(&self.align),
        }
    }
}Clone, #[automatically_derived]
impl<V: ::core::fmt::Debug> ::core::fmt::Debug for PlaceValue<V> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field3_finish(f, "PlaceValue",
            "llval", &self.llval, "llextra", &self.llextra, "align",
            &&self.align)
    }
}Debug)]
26pub struct PlaceValue<V> {
27    /// A pointer to the contents of the place.
28    pub llval: V,
29
30    /// This place's extra data if it is unsized, or `None` if null.
31    pub llextra: Option<V>,
32
33    /// The alignment we know for this place.
34    pub align: Align,
35}
36
37impl<V: CodegenObject> PlaceValue<V> {
38    /// Constructor for the ordinary case of `Sized` types.
39    ///
40    /// Sets `llextra` to `None`.
41    pub fn new_sized(llval: V, align: Align) -> PlaceValue<V> {
42        PlaceValue { llval, llextra: None, align }
43    }
44
45    /// Allocates a stack slot in the function for a value
46    /// of the specified size and alignment.
47    ///
48    /// The allocation itself is untyped.
49    pub fn alloca<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx, Value = V>>(
50        bx: &mut Bx,
51        size: Size,
52        align: Align,
53    ) -> PlaceValue<V> {
54        let llval = bx.alloca(size, align);
55        PlaceValue::new_sized(llval, align)
56    }
57
58    /// Creates a `PlaceRef` to this location with the given type.
59    pub fn with_type<'tcx>(self, layout: TyAndLayout<'tcx>) -> PlaceRef<'tcx, V> {
60        if !(layout.is_unsized() || layout.is_uninhabited() || self.llextra.is_none())
    {
    {
        ::core::panicking::panic_fmt(format_args!("Had pointer metadata {0:?} for sized type {1:?}",
                self.llextra, layout));
    }
};assert!(
61            layout.is_unsized() || layout.is_uninhabited() || self.llextra.is_none(),
62            "Had pointer metadata {:?} for sized type {layout:?}",
63            self.llextra,
64        );
65        PlaceRef { val: self, layout }
66    }
67
68    /// Gets the pointer to this place as an [`OperandValue::Immediate`]
69    /// or, for those needing metadata, an [`OperandValue::Pair`].
70    ///
71    /// This is the inverse of [`OperandValue::deref`].
72    pub fn address(self) -> OperandValue<V> {
73        if let Some(llextra) = self.llextra {
74            OperandValue::Pair(self.llval, llextra)
75        } else {
76            OperandValue::Immediate(self.llval)
77        }
78    }
79}
80
81#[derive(#[automatically_derived]
impl<'tcx, V: ::core::marker::Copy> ::core::marker::Copy for PlaceRef<'tcx, V>
    {
}Copy, #[automatically_derived]
impl<'tcx, V: ::core::clone::Clone> ::core::clone::Clone for PlaceRef<'tcx, V>
    {
    #[inline]
    fn clone(&self) -> PlaceRef<'tcx, V> {
        PlaceRef {
            val: ::core::clone::Clone::clone(&self.val),
            layout: ::core::clone::Clone::clone(&self.layout),
        }
    }
}Clone, #[automatically_derived]
impl<'tcx, V: ::core::fmt::Debug> ::core::fmt::Debug for PlaceRef<'tcx, V> {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field2_finish(f, "PlaceRef",
            "val", &self.val, "layout", &&self.layout)
    }
}Debug)]
82pub struct PlaceRef<'tcx, V> {
83    /// The location and extra runtime properties of the place.
84    pub val: PlaceValue<V>,
85
86    /// The monomorphized type of this place, including variant information.
87    ///
88    /// You probably shouldn't use the alignment from this layout;
89    /// rather you should use the `.val.align` of the actual place,
90    /// which might be different from the type's normal alignment.
91    pub layout: TyAndLayout<'tcx>,
92}
93
94impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
95    pub fn new_sized(llval: V, layout: TyAndLayout<'tcx>) -> PlaceRef<'tcx, V> {
96        PlaceRef::new_sized_aligned(llval, layout, layout.align.abi)
97    }
98
99    pub fn new_sized_aligned(
100        llval: V,
101        layout: TyAndLayout<'tcx>,
102        align: Align,
103    ) -> PlaceRef<'tcx, V> {
104        if !layout.is_sized() {
    ::core::panicking::panic("assertion failed: layout.is_sized()")
};assert!(layout.is_sized());
105        PlaceValue::new_sized(llval, align).with_type(layout)
106    }
107
108    // FIXME(eddyb) pass something else for the name so no work is done
109    // unless LLVM IR names are turned on (e.g. for `--emit=llvm-ir`).
110    pub fn alloca<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
111        bx: &mut Bx,
112        layout: TyAndLayout<'tcx>,
113    ) -> Self {
114        if layout.peel_transparent_wrappers(bx).deref().is_scalable_vector() {
115            Self::alloca_scalable(bx, layout)
116        } else {
117            Self::alloca_size(bx, layout.size, layout)
118        }
119    }
120
121    pub fn alloca_size<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
122        bx: &mut Bx,
123        size: Size,
124        layout: TyAndLayout<'tcx>,
125    ) -> Self {
126        if !layout.is_sized() {
    {
        ::core::panicking::panic_fmt(format_args!("tried to statically allocate unsized place"));
    }
};assert!(layout.is_sized(), "tried to statically allocate unsized place");
127        PlaceValue::alloca(bx, size, layout.align.abi).with_type(layout)
128    }
129
130    /// Returns a place for an indirect reference to an unsized place.
131    // FIXME(eddyb) pass something else for the name so no work is done
132    // unless LLVM IR names are turned on (e.g. for `--emit=llvm-ir`).
133    pub fn alloca_unsized_indirect<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
134        bx: &mut Bx,
135        layout: TyAndLayout<'tcx>,
136    ) -> Self {
137        if !layout.is_unsized() {
    {
        ::core::panicking::panic_fmt(format_args!("tried to allocate indirect place for sized values"));
    }
};assert!(layout.is_unsized(), "tried to allocate indirect place for sized values");
138        let ptr_ty = Ty::new_mut_ptr(bx.cx().tcx(), layout.ty);
139        let ptr_layout = bx.cx().layout_of(ptr_ty);
140        Self::alloca(bx, ptr_layout)
141    }
142
143    pub fn len<Cx: ConstCodegenMethods<Value = V>>(&self, cx: &Cx) -> V {
144        if let FieldsShape::Array { count, .. } = self.layout.fields {
145            if self.layout.is_unsized() {
146                match (&count, &0) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(count, 0);
147                self.val.llextra.unwrap()
148            } else {
149                cx.const_usize(count)
150            }
151        } else {
152            ::rustc_middle::util::bug::bug_fmt(format_args!("unexpected layout `{0:#?}` in PlaceRef::len",
        self.layout))bug!("unexpected layout `{:#?}` in PlaceRef::len", self.layout)
153        }
154    }
155
156    fn alloca_scalable<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
157        bx: &mut Bx,
158        layout: TyAndLayout<'tcx>,
159    ) -> Self {
160        PlaceValue::new_sized(
161            bx.alloca_with_ty(layout.peel_transparent_wrappers(bx)),
162            layout.align.abi,
163        )
164        .with_type(layout)
165    }
166}
167
168impl<'a, 'tcx, V: CodegenObject> PlaceRef<'tcx, V> {
169    /// Access a field, at a point when the value's case is known.
170    pub fn project_field<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
171        self,
172        bx: &mut Bx,
173        ix: usize,
174    ) -> Self {
175        let field = self.layout.field(bx.cx(), ix);
176        let offset = self.layout.fields.offset(ix);
177        let effective_field_align = self.val.align.restrict_for_offset(offset);
178
179        // `simple` is called when we don't need to adjust the offset to
180        // the dynamic alignment of the field.
181        let mut simple = || {
182            let llval = if offset.bytes() == 0 {
183                self.val.llval
184            } else {
185                bx.inbounds_ptradd(self.val.llval, bx.const_usize(offset.bytes()))
186            };
187            let val = PlaceValue {
188                llval,
189                llextra: if bx.cx().tcx().type_has_metadata(field.ty, bx.cx().typing_env()) {
190                    self.val.llextra
191                } else {
192                    None
193                },
194                align: effective_field_align,
195            };
196            val.with_type(field)
197        };
198
199        // Simple cases, which don't need DST adjustment:
200        //   * known alignment - sized types, `[T]`, `str`
201        //   * offset 0 -- rounding up to alignment cannot change the offset
202        // Note that looking at `field.align` is incorrect since that is not necessarily equal
203        // to the dynamic alignment of the type.
204        match field.ty.kind() {
205            _ if field.is_sized() => return simple(),
206            ty::Slice(..) | ty::Str => return simple(),
207            _ if offset.bytes() == 0 => return simple(),
208            _ => {}
209        }
210
211        // We need to get the pointer manually now.
212        // We do this by casting to a `*i8`, then offsetting it by the appropriate amount.
213        // We do this instead of, say, simply adjusting the pointer from the result of a GEP
214        // because the field may have an arbitrary alignment in the LLVM representation.
215        //
216        // To demonstrate:
217        //
218        //     struct Foo<T: ?Sized> {
219        //         x: u16,
220        //         y: T
221        //     }
222        //
223        // The type `Foo<Foo<Trait>>` is represented in LLVM as `{ u16, { u16, u8 }}`, meaning that
224        // the `y` field has 16-bit alignment.
225
226        let meta = self.val.llextra;
227
228        let unaligned_offset = bx.cx().const_usize(offset.bytes());
229
230        // Get the alignment of the field
231        let (_, mut unsized_align) = size_of_val::size_and_align_of_dst(bx, field.ty, meta);
232
233        // For packed types, we need to cap alignment.
234        if let ty::Adt(def, _) = self.layout.ty.kind()
235            && let Some(packed) = def.repr().pack
236        {
237            let packed = bx.const_usize(packed.bytes());
238            let cmp = bx.icmp(IntPredicate::IntULT, unsized_align, packed);
239            unsized_align = bx.select(cmp, unsized_align, packed)
240        }
241
242        // Bump the unaligned offset up to the appropriate alignment
243        let offset = round_up_const_value_to_alignment(bx, unaligned_offset, unsized_align);
244
245        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_ssa/src/mir/place.rs:245",
                        "rustc_codegen_ssa::mir::place", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/mir/place.rs"),
                        ::tracing_core::__macro_support::Option::Some(245u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::mir::place"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("struct_field_ptr: DST field offset: {0:?}",
                                                    offset) as &dyn Value))])
            });
    } else { ; }
};debug!("struct_field_ptr: DST field offset: {:?}", offset);
246
247        // Adjust pointer.
248        let ptr = bx.inbounds_ptradd(self.val.llval, offset);
249        let val =
250            PlaceValue { llval: ptr, llextra: self.val.llextra, align: effective_field_align };
251        val.with_type(field)
252    }
253
254    /// Sets the discriminant for a new value of the given case of the given
255    /// representation.
256    pub fn codegen_set_discr<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
257        &self,
258        bx: &mut Bx,
259        variant_index: VariantIdx,
260    ) {
261        match codegen_tag_value(bx.cx(), variant_index, self.layout) {
262            Err(UninhabitedVariantError) => {
263                // We play it safe by using a well-defined `abort`, but we could go for immediate UB
264                // if that turns out to be helpful.
265                bx.abort();
266            }
267            Ok(Some((tag_field, imm))) => {
268                let tag_place = self.project_field(bx, tag_field.as_usize());
269                OperandValue::Immediate(imm).store(bx, tag_place);
270            }
271            Ok(None) => {}
272        }
273    }
274
275    pub fn project_index<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
276        &self,
277        bx: &mut Bx,
278        llindex: V,
279    ) -> Self {
280        // Statically compute the offset if we can, otherwise just use the element size,
281        // as this will yield the lowest alignment.
282        let layout = self.layout.field(bx, 0);
283        let offset = if let Some(llindex) = bx.const_to_opt_uint(llindex) {
284            layout.size.checked_mul(llindex, bx).unwrap_or(layout.size)
285        } else {
286            layout.size
287        };
288
289        let llval = bx.inbounds_nuw_gep(bx.cx().backend_type(layout), self.val.llval, &[llindex]);
290        let align = self.val.align.restrict_for_offset(offset);
291        PlaceValue::new_sized(llval, align).with_type(layout)
292    }
293
294    pub fn project_downcast<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
295        &self,
296        bx: &mut Bx,
297        variant_index: VariantIdx,
298    ) -> Self {
299        let mut downcast = *self;
300        downcast.layout = self.layout.for_variant(bx.cx(), variant_index);
301        downcast
302    }
303
304    pub fn project_type<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
305        &self,
306        bx: &mut Bx,
307        ty: Ty<'tcx>,
308    ) -> Self {
309        let mut downcast = *self;
310        downcast.layout = bx.cx().layout_of(ty);
311        downcast
312    }
313
314    pub fn storage_live<Bx: BuilderMethods<'a, 'tcx, Value = V>>(&self, bx: &mut Bx) {
315        bx.lifetime_start(self.val.llval, self.layout.size);
316    }
317
318    pub fn storage_dead<Bx: BuilderMethods<'a, 'tcx, Value = V>>(&self, bx: &mut Bx) {
319        bx.lifetime_end(self.val.llval, self.layout.size);
320    }
321}
322
323impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
324    #[allow(clippy :: suspicious_else_formatting)]
{
    let __tracing_attr_span;
    let __tracing_attr_guard;
    if ::tracing::Level::TRACE <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::TRACE <=
                    ::tracing::level_filters::LevelFilter::current() ||
            { false } {
        __tracing_attr_span =
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("codegen_place",
                                    "rustc_codegen_ssa::mir::place", ::tracing::Level::TRACE,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/mir/place.rs"),
                                    ::tracing_core::__macro_support::Option::Some(324u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::mir::place"),
                                    ::tracing_core::field::FieldSet::new(&["place_ref"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::SPAN)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let mut interest = ::tracing::subscriber::Interest::never();
                if ::tracing::Level::TRACE <=
                                    ::tracing::level_filters::STATIC_MAX_LEVEL &&
                                ::tracing::Level::TRACE <=
                                    ::tracing::level_filters::LevelFilter::current() &&
                            { interest = __CALLSITE.interest(); !interest.is_never() }
                        &&
                        ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                            interest) {
                    let meta = __CALLSITE.metadata();
                    ::tracing::Span::new(meta,
                        &{
                                #[allow(unused_imports)]
                                use ::tracing::field::{debug, display, Value};
                                let mut iter = meta.fields().iter();
                                meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&place_ref)
                                                            as &dyn Value))])
                            })
                } else {
                    let span =
                        ::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
                    {};
                    span
                }
            };
        __tracing_attr_guard = __tracing_attr_span.enter();
    }

    #[warn(clippy :: suspicious_else_formatting)]
    {

        #[allow(unknown_lints, unreachable_code, clippy ::
        diverging_sub_expression, clippy :: empty_loop, clippy ::
        let_unit_value, clippy :: let_with_type_underscore, clippy ::
        needless_return, clippy :: unreachable)]
        if false {
            let __tracing_attr_fake_return: PlaceRef<'tcx, Bx::Value> =
                loop {};
            return __tracing_attr_fake_return;
        }
        {
            let cx = self.cx;
            let tcx = self.cx.tcx();
            let mut base = 0;
            let mut cg_base =
                match self.locals[place_ref.local] {
                    LocalRef::Place(place) => place,
                    LocalRef::UnsizedPlace(place) =>
                        bx.load_operand(place).deref(cx),
                    LocalRef::Operand(..) => {
                        if place_ref.is_indirect_first_projection() {
                            base = 1;
                            let cg_base =
                                self.codegen_consume(bx,
                                    mir::PlaceRef {
                                        projection: &place_ref.projection[..0],
                                        ..place_ref
                                    });
                            cg_base.deref(bx.cx())
                        } else {
                            ::rustc_middle::util::bug::bug_fmt(format_args!("using operand local {0:?} as place",
                                    place_ref));
                        }
                    }
                    LocalRef::PendingOperand => {
                        ::rustc_middle::util::bug::bug_fmt(format_args!("using still-pending operand local {0:?} as place",
                                place_ref));
                    }
                };
            for elem in place_ref.projection[base..].iter() {
                cg_base =
                    match *elem {
                        mir::ProjectionElem::Deref =>
                            bx.load_operand(cg_base).deref(bx.cx()),
                        mir::ProjectionElem::Field(ref field, _) => {
                            if !!cg_base.layout.ty.is_any_ptr() {
                                {
                                    ::core::panicking::panic_fmt(format_args!("Bad PlaceRef: destructing pointers should use cast/PtrMetadata, but tried to access field {0:?} of pointer {1:?}",
                                            field, cg_base));
                                }
                            };
                            cg_base.project_field(bx, field.index())
                        }
                        mir::ProjectionElem::OpaqueCast(ty) => {
                            ::rustc_middle::util::bug::bug_fmt(format_args!("encountered OpaqueCast({0}) in codegen",
                                    ty))
                        }
                        mir::ProjectionElem::UnwrapUnsafeBinder(ty) => {
                            cg_base.project_type(bx, self.monomorphize(ty))
                        }
                        mir::ProjectionElem::Index(index) => {
                            let index = &mir::Operand::Copy(mir::Place::from(index));
                            let index = self.codegen_operand(bx, index);
                            let llindex = index.immediate();
                            cg_base.project_index(bx, llindex)
                        }
                        mir::ProjectionElem::ConstantIndex {
                            offset, from_end: false, min_length: _ } => {
                            let lloffset = bx.cx().const_usize(offset);
                            cg_base.project_index(bx, lloffset)
                        }
                        mir::ProjectionElem::ConstantIndex {
                            offset, from_end: true, min_length: _ } => {
                            let lloffset = bx.cx().const_usize(offset);
                            let lllen = cg_base.len(bx.cx());
                            let llindex = bx.sub(lllen, lloffset);
                            cg_base.project_index(bx, llindex)
                        }
                        mir::ProjectionElem::Subslice { from, to, from_end } => {
                            let mut subslice =
                                cg_base.project_index(bx, bx.cx().const_usize(from));
                            let projected_ty =
                                PlaceTy::from_ty(cg_base.layout.ty).projection_ty(tcx,
                                        *elem).ty;
                            subslice.layout =
                                bx.cx().layout_of(self.monomorphize(projected_ty));
                            if subslice.layout.is_unsized() {
                                if !from_end {
                                    {
                                        ::core::panicking::panic_fmt(format_args!("slice subslices should be `from_end`"));
                                    }
                                };
                                subslice.val.llextra =
                                    Some(bx.sub(cg_base.val.llextra.unwrap(),
                                            bx.cx().const_usize(from + to)));
                            }
                            subslice
                        }
                        mir::ProjectionElem::Downcast(_, v) =>
                            cg_base.project_downcast(bx, v),
                    };
            }
            {
                use ::tracing::__macro_support::Callsite as _;
                static __CALLSITE: ::tracing::callsite::DefaultCallsite =
                    {
                        static META: ::tracing::Metadata<'static> =
                            {
                                ::tracing_core::metadata::Metadata::new("event compiler/rustc_codegen_ssa/src/mir/place.rs:404",
                                    "rustc_codegen_ssa::mir::place", ::tracing::Level::DEBUG,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/mir/place.rs"),
                                    ::tracing_core::__macro_support::Option::Some(404u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::mir::place"),
                                    ::tracing_core::field::FieldSet::new(&["message"],
                                        ::tracing_core::callsite::Identifier(&__CALLSITE)),
                                    ::tracing::metadata::Kind::EVENT)
                            };
                        ::tracing::callsite::DefaultCallsite::new(&META)
                    };
                let enabled =
                    ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::STATIC_MAX_LEVEL &&
                            ::tracing::Level::DEBUG <=
                                ::tracing::level_filters::LevelFilter::current() &&
                        {
                            let interest = __CALLSITE.interest();
                            !interest.is_never() &&
                                ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                                    interest)
                        };
                if enabled {
                    (|value_set: ::tracing::field::ValueSet|
                                {
                                    let meta = __CALLSITE.metadata();
                                    ::tracing::Event::dispatch(meta, &value_set);
                                    ;
                                })({
                            #[allow(unused_imports)]
                            use ::tracing::field::{debug, display, Value};
                            let mut iter = __CALLSITE.metadata().fields().iter();
                            __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                ::tracing::__macro_support::Option::Some(&format_args!("codegen_place(place={0:?}) => {1:?}",
                                                                place_ref, cg_base) as &dyn Value))])
                        });
                } else { ; }
            };
            cg_base
        }
    }
}#[instrument(level = "trace", skip(self, bx))]
325    pub fn codegen_place(
326        &mut self,
327        bx: &mut Bx,
328        place_ref: mir::PlaceRef<'tcx>,
329    ) -> PlaceRef<'tcx, Bx::Value> {
330        let cx = self.cx;
331        let tcx = self.cx.tcx();
332
333        let mut base = 0;
334        let mut cg_base = match self.locals[place_ref.local] {
335            LocalRef::Place(place) => place,
336            LocalRef::UnsizedPlace(place) => bx.load_operand(place).deref(cx),
337            LocalRef::Operand(..) => {
338                if place_ref.is_indirect_first_projection() {
339                    base = 1;
340                    let cg_base = self.codegen_consume(
341                        bx,
342                        mir::PlaceRef { projection: &place_ref.projection[..0], ..place_ref },
343                    );
344                    cg_base.deref(bx.cx())
345                } else {
346                    bug!("using operand local {:?} as place", place_ref);
347                }
348            }
349            LocalRef::PendingOperand => {
350                bug!("using still-pending operand local {:?} as place", place_ref);
351            }
352        };
353        for elem in place_ref.projection[base..].iter() {
354            cg_base = match *elem {
355                mir::ProjectionElem::Deref => bx.load_operand(cg_base).deref(bx.cx()),
356                mir::ProjectionElem::Field(ref field, _) => {
357                    assert!(
358                        !cg_base.layout.ty.is_any_ptr(),
359                        "Bad PlaceRef: destructing pointers should use cast/PtrMetadata, \
360                         but tried to access field {field:?} of pointer {cg_base:?}",
361                    );
362                    cg_base.project_field(bx, field.index())
363                }
364                mir::ProjectionElem::OpaqueCast(ty) => {
365                    bug!("encountered OpaqueCast({ty}) in codegen")
366                }
367                mir::ProjectionElem::UnwrapUnsafeBinder(ty) => {
368                    cg_base.project_type(bx, self.monomorphize(ty))
369                }
370                mir::ProjectionElem::Index(index) => {
371                    let index = &mir::Operand::Copy(mir::Place::from(index));
372                    let index = self.codegen_operand(bx, index);
373                    let llindex = index.immediate();
374                    cg_base.project_index(bx, llindex)
375                }
376                mir::ProjectionElem::ConstantIndex { offset, from_end: false, min_length: _ } => {
377                    let lloffset = bx.cx().const_usize(offset);
378                    cg_base.project_index(bx, lloffset)
379                }
380                mir::ProjectionElem::ConstantIndex { offset, from_end: true, min_length: _ } => {
381                    let lloffset = bx.cx().const_usize(offset);
382                    let lllen = cg_base.len(bx.cx());
383                    let llindex = bx.sub(lllen, lloffset);
384                    cg_base.project_index(bx, llindex)
385                }
386                mir::ProjectionElem::Subslice { from, to, from_end } => {
387                    let mut subslice = cg_base.project_index(bx, bx.cx().const_usize(from));
388                    let projected_ty =
389                        PlaceTy::from_ty(cg_base.layout.ty).projection_ty(tcx, *elem).ty;
390                    subslice.layout = bx.cx().layout_of(self.monomorphize(projected_ty));
391
392                    if subslice.layout.is_unsized() {
393                        assert!(from_end, "slice subslices should be `from_end`");
394                        subslice.val.llextra = Some(
395                            bx.sub(cg_base.val.llextra.unwrap(), bx.cx().const_usize(from + to)),
396                        );
397                    }
398
399                    subslice
400                }
401                mir::ProjectionElem::Downcast(_, v) => cg_base.project_downcast(bx, v),
402            };
403        }
404        debug!("codegen_place(place={:?}) => {:?}", place_ref, cg_base);
405        cg_base
406    }
407
408    pub fn monomorphized_place_ty(&self, place_ref: mir::PlaceRef<'tcx>) -> Ty<'tcx> {
409        let tcx = self.cx.tcx();
410        let place_ty = place_ref.ty(self.mir, tcx);
411        self.monomorphize(place_ty.ty)
412    }
413}
414
415fn round_up_const_value_to_alignment<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
416    bx: &mut Bx,
417    value: Bx::Value,
418    align: Bx::Value,
419) -> Bx::Value {
420    // In pseudo code:
421    //
422    //     if value & (align - 1) == 0 {
423    //         value
424    //     } else {
425    //         (value & !(align - 1)) + align
426    //     }
427    //
428    // Usually this is written without branches as
429    //
430    //     (value + align - 1) & !(align - 1)
431    //
432    // But this formula cannot take advantage of constant `value`. E.g. if `value` is known
433    // at compile time to be `1`, this expression should be optimized to `align`. However,
434    // optimization only holds if `align` is a power of two. Since the optimizer doesn't know
435    // that `align` is a power of two, it cannot perform this optimization.
436    //
437    // Instead we use
438    //
439    //     value + (-value & (align - 1))
440    //
441    // Since `align` is used only once, the expression can be optimized. For `value = 0`
442    // its optimized to `0` even in debug mode.
443    //
444    // NB: The previous version of this code used
445    //
446    //     (value + align - 1) & -align
447    //
448    // Even though `-align == !(align - 1)`, LLVM failed to optimize this even for
449    // `value = 0`. Bug report: https://bugs.llvm.org/show_bug.cgi?id=48559
450    let one = bx.const_usize(1);
451    let align_minus_1 = bx.sub(align, one);
452    let neg_value = bx.neg(value);
453    let offset = bx.and(neg_value, align_minus_1);
454    bx.add(value, offset)
455}
456
457/// Calculates the value that needs to be stored to mark the discriminant.
458///
459/// This might be `None` for a `struct` or a niched variant (like `Some(&3)`).
460///
461/// If it's `Some`, it returns the value to store and the field in which to
462/// store it. Note that this value is *not* the same as the discriminant, in
463/// general, as it might be a niche value or have a different size.
464///
465/// It might also be an `Err` because the variant is uninhabited.
466pub(super) fn codegen_tag_value<'tcx, V>(
467    cx: &impl CodegenMethods<'tcx, Value = V>,
468    variant_index: VariantIdx,
469    layout: TyAndLayout<'tcx>,
470) -> Result<Option<(FieldIdx, V)>, UninhabitedVariantError> {
471    // By checking uninhabited-ness first we don't need to worry about types
472    // like `(u32, !)` which are single-variant but weird.
473    if layout.for_variant(cx, variant_index).is_uninhabited() {
474        return Err(UninhabitedVariantError);
475    }
476
477    Ok(match layout.variants {
478        Variants::Empty => {
    ::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
            format_args!("we already handled uninhabited types")));
}unreachable!("we already handled uninhabited types"),
479        Variants::Single { index } => {
480            match (&index, &variant_index) {
    (left_val, right_val) => {
        if !(*left_val == *right_val) {
            let kind = ::core::panicking::AssertKind::Eq;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::None);
        }
    }
};assert_eq!(index, variant_index);
481            None
482        }
483
484        Variants::Multiple { tag_encoding: TagEncoding::Direct, tag_field, .. } => {
485            let discr = layout.ty.discriminant_for_variant(cx.tcx(), variant_index);
486            let to = discr.unwrap().val;
487            let tag_layout = layout.field(cx, tag_field.as_usize());
488            let tag_llty = cx.immediate_backend_type(tag_layout);
489            let imm = cx.const_uint_big(tag_llty, to);
490            Some((tag_field, imm))
491        }
492        Variants::Multiple {
493            tag_encoding: TagEncoding::Niche { untagged_variant, ref niche_variants, niche_start },
494            tag_field,
495            ..
496        } => {
497            if variant_index != untagged_variant {
498                let niche_layout = layout.field(cx, tag_field.as_usize());
499                let niche_llty = cx.immediate_backend_type(niche_layout);
500                let BackendRepr::Scalar(scalar) = niche_layout.backend_repr else {
501                    ::rustc_middle::util::bug::bug_fmt(format_args!("expected a scalar placeref for the niche"));bug!("expected a scalar placeref for the niche");
502                };
503                // We are supposed to compute `niche_value.wrapping_add(niche_start)` wrapping
504                // around the `niche`'s type.
505                // The easiest way to do that is to do wrapping arithmetic on `u128` and then
506                // masking off any extra bits that occur because we did the arithmetic with too many bits.
507                let niche_value = variant_index.as_u32() - niche_variants.start.as_u32();
508                let niche_value = (niche_value as u128).wrapping_add(niche_start);
509                let niche_value = niche_value & niche_layout.size.unsigned_int_max();
510
511                let niche_llval = cx.scalar_to_backend(
512                    Scalar::from_uint(niche_value, niche_layout.size),
513                    scalar,
514                    niche_llty,
515                );
516                Some((tag_field, niche_llval))
517            } else {
518                None
519            }
520        }
521    })
522}
523
524#[derive(#[automatically_derived]
impl ::core::fmt::Debug for UninhabitedVariantError {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::write_str(f, "UninhabitedVariantError")
    }
}Debug)]
525pub(super) struct UninhabitedVariantError;