Skip to main content

rustc_codegen_ssa/mir/
rvalue.rs

1use itertools::Itertools as _;
2use rustc_abi::{self as abi, BackendRepr, FIRST_VARIANT};
3use rustc_middle::ty::adjustment::PointerCoercion;
4use rustc_middle::ty::layout::{HasTyCtxt, HasTypingEnv, LayoutOf, TyAndLayout};
5use rustc_middle::ty::{self, Instance, Mutability, Ty, TyCtxt};
6use rustc_middle::{bug, mir, span_bug};
7use rustc_session::config::OptLevel;
8use tracing::{debug, instrument};
9
10use super::FunctionCx;
11use super::operand::{OperandRef, OperandRefBuilder, OperandValue};
12use super::place::{PlaceRef, PlaceValue, codegen_tag_value};
13use crate::common::{IntPredicate, TypeKind};
14use crate::traits::*;
15use crate::{MemFlags, base};
16
17impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
18    #[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_rvalue",
                                    "rustc_codegen_ssa::mir::rvalue", ::tracing::Level::TRACE,
                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/mir/rvalue.rs"),
                                    ::tracing_core::__macro_support::Option::Some(18u32),
                                    ::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::mir::rvalue"),
                                    ::tracing_core::field::FieldSet::new(&["dest", "rvalue"],
                                        ::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(&dest)
                                                            as &dyn Value)),
                                                (&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                                    ::tracing::__macro_support::Option::Some(&::tracing::field::debug(&rvalue)
                                                            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: () = loop {};
            return __tracing_attr_fake_return;
        }
        {
            match *rvalue {
                mir::Rvalue::Use(ref operand, with_retag) => {
                    if let mir::Operand::Constant(const_op) = operand {
                        let val = self.eval_mir_constant(&const_op);
                        if val.all_bytes_uninit(self.cx.tcx()) { return; }
                    }
                    let cg_operand = self.codegen_operand(bx, operand);
                    if #[allow(non_exhaustive_omitted_patterns)] match cg_operand.layout.backend_repr
                            {
                            BackendRepr::Scalar(..) | BackendRepr::ScalarPair(..) =>
                                true,
                            _ => false,
                        } {
                        if true {
                            if !!#[allow(non_exhaustive_omitted_patterns)] match cg_operand.val
                                            {
                                            OperandValue::Ref(..) => true,
                                            _ => false,
                                        } {
                                ::core::panicking::panic("assertion failed: !matches!(cg_operand.val, OperandValue::Ref(..))")
                            };
                        };
                    }
                    let flags =
                        if let ty::Ref(_, pointee_ty, Mutability::Not) =
                                        cg_operand.layout.ty.kind() && with_retag.yes() &&
                                pointee_ty.is_freeze(self.cx.tcx(), self.cx.typing_env()) {
                            MemFlags::CAPTURES_READ_ONLY
                        } else { MemFlags::empty() };
                    cg_operand.store_with_annotation_and_flags(bx, dest, flags);
                }
                mir::Rvalue::Cast(mir::CastKind::PointerCoercion(PointerCoercion::Unsize,
                    _), ref source, _) => {
                    if bx.cx().is_backend_scalar_pair(dest.layout) {
                        let temp = self.codegen_rvalue_operand(bx, rvalue);
                        temp.store_with_annotation(bx, dest);
                        return;
                    }
                    let operand = self.codegen_operand(bx, source);
                    match operand.val {
                        OperandValue::Pair(..) | OperandValue::Immediate(_) => {
                            {
                                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/rvalue.rs:87",
                                                    "rustc_codegen_ssa::mir::rvalue", ::tracing::Level::DEBUG,
                                                    ::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/mir/rvalue.rs"),
                                                    ::tracing_core::__macro_support::Option::Some(87u32),
                                                    ::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::mir::rvalue"),
                                                    ::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_rvalue: creating ugly alloca")
                                                                        as &dyn Value))])
                                        });
                                } else { ; }
                            };
                            let scratch = PlaceRef::alloca(bx, operand.layout);
                            scratch.storage_live(bx);
                            operand.store_with_annotation(bx, scratch);
                            base::coerce_unsized_into(bx, scratch, dest);
                            scratch.storage_dead(bx);
                        }
                        OperandValue::Ref(val) => {
                            if val.llextra.is_some() {
                                ::rustc_middle::util::bug::bug_fmt(format_args!("unsized coercion on an unsized rvalue"));
                            }
                            base::coerce_unsized_into(bx, val.with_type(operand.layout),
                                dest);
                        }
                        OperandValue::ZeroSized => {
                            ::rustc_middle::util::bug::bug_fmt(format_args!("unsized coercion on a ZST rvalue"));
                        }
                    }
                }
                mir::Rvalue::Cast(mir::CastKind::Transmute |
                    mir::CastKind::Subtype, ref operand, _ty) => {
                    let src = self.codegen_operand(bx, operand);
                    self.codegen_transmute(bx, src, dest);
                }
                mir::Rvalue::Repeat(ref elem, count) => {
                    if dest.layout.is_zst() { return; }
                    if let mir::Operand::Constant(const_op) = elem {
                        let val = self.eval_mir_constant(const_op);
                        if val.all_bytes_uninit(self.cx.tcx()) {
                            let size = bx.const_usize(dest.layout.size.bytes());
                            bx.memset(dest.val.llval, bx.const_undef(bx.type_i8()),
                                size, dest.val.align, MemFlags::empty());
                            return;
                        }
                    }
                    let cg_elem = self.codegen_operand(bx, elem);
                    let try_init_all_same =
                        |bx: &mut Bx, v|
                            {
                                let start = dest.val.llval;
                                let size = bx.const_usize(dest.layout.size.bytes());
                                if let Some(int) = bx.cx().const_to_opt_u128(v, false) &&
                                            let bytes =
                                                &int.to_le_bytes()[..cg_elem.layout.size.bytes_usize()] &&
                                        let Ok(&byte) = bytes.iter().all_equal_value() {
                                    let fill = bx.cx().const_u8(byte);
                                    bx.memset(start, fill, size, dest.val.align,
                                        MemFlags::empty());
                                    return true;
                                }
                                let v = bx.from_immediate(v);
                                if bx.cx().val_ty(v) == bx.cx().type_i8() {
                                    bx.memset(start, v, size, dest.val.align,
                                        MemFlags::empty());
                                    return true;
                                }
                                false
                            };
                    if let OperandValue::Immediate(v) = cg_elem.val &&
                            try_init_all_same(bx, v) {
                        return;
                    }
                    let count =
                        self.monomorphize(count).try_to_target_usize(bx.tcx()).expect("expected monomorphic const in codegen");
                    bx.write_operand_repeatedly(cg_elem, count, dest);
                }
                mir::Rvalue::Aggregate(ref kind, ref operands) if
                    !#[allow(non_exhaustive_omitted_patterns)] match **kind {
                            mir::AggregateKind::RawPtr(..) => true,
                            _ => false,
                        } => {
                    let (variant_index, variant_dest, active_field_index) =
                        match **kind {
                            mir::AggregateKind::Adt(_, variant_index, _, _,
                                active_field_index) => {
                                let variant_dest = dest.project_downcast(bx, variant_index);
                                (variant_index, variant_dest, active_field_index)
                            }
                            _ => (FIRST_VARIANT, dest, None),
                        };
                    if active_field_index.is_some() {
                        match (&operands.len(), &1) {
                            (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);
                                }
                            }
                        };
                    }
                    for (i, operand) in operands.iter_enumerated() {
                        let op = self.codegen_operand(bx, operand);
                        if !op.layout.is_zst() {
                            let field_index = active_field_index.unwrap_or(i);
                            let field =
                                if let mir::AggregateKind::Array(_) = **kind {
                                    let llindex =
                                        bx.cx().const_usize(field_index.as_u32().into());
                                    variant_dest.project_index(bx, llindex)
                                } else {
                                    variant_dest.project_field(bx, field_index.as_usize())
                                };
                            op.store_with_annotation(bx, field);
                        }
                    }
                    dest.codegen_set_discr(bx, variant_index);
                }
                _ => {
                    let temp = self.codegen_rvalue_operand(bx, rvalue);
                    temp.store_with_annotation(bx, dest);
                }
            }
        }
    }
}#[instrument(level = "trace", skip(self, bx))]
19    pub(crate) fn codegen_rvalue(
20        &mut self,
21        bx: &mut Bx,
22        dest: PlaceRef<'tcx, Bx::Value>,
23        rvalue: &mir::Rvalue<'tcx>,
24    ) {
25        match *rvalue {
26            mir::Rvalue::Use(ref operand, with_retag) => {
27                if let mir::Operand::Constant(const_op) = operand {
28                    let val = self.eval_mir_constant(&const_op);
29                    if val.all_bytes_uninit(self.cx.tcx()) {
30                        return;
31                    }
32                }
33                let cg_operand = self.codegen_operand(bx, operand);
34                // Crucially, we do *not* use `OperandValue::Ref` for types with
35                // `BackendRepr::Scalar | BackendRepr::ScalarPair`. This ensures we match the MIR
36                // semantics regarding when assignment operators allow overlap of LHS and RHS.
37                if matches!(
38                    cg_operand.layout.backend_repr,
39                    BackendRepr::Scalar(..) | BackendRepr::ScalarPair(..),
40                ) {
41                    debug_assert!(!matches!(cg_operand.val, OperandValue::Ref(..)));
42                }
43                // If this is storing a &Freeze reference with a retag, record that it's not
44                // possible to perform writes through the stored pointer.
45                let flags = if let ty::Ref(_, pointee_ty, Mutability::Not) =
46                    cg_operand.layout.ty.kind()
47                    && with_retag.yes()
48                    && pointee_ty.is_freeze(self.cx.tcx(), self.cx.typing_env())
49                {
50                    MemFlags::CAPTURES_READ_ONLY
51                } else {
52                    MemFlags::empty()
53                };
54                // FIXME: consider not copying constants through stack. (Fixable by codegen'ing
55                // constants into `OperandValue::Ref`; why don’t we do that yet if we don’t?)
56                cg_operand.store_with_annotation_and_flags(bx, dest, flags);
57            }
58
59            mir::Rvalue::Cast(
60                mir::CastKind::PointerCoercion(PointerCoercion::Unsize, _),
61                ref source,
62                _,
63            ) => {
64                // The destination necessarily contains a wide pointer, so if
65                // it's a scalar pair, it's a wide pointer or newtype thereof.
66                if bx.cx().is_backend_scalar_pair(dest.layout) {
67                    // Into-coerce of a thin pointer to a wide pointer -- just
68                    // use the operand path.
69                    let temp = self.codegen_rvalue_operand(bx, rvalue);
70                    temp.store_with_annotation(bx, dest);
71                    return;
72                }
73
74                // Unsize of a nontrivial struct. I would prefer for
75                // this to be eliminated by MIR building, but
76                // `CoerceUnsized` can be passed by a where-clause,
77                // so the (generic) MIR may not be able to expand it.
78                let operand = self.codegen_operand(bx, source);
79                match operand.val {
80                    OperandValue::Pair(..) | OperandValue::Immediate(_) => {
81                        // Unsize from an immediate structure. We don't
82                        // really need a temporary alloca here, but
83                        // avoiding it would require us to have
84                        // `coerce_unsized_into` use `extractvalue` to
85                        // index into the struct, and this case isn't
86                        // important enough for it.
87                        debug!("codegen_rvalue: creating ugly alloca");
88                        let scratch = PlaceRef::alloca(bx, operand.layout);
89                        scratch.storage_live(bx);
90                        operand.store_with_annotation(bx, scratch);
91                        base::coerce_unsized_into(bx, scratch, dest);
92                        scratch.storage_dead(bx);
93                    }
94                    OperandValue::Ref(val) => {
95                        if val.llextra.is_some() {
96                            bug!("unsized coercion on an unsized rvalue");
97                        }
98                        base::coerce_unsized_into(bx, val.with_type(operand.layout), dest);
99                    }
100                    OperandValue::ZeroSized => {
101                        bug!("unsized coercion on a ZST rvalue");
102                    }
103                }
104            }
105
106            mir::Rvalue::Cast(
107                mir::CastKind::Transmute | mir::CastKind::Subtype,
108                ref operand,
109                _ty,
110            ) => {
111                let src = self.codegen_operand(bx, operand);
112                self.codegen_transmute(bx, src, dest);
113            }
114
115            mir::Rvalue::Repeat(ref elem, count) => {
116                // Do not generate the loop for zero-sized elements or empty arrays.
117                if dest.layout.is_zst() {
118                    return;
119                }
120
121                // When the element is a const with all bytes uninit, emit a single memset that
122                // writes undef to the entire destination.
123                if let mir::Operand::Constant(const_op) = elem {
124                    let val = self.eval_mir_constant(const_op);
125                    if val.all_bytes_uninit(self.cx.tcx()) {
126                        let size = bx.const_usize(dest.layout.size.bytes());
127                        bx.memset(
128                            dest.val.llval,
129                            bx.const_undef(bx.type_i8()),
130                            size,
131                            dest.val.align,
132                            MemFlags::empty(),
133                        );
134                        return;
135                    }
136                }
137
138                let cg_elem = self.codegen_operand(bx, elem);
139
140                let try_init_all_same = |bx: &mut Bx, v| {
141                    let start = dest.val.llval;
142                    let size = bx.const_usize(dest.layout.size.bytes());
143
144                    // Use llvm.memset.p0i8.* to initialize all same byte arrays
145                    if let Some(int) = bx.cx().const_to_opt_u128(v, false)
146                        && let bytes = &int.to_le_bytes()[..cg_elem.layout.size.bytes_usize()]
147                        && let Ok(&byte) = bytes.iter().all_equal_value()
148                    {
149                        let fill = bx.cx().const_u8(byte);
150                        bx.memset(start, fill, size, dest.val.align, MemFlags::empty());
151                        return true;
152                    }
153
154                    // Use llvm.memset.p0i8.* to initialize byte arrays
155                    let v = bx.from_immediate(v);
156                    if bx.cx().val_ty(v) == bx.cx().type_i8() {
157                        bx.memset(start, v, size, dest.val.align, MemFlags::empty());
158                        return true;
159                    }
160                    false
161                };
162
163                if let OperandValue::Immediate(v) = cg_elem.val
164                    && try_init_all_same(bx, v)
165                {
166                    return;
167                }
168
169                let count = self
170                    .monomorphize(count)
171                    .try_to_target_usize(bx.tcx())
172                    .expect("expected monomorphic const in codegen");
173
174                bx.write_operand_repeatedly(cg_elem, count, dest);
175            }
176
177            // This implementation does field projection, so never use it for `RawPtr`,
178            // which will always be fine with the `codegen_rvalue_operand` path below.
179            mir::Rvalue::Aggregate(ref kind, ref operands)
180                if !matches!(**kind, mir::AggregateKind::RawPtr(..)) =>
181            {
182                let (variant_index, variant_dest, active_field_index) = match **kind {
183                    mir::AggregateKind::Adt(_, variant_index, _, _, active_field_index) => {
184                        let variant_dest = dest.project_downcast(bx, variant_index);
185                        (variant_index, variant_dest, active_field_index)
186                    }
187                    _ => (FIRST_VARIANT, dest, None),
188                };
189                if active_field_index.is_some() {
190                    assert_eq!(operands.len(), 1);
191                }
192                for (i, operand) in operands.iter_enumerated() {
193                    let op = self.codegen_operand(bx, operand);
194                    // Do not generate stores and GEPis for zero-sized fields.
195                    if !op.layout.is_zst() {
196                        let field_index = active_field_index.unwrap_or(i);
197                        let field = if let mir::AggregateKind::Array(_) = **kind {
198                            let llindex = bx.cx().const_usize(field_index.as_u32().into());
199                            variant_dest.project_index(bx, llindex)
200                        } else {
201                            variant_dest.project_field(bx, field_index.as_usize())
202                        };
203                        op.store_with_annotation(bx, field);
204                    }
205                }
206                dest.codegen_set_discr(bx, variant_index);
207            }
208
209            _ => {
210                let temp = self.codegen_rvalue_operand(bx, rvalue);
211                temp.store_with_annotation(bx, dest);
212            }
213        }
214    }
215
216    /// Transmutes the `src` value to the destination type by writing it to `dst`.
217    ///
218    /// See also [`Self::codegen_transmute_operand`] for cases that can be done
219    /// without needing a pre-allocated place for the destination.
220    fn codegen_transmute(
221        &mut self,
222        bx: &mut Bx,
223        src: OperandRef<'tcx, Bx::Value>,
224        dst: PlaceRef<'tcx, Bx::Value>,
225    ) {
226        // The MIR validator enforces no unsized transmutes.
227        if !src.layout.is_sized() {
    ::core::panicking::panic("assertion failed: src.layout.is_sized()")
};assert!(src.layout.is_sized());
228        if !dst.layout.is_sized() {
    ::core::panicking::panic("assertion failed: dst.layout.is_sized()")
};assert!(dst.layout.is_sized());
229
230        if src.layout.size != dst.layout.size
231            || src.layout.is_uninhabited()
232            || dst.layout.is_uninhabited()
233        {
234            // These cases are all UB to actually hit, so don't emit code for them.
235            // (The size mismatches are reachable via `transmute_unchecked`.)
236            bx.unreachable_nonterminator();
237        } else {
238            // Since in this path we have a place anyway, we can store or copy to it,
239            // making sure we use the destination place's alignment even if the
240            // source would normally have a higher one.
241            src.store_with_annotation(bx, dst.val.with_type(src.layout));
242        }
243    }
244
245    /// Transmutes an `OperandValue` to another `OperandValue`.
246    ///
247    /// This is supported for all cases where the `cast` type is SSA,
248    /// but for non-ZSTs with [`abi::BackendRepr::Memory`] it ICEs.
249    pub(crate) fn codegen_transmute_operand(
250        &mut self,
251        bx: &mut Bx,
252        operand: OperandRef<'tcx, Bx::Value>,
253        cast: TyAndLayout<'tcx>,
254    ) -> OperandValue<Bx::Value> {
255        if let abi::BackendRepr::Memory { .. } = cast.backend_repr
256            && !cast.is_zst()
257        {
258            ::rustc_middle::util::bug::span_bug_fmt(self.mir.span,
    format_args!("Use `codegen_transmute` to transmute to {0:?}", cast));span_bug!(self.mir.span, "Use `codegen_transmute` to transmute to {cast:?}");
259        }
260
261        // `Layout` is interned, so we can do a cheap check for things that are
262        // exactly the same and thus don't need any handling.
263        if abi::Layout::eq(&operand.layout.layout, &cast.layout) {
264            return operand.val;
265        }
266
267        // Check for transmutes that are always UB.
268        if operand.layout.size != cast.size
269            || operand.layout.is_uninhabited()
270            || cast.is_uninhabited()
271        {
272            bx.unreachable_nonterminator();
273
274            // We still need to return a value of the appropriate type, but
275            // it's already UB so do the easiest thing available.
276            return OperandValue::poison(bx, cast);
277        }
278
279        // To or from pointers takes different methods, so we use this to restrict
280        // the SimdVector case to types which can be `bitcast` between each other.
281        #[inline]
282        fn vector_can_bitcast(x: abi::Scalar) -> bool {
283            #[allow(non_exhaustive_omitted_patterns)] match x {
    abi::Scalar::Initialized {
        value: abi::Primitive::Int(..) | abi::Primitive::Float(..), .. } =>
        true,
    _ => false,
}matches!(
284                x,
285                abi::Scalar::Initialized {
286                    value: abi::Primitive::Int(..) | abi::Primitive::Float(..),
287                    ..
288                }
289            )
290        }
291
292        let cx = bx.cx();
293        match (operand.val, operand.layout.backend_repr, cast.backend_repr) {
294            _ if cast.is_zst() => OperandValue::ZeroSized,
295            (OperandValue::Ref(source_place_val), abi::BackendRepr::Memory { .. }, _) => {
296                match (&source_place_val.llextra, &None) {
    (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!(source_place_val.llextra, None);
297                // The existing alignment is part of `source_place_val`,
298                // so that alignment will be used, not `cast`'s.
299                bx.load_operand(source_place_val.with_type(cast)).val
300            }
301            (
302                OperandValue::Immediate(imm),
303                abi::BackendRepr::Scalar(from_scalar),
304                abi::BackendRepr::Scalar(to_scalar),
305            ) if from_scalar.size(cx) == to_scalar.size(cx) => {
306                OperandValue::Immediate(transmute_scalar(bx, imm, from_scalar, to_scalar))
307            }
308            (
309                OperandValue::Immediate(imm),
310                abi::BackendRepr::SimdVector { element: from_scalar, .. },
311                abi::BackendRepr::SimdVector { element: to_scalar, .. },
312            ) if vector_can_bitcast(from_scalar) && vector_can_bitcast(to_scalar) => {
313                let to_backend_ty = bx.cx().immediate_backend_type(cast);
314                OperandValue::Immediate(bx.bitcast(imm, to_backend_ty))
315            }
316            (
317                OperandValue::Immediate(imm),
318                abi::BackendRepr::SimdScalableVector { element: from_scalar, .. },
319                abi::BackendRepr::SimdScalableVector { element: to_scalar, .. },
320            ) if vector_can_bitcast(from_scalar) && vector_can_bitcast(to_scalar) => {
321                let to_backend_ty = bx.cx().immediate_backend_type(cast);
322                OperandValue::Immediate(bx.bitcast(imm, to_backend_ty))
323            }
324            (
325                OperandValue::Pair(imm_a, imm_b),
326                abi::BackendRepr::ScalarPair(in_a, in_b),
327                abi::BackendRepr::ScalarPair(out_a, out_b),
328            ) if in_a.size(cx) == out_a.size(cx) && in_b.size(cx) == out_b.size(cx) => {
329                OperandValue::Pair(
330                    transmute_scalar(bx, imm_a, in_a, out_a),
331                    transmute_scalar(bx, imm_b, in_b, out_b),
332                )
333            }
334            _ => {
335                // For any other potentially-tricky cases, make a temporary instead.
336                // If anything else wants the target local to be in memory this won't
337                // be hit, as `codegen_transmute` will get called directly. Thus this
338                // is only for places where everything else wants the operand form,
339                // and thus it's not worth making those places get it from memory.
340                //
341                // Notably, Scalar ⇌ ScalarPair cases go here to avoid padding
342                // and endianness issues, as do SimdVector ones to avoid worrying
343                // about things like f32x8 ⇌ ptrx4 that would need multiple steps.
344                let align = Ord::max(operand.layout.align.abi, cast.align.abi);
345                let size = Ord::max(operand.layout.size, cast.size);
346                let temp = PlaceValue::alloca(bx, size, align);
347                bx.lifetime_start(temp.llval, size);
348                operand.store_with_annotation(bx, temp.with_type(operand.layout));
349                let val = bx.load_operand(temp.with_type(cast)).val;
350                bx.lifetime_end(temp.llval, size);
351                val
352            }
353        }
354    }
355
356    /// Cast one of the immediates from an [`OperandValue::Immediate`]
357    /// or an [`OperandValue::Pair`] to an immediate of the target type.
358    ///
359    /// Returns `None` if the cast is not possible.
360    fn cast_immediate(
361        &self,
362        bx: &mut Bx,
363        mut imm: Bx::Value,
364        from_scalar: abi::Scalar,
365        from_backend_ty: Bx::Type,
366        to_scalar: abi::Scalar,
367        to_backend_ty: Bx::Type,
368    ) -> Option<Bx::Value> {
369        use abi::Primitive::*;
370
371        // When scalars are passed by value, there's no metadata recording their
372        // valid ranges. For example, `char`s are passed as just `i32`, with no
373        // way for LLVM to know that they're 0x10FFFF at most. Thus we assume
374        // the range of the input value too, not just the output range.
375        assume_scalar_range(bx, imm, from_scalar, from_backend_ty, None);
376
377        imm = match (from_scalar.primitive(), to_scalar.primitive()) {
378            (Int(_, is_signed), Int(..)) => bx.intcast(imm, to_backend_ty, is_signed),
379            (Float(_), Float(_)) => {
380                let srcsz = bx.cx().float_width(from_backend_ty);
381                let dstsz = bx.cx().float_width(to_backend_ty);
382                if dstsz > srcsz {
383                    bx.fpext(imm, to_backend_ty)
384                } else if srcsz > dstsz {
385                    bx.fptrunc(imm, to_backend_ty)
386                } else {
387                    imm
388                }
389            }
390            (Int(_, is_signed), Float(_)) => {
391                if is_signed {
392                    bx.sitofp(imm, to_backend_ty)
393                } else {
394                    bx.uitofp(imm, to_backend_ty)
395                }
396            }
397            (Pointer(..), Pointer(..)) => bx.pointercast(imm, to_backend_ty),
398            (Int(_, is_signed), Pointer(..)) => {
399                let usize_imm = bx.intcast(imm, bx.cx().type_isize(), is_signed);
400                bx.inttoptr(usize_imm, to_backend_ty)
401            }
402            (Float(_), Int(_, is_signed)) => bx.cast_float_to_int(is_signed, imm, to_backend_ty),
403            _ => return None,
404        };
405        Some(imm)
406    }
407
408    pub(crate) fn codegen_rvalue_operand(
409        &mut self,
410        bx: &mut Bx,
411        rvalue: &mir::Rvalue<'tcx>,
412    ) -> OperandRef<'tcx, Bx::Value> {
413        match *rvalue {
414            mir::Rvalue::Cast(ref kind, ref source, mir_cast_ty) => {
415                let operand = self.codegen_operand(bx, source);
416                {
    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/rvalue.rs:416",
                        "rustc_codegen_ssa::mir::rvalue", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_codegen_ssa/src/mir/rvalue.rs"),
                        ::tracing_core::__macro_support::Option::Some(416u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_codegen_ssa::mir::rvalue"),
                        ::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!("cast operand is {0:?}",
                                                    operand) as &dyn Value))])
            });
    } else { ; }
};debug!("cast operand is {:?}", operand);
417                let cast = bx.cx().layout_of(self.monomorphize(mir_cast_ty));
418
419                let val = match *kind {
420                    mir::CastKind::PointerExposeProvenance => {
421                        if !bx.cx().is_backend_immediate(cast) {
    ::core::panicking::panic("assertion failed: bx.cx().is_backend_immediate(cast)")
};assert!(bx.cx().is_backend_immediate(cast));
422                        let llptr = operand.immediate();
423                        let llcast_ty = bx.cx().immediate_backend_type(cast);
424                        let lladdr = bx.ptrtoint(llptr, llcast_ty);
425                        OperandValue::Immediate(lladdr)
426                    }
427                    mir::CastKind::PointerCoercion(PointerCoercion::ReifyFnPointer(_), _) => {
428                        match *operand.layout.ty.kind() {
429                            ty::FnDef(def_id, args) => {
430                                let instance = ty::Instance::resolve_for_fn_ptr(
431                                    bx.tcx(),
432                                    bx.typing_env(),
433                                    def_id,
434                                    args,
435                                )
436                                .unwrap();
437                                OperandValue::Immediate(bx.get_fn_addr(instance))
438                            }
439                            _ => ::rustc_middle::util::bug::bug_fmt(format_args!("{0} cannot be reified to a fn ptr",
        operand.layout.ty))bug!("{} cannot be reified to a fn ptr", operand.layout.ty),
440                        }
441                    }
442                    mir::CastKind::PointerCoercion(PointerCoercion::ClosureFnPointer(_), _) => {
443                        match *operand.layout.ty.kind() {
444                            ty::Closure(def_id, args) => {
445                                let instance = Instance::resolve_closure(
446                                    bx.cx().tcx(),
447                                    def_id,
448                                    args,
449                                    ty::ClosureKind::FnOnce,
450                                );
451                                OperandValue::Immediate(bx.cx().get_fn_addr(instance))
452                            }
453                            _ => ::rustc_middle::util::bug::bug_fmt(format_args!("{0} cannot be cast to a fn ptr",
        operand.layout.ty))bug!("{} cannot be cast to a fn ptr", operand.layout.ty),
454                        }
455                    }
456                    mir::CastKind::PointerCoercion(PointerCoercion::UnsafeFnPointer, _) => {
457                        // This is a no-op at the LLVM level.
458                        operand.val
459                    }
460                    mir::CastKind::PointerCoercion(PointerCoercion::Unsize, _) => {
461                        if !bx.cx().is_backend_scalar_pair(cast) {
    ::core::panicking::panic("assertion failed: bx.cx().is_backend_scalar_pair(cast)")
};assert!(bx.cx().is_backend_scalar_pair(cast));
462                        let (lldata, llextra) = operand.val.pointer_parts();
463                        let (lldata, llextra) =
464                            base::unsize_ptr(bx, lldata, operand.layout.ty, cast.ty, llextra);
465                        OperandValue::Pair(lldata, llextra)
466                    }
467                    mir::CastKind::PointerCoercion(
468                        PointerCoercion::MutToConstPointer | PointerCoercion::ArrayToPointer,
469                        _,
470                    ) => {
471                        ::rustc_middle::util::bug::bug_fmt(format_args!("{0:?} is for borrowck, and should never appear in codegen",
        kind));bug!("{kind:?} is for borrowck, and should never appear in codegen");
472                    }
473                    mir::CastKind::PtrToPtr if bx.cx().is_backend_scalar_pair(operand.layout) => {
474                        if let OperandValue::Pair(data_ptr, meta) = operand.val {
475                            if bx.cx().is_backend_scalar_pair(cast) {
476                                OperandValue::Pair(data_ptr, meta)
477                            } else {
478                                // Cast of wide-ptr to thin-ptr is an extraction of data-ptr.
479                                OperandValue::Immediate(data_ptr)
480                            }
481                        } else {
482                            ::rustc_middle::util::bug::bug_fmt(format_args!("unexpected non-pair operand"));bug!("unexpected non-pair operand");
483                        }
484                    }
485                    | mir::CastKind::IntToInt
486                    | mir::CastKind::FloatToInt
487                    | mir::CastKind::FloatToFloat
488                    | mir::CastKind::IntToFloat
489                    | mir::CastKind::PtrToPtr
490                    | mir::CastKind::FnPtrToPtr
491                    // Since int2ptr can have arbitrary integer types as input (so we have to do
492                    // sign extension and all that), it is currently best handled in the same code
493                    // path as the other integer-to-X casts.
494                    | mir::CastKind::PointerWithExposedProvenance => {
495                        let imm = operand.immediate();
496                        let abi::BackendRepr::Scalar(from_scalar) = operand.layout.backend_repr
497                        else {
498                            ::rustc_middle::util::bug::bug_fmt(format_args!("Found non-scalar for operand {0:?}",
        operand));bug!("Found non-scalar for operand {operand:?}");
499                        };
500                        let from_backend_ty = bx.cx().immediate_backend_type(operand.layout);
501
502                        if !bx.cx().is_backend_immediate(cast) {
    ::core::panicking::panic("assertion failed: bx.cx().is_backend_immediate(cast)")
};assert!(bx.cx().is_backend_immediate(cast));
503                        let to_backend_ty = bx.cx().immediate_backend_type(cast);
504                        if operand.layout.is_uninhabited() {
505                            let val = OperandValue::Immediate(bx.cx().const_poison(to_backend_ty));
506                            return OperandRef { val, layout: cast, move_annotation: None };
507                        }
508                        let abi::BackendRepr::Scalar(to_scalar) = cast.layout.backend_repr else {
509                            ::rustc_middle::util::bug::bug_fmt(format_args!("Found non-scalar for cast {0:?}",
        cast));bug!("Found non-scalar for cast {cast:?}");
510                        };
511
512                        self.cast_immediate(
513                            bx,
514                            imm,
515                            from_scalar,
516                            from_backend_ty,
517                            to_scalar,
518                            to_backend_ty,
519                        )
520                        .map(OperandValue::Immediate)
521                        .unwrap_or_else(|| {
522                            ::rustc_middle::util::bug::bug_fmt(format_args!("Unsupported cast of {0:?} to {1:?}",
        operand, cast));bug!("Unsupported cast of {operand:?} to {cast:?}");
523                        })
524                    }
525                    mir::CastKind::Transmute | mir::CastKind::Subtype => {
526                        self.codegen_transmute_operand(bx, operand, cast)
527                    }
528                };
529                OperandRef { val, layout: cast, move_annotation: None }
530            }
531
532            mir::Rvalue::Ref(_, bk, place) => {
533                let mk_ref = move |tcx: TyCtxt<'tcx>, ty: Ty<'tcx>| {
534                    Ty::new_ref(tcx, tcx.lifetimes.re_erased, ty, bk.to_mutbl_lossy())
535                };
536                let op = self.codegen_place_to_pointer(bx, place, mk_ref);
537                if self.cx.tcx().sess.opts.unstable_opts.codegen_emit_retag.is_some() {
538                    self.codegen_retag_operand(bx, op, false)
539                } else {
540                    op
541                }
542            }
543
544            // Note: Exclusive reborrowing is always equal to a memcpy, as the types do not change.
545            // Generic shared reborrowing is not (necessarily) a simple memcpy, but currently the
546            // coherence check places such restrictions on the CoerceShared trait as to guarantee
547            // that it is.
548            mir::Rvalue::Reborrow(_, _, place) => {
549                self.codegen_operand(bx, &mir::Operand::Copy(place))
550            }
551
552            mir::Rvalue::RawPtr(kind, place) => {
553                let mk_ptr = move |tcx: TyCtxt<'tcx>, ty: Ty<'tcx>| {
554                    Ty::new_ptr(tcx, ty, kind.to_mutbl_lossy())
555                };
556                self.codegen_place_to_pointer(bx, place, mk_ptr)
557            }
558
559            mir::Rvalue::BinaryOp(op_with_overflow, (ref lhs, ref rhs))
560                if let Some(op) = op_with_overflow.overflowing_to_wrapping() =>
561            {
562                let lhs = self.codegen_operand(bx, lhs);
563                let rhs = self.codegen_operand(bx, rhs);
564                let result = self.codegen_scalar_checked_binop(
565                    bx,
566                    op,
567                    lhs.immediate(),
568                    rhs.immediate(),
569                    lhs.layout.ty,
570                );
571                let val_ty = op.ty(bx.tcx(), lhs.layout.ty, rhs.layout.ty);
572                let operand_ty = Ty::new_tup(bx.tcx(), &[val_ty, bx.tcx().types.bool]);
573                OperandRef {
574                    val: result,
575                    layout: bx.cx().layout_of(operand_ty),
576                    move_annotation: None,
577                }
578            }
579
580            mir::Rvalue::BinaryOp(op, (ref lhs, ref rhs)) => {
581                let lhs = self.codegen_operand(bx, lhs);
582                let rhs = self.codegen_operand(bx, rhs);
583                let llresult = match (lhs.val, rhs.val) {
584                    (
585                        OperandValue::Pair(lhs_addr, lhs_extra),
586                        OperandValue::Pair(rhs_addr, rhs_extra),
587                    ) => self.codegen_wide_ptr_binop(
588                        bx,
589                        op,
590                        lhs_addr,
591                        lhs_extra,
592                        rhs_addr,
593                        rhs_extra,
594                        lhs.layout.ty,
595                    ),
596
597                    (OperandValue::Immediate(lhs_val), OperandValue::Immediate(rhs_val)) => self
598                        .codegen_scalar_binop(
599                            bx,
600                            op,
601                            lhs_val,
602                            rhs_val,
603                            lhs.layout.ty,
604                            rhs.layout.ty,
605                        ),
606
607                    _ => ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"))bug!(),
608                };
609                OperandRef {
610                    val: OperandValue::Immediate(llresult),
611                    layout: bx.cx().layout_of(op.ty(bx.tcx(), lhs.layout.ty, rhs.layout.ty)),
612                    move_annotation: None,
613                }
614            }
615
616            mir::Rvalue::UnaryOp(op, ref operand) => {
617                let operand = self.codegen_operand(bx, operand);
618                let is_float = operand.layout.ty.is_floating_point();
619                let (val, layout) = match op {
620                    mir::UnOp::Not => {
621                        let llval = bx.not(operand.immediate());
622                        (OperandValue::Immediate(llval), operand.layout)
623                    }
624                    mir::UnOp::Neg => {
625                        let llval = if is_float {
626                            bx.fneg(operand.immediate())
627                        } else {
628                            bx.neg(operand.immediate())
629                        };
630                        (OperandValue::Immediate(llval), operand.layout)
631                    }
632                    mir::UnOp::PtrMetadata => {
633                        if !(operand.layout.ty.is_raw_ptr() || operand.layout.ty.is_ref()) {
    ::core::panicking::panic("assertion failed: operand.layout.ty.is_raw_ptr() || operand.layout.ty.is_ref()")
};assert!(operand.layout.ty.is_raw_ptr() || operand.layout.ty.is_ref(),);
634                        let (_, meta) = operand.val.pointer_parts();
635                        match (&(operand.layout.fields.count() > 1), &meta.is_some()) {
    (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!(operand.layout.fields.count() > 1, meta.is_some());
636                        if let Some(meta) = meta {
637                            (OperandValue::Immediate(meta), operand.layout.field(self.cx, 1))
638                        } else {
639                            (OperandValue::ZeroSized, bx.cx().layout_of(bx.tcx().types.unit))
640                        }
641                    }
642                };
643                if !val.is_expected_variant_for_type(self.cx, layout) {
    {
        ::core::panicking::panic_fmt(format_args!("Made wrong variant {0:?} for type {1:?}",
                val, layout));
    }
};assert!(
644                    val.is_expected_variant_for_type(self.cx, layout),
645                    "Made wrong variant {val:?} for type {layout:?}",
646                );
647                OperandRef { val, layout, move_annotation: None }
648            }
649
650            mir::Rvalue::Discriminant(ref place) => {
651                let discr_ty = rvalue.ty(self.mir, bx.tcx());
652                let discr_ty = self.monomorphize(discr_ty);
653                let operand = self.codegen_consume(bx, place.as_ref());
654                let discr = operand.codegen_get_discr(self, bx, discr_ty);
655                OperandRef {
656                    val: OperandValue::Immediate(discr),
657                    layout: self.cx.layout_of(discr_ty),
658                    move_annotation: None,
659                }
660            }
661
662            mir::Rvalue::ThreadLocalRef(def_id) => {
663                if !bx.cx().tcx().is_static(def_id) {
    ::core::panicking::panic("assertion failed: bx.cx().tcx().is_static(def_id)")
};assert!(bx.cx().tcx().is_static(def_id));
664                let layout = bx.layout_of(bx.cx().tcx().static_ptr_ty(def_id, bx.typing_env()));
665                let static_ = if !def_id.is_local() && bx.cx().tcx().needs_thread_local_shim(def_id)
666                {
667                    let instance = ty::Instance {
668                        def: ty::InstanceKind::ThreadLocalShim(def_id),
669                        args: ty::GenericArgs::empty(),
670                    };
671                    let fn_ptr = bx.get_fn_addr(instance);
672                    let fn_abi = bx.fn_abi_of_instance(instance, ty::List::empty());
673                    let fn_ty = bx.fn_decl_backend_type(fn_abi);
674                    let fn_attrs = if bx.tcx().def_kind(instance.def_id()).has_codegen_attrs() {
675                        Some(bx.tcx().codegen_instance_attrs(instance.def))
676                    } else {
677                        None
678                    };
679                    bx.call(
680                        fn_ty,
681                        fn_attrs.as_deref(),
682                        Some(fn_abi),
683                        fn_ptr,
684                        &[],
685                        None,
686                        Some(instance),
687                    )
688                } else {
689                    bx.get_static(def_id)
690                };
691                OperandRef { val: OperandValue::Immediate(static_), layout, move_annotation: None }
692            }
693
694            mir::Rvalue::Use(ref operand, _) => self.codegen_operand(bx, operand),
695
696            mir::Rvalue::Repeat(ref elem, len_const) => {
697                // All arrays have `BackendRepr::Memory`, so only the ZST cases
698                // end up here. Anything else forces the destination local to be
699                // `Memory`, and thus ends up handled in `codegen_rvalue` instead.
700                let operand = self.codegen_operand(bx, elem);
701                let array_ty = Ty::new_array_with_const_len(bx.tcx(), operand.layout.ty, len_const);
702                let array_ty = self.monomorphize(array_ty);
703                let array_layout = bx.layout_of(array_ty);
704                if !array_layout.is_zst() {
    ::core::panicking::panic("assertion failed: array_layout.is_zst()")
};assert!(array_layout.is_zst());
705                OperandRef {
706                    val: OperandValue::ZeroSized,
707                    layout: array_layout,
708                    move_annotation: None,
709                }
710            }
711
712            mir::Rvalue::Aggregate(ref kind, ref fields) => {
713                let (variant_index, active_field_index) = match **kind {
714                    mir::AggregateKind::Adt(_, variant_index, _, _, active_field_index) => {
715                        (variant_index, active_field_index)
716                    }
717                    _ => (FIRST_VARIANT, None),
718                };
719
720                let ty = rvalue.ty(self.mir, self.cx.tcx());
721                let ty = self.monomorphize(ty);
722                let layout = self.cx.layout_of(ty);
723
724                let mut builder = OperandRefBuilder::new(layout);
725                for (field_idx, field) in fields.iter_enumerated() {
726                    let op = self.codegen_operand(bx, field);
727                    let fi = active_field_index.unwrap_or(field_idx);
728                    builder.insert_field(bx, variant_index, fi, op);
729                }
730
731                let tag_result = codegen_tag_value(self.cx, variant_index, layout);
732                match tag_result {
733                    Err(super::place::UninhabitedVariantError) => {
734                        // Like codegen_set_discr we use a sound abort, but could
735                        // potentially `unreachable` or just return the poison for
736                        // more optimizability, if that turns out to be helpful.
737                        bx.abort();
738                        let val = OperandValue::poison(bx, layout);
739                        OperandRef { val, layout, move_annotation: None }
740                    }
741                    Ok(maybe_tag_value) => {
742                        if let Some((tag_field, tag_imm)) = maybe_tag_value {
743                            builder.insert_imm(tag_field, tag_imm);
744                        }
745                        builder.build(bx.cx())
746                    }
747                }
748            }
749
750            mir::Rvalue::WrapUnsafeBinder(ref operand, binder_ty) => {
751                let operand = self.codegen_operand(bx, operand);
752                let binder_ty = self.monomorphize(binder_ty);
753                let layout = bx.cx().layout_of(binder_ty);
754                OperandRef { val: operand.val, layout, move_annotation: None }
755            }
756
757            mir::Rvalue::CopyForDeref(_) => ::rustc_middle::util::bug::bug_fmt(format_args!("`CopyForDeref` in codegen"))bug!("`CopyForDeref` in codegen"),
758        }
759    }
760
761    /// Codegen an `Rvalue::RawPtr` or `Rvalue::Ref`
762    fn codegen_place_to_pointer(
763        &mut self,
764        bx: &mut Bx,
765        place: mir::Place<'tcx>,
766        mk_ptr_ty: impl FnOnce(TyCtxt<'tcx>, Ty<'tcx>) -> Ty<'tcx>,
767    ) -> OperandRef<'tcx, Bx::Value> {
768        let cg_place = self.codegen_place(bx, place.as_ref());
769        let val = cg_place.val.address();
770
771        let ty = cg_place.layout.ty;
772        if !if bx.cx().tcx().type_has_metadata(ty, bx.cx().typing_env()) {

            #[allow(non_exhaustive_omitted_patterns)]
            match val { OperandValue::Pair(..) => true, _ => false, }
        } else {

            #[allow(non_exhaustive_omitted_patterns)]
            match val { OperandValue::Immediate(..) => true, _ => false, }
        } {
    {
        ::core::panicking::panic_fmt(format_args!("Address of place was unexpectedly {0:?} for pointee type {1:?}",
                val, ty));
    }
};assert!(
773            if bx.cx().tcx().type_has_metadata(ty, bx.cx().typing_env()) {
774                matches!(val, OperandValue::Pair(..))
775            } else {
776                matches!(val, OperandValue::Immediate(..))
777            },
778            "Address of place was unexpectedly {val:?} for pointee type {ty:?}",
779        );
780
781        OperandRef {
782            val,
783            layout: self.cx.layout_of(mk_ptr_ty(self.cx.tcx(), ty)),
784            move_annotation: None,
785        }
786    }
787
788    fn codegen_scalar_binop(
789        &mut self,
790        bx: &mut Bx,
791        op: mir::BinOp,
792        lhs: Bx::Value,
793        rhs: Bx::Value,
794        lhs_ty: Ty<'tcx>,
795        rhs_ty: Ty<'tcx>,
796    ) -> Bx::Value {
797        let is_float = lhs_ty.is_floating_point();
798        let is_signed = lhs_ty.is_signed();
799        match op {
800            mir::BinOp::Add => {
801                if is_float {
802                    bx.fadd(lhs, rhs)
803                } else {
804                    bx.add(lhs, rhs)
805                }
806            }
807            mir::BinOp::AddUnchecked => {
808                if is_signed {
809                    bx.unchecked_sadd(lhs, rhs)
810                } else {
811                    bx.unchecked_uadd(lhs, rhs)
812                }
813            }
814            mir::BinOp::Sub => {
815                if is_float {
816                    bx.fsub(lhs, rhs)
817                } else {
818                    bx.sub(lhs, rhs)
819                }
820            }
821            mir::BinOp::SubUnchecked => {
822                if is_signed {
823                    bx.unchecked_ssub(lhs, rhs)
824                } else {
825                    bx.unchecked_usub(lhs, rhs)
826                }
827            }
828            mir::BinOp::Mul => {
829                if is_float {
830                    bx.fmul(lhs, rhs)
831                } else {
832                    bx.mul(lhs, rhs)
833                }
834            }
835            mir::BinOp::MulUnchecked => {
836                if is_signed {
837                    bx.unchecked_smul(lhs, rhs)
838                } else {
839                    bx.unchecked_umul(lhs, rhs)
840                }
841            }
842            mir::BinOp::Div => {
843                if is_float {
844                    bx.fdiv(lhs, rhs)
845                } else if is_signed {
846                    bx.sdiv(lhs, rhs)
847                } else {
848                    bx.udiv(lhs, rhs)
849                }
850            }
851            mir::BinOp::Rem => {
852                if is_float {
853                    bx.frem(lhs, rhs)
854                } else if is_signed {
855                    bx.srem(lhs, rhs)
856                } else {
857                    bx.urem(lhs, rhs)
858                }
859            }
860            mir::BinOp::BitOr => bx.or(lhs, rhs),
861            mir::BinOp::BitAnd => bx.and(lhs, rhs),
862            mir::BinOp::BitXor => bx.xor(lhs, rhs),
863            mir::BinOp::Offset => {
864                let pointee_type = lhs_ty
865                    .builtin_deref(true)
866                    .unwrap_or_else(|| ::rustc_middle::util::bug::bug_fmt(format_args!("deref of non-pointer {0:?}",
        lhs_ty))bug!("deref of non-pointer {:?}", lhs_ty));
867                let pointee_layout = bx.cx().layout_of(pointee_type);
868                if pointee_layout.is_zst() {
869                    // `Offset` works in terms of the size of pointee,
870                    // so offsetting a pointer to ZST is a noop.
871                    lhs
872                } else {
873                    let llty = bx.cx().backend_type(pointee_layout);
874                    if !rhs_ty.is_signed() {
875                        bx.inbounds_nuw_gep(llty, lhs, &[rhs])
876                    } else {
877                        bx.inbounds_gep(llty, lhs, &[rhs])
878                    }
879                }
880            }
881            mir::BinOp::Shl | mir::BinOp::ShlUnchecked => {
882                let rhs = base::build_shift_expr_rhs(bx, lhs, rhs, op == mir::BinOp::ShlUnchecked);
883                bx.shl(lhs, rhs)
884            }
885            mir::BinOp::Shr | mir::BinOp::ShrUnchecked => {
886                let rhs = base::build_shift_expr_rhs(bx, lhs, rhs, op == mir::BinOp::ShrUnchecked);
887                if is_signed { bx.ashr(lhs, rhs) } else { bx.lshr(lhs, rhs) }
888            }
889            mir::BinOp::Ne
890            | mir::BinOp::Lt
891            | mir::BinOp::Gt
892            | mir::BinOp::Eq
893            | mir::BinOp::Le
894            | mir::BinOp::Ge => {
895                if is_float {
896                    bx.fcmp(base::bin_op_to_fcmp_predicate(op), lhs, rhs)
897                } else {
898                    bx.icmp(base::bin_op_to_icmp_predicate(op, is_signed), lhs, rhs)
899                }
900            }
901            mir::BinOp::Cmp => {
902                if !!is_float { ::core::panicking::panic("assertion failed: !is_float") };assert!(!is_float);
903                bx.three_way_compare(lhs_ty, lhs, rhs)
904            }
905            mir::BinOp::AddWithOverflow
906            | mir::BinOp::SubWithOverflow
907            | mir::BinOp::MulWithOverflow => {
908                ::rustc_middle::util::bug::bug_fmt(format_args!("{0:?} needs to return a pair, so call codegen_scalar_checked_binop instead",
        op))bug!("{op:?} needs to return a pair, so call codegen_scalar_checked_binop instead")
909            }
910        }
911    }
912
913    fn codegen_wide_ptr_binop(
914        &mut self,
915        bx: &mut Bx,
916        op: mir::BinOp,
917        lhs_addr: Bx::Value,
918        lhs_extra: Bx::Value,
919        rhs_addr: Bx::Value,
920        rhs_extra: Bx::Value,
921        _input_ty: Ty<'tcx>,
922    ) -> Bx::Value {
923        match op {
924            mir::BinOp::Eq => {
925                let lhs = bx.icmp(IntPredicate::IntEQ, lhs_addr, rhs_addr);
926                let rhs = bx.icmp(IntPredicate::IntEQ, lhs_extra, rhs_extra);
927                bx.and(lhs, rhs)
928            }
929            mir::BinOp::Ne => {
930                let lhs = bx.icmp(IntPredicate::IntNE, lhs_addr, rhs_addr);
931                let rhs = bx.icmp(IntPredicate::IntNE, lhs_extra, rhs_extra);
932                bx.or(lhs, rhs)
933            }
934            mir::BinOp::Le | mir::BinOp::Lt | mir::BinOp::Ge | mir::BinOp::Gt => {
935                // a OP b ~ a.0 STRICT(OP) b.0 | (a.0 == b.0 && a.1 OP a.1)
936                let (op, strict_op) = match op {
937                    mir::BinOp::Lt => (IntPredicate::IntULT, IntPredicate::IntULT),
938                    mir::BinOp::Le => (IntPredicate::IntULE, IntPredicate::IntULT),
939                    mir::BinOp::Gt => (IntPredicate::IntUGT, IntPredicate::IntUGT),
940                    mir::BinOp::Ge => (IntPredicate::IntUGE, IntPredicate::IntUGT),
941                    _ => ::rustc_middle::util::bug::bug_fmt(format_args!("impossible case reached"))bug!(),
942                };
943                let lhs = bx.icmp(strict_op, lhs_addr, rhs_addr);
944                let and_lhs = bx.icmp(IntPredicate::IntEQ, lhs_addr, rhs_addr);
945                let and_rhs = bx.icmp(op, lhs_extra, rhs_extra);
946                let rhs = bx.and(and_lhs, and_rhs);
947                bx.or(lhs, rhs)
948            }
949            _ => {
950                ::rustc_middle::util::bug::bug_fmt(format_args!("unexpected wide ptr binop"));bug!("unexpected wide ptr binop");
951            }
952        }
953    }
954
955    fn codegen_scalar_checked_binop(
956        &mut self,
957        bx: &mut Bx,
958        op: mir::BinOp,
959        lhs: Bx::Value,
960        rhs: Bx::Value,
961        input_ty: Ty<'tcx>,
962    ) -> OperandValue<Bx::Value> {
963        let (val, of) = match op {
964            // These are checked using intrinsics
965            mir::BinOp::Add | mir::BinOp::Sub | mir::BinOp::Mul => {
966                let oop = match op {
967                    mir::BinOp::Add => OverflowOp::Add,
968                    mir::BinOp::Sub => OverflowOp::Sub,
969                    mir::BinOp::Mul => OverflowOp::Mul,
970                    _ => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
971                };
972                bx.checked_binop(oop, input_ty, lhs, rhs)
973            }
974            _ => ::rustc_middle::util::bug::bug_fmt(format_args!("Operator `{0:?}` is not a checkable operator",
        op))bug!("Operator `{:?}` is not a checkable operator", op),
975        };
976
977        OperandValue::Pair(val, of)
978    }
979}
980
981/// Transmutes a single scalar value `imm` from `from_scalar` to `to_scalar`.
982///
983/// This is expected to be in *immediate* form, as seen in [`OperandValue::Immediate`]
984/// or [`OperandValue::Pair`] (so `i1` for bools, not `i8`, for example).
985///
986/// ICEs if the passed-in `imm` is not a value of the expected type for
987/// `from_scalar`, such as if it's a vector or a pair.
988pub(super) fn transmute_scalar<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
989    bx: &mut Bx,
990    mut imm: Bx::Value,
991    from_scalar: abi::Scalar,
992    to_scalar: abi::Scalar,
993) -> Bx::Value {
994    match (&from_scalar.size(bx.cx()), &to_scalar.size(bx.cx())) {
    (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!(from_scalar.size(bx.cx()), to_scalar.size(bx.cx()));
995    let imm_ty = bx.cx().val_ty(imm);
996    match (&(bx.cx().type_kind(imm_ty)), &(TypeKind::Vector)) {
    (left_val, right_val) => {
        if *left_val == *right_val {
            let kind = ::core::panicking::AssertKind::Ne;
            ::core::panicking::assert_failed(kind, &*left_val, &*right_val,
                ::core::option::Option::Some(format_args!("Vector type {0:?} not allowed in transmute_scalar {1:?} -> {2:?}",
                        imm_ty, from_scalar, to_scalar)));
        }
    }
};assert_ne!(
997        bx.cx().type_kind(imm_ty),
998        TypeKind::Vector,
999        "Vector type {imm_ty:?} not allowed in transmute_scalar {from_scalar:?} -> {to_scalar:?}"
1000    );
1001
1002    // While optimizations will remove no-op transmutes, they might still be
1003    // there in debug or things that aren't no-op in MIR because they change
1004    // the Rust type but not the underlying layout/niche.
1005    if from_scalar == to_scalar {
1006        return imm;
1007    }
1008
1009    use abi::Primitive::*;
1010    imm = bx.from_immediate(imm);
1011
1012    let from_backend_ty = bx.cx().type_from_scalar(from_scalar);
1013    if true {
    match (&bx.cx().val_ty(imm), &from_backend_ty) {
        (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);
            }
        }
    };
};debug_assert_eq!(bx.cx().val_ty(imm), from_backend_ty);
1014    let to_backend_ty = bx.cx().type_from_scalar(to_scalar);
1015
1016    // If we have a scalar, we must already know its range. Either
1017    //
1018    // 1) It's a parameter with `range` parameter metadata,
1019    // 2) It's something we `load`ed with `!range` metadata, or
1020    // 3) After a transmute we `assume`d the range (see below).
1021    //
1022    // That said, last time we tried removing this, it didn't actually help
1023    // the rustc-perf results, so might as well keep doing it
1024    // <https://github.com/rust-lang/rust/pull/135610#issuecomment-2599275182>
1025    assume_scalar_range(bx, imm, from_scalar, from_backend_ty, Some(&to_scalar));
1026
1027    imm = match (from_scalar.primitive(), to_scalar.primitive()) {
1028        (Int(..) | Float(_), Int(..) | Float(_)) => bx.bitcast(imm, to_backend_ty),
1029        (Pointer(..), Pointer(..)) => bx.pointercast(imm, to_backend_ty),
1030        (Int(..), Pointer(..)) => bx.inttoptr(imm, to_backend_ty),
1031        (Pointer(..), Int(..)) => {
1032            // FIXME: this exposes the provenance, which shouldn't be necessary.
1033            bx.ptrtoint(imm, to_backend_ty)
1034        }
1035        (Float(_), Pointer(..)) => {
1036            let int_imm = bx.bitcast(imm, bx.cx().type_isize());
1037            bx.inttoptr(int_imm, to_backend_ty)
1038        }
1039        (Pointer(..), Float(_)) => {
1040            // FIXME: this exposes the provenance, which shouldn't be necessary.
1041            let int_imm = bx.ptrtoint(imm, bx.cx().type_isize());
1042            bx.bitcast(int_imm, to_backend_ty)
1043        }
1044    };
1045
1046    if true {
    match (&bx.cx().val_ty(imm), &to_backend_ty) {
        (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);
            }
        }
    };
};debug_assert_eq!(bx.cx().val_ty(imm), to_backend_ty);
1047
1048    // This `assume` remains important for cases like (a conceptual)
1049    //    transmute::<u32, NonZeroU32>(x) == 0
1050    // since it's never passed to something with parameter metadata (especially
1051    // after MIR inlining) so the only way to tell the backend about the
1052    // constraint that the `transmute` introduced is to `assume` it.
1053    assume_scalar_range(bx, imm, to_scalar, to_backend_ty, Some(&from_scalar));
1054
1055    imm = bx.to_immediate_scalar(imm, to_scalar);
1056    imm
1057}
1058
1059/// Emits an `assume` call that `imm`'s value is within the known range of `scalar`.
1060///
1061/// If `known` is `Some`, only emits the assume if it's more specific than
1062/// whatever is already known from the range of *that* scalar.
1063fn assume_scalar_range<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
1064    bx: &mut Bx,
1065    imm: Bx::Value,
1066    scalar: abi::Scalar,
1067    backend_ty: Bx::Type,
1068    known: Option<&abi::Scalar>,
1069) {
1070    if #[allow(non_exhaustive_omitted_patterns)] match bx.cx().sess().opts.optimize {
    OptLevel::No => true,
    _ => false,
}matches!(bx.cx().sess().opts.optimize, OptLevel::No) {
1071        return;
1072    }
1073
1074    match (scalar, known) {
1075        (abi::Scalar::Union { .. }, _) => return,
1076        (_, None) => {
1077            if scalar.is_always_valid(bx.cx()) {
1078                return;
1079            }
1080        }
1081        (abi::Scalar::Initialized { valid_range, .. }, Some(known)) => {
1082            let known_range = known.valid_range(bx.cx());
1083            if valid_range.contains_range(known_range, scalar.size(bx.cx())) {
1084                return;
1085            }
1086        }
1087    }
1088
1089    match scalar.primitive() {
1090        abi::Primitive::Int(..) => {
1091            let range = scalar.valid_range(bx.cx());
1092            bx.assume_integer_range(imm, backend_ty, range);
1093        }
1094        abi::Primitive::Pointer(abi::AddressSpace::ZERO)
1095            if !scalar.valid_range(bx.cx()).contains(0) =>
1096        {
1097            bx.assume_nonnull(imm);
1098        }
1099        abi::Primitive::Pointer(..) | abi::Primitive::Float(..) => {}
1100    }
1101}